1#include "EditorRenderTask.h"
5#include "Editor/Editor.h"
8#include "Renderer/Renderer.h"
10#include "Resources/VertexFormats.h"
11#include "Resources/DefaultMaterial.h"
13#include "Scene/GetBounds.h"
15#include "Systems/Core/CameraSystem.h"
16#include "Components/Core/CameraComponent.h"
18#include "Rendering/ICapabilities.h"
19#include "Rendering/IContext.h"
20#include "Rendering/IBuffers.h"
21#include "Rendering/IEffects.h"
23#include "Foundation/Logging/Logger.h"
35 glm::mat4 projectionMatrix;
37 glm::mat4 worldMatrix;
38 glm::vec4 diffuseColor;
41 static constexpr uint16_t indexesEntity[] = {
52void Cogs::Core::EditorRenderTask::init(
const ViewContext* viewContext, IGraphicsDevice * device)
54 this->viewContext = viewContext;
58 .name =
"EditorEffect",
59 .flags = viewContext->getContext()->renderer->getEffectFlags(),
63 switch (device->getType()) {
65 desc.vertexShader =
"Engine/DebugVS.wgsl";
66 desc.pixelShader =
"Engine/DebugPS.wgsl";
67 desc.vsEntryPoint =
"vs_main";
68 desc.psEntryPoint =
"fs_main";
72 desc.vertexShader =
"Engine/DebugVS.es30.glsl";
73 desc.pixelShader =
"Engine/DebugPS.es30.glsl";
77 desc.vertexShader =
"Engine/DebugVS.hlsl";
78 desc.pixelShader =
"Engine/DebugPS.hlsl";
81 effectHandle = device->getEffects()->loadEffect(desc);
83 static constexpr VertexElement elements[] = {
86 vertexFormat = buffers->createVertexFormat(elements, 1);
89 device->getBuffers()->annotate(constantBuffer,
"SelectedBounds");
99 inputLayoutHandle = buffers->
loadInputLayout(&vertexFormat, 1, effectHandle);
103 LOG_ERROR(logger,
"EditorRenderTask: Failed to build editor debug effect.");
106 effectStatus = status;
112 IEditor* editor = renderContext->context->
engine->getEditor();
114 if (!editor->isActive())
return;
120 if (!erasedSelections.empty()) {
121 LOG_WARNING(logger,
"Removed selection of invalid entities (Asset temp Entities?): %s", erasedSelections.data());
124 Renderer* renderer = renderContext->renderer;
127 const RenderStates& renderStates = renderer->getRenderStates();
132 context->
setDepthStencilState(renderStates.commonDepthStates[RenderStates::noWriteDepthStencilState]);
135 const CameraData& cameraData = renderContext->context->cameraSystem->getMainCameraData();
140 context->
setViewport(cameraData.viewportOrigin.x, cameraData.viewportOrigin.y, cameraData.viewportSize.x, cameraData.viewportSize.y);
144 if (renderContext->context->cameraSystem->pool.size() > 1) {
145 for (
const CameraComponent& cameraComponent : renderContext->context->cameraSystem->pool) {
146 const Entity* entity = cameraComponent.getContainer();
149 if (!state->isSelected(entity->
getId()))
continue;
150 if (entity == viewContext->getCamera().get())
continue;
152 glm::vec4 diffuseColor(0.0f, 0.8f, 0.1f, 1.0f);
153 renderCamera(renderContext, cameraComponent, diffuseColor);
159 if (!state->selected.empty()) {
161 const std::array positions = {
162 glm::vec3(-1, -1, -1),
163 glm::vec3(1, -1, -1),
164 glm::vec3(-1, 1, -1),
167 glm::vec3(-1, -1, 1),
173 auto ib = buffers->
loadIndexBuffer(indexesEntity, std::size(indexesEntity),
sizeof(indexesEntity[0]));
174 auto vb = buffers->
loadVertexBuffer(positions.data(), positions.size(), VertexFormats::Pos3f);
177 static constexpr uint32_t strides[] = {
sizeof(glm::vec3) };
180 glm::vec4 boxColor(0.8f, 0.7f, 0.2f, 1.0f);
182 for (EntityId
id : state->selected) {
183 renderEntity(renderContext,
id, boxColor);
191void Cogs::Core::EditorRenderTask::renderEntity(RenderTaskContext* renderContext,
const EntityId
id, glm::vec4 diffuseColor)
193 Renderer* renderer = renderContext->renderer;
196 const CameraData& cameraData = renderContext->context->cameraSystem->getMainCameraData();
198 Geometry::BoundingBox bounds = Bounds::getBounds(renderContext->context,
id,
true);
199 if (bounds.empty()) {
203 glm::vec3 center = bounds.getCenter();
204 glm::vec3 scale = 0.5f * bounds.getExtent();
206 glm::mat4 world = glm::translate(glm::mat4(1.0f), center) * glm::scale(glm::mat4(1.0f), scale);
212 constants->projectionMatrix = cameraData.projectionMatrix;
213 constants->viewMatrix = cameraData.viewMatrix;
214 constants->worldMatrix = world;
215 constants->diffuseColor = diffuseColor;
227void Cogs::Core::EditorRenderTask::renderCamera(RenderTaskContext* renderContext,
const CameraComponent& cameraComponent, glm::vec4 diffuseColor)
229 Renderer* renderer = renderContext->renderer;
233 const CameraData& cameraData = renderContext->context->cameraSystem->getMainCameraData();
235 const std::array<glm::vec4, 8U> positionsClip = {
236 glm::vec4(-1, -1, 0, 1),
237 glm::vec4(1, -1, 0, 1),
238 glm::vec4(1, 1, 0, 1),
239 glm::vec4(-1, 1, 0, 1),
241 glm::vec4(-1, -1, 1, 1),
242 glm::vec4(1, -1, 1, 1),
243 glm::vec4(1, 1, 1, 1),
244 glm::vec4(-1, 1, 1, 1),
247 std::array<glm::vec3, positionsClip.size()> positionsWS;
248 const CameraData& cData = renderContext->context->cameraSystem->getData(&cameraComponent);
250 for (
size_t i = 0; i < positionsWS.size(); ++i) {
251 glm::vec4 posWS = cData.inverseViewProjectionMatrix * positionsClip[i];
254 positionsWS[i] = glm::vec3(posWS);
257 static constexpr uint16_t indexesCamera[] = {
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 auto ib = buffers->
loadIndexBuffer(indexesCamera, std::size(indexesCamera),
sizeof(indexesCamera[0]));
264 auto vb = buffers->
loadVertexBuffer(positionsWS.data(), positionsWS.size(), VertexFormats::Pos3f);
267 static constexpr uint32_t strides[] = {
sizeof(glm::vec3) };
270 const glm::mat4 world = glm::mat4(1.0f);
276 constants->projectionMatrix = cameraData.projectionMatrix;
277 constants->viewMatrix = cameraData.viewMatrix;
278 constants->worldMatrix = world;
279 constants->diffuseColor = diffuseColor;
282 LOG_ERROR_ONCE(logger,
"MappedBuffer<DebugConstants> for Camera fail");
Container for components, providing composition of dynamic entities.
constexpr size_t getId() const noexcept
Get the unique identifier of this entity.
std::unique_ptr< class Engine > engine
Engine instance.
Cogs::ResourceStatus checkReady(RenderTaskContext *context) override
Check if the task is ready to be applied, e.g.
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.
Log implementation class.
bool HandleIsValid(const ResourceHandle_t< T > &handle)
Check if the given resource is valid, that is not equal to NoHandle or InvalidHandle.
@ Perspective
Perspective projection.
@ None
No blending enabled for opaque shapes, defaults to Blend for transparent shapes.
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.
ResourceStatus
Status of an asynchronously loaded resource, such as an effect or a pipeline.
@ Error
The resource failed to load.
@ Ready
The resource has loaded successfully and is ready for use.
@ VertexData
Per vertex data.
@ 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.
std::string eraseInvalidSelected()
Contains an effect description used to load a single effect.
EEffectFlags
Effect source flags.
@ WGSL
Effect source is WGSL.
@ GLSL
Effect source is GLSL.
Provides buffer management functionality.
virtual void releaseVertexBuffer(VertexBufferHandle vertexBufferHandle)=0
Release the vertex buffer with the given handle.
virtual InputLayoutHandle loadInputLayout(const VertexFormatHandle *vertexFormats, const size_t count, EffectHandle effectHandle)=0
Loads a new input layout to map vertex flow between vertex buffers with the given vertexFormats to ef...
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 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 setRasterizerState(const RasterizerStateHandle handle)=0
Set the current rasterizer state.
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 setBlendState(const BlendStateHandle handle, const float *constant=nullptr)=0
Set the current blend state.
virtual void setInputLayout(const InputLayoutHandle inputLayoutHandle)=0
Sets the current input layout.
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 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 setVertexBuffers(const VertexBufferHandle *vertexBufferHandles, const size_t count, const uint32_t *strides, const uint32_t *offsets)=0
Sets the current vertex buffers.
virtual void setEffect(EffectHandle handle)=0
Set the current effect.
Provides effects and shader management functionality.
virtual ResourceStatus checkEffect(EffectHandle effectHandle)=0
Check the load status of the effect with the given effectHandle.
@ WriteDiscard
Write access. When unmapping the graphics system will discard the old contents of the resource.
@ Dynamic
Buffer will be loaded and modified with some frequency.