Skip to content

Commit

Permalink
acceleration structure
Browse files Browse the repository at this point in the history
  • Loading branch information
msiglreith committed May 24, 2021
1 parent 369b856 commit 9074423
Show file tree
Hide file tree
Showing 12 changed files with 851 additions and 306 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion glace-vk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ash-window = "0.6"
winit = "0.24"
byteorder = "1"
anyhow = "1"
gpu-allocator = "0.6"
gpu-allocator = { version = "0.7", features = ["vulkan_device_address"] }
glace = { path = "../glace" }
image = "0.23"

Expand Down
4 changes: 2 additions & 2 deletions glace-vk/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use spirv_builder::SpirvBuilder;
use spirv_builder::{MetadataPrintout, SpirvBuilder};

fn main() -> anyhow::Result<()> {
let result = SpirvBuilder::new("shader", "spirv-unknown-spv1.5")
.print_metadata(false)
.print_metadata(MetadataPrintout::DependencyOnly)
.bindless(true)
.build_multimodule()?;
let directory = result
Expand Down
106 changes: 52 additions & 54 deletions glace-vk/shader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,86 +3,84 @@
#![register_attr(spirv)]

use glace::{f32x2, f32x3, f32x4, f32x4x4, geometry::Fullscreen, vec3, vec4};
use spirv_std::bindless::Buffer;
use spirv_std::bindless::{ArrayBuffer, Buffer, Sampler, SimpleBuffer, Texture2d};
use spirv_std::num_traits::Float;
use spirv_std::{Cubemap, Sampler};

#[repr(C)]
#[derive(Copy, Clone)]
pub struct LocalsPbr {
pub struct WorldData {
world_to_view: f32x4x4,
view_to_clip: f32x4x4,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct Buffers {
locals: Buffer,
pub struct InstanceData {
sampler: Sampler,
normal_map: Texture2d,
}

#[repr(C)]
#[derive(Copy, Clone)]
struct GeometryData {
v_position_obj: ArrayBuffer<f32x3>,
v_normal_obj: ArrayBuffer<f32x3>,
v_texcoord: ArrayBuffer<f32x2>,
v_tangent_obj: ArrayBuffer<f32x4>,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct Constants {
world: SimpleBuffer<WorldData>,
geometry: SimpleBuffer<GeometryData>,
instance: SimpleBuffer<InstanceData>,
}

#[spirv(vertex)]
pub fn mesh_vs(
v_position_obj: f32x3,
v_normal_obj: f32x3,
v_texcoord: f32x2,
v_tangent_obj: f32x4,
#[spirv(vertex_index)] vert_id: i32,
#[spirv(position)] a_position: &mut f32x4,
a_normal_world: &mut f32x3,
a_texcoord: &mut f32x2,
a_tangent_world: &mut f32x4,
a_position_world: &mut f32x3,
#[spirv(push_constant)] buffers: &Buffers,
#[spirv(push_constant)] constants: &Constants,
) {
let vertex_index = vert_id as u32;

let u_world = constants.world.load();
let u_geometry = constants.geometry.load();

let v_position_obj = u_geometry.v_position_obj.load(vertex_index);
let v_normal_obj = u_geometry.v_normal_obj.load(vertex_index);
let v_texcoord = u_geometry.v_texcoord.load(vertex_index);
let v_tangent_obj = u_geometry.v_tangent_obj.load(vertex_index);

*a_normal_world = v_normal_obj;
*a_texcoord = v_texcoord;
*a_tangent_world = v_tangent_obj;

let pos_obj = v_position_obj;
let pos_world = vec4(pos_obj.x, pos_obj.y, pos_obj.z, 1.0);
let pos_world = vec4(v_position_obj.x, v_position_obj.y, v_position_obj.z, 1.0);
*a_position_world = vec3(pos_world.x, pos_world.y, pos_world.z);

let u_locals = buffers.locals.load::<LocalsPbr>(0);

let pos_view = pos_world * u_locals.world_to_view;
let pos_clip = pos_view * u_locals.view_to_clip;
let pos_view = pos_world * u_world.world_to_view;
let pos_clip = pos_view * u_world.view_to_clip;

*a_position = pos_clip;
}

// #[spirv(fragment)]
// pub fn mesh_fs() {}

// #[repr(C)]
// #[derive(Copy, Clone)]
// pub struct LocalsSkybox {
// view_to_world: f32x4x4,
// clip_to_view: f32x4x4,
// }

// #[spirv(vertex)]
// pub fn skybox_vs(
// #[spirv(vertex_index)] vert_id: i32,
// #[spirv(position)] a_position: &mut f32x4,
// a_view_dir: &mut f32x3,
// #[spirv(uniform, descriptor_set = 0, binding = 0)] u_locals: &LocalsSkybox,
// ) {
// let position_uv = Fullscreen::position(vert_id);
// let position_clip = vec4(position_uv.x, position_uv.y, 0.0, 1.0);
// let mut position_view = position_clip * u_locals.clip_to_view;
// position_view.w = 0.0;
// let position_world = position_view * u_locals.view_to_world;

// *a_view_dir = vec3(position_world.x, position_world.y, position_world.z);
// *a_position = position_clip;
// }

// #[spirv(fragment)]
// pub fn skybox_fs(
// f_view_dir: f32x3,
// #[spirv(descriptor_set = 1, binding = 0)] u_diffuse_map: &Cubemap,
// #[spirv(descriptor_set = 2, binding = 0)] u_sampler: &Sampler,
// output: &mut f32x4,
// ) {
// let sky: f32x4 = u_diffuse_map.sample(*u_sampler, f_view_dir);
// *output = vec4(sky.x, sky.y, sky.z, 1.0);
// }
#[spirv(fragment)]
pub fn mesh_fs(
a_normal_world: f32x3,
a_texcoord: f32x2,
a_tangent_world: f32x4,
a_position_world: f32x3,
output: &mut f32x4,
#[spirv(push_constant)] constants: &Constants,
) {
let instance_data = constants.instance.load();
let normal: f32x4 = instance_data
.normal_map
.sample(instance_data.sampler, a_texcoord);
*output = vec4(normal.x, normal.y, normal.z, 1.0);
}
61 changes: 61 additions & 0 deletions glace-vk/src/gpu/descriptor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use glace::std::bindless::RenderResourceTag;
use crate::gpu::GpuDescriptors;

pub use glace::std::bindless::RenderResourceHandle as CpuDescriptor;

type CpuDescriptors = Vec<CpuDescriptor>;

pub struct Descriptors {
free_handle: usize,
tag: RenderResourceTag,

cpu: CpuDescriptors,
pub(crate) gpu: GpuDescriptors,
}

impl Descriptors {
pub fn new(tag: RenderResourceTag, len: usize, gpu: GpuDescriptors) -> Self {
let mut cpu = CpuDescriptors::with_capacity(len);
for i in 0..len {
cpu.push(CpuDescriptor::new(0, tag, i as u32 + 1));
}

Self {
free_handle: 0,
tag,
cpu,
gpu,
}
}

fn invalid_index(&self) -> usize {
self.cpu.len()
}

pub unsafe fn create(&mut self) -> CpuDescriptor {
assert_ne!(self.free_handle, self.invalid_index()); // out of memory

let idx = self.free_handle;
let handle = self.cpu[self.free_handle];

let version = (handle.version() + 1) % 64;
let index = handle.index() as usize;

assert_ne!(index, self.free_handle);
self.free_handle = index;

let handle = CpuDescriptor::new(version as _, self.tag, idx as _);
self.cpu[idx] = handle;

handle
}

pub unsafe fn is_valid(&self, handle: CpuDescriptor) -> bool {
let idx = handle.index() as usize;
if idx >= self.cpu.len() {
return false;
}

self.cpu[idx] == handle
}
}
Loading

0 comments on commit 9074423

Please sign in to comment.