diff --git a/.gitignore b/.gitignore index db4a59b65..621da21d8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ **/*.rs.bk #Cargo.lock .vscode +build diff --git a/examples/Cargo.toml b/examples/Cargo.toml index ae80337c4..12f60412d 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -9,7 +9,7 @@ publish = false [[bin]] name = "hello_triangle" -path = "hello_triangle/main.rs" +path = "hello_triangle_rust/main.rs" [features] default = [] diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 000000000..c78b661b4 --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,14 @@ +all: hello_world_c + +CC=gcc +CFLAGS=-I. +DEPS=./../wgpu-bindings/wgpu.h +OUTDIR=./build +LINK_ARGS=-L ./../target/debug -lwgpu_native + +%.o: %.c $(DEPS) + $(CC) $(LINK_ARGS) -c -o $(OUTDIR)/$@ $< $(CFLAGS) + +hello_world_c: hello_triangle_c/main.c + mkdir -p $(OUTDIR) + $(CC) $(LINK_ARGS) -o $(OUTDIR)/$@ $^ $(CFLAGS) diff --git a/examples/hello_triangle/shaders/hello_triangle.frag b/examples/data/hello_triangle.frag similarity index 100% rename from examples/hello_triangle/shaders/hello_triangle.frag rename to examples/data/hello_triangle.frag diff --git a/examples/hello_triangle/shaders/hello_triangle.frag.spv b/examples/data/hello_triangle.frag.spv similarity index 100% rename from examples/hello_triangle/shaders/hello_triangle.frag.spv rename to examples/data/hello_triangle.frag.spv diff --git a/examples/hello_triangle/shaders/hello_triangle.vert b/examples/data/hello_triangle.vert similarity index 100% rename from examples/hello_triangle/shaders/hello_triangle.vert rename to examples/data/hello_triangle.vert diff --git a/examples/hello_triangle/shaders/hello_triangle.vert.spv b/examples/data/hello_triangle.vert.spv similarity index 100% rename from examples/hello_triangle/shaders/hello_triangle.vert.spv rename to examples/data/hello_triangle.vert.spv diff --git a/examples/hello_triangle/main.rs b/examples/hello_triangle/main.rs deleted file mode 100644 index 3025ea6f8..000000000 --- a/examples/hello_triangle/main.rs +++ /dev/null @@ -1,31 +0,0 @@ -extern crate wgpu_native as wgn; - -fn main() { - let instance = wgn::create_instance(); - let adapter = wgn::instance_get_adapter( - instance, - wgn::AdapterDescriptor { - power_preference: wgn::PowerPreference::LowPower, - }, - ); - let device = wgn::adapter_create_device( - adapter, - wgn::DeviceDescriptor { - extensions: wgn::Extensions { - anisotropic_filtering: false, - }, - }, - ); - let _vs = wgn::device_create_shader_module( - device, - wgn::ShaderModuleDescriptor { - code: include_bytes!("./shaders/hello_triangle.vert.spv"), - }, - ); - let _fs = wgn::device_create_shader_module( - device, - wgn::ShaderModuleDescriptor { - code: include_bytes!("./shaders/hello_triangle.frag.spv"), - }, - ); -} diff --git a/examples/hello_triangle_c/main.c b/examples/hello_triangle_c/main.c new file mode 100644 index 000000000..7e06a3a9a --- /dev/null +++ b/examples/hello_triangle_c/main.c @@ -0,0 +1,23 @@ +#include +#include "./../../wgpu-bindings/wgpu.h" + +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); + /*WGPUShaderModuleDescriptor vs_desc = { + .code = "", + }; + WGPUShaderModuleId _vs = wgpu_device_create_shader_module(device, vs_desc); + */ + return 0; +} diff --git a/examples/hello_triangle_rust/main.rs b/examples/hello_triangle_rust/main.rs new file mode 100644 index 000000000..63b0b7ab8 --- /dev/null +++ b/examples/hello_triangle_rust/main.rs @@ -0,0 +1,32 @@ +extern crate wgpu_native; +use wgpu_native::*; + +fn main() { + let instance = wgpu_create_instance(); + let adapter = wgpu_instance_get_adapter( + instance, + AdapterDescriptor { + power_preference: PowerPreference::LowPower, + }, + ); + let device = wgpu_adapter_create_device( + adapter, + DeviceDescriptor { + extensions: Extensions { + anisotropic_filtering: false, + }, + }, + ); + let _vs = wgpu_device_create_shader_module( + device, + ShaderModuleDescriptor { + code: include_bytes!("./../data/hello_triangle.vert.spv"), + }, + ); + let _fs = wgpu_device_create_shader_module( + device, + ShaderModuleDescriptor { + code: include_bytes!("./../data/hello_triangle.frag.spv"), + }, + ); +} diff --git a/wgpu-bindings/bindings.h b/wgpu-bindings/bindings.h deleted file mode 100644 index fcf9468ec..000000000 --- a/wgpu-bindings/bindings.h +++ /dev/null @@ -1,51 +0,0 @@ -#include -#include -#include - -typedef enum { - Default = 0, - LowPower = 1, - HighPerformance = 2, -} PowerPreference; - -typedef struct ShaderModuleDescriptor ShaderModuleDescriptor; - -typedef uint32_t Id; - -typedef Id DeviceId; - -typedef Id AdapterId; - -typedef struct { - bool anisotropic_filtering; -} Extensions; - -typedef struct { - Extensions extensions; -} DeviceDescriptor; - -typedef Id ComputePassId; - -typedef Id RenderPassId; - -typedef Id CommandBufferId; - -typedef Id InstanceId; - -typedef Id ShaderModuleId; - -typedef struct { - PowerPreference power_preference; -} AdapterDescriptor; - -DeviceId adapter_create_device(AdapterId adapter_id, DeviceDescriptor desc); - -ComputePassId command_buffer_begin_compute_pass(void); - -RenderPassId command_buffer_begin_render_pass(CommandBufferId command_buffer); - -InstanceId create_instance(void); - -ShaderModuleId device_create_shader_module(DeviceId device_id, ShaderModuleDescriptor desc); - -AdapterId instance_get_adapter(InstanceId instance_id, AdapterDescriptor desc); diff --git a/wgpu-bindings/src/main.rs b/wgpu-bindings/src/main.rs index 627f0aeaf..9d31ef501 100644 --- a/wgpu-bindings/src/main.rs +++ b/wgpu-bindings/src/main.rs @@ -6,10 +6,20 @@ fn main() { let mut crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); crate_dir.push("../wgpu-native"); + let config = cbindgen::Config { + enumeration: cbindgen::EnumConfig { + prefix_with_name: true, + ..Default::default() + }, + ..Default::default() + }; + cbindgen::Builder::new() .with_crate(crate_dir) + .with_config(config) .with_language(cbindgen::Language::C) + .with_item_prefix("WGPU") .generate() .expect("Unable to generate bindings") - .write_to_file("bindings.h"); + .write_to_file("wgpu.h"); } diff --git a/wgpu-bindings/wgpu.h b/wgpu-bindings/wgpu.h new file mode 100644 index 000000000..802227d9e --- /dev/null +++ b/wgpu-bindings/wgpu.h @@ -0,0 +1,52 @@ +#include +#include +#include + +typedef enum { + WGPUPowerPreference_Default = 0, + WGPUPowerPreference_LowPower = 1, + WGPUPowerPreference_HighPerformance = 2, +} WGPUPowerPreference; + +typedef struct WGPUShaderModuleDescriptor WGPUShaderModuleDescriptor; + +typedef uint32_t WGPUId; + +typedef WGPUId WGPUDeviceId; + +typedef WGPUId WGPUAdapterId; + +typedef struct { + bool anisotropic_filtering; +} WGPUExtensions; + +typedef struct { + WGPUExtensions extensions; +} WGPUDeviceDescriptor; + +typedef WGPUId WGPUComputePassId; + +typedef WGPUId WGPURenderPassId; + +typedef WGPUId WGPUCommandBufferId; + +typedef WGPUId WGPUInstanceId; + +typedef WGPUId WGPUShaderModuleId; + +typedef struct { + WGPUPowerPreference power_preference; +} WGPUAdapterDescriptor; + +WGPUDeviceId wgpu_adapter_create_device(WGPUAdapterId adapter_id, WGPUDeviceDescriptor desc); + +WGPUComputePassId wgpu_command_buffer_begin_compute_pass(void); + +WGPURenderPassId wgpu_command_buffer_begin_render_pass(WGPUCommandBufferId command_buffer); + +WGPUInstanceId wgpu_create_instance(void); + +WGPUShaderModuleId wgpu_device_create_shader_module(WGPUDeviceId device_id, + WGPUShaderModuleDescriptor desc); + +WGPUAdapterId wgpu_instance_get_adapter(WGPUInstanceId instance_id, WGPUAdapterDescriptor desc); diff --git a/wgpu-native/src/command/mod.rs b/wgpu-native/src/command/mod.rs index 5a90af140..e70a017c1 100644 --- a/wgpu-native/src/command/mod.rs +++ b/wgpu-native/src/command/mod.rs @@ -72,13 +72,13 @@ pub struct CommandBuffer { pub struct CommandBufferDescriptor; #[no_mangle] -pub extern "C" fn command_buffer_begin_render_pass( +pub extern "C" fn wgpu_command_buffer_begin_render_pass( command_buffer: CommandBufferId, ) -> RenderPassId { unimplemented!() } #[no_mangle] -pub extern "C" fn command_buffer_begin_compute_pass() -> ComputePassId { +pub extern "C" fn wgpu_command_buffer_begin_compute_pass() -> ComputePassId { unimplemented!() } diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 37ef45d64..22c418bc7 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -32,7 +32,7 @@ pub struct ShaderModule { } #[no_mangle] -pub extern "C" fn device_create_shader_module( +pub extern "C" fn wgpu_device_create_shader_module( device_id: DeviceId, desc: pipeline::ShaderModuleDescriptor, ) -> ShaderModuleId { diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index d9943958e..8b8dee710 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -26,7 +26,7 @@ pub struct DeviceDescriptor { } #[no_mangle] -pub extern "C" fn create_instance() -> InstanceId { +pub extern "C" fn wgpu_create_instance() -> InstanceId { #[cfg(any( feature = "gfx-backend-vulkan", feature = "gfx-backend-dx12", @@ -48,7 +48,7 @@ pub extern "C" fn create_instance() -> InstanceId { } #[no_mangle] -pub extern "C" fn instance_get_adapter( +pub extern "C" fn wgpu_instance_get_adapter( instance_id: InstanceId, desc: AdapterDescriptor, ) -> AdapterId { @@ -74,7 +74,7 @@ pub extern "C" fn instance_get_adapter( } #[no_mangle] -pub extern "C" fn adapter_create_device(adapter_id: AdapterId, desc: DeviceDescriptor) -> DeviceId { +pub extern "C" fn wgpu_adapter_create_device(adapter_id: AdapterId, desc: DeviceDescriptor) -> DeviceId { let mut adapter_registry = registry::ADAPTER_REGISTRY.lock().unwrap(); let adapter = adapter_registry.get_mut(adapter_id).unwrap(); let (device, queue_group) = adapter.open_with::<_, hal::General>(1, |_qf| true).unwrap();