From e08857792484935ba3c1460484038e0fd1570103 Mon Sep 17 00:00:00 2001 From: Eidolon Date: Thu, 23 Mar 2023 13:19:48 -0500 Subject: [PATCH] Use GLM in RHI for uniforms and color parameters --- src/hwr2/pass_blit_rect.cpp | 33 +++++++++------- src/hwr2/pass_imgui.cpp | 34 ++++++++++------ src/hwr2/pass_postprocess.cpp | 9 +++-- src/hwr2/pass_twodee.cpp | 32 ++++++++------- src/rhi/gl3_core/gl3_core_rhi.cpp | 37 ++++++++--------- src/rhi/rhi.hpp | 66 +++++++++++++------------------ 6 files changed, 108 insertions(+), 103 deletions(-) diff --git a/src/hwr2/pass_blit_rect.cpp b/src/hwr2/pass_blit_rect.cpp index 56fb4f6dc..74726cf7d 100644 --- a/src/hwr2/pass_blit_rect.cpp +++ b/src/hwr2/pass_blit_rect.cpp @@ -11,6 +11,7 @@ #include +#include #include #include "../cxxutil.hpp" @@ -143,23 +144,25 @@ void BlitRectPass::transfer(Rhi& rhi, Handle ctx) std::array g1_uniforms = {{ // Projection - std::array, 4> { - {{taller ? 1.f : 1.f / output_aspect, 0.f, 0.f, 0.f}, - {0.f, taller ? -1.f / (1.f / output_aspect) : -1.f, 0.f, 0.f}, - {0.f, 0.f, 1.f, 0.f}, - {0.f, 0.f, 0.f, 1.f}}}, + glm::scale( + glm::identity(), + glm::vec3(taller ? 1.f : 1.f / output_aspect, taller ? -1.f / (1.f / output_aspect) : -1.f, 1.f) + ) }}; std::array g2_uniforms = { - {// ModelView - std::array, 4> { - {{taller ? 2.f : 2.f * aspect, 0.f, 0.f, 0.f}, - {0.f, taller ? 2.f * (1.f / aspect) : 2.f, 0.f, 0.f}, - {0.f, 0.f, 1.f, 0.f}, - {0.f, 0.f, 0.f, 1.f}}}, - // Texcoord0 Transform - std::array, 3> { - {{1.f, 0.f, 0.f}, {0.f, output_flip_ ? -1.f : 1.f, 0.f}, {0.f, 0.f, 1.f}}}}}; + // ModelView + glm::scale( + glm::identity(), + glm::vec3(taller ? 2.f : 2.f * aspect, taller ? 2.f * (1.f / aspect) : 2.f, 1.f) + ), + // Texcoord0 Transform + glm::mat3( + glm::vec3(1.f, 0.f, 0.f), + glm::vec3(0.f, output_flip_ ? -1.f : 1.f, 0.f), + glm::vec3(0.f, 0.f, 1.f) + ) + }; uniform_sets_[0] = rhi.create_uniform_set(ctx, {g1_uniforms}); uniform_sets_[1] = rhi.create_uniform_set(ctx, {g2_uniforms}); @@ -178,7 +181,7 @@ void BlitRectPass::transfer(Rhi& rhi, Handle ctx) } } -static constexpr const rhi::Color kClearColor = {0, 0, 0, 1}; +static constexpr const glm::vec4 kClearColor = {0, 0, 0, 1}; void BlitRectPass::graphics(Rhi& rhi, Handle ctx) { diff --git a/src/hwr2/pass_imgui.cpp b/src/hwr2/pass_imgui.cpp index e0d1d3cb6..e644e3c09 100644 --- a/src/hwr2/pass_imgui.cpp +++ b/src/hwr2/pass_imgui.cpp @@ -162,20 +162,30 @@ void ImguiPass::transfer(Rhi& rhi, Handle ctx) rhi.update_buffer_contents(ctx, ibo, 0, tcb::as_bytes(index_span)); // Uniform sets - std::array g1_uniforms = {{ + std::array g1_uniforms = { // Projection - std::array, 4> { - {{2.f / vid.realwidth, 0.f, 0.f, 0.f}, - {0.f, 2.f / vid.realheight, 0.f, 0.f}, - {0.f, 0.f, 1.f, 0.f}, - {-1.f, 1.f, 0.f, 1.f}}}, - }}; + glm::mat4( + glm::vec4(2.f / vid.realwidth, 0.f, 0.f, 0.f), + glm::vec4(0.f, 2.f / vid.realheight, 0.f, 0.f), + glm::vec4(0.f, 0.f, 1.f, 0.f), + glm::vec4(-1.f, 1.f, 0.f, 1.f) + ) + }; std::array g2_uniforms = { - {// ModelView - std::array, 4> { - {{1.f, 0.f, 0.f, 0.f}, {0.f, -1.f, 0.f, 0.f}, {0.f, 0.f, 1.f, 0.f}, {0.f, 0, 0.f, 1.f}}}, - // Texcoord0 Transform - std::array, 3> {{{1.f, 0.f, 0.f}, {0.f, 1.f, 0.f}, {0.f, 0.f, 1.f}}}}}; + // ModelView + glm::mat4( + glm::vec4(1.f, 0.f, 0.f, 0.f), + glm::vec4(0.f, -1.f, 0.f, 0.f), + glm::vec4(0.f, 0.f, 1.f, 0.f), + glm::vec4(0.f, 0, 0.f, 1.f) + ), + // Texcoord0 Transform + glm::mat3( + glm::vec3(1.f, 0.f, 0.f), + glm::vec3(0.f, 1.f, 0.f), + glm::vec3(0.f, 0.f, 1.f) + ) + }; Handle us_1 = rhi.create_uniform_set(ctx, {g1_uniforms}); Handle us_2 = rhi.create_uniform_set(ctx, {g2_uniforms}); diff --git a/src/hwr2/pass_postprocess.cpp b/src/hwr2/pass_postprocess.cpp index 240ef1c47..e119ebbb1 100644 --- a/src/hwr2/pass_postprocess.cpp +++ b/src/hwr2/pass_postprocess.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include "../f_finale.h" @@ -188,10 +189,10 @@ void PostprocessWipePass::transfer(Rhi& rhi, Handle ctx) rhi.update_texture(ctx, wipe_tex_, {0, 0, mask_w_, mask_h_}, PixelFormat::kR8, data); UniformVariant uniforms[] = { - {std::array, 4> { - {{2.f, 0.f, 0.f, 0.f}, {0.f, 2.f, 0.f, 0.f}, {0.f, 0.f, 1.f, 0.f}, {0.f, 0.f, 0.f, 1.f}}}}, - {static_cast(wipe_color_mode_)}, - {static_cast(wipe_swizzle_)}}; + glm::scale(glm::identity(), glm::vec3(2.f, 2.f, 1.f)), + static_cast(wipe_color_mode_), + static_cast(wipe_swizzle_) + }; us_ = rhi.create_uniform_set(ctx, {tcb::span(uniforms)}); VertexAttributeBufferBinding vbos[] = {{0, vbo_}}; diff --git a/src/hwr2/pass_twodee.cpp b/src/hwr2/pass_twodee.cpp index 7b9d969d2..6fd9b521e 100644 --- a/src/hwr2/pass_twodee.cpp +++ b/src/hwr2/pass_twodee.cpp @@ -12,6 +12,7 @@ #include #include +#include #include "../r_patch.h" #include "../v_video.h" @@ -879,27 +880,28 @@ void TwodeePass::transfer(Rhi& rhi, Handle ctx) } // Uniform sets - std::array g1_uniforms = {{ + std::array g1_uniforms = { // Projection - std::array, 4> { - {{2.f / vid.width, 0.f, 0.f, 0.f}, - {0.f, -2.f / vid.height, 0.f, 0.f}, - {0.f, 0.f, 1.f, 0.f}, - {-1.f, 1.f, 0.f, 1.f}}}, - }}; + glm::mat4( + glm::vec4(2.f / vid.width, 0.f, 0.f, 0.f), + glm::vec4(0.f, -2.f / vid.height, 0.f, 0.f), + glm::vec4(0.f, 0.f, 1.f, 0.f), + glm::vec4(-1.f, 1.f, 0.f, 1.f) + ), + }; std::array g2_uniforms = { - {// ModelView - std::array, 4> { - {{1.f, 0.f, 0.f, 0.f}, {0.f, 1.f, 0.f, 0.f}, {0.f, 0.f, 1.f, 0.f}, {0.f, 0.f, 0.f, 1.f}}}, - // Texcoord0 Transform - std::array, 3> {{{1.f, 0.f, 0.f}, {0.f, 1.f, 0.f}, {0.f, 0.f, 1.f}}}, - // Sampler 0 Is Indexed Alpha (yes, it always is) - static_cast(1)}}; + // ModelView + glm::identity(), + // Texcoord0 Transform + glm::identity(), + // Sampler 0 Is Indexed Alpha (yes, it always is) + static_cast(1) + }; us_1 = rhi.create_uniform_set(ctx, {tcb::span(g1_uniforms)}); us_2 = rhi.create_uniform_set(ctx, {tcb::span(g2_uniforms)}); } -static constexpr const rhi::Color kClearColor = {0, 0, 0, 1}; +static constexpr const glm::vec4 kClearColor = {0, 0, 0, 1}; void TwodeePass::graphics(Rhi& rhi, Handle ctx) { diff --git a/src/rhi/gl3_core/gl3_core_rhi.cpp b/src/rhi/gl3_core/gl3_core_rhi.cpp index 856913d53..f5226e862 100644 --- a/src/rhi/gl3_core/gl3_core_rhi.cpp +++ b/src/rhi/gl3_core/gl3_core_rhi.cpp @@ -18,6 +18,7 @@ #include #include +#include using namespace srb2; using namespace rhi; @@ -1426,19 +1427,19 @@ void GlCoreRhi::bind_uniform_set(Handle ctx, uint32_t slot, Han gl_->Uniform1f(pipeline_uniform, value); GL_ASSERT }, - [&](const std::array& value) + [&](const glm::vec2& value) { - gl_->Uniform2f(pipeline_uniform, value[0], value[1]); + gl_->Uniform2f(pipeline_uniform, value.x, value.y); GL_ASSERT }, - [&](const std::array& value) + [&](const glm::vec3& value) { - gl_->Uniform3f(pipeline_uniform, value[0], value[1], value[2]); + gl_->Uniform3f(pipeline_uniform, value.x, value.y, value.z); GL_ASSERT }, - [&](const std::array& value) + [&](const glm::vec4& value) { - gl_->Uniform4f(pipeline_uniform, value[0], value[1], value[2], value[3]); + gl_->Uniform4f(pipeline_uniform, value.x, value.y, value.z, value.w); GL_ASSERT }, [&](const int32_t& value) @@ -1446,34 +1447,34 @@ void GlCoreRhi::bind_uniform_set(Handle ctx, uint32_t slot, Han gl_->Uniform1i(pipeline_uniform, value); GL_ASSERT }, - [&](const std::array& value) + [&](const glm::ivec2& value) { - gl_->Uniform2i(pipeline_uniform, value[0], value[1]); + gl_->Uniform2i(pipeline_uniform, value.x, value.y); GL_ASSERT }, - [&](const std::array& value) + [&](const glm::ivec3& value) { - gl_->Uniform3i(pipeline_uniform, value[0], value[1], value[2]); + gl_->Uniform3i(pipeline_uniform, value.x, value.y, value.z); GL_ASSERT }, - [&](const std::array& value) + [&](const glm::ivec4& value) { - gl_->Uniform4i(pipeline_uniform, value[0], value[1], value[2], value[3]); + gl_->Uniform4i(pipeline_uniform, value.x, value.y, value.z, value.w); GL_ASSERT }, - [&](const std::array, 2>& value) + [&](const glm::mat2& value) { - gl_->UniformMatrix2fv(pipeline_uniform, 1, false, reinterpret_cast(&value)); + gl_->UniformMatrix2fv(pipeline_uniform, 1, false, glm::value_ptr(value)); GL_ASSERT }, - [&](const std::array, 3>& value) + [&](const glm::mat3& value) { - gl_->UniformMatrix3fv(pipeline_uniform, 1, false, reinterpret_cast(&value)); + gl_->UniformMatrix3fv(pipeline_uniform, 1, false, glm::value_ptr(value)); GL_ASSERT }, - [&](const std::array, 4>& value) + [&](const glm::mat4& value) { - gl_->UniformMatrix4fv(pipeline_uniform, 1, false, reinterpret_cast(&value)); + gl_->UniformMatrix4fv(pipeline_uniform, 1, false, glm::value_ptr(value)); GL_ASSERT }, }; diff --git a/src/rhi/rhi.hpp b/src/rhi/rhi.hpp index a6186ba97..7b8d18065 100644 --- a/src/rhi/rhi.hpp +++ b/src/rhi/rhi.hpp @@ -16,6 +16,12 @@ #include #include +#include +#include +#include +#include +#include +#include #include #include "../core/static_vec.hpp" @@ -209,14 +215,6 @@ enum class SamplerName kSampler3 }; -struct Color -{ - float r; - float g; - float b; - float a; -}; - struct Rect { int32_t x; @@ -391,7 +389,7 @@ struct PipelineDesc PrimitiveType primitive; CullMode cull; FaceWinding winding; - Color blend_color; + glm::vec4 blend_color; }; struct RenderPassDesc @@ -428,50 +426,40 @@ struct RenderPassBeginInfo Handle render_pass; TextureOrRenderbuffer color_attachment; std::optional depth_attachment; - Color clear_color; + glm::vec4 clear_color; }; using UniformVariant = std::variant< float, - std::array, - std::array, - std::array, + glm::vec2, + glm::vec3, + glm::vec4, int32_t, - std::array, - std::array, - std::array, + glm::ivec2, + glm::ivec3, + glm::ivec4, - // The indexing order of matrices is [row][column]. - - std::array, 2>, - std::array, 3>, - std::array, 4>>; + glm::mat2, + glm::mat3, + glm::mat4 +>; inline constexpr UniformFormat uniform_variant_format(const UniformVariant& variant) { struct Visitor { UniformFormat operator()(const float&) const noexcept { return UniformFormat::kFloat; } - UniformFormat operator()(const std::array&) const noexcept { return UniformFormat::kFloat2; } - UniformFormat operator()(const std::array&) const noexcept { return UniformFormat::kFloat3; } - UniformFormat operator()(const std::array&) const noexcept { return UniformFormat::kFloat4; } + UniformFormat operator()(const glm::vec2&) const noexcept { return UniformFormat::kFloat2; } + UniformFormat operator()(const glm::vec3&) const noexcept { return UniformFormat::kFloat3; } + UniformFormat operator()(const glm::vec4&) const noexcept { return UniformFormat::kFloat4; } UniformFormat operator()(const int32_t&) const noexcept { return UniformFormat::kInt; } - UniformFormat operator()(const std::array&) const noexcept { return UniformFormat::kInt2; } - UniformFormat operator()(const std::array&) const noexcept { return UniformFormat::kInt3; } - UniformFormat operator()(const std::array&) const noexcept { return UniformFormat::kInt4; } - UniformFormat operator()(const std::array, 2>&) const noexcept - { - return UniformFormat::kMat2; - } - UniformFormat operator()(const std::array, 3>&) const noexcept - { - return UniformFormat::kMat3; - } - UniformFormat operator()(const std::array, 4>&) const noexcept - { - return UniformFormat::kMat4; - } + UniformFormat operator()(const glm::ivec2&) const noexcept { return UniformFormat::kInt2; } + UniformFormat operator()(const glm::ivec3&) const noexcept { return UniformFormat::kInt3; } + UniformFormat operator()(const glm::ivec4&) const noexcept { return UniformFormat::kInt4; } + UniformFormat operator()(const glm::mat2&) const noexcept { return UniformFormat::kMat2; } + UniformFormat operator()(const glm::mat3&) const noexcept { return UniformFormat::kMat3; } + UniformFormat operator()(const glm::mat4&) const noexcept { return UniformFormat::kMat4; } }; return std::visit(Visitor {}, variant); }