1#include "Foundation/Logging/Logger.h"
3#include "Rendering/IBuffers.h"
4#include "Rendering/ICapabilities.h"
5#include "Rendering/IContext.h"
6#include "Rendering/IEffects.h"
7#include "Rendering/IGraphicsDevice.h"
8#include "Rendering/IRenderTargets.h"
10#include "Components/Core/ClipShapeComponent.h"
12#include "Resources/MaterialInstance.h"
14#include "Systems/Core/CameraSystem.h"
16#include "Renderer/Renderer.h"
17#include "Renderer/Tasks/RenderTask.h"
18#include "Renderer/Tasks/FilterListTask.h"
19#include "Renderer/RenderTarget.h"
20#include "Renderer/RenderStateUpdater.h"
22#include "HighlightRegionSystem.h"
23#include "HighlightRegionRenderer.h"
30 glm::mat4 clipFromLocal;
31 glm::mat4 localFromClip;
33 glm::vec4 nearPlaneLocal;
34 glm::vec4 viewPlaneLocal;
39 if (key == NoProperty)
return;
43 deviceContext->
setTexture(texBinding, texture->textureHandle);
50 if (
HandleIsValid(renderMaterialInstance->samplerStates[key])) {
51 deviceContext->
setSamplerState(samplerBinding, renderMaterialInstance->samplerStates[key]);
62 uint32_t packSplitVertex4(
size_t a,
size_t b,
size_t c,
size_t d)
64 return static_cast<uint32_t
>(((d) << 9) | ((c ) << 6) | ((b ) << 3) | ((a ) << 0));
67 uint32_t packSplitVertex2(
size_t a,
size_t b)
69 return packSplitVertex4(a, a, b, b);
72 uint32_t packSplitVertex1(
size_t a)
74 return packSplitVertex4(a, a, a, a);
83 LOG_WARNING_ONCE(logger,
"HighlightRegionRenderer only supports OpenGLES30 or WebGPU currently");
90 VertexFormatHandle vertexFormatHandle = VertexFormats::createVertexFormat(vertexElements, std::size(vertexElements));
95 VertexFormatHandle splitCubeVertexFormatHandle = VertexFormats::createVertexFormat(splitCubeVertexElements, std::size(splitCubeVertexElements));
102 VertexFormatHandle instanceFormatHandle = VertexFormats::createVertexFormat(instanceElements, std::size(instanceElements));
110 splitCubeStreamsLayout.
vertexFormats[0] = splitCubeVertexFormatHandle;
111 splitCubeStreamsLayout.
vertexFormats[1] = instanceFormatHandle;
118 static const float cubeVertexData[8][3] = {
128 static_assert(std::size(cubeVertexData) == 8);
131 static const uint16_t cubeTriIndicesData[] = {
139 cubeTriIndicesCount =
static_cast<uint32_t
>(std::size(cubeTriIndicesData));
140 cubeTriIndices = buffers->
loadIndexBuffer(cubeTriIndicesData, cubeTriIndicesCount,
sizeof(cubeTriIndicesData[0]));
144 static const uint32_t splitCubeVertexData[26] = {
146 packSplitVertex2(0, 1),
148 packSplitVertex2(0, 2),
149 packSplitVertex4(0, 2, 3, 1),
150 packSplitVertex2(1, 3),
152 packSplitVertex2(2, 3),
155 packSplitVertex2(0, 4),
156 packSplitVertex4(0, 1, 5, 4),
157 packSplitVertex2(1, 5),
158 packSplitVertex4(0, 4, 6, 2),
159 packSplitVertex4(1, 3, 7, 5),
160 packSplitVertex2(2, 6),
161 packSplitVertex4(2, 6, 7, 3),
162 packSplitVertex2(3, 7),
165 packSplitVertex2(4, 5),
167 packSplitVertex2(4, 6),
168 packSplitVertex4(4, 5, 7, 6),
169 packSplitVertex2(5, 7),
171 packSplitVertex2(6, 7),
176 static const uint16_t splitCubeTriIndicesData[] = {
182 18, 20, 17, 21, 20, 18,
183 20, 24, 23, 21, 24, 20,
184 24, 22, 25, 21, 22, 24,
185 22, 18, 19, 21, 18, 22,
189 14, 20, 23, 12, 20, 14,
190 20, 9, 17, 12, 9, 20,
194 22, 16, 25, 13, 16, 22,
195 11, 22, 19, 13, 22, 11,
199 16, 24, 25, 15, 24, 16,
200 24, 14, 23, 15, 14, 24,
204 18, 11, 19, 10, 11, 18,
207 splitCubeTriIndicesCount =
static_cast<uint32_t
>(std::size(splitCubeTriIndicesData));
208 splitCubeTriIndices = buffers->
loadIndexBuffer(splitCubeTriIndicesData, splitCubeTriIndicesCount,
sizeof(splitCubeTriIndicesData[0]));
232 if (matrices.index == NoProperty && hrSystem->material && hrSystem->material->isActive()) {
234 if (buffer.
name ==
"Matrices" &&
sizeof(Matrices) <= buffer.
size) {
235 LOG_DEBUG(logger,
"Found Matrices buffer: %u (size=%zu)", buffer.
index, buffer.
size);
236 matrices.index = buffer.
index;
244 RenderResources& renderResources = renderingContext->renderer->getRenderResources();
250 if (data.materialInstance && data.materialInstance->isActive()) {
251 data.renderMaterialInstance = renderResources.getRenderMaterialInstance(data.materialInstance);
258 data.instanceDataUpdated =
true;
262 if (data.instanceDataUpdated) {
263 data.instanceDataUpdated =
false;
268 if (!data.instanceData.empty()) {
269 data.instanceDataHandle = buffers->
loadVertexBuffer(data.instanceData.data(), data.instanceData.size(), solidStreamsLayout.vertexFormats[1]);
290 const CameraData* camData = renderList->viewportData;
291 if (!camData)
return;
293 size_t permutationIndex = 0;
295 if (!permutation)
return;
298 drawContext.context = renderTaskContext->context;
299 drawContext.renderer = renderTaskContext->renderer;
300 drawContext.device = drawContext.renderer->
getDevice();
302 drawContext.cameraData = renderList->viewportData;
303 drawContext.permutation = permutation;
305 drawContext.taskContext = renderTaskContext;
306 drawContext.renderTarget = renderTarget;
308 if (drawContext.permutation ==
nullptr)
return;
310 IContext* deviceContext = drawContext.deviceContext;
311 bool reverseDepth = drawContext.taskContext->states->reverseDepth;
317 info.renderTargetHandle = renderTarget->renderTargetHandle;
318 info.depthStencilHandle = renderTarget->depthTargetHandle;
321 info.loadOp[i] = LoadOp::Load;
322 info.storeOp[i] = StoreOp::Store;
324 info.depthLoadOp = LoadOp::Load;
325 info.depthStoreOp = StoreOp::Store;
330 deviceContext->
setRenderTarget(renderTarget->renderTargetHandle, renderTarget->depthTargetHandle);
335 deviceContext->
setViewport(0.f, 0.f,
float(drawContext.renderTarget->width),
float(drawContext.renderTarget->height));
338 deviceContext->
setViewport(camData->viewportOrigin.x, camData->viewportOrigin.y, camData->viewportSize.x, camData->viewportSize.y);
342 updateViewportBuffer(renderTaskContext, renderTaskContext->engineBuffers->sceneBufferHandle, renderTaskContext->engineBuffers->viewBufferHandle, renderTarget, camData,
nullptr);
345 ? noWriteDepthStencilStateReverse
346 : noWriteDepthStencilState);
350 if (!
HandleIsValid(data.instanceDataHandle) || !data.renderMaterialInstance)
continue;
352 MaterialInstance* materialInstance = data.renderMaterialInstance->getResource();
353 assert(materialInstance);
360 colorKey = texProp.key;
363 depthKey = texProp.key;
370 RenderPassOptions passOptions = initRenderPassOptions(*camData, materialInstance);
373 passOptions.setFlag(RenderPassOptions::Flags::NoDepthClip);
377 deviceContext->
setRasterizerState(drawContext.taskContext->states->rasterizerStateHandles[rasterizerState]);
382 if (data.debugForceNoSplit) {
383 effectBinding = data.renderMaterialInstance->checkReady(solidStreamsLayout, drawContext.permutation, passOptions, clipShape);
386 effectBinding = data.renderMaterialInstance->checkReady(splitCubeStreamsLayout, drawContext.permutation, passOptions, clipShape);
388 if (!effectBinding)
continue;
392 permutation->bufferHandles.resize(permutation->constantBuffers.
buffers.size());
401 applyMaterialPermutation(drawContext.taskContext, &drawContext, effectBinding, data.renderMaterialInstance);
402 applyMaterialInstance(&drawContext, effectBinding, data.renderMaterialInstance);
403 updateEnvironmentBindings(&drawContext, effectBinding);
404 updateSceneBindings(drawContext.taskContext, effectBinding);
405 updateMaterialBindings(&drawContext, data.renderMaterialInstance, effectBinding,
true);
407 setTexture(deviceContext, data.renderMaterialInstance, effectBinding, color, colorKey);
408 setTexture(deviceContext, data.renderMaterialInstance, effectBinding, depth, depthKey);
410 if (
HandleIsValid(matrices.buffer) && matrices.index < effectBinding->buffers.size()) {
413 glm::dmat4 viewMatrix = camData->viewMatrix;
414 glm::dmat4 viewFromLocal = viewMatrix * glm::dmat4(data.worldMatrix);
415 glm::dmat4 clipFromLocal = glm::dmat4(camData->projectionMatrix) * viewFromLocal;
416 glm::dmat4 localFromClip = glm::inverse(clipFromLocal);
419 matricesBuffer->clipFromLocal = clipFromLocal;
420 matricesBuffer->localFromClip = localFromClip;
421 matricesBuffer->eye = glm::inverse(viewFromLocal) * glm::vec4(0,0,0,1);
422 matricesBuffer->nearPlaneLocal = glm::vec4(0, 0, -1, -camData->nearDistance) * viewFromLocal;
423 matricesBuffer->viewPlaneLocal = glm::vec4(0, 0, -1, 0) * viewFromLocal;
429 if (data.debugForceNoSplit) {
432 data.instanceDataHandle
441 data.instanceDataHandle
A Context instance contains all the services, systems and runtime components needed to use Cogs.
float depthConstantBias
Pull highlight frontface slightly towards the camera with a constant.
float depthSlopedFactor
Pull highlight frontface slightly towards the camera based on surface slope.
constexpr bool isVisible() const
Check if the entity is visible or not.
Contains render resources used by the renderer.
void updatePermutation(EnginePermutation *permutation) override
Updates the EnginePermutation.
EngineBuffers & getEngineBuffers() override
Get the reference to the EngineBuffers structure.
RenderStates & getRenderStates() override
Get the reference to the RenderStates structure.
IGraphicsDevice * getDevice() override
Get the graphics device used by the renderer.
EnginePermutations & getEnginePermutations() override
Get the reference to the EnginePermutations structure.
Represents a graphics device used to manage graphics resources and issue drawing commands.
virtual ICapabilities * getCapabilities()=0
Get a pointer to the capability management interface used to query the graphics device capability fla...
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.
virtual IRenderTargets * getRenderTargets()=0
Get a pointer to the render target management interface.
Log implementation class.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
ClipShapeType
Specifices what kind of shape a clip shape has.
@ None
No clipping at all.
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.
uint16_t VariableKey
Used to lookup material properties.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
@ OpenGLES30
Graphics device using the OpenGLES 3.0 API.
@ WebGPU
Graphics device using the WebGPU API Backend.
constexpr size_t hash() noexcept
Simple getter function that returns the initial value for fnv1a hashing.
@ InstanceData
Per instance data.
@ VertexData
Per vertex data.
@ TriangleList
List of triangles.
@ Position
Position semantic.
@ InstanceMatrix
Instance matrix 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 data describing a Camera instance and its derived data structured such as matrix data and vi...
std::vector< MaterialPropertyBuffer > buffers
Constant buffer instances.
void initialize(Context *context, IGraphicsDevice *device) override
Initialize the extension using the given context and device.
void handleEvent(uint32_t eventId, const DrawContext *renderingContext) override
Called when rendering events occur.
Material instances represent a specialized Material combined with state for all its buffers and prope...
std::vector< TextureValue > textureVariables
Texture property values for this instance.
Material property buffers contain properties for a material, such as color, reflectiveness or other v...
ConstantBufferKey index
Index of the buffer in the set of buffers owned by the same Material.
size_t size
Total size of the buffer in bytes.
PropertyName name
Name used to refer to the buffer in shaders.
std::vector< uint8_t > content
Default content for property buffer instances created from this buffer.
PropertyName name
Name of the property, used to reference named uniforms of constant buffer members in shaders.
VertexFormatHandle vertexFormats[maxStreams]
COGSCORE_DLL_API void updateHash()
COGSCORE_DLL_API void updateHash()
@ PreRender
Pre rendering happening for a given rendering context.
const TextureProperty * property
Pointer to the property this value is for.
Encapsulates state for depth buffer usage and stencil buffer usage in a state object.
@ GreaterOrEqual
Greater or equal depth.
@ LessOrEqual
Less or equal depth.
DepthFunction depthFunction
The depth function to use for depth testing.
static const Handle_t NoHandle
Represents a handle to nothing.
Provides buffer management functionality.
virtual void releaseVertexBuffer(VertexBufferHandle vertexBufferHandle)=0
Release the vertex buffer with the given handle.
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 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 const GraphicsDeviceCapabilities & getDeviceCapabilities() const
Gets the device capabilities in a structure.
Represents a graphics device context which can receive rendering commands.
virtual void setTexture(const StringView &name, unsigned int unit, TextureHandle textureHandle)=0
Sets the texture slot given by unit with the given name to contain the given texture.
virtual void setRasterizerState(const RasterizerStateHandle handle)=0
Set the current rasterizer state.
virtual void setBlendState(const BlendStateHandle handle, const float *constant=nullptr)=0
Set the current blend state.
virtual void drawInstancedIndexed(PrimitiveType primitiveType, const size_t startInstance, const size_t numInstances, const size_t startIndex, const size_t numIndexes)=0
Draws indexed, instanced primitives.
virtual void endRenderPass()=0
End a render pass.
virtual void setIndexBuffer(IndexBufferHandle bufferHandle, uint32_t stride=4, uint32_t offset=0)=0
Sets the current index buffer.
virtual void setDepthStencilState(const DepthStencilStateHandle handle)=0
Set the current depth stencil state.
virtual void setConstantBuffer(const StringView &name, const BufferHandle bufferHandle, const uint32_t offset=0, const uint32_t size=~0u)=0
Sets a constant buffer to be bound to the given name and slot.
virtual void updateBuffer(BufferHandle bufferHandle, const void *data, size_t size)=0
Replace contents of buffer with new data.
virtual void setViewport(const float x, const float y, const float width, const float height)=0
Sets the current viewport to the given location and dimensions.
virtual void beginRenderPass(const RenderPassInfo &info)=0
Begin a render pass.
virtual void setSamplerState(const StringView &name, unsigned int unit, SamplerStateHandle samplerStateHandle)=0
Sets the sampler slot given by unit with the given name to contain the given sampler state.
virtual void setVertexBuffers(const VertexBufferHandle *vertexBufferHandles, const size_t count, const uint32_t *strides, const uint32_t *offsets)=0
Sets the current vertex buffers.
virtual void setRenderTarget(const RenderTargetHandle handle, const DepthStencilHandle depthStencilHandle)=0
Sets the current render target and an associated depth stencil target.
Provides render target management functionality.
virtual DepthStencilStateHandle loadDepthStencilState(const DepthStencilState &depthStencilState)=0
Load a depth stencil state object.
@ WriteDiscard
Write access. When unmapping the graphics system will discard the old contents of the resource.
Provides RAII style mapping of a buffer resource.
@ Back
Cull back facing primitives.
@ 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.