Skip to content

Commit

Permalink
fix calls to as_rgba_linear (#3200)
Browse files Browse the repository at this point in the history
# Objective

- After #3192, some places where `as_rgba_linear` was called were doing too many conversions

## Solution

- Fix the conversions
  • Loading branch information
mockersf committed Nov 28, 2021
1 parent e8412df commit 3de391b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/shader/custom_shader_pipelined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl RenderAsset for CustomMaterial {
extracted_asset: Self::ExtractedAsset,
(render_device, custom_pipeline): &mut SystemParamItem<Self::Param>,
) -> Result<Self::PreparedAsset, PrepareAssetError<Self::ExtractedAsset>> {
let color: Vec4 = extracted_asset.color.as_rgba_linear().into();
let color = Vec4::from_slice(&extracted_asset.color.as_linear_rgba_f32());
let buffer = render_device.create_buffer_with_data(&BufferInitDescriptor {
contents: color.as_std140().as_bytes(),
label: None,
Expand Down
2 changes: 1 addition & 1 deletion pipelined/bevy_pbr2/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl RenderAsset for StandardMaterial {
};

let value = StandardMaterialUniformData {
base_color: material.base_color.as_rgba_linear().into(),
base_color: material.base_color.as_linear_rgba_f32().into(),
emissive: material.emissive.into(),
roughness: material.perceptual_roughness,
metallic: material.metallic,
Expand Down
8 changes: 4 additions & 4 deletions pipelined/bevy_pbr2/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,6 @@ pub fn prepare_lights(
) {
light_meta.view_gpu_lights.clear();

let ambient_color = ambient_light.color.as_rgba_linear() * ambient_light.brightness;
// Pre-calculate for PointLights
let cube_face_projection =
Mat4::perspective_infinite_reverse_rh(std::f32::consts::FRAC_PI_2, 1.0, 0.1);
Expand Down Expand Up @@ -555,7 +554,8 @@ pub fn prepare_lights(
let mut view_lights = Vec::new();

let mut gpu_lights = GpuLights {
ambient_color: ambient_color.into(),
ambient_color: Vec4::from_slice(&ambient_light.color.as_linear_rgba_f32())
* ambient_light.brightness,
n_point_lights: point_lights.iter().len() as u32,
n_directional_lights: directional_lights.iter().len() as u32,
point_lights: [GpuPointLight::default(); MAX_POINT_LIGHTS],
Expand Down Expand Up @@ -622,7 +622,7 @@ pub fn prepare_lights(
projection: cube_face_projection,
// premultiply color by intensity
// we don't use the alpha at all, so no reason to multiply only [0..3]
color: (light.color.as_rgba_linear() * light.intensity).into(),
color: Vec4::from_slice(&light.color.as_linear_rgba_f32()) * light.intensity,
radius: light.radius,
position: light.transform.translation,
inverse_square_range: 1.0 / (light.range * light.range),
Expand Down Expand Up @@ -669,7 +669,7 @@ pub fn prepare_lights(
gpu_lights.directional_lights[i] = GpuDirectionalLight {
// premultiply color by intensity
// we don't use the alpha at all, so no reason to multiply only [0..3]
color: (light.color.as_rgba_linear() * intensity).into(),
color: Vec4::from_slice(&light.color.as_linear_rgba_f32()) * intensity,
dir_to_light,
// NOTE: * view is correct, it should not be view.inverse() here
view_projection: projection * view,
Expand Down
21 changes: 15 additions & 6 deletions pipelined/bevy_render2/src/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,12 +633,21 @@ impl From<Vec4> for Color {

impl From<Color> for wgpu::Color {
fn from(color: Color) -> Self {
let color = color.as_rgba_linear();
wgpu::Color {
r: color.r() as f64,
g: color.g() as f64,
b: color.b() as f64,
a: color.a() as f64,
if let Color::RgbaLinear {
red,
green,
blue,
alpha,
} = color.as_rgba_linear()
{
wgpu::Color {
r: red as f64,
g: green as f64,
b: blue as f64,
a: alpha as f64,
}
} else {
unreachable!()
}
}
}
Expand Down

0 comments on commit 3de391b

Please sign in to comment.