1#include "ExtensionRegistry.h"
2#include "Renderer/Renderer.h"
3#include "Renderer/RenderList.h"
4#include "Renderer/RenderStateUpdater.h"
5#include "Renderer/RenderTarget.h"
6#include "Renderer/Tasks/RenderListTask.h"
7#include "Renderer/Tasks/FilterListTask.h"
8#include "Renderer/Tasks/GenerateListTask.h"
9#include "Renderer/CustomRenderer/DebugGeometryRenderers.h"
11#include "PotreeRenderer.h"
12#include "PotreeSystem.h"
14#include "Rendering/IGraphicsDevice.h"
16#include "Foundation/Logging/Logger.h"
30 const auto* camData = drawContext->cameraData;
31 if (camData ==
nullptr)
return false;
34 float w = camData->viewportSize.x;
35 float h = camData->viewportSize.y;
36 if (drawContext->task->viewportFromTarget && drawContext->renderTarget) {
37 w = float(drawContext->renderTarget->width);
38 h = float(drawContext->renderTarget->height);
39 scale = 0.5f*(w / std::max(1.f, camData->viewportSize.x) + h / std::max(1.f, camData->viewportSize.y));
42 Renderer* renderer = taskContext->renderer;
47 iContext->
setViewport(camData->viewportOrigin.x, camData->viewportOrigin.y, w, h);
48 iContext->setDepthStencilState(taskContext->states->commonDepthStates[renderItem->depthState]);
49 iContext->setBlendState(taskContext->states->blendStates[renderItem->blendState].handle);
51 const RenderPassOptions passOptions = initRenderPassOptions(*camData, renderItem->materialInstance);
52 iContext->setRasterizerState(taskContext->states->rasterizerStateHandles[getRasterizerState(renderer, *renderItem, passOptions, camData->flipWindingOrder,
true)]);
54 const ClipShapeCache::Item& clipShape = drawContext->clipShapeCache->data[renderItem->clipShapeIx];
55 updateViewportBuffer(taskContext, taskContext->engineBuffers->sceneBufferHandle, taskContext->engineBuffers->viewBufferHandle, drawContext->renderTarget, renderItem->viewportData ? renderItem->viewportData : drawContext->cameraData, &clipShape.clipEquations);
59 applyMaterialPermutation(taskContext, drawContext, renderItem->binding, renderItem->renderMaterialInstance);
63 drawContext->task->applyMaterial(*drawContext, *renderItem);
66 applyMaterialInstance(drawContext, renderItem->binding, renderItem->renderMaterialInstance);
68 updateSceneBindings(drawContext, drawContext->cameraData, renderItem->binding);
70 updateMaterialBindings(drawContext, renderItem->renderMaterialInstance, renderItem->binding,
true);
73 const auto bindings = renderItem->binding;
74 auto& engineBuffers = *drawContext->engineBuffers;
75 auto* potreeData =
static_cast<PotreeData*
>(renderItem->callbackData);
77 if(!
HandleIsValid(bindings->objectBufferBinding))
return false;
81 objectBuffer->worldMatrix = potreeData->worldFromOcttreeFrame;
82 objectBuffer->objectId = renderItem->
objectId;
85 iContext->setConstantBuffer(bindings->objectBufferBinding, engineBuffers.objectBufferHandle);
93 auto* potreeData =
static_cast<PotreeData*
>(renderItem->callbackData);
94 if (potreeData->state != PotreeState::Running)
return;
97 if (!setRenderItemState(scale, taskContext, drawContext, renderItem))
return;
101 const float w = drawContext->cameraData->viewportSize.x;
102 const float h = drawContext->cameraData->viewportSize.y;
112 PotreeSystem* potreeSystem = potreeRenderer->potreeSystem;
113 bool useInstancing = potreeSystem->useInstancing;
115 iContext->
setTexture(
"octtree", 0, potreeData->octtreeTexture);
118 float viewScale = std::cbrt(glm::determinant(glm::mat3(potreeData->worldFromOcttreeFrame) * glm::mat3(drawContext->cameraData->viewMatrix)));
121 passControl.push_back(glm::vec4(viewScale * potreeData->modelSpaceRadius,
122 w * viewScale * potreeData->modelSpaceRadius,
123 2.f / (w * scale), 2.f / (h * scale)));
124 for (
size_t i = 0; i < potreeData->clipPlaneCount; i++) {
125 passControl.push_back(potreeData->clipPlanes[i] * drawContext->cameraData->inverseViewProjectionMatrix);
127 glm::vec4 pointControl[2] = {
128 glm::vec4(potreeData->densitySized.levelScale,
129 potreeData->densitySized.scale,
130 potreeData->densitySized.bias,
132 glm::vec4(scale, 1.f / w, 1.f / h, 0.f)
135 if(!
HandleIsValid(potreeData->pointParametersBuffer))
return;
139 buffer->passControl[0] = passControl[0];
140 for (
size_t i = 1; i <= potreeData->clipPlaneCount; i++) {
141 buffer->passControl[i] = passControl[i];
143 buffer->pointControl[0] = pointControl[0];
144 buffer->pointControl[1] = pointControl[1];
147 iContext->
setConstantBuffer(
"PointParameters", potreeData->pointParametersBuffer);
150 for (
auto* cell : potreeData->activeCells) {
155 potreeRenderer->quadVertices
170 auto* potreeData =
static_cast<PotreeData*
>(renderItem->callbackData);
171 if (potreeData->state != PotreeState::Running)
return;
174 if (!setRenderItemState(scale, taskContext, drawContext, renderItem))
return;
176 auto* renderer = taskContext->renderer;
180 auto* potreeRenderer =
static_cast<PotreeRenderer*
>(renderItem->callbackData2);
185 for (
auto* cell : potreeData->activeCells) {
186 if (cell->kind != PotreeCell::Kind::RegularNode)
continue;
187 if (potreeData->debugBoxes == PotreeDebugBoxes::ActiveLeafCells && (cell->texOffset == 0xffffu || potreeData->octtreeTextureData[cell->texOffset].r != 0))
continue;
189 auto color = cell->owningSubtree->l + cell->l;
191 glm::vec4 boxControl[3] = {
192 glm::vec4(cell->tbmin, 1.f),
193 glm::vec4(cell->tbmax - cell->tbmin, 1.f),
195 glm::vec4(((color >> 0) & 0x1), ((color >> 1) & 0x1), ((color >> 2) & 0x1), 1.f)
206 buffer->boxControl[0] = boxControl[0];
207 buffer->boxControl[1] = boxControl[1];
208 buffer->boxControl[2] = boxControl[2];
216 const glm::mat4 M = drawContext->cameraData->viewProjection * potreeData->frustum;
217 drawOneOneWireBox(taskContext, drawContext,
nullptr, M, 0);
223 this->device = device;
224 this->context = context;
225 potreeSystem = ExtensionRegistry::getExtensionSystem<PotreeSystem>(context);
229 std::array<Cogs::VertexElement,1> quadElements = {
232 static const float quadVertexData[][2] = {
238 quadStreamsLayout.
vertexFormats[0] = VertexFormats::createVertexFormat(quadElements.data(), quadElements.size());
241 quadVertices = buffers->
loadVertexBuffer(quadVertexData,
sizeof(quadVertexData) /
sizeof(quadVertexData[0]), quadStreamsLayout.
vertexFormats[0]);
244 std::array<Cogs::VertexElement,1> boxElements = {
247 static const float boxVertexData[][3] = {
257 static const uint16_t boxEdgeIndexData[] = {
258 0, 1, 1, 2, 2, 3, 3, 0,
259 4, 5, 5, 6, 6, 7, 7, 4,
260 0, 4, 1, 5, 2, 6, 3, 7
263 boxStreamsLayout.
vertexFormats[0] = VertexFormats::createVertexFormat(boxElements.data(), boxElements.size());
267 boxEdgeIndexPrimitives =
static_cast<unsigned>(
sizeof(boxEdgeIndexData) /
sizeof(boxEdgeIndexData[0]));
268 boxEdgeIndices = buffers->
loadIndexBuffer(boxEdgeIndexData, boxEdgeIndexPrimitives,
sizeof(boxEdgeIndexData[0]));
273 if (!renderingContext)
return;
279 for (
auto& poComp : potreeSystem->
pool) {
281 PotreeData* poData = poDataHolder.poData.get();
293 if (poData->octtreeTextureData.size()) {
295 desc.target = Cogs::ResourceDimensions::Texture2D;
296 desc.width =
static_cast<uint32_t
>(poData->octtreeTextureData.size());
298 desc.format = Cogs::TextureFormat::R8G8B8A8_UNORM;
302 poData->octtreeTexture = textures->
loadTexture(desc, &data);
317 if (!renderingContext)
return;
319 for (
auto & poComp : potreeSystem->
pool) {
320 if (!poComp.isVisible())
continue;
323 PotreeData* poData = poDataHolder.poData.get();
326 if (poData->state != PotreeState::Running)
continue;
329 if (poData->streamsLayout.
numStreams == 0)
continue;
332 if (pointMaterialInstance) {
334 assert(!
HandleIsValid(poComp.pointMaterial) || poComp.pointMaterial == poData->pointMaterial.instance);
336 RenderItem& renderItem = renderList->createCustom(&poData->streamsLayout);
338 if (!poComp.clipShapeComponent.Empty()) {
339 renderItem.clipShapeIx = renderList->clipShapeCache->getIndex(context, poComp.clipShapeComponent);
342 renderItem.lod = poComp.lod;
343 renderItem.
layer = poComp.layer;
344 renderItem.cullingIndex = ~0u;;
345 renderItem.
objectId = poComp.objectId;
347 renderItem.flags |= pointMaterialInstance->
hasTransparency() ? RenderItemFlags::Transparent : RenderItemFlags::None;
348 renderItem.flags |= pointMaterialInstance->
isBackdrop() ? RenderItemFlags::Backdrop : RenderItemFlags::None;
349 renderItem.flags |= poComp.castShadows() ? RenderItemFlags::CastShadows : RenderItemFlags::None;
350 renderItem.materialInstance = pointMaterialInstance;
351 getTransparencyState(renderingContext->renderer->
getRenderStates(), pointMaterialInstance, renderItem);
353 renderItem.setCallbackData(poData);
354 renderItem.setCallbackData2(
this);
355 renderItem.callback = potreeCallback;
360 if (boxMaterialInstance) {
362 assert(!
HandleIsValid(poComp.boxMaterial) || poComp.boxMaterial == poData->boxMaterial.instance);
364 RenderItem& renderItem = renderList->createCustom(&boxStreamsLayout);
365 renderItem.lod = poComp.lod;
366 renderItem.
layer = poComp.layer;
367 renderItem.cullingIndex = ~0u;;
368 renderItem.
objectId = poComp.objectId;
370 renderItem.flags |= boxMaterialInstance->
hasTransparency() ? RenderItemFlags::Transparent : RenderItemFlags::None;
371 renderItem.flags |= boxMaterialInstance->
isBackdrop() ? RenderItemFlags::Backdrop : RenderItemFlags::None;
372 renderItem.flags |= poComp.castShadows() ? RenderItemFlags::CastShadows : RenderItemFlags::None;
373 renderItem.materialInstance = boxMaterialInstance;
374 getTransparencyState(renderingContext->renderer->
getRenderStates(), boxMaterialInstance, renderItem);
376 renderItem.setCallbackData(poData);
377 renderItem.setCallbackData2(
this);
378 renderItem.callback = potreeBoxCallback;
Vector with inline storage for up to Size elements, avoiding heap allocation for small collections.
ComponentPool< ComponentType > pool
Pool of components managed by the system.
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.
IGraphicsDevice * getDevice() override
Get the graphics device used by the renderer.
Represents a graphics device used to manage graphics resources and issue drawing commands.
virtual ITextures * getTextures()=0
Get a pointer to the texture 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.
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.
@ None
No debug node outlines.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Contains all Cogs related functionality.
@ VertexData
Per vertex data.
@ PointList
List of points.
@ TriangleStrip
Triangle strip.
@ 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.
@ CustomBucket
Items with this flag should be put in Custom rendering buckets.
Material instances represent a specialized Material combined with state for all its buffers and prope...
bool hasTransparency() const
Get if this instance has any transparency and should be rendered with blending enabled.
bool isBackdrop() const
Get if geometry rendered with this material instance is to be treated as backdrops.
MaterialOptions options
Material rendering options used by this instance.
uint16_t instanceFlags
Material instance flags.
VertexFormatHandle vertexFormats[maxStreams]
COGSCORE_DLL_API void updateHash()
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.
int32_t drawOrder
Ordering of draw items within a bucket.
uint32_t objectId
Lower 6 of upper 8 bits are instance id bits, lower 24 bits are object id.
RenderLayers layer
Visibility mask.
@ PostRender
Rendering has finished for a given rendering context.
@ PreRender
Pre rendering happening for a given rendering context.
ResourceType * resolve() const
Resolve the handle, returning a pointer to the actual resource.
static const Handle_t NoHandle
Represents a handle to nothing.
Provides buffer management functionality.
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 drawIndexed(PrimitiveType primitiveType, const size_t startIndex, const size_t numIndexes, const size_t startVertex=0)=0
Draws indexed, non-instanced primitives.
virtual void setIndexBuffer(IndexBufferHandle bufferHandle, uint32_t stride=4, uint32_t offset=0)=0
Sets the current index buffer.
virtual void drawInstanced(PrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes, const size_t startInstance, const size_t numInstances)=0
Draws non-indexed, instanced primitives.
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 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 draw(PrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes)=0
Draws non-indexed, non-instanced primitives.
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.
Provides texture management functionality.
virtual void releaseTexture(TextureHandle textureHandle)=0
Release the texture with the given textureHandle.
virtual TextureHandle loadTexture(const unsigned char *bytes, unsigned int width, unsigned int height, TextureFormat format, unsigned int flags=0)=0
Load a texture using the given data to populate the texture contents.
@ WriteDiscard
Write access. When unmapping the graphics system will discard the old contents of the resource.
Provides RAII style mapping of a buffer resource.
@ Default
Default usage, the texture can be loaded once and bound and sampled in shaders.
@ 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.