Skip to content

Commit

Permalink
Add uv offset & scale to quad
Browse files Browse the repository at this point in the history
  • Loading branch information
hakolao committed Jan 5, 2024
1 parent 3ad430a commit 8d6740d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 18 deletions.
6 changes: 3 additions & 3 deletions examples/quad/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ fn create_tree_texture(app: &GlassContext) -> Texture {
"tree.png",
TextureFormat::Rgba8UnormSrgb,
&SamplerDescriptor {
address_mode_u: AddressMode::ClampToEdge,
address_mode_v: AddressMode::ClampToEdge,
address_mode_w: AddressMode::ClampToEdge,
address_mode_u: AddressMode::Repeat,
address_mode_v: AddressMode::Repeat,
address_mode_w: AddressMode::Repeat,
mag_filter: FilterMode::Linear,
min_filter: FilterMode::Linear,
mipmap_filter: FilterMode::Linear,
Expand Down
84 changes: 70 additions & 14 deletions src/pipelines/quad/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,6 @@ impl QuadPipeline {
pipeline
}

pub fn push_constants(
quad_pos: [f32; 4],
view_proj: [[f32; 4]; 4],
quad_size: [f32; 2],
aa_strength: f32,
) -> QuadPushConstants {
QuadPushConstants {
quad_pos,
view_proj,
dims: quad_size,
aa_strength,
}
}

pub fn create_bind_group(
&self,
device: &Device,
Expand Down Expand Up @@ -154,6 +140,54 @@ impl QuadPipeline {
view_proj: [[f32; 4]; 4],
quad_size: [f32; 2],
aa_strength: f32,
) {
self.draw_inner(
rpass,
bind_group,
quad_pos,
view_proj,
quad_size,
[0.0; 2],
[1.0, 1.0],
aa_strength,
);
}

#[allow(clippy::too_many_arguments)]
pub fn draw_with_uv<'r>(
&'r self,
rpass: &mut RenderPass<'r>,
bind_group: &'r BindGroup,
quad_pos: [f32; 4],
view_proj: [[f32; 4]; 4],
quad_size: [f32; 2],
uv_offset: [f32; 2],
uv_scale: [f32; 2],
aa_strength: f32,
) {
self.draw_inner(
rpass,
bind_group,
quad_pos,
view_proj,
quad_size,
uv_offset,
uv_scale,
aa_strength,
);
}

#[allow(clippy::too_many_arguments)]
fn draw_inner<'r>(
&'r self,
rpass: &mut RenderPass<'r>,
bind_group: &'r BindGroup,
quad_pos: [f32; 4],
view_proj: [[f32; 4]; 4],
quad_size: [f32; 2],
uv_offset: [f32; 2],
uv_scale: [f32; 2],
aa_strength: f32,
) {
rpass.set_pipeline(&self.pipeline);
rpass.set_bind_group(0, bind_group, &[]);
Expand All @@ -166,11 +200,31 @@ impl QuadPipeline {
quad_pos,
view_proj,
quad_size,
uv_offset,
uv_scale,
aa_strength,
)]),
);
rpass.draw_indexed(0..(QUAD_INDICES.len() as u32), 0, 0..1);
}

fn push_constants(
quad_pos: [f32; 4],
view_proj: [[f32; 4]; 4],
quad_size: [f32; 2],
uv_offset: [f32; 2],
uv_scale: [f32; 2],
aa_strength: f32,
) -> QuadPushConstants {
QuadPushConstants {
quad_pos,
view_proj,
dims: quad_size,
uv_offset,
uv_scale,
aa_strength,
}
}
}

/// Quad instance specific values passed to the shader.
Expand All @@ -180,5 +234,7 @@ pub struct QuadPushConstants {
pub quad_pos: [f32; 4],
pub view_proj: [[f32; 4]; 4],
pub dims: [f32; 2],
pub uv_offset: [f32; 2],
pub uv_scale: [f32; 2],
pub aa_strength: f32,
}
4 changes: 3 additions & 1 deletion src/pipelines/quad/quad.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ struct PushConstants {
quad_pos: vec4<f32>,
view_proj: mat4x4<f32>,
dims: vec2<f32>,
uv_offset: vec2<f32>,
uv_scale: vec2<f32>,
aa_strength: f32,
}
var<push_constant> pc: PushConstants;
Expand All @@ -23,7 +25,7 @@ fn vs_main(
model: VertexInput,
) -> VertexOutput {
var out: VertexOutput;
out.tex_coords = model.tex_coords;
out.tex_coords = (model.tex_coords + pc.uv_offset) / pc.uv_scale;
let world_position = vec4<f32>(pc.dims, 0.0, 1.0) *
// Scale vertices
model.position +
Expand Down

0 comments on commit 8d6740d

Please sign in to comment.