Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "DebugRenderer.h"
21 :
22 : #include "graphics/Camera.h"
23 : #include "graphics/Color.h"
24 : #include "graphics/ShaderDefines.h"
25 : #include "graphics/ShaderManager.h"
26 : #include "graphics/ShaderTechnique.h"
27 : #include "lib/code_generation.h"
28 : #include "lib/hash.h"
29 : #include "maths/BoundingBoxAligned.h"
30 : #include "maths/Brush.h"
31 : #include "maths/Matrix3D.h"
32 : #include "maths/Vector3D.h"
33 : #include "ps/CStrInternStatic.h"
34 : #include "renderer/Renderer.h"
35 : #include "renderer/SceneRenderer.h"
36 : #include "renderer/backend/Format.h"
37 : #include "renderer/backend/IDeviceCommandContext.h"
38 : #include "renderer/backend/IShaderProgram.h"
39 : #include "renderer/backend/PipelineState.h"
40 :
41 : #include <array>
42 : #include <cmath>
43 : #include <numbers>
44 :
45 10 : void CDebugRenderer::Initialize()
46 : {
47 10 : const std::array<Renderer::Backend::SVertexAttributeFormat, 1> attributes{{
48 : {Renderer::Backend::VertexAttributeStream::POSITION,
49 : Renderer::Backend::Format::R32G32B32_SFLOAT, 0, sizeof(float) * 3,
50 : Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0}
51 : }};
52 10 : m_VertexInputLayout = g_Renderer.GetVertexInputLayout(attributes);
53 10 : }
54 :
55 0 : void CDebugRenderer::DrawLine(
56 : Renderer::Backend::IDeviceCommandContext& deviceCommandContext,
57 : const CVector3D& from, const CVector3D& to, const CColor& color,
58 : const float width, const bool depthTestEnabled)
59 : {
60 0 : if (from == to)
61 : return;
62 :
63 0 : DrawLine(deviceCommandContext, {from, to}, color, width, depthTestEnabled);
64 : }
65 :
66 0 : void CDebugRenderer::DrawLine(
67 : Renderer::Backend::IDeviceCommandContext& deviceCommandContext,
68 : const std::vector<CVector3D>& line, const CColor& color,
69 : const float width, const bool depthTestEnabled)
70 : {
71 0 : if (line.size() <= 1)
72 0 : return;
73 :
74 0 : CShaderTechniquePtr debugLineTech =
75 0 : GetShaderTechnique(str_debug_line, color, depthTestEnabled);
76 0 : deviceCommandContext.SetGraphicsPipelineState(
77 : debugLineTech->GetGraphicsPipelineState());
78 0 : deviceCommandContext.BeginPass();
79 :
80 0 : const CCamera& viewCamera = g_Renderer.GetSceneRenderer().GetViewCamera();
81 :
82 0 : Renderer::Backend::IShaderProgram* debugLineShader = debugLineTech->GetShader();
83 0 : const CMatrix3D transform = viewCamera.GetViewProjection();
84 0 : deviceCommandContext.SetUniform(
85 0 : debugLineShader->GetBindingSlot(str_transform), transform.AsFloatArray());
86 0 : deviceCommandContext.SetUniform(
87 0 : debugLineShader->GetBindingSlot(str_color), color.AsFloatArray());
88 :
89 0 : const CVector3D cameraIn = viewCamera.GetOrientation().GetIn();
90 :
91 0 : std::vector<float> vertices;
92 0 : vertices.reserve(line.size() * 6 * 3);
93 : #define ADD(position) \
94 : vertices.emplace_back((position).X); \
95 : vertices.emplace_back((position).Y); \
96 : vertices.emplace_back((position).Z);
97 :
98 0 : for (size_t idx = 1; idx < line.size(); ++idx)
99 : {
100 0 : const CVector3D from = line[idx - 1];
101 0 : const CVector3D to = line[idx];
102 0 : const CVector3D direction = (to - from).Normalized();
103 0 : const CVector3D view = direction.Dot(cameraIn) > 0.9f ?
104 : CVector3D(0.0f, 1.0f, 0.0f) :
105 0 : cameraIn;
106 0 : const CVector3D offset = view.Cross(direction).Normalized() * width;
107 :
108 0 : ADD(from + offset)
109 0 : ADD(to - offset)
110 0 : ADD(to + offset)
111 0 : ADD(from + offset)
112 0 : ADD(from - offset)
113 0 : ADD(to - offset)
114 : }
115 :
116 : #undef ADD
117 :
118 0 : deviceCommandContext.SetVertexInputLayout(m_VertexInputLayout);
119 0 : deviceCommandContext.SetVertexBufferData(
120 0 : 0, vertices.data(), vertices.size() * sizeof(vertices[0]));
121 :
122 0 : deviceCommandContext.Draw(0, vertices.size() / 3);
123 :
124 0 : deviceCommandContext.EndPass();
125 0 : }
126 :
127 0 : void CDebugRenderer::DrawCircle(
128 : Renderer::Backend::IDeviceCommandContext& deviceCommandContext,
129 : const CVector3D& origin, const float radius, const CColor& color)
130 : {
131 0 : CShaderTechniquePtr debugCircleTech =
132 0 : GetShaderTechnique(str_debug_line, color);
133 :
134 0 : deviceCommandContext.SetGraphicsPipelineState(
135 : debugCircleTech->GetGraphicsPipelineState());
136 0 : deviceCommandContext.BeginPass();
137 :
138 0 : const CCamera& camera = g_Renderer.GetSceneRenderer().GetViewCamera();
139 :
140 0 : Renderer::Backend::IShaderProgram* debugCircleShader = debugCircleTech->GetShader();
141 :
142 0 : const CMatrix3D transform = camera.GetViewProjection();
143 0 : deviceCommandContext.SetUniform(
144 0 : debugCircleShader->GetBindingSlot(str_transform), transform.AsFloatArray());
145 0 : deviceCommandContext.SetUniform(
146 0 : debugCircleShader->GetBindingSlot(str_color), color.AsFloatArray());
147 :
148 0 : const CVector3D cameraUp = camera.GetOrientation().GetUp();
149 0 : const CVector3D cameraLeft = camera.GetOrientation().GetLeft();
150 :
151 0 : std::vector<float> vertices;
152 : #define ADD(position) \
153 : vertices.emplace_back((position).X); \
154 : vertices.emplace_back((position).Y); \
155 : vertices.emplace_back((position).Z);
156 :
157 0 : constexpr size_t segments = 16;
158 0 : for (size_t idx = 0; idx <= segments; ++idx)
159 : {
160 0 : const float angle = std::numbers::pi_v<float> * 2.0f * idx / segments;
161 0 : const CVector3D offset = cameraUp * sin(angle) - cameraLeft * cos(angle);
162 0 : const float nextAngle = std::numbers::pi_v<float> * 2.0f * (idx + 1) / segments;
163 0 : const CVector3D nextOffset = cameraUp * sin(nextAngle) - cameraLeft * cos(nextAngle);
164 0 : ADD(origin)
165 0 : ADD(origin + offset * radius)
166 0 : ADD(origin + nextOffset * radius)
167 : }
168 :
169 : #undef ADD
170 :
171 0 : deviceCommandContext.SetVertexInputLayout(m_VertexInputLayout);
172 0 : deviceCommandContext.SetVertexBufferData(
173 0 : 0, vertices.data(), vertices.size() * sizeof(vertices[0]));
174 :
175 0 : deviceCommandContext.Draw(0, vertices.size() / 3);
176 :
177 0 : deviceCommandContext.EndPass();
178 0 : }
179 :
180 0 : void CDebugRenderer::DrawCameraFrustum(Renderer::Backend::IDeviceCommandContext& deviceCommandContext,
181 : const CCamera& camera, const CColor& color, int intermediates, bool wireframe)
182 : {
183 0 : CCamera::Quad nearPoints{camera.GetViewQuad(camera.GetNearPlane())};
184 0 : for (CVector3D& point : nearPoints)
185 0 : point = camera.m_Orientation.Transform(point);
186 0 : CCamera::Quad farPoints{camera.GetViewQuad(camera.GetFarPlane())};
187 0 : for (CVector3D& point : farPoints)
188 0 : point = camera.m_Orientation.Transform(point);
189 :
190 0 : CShaderTechniquePtr overlayTech =
191 0 : GetShaderTechnique(str_debug_line, color, true, wireframe);
192 :
193 0 : deviceCommandContext.SetGraphicsPipelineState(
194 : overlayTech->GetGraphicsPipelineState());
195 0 : deviceCommandContext.BeginPass();
196 :
197 0 : Renderer::Backend::IShaderProgram* overlayShader = overlayTech->GetShader();
198 :
199 0 : const CMatrix3D transform = g_Renderer.GetSceneRenderer().GetViewCamera().GetViewProjection();
200 0 : deviceCommandContext.SetUniform(
201 0 : overlayShader->GetBindingSlot(str_transform), transform.AsFloatArray());
202 0 : deviceCommandContext.SetUniform(
203 0 : overlayShader->GetBindingSlot(str_color), color.AsFloatArray());
204 :
205 0 : std::vector<float> vertices;
206 : #define ADD(position) \
207 : vertices.emplace_back((position).X); \
208 : vertices.emplace_back((position).Y); \
209 : vertices.emplace_back((position).Z);
210 :
211 : // Near plane.
212 0 : ADD(nearPoints[0]);
213 0 : ADD(nearPoints[1]);
214 0 : ADD(nearPoints[2]);
215 0 : ADD(nearPoints[0]);
216 0 : ADD(nearPoints[2]);
217 0 : ADD(nearPoints[3]);
218 :
219 : // Far plane.
220 0 : ADD(farPoints[0]);
221 0 : ADD(farPoints[1]);
222 0 : ADD(farPoints[2]);
223 0 : ADD(farPoints[0]);
224 0 : ADD(farPoints[2]);
225 0 : ADD(farPoints[3]);
226 :
227 : // Intermediate planes.
228 0 : CVector3D intermediatePoints[4];
229 0 : for (int i = 0; i < intermediates; ++i)
230 : {
231 0 : const float t = (i + 1.0f) / (intermediates + 1.0f);
232 :
233 0 : for (int j = 0; j < 4; ++j)
234 0 : intermediatePoints[j] = nearPoints[j] * t + farPoints[j] * (1.0f - t);
235 :
236 0 : ADD(intermediatePoints[0]);
237 0 : ADD(intermediatePoints[1]);
238 0 : ADD(intermediatePoints[2]);
239 0 : ADD(intermediatePoints[0]);
240 0 : ADD(intermediatePoints[2]);
241 0 : ADD(intermediatePoints[3]);
242 : }
243 :
244 0 : deviceCommandContext.SetVertexInputLayout(m_VertexInputLayout);
245 0 : deviceCommandContext.SetVertexBufferData(
246 0 : 0, vertices.data(), vertices.size() * sizeof(vertices[0]));
247 :
248 0 : deviceCommandContext.Draw(0, vertices.size() / 3);
249 :
250 0 : vertices.clear();
251 :
252 : // Connection lines.
253 0 : for (int i = 0; i < 4; ++i)
254 : {
255 0 : const int nextI = (i + 1) % 4;
256 0 : ADD(nearPoints[i]);
257 0 : ADD(farPoints[nextI]);
258 0 : ADD(farPoints[i]);
259 0 : ADD(nearPoints[i]);
260 0 : ADD(nearPoints[nextI]);
261 0 : ADD(farPoints[nextI]);
262 : }
263 :
264 0 : deviceCommandContext.SetVertexInputLayout(m_VertexInputLayout);
265 0 : deviceCommandContext.SetVertexBufferData(
266 0 : 0, vertices.data(), vertices.size() * sizeof(vertices[0]));
267 :
268 0 : deviceCommandContext.Draw(0, vertices.size() / 3);
269 : #undef ADD
270 :
271 0 : deviceCommandContext.EndPass();
272 0 : }
273 :
274 0 : void CDebugRenderer::DrawBoundingBox(
275 : Renderer::Backend::IDeviceCommandContext& deviceCommandContext,
276 : const CBoundingBoxAligned& boundingBox, const CColor& color,
277 : bool wireframe)
278 : {
279 0 : DrawBoundingBox(
280 : deviceCommandContext,
281 : boundingBox, color,
282 0 : g_Renderer.GetSceneRenderer().GetViewCamera().GetViewProjection(), wireframe);
283 0 : }
284 :
285 0 : void CDebugRenderer::DrawBoundingBox(
286 : Renderer::Backend::IDeviceCommandContext& deviceCommandContext,
287 : const CBoundingBoxAligned& boundingBox, const CColor& color,
288 : const CMatrix3D& transform, bool wireframe)
289 : {
290 0 : CShaderTechniquePtr shaderTech =
291 0 : GetShaderTechnique(str_debug_line, color, true, wireframe);
292 :
293 0 : deviceCommandContext.SetGraphicsPipelineState(
294 : shaderTech->GetGraphicsPipelineState());
295 0 : deviceCommandContext.BeginPass();
296 :
297 0 : Renderer::Backend::IShaderProgram* shader = shaderTech->GetShader();
298 :
299 0 : deviceCommandContext.SetUniform(
300 0 : shader->GetBindingSlot(str_transform), transform.AsFloatArray());
301 0 : deviceCommandContext.SetUniform(
302 0 : shader->GetBindingSlot(str_color), color.AsFloatArray());
303 :
304 0 : std::vector<float> data;
305 :
306 : #define ADD_FACE(x, y, z) \
307 : ADD_PT(0, 0, x, y, z); ADD_PT(1, 0, x, y, z); ADD_PT(1, 1, x, y, z); \
308 : ADD_PT(1, 1, x, y, z); ADD_PT(0, 1, x, y, z); ADD_PT(0, 0, x, y, z);
309 : #define ADD_PT(u_, v_, x, y, z) \
310 : STMT(int u = u_; int v = v_; \
311 : data.push_back(boundingBox[x].X); \
312 : data.push_back(boundingBox[y].Y); \
313 : data.push_back(boundingBox[z].Z); \
314 : )
315 :
316 0 : ADD_FACE(u, v, 0);
317 0 : ADD_FACE(0, u, v);
318 0 : ADD_FACE(u, 0, 1-v);
319 0 : ADD_FACE(u, 1-v, 1);
320 0 : ADD_FACE(1, u, 1-v);
321 0 : ADD_FACE(u, 1, v);
322 :
323 : #undef ADD_FACE
324 :
325 0 : deviceCommandContext.SetVertexInputLayout(m_VertexInputLayout);
326 0 : deviceCommandContext.SetVertexBufferData(
327 0 : 0, data.data(), data.size() * sizeof(data[0]));
328 :
329 0 : deviceCommandContext.Draw(0, 6 * 6);
330 :
331 0 : deviceCommandContext.EndPass();
332 0 : }
333 :
334 0 : void CDebugRenderer::DrawBrush(
335 : Renderer::Backend::IDeviceCommandContext& deviceCommandContext,
336 : const CBrush& brush, const CColor& color, bool wireframe)
337 : {
338 0 : CShaderTechniquePtr shaderTech =
339 0 : GetShaderTechnique(str_debug_line, color, true, wireframe);
340 :
341 0 : deviceCommandContext.SetGraphicsPipelineState(
342 : shaderTech->GetGraphicsPipelineState());
343 0 : deviceCommandContext.BeginPass();
344 :
345 0 : Renderer::Backend::IShaderProgram* shader = shaderTech->GetShader();
346 :
347 0 : const CMatrix3D transform = g_Renderer.GetSceneRenderer().GetViewCamera().GetViewProjection();
348 0 : deviceCommandContext.SetUniform(
349 0 : shader->GetBindingSlot(str_transform), transform.AsFloatArray());
350 0 : deviceCommandContext.SetUniform(
351 0 : shader->GetBindingSlot(str_color), color.AsFloatArray());
352 :
353 0 : std::vector<float> data;
354 :
355 0 : std::vector<std::vector<size_t>> faces;
356 0 : brush.GetFaces(faces);
357 :
358 : #define ADD_VERT(a) \
359 : STMT( \
360 : data.push_back(brush.GetVertices()[faces[i][a]].X); \
361 : data.push_back(brush.GetVertices()[faces[i][a]].Y); \
362 : data.push_back(brush.GetVertices()[faces[i][a]].Z); \
363 : )
364 :
365 0 : for (size_t i = 0; i < faces.size(); ++i)
366 : {
367 : // Triangulate into (0,1,2), (0,2,3), ...
368 0 : for (size_t j = 1; j < faces[i].size() - 2; ++j)
369 : {
370 0 : ADD_VERT(0);
371 0 : ADD_VERT(j);
372 0 : ADD_VERT(j+1);
373 : }
374 : }
375 :
376 : #undef ADD_VERT
377 :
378 0 : deviceCommandContext.SetVertexInputLayout(m_VertexInputLayout);
379 0 : deviceCommandContext.SetVertexBufferData(
380 0 : 0, data.data(), data.size() * sizeof(data[0]));
381 :
382 0 : deviceCommandContext.Draw(0, data.size() / 5);
383 :
384 0 : deviceCommandContext.EndPass();
385 0 : }
386 :
387 0 : size_t CDebugRenderer::ShaderTechniqueKeyHash::operator()(
388 : const ShaderTechniqueKey& key) const
389 : {
390 0 : size_t seed = 0;
391 0 : hash_combine(seed, key.name.GetHash());
392 0 : hash_combine(seed, key.transparent);
393 0 : hash_combine(seed, key.depthTestEnabled);
394 0 : hash_combine(seed, key.wireframe);
395 0 : return seed;
396 : }
397 :
398 0 : bool CDebugRenderer::ShaderTechniqueKeyEqual::operator()(
399 : const ShaderTechniqueKey& lhs, const ShaderTechniqueKey& rhs) const
400 : {
401 0 : return
402 0 : lhs.name == rhs.name && lhs.transparent == rhs.transparent &&
403 0 : lhs.depthTestEnabled == rhs.depthTestEnabled &&
404 0 : lhs.wireframe == rhs.wireframe;
405 : }
406 :
407 0 : const CShaderTechniquePtr& CDebugRenderer::GetShaderTechnique(
408 : const CStrIntern name, const CColor& color, const bool depthTestEnabled,
409 : const bool wireframe)
410 : {
411 0 : const ShaderTechniqueKey key{
412 0 : name, color.a != 1.0f, depthTestEnabled, wireframe};
413 0 : CShaderTechniquePtr& shaderTechnique = m_ShaderTechniqueMapping[key];
414 0 : if (shaderTechnique)
415 : return shaderTechnique;
416 :
417 0 : shaderTechnique = g_Renderer.GetShaderManager().LoadEffect(
418 : name, {},
419 0 : [key](Renderer::Backend::SGraphicsPipelineStateDesc& pipelineStateDesc)
420 : {
421 0 : pipelineStateDesc.depthStencilState.depthTestEnabled = key.depthTestEnabled;
422 0 : if (key.transparent)
423 : {
424 0 : pipelineStateDesc.blendState.enabled = true;
425 0 : pipelineStateDesc.blendState.srcColorBlendFactor = pipelineStateDesc.blendState.srcAlphaBlendFactor =
426 : Renderer::Backend::BlendFactor::SRC_ALPHA;
427 0 : pipelineStateDesc.blendState.dstColorBlendFactor = pipelineStateDesc.blendState.dstAlphaBlendFactor =
428 : Renderer::Backend::BlendFactor::ONE_MINUS_SRC_ALPHA;
429 0 : pipelineStateDesc.blendState.colorBlendOp = pipelineStateDesc.blendState.alphaBlendOp =
430 : Renderer::Backend::BlendOp::ADD;
431 : }
432 : else
433 0 : pipelineStateDesc.blendState.enabled = false;
434 0 : if (key.wireframe)
435 0 : pipelineStateDesc.rasterizationState.polygonMode = Renderer::Backend::PolygonMode::LINE;
436 0 : pipelineStateDesc.rasterizationState.cullMode = Renderer::Backend::CullMode::NONE;
437 0 : });
438 0 : return shaderTechnique;
439 : }
|