Skip to content

Commit 8ec6552

Browse files
alice-i-cecileAlice Cecile
and
Alice Cecile
authored
Port bevy_core_pipeline to LinearRgba (#12116)
# Objective - We should move towards a consistent use of the new `bevy_color` crate. - As discussed in #12089, splitting this work up into small pieces makes it easier to review. ## Solution - Port all uses of `LegacyColor` in the `bevy_core_pipeline` to `LinearRgba` - `LinearRgba` is the correct type to use for internal rendering types - Added `LinearRgba::BLACK` and `WHITE` (used during migration) - Add `LinearRgba::grey` to more easily construct balanced grey colors (used during migration) - Add a conversion from `LinearRgba` to `wgpu::Color`. The converse was not done at this time, as this is typically a user error. I did not change the field type of the clear color on the cameras: as this is user-facing, this should be done in concert with the other configurable fields. ## Migration Guide `ColorAttachment` now stores a `LinearRgba` color, rather than a Bevy 0.13 `Color`. `set_blend_constant` now takes a `LinearRgba` argument, rather than a Bevy 0.13 `Color`. --------- Co-authored-by: Alice Cecile <[email protected]>
1 parent 43b859d commit 8ec6552

File tree

9 files changed

+71
-24
lines changed

9 files changed

+71
-24
lines changed

crates/bevy_color/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ bevy_reflect = { path = "../bevy_reflect", version = "0.14.0-dev", features = [
1515
] }
1616
serde = "1.0"
1717
thiserror = "1.0"
18+
wgpu = { version = "0.19.1", default-features = false }
1819

1920
[lints]
2021
workspace = true

crates/bevy_color/src/linear_rgba.rs

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,30 @@ pub struct LinearRgba {
2323
impl StandardColor for LinearRgba {}
2424

2525
impl LinearRgba {
26+
/// A fully black color with full alpha.
27+
pub const BLACK: Self = Self {
28+
red: 0.0,
29+
green: 0.0,
30+
blue: 0.0,
31+
alpha: 1.0,
32+
};
33+
34+
/// A fully white color with full alpha.
35+
pub const WHITE: Self = Self {
36+
red: 1.0,
37+
green: 1.0,
38+
blue: 1.0,
39+
alpha: 1.0,
40+
};
41+
42+
/// A fully transparent color.
43+
pub const NONE: Self = Self {
44+
red: 0.0,
45+
green: 0.0,
46+
blue: 0.0,
47+
alpha: 0.0,
48+
};
49+
2650
/// Construct a new [`LinearRgba`] color from components.
2751
pub const fn new(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
2852
Self {
@@ -49,6 +73,18 @@ impl LinearRgba {
4973
}
5074
}
5175

76+
/// Construct a new [`LinearRgba`] color with the same value for all channels and an alpha of 1.0.
77+
///
78+
/// A value of 0.0 is black, and a value of 1.0 is white.
79+
pub const fn gray(value: f32) -> Self {
80+
Self {
81+
red: value,
82+
green: value,
83+
blue: value,
84+
alpha: 1.0,
85+
}
86+
}
87+
5288
/// Return a copy of this color with the red channel set to the given value.
5389
pub const fn with_red(self, red: f32) -> Self {
5490
Self { red, ..self }
@@ -81,12 +117,7 @@ impl LinearRgba {
81117
impl Default for LinearRgba {
82118
/// Construct a new [`LinearRgba`] color with the default values (white with full alpha).
83119
fn default() -> Self {
84-
Self {
85-
red: 1.,
86-
green: 1.,
87-
blue: 1.,
88-
alpha: 1.,
89-
}
120+
Self::WHITE
90121
}
91122
}
92123

@@ -205,6 +236,17 @@ impl From<Hsla> for LinearRgba {
205236
}
206237
}
207238

239+
impl From<LinearRgba> for wgpu::Color {
240+
fn from(color: LinearRgba) -> Self {
241+
wgpu::Color {
242+
r: color.red as f64,
243+
g: color.green as f64,
244+
b: color.blue as f64,
245+
a: color.alpha as f64,
246+
}
247+
}
248+
}
249+
208250
#[cfg(test)]
209251
mod tests {
210252
use super::*;

crates/bevy_core_pipeline/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ tonemapping_luts = ["bevy_render/ktx2", "bevy_render/zstd"]
2424
bevy_app = { path = "../bevy_app", version = "0.14.0-dev" }
2525
bevy_asset = { path = "../bevy_asset", version = "0.14.0-dev" }
2626
bevy_core = { path = "../bevy_core", version = "0.14.0-dev" }
27+
bevy_color = { path = "../bevy_color", version = "0.14.0-dev" }
2728
bevy_derive = { path = "../bevy_derive", version = "0.14.0-dev" }
2829
bevy_ecs = { path = "../bevy_ecs", version = "0.14.0-dev" }
2930
bevy_log = { path = "../bevy_log", version = "0.14.0-dev" }

crates/bevy_core_pipeline/src/bloom/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod downsampling_pipeline;
22
mod settings;
33
mod upsampling_pipeline;
44

5+
use bevy_color::LinearRgba;
56
pub use settings::{BloomCompositeMode, BloomPrefilterSettings, BloomSettings};
67

78
use crate::{
@@ -17,7 +18,6 @@ use bevy_render::{
1718
extract_component::{
1819
ComponentUniforms, DynamicUniformIndex, ExtractComponentPlugin, UniformComponentPlugin,
1920
},
20-
prelude::LegacyColor,
2121
render_graph::{NodeRunError, RenderGraphApp, RenderGraphContext, ViewNode, ViewNodeRunner},
2222
render_resource::*,
2323
renderer::{RenderContext, RenderDevice},
@@ -244,7 +244,7 @@ impl ViewNode for BloomNode {
244244
mip as f32,
245245
(bloom_texture.mip_count - 1) as f32,
246246
);
247-
upsampling_pass.set_blend_constant(LegacyColor::rgb_linear(blend, blend, blend));
247+
upsampling_pass.set_blend_constant(LinearRgba::gray(blend));
248248
upsampling_pass.draw(0..3, 0..1);
249249
}
250250

@@ -271,7 +271,7 @@ impl ViewNode for BloomNode {
271271
}
272272
let blend =
273273
compute_blend_factor(bloom_settings, 0.0, (bloom_texture.mip_count - 1) as f32);
274-
upsampling_final_pass.set_blend_constant(LegacyColor::rgb_linear(blend, blend, blend));
274+
upsampling_final_pass.set_blend_constant(LinearRgba::gray(blend));
275275
upsampling_final_pass.draw(0..3, 0..1);
276276
}
277277

crates/bevy_core_pipeline/src/core_3d/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub const CORE_3D_DEPTH_FORMAT: TextureFormat = TextureFormat::Depth32Float;
4141
use std::ops::Range;
4242

4343
use bevy_asset::AssetId;
44+
use bevy_color::LinearRgba;
4445
pub use camera_3d::*;
4546
pub use main_opaque_pass_3d_node::*;
4647
pub use main_transparent_pass_3d_node::*;
@@ -49,7 +50,6 @@ use bevy_app::{App, Plugin, PostUpdate};
4950
use bevy_ecs::prelude::*;
5051
use bevy_render::{
5152
camera::{Camera, ExtractedCamera},
52-
color::LegacyColor,
5353
extract_component::ExtractComponentPlugin,
5454
mesh::Mesh,
5555
prelude::Msaa,
@@ -836,18 +836,18 @@ pub fn prepare_prepass_textures(
836836

837837
commands.entity(entity).insert(ViewPrepassTextures {
838838
depth: cached_depth_texture
839-
.map(|t| ColorAttachment::new(t, None, Some(LegacyColor::BLACK))),
839+
.map(|t| ColorAttachment::new(t, None, Some(LinearRgba::BLACK))),
840840
normal: cached_normals_texture
841-
.map(|t| ColorAttachment::new(t, None, Some(LegacyColor::BLACK))),
841+
.map(|t| ColorAttachment::new(t, None, Some(LinearRgba::BLACK))),
842842
// Red and Green channels are X and Y components of the motion vectors
843843
// Blue channel doesn't matter, but set to 0.0 for possible faster clear
844844
// https://gpuopen.com/performance/#clears
845845
motion_vectors: cached_motion_vectors_texture
846-
.map(|t| ColorAttachment::new(t, None, Some(LegacyColor::BLACK))),
846+
.map(|t| ColorAttachment::new(t, None, Some(LinearRgba::BLACK))),
847847
deferred: cached_deferred_texture
848-
.map(|t| ColorAttachment::new(t, None, Some(LegacyColor::BLACK))),
848+
.map(|t| ColorAttachment::new(t, None, Some(LinearRgba::BLACK))),
849849
deferred_lighting_pass_id: cached_deferred_lighting_pass_id_texture
850-
.map(|t| ColorAttachment::new(t, None, Some(LegacyColor::BLACK))),
850+
.map(|t| ColorAttachment::new(t, None, Some(LinearRgba::BLACK))),
851851
size,
852852
});
853853
}

crates/bevy_core_pipeline/src/msaa_writeback.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use crate::{
44
core_3d::graph::{Core3d, Node3d},
55
};
66
use bevy_app::{App, Plugin};
7+
use bevy_color::LinearRgba;
78
use bevy_ecs::prelude::*;
89
use bevy_render::{
910
camera::ExtractedCamera,
10-
color::LegacyColor,
1111
render_graph::{Node, NodeRunError, RenderGraphApp, RenderGraphContext},
1212
renderer::RenderContext,
1313
view::{Msaa, ViewTarget},
@@ -92,7 +92,7 @@ impl Node for MsaaWritebackNode {
9292
view: target.sampled_main_texture_view().unwrap(),
9393
resolve_target: Some(post_process.destination),
9494
ops: Operations {
95-
load: LoadOp::Clear(LegacyColor::BLACK.into()),
95+
load: LoadOp::Clear(LinearRgba::BLACK.into()),
9696
store: StoreOp::Store,
9797
},
9898
})],

crates/bevy_render/src/render_phase/draw_state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::{
22
camera::Viewport,
3-
prelude::LegacyColor,
43
render_resource::{
54
BindGroup, BindGroupId, Buffer, BufferId, BufferSlice, RenderPipeline, RenderPipelineId,
65
ShaderStages,
76
},
87
renderer::RenderDevice,
98
};
9+
use bevy_color::LinearRgba;
1010
use bevy_utils::{default, detailed_trace};
1111
use std::ops::Range;
1212
use wgpu::{IndexFormat, RenderPass};
@@ -598,7 +598,7 @@ impl<'a> TrackedRenderPass<'a> {
598598
/// Sets the blend color as used by some of the blending modes.
599599
///
600600
/// Subsequent blending tests will test against this value.
601-
pub fn set_blend_constant(&mut self, color: LegacyColor) {
601+
pub fn set_blend_constant(&mut self, color: LinearRgba) {
602602
detailed_trace!("set blend constant: {:?}", color);
603603
self.pass.set_blend_constant(wgpu::Color::from(color));
604604
}

crates/bevy_render/src/texture/texture_attachment.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::CachedTexture;
2-
use crate::{prelude::LegacyColor, render_resource::TextureView};
2+
use crate::render_resource::TextureView;
3+
use bevy_color::LinearRgba;
34
use std::sync::{
45
atomic::{AtomicBool, Ordering},
56
Arc,
@@ -13,15 +14,15 @@ use wgpu::{
1314
pub struct ColorAttachment {
1415
pub texture: CachedTexture,
1516
pub resolve_target: Option<CachedTexture>,
16-
clear_color: Option<LegacyColor>,
17+
clear_color: Option<LinearRgba>,
1718
is_first_call: Arc<AtomicBool>,
1819
}
1920

2021
impl ColorAttachment {
2122
pub fn new(
2223
texture: CachedTexture,
2324
resolve_target: Option<CachedTexture>,
24-
clear_color: Option<LegacyColor>,
25+
clear_color: Option<LinearRgba>,
2526
) -> Self {
2627
Self {
2728
texture,

crates/bevy_render/src/view/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,9 +551,11 @@ pub fn prepare_view_targets(
551551
(a, b, sampled, main_texture)
552552
});
553553

554+
let converted_clear_color = clear_color.map(|color| color.into());
555+
554556
let main_textures = MainTargetTextures {
555-
a: ColorAttachment::new(a.clone(), sampled.clone(), clear_color),
556-
b: ColorAttachment::new(b.clone(), sampled.clone(), clear_color),
557+
a: ColorAttachment::new(a.clone(), sampled.clone(), converted_clear_color),
558+
b: ColorAttachment::new(b.clone(), sampled.clone(), converted_clear_color),
557559
main_texture: main_texture.clone(),
558560
};
559561

0 commit comments

Comments
 (0)