summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--assets/shaders/cube.frag2
-rw-r--r--assets/shaders/cube.vert8
-rw-r--r--examples/cube/ex_cube.c16
-rw-r--r--src/maths/maths.h1
-rw-r--r--src/renderer/backends/opengl/backend_opengl.c39
-rw-r--r--src/renderer/backends/opengl/opengl_helpers.h2
-rw-r--r--src/renderer/render.c1
7 files changed, 49 insertions, 20 deletions
diff --git a/assets/shaders/cube.frag b/assets/shaders/cube.frag
index 796f1c8..9d8ab28 100644
--- a/assets/shaders/cube.frag
+++ b/assets/shaders/cube.frag
@@ -9,5 +9,5 @@ layout(location = 0) out vec4 outColor;
void main() {
// outColor = texture(texSampler, fragTexCoord); // vec4(fragTexCoord, 0.0);
- outColor = vec4(1.0);
+ outColor = vec4(fragColor, 1.0);
}
diff --git a/assets/shaders/cube.vert b/assets/shaders/cube.vert
index 57475e7..e7d6a79 100644
--- a/assets/shaders/cube.vert
+++ b/assets/shaders/cube.vert
@@ -1,10 +1,13 @@
#version 430
-layout(binding = 0) uniform Matrices {
+layout( binding = 0) uniform Matrices {
mat4 model;
mat4 view;
mat4 proj;
} ubo;
+uniform mat4 model;
+uniform mat4 view;
+uniform mat4 projection;
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inNormal;
@@ -14,7 +17,8 @@ layout(location = 0) out vec3 fragColor;
layout(location = 1) out vec2 fragTexCoord;
void main() {
- gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0);
+ // gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0);
+ gl_Position = projection * view * model * vec4(inPosition, 1.0);
// gl_Position = vec4(inPosition, 1.0);
fragColor = abs(inNormal);
fragTexCoord = inTexCoords;
diff --git a/examples/cube/ex_cube.c b/examples/cube/ex_cube.c
index a3d5a9f..e85b5c1 100644
--- a/examples/cube/ex_cube.c
+++ b/examples/cube/ex_cube.c
@@ -62,6 +62,7 @@ int main() {
vertex_desc_add(&vertex_input, "inPosition", ATTR_F32x3);
vertex_desc_add(&vertex_input, "inNormal", ATTR_F32x3);
vertex_desc_add(&vertex_input, "inTexCoords", ATTR_F32x2);
+ vertex_input.use_full_vertex_size = true;
shader_data mvp_uniforms_data = { .data = NULL, .shader_data_get_layout = &mvp_uniforms_layout };
@@ -129,14 +130,23 @@ int main() {
transform transform = { .position = vec3(-0.5, -0.5, -0.5),
.rotation = quat_from_axis_angle(VEC3_Y, theta, true),
.scale = 1.0 };
+ /* INFO("Swapchain dimensions x %d y %d", g_core.renderer.swapchain.dimensions.x,
+ * g_core.renderer.swapchain.dimensions.y); */
+
mat4 model = transform_to_mat(&transform);
mat4 view, proj;
- camera_view_projection(&cam, g_core.renderer.swapchain.dimensions.x,
- g_core.renderer.swapchain.dimensions.y, &view, &proj);
+ camera_view_projection(&cam, 1000, 1000,
+ /* g_core.renderer.swapchain.dimensions.x, */
+ /* g_core.renderer.swapchain.dimensions.y, */
+ &view, &proj);
mvp_uniforms mvp_data = { .model = model, .view = view, .projection = proj };
my_shader_bind_group shader_bind_data = { .mvp = mvp_data, .tex = texture };
mvp_uniforms_data.data = &shader_bind_data;
- encode_bind_shader_data(enc, 0, &mvp_uniforms_data);
+ /* encode_bind_shader_data(enc, 0, &mvp_uniforms_data); */
+
+ uniform_mat4f(enc->pipeline->shader_id, "model", &model);
+ uniform_mat4f(enc->pipeline->shader_id, "view", &view);
+ uniform_mat4f(enc->pipeline->shader_id, "projection", &proj);
// Record draw calls
draw_mesh(&cube, &model);
diff --git a/src/maths/maths.h b/src/maths/maths.h
index 217f2e0..45d69c1 100644
--- a/src/maths/maths.h
+++ b/src/maths/maths.h
@@ -10,6 +10,7 @@
#include <math.h>
#include <stdio.h>
+#include "defines.h"
#include "maths_types.h"
// --- Helpers
diff --git a/src/renderer/backends/opengl/backend_opengl.c b/src/renderer/backends/opengl/backend_opengl.c
index 073f343..17587a2 100644
--- a/src/renderer/backends/opengl/backend_opengl.c
+++ b/src/renderer/backends/opengl/backend_opengl.c
@@ -1,12 +1,11 @@
#include <stddef.h>
+#include <stdio.h>
#include "colours.h"
#include "opengl_helpers.h"
#include "ral_types.h"
-#define CEL_REND_BACKEND_OPENGL 1
#if defined(CEL_REND_BACKEND_OPENGL)
#include <assert.h>
#include <stdlib.h>
-#include "camera.h"
#include "backend_opengl.h"
#include "defines.h"
@@ -55,6 +54,7 @@ bool gpu_backend_init(const char* window_name, struct GLFWwindow* window) {
}
glEnable(GL_DEPTH_TEST);
+ // glFrontFace(GL_CW);
return true;
}
@@ -62,7 +62,7 @@ bool gpu_backend_init(const char* window_name, struct GLFWwindow* window) {
void gpu_backend_shutdown() {}
bool gpu_device_create(gpu_device* out_device) { /* No-op in OpenGL */ }
-void gpu_device_destroy() {}
+void gpu_device_destroy() { /* No-op in OpenGL */ }
// --- Render Pipeline
gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc description) {
@@ -91,8 +91,20 @@ gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc descrip
NULL); // no data right now
pipeline->uniform_bindings[binding_id] = ubo_handle;
gpu_buffer* ubo_buf = BUFFER_GET(ubo_handle);
+
+ u32 blockIndex = glGetUniformBlockIndex(pipeline->shader_id, "Matrices");
+ printf("Block index for Matrices: %d", blockIndex);
+ u32 blocksize;
+ glGetActiveUniformBlockiv(pipeline->shader_id, blockIndex, GL_UNIFORM_BLOCK_DATA_SIZE,
+ &blocksize);
+ printf("\t with size %d bytes\n", blocksize);
+
glBindBuffer(GL_UNIFORM_BUFFER, ubo_buf->id.ubo);
glBindBufferBase(GL_UNIFORM_BUFFER, binding_j, ubo_buf->id.ubo);
+ if (blockIndex != GL_INVALID_INDEX) {
+ printf("Here\n");
+ glUniformBlockBinding(pipeline->shader_id, blockIndex, 0);
+ }
// Now we want to store a handle associated with the shader for this
}
@@ -151,7 +163,7 @@ 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) {
encoder->pipeline = pipeline;
- // In OpenGL this is more or less equivalent to just setting the shader
+ // In OpenGL binding a pipeline 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) {
@@ -159,14 +171,16 @@ void encode_bind_shader_data(gpu_cmd_encoder* encoder, u32 group, shader_data* d
for (u32 i = 0; i < sdl.bindings_count; i++) {
shader_binding binding = sdl.bindings[i];
- print_shader_binding(binding);
+ /* print_shader_binding(binding); */
if (binding.type == SHADER_BINDING_BYTES) {
buffer_handle b = encoder->pipeline->uniform_bindings[i];
gpu_buffer* ubo_buf = BUFFER_GET(b);
glBindBuffer(GL_UNIFORM_BUFFER, ubo_buf->id.ubo);
glBufferSubData(GL_UNIFORM_BUFFER, 0, ubo_buf->size, data->data);
- glBindBuffer(GL_UNIFORM_BUFFER, 0);
+ /* printf("Size %d\n", ubo_buf->size); */
+
+ /* glBindBuffer(GL_UNIFORM_BUFFER, 0); */
}
}
}
@@ -174,6 +188,7 @@ void encode_set_default_settings(gpu_cmd_encoder* encoder) {}
void encode_set_vertex_buffer(gpu_cmd_encoder* encoder, buffer_handle buf) {
gpu_buffer* buffer = BUFFER_GET(buf);
if (buffer->vao == 0) { // if no VAO for this vertex buffer, create it
+ INFO("Setting up VAO");
buffer->vao = opengl_bindcreate_vao(buffer, encoder->pipeline->vertex_desc);
}
glBindVertexArray(buffer->vao);
@@ -208,7 +223,7 @@ buffer_handle gpu_buffer_create(u64 size, gpu_buffer_type buf_type, gpu_buffer_f
case CEL_BUFFER_UNIFORM:
DEBUG("Creating Uniform buffer");
gl_buf_type = GL_UNIFORM_BUFFER;
- gl_buf_usage = GL_DYNAMIC_DRAW;
+ /* gl_buf_usage = GL_DYNAMIC_DRAW; */
buffer->id.ubo = gl_buffer_id;
break;
case CEL_BUFFER_DEFAULT:
@@ -254,7 +269,10 @@ bytebuffer vertices_as_bytebuffer(arena* a, vertex_format format, vertex_darray*
// --- TEMP
bool gpu_backend_begin_frame() { return true; }
-void gpu_backend_end_frame() { glfwSwapBuffers(context.window); }
+void gpu_backend_end_frame() {
+ // TODO: Reset all bindings
+ glfwSwapBuffers(context.window);
+}
void gpu_temp_draw(size_t n_verts) {}
u32 shader_create_separate(const char* vert_shader, const char* frag_shader) {
@@ -370,9 +388,4 @@ inline void uniform_mat4f(u32 program_id, const char* uniform_name, mat4* value)
// break;
// }
// }
-
-// void draw_primitives(cel_primitive_topology primitive, u32 start_index, u32 count) {
-// u32 gl_primitive = to_gl_prim_topology(primitive);
-// glDrawArrays(gl_primitive, start_index, count);
-// }
#endif
diff --git a/src/renderer/backends/opengl/opengl_helpers.h b/src/renderer/backends/opengl/opengl_helpers.h
index 405a8f1..a3c4014 100644
--- a/src/renderer/backends/opengl/opengl_helpers.h
+++ b/src/renderer/backends/opengl/opengl_helpers.h
@@ -54,7 +54,7 @@ static u32 opengl_bindcreate_vao(gpu_buffer* buf, vertex_description desc) {
u32 attr_count = desc.attributes_count;
printf("N attributes %d\n", attr_count);
u64 offset = 0;
- size_t vertex_size = desc.stride;
+ size_t vertex_size = desc.use_full_vertex_size ? sizeof(vertex) : desc.stride;
for (u32 i = 0; i < desc.attributes_count; i++) {
opengl_vertex_attr format = format_from_vertex_attr(desc.attributes[i]);
glVertexAttribPointer(i, format.count, format.data_type, GL_FALSE, vertex_size, (void*)offset);
diff --git a/src/renderer/render.c b/src/renderer/render.c
index 5723c9e..7833ac9 100644
--- a/src/renderer/render.c
+++ b/src/renderer/render.c
@@ -161,6 +161,7 @@ void draw_mesh(mesh* mesh, mat4* model) { // , mat4* view, mat4* proj) {
}
// Assume this has already been done
/* encode_bind_shader_data(enc, 0, &mvp_uniforms_data); */
+
encode_draw_indexed(enc, mesh->index_count);
}