diff --git a/README.md b/README.md index e4aa84259..768c6c05c 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,20 @@ Supported platforms: ## Usage -This repository contains C-language examples that link to the native library targets and perform basic rendering and computation. The prerequisites are a C compiler and GLFW 3 for running the examples. +This repository contains C-language examples that link to the native library targets and perform basic rendering and computation. + +### Prerequisites + - C compiler + - GLFW 3 + - Rust nightly toolchain + ```bash + rustup toolchain install nightly + ``` + - [Cbindgen](https://github.com/eqrion/cbindgen) + ```bash + cargo install cbindgen + ``` + ### Running an example The examples are located under the [examples](examples) directory. We are using a [Makefile](Makefile) for running the examples. diff --git a/examples/compute/CMakeLists.txt b/examples/compute/CMakeLists.txt index 2a64186e6..e8558c774 100644 --- a/examples/compute/CMakeLists.txt +++ b/examples/compute/CMakeLists.txt @@ -18,7 +18,7 @@ project(compute) set(TARGET_NAME compute) -add_executable(compute main.c) +add_executable(compute main.c ../framework.c) if(MSVC) add_definitions(-DWGPU_TARGET=WGPU_TARGET_WINDOWS) @@ -47,5 +47,7 @@ find_library(WGPU_LIBRARY wgpu_native ) target_include_directories(${TARGET_NAME} PUBLIC $ENV{GLFW3_INCLUDE_DIR}) +target_include_directories(${TARGET_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../ffi) +target_include_directories(${TARGET_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../) -target_link_libraries(${TARGET_NAME} glfw ${WGPU_LIBRARY} ${OS_LIBRARIES}) \ No newline at end of file +target_link_libraries(${TARGET_NAME} glfw ${WGPU_LIBRARY} ${OS_LIBRARIES}) diff --git a/examples/compute/main.c b/examples/compute/main.c index c1023ddb7..09740c47b 100644 --- a/examples/compute/main.c +++ b/examples/compute/main.c @@ -1,43 +1,22 @@ -#include "./../../ffi/wgpu.h" +#ifndef WGPU_H +#define WGPU_H +#include "wgpu.h" +#endif + +#include "framework.h" + #include #include #define BINDINGS_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); - return (WGPUByteArray){ - .bytes = bytes, - .length = length, - }; -} - -void read_buffer_map( - WGPUBufferMapAsyncStatus status, - const uint8_t *data, - uint8_t *userdata) { - if (status == WGPUBufferMapAsyncStatus_Success) { - uint32_t *times = (uint32_t *) data; - printf("Times: [%d, %d, %d, %d]", - times[0], - times[1], - times[2], - times[3]); - } -} - int main( int argc, char *argv[]) { if (argc != 5) { + printf("You must pass 4 positive integers!"); return 0; } @@ -61,18 +40,15 @@ int main( WGPUDeviceId device = wgpu_adapter_request_device(adapter, &(WGPUDeviceDescriptor){ - .extensions = { - .anisotropic_filtering = false, - }, + .extensions = NULL }); uint8_t *staging_memory; WGPUBufferId staging_buffer = wgpu_device_create_buffer_mapped(device, - &(WGPUBufferDescriptor){ - .size = size, - .usage = WGPUBufferUsage_MAP_READ | WGPUBufferUsage_TRANSFER_DST | - WGPUBufferUsage_TRANSFER_SRC}, + &(WGPUBufferDescriptor){ + .size = size, + .usage = WGPUBufferUsage_MAP_READ}, &staging_memory); memcpy((uint32_t *) staging_memory, numbers, size); @@ -82,8 +58,7 @@ int main( WGPUBufferId storage_buffer = wgpu_device_create_buffer(device, &(WGPUBufferDescriptor){ .size = size, - .usage = WGPUBufferUsage_STORAGE | WGPUBufferUsage_TRANSFER_DST | - WGPUBufferUsage_TRANSFER_SRC}); + .usage = WGPUBufferUsage_STORAGE}); WGPUBindGroupLayoutId bind_group_layout = wgpu_device_create_bind_group_layout(device, @@ -91,49 +66,35 @@ int main( .bindings = &(WGPUBindGroupLayoutBinding){ .binding = 0, .visibility = WGPUShaderStage_COMPUTE, - .ty = WGPUBindingType_StorageBuffer - }, - .bindings_length = BINDINGS_LENGTH - } - ); + .ty = WGPUBindingType_StorageBuffer}, + .bindings_length = BINDINGS_LENGTH}); WGPUBindingResource resource = { - .tag = WGPUBindingResource_Buffer, + .tag = WGPUBindingResource_Buffer, .buffer = (WGPUBufferBinding){ - .buffer = storage_buffer, + .buffer = storage_buffer, .size = size, - .offset = 0 - } - }; + .offset = 0}}; WGPUBindGroupId bind_group = wgpu_device_create_bind_group(device, - &(WGPUBindGroupDescriptor){ - .layout = bind_group_layout, - .bindings = - &(WGPUBindGroupBinding){ - .binding = 0, - .resource = resource - }, - .bindings_length = BINDINGS_LENGTH - } - ); + &(WGPUBindGroupDescriptor){.layout = bind_group_layout, + .bindings = &(WGPUBindGroupBinding){ + .binding = 0, + .resource = resource}, + .bindings_length = BINDINGS_LENGTH}); WGPUBindGroupLayoutId bind_group_layouts[BIND_GROUP_LAYOUTS_LENGTH] = { - bind_group_layout - }; + 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 - }); + wgpu_device_create_pipeline_layout(device, + &(WGPUPipelineLayoutDescriptor){ + .bind_group_layouts = bind_group_layouts, + .bind_group_layouts_length = BIND_GROUP_LAYOUTS_LENGTH}); WGPUShaderModuleId shader_module = wgpu_device_create_shader_module(device, &(WGPUShaderModuleDescriptor){ - .code = read_file("./../../data/collatz.comp.spv") - } - ); + .code = read_file("./../../data/collatz.comp.spv")}); WGPUComputePipelineId compute_pipeline = wgpu_device_create_compute_pipeline(device, @@ -142,15 +103,12 @@ int main( .compute_stage = (WGPUPipelineStageDescriptor){ .module = shader_module, .entry_point = "main" - } - } - ); + }}); WGPUCommandEncoderId encoder = wgpu_device_create_command_encoder( device, &(WGPUCommandEncoderDescriptor){ .todo = 0 - } - ); + }); wgpu_command_buffer_copy_buffer_to_buffer( encoder, staging_buffer, 0, storage_buffer, 0, size); diff --git a/examples/framework.c b/examples/framework.c new file mode 100644 index 000000000..58096b305 --- /dev/null +++ b/examples/framework.c @@ -0,0 +1,35 @@ +#ifndef WGPU_H +#define WGPU_H +#include "wgpu.h" +#endif + +#include +#include + +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, + }; +} + +void read_buffer_map( + WGPUBufferMapAsyncStatus status, + const uint8_t *data, + uint8_t *userdata) { + if (status == WGPUBufferMapAsyncStatus_Success) { + uint32_t *times = (uint32_t *) data; + printf("Times: [%d, %d, %d, %d]", + times[0], + times[1], + times[2], + times[3]); + } +} diff --git a/examples/framework.h b/examples/framework.h new file mode 100644 index 000000000..6130df310 --- /dev/null +++ b/examples/framework.h @@ -0,0 +1,11 @@ +#ifndef WGPU_H +#define WGPU_H +#include "wgpu.h" +#endif + +WGPUByteArray read_file(const char *name); + +void read_buffer_map( + WGPUBufferMapAsyncStatus status, + const uint8_t *data, + uint8_t *userdata); diff --git a/examples/triangle/CMakeLists.txt b/examples/triangle/CMakeLists.txt index 44b211b42..0cc07c370 100644 --- a/examples/triangle/CMakeLists.txt +++ b/examples/triangle/CMakeLists.txt @@ -18,7 +18,7 @@ project(triangle) set(TARGET_NAME triangle) -add_executable(triangle main.c) +add_executable(triangle main.c ../framework.c) if(MSVC) add_definitions(-DWGPU_TARGET=WGPU_TARGET_WINDOWS) @@ -47,5 +47,7 @@ find_library(WGPU_LIBRARY wgpu_native ) target_include_directories(${TARGET_NAME} PUBLIC $ENV{GLFW3_INCLUDE_DIR}) +target_include_directories(${TARGET_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../ffi) +target_include_directories(${TARGET_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../) -target_link_libraries(${TARGET_NAME} glfw ${WGPU_LIBRARY} ${OS_LIBRARIES}) \ No newline at end of file +target_link_libraries(${TARGET_NAME} glfw ${WGPU_LIBRARY} ${OS_LIBRARIES}) diff --git a/examples/triangle/main.c b/examples/triangle/main.c index de9ac8075..2b7eea6e1 100644 --- a/examples/triangle/main.c +++ b/examples/triangle/main.c @@ -1,4 +1,9 @@ -#include "./../../ffi/wgpu.h" +#ifndef WGPU_H +#define WGPU_H +#include "wgpu.h" +#endif + +#include "framework.h" #include #include @@ -27,20 +32,6 @@ #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); - return (WGPUByteArray){ - .bytes = bytes, - .length = length, - }; -} - int main() { WGPUInstanceId instance = wgpu_create_instance();