-
Notifications
You must be signed in to change notification settings - Fork 20
export some of the shader loading functionality #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
pwfff
wants to merge
1
commit into
compute-toys:master
Choose a base branch
from
pwfff:pwf/shader-loader
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| use pollster::block_on; | ||
| use reqwest_middleware::ClientWithMiddleware; | ||
| use serde::{Deserialize, Serialize}; | ||
| use std::error::Error; | ||
|
|
||
| use crate::utils::fetch; | ||
|
|
||
| pub struct Shader { | ||
| pub shader: String, | ||
| pub meta: ShaderMeta, | ||
| pub textures: Vec<LoadedTexture>, | ||
| } | ||
|
|
||
| #[derive(Serialize, Deserialize, Debug, Default)] | ||
| #[serde(rename_all = "camelCase")] | ||
| pub struct ShaderMeta { | ||
| pub uniforms: Vec<Uniform>, | ||
| pub textures: Vec<Texture>, | ||
| #[serde(default)] | ||
| pub float32_enabled: bool, | ||
| } | ||
|
|
||
| #[derive(Serialize, Deserialize, Debug, Clone)] | ||
| pub struct Uniform { | ||
| pub name: String, | ||
| pub value: f32, | ||
| } | ||
|
|
||
| #[derive(Serialize, Deserialize, Debug)] | ||
| pub struct Texture { | ||
| pub img: String, | ||
| } | ||
|
|
||
| pub struct LoadedTexture { | ||
| pub img: String, | ||
| pub data: Vec<u8>, | ||
| } | ||
|
|
||
| pub fn load_shader_meta(json: String) -> Result<ShaderMeta, Box<dyn Error>> { | ||
| Ok(serde_json::from_str(&json)?) | ||
| } | ||
|
|
||
| pub fn load_shader<S: Loader, T: Loader>( | ||
| source_loader: S, | ||
| texture_loader: T, | ||
| name: &String, | ||
| ) -> Result<Shader, String> { | ||
| let shader_filename = format!("{name}.wgsl"); | ||
| let meta_filename = format!("{name}.wgsl.json"); | ||
|
|
||
| let shader_source = source_loader.load_string(&shader_filename)?; | ||
| let meta = if let Ok(meta_json) = source_loader.load_string(&meta_filename) { | ||
| load_shader_meta(meta_json) | ||
| .map_err(|e| format!("error loading meta for {}: {:?}", name, e))? | ||
| } else { | ||
| ShaderMeta::default() | ||
| }; | ||
|
|
||
| let textures = meta | ||
| .textures | ||
| .iter() | ||
| .map(|t| { | ||
| let data = texture_loader.load_bytes(&t.img)?; | ||
| Ok(LoadedTexture { | ||
| img: t.img.clone(), | ||
| data, | ||
| }) | ||
| }) | ||
| .collect::<Result<Vec<LoadedTexture>, String>>()?; | ||
|
|
||
| Ok(Shader { | ||
| shader: shader_source, | ||
| meta, | ||
| textures, | ||
| }) | ||
| } | ||
|
|
||
| pub trait Loader { | ||
| fn load_bytes(&self, path: &String) -> Result<Vec<u8>, String>; | ||
|
|
||
| fn load_string(&self, path: &String) -> Result<String, String> { | ||
| String::from_utf8(self.load_bytes(path)?) | ||
| .map_err(|e| format!("error reading {} as utf8: {:?}", path, e)) | ||
| } | ||
| } | ||
|
|
||
| pub struct FolderLoader { | ||
| base_path: String, | ||
| } | ||
|
|
||
| impl FolderLoader { | ||
| pub fn new(base_path: String) -> Self { | ||
| Self { base_path } | ||
| } | ||
| } | ||
|
|
||
| impl Loader for &FolderLoader { | ||
| fn load_bytes(&self, path: &String) -> Result<Vec<u8>, String> { | ||
| Ok(std::fs::read(format!("{}/{path}", self.base_path)) | ||
| .map_err(|e| format!("error including file {}: {:?}", path, e))?) | ||
| } | ||
| } | ||
|
|
||
| pub struct WebLoader { | ||
| client: ClientWithMiddleware, | ||
| } | ||
|
|
||
| impl WebLoader { | ||
| pub fn new() -> Self { | ||
| let client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new()) | ||
| .with(reqwest_middleware_cache::Cache { | ||
| mode: reqwest_middleware_cache::CacheMode::Default, | ||
| cache_manager: reqwest_middleware_cache::managers::CACacheManager::default(), | ||
| }) | ||
| .build(); | ||
| Self { client } | ||
| } | ||
| } | ||
|
|
||
| impl Loader for &WebLoader { | ||
| fn load_bytes(&self, path: &String) -> Result<Vec<u8>, String> { | ||
| block_on(async { | ||
| let url = if path.starts_with("http") { | ||
| path.clone() | ||
| } else { | ||
| std::format!("https://compute.toys/{}", path) | ||
| }; | ||
| let resp = self | ||
| .client | ||
| .get(&url) | ||
| .send() | ||
| .await | ||
| .map_err(|e| format!("{:?}", e))?; | ||
| Ok(resp.bytes().await.map_err(|e| format!("{:?}", e))?.to_vec()) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for this change?