diff options
author | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-06-08 14:42:04 +1000 |
---|---|---|
committer | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-06-08 14:42:04 +1000 |
commit | 19a5fae08d7f1f85cb5448a5f2b19f0f9d342a0e (patch) | |
tree | 3958b9cd80999a46f711506c25fb759e02595922 | |
parent | 037cb840dfb90264cd1bef36736cefb3cf7f2dd9 (diff) |
some more opengl work
-rw-r--r-- | examples/triangle/ex_triangle.c | 10 | ||||
-rw-r--r-- | src/renderer/backends/opengl/backend_opengl.c | 25 | ||||
-rw-r--r-- | src/renderer/backends/opengl/backend_opengl.h | 2 | ||||
-rw-r--r-- | src/renderer/backends/opengl/opengl_helpers.h | 35 |
4 files changed, 68 insertions, 4 deletions
diff --git a/examples/triangle/ex_triangle.c b/examples/triangle/ex_triangle.c index 5b6fbea..0191c18 100644 --- a/examples/triangle/ex_triangle.c +++ b/examples/triangle/ex_triangle.c @@ -35,8 +35,14 @@ int main() { gpu_renderpass_desc pass_description = {}; gpu_renderpass* renderpass = gpu_renderpass_create(&pass_description); - str8 vert_path = str8lit("build/linux/x86_64/debug/triangle.vert.spv"); - str8 frag_path = str8lit("build/linux/x86_64/debug/triangle.frag.spv"); + str8 vert_path, frag_path; +#ifdef CEL_REND_BACKEND_OPENGL + vert_path = str8lit("assets/shaders/triangle.vert"); + frag_path = str8lit("assets/shaders/triangle.frag"); +#else + vert_path = str8lit("build/linux/x86_64/debug/triangle.vert.spv"); + frag_path = str8lit("build/linux/x86_64/debug/triangle.frag.spv"); +#endif str8_opt vertex_shader = str8_from_file(&scratch, vert_path); str8_opt fragment_shader = str8_from_file(&scratch, frag_path); if (!vertex_shader.has_value || !fragment_shader.has_value) { diff --git a/src/renderer/backends/opengl/backend_opengl.c b/src/renderer/backends/opengl/backend_opengl.c index 529a0d9..8be1c2a 100644 --- a/src/renderer/backends/opengl/backend_opengl.c +++ b/src/renderer/backends/opengl/backend_opengl.c @@ -1,4 +1,6 @@ +#include <stddef.h> #include "colours.h" +#include "opengl_helpers.h" #include "ral_types.h" #define CEL_REND_BACKEND_OPENGL #if defined(CEL_REND_BACKEND_OPENGL) @@ -27,6 +29,8 @@ static opengl_context context; struct GLFWwindow; +size_t vertex_attrib_size(vertex_attrib_type attr); + bool gpu_backend_init(const char* window_name, struct GLFWwindow* window) { INFO("loading OpenGL backend"); @@ -69,6 +73,24 @@ gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc descrip u32 shader_id = shader_create_separate(description.vs.filepath.buf, description.fs.filepath.buf); pipeline->shader_id = shader_id; + // Vertex + u32 vao; + glGenVertexArrays(1, &vao); + pipeline->vao = vao; + + // Attributes + u32 attr_count = description.vertex_desc.attributes_count; + printf("N attributes %d\n", attr_count); + u64 offset = 0; + size_t vertex_size = description.vertex_desc.stride; + for (u32 i = 0; i < description.vertex_desc.attributes_count; i++) { + opengl_vertex_attr format = format_from_vertex_attr(description.vertex_desc.attributes[i]); + glVertexAttribPointer(i, format.count, format.data_type, GL_FALSE, vertex_size, (void*)offset); + size_t this_offset = format.count * vertex_attrib_size(description.vertex_desc.attributes[i]); + printf("offset total %lld this attr %ld\n", offset, this_offset); + offset += this_offset; + } + // 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++) { @@ -129,6 +151,7 @@ void copy_buffer_to_image_oneshot(buffer_handle src, texture_handle dst) {} 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); + glBindVertexArray(pipeline->vao); } 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); @@ -177,7 +200,7 @@ buffer_handle gpu_buffer_create(u64 size, gpu_buffer_type buf_type, gpu_buffer_f buffer->size = size; if (data) { - TRACE("Upload data as part of buffer creation"); + TRACE("Upload data (%d bytes) as part of buffer creation", size); glBufferData(gl_buf_type, buffer->size, data, GL_STATIC_DRAW); } diff --git a/src/renderer/backends/opengl/backend_opengl.h b/src/renderer/backends/opengl/backend_opengl.h index 876cfe4..a9835c1 100644 --- a/src/renderer/backends/opengl/backend_opengl.h +++ b/src/renderer/backends/opengl/backend_opengl.h @@ -15,7 +15,7 @@ typedef struct gpu_pipeline_layout { } gpu_pipeline_layout; typedef struct gpu_pipeline { u32 shader_id; - u32 pad; + u32 vao; } gpu_pipeline; typedef struct gpu_renderpass { void *pad diff --git a/src/renderer/backends/opengl/opengl_helpers.h b/src/renderer/backends/opengl/opengl_helpers.h index 6f70f09..2f6bef3 100644 --- a/src/renderer/backends/opengl/opengl_helpers.h +++ b/src/renderer/backends/opengl/opengl_helpers.h @@ -1 +1,36 @@ #pragma once +#include "ral_types.h" +#include <glad/glad.h> +#include <glfw3.h> +typedef struct opengl_vertex_attr { + u32 count; + GLenum data_type; +} opengl_vertex_attr ; +opengl_vertex_attr format_from_vertex_attr(vertex_attrib_type attr) { + switch (attr) { + case ATTR_F32: + return (opengl_vertex_attr){.count = 1, .data_type = GL_FLOAT }; + case ATTR_U32: + return (opengl_vertex_attr){.count = 1, .data_type = GL_UNSIGNED_INT }; + case ATTR_I32: + return (opengl_vertex_attr){.count = 1, .data_type = GL_INT }; + case ATTR_F32x2: + return (opengl_vertex_attr){.count = 2, .data_type = GL_FLOAT }; + case ATTR_U32x2: + // return VK_FORMAT_R32G32_UINT; + case ATTR_I32x2: + // return VK_FORMAT_R32G32_UINT; + case ATTR_F32x3: + return (opengl_vertex_attr){.count = 3, .data_type = GL_FLOAT }; + case ATTR_U32x3: + // return VK_FORMAT_R32G32B32_UINT; + case ATTR_I32x3: + // return VK_FORMAT_R32G32B32_SINT; + case ATTR_F32x4: + return (opengl_vertex_attr){.count = 4, .data_type = GL_FLOAT }; + case ATTR_U32x4: + // return VK_FORMAT_R32G32B32A32_UINT; + case ATTR_I32x4: + // return VK_FORMAT_R32G32B32A32_SINT; + } +}
\ No newline at end of file |