Skip to content

Commit f9b490c

Browse files
committed
0.10 shader features (#564)
1 parent a1ffac5 commit f9b490c

File tree

1 file changed

+85
-0
lines changed
  • content/news/2023-03-04-bevy-0.10

1 file changed

+85
-0
lines changed

content/news/2023-03-04-bevy-0.10/index.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,91 @@ app from the app to send it between the main thread and the rendering thread. Th
690690
only valid to do after all the plugin build methods have been called, because any plugin may
691691
want to modify the rendering sub app.
692692

693+
## ShaderDef Values
694+
695+
<div class="release-feature-authors">authors: @mockersf</div>
696+
697+
Bevy's shader processor now supports ShaderDefs with values, using the new [`ShaderDefVal`]. This allows developers to pass constant values into their shaders:
698+
699+
```rust
700+
let shader_defs = vec![
701+
ShaderDefVal::Int("MAX_DIRECTIONAL_LIGHTS".to_string(), 10),
702+
];
703+
```
704+
705+
These can be used in `#if` statements to selectively enable shader code based on the value:
706+
707+
```rust
708+
#if MAX_DIRECTIONAL_LIGHTS >= 10
709+
let color = vec4<f32>(1.0, 0.0, 0.0, 1.0);
710+
#else
711+
let color = vec4<f32>(0.0, 1.0, 0.0, 1.0);
712+
#endif
713+
```
714+
715+
ShaderDef values can be inlined into shaders:
716+
717+
```rust
718+
for (var i: u32 = 0u; i < #{MAX_DIRECTIONAL_LIGHTS}; i = i + 1u) {
719+
}
720+
```
721+
722+
They can also be defined inline in shaders:
723+
724+
```rust
725+
#define MAX_DIRECTIONAL_LIGHTS 10
726+
```
727+
728+
ShaderDefs defined in shaders override values passed in from Bevy.
729+
730+
[`ShaderDefVal`]: https://docs.rs/bevy/0.10.0/bevy/render/render_resource/enum.ShaderDefVal.html
731+
732+
## `#else ifdef` Chains in Shaders
733+
734+
<div class="release-feature-authors">authors: @torsteingrindvik</div>
735+
736+
Bevy's shader processor now also supports `#else ifdef` chains like this:
737+
738+
```rust
739+
#ifdef FOO
740+
// foo code
741+
#else ifdef BAR
742+
// bar code
743+
#else ifdef BAZ
744+
// baz code
745+
#else
746+
// fallback code
747+
#endif
748+
```
749+
750+
## New Shader Imports: Global and View
751+
752+
<div class="release-feature-authors">authors: @torsteingrindvik</div>
753+
754+
The `Global` and `View` structs are now importable in shaders using `#import bevy_render::globals` and `#import bevy_render::view`. Bevy's internal shaders now use these imports (saving a lot of redundancy). Previously you either needed to re-define in each shader or import the larger `bevy_pbr::mesh_view_types` (which wasn't always what was needed).
755+
756+
Previously this was needed:
757+
758+
```rust
759+
struct View {
760+
view_proj: mat4x4<f32>,
761+
inverse_view_proj: mat4x4<f32>,
762+
view: mat4x4<f32>,
763+
inverse_view: mat4x4<f32>,
764+
projection: mat4x4<f32>,
765+
inverse_projection: mat4x4<f32>,
766+
world_position: vec3<f32>,
767+
// viewport(x_origin, y_origin, width, height)
768+
viewport: vec4<f32>,
769+
};
770+
```
771+
772+
Now you can just do this!
773+
774+
```rust
775+
#import bevy_render::view
776+
```
777+
693778
## ECS Optimizations
694779

695780
<div class="release-feature-authors">authors: @james7132, @JoJoJet</div>

0 commit comments

Comments
 (0)