diff --git a/examples/hello_triangle_c/main.c b/examples/hello_triangle_c/main.c index 7e06a3a9a..ef451a0b4 100644 --- a/examples/hello_triangle_c/main.c +++ b/examples/hello_triangle_c/main.c @@ -1,6 +1,22 @@ #include #include "./../../wgpu-bindings/wgpu.h" +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; +} + int main() { WGPUInstanceId instance = wgpu_create_instance(); @@ -14,10 +30,13 @@ int main() }, }; WGPUDeviceId device = wgpu_adapter_create_device(adapter, device_desc); - /*WGPUShaderModuleDescriptor vs_desc = { - .code = "", + WGPUShaderModuleDescriptor vs_desc = { + .code = read_file("./../data/hello_triangle.vert.spv"), }; WGPUShaderModuleId _vs = wgpu_device_create_shader_module(device, vs_desc); - */ + WGPUShaderModuleDescriptor fs_desc = { + .code = read_file("./../data/hello_triangle.frag.spv"), + }; + WGPUShaderModuleId _fs = wgpu_device_create_shader_module(device, fs_desc); return 0; } diff --git a/examples/hello_triangle_rust/main.rs b/examples/hello_triangle_rust/main.rs index 63b0b7ab8..313e68029 100644 --- a/examples/hello_triangle_rust/main.rs +++ b/examples/hello_triangle_rust/main.rs @@ -17,16 +17,24 @@ fn main() { }, }, ); + let vs_bytes = include_bytes!("./../data/hello_triangle.vert.spv"); let _vs = wgpu_device_create_shader_module( device, ShaderModuleDescriptor { - code: include_bytes!("./../data/hello_triangle.vert.spv"), + code: ByteArray { + bytes: vs_bytes.as_ptr(), + length: vs_bytes.len(), + }, }, ); + let fs_bytes = include_bytes!("./../data/hello_triangle.frag.spv"); let _fs = wgpu_device_create_shader_module( device, ShaderModuleDescriptor { - code: include_bytes!("./../data/hello_triangle.frag.spv"), + code: ByteArray { + bytes: fs_bytes.as_ptr(), + length: fs_bytes.len(), + }, }, ); } diff --git a/wgpu-bindings/wgpu.h b/wgpu-bindings/wgpu.h index 802227d9e..93499b6c8 100644 --- a/wgpu-bindings/wgpu.h +++ b/wgpu-bindings/wgpu.h @@ -8,8 +8,6 @@ typedef enum { WGPUPowerPreference_HighPerformance = 2, } WGPUPowerPreference; -typedef struct WGPUShaderModuleDescriptor WGPUShaderModuleDescriptor; - typedef uint32_t WGPUId; typedef WGPUId WGPUDeviceId; @@ -34,6 +32,15 @@ typedef WGPUId WGPUInstanceId; typedef WGPUId WGPUShaderModuleId; +typedef struct { + const uint8_t *bytes; + uintptr_t length; +} WGPUByteArray; + +typedef struct { + WGPUByteArray code; +} WGPUShaderModuleDescriptor; + typedef struct { WGPUPowerPreference power_preference; } WGPUAdapterDescriptor; diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 22c418bc7..c630725c7 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -38,7 +38,11 @@ pub extern "C" fn wgpu_device_create_shader_module( ) -> ShaderModuleId { let device_registry = registry::DEVICE_REGISTRY.lock().unwrap(); let device = device_registry.get(device_id).unwrap(); - let shader = device.device.create_shader_module(desc.code).unwrap(); + let shader = device + .device + .create_shader_module(unsafe { + ::std::slice::from_raw_parts(desc.code.bytes, desc.code.length) + }).unwrap(); registry::SHADER_MODULE_REGISTRY .lock() .unwrap() diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index 8b8dee710..5c3255fa0 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -74,7 +74,10 @@ pub extern "C" fn wgpu_instance_get_adapter( } #[no_mangle] -pub extern "C" fn wgpu_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(); diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index d53af324c..86f8c111a 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -60,6 +60,12 @@ pub struct Extent3d { pub depth: f32, } +#[repr(C)] +pub struct ByteArray { + pub bytes: *const u8, + pub length: usize, +} + pub type InstanceId = Id; pub(crate) type InstanceHandle = back::Instance; pub type AdapterId = Id; diff --git a/wgpu-native/src/pipeline.rs b/wgpu-native/src/pipeline.rs index 54d3626ff..0d94aa2ac 100644 --- a/wgpu-native/src/pipeline.rs +++ b/wgpu-native/src/pipeline.rs @@ -1,7 +1,7 @@ use hal; use resource; -use {BlendStateId, DepthStencilStateId, PipelineLayoutId}; +use {BlendStateId, ByteArray, DepthStencilStateId, PipelineLayoutId}; #[repr(C)] pub enum BlendFactor { @@ -141,8 +141,8 @@ pub struct InputState { } #[repr(C)] -pub struct ShaderModuleDescriptor<'a> { - pub code: &'a [u8], +pub struct ShaderModuleDescriptor { + pub code: ByteArray, } #[repr(C)] @@ -162,8 +162,8 @@ pub enum ShaderStage { } #[repr(C)] -pub struct PipelineStageDescriptor<'a> { - pub module: ShaderModuleDescriptor<'a>, +pub struct PipelineStageDescriptor { + pub module: ShaderModuleDescriptor, pub stage: ShaderStage, pub entry_point: *const ::std::os::raw::c_char, } @@ -171,7 +171,7 @@ pub struct PipelineStageDescriptor<'a> { #[repr(C)] pub struct ComputePipelineDescriptor<'a> { pub layout: PipelineLayoutId, - pub stages: &'a [PipelineStageDescriptor<'a>], + pub stages: &'a [PipelineStageDescriptor], } pub struct ComputePipeline { @@ -190,7 +190,7 @@ pub enum PrimitiveTopology { #[repr(C)] pub struct RenderPipelineDescriptor<'a> { pub layout: PipelineLayoutId, - pub stages: &'a [PipelineStageDescriptor<'a>], + pub stages: &'a [PipelineStageDescriptor], pub primitive_topology: PrimitiveTopology, pub blend_state: &'a [BlendStateId], pub depth_stencil_state: DepthStencilStateId,