refactor: gpu webgl fallback and batched command encoding

Made-with: Cursor
This commit is contained in:
Maze Winther
2026-04-13 05:01:51 +02:00
parent 0d0d737563
commit dbd68ff139
8 changed files with 439 additions and 125 deletions
+34 -12
View File
@@ -1,5 +1,5 @@
use bytemuck::{Pod, Zeroable};
use gpu::{FULLSCREEN_SHADER_SOURCE, GPU_TEXTURE_FORMAT, GpuContext};
use gpu::{FULLSCREEN_SHADER_SOURCE, GpuContext};
use wgpu::util::DeviceExt;
use crate::SdfPipeline;
@@ -128,7 +128,7 @@ impl MaskFeatherPipeline {
module: &fragment_shader_module,
entry_point: Some("fragment_main"),
targets: &[Some(wgpu::ColorTargetState {
format: GPU_TEXTURE_FORMAT,
format: context.texture_format(),
blend: None,
write_mask: wgpu::ColorWrites::ALL,
})],
@@ -159,10 +159,41 @@ impl MaskFeatherPipeline {
height,
feather,
}: ApplyMaskFeatherOptions<'_>,
) -> wgpu::Texture {
let mut encoder =
context
.device()
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("gpu-mask-distance-command-encoder"),
});
let output = self.apply_mask_feather_with_encoder(
context,
&mut encoder,
ApplyMaskFeatherOptions {
mask,
width,
height,
feather,
},
);
context.queue().submit([encoder.finish()]);
output
}
pub fn apply_mask_feather_with_encoder(
&self,
context: &GpuContext,
encoder: &mut wgpu::CommandEncoder,
ApplyMaskFeatherOptions {
mask,
width,
height,
feather,
}: ApplyMaskFeatherOptions<'_>,
) -> wgpu::Texture {
let sdf = self
.sdf_pipeline
.compute_signed_distance_field(context, mask, width, height);
.compute_signed_distance_field_with_encoder(context, encoder, mask, width, height);
let output_texture = context.create_render_texture(width, height, "masks-feather-output");
let inside_view = sdf
.inside_texture
@@ -225,13 +256,6 @@ impl MaskFeatherPipeline {
resource: uniform_buffer.as_entire_binding(),
}],
});
let mut encoder =
context
.device()
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("gpu-mask-distance-command-encoder"),
});
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("gpu-mask-distance-render-pass"),
@@ -256,8 +280,6 @@ impl MaskFeatherPipeline {
render_pass.set_bind_group(2, &uniform_bind_group, &[]);
render_pass.draw(0..6, 0..1);
}
context.queue().submit([encoder.finish()]);
output_texture
}
}
+36 -12
View File
@@ -1,5 +1,5 @@
use bytemuck::{Pod, Zeroable};
use gpu::{FULLSCREEN_SHADER_SOURCE, GPU_TEXTURE_FORMAT, GpuContext};
use gpu::{FULLSCREEN_SHADER_SOURCE, GpuContext};
use wgpu::util::DeviceExt;
const JFA_INIT_SHADER_SOURCE: &str = include_str!("shaders/jfa_init.wgsl");
@@ -98,6 +98,7 @@ impl SdfPipeline {
&pipeline_layout,
&vertex_shader_module,
&init_shader_module,
context.texture_format(),
);
let step_pipeline = create_pipeline(
device,
@@ -105,6 +106,7 @@ impl SdfPipeline {
&pipeline_layout,
&vertex_shader_module,
&step_shader_module,
context.texture_format(),
);
Self {
@@ -121,16 +123,42 @@ impl SdfPipeline {
source_texture: &wgpu::Texture,
width: u32,
height: u32,
) -> SignedDistanceFieldTextures {
let mut encoder =
context
.device()
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("gpu-sdf-command-encoder"),
});
let textures = self.compute_signed_distance_field_with_encoder(
context,
&mut encoder,
source_texture,
width,
height,
);
context.queue().submit([encoder.finish()]);
textures
}
pub fn compute_signed_distance_field_with_encoder(
&self,
context: &GpuContext,
encoder: &mut wgpu::CommandEncoder,
source_texture: &wgpu::Texture,
width: u32,
height: u32,
) -> SignedDistanceFieldTextures {
SignedDistanceFieldTextures {
inside_texture: self.run_jfa(context, source_texture, width, height, false),
outside_texture: self.run_jfa(context, source_texture, width, height, true),
inside_texture: self.run_jfa(context, encoder, source_texture, width, height, false),
outside_texture: self.run_jfa(context, encoder, source_texture, width, height, true),
}
}
fn run_jfa(
&self,
context: &GpuContext,
encoder: &mut wgpu::CommandEncoder,
source_texture: &wgpu::Texture,
width: u32,
height: u32,
@@ -141,6 +169,7 @@ impl SdfPipeline {
self.run_pass(
context,
encoder,
source_texture,
&ping_texture,
&self.init_pipeline,
@@ -167,6 +196,7 @@ impl SdfPipeline {
};
self.run_pass(
context,
encoder,
input_texture,
output_texture,
&self.step_pipeline,
@@ -189,6 +219,7 @@ impl SdfPipeline {
fn run_pass(
&self,
context: &GpuContext,
encoder: &mut wgpu::CommandEncoder,
input_texture: &wgpu::Texture,
output_texture: &wgpu::Texture,
pipeline: &wgpu::RenderPipeline,
@@ -230,12 +261,6 @@ impl SdfPipeline {
resource: uniform_buffer.as_entire_binding(),
}],
});
let mut encoder =
context
.device()
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("gpu-sdf-command-encoder"),
});
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
@@ -260,8 +285,6 @@ impl SdfPipeline {
render_pass.set_bind_group(1, &uniform_bind_group, &[]);
render_pass.draw(0..6, 0..1);
}
context.queue().submit([encoder.finish()]);
}
}
@@ -271,6 +294,7 @@ fn create_pipeline(
layout: &wgpu::PipelineLayout,
vertex_shader_module: &wgpu::ShaderModule,
fragment_shader_module: &wgpu::ShaderModule,
texture_format: wgpu::TextureFormat,
) -> wgpu::RenderPipeline {
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some(label),
@@ -293,7 +317,7 @@ fn create_pipeline(
module: fragment_shader_module,
entry_point: Some("fragment_main"),
targets: &[Some(wgpu::ColorTargetState {
format: GPU_TEXTURE_FORMAT,
format: texture_format,
blend: None,
write_mask: wgpu::ColorWrites::ALL,
})],
+4 -1
View File
@@ -24,7 +24,10 @@ fn encode_seed(seed: vec2f) -> vec4f {
@fragment
fn fragment_main(input: VertexOutput) -> @location(0) vec4f {
let mask = textureSample(input_texture, input_sampler, input.tex_coord).r;
let is_seed = select(mask > 0.5, mask < 0.5, uniforms.invert > 0.5);
let above = mask > 0.5;
let below = mask < 0.5;
let inverted = uniforms.invert > 0.5;
let is_seed = select(above, below, inverted);
if (is_seed) {
let pixel_coord = floor(input.tex_coord * uniforms.resolution);
+1 -1
View File
@@ -53,7 +53,7 @@ fn fragment_main(input: VertexOutput) -> @location(0) vec4f {
continue;
}
let encoded = textureSample(input_texture, input_sampler, sample_uv);
let encoded = textureSampleLevel(input_texture, input_sampler, sample_uv, 0.0);
if (is_no_seed(encoded)) {
continue;
}