diff options
author | Joshua Rowe <17525998+omnisci3nce@users.noreply.github.com> | 2024-06-09 14:59:01 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-09 14:59:01 +1000 |
commit | 9c79df522980eabdc5e52592cbd152e2a285c4cc (patch) | |
tree | 9082af194033e5e3e4a770456209d3bac7784943 /examples | |
parent | 8d116bd23d9441e33cb3377e90c08169109b438a (diff) | |
parent | d4ff15d9cd82a6e3bc71da9d04ee0f250460cef1 (diff) |
Merge pull request #16 from omnisci3nce/port-opengl-ral
Bring back OpenGL (part 1)
Diffstat (limited to 'examples')
-rw-r--r-- | examples/cube/ex_cube.c | 26 | ||||
-rw-r--r-- | examples/triangle/ex_triangle.c | 55 |
2 files changed, 54 insertions, 27 deletions
diff --git a/examples/cube/ex_cube.c b/examples/cube/ex_cube.c index 68ce0d5..e85b5c1 100644 --- a/examples/cube/ex_cube.c +++ b/examples/cube/ex_cube.c @@ -62,14 +62,21 @@ 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 }; 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/cube.frag.spv"); + str8 vert_path, frag_path; +#ifdef CEL_REND_BACKEND_OPENGL + vert_path = str8lit("assets/shaders/cube.vert"); + frag_path = str8lit("assets/shaders/cube.frag"); +#else + vert_path = str8lit("build/linux/x86_64/debug/cube.vert.spv"); + frag_path = str8lit("build/linux/x86_64/debug/cube.frag.spv"); +#endif 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) { @@ -123,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.extent.width, - g_core.renderer.swapchain.extent.height, &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/examples/triangle/ex_triangle.c b/examples/triangle/ex_triangle.c index d57e224..cd401c7 100644 --- a/examples/triangle/ex_triangle.c +++ b/examples/triangle/ex_triangle.c @@ -4,6 +4,7 @@ #include "buf.h" #include "camera.h" #include "core.h" +#include "defines.h" #include "file.h" #include "log.h" #include "maths.h" @@ -15,15 +16,15 @@ extern core g_core; -const custom_vertex vertices[] = { - (custom_vertex){ .pos = vec2(-0.5, -0.5), .color = vec3(1.0, 1.0, 1.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(0.0, 0.0, 1.0) }, - (custom_vertex){ .pos = vec2(0.5, 0.5), .color = vec3(0.0, 1.0, 0.0) }, -}; -const u32 indices[] = { 2, 1, 0, 1, 2, 3 }; - int main() { + custom_vertex vertices[] = { + (custom_vertex){ .pos = vec2(-0.5, -0.5), .color = vec3(1.0, 1.0, 1.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(0.0, 0.0, 1.0) }, + (custom_vertex){ .pos = vec2(0.5, 0.5), .color = vec3(0.0, 1.0, 0.0) }, + }; + const u32 indices[] = { 2, 1, 0, 1, 2, 3 }; + core_bringup(); arena scratch = arena_create(malloc(1024 * 1024), 1024 * 1024); @@ -35,8 +36,14 @@ int main() { gpu_renderpass_desc pass_description = {}; gpu_renderpass* renderpass = gpu_renderpass_create(&pass_description); - str8 vert_path = str8lit("build/linux/x86_64/debug/triangle.vert.spv"); - str8 frag_path = str8lit("build/linux/x86_64/debug/triangle.frag.spv"); + str8 vert_path, frag_path; +#if defined(CEL_REND_BACKEND_OPENGL) + vert_path = str8lit("assets/shaders/triangle.vert"); + frag_path = str8lit("assets/shaders/triangle.frag"); +#elif defined(CEL_REND_BACKEND_METAL) + vert_path = str8lit("build/gfx.metallib"); + frag_path = str8lit("build/gfx.metallib"); +#endif 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) { @@ -51,7 +58,11 @@ int main() { .vs = { .debug_name = "Triangle Vertex Shader", .filepath = vert_path, .code = vertex_shader.contents, - .is_spirv = true }, +#ifdef CEL_REND_BACKEND_METAL + .is_combined_vert_frag = true, +#endif + .is_spirv = true, + }, .fs = { .debug_name = "Triangle Fragment Shader", .filepath = frag_path, .code = fragment_shader.contents, @@ -63,13 +74,13 @@ int main() { gpu_pipeline* gfx_pipeline = gpu_graphics_pipeline_create(pipeline_description); // Load triangle vertex and index data - buffer_handle triangle_vert_buf = - gpu_buffer_create(4 * sizeof(vertex), CEL_BUFFER_VERTEX, CEL_BUFFER_FLAG_GPU, vertices); + buffer_handle triangle_vert_buf = gpu_buffer_create(4 * sizeof(custom_vertex), 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 + // // Main loop while (!should_exit()) { input_update(&g_core.input); @@ -77,24 +88,24 @@ int main() { continue; } gpu_cmd_encoder* enc = gpu_get_default_cmd_encoder(); - // Begin recording + // // 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); - // Record draw calls + // // Record draw calls encode_set_vertex_buffer(enc, triangle_vert_buf); encode_set_index_buffer(enc, triangle_index_buf); encode_draw_indexed(enc, 6); - // End recording - gpu_cmd_encoder_end_render(enc); + // // End recording + // gpu_cmd_encoder_end_render(enc); - gpu_cmd_buffer buf = gpu_cmd_encoder_finish( - enc); // Command buffer is no longer recording and is ready to submit - // Submit - gpu_queue_submit(&buf); + // gpu_cmd_buffer buf = gpu_cmd_encoder_finish(enc); // Command buffer is no longer recording + // and is ready to submit + // // Submit + // gpu_queue_submit(&buf); gpu_backend_end_frame(); } |