Skip to content

Commit

Permalink
Create C example
Browse files Browse the repository at this point in the history
  • Loading branch information
grovesNL committed Sep 23, 2018
1 parent d94d45c commit 08ad0f4
Show file tree
Hide file tree
Showing 16 changed files with 140 additions and 90 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
**/*.rs.bk
#Cargo.lock
.vscode
build
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ publish = false

[[bin]]
name = "hello_triangle"
path = "hello_triangle/main.rs"
path = "hello_triangle_rust/main.rs"

[features]
default = []
Expand Down
14 changes: 14 additions & 0 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -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)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
31 changes: 0 additions & 31 deletions examples/hello_triangle/main.rs

This file was deleted.

23 changes: 23 additions & 0 deletions examples/hello_triangle_c/main.c
Original file line number Diff line number Diff line change
@@ -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 changes: 32 additions & 0 deletions examples/hello_triangle_rust/main.rs
Original file line number Diff line number Diff line change
@@ -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"),
},
);
}
51 changes: 0 additions & 51 deletions wgpu-bindings/bindings.h

This file was deleted.

12 changes: 11 additions & 1 deletion wgpu-bindings/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
52 changes: 52 additions & 0 deletions wgpu-bindings/wgpu.h
Original file line number Diff line number Diff line change
@@ -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);
4 changes: 2 additions & 2 deletions wgpu-native/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ pub struct CommandBuffer<B: hal::Backend> {
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!()
}
2 changes: 1 addition & 1 deletion wgpu-native/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct ShaderModule<B: hal::Backend> {
}

#[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 {
Expand Down
6 changes: 3 additions & 3 deletions wgpu-native/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 {
Expand All @@ -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();
Expand Down

0 comments on commit 08ad0f4

Please sign in to comment.