summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-06-08 14:42:04 +1000
committeromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-06-08 14:42:04 +1000
commit19a5fae08d7f1f85cb5448a5f2b19f0f9d342a0e (patch)
tree3958b9cd80999a46f711506c25fb759e02595922 /src
parent037cb840dfb90264cd1bef36736cefb3cf7f2dd9 (diff)
some more opengl work
Diffstat (limited to 'src')
-rw-r--r--src/renderer/backends/opengl/backend_opengl.c25
-rw-r--r--src/renderer/backends/opengl/backend_opengl.h2
-rw-r--r--src/renderer/backends/opengl/opengl_helpers.h35
3 files changed, 60 insertions, 2 deletions
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