From ffe9dac56488ebfc9f5c37c4e400f4f5469a8a46 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 26 Dec 2017 17:29:28 +0100 Subject: Add device memory, device frame. Rendering works --- include/RenderBackend/OpenGL/CompiledShader.hpp | 9 +++++- include/RenderBackend/OpenGL/DeviceFrame.hpp | 24 ++++++++++++++ include/RenderBackend/OpenGL/DeviceMemory.hpp | 42 +++++++++++++++++++------ include/RenderBackend/OpenGL/ShaderProgram.hpp | 4 +-- include/RenderBackend/OpenGL/ShaderVec.hpp | 10 ++++++ include/RenderBackend/OpenGL/VertexShader.hpp | 6 ++-- 6 files changed, 80 insertions(+), 15 deletions(-) create mode 100644 include/RenderBackend/OpenGL/DeviceFrame.hpp (limited to 'include') diff --git a/include/RenderBackend/OpenGL/CompiledShader.hpp b/include/RenderBackend/OpenGL/CompiledShader.hpp index 67e6418..8d2ee84 100644 --- a/include/RenderBackend/OpenGL/CompiledShader.hpp +++ b/include/RenderBackend/OpenGL/CompiledShader.hpp @@ -1,6 +1,8 @@ #pragma once #include "../../types.hpp" +#include +#include namespace amalgine { @@ -22,9 +24,14 @@ namespace amalgine public: ~CompiledPixelShader(); u32 getShaderId() const; + const auto& getPixelAttributes() const + { + return pixelAttributes; + } private: - CompiledPixelShader(u32 _shaderId); + CompiledPixelShader(u32 _shaderId, std::unordered_map &&_pixelAttributes); private: u32 shaderId; + std::unordered_map pixelAttributes; }; } diff --git a/include/RenderBackend/OpenGL/DeviceFrame.hpp b/include/RenderBackend/OpenGL/DeviceFrame.hpp new file mode 100644 index 0000000..12e578a --- /dev/null +++ b/include/RenderBackend/OpenGL/DeviceFrame.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include "../../types.hpp" +#include "../../utils.hpp" +#include + +namespace amalgine +{ + class DeviceMemory; + + class DeviceFrame + { + DISABLE_COPY(DeviceFrame) + public: + DeviceFrame(); + ~DeviceFrame(); + + DeviceMemory* alloc(); + void draw(); + private: + u32 vertexArrayObjectId; + std::vector buffers; + }; +} diff --git a/include/RenderBackend/OpenGL/DeviceMemory.hpp b/include/RenderBackend/OpenGL/DeviceMemory.hpp index 974c7cf..163b0ad 100644 --- a/include/RenderBackend/OpenGL/DeviceMemory.hpp +++ b/include/RenderBackend/OpenGL/DeviceMemory.hpp @@ -2,27 +2,49 @@ #include "../../DataView.hpp" #include "../../utils.hpp" +#include namespace amalgine { + class DeviceMemoryEmpty : public std::runtime_error + { + public: + DeviceMemoryEmpty(const std::string &errMsg) : std::runtime_error(errMsg) {} + }; + class DeviceMemory { DISABLE_COPY(DeviceMemory) + friend class DeviceFrame; + friend class ShaderInputVec4; public: - DeviceMemory(); - ~DeviceMemory(); - - // Uploaded once, drawn many times - void copyAsStatic(const DataView &data); + enum class StorageType + { + // Uploaded once, drawn many times + STATIC, + + // Created once, changed from time to time but drawn many times more than that + DYNAMIC, + + // Uploaded once, drawn once (for example data that is changed every frame) + STREAM + }; - // Created once, changed from time to time but drawn many times more than that - void copyAsDynamic(const DataView &data); + enum class PrimitiveType + { + TRIANGLE + }; - // Uploaded once, drawn once (for example data that is changed every frame) - void copyAsStream(const DataView &data); + ~DeviceMemory(); + void copy(const DataView &data, StorageType storageType, PrimitiveType primitiveType = PrimitiveType::TRIANGLE); + void draw(); private: - void copy(const DataView &data, i32 storageType); + DeviceMemory(); + void operator delete(void *data); + void use() const; private: u32 vertexBufferObjectId; + u32 primitiveType; + u32 numVertices; }; } diff --git a/include/RenderBackend/OpenGL/ShaderProgram.hpp b/include/RenderBackend/OpenGL/ShaderProgram.hpp index a3e8a0f..2b1c5b4 100644 --- a/include/RenderBackend/OpenGL/ShaderProgram.hpp +++ b/include/RenderBackend/OpenGL/ShaderProgram.hpp @@ -26,8 +26,8 @@ namespace amalgine ShaderProgram(); ~ShaderProgram(); - bool addVertexShader(CompiledVertexShader *vertexShader); - bool addPixelShader(CompiledPixelShader *pixelShader); + bool setVertexShader(CompiledVertexShader *vertexShader); + bool setPixelShader(CompiledPixelShader *pixelShader); Result build(); void use(); diff --git a/include/RenderBackend/OpenGL/ShaderVec.hpp b/include/RenderBackend/OpenGL/ShaderVec.hpp index 10e628c..a5163ba 100644 --- a/include/RenderBackend/OpenGL/ShaderVec.hpp +++ b/include/RenderBackend/OpenGL/ShaderVec.hpp @@ -1,6 +1,7 @@ #pragma once #include "../../types.hpp" +#include "../../RenderBackend/OpenGL/DeviceMemory.hpp" #include namespace amalgine @@ -8,11 +9,20 @@ namespace amalgine class VertexShader; class PixelShader; + enum class AttributeType + { + NONE, + VEC2 + }; + + AttributeType getAttributeTypeByName(const char *attributeName); + class ShaderInputVec2 { friend class VertexShader; public: const std::string& getName() const; + void setData(const DeviceMemory &data); private: ShaderInputVec2(VertexShader *_vertexShader, i32 _attributeIndex) : vertexShader(_vertexShader), diff --git a/include/RenderBackend/OpenGL/VertexShader.hpp b/include/RenderBackend/OpenGL/VertexShader.hpp index 43a5dd8..aaf8b24 100644 --- a/include/RenderBackend/OpenGL/VertexShader.hpp +++ b/include/RenderBackend/OpenGL/VertexShader.hpp @@ -3,6 +3,7 @@ #include "../../DataView.hpp" #include "../../utils.hpp" #include "../../Result.hpp" +#include "CommonShader.hpp" #include "ShaderVec.hpp" #include #include @@ -51,7 +52,8 @@ namespace amalgine public: VertexShader(); - const std::string& getInputAttributeName(i32 attributeIndex); + const std::string& getInputAttributeName(i32 attributeIndex) const; + AttributeType getInputAttributeType(i32 attributeIndex) const; ShaderInputVec2 defineInputVec2(const std::string &name); void defineMain(VertexShaderMainFunc mainFunc); Result compile(); @@ -71,7 +73,7 @@ namespace amalgine int locationCounter; i32 maxVertexAttribs; // Could make this static std::unordered_map vertexAttributes; - std::vector vertexAttributeNames; + std::vector vertexAttributeNames; bool mainFuncDefined; }; } -- cgit v1.2.3