Skip to content

Commit

Permalink
[ren.quad] various fixes, aswell as a TRS modification which seems co…
Browse files Browse the repository at this point in the history
…rrect but could potentially be wrong
  • Loading branch information
harrand committed Oct 31, 2024
1 parent 0792364 commit 2a71f6d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/tz/core/trs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace tz

tz::m4f trs::matrix() const
{
return matrix_translate(this->translate) * this->rotate.matrix() * matrix_scale(this->scale);
return matrix_scale(this->scale) * matrix_translate(this->translate) * this->rotate.matrix();
}

trs trs::from_matrix(tz::m4f mat)
Expand Down
53 changes: 35 additions & 18 deletions src/tz/ren/quad.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "tz/ren/quad.hpp"
#include "tz/core/matrix.hpp"
#include "tz/core/trs.hpp"
#include "tz/topaz.hpp"
#include "tz/gpu/resource.hpp"
#include "tz/gpu/pass.hpp"
Expand All @@ -12,13 +13,19 @@

namespace tz::ren
{
struct quad_internal_data
{
tz::trs transform;
};

struct quad_renderer_data
{
quad_renderer_info info = {};
tz::gpu::resource_handle data_buffer = tz::nullhand;
tz::gpu::resource_handle camera_buffer = tz::nullhand;
tz::gpu::pass_handle main_pass = tz::nullhand;
tz::gpu::graph_handle graph = tz::nullhand;
std::vector<quad_internal_data> internals = {};
std::size_t quad_count = 0;
std::size_t texture_count = 0;
unsigned int window_width_cache;
Expand All @@ -27,8 +34,8 @@ namespace tz::ren

struct quad_data
{
tz::v4f pos_scale = {0.0f, 0.0f, 1.0f, 1.0f};
tz::v3f colour_tint = {1.0f, 1.0f, 1.0f};
tz::m4f model = tz::m4f::iden();
tz::v3f colour = {1.0f, 1.0f, 1.0f};
std::uint32_t texture_id = -1;
};

Expand Down Expand Up @@ -123,13 +130,21 @@ namespace tz::ren

std::expected<quad_handle, tz::error_code> quad_renderer_create_quad(quad_renderer_handle renh, quad_info info)
{
auto& ren = renderers[renh.peek()];

quad_internal_data& internal = ren.internals.emplace_back();
internal.transform =
{
.translate = {info.position[0], info.position[1], 0.0f},
.rotate = tz::quat::from_axis_angle({0.0f, 0.0f, 1.0f}, info.rotation),
.scale = {info.scale[0], info.scale[1], 1.0f}
};

quad_data new_data;
new_data.pos_scale = {info.position[0], info.position[1], info.scale[0], info.scale[1]};
new_data.colour_tint = info.colour;
new_data.model = internal.transform.matrix();
new_data.colour = info.colour;
new_data.texture_id = info.texture_id;

auto& ren = renderers[renh.peek()];

if(info.texture_id != static_cast<unsigned int>(-1))
{
if(info.texture_id >= ren.texture_count)
Expand Down Expand Up @@ -158,46 +173,48 @@ namespace tz::ren
tz::v2f get_quad_position(quad_renderer_handle renh, quad_handle quad)
{
const auto& ren = renderers[renh.peek()];
auto quad_data_array = tz::gpu::resource_read(ren.data_buffer);
tz::v4f pos_scale = *reinterpret_cast<const tz::v4f*>(quad_data_array.data() + (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, pos_scale));
return {pos_scale[0], pos_scale[1]};
tz::v3f pos = ren.internals[quad.peek()].transform.translate;
return {pos[0], pos[1]};
}

void set_quad_position(quad_renderer_handle renh, quad_handle quad, tz::v2f position)
{
auto& ren = renderers[renh.peek()];
std::size_t offset = (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, pos_scale);
auto& internal = ren.internals[quad.peek()];
internal.transform.translate = {position[0], position[1]};

tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span<const tz::v2f>(&position, 1)), offset);
tz::m4f model = internal.transform.matrix();
tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span<const tz::m4f>(&model, 1)), sizeof(quad_data) * quad.peek() + offsetof(quad_data, model));
}

tz::v2f get_quad_scale(quad_renderer_handle renh, quad_handle quad)
{
const auto& ren = renderers[renh.peek()];
auto quad_data_array = tz::gpu::resource_read(ren.data_buffer);
tz::v4f pos_scale = *reinterpret_cast<const tz::v4f*>(quad_data_array.data() + (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, pos_scale));
return {pos_scale[2], pos_scale[3]};
tz::v3f scale = ren.internals[quad.peek()].transform.scale;
return {scale[0], scale[1]};
}

void set_quad_scale(quad_renderer_handle renh, quad_handle quad, tz::v2f scale)
{
auto& ren = renderers[renh.peek()];
std::size_t offset = (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, pos_scale) + sizeof(tz::v2f);
auto& internal = ren.internals[quad.peek()];
internal.transform.scale = {scale[0], scale[1]};

tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span<const tz::v2f>(&scale, 1)), offset);
tz::m4f model = internal.transform.matrix();
tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span<const tz::m4f>(&model, 1)), sizeof(quad_data) * quad.peek() + offsetof(quad_data, model));
}

tz::v3f get_quad_colour(quad_renderer_handle renh, quad_handle quad)
{
const auto& ren = renderers[renh.peek()];
auto quad_data_array = tz::gpu::resource_read(ren.data_buffer);
return *reinterpret_cast<const tz::v3f*>(quad_data_array.data() + (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, colour_tint));
return *reinterpret_cast<const tz::v3f*>(quad_data_array.data() + (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, colour));
}

void set_quad_colour(quad_renderer_handle renh, quad_handle quad, tz::v3f colour)
{
auto& ren = renderers[renh.peek()];
std::size_t offset = (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, colour_tint);
std::size_t offset = (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, colour);

tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span<const tz::v3f>(&colour, 1)), offset);
}
Expand Down
12 changes: 4 additions & 8 deletions src/tz/ren/quad.vertex.tzsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ shader(type = vertex);

struct quad_data
{
vec4 pos_scale;
float tint_red;
float tint_green;
float tint_blue;
mat4 model;
vec3 colour;
uint texture_id;
};

Expand Down Expand Up @@ -39,10 +37,8 @@ void main()
vec2 local_pos = quad_positions[in::vertex_id % 6];
quad_data cur_quad = quad.data[quad_id];

vec2 position_worldspace = cur_quad.pos_scale.xy + (local_pos * cur_quad.pos_scale.zw);

out::position = camera.projection * vec4(position_worldspace, 0, 1);
out::tint = vec3(cur_quad.tint_red, cur_quad.tint_green, cur_quad.tint_blue);
out::position = camera.projection * cur_quad.model * vec4(local_pos, 0, 1);
out::tint = vec3(cur_quad.colour);
out::uv = quad_texcoords[in::vertex_id % 6];
out::texture_id = cur_quad.texture_id;
}

0 comments on commit 2a71f6d

Please sign in to comment.