From c9384eb795ab2a0b2e8ea4747b6d54f3d456d088 Mon Sep 17 00:00:00 2001 From: Piotr Krygier Date: Wed, 7 Feb 2024 12:57:20 +0100 Subject: [PATCH] [WIP] Try to enable alpha blending --- graphics/shaders/shader.frag | 8 ++++++-- graphics/shaders/shader.vert | 6 +++--- graphics/src/rse_graphics.c | 20 ++++++++++---------- graphics/src/rse_math.h | 13 ++++++++++++- graphics/src/rse_text_renderer.c | 10 +++++----- graphics/src/vulkan_buffers.c | 4 ++-- graphics/src/vulkan_commons.h | 2 +- graphics/src/vulkan_pipeline.c | 4 ++-- 8 files changed, 41 insertions(+), 26 deletions(-) diff --git a/graphics/shaders/shader.frag b/graphics/shaders/shader.frag index 19ed2efd..c2089369 100644 --- a/graphics/shaders/shader.frag +++ b/graphics/shaders/shader.frag @@ -6,12 +6,16 @@ layout(set = 1, binding = 1) uniform sampler2D texSampler[]; -layout(location = 0) in vec3 fragColor; +layout(location = 0) in vec4 fragColor; layout(location = 1) in vec2 fragTexCoord; layout(location = 2) in flat uint instanceTextureId; layout(location = 0) out vec4 outColor; void main() { - outColor = vec4(fragColor * texture(texSampler[instanceTextureId], fragTexCoord).rgb, 1.0); + /* vec4 sampled = vec4(1.0, 1.0, 1.0, texture(texSampler[instanceTextureId], fragTexCoord).r);*/ + /* if (sampled.a < 0.1) + discard; + outColor = sampled; */ + outColor = fragColor * texture(texSampler[instanceTextureId], fragTexCoord).r; } diff --git a/graphics/shaders/shader.vert b/graphics/shaders/shader.vert index 9fd5d594..1d6e2365 100644 --- a/graphics/shaders/shader.vert +++ b/graphics/shaders/shader.vert @@ -7,7 +7,7 @@ layout(binding = 0) uniform UniformBufferObject { } ubo; layout(location = 0) in vec3 inPosition; -layout(location = 1) in vec3 inColor; +layout(location = 1) in vec4 inColor; layout(location = 2) in vec2 inTexCoord; layout(location = 3) in vec3 instancePosition; @@ -15,7 +15,7 @@ layout(location = 4) in vec3 instanceRotation; layout(location = 5) in float instanceScale; layout(location = 6) in uint instanceTextureId; -layout(location = 0) out vec3 fragColor; +layout(location = 0) out vec4 fragColor; layout(location = 1) out vec2 fragTexCoord; layout(location = 2) out uint outInstanceTextureId; @@ -66,4 +66,4 @@ void main() fragColor = inColor; fragTexCoord = inTexCoord; outInstanceTextureId = instanceTextureId; -} \ No newline at end of file +} diff --git a/graphics/src/rse_graphics.c b/graphics/src/rse_graphics.c index 032dff8e..17a36f42 100644 --- a/graphics/src/rse_graphics.c +++ b/graphics/src/rse_graphics.c @@ -44,18 +44,18 @@ void rse_graphics_test_function(void) return; } rse_fonts_initialize(); - rse_fonts_load_from_file("/usr/share/fonts/TTF/JetBrainsMonoNerdFont-Medium.ttf"); + rse_fonts_load_from_file("/usr/share/fonts/JetBrainsMonoNerdFont/JetBrainsMonoNerdFont-Medium.ttf"); // FIXME: Temporary array of vertices, for testing purposes - struct rse_vertex_t vertices[] = {{{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}, - {{0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f}}, - {{0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}}, - {{-0.5f, 0.5f, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}}}; + struct rse_vertex_t vertices[] = {{{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f}}, + {{0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f, 1.0f}, {1.0f, 0.0f}}, + {{0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f, 1.0f}, {1.0f, 1.0f}}, + {{-0.5f, 0.5f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}}}; - struct rse_vertex_t vertices2[] = {{{-0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}, - {{0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}}, - {{0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}}, - {{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}}}; + struct rse_vertex_t vertices2[] = {{{-0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}, + {{0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}}, + {{0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}}, + {{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}}}; // FIXME: Temporary array of vertices, for testing purposes uint16_t indices[] = {0, 1, 2, 2, 3, 0}; @@ -83,7 +83,7 @@ void rse_graphics_test_function(void) tex_id_2 }); - rse_print_text("Hello World!", (struct vec3_t){-1.0f, 1.0f, 0.0f}, 0.3f); + rse_print_text("Hello World!", (struct vec3_t){-1.0f, 0.0f, 0.0f}, 0.3f); } rse_err_t rse_graphics_run(void) diff --git a/graphics/src/rse_math.h b/graphics/src/rse_math.h index 7613895a..b4d96220 100644 --- a/graphics/src/rse_math.h +++ b/graphics/src/rse_math.h @@ -22,6 +22,17 @@ struct vec3_t { float z; }; +/** + * @brief 4 points vector, useful for colors with alpha channel + * + */ +struct vec4_t { + float x; + float y; + float z; + float w; +}; + /* 4x4 matrix. * [x_1 | x_2 | x_3 | x_4] * [y_1 | y_2 | y_3 | y_4] @@ -119,4 +130,4 @@ struct mat4_t rse_math_perspective(float fovy_rad, float aspect_ratio, float z_n */ struct mat4_t rse_math_rotate(struct mat4_t matrix, float radians, struct vec3_t rotation_vector); -#endif /* RSE_MATH_H */ \ No newline at end of file +#endif /* RSE_MATH_H */ diff --git a/graphics/src/rse_text_renderer.c b/graphics/src/rse_text_renderer.c index 86c32208..63aecece 100644 --- a/graphics/src/rse_text_renderer.c +++ b/graphics/src/rse_text_renderer.c @@ -98,16 +98,16 @@ rse_err_t rse_fonts_load_from_file(const char* file_path) } font_vertices[0] = (struct rse_vertex_t) { - {g_fonts_data[iter].bearing_x, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f} + {g_fonts_data[iter].bearing_x, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}, {0.0f, 0.0f} }; font_vertices[1] = (struct rse_vertex_t) { - {g_fonts_data[iter].bearing_x + g_fonts_data[iter].width, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f} + {g_fonts_data[iter].bearing_x + g_fonts_data[iter].width, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}, {1.0f, 0.0f} }; font_vertices[2] = (struct rse_vertex_t) { - {g_fonts_data[iter].bearing_x + g_fonts_data[iter].width, g_fonts_data[iter].height, 0.0f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f} + {g_fonts_data[iter].bearing_x + g_fonts_data[iter].width, g_fonts_data[iter].height, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}, {1.0f, 1.0f} }; font_vertices[3] = (struct rse_vertex_t) { - {g_fonts_data[iter].bearing_x, g_fonts_data[iter].height, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f} + {g_fonts_data[iter].bearing_x, g_fonts_data[iter].height, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}, {0.0f, 1.0f} }; g_fonts_data[iter].mesh_id = create_mesh(font_vertices, font_indices, 8, 6); @@ -139,7 +139,7 @@ rse_err_t rse_print_text(const char* text, struct vec3_t position, float font_si charactet_code = (size_t)text[iter] - FONT_START_CHARACTER; x_pos = position.x + advance - (g_fonts_data[charactet_code].bearing_x * scale); - y_pos = position.y - (g_fonts_data[charactet_code].bearing_y * scale); + y_pos = position.y - (g_fonts_data[charactet_code].bearing_y * scale); create_mesh_instance(g_fonts_data[charactet_code].mesh_id, (struct rse_instance_data_t){ {x_pos, y_pos, 0.0f}, {0.0f, 0.0f, rse_math_deg_to_radians(0.0f)}, diff --git a/graphics/src/vulkan_buffers.c b/graphics/src/vulkan_buffers.c index 889b0768..7a3df4cc 100644 --- a/graphics/src/vulkan_buffers.c +++ b/graphics/src/vulkan_buffers.c @@ -259,7 +259,7 @@ rse_err_t rse_add_vertices(uint16_t mesh_id, index_data->buffer_offset = index_buffer->allocated_size; index_buffer->allocated_size += indices_size; - vmaDestroyBuffer(g_vulkan_state.allocator, staging_buffer.buffer, staging_buffer.allocation); + destroy_buffer(&staging_buffer); draw_indirect_command.indexCount = vertex_data->count; draw_indirect_command.instanceCount = 0; @@ -295,7 +295,7 @@ rse_err_t update_mesh_instances(uint16_t mesh_id, struct rse_instance_data_t* in instance_size, instance_buffer->allocated_size); - vmaDestroyBuffer(g_vulkan_state.allocator, staging_buffer.buffer, staging_buffer.allocation); + destroy_buffer(&staging_buffer); instance_buffer->allocated_size += instance_size; diff --git a/graphics/src/vulkan_commons.h b/graphics/src/vulkan_commons.h index 3143cc38..05410303 100644 --- a/graphics/src/vulkan_commons.h +++ b/graphics/src/vulkan_commons.h @@ -29,7 +29,7 @@ extern struct rse_vulkan_state_t g_vulkan_state; struct rse_vertex_t { struct vec3_t pos; - struct vec3_t color; + struct vec4_t color; struct vec3_t tex_coords; }; diff --git a/graphics/src/vulkan_pipeline.c b/graphics/src/vulkan_pipeline.c index a38240e7..2db1b8ca 100644 --- a/graphics/src/vulkan_pipeline.c +++ b/graphics/src/vulkan_pipeline.c @@ -57,7 +57,7 @@ static VkVertexInputAttributeDescription* get_attribute_descriptions() attribute_descriptions[1].binding = 0; attribute_descriptions[1].location = 1; /* inColor in shader.vert */ - attribute_descriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT; + attribute_descriptions[1].format = VK_FORMAT_R32G32B32A32_SFLOAT; attribute_descriptions[1].offset = offsetof(struct rse_vertex_t, color); attribute_descriptions[2].binding = 0; @@ -640,7 +640,7 @@ static void fill_color_blend_state_info(VkPipelineColorBlendStateCreateInfo* col color_blending->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; color_blending->pNext = NULL; color_blending->flags = 0U; - color_blending->logicOpEnable = VK_TRUE; + color_blending->logicOpEnable = VK_FALSE; color_blending->logicOp = VK_LOGIC_OP_COPY; color_blending->attachmentCount = 1U; color_blending->pAttachments = color_blend_attachment;