1#include "BoundsRenderer.h"
3#include "Rendering/ICapabilities.h"
4#include "Rendering/IContext.h"
5#include "Rendering/IBuffers.h"
9#include "Renderer/IRenderer.h"
11#include "Resources/VertexFormats.h"
12#include "Resources/DefaultMaterial.h"
13#include "Resources/Mesh.h"
15#include "Systems/Core/CameraSystem.h"
16#include "Systems/Core/TransformSystem.h"
18#include "Scene/GetBounds.h"
20#include <glm/vec3.hpp>
21#include <glm/vec4.hpp>
22#include <glm/mat4x4.hpp>
23#include <glm/ext/matrix_transform.hpp>
30 glm::mat4 projectionMatrix;
32 glm::mat4 worldMatrix;
33 glm::vec4 diffuseColor;
36 const uint16_t indexes[] = {
46 const glm::vec3 positions[] = {
47 glm::vec3(-1, -1, -1),
59void Cogs::Core::BoundsRenderer::initialize(
class IRenderer* renderer,
IGraphicsDevice * device)
64 desc.name =
"DebugEffect";
65 desc.flags = renderer->getEffectFlags();
69 desc.vertexShader =
"Engine/DebugVS.wgsl";
70 desc.pixelShader =
"Engine/DebugPS.wgsl";
71 desc.vsEntryPoint =
"vs_main";
72 desc.psEntryPoint =
"fs_main";
76 desc.vertexShader =
"Engine/DebugVS.es30.glsl";
77 desc.pixelShader =
"Engine/DebugPS.es30.glsl";
81 desc.vertexShader =
"Engine/DebugVS.hlsl";
82 desc.pixelShader =
"Engine/DebugPS.hlsl";
90 auto vertexFormat = buffers->createVertexFormat(elements, 1);
91 inputLayoutHandle = buffers->loadInputLayout(&vertexFormat, 1, effectHandle);
96void Cogs::Core::BoundsRenderer::renderBounds(
class IRenderer * renderer,
class Context* mainContext,
const RenderList & renderList)
98 auto device = renderer->getDevice();
100 auto & renderStates = renderer->getRenderStates();
104 context->setRasterizerState(renderStates.defaultRasterizerStateHandle);
105 context->setDepthStencilState(renderStates.noWriteDepthStencilStateHandle);
106 context->setBlendState(renderStates.blendStates[
size_t(
BlendMode::None)].handle);
111 context->setInputLayout(inputLayoutHandle);
112 context->setIndexBuffer(ib, 2);
113 uint32_t strides[] = {
sizeof(glm::vec3) };
114 context->setVertexBuffers(&vb, 1, strides,
nullptr);
116 auto & cameraData = mainContext->cameraSystem->getMainCameraData();
118 for (
auto & renderItem : renderList) {
119 if (renderItem.layer == RenderLayers::Sky)
continue;
120 if (renderItem.isSprite())
continue;
122 Geometry::BoundingBox bbox;
124 if (renderItem.hasBoundingBox()) {
125 bbox = *renderItem.boundingBox;
127 if (renderItem.cullingIndex ==
static_cast<uint32_t
>(-1))
continue;
129 assert(renderItem.cullingIndex < cameraData.cullingData->source->bbMinWorld.size() &&
"Bounds index out of range.");
131 cameraData.cullingData->source->bbMinWorld[renderItem.cullingIndex],
132 cameraData.cullingData->source->bbMaxWorld[renderItem.cullingIndex]
136 auto center = (bbox.max + bbox.min) * 0.5f;
137 auto scale = 0.5f * glm::vec3(
138 bbox.max.x - bbox.min.x,
139 bbox.max.y - bbox.min.y,
140 bbox.max.z - bbox.min.z);
142 glm::mat4 world = glm::translate(glm::mat4(1.0f), center) * glm::scale(glm::mat4(1.0f), scale);
143 glm::vec4 diffuseColor = glm::vec4(0.4f, 1.0f, 0.2f, 1.0f);
144 if (renderItem.materialInstance->isDefaultMaterial()) {
145 diffuseColor = renderItem.materialInstance->getVec4Property(DefaultMaterial::DiffuseColor);
146 diffuseColor.a = 1.0f;
154 constants->projectionMatrix = cameraData.projectionMatrix;
155 constants->viewMatrix = cameraData.viewMatrix;
156 constants->worldMatrix = world;
157 constants->diffuseColor = diffuseColor;
160 context->setConstantBuffer(
"DebugBuffer", constantBuffer);
168 if (renderItem.isInstanced() && renderItem.numInstances < 10000) {
169 auto instanceMesh = renderItem.instanceData;
170 const auto & instanceBounds = renderItem.meshData->getMesh()->boundingBox;
176 if (!isEmpty(instanceBounds) && pPos && pCount >= renderItem.numInstances) {
177 auto instanceCenter = (instanceBounds.min + instanceBounds.max) * 0.5f;
178 auto instanceScale = 0.5f * glm::vec3(
179 instanceBounds.max.x - instanceBounds.min.x,
180 instanceBounds.max.y - instanceBounds.min.y,
181 instanceBounds.max.z - instanceBounds.min.z);
183 const auto worldMatrix = renderItem.worldMatrix ? *renderItem.worldMatrix : glm::mat4(1.0f);
185 for (
size_t i = 0; i < renderItem.numInstances; ++i) {
186 const glm::vec3 & instancePosition = *
reinterpret_cast<const glm::vec3 *
>(pPos + pStride * (renderItem.startInstance + i));
187 const glm::mat4 instanceTransform = worldMatrix * glm::translate(glm::mat4(1.0f), instanceCenter + instancePosition) * glm::scale(glm::mat4(1.0f), instanceScale);
190 if (cColor && cCount >= renderItem.numInstances) {
191 const float *color =
reinterpret_cast<const float *
>(cColor + cStride * i);
192 col = glm::vec4(color[0], color[1], color[2], color[3]);
202 constants->projectionMatrix = cameraData.projectionMatrix;
203 constants->viewMatrix = cameraData.viewMatrix;
204 constants->worldMatrix = instanceTransform;
205 constants->diffuseColor = col;
208 context->setConstantBuffer(
"DebugBuffer", constantBuffer);
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.
bool HandleIsValid(const ResourceHandle_t< T > &handle)
Check if the given resource is valid, that is not equal to NoHandle or InvalidHandle.
@ None
No blending enabled for opaque shapes, defaults to Blend for transparent shapes.
Contains geometry calculations and generation.
@ OpenGLES30
Graphics device using the OpenGLES 3.0 API.
@ WebGPU
Graphics device using the WebGPU API Backend.
@ VertexData
Per vertex data.
@ Position
Position semantic.
@ InstanceVector
Instance vector 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.
Contains an effect description used to load a single effect.
EEffectFlags
Effect source flags.
@ WGSL
Effect source is WGSL.
@ GLSL
Effect source is GLSL.
virtual void releaseVertexBuffer(VertexBufferHandle vertexBufferHandle)=0
Release the vertex buffer with the given handle.
virtual void annotate(BufferHandle handle, const StringView &name)
Associate a name with an object for use in graphics debugging.
virtual IndexBufferHandle loadIndexBuffer(const void *indexData, const size_t count, const size_t indexSize)=0
Loads a new index buffer and populates it with the given indexData.
virtual void releaseIndexBuffer(IndexBufferHandle indexBufferHandle)=0
Releases the index buffer with the given handle.
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 VertexBufferHandle loadVertexBuffer(const void *vertexData, const size_t count, const VertexFormat &vertexFormat)=0
Loads a new vertex buffer and populates it with the given data.
virtual void setEffect(EffectHandle handle)=0
Set the current effect.
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.
@ 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.