Material Assets
How to load and use materials and shaders in Byte Engine.
This document intends to describe the process of loading user declared shaders and materials and transforming them into usable resources.
Assets
Materials are declared by the user as a JSON, specifying the material type and references to shaders.
{
"domain":"World",
"type": "Surface",
"shaders": {
"Fragment": "../.."
},
"variables": [
{
"name":"color",
"data_type": "vec4f",
"value":"Purple",
"type":"Static"
}
]
}Materials can also reference other materials as their parent to inherit all their characteristics but they can alter values of variables declared in the parent.
{
"parent":"material",
"variables": [
{
"name": "color",
"value": "Red"
}
]
}The engine can use this relationship for optimization. If the shader is the same and only variable values change, the engine can treat the variants as the same pipeline with different per-instance uniform values.
Do not rely on that optimization as a stable behavior. The engine may increase shader or pipeline permutations when static replacement would produce better runtime code.
Pipeline
Loading
When a load request is performed in the code, the material/variant is first loaded, shaders are then fetched from there, once all assets are loaded, processing starts.
Our objective is to produce shader code and metadata that can be used by a render model during runtime to produce the intended results.
flowchart TD
ma ---> Materials --> Shaders
subgraph Materials
Material
end
subgraph Shaders
Vertex
Fragment
...
end
shader_manager[[Shader Manager]]
Materials ---> shader_manager
Shaders ---> shader_manager
render_model_a[Visibility]
render_model_b[Forward]
shader_manager ---> render_model_a
shader_manager ---> render_model_b
subgraph Resources
subgraph resources.materials[Materials]
Material
end
subgraph resource.shaders[Shaders]
Compute
end
end
render_model_a ---> ResourcesGenerated materials are tagged by render model.
{
"name":"material",
"model": {
"name":"Visibility",
"pass":"MaterialEvaluation",
},
"variables": [
{
"name": "color",
"type": "Static",
"data_type": "vec4f",
"value": "Purple",
}
]
...
}Variables are also defined/extracted from the code.
Generation
Generation details are not complete yet. This section will describe how material declarations become render-model-specific shader code and metadata.