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"
22#include "Foundation/Logging/Logger.h"
33 glm::mat4 projectionMatrix;
35 glm::mat4 worldMatrix;
36 glm::vec4 diffuseColor;
39 static constexpr uint16_t indexesEntity[] = {
50void Cogs::Core::EditorRenderTask::init(
const ViewContext* viewContext, IGraphicsDevice * device)
52 this->viewContext = viewContext;
56 .name =
"EditorEffect",
57 .flags = viewContext->getContext()->renderer->getEffectFlags(),
61 switch (device->getType()) {
63 desc.vertexShader =
"Engine/DebugVS.wgsl";
64 desc.pixelShader =
"Engine/DebugPS.wgsl";
65 desc.vsEntryPoint =
"vs_main";
66 desc.psEntryPoint =
"fs_main";
70 desc.vertexShader =
"Engine/DebugVS.es30.glsl";
71 desc.pixelShader =
"Engine/DebugPS.es30.glsl";
75 desc.vertexShader =
"Engine/DebugVS.hlsl";
76 desc.pixelShader =
"Engine/DebugPS.hlsl";
79 effectHandle = device->getEffects()->loadEffect(desc);
81 static constexpr VertexElement elements[] = {
85 inputLayoutHandle = buffers->
loadInputLayout(&vertexFormat, 1, effectHandle);
88 device->getBuffers()->annotate(constantBuffer,
"SelectedBounds");
91void Cogs::Core::EditorRenderTask::apply(RenderTaskContext * renderContext)
93 IEditor* editor = renderContext->context->engine->getEditor();
95 if (!editor->isActive())
return;
97 EditorState* state = editor->getState();
98 std::string erasedSelections = state->eraseInvalidSelected();
99 if (!erasedSelections.empty()) {
100 LOG_WARNING(logger,
"Removed selection of invalid entities (Asset temp Entities?): %s", erasedSelections.data());
103 Renderer* renderer = renderContext->renderer;
106 const RenderStates& renderStates = renderer->getRenderStates();
114 const CameraData& cameraData = renderContext->context->cameraSystem->getMainCameraData();
119 context->
setViewport(cameraData.viewportOrigin.x, cameraData.viewportOrigin.y, cameraData.viewportSize.x, cameraData.viewportSize.y);
123 if (renderContext->context->cameraSystem->pool.size() > 1) {
124 for (
const CameraComponent& cameraComponent : renderContext->context->cameraSystem->pool) {
125 const Entity* entity = cameraComponent.getContainer();
128 if (!state->isSelected(entity->
getId()))
continue;
129 if (entity == viewContext->getCamera().get())
continue;
131 glm::vec4 diffuseColor(0.0f, 0.8f, 0.1f, 1.0f);
132 renderCamera(renderContext, cameraComponent, diffuseColor);
138 if (!state->selected.empty()) {
140 static constexpr std::array positions = {
141 glm::vec3(-1, -1, -1),
142 glm::vec3(1, -1, -1),
143 glm::vec3(-1, 1, -1),
146 glm::vec3(-1, -1, 1),
152 auto ib = buffers->
loadIndexBuffer(indexesEntity, std::size(indexesEntity),
sizeof(indexesEntity[0]));
153 auto vb = buffers->
loadVertexBuffer(positions.data(), positions.size(), VertexFormats::Pos3f);
156 static constexpr uint32_t strides[] = {
sizeof(glm::vec3) };
159 glm::vec4 boxColor(0.8f, 0.7f, 0.2f, 1.0f);
161 for (EntityId
id : state->selected) {
162 renderEntity(renderContext,
id, boxColor);
170void Cogs::Core::EditorRenderTask::renderEntity(RenderTaskContext* renderContext,
const EntityId
id, glm::vec4 diffuseColor)
172 Renderer* renderer = renderContext->renderer;
175 const CameraData& cameraData = renderContext->context->cameraSystem->getMainCameraData();
177 Geometry::BoundingBox bounds = Bounds::getBounds(renderContext->context,
id,
true);
178 if (bounds.empty()) {
182 glm::vec3 center = bounds.getCenter();
183 glm::vec3 scale = 0.5f * bounds.getExtent();
185 glm::mat4 world = glm::translate(glm::mat4(1.0f), center) * glm::scale(glm::mat4(1.0f), scale);
191 constants->projectionMatrix = cameraData.projectionMatrix;
192 constants->viewMatrix = cameraData.viewMatrix;
193 constants->worldMatrix = world;
194 constants->diffuseColor = diffuseColor;
206void Cogs::Core::EditorRenderTask::renderCamera(RenderTaskContext* renderContext,
const CameraComponent& cameraComponent, glm::vec4 diffuseColor)
208 Renderer* renderer = renderContext->renderer;
212 const CameraData& cameraData = renderContext->context->cameraSystem->getMainCameraData();
214 static constexpr std::array<glm::vec4, 8U> positionsClip = {
215 glm::vec4(-1, -1, 0, 1),
216 glm::vec4(1, -1, 0, 1),
217 glm::vec4(1, 1, 0, 1),
218 glm::vec4(-1, 1, 0, 1),
220 glm::vec4(-1, -1, 1, 1),
221 glm::vec4(1, -1, 1, 1),
222 glm::vec4(1, 1, 1, 1),
223 glm::vec4(-1, 1, 1, 1),
226 std::array<glm::vec3, positionsClip.size()> positionsWS;
227 const CameraData& cData = renderContext->context->cameraSystem->getData(&cameraComponent);
229 for (
size_t i = 0; i < positionsWS.size(); ++i) {
230 glm::vec4 posWS = cData.inverseViewProjectionMatrix * positionsClip[i];
233 positionsWS[i] = glm::vec3(posWS);
236 static constexpr uint16_t indexesCamera[] = {
237 0, 1, 1, 2, 2, 3, 3, 0,
238 4, 5, 5, 6, 6, 7, 7, 4,
239 0, 4, 1, 5, 2, 6, 3, 7
242 auto ib = buffers->
loadIndexBuffer(indexesCamera, std::size(indexesCamera),
sizeof(indexesCamera[0]));
243 auto vb = buffers->
loadVertexBuffer(positionsWS.data(), positionsWS.size(), VertexFormats::Pos3f);
246 static constexpr uint32_t strides[] = {
sizeof(glm::vec3) };
249 static constexpr glm::mat4 world = glm::mat4(1.0f);
255 constants->projectionMatrix = cameraData.projectionMatrix;
256 constants->viewMatrix = cameraData.viewMatrix;
257 constants->worldMatrix = world;
258 constants->diffuseColor = diffuseColor;
261 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.
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.
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.
@ 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.
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 drawIndexed(PrimitiveType::EPrimitiveType primitiveType, const size_t startIndex, const size_t numIndexes, const size_t startVertex=0)=0
Draws indexed, non-instanced primitives.
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 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.
@ 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.