Skip to content

Commit

Permalink
fix(gpu): 🐛 use textureSampleLevel to avoid unroll the loop in shader
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Adoo authored and wjian23 committed Sep 20, 2023
1 parent f52ac97 commit cc82633
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ribir_geom = {path = "../geom", version = "0.0.1-alpha.4" }
ribir_painter = {path = "../painter", features = ["tessellation"], version = "0.0.1-alpha.4" }
slab = "0.4.8"
wgpu = {workspace = true, optional = true}
zerocopy.workspace = true
zerocopy = {workspace=true, features = ["derive"]}

[dev-dependencies]
paste.workspace = true
Expand Down
3 changes: 1 addition & 2 deletions gpu/src/wgpu_impl/shaders/color_triangles.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ struct FragInput {
}



@vertex
fn vs_main(v: Vertex) -> FragInput {
var input: FragInput;
Expand Down Expand Up @@ -63,7 +62,7 @@ fn fs_main(input: FragInput) -> @location(0) vec4<f32> {

let tex_size = textureDimensions(texture);
mask_pos = mask_pos / vec2<f32>(f32(tex_size.x), f32(tex_size.y));
let alpha = textureSample(texture, s_sampler, mask_pos).r;
let alpha = textureSampleLevel(texture, s_sampler, mask_pos, 0.).r;
if alpha == 0. {
color.a = 0.;
break;
Expand Down
2 changes: 1 addition & 1 deletion gpu/src/wgpu_impl/shaders/img_triangles.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn fs_main(f: VertexOutput) -> @location(0) vec4<f32> {
let mask_sampler = samplers[mask_tex_idx];
let mask_tex_size = textureDimensions(mask_tex);
mask_pos = mask_pos / vec2<f32>(f32(mask_tex_size.x), f32(mask_tex_size.y));
let alpha = textureSample(mask_tex, mask_sampler, mask_pos).r;
let alpha = textureSampleLevel(mask_tex, mask_sampler, mask_pos, 0.).r;
if alpha == 0. {
color.a = 0.;
break;
Expand Down
2 changes: 1 addition & 1 deletion gpu/src/wgpu_impl/shaders/linear_gradient_triangles.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn calc_mask_alpha(pos: vec2<f32>, mask_idx: i32) -> f32 {

let tex_size = textureDimensions(texture);
mask_pos = mask_pos / vec2<f32>(f32(tex_size.x), f32(tex_size.y));
let a = textureSample(texture, s_sampler, mask_pos).r;
let a = textureSampleLevel(texture, s_sampler, mask_pos, 0.).r;
alpha = alpha * a;
if alpha == 0. {
break;
Expand Down
2 changes: 1 addition & 1 deletion gpu/src/wgpu_impl/shaders/radial_gradient_triangles.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn calc_mask_alpha(pos: vec2<f32>, mask_idx: i32) -> f32 {

let tex_size = textureDimensions(texture);
mask_pos = mask_pos / vec2<f32>(f32(tex_size.x), f32(tex_size.y));
let a = textureSample(texture, s_sampler, mask_pos).r;
let a = textureSampleLevel(texture, s_sampler, mask_pos, 0.).r;
alpha = alpha * a;
if alpha == 0. {
break;
Expand Down

0 comments on commit cc82633

Please sign in to comment.