2023-01-06 01:39:25 +01:00
|
|
|
/* Copyright (C) 2023 Wildfire Games.
|
2023-07-27 22:54:46 +02:00
|
|
|
* This file is part of 0 A.D.
|
2021-04-15 20:07:01 +02:00
|
|
|
*
|
2023-07-27 22:54:46 +02:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2021-04-15 20:07:01 +02:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2023-07-27 22:54:46 +02:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2021-04-15 20:07:01 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-07-27 22:54:46 +02:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2021-04-15 20:07:01 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "renderer/DebugRenderer.h"
|
|
|
|
|
|
|
|
#include "graphics/Camera.h"
|
2021-04-16 00:51:25 +02:00
|
|
|
#include "graphics/Color.h"
|
|
|
|
#include "graphics/ShaderManager.h"
|
2021-04-15 20:07:01 +02:00
|
|
|
#include "graphics/ShaderProgram.h"
|
2022-12-31 19:29:44 +01:00
|
|
|
#include "lib/hash.h"
|
2021-04-15 20:07:01 +02:00
|
|
|
#include "maths/BoundingBoxAligned.h"
|
|
|
|
#include "maths/Brush.h"
|
2021-04-27 21:23:37 +02:00
|
|
|
#include "maths/Matrix3D.h"
|
2021-04-16 00:51:25 +02:00
|
|
|
#include "maths/Vector3D.h"
|
2021-05-18 13:09:54 +02:00
|
|
|
#include "ps/CStrInternStatic.h"
|
2022-05-09 00:02:46 +02:00
|
|
|
#include "renderer/backend/IDeviceCommandContext.h"
|
2021-04-16 00:51:25 +02:00
|
|
|
#include "renderer/Renderer.h"
|
2022-01-04 14:29:01 +01:00
|
|
|
#include "renderer/SceneRenderer.h"
|
2021-04-16 00:51:25 +02:00
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
2023-01-06 01:39:25 +01:00
|
|
|
void CDebugRenderer::Initialize()
|
|
|
|
{
|
|
|
|
const std::array<Renderer::Backend::SVertexAttributeFormat, 1> attributes{{
|
|
|
|
{Renderer::Backend::VertexAttributeStream::POSITION,
|
|
|
|
Renderer::Backend::Format::R32G32B32_SFLOAT, 0, sizeof(float) * 3,
|
|
|
|
Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0}
|
|
|
|
}};
|
|
|
|
m_VertexInputLayout = g_Renderer.GetVertexInputLayout(attributes);
|
|
|
|
}
|
|
|
|
|
2022-02-01 17:38:55 +01:00
|
|
|
void CDebugRenderer::DrawLine(
|
|
|
|
const CVector3D& from, const CVector3D& to, const CColor& color,
|
|
|
|
const float width, const bool depthTestEnabled)
|
2021-04-16 00:51:25 +02:00
|
|
|
{
|
|
|
|
if (from == to)
|
|
|
|
return;
|
|
|
|
|
2022-02-01 17:38:55 +01:00
|
|
|
DrawLine({from, to}, color, width, depthTestEnabled);
|
2021-04-25 22:48:44 +02:00
|
|
|
}
|
|
|
|
|
2022-02-01 17:38:55 +01:00
|
|
|
void CDebugRenderer::DrawLine(
|
|
|
|
const std::vector<CVector3D>& line, const CColor& color,
|
|
|
|
const float width, const bool depthTestEnabled)
|
2021-04-25 22:48:44 +02:00
|
|
|
{
|
2022-07-25 22:08:03 +02:00
|
|
|
if (line.size() <= 1)
|
|
|
|
return;
|
|
|
|
|
2022-05-09 00:02:46 +02:00
|
|
|
Renderer::Backend::IDeviceCommandContext* deviceCommandContext =
|
2022-03-03 08:09:59 +01:00
|
|
|
g_Renderer.GetDeviceCommandContext();
|
2022-12-31 19:29:44 +01:00
|
|
|
|
|
|
|
CShaderTechniquePtr debugLineTech =
|
|
|
|
GetShaderTechnique(str_debug_line, color, depthTestEnabled);
|
|
|
|
deviceCommandContext->SetGraphicsPipelineState(
|
|
|
|
debugLineTech->GetGraphicsPipelineState());
|
2022-03-14 23:16:14 +01:00
|
|
|
deviceCommandContext->BeginPass();
|
2021-04-16 00:51:25 +02:00
|
|
|
|
2022-01-04 14:29:01 +01:00
|
|
|
const CCamera& viewCamera = g_Renderer.GetSceneRenderer().GetViewCamera();
|
|
|
|
|
2022-05-02 22:57:22 +02:00
|
|
|
Renderer::Backend::IShaderProgram* debugLineShader = debugLineTech->GetShader();
|
|
|
|
const CMatrix3D transform = viewCamera.GetViewProjection();
|
|
|
|
deviceCommandContext->SetUniform(
|
|
|
|
debugLineShader->GetBindingSlot(str_transform), transform.AsFloatArray());
|
|
|
|
deviceCommandContext->SetUniform(
|
|
|
|
debugLineShader->GetBindingSlot(str_color), color.AsFloatArray());
|
2021-04-16 00:51:25 +02:00
|
|
|
|
2022-01-04 14:29:01 +01:00
|
|
|
const CVector3D cameraIn = viewCamera.GetOrientation().GetIn();
|
2021-04-16 00:51:25 +02:00
|
|
|
|
|
|
|
std::vector<float> vertices;
|
2021-04-25 23:55:19 +02:00
|
|
|
vertices.reserve(line.size() * 6 * 3);
|
2021-04-25 22:48:44 +02:00
|
|
|
#define ADD(position) \
|
|
|
|
vertices.emplace_back((position).X); \
|
|
|
|
vertices.emplace_back((position).Y); \
|
|
|
|
vertices.emplace_back((position).Z);
|
|
|
|
|
|
|
|
for (size_t idx = 1; idx < line.size(); ++idx)
|
2021-04-16 00:51:25 +02:00
|
|
|
{
|
2021-04-25 22:48:44 +02:00
|
|
|
const CVector3D from = line[idx - 1];
|
|
|
|
const CVector3D to = line[idx];
|
|
|
|
const CVector3D direction = (to - from).Normalized();
|
|
|
|
const CVector3D view = direction.Dot(cameraIn) > 0.9f ?
|
|
|
|
CVector3D(0.0f, 1.0f, 0.0f) :
|
|
|
|
cameraIn;
|
|
|
|
const CVector3D offset = view.Cross(direction).Normalized() * width;
|
|
|
|
|
|
|
|
ADD(from + offset)
|
|
|
|
ADD(to - offset)
|
|
|
|
ADD(to + offset)
|
|
|
|
ADD(from + offset)
|
|
|
|
ADD(from - offset)
|
|
|
|
ADD(to - offset)
|
2021-04-16 00:51:25 +02:00
|
|
|
}
|
2021-04-25 22:48:44 +02:00
|
|
|
|
|
|
|
#undef ADD
|
|
|
|
|
2023-01-06 01:39:25 +01:00
|
|
|
deviceCommandContext->SetVertexInputLayout(m_VertexInputLayout);
|
2022-05-24 09:11:23 +02:00
|
|
|
deviceCommandContext->SetVertexBufferData(
|
|
|
|
0, vertices.data(), vertices.size() * sizeof(vertices[0]));
|
2022-04-23 22:11:14 +02:00
|
|
|
|
2022-03-03 08:09:59 +01:00
|
|
|
deviceCommandContext->Draw(0, vertices.size() / 3);
|
2021-04-16 00:51:25 +02:00
|
|
|
|
2022-03-14 23:16:14 +01:00
|
|
|
deviceCommandContext->EndPass();
|
2021-04-16 00:51:25 +02:00
|
|
|
}
|
2021-04-15 20:07:01 +02:00
|
|
|
|
2021-04-25 23:55:19 +02:00
|
|
|
void CDebugRenderer::DrawCircle(const CVector3D& origin, const float radius, const CColor& color)
|
|
|
|
{
|
|
|
|
CShaderTechniquePtr debugCircleTech =
|
2022-12-31 19:29:44 +01:00
|
|
|
GetShaderTechnique(str_debug_line, color);
|
|
|
|
|
2022-05-09 00:02:46 +02:00
|
|
|
Renderer::Backend::IDeviceCommandContext* deviceCommandContext =
|
2022-03-03 08:09:59 +01:00
|
|
|
g_Renderer.GetDeviceCommandContext();
|
2022-12-31 19:29:44 +01:00
|
|
|
|
|
|
|
deviceCommandContext->SetGraphicsPipelineState(
|
|
|
|
debugCircleTech->GetGraphicsPipelineState());
|
2022-03-14 23:16:14 +01:00
|
|
|
deviceCommandContext->BeginPass();
|
2021-04-25 23:55:19 +02:00
|
|
|
|
2022-01-04 14:29:01 +01:00
|
|
|
const CCamera& camera = g_Renderer.GetSceneRenderer().GetViewCamera();
|
2021-04-25 23:55:19 +02:00
|
|
|
|
2022-05-02 22:57:22 +02:00
|
|
|
Renderer::Backend::IShaderProgram* debugCircleShader = debugCircleTech->GetShader();
|
|
|
|
|
|
|
|
const CMatrix3D transform = camera.GetViewProjection();
|
|
|
|
deviceCommandContext->SetUniform(
|
|
|
|
debugCircleShader->GetBindingSlot(str_transform), transform.AsFloatArray());
|
|
|
|
deviceCommandContext->SetUniform(
|
|
|
|
debugCircleShader->GetBindingSlot(str_color), color.AsFloatArray());
|
2021-04-25 23:55:19 +02:00
|
|
|
|
|
|
|
const CVector3D cameraUp = camera.GetOrientation().GetUp();
|
|
|
|
const CVector3D cameraLeft = camera.GetOrientation().GetLeft();
|
|
|
|
|
|
|
|
std::vector<float> vertices;
|
|
|
|
#define ADD(position) \
|
|
|
|
vertices.emplace_back((position).X); \
|
|
|
|
vertices.emplace_back((position).Y); \
|
|
|
|
vertices.emplace_back((position).Z);
|
|
|
|
|
|
|
|
constexpr size_t segments = 16;
|
|
|
|
for (size_t idx = 0; idx <= segments; ++idx)
|
|
|
|
{
|
|
|
|
const float angle = M_PI * 2.0f * idx / segments;
|
|
|
|
const CVector3D offset = cameraUp * sin(angle) - cameraLeft * cos(angle);
|
2022-02-26 01:37:27 +01:00
|
|
|
const float nextAngle = M_PI * 2.0f * (idx + 1) / segments;
|
|
|
|
const CVector3D nextOffset = cameraUp * sin(nextAngle) - cameraLeft * cos(nextAngle);
|
|
|
|
ADD(origin)
|
2021-04-25 23:55:19 +02:00
|
|
|
ADD(origin + offset * radius)
|
2022-02-26 01:37:27 +01:00
|
|
|
ADD(origin + nextOffset * radius)
|
2021-04-25 23:55:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef ADD
|
|
|
|
|
2023-01-06 01:39:25 +01:00
|
|
|
deviceCommandContext->SetVertexInputLayout(m_VertexInputLayout);
|
2022-05-24 09:11:23 +02:00
|
|
|
deviceCommandContext->SetVertexBufferData(
|
|
|
|
0, vertices.data(), vertices.size() * sizeof(vertices[0]));
|
2022-04-23 22:11:14 +02:00
|
|
|
|
2022-03-03 08:09:59 +01:00
|
|
|
deviceCommandContext->Draw(0, vertices.size() / 3);
|
2021-04-25 23:55:19 +02:00
|
|
|
|
2022-03-14 23:16:14 +01:00
|
|
|
deviceCommandContext->EndPass();
|
2021-04-25 23:55:19 +02:00
|
|
|
}
|
|
|
|
|
2022-02-25 23:05:06 +01:00
|
|
|
void CDebugRenderer::DrawCameraFrustum(const CCamera& camera, const CColor& color, int intermediates, bool wireframe)
|
2021-04-15 20:07:01 +02:00
|
|
|
{
|
|
|
|
CCamera::Quad nearPoints;
|
|
|
|
CCamera::Quad farPoints;
|
|
|
|
|
|
|
|
camera.GetViewQuad(camera.GetNearPlane(), nearPoints);
|
|
|
|
camera.GetViewQuad(camera.GetFarPlane(), farPoints);
|
2022-02-22 19:41:31 +01:00
|
|
|
for (int i = 0; i < 4; ++i)
|
2021-04-15 20:07:01 +02:00
|
|
|
{
|
|
|
|
nearPoints[i] = camera.m_Orientation.Transform(nearPoints[i]);
|
|
|
|
farPoints[i] = camera.m_Orientation.Transform(farPoints[i]);
|
|
|
|
}
|
|
|
|
|
2021-04-26 23:11:35 +02:00
|
|
|
CShaderTechniquePtr overlayTech =
|
2022-12-31 19:29:44 +01:00
|
|
|
GetShaderTechnique(str_debug_line, color, true, wireframe);
|
|
|
|
|
2022-05-09 00:02:46 +02:00
|
|
|
Renderer::Backend::IDeviceCommandContext* deviceCommandContext =
|
2022-03-03 08:09:59 +01:00
|
|
|
g_Renderer.GetDeviceCommandContext();
|
2022-12-31 19:29:44 +01:00
|
|
|
deviceCommandContext->SetGraphicsPipelineState(
|
|
|
|
overlayTech->GetGraphicsPipelineState());
|
2022-03-14 23:16:14 +01:00
|
|
|
deviceCommandContext->BeginPass();
|
2021-04-26 23:11:35 +02:00
|
|
|
|
2022-05-02 22:57:22 +02:00
|
|
|
Renderer::Backend::IShaderProgram* overlayShader = overlayTech->GetShader();
|
|
|
|
|
|
|
|
const CMatrix3D transform = g_Renderer.GetSceneRenderer().GetViewCamera().GetViewProjection();
|
|
|
|
deviceCommandContext->SetUniform(
|
|
|
|
overlayShader->GetBindingSlot(str_transform), transform.AsFloatArray());
|
|
|
|
deviceCommandContext->SetUniform(
|
|
|
|
overlayShader->GetBindingSlot(str_color), color.AsFloatArray());
|
2021-04-26 23:11:35 +02:00
|
|
|
|
|
|
|
std::vector<float> vertices;
|
|
|
|
#define ADD(position) \
|
|
|
|
vertices.emplace_back((position).X); \
|
|
|
|
vertices.emplace_back((position).Y); \
|
|
|
|
vertices.emplace_back((position).Z);
|
|
|
|
|
|
|
|
// Near plane.
|
|
|
|
ADD(nearPoints[0]);
|
|
|
|
ADD(nearPoints[1]);
|
|
|
|
ADD(nearPoints[2]);
|
2022-02-22 19:41:31 +01:00
|
|
|
ADD(nearPoints[0]);
|
|
|
|
ADD(nearPoints[2]);
|
2021-04-26 23:11:35 +02:00
|
|
|
ADD(nearPoints[3]);
|
|
|
|
|
|
|
|
// Far plane.
|
|
|
|
ADD(farPoints[0]);
|
|
|
|
ADD(farPoints[1]);
|
|
|
|
ADD(farPoints[2]);
|
2022-02-22 19:41:31 +01:00
|
|
|
ADD(farPoints[0]);
|
|
|
|
ADD(farPoints[2]);
|
2021-04-26 23:11:35 +02:00
|
|
|
ADD(farPoints[3]);
|
|
|
|
|
|
|
|
// Intermediate planes.
|
2021-04-15 20:07:01 +02:00
|
|
|
CVector3D intermediatePoints[4];
|
2022-02-22 19:41:31 +01:00
|
|
|
for (int i = 0; i < intermediates; ++i)
|
2021-04-15 20:07:01 +02:00
|
|
|
{
|
2021-04-26 23:11:35 +02:00
|
|
|
const float t = (i + 1.0f) / (intermediates + 1.0f);
|
2021-04-15 20:07:01 +02:00
|
|
|
|
2022-02-22 19:41:31 +01:00
|
|
|
for (int j = 0; j < 4; ++j)
|
2021-04-15 20:07:01 +02:00
|
|
|
intermediatePoints[j] = nearPoints[j] * t + farPoints[j] * (1.0f - t);
|
|
|
|
|
2021-04-26 23:11:35 +02:00
|
|
|
ADD(intermediatePoints[0]);
|
|
|
|
ADD(intermediatePoints[1]);
|
|
|
|
ADD(intermediatePoints[2]);
|
2022-02-22 19:41:31 +01:00
|
|
|
ADD(intermediatePoints[0]);
|
|
|
|
ADD(intermediatePoints[2]);
|
2021-04-26 23:11:35 +02:00
|
|
|
ADD(intermediatePoints[3]);
|
2021-04-15 20:07:01 +02:00
|
|
|
}
|
2021-04-26 23:11:35 +02:00
|
|
|
|
2023-01-06 01:39:25 +01:00
|
|
|
deviceCommandContext->SetVertexInputLayout(m_VertexInputLayout);
|
2022-05-24 09:11:23 +02:00
|
|
|
deviceCommandContext->SetVertexBufferData(
|
|
|
|
0, vertices.data(), vertices.size() * sizeof(vertices[0]));
|
2022-04-23 22:11:14 +02:00
|
|
|
|
2022-03-03 08:09:59 +01:00
|
|
|
deviceCommandContext->Draw(0, vertices.size() / 3);
|
2021-04-26 23:11:35 +02:00
|
|
|
|
|
|
|
vertices.clear();
|
|
|
|
|
|
|
|
// Connection lines.
|
2022-02-22 19:41:31 +01:00
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
{
|
|
|
|
const int nextI = (i + 1) % 4;
|
|
|
|
ADD(nearPoints[i]);
|
|
|
|
ADD(farPoints[nextI]);
|
|
|
|
ADD(farPoints[i]);
|
|
|
|
ADD(nearPoints[i]);
|
|
|
|
ADD(nearPoints[nextI]);
|
|
|
|
ADD(farPoints[nextI]);
|
|
|
|
}
|
2021-04-26 23:11:35 +02:00
|
|
|
|
2023-01-06 01:39:25 +01:00
|
|
|
deviceCommandContext->SetVertexInputLayout(m_VertexInputLayout);
|
2022-05-24 09:11:23 +02:00
|
|
|
deviceCommandContext->SetVertexBufferData(
|
|
|
|
0, vertices.data(), vertices.size() * sizeof(vertices[0]));
|
2022-04-23 22:11:14 +02:00
|
|
|
|
2022-03-03 08:09:59 +01:00
|
|
|
deviceCommandContext->Draw(0, vertices.size() / 3);
|
2021-04-26 23:11:35 +02:00
|
|
|
#undef ADD
|
|
|
|
|
2022-03-14 23:16:14 +01:00
|
|
|
deviceCommandContext->EndPass();
|
2021-04-15 20:07:01 +02:00
|
|
|
}
|
|
|
|
|
2022-02-26 01:37:27 +01:00
|
|
|
void CDebugRenderer::DrawBoundingBox(
|
|
|
|
const CBoundingBoxAligned& boundingBox, const CColor& color,
|
|
|
|
bool wireframe)
|
2021-04-15 20:07:01 +02:00
|
|
|
{
|
2022-02-26 01:37:27 +01:00
|
|
|
DrawBoundingBox(
|
|
|
|
boundingBox, color,
|
|
|
|
g_Renderer.GetSceneRenderer().GetViewCamera().GetViewProjection(), wireframe);
|
2021-04-27 21:23:37 +02:00
|
|
|
}
|
|
|
|
|
2022-02-26 01:37:27 +01:00
|
|
|
void CDebugRenderer::DrawBoundingBox(
|
|
|
|
const CBoundingBoxAligned& boundingBox, const CColor& color,
|
|
|
|
const CMatrix3D& transform, bool wireframe)
|
2021-04-27 21:23:37 +02:00
|
|
|
{
|
2022-12-31 19:29:44 +01:00
|
|
|
CShaderTechniquePtr shaderTech =
|
|
|
|
GetShaderTechnique(str_debug_line, color, true, wireframe);
|
|
|
|
|
2022-05-09 00:02:46 +02:00
|
|
|
Renderer::Backend::IDeviceCommandContext* deviceCommandContext =
|
2022-03-03 08:09:59 +01:00
|
|
|
g_Renderer.GetDeviceCommandContext();
|
2022-12-31 19:29:44 +01:00
|
|
|
deviceCommandContext->SetGraphicsPipelineState(
|
|
|
|
shaderTech->GetGraphicsPipelineState());
|
2022-03-14 23:16:14 +01:00
|
|
|
deviceCommandContext->BeginPass();
|
2021-04-27 21:23:37 +02:00
|
|
|
|
2022-05-02 22:57:22 +02:00
|
|
|
Renderer::Backend::IShaderProgram* shader = shaderTech->GetShader();
|
|
|
|
|
|
|
|
deviceCommandContext->SetUniform(
|
|
|
|
shader->GetBindingSlot(str_transform), transform.AsFloatArray());
|
|
|
|
deviceCommandContext->SetUniform(
|
|
|
|
shader->GetBindingSlot(str_color), color.AsFloatArray());
|
2021-04-27 21:23:37 +02:00
|
|
|
|
2021-04-15 20:07:01 +02:00
|
|
|
std::vector<float> data;
|
|
|
|
|
|
|
|
#define ADD_FACE(x, y, z) \
|
|
|
|
ADD_PT(0, 0, x, y, z); ADD_PT(1, 0, x, y, z); ADD_PT(1, 1, x, y, z); \
|
|
|
|
ADD_PT(1, 1, x, y, z); ADD_PT(0, 1, x, y, z); ADD_PT(0, 0, x, y, z);
|
|
|
|
#define ADD_PT(u_, v_, x, y, z) \
|
|
|
|
STMT(int u = u_; int v = v_; \
|
|
|
|
data.push_back(boundingBox[x].X); \
|
|
|
|
data.push_back(boundingBox[y].Y); \
|
|
|
|
data.push_back(boundingBox[z].Z); \
|
|
|
|
)
|
|
|
|
|
|
|
|
ADD_FACE(u, v, 0);
|
|
|
|
ADD_FACE(0, u, v);
|
|
|
|
ADD_FACE(u, 0, 1-v);
|
|
|
|
ADD_FACE(u, 1-v, 1);
|
|
|
|
ADD_FACE(1, u, 1-v);
|
|
|
|
ADD_FACE(u, 1, v);
|
|
|
|
|
|
|
|
#undef ADD_FACE
|
|
|
|
|
2023-01-06 01:39:25 +01:00
|
|
|
deviceCommandContext->SetVertexInputLayout(m_VertexInputLayout);
|
2022-05-24 09:11:23 +02:00
|
|
|
deviceCommandContext->SetVertexBufferData(
|
|
|
|
0, data.data(), data.size() * sizeof(data[0]));
|
2021-04-15 20:07:01 +02:00
|
|
|
|
2022-04-23 22:11:14 +02:00
|
|
|
deviceCommandContext->Draw(0, 6 * 6);
|
2021-04-27 21:23:37 +02:00
|
|
|
|
2022-03-14 23:16:14 +01:00
|
|
|
deviceCommandContext->EndPass();
|
2021-04-15 20:07:01 +02:00
|
|
|
}
|
|
|
|
|
2022-02-26 01:37:27 +01:00
|
|
|
void CDebugRenderer::DrawBrush(const CBrush& brush, const CColor& color, bool wireframe)
|
2021-04-15 20:07:01 +02:00
|
|
|
{
|
2022-12-31 19:29:44 +01:00
|
|
|
CShaderTechniquePtr shaderTech =
|
|
|
|
GetShaderTechnique(str_debug_line, color, true, wireframe);
|
|
|
|
|
2022-05-09 00:02:46 +02:00
|
|
|
Renderer::Backend::IDeviceCommandContext* deviceCommandContext =
|
2022-03-03 08:09:59 +01:00
|
|
|
g_Renderer.GetDeviceCommandContext();
|
2022-12-31 19:29:44 +01:00
|
|
|
deviceCommandContext->SetGraphicsPipelineState(
|
|
|
|
shaderTech->GetGraphicsPipelineState());
|
2022-03-14 23:16:14 +01:00
|
|
|
deviceCommandContext->BeginPass();
|
2021-04-27 21:23:37 +02:00
|
|
|
|
2022-05-02 22:57:22 +02:00
|
|
|
Renderer::Backend::IShaderProgram* shader = shaderTech->GetShader();
|
|
|
|
|
|
|
|
const CMatrix3D transform = g_Renderer.GetSceneRenderer().GetViewCamera().GetViewProjection();
|
|
|
|
deviceCommandContext->SetUniform(
|
|
|
|
shader->GetBindingSlot(str_transform), transform.AsFloatArray());
|
|
|
|
deviceCommandContext->SetUniform(
|
|
|
|
shader->GetBindingSlot(str_color), color.AsFloatArray());
|
2021-04-27 21:23:37 +02:00
|
|
|
|
2021-04-15 20:07:01 +02:00
|
|
|
std::vector<float> data;
|
|
|
|
|
|
|
|
std::vector<std::vector<size_t>> faces;
|
|
|
|
brush.GetFaces(faces);
|
|
|
|
|
|
|
|
#define ADD_VERT(a) \
|
|
|
|
STMT( \
|
|
|
|
data.push_back(brush.GetVertices()[faces[i][a]].X); \
|
|
|
|
data.push_back(brush.GetVertices()[faces[i][a]].Y); \
|
|
|
|
data.push_back(brush.GetVertices()[faces[i][a]].Z); \
|
|
|
|
)
|
|
|
|
|
|
|
|
for (size_t i = 0; i < faces.size(); ++i)
|
|
|
|
{
|
|
|
|
// Triangulate into (0,1,2), (0,2,3), ...
|
|
|
|
for (size_t j = 1; j < faces[i].size() - 2; ++j)
|
|
|
|
{
|
|
|
|
ADD_VERT(0);
|
|
|
|
ADD_VERT(j);
|
|
|
|
ADD_VERT(j+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef ADD_VERT
|
|
|
|
|
2023-01-06 01:39:25 +01:00
|
|
|
deviceCommandContext->SetVertexInputLayout(m_VertexInputLayout);
|
2022-05-24 09:11:23 +02:00
|
|
|
deviceCommandContext->SetVertexBufferData(
|
|
|
|
0, data.data(), data.size() * sizeof(data[0]));
|
2021-04-15 20:07:01 +02:00
|
|
|
|
2022-03-03 08:09:59 +01:00
|
|
|
deviceCommandContext->Draw(0, data.size() / 5);
|
2021-04-27 21:23:37 +02:00
|
|
|
|
2022-03-14 23:16:14 +01:00
|
|
|
deviceCommandContext->EndPass();
|
2021-04-15 20:07:01 +02:00
|
|
|
}
|
|
|
|
|
2022-12-31 19:29:44 +01:00
|
|
|
size_t CDebugRenderer::ShaderTechniqueKeyHash::operator()(
|
|
|
|
const ShaderTechniqueKey& key) const
|
|
|
|
{
|
|
|
|
size_t seed = 0;
|
|
|
|
hash_combine(seed, key.name.GetHash());
|
|
|
|
hash_combine(seed, key.transparent);
|
|
|
|
hash_combine(seed, key.depthTestEnabled);
|
|
|
|
hash_combine(seed, key.wireframe);
|
|
|
|
return seed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CDebugRenderer::ShaderTechniqueKeyEqual::operator()(
|
|
|
|
const ShaderTechniqueKey& lhs, const ShaderTechniqueKey& rhs) const
|
|
|
|
{
|
|
|
|
return
|
|
|
|
lhs.name == rhs.name && lhs.transparent == rhs.transparent &&
|
|
|
|
lhs.depthTestEnabled == rhs.depthTestEnabled &&
|
|
|
|
lhs.wireframe == rhs.wireframe;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CShaderTechniquePtr& CDebugRenderer::GetShaderTechnique(
|
|
|
|
const CStrIntern name, const CColor& color, const bool depthTestEnabled,
|
|
|
|
const bool wireframe)
|
|
|
|
{
|
|
|
|
const ShaderTechniqueKey key{
|
|
|
|
name, color.a != 1.0f, depthTestEnabled, wireframe};
|
|
|
|
CShaderTechniquePtr& shaderTechnique = m_ShaderTechniqueMapping[key];
|
|
|
|
if (shaderTechnique)
|
|
|
|
return shaderTechnique;
|
|
|
|
|
|
|
|
shaderTechnique = g_Renderer.GetShaderManager().LoadEffect(
|
|
|
|
name, {},
|
|
|
|
[key](Renderer::Backend::SGraphicsPipelineStateDesc& pipelineStateDesc)
|
|
|
|
{
|
|
|
|
pipelineStateDesc.depthStencilState.depthTestEnabled = key.depthTestEnabled;
|
|
|
|
if (key.transparent)
|
|
|
|
{
|
|
|
|
pipelineStateDesc.blendState.enabled = true;
|
|
|
|
pipelineStateDesc.blendState.srcColorBlendFactor = pipelineStateDesc.blendState.srcAlphaBlendFactor =
|
|
|
|
Renderer::Backend::BlendFactor::SRC_ALPHA;
|
|
|
|
pipelineStateDesc.blendState.dstColorBlendFactor = pipelineStateDesc.blendState.dstAlphaBlendFactor =
|
|
|
|
Renderer::Backend::BlendFactor::ONE_MINUS_SRC_ALPHA;
|
|
|
|
pipelineStateDesc.blendState.colorBlendOp = pipelineStateDesc.blendState.alphaBlendOp =
|
|
|
|
Renderer::Backend::BlendOp::ADD;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pipelineStateDesc.blendState.enabled = false;
|
|
|
|
if (key.wireframe)
|
|
|
|
pipelineStateDesc.rasterizationState.polygonMode = Renderer::Backend::PolygonMode::LINE;
|
|
|
|
pipelineStateDesc.rasterizationState.cullMode = Renderer::Backend::CullMode::NONE;
|
|
|
|
});
|
|
|
|
return shaderTechnique;
|
|
|
|
}
|