mirror of
https://github.com/gfx-rs/wgpu.git
synced 2024-11-25 16:24:24 +00:00
Create shader modules from C
This commit is contained in:
parent
08ad0f40ed
commit
211189a090
@ -1,6 +1,22 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "./../../wgpu-bindings/wgpu.h"
|
#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()
|
int main()
|
||||||
{
|
{
|
||||||
WGPUInstanceId instance = wgpu_create_instance();
|
WGPUInstanceId instance = wgpu_create_instance();
|
||||||
@ -14,10 +30,13 @@ int main()
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
WGPUDeviceId device = wgpu_adapter_create_device(adapter, device_desc);
|
WGPUDeviceId device = wgpu_adapter_create_device(adapter, device_desc);
|
||||||
/*WGPUShaderModuleDescriptor vs_desc = {
|
WGPUShaderModuleDescriptor vs_desc = {
|
||||||
.code = "",
|
.code = read_file("./../data/hello_triangle.vert.spv"),
|
||||||
};
|
};
|
||||||
WGPUShaderModuleId _vs = wgpu_device_create_shader_module(device, vs_desc);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -17,16 +17,24 @@ fn main() {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
let vs_bytes = include_bytes!("./../data/hello_triangle.vert.spv");
|
||||||
let _vs = wgpu_device_create_shader_module(
|
let _vs = wgpu_device_create_shader_module(
|
||||||
device,
|
device,
|
||||||
ShaderModuleDescriptor {
|
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(
|
let _fs = wgpu_device_create_shader_module(
|
||||||
device,
|
device,
|
||||||
ShaderModuleDescriptor {
|
ShaderModuleDescriptor {
|
||||||
code: include_bytes!("./../data/hello_triangle.frag.spv"),
|
code: ByteArray {
|
||||||
|
bytes: fs_bytes.as_ptr(),
|
||||||
|
length: fs_bytes.len(),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,6 @@ typedef enum {
|
|||||||
WGPUPowerPreference_HighPerformance = 2,
|
WGPUPowerPreference_HighPerformance = 2,
|
||||||
} WGPUPowerPreference;
|
} WGPUPowerPreference;
|
||||||
|
|
||||||
typedef struct WGPUShaderModuleDescriptor WGPUShaderModuleDescriptor;
|
|
||||||
|
|
||||||
typedef uint32_t WGPUId;
|
typedef uint32_t WGPUId;
|
||||||
|
|
||||||
typedef WGPUId WGPUDeviceId;
|
typedef WGPUId WGPUDeviceId;
|
||||||
@ -34,6 +32,15 @@ typedef WGPUId WGPUInstanceId;
|
|||||||
|
|
||||||
typedef WGPUId WGPUShaderModuleId;
|
typedef WGPUId WGPUShaderModuleId;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const uint8_t *bytes;
|
||||||
|
uintptr_t length;
|
||||||
|
} WGPUByteArray;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
WGPUByteArray code;
|
||||||
|
} WGPUShaderModuleDescriptor;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
WGPUPowerPreference power_preference;
|
WGPUPowerPreference power_preference;
|
||||||
} WGPUAdapterDescriptor;
|
} WGPUAdapterDescriptor;
|
||||||
|
@ -38,7 +38,11 @@ pub extern "C" fn wgpu_device_create_shader_module(
|
|||||||
) -> ShaderModuleId {
|
) -> ShaderModuleId {
|
||||||
let device_registry = registry::DEVICE_REGISTRY.lock().unwrap();
|
let device_registry = registry::DEVICE_REGISTRY.lock().unwrap();
|
||||||
let device = device_registry.get(device_id).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
|
registry::SHADER_MODULE_REGISTRY
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -74,7 +74,10 @@ pub extern "C" fn wgpu_instance_get_adapter(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[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 mut adapter_registry = registry::ADAPTER_REGISTRY.lock().unwrap();
|
||||||
let adapter = adapter_registry.get_mut(adapter_id).unwrap();
|
let adapter = adapter_registry.get_mut(adapter_id).unwrap();
|
||||||
let (device, queue_group) = adapter.open_with::<_, hal::General>(1, |_qf| true).unwrap();
|
let (device, queue_group) = adapter.open_with::<_, hal::General>(1, |_qf| true).unwrap();
|
||||||
|
@ -60,6 +60,12 @@ pub struct Extent3d {
|
|||||||
pub depth: f32,
|
pub depth: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
pub struct ByteArray {
|
||||||
|
pub bytes: *const u8,
|
||||||
|
pub length: usize,
|
||||||
|
}
|
||||||
|
|
||||||
pub type InstanceId = Id;
|
pub type InstanceId = Id;
|
||||||
pub(crate) type InstanceHandle = back::Instance;
|
pub(crate) type InstanceHandle = back::Instance;
|
||||||
pub type AdapterId = Id;
|
pub type AdapterId = Id;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use hal;
|
use hal;
|
||||||
use resource;
|
use resource;
|
||||||
|
|
||||||
use {BlendStateId, DepthStencilStateId, PipelineLayoutId};
|
use {BlendStateId, ByteArray, DepthStencilStateId, PipelineLayoutId};
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub enum BlendFactor {
|
pub enum BlendFactor {
|
||||||
@ -141,8 +141,8 @@ pub struct InputState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct ShaderModuleDescriptor<'a> {
|
pub struct ShaderModuleDescriptor {
|
||||||
pub code: &'a [u8],
|
pub code: ByteArray,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
@ -162,8 +162,8 @@ pub enum ShaderStage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct PipelineStageDescriptor<'a> {
|
pub struct PipelineStageDescriptor {
|
||||||
pub module: ShaderModuleDescriptor<'a>,
|
pub module: ShaderModuleDescriptor,
|
||||||
pub stage: ShaderStage,
|
pub stage: ShaderStage,
|
||||||
pub entry_point: *const ::std::os::raw::c_char,
|
pub entry_point: *const ::std::os::raw::c_char,
|
||||||
}
|
}
|
||||||
@ -171,7 +171,7 @@ pub struct PipelineStageDescriptor<'a> {
|
|||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct ComputePipelineDescriptor<'a> {
|
pub struct ComputePipelineDescriptor<'a> {
|
||||||
pub layout: PipelineLayoutId,
|
pub layout: PipelineLayoutId,
|
||||||
pub stages: &'a [PipelineStageDescriptor<'a>],
|
pub stages: &'a [PipelineStageDescriptor],
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ComputePipeline {
|
pub struct ComputePipeline {
|
||||||
@ -190,7 +190,7 @@ pub enum PrimitiveTopology {
|
|||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct RenderPipelineDescriptor<'a> {
|
pub struct RenderPipelineDescriptor<'a> {
|
||||||
pub layout: PipelineLayoutId,
|
pub layout: PipelineLayoutId,
|
||||||
pub stages: &'a [PipelineStageDescriptor<'a>],
|
pub stages: &'a [PipelineStageDescriptor],
|
||||||
pub primitive_topology: PrimitiveTopology,
|
pub primitive_topology: PrimitiveTopology,
|
||||||
pub blend_state: &'a [BlendStateId],
|
pub blend_state: &'a [BlendStateId],
|
||||||
pub depth_stencil_state: DepthStencilStateId,
|
pub depth_stencil_state: DepthStencilStateId,
|
||||||
|
Loading…
Reference in New Issue
Block a user