From 889bb45d55b8083a0886abb357459c2fee212d4e Mon Sep 17 00:00:00 2001 From: prushton2 Date: Fri, 10 Apr 2026 09:53:04 -0400 Subject: [PATCH 1/3] GPU Threading fix --- src/shaders/main.wgsl | 2 +- src/shaders/material.wgsl | 13 +++++++------ src/wgpu_handler.rs | 4 ++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/shaders/main.wgsl b/src/shaders/main.wgsl index c65543d..978c989 100644 --- a/src/shaders/main.wgsl +++ b/src/shaders/main.wgsl @@ -24,7 +24,7 @@ fn main( return; } - output[idx] = ray_color(uniforms.pos, ray_dir, local_invocation_index); + output[idx] = ray_color(uniforms.pos, ray_dir); } // not sure what these do but docs say i need them diff --git a/src/shaders/material.wgsl b/src/shaders/material.wgsl index de6df09..4d8f50b 100644 --- a/src/shaders/material.wgsl +++ b/src/shaders/material.wgsl @@ -3,13 +3,13 @@ struct Call { caller: i32, ray_dir: vec3, output_id: u32, - outputs: array, 3>, + outputs: array, 2>, depth: u32, } var callstack: array; -fn ray_color(ray_pos: vec3, ray_dir: vec3, tid: u32) -> u32 { +fn ray_color(ray_pos: vec3, ray_dir: vec3) -> u32 { var callstack_len = 1; let light_dir = vec3(0.57735,0.57735,0); @@ -19,10 +19,9 @@ fn ray_color(ray_pos: vec3, ray_dir: vec3, tid: u32) -> u32 { callstack[0].output_id = 0u; callstack[0].outputs[0] = vec3(-1.0, -1.0, -1.0); callstack[0].outputs[1] = vec3(-1.0, -1.0, -1.0); - callstack[0].outputs[2] = vec3(-1.0, -1.0, -1.0); callstack[0].depth = 4u; - for (var i = 0; i < 32; i++) { + for (var i = 0; i < 16; i++) { if callstack_len == 0 { break; } let index = callstack_len-1; var call = callstack[index]; @@ -50,6 +49,7 @@ fn ray_color(ray_pos: vec3, ray_dir: vec3, tid: u32) -> u32 { continue; } + // dereference the material switch record.obj_type { case 0u: { material = spheres[record.obj_index].material; @@ -60,6 +60,7 @@ fn ray_color(ray_pos: vec3, ray_dir: vec3, tid: u32) -> u32 { default: {} } + // calculate the materials if material.translucent != 0u && call.outputs[0].x <= -1.0 { let new_origin = ray_at(call.ray_pos, call.ray_dir, record.t + 0.00001); @@ -69,7 +70,6 @@ fn ray_color(ray_pos: vec3, ray_dir: vec3, tid: u32) -> u32 { callstack[callstack_len].output_id = 0; callstack[callstack_len].outputs[0] = vec3(-1.0, -1.0, -1.0); callstack[callstack_len].outputs[1] = vec3(-1.0, -1.0, -1.0); - callstack[callstack_len].outputs[2] = vec3(-1.0, -1.0, -1.0); callstack[callstack_len].depth = call.depth - 1; callstack_len += 1; @@ -86,13 +86,13 @@ fn ray_color(ray_pos: vec3, ray_dir: vec3, tid: u32) -> u32 { callstack[callstack_len].output_id = 1; callstack[callstack_len].outputs[0] = vec3(-1.0, -1.0, -1.0); callstack[callstack_len].outputs[1] = vec3(-1.0, -1.0, -1.0); - callstack[callstack_len].outputs[2] = vec3(-1.0, -1.0, -1.0); callstack[callstack_len].depth = call.depth - 1; callstack_len += 1; pushed_to_stack = true; } + // lighting / texturing var lit_color = vec3(0.0, 0.0, 0.0); if material.texture_id != -1 { lit_color = get_texture_color(record); @@ -109,6 +109,7 @@ fn ray_color(ray_pos: vec3, ray_dir: vec3, tid: u32) -> u32 { ); } + // cleanup color if !pushed_to_stack { call = callstack[index]; var color = f32(material.translucent) * call.outputs[0] + diff --git a/src/wgpu_handler.rs b/src/wgpu_handler.rs index d0b3bde..83c1c9d 100644 --- a/src/wgpu_handler.rs +++ b/src/wgpu_handler.rs @@ -174,8 +174,8 @@ impl GpuHandler { pass.set_pipeline(gpu.compute_pipeline); pass.set_bind_group(0, gpu.bind_group, &[]); pass.dispatch_workgroups( - (gpu.surface_config.width + 7) / 8, - (gpu.surface_config.height + 7) / 8, + (gpu.surface_config.width + 15) / 16, + (gpu.surface_config.height + 15) / 16, 1 ); } From a183dab299cb6b02dcf2ba5244cb942e276dbcad Mon Sep 17 00:00:00 2001 From: prushton2 Date: Fri, 10 Apr 2026 10:44:06 -0400 Subject: [PATCH 2/3] turned into a library --- Cargo.toml | 18 +++++++++++++++++- src/lib.rs | 4 ++++ src/main.rs | 6 +----- src/shaders/intersection.wgsl | 1 - 4 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 8af1bf4..1a70fc3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,18 @@ [package] name = "RenderEngine" +description = "A GPU ray tracing render engine" version = "1.93.0" edition = "2024" resolver = "2" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +license = "GPL-3.0" +repository = "https://github.com/prushton2/RenderEngine" + +# Include the shaders +include = [ + "src/**/*", + "Cargo.toml", +] [dependencies] auto_ops = "0.3.0" @@ -21,3 +29,11 @@ winit = "0.30.12" [profile.release] debug = true + +[lib] +name = "render_engine" +path = "src/lib.rs" + +[[bin]] +name = "RenderEngine" +path = "src/main.rs" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..f293da9 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,4 @@ +pub mod ds; +pub mod object; +pub mod material; +pub mod wgpu_handler; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 0c47a90..53f9054 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,16 +7,12 @@ use winit::event::{ElementState, KeyEvent, WindowEvent, DeviceEvent, DeviceId}; use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop}; use winit::window::{Window, WindowId, CursorGrabMode}; use winit::keyboard::{KeyCode, PhysicalKey}; +use render_engine::{ds, material, object, wgpu_handler}; use crate::material::GpuMaterial; use crate::object::Renderable; use crate::object::renderable::ToGpu; -mod wgpu_handler; -mod material; -mod object; -mod ds; - #[derive(Parser, Debug)] #[command(version, about, long_about = None)] struct Arguments { diff --git a/src/shaders/intersection.wgsl b/src/shaders/intersection.wgsl index 873515a..ea36dad 100644 --- a/src/shaders/intersection.wgsl +++ b/src/shaders/intersection.wgsl @@ -14,7 +14,6 @@ fn sphere_intersects(origin: vec3, dir: vec3, sphere: Sphere) -> f32 { if t1 > 0.0001 { return t1; } if t2 > 0.0001 { return t2; } - // return min(t1, t2); return -1.0; } From a50528810a07e7ad85dac2ce6f038dc48d7d70ef Mon Sep 17 00:00:00 2001 From: prushton2 Date: Fri, 10 Apr 2026 11:11:04 -0400 Subject: [PATCH 3/3] formatting fix --- src/wgpu_handler.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/wgpu_handler.rs b/src/wgpu_handler.rs index 83c1c9d..3040576 100644 --- a/src/wgpu_handler.rs +++ b/src/wgpu_handler.rs @@ -19,9 +19,8 @@ pub struct GpuHandler { pub spheres_buf: Option, pub quads_buf: Option, pub output_buf_size: Option, - - texture: Option, - view: Option + texture: Option, + view: Option } #[allow(unused)]