WGSL preprocess and composer utilities aligned with naga_oil.
///|
test "README: package is available" {
let value_defines = @common.default_wgsl_value_defines()
debug_inspect(value_defines.length() > 0, content="true")
}///|
test "README: compose registered modules" {
let composer : @compose.Composer = @compose.Composer::default()
composer.clear_sources()
composer.register_source(
"maths.wgsl", "#define_import_path demo::maths\nconst TWO: f32 = 2.0;\n",
)
composer.register_source(
"main.wgsl", "#import demo::maths::TWO\nfn scale(x: f32) -> f32 {\n return x * TWO;\n}\n",
)
let options : @common.WgslComposeOptions = @common.WgslComposeOptions::default()
let composed = composer.compose_wgsl("main.wgsl", options) catch {
err => abort(err.message())
}
debug_inspect(composed.contains("fn scale"), content="true")
debug_inspect(composed.contains("#import"), content="false")
}///|
test "README: preprocess one shader" {
let defs : Map[String, @common.ShaderDefValue] = Map([])
defs.set("TEXTURE", @common.ShaderDefValue::Bool(true))
let source = "#ifdef TEXTURE\nvar sprite_texture: texture_2d<f32>;\n#else\nvar sprite_texture: texture_2d_array<f32>;\n#endif\n"
let output = @preprocess.Preprocessor::default().preprocess(source, defs) catch {
_ => abort("preprocess failed")
}
debug_inspect(
output.preprocessed_source.contains("texture_2d<f32>"),
content="true",
)
}///|
test "README: inspect metadata" {
let source = "#define_import_path demo::main\n#define HDR\n#import demo::maths::TWO\nfn scale(x: f32) -> f32 {\n return x * TWO;\n}\n"
let metadata = @metadata.get_preprocessor_metadata(source) catch {
_ => abort("metadata extraction failed")
}
debug_inspect(metadata.name, content="Some(\"demo::main\")")
debug_inspect(metadata.imports.length(), content="1")
}///|
test "README: export single file" {
let composer : @compose.Composer = @compose.Composer::default()
composer.clear_sources()
composer.register_source(
"shared.wgsl", "#define_import_path demo::shared\nstruct Value {\n x: f32,\n}\nfn read(value: Value) -> f32 {\n return value.x;\n}\n",
)
composer.register_source(
"main.wgsl", "#import demo::shared::{Value, read}\nfn shade(value: Value) -> f32 {\n return read(value);\n}\n",
)
let compose_options : @common.WgslComposeOptions = @common.WgslComposeOptions::default()
let export_options : @common.WgslExportOptions = { root_items: ["shade"] }
let output = @export.export_wgsl_with_options(
composer, "main.wgsl", compose_options, export_options,
) catch {
err => abort(err.message())
}
debug_inspect(output.source.contains("#import"), content="false")
debug_inspect(output.source.contains("fn shade"), content="true")
debug_inspect(output.diagnostics.length(), content="0")
}#import bevy_render::view::View
#import bevy_render::maths as maths
#import bevy_render::{view::View, globals::Globals}
#import bevy_render::{maths::{PI_2, powsafe}}
#import "shaders/skills/shared.wgsl" Vertex, VertexOutput
#import "../shared/common.wgsl" SharedVertex, build_colormoon test