From 5651b71788487e986ea945d76cbaf647d4554813 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 14 Nov 2021 16:23:15 +0500 Subject: Added dynamic textures for Gal --- src/Gal.hpp | 2 ++ src/GalOgl.cpp | 27 +++++++++++++++++++-------- src/Rml.cpp | 32 ++++++++++++++------------------ src/Rml.hpp | 3 +-- 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/Gal.hpp b/src/Gal.hpp index 0f2d162..ec00442 100644 --- a/src/Gal.hpp +++ b/src/Gal.hpp @@ -172,6 +172,8 @@ namespace Gal { virtual std::shared_ptr CreateInstance(std::vector, std::shared_ptr>> &&buffers) = 0; + virtual void SetDynamicTexture(std::string_view name, std::shared_ptr texture) = 0; + virtual void SetShaderParameter(std::string_view name, float value) = 0; virtual void SetShaderParameter(std::string_view name, double value) = 0; diff --git a/src/GalOgl.cpp b/src/GalOgl.cpp index 4fb2794..e39f5f0 100644 --- a/src/GalOgl.cpp +++ b/src/GalOgl.cpp @@ -265,7 +265,7 @@ class TextureConfigOgl : public TextureConfig { public: Format format; - size_t width = 0, height = 0, depth = 0; + size_t width = 1, height = 1, depth = 1; bool interpolateLayers = false; Filtering min = Filtering::Nearest, max = Filtering::Nearest; @@ -292,10 +292,11 @@ public: GLenum type; GLuint texture; Format format; - size_t width = 0, height = 0, depth = 0; + size_t width, height, depth; virtual void SetData(std::vector&& data, size_t mipLevel = 0) override { - if (data.size() != width * height * depth * GalFormatGetSize(format)) + size_t expectedSize = width * height * depth * GalFormatGetSize(format); + if (data.size() != expectedSize) throw std::logic_error("Size of data is not valid for this texture"); glBindTexture(type, texture); @@ -405,6 +406,14 @@ public: glCheckError(); } + virtual void SetDynamicTexture(std::string_view name, std::shared_ptr texture) override { + Activate(); + glActiveTexture(GL_TEXTURE0); + auto tex = std::static_pointer_cast(texture); + glBindTexture(tex->type, tex->texture); + SetShaderParameter(name, 0); + } + virtual std::shared_ptr CreateInstance(std::vector, std::shared_ptr>>&& buffers) override { auto instance = std::make_shared(); @@ -590,6 +599,7 @@ public: config->width = width; config->height = height; + config->depth = 1; config->format = format; return std::static_pointer_cast(config); @@ -619,14 +629,15 @@ public: glGenTextures(1, &texture->texture); glCheckError(); + glBindTexture(texture->type, texture->texture); - glTexParameteri(texture->type, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(texture->type, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(texture->type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(texture->type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - + glTexParameteri(texture->type, GL_TEXTURE_MIN_FILTER, GalFilteringGetGlType(texConfig->min)); + glTexParameteri(texture->type, GL_TEXTURE_MAG_FILTER, GalFilteringGetGlType(texConfig->max)); + glTexParameteri(texture->type, GL_TEXTURE_WRAP_S, GalWrappingGetGlType(texConfig->wrap)); + glTexParameteri(texture->type, GL_TEXTURE_WRAP_T, GalWrappingGetGlType(texConfig->wrap)); glCheckError(); + glBindTexture(texture->type, 0); texture->SetData(std::vector(texture->width * texture->height * texture->depth * GalFormatGetSize(texture->format))); glCheckError(); diff --git a/src/Rml.cpp b/src/Rml.cpp index 4e28529..4f5c0b9 100644 --- a/src/Rml.cpp +++ b/src/Rml.cpp @@ -45,7 +45,7 @@ void RmlSystemInterface::GetClipboardText(Rml::String& text) { text = clipboard; } -RmlRenderInterface::RmlRenderInterface(RenderState& renderState) : State(&renderState) { +RmlRenderInterface::RmlRenderInterface(RenderState& renderState) { auto gal = Gal::GetImplementation(); auto pipelineConfig = gal->CreatePipelineConfig(); pipelineConfig->AddShaderParameter("viewportSize", Gal::Type::Vec2u32); @@ -117,15 +117,15 @@ void RmlRenderInterface::RenderGeometry(Rml::Vertex* vertices, int num_vertices, vertexBuffer->SetData({ reinterpret_cast(vertices), reinterpret_cast(vertices + num_vertices) }); glCheckError(); - - if (texture) { + auto tex = textures.find(texture); + if (tex != textures.end()) { texPipeline->Activate(); glCheckError(); texPipeline->SetShaderParameter("translation", glm::vec2(translation.x, translation.y)); glCheckError(); - texPipelineInstance->Activate(); + texPipeline->SetDynamicTexture("fontTexture", tex->second); glCheckError(); - glBindTexture(GL_TEXTURE_2D, texture); + texPipelineInstance->Activate(); glCheckError(); texPipelineInstance->Render(0, num_indices); } else { @@ -157,21 +157,16 @@ bool RmlRenderInterface::LoadTexture(Rml::TextureHandle& texture_handle, Rml::Ve } bool RmlRenderInterface::GenerateTexture(Rml::TextureHandle& texture_handle, const Rml::byte* source, const Rml::Vector2i& source_dimensions) { - int mipLevelCount = 1; - glActiveTexture(GL_TEXTURE0); - GLuint texture; - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glCheckError(); - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, source_dimensions.x, source_dimensions.y, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, source); + size_t textureId = textures.empty() ? 1 : textures.rbegin()->first + 1; + auto gal = Gal::GetImplementation(); + auto textureConfig = gal->CreateTexture2DConfig(source_dimensions.x, source_dimensions.y, Gal::Format::R8G8B8A8); + auto texture = gal->BuildTexture(textureConfig); + texture->SetData({ reinterpret_cast(source),reinterpret_cast(source + (source_dimensions.x * source_dimensions.y) * 4) }); + textures.insert({ textureId,texture }); + texture_handle = textureId; glCheckError(); - texture_handle = texture; return true; } @@ -185,11 +180,12 @@ void RmlRenderInterface::Update(unsigned int windowWidth, unsigned int windowHei glCheckError(); - pipeline->SetShaderParameter("viewportSize", glm::uvec2(windowWidth, windowHeight)); texPipeline->SetShaderParameter("viewportSize", glm::uvec2(windowWidth, windowHeight)); texPipeline->SetShaderParameter("fontTexture", 0); + glCheckError(); + vpWidth = windowWidth; vpHeight = windowHeight; } diff --git a/src/Rml.hpp b/src/Rml.hpp index 42203d4..9aa6be0 100644 --- a/src/Rml.hpp +++ b/src/Rml.hpp @@ -31,11 +31,10 @@ public: }; class RmlRenderInterface : public Rml::RenderInterface { - RenderState* State; - std::shared_ptr pipeline, texPipeline; std::shared_ptr pipelineInstance, texPipelineInstance; std::shared_ptr vertexBuffer, indexBuffer; + std::map> textures; unsigned int vpWidth, vpHeight; public: -- cgit v1.2.3