Use GLM in RHI for uniforms and color parameters

This commit is contained in:
Eidolon 2023-03-23 13:19:48 -05:00
parent ddf9de1757
commit e088577924
6 changed files with 108 additions and 103 deletions

View file

@ -11,6 +11,7 @@
#include <optional>
#include <glm/gtc/matrix_transform.hpp>
#include <tcb/span.hpp>
#include "../cxxutil.hpp"
@ -143,23 +144,25 @@ void BlitRectPass::transfer(Rhi& rhi, Handle<TransferContext> ctx)
std::array<rhi::UniformVariant, 1> g1_uniforms = {{
// Projection
std::array<std::array<float, 4>, 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::mat4>(),
glm::vec3(taller ? 1.f : 1.f / output_aspect, taller ? -1.f / (1.f / output_aspect) : -1.f, 1.f)
)
}};
std::array<rhi::UniformVariant, 2> g2_uniforms = {
{// ModelView
std::array<std::array<float, 4>, 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<std::array<float, 3>, 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::mat4>(),
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<TransferContext> 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<GraphicsContext> ctx)
{

View file

@ -162,20 +162,30 @@ void ImguiPass::transfer(Rhi& rhi, Handle<TransferContext> ctx)
rhi.update_buffer_contents(ctx, ibo, 0, tcb::as_bytes(index_span));
// Uniform sets
std::array<UniformVariant, 1> g1_uniforms = {{
std::array<UniformVariant, 1> g1_uniforms = {
// Projection
std::array<std::array<float, 4>, 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<UniformVariant, 2> g2_uniforms = {
{// ModelView
std::array<std::array<float, 4>, 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<std::array<float, 3>, 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<UniformSet> us_1 = rhi.create_uniform_set(ctx, {g1_uniforms});
Handle<UniformSet> us_2 = rhi.create_uniform_set(ctx, {g2_uniforms});

View file

@ -12,6 +12,7 @@
#include <string>
#include <fmt/format.h>
#include <glm/gtc/matrix_transform.hpp>
#include <tcb/span.hpp>
#include "../f_finale.h"
@ -188,10 +189,10 @@ void PostprocessWipePass::transfer(Rhi& rhi, Handle<TransferContext> ctx)
rhi.update_texture(ctx, wipe_tex_, {0, 0, mask_w_, mask_h_}, PixelFormat::kR8, data);
UniformVariant uniforms[] = {
{std::array<std::array<float, 4>, 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<int32_t>(wipe_color_mode_)},
{static_cast<int32_t>(wipe_swizzle_)}};
glm::scale(glm::identity<glm::mat4>(), glm::vec3(2.f, 2.f, 1.f)),
static_cast<int32_t>(wipe_color_mode_),
static_cast<int32_t>(wipe_swizzle_)
};
us_ = rhi.create_uniform_set(ctx, {tcb::span(uniforms)});
VertexAttributeBufferBinding vbos[] = {{0, vbo_}};

View file

@ -12,6 +12,7 @@
#include <unordered_set>
#include <stb_rect_pack.h>
#include <glm/gtc/matrix_transform.hpp>
#include "../r_patch.h"
#include "../v_video.h"
@ -879,27 +880,28 @@ void TwodeePass::transfer(Rhi& rhi, Handle<TransferContext> ctx)
}
// Uniform sets
std::array<UniformVariant, 1> g1_uniforms = {{
std::array<UniformVariant, 1> g1_uniforms = {
// Projection
std::array<std::array<float, 4>, 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<UniformVariant, 3> g2_uniforms = {
{// ModelView
std::array<std::array<float, 4>, 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<std::array<float, 3>, 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<int32_t>(1)}};
// ModelView
glm::identity<glm::mat4>(),
// Texcoord0 Transform
glm::identity<glm::mat3>(),
// Sampler 0 Is Indexed Alpha (yes, it always is)
static_cast<int32_t>(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<GraphicsContext> ctx)
{

View file

@ -18,6 +18,7 @@
#include <fmt/format.h>
#include <glad/gl.h>
#include <glm/gtc/type_ptr.hpp>
using namespace srb2;
using namespace rhi;
@ -1426,19 +1427,19 @@ void GlCoreRhi::bind_uniform_set(Handle<GraphicsContext> ctx, uint32_t slot, Han
gl_->Uniform1f(pipeline_uniform, value);
GL_ASSERT
},
[&](const std::array<float, 2>& 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<float, 3>& 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<float, 4>& 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<GraphicsContext> ctx, uint32_t slot, Han
gl_->Uniform1i(pipeline_uniform, value);
GL_ASSERT
},
[&](const std::array<int32_t, 2>& 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<int32_t, 3>& 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<int32_t, 4>& 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<std::array<float, 2>, 2>& value)
[&](const glm::mat2& value)
{
gl_->UniformMatrix2fv(pipeline_uniform, 1, false, reinterpret_cast<const GLfloat*>(&value));
gl_->UniformMatrix2fv(pipeline_uniform, 1, false, glm::value_ptr(value));
GL_ASSERT
},
[&](const std::array<std::array<float, 3>, 3>& value)
[&](const glm::mat3& value)
{
gl_->UniformMatrix3fv(pipeline_uniform, 1, false, reinterpret_cast<const GLfloat*>(&value));
gl_->UniformMatrix3fv(pipeline_uniform, 1, false, glm::value_ptr(value));
GL_ASSERT
},
[&](const std::array<std::array<float, 4>, 4>& value)
[&](const glm::mat4& value)
{
gl_->UniformMatrix4fv(pipeline_uniform, 1, false, reinterpret_cast<const GLfloat*>(&value));
gl_->UniformMatrix4fv(pipeline_uniform, 1, false, glm::value_ptr(value));
GL_ASSERT
},
};

View file

@ -16,6 +16,12 @@
#include <variant>
#include <vector>
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
#include <glm/vec4.hpp>
#include <glm/mat2x2.hpp>
#include <glm/mat3x3.hpp>
#include <glm/mat4x4.hpp>
#include <tcb/span.hpp>
#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<RenderPass> render_pass;
TextureOrRenderbuffer color_attachment;
std::optional<TextureOrRenderbuffer> depth_attachment;
Color clear_color;
glm::vec4 clear_color;
};
using UniformVariant = std::variant<
float,
std::array<float, 2>,
std::array<float, 3>,
std::array<float, 4>,
glm::vec2,
glm::vec3,
glm::vec4,
int32_t,
std::array<int32_t, 2>,
std::array<int32_t, 3>,
std::array<int32_t, 4>,
glm::ivec2,
glm::ivec3,
glm::ivec4,
// The indexing order of matrices is [row][column].
std::array<std::array<float, 2>, 2>,
std::array<std::array<float, 3>, 3>,
std::array<std::array<float, 4>, 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<float, 2>&) const noexcept { return UniformFormat::kFloat2; }
UniformFormat operator()(const std::array<float, 3>&) const noexcept { return UniformFormat::kFloat3; }
UniformFormat operator()(const std::array<float, 4>&) 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<int32_t, 2>&) const noexcept { return UniformFormat::kInt2; }
UniformFormat operator()(const std::array<int32_t, 3>&) const noexcept { return UniformFormat::kInt3; }
UniformFormat operator()(const std::array<int32_t, 4>&) const noexcept { return UniformFormat::kInt4; }
UniformFormat operator()(const std::array<std::array<float, 2>, 2>&) const noexcept
{
return UniformFormat::kMat2;
}
UniformFormat operator()(const std::array<std::array<float, 3>, 3>&) const noexcept
{
return UniformFormat::kMat3;
}
UniformFormat operator()(const std::array<std::array<float, 4>, 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);
}