summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-06-17 22:17:48 +1000
committeromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-06-18 10:30:28 +1000
commit4b5695ce26a8f821dfac987e8e11a6ba5eeff610 (patch)
tree0cd8f80bdb0520ffcf0cd62851f72055d4ee5546 /src
parentf1cb5bece520dba63fa77e4721909ab8f0a6c26f (diff)
start working on a immediate mode drawing API
Diffstat (limited to 'src')
-rw-r--r--src/maths/maths.h3
-rw-r--r--src/renderer/backends/opengl/backend_opengl.c7
-rw-r--r--src/renderer/builtin_materials.h53
-rw-r--r--src/renderer/immediate.c50
-rw-r--r--src/renderer/immediate.h21
-rw-r--r--src/renderer/render_types.h6
-rw-r--r--src/resources/gltf.c33
-rw-r--r--src/transform_hierarchy.c1
8 files changed, 127 insertions, 47 deletions
diff --git a/src/maths/maths.h b/src/maths/maths.h
index fa58088..7c0e06e 100644
--- a/src/maths/maths.h
+++ b/src/maths/maths.h
@@ -314,7 +314,8 @@ static inline mat4 transform_to_mat(transform *tf) {
mat4 scale = mat4_scale(tf->scale);
mat4 rotation = mat4_rotation(tf->rotation);
mat4 translation = mat4_translation(tf->position);
- return mat4_mult(mat4_mult(scale, rotation), translation);
+ return mat4_mult(translation, mat4_mult(rotation, scale));
+ // return mat4_mult(mat4_mult(scale, rotation), translation);
}
// --- Sizing asserts
diff --git a/src/renderer/backends/opengl/backend_opengl.c b/src/renderer/backends/opengl/backend_opengl.c
index d9859b2..18cd7ee 100644
--- a/src/renderer/backends/opengl/backend_opengl.c
+++ b/src/renderer/backends/opengl/backend_opengl.c
@@ -207,16 +207,11 @@ void encode_bind_shader_data(gpu_cmd_encoder* encoder, u32 group, shader_data* d
glBindBuffer(GL_UNIFORM_BUFFER, ubo_buf->id.ubo);
glBindBufferBase(GL_UNIFORM_BUFFER, ubo_buf->ubo_binding_point, ubo_buf->id.ubo);
- if (i == 2) {
- // pbr_params_light_uniforms* u = binding.data.bytes.data;
- // vec4* v = &u->viewPos;
- }
glBufferSubData(GL_UNIFORM_BUFFER, 0, ubo_buf->size, binding.data.bytes.data);
} else if (binding.type == SHADER_BINDING_TEXTURE) {
gpu_texture* tex = TEXTURE_GET(binding.data.texture.handle);
- glActiveTexture(GL_TEXTURE0);
- // glActiveTexture(GL_TEXTURE0 + i);
+ glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, tex->id);
}
}
diff --git a/src/renderer/builtin_materials.h b/src/renderer/builtin_materials.h
index 7748599..1a04f32 100644
--- a/src/renderer/builtin_materials.h
+++ b/src/renderer/builtin_materials.h
@@ -102,3 +102,56 @@ static void* shader_layout_get_binding(shader_data_layout* layout, u32 nth_bindi
assert(nth_binding < layout->bindings_count);
return &layout->bindings[nth_binding].data;
}
+
+typedef struct pbr_textures {
+ texture_handle albedo_tex;
+ texture_handle metal_roughness_tex;
+ texture_handle ao_tex;
+ texture_handle normal_tex;
+} pbr_textures;
+
+typedef struct pbr_textured_bindgroup {
+ mvp_matrix_uniforms mvp_matrices;
+ pbr_params_light_uniforms lights;
+ pbr_textures textures;
+} pbr_textured_bindgroup;
+
+static shader_data_layout pbr_textured_shader_layout(void* data) {
+ pbr_textured_bindgroup* d = (pbr_textured_bindgroup*)data;
+ bool has_data = data != NULL;
+
+ shader_binding b1 = { .label = "MVP_Matrices",
+ .type = SHADER_BINDING_BYTES,
+ .stores_data = has_data,
+ .data = { .bytes = { .size = sizeof(mvp_matrix_uniforms) } } };
+
+ shader_binding b2 = { .label = "Scene_Lights",
+ .type = SHADER_BINDING_BYTES,
+ .stores_data = has_data,
+ .data = { .bytes = { .size = sizeof(pbr_params_light_uniforms) } } };
+
+ shader_binding b3 = {.label = "Albedo",
+ .type = SHADER_BINDING_TEXTURE,
+ .stores_data = has_data };
+ shader_binding b4 = {.label = "Metallic Roughness",
+ .type = SHADER_BINDING_TEXTURE,
+ .stores_data = has_data };
+ shader_binding b5 = {.label = "Ambient Occlusion",
+ .type = SHADER_BINDING_TEXTURE,
+ .stores_data = has_data };
+ shader_binding b6 = {.label = "Normal Vectors",
+ .type = SHADER_BINDING_TEXTURE,
+ .stores_data = has_data };
+
+ if (has_data) {
+ b1.data.bytes.data = &d->mvp_matrices;
+ b2.data.bytes.data = &d->lights;
+ b3.data.texture.handle = d->textures.albedo_tex;
+ b4.data.texture.handle = d->textures.metal_roughness_tex;
+ b5.data.texture.handle = d->textures.ao_tex;
+ b6.data.texture.handle = d->textures.normal_tex;
+ }
+
+ return (shader_data_layout){ .name = "pbr_params", .bindings = { b1, b2, b3, b4, b5, b6 }, .bindings_count = 6
+ };
+} \ No newline at end of file
diff --git a/src/renderer/immediate.c b/src/renderer/immediate.c
index e76d102..b3431fa 100644
--- a/src/renderer/immediate.c
+++ b/src/renderer/immediate.c
@@ -1,18 +1,50 @@
#include "immediate.h"
+#include "glad/glad.h"
#include "maths.h"
#include "primitives.h"
#include "ral_types.h"
#include "render.h"
+#include "render_types.h"
-void imm_draw_sphere(vec3 pos, f32 radius, vec4 colour) {
- // Create the vertices
- geometry_data geometry = geo_create_uvsphere(radius, 16, 16);
- geo_set_vertex_colours(&geometry, colour);
+typedef struct immdraw_system {
+ // primitive meshes (get reused for each draw call)
+ mesh plane;
+ mesh cube;
+ mesh sphere;
+ // command lists
+
+} immdraw_system;
- // Upload to GPU
- mat4 model = mat4_translation(pos);
+bool immdraw_system_init(immdraw_system* state) {
+ geometry_data plane_geometry = geo_create_plane(f32x2(1, 1));
+ state->plane = mesh_create(&plane_geometry, true);
- // Set pipeline
+ geometry_data cube_geometry = geo_create_cuboid(f32x3(1, 1, 1));
+ state->cube = mesh_create(&cube_geometry, true);
- // Draw
-} \ No newline at end of file
+ geometry_data sphere_geometry = geo_create_uvsphere(1.0, 48, 48);
+ state->sphere = mesh_create(&sphere_geometry, true);
+
+ return true;
+}
+
+void immdraw_plane(vec3 pos, quat rotation, f32 u_scale, f32 v_scale, vec4 colour) {
+
+}
+
+void immdraw_system_render(immdraw_system* state) {
+
+}
+
+// void imm_draw_sphere(vec3 pos, f32 radius, vec4 colour) {
+// // Create the vertices
+// geometry_data geometry = geo_create_uvsphere(radius, 16, 16);
+// geo_set_vertex_colours(&geometry, colour);
+
+// // Upload to GPU
+// mat4 model = mat4_translation(pos);
+
+// // Set pipeline
+
+// // Draw
+// } \ No newline at end of file
diff --git a/src/renderer/immediate.h b/src/renderer/immediate.h
index b9d7c61..f4b1729 100644
--- a/src/renderer/immediate.h
+++ b/src/renderer/immediate.h
@@ -3,18 +3,17 @@
#include "geometry.h"
#include "maths_types.h"
+typedef struct immdraw_system immdraw_system;
+
+bool immdraw_system_init(immdraw_system* state);
+void immdraw_system_shutdown(immdraw_system* state);
+void immdraw_system_render(immdraw_system* state);
+
// 3. SIMA (simplified immediate mode api) / render.h
// - dont need to worry about uploading mesh data
// - very useful for debugging
-void imm_draw_cuboid(vec3 pos, quat rotation, f32x3 extents, vec4 colour);
-void imm_draw_sphere(vec3 pos, f32 radius, vec4 colour);
-void imm_draw_camera_frustum();
-
-// static void imm_draw_model(
-// const char* model_filepath); // tracks internally whether the model is loaded
+void immdraw_plane(vec3 pos, quat rotation, f32 u_scale, f32 v_scale, vec4 colour);
+void immdraw_cuboid(vec3 pos, quat rotation, f32x3 extents, vec4 colour);
+void immdraw_sphere(vec3 pos, f32 radius, vec4 colour);
-// static void imm_draw_model(const char* model_filepath) {
-// check that model is loaded
-// if not loaded, load model and upload to gpu - LRU cache for models
-// else submit draw call
-// } \ No newline at end of file
+void immdraw_camera_frustum();
diff --git a/src/renderer/render_types.h b/src/renderer/render_types.h
index 8588b87..b25fa14 100644
--- a/src/renderer/render_types.h
+++ b/src/renderer/render_types.h
@@ -35,11 +35,17 @@ typedef struct geometry_data {
rgba colour; /** Optional: set vertex colours */
} geometry_data;
+typedef struct u32_opt {
+ u32 value;
+ bool has_value;
+} u32_opt;
+
// 'Upload' a geometry_data (to GPU) -> get back a mesh
typedef struct mesh {
buffer_handle vertex_buffer;
buffer_handle index_buffer;
geometry_data* geometry; // NULL means it has been freed
+ u32_opt material_index;
bool is_uploaded;
bool is_latent;
} mesh;
diff --git a/src/resources/gltf.c b/src/resources/gltf.c
index 46ad4f3..f136595 100644
--- a/src/resources/gltf.c
+++ b/src/resources/gltf.c
@@ -192,6 +192,9 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel
material_darray_push(out_model->materials, our_material);
}
+ // TEMP
+ u32 mat_idx = 9999;
+
// --- Meshes
size_t num_meshes = data->meshes_count;
TRACE("Num meshes %d", num_meshes);
@@ -288,14 +291,15 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel
if (primitive.material != NULL) {
ERROR("Primitive Material %s", primitive.material->name);
- // for (int i = 0; i < material_darray_len(out_model->materials); i++) {
- // printf("%s vs %s \n", primitive.material->name, out_model->materials->data[i].name);
- // if (strcmp(primitive.material->name, out_model->materials->data[i].name) == 0) {
- // TRACE("Found material");
- // mesh.material_index = i;
- // break;
- // }
- // }
+ for (u32 i = 0; i < material_darray_len(out_model->materials); i++) {
+ printf("%s vs %s \n", primitive.material->name, out_model->materials->data[i].name);
+ if (strcmp(primitive.material->name, out_model->materials->data[i].name) == 0) {
+ INFO("Found material");
+ mat_idx = i;
+ // mesh.material_index = i;
+ break;
+ }
+ }
}
// // FIXME
@@ -317,17 +321,6 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel
// // }
// // }
- /*
- typedef struct mesh {
- buffer_handle vertex_buffer;
- buffer_handle index_buffer;
- u32 index_count;
- bool has_indices;
- geometry_data* vertices; // NULL means it has been freed
- bool is_uploaded;
- bool is_latent;
- } mesh;
- */
bool has_indices = false;
vertex_darray *geo_vertices = vertex_darray_new(3);
u32_darray *geo_indices = u32_darray_new(0);
@@ -384,7 +377,9 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel
geometry->vertices = geo_vertices;
geometry->indices = geo_indices;
geometry->has_indices = has_indices;
+
mesh m = mesh_create(geometry, true);
+ m.material_index = (u32_opt){.has_value = mat_idx == 9999, .value = mat_idx };
mesh_darray_push(out_model->meshes, m);
}
diff --git a/src/transform_hierarchy.c b/src/transform_hierarchy.c
index 2c427b2..b9795bc 100644
--- a/src/transform_hierarchy.c
+++ b/src/transform_hierarchy.c
@@ -11,7 +11,6 @@
#include "log.h"
#include "maths.h"
#include "maths_types.h"
-// #include "render_types.h"
struct transform_hierarchy {
transform_node root;