From 8d9b24b1b84107c90a77d94c86a810cc068fe073 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 19 Feb 2020 22:14:12 +0100 Subject: Fix rendering of texture with multiple frames --- include/RenderBackend/OpenGL/DeviceFrame.hpp | 26 ------------------ include/RenderBackend/OpenGL/DeviceMemory.hpp | 37 +++++++++++--------------- include/RenderBackend/OpenGL/ShaderFrame.hpp | 35 ++++++++++++++++++++++++ include/RenderBackend/OpenGL/ShaderProgram.hpp | 14 +++------- include/RenderBackend/OpenGL/Texture2D.hpp | 3 +++ include/RenderBackend/OpenGL/Uniform.hpp | 2 +- 6 files changed, 58 insertions(+), 59 deletions(-) delete mode 100644 include/RenderBackend/OpenGL/DeviceFrame.hpp create mode 100644 include/RenderBackend/OpenGL/ShaderFrame.hpp (limited to 'include') diff --git a/include/RenderBackend/OpenGL/DeviceFrame.hpp b/include/RenderBackend/OpenGL/DeviceFrame.hpp deleted file mode 100644 index 3fa7fe7..0000000 --- a/include/RenderBackend/OpenGL/DeviceFrame.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include "../../types.hpp" -#include "../../utils.hpp" -#include - -namespace amalgine -{ - class DeviceMemory; - class ShaderProgram; - - class DeviceFrame - { - DISABLE_COPY(DeviceFrame) - public: - DeviceFrame(); - ~DeviceFrame(); - - // The returned pointers cleaned when the frame is cleaned. Do not deallocate the returned pointer! - DeviceMemory* alloc(); - void draw(ShaderProgram *shader); - private: - u32 vertexArrayObjectId; - std::vector buffers; - }; -} diff --git a/include/RenderBackend/OpenGL/DeviceMemory.hpp b/include/RenderBackend/OpenGL/DeviceMemory.hpp index 5f593fc..802230d 100644 --- a/include/RenderBackend/OpenGL/DeviceMemory.hpp +++ b/include/RenderBackend/OpenGL/DeviceMemory.hpp @@ -5,6 +5,8 @@ #include "../../Triangle.hpp" #include "../../Vec.hpp" +#include + namespace amalgine { enum class DeviceMemoryType { NONE, @@ -12,11 +14,10 @@ namespace amalgine { VEC3 }; - class DeviceMemory - { + class DeviceMemory { DISABLE_COPY(DeviceMemory) - friend class DeviceFrame; - friend class ShaderProgram; + friend class ShaderFrame; + friend class std::default_delete; public: enum class StorageType { @@ -30,26 +31,20 @@ namespace amalgine { STREAM }; - enum class PrimitiveType - { - TRIANGLE - }; - - //void copy(const DataView &data, StorageType storageType, PrimitiveType primitiveType = PrimitiveType::TRIANGLE); - void copy(const DataView &texture_coords, StorageType storageType); - void copy(const DataView &triangles, StorageType storageType); - void copy(const DataView &triangles, StorageType storageType); - void draw(); + void set(const DataView &texture_coords, StorageType storageType); + void set(const DataView &triangles, StorageType storageType); + void set(const DataView &triangles, StorageType storageType); - DeviceMemoryType get_type() const { return type; } + i32 get_num_vertices() const { return num_vertices; } private: - DeviceMemory(); + DeviceMemory(u32 vertex_array_object_id, i32 attrib_location); ~DeviceMemory(); - void use() const; + void use(); private: - u32 vertexBufferObjectId; - u32 primitiveType; - u32 numVertices; - DeviceMemoryType type; + u32 vertex_array_object_id; + u32 vertex_buffer_object_id; + i32 attrib_location; + i32 num_vertices; + DeviceMemoryType memory_type; }; } diff --git a/include/RenderBackend/OpenGL/ShaderFrame.hpp b/include/RenderBackend/OpenGL/ShaderFrame.hpp new file mode 100644 index 0000000..da0918e --- /dev/null +++ b/include/RenderBackend/OpenGL/ShaderFrame.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include "../../Result.hpp" +#include "../../types.hpp" +#include "../../utils.hpp" +#include "Uniform.hpp" +#include "DeviceMemory.hpp" + +#include +#include +#include + +namespace amalgine { + class ShaderFrame { + DISABLE_COPY(ShaderFrame) + friend class ShaderProgram; + public: + ~ShaderFrame(); + ShaderFrame(ShaderFrame &&other) = default; + ShaderFrame& operator=(ShaderFrame &&other) = default; + + DeviceMemory* get_input_by_name(const char *name); + Result get_uniform_by_name(const char *name); + + void draw(); + private: + ShaderFrame(u32 shader_program_id); + private: + u32 shader_program_id; + u32 vertex_array_object_id; + + // TODO: Use a const char* as a key instead + std::map> shader_inputs; + }; +} diff --git a/include/RenderBackend/OpenGL/ShaderProgram.hpp b/include/RenderBackend/OpenGL/ShaderProgram.hpp index 177e43c..a1db6a0 100644 --- a/include/RenderBackend/OpenGL/ShaderProgram.hpp +++ b/include/RenderBackend/OpenGL/ShaderProgram.hpp @@ -3,14 +3,10 @@ #include "../../Result.hpp" #include "../../types.hpp" #include "../../utils.hpp" -#include "../../Vec.hpp" -#include "Uniform.hpp" -#include "DeviceMemory.hpp" +#include "ShaderFrame.hpp" -#include #include -#include -#include +#include namespace amalgine { class Shader; @@ -21,11 +17,7 @@ namespace amalgine { public: ~ShaderProgram(); static Result> build(const std::vector &shaders); - - Result get_uniform_by_name(const char *name); - int set_input_data(const char *name, const DeviceMemory &data); - - void use(); + ShaderFrame create_frame(); private: ShaderProgram(u32 shader_program_id); private: diff --git a/include/RenderBackend/OpenGL/Texture2D.hpp b/include/RenderBackend/OpenGL/Texture2D.hpp index 48b613b..c0bfbd2 100644 --- a/include/RenderBackend/OpenGL/Texture2D.hpp +++ b/include/RenderBackend/OpenGL/Texture2D.hpp @@ -11,9 +11,12 @@ namespace amalgine { DISABLE_COPY(Texture2D) public: + Texture2D(); // Not thread safe Texture2D(Image *image); ~Texture2D(); + Texture2D(Texture2D &&other); + Texture2D& operator=(Texture2D &&other); i32 get_texture_id() const { return texture_id; } private: diff --git a/include/RenderBackend/OpenGL/Uniform.hpp b/include/RenderBackend/OpenGL/Uniform.hpp index dbffa03..d6e6e64 100644 --- a/include/RenderBackend/OpenGL/Uniform.hpp +++ b/include/RenderBackend/OpenGL/Uniform.hpp @@ -11,7 +11,7 @@ namespace amalgine { class Uniform { DISABLE_COPY(Uniform) - friend class ShaderProgram; + friend class ShaderFrame; friend class Result; public: Uniform(Uniform&&) = default; -- cgit v1.2.3