diff --git a/src/dawn/display/RenderPipeline.cpp b/src/dawn/display/RenderPipeline.cpp index 2c83a8ad..973e2770 100644 --- a/src/dawn/display/RenderPipeline.cpp +++ b/src/dawn/display/RenderPipeline.cpp @@ -5,14 +5,12 @@ #include "RenderPipeline.hpp" #include "game/DawnGame.hpp" -#include "display/mesh/QuadMesh.hpp" -#include "scene/SceneItem.hpp" +#include "display/RenderManager.hpp" using namespace Dawn; RenderPipeline::RenderPipeline(RenderManager *renderManager) { assertNotNull(renderManager); - this->renderManager = renderManager; } @@ -77,12 +75,84 @@ void RenderPipeline::renderScene(Scene *scene) { void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) { assertNotNull(scene); assertNotNull(camera); + + // Create a new render ID. Long story short this is a really dirty way of + // not sending parameters to shaders more than we need. + this->renderId++; + + // Get the list of things to render first. + std::vector pipelineItems; + + // Meshes auto meshes = scene->findComponents(); - auto uiCanvasList = scene->findComponents(); + auto itMesh = meshes.begin(); + while(itMesh != meshes.end()) { + // Get Mesh + auto mesh = *itMesh; + assertNotNull(mesh->mesh); + + // Make sure this mesh has a material + auto mat = mesh->item->getComponent(); + assertNotNull(mat); + + auto shader = mat->getShader(); + assertNotNull(shader); + + // Now do each pass. + auto passes = shader->getItemPasses(mesh, mat); + auto itPass = passes.begin(); + while(itPass != passes.end()) { + struct RenderPipelineItem item; + item.mesh = mesh; + item.pass = *itPass; + + // Do we need to get the W Vector? + if(item.pass.needsW) { + assertUnreachable();// TODO: Add distance from camera for W vector. + } else { + item.w = 0; + } + + // Queue + pipelineItems.push_back(item); + ++itPass; + } + + // Now, for optimization, we bind the global parameters here, once for each + // shader. + if(shader->renderId != this->renderId) { + shader->setGlobalParameters( + camera->projection, + camera->transform->getWorldTransform() + ); + shader->renderId = this->renderId; + } + + ++itMesh; + } + + // TODO: Get UI stuff here. + + // Now we've queued everything, let's sort the rendering queue by the priority + std::sort( + pipelineItems.begin(), + pipelineItems.end(), + [](struct RenderPipelineItem &a, struct RenderPipelineItem &b){ + if(a.pass.orderShader == b.pass.orderShader) { + return a.w < b.w; + } + return a.pass.orderShader < b.pass.orderShader; + } + ); + + // Now we've sorted everything! Let's actually start rendering. + ShaderProgram *boundProgram = nullptr; + std::map boundTextures; + auto renderTarget = camera->getRenderTarget(); - assertNotNull(renderTarget); - + + // TODO: This will be editable! renderTarget->bind(); renderTarget->clear( RENDER_TARGET_CLEAR_FLAG_DEPTH | @@ -92,92 +162,67 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) { RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST | RENDER_MANAGER_RENDER_FLAG_BLEND ); - - // Render all Meshes on the scene - auto it = meshes.begin(); - while(it != meshes.end()) { - auto mesh = *it; - auto material = mesh->item->getComponent(); - // TODO: fallback material? - if(material == nullptr) { - ++it; - continue; + auto itItems = pipelineItems.begin(); + while(itItems != pipelineItems.end()) { + auto item = *itItems; + + // Bind the program. + if(boundProgram != item.pass.shaderProgram) { + boundProgram->bind(); + boundProgram = item.pass.shaderProgram; } - auto shader = material->getShader(); - assertNotNull(shader); - shader->bind(); - shader->setGlobalParameters(camera->projection, camera->transform->getWorldTransform()); - shader->setMeshParameters(mesh->item->transform.getWorldTransform()); - material->setShaderParameters(); - - mesh->mesh->draw(MESH_DRAW_MODE_TRIANGLES, 0, -1); - ++it; - } - - - // Now we only render world-absolute UI canvas' - auto uiShader = this->renderManager->getUIShader(); - assertNotNull(uiShader); - uiShader->bind(); - uiShader->setUICamera(camera->projection, camera->transform->getWorldTransform()); - this->renderManager->setRenderFlags(RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST); - - auto it2 = uiCanvasList.begin(); - while(it2 != uiCanvasList.end()) { - auto canvas = *it2; - - switch(canvas->drawType) { - case UI_DRAW_TYPE_WORLD_ABSOLUTE: - break; - - default: - ++it2; - continue; - } - - auto it3 = canvas->children.begin(); - auto rootMatrix = canvas->transform->getWorldTransform(); - while(it3 != canvas->children.end()) { - (*it3)->draw(uiShader, - glm::translate(glm::scale(rootMatrix, glm::vec3(1.0f, -1.0f, 1.0f)), glm::vec3(0, -renderTarget->getHeight(), 0)) - ); - ++it3; - } - ++it2; - } - - - // Now render camera-relative UI - this->renderManager->setRenderFlags( - RENDER_MANAGER_RENDER_FLAG_BLEND - ); - uiShader->setUICamera( - glm::ortho(0.0f, renderTarget->getWidth(), renderTarget->getHeight(), 0.0f), - glm::mat4(1.0f) - ); - - it2 = uiCanvasList.begin(); - while(it2 != uiCanvasList.end()) { - auto canvas = *it2; - - switch(canvas->drawType) { - case UI_DRAW_TYPE_WORLD_CAMERA_RELATIVE: - break; - - default: - ++it2; - continue; + // Bind the textures to the slots + auto itTextureSlot = item.pass.textureSlots.begin(); + while(itTextureSlot != item.pass.textureSlots.end()) { + if(boundTextures[itTextureSlot->first] != itTextureSlot->second) { + itTextureSlot->second->bind(itTextureSlot->first); + boundTextures[itTextureSlot->first] = itTextureSlot->second; + } + ++itTextureSlot; } - auto it3 = canvas->children.begin(); - auto rootMatrix = canvas->transform->getWorldTransform(); - while(it3 != canvas->children.end()) { - (*it3)->draw(uiShader, rootMatrix); - ++it3; + // Now set each of the parameters. Nothing exciting here. + auto itColors = item.pass.colorValues.begin(); + while(itColors != item.pass.colorValues.end()) { + item.pass.shaderProgram->setColor(itColors->first, itColors->second); + ++itColors; } - ++it2; + + auto itBool = item.pass.boolValues.begin(); + while(itBool != item.pass.boolValues.end()) { + item.pass.shaderProgram->setBoolean(itBool->first, itBool->second); + ++itBool; + } + + auto itMat = item.pass.matrixValues.begin(); + while(itMat != item.pass.matrixValues.end()) { + item.pass.shaderProgram->setMatrix(itMat->first, itMat->second); + ++itMat; + } + + auto itVec3 = item.pass.vec3Values.begin(); + while(itVec3 != item.pass.vec3Values.end()) { + item.pass.shaderProgram->setVector3(itVec3->first, itVec3->second); + ++itVec3; + } + + auto itText = item.pass.textureValues.begin(); + while(itText != item.pass.textureValues.end()) { + item.pass.shaderProgram->setTexture(itText->first, itText->second); + ++itText; + } + + auto itFloat = item.pass.floatValues.begin(); + while(itFloat != item.pass.floatValues.end()) { + item.pass.shaderProgram->setFloat(itFloat->first, itFloat->second); + ++itFloat; + } + + // Thank god that's done, now just draw the damn mesh. + item.mesh->mesh->draw(MESH_DRAW_MODE_TRIANGLES, 0, -1); + ++itItems; } } diff --git a/src/dawn/display/RenderPipeline.hpp b/src/dawn/display/RenderPipeline.hpp index 8758ecf1..5c5410b1 100644 --- a/src/dawn/display/RenderPipeline.hpp +++ b/src/dawn/display/RenderPipeline.hpp @@ -5,14 +5,25 @@ #pragma once #include "dawnlibs.hpp" -#include "display/RenderManager.hpp" +#include "scene/SceneItem.hpp" #include "scene/Scene.hpp" -#include "scene/components/Components.hpp" -#include "scene/components/ui/UICanvas.hpp" -#include "ui/UIComponent.hpp" +#include "scene/components/display/Material.hpp" +#include "scene/components/display/MeshRenderer.hpp" +#include "scene/components/display/Camera.hpp" namespace Dawn { + class RenderManager; + + struct RenderPipelineItem { + MeshRenderer *mesh; + struct ShaderPass pass; + float_t w; + }; + class RenderPipeline { + private: + int_fast16_t renderId; + public: RenderManager *renderManager; diff --git a/src/dawn/display/shader/Shader.hpp b/src/dawn/display/shader/Shader.hpp new file mode 100644 index 00000000..6ad78c9c --- /dev/null +++ b/src/dawn/display/shader/Shader.hpp @@ -0,0 +1,65 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "display/shader/ShaderProgram.hpp" +#include "scene/components/display/MeshRenderer.hpp" + +namespace Dawn { + class Material; + + struct ShaderPass { + ShaderProgram *shaderProgram; + int32_t orderShader; + bool_t needsW; + + // Parameters + std::map colorValues; + std::map boolValues; + std::map matrixValues; + std::map vec3Values; + std::map textureValues; + std::map floatValues; + + // Textures + std::map textureSlots; + }; + + class Shader { + public: + int_fast16_t renderId; + + /** + * Compile all programs for this shader. This amy not remain forever as I + * may decide to allow shaders to share programs somehow. For now though + * this is clean enough. + */ + virtual void compile() = 0; + + /** + * Returns the list of passes to render for the given scene item. + * + * @param mesh Mesh Renderer for the scene item. + * @param material Material for the scene item. + * @return List of passes to render. + */ + virtual std::vector getItemPasses( + MeshRenderer *mesh, + Material *material + ) = 0; + + /** + * Called once per frame, set the global shader parameters that is used by + * every item in the scene. + * + * @param cameraProjection Projection matrix of the camera. + * @param cameraView View matrix of the camera. + */ + virtual void setGlobalParameters( + glm::mat4 cameraProjection, + glm::mat4 cameraView + ) = 0; + }; +} \ No newline at end of file diff --git a/src/dawn/display/shader/_Shader.hpp b/src/dawn/display/shader/_Shader.hpp deleted file mode 100644 index ed8d4a8b..00000000 --- a/src/dawn/display/shader/_Shader.hpp +++ /dev/null @@ -1,115 +0,0 @@ -// Copyright (c) 2022 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#pragma once -#include "display/Texture.hpp" - -namespace Dawn { - class Material; - - enum ShaderParameterType { - SHADER_PARAMETER_TYPE_MATRIX, - SHADER_PARAMETER_TYPE_BOOLEAN, - SHADER_PARAMETER_TYPE_COLOR, - SHADER_PARAMETER_TYPE_VECTOR3, - SHADER_PARAMETER_TYPE_TEXTURE, - SHADER_PARAMETER_TYPE_FLOAT - }; - - template - class IShader { - public: - /** - * Attaches the supplied shader as the current shader. - */ - virtual void bind() = 0; - - /** - * Requested by the Material to set the default parameters of the shader. - * Each parameter really should have a default value set so that there is - * no nullptr's or other issues. - * - * @param material Material to set the default parameters on to. - */ - virtual void setDefaultParameters(Material *material) = 0; - - /** - * Requested by the render pipeline (typically) to set global level (once - * per frame) parameters. - * - * @param projection Projection matrix of the current viewport. - * @param view View matrix of the current viewport. - */ - virtual void setGlobalParameters( - glm::mat4 projection, - glm::mat4 view - ) = 0; - - /** - * Requested by the render pipeline (typically) to set mesh-level params. - * This may be performed multiple times per frame. - * - * @param position Matrix of the position of the mesh. - */ - virtual void setMeshParameters(glm::mat4 position) = 0; - - /** - * Retreives the list of all parameters that the shader supports. This - * should not include the GLOBAL shader parameters (listed above) since - * those will be modified by the engine directly. - * - * @return Key-Value-Pair of Shader parameters and their type. - */ - virtual std::map getParameters() = 0; - - /** - * Set's a specific shader parameter to a matrix. - * - * @param parameter parameter on the shader to set. - * @param matrix Matrix to apply. - */ - virtual void setMatrix(T parameter, glm::mat4 matrix) = 0; - - /** - * Attaches a boolean to a shader. - * - * @param parameter parameter to set. - * @param value Value to set. - */ - virtual void setBoolean(T parameter, bool_t value) = 0; - - /** - * Set a color on to the shader. - * - * @param parameter parameter to set the color to. - * @param color Color to set. - */ - virtual void setColor(T parameter, struct Color color) = 0; - - /** - * Set a 3D vector on to the shader. - * - * @param parameter parameter to set the vector to. - * @param vector Vector to set. - */ - virtual void setVector3(T parameter, glm::vec3 vector) = 0; - - /** - * Attaches a texture to the currently bound shader. - * - * @param parameter parameter to set the texture on to. - * @param texture Texture to bind to the parameter. - */ - virtual void setTexture(T parameter, Texture *texture) = 0; - - /** - * Sets a floating point value to the shader. - * - * @param parameter Paramater to set the float ont o. - * @param Float to bind. - */ - virtual void setFloat(T parameter, float_t value) = 0; - }; -} \ No newline at end of file diff --git a/src/dawn/display/shader/_ShaderProgram.hpp b/src/dawn/display/shader/_ShaderProgram.hpp new file mode 100644 index 00000000..1a87f2d5 --- /dev/null +++ b/src/dawn/display/shader/_ShaderProgram.hpp @@ -0,0 +1,76 @@ +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "display/Texture.hpp" + +namespace Dawn { + // class Material; + // enum ShaderParameterType { + // SHADER_PARAMETER_TYPE_MATRIX, + // SHADER_PARAMETER_TYPE_BOOLEAN, + // SHADER_PARAMETER_TYPE_COLOR, + // SHADER_PARAMETER_TYPE_VECTOR3, + // SHADER_PARAMETER_TYPE_TEXTURE, + // SHADER_PARAMETER_TYPE_FLOAT + // }; + + template + class IShaderProgram { + public: + /** + * Attaches the supplied shader as the current shader. + */ + virtual void bind() = 0; + + /** + * Set's a specific shader parameter to a matrix. + * + * @param parameter parameter on the shader to set. + * @param matrix Matrix to apply. + */ + virtual void setMatrix(T parameter, glm::mat4 matrix) = 0; + + /** + * Attaches a boolean to a shader. + * + * @param parameter parameter to set. + * @param value Value to set. + */ + virtual void setBoolean(T parameter, bool_t value) = 0; + + /** + * Set a color on to the shader. + * + * @param parameter parameter to set the color to. + * @param color Color to set. + */ + virtual void setColor(T parameter, struct Color color) = 0; + + /** + * Set a 3D vector on to the shader. + * + * @param parameter parameter to set the vector to. + * @param vector Vector to set. + */ + virtual void setVector3(T parameter, glm::vec3 vector) = 0; + + /** + * Attaches a texture to the currently bound shader. + * + * @param parameter parameter to set the texture on to. + * @param texture Texture slot to bind to the parameter. + */ + virtual void setTexture(T parameter, textureslot_t texture) = 0; + + /** + * Sets a floating point value to the shader. + * + * @param parameter Paramater to set the float ont o. + * @param Float to bind. + */ + virtual void setFloat(T parameter, float_t value) = 0; + }; +} \ No newline at end of file diff --git a/src/dawn/scene/components/display/Material.cpp b/src/dawn/scene/components/display/Material.cpp index c142e8d9..1be1e70a 100644 --- a/src/dawn/scene/components/display/Material.cpp +++ b/src/dawn/scene/components/display/Material.cpp @@ -10,61 +10,4 @@ using namespace Dawn; Material::Material(SceneItem *item) : SceneItemComponent(item) { - this->shader = item->scene->game->renderManager.getDefaultShader(); - this->updateShaderParameters(); -} - -void Material::updateShaderParameters() { - this->colorValues.clear(); - this->boolValues.clear(); - - this->parameters = this->shader->getParameters(); - this->shader->setDefaultParameters(this); - - // We do need to validate these params at some point to make sure that the - // shader has actually bound them. -} - -void Material::setShaderParameters() { - auto it = this->parameters.begin(); - while(it != this->parameters.end()) { - switch(it->second) { - case SHADER_PARAMETER_TYPE_COLOR: - this->shader->setColor(it->first, this->colorValues[it->first]); - break; - - case SHADER_PARAMETER_TYPE_MATRIX: - this->shader->setMatrix(it->first, this->matrixValues[it->first]); - break; - - case SHADER_PARAMETER_TYPE_BOOLEAN: - this->shader->setBoolean(it->first, this->boolValues[it->first]); - break; - - case SHADER_PARAMETER_TYPE_VECTOR3: - this->shader->setVector3(it->first, this->vec3Values[it->first]); - break; - - case SHADER_PARAMETER_TYPE_TEXTURE: - this->shader->setTexture(it->first, this->textureValues[it->first]); - break; - - case SHADER_PARAMETER_TYPE_FLOAT: - this->shader->setFloat(it->first, this->floatValues[it->first]); - break; - - default: - throw "An unsupported or invalid shader parameter type was supplied."; - } - ++it; - } -} - -Shader * Material::getShader() { - return this->shader; -} - -void Material::setShader(Shader * shader) { - this->shader = shader; - this->updateShaderParameters(); } \ No newline at end of file diff --git a/src/dawn/scene/components/display/Material.hpp b/src/dawn/scene/components/display/Material.hpp index 017f0d47..14b75c97 100644 --- a/src/dawn/scene/components/display/Material.hpp +++ b/src/dawn/scene/components/display/Material.hpp @@ -8,56 +8,20 @@ #include "display/shader/Shader.hpp" namespace Dawn { - class Shader; - class Material : public SceneItemComponent { - private: - Shader *shader = nullptr; - - /** - * Internal method that will be invoked to go through and update all of - * the shader parameters whenever the shader is swapped out. - */ - void updateShaderParameters(); - public: - std::map parameters; - std::map colorValues; - std::map boolValues; - std::map matrixValues; - std::map vec3Values; - std::map textureValues; - std::map floatValues; - /** * Material component constructor. * * @param item Scene Item this component belongs to. */ Material(SceneItem *item); - + /** - * Return the shader this material is currently using. + * Returns the shader that this material uses. * - * @return Shader pointer to the currently bound shader. + * @return Shader that belongs to this material. */ - Shader * getShader(); - - /** - * Sets the shader for the material to use. This will also clear and - * update all of the parameters from the shaders' default parameter list. - * - * @param shader Shader to set. - */ - void setShader(Shader * shader); - - /** - * Protected method that can be called, likely by the render pipeline, to - * set and update all of the shader parameters from this material on to - * the shader. - * - * This method assumes that the shader has already been bound. - */ - void setShaderParameters(); + virtual Shader * getShader() = 0; }; } \ No newline at end of file diff --git a/src/dawn/scene/components/display/ShaderInterface.hpp b/src/dawn/scene/components/display/ShaderInterface.hpp deleted file mode 100644 index 304a903f..00000000 --- a/src/dawn/scene/components/display/ShaderInterface.hpp +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (c) 2023 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#pragma once -#include "scene/components/display/Material.hpp" -#include "scene/SceneItem.hpp" - -namespace Dawn { - template - class ShaderInterface : public SceneItemComponent { - public: - /** - * ShaderInterface to provide a common interface language for all shaders - * that can be referenced by scene components. - * - * @param i SceneItem this Shader interface belongs to. - */ - ShaderInterface(SceneItem *i) : SceneItemComponent(i) {} - - /** - * Sets the shader for this shader interface to use. Will also update the - * underlying material for you. - * - * @param shader Shader to use for this interface. - */ - void setShader(T *shader) { - assertNotNull(shader); - this->getMaterial()->setShader(shader); - } - - /** - * Returns the shader, assuming that the material has the correct shader. - * - * @return Pointer to the shader. - */ - T * getShader() { - auto material = dynamic_cast(this->getMaterial()->getShader()); - return material; - } - - /** - * Returns the material attached to this scene item. - * - * @return The attached material. - */ - Material * getMaterial() { - auto mat = this->item->getComponent(); - assertNotNull(mat); - return mat; - } - - std::vector getDependencies() override { - return std::vector{ - this->item->getComponent() - }; - } - }; -} \ No newline at end of file diff --git a/src/dawnopengl/display/CMakeLists.txt b/src/dawnopengl/display/CMakeLists.txt index 35d44d6b..db9c272a 100644 --- a/src/dawnopengl/display/CMakeLists.txt +++ b/src/dawnopengl/display/CMakeLists.txt @@ -8,7 +8,6 @@ target_sources(${DAWN_TARGET_NAME} PRIVATE RenderManager.cpp BackBufferRenderTarget.cpp - StandardRenderPipeline.cpp Texture.cpp TextureRenderTarget.cpp ) diff --git a/src/dawnopengl/display/RenderManager.cpp b/src/dawnopengl/display/RenderManager.cpp index 0555d4b7..acd3f5d8 100644 --- a/src/dawnopengl/display/RenderManager.cpp +++ b/src/dawnopengl/display/RenderManager.cpp @@ -6,21 +6,20 @@ #include "dawnopengl.hpp" #include "game/DawnGame.hpp" #include "display/RenderManager.hpp" -#include "display/StandardRenderPipeline.hpp" using namespace Dawn; RenderManager::RenderManager(DawnGame *game) : IRenderManager(game), - backBuffer(*this) + backBuffer(*this), + renderPipeline(this) { - this->standardRenderPipeline = new StandardRenderPipeline(this); this->simpleShader = new SimpleTexturedShader(); this->uiShader = new UIShader(); } void RenderManager::init() { - this->standardRenderPipeline->init(); + this->renderPipeline.init(); this->simpleShader->compile(); this->uiShader->compile(); @@ -36,7 +35,7 @@ RenderTarget * RenderManager::getBackBuffer() { } RenderPipeline * RenderManager::getRenderPipeline() { - return this->standardRenderPipeline; + return &this->renderPipeline; } Shader * RenderManager::getDefaultShader() { @@ -68,7 +67,6 @@ void RenderManager::update() { } RenderManager::~RenderManager() { - delete this->standardRenderPipeline; delete this->simpleShader; delete this->uiShader; } \ No newline at end of file diff --git a/src/dawnopengl/display/RenderManager.hpp b/src/dawnopengl/display/RenderManager.hpp index 27cbd8fe..ea3266c4 100644 --- a/src/dawnopengl/display/RenderManager.hpp +++ b/src/dawnopengl/display/RenderManager.hpp @@ -7,13 +7,12 @@ #include "display/_RenderManager.hpp" #include "display/BackBufferRenderTarget.hpp" #include "display/shader/SimpleTexturedShader.hpp" +#include "display/RenderPipeline.hpp" namespace Dawn { - class StandardRenderPipeline; - class RenderManager : public IRenderManager { private: - StandardRenderPipeline *standardRenderPipeline; + RenderPipeline renderPipeline; public: BackBufferRenderTarget backBuffer; diff --git a/src/dawnopengl/display/StandardRenderPipeline.cpp b/src/dawnopengl/display/StandardRenderPipeline.cpp deleted file mode 100644 index eb70d188..00000000 --- a/src/dawnopengl/display/StandardRenderPipeline.cpp +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright (c) 2022 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#include "StandardRenderPipeline.hpp" -#include "scene/components/Components.hpp" - -using namespace Dawn; - -StandardRenderPipeline::StandardRenderPipeline(RenderManager *renderManager) : - RenderPipeline(renderManager) -{ -} \ No newline at end of file diff --git a/src/dawnopengl/display/StandardRenderPipeline.hpp b/src/dawnopengl/display/StandardRenderPipeline.hpp deleted file mode 100644 index febe9333..00000000 --- a/src/dawnopengl/display/StandardRenderPipeline.hpp +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) 2022 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#pragma once -#include "dawnopengl.hpp" -#include "display/RenderPipeline.hpp" - -namespace Dawn { - class StandardRenderPipeline : public RenderPipeline { - public: - StandardRenderPipeline(RenderManager *renderManager); - }; -} \ No newline at end of file diff --git a/src/dawnopengl/display/shader/CMakeLists.txt b/src/dawnopengl/display/shader/CMakeLists.txt index d66d1bd2..25d16ee2 100644 --- a/src/dawnopengl/display/shader/CMakeLists.txt +++ b/src/dawnopengl/display/shader/CMakeLists.txt @@ -6,5 +6,5 @@ # Sources target_sources(${DAWN_TARGET_NAME} PRIVATE - Shader.cpp + ShaderProgram.cpp ) \ No newline at end of file diff --git a/src/dawnopengl/display/shader/Shader.cpp b/src/dawnopengl/display/shader/ShaderProgram.cpp similarity index 76% rename from src/dawnopengl/display/shader/Shader.cpp rename to src/dawnopengl/display/shader/ShaderProgram.cpp index f8238bde..7a3b8f4e 100644 --- a/src/dawnopengl/display/shader/Shader.cpp +++ b/src/dawnopengl/display/shader/ShaderProgram.cpp @@ -3,11 +3,11 @@ // This software is released under the MIT License. // https://opensource.org/licenses/MIT -#include "Shader.hpp" +#include "ShaderProgram.hpp" using namespace Dawn; -void Shader::compileShader( +void ShaderProgram::compileShader( std::string vertexShader, std::string fragmentShader ) { @@ -61,51 +61,40 @@ void Shader::compileShader( // Now parse out the variables. } -void Shader::setTextureSlot(shaderparameter_t param, textureslot_t slot) { +void ShaderProgram::setTexture(shaderparameter_t param, textureslot_t slot) { glUniform1i(param, slot); } -shaderparameter_t Shader::getParameterByName(std::string name) { +shaderparameter_t ShaderProgram::getParameterByName(std::string name) { return glGetUniformLocation(this->shaderProgram, name.c_str()); } -void Shader::setMatrix(shaderparameter_t uniform, glm::mat4 matrix) { +void ShaderProgram::setMatrix(shaderparameter_t uniform, glm::mat4 matrix) { glUniformMatrix4fv(uniform, 1, GL_FALSE, glm::value_ptr(matrix)); } -void Shader::setBoolean(shaderparameter_t uni, bool value) { +void ShaderProgram::setBoolean(shaderparameter_t uni, bool value) { glUniform1i(uni, value); } -void Shader::setColor(shaderparameter_t uniform, struct Color color) { +void ShaderProgram::setColor(shaderparameter_t uniform, struct Color color) { glUniform4f(uniform, color.r, color.g, color.b, color.a); } -void Shader::setVector3(shaderparameter_t uniform, glm::vec3 vector) { +void ShaderProgram::setVector3(shaderparameter_t uniform, glm::vec3 vector) { glUniform3f(uniform, vector.x, vector.y, vector.z); } -void Shader::setTexture( - shaderparameter_t param, - Texture *texture -) { - if(texture == nullptr || !texture->isReady()) { - this->bindTexture(param, nullptr); - return; - } - this->bindTexture(param, texture); -} - -void Shader::setFloat(shaderparameter_t param, float_t value) { +void ShaderProgram::setFloat(shaderparameter_t param, float_t value) { glUniform1f(param, value); } -void Shader::bind() { +void ShaderProgram::bind() { if(this->shaderProgram == -1) throw "Shader has not yet been compiled"; glUseProgram(this->shaderProgram); } -Shader::~Shader() { +ShaderProgram::~ShaderProgram() { if(this->shaderProgram != -1) glDeleteProgram(this->shaderProgram); if(this->shaderVertex != -1) glDeleteShader(this->shaderVertex); if(this->shaderFrag != -1) glDeleteShader(this->shaderFrag); diff --git a/src/dawnopengl/display/shader/Shader.hpp b/src/dawnopengl/display/shader/ShaderProgram.hpp similarity index 64% rename from src/dawnopengl/display/shader/Shader.hpp rename to src/dawnopengl/display/shader/ShaderProgram.hpp index 54c0d5bb..280a4a02 100644 --- a/src/dawnopengl/display/shader/Shader.hpp +++ b/src/dawnopengl/display/shader/ShaderProgram.hpp @@ -4,14 +4,14 @@ // https://opensource.org/licenses/MIT #pragma once -#include "display/shader/_Shader.hpp" +#include "display/shader/_ShaderProgram.hpp" #include "dawnopengl.hpp" #include "display/Color.hpp" typedef GLuint shaderparameter_t; namespace Dawn { - class Shader : public IShader { + class ShaderProgram : public IShaderProgram { private: /** Pointer to an uploaded vertex shader program */ GLuint shaderVertex = -1; @@ -32,28 +32,6 @@ namespace Dawn { */ void compileShader(std::string vertexShader, std::string fragmentShader); - /** - * Bind a specific texture slot (of an already bound texture) to a shader - * parameter. - * - * @param param Parameter to bind the texture slot for. - * @param slot Slot to bind. - */ - void setTextureSlot(shaderparameter_t param, textureslot_t slot); - - /** - * Method designed to be overwritten by child shaders on how to handle a - * bind texture request. This is left to the discretion of the child - * shader for which slot(s) to use, how to handle nullptr textures, etc. - * - * @param param Parameter to bind the requested texture to. - * @param texture Texture trying to be bound, may be nullptr. - */ - virtual void bindTexture( - shaderparameter_t param, - Texture *texture - ) = 0; - public: /** @@ -75,12 +53,12 @@ namespace Dawn { void setBoolean(shaderparameter_t parameter, bool_t value) override; void setColor(shaderparameter_t parameter, struct Color color) override; void setVector3(shaderparameter_t parameter, glm::vec3 vector) override; - void setTexture(shaderparameter_t parameter, Texture *texture) override; + void setTexture(shaderparameter_t parameter, textureslot_t texture) override; void setFloat(shaderparameter_t parameter, float_t value) override; /** * Destroys and deletes the shader from the GPU. */ - virtual ~Shader(); + virtual ~ShaderProgram(); }; } \ No newline at end of file diff --git a/src/dawnopengl/display/shader/SimpleTexturedShader.hpp b/src/dawnopengl/display/shader/SimpleTexturedShader.hpp index c1713a59..f33bcbb6 100644 --- a/src/dawnopengl/display/shader/SimpleTexturedShader.hpp +++ b/src/dawnopengl/display/shader/SimpleTexturedShader.hpp @@ -1,102 +1,33 @@ -// Copyright (c) 2022 Dominic Masters +// 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" -#include "scene/components/Components.hpp" +#include "SimpleTexturedShaderProgram.hpp" namespace Dawn { class SimpleTexturedShader : public Shader { + protected: + SimpleTexturedShaderProgram program; + public: - shaderparameter_t paramProjection; - shaderparameter_t paramView; - shaderparameter_t paramModel; - shaderparameter_t paramColor; - shaderparameter_t paramTexture; - shaderparameter_t paramHasTexture; - - std::map - getParameters() override { - std::map ps; - - ps[this->paramColor] = SHADER_PARAMETER_TYPE_COLOR; - ps[this->paramHasTexture] = SHADER_PARAMETER_TYPE_BOOLEAN; - ps[this->paramTexture] = SHADER_PARAMETER_TYPE_TEXTURE; - - return ps; - } - - void setDefaultParameters(Material *material) override { - material->colorValues[this->paramColor] = COLOR_WHITE; - material->textureValues[this->paramTexture] = nullptr; - } - - void setGlobalParameters(glm::mat4 proj, glm::mat4 view) override { - this->setMatrix(this->paramProjection, proj); - this->setMatrix(this->paramView, view); - } - - void setMeshParameters(glm::mat4 transform) override { - this->setMatrix(this->paramModel, transform); - } - - void bindTexture( - shaderparameter_t param, - Texture *texture - ) override { - if(texture == nullptr || !texture->isReady()) { - this->setBoolean(this->paramHasTexture, false); - } else { - this->setBoolean(this->paramHasTexture, true); - this->setTextureSlot(param, 0x01); - texture->bind(0x01); - } - } - void compile() override { - this->compileShader( - // Vertex Shader - "#version 330 core\n" - "layout (location = 0) in vec3 aPos;\n" - "layout (location = 1) in vec2 aTexCoord;\n" + this->program.compile(); + } - "uniform mat4 u_Proj;\n" - "uniform mat4 u_View;\n" - "uniform mat4 u_Model;\n" + std::vector getItemPasses( + MeshRenderer *mesh, + Material *material + ) override { + return std::vector(); + } - "out vec2 o_TextCoord;\n" - "void main() {\n" - "gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n" - "o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n" - "}", - - // Fragment Shader - "#version 330 core\n" - "in vec2 o_TextCoord;\n" - "out vec4 o_Color;\n" - "uniform vec4 u_Color;\n" - "uniform bool u_HasTexture;\n" - "uniform sampler2D u_Text;\n" - - "void main() {\n" - "if(u_HasTexture) {\n" - "o_Color = texture(u_Text, o_TextCoord) * u_Color;\n" - "} else {\n" - "o_Color = u_Color;" - "}\n" - "}\n" - ); - - this->paramProjection = this->getParameterByName("u_Proj"); - this->paramView = this->getParameterByName("u_View"); - this->paramModel = this->getParameterByName("u_Model"); - this->paramColor = this->getParameterByName("u_Color"); - this->paramTexture = this->getParameterByName("u_Text"); - this->paramHasTexture = this->getParameterByName("u_HasTexture"); - - this->setBoolean(this->paramHasTexture, false); + void setGlobalParameters( + glm::mat4 cameraProjection, + glm::mat4 cameraView + ) override { } }; } \ No newline at end of file diff --git a/src/dawnopengl/display/shader/SimpleTexturedShaderProgram.hpp b/src/dawnopengl/display/shader/SimpleTexturedShaderProgram.hpp new file mode 100644 index 00000000..fafa4741 --- /dev/null +++ b/src/dawnopengl/display/shader/SimpleTexturedShaderProgram.hpp @@ -0,0 +1,62 @@ +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "display/shader/Shader.hpp" +#include "scene/components/Components.hpp" + +namespace Dawn { + class SimpleTexturedShaderProgram : public ShaderProgram { + public: + shaderparameter_t paramProjection; + shaderparameter_t paramView; + shaderparameter_t paramModel; + shaderparameter_t paramColor; + shaderparameter_t paramTexture; + shaderparameter_t paramHasTexture; + + void compile() override { + this->compileShader( + // Vertex Shader + "#version 330 core\n" + "layout (location = 0) in vec3 aPos;\n" + "layout (location = 1) in vec2 aTexCoord;\n" + + "uniform mat4 u_Proj;\n" + "uniform mat4 u_View;\n" + "uniform mat4 u_Model;\n" + + "out vec2 o_TextCoord;\n" + "void main() {\n" + "gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n" + "o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n" + "}", + + // Fragment Shader + "#version 330 core\n" + "in vec2 o_TextCoord;\n" + "out vec4 o_Color;\n" + "uniform vec4 u_Color;\n" + "uniform bool u_HasTexture;\n" + "uniform sampler2D u_Text;\n" + + "void main() {\n" + "if(u_HasTexture) {\n" + "o_Color = texture(u_Text, o_TextCoord) * u_Color;\n" + "} else {\n" + "o_Color = u_Color;" + "}\n" + "}\n" + ); + + this->paramProjection = this->getParameterByName("u_Proj"); + this->paramView = this->getParameterByName("u_View"); + this->paramModel = this->getParameterByName("u_Model"); + this->paramColor = this->getParameterByName("u_Color"); + this->paramTexture = this->getParameterByName("u_Text"); + this->paramHasTexture = this->getParameterByName("u_HasTexture"); + } + }; +} \ No newline at end of file diff --git a/src/dawnopengl/display/shader/UIShader.hpp b/src/dawnopengl/display/shader/UIShader.hpp index 8722c381..d8dcf431 100644 --- a/src/dawnopengl/display/shader/UIShader.hpp +++ b/src/dawnopengl/display/shader/UIShader.hpp @@ -4,116 +4,9 @@ // https://opensource.org/licenses/MIT #pragma once -#include "display/shader/Shader.hpp" -#include "scene/components/Components.hpp" +#include "SimpleTexturedShader.hpp" namespace Dawn { - class UIShader : public Shader { - public: - shaderparameter_t paramProjection; - shaderparameter_t paramView; - shaderparameter_t paramModel; - shaderparameter_t paramColor; - shaderparameter_t paramTexture; - shaderparameter_t paramHasTexture; - - std::map - getParameters() override { - std::map ps; - - ps[this->paramColor] = SHADER_PARAMETER_TYPE_COLOR; - ps[this->paramHasTexture] = SHADER_PARAMETER_TYPE_BOOLEAN; - ps[this->paramTexture] = SHADER_PARAMETER_TYPE_TEXTURE; - - return ps; - } - - void setDefaultParameters(Material *material) override { - material->colorValues[this->paramColor] = COLOR_WHITE; - material->textureValues[this->paramTexture] = nullptr; - } - - void setGlobalParameters(glm::mat4 proj, glm::mat4 view) override { - this->setMatrix(this->paramProjection, proj); - this->setMatrix(this->paramView, view); - } - - void setMeshParameters(glm::mat4 transform) override { - this->setMatrix(this->paramModel, transform); - } - - void bindTexture( - shaderparameter_t param, - Texture *texture - ) override { - if(texture == nullptr || !texture->isReady()) { - this->setBoolean(this->paramHasTexture, false); - } else { - this->setBoolean(this->paramHasTexture, true); - this->setTextureSlot(param, 0x00); - texture->bind(0x00); - } - } - - void compile() override { - this->compileShader( - // Vertex Shader - "#version 330 core\n" - "layout (location = 0) in vec3 aPos;\n" - "layout (location = 1) in vec2 aTexCoord;\n" - - "uniform mat4 u_Proj;\n" - "uniform mat4 u_View;\n" - "uniform mat4 u_Model;\n" - - "out vec2 o_TextCoord;\n" - "void main() {\n" - "gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n" - "o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n" - "}", - - // Fragment Shader - "#version 330 core\n" - "in vec2 o_TextCoord;\n" - "out vec4 o_Color;\n" - "uniform vec4 u_Color;\n" - "uniform bool u_HasTexture;\n" - "uniform sampler2D u_Text;\n" - - "void main() {\n" - "if(u_HasTexture) {\n" - "o_Color = texture(u_Text, o_TextCoord) * u_Color;\n" - "} else {\n" - "o_Color = u_Color;" - "}\n" - "}\n" - ); - - this->paramProjection = this->getParameterByName("u_Proj"); - this->paramView = this->getParameterByName("u_View"); - this->paramModel = this->getParameterByName("u_Model"); - this->paramColor = this->getParameterByName("u_Color"); - this->paramTexture = this->getParameterByName("u_Text"); - this->paramHasTexture = this->getParameterByName("u_HasTexture"); - - this->setBoolean(this->paramHasTexture, false); - } - - void setUICamera(glm::mat4 projection, glm::mat4 view) { - this->setMatrix(this->paramProjection, projection); - this->setMatrix(this->paramView, view); - } - - void setUIModel(glm::mat4 model) { - this->setMatrix(this->paramModel, model); - } - - void setUITexture(Texture *texture) { - this->bindTexture(this->paramTexture, texture); - } - - void setUIColor(struct Color color) { - this->setColor(this->paramColor, color); - } + class UIShader : public SimpleTexturedShader { }; } \ No newline at end of file diff --git a/src/dawnopengl/scene/components/display/CMakeLists.txt b/src/dawnopengl/scene/components/display/CMakeLists.txt index 82ceb310..2c73585d 100644 --- a/src/dawnopengl/scene/components/display/CMakeLists.txt +++ b/src/dawnopengl/scene/components/display/CMakeLists.txt @@ -4,4 +4,4 @@ # https://opensource.org/licenses/MIT # Subdirs -add_subdirectory(shader) \ No newline at end of file +add_subdirectory(material) \ No newline at end of file diff --git a/src/dawnopengl/scene/components/display/shader/CMakeLists.txt b/src/dawnopengl/scene/components/display/material/CMakeLists.txt similarity index 83% rename from src/dawnopengl/scene/components/display/shader/CMakeLists.txt rename to src/dawnopengl/scene/components/display/material/CMakeLists.txt index 738c9005..97005fae 100644 --- a/src/dawnopengl/scene/components/display/shader/CMakeLists.txt +++ b/src/dawnopengl/scene/components/display/material/CMakeLists.txt @@ -6,5 +6,5 @@ # Sources target_sources(${DAWN_TARGET_NAME} PRIVATE - SimpleTexturedShaderInterface.cpp + SimpleTexturedMaterial.cpp ) \ 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 new file mode 100644 index 00000000..025695fa --- /dev/null +++ b/src/dawnopengl/scene/components/display/material/SimpleTexturedMaterial.cpp @@ -0,0 +1,17 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "SimpleTexturedMaterial.hpp" + +using namespace Dawn; + +SimpleTexturedMaterial::SimpleTexturedMaterial(SceneItem *i) : + Material(i) +{ +} + +Shader * SimpleTexturedMaterial::getShader() { + return this->shader; +} \ 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 new file mode 100644 index 00000000..fb143006 --- /dev/null +++ b/src/dawnopengl/scene/components/display/material/SimpleTexturedMaterial.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 "display/shader/SimpleTexturedShader.hpp" +#include "scene/components/display/Material.hpp" + +namespace Dawn { + class SimpleTexturedMaterial : public Material { + public: + SimpleTexturedShader *shader = nullptr; + Texture *texture = nullptr; + struct Color color = COLOR_WHITE; + + /** + * SimpleTexturedShader scene item component interface. + * + * @param i Scene Item this interface belongs to. + */ + SimpleTexturedMaterial(SceneItem *i); + + Shader * getShader() override; + }; +} \ No newline at end of file diff --git a/src/dawnopengl/scene/components/display/shader/SimpleTexturedShaderInterface.cpp b/src/dawnopengl/scene/components/display/shader/SimpleTexturedShaderInterface.cpp deleted file mode 100644 index 92846719..00000000 --- a/src/dawnopengl/scene/components/display/shader/SimpleTexturedShaderInterface.cpp +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) 2023 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#include "SimpleTexturedShaderInterface.hpp" - -using namespace Dawn; - -SimpleTexturedShaderInterface::SimpleTexturedShaderInterface(SceneItem *i) : - ShaderInterface(i) -{ -} - - -void SimpleTexturedShaderInterface::setTexture(Texture *texture) { - this->getMaterial()->textureValues[this->getShader()->paramTexture] = texture; -} - -void SimpleTexturedShaderInterface::setColor(struct Color color) { - this->getMaterial()->colorValues[this->getShader()->paramColor] = color; -} - -struct Color SimpleTexturedShaderInterface::getColor() { - return this->getMaterial()->colorValues[this->getShader()->paramColor]; -} \ No newline at end of file diff --git a/src/dawnopengl/scene/components/display/shader/SimpleTexturedShaderInterface.hpp b/src/dawnopengl/scene/components/display/shader/SimpleTexturedShaderInterface.hpp deleted file mode 100644 index ee9162db..00000000 --- a/src/dawnopengl/scene/components/display/shader/SimpleTexturedShaderInterface.hpp +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright (c) 2023 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#pragma once -#include "scene/components/display/ShaderInterface.hpp" -#include "display/shader/SimpleTexturedShader.hpp" - -namespace Dawn { - class SimpleTexturedShaderInterface : - public ShaderInterface - { - public: - /** - * SimpleTexturedShader scene item component interface. - * - * @param i Scene Item this interface belongs to. - */ - SimpleTexturedShaderInterface(SceneItem *i); - - /** - * Sets the primary colour texture to be used by the shader. - * - * @param texture Texture to use. - */ - void setTexture(Texture *texture); - - /** - * Sets the multiplicitive color to be used by the shader. - * - * @param color Color to be used. - */ - void setColor(struct Color color); - - /** - * Returns the current color from the shader. - * - * @return Current color. - */ - struct Color getColor(); - }; -} \ No newline at end of file