summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOmniscient <omniscient.oce@gmail.com>2024-05-17 08:39:51 +1000
committerOmniscient <omniscient.oce@gmail.com>2024-05-17 08:39:51 +1000
commite61a2e43947cebaafe4c3725414d33e092bb6fad (patch)
tree84f2e52a1eb62ec6b1957628307f59ad39a621af
parent634f22e2b6d538fc5a45da2b1b23af631f6f8703 (diff)
cube working
-rw-r--r--assets/shaders/cube.vert9
-rw-r--r--assets/shaders/triangle.frag4
-rw-r--r--examples/README.md11
-rw-r--r--examples/cube/ex_cube.c58
-rw-r--r--src/maths/primitives.c253
-rw-r--r--src/renderer/backends/backend_vulkan.c103
-rw-r--r--src/renderer/ral_types.h3
-rw-r--r--src/renderer/render.c43
-rw-r--r--src/renderer/render.h21
-rw-r--r--src/renderer/render_types.h3
10 files changed, 302 insertions, 206 deletions
diff --git a/assets/shaders/cube.vert b/assets/shaders/cube.vert
index 80ad854..1818c3c 100644
--- a/assets/shaders/cube.vert
+++ b/assets/shaders/cube.vert
@@ -7,12 +7,13 @@ layout(binding = 0) uniform UniformBufferObject {
}
ubo;
-layout(location = 0) in vec2 inPosition;
-layout(location = 1) in vec3 inColor;
+layout(location = 0) in vec3 inPosition;
+layout(location = 1) in vec3 inNormal;
+layout(location = 2) in vec3 inTexCoords;
layout(location = 0) out vec3 fragColor;
void main() {
- gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 0.0, 1.0);
- fragColor = inColor;
+ gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0);
+ fragColor = abs(inNormal);
}
diff --git a/assets/shaders/triangle.frag b/assets/shaders/triangle.frag
index 2151162..a8c6e69 100644
--- a/assets/shaders/triangle.frag
+++ b/assets/shaders/triangle.frag
@@ -3,4 +3,6 @@
layout(location = 0) in vec3 fragColor;
layout(location = 0) out vec4 outColor;
-void main() { outColor = vec4(fragColor, 1.0); } \ No newline at end of file
+void main() {
+ outColor = vec4(fragColor, 0.0);
+}
diff --git a/examples/README.md b/examples/README.md
new file mode 100644
index 0000000..f289e80
--- /dev/null
+++ b/examples/README.md
@@ -0,0 +1,11 @@
+
+### RAL examples
+
+These examples show off how the lowest register of rendering, the Rendering Abstraction Layer (RAL), is used to draw things to the screen.
+
+- triangle
+- cube
+
+### Renderer
+
+The 'renderer' is the default renderer provided by Celeritas.
diff --git a/examples/cube/ex_cube.c b/examples/cube/ex_cube.c
index a115fe3..7c7831d 100644
--- a/examples/cube/ex_cube.c
+++ b/examples/cube/ex_cube.c
@@ -8,20 +8,14 @@
#include "maths.h"
#include "maths_types.h"
#include "mem.h"
+#include "primitives.h"
#include "ral.h"
#include "ral_types.h"
#include "render.h"
+#include "render_types.h"
extern core g_core;
-const custom_vertex vertices[] = {
- (custom_vertex){ .pos = vec2(-0.5, 0.5), .color = vec3(0.0, 0.0, 1.0) },
- (custom_vertex){ .pos = vec2(0.5, 0.5), .color = vec3(0.0, 1.0, 0.0) },
- (custom_vertex){ .pos = vec2(0.5, -0.5), .color = vec3(1.0, 0.0, 0.0) },
- (custom_vertex){ .pos = vec2(-0.5, -0.5), .color = vec3(1.0, 1.0, 1.0) },
-};
-const u16 indices[] = { 0, 1, 2, 2, 3, 0 };
-
// Define the shader data
typedef struct mvp_uniforms {
mat4 model;
@@ -29,6 +23,7 @@ typedef struct mvp_uniforms {
mat4 projection;
} mvp_uniforms;
+// We also must create a function that knows how to return a `shader_data_layout`
shader_data_layout mvp_uniforms_layout(void* data) {
mvp_uniforms* d = (mvp_uniforms*)data;
bool has_data = data != NULL;
@@ -40,9 +35,6 @@ shader_data_layout mvp_uniforms_layout(void* data) {
if (has_data) {
b1.data.bytes.data = d;
}
- /* char* name; */
- /* shader_binding bindings[MAX_LAYOUT_BINDINGS]; */
- /* u32 bindings_count; */
return (shader_data_layout){ .name = "global_ubo", .bindings = { b1 }, .bindings_count = 1 };
}
@@ -52,9 +44,15 @@ int main() {
DEBUG("render capacity %d", g_core.default_scene.renderables->capacity);
- vertex_description vertex_input = {0};
- vertex_desc_add(&vertex_input, "inPosition", ATTR_F32x2);
- vertex_desc_add(&vertex_input, "inColor", ATTR_F32x3);
+ vec3 camera_pos = vec3(2., 2., 2.);
+ vec3 camera_front = vec3_normalise(vec3_negate(camera_pos));
+ camera cam = camera_create(camera_pos, camera_front, VEC3_Y, deg_to_rad(45.0));
+
+ vertex_description vertex_input;
+ vertex_input.debug_label = "Standard Static 3D Vertex Format";
+ vertex_desc_add(&vertex_input, "inPosition", ATTR_F32x3);
+ vertex_desc_add(&vertex_input, "inNormal", ATTR_F32x3);
+ vertex_desc_add(&vertex_input, "inTexCoords", ATTR_F32x2);
shader_data mvp_uniforms_data = { .data = NULL, .shader_data_get_layout = &mvp_uniforms_layout };
@@ -88,19 +86,13 @@ int main() {
};
gpu_pipeline* gfx_pipeline = gpu_graphics_pipeline_create(pipeline_description);
- buffer_handle triangle_vert_buf =
- gpu_buffer_create(sizeof(vertices), CEL_BUFFER_VERTEX, CEL_BUFFER_FLAG_GPU, vertices);
-
- buffer_handle triangle_index_buf =
- gpu_buffer_create(sizeof(indices), CEL_BUFFER_INDEX, CEL_BUFFER_FLAG_GPU, indices);
+ geometry_data cube_data = geo_create_cuboid(f32x3(1, 1, 1));
+ mesh cube = mesh_create(&cube_data, false);
// Main loop
while (!should_exit(&g_core)) {
- glfwPollEvents();
input_update(&g_core.input);
- // render_frame_begin(&g_core.renderer);
-
if (!gpu_backend_begin_frame()) {
continue;
}
@@ -111,20 +103,26 @@ int main() {
encode_bind_pipeline(enc, PIPELINE_GRAPHICS, gfx_pipeline);
encode_set_default_settings(enc);
- /* shader_data_layout mvp_layout = mvp_uniforms_layout(NULL); */
-
static f32 x = 0.0;
x += 0.01;
- quat rotation = quat_from_axis_angle(VEC3_Z, x, true);
+ quat rotation = quat_from_axis_angle(VEC3_Y, x, true);
+ mat4 translation = mat4_translation(vec3(-0.5, -0.5, -0.5));
mat4 model = mat4_rotation(rotation);
- mvp_uniforms mvp_data = { .model = model, .view = mat4_ident(), .projection = mat4_ident() };
+ model = mat4_mult(translation, model);
+ mat4 view, proj;
+ camera_view_projection(&cam, g_core.renderer.swapchain.extent.width,
+ g_core.renderer.swapchain.extent.height, &view, &proj);
+ mvp_uniforms mvp_data = { .model = model, .view = view, .projection = proj };
mvp_uniforms_data.data = &mvp_data;
+ encode_bind_shader_data(enc, 0, &mvp_uniforms_data);
// Record draw calls
- encode_set_vertex_buffer(enc, triangle_vert_buf);
- encode_set_index_buffer(enc, triangle_index_buf);
- encode_bind_shader_data(enc, 0, &mvp_uniforms_data);
- gpu_temp_draw(6);
+ // -- NEW
+ draw_mesh(&cube, &model);
+ // -- OLD
+ /* encode_set_vertex_buffer(enc, triangle_vert_buf); */
+ /* encode_set_index_buffer(enc, triangle_index_buf); */
+ /* gpu_temp_draw(6); */
// End recording
gpu_cmd_encoder_end_render(enc);
diff --git a/src/maths/primitives.c b/src/maths/primitives.c
index 459a535..b9ec868 100644
--- a/src/maths/primitives.c
+++ b/src/maths/primitives.c
@@ -1,6 +1,18 @@
#include "primitives.h"
+#include "colours.h"
#include "maths.h"
#include "ral_types.h"
+#include "render_types.h"
+
+// TODO: move to another file
+void geo_free_data(geometry_data* geo) {
+ vertex_darray_free(geo->vertices);
+ geo->vertices = NULL;
+ // TODO: do indices as well
+ /* if (geo->has_indices) { */
+ /* u32_darray_free(&geo->indices); */
+ /* } */
+}
// vertices
f32 plane_vertex_positions[] = {
@@ -31,169 +43,96 @@ static const vec3 FRONT_BOT_RIGHT = (vec3){ 1, 0, 1 };
static const vec3 FRONT_TOP_LEFT = (vec3){ 0, 1, 1 };
static const vec3 FRONT_TOP_RIGHT = (vec3){ 1, 1, 1 };
-#define VERT_3D(arr, pos, norm, uv) \
- { \
- vertex v = {
- .static_3d = { \
- .position = pos, \
- .normal = norm, \
- .tex_coords = uv \
- }}; \
- vertex_darray_push(arr, v); \
- }
-
-static mesh prim_cube_mesh_create() {
- mesh cube = { 0 };
- cube.vertices = vertex_darray_new(36);
-
- // back faces
- VERT_3D(cube.vertices, BACK_BOT_LEFT, VEC3_NEG_Z, (vec2){ 0, 1 }))
- // vertex_darray_push(
- // cube.vertices,
- // (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_Z, .uv = (vec2){ 0, 1 } });
- // vertex_darray_push(
- // cube.vertices,
- // (vertex){ .position = BACK_TOP_LEFT, .normal = VEC3_NEG_Z, .uv = (vec2){ 0, 0 } });
- // vertex_darray_push(
- // cube.vertices,
- // (vertex){ .position = BACK_TOP_RIGHT, .normal = VEC3_NEG_Z, .uv = (vec2){ 1, 0 } });
- // vertex_darray_push(
- // cube.vertices,
- // (vertex){ .position = BACK_TOP_RIGHT, .normal = VEC3_NEG_Z, .uv = (vec2){ 1, 0 } });
- // vertex_darray_push(
- // cube.vertices,
- // (vertex){ .position = BACK_BOT_RIGHT, .normal = VEC3_NEG_Z, .uv = (vec2){ 1, 1 } });
- // vertex_darray_push(
- // cube.vertices,
- // (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_Z, .uv = (vec2){ 0, 1 } });
-
- // front faces
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_BOT_LEFT, .normal = VEC3_Z, .uv = (vec2){ 0, 1 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_Z, .uv = (vec2){ 1, 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_TOP_LEFT, .normal = VEC3_Z, .uv = (vec2){ 0, 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_BOT_LEFT, .normal = VEC3_Z, .uv = (vec2){ 0, 1 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_BOT_RIGHT, .normal = VEC3_Z, .uv = (vec2){ 1, 1 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_Z, .uv = (vec2){ 1, 0 } });
-
- // top faces
- vertex_darray_push(cube.vertices,
- (vertex){ .position = BACK_TOP_LEFT, .normal = VEC3_Y, .uv = (vec2){ 0, 0
- } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_TOP_LEFT, .normal = VEC3_Y, .uv = (vec2){ 0, 1 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_Y, .uv = (vec2){ 1, 1 } });
- vertex_darray_push(cube.vertices,
- (vertex){ .position = BACK_TOP_LEFT, .normal = VEC3_Y, .uv = (vec2){ 0, 0
- } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_Y, .uv = (vec2){ 1, 1 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_TOP_RIGHT, .normal = VEC3_Y, .uv = (vec2){ 1, 0 } });
-
- // bottom faces
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_BOT_RIGHT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_BOT_LEFT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_BOT_RIGHT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_BOT_RIGHT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
-
- // right faces
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_X, .uv = (vec2){ 0, 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_BOT_RIGHT, .normal = VEC3_X, .uv = (vec2){ 1, 1 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_TOP_RIGHT, .normal = VEC3_X, .uv = (vec2){ 1, 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_BOT_RIGHT, .normal = VEC3_X, .uv = (vec2){ 1, 1 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_X, .uv = (vec2){ 0, 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_BOT_RIGHT, .normal = VEC3_X, .uv = (vec2){ 0, 1 } });
-
- // left faces
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_TOP_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_TOP_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_BOT_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_TOP_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
-
- cube.indices_len = cube.vertices->len;
- cube.indices = malloc(sizeof(u32) * cube.indices_len);
-
- for (u32 i = 0; i < cube.indices_len; i++) {
- cube.indices[i] = i;
- }
-
- cube.has_indices = true;
-
- return cube;
+#define VERT_3D(arr, pos, norm, uv) \
+ { \
+ vertex v = { .static_3d = { .position = pos, .normal = norm, .tex_coords = uv } }; \
+ vertex_darray_push(arr, v); \
+ }
+
+geometry_data geo_create_cuboid(f32x3 extents) {
+ /* static mesh prim_cube_mesh_create() { */
+ vertex_darray* vertices = vertex_darray_new(36);
+
+ // back faces
+ VERT_3D(vertices, BACK_TOP_RIGHT, VEC3_NEG_Z, vec2(1, 0));
+ VERT_3D(vertices, BACK_BOT_LEFT, VEC3_NEG_Z, vec2(0, 1));
+ VERT_3D(vertices, BACK_TOP_LEFT, VEC3_NEG_Z, vec2(0, 0));
+ VERT_3D(vertices, BACK_TOP_RIGHT, VEC3_NEG_Z, vec2(1, 0));
+ VERT_3D(vertices, BACK_BOT_RIGHT, VEC3_NEG_Z, vec2(1, 1));
+ VERT_3D(vertices, BACK_BOT_LEFT, VEC3_NEG_Z, vec2(0, 1));
+
+ // front faces
+ VERT_3D(vertices, FRONT_BOT_LEFT, VEC3_Z, vec2(0, 1));
+ VERT_3D(vertices, FRONT_TOP_RIGHT, VEC3_Z, vec2(1, 0));
+ VERT_3D(vertices, FRONT_TOP_LEFT, VEC3_Z, vec2(0, 0));
+ VERT_3D(vertices, FRONT_BOT_LEFT, VEC3_Z, vec2(0, 1));
+ VERT_3D(vertices, FRONT_BOT_RIGHT, VEC3_Z, vec2(1, 1));
+ VERT_3D(vertices, FRONT_TOP_RIGHT, VEC3_Z, vec2(1, 0));
+
+ // top faces
+ VERT_3D(vertices, BACK_TOP_LEFT, VEC3_Y, vec2(0, 0));
+ VERT_3D(vertices, FRONT_TOP_LEFT, VEC3_Y, vec2(0, 1));
+ VERT_3D(vertices, FRONT_TOP_RIGHT, VEC3_Y, vec2(1, 1));
+ VERT_3D(vertices, BACK_TOP_LEFT, VEC3_Y, vec2(0, 0));
+ VERT_3D(vertices, FRONT_TOP_RIGHT, VEC3_Y, vec2(1, 1));
+ VERT_3D(vertices, BACK_TOP_RIGHT, VEC3_Y, vec2(1, 0));
+
+ // bottom faces
+ VERT_3D(vertices, BACK_BOT_LEFT, VEC3_NEG_Y, vec2(0, 1));
+ VERT_3D(vertices, FRONT_BOT_RIGHT, VEC3_NEG_Y, vec2(1, 1));
+ VERT_3D(vertices, FRONT_BOT_LEFT, VEC3_NEG_Y, vec2(0, 1));
+ VERT_3D(vertices, BACK_BOT_LEFT, VEC3_NEG_Y, vec2(0, 1));
+ VERT_3D(vertices, BACK_BOT_RIGHT, VEC3_NEG_Y, vec2(1, 1));
+ VERT_3D(vertices, FRONT_BOT_RIGHT, VEC3_NEG_Y, vec2(0, 1));
+
+ // right faces
+ VERT_3D(vertices, FRONT_TOP_RIGHT, VEC3_X, vec2(0, 0));
+ VERT_3D(vertices, BACK_BOT_RIGHT, VEC3_X, vec2(1, 1));
+ VERT_3D(vertices, BACK_TOP_RIGHT, VEC3_X, vec2(1, 0));
+ VERT_3D(vertices, BACK_BOT_RIGHT, VEC3_X, vec2(1, 1));
+ VERT_3D(vertices, FRONT_TOP_RIGHT, VEC3_X, vec2(0, 0));
+ VERT_3D(vertices, FRONT_BOT_RIGHT, VEC3_X, vec2(0, 1));
+
+ // left faces
+ VERT_3D(vertices, FRONT_TOP_LEFT, VEC3_NEG_X, vec2(0, 0));
+ VERT_3D(vertices, BACK_TOP_LEFT, VEC3_NEG_X, vec2(0, 0));
+ VERT_3D(vertices, BACK_BOT_LEFT, VEC3_NEG_X, vec2(0, 0));
+ VERT_3D(vertices, BACK_BOT_LEFT, VEC3_NEG_X, vec2(0, 0));
+ VERT_3D(vertices, FRONT_BOT_LEFT, VEC3_NEG_X, vec2(0, 0));
+ VERT_3D(vertices, FRONT_TOP_LEFT, VEC3_NEG_X, vec2(0, 0));
+
+ u32_darray* indices = u32_darray_new(vertices->len);
+
+ for (u32 i = 0; i < vertices->len; i++) {
+ u32_darray_push(indices, i);
+ }
+
+ geometry_data geo = {
+ .format = VERTEX_STATIC_3D,
+ .vertices = vertices,
+ .has_indices = true,
+ .indices = *indices, // FIXME: make darray methods that return stack allocated struct
+ .colour = vec3(0, 0, 0),
+ };
+
+ return geo;
}
-/** @brief create a new model with the shape of a cube */
-static model_handle prim_cube_new(core* core) {
- model model = { 0 };
- mesh cube = prim_cube_mesh_create();
+/* /\** @brief create a new model with the shape of a cube *\/ */
+/* static model_handle prim_cube_new(core* core) { */
+/* model model = { 0 }; */
+/* mesh cube = prim_cube_mesh_create(); */
- mesh_darray_push(model.meshes, cube);
- assert(mesh_darray_len(model.meshes) == 1);
+/* mesh_darray_push(model.meshes, cube); */
+/* assert(mesh_darray_len(model.meshes) == 1); */
- u32 index = (u32)model_darray_len(core->models);
- model_darray_push_copy(core->models, &model);
- return (model_handle){ .raw = index };
-}
+/* u32 index = (u32)model_darray_len(core->models); */
+/* model_darray_push_copy(core->models, &model); */
+/* return (model_handle){ .raw = index }; */
+/* } */
// --- Spheres
geometry_data geo_create_uvsphere(f32 radius, f32 north_south_lines, f32 east_west_lines) {
// TODO
-} \ No newline at end of file
+}
diff --git a/src/renderer/backends/backend_vulkan.c b/src/renderer/backends/backend_vulkan.c
index f83ef84..3a9c4e1 100644
--- a/src/renderer/backends/backend_vulkan.c
+++ b/src/renderer/backends/backend_vulkan.c
@@ -84,6 +84,7 @@ bool create_logical_device(gpu_device* out_device);
void create_swapchain_framebuffers();
void create_sync_objects();
void create_descriptor_pools();
+size_t vertex_attrib_size(vertex_attrib_type attr);
VkShaderModule create_shader_module(str8 spirv);
@@ -106,7 +107,7 @@ bool gpu_backend_init(const char* window_name, GLFWwindow* window) {
// Setup Vulkan instance
VkApplicationInfo app_info = { VK_STRUCTURE_TYPE_APPLICATION_INFO };
- app_info.apiVersion = VK_API_VERSION_1_3;
+ app_info.apiVersion = VK_API_VERSION_1_2;
app_info.pApplicationName = window_name;
app_info.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
app_info.pEngineName = "Celeritas Engine";
@@ -351,6 +352,35 @@ static void recreate_swapchain(gpu_swapchain* swapchain) {
create_swapchain_framebuffers();
}
+VkFormat format_from_vertex_attr(vertex_attrib_type attr) {
+ switch (attr) {
+ case ATTR_F32:
+ return VK_FORMAT_R32_SFLOAT;
+ case ATTR_U32:
+ return VK_FORMAT_R32_UINT;
+ case ATTR_I32:
+ return VK_FORMAT_R32_SINT;
+ case ATTR_F32x2:
+ return VK_FORMAT_R32G32_SFLOAT;
+ case ATTR_U32x2:
+ return VK_FORMAT_R32G32_UINT;
+ case ATTR_I32x2:
+ return VK_FORMAT_R32G32_UINT;
+ case ATTR_F32x3:
+ return VK_FORMAT_R32G32B32_SFLOAT;
+ case ATTR_U32x3:
+ return VK_FORMAT_R32G32B32_UINT;
+ case ATTR_I32x3:
+ return VK_FORMAT_R32G32B32_SINT;
+ case ATTR_F32x4:
+ return VK_FORMAT_R32G32B32A32_SFLOAT;
+ case ATTR_U32x4:
+ return VK_FORMAT_R32G32B32A32_UINT;
+ case ATTR_I32x4:
+ return VK_FORMAT_R32G32B32A32_SINT;
+ }
+}
+
gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc description) {
// Allocate
gpu_pipeline_layout* layout = malloc(sizeof(gpu_pipeline_layout));
@@ -381,22 +411,35 @@ gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc descrip
frag_shader_stage_info };
// TODO: Attributes
- VkVertexInputAttributeDescription attribute_descs[2];
+ VkVertexInputAttributeDescription attribute_descs[2] = {0};
+ /* u32 offset = 0; */
+ /* for (u32 i = 0; i < description.vertex_desc.attributes_count; i++) { */
+ /* attribute_descs[i].binding = 0; */
+ /* attribute_descs[i].location = i; */
+ /* attribute_descs[i].format = format_from_vertex_attr(description.vertex_desc.attributes[i]); */
+ /* attribute_descs[i].offset = offset; */
+ /* size_t this_offset = vertex_attrib_size(description.vertex_desc.attributes[i]); */
+ /* printf("offset total %d this attr %ld\n", offset, this_offset); */
+ /* printf("sizeof vertex %ld\n", sizeof(vertex)); */
+ /* printf("%d \n", offsetof(vertex, static_3d)); */
+ /* offset += this_offset; */
+ /* } */
+ printf("Vertex attributes\n");
attribute_descs[0].binding = 0;
attribute_descs[0].location = 0;
- attribute_descs[0].format = VK_FORMAT_R32G32_SFLOAT;
- attribute_descs[0].offset = offsetof(custom_vertex, pos);
+ attribute_descs[0].format = VK_FORMAT_R32G32B32_SFLOAT;
+ attribute_descs[0].offset = 0; // offsetof(custom_vertex, pos);
attribute_descs[1].binding = 0;
attribute_descs[1].location = 1;
attribute_descs[1].format = VK_FORMAT_R32G32B32_SFLOAT;
- attribute_descs[1].offset = offsetof(custom_vertex, color);
+ attribute_descs[1].offset = 12; // offsetof(custom_vertex, color);
// Vertex input
// TODO: Generate this from descroiption now
VkVertexInputBindingDescription binding_desc;
binding_desc.binding = 0;
- binding_desc.stride = sizeof(custom_vertex);
+ binding_desc.stride = sizeof(vertex); // description.vertex_desc.stride;
binding_desc.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
VkPipelineVertexInputStateCreateInfo vertex_input_info = {
@@ -404,7 +447,7 @@ gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc descrip
};
vertex_input_info.vertexBindingDescriptionCount = 1;
vertex_input_info.pVertexBindingDescriptions = &binding_desc;
- vertex_input_info.vertexAttributeDescriptionCount = 2;
+ vertex_input_info.vertexAttributeDescriptionCount = 2; // description.vertex_desc.attributes_count;
vertex_input_info.pVertexAttributeDescriptions = attribute_descs;
// Input Assembly
@@ -441,6 +484,7 @@ gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc descrip
rasterizer_create_info.lineWidth = 1.0f;
rasterizer_create_info.cullMode = VK_CULL_MODE_BACK_BIT;
rasterizer_create_info.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
+ /* rasterizer_create_info.frontFace = VK_FRONT_FACE_CLOCKWISE; */
rasterizer_create_info.depthBiasEnable = VK_FALSE;
rasterizer_create_info.depthBiasConstantFactor = 0.0;
rasterizer_create_info.depthBiasClamp = 0.0;
@@ -584,6 +628,7 @@ gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc descrip
VK_CHECK(vkCreateDescriptorSetLayout(context.device->logical_device, &desc_set_layout_info,
context.allocator, &desc_set_layouts[i]));
}
+ printf("Descriptor set layouts\n");
// Layout
VkPipelineLayoutCreateInfo pipeline_layout_create_info = {
@@ -621,13 +666,18 @@ gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc descrip
pipeline_create_info.basePipelineHandle = VK_NULL_HANDLE;
pipeline_create_info.basePipelineIndex = -1;
+ printf("Here\n");
+
VkResult result =
vkCreateGraphicsPipelines(context.device->logical_device, VK_NULL_HANDLE, 1,
&pipeline_create_info, context.allocator, &pipeline->handle);
if (result != VK_SUCCESS) {
+ printf("BAD\n");
FATAL("graphics pipeline creation failed. its fked mate");
ERROR_EXIT("Doomed");
}
+ printf("Here2\n");
+ TRACE("Vulkan Graphics pipeline created");
// once the pipeline has been created we can destroy these
vkDestroyShaderModule(context.device->logical_device, vertex_shader, context.allocator);
@@ -820,9 +870,7 @@ void encode_bind_shader_data(gpu_cmd_encoder* encoder, u32 group, shader_data* d
alloc_info.pSetLayouts = &encoder->pipeline->desc_set_layouts[group];
VkDescriptorSet sets[1];
- VK_CHECK(
- vkAllocateDescriptorSets(context.device->logical_device, &alloc_info, sets)
- );
+ VK_CHECK(vkAllocateDescriptorSets(context.device->logical_device, &alloc_info, sets));
VkDescriptorSet_darray_push(context.free_set_queue, sets[0]);
shader_data_layout sdl = data->shader_data_get_layout(NULL);
@@ -870,7 +918,7 @@ void encode_set_vertex_buffer(gpu_cmd_encoder* encoder, buffer_handle buf) {
void encode_set_index_buffer(gpu_cmd_encoder* encoder, buffer_handle buf) {
gpu_buffer buffer = context.buffers[buf.raw];
- vkCmdBindIndexBuffer(encoder->cmd_buffer, buffer.handle, 0, VK_INDEX_TYPE_UINT16);
+ vkCmdBindIndexBuffer(encoder->cmd_buffer, buffer.handle, 0, VK_INDEX_TYPE_UINT32);
}
// TEMP
@@ -1310,3 +1358,36 @@ void encode_buffer_copy(gpu_cmd_encoder* encoder, buffer_handle src, u64 src_off
vkCmdCopyBuffer(encoder->cmd_buffer, context.buffers[src.raw].handle,
context.buffers[dst.raw].handle, 1, &copy_region);
}
+
+size_t vertex_attrib_size(vertex_attrib_type attr) {
+ switch (attr) {
+ case ATTR_F32:
+ case ATTR_U32:
+ case ATTR_I32:
+ return 4;
+ case ATTR_F32x2:
+ case ATTR_U32x2:
+ case ATTR_I32x2:
+ return 8;
+ case ATTR_F32x3:
+ case ATTR_U32x3:
+ case ATTR_I32x3:
+ return 12;
+ case ATTR_F32x4:
+ case ATTR_U32x4:
+ case ATTR_I32x4:
+ return 16;
+ break;
+ }
+}
+
+void vertex_desc_add(vertex_description* builder, const char* name, vertex_attrib_type type) {
+ u32 i = builder->attributes_count;
+
+ size_t size = vertex_attrib_size(type);
+ builder->attributes[i] = type;
+ builder->stride += size;
+ builder->attr_names[i] = name;
+
+ builder->attributes_count++;
+}
diff --git a/src/renderer/ral_types.h b/src/renderer/ral_types.h
index 48695ea..c802a9b 100644
--- a/src/renderer/ral_types.h
+++ b/src/renderer/ral_types.h
@@ -85,8 +85,8 @@ typedef enum vertex_format {
typedef union vertex {
struct {
vec3 position;
- vec2 tex_coords;
vec3 normal;
+ vec2 tex_coords;
} static_3d; /** @brief standard vertex format for static geometry in 3D */
struct {
@@ -149,6 +149,7 @@ typedef struct vertex_description {
char* debug_label;
const char* attr_names[MAX_VERTEX_ATTRIBUTES];
vertex_attrib_type attributes[MAX_VERTEX_ATTRIBUTES];
+ u32 attributes_count;
size_t stride;
} vertex_description;
diff --git a/src/renderer/render.c b/src/renderer/render.c
index 9f5b21a..efef6a6 100644
--- a/src/renderer/render.c
+++ b/src/renderer/render.c
@@ -4,6 +4,7 @@
#include "file.h"
#include "log.h"
#include "ral.h"
+#include "ral_types.h"
/** @brief Creates the pipelines built into Celeritas such as rendering static opaque geometry,
debug visualisations, immediate mode UI, etc */
@@ -126,6 +127,48 @@ void render_frame_end(renderer* ren) {
}
void render_frame_draw(renderer* ren) {}
+void draw_mesh(mesh* mesh, mat4* model) { // , mat4* view, mat4* proj) {
+ gpu_cmd_encoder* enc = gpu_get_default_cmd_encoder();
+ encode_set_vertex_buffer(enc, mesh->vertex_buffer);
+ if (mesh->has_indices) {
+ encode_set_index_buffer(enc, mesh->index_buffer);
+ }
+ // Assume this has already been done
+ /* encode_bind_shader_data(enc, 0, &mvp_uniforms_data); */
+ encode_draw_indexed(enc, mesh->index_count);
+}
+
void gfx_backend_draw_frame(renderer* ren, camera* camera, mat4 model, texture* tex) {}
void geo_set_vertex_colours(geometry_data* geo, vec4 colour) {}
+
+// --- NEW
+
+mesh mesh_create(geometry_data* geometry, bool free_on_upload) {
+ mesh m = { 0 };
+
+ // Create and upload vertex buffer
+ size_t vert_bytes = geometry->vertices->len * sizeof(vertex);
+ INFO("Creating vertex buffer with size %d (%d x %d)", vert_bytes, geometry->vertices->len,
+ sizeof(vertex));
+ m.vertex_buffer = gpu_buffer_create(vert_bytes, CEL_BUFFER_VERTEX, CEL_BUFFER_FLAG_GPU,
+ geometry->vertices->data);
+
+ // Create and upload index buffer
+ size_t index_bytes = geometry->indices.len * sizeof(u32);
+ INFO("Creating index buffer with size %d (len: %d)", index_bytes, geometry->indices.len);
+ m.index_buffer =
+ gpu_buffer_create(index_bytes, CEL_BUFFER_INDEX, CEL_BUFFER_FLAG_GPU, geometry->indices.data);
+
+ m.is_uploaded = true;
+ m.has_indices = geometry->has_indices;
+ m.index_count = geometry->indices.len;
+ m.vertices = geometry;
+ if (free_on_upload) {
+ geo_free_data(geometry);
+ }
+
+ // TODO: materials?
+
+ return m;
+}
diff --git a/src/renderer/render.h b/src/renderer/render.h
index 4477121..c87e5f7 100644
--- a/src/renderer/render.h
+++ b/src/renderer/render.h
@@ -25,6 +25,11 @@ void render_frame_draw(renderer* ren);
typedef struct camera camera;
void gfx_backend_draw_frame(renderer* ren, camera* camera, mat4 model, texture* tex);
+typedef struct render_ctx {
+ mat4 view;
+ mat4 projection;
+} render_ctx;
+
// frontend -- these can be called from say a loop in an example, or via FFI
texture_handle texture_create(const char* debug_name, texture_desc description, const u8* data);
@@ -39,10 +44,22 @@ sampler_handle sampler_create();
void shader_hot_reload(const char* filepath);
// models and meshes are implemented **in terms of the above**
-mesh mesh_create(geometry_data* geometry);
+
+/**
+ * @brief Creates buffers and returns a struct that holds handles to our resources
+ *
+ * @param geometry
+ * @param free_on_upload frees the CPU-side vertex/index data stored in geometry_data when we successfully upload
+ that data to the GPU-side buffer
+ * @return mesh
+ */
+mesh mesh_create(geometry_data* geometry, bool free_on_upload);
+
+void draw_mesh(mesh* mesh, mat4* model);//, mat4* view, mat4* proj); // TODO: material
model_handle model_load(const char* debug_name, const char* filepath);
+void geo_free_data(geometry_data* geo);
void geo_set_vertex_colours(geometry_data* geo, vec4 colour);
-vertex_description static_3d_vertex_description(); \ No newline at end of file
+vertex_description static_3d_vertex_description();
diff --git a/src/renderer/render_types.h b/src/renderer/render_types.h
index 06a8415..ac98627 100644
--- a/src/renderer/render_types.h
+++ b/src/renderer/render_types.h
@@ -45,12 +45,15 @@ typedef struct geometry_data {
vec3 colour; /** Optional: set vertex colours */
} geometry_data;
+// 'Upload' a geometry_data (to GPU) -> get back a mesh
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;
/* Hot reloading: