From ce116b8ba834363921cc31ce2ef0781d6f6b2627 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 14 Nov 2021 07:27:39 +0500 Subject: Added basic Graphics Abstraction Layer --- src/Rml.cpp | 143 +++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 84 insertions(+), 59 deletions(-) (limited to 'src/Rml.cpp') diff --git a/src/Rml.cpp b/src/Rml.cpp index 179d4b9..4e28529 100644 --- a/src/Rml.cpp +++ b/src/Rml.cpp @@ -46,74 +46,98 @@ void RmlSystemInterface::GetClipboardText(Rml::String& text) { } RmlRenderInterface::RmlRenderInterface(RenderState& renderState) : State(&renderState) { - glGenVertexArrays(1, &Vao); - glBindVertexArray(Vao); - glCheckError(); + auto gal = Gal::GetImplementation(); + auto pipelineConfig = gal->CreatePipelineConfig(); + pipelineConfig->AddShaderParameter("viewportSize", Gal::Type::Vec2u32); + pipelineConfig->AddShaderParameter("translation", Gal::Type::Vec2); + pipelineConfig->SetTarget(gal->GetDefaultFramebuffer()); - glGenBuffers(1, &Ebo); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Ebo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, 0, nullptr, GL_STREAM_DRAW); - glCheckError(); + auto vertAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/vert/rml"); + std::string vertSource((char*)vertAsset->data.data(), (char*)vertAsset->data.data() + vertAsset->data.size()); + auto pixelAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/rml"); + std::string pixelSource((char*)pixelAsset->data.data(), (char*)pixelAsset->data.data() + pixelAsset->data.size()); + pipelineConfig->SetVertexShader(gal->LoadVertexShader(vertSource)); + pipelineConfig->SetPixelShader(gal->LoadPixelShader(pixelSource)); + + auto vertBuffBind = pipelineConfig->BindVertexBuffer({ + {"pos", Gal::Type::Vec2}, + {"color", Gal::Type::Vec4u8}, + {"", Gal::Type::Vec2}, //it's not used in shader, so driver optimizes it away + }); + + auto indexBuffBind = pipelineConfig->BindIndexBuffer(); + + pipeline = gal->BuildPipeline(pipelineConfig); + + vertexBuffer = gal->CreateBuffer(); + + indexBuffer = gal->CreateBuffer(); + + pipelineInstance = pipeline->CreateInstance({ + {vertBuffBind, vertexBuffer}, + {indexBuffBind, indexBuffer}, + }); - glGenBuffers(1, &Vbo); - glBindBuffer(GL_ARRAY_BUFFER, Vbo); - glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_STREAM_DRAW); glCheckError(); - - { - //Vertex position (2 float) - GLuint PosAttribPos = 0; - glVertexAttribPointer(PosAttribPos, 2, GL_FLOAT, GL_FALSE, 20, (void*)0); - glEnableVertexAttribArray(PosAttribPos); - - //Vertex colour (4 uint8 RGBA) - GLuint ColAttribPos = 1; - glVertexAttribIPointer(ColAttribPos, 4, GL_UNSIGNED_BYTE, 20, (void*)8); - glEnableVertexAttribArray(ColAttribPos); - - //Vertex tex_coord (2 float) - GLuint TexAttribPos = 2; - glVertexAttribPointer(TexAttribPos, 2, GL_FLOAT, GL_FALSE, 20, (void*)12); - glEnableVertexAttribArray(TexAttribPos); - } - glBindVertexArray(0); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + + auto texturePipelineConfig = gal->CreatePipelineConfig(); + texturePipelineConfig->AddShaderParameter("viewportSize", Gal::Type::Vec2u32); + texturePipelineConfig->AddShaderParameter("translation", Gal::Type::Vec2); + texturePipelineConfig->AddShaderParameter("fontTexture", Gal::Type::Int32); + texturePipelineConfig->SetTarget(gal->GetDefaultFramebuffer()); + + auto texturePixelAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/rmltex"); + std::string texturePixelSource((char*)texturePixelAsset->data.data(), (char*)texturePixelAsset->data.data() + texturePixelAsset->data.size()); + texturePipelineConfig->SetVertexShader(gal->LoadVertexShader(vertSource)); + texturePipelineConfig->SetPixelShader(gal->LoadPixelShader(texturePixelSource)); + + auto texVertBuffBind = texturePipelineConfig->BindVertexBuffer({ + {"pos", Gal::Type::Vec2}, + {"color", Gal::Type::Vec4u8}, + {"tex_coord", Gal::Type::Vec2}, + }); + + auto texIndexBuffBind = texturePipelineConfig->BindIndexBuffer(); + + texPipeline = gal->BuildPipeline(texturePipelineConfig); + + texPipelineInstance = texPipeline->CreateInstance({ + {texVertBuffBind, vertexBuffer}, + {texIndexBuffBind, indexBuffer}, + }); glCheckError(); } RmlRenderInterface::~RmlRenderInterface() { - glDeleteVertexArrays(1, &Vao); - glDeleteBuffers(1, &Vbo); - glDeleteBuffers(1, &Ebo); glCheckError(); } void RmlRenderInterface::RenderGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices, Rml::TextureHandle texture, const Rml::Vector2f& translation) { + indexBuffer->SetData({ reinterpret_cast(indices), reinterpret_cast(indices + num_indices) }); + vertexBuffer->SetData({ reinterpret_cast(vertices), reinterpret_cast(vertices + num_vertices) }); + glCheckError(); + + if (texture) { - AssetManager::GetAsset("/altcraft/shaders/rmltex")->shader->Activate(); - AssetManager::GetAsset("/altcraft/shaders/rmltex")->shader->SetUniform("translation", glm::vec2(translation.x, translation.y)); + texPipeline->Activate(); + glCheckError(); + texPipeline->SetShaderParameter("translation", glm::vec2(translation.x, translation.y)); + glCheckError(); + texPipelineInstance->Activate(); + glCheckError(); glBindTexture(GL_TEXTURE_2D, texture); + glCheckError(); + texPipelineInstance->Render(0, num_indices); } else { - AssetManager::GetAsset("/altcraft/shaders/rml")->shader->Activate(); - AssetManager::GetAsset("/altcraft/shaders/rml")->shader->SetUniform("translation", glm::vec2(translation.x, translation.y)); - } - glCheckError(); - - glBindVertexArray(Vao); - glCheckError(); - - glBindBuffer(GL_ARRAY_BUFFER, Vbo); - glBufferData(GL_ARRAY_BUFFER, num_vertices * sizeof(Rml::Vertex), vertices, GL_STREAM_DRAW); - glCheckError(); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Ebo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, num_indices * sizeof(int), indices, GL_STREAM_DRAW); - glCheckError(); - - glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_INT, 0); + pipeline->Activate(); + glCheckError(); + pipeline->SetShaderParameter("translation", glm::vec2(translation.x, translation.y)); + glCheckError(); + pipelineInstance->Activate(); + glCheckError(); + pipelineInstance->Render(0, num_indices); + } glCheckError(); - glBindVertexArray(0); } void RmlRenderInterface::EnableScissorRegion(bool enable) { @@ -158,13 +182,14 @@ void RmlRenderInterface::ReleaseTexture(Rml::TextureHandle texture) { } void RmlRenderInterface::Update(unsigned int windowWidth, unsigned int windowHeight) { - AssetManager::GetAsset("/altcraft/shaders/rml")->shader->Activate(); - AssetManager::GetAsset("/altcraft/shaders/rml")->shader->SetUniform("viewportSize", windowWidth, windowHeight); - glCheckError(); - AssetManager::GetAsset("/altcraft/shaders/rmltex")->shader->Activate(); - AssetManager::GetAsset("/altcraft/shaders/rmltex")->shader->SetUniform("viewportSize", windowWidth, windowHeight); - AssetManager::GetAsset("/altcraft/shaders/rmltex")->shader->SetUniform("fontTexture", 0); + glCheckError(); + + + pipeline->SetShaderParameter("viewportSize", glm::uvec2(windowWidth, windowHeight)); + texPipeline->SetShaderParameter("viewportSize", glm::uvec2(windowWidth, windowHeight)); + texPipeline->SetShaderParameter("fontTexture", 0); + vpWidth = windowWidth; vpHeight = windowHeight; } -- cgit v1.2.3 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/Rml.cpp | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) (limited to 'src/Rml.cpp') 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; } -- cgit v1.2.3 From 1e40c0b0e6928558c5ea02612d52860ba80d9f05 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 14 Nov 2021 17:24:30 +0500 Subject: Added ScissorTest to Gal --- src/Rml.cpp | 134 ++++++++++++++++++++++++++---------------------------------- 1 file changed, 58 insertions(+), 76 deletions(-) (limited to 'src/Rml.cpp') diff --git a/src/Rml.cpp b/src/Rml.cpp index 4f5c0b9..d886377 100644 --- a/src/Rml.cpp +++ b/src/Rml.cpp @@ -3,7 +3,6 @@ #include #include "AssetManager.hpp" -#include "Shader.hpp" #include "Utility.hpp" double RmlSystemInterface::GetElapsedTime() { @@ -46,110 +45,103 @@ void RmlSystemInterface::GetClipboardText(Rml::String& text) { } RmlRenderInterface::RmlRenderInterface(RenderState& renderState) { - auto gal = Gal::GetImplementation(); - auto pipelineConfig = gal->CreatePipelineConfig(); - pipelineConfig->AddShaderParameter("viewportSize", Gal::Type::Vec2u32); - pipelineConfig->AddShaderParameter("translation", Gal::Type::Vec2); - pipelineConfig->SetTarget(gal->GetDefaultFramebuffer()); - - auto vertAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/vert/rml"); - std::string vertSource((char*)vertAsset->data.data(), (char*)vertAsset->data.data() + vertAsset->data.size()); - auto pixelAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/rml"); - std::string pixelSource((char*)pixelAsset->data.data(), (char*)pixelAsset->data.data() + pixelAsset->data.size()); - pipelineConfig->SetVertexShader(gal->LoadVertexShader(vertSource)); - pipelineConfig->SetPixelShader(gal->LoadPixelShader(pixelSource)); + std::string vertexSource, pixelSource, texPixelSource; + { + auto vertAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/vert/rml"); + vertexSource = std::string((char*)vertAsset->data.data(), (char*)vertAsset->data.data() + vertAsset->data.size()); - auto vertBuffBind = pipelineConfig->BindVertexBuffer({ - {"pos", Gal::Type::Vec2}, - {"color", Gal::Type::Vec4u8}, - {"", Gal::Type::Vec2}, //it's not used in shader, so driver optimizes it away - }); + auto pixelAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/rml"); + pixelSource = std::string((char*)pixelAsset->data.data(), (char*)pixelAsset->data.data() + pixelAsset->data.size()); - auto indexBuffBind = pipelineConfig->BindIndexBuffer(); + auto texPixelAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/rmltex"); + texPixelSource = std::string((char*)texPixelAsset->data.data(), (char*)texPixelAsset->data.data() + texPixelAsset->data.size()); + } - pipeline = gal->BuildPipeline(pipelineConfig); + auto gal = Gal::GetImplementation(); vertexBuffer = gal->CreateBuffer(); - indexBuffer = gal->CreateBuffer(); - pipelineInstance = pipeline->CreateInstance({ - {vertBuffBind, vertexBuffer}, - {indexBuffBind, indexBuffer}, - }); - - glCheckError(); + { + auto pipelineConfig = gal->CreatePipelineConfig(); + pipelineConfig->AddShaderParameter("viewportSize", Gal::Type::Vec2u32); + pipelineConfig->AddShaderParameter("translation", Gal::Type::Vec2); + pipelineConfig->SetTarget(gal->GetDefaultFramebuffer()); + pipelineConfig->SetVertexShader(gal->LoadVertexShader(vertexSource)); + pipelineConfig->SetPixelShader(gal->LoadPixelShader(pixelSource)); - auto texturePipelineConfig = gal->CreatePipelineConfig(); - texturePipelineConfig->AddShaderParameter("viewportSize", Gal::Type::Vec2u32); - texturePipelineConfig->AddShaderParameter("translation", Gal::Type::Vec2); - texturePipelineConfig->AddShaderParameter("fontTexture", Gal::Type::Int32); - texturePipelineConfig->SetTarget(gal->GetDefaultFramebuffer()); + auto vertBuffBind = pipelineConfig->BindVertexBuffer({ + {"pos", Gal::Type::Vec2}, + {"color", Gal::Type::Vec4u8}, + {"", Gal::Type::Vec2}, //it's not used in shader, so driver optimizes it away + }); - auto texturePixelAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/rmltex"); - std::string texturePixelSource((char*)texturePixelAsset->data.data(), (char*)texturePixelAsset->data.data() + texturePixelAsset->data.size()); - texturePipelineConfig->SetVertexShader(gal->LoadVertexShader(vertSource)); - texturePipelineConfig->SetPixelShader(gal->LoadPixelShader(texturePixelSource)); + auto indexBuffBind = pipelineConfig->BindIndexBuffer(); - auto texVertBuffBind = texturePipelineConfig->BindVertexBuffer({ - {"pos", Gal::Type::Vec2}, - {"color", Gal::Type::Vec4u8}, - {"tex_coord", Gal::Type::Vec2}, - }); + pipeline = gal->BuildPipeline(pipelineConfig); - auto texIndexBuffBind = texturePipelineConfig->BindIndexBuffer(); - - texPipeline = gal->BuildPipeline(texturePipelineConfig); + pipelineInstance = pipeline->CreateInstance({ + {vertBuffBind, vertexBuffer}, + {indexBuffBind, indexBuffer}, + }); + } + + { + auto texPipelineConfig = gal->CreatePipelineConfig(); + texPipelineConfig->AddShaderParameter("viewportSize", Gal::Type::Vec2u32); + texPipelineConfig->AddShaderParameter("translation", Gal::Type::Vec2); + texPipelineConfig->AddShaderParameter("fontTexture", Gal::Type::Int32); + texPipelineConfig->SetTarget(gal->GetDefaultFramebuffer()); + texPipelineConfig->SetVertexShader(gal->LoadVertexShader(vertexSource)); + texPipelineConfig->SetPixelShader(gal->LoadPixelShader(texPixelSource)); + + auto texVertBuffBind = texPipelineConfig->BindVertexBuffer({ + {"pos", Gal::Type::Vec2}, + {"color", Gal::Type::Vec4u8}, + {"tex_coord", Gal::Type::Vec2}, + }); + + auto texIndexBuffBind = texPipelineConfig->BindIndexBuffer(); + + texPipeline = gal->BuildPipeline(texPipelineConfig); + + texPipelineInstance = texPipeline->CreateInstance({ + {texVertBuffBind, vertexBuffer}, + {texIndexBuffBind, indexBuffer}, + }); + } - texPipelineInstance = texPipeline->CreateInstance({ - {texVertBuffBind, vertexBuffer}, - {texIndexBuffBind, indexBuffer}, - }); - glCheckError(); + } RmlRenderInterface::~RmlRenderInterface() { - glCheckError(); } void RmlRenderInterface::RenderGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices, Rml::TextureHandle texture, const Rml::Vector2f& translation) { indexBuffer->SetData({ reinterpret_cast(indices), reinterpret_cast(indices + num_indices) }); vertexBuffer->SetData({ reinterpret_cast(vertices), reinterpret_cast(vertices + num_vertices) }); - glCheckError(); auto tex = textures.find(texture); if (tex != textures.end()) { texPipeline->Activate(); - glCheckError(); texPipeline->SetShaderParameter("translation", glm::vec2(translation.x, translation.y)); - glCheckError(); texPipeline->SetDynamicTexture("fontTexture", tex->second); - glCheckError(); texPipelineInstance->Activate(); - glCheckError(); texPipelineInstance->Render(0, num_indices); } else { pipeline->Activate(); - glCheckError(); pipeline->SetShaderParameter("translation", glm::vec2(translation.x, translation.y)); - glCheckError(); pipelineInstance->Activate(); - glCheckError(); pipelineInstance->Render(0, num_indices); } - glCheckError(); } void RmlRenderInterface::EnableScissorRegion(bool enable) { - if (enable) - glEnable(GL_SCISSOR_TEST); - else - glDisable(GL_SCISSOR_TEST); + Gal::GetImplementation()->SetScissor(enable); } void RmlRenderInterface::SetScissorRegion(int x, int y, int width, int height) { - glScissor(x, vpHeight - (y + height), width, height); - glCheckError(); + Gal::GetImplementation()->SetScissor(x, y, width, height); } bool RmlRenderInterface::LoadTexture(Rml::TextureHandle& texture_handle, Rml::Vector2i& texture_dimensions, const Rml::String& source) { @@ -157,7 +149,6 @@ 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) { - glCheckError(); 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); @@ -165,26 +156,17 @@ bool RmlRenderInterface::GenerateTexture(Rml::TextureHandle& texture_handle, con texture->SetData({ reinterpret_cast(source),reinterpret_cast(source + (source_dimensions.x * source_dimensions.y) * 4) }); textures.insert({ textureId,texture }); texture_handle = textureId; - glCheckError(); return true; } void RmlRenderInterface::ReleaseTexture(Rml::TextureHandle texture) { - GLuint textures = texture; - glDeleteTextures(1, &textures); - glCheckError(); + textures.erase(textures.find(texture)); } void RmlRenderInterface::Update(unsigned int windowWidth, unsigned int windowHeight) { - - glCheckError(); - pipeline->SetShaderParameter("viewportSize", glm::uvec2(windowWidth, windowHeight)); texPipeline->SetShaderParameter("viewportSize", glm::uvec2(windowWidth, windowHeight)); - texPipeline->SetShaderParameter("fontTexture", 0); - - glCheckError(); vpWidth = windowWidth; vpHeight = windowHeight; -- cgit v1.2.3 From 83c61036966c4c358a094cabe27c8de60082200d Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sat, 20 Nov 2021 00:29:28 +0500 Subject: Removed every reference to OpenGL except GalOgl.cpp --- src/Rml.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Rml.cpp') diff --git a/src/Rml.cpp b/src/Rml.cpp index d886377..fa2d4e7 100644 --- a/src/Rml.cpp +++ b/src/Rml.cpp @@ -44,7 +44,7 @@ void RmlSystemInterface::GetClipboardText(Rml::String& text) { text = clipboard; } -RmlRenderInterface::RmlRenderInterface(RenderState& renderState) { +RmlRenderInterface::RmlRenderInterface() { std::string vertexSource, pixelSource, texPixelSource; { auto vertAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/vert/rml"); -- cgit v1.2.3