Skip to content

Commit

Permalink
Shader code paths (#13908)
Browse files Browse the repository at this point in the history
# Objective

Add extra metadata for the shader examples that contains the location of
their associated shader file(s). This is to be used for the bevy website
shader examples so that the shader code is underneath the rust code.

## Solution

Parse the example rust files for mentions of `.wgsl`, `.frag`, and
`.vert`, then append the found paths to a field called
`shader_code_paths` in the generated `index.md`s for each shader
example.
  • Loading branch information
AndrewDanial authored Jun 19, 2024
1 parent 6b2d483 commit f8014e0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions tools/example-showcase/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ toml_edit = { version = "0.22.7", default-features = false, features = [
"parse",
] }
pbr = "1.1"
regex = "1.10.5"

[lints]
workspace = true
Expand Down
21 changes: 19 additions & 2 deletions tools/example-showcase/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Tool to run all examples or generate a showcase page for the Bevy website.

use std::{
collections::{hash_map::DefaultHasher, HashMap},
collections::{hash_map::DefaultHasher, HashMap, HashSet},
fmt::Display,
fs::{self, File},
hash::{Hash, Hasher},
Expand All @@ -14,6 +14,7 @@ use std::{

use clap::{error::ErrorKind, CommandFactory, Parser, ValueEnum};
use pbr::ProgressBar;
use regex::Regex;
use toml_edit::DocumentMut;
use xshell::{cmd, Shell};

Expand Down Expand Up @@ -546,6 +547,7 @@ technical_name = \"{}\"
link = \"/examples{}/{}/{}\"
image = \"../static/screenshots/{}/{}.png\"
code_path = \"content/examples{}/{}\"
shader_code_paths = {:?}
github_code_path = \"{}\"
header_message = \"Examples ({})\"
+++",
Expand Down Expand Up @@ -580,6 +582,7 @@ header_message = \"Examples ({})\"
.skip(1)
.collect::<PathBuf>()
.display(),
to_show.shader_paths,
&to_show.path,
match api {
WebApi::Webgpu => "WebGPU",
Expand Down Expand Up @@ -723,6 +726,18 @@ fn parse_examples() -> Vec<Example> {
.flat_map(|val| {
let technical_name = val.get("name").unwrap().as_str().unwrap().to_string();

let source_code = fs::read_to_string(val["path"].as_str().unwrap()).unwrap();
let shader_regex =
Regex::new(r"(shaders\/\w+\.wgsl)|(shaders\/\w+\.frag)|(shaders\/\w+\.vert)")
.unwrap();

// Find all instances of references to shader files, collect into set to avoid duplicates, then convert to vec of strings.
let shader_paths = Vec::from_iter(
shader_regex
.find_iter(&source_code)
.map(|matches| matches.as_str().to_owned())
.collect::<HashSet<String>>(),
);
if metadatas
.get(&technical_name)
.and_then(|metadata| metadata.get("hidden"))
Expand All @@ -736,6 +751,7 @@ fn parse_examples() -> Vec<Example> {
metadatas.get(&technical_name).map(|metadata| Example {
technical_name,
path: val["path"].as_str().unwrap().to_string(),
shader_paths,
name: metadata["name"].as_str().unwrap().to_string(),
description: metadata["description"].as_str().unwrap().to_string(),
category: metadata["category"].as_str().unwrap().to_string(),
Expand Down Expand Up @@ -780,9 +796,10 @@ struct Example {
technical_name: String,
/// Path to the example file
path: String,
/// Path to the associated wgsl file if it exists
shader_paths: Vec<String>,
/// List of non default required features
required_features: Vec<String>,

// From the example metadata
/// Pretty name, used for display
name: String,
Expand Down

0 comments on commit f8014e0

Please sign in to comment.