1#include "Foundation/Logging/Logger.h"
3#include "Rendering/IGraphicsDevice.h"
4#include "Rendering/IContext.h"
5#include "Rendering/IEffects.h"
6#include "Rendering/IBuffers.h"
7#include "Rendering/IRenderTargets.h"
9#include "Components/Core/ClipShapeComponent.h"
11#include "Resources/MaterialInstance.h"
13#include "Systems/Core/CameraSystem.h"
15#include "Renderer/Renderer.h"
16#include "Renderer/Tasks/RenderTask.h"
17#include "Renderer/Tasks/FilterListTask.h"
18#include "Renderer/RenderTarget.h"
19#include "Renderer/RenderStateUpdater.h"
21#include "HighlightRegionSystem.h"
22#include "HighlightRegionRenderer.h"
29 glm::mat4 clipFromLocal;
30 glm::mat4 localFromClip;
32 glm::vec4 nearPlaneLocal;
33 glm::vec4 viewPlaneLocal;
38 if (key == NoProperty)
return;
42 deviceContext->
setTexture(texBinding, texture->textureHandle);
49 if (
HandleIsValid(renderMaterialInstance->samplerStates[key])) {
50 deviceContext->
setSamplerState(samplerBinding, renderMaterialInstance->samplerStates[key]);
61 uint32_t packSplitVertex4(
size_t a,
size_t b,
size_t c,
size_t d)
63 return static_cast<uint32_t
>(((d) << 9) | ((c ) << 6) | ((b ) << 3) | ((a ) << 0));
66 uint32_t packSplitVertex2(
size_t a,
size_t b)
68 return packSplitVertex4(a, a, b, b);
71 uint32_t packSplitVertex1(
size_t a)
73 return packSplitVertex4(a, a, a, a);
81 LOG_WARNING_ONCE(logger,
"HighlightRegionRenderer only supports OpenGLES30 currently");
88 VertexFormatHandle vertexFormatHandle = VertexFormats::createVertexFormat(vertexElements, std::size(vertexElements));
93 VertexFormatHandle splitCubeVertexFormatHandle = VertexFormats::createVertexFormat(splitCubeVertexElements, std::size(splitCubeVertexElements));
100 VertexFormatHandle instanceFormatHandle = VertexFormats::createVertexFormat(instanceElements, std::size(instanceElements));
108 splitCubeStreamsLayout.
vertexFormats[0] = splitCubeVertexFormatHandle;
109 splitCubeStreamsLayout.
vertexFormats[1] = instanceFormatHandle;
116 static const float cubeVertexData[8][3] = {
126 static_assert(std::size(cubeVertexData) == 8);
129 static const uint16_t cubeTriIndicesData[] = {
137 cubeTriIndicesCount =
static_cast<uint32_t
>(std::size(cubeTriIndicesData));
138 cubeTriIndices = buffers->
loadIndexBuffer(cubeTriIndicesData, cubeTriIndicesCount,
sizeof(cubeTriIndicesData[0]));
142 static const uint32_t splitCubeVertexData[26] = {
144 packSplitVertex2(0, 1),
146 packSplitVertex2(0, 2),
147 packSplitVertex4(0, 2, 3, 1),
148 packSplitVertex2(1, 3),
150 packSplitVertex2(2, 3),
153 packSplitVertex2(0, 4),
154 packSplitVertex4(0, 1, 5, 4),
155 packSplitVertex2(1, 5),
156 packSplitVertex4(0, 4, 6, 2),
157 packSplitVertex4(1, 3, 7, 5),
158 packSplitVertex2(2, 6),
159 packSplitVertex4(2, 6, 7, 3),
160 packSplitVertex2(3, 7),
163 packSplitVertex2(4, 5),
165 packSplitVertex2(4, 6),
166 packSplitVertex4(4, 5, 7, 6),
167 packSplitVertex2(5, 7),
169 packSplitVertex2(6, 7),
174 static const uint16_t splitCubeTriIndicesData[] = {
180 18, 20, 17, 21, 20, 18,
181 20, 24, 23, 21, 24, 20,
182 24, 22, 25, 21, 22, 24,
183 22, 18, 19, 21, 18, 22,
187 14, 20, 23, 12, 20, 14,
188 20, 9, 17, 12, 9, 20,
192 22, 16, 25, 13, 16, 22,
193 11, 22, 19, 13, 22, 11,
197 16, 24, 25, 15, 24, 16,
198 24, 14, 23, 15, 14, 24,
202 18, 11, 19, 10, 11, 18,
205 splitCubeTriIndicesCount =
static_cast<uint32_t
>(std::size(splitCubeTriIndicesData));
206 splitCubeTriIndices = buffers->
loadIndexBuffer(splitCubeTriIndicesData, splitCubeTriIndicesCount,
sizeof(splitCubeTriIndicesData[0]));
229 if (matrices.index == NoProperty && hrSystem->material && hrSystem->material->isActive()) {
231 if (buffer.
name ==
"Matrices" &&
sizeof(Matrices) <= buffer.
size) {
232 LOG_DEBUG(logger,
"Found Matrices buffer: %u (size=%zu)", buffer.
index, buffer.
size);
233 matrices.index = buffer.
index;
241 RenderResources& renderResources = renderingContext->renderer->getRenderResources();
247 if (data.materialInstance && data.materialInstance->isActive()) {
248 data.renderMaterialInstance = renderResources.getRenderMaterialInstance(data.materialInstance);
255 data.instanceDataUpdated =
true;
259 if (data.instanceDataUpdated) {
260 data.instanceDataUpdated =
false;
265 if (!data.instanceData.empty()) {
266 data.instanceDataHandle = buffers->
loadVertexBuffer(data.instanceData.data(), data.instanceData.size(), solidStreamsLayout.vertexFormats[1]);
286 const CameraData* camData = renderList->viewportData;
287 if (!camData)
return;
289 size_t permutationIndex = 0;
291 if (!permutation)
return;
294 drawContext.context = renderTaskContext->context;
295 drawContext.renderer = renderTaskContext->renderer;
296 drawContext.device = drawContext.renderer->
getDevice();
298 drawContext.cameraData = renderList->viewportData;
299 drawContext.permutation = permutation;
301 drawContext.taskContext = renderTaskContext;
302 drawContext.renderTarget = renderTarget;
304 if (drawContext.permutation ==
nullptr)
return;
306 IContext* deviceContext = drawContext.deviceContext;
307 bool reverseDepth = drawContext.taskContext->states->reverseDepth;
314 deviceContext->
setRenderTarget(renderTarget->renderTargetHandle, renderTarget->depthTargetHandle);
318 deviceContext->
setViewport(0.f, 0.f,
float(drawContext.renderTarget->width),
float(drawContext.renderTarget->height));
321 deviceContext->
setViewport(camData->viewportOrigin.x, camData->viewportOrigin.y, camData->viewportSize.x, camData->viewportSize.y);
325 updateViewportBuffer(renderTaskContext, renderTaskContext->engineBuffers->sceneBufferHandle, renderTaskContext->engineBuffers->viewBufferHandle, renderTarget, camData,
nullptr);
328 ? noWriteDepthStencilStateReverse
329 : noWriteDepthStencilState);
332 if (!
HandleIsValid(data.instanceDataHandle) || !data.renderMaterialInstance)
continue;
334 MaterialInstance* materialInstance = data.renderMaterialInstance->getResource();
335 assert(materialInstance);
342 colorKey = texProp.key;
345 depthKey = texProp.key;
352 RenderPassOptions passOptions = initRenderPassOptions(*camData, materialInstance);
355 passOptions.setFlag(RenderPassOptions::Flags::NoDepthClip);
359 deviceContext->
setRasterizerState(drawContext.taskContext->states->rasterizerStateHandles[rasterizerState]);
364 if (data.debugForceNoSplit) {
365 effectBinding = data.renderMaterialInstance->checkReady(solidStreamsLayout, drawContext.permutation, passOptions, clipShape);
368 effectBinding = data.renderMaterialInstance->checkReady(splitCubeStreamsLayout, drawContext.permutation, passOptions, clipShape);
370 if (!effectBinding)
continue;
374 permutation->bufferHandles.resize(permutation->constantBuffers.
buffers.size());
383 applyMaterialPermutation(drawContext.taskContext, &drawContext, effectBinding, data.renderMaterialInstance);
384 applyMaterialInstance(&drawContext, effectBinding, data.renderMaterialInstance);
385 updateEnvironmentBindings(&drawContext, effectBinding);
386 updateSceneBindings(drawContext.taskContext, effectBinding);
387 updateMaterialBindings(&drawContext, data.renderMaterialInstance, effectBinding,
true);
389 setTexture(deviceContext, data.renderMaterialInstance, effectBinding, color, colorKey);
390 setTexture(deviceContext, data.renderMaterialInstance, effectBinding, depth, depthKey);
392 if (
HandleIsValid(matrices.buffer) && matrices.index < effectBinding->buffers.size()) {
395 glm::dmat4 inverseView = camData->inverseViewMatrix;
396 glm::dmat4 viewMatrix = glm::inverse(inverseView);
397 glm::dmat4 viewFromLocal = viewMatrix * glm::dmat4(data.worldMatrix);
398 glm::dmat4 clipFromLocal = glm::dmat4(camData->projectionMatrix) * viewFromLocal;
399 glm::dmat4 localFromClip = glm::inverse(clipFromLocal);
402 matricesBuffer->clipFromLocal = clipFromLocal;
403 matricesBuffer->localFromClip = localFromClip;
404 matricesBuffer->eye = glm::inverse(viewFromLocal) * glm::vec4(0,0,0,1);
405 matricesBuffer->nearPlaneLocal = glm::vec4(0, 0, -1, -camData->nearDistance) * viewFromLocal;
406 matricesBuffer->viewPlaneLocal = glm::vec4(0, 0, -1, 0) * viewFromLocal;
412 if (data.debugForceNoSplit) {
415 data.instanceDataHandle
424 data.instanceDataHandle
A Context instance contains all the services, systems and runtime components needed to use Cogs.
class IRenderer * renderer
Renderer.
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.
virtual RenderStates & getRenderStates()=0
Get the reference to the RenderStates structure.
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 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.
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.
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 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 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.