1#include <glm/gtc/type_ptr.hpp>
2#include "Rendering/ICapabilities.h"
3#include "Rendering/IGraphicsDevice.h"
4#include "Rendering/IBuffers.h"
6#include "Renderer/RenderList.h"
7#include "Renderer/Tasks/RenderTask.h"
8#include "Renderer/RenderStateUpdater.h"
9#include "Renderer/RenderTarget.h"
10#include "Renderer/Renderer.h"
12#include "Resources/VertexFormats.h"
14#include "Resources/MaterialManager.h"
16#include "DebugGeometryRenderers.h"
24 glm::mat4 projectionMatrix;
26 glm::mat4 worldMatrix;
27 glm::vec4 diffuseColor;
34 const glm::mat4 identityMatrix = glm::mat4();
36 const glm::vec3 coordSysData[] = {
50 *coordSysVerticesP = buffers->loadVertexBuffer(coordSysData,
sizeof(coordSysData) /
sizeof(glm::vec3), Cogs::Core::VertexFormats::Pos3f);
51 buffers->annotate(*coordSysVerticesP,
"CoordSys vertices");
54 const glm::vec3 unitVerticesData[] = {
71 *unitVerticesP = buffers->loadVertexBuffer(unitVerticesData,
sizeof(unitVerticesData) /
sizeof(glm::vec3), Cogs::Core::VertexFormats::Pos3f);
72 buffers->annotate(*unitVerticesP,
"Unit box vertices");
76 const glm::vec3 oneOneVerticesData[] = {
77 glm::vec3(-1, -1, -1),
88 glm::vec3(-1 + 0.1f, 1 - 0.1f, -1),
89 glm::vec3(-1 + 0.3f, 1 - 0.1f, -1),
90 glm::vec3(-1 + 0.1f, 1 - 0.3f, -1),
91 glm::vec3(-1 + +.3f, 1 - 0.3f, -1),
94 glm::vec3(1 - 0.1f, 1 - 0.1f, 1),
95 glm::vec3(1 - 0.1f, 1 - 0.2f, 1),
96 glm::vec3(1 - 0.1f, 1 - 0.3f, 1),
97 glm::vec3(1 - 0.2f, 1 - 0.2f, 1),
98 glm::vec3(1 - 0.3f, 1 - 0.1f, 1),
101 glm::vec3(1, 1 - 0.1f, -1 + 0.1f),
102 glm::vec3(1, 1 - 0.3f, -1 + 0.1f),
103 glm::vec3(1, 1 - 0.1f, -1 + 0.3f),
104 glm::vec3(1, 1 - 0.3f, -1 + 0.3f),
106 glm::vec3(1, 1 - 0.2, -1 + 0.4f),
107 glm::vec3(1, 1 - 0.2, -1 + 0.6f),
108 glm::vec3(1, 1 - 0.1, -1 + 0.5f),
109 glm::vec3(1, 1 - 0.3, -1 + 0.5f),
112 glm::vec3(-1, 1 - 0.1f, 1 - 0.1f),
113 glm::vec3(-1, 1 - 0.3f, 1 - 0.1f),
114 glm::vec3(-1, 1 - 0.1f, 1 - 0.3f),
115 glm::vec3(-1, 1 - 0.3f, 1 - 0.3f),
116 glm::vec3(-1, 1 - 0.2, 1 - 0.4f),
117 glm::vec3(-1, 1 - 0.2, 1 - 0.6f),
120 glm::vec3(1 - 0.1f, 1, -1 + 0.1f),
121 glm::vec3(1 - 0.3f, 1, -1 + 0.1f),
122 glm::vec3(1 - 0.2f, 1, -1 + 0.2f),
123 glm::vec3(1 - 0.2f, 1, -1 + 0.3f),
125 glm::vec3(1 - 0.4f, 1, -1 + 0.2f),
126 glm::vec3(1 - 0.6f, 1, -1 + 0.2f),
127 glm::vec3(1 - 0.5f, 1, -1 + 0.1f),
128 glm::vec3(1 - 0.5f, 1, -1 + 0.3f),
131 glm::vec3(-1 + 0.1f, -1, -1 + 0.1f),
132 glm::vec3(-1 + 0.3f, -1, -1 + 0.1f),
133 glm::vec3(-1 + 0.2f, -1, -1 + 0.2f),
134 glm::vec3(-1 + 0.2f, -1, -1 + 0.3f),
136 glm::vec3(-1 + 0.4f, -1, -1 + 0.2f),
137 glm::vec3(-1 + 0.6f, -1, -1 + 0.2f),
146 *oneOneVerticesP = buffers->loadVertexBuffer(oneOneVerticesData,
sizeof(oneOneVerticesData) /
sizeof(glm::vec3), Cogs::Core::VertexFormats::Pos3f);
147 buffers->annotate(*oneOneVerticesP,
"One-one box vertices");
151 const uint16_t edgeIndicesData[] = {
161 12, 13, 13, 14, 13, 15, 12, 16,
162 17, 20, 18, 19, 21, 22, 23, 24,
163 25, 28, 26, 27, 29, 30,
164 31, 33, 33, 32, 33, 34, 35, 36, 37, 38,
165 39, 41, 41, 40, 41, 42, 43, 44
173 *edgeIndicesP = buffers->loadIndexBuffer(edgeIndicesData,
sizeof(edgeIndicesData) /
sizeof(uint16_t),
sizeof(uint16_t));
174 buffers->annotate(*edgeIndicesP,
"Box edge indices");
177 const glm::vec4 colors[6] =
179 glm::vec4(1.f, 0.f, 0.f, 1.f),
180 glm::vec4(0.f, 1.f, 0.f, 1.f),
181 glm::vec4(0.f, 0.f, 1.f, 1.f),
182 glm::vec4(0.f, 1.f, 1.f, 1.f),
183 glm::vec4(1.f, 1.f, 0.f, 1.f),
184 glm::vec4(1.f, 0.f, 1.f, 1.f),
189 auto * device = taskContext->device;
197 constants->projectionMatrix = M;
198 constants->viewMatrix = identityMatrix;
199 constants->worldMatrix = identityMatrix;
200 constants->diffuseColor = color;
203 context->setConstantBuffer(
"DebugBuffer", constantBuffer);
212 auto * renderer = taskContext->renderer;
213 auto * device = taskContext->device;
215 const auto * camData = drawContext->cameraData;
217 setupEdgeIndices(device);
219 bool useCamData = 2.f < std::min(camData->viewportSize.x, camData->viewportSize.y);
221 context->setEffect(*effectHandleP);
222 context->setInputLayout(*inputLayoutHandleP);
224 auto w = useCamData ? camData->viewportSize.x : (drawContext->renderTarget ? drawContext->renderTarget->width : renderer->getSize().x);
225 auto h = useCamData ? camData->viewportSize.y : (drawContext->renderTarget ? drawContext->renderTarget->height : renderer->getSize().y);
226 context->setViewport(camData->viewportOrigin.x, camData->viewportOrigin.y, w, h);
228 context->setDepthStencilState(drawContext->renderer->
getRenderStates().defaultDepthStencilStateHandle);
229 context->setBlendState(drawContext->renderer->
getRenderStates().blendStates[size_t(BlendMode::None)].handle);
230 const uint32_t strides[] = {
sizeof(glm::vec3) };
231 context->setVertexBuffers(&vertices, 1, strides,
nullptr);
232 context->setIndexBuffer(*edgeIndicesP, 2);
234 setConstants(taskContext, constantBuffer, M, color);
245 *defaultMaterialInstanceP = context->materialInstanceManager->createMaterialInstance(context->materialManager->getDefaultMaterial());
247 return *defaultMaterialInstanceP;
252 setupUnitVertices(taskContext->device);
253 drawWireBox(taskContext, drawContext, *unitVerticesP, *constantBufferP, M, colors[color % 6]);
258 setupOneOneVertices(taskContext->device);
259 drawWireBox(taskContext, drawContext, *oneOneVerticesP, *constantBufferP, M, colors[color % 6]);
264 auto * renderer = taskContext->renderer;
265 auto * device = taskContext->device;
267 const auto * camData = drawContext->cameraData;
269 setupCoordSysVertices(taskContext->device);
271 context->setEffect(*effectHandleP);
272 context->setInputLayout(*inputLayoutHandleP);
275 context->setDepthStencilState(drawContext->renderer->
getRenderStates().noTestDepthStencilStateHandle);
278 bool useCamData = 2.f < std::min(camData->viewportSize.x, camData->viewportSize.y);
279 auto w = useCamData ? camData->viewportSize.x : (drawContext->renderTarget ? drawContext->renderTarget->width : renderer->getSize().x);
280 auto h = useCamData ? camData->viewportSize.y : (drawContext->renderTarget ? drawContext->renderTarget->height : renderer->getSize().y);
281 context->setViewport(camData->viewportOrigin.x, camData->viewportOrigin.y, w, h);
282 context->setDepthStencilState(drawContext->renderer->
getRenderStates().defaultDepthStencilStateHandle);
286 context->setBlendState(drawContext->renderer->
getRenderStates().blendStates[size_t(BlendMode::None)].handle);
287 const uint32_t strides[] = {
sizeof(glm::vec3) };
288 context->setVertexBuffers(coordSysVerticesP, 1, strides,
nullptr);
290 glm::vec4 colors3[3] = {
295 setConstants(taskContext, *constantBufferP, M, colors3[0]);
297 setConstants(taskContext, *constantBufferP, M, colors3[1]);
299 setConstants(taskContext, *constantBufferP, M, colors3[2]);
307 desc.name =
"DebugEffect";
312 desc.vertexShader =
"Engine/DebugVS.wgsl";
313 desc.pixelShader =
"Engine/DebugPS.wgsl";
314 desc.vsEntryPoint =
"vs_main";
315 desc.psEntryPoint =
"fs_main";
319 desc.vertexShader =
"Engine/DebugVS.es30.glsl";
320 desc.pixelShader =
"Engine/DebugPS.es30.glsl";
324 desc.vertexShader =
"Engine/DebugVS.hlsl";
325 desc.pixelShader =
"Engine/DebugPS.hlsl";
333 auto vertexFormat = buffers->createVertexFormat(elements, 1);
334 inputLayoutHandle = buffers->loadInputLayout(&vertexFormat, 1, effectHandle);
339 effectHandleP = &effectHandle;
340 inputLayoutHandleP = &inputLayoutHandle;
341 constantBufferP = &constantBuffer;
342 coordSysVerticesP = &coordSysVertices;
343 unitVerticesP = &unitVertices;
344 oneOneVerticesP = &oneOneVertices;
345 edgeIndicesP = &edgeIndices;
346 defaultMaterialInstanceP = &defaultMaterialInstance;
348void DebugGeometryRenderer::cleanup()
358 effectHandleP =
nullptr;
359 inputLayoutHandleP =
nullptr;
360 constantBufferP =
nullptr;
361 coordSysVerticesP =
nullptr;
362 unitVerticesP =
nullptr;
363 oneOneVerticesP =
nullptr;
364 edgeIndicesP =
nullptr;
365 defaultMaterialInstanceP =
nullptr;
A Context instance contains all the services, systems and runtime components needed to use Cogs.
RenderStates & getRenderStates() override
Get the reference to the RenderStates structure.
EffectFlags::EEffectFlags getEffectFlags() const override
Get the EffectFlags.
Represents a graphics device used to manage graphics resources and issue drawing commands.
virtual IEffects * getEffects()=0
Get a pointer to the effect management interface.
virtual IContext * getImmediateContext()=0
Get a pointer to the immediate context used to issue commands to the graphics device.
virtual IBuffers * getBuffers()=0
Get a pointer to the buffer management interface.
virtual GraphicsDeviceType getType() const
Get the type of the graphics device.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
bool HandleIsValid(const ResourceHandle_t< T > &handle)
Check if the given resource is valid, that is not equal to NoHandle or InvalidHandle.
Contains all Cogs related functionality.
@ OpenGLES30
Graphics device using the OpenGLES 3.0 API.
@ WebGPU
Graphics device using the WebGPU API Backend.
@ VertexData
Per vertex data.
@ Position
Position semantic.
@ Write
The buffer can be mapped and written to by the CPU after creation.
@ ConstantBuffer
The buffer can be bound as input to effects as a constant buffer.
static const ResourceHandle_t NoHandle
Handle representing a default (or none if default not present) resource.
Contains an effect description used to load a single effect.
EEffectFlags
Effect source flags.
@ WGSL
Effect source is WGSL.
@ GLSL
Effect source is GLSL.
static const Handle_t InvalidHandle
Represents an invalid handle.
virtual void annotate(BufferHandle handle, const StringView &name)
Associate a name with an object for use in graphics debugging.
virtual BufferHandle loadBuffer(const void *data, const size_t size, Usage::EUsage usage, uint32_t accessMode, uint32_t bindFlags, uint32_t stride=0)=0
Loads a new buffer using the given data to populate the buffer.
virtual EffectHandle loadEffect(const EffectDescription &description)=0
Load an effect from the given description.
@ WriteDiscard
Write access. When unmapping the graphics system will discard the old contents of the resource.
Provides RAII style mapping of a buffer resource.
static RasterizerState DefaultState()
Constructs a rasterizer state initialized with the default values.
@ Dynamic
Buffer will be loaded and modified with some frequency.
Vertex element structure used to describe a single data element in a vertex for the input assembler.