Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 994ffc7

Browse files
committedMay 19, 2025·
use modules and str's for the compiled identifier mapping.
This makes the webgl renderer backend compile safe.
1 parent ef903a9 commit 994ffc7

File tree

4 files changed

+98
-216
lines changed

4 files changed

+98
-216
lines changed
 

‎sparse_strips/vello_hybrid/src/render/webgl.rs

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ use alloc::vec::Vec;
3131
use bytemuck::{Pod, Zeroable};
3232
use core::{fmt::Debug, mem};
3333
use vello_common::{coarse::WideTile, tile::Tile};
34-
use vello_sparse_shaders::shaders::{clear_slots, render_strips};
35-
use vello_sparse_shaders::types::ReflectionMap;
34+
use vello_sparse_shaders::{clear_slots, render_strips};
3635
use web_sys::wasm_bindgen::{JsCast, JsValue};
3736
use web_sys::{
3837
WebGl2RenderingContext, WebGlBuffer, WebGlFramebuffer, WebGlProgram, WebGlTexture,
@@ -254,22 +253,19 @@ struct ClearSlotsConfig {
254253
impl WebGlPrograms {
255254
/// Creates programs and initializes resources.
256255
fn new(gl: WebGl2RenderingContext, slot_count: usize) -> Self {
257-
let strip_glsl = render_strips();
258-
let clear_glsl = clear_slots();
259-
260-
let strip_program =
261-
create_shader_program(&gl, strip_glsl.vertex.source, strip_glsl.fragment.source);
262-
let clear_program =
263-
create_shader_program(&gl, clear_glsl.vertex.source, clear_glsl.fragment.source);
264-
265-
let strip_uniforms = get_strip_uniforms(
256+
let strip_program = create_shader_program(
266257
&gl,
267-
&strip_program,
268-
&strip_glsl.vertex.reflection_map,
269-
&strip_glsl.fragment.reflection_map,
258+
render_strips::VERTEX_SOURCE,
259+
render_strips::FRAGMENT_SOURCE,
270260
);
271-
let clear_uniforms =
272-
get_clear_uniforms(&gl, &clear_program, &clear_glsl.vertex.reflection_map);
261+
let clear_program = create_shader_program(
262+
&gl,
263+
clear_slots::VERTEX_SOURCE,
264+
clear_slots::FRAGMENT_SOURCE,
265+
);
266+
267+
let strip_uniforms = get_strip_uniforms(&gl, &strip_program);
268+
let clear_uniforms = get_clear_uniforms(&gl, &clear_program);
273269

274270
let resources = create_webgl_resources(&gl, slot_count);
275271

@@ -546,17 +542,12 @@ fn create_shader_program(
546542
program
547543
}
548544

549-
/// Get the uniform locations for the strip program
550-
fn get_strip_uniforms(
551-
gl: &WebGl2RenderingContext,
552-
program: &WebGlProgram,
553-
vertex_reflection: &ReflectionMap<&'static str>,
554-
fragment_reflection: &ReflectionMap<&'static str>,
555-
) -> StripUniforms {
556-
let config_vs_name = vertex_reflection.uniforms.get("config").unwrap();
545+
/// Get the uniform locations for the `render_strips` program.
546+
fn get_strip_uniforms(gl: &WebGl2RenderingContext, program: &WebGlProgram) -> StripUniforms {
547+
let config_vs_name = render_strips::vertex::CONFIG;
557548
let config_vs_block_index = gl.get_uniform_block_index(program, config_vs_name);
558549

559-
let config_fs_name = fragment_reflection.uniforms.get("config").unwrap();
550+
let config_fs_name = render_strips::fragment::CONFIG;
560551
let config_fs_block_index = gl.get_uniform_block_index(program, config_fs_name);
561552

562553
debug_assert_ne!(
@@ -575,14 +566,8 @@ fn get_strip_uniforms(
575566
gl.uniform_block_binding(program, config_fs_block_index, 0);
576567

577568
// Get texture uniform locations.
578-
let alphas_texture_name = fragment_reflection
579-
.texture_mapping
580-
.get("alphas_texture")
581-
.unwrap();
582-
let clip_input_texture_name = fragment_reflection
583-
.texture_mapping
584-
.get("clip_input_texture")
585-
.unwrap();
569+
let alphas_texture_name = render_strips::fragment::ALPHAS_TEXTURE;
570+
let clip_input_texture_name = render_strips::fragment::CLIP_INPUT_TEXTURE;
586571

587572
StripUniforms {
588573
config_vs_block_index,
@@ -596,13 +581,9 @@ fn get_strip_uniforms(
596581
}
597582
}
598583

599-
/// Get the uniform locations for the clear program.
600-
fn get_clear_uniforms(
601-
gl: &WebGl2RenderingContext,
602-
program: &WebGlProgram,
603-
vertex_reflection: &ReflectionMap<&'static str>,
604-
) -> ClearUniforms {
605-
let config_name = vertex_reflection.uniforms.get("config").unwrap();
584+
/// Get the uniform locations for the `clear_slots` program.
585+
fn get_clear_uniforms(gl: &WebGl2RenderingContext, program: &WebGlProgram) -> ClearUniforms {
586+
let config_name = clear_slots::vertex::CONFIG;
606587
let config_block_index = gl.get_uniform_block_index(program, config_name);
607588

608589
debug_assert_ne!(

‎sparse_strips/vello_sparse_shaders/build.rs

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ mod types;
1717

1818
use compile::compile_wgsl_shader;
1919

20-
// TODO: Format the generated code via `rustfmt`.
2120
// TODO: Use `quote` instead of string concatenation to generate code.
2221
fn main() {
2322
// Rerun build if the shaders directory changes
@@ -63,48 +62,18 @@ fn generate_compiled_shaders_module(buf: &mut String, shader_infos: &[(String, S
6362
"// Generated code by `vello_sparse_shaders` - DO NOT EDIT"
6463
)
6564
.unwrap();
66-
writeln!(
67-
buf,
68-
"use crate::types::{{CompiledGlsl, ReflectionMap, Stage}};"
69-
)
70-
.unwrap();
7165
writeln!(
7266
buf,
7367
"/// Build time GLSL shaders derived from wgsl shaders."
7468
)
7569
.unwrap();
76-
writeln!(buf, "pub mod shaders {{").unwrap();
77-
writeln!(
78-
buf,
79-
r#" #![allow(unused_mut, reason = "Increases code gen complexity")]"#
80-
)
81-
.unwrap();
82-
writeln!(
83-
buf,
84-
" use super::{{CompiledGlsl, ReflectionMap, Stage}};"
85-
)
86-
.unwrap();
87-
88-
// Public interface for accessing the CompiledGlsl struct per shader.
89-
for (shader_name, _) in shader_infos {
90-
writeln!(buf, " /// Compiled glsl for `{shader_name}.wgsl`").unwrap();
91-
writeln!(
92-
buf,
93-
" pub fn {shader_name}() -> CompiledGlsl<&'static str> {{"
94-
)
95-
.unwrap();
96-
writeln!(buf, " {shader_name}_impl()").unwrap();
97-
writeln!(buf, " }}").unwrap();
98-
}
9970

10071
// Implementation for creating a CompiledGlsl struct per shader assuming the standard entry
10172
// names of `vs_main` and `fs_main`.
10273
for (shader_name, shader_source) in shader_infos {
10374
let compiled = compile_wgsl_shader(shader_source, "vs_main", "fs_main");
10475

105-
let generated_code = compiled.to_generated_code(&format!("{shader_name}_impl"));
76+
let generated_code = compiled.to_generated_code(shader_name);
10677
writeln!(buf, "{generated_code}").unwrap();
10778
}
108-
109-
writeln!(buf, "}}\n").unwrap();
11079
}

‎sparse_strips/vello_sparse_shaders/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
//! This is a utility library to help integrate `vello_hybrid` WebGPU wgsl shaders into glsl.
55
66
mod compile;
7-
pub mod types;
8-
9-
extern crate alloc;
7+
mod types;
108

119
include!(concat!(env!("OUT_DIR"), "/compiled_shaders.rs"));

0 commit comments

Comments
 (0)
Please sign in to comment.