Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod ds;
pub mod object;
pub mod material;
pub mod wgpu_handler;
6 changes: 1 addition & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion src/shaders/intersection.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ fn sphere_intersects(origin: vec3<f32>, dir: vec3<f32>, sphere: Sphere) -> f32 {

if t1 > 0.0001 { return t1; }
if t2 > 0.0001 { return t2; }
// return min(t1, t2);
return -1.0;
}

Expand Down
2 changes: 1 addition & 1 deletion src/shaders/main.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions src/shaders/material.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ struct Call {
caller: i32,
ray_dir: vec3<f32>,
output_id: u32,
outputs: array<vec3<f32>, 3>,
outputs: array<vec3<f32>, 2>,
depth: u32,
}

var<private> callstack: array<Call, 7>;

fn ray_color(ray_pos: vec3<f32>, ray_dir: vec3<f32>, tid: u32) -> u32 {
fn ray_color(ray_pos: vec3<f32>, ray_dir: vec3<f32>) -> u32 {
var callstack_len = 1;
let light_dir = vec3<f32>(0.57735,0.57735,0);

Expand All @@ -19,10 +19,9 @@ fn ray_color(ray_pos: vec3<f32>, ray_dir: vec3<f32>, tid: u32) -> u32 {
callstack[0].output_id = 0u;
callstack[0].outputs[0] = vec3<f32>(-1.0, -1.0, -1.0);
callstack[0].outputs[1] = vec3<f32>(-1.0, -1.0, -1.0);
callstack[0].outputs[2] = vec3<f32>(-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];
Expand Down Expand Up @@ -50,6 +49,7 @@ fn ray_color(ray_pos: vec3<f32>, ray_dir: vec3<f32>, tid: u32) -> u32 {
continue;
}

// dereference the material
switch record.obj_type {
case 0u: {
material = spheres[record.obj_index].material;
Expand All @@ -60,6 +60,7 @@ fn ray_color(ray_pos: vec3<f32>, ray_dir: vec3<f32>, 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);

Expand All @@ -69,7 +70,6 @@ fn ray_color(ray_pos: vec3<f32>, ray_dir: vec3<f32>, tid: u32) -> u32 {
callstack[callstack_len].output_id = 0;
callstack[callstack_len].outputs[0] = vec3<f32>(-1.0, -1.0, -1.0);
callstack[callstack_len].outputs[1] = vec3<f32>(-1.0, -1.0, -1.0);
callstack[callstack_len].outputs[2] = vec3<f32>(-1.0, -1.0, -1.0);
callstack[callstack_len].depth = call.depth - 1;

callstack_len += 1;
Expand All @@ -86,13 +86,13 @@ fn ray_color(ray_pos: vec3<f32>, ray_dir: vec3<f32>, tid: u32) -> u32 {
callstack[callstack_len].output_id = 1;
callstack[callstack_len].outputs[0] = vec3<f32>(-1.0, -1.0, -1.0);
callstack[callstack_len].outputs[1] = vec3<f32>(-1.0, -1.0, -1.0);
callstack[callstack_len].outputs[2] = vec3<f32>(-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<f32>(0.0, 0.0, 0.0);
if material.texture_id != -1 {
lit_color = get_texture_color(record);
Expand All @@ -109,6 +109,7 @@ fn ray_color(ray_pos: vec3<f32>, ray_dir: vec3<f32>, tid: u32) -> u32 {
);
}

// cleanup color
if !pushed_to_stack {
call = callstack[index];
var color = f32(material.translucent) * call.outputs[0] +
Expand Down
9 changes: 4 additions & 5 deletions src/wgpu_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ pub struct GpuHandler {
pub spheres_buf: Option<wgpu::Buffer>,
pub quads_buf: Option<wgpu::Buffer>,
pub output_buf_size: Option<u64>,

texture: Option<Texture>,
view: Option<TextureView>
texture: Option<Texture>,
view: Option<TextureView>
}

#[allow(unused)]
Expand Down Expand Up @@ -174,8 +173,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
);
}
Expand Down
Loading