Skip to content

Commit

Permalink
fix: sorted texture binding
Browse files Browse the repository at this point in the history
  • Loading branch information
mosure committed Jan 2, 2024
1 parent 24b0abe commit 02ff96b
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ default = [
"query_select",
"query_sparse",

"sort_radix",
# "sort_radix",
"sort_rayon",
"sort_std",

Expand Down
10 changes: 9 additions & 1 deletion src/morph/particle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ use bevy::{
RenderAssets,
RenderAssetPlugin,
},
render_resource::*,
render_resource::{
Buffer,
BufferInitDescriptor,
BufferUsages,
Extent3d,
ShaderType,
TextureDimension,
TextureFormat,
},
renderer::{
RenderContext,
RenderDevice,
Expand Down
34 changes: 30 additions & 4 deletions src/render/gaussian.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,36 @@
get_scale,
get_opacity,
get_visibility,
location,
}
#endif


#ifdef BUFFER_STORAGE
@group(3) @binding(0) var<storage, read> sorted_entries: array<Entry>;

fn get_entry(index: u32) -> Entry {
return sorted_entries[index];
}
#endif

#ifdef BUFFER_TEXTURE
@group(3) @binding(0) var sorted_entries: texture_2d<u32>;

fn get_entry(index: u32) -> Entry {
let sample = textureLoad(
sorted_entries,
location(index),
0,
);

return Entry(
sample.r,
sample.g,
);
}
#endif

struct GaussianVertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) @interpolate(flat) color: vec4<f32>,
Expand Down Expand Up @@ -239,11 +263,13 @@ fn vs_points(
@builtin(vertex_index) vertex_index: u32,
) -> GaussianVertexOutput {
var output: GaussianVertexOutput;
let splat_index = sorted_entries[instance_index][1];

let entry = get_entry(instance_index);
let splat_index = entry.value;

var discard_quad = false;

discard_quad |= sorted_entries[instance_index][0] == 0xFFFFFFFFu; // || splat_index == 0u;
discard_quad |= entry.key == 0xFFFFFFFFu; // || splat_index == 0u;

let position = vec4<f32>(get_position(splat_index), 1.0);

Expand Down Expand Up @@ -277,8 +303,8 @@ fn vs_points(
var rgb = vec3<f32>(0.0);

#ifdef VISUALIZE_DEPTH
let first_position = vec4<f32>(get_position(sorted_entries[1][1]), 1.0);
let last_position = vec4<f32>(get_position(sorted_entries[gaussian_uniforms.count - 1u][1]), 1.0);
let first_position = vec4<f32>(get_position(get_entry(1u).value), 1.0);
let last_position = vec4<f32>(get_position(get_entry(gaussian_uniforms.count - 1u).value), 1.0);

let min_position = (gaussian_uniforms.global_transform * first_position).xyz;
let max_position = (gaussian_uniforms.global_transform * last_position).xyz;
Expand Down
61 changes: 59 additions & 2 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,43 @@ use bevy::{
SetItemPipeline,
TrackedRenderPass,
},
render_resource::*,
render_resource::{
BindGroup,
BindGroupEntry,
BindGroupLayout,
BindGroupLayoutDescriptor,
BindGroupLayoutEntry,
BindingResource,
BindingType,
BlendState,
Buffer,
BufferBinding,
BufferBindingType,
BufferInitDescriptor,
BufferUsages,
ColorTargetState,
ColorWrites,
CompareFunction,
DepthBiasState,
DepthStencilState,
FragmentState,
FrontFace,
MultisampleState,
PipelineCache,
PolygonMode,
PrimitiveState,
PrimitiveTopology,
RenderPipelineDescriptor,
ShaderDefVal,
ShaderStages,
ShaderType,
SpecializedRenderPipeline,
SpecializedRenderPipelines,
StencilFaceState,
StencilState,
TextureFormat,
VertexState,
},
renderer::RenderDevice,
Render,
RenderApp,
Expand Down Expand Up @@ -393,7 +429,7 @@ impl FromWorld for GaussianCloudPipeline {
#[cfg(feature = "buffer_texture")]
let gaussian_cloud_layout = texture::get_bind_group_layout(&render_device, read_only);

// TODO: support sorted layout as a texture
#[cfg(feature = "buffer_storage")]
let sorted_layout = render_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
label: Some("sorted_layout"),
entries: &[
Expand All @@ -409,6 +445,8 @@ impl FromWorld for GaussianCloudPipeline {
},
],
});
#[cfg(feature = "buffer_texture")]
let sorted_layout = texture::get_sorted_bind_group_layout(&render_device);

GaussianCloudPipeline {
gaussian_cloud_layout,
Expand Down Expand Up @@ -742,6 +780,9 @@ fn queue_gaussian_bind_group(
&Handle<SortedEntries>,
&texture::GpuTextureBuffers,
)>,

#[cfg(feature = "buffer_texture")]
gpu_images: Res<RenderAssets<Image>>,
) {
let Some(model) = gaussian_uniforms.buffer() else {
return;
Expand Down Expand Up @@ -788,7 +829,9 @@ fn queue_gaussian_bind_group(
continue;
}

#[cfg(not(feature = "buffer_texture"))]
let cloud: &GpuGaussianCloud = gaussian_cloud_res.get(cloud_handle).unwrap();

let sorted_entries = sorted_entries_res.get(sorted_entries_handle).unwrap();

#[cfg(feature = "packed")]
Expand All @@ -798,6 +841,7 @@ fn queue_gaussian_bind_group(
#[cfg(feature = "buffer_texture")]
let cloud_bind_group = texture_buffers.bind_group.clone();

#[cfg(feature = "buffer_storage")]
let sorted_bind_group = render_device.create_bind_group(
"render_sorted_bind_group",
&gaussian_cloud_pipeline.sorted_layout,
Expand All @@ -812,6 +856,19 @@ fn queue_gaussian_bind_group(
},
],
);
#[cfg(feature = "buffer_texture")]
let sorted_bind_group = render_device.create_bind_group(
Some("render_sorted_bind_group"),
&gaussian_cloud_pipeline.sorted_layout,
&[
BindGroupEntry {
binding: 0,
resource: BindingResource::TextureView(
&gpu_images.get(&sorted_entries.texture).unwrap().texture_view
),
},
],
);

commands.entity(entity).insert(GaussianCloudBindGroup {
cloud_bind_group,
Expand Down
10 changes: 9 additions & 1 deletion src/render/packed.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
use bevy::render::{
render_resource::*,
render_resource::{
Buffer,
BufferInitDescriptor,
BufferUsages,
Extent3d,
ShaderType,
TextureDimension,
TextureFormat,
},
renderer::RenderDevice,
};

Expand Down
10 changes: 9 additions & 1 deletion src/render/planar.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
use bevy::render::{
render_resource::*,
render_resource::{
Buffer,
BufferInitDescriptor,
BufferUsages,
Extent3d,
ShaderType,
TextureDimension,
TextureFormat,
},
renderer::RenderDevice,
};

Expand Down
60 changes: 49 additions & 11 deletions src/render/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@ use bevy::{
RenderApp,
RenderSet,
render_asset::RenderAssets,
render_resource::*,
render_resource::{
BindGroup,
BindGroupLayout,
BindGroupLayoutDescriptor,
BindGroupLayoutEntry,
BindGroupEntry,
BindingType,
BindingResource,
Extent3d,
TextureDimension,
TextureFormat,
TextureSampleType,
TextureUsages,
TextureViewDimension,
ShaderStages,
},
renderer::RenderDevice,
},
};
Expand All @@ -27,15 +42,15 @@ use crate::{
ScaleOpacity,
},
},
render::{
GaussianCloudPipeline,
GpuGaussianCloud,
},
material::spherical_harmonics::{
SH_COEFF_COUNT,
SH_VEC4_PLANES,
SphericalHarmonicCoefficients,
},
render::{
GaussianCloudPipeline,
GpuGaussianCloud,
},
};


Expand Down Expand Up @@ -275,6 +290,27 @@ fn queue_textures(
}


pub fn get_sorted_bind_group_layout(
render_device: &RenderDevice,
) -> BindGroupLayout {
render_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
label: Some("texture_sorted_layout"),
entries: &[
BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::all(),
ty: BindingType::Texture {
view_dimension: TextureViewDimension::D2,
sample_type: TextureSampleType::Uint,
multisampled: false,
},
count: None,
},
],
})
}


#[cfg(feature = "f16")]
pub fn get_bind_group_layout(
render_device: &RenderDevice,
Expand All @@ -287,21 +323,23 @@ pub fn get_bind_group_layout(
};

render_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
label: Some("planar_f16_gaussian_cloud_layout"),
label: Some("texture_f16_gaussian_cloud_layout"),
entries: &[
BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::VERTEX_FRAGMENT,
visibility: ShaderStages::all(),
ty: BindingType::Texture {
view_dimension: TextureViewDimension::D2,
sample_type: TextureSampleType::Uint,
sample_type: TextureSampleType::Float {
filterable: false,
},
multisampled: false,
},
count: None,
},
BindGroupLayoutEntry {
binding: 1,
visibility: ShaderStages::VERTEX_FRAGMENT,
visibility: ShaderStages::all(),
ty: BindingType::Texture {
view_dimension: sh_view_dimension,
sample_type: TextureSampleType::Uint,
Expand All @@ -311,7 +349,7 @@ pub fn get_bind_group_layout(
},
BindGroupLayoutEntry {
binding: 2,
visibility: ShaderStages::VERTEX_FRAGMENT,
visibility: ShaderStages::all(),
ty: BindingType::Texture {
view_dimension: TextureViewDimension::D2,
sample_type: TextureSampleType::Uint,
Expand All @@ -330,7 +368,7 @@ pub fn get_bind_group_layout(
read_only: bool
) -> BindGroupLayout {
render_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
label: Some("planar_f32_gaussian_cloud_layout"),
label: Some("texture_f32_gaussian_cloud_layout"),
entries: &[
BindGroupLayoutEntry {
binding: 0,
Expand Down
Loading

0 comments on commit 02ff96b

Please sign in to comment.