Skip to content

Commit

Permalink
use r32float
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Feb 27, 2024
1 parent d48584e commit 3b8e110
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
2 changes: 1 addition & 1 deletion assets/shaders/game_of_life.wgsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@group(0) @binding(0) var texture: texture_storage_2d<rgba8unorm, read_write>;
@group(0) @binding(0) var texture: texture_storage_2d<r32float, read_write>;

fn hash(value: u32) -> u32 {
var state = value;
Expand Down
33 changes: 11 additions & 22 deletions examples/shader/compute_shader_game_of_life.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn setup(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
},
TextureDimension::D2,
&[0, 0, 0, 255],
TextureFormat::Rgba8Unorm,
TextureFormat::R32Float,
RenderAssetUsages::RENDER_WORLD,
);
image.texture_descriptor.usage =
Expand All @@ -75,43 +75,32 @@ struct GameOfLifeComputePlugin;
struct GameOfLifeLabel;

impl Plugin for GameOfLifeComputePlugin {
fn build(&self, _app: &mut App) {}
fn build(&self, app: &mut App) {
// Extract the game of life image resource from the main world into the render world
// for operation on by the compute shader and display on the sprite.
app.add_plugins(ExtractResourcePlugin::<GameOfLifeImage>::default());

fn finish(&self, app: &mut App) {
let render_app = app.sub_app_mut(RenderApp);

if !dbg!(
render_app
.world
.resource::<RenderAdapter>()
.get_texture_format_features(TextureFormat::Rgba8Unorm)
.allowed_usages
)
.contains(TextureUsages::STORAGE_BINDING)
{
error!("Rgba8Unorm texture format does not support STORAGE_BINDING usage, exiting.");
app.world.send_event(AppExit);
return;
}

render_app.init_resource::<GameOfLifePipeline>();
render_app.add_systems(
Render,
prepare_bind_group.in_set(RenderSet::PrepareBindGroups),
);
let mut render_graph = render_app.world.resource_mut::<RenderGraph>();
render_graph.add_node(GameOfLifeLabel, GameOfLifeNode::default());
render_graph.add_node_edge(GameOfLifeLabel, bevy::render::graph::CameraDriverLabel);
}

// Extract the game of life image resource from the main world into the render world
// for operation on by the compute shader and display on the sprite.
app.add_plugins(ExtractResourcePlugin::<GameOfLifeImage>::default());
fn finish(&self, app: &mut App) {
let render_app = app.sub_app_mut(RenderApp);

render_app.init_resource::<GameOfLifePipeline>();
}
}

#[derive(Resource, Clone, Deref, ExtractResource, AsBindGroup)]
struct GameOfLifeImage {
#[storage_texture(0, image_format = Rgba8Unorm, access = ReadWrite)]
#[storage_texture(0, image_format = R32Float, access = ReadWrite)]
texture: Handle<Image>,
}

Expand Down

0 comments on commit 3b8e110

Please sign in to comment.