Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Texture mapping #11

Merged
merged 7 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dependencies/MeshLib
Binary file modified external-dependency-libs/vulkan-1.lib
Binary file not shown.
8 changes: 8 additions & 0 deletions include/renderer/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
# define ASSERT(boolean, ...)
#endif

#if defined(GLOBAL_DEBUG)
# define assert(condition) ASSERT((condition) != false, "\"%s\" is found to be false\n", #condition)
# define ASSERT_NOT_NULL(ptr) assert(ptr != NULL)
#else
# define assert(condition)
# define ASSERT_NOT_NULL(ptr)
#endif

#if defined(GLOBAL_DEBUG)
# define LOG_MSG(...) log_msg(__VA_ARGS__)
# define LOG_ERR(...) log_err(__VA_ARGS__)
Expand Down
20 changes: 20 additions & 0 deletions include/renderer/bmp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

#pragma once

#include <renderer/defines.h>
#include <calltrace/calltrace.h>

typedef struct bmp_t
{
void* data;
u32 width;
u32 height;
u8 channel_count;
} bmp_t;


#define bmp_load(...) define_alias_function_macro(bmp_load, __VA_ARGS__)
#define bmp_destroy(...) define_alias_function_macro(bmp_destroy, __VA_ARGS__)

function_signature(bmp_t, bmp_load, const char* file_path);
function_signature(void, bmp_destroy, bmp_t bmp);
6 changes: 5 additions & 1 deletion include/renderer/debug.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#pragma once

#include <calltrace/calltrace.h>
#include <calltrace/calltrace.h>

#define log_u32(var) log_msg("%s: %u\n", #var, var)
#define log_ptr(var) log_msg("%s: %p\n", #var, var)

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ typedef struct vulkan_graphics_pipeline_create_info_t
typedef struct vulkan_graphics_pipeline_t
{
VkPipeline pipeline;
vulkan_pipeline_layout_t* layout;
vulkan_pipeline_layout_t* pipeline_layout;
} vulkan_graphics_pipeline_t;

vulkan_graphics_pipeline_t* vulkan_graphics_pipeline_new();
Expand Down
8 changes: 7 additions & 1 deletion include/renderer/internal/vulkan/vulkan_material.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
typedef struct renderer_t renderer_t;
typedef struct vulkan_graphics_pipeline_t vulkan_graphics_pipeline_t;
typedef struct vulkan_shader_t vulkan_shader_t;
typedef struct vulkan_texture_t vulkan_texture_t;

typedef struct vulkan_material_create_info_t
{
Expand All @@ -23,8 +24,12 @@ typedef struct vulkan_material_t
renderer_t* renderer;
void* self_reference; //self_reference

//For recreating the graphics pipeline on render window resize
vulkan_material_create_info_t create_info;
vulkan_graphics_pipeline_t* pipeline;

uint32_t descriptor_set_count;
VkDescriptorSet* descriptor_sets;
vulkan_graphics_pipeline_t* graphics_pipeline;
} vulkan_material_t;

vulkan_material_t* vulkan_material_new();
Expand All @@ -34,3 +39,4 @@ void vulkan_material_destroy(vulkan_material_t* material, renderer_t* renderer);
void vulkan_material_release_resources(vulkan_material_t* material);
void vulkan_material_bind(vulkan_material_t* material, renderer_t* renderer);
void vulkan_material_push_constants(vulkan_material_t* material, renderer_t* renderer, void* bytes);
void vulkan_material_set_texture(vulkan_material_t* material, renderer_t* renderer, vulkan_texture_t* texture);
11 changes: 11 additions & 0 deletions include/renderer/internal/vulkan/vulkan_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ typedef struct vulkan_mesh_create_info_t
void* p_position_data;
void* p_color_data;
void* p_normal_data;
void* p_uv_data;
void* p_index_data;

uint32_t position_stride;
uint32_t color_stride;
uint32_t normal_stride;
uint32_t uv_stride;
uint32_t index_stride;

uint32_t vertex_count;
uint32_t index_count;
} vulkan_mesh_create_info_t;
Expand All @@ -27,15 +31,21 @@ typedef struct vulkan_mesh_t
VkBuffer position_buffer;
VkBuffer color_buffer;
VkBuffer normal_buffer;
VkBuffer uv_buffer;
VkBuffer index_buffer;

VkDeviceMemory position_buffer_memory;
VkDeviceMemory color_buffer_memory;
VkDeviceMemory normal_buffer_memory;
VkDeviceMemory uv_buffer_memory;
VkDeviceMemory index_buffer_memory;

uint32_t position_stride;
uint32_t color_stride;
uint32_t normal_stride;
uint32_t uv_stride;
uint32_t index_stride;

uint32_t vertex_count;
uint32_t index_count;
} vulkan_mesh_t;
Expand All @@ -49,3 +59,4 @@ void vulkan_mesh_sync(vulkan_mesh_t* mesh, renderer_t* renderer, vulkan_mesh_cre
void vulkan_mesh_release_resources(vulkan_mesh_t* mesh);
void vulkan_mesh_draw_indexed(vulkan_mesh_t* mesh, renderer_t* renderer);
void vulkan_mesh_draw(vulkan_mesh_t* mesh, renderer_t* renderer);

14 changes: 6 additions & 8 deletions include/renderer/internal/vulkan/vulkan_pipeline_layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ typedef struct renderer_t renderer_t;

typedef struct vulkan_pipeline_layout_t
{
VkPipelineLayout layout;
/*
Descriptor sets etc.
*/
VkPipelineLayout pipeline_layout;
VkDescriptorSetLayout descriptor_set_layout;
} vulkan_pipeline_layout_t;


vulkan_pipeline_layout_t* vulkan_pipeline_layout_new();
vulkan_pipeline_layout_t* vulkan_pipeline_layout_create(renderer_t* renderer);
void vulkan_pipeline_layout_create_no_alloc(renderer_t* renderer, vulkan_pipeline_layout_t* layout);
void vulkan_pipeline_layout_destroy(vulkan_pipeline_layout_t* layout, renderer_t* renderer);
void vulkan_pipeline_layout_release_resources(vulkan_pipeline_layout_t* layout);
void vulkan_pipeline_layout_create_no_alloc(renderer_t* renderer, vulkan_pipeline_layout_t* pipeline_layout);
void vulkan_pipeline_layout_destroy(vulkan_pipeline_layout_t* pipeline_layout, renderer_t* renderer);
void vulkan_pipeline_layout_release_resources(vulkan_pipeline_layout_t* pipeline_layout);

void vulkan_pipeline_layout_push_constants(vulkan_pipeline_layout_t* layout, renderer_t* renderer, void* bytes);
void vulkan_pipeline_layout_push_constants(vulkan_pipeline_layout_t* pipeline_layout, renderer_t* renderer, void* bytes);
1 change: 1 addition & 0 deletions include/renderer/internal/vulkan/vulkan_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ typedef struct renderer_t
VkQueue vk_graphics_queue;
VkRenderPass vk_render_pass;
VkCommandPool vk_command_pool;
VkDescriptorPool vk_descriptor_pool;

render_window_t* window;
vulkan_swapchain_t* swapchain;
Expand Down
7 changes: 5 additions & 2 deletions include/renderer/internal/vulkan/vulkan_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ typedef struct vulkan_shader_t


vulkan_shader_t* vulkan_shader_new();
vulkan_shader_t* vulkan_shader_create(renderer_t* renderer, const char* file_name, vulkan_shader_type_t vulkan_shader_type);
void vulkan_shader_create_no_alloc(renderer_t* renderer, const char* file_name, vulkan_shader_type_t vulkan_shader_type, vulkan_shader_t* shader);
vulkan_shader_t* vulkan_shader_create(renderer_t* renderer, void* spirv, uint32_t length, vulkan_shader_type_t vulkan_shader_type);
void vulkan_shader_create_no_alloc(renderer_t* renderer, void* spirv, uint32_t length, vulkan_shader_type_t vulkan_shader_type, vulkan_shader_t* shader);
vulkan_shader_t* vulkan_shader_load_and_create(renderer_t* renderer, const char* file_name, vulkan_shader_type_t vulkan_shader_type);
void vulkan_shader_load_and_create_no_alloc(renderer_t* renderer, const char* file_name, vulkan_shader_type_t vulkan_shader_type, vulkan_shader_t* shader);

void vulkan_shader_destroy(vulkan_shader_t* shader, renderer_t* renderer);
void vulkan_shader_release_resources(vulkan_shader_t* shader);
27 changes: 27 additions & 0 deletions include/renderer/internal/vulkan/vulkan_texture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

#pragma once

#include <vulkan/vulkan_wrapper.h>
#include <renderer/defines.h>

typedef struct renderer_t renderer_t;

typedef struct vulkan_texture_create_info_t
{
void* data;
u32 width;
u32 height;
u8 channel_count;
} vulkan_texture_create_info_t;

typedef struct vulkan_texture_t
{
VkImage image;
VkImageView image_view;
VkSampler image_sampler;
VkDeviceMemory image_memory;
} vulkan_texture_t;

vulkan_texture_t* vulkan_texture_create(renderer_t* renderer, vulkan_texture_create_info_t* create_info);
void vulkan_texture_destroy(vulkan_texture_t* texture, renderer_t* renderer);
void vulkan_texture_release_resources(vulkan_texture_t* texture);
9 changes: 6 additions & 3 deletions include/renderer/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ typedef struct renderer_t renderer_t;
#ifdef RENDERER_VULKAN_DRIVER
typedef struct vulkan_material_t vulkan_material_t;
typedef struct vulkan_shader_t vulkan_shader_t;
typedef vulkan_shader_t shader_t;
typedef struct vulkan_texture_t vulkan_texture_t;
typedef vulkan_texture_t texture_t;
typedef vulkan_shader_t stage_shader_t;
typedef vulkan_material_t material_t;
#endif

Expand All @@ -23,10 +25,11 @@ typedef vulkan_material_t material_t;
#endif

material_t* material_new();
material_t* material_create(renderer_t* renderer, u32 shader_count, shader_t** shaders);
void material_create_no_alloc(renderer_t* renderer, u32 shader_count, shader_t** shaders, material_t* material);
material_t* material_create(renderer_t* renderer, u32 shader_count, stage_shader_t** shaders);
void material_create_no_alloc(renderer_t* renderer, u32 shader_count, stage_shader_t** shaders, material_t* material);
void material_destroy(material_t* material, renderer_t* renderer);
void material_release_resources(material_t* material);

void material_bind(material_t* material, renderer_t* renderer);
void material_push_constants(material_t* material, renderer_t* renderer, void* bytes);
void material_set_texture(material_t* material, renderer_t* renderer, texture_t* texture);
2 changes: 2 additions & 0 deletions include/renderer/mesh3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ function_signature(float, mesh3d_color_get_z, mesh3d_t* mesh, index_t index);

function_signature(mesh3d_t*, mesh3d_cube, float size);
#define mesh3d_cube(...) define_alias_function_macro(mesh3d_cube, __VA_ARGS__)
function_signature(mesh3d_t*, mesh3d_plane, float size);
#define mesh3d_plane(...) define_alias_function_macro(mesh3d_plane, __VA_ARGS__)

function_signature(index_t, mesh3d_sizeof_position, mesh3d_t* mesh);
function_signature(index_t, mesh3d_sizeof_normal, mesh3d_t* mesh);
Expand Down
16 changes: 16 additions & 0 deletions include/renderer/png.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#pragma once

#include <renderer/defines.h>
#include <calltrace/calltrace.h>

typedef struct png_t
{
void* data;
u32 width;
u32 height;
} png_t;

#define png_load(...) define_alias_function_macro(png_load, __VA_ARGS__)
function_signature(png_t, png_load, const char* file_path);
void png_destroy(png_t png);
4 changes: 4 additions & 0 deletions include/renderer/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <renderer/defines.h>

typedef struct render_window_t render_window_t;
typedef struct renderer_t renderer_t;

renderer_t* renderer_init(u32 width, u32 height, const char* title, bool full_screen);
Expand All @@ -12,3 +13,6 @@ bool renderer_is_running(renderer_t* renderer);

void renderer_begin_frame(renderer_t* renderer, float r, float g, float b, float a);
void renderer_end_frame(renderer_t* renderer);


render_window_t* renderer_get_window(renderer_t* renderer);
40 changes: 10 additions & 30 deletions include/renderer/shader.h
Original file line number Diff line number Diff line change
@@ -1,37 +1,17 @@

#pragma once

typedef struct renderer_t renderer_t;
#include <renderer/stage_shader.h>
#include <renderer/defines.h>

#ifdef RENDERER_VULKAN_DRIVER

typedef struct vulkan_shader_t vulkan_shader_t;
typedef vulkan_shader_t shader_t;

#endif

#ifdef RENDERER_OPENGL_DRIVER
#error "OpenGL is not supported yet!"
#endif

#ifdef RENDERER_DIRECTX_DRIVER
#error "DirectX is not supported yet!"
#endif

#ifdef RENDERER_METAL_DRIVER
#error "Metal is not supported yet!"
#endif

typedef enum shader_type_t
typedef struct shader_t
{
SHADER_TYPE_VERTEX,
SHADER_TYPE_FRAGMENT,
SHADER_TYPE_GEOMETRY,
SHADER_TYPE_TESSELLATION
} shader_type_t;
stage_shader_t** stage_shaders;
u8 stage_count;
} shader_t;


shader_t* shader_new();
shader_t* shader_create(renderer_t*, const char* file_name, shader_type_t shader_type);
void shader_create_no_alloc(renderer_t*, const char* file_name, shader_type_t shader_type, shader_t* shader);
void shader_destroy(shader_t* shader, renderer_t*);
shader_t* shader_create(renderer_t* renderer, stage_shader_t** stage_shaders, u8 stage_count);
shader_t* shader_load(renderer_t* renderer, const char* file_path);
void shader_destroy(shader_t* shader, renderer_t* renderer);
void shader_release_resources(shader_t* shader);
42 changes: 42 additions & 0 deletions include/renderer/stage_shader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

#pragma once

typedef struct renderer_t renderer_t;

#ifdef RENDERER_VULKAN_DRIVER

typedef struct vulkan_shader_t vulkan_shader_t;
typedef vulkan_shader_t stage_shader_t;

#endif

#ifdef RENDERER_OPENGL_DRIVER
#error "OpenGL is not supported yet!"
#endif

#ifdef RENDERER_DIRECTX_DRIVER
#error "DirectX is not supported yet!"
#endif

#ifdef RENDERER_METAL_DRIVER
#error "Metal is not supported yet!"
#endif

#include <renderer/defines.h>

/*This enumeration should be in sync with vulkan_shader_type_t*/
typedef enum shader_stage_t
{
SHADER_STAGE_VERTEX,
SHADER_STAGE_FRAGMENT,
SHADER_STAGE_GEOMETRY,
SHADER_STAGE_TESSELLATION
} shader_stage_t;

stage_shader_t* stage_shader_new();
stage_shader_t* stage_shader_create(renderer_t*, void* spirv, u32 length, shader_stage_t shader_stage);
void stage_shader_create_no_alloc(renderer_t*, void* spirv, u32 length, shader_stage_t shader_stage, stage_shader_t* shader);
stage_shader_t* stage_shader_load_and_create(renderer_t*, const char* file_name, shader_stage_t shader_stage);
void stage_shader_load_and_create_no_alloc(renderer_t*, const char* file_name, shader_stage_t shader_stage, stage_shader_t* shader);
void stage_shader_destroy(stage_shader_t* shader, renderer_t*);
void stage_shader_release_resources(stage_shader_t* shader);
36 changes: 36 additions & 0 deletions include/renderer/texture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

#pragma once

typedef struct renderer_t renderer_t;

#ifdef RENDERER_VULKAN_DRIVER
typedef struct vulkan_texture_t vulkan_texture_t;
typedef vulkan_texture_t texture_t;
#endif

#ifdef RENDERER_OPENGL_DRIVER
#error "OpenGL is not supported yet!"
#endif

#ifdef RENDERER_DIRECTX_DRIVER
#error "DirectX is not supported yet!"
#endif

#ifdef RENDERER_METAL_DRIVER
#error "Metal is not supported yet!"
#endif

#include <renderer/defines.h>

typedef struct texture_create_info_t
{
void* data;
u32 width;
u32 height;
u8 channel_count;
} texture_create_info_t;

texture_t* texture_create(renderer_t* renderer, texture_create_info_t* create_info);
texture_t* texture_load(renderer_t* renderer, const char* file_path);
void texture_destroy(texture_t* texture, renderer_t* renderer);
void texture_release_resources(texture_t* texture);
Loading