summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaG1924 <lag1924@gmail.com>2021-11-14 12:23:15 +0100
committerLaG1924 <lag1924@gmail.com>2021-11-14 12:23:15 +0100
commit5651b71788487e986ea945d76cbaf647d4554813 (patch)
treeefec4c9a455dd4911bbeefd9f8c593e36ee97973
parentAdded basic Graphics Abstraction Layer (diff)
downloadAltCraft-5651b71788487e986ea945d76cbaf647d4554813.tar
AltCraft-5651b71788487e986ea945d76cbaf647d4554813.tar.gz
AltCraft-5651b71788487e986ea945d76cbaf647d4554813.tar.bz2
AltCraft-5651b71788487e986ea945d76cbaf647d4554813.tar.lz
AltCraft-5651b71788487e986ea945d76cbaf647d4554813.tar.xz
AltCraft-5651b71788487e986ea945d76cbaf647d4554813.tar.zst
AltCraft-5651b71788487e986ea945d76cbaf647d4554813.zip
-rw-r--r--src/Gal.hpp2
-rw-r--r--src/GalOgl.cpp27
-rw-r--r--src/Rml.cpp32
-rw-r--r--src/Rml.hpp3
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<PipelineInstance> CreateInstance(std::vector<std::pair<std::shared_ptr<BufferBinding>, std::shared_ptr<Buffer>>> &&buffers) = 0;
+ virtual void SetDynamicTexture(std::string_view name, std::shared_ptr<Texture> 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<std::byte>&& 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> texture) override {
+ Activate();
+ glActiveTexture(GL_TEXTURE0);
+ auto tex = std::static_pointer_cast<TextureOgl>(texture);
+ glBindTexture(tex->type, tex->texture);
+ SetShaderParameter(name, 0);
+ }
+
virtual std::shared_ptr<PipelineInstance> CreateInstance(std::vector<std::pair<std::shared_ptr<BufferBinding>, std::shared_ptr<Buffer>>>&& buffers) override {
auto instance = std::make_shared<PipelineInstanceOgl>();
@@ -590,6 +599,7 @@ public:
config->width = width;
config->height = height;
+ config->depth = 1;
config->format = format;
return std::static_pointer_cast<TextureConfig, TextureConfigOgl>(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<std::byte>(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<std::byte*>(vertices), reinterpret_cast<std::byte*>(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<const std::byte*>(source),reinterpret_cast<const std::byte*>(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<Gal::Pipeline> pipeline, texPipeline;
std::shared_ptr<Gal::PipelineInstance> pipelineInstance, texPipelineInstance;
std::shared_ptr<Gal::Buffer> vertexBuffer, indexBuffer;
+ std::map<size_t, std::shared_ptr<Gal::Texture>> textures;
unsigned int vpWidth, vpHeight;
public: