From dc26480ddb54b35ff2bb0d411d6ce660fa564c16 Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Thu, 7 Mar 2019 20:48:37 -0700 Subject: [PATCH] Add nested designated initialization --- examples/hello_triangle_c/main.c | 320 ++++++++++++++++--------------- 1 file changed, 164 insertions(+), 156 deletions(-) diff --git a/examples/hello_triangle_c/main.c b/examples/hello_triangle_c/main.c index fa6cb16b7..e44e8e8f3 100644 --- a/examples/hello_triangle_c/main.c +++ b/examples/hello_triangle_c/main.c @@ -1,13 +1,13 @@ -#include #include "./../../wgpu-bindings/wgpu.h" +#include #define WGPU_TARGET_MACOS 1 #define WGPU_TARGET_LINUX 2 #define WGPU_TARGET_WINDOWS 3 #if WGPU_TARGET == WGPU_TARGET_MACOS -#include #include +#include #endif #include @@ -26,166 +26,171 @@ #define RENDER_PASS_ATTACHMENTS_LENGTH (1) #define BIND_GROUP_LAYOUTS_LENGTH (1) -WGPUByteArray read_file(const char *name) -{ - FILE *file = fopen(name, "rb"); - fseek(file, 0, SEEK_END); - long length = ftell(file); - unsigned char *bytes = malloc(length); - fseek(file, 0, SEEK_SET); - fread(bytes, 1, length, file); - fclose(file); - WGPUByteArray ret = { - .bytes = bytes, - .length = length, - }; - return ret; +WGPUByteArray read_file(const char *name) { + FILE *file = fopen(name, "rb"); + fseek(file, 0, SEEK_END); + long length = ftell(file); + unsigned char *bytes = malloc(length); + fseek(file, 0, SEEK_SET); + fread(bytes, 1, length, file); + fclose(file); + return (WGPUByteArray){ + .bytes = bytes, + .length = length, + }; } -int main() -{ - WGPUInstanceId instance = wgpu_create_instance(); - WGPUAdapterDescriptor adapter_desc = { - .power_preference = WGPUPowerPreference_LowPower, - }; - WGPUAdapterId adapter = wgpu_instance_get_adapter(instance, &adapter_desc); - WGPUDeviceDescriptor device_desc = { - .extensions = { - .anisotropic_filtering = false, - }, - }; - WGPUDeviceId device = wgpu_adapter_create_device(adapter, &device_desc); +int main() { + WGPUInstanceId instance = wgpu_create_instance(); - WGPUShaderModuleDescriptor vertex_shader_desc = { - .code = read_file("./../../data/hello_triangle.vert.spv"), - }; - WGPUShaderModuleId vertex_shader = wgpu_device_create_shader_module(device, &vertex_shader_desc); - WGPUPipelineStageDescriptor vertex_stage = { - .module = vertex_shader, - .entry_point = "main", - }; + WGPUAdapterId adapter = wgpu_instance_get_adapter( + instance, &(WGPUAdapterDescriptor){ + .power_preference = WGPUPowerPreference_LowPower, + }); - WGPUShaderModuleDescriptor fragment_shader_desc = { - .code = read_file("./../../data/hello_triangle.frag.spv"), - }; - WGPUShaderModuleId fragment_shader = wgpu_device_create_shader_module(device, &fragment_shader_desc); - WGPUPipelineStageDescriptor fragment_stage = { - .module = fragment_shader, - .entry_point = "main", - }; + WGPUDeviceId device = wgpu_adapter_create_device( + adapter, &(WGPUDeviceDescriptor){ + .extensions = + { + .anisotropic_filtering = false, + }, + }); - WGPUBindGroupLayoutDescriptor bind_group_layout_desc = { - .bindings = NULL, - .bindings_length = 0, - }; - WGPUBindGroupLayoutId bind_group_layout = wgpu_device_create_bind_group_layout(device, &bind_group_layout_desc); + WGPUShaderModuleId vertex_shader = wgpu_device_create_shader_module( + device, &(WGPUShaderModuleDescriptor){ + .code = read_file("./../../data/hello_triangle.vert.spv"), + }); - WGPUBindGroupLayoutId bind_group_layouts[BIND_GROUP_LAYOUTS_LENGTH] = { bind_group_layout }; + WGPUShaderModuleId fragment_shader = wgpu_device_create_shader_module( + device, &(WGPUShaderModuleDescriptor){ + .code = read_file("./../../data/hello_triangle.frag.spv"), + }); - WGPUPipelineLayoutDescriptor pipeline_layout_desc = { - .bind_group_layouts = bind_group_layouts, - .bind_group_layouts_length = BIND_GROUP_LAYOUTS_LENGTH, - }; - WGPUPipelineLayoutId pipeline_layout = wgpu_device_create_pipeline_layout(device, &pipeline_layout_desc); + WGPUBindGroupLayoutId bind_group_layout = + wgpu_device_create_bind_group_layout(device, + &(WGPUBindGroupLayoutDescriptor){ + .bindings = NULL, + .bindings_length = 0, + }); + WGPUBindGroupLayoutId bind_group_layouts[BIND_GROUP_LAYOUTS_LENGTH] = { + bind_group_layout}; - WGPUBlendDescriptor blend_alpha = { - .src_factor = WGPUBlendFactor_One, - .dst_factor = WGPUBlendFactor_Zero, - .operation = WGPUBlendOperation_Add, - }; - WGPUBlendDescriptor blend_color = { - .src_factor = WGPUBlendFactor_One, - .dst_factor = WGPUBlendFactor_Zero, - .operation = WGPUBlendOperation_Add, - }; - WGPUColorStateDescriptor color_state_desc = { - .format = WGPUTextureFormat_Bgra8Unorm, - .alpha = blend_alpha, - .color = blend_color, - .write_mask = WGPUColorWriteFlags_ALL, - }; - WGPURasterizationStateDescriptor rasterization_state = { - .front_face = WGPUFrontFace_Ccw, - .cull_mode = WGPUCullMode_None, - .depth_bias = 0, - .depth_bias_slope_scale = 0.0, - .depth_bias_clamp = 0.0, - }; - WGPUVertexBufferStateDescriptor vertex_buffer_state = { - .index_format = WGPUIndexFormat_Uint16, - .vertex_buffers = NULL, - .vertex_buffers_count = 0, - }; - WGPURenderPipelineDescriptor render_pipeline_desc = { - .layout = pipeline_layout, - .vertex_stage = vertex_stage, - .fragment_stage = fragment_stage, - .rasterization_state = rasterization_state, - .primitive_topology = WGPUPrimitiveTopology_TriangleList, - .color_states = &color_state_desc, - .color_states_length = 1, - .depth_stencil_state = NULL, - .vertex_buffer_state = vertex_buffer_state, - .sample_count = 1, - }; + WGPUPipelineLayoutId pipeline_layout = wgpu_device_create_pipeline_layout( + device, &(WGPUPipelineLayoutDescriptor){ + .bind_group_layouts = bind_group_layouts, + .bind_group_layouts_length = BIND_GROUP_LAYOUTS_LENGTH, + }); - WGPURenderPipelineId render_pipeline = wgpu_device_create_render_pipeline(device, &render_pipeline_desc); + WGPURenderPipelineId render_pipeline = wgpu_device_create_render_pipeline( + device, &(WGPURenderPipelineDescriptor){ + .layout = pipeline_layout, + .vertex_stage = + (WGPUPipelineStageDescriptor){ + .module = vertex_shader, + .entry_point = "main", + }, + .fragment_stage = + (WGPUPipelineStageDescriptor){ + .module = fragment_shader, + .entry_point = "main", + }, + .rasterization_state = + (WGPURasterizationStateDescriptor){ + .front_face = WGPUFrontFace_Ccw, + .cull_mode = WGPUCullMode_None, + .depth_bias = 0, + .depth_bias_slope_scale = 0.0, + .depth_bias_clamp = 0.0, + }, + .primitive_topology = WGPUPrimitiveTopology_TriangleList, + .color_states = + &(WGPUColorStateDescriptor){ + .format = WGPUTextureFormat_Bgra8Unorm, + .alpha = + (WGPUBlendDescriptor){ + .src_factor = WGPUBlendFactor_One, + .dst_factor = WGPUBlendFactor_Zero, + .operation = WGPUBlendOperation_Add, + }, + .color = + (WGPUBlendDescriptor){ + .src_factor = WGPUBlendFactor_One, + .dst_factor = WGPUBlendFactor_Zero, + .operation = WGPUBlendOperation_Add, + }, + .write_mask = WGPUColorWriteFlags_ALL, + }, + .color_states_length = 1, + .depth_stencil_state = NULL, + .vertex_buffer_state = + (WGPUVertexBufferStateDescriptor){ + .index_format = WGPUIndexFormat_Uint16, + .vertex_buffers = NULL, + .vertex_buffers_count = 0, + }, + .sample_count = 1, + }); - if (!glfwInit()) - { - printf("Cannot initialize glfw"); - return 1; - } + if (!glfwInit()) { + printf("Cannot initialize glfw"); + return 1; + } - glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); - GLFWwindow *window = glfwCreateWindow(640, 480, "wgpu with glfw", NULL, NULL); + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + GLFWwindow *window = glfwCreateWindow(640, 480, "wgpu with glfw", NULL, NULL); - if (!window) - { - printf("Cannot create window"); - return 1; - } + if (!window) { + printf("Cannot create window"); + return 1; + } - WGPUSurfaceId surface = {}; + WGPUSurfaceId surface = {}; #if WGPU_TARGET == WGPU_TARGET_MACOS - { - id metal_layer = NULL; - NSWindow *ns_window = glfwGetCocoaWindow(window); - CALayer *layer = ns_window.contentView.layer; - [ns_window.contentView setWantsLayer:YES]; - metal_layer = [CAMetalLayer layer]; - [ns_window.contentView setLayer:metal_layer]; - surface = wgpu_instance_create_surface_from_macos_layer(instance, metal_layer); - } + { + id metal_layer = NULL; + NSWindow *ns_window = glfwGetCocoaWindow(window); + CALayer *layer = ns_window.contentView.layer; + [ns_window.contentView setWantsLayer:YES]; + metal_layer = [CAMetalLayer layer]; + [ns_window.contentView setLayer:metal_layer]; + surface = + wgpu_instance_create_surface_from_macos_layer(instance, metal_layer); + } #elif WGPU_TARGET == WGPU_TARGET_LINUX - { - Display* x11_display = glfwGetX11Display(); - Window x11_window = glfwGetX11Window(window); - surface = wgpu_instance_create_surface_from_xlib(instance, (const void**)x11_display, x11_window); - } + { + Display *x11_display = glfwGetX11Display(); + Window x11_window = glfwGetX11Window(window); + surface = wgpu_instance_create_surface_from_xlib( + instance, (const void **)x11_display, x11_window); + } #elif WGPU_TARGET == WGPU_TARGET_WINDOWS - { - HWND hwnd = glfwGetWin32Window(window); - HINSTANCE hinstance = GetModuleHandle(NULL); - surface = wgpu_instance_create_surface_from_windows_hwnd(instance, hinstance, hwnd); - } + { + HWND hwnd = glfwGetWin32Window(window); + HINSTANCE hinstance = GetModuleHandle(NULL); + surface = wgpu_instance_create_surface_from_windows_hwnd(instance, + hinstance, hwnd); + } #endif - WGPUSwapChainDescriptor swap_chain_desc = { - .usage = WGPUTextureUsageFlags_OUTPUT_ATTACHMENT, - .format = WGPUTextureFormat_Bgra8Unorm, - .width = 640, - .height = 480, - }; - WGPUSwapChainId swap_chain = wgpu_device_create_swap_chain(device, surface, &swap_chain_desc); + WGPUSwapChainId swap_chain = wgpu_device_create_swap_chain( + device, surface, + &(WGPUSwapChainDescriptor){ + .usage = WGPUTextureUsageFlags_OUTPUT_ATTACHMENT, + .format = WGPUTextureFormat_Bgra8Unorm, + .width = 640, + .height = 480, + }); - while (!glfwWindowShouldClose(window)) - { - WGPUSwapChainOutput next_texture = wgpu_swap_chain_get_next_texture(swap_chain); - WGPUCommandEncoderDescriptor cmd_encoder_desc = { .todo = 0 }; - WGPUCommandEncoderId cmd_encoder = wgpu_device_create_command_encoder(device, &cmd_encoder_desc); - WGPURenderPassColorAttachmentDescriptor_TextureViewId color_attachments[ATTACHMENTS_LENGTH] = { + while (!glfwWindowShouldClose(window)) { + WGPUSwapChainOutput next_texture = + wgpu_swap_chain_get_next_texture(swap_chain); + + WGPUCommandEncoderId cmd_encoder = wgpu_device_create_command_encoder( + device, &(WGPUCommandEncoderDescriptor){.todo = 0}); + + WGPURenderPassColorAttachmentDescriptor_TextureViewId + color_attachments[ATTACHMENTS_LENGTH] = { { .attachment = next_texture.view_id, .load_op = WGPULoadOp_Clear, @@ -193,24 +198,27 @@ int main() .clear_color = WGPUColor_GREEN, }, }; - WGPURenderPassDescriptor rpass_desc = { + + WGPURenderPassId rpass = wgpu_command_encoder_begin_render_pass( + cmd_encoder, + (WGPURenderPassDescriptor){ .color_attachments = color_attachments, .color_attachments_length = RENDER_PASS_ATTACHMENTS_LENGTH, .depth_stencil_attachment = NULL, - }; - WGPURenderPassId rpass = wgpu_command_encoder_begin_render_pass(cmd_encoder, rpass_desc); - wgpu_render_pass_set_pipeline(rpass, render_pipeline); - wgpu_render_pass_draw(rpass, 3, 1, 0, 0); - WGPUCommandBufferId cmd_buf = wgpu_render_pass_end_pass(rpass); - WGPUQueueId queue = wgpu_device_get_queue(device); - wgpu_queue_submit(queue, &cmd_buf, 1); - wgpu_swap_chain_present(swap_chain); + }); - glfwPollEvents(); - } + wgpu_render_pass_set_pipeline(rpass, render_pipeline); + wgpu_render_pass_draw(rpass, 3, 1, 0, 0); + WGPUQueueId queue = wgpu_device_get_queue(device); + WGPUCommandBufferId cmd_buf = wgpu_render_pass_end_pass(rpass); + wgpu_queue_submit(queue, &cmd_buf, 1); + wgpu_swap_chain_present(swap_chain); - glfwDestroyWindow(window); - glfwTerminate(); + glfwPollEvents(); + } - return 0; + glfwDestroyWindow(window); + glfwTerminate(); + + return 0; }