From e3d0070e952c1ac034eae5a4a495f2b23caf14d6 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Wed, 31 May 2023 09:30:21 -0700 Subject: [PATCH] Fixed UI not rendering (partially) --- src/dawn/display/RenderPipeline.cpp | 14 +++++++--- src/dawn/display/RenderPipeline.hpp | 11 +++----- src/dawn/display/shader/ShaderPass.hpp | 1 - .../scene/components/display/IRenderable.hpp | 17 ++++++++++-- src/dawn/scene/components/ui/CMakeLists.txt | 1 + src/dawn/scene/components/ui/UIBorder.cpp | 4 +-- src/dawn/scene/components/ui/UIBorder.hpp | 6 ++--- src/dawn/scene/components/ui/UIComponent.cpp | 1 - src/dawn/scene/components/ui/UIComponent.hpp | 18 +++---------- .../components/ui/UIComponentRenderable.cpp | 26 +++++++++++++++++++ .../components/ui/UIComponentRenderable.hpp | 26 +++++++++++++++++++ src/dawn/scene/components/ui/UIImage.cpp | 6 ++--- src/dawn/scene/components/ui/UIImage.hpp | 6 ++--- src/dawn/scene/components/ui/UILabel.cpp | 4 +-- src/dawn/scene/components/ui/UILabel.hpp | 6 ++--- src/dawnopengl/display/RenderManager.hpp | 1 - src/dawnopengl/display/shader/Shader.hpp | 2 -- .../display/shader/shaders/FontShader.hpp | 7 ----- .../shader/shaders/SimpleTexturedShader.hpp | 2 +- .../display/shader/shaders/UIShader.hpp | 2 +- .../material/SimpleBillboardedMaterial.cpp | 2 +- .../material/SimpleBillboardedMaterial.hpp | 2 +- .../material/SimpleTexturedMaterial.cpp | 16 +++--------- .../material/SimpleTexturedMaterial.hpp | 2 +- 24 files changed, 110 insertions(+), 73 deletions(-) create mode 100644 src/dawn/scene/components/ui/UIComponentRenderable.cpp create mode 100644 src/dawn/scene/components/ui/UIComponentRenderable.hpp diff --git a/src/dawn/display/RenderPipeline.cpp b/src/dawn/display/RenderPipeline.cpp index 70d8cad7..9cce4562 100644 --- a/src/dawn/display/RenderPipeline.cpp +++ b/src/dawn/display/RenderPipeline.cpp @@ -5,6 +5,7 @@ #include "RenderPipeline.hpp" #include "game/DawnGame.hpp" +#include "scene/components/scene/SubSceneController.hpp" #if DAWN_DEBUG_BUILD #include "scene/debug/SceneDebugLine.hpp" @@ -174,7 +175,14 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) { shaderBufferData.view = camera->transform->getWorldTransform(); this->shaderBuffer.buffer(&shaderBufferData); - this->camera = camera; + // Prepare a render context. This is just a nice way of letting renderables + // know a bit about what is happening, they can choose to use this data to + // render themselves differently. + struct IRenderableContext context = { + scene, + camera, + this + }; // Get the list of things to render first. std::vector shaderPassItems; @@ -183,8 +191,8 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) { auto renderables = scene->findComponents(); auto itRenderables = renderables.begin(); while(itRenderables != renderables.end()) { - vectorAppend(&shaderPassItems, (*itRenderables)->getRenderPasses()); - ++itRenderables; + vectorAppend(&shaderPassItems, (*itRenderables)->getRenderPasses(context)); + ++itRenderables; } // Debug Lines diff --git a/src/dawn/display/RenderPipeline.hpp b/src/dawn/display/RenderPipeline.hpp index 283166ea..aa8ec107 100644 --- a/src/dawn/display/RenderPipeline.hpp +++ b/src/dawn/display/RenderPipeline.hpp @@ -4,13 +4,11 @@ // https://opensource.org/licenses/MIT #pragma once -#include "dawnlibs.hpp" -#include "scene/components/display/Material.hpp" -#include "scene/components/display/Camera.hpp" -#include "scene/components/scene/SubSceneController.hpp" -#include "scene/components/ui/UIComponent.hpp" +#include "display/_RenderManager.hpp" #include "display/shader/ShaderPass.hpp" +#include "scene/components/display/IRenderable.hpp" #include "display/shader/buffers/RenderPipelineShaderBuffer.hpp" +#include "scene/components/display/Camera.hpp" namespace Dawn { class RenderManager; @@ -21,9 +19,6 @@ namespace Dawn { public: RenderManager *renderManager; - - // Temporary hack - Camera *camera = nullptr; RenderPipelineShaderBuffer shaderBuffer; /** diff --git a/src/dawn/display/shader/ShaderPass.hpp b/src/dawn/display/shader/ShaderPass.hpp index d749d2d5..4943ffc1 100644 --- a/src/dawn/display/shader/ShaderPass.hpp +++ b/src/dawn/display/shader/ShaderPass.hpp @@ -6,7 +6,6 @@ #pragma once #include "display/shader/Shader.hpp" #include "display/mesh/Mesh.hpp" -#include "display/shader/ShaderParameterBuffer.hpp" namespace Dawn { struct ShaderPassItem { diff --git a/src/dawn/scene/components/display/IRenderable.hpp b/src/dawn/scene/components/display/IRenderable.hpp index bdb1d858..91990f9a 100644 --- a/src/dawn/scene/components/display/IRenderable.hpp +++ b/src/dawn/scene/components/display/IRenderable.hpp @@ -4,17 +4,30 @@ // https://opensource.org/licenses/MIT #pragma once -#include "display/shader/Shader.hpp" +#include "dawnlibs.hpp" namespace Dawn { + class Scene; + class Camera; + class RenderPipeline; + + struct IRenderableContext { + Scene *scene; + Camera *camera; + RenderPipeline *renderPipeline; + }; + class IRenderable { public: /** * Returns the render passes for this renderable item, typically a scene * item component, e.g. a Material or a UI Item. * + * @param context Context about the current renderer state. * @return Array of renderable passes. */ - virtual std::vector getRenderPasses() = 0; + virtual std::vector getRenderPasses( + struct IRenderableContext &context + ) = 0; }; } \ No newline at end of file diff --git a/src/dawn/scene/components/ui/CMakeLists.txt b/src/dawn/scene/components/ui/CMakeLists.txt index 2c0b9329..4ff90deb 100644 --- a/src/dawn/scene/components/ui/CMakeLists.txt +++ b/src/dawn/scene/components/ui/CMakeLists.txt @@ -8,6 +8,7 @@ target_sources(${DAWN_TARGET_NAME} PRIVATE UICanvas.cpp UIComponent.cpp + UIComponentRenderable.cpp UILabel.cpp UIImage.cpp UIBorder.cpp diff --git a/src/dawn/scene/components/ui/UIBorder.cpp b/src/dawn/scene/components/ui/UIBorder.cpp index 6c5576c5..3e8179e3 100644 --- a/src/dawn/scene/components/ui/UIBorder.cpp +++ b/src/dawn/scene/components/ui/UIBorder.cpp @@ -11,7 +11,7 @@ using namespace Dawn; UIBorder::UIBorder(SceneItem *item) : texture(nullptr), borderSize(glm::vec2(8, 8)), - UIComponent(item) + UIComponentRenderable(item) { } @@ -25,7 +25,7 @@ float_t UIBorder::getContentHeight() { return this->height; } -std::vector UIBorder::getRenderPasses() { +std::vector UIBorder::getUIRenderPasses() { struct ShaderPassItem item; auto shader = getGame()->renderManager.uiShader; item.shader = shader; diff --git a/src/dawn/scene/components/ui/UIBorder.hpp b/src/dawn/scene/components/ui/UIBorder.hpp index 1169e6bb..bbe84b2e 100644 --- a/src/dawn/scene/components/ui/UIBorder.hpp +++ b/src/dawn/scene/components/ui/UIBorder.hpp @@ -4,13 +4,13 @@ // https://opensource.org/licenses/MIT #pragma once -#include "UIComponent.hpp" +#include "UIComponentRenderable.hpp" #include "display/mesh/QuadMesh.hpp" #define UI_BORDER_QUAD_COUNT 9 namespace Dawn { - class UIBorder : public UIComponent, public UIComponentRenderable { + class UIBorder : public UIComponentRenderable { private: Mesh mesh; @@ -24,7 +24,7 @@ namespace Dawn { float_t getContentWidth() override; float_t getContentHeight() override; - std::vector getRenderPasses() override; + std::vector getUIRenderPasses() override; void onStart() override; }; } \ No newline at end of file diff --git a/src/dawn/scene/components/ui/UIComponent.cpp b/src/dawn/scene/components/ui/UIComponent.cpp index 6fead769..d85a67a2 100644 --- a/src/dawn/scene/components/ui/UIComponent.cpp +++ b/src/dawn/scene/components/ui/UIComponent.cpp @@ -7,7 +7,6 @@ using namespace Dawn; - UIComponent::UIComponent(SceneItem *item) : SceneItemComponent(item), alignment(glm::vec4(0, 0, 128, 128)), diff --git a/src/dawn/scene/components/ui/UIComponent.hpp b/src/dawn/scene/components/ui/UIComponent.hpp index a91934c5..6e5a88e6 100644 --- a/src/dawn/scene/components/ui/UIComponent.hpp +++ b/src/dawn/scene/components/ui/UIComponent.hpp @@ -6,6 +6,8 @@ #pragma once #include "scene/SceneItemComponent.hpp" #include "scene/components/display/IRenderable.hpp" +#include "display/RenderPipeline.hpp" +#include "display/RenderManager.hpp" #include "UICanvas.hpp" #include "util/mathutils.hpp" @@ -22,20 +24,6 @@ namespace Dawn { UI_COMPONENT_ALIGN_UNIT_PERCENT }; - class UIComponentRenderable : public IRenderable { - public: - /** - * Implemented UI Components that have rendering should implement this and - * return their rendering items. Items are passed back to the render - * pipeline. - * - * @param projection Camera projection, obtained from the canvas. - * @param view Camera view, obtained from the canvas. - * @return A list of renderable shader pass items for this renderable. - */ - virtual std::vector getRenderPasses() = 0; - }; - class UIComponent : public SceneItemComponent, public UIComponentDimensional { protected: float_t width = 1; @@ -117,7 +105,7 @@ namespace Dawn { * * @return Pointer to the UI Canvas this component is a child of. */ - UICanvas * getCanvas(); + virtual UICanvas * getCanvas(); float_t getWidth() override; float_t getHeight() override; diff --git a/src/dawn/scene/components/ui/UIComponentRenderable.cpp b/src/dawn/scene/components/ui/UIComponentRenderable.cpp new file mode 100644 index 00000000..c8783288 --- /dev/null +++ b/src/dawn/scene/components/ui/UIComponentRenderable.cpp @@ -0,0 +1,26 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "UIComponentRenderable.hpp" + +using namespace Dawn; + +UIComponentRenderable::UIComponentRenderable(SceneItem *i) : UIComponent(i) { + +} + +std::vector UIComponentRenderable::getRenderPasses( + struct IRenderableContext &context +) { + if( + context.camera->getRenderTarget() != + context.renderPipeline->renderManager->getBackBuffer() + ) { + auto canvas = this->getCanvas(); + if(canvas->drawType == UI_DRAW_TYPE_WORLD_CAMERA_RELATIVE) return {}; + } + + return this->getUIRenderPasses(); +} \ No newline at end of file diff --git a/src/dawn/scene/components/ui/UIComponentRenderable.hpp b/src/dawn/scene/components/ui/UIComponentRenderable.hpp new file mode 100644 index 00000000..0f65ce9f --- /dev/null +++ b/src/dawn/scene/components/ui/UIComponentRenderable.hpp @@ -0,0 +1,26 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "UIComponent.hpp" + +namespace Dawn { + class UIComponentRenderable : public UIComponent, public IRenderable { + public: + UIComponentRenderable(SceneItem *item); + + std::vector getRenderPasses( + struct IRenderableContext &context + ) override; + + /** + * Higher level means to determine the render passes for this UI item. We + * handle some of the more common functionality in the above override. + * + * @return Array of render passes for this UI item. + */ + virtual std::vector getUIRenderPasses() = 0; + }; +} \ No newline at end of file diff --git a/src/dawn/scene/components/ui/UIImage.cpp b/src/dawn/scene/components/ui/UIImage.cpp index 779ff541..5168b025 100644 --- a/src/dawn/scene/components/ui/UIImage.cpp +++ b/src/dawn/scene/components/ui/UIImage.cpp @@ -10,7 +10,7 @@ using namespace Dawn; UIImage::UIImage(SceneItem *item) : texture(nullptr), - UIComponent(item) + UIComponentRenderable(item) { } @@ -25,12 +25,12 @@ float_t UIImage::getContentHeight() { return this->height; } -std::vector UIImage::getRenderPasses() { +std::vector UIImage::getUIRenderPasses() { struct ShaderPassItem item; auto shader = getGame()->renderManager.uiShader; item.shader = shader; item.colorValues[shader->paramColor] = this->color; - item.parameterBuffers[shader->bufferUiCanvas] = &this->getCanvas()->shaderBuffer; + item.parameterBuffers[shader->bufferUiCanvas] = &getCanvas()->shaderBuffer; item.matrixValues[shader->paramModel] = this->transform->getWorldTransform(); if(this->texture == nullptr) { item.boolValues[shader->paramHasTexture] = false; diff --git a/src/dawn/scene/components/ui/UIImage.hpp b/src/dawn/scene/components/ui/UIImage.hpp index 3cd86e7f..675f3aad 100644 --- a/src/dawn/scene/components/ui/UIImage.hpp +++ b/src/dawn/scene/components/ui/UIImage.hpp @@ -4,11 +4,11 @@ // https://opensource.org/licenses/MIT #pragma once -#include "UIComponent.hpp" +#include "UIComponentRenderable.hpp" #include "display/mesh/QuadMesh.hpp" namespace Dawn { - class UIImage : public UIComponent, public UIComponentRenderable { + class UIImage : public UIComponentRenderable { private: Mesh mesh; @@ -22,7 +22,7 @@ namespace Dawn { float_t getContentWidth() override; float_t getContentHeight() override; - std::vector getRenderPasses() override; + std::vector getUIRenderPasses() override; void onStart() override; }; } \ No newline at end of file diff --git a/src/dawn/scene/components/ui/UILabel.cpp b/src/dawn/scene/components/ui/UILabel.cpp index fe5b612c..ca3c3c4b 100644 --- a/src/dawn/scene/components/ui/UILabel.cpp +++ b/src/dawn/scene/components/ui/UILabel.cpp @@ -9,7 +9,7 @@ using namespace Dawn; UILabel::UILabel(SceneItem *item) : - UIComponent(item), + UIComponentRenderable(item), text(""), fontSize(10.0f), font(&item->scene->game->renderManager.defaultFont), @@ -67,7 +67,7 @@ void UILabel::updateMesh() { this->eventFontRebuffered.invoke(); } -std::vector UILabel::getRenderPasses() { +std::vector UILabel::getUIRenderPasses() { if(!this->hasText()) return {}; this->updateMesh(); diff --git a/src/dawn/scene/components/ui/UILabel.hpp b/src/dawn/scene/components/ui/UILabel.hpp index 9a2edeea..326950b5 100644 --- a/src/dawn/scene/components/ui/UILabel.hpp +++ b/src/dawn/scene/components/ui/UILabel.hpp @@ -4,14 +4,14 @@ // https://opensource.org/licenses/MIT #pragma once -#include "UIComponent.hpp" +#include "UIComponentRenderable.hpp" #include "display/font/Font.hpp" #define UI_LABEL_MAX_WIDTH_NONE -1 #define UI_LABEL_MAX_WIDTH_ALIGN 0 namespace Dawn { - class UILabel : public UIComponent, public UIComponentRenderable { + class UILabel : public UIComponentRenderable { private: bool_t needsRebuffering = true; Mesh mesh; @@ -53,7 +53,7 @@ namespace Dawn { float_t getContentWidth() override; float_t getContentHeight() override; - std::vector getRenderPasses() override; + std::vector getUIRenderPasses() override; void onStart() override; }; } \ No newline at end of file diff --git a/src/dawnopengl/display/RenderManager.hpp b/src/dawnopengl/display/RenderManager.hpp index 646492ea..3f0758cc 100644 --- a/src/dawnopengl/display/RenderManager.hpp +++ b/src/dawnopengl/display/RenderManager.hpp @@ -4,7 +4,6 @@ // https://opensource.org/licenses/MIT #pragma once -#include "display/_RenderManager.hpp" #include "display/BackBufferRenderTarget.hpp" #include "display/shader/ShaderManager.hpp" #include "display/shader/shaders/SimpleTexturedShader.hpp" diff --git a/src/dawnopengl/display/shader/Shader.hpp b/src/dawnopengl/display/shader/Shader.hpp index 4be9f74e..c908980b 100644 --- a/src/dawnopengl/display/shader/Shader.hpp +++ b/src/dawnopengl/display/shader/Shader.hpp @@ -9,8 +9,6 @@ #include "display/Color.hpp" #include "debug/debug.hpp" -#include "ShaderParameterBuffer.hpp" - typedef GLuint shaderparameter_t; namespace Dawn { diff --git a/src/dawnopengl/display/shader/shaders/FontShader.hpp b/src/dawnopengl/display/shader/shaders/FontShader.hpp index d988b86f..5501c9d7 100644 --- a/src/dawnopengl/display/shader/shaders/FontShader.hpp +++ b/src/dawnopengl/display/shader/shaders/FontShader.hpp @@ -2,13 +2,6 @@ // // This software is released under the MIT License. // https://opensource.org/licenses/MIT - -#pragma once -// Copyright (c) 2023 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - #pragma once #include "display/shader/Shader.hpp" diff --git a/src/dawnopengl/display/shader/shaders/SimpleTexturedShader.hpp b/src/dawnopengl/display/shader/shaders/SimpleTexturedShader.hpp index 9b6fb01f..e987dd83 100644 --- a/src/dawnopengl/display/shader/shaders/SimpleTexturedShader.hpp +++ b/src/dawnopengl/display/shader/shaders/SimpleTexturedShader.hpp @@ -4,8 +4,8 @@ // https://opensource.org/licenses/MIT #pragma once -#include "display/shader/buffers/RenderPipelineShaderBuffer.hpp" #include "display/shader/Shader.hpp" +#include "display/shader/buffers/RenderPipelineShaderBuffer.hpp" namespace Dawn { class SimpleTexturedShader : public Shader { diff --git a/src/dawnopengl/display/shader/shaders/UIShader.hpp b/src/dawnopengl/display/shader/shaders/UIShader.hpp index 8eec0571..bc64e1fe 100644 --- a/src/dawnopengl/display/shader/shaders/UIShader.hpp +++ b/src/dawnopengl/display/shader/shaders/UIShader.hpp @@ -4,8 +4,8 @@ // https://opensource.org/licenses/MIT #pragma once -#include "display/shader/buffers/RenderPipelineShaderBuffer.hpp" #include "display/shader/Shader.hpp" +#include "display/shader/buffers/RenderPipelineShaderBuffer.hpp" #define UI_SHADER_PROGRAM_PRIORITY 100 diff --git a/src/dawnopengl/scene/components/display/material/SimpleBillboardedMaterial.cpp b/src/dawnopengl/scene/components/display/material/SimpleBillboardedMaterial.cpp index adadc25e..a2ab71a4 100644 --- a/src/dawnopengl/scene/components/display/material/SimpleBillboardedMaterial.cpp +++ b/src/dawnopengl/scene/components/display/material/SimpleBillboardedMaterial.cpp @@ -22,7 +22,7 @@ void SimpleBillboardedMaterial::onDispose() { this->getGame()->renderManager.getShaderManager()->releaseShader(this->shaderLock); } -std::vector SimpleBillboardedMaterial::getRenderPasses() { +std::vector SimpleBillboardedMaterial::getRenderPasses(IRenderableContext &context) { return { }; diff --git a/src/dawnopengl/scene/components/display/material/SimpleBillboardedMaterial.hpp b/src/dawnopengl/scene/components/display/material/SimpleBillboardedMaterial.hpp index 563c0168..2a752027 100644 --- a/src/dawnopengl/scene/components/display/material/SimpleBillboardedMaterial.hpp +++ b/src/dawnopengl/scene/components/display/material/SimpleBillboardedMaterial.hpp @@ -26,6 +26,6 @@ namespace Dawn { void onStart() override; void onDispose() override; - std::vector getRenderPasses() override; + std::vector getRenderPasses(IRenderableContext &context) override; }; } \ No newline at end of file diff --git a/src/dawnopengl/scene/components/display/material/SimpleTexturedMaterial.cpp b/src/dawnopengl/scene/components/display/material/SimpleTexturedMaterial.cpp index 86f6f43d..12dc52a1 100644 --- a/src/dawnopengl/scene/components/display/material/SimpleTexturedMaterial.cpp +++ b/src/dawnopengl/scene/components/display/material/SimpleTexturedMaterial.cpp @@ -21,27 +21,21 @@ void SimpleTexturedMaterial::onDispose() { this->getGame()->renderManager.getShaderManager()->releaseShader(this->shaderLock); } -std::vector SimpleTexturedMaterial::getRenderPasses() { +std::vector SimpleTexturedMaterial::getRenderPasses(IRenderableContext &context) { auto mesh = this->item->getComponent(); auto shader = this->getGame()->renderManager.getShaderManager()->getShader(this->shaderLock); - auto camera = this->getGame()->renderManager.getRenderPipeline()->camera; assertNotNull(mesh); assertNotNull(mesh->mesh); assertNotNull(shader); - assertNotNull(camera); struct ShaderPassItem onlyPass; onlyPass.mesh = mesh->mesh; onlyPass.shader = shader; onlyPass.colorValues[shader->paramColor] = this->color; onlyPass.matrixValues[shader->paramModel] = this->transform->getWorldTransform(); + onlyPass.parameterBuffers[shader->bufferRenderPipeline] = &context.renderPipeline->shaderBuffer; - onlyPass.parameterBuffers[shader->bufferRenderPipeline] = &this->getGame()->renderManager.getRenderPipeline()->shaderBuffer; - - // onlyPass.matrixValues[shader->paramView] = camera->transform->getWorldTransform(); - // onlyPass.matrixValues[shader->paramProjection] = camera->getProjection(); - onlyPass.renderFlags = ( RENDER_MANAGER_RENDER_FLAG_BLEND | RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST @@ -54,8 +48,6 @@ std::vector SimpleTexturedMaterial::getRenderPasses() { } else { onlyPass.boolValues[shader->paramHasTexture] = false; } - - std::vector passes; - passes.push_back(onlyPass); - return passes; + + return { onlyPass }; } \ No newline at end of file diff --git a/src/dawnopengl/scene/components/display/material/SimpleTexturedMaterial.hpp b/src/dawnopengl/scene/components/display/material/SimpleTexturedMaterial.hpp index d4548531..0a3c60e2 100644 --- a/src/dawnopengl/scene/components/display/material/SimpleTexturedMaterial.hpp +++ b/src/dawnopengl/scene/components/display/material/SimpleTexturedMaterial.hpp @@ -27,6 +27,6 @@ namespace Dawn { void onStart() override; void onDispose() override; - std::vector getRenderPasses() override; + std::vector getRenderPasses(IRenderableContext &context) override; }; } \ No newline at end of file