khora_io/asset/decoders/shader.rs
1// Copyright 2025 eraflo
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Shader decoder: raw `.wgsl` bytes → `CpuShaderSource` (UTF-8).
16//!
17//! Auto-registered via [`inventory::submit!`] under the canonical
18//! `"shader"` slot. Pure CPU work; the hot-reload pump feeds the decoded
19//! string to `PipelineSystem::set_overlay_source`.
20
21use anyhow::{Context, Result};
22use khora_core::renderer::api::resource::CpuShaderSource;
23
24use crate::asset::{AssetDecoder, DecoderRegistration};
25
26/// Decodes a `.wgsl` shader file into a `CpuShaderSource` (validated UTF-8).
27#[derive(Clone, Default)]
28pub struct ShaderDecoder;
29
30impl AssetDecoder<CpuShaderSource> for ShaderDecoder {
31 fn load(
32 &self,
33 bytes: &[u8],
34 ) -> Result<CpuShaderSource, Box<dyn std::error::Error + Send + Sync + 'static>> {
35 let source =
36 String::from_utf8(bytes.to_vec()).context("Shader source is not valid UTF-8")?;
37 Ok(CpuShaderSource(source))
38 }
39}
40
41inventory::submit! {
42 DecoderRegistration {
43 type_name: "shader",
44 register: |svc| {
45 svc.register_decoder::<CpuShaderSource>("shader", ShaderDecoder);
46 },
47 }
48}