First working example

This commit is contained in:
2023-05-29 19:50:04 -07:00
parent 96dd33c6b8
commit 8da23b123a
21 changed files with 263 additions and 161 deletions

View File

@ -93,6 +93,8 @@ void RenderPipeline::init() {
// 0.0f // 0.0f
// ); // );
// glyphMesh.draw(MESH_DRAW_MODE_TRIANGLES, 0, -1); // glyphMesh.draw(MESH_DRAW_MODE_TRIANGLES, 0, -1);
this->shaderBuffer.init();
} }
void RenderPipeline::render() { void RenderPipeline::render() {
@ -167,6 +169,11 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) {
assertNotNull(renderTarget); assertNotNull(renderTarget);
// Update shader parameter buffers with current knowledge // Update shader parameter buffers with current knowledge
struct RenderPipelineShaderBufferData shaderBufferData;
shaderBufferData.projection = camera->getProjection();
shaderBufferData.view = camera->transform->getWorldTransform();
this->shaderBuffer.buffer(&shaderBufferData);
this->camera = camera; this->camera = camera;
// Get the list of things to render first. // Get the list of things to render first.
@ -219,6 +226,9 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) {
// Now we've sorted everything! Let's actually start rendering. // Now we've sorted everything! Let's actually start rendering.
Shader *boundShader = nullptr; Shader *boundShader = nullptr;
std::map<textureslot_t, Texture*> boundTextures; std::map<textureslot_t, Texture*> boundTextures;
std::map<shaderbufferlocation_t, shaderbufferslot_t> locationSlotMap;
std::map<IShaderParameterBuffer<shaderbufferlocation_t>*, shaderbufferslot_t> bufferSlotMap;
shaderbufferslot_t globalSlot = 0;
// TODO: This will be editable! // TODO: This will be editable!
renderTarget->bind(); renderTarget->bind();
@ -251,6 +261,29 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) {
++itTextureSlot; ++itTextureSlot;
} }
// Bind the buffers to their slots
auto itBufferSlot = item.parameterBuffers.begin();
while(itBufferSlot != item.parameterBuffers.end()) {
// First check if buffer is already bound
auto location = itBufferSlot->first;
auto buff = itBufferSlot->second;
// auto existingBind = bufferSlotMap.find(buff);
shaderbufferslot_t slot;
// if(existingBind == bufferSlotMap.end()) {
// Not bound, let's find a slot we can use!
// slot = -1;
slot = globalSlot++;
// } else {
// // Already bound
// slot = existingBind->second;
// }
buff->bind(slot);
locationSlotMap[itBufferSlot->first] = slot;
++itBufferSlot;
}
// Now set each of the parameters. Nothing exciting here. // Now set each of the parameters. Nothing exciting here.
auto itColors = item.colorValues.begin(); auto itColors = item.colorValues.begin();
while(itColors != item.colorValues.end()) { while(itColors != item.colorValues.end()) {
@ -282,6 +315,12 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) {
++itText; ++itText;
} }
auto itBuffer = item.parameterBuffers.begin();
while(itBuffer != item.parameterBuffers.end()) {
item.shader->setParameterBuffer(itBuffer->first, locationSlotMap[itBuffer->first]);
++itBuffer;
}
auto itFloat = item.floatValues.begin(); auto itFloat = item.floatValues.begin();
while(itFloat != item.floatValues.end()) { while(itFloat != item.floatValues.end()) {
item.shader->setFloat(itFloat->first, itFloat->second); item.shader->setFloat(itFloat->first, itFloat->second);

View File

@ -10,6 +10,7 @@
#include "scene/components/scene/SubSceneController.hpp" #include "scene/components/scene/SubSceneController.hpp"
#include "scene/components/ui/UIComponent.hpp" #include "scene/components/ui/UIComponent.hpp"
#include "display/shader/ShaderPass.hpp" #include "display/shader/ShaderPass.hpp"
#include "display/shader/buffers/RenderPipelineShaderBuffer.hpp"
namespace Dawn { namespace Dawn {
class RenderManager; class RenderManager;
@ -23,6 +24,7 @@ namespace Dawn {
// Temporary hack // Temporary hack
Camera *camera = nullptr; Camera *camera = nullptr;
RenderPipelineShaderBuffer shaderBuffer;
/** /**
* Constructs a new RenderPipeline. Render Pipelines are my attempt to * Constructs a new RenderPipeline. Render Pipelines are my attempt to

View File

@ -6,6 +6,7 @@
#pragma once #pragma once
#include "display/shader/Shader.hpp" #include "display/shader/Shader.hpp"
#include "display/mesh/Mesh.hpp" #include "display/mesh/Mesh.hpp"
#include "display/shader/ShaderParameterBuffer.hpp"
namespace Dawn { namespace Dawn {
struct ShaderPassItem { struct ShaderPassItem {
@ -26,6 +27,7 @@ namespace Dawn {
std::map<shaderparameter_t, glm::vec3> vec3Values; std::map<shaderparameter_t, glm::vec3> vec3Values;
std::map<shaderparameter_t, textureslot_t> textureValues; std::map<shaderparameter_t, textureslot_t> textureValues;
std::map<shaderparameter_t, float_t> floatValues; std::map<shaderparameter_t, float_t> floatValues;
std::map<shaderbufferlocation_t, IShaderParameterBuffer<shaderbufferslot_t>*> parameterBuffers;
// Textures // Textures
std::map<textureslot_t, Texture*> textureSlots; std::map<textureslot_t, Texture*> textureSlots;

View File

@ -7,7 +7,7 @@
#include "dawnlibs.hpp" #include "dawnlibs.hpp"
namespace Dawn { namespace Dawn {
template<typename T, typename L> template<typename L>
class IShaderParameterBuffer { class IShaderParameterBuffer {
public: public:
/** /**
@ -15,14 +15,6 @@ namespace Dawn {
*/ */
virtual void init() = 0; virtual void init() = 0;
/**
* Basic buffer method. Buffers the entire contents of the data struct to
* this shader parameter buffer.
*
* @param data Data to buffer to the parameter.
*/
virtual void buffer(T *data) = 0;
/** /**
* Bind this shader buffer to the supplied location. * Bind this shader buffer to the supplied location.
* *

View File

@ -4,7 +4,7 @@
// https://opensource.org/licenses/MIT // https://opensource.org/licenses/MIT
#include "SceneDebugLine.hpp" #include "SceneDebugLine.hpp"
#include "display/shader/SimpleTexturedShader.hpp" #include "display/shader/shaders/SimpleTexturedShader.hpp"
#include "scene/components/display/Camera.hpp" #include "scene/components/display/Camera.hpp"
#include "scene/Scene.hpp" #include "scene/Scene.hpp"
#include "scene/components/physics/3d/Collider3D.hpp" #include "scene/components/physics/3d/Collider3D.hpp"

View File

@ -27,33 +27,33 @@ namespace Dawn {
camera = Camera::create(this); camera = Camera::create(this);
camera->transform->lookAt(glm::vec3(3, 3, 3), glm::vec3(0, 0, 0)); camera->transform->lookAt(glm::vec3(3, 3, 3), glm::vec3(0, 0, 0));
// cube = SimpleSpinningCubePrefab::create(this); cube = SimpleSpinningCubePrefab::create(this);
item = this->createSceneItem(); // item = this->createSceneItem();
auto meshRenderer = item->addComponent<MeshRenderer>(); // auto meshRenderer = item->addComponent<MeshRenderer>();
auto quadMeshHost = item->addComponent<QuadMeshHost>(); // auto quadMeshHost = item->addComponent<QuadMeshHost>();
auto material = item->addComponent<SimpleTexturedMaterial>(); // auto material = item->addComponent<SimpleTexturedMaterial>();
if(FT_New_Face( // if(FT_New_Face(
this->game->renderManager.getFontManager()->fontLibrary, // this->game->renderManager.getFontManager()->fontLibrary,
"C:\\Windows\\Fonts\\Arial.ttf", // "C:\\Windows\\Fonts\\Arial.ttf",
0, // 0,
&face // &face
)) { // )) {
assertUnreachable(); // assertUnreachable();
} // }
if(FT_Set_Pixel_Sizes(face, 0, 16)) { // if(FT_Set_Pixel_Sizes(face, 0, 16)) {
assertUnreachable(); // assertUnreachable();
} // }
_newTrueTypePlaceholder(face, texture, charStore); // _newTrueTypePlaceholder(face, texture, charStore);
FT_ULong c = 'B'; // FT_ULong c = 'B';
auto x = charStore[c]; // auto x = charStore[c];
glm::vec2 wh = glm::vec2(texture.getWidth(), texture.getHeight()); // glm::vec2 wh = glm::vec2(texture.getWidth(), texture.getHeight());
quadMeshHost->uv0 = glm::vec2(0.0f, x.textureY) / wh; // quadMeshHost->uv0 = glm::vec2(0.0f, x.textureY) / wh;
quadMeshHost->uv1 = (glm::vec2)quadMeshHost->uv0 + (x.bitmapSize / wh); // quadMeshHost->uv1 = (glm::vec2)quadMeshHost->uv0 + (x.bitmapSize / wh);
std::cout << "Done" << std::endl; // std::cout << "Done" << std::endl;
material->texture = &texture; // material->texture = &texture;
} }
std::vector<Asset*> getRequiredAssets() override { std::vector<Asset*> getRequiredAssets() override {

View File

@ -7,9 +7,9 @@
#include "display/_RenderManager.hpp" #include "display/_RenderManager.hpp"
#include "display/BackBufferRenderTarget.hpp" #include "display/BackBufferRenderTarget.hpp"
#include "display/shader/ShaderManager.hpp" #include "display/shader/ShaderManager.hpp"
#include "display/shader/SimpleTexturedShader.hpp" #include "display/shader/shaders/SimpleTexturedShader.hpp"
#include "display/shader/FontShader.hpp" #include "display/shader/shaders/FontShader.hpp"
#include "display/shader/UIShader.hpp" #include "display/shader/shaders/UIShader.hpp"
#include "display/RenderPipeline.hpp" #include "display/RenderPipeline.hpp"
#include "display/font/FontManager.hpp" #include "display/font/FontManager.hpp"
#include "display/font/ExampleFont.hpp" #include "display/font/ExampleFont.hpp"

View File

@ -7,7 +7,7 @@
target_sources(${DAWN_TARGET_NAME} target_sources(${DAWN_TARGET_NAME}
PRIVATE PRIVATE
Shader.cpp Shader.cpp
FontShader.cpp
SimpleTexturedShader.cpp
SimpleBillboardedShader.cpp
) )
add_subdirectory(buffers)
add_subdirectory(shaders)

View File

@ -13,7 +13,7 @@ namespace Dawn {
typedef GLuint shaderbufferlocation_t; typedef GLuint shaderbufferlocation_t;
template<typename T> template<typename T>
class ShaderParameterBuffer : public IShaderParameterBuffer<T, shaderbufferslot_t> { class ShaderParameterBuffer : public IShaderParameterBuffer<shaderbufferslot_t> {
protected: protected:
shaderbufferlocation_t id = -1; shaderbufferlocation_t id = -1;
size_t size; size_t size;
@ -29,7 +29,14 @@ namespace Dawn {
glBufferData(GL_UNIFORM_BUFFER, this->size, NULL, GL_DYNAMIC_DRAW); glBufferData(GL_UNIFORM_BUFFER, this->size, NULL, GL_DYNAMIC_DRAW);
} }
void buffer(T *data) override { /**
* Basic buffer method. Buffers the entire contents of the data struct to
* this shader parameter buffer.
*
* @param data Data to buffer to the parameter.
*/
void buffer(T *data) {
this->bind(0);
this->bufferRaw((void*)data); this->bufferRaw((void*)data);
} }

View File

@ -0,0 +1,9 @@
# Copyright (c) 2023 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
)

View File

@ -0,0 +1,30 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/shader/ShaderParameterBuffer.hpp"
namespace Dawn {
struct RenderPipelineShaderBufferData {
glm::mat4 view;
glm::mat4 projection;
};
class RenderPipelineShaderBuffer : public ShaderParameterBuffer<struct RenderPipelineShaderBufferData> {
public:
static std::string getShaderUniformName() {
return "ub_RenderPipeline";
}
static std::string getShaderUniform() {
return std::string(
"layout (std140) uniform ub_RenderPipeline {\n"
"mat4 u_View;\n"
"mat4 u_Projection;\n"
"};"
);
}
};
}

View File

@ -0,0 +1,12 @@
# Copyright (c) 2023 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
FontShader.cpp
SimpleTexturedShader.cpp
SimpleBillboardedShader.cpp
)

View File

@ -17,15 +17,17 @@ void SimpleTexturedShader::compile() {
// Vertex Shader // Vertex Shader
"#version 330 core\n" "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n" "layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec2 aTexCoord;\n" "layout (location = 1) in vec2 aTexCoord;\n" +
"uniform mat4 u_Proj;\n" RenderPipelineShaderBuffer::getShaderUniform() + ""
"uniform mat4 u_View;\n"
// "uniform mat4 u_Proj;\n"
// "uniform mat4 u_View;\n"
"uniform mat4 u_Model;\n" "uniform mat4 u_Model;\n"
"out vec2 o_TextCoord;\n" "out vec2 o_TextCoord;\n"
"void main() {\n" "void main() {\n"
"gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n" "gl_Position = u_Projection * u_View * u_Model * vec4(aPos, 1.0);\n"
"o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n" "o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n"
"}", "}",
@ -87,10 +89,10 @@ void SimpleTexturedShader::compile() {
#error Shader Type must be either GLSL or HLSL #error Shader Type must be either GLSL or HLSL
#endif #endif
this->paramProjection = this->getParameterByName("u_Proj");
this->paramView = this->getParameterByName("u_View");
this->paramModel = this->getParameterByName("u_Model"); this->paramModel = this->getParameterByName("u_Model");
this->paramColor = this->getParameterByName("u_Color"); this->paramColor = this->getParameterByName("u_Color");
this->paramTexture = this->getParameterByName("u_Text"); this->paramTexture = this->getParameterByName("u_Text");
this->paramHasTexture = this->getParameterByName("u_HasTexture"); this->paramHasTexture = this->getParameterByName("u_HasTexture");
this->bufferRenderPipeline = this->getBufferLocationByName(RenderPipelineShaderBuffer::getShaderUniformName());
} }

View File

@ -4,6 +4,7 @@
// https://opensource.org/licenses/MIT // https://opensource.org/licenses/MIT
#pragma once #pragma once
#include "display/shader/buffers/RenderPipelineShaderBuffer.hpp"
#include "display/shader/Shader.hpp" #include "display/shader/Shader.hpp"
namespace Dawn { namespace Dawn {
@ -11,10 +12,12 @@ namespace Dawn {
public: public:
shaderparameter_t paramProjection; shaderparameter_t paramProjection;
shaderparameter_t paramView; shaderparameter_t paramView;
shaderparameter_t paramModel; shaderparameter_t paramModel;
shaderparameter_t paramColor; shaderparameter_t paramColor;
shaderparameter_t paramTexture; shaderparameter_t paramTexture;
shaderparameter_t paramHasTexture; shaderparameter_t paramHasTexture;
shaderbufferlocation_t bufferRenderPipeline;
void compile() override; void compile() override;
}; };

View File

@ -3,7 +3,7 @@
// This software is released under the MIT License. // This software is released under the MIT License.
// https://opensource.org/licenses/MIT // https://opensource.org/licenses/MIT
#include "display/shader/SimpleBillboardedShader.hpp" #include "display/shader/shaders/SimpleBillboardedShader.hpp"
#include "SimpleBillboardedMaterial.hpp" #include "SimpleBillboardedMaterial.hpp"
#include "game/DawnGame.hpp" #include "game/DawnGame.hpp"

View File

@ -36,8 +36,12 @@ std::vector<struct ShaderPassItem> SimpleTexturedMaterial::getRenderPasses() {
onlyPass.shader = shader; onlyPass.shader = shader;
onlyPass.colorValues[shader->paramColor] = this->color; onlyPass.colorValues[shader->paramColor] = this->color;
onlyPass.matrixValues[shader->paramModel] = this->transform->getWorldTransform(); onlyPass.matrixValues[shader->paramModel] = this->transform->getWorldTransform();
onlyPass.matrixValues[shader->paramView] = camera->transform->getWorldTransform();
onlyPass.matrixValues[shader->paramProjection] = camera->getProjection(); onlyPass.parameterBuffers[shader->bufferRenderPipeline] = &this->getGame()->renderManager.getRenderPipeline()->shaderBuffer;
// onlyPass.matrixValues[shader->paramView] = camera->transform->getWorldTransform();
// onlyPass.matrixValues[shader->paramProjection] = camera->getProjection();
onlyPass.renderFlags = ( onlyPass.renderFlags = (
RENDER_MANAGER_RENDER_FLAG_BLEND | RENDER_MANAGER_RENDER_FLAG_BLEND |
RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST