mirror of
https://github.com/gfx-rs/wgpu.git
synced 2024-11-21 22:33:49 +00:00
Create C example
This commit is contained in:
parent
d94d45cd80
commit
08ad0f40ed
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@
|
|||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
#Cargo.lock
|
#Cargo.lock
|
||||||
.vscode
|
.vscode
|
||||||
|
build
|
||||||
|
@ -9,7 +9,7 @@ publish = false
|
|||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "hello_triangle"
|
name = "hello_triangle"
|
||||||
path = "hello_triangle/main.rs"
|
path = "hello_triangle_rust/main.rs"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
|
14
examples/Makefile
Normal file
14
examples/Makefile
Normal file
@ -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)
|
@ -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"),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
23
examples/hello_triangle_c/main.c
Normal file
23
examples/hello_triangle_c/main.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#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;
|
||||||
|
}
|
32
examples/hello_triangle_rust/main.rs
Normal file
32
examples/hello_triangle_rust/main.rs
Normal file
@ -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"),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
@ -1,51 +0,0 @@
|
|||||||
#include <stdint.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
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);
|
|
@ -6,10 +6,20 @@ fn main() {
|
|||||||
let mut crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
let mut crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||||
crate_dir.push("../wgpu-native");
|
crate_dir.push("../wgpu-native");
|
||||||
|
|
||||||
|
let config = cbindgen::Config {
|
||||||
|
enumeration: cbindgen::EnumConfig {
|
||||||
|
prefix_with_name: true,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
cbindgen::Builder::new()
|
cbindgen::Builder::new()
|
||||||
.with_crate(crate_dir)
|
.with_crate(crate_dir)
|
||||||
|
.with_config(config)
|
||||||
.with_language(cbindgen::Language::C)
|
.with_language(cbindgen::Language::C)
|
||||||
|
.with_item_prefix("WGPU")
|
||||||
.generate()
|
.generate()
|
||||||
.expect("Unable to generate bindings")
|
.expect("Unable to generate bindings")
|
||||||
.write_to_file("bindings.h");
|
.write_to_file("wgpu.h");
|
||||||
}
|
}
|
||||||
|
52
wgpu-bindings/wgpu.h
Normal file
52
wgpu-bindings/wgpu.h
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
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);
|
@ -72,13 +72,13 @@ pub struct CommandBuffer<B: hal::Backend> {
|
|||||||
pub struct CommandBufferDescriptor;
|
pub struct CommandBufferDescriptor;
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn command_buffer_begin_render_pass(
|
pub extern "C" fn wgpu_command_buffer_begin_render_pass(
|
||||||
command_buffer: CommandBufferId,
|
command_buffer: CommandBufferId,
|
||||||
) -> RenderPassId {
|
) -> RenderPassId {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn command_buffer_begin_compute_pass() -> ComputePassId {
|
pub extern "C" fn wgpu_command_buffer_begin_compute_pass() -> ComputePassId {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ pub struct ShaderModule<B: hal::Backend> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn device_create_shader_module(
|
pub extern "C" fn wgpu_device_create_shader_module(
|
||||||
device_id: DeviceId,
|
device_id: DeviceId,
|
||||||
desc: pipeline::ShaderModuleDescriptor,
|
desc: pipeline::ShaderModuleDescriptor,
|
||||||
) -> ShaderModuleId {
|
) -> ShaderModuleId {
|
||||||
|
@ -26,7 +26,7 @@ pub struct DeviceDescriptor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn create_instance() -> InstanceId {
|
pub extern "C" fn wgpu_create_instance() -> InstanceId {
|
||||||
#[cfg(any(
|
#[cfg(any(
|
||||||
feature = "gfx-backend-vulkan",
|
feature = "gfx-backend-vulkan",
|
||||||
feature = "gfx-backend-dx12",
|
feature = "gfx-backend-dx12",
|
||||||
@ -48,7 +48,7 @@ pub extern "C" fn create_instance() -> InstanceId {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn instance_get_adapter(
|
pub extern "C" fn wgpu_instance_get_adapter(
|
||||||
instance_id: InstanceId,
|
instance_id: InstanceId,
|
||||||
desc: AdapterDescriptor,
|
desc: AdapterDescriptor,
|
||||||
) -> AdapterId {
|
) -> AdapterId {
|
||||||
@ -74,7 +74,7 @@ pub extern "C" fn instance_get_adapter(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[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 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();
|
||||||
|
Loading…
Reference in New Issue
Block a user