Skip to content

Commit

Permalink
Merge remote-tracking branch 'gitlab/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
ravi688 committed May 9, 2024
2 parents 6cf2f5b + 41bf4cc commit 76149bb
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
2 changes: 1 addition & 1 deletion dependencies/MeshLib
6 changes: 6 additions & 0 deletions include/renderer/internal/vulkan/vulkan_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ typedef struct vulkan_vertex_buffer_t
{
vulkan_buffer_ptr_t buffer;
u32 binding;
/* holds true if this vulkan buffer has been created internally by calling vulkan_buffer_destroy,
* this extra information is needed for graceful vulkan object deallocation */
bool is_internal;
} vulkan_vertex_buffer_t;

/* list of vulkan_vertex_buffer_t */
Expand All @@ -88,6 +91,9 @@ typedef struct vulkan_index_buffer_t
{
vulkan_buffer_ptr_t buffer;
VkIndexType index_type;
/* holds true if this vulkan buffer has been created internally, otherwise false, i.e.
* if the vulkan buffer has been set using vulkan_mesh_set_index_buffer */
bool is_internal;
} vulkan_index_buffer_t;

typedef struct vulkan_material_t vulkan_material_t;
Expand Down
8 changes: 8 additions & 0 deletions include/renderer/shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,32 @@
typedef vulkan_shader_create_info_t shader_create_info_t;
typedef vulkan_shader_handle_t shader_handle_t;
#define SHADER_HANDLE_INVALID VULKAN_SHADER_HANDLE_INVALID
#define SHADER(ptr) VULKAN_SHADER(ptr)
#define SHADER_CONST(ptr) VULKAN_SHADER_CONST(ptr)
#elif RENDERER_OPENGL_DRIVER
#include <renderer/internal/opengl/opengl_shader.h>
typedef opengl_shader_t shader_t;
typedef opengl_shader_create_info_t shader_create_info_t;
typedef opengl_shader_handle_t shader_handle_t;
#define SHADER_HANDLE_INVALID OPENGL_SHADER_HANDLE_INVALID
#define SHADER(ptr) OPENGL_SHADER(ptr)
#define SHADER_CONST(ptr) OPENGL_SHADER_CONST(ptr)
#elif RENDERER_DIRECTX_DRIVER
#include <renderer/internal/directx/directx_shader.h>
typedef directx_shader_t shader_t;
typedef directx_shader_create_info_t shader_create_info_t;
typedef directx_shader_handle_t shader_handle_t;
#define SHADER_HANDLE_INVALID DIRECTX_SHADER_HANDLE_INVALID
#define SHADER(ptr) DIRECTX_SHADER(ptr)
#define SHADER_CONST(ptr) DIRECTX_SHADER_CONST(ptr)
#elif RENDERER_METAL_DRIVER
#include <renderer/internal/metal/metal_shader.h>
typedef metal_shader_t shader_t;
typedef metal_shader_create_info_t shader_create_info_t;
typedef metal_shader_handle_t shader_handle_t;
#define SHADER_HANDLE_INVALID METAL_SHADER_HANDLE_INVALID
#define SHADER(ptr) METAL_SHADER(ptr)
#define SHADER_CONST(ptr) METAL_SHADER_CONST(ptr)
#endif

BEGIN_CPP_COMPATIBLE
Expand Down
4 changes: 2 additions & 2 deletions source/renderer/shader_library.c
Original file line number Diff line number Diff line change
Expand Up @@ -711,10 +711,10 @@ RENDERER_API const char* shader_library_get_nameH(shader_library_t* library, sha

RENDERER_API shader_t* shader_library_getH(shader_library_t* library, shader_handle_t handle)
{
return CAST_TO(shader_t*, vulkan_shader_library_getH(library, handle));
return SHADER(vulkan_shader_library_getH(library, handle));
}

RENDERER_API shader_t* shader_library_get(shader_library_t* library, const char* shader_name)
{
return CAST_TO(shader_t*, vulkan_shader_library_get(library, shader_name));
return SHADER(vulkan_shader_library_get(library, shader_name));
}
25 changes: 18 additions & 7 deletions source/renderer/vulkan/vulkan_mesh.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ RENDERER_API void vulkan_mesh_create_no_alloc(vulkan_renderer_t* renderer, vulka
};
mesh->index_buffer.buffer = vulkan_buffer_create(renderer, &buffer_create_info);
mesh->index_buffer.index_type = create_info->index_buffer_info.index_type;
mesh->index_buffer.is_internal = true;
}
else
{
Expand All @@ -102,13 +103,17 @@ RENDERER_API void vulkan_mesh_destroy(vulkan_mesh_t* mesh)
for(u32 i = 0; i < vertex_buffer_count; i++)
{
AUTO vertex_buffer = CAST_TO(vulkan_vertex_buffer_t*, buf_get_ptr_at(&mesh->vertex_buffers, i));
vulkan_buffer_destroy(vertex_buffer->buffer);
vulkan_buffer_release_resources(vertex_buffer->buffer);
/* only destroy and release resources if the vulkan_buffer_t has been allocated internally (i.e. by vulkan_mesh_t) */
if(vertex_buffer->is_internal)
{
vulkan_buffer_destroy(vertex_buffer->buffer);
vulkan_buffer_release_resources(vertex_buffer->buffer);
}
}
buf_clear(&mesh->vertex_buffers, NULL);

/* destroy the index buffers */
if(mesh->index_buffer.buffer != NULL)
/* destroy the index buffer if it non null and created internally */
if((mesh->index_buffer.buffer != NULL) && mesh->index_buffer.is_internal)
vulkan_buffer_destroy(mesh->index_buffer.buffer);

mesh->binding_index = 0;
Expand Down Expand Up @@ -199,21 +204,27 @@ UNUSED_FUNCTION static void print_binding(void* ptr, void* user_data)
printf("%u ", CAST_TO(vulkan_vertex_buffer_t*, ptr)->binding);
}

RENDERER_API void vulkan_mesh_add_vertex_buffer(vulkan_mesh_t* mesh, vulkan_buffer_t* buffer, u32 binding)
static void _vulkan_mesh_add_vertex_buffer(vulkan_mesh_t* mesh, vulkan_buffer_t* buffer, u32 binding, bool is_internal)
{
_debug_assert__(buffer != NULL);
vulkan_vertex_buffer_t _buffer = { .buffer = buffer, .binding = binding };
vulkan_vertex_buffer_t _buffer = { .buffer = buffer, .binding = binding, .is_internal = is_internal };
/* push and sort the vertex buffers in increasing order of binding */
buf_push_sort(&mesh->vertex_buffers, &_buffer, binding_less_than, NULL);
// printf("Bindings: ");
// buf_traverse_elements(&mesh->vertex_buffers, 0, buf_get_element_count(&mesh->vertex_buffers) - 1, print_binding, NULL);
// puts("");
}

RENDERER_API void vulkan_mesh_add_vertex_buffer(vulkan_mesh_t* mesh, vulkan_buffer_t* buffer, u32 binding)
{
_vulkan_mesh_add_vertex_buffer(mesh, buffer, binding, false);
}

RENDERER_API void vulkan_mesh_set_index_buffer(vulkan_mesh_t* mesh, vulkan_buffer_t* buffer, VkIndexType vo_type)
{
mesh->index_buffer.buffer = buffer;
mesh->index_buffer.index_type = vo_type;
mesh->index_buffer.is_internal = false;
}

RENDERER_API void vulkan_mesh_create_and_add_vertex_buffer(vulkan_mesh_t* mesh, vulkan_vertex_buffer_create_info_t* create_info)
Expand All @@ -232,7 +243,7 @@ RENDERER_API void vulkan_mesh_create_and_add_vertex_buffer(vulkan_mesh_t* mesh,
.vo_memory_property_flags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
};

vulkan_mesh_add_vertex_buffer(mesh, vulkan_buffer_create(mesh->renderer, &buffer_create_info), create_info->binding);
_vulkan_mesh_add_vertex_buffer(mesh, vulkan_buffer_create(mesh->renderer, &buffer_create_info), create_info->binding, true);
}

static u32 get_index_stride(VkIndexType index_type)
Expand Down

0 comments on commit 76149bb

Please sign in to comment.