Skip to content

Commit

Permalink
HACK(eddyb): avoid push constants on wasm/WebGPU.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Apr 15, 2023
1 parent 8f1d75b commit 3ef6ae0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
17 changes: 12 additions & 5 deletions examples/runners/wgpu/src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ async fn run(
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[],
// HACK(eddyb) disabling push constants to test WebGPU.
#[cfg(target_arch = "wasm32")]
push_constant_ranges: &[],
#[cfg(not(target_arch = "wasm32"))]
push_constant_ranges: &[wgpu::PushConstantRange {
stages: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
range: 0..std::mem::size_of::<ShaderConstants>() as u32,
Expand Down Expand Up @@ -305,11 +309,14 @@ async fn run(
};

rpass.set_pipeline(render_pipeline);
rpass.set_push_constants(
wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
0,
bytemuck::bytes_of(&push_constants),
);
// HACK(eddyb) disabling push constants to test WebGPU.
if cfg!(not(target_arch = "wasm32")) {
rpass.set_push_constants(
wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
0,
bytemuck::bytes_of(&push_constants),
);
}
rpass.draw(0..3, 0..1);
}

Expand Down
22 changes: 10 additions & 12 deletions examples/shaders/sky-shader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,8 @@ fn get_ray_dir(uv: Vec2, pos: Vec3, look_at_pos: Vec3) -> Vec3 {
(forward + uv.x * right + uv.y * up).normalize()
}

pub fn fs(constants: &ShaderConstants, frag_coord: Vec2) -> Vec4 {
let mut uv = (frag_coord - 0.5 * vec2(constants.width as f32, constants.height as f32))
/ constants.height as f32;
uv.y = -uv.y;
pub fn fs(uv: Vec2) -> Vec4 {
let uv = uv - 0.5;

// hard-code information because we can't bind buffers at the moment
let eye_pos = vec3(0.0, 0.0997, 0.2);
Expand All @@ -150,23 +148,23 @@ pub fn fs(constants: &ShaderConstants, frag_coord: Vec2) -> Vec4 {
}

#[spirv(fragment)]
pub fn main_fs(
#[spirv(frag_coord)] in_frag_coord: Vec4,
#[spirv(push_constant)] constants: &ShaderConstants,
output: &mut Vec4,
) {
let frag_coord = vec2(in_frag_coord.x, in_frag_coord.y);
*output = fs(constants, frag_coord);
pub fn main_fs(uv: Vec2, output: &mut Vec4) {
*output = fs(uv);
}

#[spirv(vertex)]
pub fn main_vs(#[spirv(vertex_index)] vert_idx: i32, #[spirv(position)] builtin_pos: &mut Vec4) {
pub fn main_vs(
#[spirv(vertex_index)] vert_idx: i32,
#[spirv(position)] builtin_pos: &mut Vec4,
out_uv: &mut Vec2,
) {
// Create a "full screen triangle" by mapping the vertex index.
// ported from https://www.saschawillems.de/blog/2016/08/13/vulkan-tutorial-on-rendering-a-fullscreen-quad-without-buffers/
let uv = vec2(((vert_idx << 1) & 2) as f32, (vert_idx & 2) as f32);
let pos = 2.0 * uv - Vec2::ONE;

*builtin_pos = pos.extend(0.0).extend(1.0);
*out_uv = uv;
}

#[cfg(test)]
Expand Down

0 comments on commit 3ef6ae0

Please sign in to comment.