Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
6b232f9
fix compile errors from new nightly zig
btipling Nov 4, 2024
14aa3a1
Stubbed out a compute shader scene
btipling Nov 4, 2024
5256613
refractor buffer
btipling Nov 4, 2024
7a964da
temp rename for casing
btipling Nov 4, 2024
4c83744
renaming
btipling Nov 4, 2024
fd8b2c0
fix the long names
btipling Nov 4, 2024
57c67b7
ok this is nicer
btipling Nov 4, 2024
be2b086
data
btipling Nov 4, 2024
1bdbe66
Add compute shader
btipling Nov 4, 2024
c705dfa
compute just once
btipling Nov 4, 2024
39d61c6
stubbed out ray casting
btipling Nov 6, 2024
27a276a
set up ray casting scene
btipling Nov 6, 2024
12cbbeb
okok
btipling Nov 7, 2024
9052678
ok?
btipling Nov 7, 2024
7013e93
this ain't right
btipling Nov 7, 2024
34fa1d8
this is correct
btipling Nov 7, 2024
046beb8
fix renderdoc
btipling Nov 7, 2024
937350e
hey it works
btipling Nov 7, 2024
534347f
use ubo and stuff
btipling Nov 7, 2024
71c1575
a thing renders
btipling Nov 7, 2024
7c3bd8e
cool nobody cares
btipling Nov 7, 2024
98e7da6
ok I have a sphere though
btipling Nov 7, 2024
d51cb3f
let's try this
btipling Nov 8, 2024
84140de
Updating
btipling Nov 8, 2024
3a51651
sphere radius
btipling Nov 8, 2024
6fa340b
hrm
btipling Nov 8, 2024
222a4ad
it works
btipling Nov 8, 2024
17f40f6
lol bugs
btipling Nov 9, 2024
410e36e
what is 0 based indexing
btipling Nov 9, 2024
63fc567
need bigger and better
btipling Nov 9, 2024
0973170
have one trick
btipling Nov 9, 2024
7a529cc
add lighting stuff
btipling Nov 9, 2024
9e5d035
adding some stuff
btipling Nov 9, 2024
775d749
adding lighting and camera
btipling Nov 9, 2024
db2b9c0
well not crashing
btipling Nov 9, 2024
51acc48
better defaults
btipling Nov 9, 2024
3732e11
lol light
btipling Nov 9, 2024
4fa7c51
where is the specular???
btipling Nov 9, 2024
fd11428
lighting
btipling Nov 9, 2024
3441adf
add a shadows image
btipling Nov 9, 2024
a6b7742
shadows
btipling Nov 9, 2024
ebe2a14
add rotation
btipling Nov 9, 2024
5f3cccc
add rotation
btipling Nov 9, 2024
741a0a5
fix light position
btipling Nov 9, 2024
3204907
better
btipling Nov 9, 2024
d0ed0b2
set up to debug
btipling Nov 9, 2024
c5e6cbc
typo
btipling Nov 9, 2024
0d4105a
Add a fifth scene
btipling Nov 9, 2024
79b3bfe
getting ready for textures
btipling Nov 9, 2024
3d428ed
earth texture
btipling Nov 9, 2024
57491c7
progress
btipling Nov 9, 2024
1658ce5
textures work
btipling Nov 9, 2024
c108b1f
adding a cubemap scene
btipling Nov 9, 2024
1c667c4
cool
btipling Nov 9, 2024
8534e1b
skybox progress
btipling Nov 9, 2024
a900c5a
this isn't right
btipling Nov 9, 2024
f89a2e1
spacing
btipling Nov 9, 2024
099e500
ok
btipling Nov 9, 2024
588de5d
well, that's a skybox
btipling Nov 9, 2024
9d3dc97
well I hope this works
btipling Nov 9, 2024
8b1e21b
skybox
btipling Nov 10, 2024
f5a6c5e
start on the plane thingy
btipling Nov 10, 2024
5e78466
plane works
btipling Nov 10, 2024
8d512e1
let's goooo
btipling Nov 10, 2024
2ca5dfd
add reflection
btipling Nov 10, 2024
c2dea14
add a 9th image
btipling Nov 10, 2024
cda253c
refraction
btipling Nov 11, 2024
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
7 changes: 7 additions & 0 deletions src/foundations/lighting/Light.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ light_kind: light_type,
padding_1: f32 = 0,
padding_2: f32 = 0,

const Light = @This();

pub const light_type = enum(u32) {
direction,
positional,
spotlight,
};

pub const SSBO = rhi.storage_buffer.Buffer([]const Light, rhi.storage_buffer.bbp_lights, c.GL_STATIC_DRAW);

const c = @import("../c.zig").c;
const rhi = @import("../rhi/rhi.zig");
7 changes: 7 additions & 0 deletions src/foundations/lighting/Material.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ shininess: f32,
padding_1: f32 = 0,
padding_2: f32 = 0,
padding_3: f32 = 0,

const Material = @This();

pub const SSBO = rhi.storage_buffer.Buffer([]const Material, rhi.storage_buffer.bbp_materials, c.GL_STATIC_DRAW);

const c = @import("../c.zig").c;
const rhi = @import("../rhi/rhi.zig");
19 changes: 11 additions & 8 deletions src/foundations/physics/camera.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub const world_up: math.vector.vec3 = .{ 1, 0, 0 };
pub const world_right: math.vector.vec3 = .{ 0, 0, 1 };
pub const world_forward: math.vector.vec3 = .{ 0, 1, 0 };

pub const UBO = rhi.storage_buffer.Buffer(CameraData, rhi.storage_buffer.bbp_camera, c.GL_DYNAMIC_DRAW);

pub const CameraData = struct {
f_mvp: [16]f32,
v_matrix: [16]f32,
Expand Down Expand Up @@ -38,7 +40,7 @@ pub fn Camera(comptime T: type, comptime IntegratorT: type) type {
input_inactive: bool = false,
perspective_plane_distance_g: f32 = 0,
aspect_ratio_s: f32 = 0,
camera_buffer: rhi.Buffer,
camera_buffer: UBO,
global_ambient: [4]f32,
name: []const u8 = "main camera",
owns_buffer: bool,
Expand Down Expand Up @@ -78,14 +80,15 @@ pub fn Camera(comptime T: type, comptime IntegratorT: type) type {
const global_ambient: [4]f32 = .{ 0.7, 0.7, 0.7, 1.0 };
const P = initPerspective(cfg);
const v_matrix = math.matrix.identity();
const cd: rhi.Buffer.buffer_data = .{ .camera = .{
const cd: CameraData = .{
.f_mvp = P.array(),
.v_matrix = v_matrix.array(),
.f_camera_pos = .{ pos[0], pos[1], pos[2], 1 },
.f_global_ambient = global_ambient,
.f_shadow_view_m = math.matrix.identity().array(),
} };
var camera_buffer = rhi.Buffer.init(cd, "camera");
};

var camera_buffer: UBO = UBO.init(cd, "camera");
errdefer camera_buffer.deinit();
return initInternal(allocator, cfg, scene, integrator, pos, heading, camera_buffer, false);
}
Expand All @@ -97,7 +100,7 @@ pub fn Camera(comptime T: type, comptime IntegratorT: type) type {
integrator: IntegratorT,
pos: math.vector.vec3,
heading: ?f32,
camera_buffer: rhi.Buffer,
camera_buffer: UBO,
) *Self {
return initInternal(allocator, cfg, scene, integrator, pos, heading, camera_buffer, false);
}
Expand All @@ -109,7 +112,7 @@ pub fn Camera(comptime T: type, comptime IntegratorT: type) type {
integrator: IntegratorT,
pos: math.vector.vec3,
heading: ?f32,
camera_buffer: rhi.Buffer,
camera_buffer: UBO,
owns_buffer: bool,
) *Self {
const cam = allocator.create(Self) catch @panic("OOM");
Expand Down Expand Up @@ -487,7 +490,7 @@ pub fn Camera(comptime T: type, comptime IntegratorT: type) type {
if (!self.emit_matrix) {
return;
}
self.camera_buffer.update(.{ .camera = .{
self.camera_buffer.update(.{
.f_mvp = self.mvp.array(),
.v_matrix = self.view_m.array(),
.f_camera_pos = .{
Expand All @@ -498,7 +501,7 @@ pub fn Camera(comptime T: type, comptime IntegratorT: type) type {
},
.f_global_ambient = self.global_ambient,
.f_shadow_view_m = self.f_shadow_view_m.array(),
} });
});
self.scene.updateCamera();
}
};
Expand Down
127 changes: 0 additions & 127 deletions src/foundations/rhi/Buffer.zig

This file was deleted.

65 changes: 65 additions & 0 deletions src/foundations/rhi/Texture.zig
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,67 @@ pub fn setup(self: *Texture, image: ?*assets.Image, program: u32, uniform_name:
return;
}

pub fn setupWriteable(
self: *Texture,
data: []u8,
program: u32,
uniform_name: []const u8,
label: [:0]const u8,
width: usize,
height: usize,
) TextureError!void {
var name: u32 = undefined;
c.glCreateTextures(c.GL_TEXTURE_2D, 1, @ptrCast(&name));
var buf: [500]u8 = undefined;
const label_text = std.fmt.bufPrintZ(&buf, "✍️writeable_texture_{s}", .{label}) catch @panic("bufsize too small");
c.glObjectLabel(c.GL_TEXTURE, name, -1, label_text);
c.glTextureParameteri(name, c.GL_TEXTURE_WRAP_S, self.wrap_s);
c.glTextureParameteri(name, c.GL_TEXTURE_WRAP_T, self.wrap_t);
c.glTextureParameteri(name, c.GL_TEXTURE_MIN_FILTER, c.GL_NEAREST);
c.glTextureParameteri(name, c.GL_TEXTURE_MAG_FILTER, c.GL_NEAREST);

c.glTextureStorage2D(name, 1, c.GL_RGBA8, @intCast(width), @intCast(height));
c.glTextureSubImage2D(
name,
0,
0,
0,
@intCast(width),
@intCast(height),
c.GL_RGBA,
c.GL_UNSIGNED_BYTE,
data.ptr,
);

c.glGenerateTextureMipmap(name);
if (c.glfwExtensionSupported("GL_EXT_texture_filter_anisotropic") == 1) {
var ansio_setting: f32 = 0;
c.glGetFloatv(c.GL_MAX_TEXTURE_MAX_ANISOTROPY, &ansio_setting);
c.glTextureParameterf(name, c.GL_TEXTURE_MAX_ANISOTROPY, ansio_setting);
}

self.name = name;

self.uniforms[0] = Uniform.init(program, uniform_name) catch {
return TextureError.UniformCreationFailed;
};
self.num_uniforms += 1;

if (self.disable_bindless) {
return;
}
// Generate bindless handle
self.handle = c.glGetTextureHandleARB(self.name);
if (self.handle == 0) {
return TextureError.BindlessHandleCreationFailed;
}

// Make the texture resident
c.glMakeTextureHandleResidentARB(self.handle);

return;
}
Comment on lines +186 to +245
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Optimize texture setup for write-only usage

The current implementation includes several operations that are unnecessary for a write-only texture used in ray tracing:

  1. Mipmap generation is unnecessary for write-only textures
  2. Anisotropic filtering setup is not needed for compute shader output

Consider this optimized version:

 pub fn setupWriteable(
     self: *Texture,
     data: []u8,
     program: u32,
     uniform_name: []const u8,
     label: [:0]const u8,
     width: usize,
     height: usize,
 ) TextureError!void {
     var name: u32 = undefined;
     c.glCreateTextures(c.GL_TEXTURE_2D, 1, @ptrCast(&name));
     var buf: [500]u8 = undefined;
     const label_text = std.fmt.bufPrintZ(&buf, "✍️writeable_texture_{s}", .{label}) catch @panic("bufsize too small");
     c.glObjectLabel(c.GL_TEXTURE, name, -1, label_text);
     c.glTextureParameteri(name, c.GL_TEXTURE_WRAP_S, self.wrap_s);
     c.glTextureParameteri(name, c.GL_TEXTURE_WRAP_T, self.wrap_t);
     c.glTextureParameteri(name, c.GL_TEXTURE_MIN_FILTER, c.GL_NEAREST);
     c.glTextureParameteri(name, c.GL_TEXTURE_MAG_FILTER, c.GL_NEAREST);

     c.glTextureStorage2D(name, 1, c.GL_RGBA8, @intCast(width), @intCast(height));
     c.glTextureSubImage2D(
         name,
         0,
         0,
         0,
         @intCast(width),
         @intCast(height),
         c.GL_RGBA,
         c.GL_UNSIGNED_BYTE,
         data.ptr,
     );

-    c.glGenerateTextureMipmap(name);
-    if (c.glfwExtensionSupported("GL_EXT_texture_filter_anisotropic") == 1) {
-        var ansio_setting: f32 = 0;
-        c.glGetFloatv(c.GL_MAX_TEXTURE_MAX_ANISOTROPY, &ansio_setting);
-        c.glTextureParameterf(name, c.GL_TEXTURE_MAX_ANISOTROPY, ansio_setting);
-    }

     self.name = name;

     self.uniforms[0] = Uniform.init(program, uniform_name) catch {
         return TextureError.UniformCreationFailed;
     };
     self.num_uniforms += 1;

     if (self.disable_bindless) {
         return;
     }
     // Generate bindless handle
     self.handle = c.glGetTextureHandleARB(self.name);
     if (self.handle == 0) {
         return TextureError.BindlessHandleCreationFailed;
     }

     // Make the texture resident
     c.glMakeTextureHandleResidentARB(self.handle);

     return;
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
pub fn setupWriteable(
self: *Texture,
data: []u8,
program: u32,
uniform_name: []const u8,
label: [:0]const u8,
width: usize,
height: usize,
) TextureError!void {
var name: u32 = undefined;
c.glCreateTextures(c.GL_TEXTURE_2D, 1, @ptrCast(&name));
var buf: [500]u8 = undefined;
const label_text = std.fmt.bufPrintZ(&buf, "✍️writeable_texture_{s}", .{label}) catch @panic("bufsize too small");
c.glObjectLabel(c.GL_TEXTURE, name, -1, label_text);
c.glTextureParameteri(name, c.GL_TEXTURE_WRAP_S, self.wrap_s);
c.glTextureParameteri(name, c.GL_TEXTURE_WRAP_T, self.wrap_t);
c.glTextureParameteri(name, c.GL_TEXTURE_MIN_FILTER, c.GL_NEAREST);
c.glTextureParameteri(name, c.GL_TEXTURE_MAG_FILTER, c.GL_NEAREST);
c.glTextureStorage2D(name, 1, c.GL_RGBA8, @intCast(width), @intCast(height));
c.glTextureSubImage2D(
name,
0,
0,
0,
@intCast(width),
@intCast(height),
c.GL_RGBA,
c.GL_UNSIGNED_BYTE,
data.ptr,
);
c.glGenerateTextureMipmap(name);
if (c.glfwExtensionSupported("GL_EXT_texture_filter_anisotropic") == 1) {
var ansio_setting: f32 = 0;
c.glGetFloatv(c.GL_MAX_TEXTURE_MAX_ANISOTROPY, &ansio_setting);
c.glTextureParameterf(name, c.GL_TEXTURE_MAX_ANISOTROPY, ansio_setting);
}
self.name = name;
self.uniforms[0] = Uniform.init(program, uniform_name) catch {
return TextureError.UniformCreationFailed;
};
self.num_uniforms += 1;
if (self.disable_bindless) {
return;
}
// Generate bindless handle
self.handle = c.glGetTextureHandleARB(self.name);
if (self.handle == 0) {
return TextureError.BindlessHandleCreationFailed;
}
// Make the texture resident
c.glMakeTextureHandleResidentARB(self.handle);
return;
}
pub fn setupWriteable(
self: *Texture,
data: []u8,
program: u32,
uniform_name: []const u8,
label: [:0]const u8,
width: usize,
height: usize,
) TextureError!void {
var name: u32 = undefined;
c.glCreateTextures(c.GL_TEXTURE_2D, 1, @ptrCast(&name));
var buf: [500]u8 = undefined;
const label_text = std.fmt.bufPrintZ(&buf, "✍️writeable_texture_{s}", .{label}) catch @panic("bufsize too small");
c.glObjectLabel(c.GL_TEXTURE, name, -1, label_text);
c.glTextureParameteri(name, c.GL_TEXTURE_WRAP_S, self.wrap_s);
c.glTextureParameteri(name, c.GL_TEXTURE_WRAP_T, self.wrap_t);
c.glTextureParameteri(name, c.GL_TEXTURE_MIN_FILTER, c.GL_NEAREST);
c.glTextureParameteri(name, c.GL_TEXTURE_MAG_FILTER, c.GL_NEAREST);
c.glTextureStorage2D(name, 1, c.GL_RGBA8, @intCast(width), @intCast(height));
c.glTextureSubImage2D(
name,
0,
0,
0,
@intCast(width),
@intCast(height),
c.GL_RGBA,
c.GL_UNSIGNED_BYTE,
data.ptr,
);
self.name = name;
self.uniforms[0] = Uniform.init(program, uniform_name) catch {
return TextureError.UniformCreationFailed;
};
self.num_uniforms += 1;
if (self.disable_bindless) {
return;
}
// Generate bindless handle
self.handle = c.glGetTextureHandleARB(self.name);
if (self.handle == 0) {
return TextureError.BindlessHandleCreationFailed;
}
// Make the texture resident
c.glMakeTextureHandleResidentARB(self.handle);
return;
}


pub fn setupCubemap(self: *Texture, images: ?[6]*assets.Image, program: u32, uniform_name: []const u8, label: [:0]const u8) TextureError!void {
var name: u32 = undefined;
c.glCreateTextures(c.GL_TEXTURE_CUBE_MAP, 1, @ptrCast(&name));
Expand Down Expand Up @@ -352,6 +413,10 @@ pub fn addUniform(self: *Texture, program: u32, uniform_name: []const u8) Textur
self.num_uniforms += 1;
}

pub fn bindWritableImage(self: Texture) void {
c.glBindImageTexture(self.texture_unit, self.name, 0, c.GL_FALSE, 0, c.GL_WRITE_ONLY, c.GL_RGBA8);
}

pub fn bind(self: Texture) void {
if (self.disable_bindless) {
c.glBindTextureUnit(self.texture_unit, self.name);
Expand Down
Loading