diff options
-rw-r--r-- | assets/shaders/blinn_phong.frag | 2 | ||||
-rw-r--r-- | assets/shaders/blinn_phong.vert | 27 | ||||
-rw-r--r-- | assets/shaders/cube.vert | 3 | ||||
-rw-r--r-- | src/renderer/archive/render_backend.h | 2 | ||||
-rw-r--r-- | src/renderer/archive/render_types.h | 8 | ||||
-rw-r--r-- | src/renderer/backends/backend_vulkan.c | 12 | ||||
-rw-r--r-- | src/renderer/backends/opengl/backend_opengl.c | 142 | ||||
-rw-r--r-- | src/renderer/backends/opengl/backend_opengl.h | 11 | ||||
-rw-r--r-- | src/renderer/ral.c | 23 | ||||
-rw-r--r-- | src/renderer/ral.h | 2 |
10 files changed, 187 insertions, 45 deletions
diff --git a/assets/shaders/blinn_phong.frag b/assets/shaders/blinn_phong.frag index a0ba905..f7c4322 100644 --- a/assets/shaders/blinn_phong.frag +++ b/assets/shaders/blinn_phong.frag @@ -32,7 +32,7 @@ in VS_OUT { vec3 FragPos; vec3 Normal; vec2 TexCoords; - vec4 FragPosLightSpace; + // vec4 FragPosLightSpace; vec4 Color; } fs_in; diff --git a/assets/shaders/blinn_phong.vert b/assets/shaders/blinn_phong.vert index 06dc5e7..aefb129 100644 --- a/assets/shaders/blinn_phong.vert +++ b/assets/shaders/blinn_phong.vert @@ -1,28 +1,37 @@ -#version 410 core +#version 450 core + +struct Uniforms { + mat4 model; + mat4 view; + mat4 projection; +}; + +uniform Uniforms ubo; + // Inputs layout (location = 0) in vec3 inPos; layout (location = 1) in vec3 inNormal; layout (location = 2) in vec2 inTexCoords; -uniform mat4 model; -uniform mat4 view; -uniform mat4 projection; -uniform mat4 lightSpaceMatrix; +// uniform mat4 model; +// uniform mat4 view; +// uniform mat4 projection; +// uniform mat4 lightSpaceMatrix; // Output out VS_OUT { vec3 FragPos; vec3 Normal; vec2 TexCoords; - vec4 FragPosLightSpace; + // vec4 FragPosLightSpace; vec4 Color; } vs_out; void main() { - vs_out.FragPos = vec3(model * vec4(inPos, 1.0)); + vs_out.FragPos = vec3(ubo.model * vec4(inPos, 1.0)); vs_out.Normal = inNormal; vs_out.TexCoords = inTexCoords; - vs_out.FragPosLightSpace = lightSpaceMatrix * vec4(vs_out.FragPos, 1.0); + // vs_out.FragPosLightSpace = lightSpaceMatrix * vec4(vs_out.FragPos, 1.0); vs_out.Color = vec4(1.0); - gl_Position = projection * view * model * vec4(inPos, 1.0); + gl_Position = ubo.projection * ubo.view * ubo.model * vec4(inPos, 1.0); }
\ No newline at end of file diff --git a/assets/shaders/cube.vert b/assets/shaders/cube.vert index dc625ec..fa9f85b 100644 --- a/assets/shaders/cube.vert +++ b/assets/shaders/cube.vert @@ -4,8 +4,7 @@ layout(binding = 0) uniform UniformBufferObject { mat4 model; mat4 view; mat4 proj; -} -ubo; +} ubo; layout(location = 0) in vec3 inPosition; layout(location = 1) in vec3 inNormal; diff --git a/src/renderer/archive/render_backend.h b/src/renderer/archive/render_backend.h index da30bcc..6df9c81 100644 --- a/src/renderer/archive/render_backend.h +++ b/src/renderer/archive/render_backend.h @@ -22,7 +22,7 @@ void clear_screen(vec3 colour); void texture_data_upload(texture* tex); void bind_texture(shader s, texture* tex, u32 slot); void bind_mesh_vertex_buffer(void* backend, mesh* mesh); -void draw_primitives(cel_primitive_topology primitive, u32 start_index, u32 count); +// void draw_primitives(cel_primitive_topology primitive, u32 start_index, u32 count); shader shader_create_separate(const char* vert_shader, const char* frag_shader); void set_shader(shader s); diff --git a/src/renderer/archive/render_types.h b/src/renderer/archive/render_types.h index f5ea986..5ee3316 100644 --- a/src/renderer/archive/render_types.h +++ b/src/renderer/archive/render_types.h @@ -147,10 +147,10 @@ typedef struct mesh { u32 vbo, vao; /** OpenGL data. TODO: dont leak OpenGL details */ } mesh; -// #ifndef TYPED_MESH_ARRAY -// KITC_DECL_TYPED_ARRAY(mesh) // creates "mesh_darray" -// #define TYPED_MESH_ARRAY -// #endif +#ifndef TYPED_MESH_ARRAY +KITC_DECL_TYPED_ARRAY(mesh) // creates "mesh_darray" +#define TYPED_MESH_ARRAY +#endif typedef struct model { str8 name; diff --git a/src/renderer/backends/backend_vulkan.c b/src/renderer/backends/backend_vulkan.c index c3fad75..25a78e7 100644 --- a/src/renderer/backends/backend_vulkan.c +++ b/src/renderer/backends/backend_vulkan.c @@ -1699,16 +1699,6 @@ void resource_pools_init(arena* a, struct resource_pools* res_pools) { context.resource_pools = res_pools; } -void backend_pools_init(arena* a, gpu_backend_pools* backend_pools) { - pipeline_layout_pool pipeline_layout_pool = - pipeline_layout_pool_create(a, MAX_PIPELINES, sizeof(gpu_pipeline_layout)); - backend_pools->pipeline_layouts = pipeline_layout_pool; - pipeline_pool pipeline_pool = pipeline_pool_create(a, MAX_PIPELINES, sizeof(gpu_pipeline)); - backend_pools->pipelines = pipeline_pool; - renderpass_pool rpass_pool = renderpass_pool_create(a, MAX_RENDERPASSES, sizeof(gpu_renderpass)); - backend_pools->renderpasses = rpass_pool; - - context.gpu_pools; -} + #endif
\ No newline at end of file diff --git a/src/renderer/backends/opengl/backend_opengl.c b/src/renderer/backends/opengl/backend_opengl.c index cbc90f2..a933462 100644 --- a/src/renderer/backends/opengl/backend_opengl.c +++ b/src/renderer/backends/opengl/backend_opengl.c @@ -1,4 +1,5 @@ #include "colours.h" +#include "ral_types.h" #define CEL_REND_BACKEND_OPENGL #if defined(CEL_REND_BACKEND_OPENGL) #include <stdlib.h> @@ -16,6 +17,8 @@ typedef struct opengl_context { GLFWwindow* window; + arena pool_arena; + gpu_backend_pools gpu_pools; } opengl_context; static opengl_context context; @@ -28,6 +31,11 @@ bool gpu_backend_init(const char* window_name, struct GLFWwindow* window) { memset(&context, 0, sizeof(opengl_context)); context.window = window; + size_t pool_buffer_size = 1024 * 1024; + context.pool_arena = arena_create(malloc(pool_buffer_size), pool_buffer_size); + + backend_pools_init(&context.pool_arena, &context.gpu_pools); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); @@ -51,7 +59,25 @@ bool gpu_device_create(gpu_device* out_device) {} void gpu_device_destroy() {} // --- Render Pipeline -gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc description) {} +gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc description) { + gpu_pipeline* pipeline = pipeline_pool_alloc(&context.gpu_pools.pipelines, NULL); + + // Create shader program + u32 shader_id = shader_create_separate(description.vs.filepath.buf, description.fs.filepath.buf); + pipeline->shader_id = shader_id; + + // Allocate uniform buffers if needed + printf("data layouts %d\n", description.data_layouts_count); + for (u32 layout_i = 0; layout_i < description.data_layouts_count; layout_i++) { + shader_data_layout sdl = description.data_layouts[layout_i].shader_data_get_layout(NULL); + TRACE("Got shader data layout %d's bindings! . found %d", layout_i, sdl.bindings_count); + + for (u32 binding_j = 0; binding_j < sdl.bindings_count; binding_j++) { + } + } + + return pipeline; +} void gpu_pipeline_destroy(gpu_pipeline* pipeline) {} // --- Renderpass @@ -97,8 +123,19 @@ void copy_buffer_to_buffer_oneshot(buffer_handle src, u64 src_offset, buffer_han void copy_buffer_to_image_oneshot(buffer_handle src, texture_handle dst) {} // --- Render commands -void encode_bind_pipeline(gpu_cmd_encoder* encoder, pipeline_kind kind, gpu_pipeline* pipeline) {} -void encode_bind_shader_data(gpu_cmd_encoder* encoder, u32 group, shader_data* data) {} +void encode_bind_pipeline(gpu_cmd_encoder* encoder, pipeline_kind kind, gpu_pipeline* pipeline) { + // In OpenGL this is more or less equivalent to just setting the shader + glUseProgram(pipeline->shader_id); +} +void encode_bind_shader_data(gpu_cmd_encoder* encoder, u32 group, shader_data* data) { + shader_data_layout sdl = data->shader_data_get_layout(data->data); + size_t binding_count = sdl.bindings_count; + + for (u32 i = 0; i < sdl.bindings_count; i++) { + shader_binding binding = sdl.bindings[i]; + + } +} void encode_set_default_settings(gpu_cmd_encoder* encoder) {} void encode_set_vertex_buffer(gpu_cmd_encoder* encoder, buffer_handle buf) {} void encode_set_index_buffer(gpu_cmd_encoder* encoder, buffer_handle buf) {} @@ -108,7 +145,28 @@ void encode_clear_buffer(gpu_cmd_encoder* encoder, buffer_handle buf) {} // --- Buffers buffer_handle gpu_buffer_create(u64 size, gpu_buffer_type buf_type, gpu_buffer_flags flags, - const void* data) {} + const void* data) { + GLuint gl_buffer_id; + glGenBuffers(1, &gl_buffer_id); + + switch (buf_type) { + case CEL_BUFFER_UNIFORM: + glBindBuffer(GL_UNIFORM_BUFFER, gl_buffer_id); + case CEL_BUFFER_DEFAULT: + case CEL_BUFFER_VERTEX: + case CEL_BUFFER_INDEX: + WARN("Unimplemented gpu_buffer_type!"); + break; + case CEL_BUFFER_COUNT: + WARN("Incorrect gpu_buffer_type provided."); + break; + } + + buffer_handle handle; + gpu_buffer* buffer = buffer_pool_alloc(&context.resource_pools, &handle); + +} + void gpu_buffer_destroy(buffer_handle buffer) {} void gpu_buffer_upload(const void* data) {} @@ -131,6 +189,58 @@ void gpu_backend_end_frame() { } void gpu_temp_draw(size_t n_verts) {} +u32 shader_create_separate(const char *vert_shader, const char *frag_shader){ + INFO("Load shaders at %s and %s", vert_shader, frag_shader); + int success; + char info_log[512]; + + u32 vertex = glCreateShader(GL_VERTEX_SHADER); + const char *vertex_shader_src = string_from_file(vert_shader); + if (vertex_shader_src == NULL) { + ERROR("EXIT: couldnt load shader"); + exit(-1); + } + glShaderSource(vertex, 1, &vertex_shader_src, NULL); + glCompileShader(vertex); + glGetShaderiv(vertex, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(vertex, 512, NULL, info_log); + printf("%s\n", info_log); + ERROR("EXIT: vertex shader compilation failed"); + exit(-1); + } + + // fragment shader + u32 fragment = glCreateShader(GL_FRAGMENT_SHADER); + const char *fragment_shader_src = string_from_file(frag_shader); + if (fragment_shader_src == NULL) { + ERROR("EXIT: couldnt load shader"); + exit(-1); + } + glShaderSource(fragment, 1, &fragment_shader_src, NULL); + glCompileShader(fragment); + glGetShaderiv(fragment, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(fragment, 512, NULL, info_log); + printf("%s\n", info_log); + ERROR("EXIT: fragment shader compilation failed"); + exit(-1); + } + + u32 shader_prog; + shader_prog = glCreateProgram(); + + glAttachShader(shader_prog, vertex); + glAttachShader(shader_prog, fragment); + glLinkProgram(shader_prog); + glDeleteShader(vertex); + glDeleteShader(fragment); + free((char *)vertex_shader_src); + free((char *)fragment_shader_src); + + return shader_prog; +} + // /** @brief Internal backend state */ // typedef struct opengl_state { // } opengl_state; @@ -163,18 +273,18 @@ void gpu_temp_draw(size_t n_verts) {} // void gfx_backend_shutdown(renderer *ren) {} -// void uniform_vec3f(u32 program_id, const char *uniform_name, vec3 *value) { -// glUniform3fv(glGetUniformLocation(program_id, uniform_name), 1, &value->x); -// } -// void uniform_f32(u32 program_id, const char *uniform_name, f32 value) { -// glUniform1f(glGetUniformLocation(program_id, uniform_name), value); -// } -// void uniform_i32(u32 program_id, const char *uniform_name, i32 value) { -// glUniform1i(glGetUniformLocation(program_id, uniform_name), value); -// } -// void uniform_mat4f(u32 program_id, const char *uniform_name, mat4 *value) { -// glUniformMatrix4fv(glGetUniformLocation(program_id, uniform_name), 1, GL_FALSE, value->data); -// } +void uniform_vec3f(u32 program_id, const char *uniform_name, vec3 *value) { + glUniform3fv(glGetUniformLocation(program_id, uniform_name), 1, &value->x); +} +void uniform_f32(u32 program_id, const char *uniform_name, f32 value) { + glUniform1f(glGetUniformLocation(program_id, uniform_name), value); +} +void uniform_i32(u32 program_id, const char *uniform_name, i32 value) { + glUniform1i(glGetUniformLocation(program_id, uniform_name), value); +} +void uniform_mat4f(u32 program_id, const char *uniform_name, mat4 *value) { + glUniformMatrix4fv(glGetUniformLocation(program_id, uniform_name), 1, GL_FALSE, value->data); +} // void clear_screen(vec3 colour) { // glClearColor(colour.x, colour.y, colour.z, 1.0f); diff --git a/src/renderer/backends/opengl/backend_opengl.h b/src/renderer/backends/opengl/backend_opengl.h index 649bc7b..784c061 100644 --- a/src/renderer/backends/opengl/backend_opengl.h +++ b/src/renderer/backends/opengl/backend_opengl.h @@ -10,7 +10,9 @@ typedef struct gpu_swapchain { } gpu_swapchain; typedef struct gpu_device {} gpu_device; typedef struct gpu_pipeline_layout {} gpu_pipeline_layout; -typedef struct gpu_pipeline {} gpu_pipeline; +typedef struct gpu_pipeline { + u32 shader_id; +} gpu_pipeline; typedef struct gpu_renderpass {} gpu_renderpass; typedef struct gpu_cmd_encoder {} gpu_cmd_encoder; // Recording typedef struct gpu_cmd_buffer {} gpu_cmd_buffer; // Ready for submission @@ -23,4 +25,11 @@ typedef struct gpu_buffer { } gpu_buffer; typedef struct gpu_texture {} gpu_texture; + +u32 shader_create_separate(const char *vert_shader, const char *frag_shader); + +void uniform_vec3f(u32 program_id, const char *uniform_name, vec3 *value); +void uniform_f32(u32 program_id, const char *uniform_name, f32 value); +void uniform_i32(u32 program_id, const char *uniform_name, i32 value); +void uniform_mat4f(u32 program_id, const char *uniform_name, mat4 *value); #endif
\ No newline at end of file diff --git a/src/renderer/ral.c b/src/renderer/ral.c index 7d868be..4f2a999 100644 --- a/src/renderer/ral.c +++ b/src/renderer/ral.c @@ -1,5 +1,7 @@ #include "ral.h" +#include "backend_opengl.h" + size_t vertex_attrib_size(vertex_attrib_type attr) { switch (attr) { case ATTR_F32: @@ -40,3 +42,24 @@ vertex_description static_3d_vertex_description() { vertex_desc_add(&builder, "texCoords", ATTR_F32x2); return builder; } + +void backend_pools_init(arena* a, gpu_backend_pools* backend_pools) { + pipeline_layout_pool pipeline_layout_pool = + pipeline_layout_pool_create(a, MAX_PIPELINES, sizeof(gpu_pipeline_layout)); + backend_pools->pipeline_layouts = pipeline_layout_pool; + pipeline_pool pipeline_pool = pipeline_pool_create(a, MAX_PIPELINES, sizeof(gpu_pipeline)); + backend_pools->pipelines = pipeline_pool; + renderpass_pool rpass_pool = renderpass_pool_create(a, MAX_RENDERPASSES, sizeof(gpu_renderpass)); + backend_pools->renderpasses = rpass_pool; + + // context.gpu_pools; +} + +void resource_pools_init(arena* a, struct resource_pools* res_pools) { + buffer_pool buf_pool = buffer_pool_create(a, MAX_BUFFERS, sizeof(gpu_buffer)); + res_pools->buffers = buf_pool; + texture_pool tex_pool = texture_pool_create(a, MAX_TEXTURES, sizeof(gpu_texture)); + res_pools->textures = tex_pool; + + // context.resource_pools = res_pools; +}
\ No newline at end of file diff --git a/src/renderer/ral.h b/src/renderer/ral.h index 1ca37b4..a18ea87 100644 --- a/src/renderer/ral.h +++ b/src/renderer/ral.h @@ -50,11 +50,13 @@ typedef struct gpu_backend_pools { pipeline_layout_pool pipeline_layouts; renderpass_pool renderpasses; } gpu_backend_pools; +void backend_pools_init(arena* a, gpu_backend_pools* backend_pools); struct resource_pools { buffer_pool buffers; texture_pool textures; }; +void resource_pools_init(arena* a, struct resource_pools* res_pools); // --- Pipeline description typedef enum pipeline_kind { |