1#include <glm/gtc/type_ptr.hpp>
3#include "Rendering/ICapabilities.h"
4#include "Rendering/IGraphicsDevice.h"
5#include "Rendering/IBuffers.h"
6#include "Rendering/IEffects.h"
8#include "Foundation/Logging/Logger.h"
10#include "Renderer/RenderList.h"
11#include "Renderer/Tasks/RenderTask.h"
12#include "Renderer/RenderStateUpdater.h"
13#include "Renderer/RenderTarget.h"
14#include "Renderer/Renderer.h"
16#include "Resources/VertexFormats.h"
18#include "Resources/MaterialManager.h"
20#include "DebugGeometryRenderers.h"
30 glm::mat4 projectionMatrix;
32 glm::mat4 worldMatrix;
33 glm::vec4 diffuseColor;
42 const glm::mat4 identityMatrix = glm::mat4();
44 const glm::vec3 coordSysData[] = {
58 *coordSysVerticesP = buffers->loadVertexBuffer(coordSysData,
sizeof(coordSysData) /
sizeof(glm::vec3), Cogs::Core::VertexFormats::Pos3f);
59 buffers->annotate(*coordSysVerticesP,
"CoordSys vertices");
62 const glm::vec3 unitVerticesData[] = {
79 *unitVerticesP = buffers->loadVertexBuffer(unitVerticesData,
sizeof(unitVerticesData) /
sizeof(glm::vec3), Cogs::Core::VertexFormats::Pos3f);
80 buffers->annotate(*unitVerticesP,
"Unit box vertices");
84 const glm::vec3 oneOneVerticesData[] = {
85 glm::vec3(-1, -1, -1),
96 glm::vec3(-1 + 0.1f, 1 - 0.1f, -1),
97 glm::vec3(-1 + 0.3f, 1 - 0.1f, -1),
98 glm::vec3(-1 + 0.1f, 1 - 0.3f, -1),
99 glm::vec3(-1 + +.3f, 1 - 0.3f, -1),
102 glm::vec3(1 - 0.1f, 1 - 0.1f, 1),
103 glm::vec3(1 - 0.1f, 1 - 0.2f, 1),
104 glm::vec3(1 - 0.1f, 1 - 0.3f, 1),
105 glm::vec3(1 - 0.2f, 1 - 0.2f, 1),
106 glm::vec3(1 - 0.3f, 1 - 0.1f, 1),
109 glm::vec3(1, 1 - 0.1f, -1 + 0.1f),
110 glm::vec3(1, 1 - 0.3f, -1 + 0.1f),
111 glm::vec3(1, 1 - 0.1f, -1 + 0.3f),
112 glm::vec3(1, 1 - 0.3f, -1 + 0.3f),
114 glm::vec3(1, 1 - 0.2, -1 + 0.4f),
115 glm::vec3(1, 1 - 0.2, -1 + 0.6f),
116 glm::vec3(1, 1 - 0.1, -1 + 0.5f),
117 glm::vec3(1, 1 - 0.3, -1 + 0.5f),
120 glm::vec3(-1, 1 - 0.1f, 1 - 0.1f),
121 glm::vec3(-1, 1 - 0.3f, 1 - 0.1f),
122 glm::vec3(-1, 1 - 0.1f, 1 - 0.3f),
123 glm::vec3(-1, 1 - 0.3f, 1 - 0.3f),
124 glm::vec3(-1, 1 - 0.2, 1 - 0.4f),
125 glm::vec3(-1, 1 - 0.2, 1 - 0.6f),
128 glm::vec3(1 - 0.1f, 1, -1 + 0.1f),
129 glm::vec3(1 - 0.3f, 1, -1 + 0.1f),
130 glm::vec3(1 - 0.2f, 1, -1 + 0.2f),
131 glm::vec3(1 - 0.2f, 1, -1 + 0.3f),
133 glm::vec3(1 - 0.4f, 1, -1 + 0.2f),
134 glm::vec3(1 - 0.6f, 1, -1 + 0.2f),
135 glm::vec3(1 - 0.5f, 1, -1 + 0.1f),
136 glm::vec3(1 - 0.5f, 1, -1 + 0.3f),
139 glm::vec3(-1 + 0.1f, -1, -1 + 0.1f),
140 glm::vec3(-1 + 0.3f, -1, -1 + 0.1f),
141 glm::vec3(-1 + 0.2f, -1, -1 + 0.2f),
142 glm::vec3(-1 + 0.2f, -1, -1 + 0.3f),
144 glm::vec3(-1 + 0.4f, -1, -1 + 0.2f),
145 glm::vec3(-1 + 0.6f, -1, -1 + 0.2f),
154 *oneOneVerticesP = buffers->loadVertexBuffer(oneOneVerticesData,
sizeof(oneOneVerticesData) /
sizeof(glm::vec3), Cogs::Core::VertexFormats::Pos3f);
155 buffers->annotate(*oneOneVerticesP,
"One-one box vertices");
159 const uint16_t edgeIndicesData[] = {
169 12, 13, 13, 14, 13, 15, 12, 16,
170 17, 20, 18, 19, 21, 22, 23, 24,
171 25, 28, 26, 27, 29, 30,
172 31, 33, 33, 32, 33, 34, 35, 36, 37, 38,
173 39, 41, 41, 40, 41, 42, 43, 44
181 *edgeIndicesP = buffers->loadIndexBuffer(edgeIndicesData,
sizeof(edgeIndicesData) /
sizeof(uint16_t),
sizeof(uint16_t));
182 buffers->annotate(*edgeIndicesP,
"Box edge indices");
185 const glm::vec4 colors[6] =
187 glm::vec4(1.f, 0.f, 0.f, 1.f),
188 glm::vec4(0.f, 1.f, 0.f, 1.f),
189 glm::vec4(0.f, 0.f, 1.f, 1.f),
190 glm::vec4(0.f, 1.f, 1.f, 1.f),
191 glm::vec4(1.f, 1.f, 0.f, 1.f),
192 glm::vec4(1.f, 0.f, 1.f, 1.f),
197 auto * device = taskContext->device;
205 constants->projectionMatrix = M;
206 constants->viewMatrix = identityMatrix;
207 constants->worldMatrix = identityMatrix;
208 constants->diffuseColor = color;
211 context->setConstantBuffer(
"DebugBuffer", constantBuffer);
231 LOG_ERROR(logger,
"DebugGeometryRenderers: Invalid debug effect handle.");
234 *effectStatusP = status;
240 auto * renderer = taskContext->renderer;
241 auto * device = taskContext->device;
243 const auto * camData = drawContext->cameraData;
245 setupEdgeIndices(device);
247 if (!checkEffectReady(device))
return;
249 bool useCamData = 2.f < std::min(camData->viewportSize.x, camData->viewportSize.y);
251 context->setEffect(*effectHandleP);
252 context->setInputLayout(*inputLayoutHandleP);
254 auto w = useCamData ? camData->viewportSize.x : (drawContext->renderTarget ? drawContext->renderTarget->width : renderer->getSize().x);
255 auto h = useCamData ? camData->viewportSize.y : (drawContext->renderTarget ? drawContext->renderTarget->height : renderer->getSize().y);
256 context->setViewport(camData->viewportOrigin.x, camData->viewportOrigin.y, w, h);
258 context->setDepthStencilState(drawContext->renderer->
getRenderStates().commonDepthStates[RenderStates::defaultDepthStencilState]);
259 context->setBlendState(drawContext->renderer->
getRenderStates().blendStates[size_t(BlendMode::None)].handle);
260 const uint32_t strides[] = {
sizeof(glm::vec3) };
261 context->setVertexBuffers(&vertices, 1, strides,
nullptr);
262 context->setIndexBuffer(*edgeIndicesP, 2);
264 setConstants(taskContext, constantBuffer, M, color);
275 *defaultMaterialInstanceP = context->materialInstanceManager->createMaterialInstance(context->materialManager->getDefaultMaterial());
277 return *defaultMaterialInstanceP;
282 setupUnitVertices(taskContext->device);
283 drawWireBox(taskContext, drawContext, *unitVerticesP, *constantBufferP, M, colors[color % 6]);
288 setupOneOneVertices(taskContext->device);
289 drawWireBox(taskContext, drawContext, *oneOneVerticesP, *constantBufferP, M, colors[color % 6]);
294 auto * renderer = taskContext->renderer;
295 auto * device = taskContext->device;
297 const auto * camData = drawContext->cameraData;
299 setupCoordSysVertices(taskContext->device);
301 if (!checkEffectReady(device))
return;
303 context->setEffect(*effectHandleP);
304 context->setInputLayout(*inputLayoutHandleP);
307 context->setDepthStencilState(drawContext->renderer->
getRenderStates().commonDepthStates[RenderStates::noTestDepthStencilState]);
310 bool useCamData = 2.f < std::min(camData->viewportSize.x, camData->viewportSize.y);
311 auto w = useCamData ? camData->viewportSize.x : (drawContext->renderTarget ? drawContext->renderTarget->width : renderer->getSize().x);
312 auto h = useCamData ? camData->viewportSize.y : (drawContext->renderTarget ? drawContext->renderTarget->height : renderer->getSize().y);
313 context->setViewport(camData->viewportOrigin.x, camData->viewportOrigin.y, w, h);
314 context->setDepthStencilState(drawContext->renderer->
getRenderStates().commonDepthStates[RenderStates::defaultDepthStencilState]);
318 context->setBlendState(drawContext->renderer->
getRenderStates().blendStates[size_t(BlendMode::None)].handle);
319 const uint32_t strides[] = {
sizeof(glm::vec3) };
320 context->setVertexBuffers(coordSysVerticesP, 1, strides,
nullptr);
322 glm::vec4 colors3[3] = {
327 setConstants(taskContext, *constantBufferP, M, colors3[0]);
329 setConstants(taskContext, *constantBufferP, M, colors3[1]);
331 setConstants(taskContext, *constantBufferP, M, colors3[2]);
339 desc.name =
"DebugEffect";
344 desc.vertexShader =
"Engine/DebugVS.wgsl";
345 desc.pixelShader =
"Engine/DebugPS.wgsl";
346 desc.vsEntryPoint =
"vs_main";
347 desc.psEntryPoint =
"fs_main";
351 desc.vertexShader =
"Engine/DebugVS.es30.glsl";
352 desc.pixelShader =
"Engine/DebugPS.es30.glsl";
356 desc.vertexShader =
"Engine/DebugVS.hlsl";
357 desc.pixelShader =
"Engine/DebugPS.hlsl";
365 vertexFormat = buffers->createVertexFormat(elements, 1);
370 effectHandleP = &effectHandle;
371 inputLayoutHandleP = &inputLayoutHandle;
372 vertexFormatP = &vertexFormat;
373 effectStatusP = &effectStatus;
374 constantBufferP = &constantBuffer;
375 coordSysVerticesP = &coordSysVertices;
376 unitVerticesP = &unitVertices;
377 oneOneVerticesP = &oneOneVertices;
378 edgeIndicesP = &edgeIndices;
379 defaultMaterialInstanceP = &defaultMaterialInstance;
381void DebugGeometryRenderer::cleanup()
393 effectHandleP =
nullptr;
394 inputLayoutHandleP =
nullptr;
395 constantBufferP =
nullptr;
396 coordSysVerticesP =
nullptr;
397 unitVerticesP =
nullptr;
398 oneOneVerticesP =
nullptr;
399 edgeIndicesP =
nullptr;
400 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.
Log implementation class.
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.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Contains all Cogs related functionality.
@ OpenGLES30
Graphics device using the OpenGLES 3.0 API.
@ WebGPU
Graphics device using the WebGPU API Backend.
ResourceStatus
Status of an asynchronously loaded resource, such as an effect or a pipeline.
@ Pending
The resource is still loading.
@ Error
The resource failed to load.
@ Ready
The resource has loaded successfully and is ready for use.
@ 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 NoHandle
Represents a handle to nothing.
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 InputLayoutHandle loadInputLayout(const VertexFormatHandle *vertexFormats, const size_t count, EffectHandle effectHandle)=0
Loads a new input layout to map vertex flow between vertex buffers with the given vertexFormats to ef...
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.
Provides effects and shader management functionality.
virtual EffectHandle loadEffect(const EffectDescription &description)=0
Load an effect from the given description.
virtual ResourceStatus checkEffect(EffectHandle effectHandle)=0
Check the load status of the effect with the given effectHandle.
@ 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.