Skip to content

Commit

Permalink
Using min
Browse files Browse the repository at this point in the history
  • Loading branch information
gents83 committed Feb 21, 2024
1 parent df659e1 commit 92d9567
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
4 changes: 2 additions & 2 deletions crates/graphics/src/common/passes/compute_culling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
ArrayI32, ArrayU32, AsBinding, BVHBuffer, BindingData, BindingFlags, BindingInfo,
CommandBuffer, ComputePass, ComputePassData, ConstantDataRw, DrawCommandType,
DrawCommandsBuffer, GpuBuffer, Mesh, MeshFlags, MeshesBuffer, MeshletsBuffer, Pass,
RenderContext, RenderContextRc, ShaderStage, TextureView, ATOMIC_SIZE,
RenderContext, RenderContextRc, ShaderStage, TextureView, ATOMIC_SIZE, MAX_LOD_LEVELS,
};

use inox_commands::CommandParser;
Expand Down Expand Up @@ -170,7 +170,7 @@ impl Pass for CullingPass {
let num_meshlets = self.meshlets.read().unwrap().item_count();
let mut max_meshlets_size = num_meshlets as u32 + ATOMIC_SIZE;
max_meshlets_size += ATOMIC_SIZE - (max_meshlets_size % ATOMIC_SIZE);
let processing_data = vec![-1; max_meshlets_size as usize];
let processing_data = vec![MAX_LOD_LEVELS as i32; max_meshlets_size as usize];
self.processing_data.write().unwrap().set(processing_data);

if self.update_meshes {
Expand Down
5 changes: 4 additions & 1 deletion crates/graphics/src/common/render_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::{
};

const USE_FORCED_VULKAN: bool = false;
const USE_FORCED_DX12: bool = false;

#[derive(PartialEq, Eq, Copy, Clone)]
pub enum RendererState {
Expand Down Expand Up @@ -144,12 +145,14 @@ impl RenderContext {
let (instance, surface, adapter, device, queue) = {
let backends = if USE_FORCED_VULKAN {
wgpu::Backends::VULKAN
} else if USE_FORCED_DX12 {
wgpu::Backends::DX12
} else {
wgpu::Backends::all()
};
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends,
flags: wgpu::InstanceFlags::advanced_debugging(),
flags: wgpu::InstanceFlags::empty(),
..Default::default()
});
let surface = Self::create_surface(&instance, handle.clone());
Expand Down
8 changes: 4 additions & 4 deletions crates/graphics/src/data/gpu_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct DrawIndexedCommand {
pub base_instance: u32,
}

#[repr(C, align(4))]
#[repr(C)]
#[derive(Default, PartialEq, Eq, Clone, Copy, Debug)]
pub struct DrawCommand {
pub vertex_count: u32,
Expand Down Expand Up @@ -91,7 +91,7 @@ impl GPUMesh {
}
}

#[repr(C, align(16))]
#[repr(C)]
#[derive(Default, PartialEq, Clone, Copy, Debug)]
pub struct GPUMeshlet {
pub mesh_index_and_lod_level: u32, // 29 mesh + 3 lod bits
Expand All @@ -114,7 +114,7 @@ impl GPUMeshlet {
}
}

#[repr(C, align(16))]
#[repr(C)]
#[derive(PartialEq, Clone, Copy, Debug)]
pub struct GPUMaterial {
pub roughness_factor: f32,
Expand Down Expand Up @@ -159,7 +159,7 @@ impl Default for GPUMaterial {
}
}

#[repr(C, align(4))]
#[repr(C)]
#[derive(Default, PartialEq, Clone, Copy, Debug)]
pub struct GPURuntimeVertexData {
pub world_pos: [f32; 3],
Expand Down
2 changes: 1 addition & 1 deletion crates/graphics/src/platform/pc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg(not(target_arch = "wasm32"))]

pub fn required_gpu_features() -> wgpu::Features {
wgpu::Features::all_webgpu_mask()
wgpu::Features::default()
| wgpu::Features::POLYGON_MODE_LINE
| wgpu::Features::INDIRECT_FIRST_INSTANCE
| wgpu::Features::MULTI_DRAW_INDIRECT
Expand Down
27 changes: 14 additions & 13 deletions data_raw/shaders/wgsl/compute_culling.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn main(
let ncd_max = clip_mvp * vec4<f32>(max, 1.);
let clip_max = ncd_max.xyz / ncd_max.w;
let screen_max = clip_to_normalized(clip_max.xy);
let screen_diff = max(screen_max, screen_min) - min(screen_max, screen_min);
let screen_diff = (max(screen_max, screen_min) - min(screen_max, screen_min)) * 2.;
if clip_min.z > 1. && clip_max.z > 1. {
desired_lod_level = 0;
}
Expand All @@ -140,33 +140,34 @@ fn main(
let meshlet_lod_level = meshlet.mesh_index_and_lod_level & 7u;
let lod_level = u32(desired_lod_level);
if(meshlet_lod_level < lod_level) {
let max_lod_level = i32(MAX_LOD_LEVELS);
if(meshlet.child_meshlets.x >= 0) {
let result = atomicCompareExchangeWeak(&processing_data[meshlet.child_meshlets.x], -1, desired_lod_level);
if(result.exchanged) {
let v = atomicMin(&processing_data[meshlet.child_meshlets.x], desired_lod_level);
if (v == max_lod_level) {
let child_index = atomicAdd(&global_count, 1u);
atomicStore(&meshlet_culling_data[child_index], u32(meshlet.child_meshlets.x));
}
}
if(meshlet.child_meshlets.y >= 0) {
let result = atomicCompareExchangeWeak(&processing_data[meshlet.child_meshlets.y], -1, desired_lod_level);
if(result.exchanged) {
let v = atomicMin(&processing_data[meshlet.child_meshlets.y], desired_lod_level);
if (v == max_lod_level) {
let child_index = atomicAdd(&global_count, 1u);
atomicStore(&meshlet_culling_data[child_index], u32(meshlet.child_meshlets.y));
atomicStore(&meshlet_culling_data[child_index], u32(meshlet.child_meshlets.y));
}
}
if(meshlet.child_meshlets.z >= 0) {
let result = atomicCompareExchangeWeak(&processing_data[meshlet.child_meshlets.z], -1, desired_lod_level);
if(result.exchanged) {
let v = atomicMin(&processing_data[meshlet.child_meshlets.z], desired_lod_level);
if (v == max_lod_level) {
let child_index = atomicAdd(&global_count, 1u);
atomicStore(&meshlet_culling_data[child_index], u32(meshlet.child_meshlets.z));
atomicStore(&meshlet_culling_data[child_index], u32(meshlet.child_meshlets.z));
}
}
if(meshlet.child_meshlets.w >= 0) {
let result = atomicCompareExchangeWeak(&processing_data[meshlet.child_meshlets.w], -1, desired_lod_level);
if(result.exchanged) {
let v = atomicMin(&processing_data[meshlet.child_meshlets.w], desired_lod_level);
if (v == max_lod_level) {
let child_index = atomicAdd(&global_count, 1u);
atomicStore(&meshlet_culling_data[child_index], u32(meshlet.child_meshlets.w));
}
atomicStore(&meshlet_culling_data[child_index], u32(meshlet.child_meshlets.w));
}
}
}
else if(meshlet_lod_level == lod_level)
Expand Down

0 comments on commit 92d9567

Please sign in to comment.