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 iContext->
setTexture(
"octtree", 0, potreeData->octtreeTexture);
115 float viewScale = std::cbrt(glm::determinant(glm::mat3(potreeData->worldFromOcttreeFrame) * glm::mat3(drawContext->cameraData->viewMatrix)));
118 passControl.push_back(glm::vec4(viewScale * potreeData->modelSpaceRadius,
119 w * viewScale * potreeData->modelSpaceRadius,
120 2.f / (w * scale), 2.f / (h * scale)));
121 for (
size_t i = 0; i < potreeData->clipPlaneCount; i++) {
122 passControl.push_back(potreeData->clipPlanes[i] * drawContext->cameraData->inverseViewProjectionMatrix);
124 glm::vec4 pointControl[2] = {
125 glm::vec4(potreeData->densitySized.levelScale,
126 potreeData->densitySized.scale,
127 potreeData->densitySized.bias,
129 glm::vec4(scale, 0.f, 0.f, 0.f)
132 if(!
HandleIsValid(potreeData->pointParametersBuffer))
return;
136 buffer->passControl[0] = passControl[0];
137 for (
size_t i = 1; i <= potreeData->clipPlaneCount; i++) {
138 buffer->passControl[i] = passControl[i];
140 buffer->pointControl[0] = pointControl[0];
141 buffer->pointControl[1] = pointControl[1];
144 iContext->
setConstantBuffer(
"PointParameters", potreeData->pointParametersBuffer);
146 for (
auto* cell : potreeData->activeCells) {
156 auto* potreeData =
static_cast<PotreeData*
>(renderItem->callbackData);
157 if (potreeData->state != PotreeState::Running)
return;
160 if (!setRenderItemState(scale, taskContext, drawContext, renderItem))
return;
162 auto* renderer = taskContext->renderer;
166 auto* potreeRenderer =
static_cast<PotreeRenderer*
>(renderItem->callbackData2);
171 for (
auto* cell : potreeData->activeCells) {
172 if (cell->kind != PotreeCell::Kind::RegularNode)
continue;
173 if (potreeData->debugBoxes == PotreeDebugBoxes::ActiveLeafCells && (cell->texOffset == 0xffffu || potreeData->octtreeTextureData[cell->texOffset].r != 0))
continue;
175 auto color = cell->owningSubtree->l + cell->l;
177 glm::vec4 boxControl[3] = {
178 glm::vec4(cell->tbmin, 1.f),
179 glm::vec4(cell->tbmax - cell->tbmin, 1.f),
181 glm::vec4(((color >> 0) & 0x1), ((color >> 1) & 0x1), ((color >> 2) & 0x1), 1.f)
192 buffer->boxControl[0] = boxControl[0];
193 buffer->boxControl[1] = boxControl[1];
194 buffer->boxControl[2] = boxControl[2];
202 const glm::mat4 M = drawContext->cameraData->viewProjection * potreeData->frustum;
203 drawOneOneWireBox(taskContext, drawContext,
nullptr, M, 0);
209 this->device = device;
210 this->context = context;
211 potreeSystem = ExtensionRegistry::getExtensionSystem<PotreeSystem>(context);
214 std::array<Cogs::VertexElement,1> elements = {
218 boxStreamsLayout.
vertexFormats[0] = VertexFormats::createVertexFormat(elements.data(), elements.size());
225 static const float boxVertexData[] = {
235 boxVertices = buffers->loadVertexBuffer(boxVertexData,
sizeof(boxVertexData) /
sizeof(boxVertexData[0]), boxStreamsLayout.
vertexFormats[0]);
237 static const uint16_t boxEdgeIndexData[] = {
238 0, 1, 1, 2, 2, 3, 3, 0,
239 4, 5, 5, 6, 6, 7, 7, 4,
240 0, 4, 1, 5, 2, 6, 3, 7
242 boxEdgeIndexPrimitives =
static_cast<unsigned>(
sizeof(boxEdgeIndexData) /
sizeof(boxEdgeIndexData[0]));
243 boxEdgeIndices = buffers->loadIndexBuffer(boxEdgeIndexData, boxEdgeIndexPrimitives,
sizeof(boxEdgeIndexData[0]));
249 if (!renderingContext)
return;
255 for (
auto& poComp : potreeSystem->pool) {
257 PotreeData* poData = poDataHolder.poData.get();
269 if (poData->octtreeTextureData.size()) {
271 desc.target = Cogs::ResourceDimensions::Texture2D;
272 desc.width =
static_cast<uint32_t
>(poData->octtreeTextureData.size());
274 desc.format = Cogs::TextureFormat::R8G8B8A8_UNORM;
278 poData->octtreeTexture = textures->
loadTexture(desc, &data);
293 if (!renderingContext)
return;
295 for (
auto & poComp : potreeSystem->pool) {
296 if (!poComp.isVisible())
continue;
299 PotreeData* poData = poDataHolder.poData.get();
302 if (poData->state != PotreeState::Running)
continue;
305 if (poData->streamsLayout.
numStreams == 0)
continue;
308 if (pointMaterialInstance) {
310 assert(!
HandleIsValid(poComp.pointMaterial) || poComp.pointMaterial == poData->pointMaterial.instance);
312 RenderItem& renderItem = renderList->createCustom(&poData->streamsLayout);
314 if (!poComp.clipShapeComponent.Empty()) {
315 renderItem.clipShapeIx = renderList->clipShapeCache->getIndex(context, poComp.clipShapeComponent);
318 renderItem.lod = poComp.lod;
319 renderItem.
layer = poComp.layer;
320 renderItem.cullingIndex = ~0u;;
321 renderItem.
objectId = poComp.objectId;
323 renderItem.flags |= pointMaterialInstance->
hasTransparency() ? RenderItemFlags::Transparent : RenderItemFlags::None;
324 renderItem.flags |= pointMaterialInstance->
isBackdrop() ? RenderItemFlags::Backdrop : RenderItemFlags::None;
325 renderItem.flags |= poComp.castShadows() ? RenderItemFlags::CastShadows : RenderItemFlags::None;
326 renderItem.materialInstance = pointMaterialInstance;
327 getTransparencyState(renderingContext->renderer->
getRenderStates(), pointMaterialInstance, renderItem);
329 renderItem.setCallbackData(poData);
330 renderItem.callback = potreeCallback;
335 if (boxMaterialInstance) {
337 assert(!
HandleIsValid(poComp.boxMaterial) || poComp.boxMaterial == poData->boxMaterial.instance);
339 RenderItem& renderItem = renderList->createCustom(&boxStreamsLayout);
340 renderItem.lod = poComp.lod;
341 renderItem.
layer = poComp.layer;
342 renderItem.cullingIndex = ~0u;;
343 renderItem.
objectId = poComp.objectId;
345 renderItem.flags |= boxMaterialInstance->
hasTransparency() ? RenderItemFlags::Transparent : RenderItemFlags::None;
346 renderItem.flags |= boxMaterialInstance->
isBackdrop() ? RenderItemFlags::Backdrop : RenderItemFlags::None;
347 renderItem.flags |= poComp.castShadows() ? RenderItemFlags::CastShadows : RenderItemFlags::None;
348 renderItem.materialInstance = boxMaterialInstance;
349 getTransparencyState(renderingContext->renderer->
getRenderStates(), boxMaterialInstance, renderItem);
351 renderItem.setCallbackData(poData);
352 renderItem.setCallbackData2(
this);
353 renderItem.callback = potreeBoxCallback;
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.
@ 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 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.
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 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.