Skip to content

Commit

Permalink
we're stuck around here w/ hello-world code getting started - calebwi…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffrey-P-McAteer committed Sep 3, 2024
1 parent 461a72a commit ff8f272
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 3 deletions.
67 changes: 65 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ csv = { version = "1.3" }
#serde_json = { version = "1" }
serde_jsonrc = { version = "0.1" }
tokio = { version = "1", features = ["full"] }
emu_core = { version = "0.1" }
emu_core = { version = "0.1", features = ["glsl-compile"] }

emu_glsl = { version = "0.1" }
zerocopy = { version = "0.7", features = ["derive"] }

71 changes: 71 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ use clap::Parser;
pub mod structs;
pub mod utils;

// From hello world code
use emu_glsl::*;
use emu_core::prelude::*;
use zerocopy::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = structs::Args::parse();

Expand All @@ -26,6 +31,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}



#[repr(C)]
#[derive(AsBytes, FromBytes, Copy, Clone, Default, Debug)]
pub struct Rectangle {
x: u32,
y: u32,
w: i32,
h: i32,
}


async fn main_async(args: &structs::Args) -> Result<(), Box<dyn std::error::Error>> {

emu_core::pool::assert_device_pool_initialized().await;
Expand Down Expand Up @@ -54,6 +71,60 @@ async fn main_async(args: &structs::Args) -> Result<(), Box<dyn std::error::Erro
println!("t0_data = {:?}", &t0_data);
println!("delta_data = {:?}", &delta_data);

// first, we move a bunch of rectangles to the GPU
let mut x: DeviceBox<[Rectangle]> = vec![Default::default(); 128].as_device_boxed()?;

// then we compile some GLSL code using the GlslCompile compiler and
// the GlobalCache for caching compiler artifacts
let c = compile::<String, GlslCompile, _, GlobalCache>(
GlslBuilder::new()
.set_entry_point_name("main")
.add_param_mut()
.set_code_with_glsl(
r#"
#version 450
layout(local_size_x = 1) in; // our thread block size is 1, that is we only have 1 thread per block
struct Rectangle {
uint x;
uint y;
int w;
int h;
};
// make sure to use only a single set and keep all your n parameters in n storage buffers in bindings 0 to n-1
// you shouldn't use push constants or anything OTHER than storage buffers for passing stuff into the kernel
// just use buffers with one buffer per binding
layout(set = 0, binding = 0) buffer Rectangles {
Rectangle[] rectangles;
}; // this is used as both input and output for convenience
Rectangle flip(Rectangle r) {
r.x = r.x + r.w;
r.y = r.y + r.h;
r.w *= -1;
r.h *= -1;
return r;
}
// there should be only one entry point and it should be named "main"
// ultimately, Emu has to kind of restrict how you use GLSL because it is compute focused
void main() {
uint index = gl_GlobalInvocationID.x; // this gives us the index in the x dimension of the thread space
rectangles[index] = flip(rectangles[index]);
}
"#,
)
)?.finish()?;

// we spawn 128 threads (really 128 thread blocks)
unsafe {
spawn(128).launch(call!(c, &mut x));
}

// this is the Future we need to block on to get stuff to happen
// everything else is non-blocking in the API (except stuff like compilation)
println!("{:?}", x.get().await?);



Expand Down

0 comments on commit ff8f272

Please sign in to comment.