1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
#include <glfw3.h>
#include "buf.h"
#include "camera.h"
#include "core.h"
#include "file.h"
#include "log.h"
#include "maths.h"
#include "maths_types.h"
#include "mem.h"
#include "ral.h"
#include "ral_types.h"
#include "render.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;
mat4 view;
mat4 projection;
} mvp_uniforms;
shader_data_layout mvp_uniforms_layout(void* data) {
mvp_uniforms* d = (mvp_uniforms*)data;
bool has_data = data != NULL;
shader_binding b1 = { .label = "mvp_uniforms",
.type = SHADER_BINDING_BYTES,
.stores_data = has_data,
.data = { .bytes = { .size = sizeof(mvp_uniforms) } } };
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 };
}
int main() {
core_bringup();
arena scratch = arena_create(malloc(1024 * 1024), 1024 * 1024);
DEBUG("render capacity %d", g_core.default_scene.renderables->capacity);
shader_data mvp_uniforms_data = { .data = NULL, .shader_data_get_layout = &mvp_uniforms_layout };
gpu_renderpass_desc pass_description = {};
gpu_renderpass* renderpass = gpu_renderpass_create(&pass_description);
str8 vert_path = str8lit("build/linux/x86_64/debug/cube.vert.spv");
str8 frag_path = str8lit("build/linux/x86_64/debug/triangle.frag.spv");
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) {
ERROR_EXIT("Failed to load shaders from disk")
}
struct graphics_pipeline_desc pipeline_description = {
.debug_name = "Basic Pipeline",
.data_layouts = { mvp_uniforms_data },
.data_layouts_count = 1,
.vs = { .debug_name = "Triangle Vertex Shader",
.filepath = vert_path,
.code = vertex_shader.contents,
.is_spirv = true },
.fs = { .debug_name = "Triangle Fragment Shader",
.filepath = frag_path,
.code = fragment_shader.contents,
.is_spirv = true },
.renderpass = renderpass,
.wireframe = false,
.depth_test = false
};
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);
// 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;
}
gpu_cmd_encoder* enc = gpu_get_default_cmd_encoder();
// begin recording
gpu_cmd_encoder_begin(*enc);
gpu_cmd_encoder_begin_render(enc, renderpass);
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);
mat4 model = mat4_rotation(rotation);
mvp_uniforms mvp_data = { .model = model, .view = mat4_ident(), .projection = mat4_ident() };
mvp_uniforms_data.data = &mvp_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);
// End recording
gpu_cmd_encoder_end_render(enc);
gpu_cmd_buffer buf = gpu_cmd_encoder_finish(enc);
gpu_queue_submit(&buf);
// Submit
gpu_backend_end_frame();
// render_frame_end(&g_core.renderer);
// glfwSwapBuffers(core->renderer.window);
}
renderer_shutdown(&g_core.renderer);
return 0;
}
|