From fa2c2d745729a34516e31e1e4a53a0a46279ce05 Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Thu, 7 Mar 2019 07:34:27 -0700 Subject: [PATCH 1/9] Combine Apple with other targets --- examples/hello_triangle_c/CMakeLists.txt | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/examples/hello_triangle_c/CMakeLists.txt b/examples/hello_triangle_c/CMakeLists.txt index 204b0c4685..da89d317b7 100644 --- a/examples/hello_triangle_c/CMakeLists.txt +++ b/examples/hello_triangle_c/CMakeLists.txt @@ -10,18 +10,16 @@ if(MSVC) target_compile_options(${TARGET_NAME} PRIVATE /W4) add_compile_definitions(WGPU_TARGET=WGPU_TARGET_WINDOWS) set(GLFW_LIBRARY glfw3) +elseif(APPLE) + add_compile_definitions(WGPU_TARGET=WGPU_TARGET_MACOS) + set(OS_LIBRARIES "-framework Cocoa" "-framework CoreVideo" "-framework IOKit" "-framework QuartzCore") + target_compile_options(${TARGET_NAME} PRIVATE -x objective-c) else(MSVC) target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra -pedantic) add_compile_definitions(WGPU_TARGET=WGPU_TARGET_LINUX) set(GLFW_LIBRARY glfw) endif(MSVC) -if(APPLE) - add_compile_definitions(WGPU_TARGET=WGPU_TARGET_MACOS) - set(OS_LIBRARIES "-framework Cocoa" "-framework CoreVideo" "-framework IOKit" "-framework QuartzCore") - target_compile_options(${TARGET_NAME} PRIVATE -x objective-c) -endif(APPLE) - find_package(glfw3) find_library(WGPU_LIBRARY wgpu_native From 75ac875902d3ae095d6af1ef8bb2841f1a54b4de Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Thu, 7 Mar 2019 07:43:19 -0700 Subject: [PATCH 2/9] Start to update C example --- examples/hello_triangle_c/main.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/examples/hello_triangle_c/main.c b/examples/hello_triangle_c/main.c index ca6923815a..c851b99baa 100644 --- a/examples/hello_triangle_c/main.c +++ b/examples/hello_triangle_c/main.c @@ -63,7 +63,6 @@ int main() WGPUShaderModuleId vertex_shader = wgpu_device_create_shader_module(device, &vertex_shader_desc); WGPUPipelineStageDescriptor vertex_stage = { .module = vertex_shader, - .stage = WGPUShaderStage_Vertex, .entry_point = "main", }; @@ -73,7 +72,6 @@ int main() WGPUShaderModuleId fragment_shader = wgpu_device_create_shader_module(device, &fragment_shader_desc); WGPUPipelineStageDescriptor fragment_stage = { .module = fragment_shader, - .stage = WGPUShaderStage_Fragment, .entry_point = "main", }; @@ -140,7 +138,7 @@ int main() .samples = 1, }, }; - WGPUAttachmentsState attachment_state = { + WGPUColorStateDescriptor attachment_state = { .color_attachments = attachments, .color_attachments_length = ATTACHMENTS_LENGTH, .depth_stencil_attachment = NULL, @@ -148,8 +146,8 @@ int main() WGPURenderPipelineDescriptor render_pipeline_desc = { .layout = pipeline_layout, - .stages = stages, - .stages_length = STAGES_LENGTH, + .vertex_stage = vertex_stage, + .fragment_stage = fragment_stage, .primitive_topology = WGPUPrimitiveTopology_TriangleList, .attachments_state = attachment_state, .blend_states = blend_state, @@ -201,7 +199,7 @@ int main() #endif WGPUSwapChainDescriptor swap_chain_desc = { - .usage = WGPUTextureUsageFlags_OUTPUT_ATTACHMENT | WGPUTextureUsageFlags_PRESENT, + .usage = WGPUTextureUsageFlags_OUTPUT_ATTACHMENT, .format = WGPUTextureFormat_Bgra8Unorm, .width = 640, .height = 480, @@ -211,8 +209,8 @@ int main() while (!glfwWindowShouldClose(window)) { WGPUSwapChainOutput next_texture = wgpu_swap_chain_get_next_texture(swap_chain); - WGPUCommandBufferDescriptor cmd_buf_desc = { .todo = 0 }; - WGPUCommandBufferId cmd_buf = wgpu_device_create_command_buffer(device, &cmd_buf_desc); + WGPUCommandEncoderDescriptor cmd_encoder_desc = { .todo = 0 }; + WGPUCommandEncoderId cmd_encoder = wgpu_device_create_command_encoder(device, &cmd_encoder_desc); WGPURenderPassColorAttachmentDescriptor_TextureViewId color_attachments[ATTACHMENTS_LENGTH] = { { .attachment = next_texture.view_id, @@ -226,10 +224,10 @@ int main() .color_attachments_length = RENDER_PASS_ATTACHMENTS_LENGTH, .depth_stencil_attachment = NULL, }; - WGPURenderPassId rpass = wgpu_command_buffer_begin_render_pass(cmd_buf, rpass_desc); + WGPURenderPassId rpass = wgpu_command_buffer_begin_render_pass(cmd_encoder, rpass_desc); wgpu_render_pass_set_pipeline(rpass, render_pipeline); wgpu_render_pass_draw(rpass, 3, 1, 0, 0); - wgpu_render_pass_end_pass(rpass); + 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); From a55502c1e2a747b1b2c4ee2d3fd5c34f121088e6 Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Thu, 7 Mar 2019 12:37:50 -0700 Subject: [PATCH 3/9] Remove winit from bindings generation --- wgpu-bindings/wgpu.h | 3 --- wgpu-native/Cargo.toml | 3 ++- wgpu-native/src/instance.rs | 2 +- wgpu-native/src/lib.rs | 2 +- wgpu-rs/Cargo.toml | 2 +- 5 files changed, 5 insertions(+), 7 deletions(-) diff --git a/wgpu-bindings/wgpu.h b/wgpu-bindings/wgpu.h index 60c2f51198..0e503ec20e 100644 --- a/wgpu-bindings/wgpu.h +++ b/wgpu-bindings/wgpu.h @@ -752,9 +752,6 @@ WGPUSurfaceId wgpu_instance_create_surface_from_windows_hwnd(WGPUInstanceId inst void *hinstance, void *hwnd); -WGPUSurfaceId wgpu_instance_create_surface_from_winit(WGPUInstanceId instance_id, - const WGPUWindow *window); - WGPUSurfaceId wgpu_instance_create_surface_from_xlib(WGPUInstanceId instance_id, const void **display, uint64_t window); diff --git a/wgpu-native/Cargo.toml b/wgpu-native/Cargo.toml index ab06f10f13..44fb34c7ac 100644 --- a/wgpu-native/Cargo.toml +++ b/wgpu-native/Cargo.toml @@ -17,9 +17,10 @@ crate-type = ["lib", "cdylib", "staticlib"] [features] default = [] -local = ["winit", "gfx-backend-empty/winit"] +local = [] remote = ["serde"] metal-auto-capture = ["gfx-backend-metal/auto-capture"] +window-winit = ["winit", "gfx-backend-empty/winit"] [dependencies] arrayvec = "0.4" diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index 1c6336e1d4..c479881278 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -46,7 +46,7 @@ pub extern "C" fn wgpu_create_instance() -> InstanceId { HUB.instances.register_local(inst) } -#[cfg(feature = "local")] +#[cfg(all(feature = "local", feature = "window-winit"))] #[no_mangle] pub extern "C" fn wgpu_instance_create_surface_from_winit( instance_id: InstanceId, diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index 8acf154feb..25a99c1bf6 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -1,4 +1,4 @@ -#[cfg(feature = "local")] +#[cfg(all(feature = "local", feature = "window-winit"))] pub extern crate winit; #[cfg(feature = "gfx-backend-dx11")] diff --git a/wgpu-rs/Cargo.toml b/wgpu-rs/Cargo.toml index 83a2ff8fe1..c79655bf39 100644 --- a/wgpu-rs/Cargo.toml +++ b/wgpu-rs/Cargo.toml @@ -23,5 +23,5 @@ dx12 = ["wgpu-native/gfx-backend-dx12"] vulkan = ["wgpu-native/gfx-backend-vulkan"] [dependencies] -wgpu-native = { version = "0.2", path = "../wgpu-native", features = ["local"] } +wgpu-native = { version = "0.2", path = "../wgpu-native", features = ["local", "window-winit"] } arrayvec = "0.4" From e921b592989516fcbc2d94ad023c547565fcb4ca Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Thu, 7 Mar 2019 19:04:01 -0700 Subject: [PATCH 4/9] Use `repr(C)` for `Id` --- wgpu-bindings/wgpu.h | 9 ++++++++- wgpu-native/src/hub.rs | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/wgpu-bindings/wgpu.h b/wgpu-bindings/wgpu.h index 0e503ec20e..6d1ad8d3d0 100644 --- a/wgpu-bindings/wgpu.h +++ b/wgpu-bindings/wgpu.h @@ -240,7 +240,14 @@ typedef enum { typedef struct WGPUBufferMapAsyncStatus WGPUBufferMapAsyncStatus; -typedef struct WGPUId WGPUId; +typedef uint32_t WGPUIndex; + +typedef uint32_t WGPUEpoch; + +typedef struct { + WGPUIndex _0; + WGPUEpoch _1; +} WGPUId; typedef WGPUId WGPUDeviceId; diff --git a/wgpu-native/src/hub.rs b/wgpu-native/src/hub.rs index b8063944bb..4e50ab23b1 100644 --- a/wgpu-native/src/hub.rs +++ b/wgpu-native/src/hub.rs @@ -18,6 +18,8 @@ use std::sync::Arc; pub(crate) type Index = u32; pub(crate) type Epoch = u32; + +#[repr(C)] #[derive(Clone, Copy, Debug, Hash, PartialEq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Id(Index, Epoch); From 510865dd2795d79e234ef9a24dc760ae1a45bc30 Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Thu, 7 Mar 2019 19:19:10 -0700 Subject: [PATCH 5/9] Restore GLFW for macOS --- examples/hello_triangle_c/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/hello_triangle_c/CMakeLists.txt b/examples/hello_triangle_c/CMakeLists.txt index da89d317b7..3803b384ed 100644 --- a/examples/hello_triangle_c/CMakeLists.txt +++ b/examples/hello_triangle_c/CMakeLists.txt @@ -14,6 +14,7 @@ elseif(APPLE) add_compile_definitions(WGPU_TARGET=WGPU_TARGET_MACOS) set(OS_LIBRARIES "-framework Cocoa" "-framework CoreVideo" "-framework IOKit" "-framework QuartzCore") target_compile_options(${TARGET_NAME} PRIVATE -x objective-c) + set(GLFW_LIBRARY glfw) else(MSVC) target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra -pedantic) add_compile_definitions(WGPU_TARGET=WGPU_TARGET_LINUX) From 580e49f2cf0e00cc6f533442fff2130ae6791544 Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Thu, 7 Mar 2019 20:15:12 -0700 Subject: [PATCH 6/9] Start rendering C example again --- examples/hello_triangle_c/main.c | 66 ++++++++++---------------------- 1 file changed, 20 insertions(+), 46 deletions(-) diff --git a/examples/hello_triangle_c/main.c b/examples/hello_triangle_c/main.c index c851b99baa..fa6cb16b77 100644 --- a/examples/hello_triangle_c/main.c +++ b/examples/hello_triangle_c/main.c @@ -21,7 +21,6 @@ #endif #include -#define STAGES_LENGTH (2) #define BLEND_STATES_LENGTH (1) #define ATTACHMENTS_LENGTH (1) #define RENDER_PASS_ATTACHMENTS_LENGTH (1) @@ -75,8 +74,6 @@ int main() .entry_point = "main", }; - WGPUPipelineStageDescriptor stages[STAGES_LENGTH] = {vertex_stage, fragment_stage}; - WGPUBindGroupLayoutDescriptor bind_group_layout_desc = { .bindings = NULL, .bindings_length = 0, @@ -101,58 +98,35 @@ int main() .dst_factor = WGPUBlendFactor_Zero, .operation = WGPUBlendOperation_Add, }; - WGPUBlendStateDescriptor blend_state_0_desc = { - .blend_enabled = false, + WGPUColorStateDescriptor color_state_desc = { + .format = WGPUTextureFormat_Bgra8Unorm, .alpha = blend_alpha, .color = blend_color, .write_mask = WGPUColorWriteFlags_ALL, }; - WGPUBlendStateId blend_state_0 = wgpu_device_create_blend_state(device, &blend_state_0_desc); - WGPUBlendStateId blend_state[BLEND_STATES_LENGTH] = {blend_state_0}; - - WGPUStencilStateFaceDescriptor stencil_state_front = { - .compare = WGPUCompareFunction_Never, - .stencil_fail_op = WGPUStencilOperation_Keep, - .depth_fail_op = WGPUStencilOperation_Keep, - .pass_op = WGPUStencilOperation_Keep, + 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, }; - WGPUStencilStateFaceDescriptor stencil_state_back = { - .compare = WGPUCompareFunction_Never, - .stencil_fail_op = WGPUStencilOperation_Keep, - .depth_fail_op = WGPUStencilOperation_Keep, - .pass_op = WGPUStencilOperation_Keep, + WGPUVertexBufferStateDescriptor vertex_buffer_state = { + .index_format = WGPUIndexFormat_Uint16, + .vertex_buffers = NULL, + .vertex_buffers_count = 0, }; - WGPUDepthStencilStateDescriptor depth_stencil_state_desc = { - .depth_write_enabled = false, - .depth_compare = WGPUCompareFunction_Never, - .front = stencil_state_front, - .back = stencil_state_back, - .stencil_read_mask = 0, - .stencil_write_mask = 0, - }; - WGPUDepthStencilStateId depth_stencil_state = wgpu_device_create_depth_stencil_state(device, &depth_stencil_state_desc); - - WGPUAttachment attachments[ATTACHMENTS_LENGTH] = { - { - .format = WGPUTextureFormat_Bgra8Unorm, - .samples = 1, - }, - }; - WGPUColorStateDescriptor attachment_state = { - .color_attachments = attachments, - .color_attachments_length = ATTACHMENTS_LENGTH, - .depth_stencil_attachment = NULL, - }; - WGPURenderPipelineDescriptor render_pipeline_desc = { .layout = pipeline_layout, .vertex_stage = vertex_stage, .fragment_stage = fragment_stage, + .rasterization_state = rasterization_state, .primitive_topology = WGPUPrimitiveTopology_TriangleList, - .attachments_state = attachment_state, - .blend_states = blend_state, - .blend_states_length = BLEND_STATES_LENGTH, - .depth_stencil_state = depth_stencil_state, + .color_states = &color_state_desc, + .color_states_length = 1, + .depth_stencil_state = NULL, + .vertex_buffer_state = vertex_buffer_state, + .sample_count = 1, }; WGPURenderPipelineId render_pipeline = wgpu_device_create_render_pipeline(device, &render_pipeline_desc); @@ -172,7 +146,7 @@ int main() return 1; } - WGPUSurfaceId surface = NULL; + WGPUSurfaceId surface = {}; #if WGPU_TARGET == WGPU_TARGET_MACOS { @@ -224,7 +198,7 @@ int main() .color_attachments_length = RENDER_PASS_ATTACHMENTS_LENGTH, .depth_stencil_attachment = NULL, }; - WGPURenderPassId rpass = wgpu_command_buffer_begin_render_pass(cmd_encoder, rpass_desc); + 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); From b2c58c7791dba9612ac69ebe4d1011fce79c8c7b Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Thu, 7 Mar 2019 20:48:37 -0700 Subject: [PATCH 7/9] 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 fa6cb16b77..e44e8e8f39 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; } From 5322f3c6100614b4fb0d897c9308233486228e2f Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Fri, 8 Mar 2019 07:15:36 -0700 Subject: [PATCH 8/9] Reduce left indentation for nested structs --- .clang-format | 5 + examples/hello_triangle_c/main.c | 373 ++++++++++++++++--------------- 2 files changed, 193 insertions(+), 185 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000000..3adffa8ef5 --- /dev/null +++ b/.clang-format @@ -0,0 +1,5 @@ +IndentWidth: 4 + +# Reduce left indentation for nested structs +AlignOperands: false +AlignAfterOpenBracket: DontAlign diff --git a/examples/hello_triangle_c/main.c b/examples/hello_triangle_c/main.c index e44e8e8f39..6fe9b9092e 100644 --- a/examples/hello_triangle_c/main.c +++ b/examples/hello_triangle_c/main.c @@ -27,198 +27,201 @@ #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); - return (WGPUByteArray){ - .bytes = bytes, - .length = length, - }; + 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(); + WGPUInstanceId instance = wgpu_create_instance(); - WGPUAdapterId adapter = wgpu_instance_get_adapter( - instance, &(WGPUAdapterDescriptor){ - .power_preference = WGPUPowerPreference_LowPower, - }); - - WGPUDeviceId device = wgpu_adapter_create_device( - adapter, &(WGPUDeviceDescriptor){ - .extensions = - { - .anisotropic_filtering = false, - }, - }); - - WGPUShaderModuleId vertex_shader = wgpu_device_create_shader_module( - device, &(WGPUShaderModuleDescriptor){ - .code = read_file("./../../data/hello_triangle.vert.spv"), - }); - - WGPUShaderModuleId fragment_shader = wgpu_device_create_shader_module( - device, &(WGPUShaderModuleDescriptor){ - .code = read_file("./../../data/hello_triangle.frag.spv"), - }); - - 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}; - - 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, &(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; - } - - 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; - } - - 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); - } -#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); - } -#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); - } -#endif - - 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); - - 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, - .store_op = WGPUStoreOp_Store, - .clear_color = WGPUColor_GREEN, - }, - }; - - 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, + WGPUAdapterId adapter = wgpu_instance_get_adapter(instance, + &(WGPUAdapterDescriptor){ + .power_preference = WGPUPowerPreference_LowPower, }); - 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); + WGPUDeviceId device = wgpu_adapter_create_device(adapter, + &(WGPUDeviceDescriptor){ + .extensions = + { + .anisotropic_filtering = false, + }, + }); - glfwPollEvents(); - } + WGPUShaderModuleId vertex_shader = wgpu_device_create_shader_module(device, + &(WGPUShaderModuleDescriptor){ + .code = read_file("./../../data/hello_triangle.vert.spv"), + }); - glfwDestroyWindow(window); - glfwTerminate(); + WGPUShaderModuleId fragment_shader = + wgpu_device_create_shader_module(device, + &(WGPUShaderModuleDescriptor){ + .code = read_file("./../../data/hello_triangle.frag.spv"), + }); - return 0; + 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}; + + 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, + &(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; + } + + 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; + } + + 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); + } +#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); + } +#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); + } +#endif + + 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); + + 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, + .store_op = WGPUStoreOp_Store, + .clear_color = WGPUColor_GREEN, + }, + }; + + 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, + }); + + 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); + + glfwPollEvents(); + } + + glfwDestroyWindow(window); + glfwTerminate(); + + return 0; } From 102ebdb158b9395bbbe361c6ebde176fb8c289c1 Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Sat, 9 Mar 2019 12:38:56 -0700 Subject: [PATCH 9/9] Add CI for C example --- .travis.yml | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 97b2c3532e..502bd1b074 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,40 @@ language: rust -rust: - - stable - - nightly + +matrix: + include: + # Linux 64bit + - os: linux + rust: stable + compiler: gcc + - os: linux + rust: nightly + compiler: gcc + + # macOS 64bit + - env: MACOSX_DEPLOYMENT_TARGET=10.9 + os: osx + rust: stable + osx_image: xcode9 + compiler: clang + #- env: MACOSX_DEPLOYMENT_TARGET=10.9 + # os: osx + # rust: nightly + # osx_image: xcode9 + # compiler: clang + + # iPhoneOS 64bit + #- env: TARGET=aarch64-apple-ios + # os: osx + # osx_image: xcode9 + # rust: nightly + + # Windows 64bit + #- os: windows + # rust: stable branches: except: - - staging.tmp + - staging.tmp before_install: # Do not run bors builds against the nightly compiler. @@ -15,3 +44,4 @@ before_install: script: - cargo test - cargo check --manifest-path wgpu-native/Cargo.toml + - if [[ $TRAVIS_OS_NAME == "osx" ]]; then (brew update && brew upgrade cmake && brew install glfw3 && cd wgpu-native && cargo build --features=local,gfx-backend-metal && cd ../examples/hello_triangle_c && mkdir build && cd build && cmake .. && make); fi