Skip to content

Commit 0916497

Browse files
committed
Vulkan, take 6
1 parent df62e31 commit 0916497

7 files changed

Lines changed: 163 additions & 242 deletions

File tree

wyvern-vulkan/examples/bfs.rs

Lines changed: 0 additions & 159 deletions
This file was deleted.

wyvern-vulkan/examples/simple.rs

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![feature(nll)]
2+
#![allow(unused_imports)]
23

34
extern crate wyvern_core as wcore;
45
extern crate wyvern_vulkan as wvk;
@@ -15,23 +16,48 @@ fn main() {
1516
let program = builder.finalize().unwrap();
1617
let executor = VkExecutor::new(Default::default()).unwrap();
1718
let mut executable = executor.compile(program).unwrap();
18-
let asd = executor.new_resource().unwrap();
19-
let foo = executor.new_resource().unwrap();
20-
let bar = executor.new_resource().unwrap();
21-
foo.set_data(TokenValue::Scalar(ConstantScalar::U32(0)));
22-
asd.set_data(TokenValue::Scalar(ConstantScalar::U32(2)));
23-
bar.set_data(TokenValue::Scalar(ConstantScalar::U32(21)));
24-
executable.bind("foo", IO::Output, foo.clone());
25-
executable.bind("asd", IO::Input, asd.clone());
26-
executable.bind("bar", IO::Input, asd.clone());
19+
let in1 = executor.new_resource().unwrap();
20+
let in2 = executor.new_resource().unwrap();
21+
let out = executor.new_resource().unwrap();
22+
in1.set_data(TokenValue::Vector(ConstantVector::U32(vec![2; 10000])));
23+
in2.set_data(TokenValue::Scalar(ConstantScalar::U32(3)));
24+
out.set_data(TokenValue::Scalar(ConstantScalar::U32(0)));
25+
executable.bind("out", IO::Output, out.clone());
26+
executable.bind("in1", IO::Input, in1.clone());
27+
executable.bind("in2", IO::Input, in2.clone());
2728
executable.run().unwrap();
28-
println!("{:?}", foo.get_data());
29+
println!("{:?}", out.get_data());
2930
}
3031

3132
fn program(builder: &ProgramBuilder) {
32-
let x = Variable::new(builder).mark_as_input("asd").load();
33-
let y = Variable::new(builder).mark_as_input("bar").load();
34-
let a: Constant<u32> = y * x;
35-
let b = Variable::new(builder).mark_as_output("foo");
36-
b.store(a);
33+
let zero = Constant::new(0_u32, builder);
34+
let one = Constant::new(1_u32, builder);
35+
let v: Array<u32> = Array::new(zero, 10000, true, builder).mark_as_input("in1");
36+
let a: Constant<u32> = Variable::new(builder).mark_as_input("in2").load();
37+
let o = Variable::new(builder).mark_as_output("out");
38+
let id = builder.worker_id();
39+
let size = builder.num_workers();
40+
let tmp: Array<u32> = Array::new(size, 10000, true, builder);
41+
let v_length = v.len();
42+
let index = Variable::new(builder);
43+
index.store(id);
44+
builder.while_loop(|_| {
45+
index.load().lt(v_length)
46+
}, |_| {
47+
let old_value = tmp.at(id).load();
48+
tmp.at(id).store(old_value + v.at(index.load()).load() * a);
49+
index.store(index.load() + size);
50+
});
51+
builder.memory_barrier();
52+
builder.if_then(|_| {
53+
id.eq(zero)
54+
}, |_| {
55+
index.store(zero);
56+
builder.while_loop(|_| {
57+
index.load().lt(size)
58+
}, |_| {
59+
o.store(o.load() + tmp.at(index.load()).load());
60+
index.store(index.load() + one);
61+
});
62+
});
3763
}

wyvern-vulkan/src/executable.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ impl Executable for VkExecutable {
104104
if let Some(ref x) = self.assoc[i] {
105105
buffers.push(x.get_handle().lock().unwrap().clone())
106106
} else if let BindType::Private(size, ty) = self.bindings[i].1 {
107+
let size = size + 1;
107108
match ty {
108109
DataType::U32 => buffers.push(ResourceType::VU32(
109110
CpuAccessibleBuffer::from_iter(
@@ -161,15 +162,15 @@ impl Executable for VkExecutable {
161162
});
162163
unsafe { set.write(&self.device, writer) };
163164
let sanitized_set = MyDescriptorSet {
164-
set: set,
165+
set,
165166
layout: self.layout.clone(),
166-
buffers: buffers
167+
buffers
167168
};
168169
let command_buffer = AutoCommandBufferBuilder::primary_one_time_submit(
169170
self.device.clone(),
170171
self.queue.family(),
171172
).unwrap()
172-
.dispatch([896, 1, 1], self.pipeline.clone(), sanitized_set, ())
173+
.dispatch([1536, 1, 1], self.pipeline.clone(), sanitized_set, ())
173174
.unwrap()
174175
.build()
175176
.unwrap();
@@ -194,7 +195,6 @@ unsafe impl DescriptorSetDesc for MyDescriptorSet {
194195
}
195196

196197
fn descriptor(&self, binding: usize) -> Option<DescriptorDesc> {
197-
eprintln!("Binding: {:?}", binding);
198198
self.layout.descriptor(0, binding)
199199
}
200200
}
@@ -212,7 +212,6 @@ unsafe impl DescriptorSet for MyDescriptorSet {
212212
if index >= self.buffers.len() {
213213
return None;
214214
}
215-
eprintln!("Index: {:?}", index);
216215
let desc_index = index as u32;
217216
match self.buffers[index] {
218217
ResourceType::U32(ref x) => Some((x, desc_index)),

wyvern-vulkan/src/executor.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,19 @@ impl Executor for VkExecutor {
8282

8383
fn compile(&self, program: Program) -> Result<VkExecutable, String> {
8484
let (binary, bindings) = generate(&program, self.version)?;
85+
fn u32tou8(v: &[u32]) -> Vec<u8> {
86+
use byteorder::{ByteOrder, LittleEndian};
87+
let mut result = Vec::new();
88+
for i in v {
89+
let mut buf = [0; 4];
90+
LittleEndian::write_u32(&mut buf, *i);
91+
for j in &buf {
92+
result.push(*j);
93+
}
94+
}
95+
result
96+
}
97+
::std::fs::write("dump.spv", u32tou8(&binary)).unwrap();
8598
// TODO: validate and optimize the binary
8699
let module = unsafe {
87100
ShaderModule::from_words(self.device.clone(), &binary).map_err(|x| format!("{:?}", x))?

0 commit comments

Comments
 (0)