Skip to content

Commit

Permalink
Consolidate Render(Ui)Materials(2d) into RenderAssets (#12827)
Browse files Browse the repository at this point in the history
# Objective

- Replace `RenderMaterials` / `RenderMaterials2d` / `RenderUiMaterials`
with `RenderAssets` to enable implementing changes to one thing,
`RenderAssets`, that applies to all use cases rather than duplicating
changes everywhere for multiple things that should be one thing.
- Adopts #8149 

## Solution

- Make RenderAsset generic over the destination type rather than the
source type as in #8149
- Use `RenderAssets<PreparedMaterial<M>>` etc for render materials

---

## Changelog

- Changed:
- The `RenderAsset` trait is now implemented on the destination type.
Its `SourceAsset` associated type refers to the type of the source
asset.
- `RenderMaterials`, `RenderMaterials2d`, and `RenderUiMaterials` have
been replaced by `RenderAssets<PreparedMaterial<M>>` and similar.

## Migration Guide

- `RenderAsset` is now implemented for the destination type rather that
the source asset type. The source asset type is now the `RenderAsset`
trait's `SourceAsset` associated type.
  • Loading branch information
superdump committed Apr 9, 2024
1 parent 10af274 commit ab7cbfa
Show file tree
Hide file tree
Showing 40 changed files with 356 additions and 717 deletions.
4 changes: 2 additions & 2 deletions crates/bevy_core_pipeline/src/skybox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use bevy_render::{
*,
},
renderer::RenderDevice,
texture::{BevyDefault, Image},
texture::{BevyDefault, GpuImage, Image},
view::{ExtractedView, Msaa, ViewTarget, ViewUniform, ViewUniforms},
Render, RenderApp, RenderSet,
};
Expand Down Expand Up @@ -236,7 +236,7 @@ fn prepare_skybox_bind_groups(
pipeline: Res<SkyboxPipeline>,
view_uniforms: Res<ViewUniforms>,
skybox_uniforms: Res<ComponentUniforms<SkyboxUniforms>>,
images: Res<RenderAssets<Image>>,
images: Res<RenderAssets<GpuImage>>,
render_device: Res<RenderDevice>,
views: Query<(Entity, &Skybox, &DynamicUniformIndex<SkyboxUniforms>)>,
) {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_core_pipeline/src/tonemapping/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bevy_render::render_resource::binding_types::{
sampler, texture_2d, texture_3d, uniform_buffer,
};
use bevy_render::renderer::RenderDevice;
use bevy_render::texture::{CompressedImageFormats, Image, ImageSampler, ImageType};
use bevy_render::texture::{CompressedImageFormats, GpuImage, Image, ImageSampler, ImageType};
use bevy_render::view::{ViewTarget, ViewUniform};
use bevy_render::{camera::Camera, texture::FallbackImage};
use bevy_render::{render_resource::*, Render, RenderApp, RenderSet};
Expand Down Expand Up @@ -319,7 +319,7 @@ pub enum DebandDither {
}

pub fn get_lut_bindings<'a>(
images: &'a RenderAssets<Image>,
images: &'a RenderAssets<GpuImage>,
tonemapping_luts: &'a TonemappingLuts,
tonemapping: &Tonemapping,
fallback_image: &'a FallbackImage,
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_core_pipeline/src/tonemapping/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bevy_render::{
RenderPassColorAttachment, RenderPassDescriptor, StoreOp, TextureViewId,
},
renderer::RenderContext,
texture::{FallbackImage, Image},
texture::{FallbackImage, GpuImage},
view::{ViewTarget, ViewUniformOffset, ViewUniforms},
};

Expand Down Expand Up @@ -42,7 +42,7 @@ impl ViewNode for TonemappingNode {
) -> Result<(), NodeRunError> {
let pipeline_cache = world.resource::<PipelineCache>();
let tonemapping_pipeline = world.resource::<TonemappingPipeline>();
let gpu_images = world.get_resource::<RenderAssets<Image>>().unwrap();
let gpu_images = world.get_resource::<RenderAssets<GpuImage>>().unwrap();
let fallback_image = world.resource::<FallbackImage>();
let view_uniforms_resource = world.resource::<ViewUniforms>();
let view_uniforms = &view_uniforms_resource.uniforms;
Expand Down
32 changes: 13 additions & 19 deletions crates/bevy_gizmos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ use bevy_math::Vec3;
use bevy_reflect::TypePath;
use bevy_render::{
extract_component::{ComponentUniforms, DynamicUniformIndex, UniformComponentPlugin},
render_asset::{
PrepareAssetError, RenderAsset, RenderAssetPlugin, RenderAssetUsages, RenderAssets,
},
render_asset::{PrepareAssetError, RenderAsset, RenderAssetPlugin, RenderAssets},
render_phase::{PhaseItem, RenderCommand, RenderCommandResult, TrackedRenderPass},
render_resource::{
binding_types::uniform_buffer, BindGroup, BindGroupEntries, BindGroupLayout,
Expand Down Expand Up @@ -129,7 +127,7 @@ impl Plugin for GizmoPlugin {
.register_type::<GizmoConfigStore>()
.add_plugins(UniformComponentPlugin::<LineGizmoUniform>::default())
.init_asset::<LineGizmo>()
.add_plugins(RenderAssetPlugin::<LineGizmo>::default())
.add_plugins(RenderAssetPlugin::<GpuLineGizmo>::default())
.init_resource::<LineGizmoHandles>()
// We insert the Resource GizmoConfigStore into the world implicitly here if it does not exist.
.init_gizmo_group::<DefaultGizmoConfigGroup>()
Expand Down Expand Up @@ -377,26 +375,22 @@ struct GpuLineGizmo {
joints: GizmoLineJoint,
}

impl RenderAsset for LineGizmo {
type PreparedAsset = GpuLineGizmo;
impl RenderAsset for GpuLineGizmo {
type SourceAsset = LineGizmo;
type Param = SRes<RenderDevice>;

fn asset_usage(&self) -> RenderAssetUsages {
RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD
}

fn prepare_asset(
self,
gizmo: Self::SourceAsset,
render_device: &mut SystemParamItem<Self::Param>,
) -> Result<Self::PreparedAsset, PrepareAssetError<Self>> {
let position_buffer_data = cast_slice(&self.positions);
) -> Result<Self, PrepareAssetError<Self::SourceAsset>> {
let position_buffer_data = cast_slice(&gizmo.positions);
let position_buffer = render_device.create_buffer_with_data(&BufferInitDescriptor {
usage: BufferUsages::VERTEX,
label: Some("LineGizmo Position Buffer"),
contents: position_buffer_data,
});

let color_buffer_data = cast_slice(&self.colors);
let color_buffer_data = cast_slice(&gizmo.colors);
let color_buffer = render_device.create_buffer_with_data(&BufferInitDescriptor {
usage: BufferUsages::VERTEX,
label: Some("LineGizmo Color Buffer"),
Expand All @@ -406,9 +400,9 @@ impl RenderAsset for LineGizmo {
Ok(GpuLineGizmo {
position_buffer,
color_buffer,
vertex_count: self.positions.len() as u32,
strip: self.strip,
joints: self.joints,
vertex_count: gizmo.positions.len() as u32,
strip: gizmo.strip,
joints: gizmo.joints,
})
}
}
Expand Down Expand Up @@ -468,7 +462,7 @@ impl<const I: usize, P: PhaseItem> RenderCommand<P> for SetLineGizmoBindGroup<I>

struct DrawLineGizmo;
impl<P: PhaseItem> RenderCommand<P> for DrawLineGizmo {
type Param = SRes<RenderAssets<LineGizmo>>;
type Param = SRes<RenderAssets<GpuLineGizmo>>;
type ViewQuery = ();
type ItemQuery = Read<Handle<LineGizmo>>;

Expand Down Expand Up @@ -514,7 +508,7 @@ impl<P: PhaseItem> RenderCommand<P> for DrawLineGizmo {

struct DrawLineJointGizmo;
impl<P: PhaseItem> RenderCommand<P> for DrawLineJointGizmo {
type Param = SRes<RenderAssets<LineGizmo>>;
type Param = SRes<RenderAssets<GpuLineGizmo>>;
type ViewQuery = ();
type ItemQuery = Read<Handle<LineGizmo>>;

Expand Down
11 changes: 6 additions & 5 deletions crates/bevy_gizmos/src/pipeline_2d.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::{
config::{GizmoLineJoint, GizmoLineStyle, GizmoMeshConfig},
line_gizmo_vertex_buffer_layouts, line_joint_gizmo_vertex_buffer_layouts, DrawLineGizmo,
DrawLineJointGizmo, GizmoRenderSystem, LineGizmo, LineGizmoUniformBindgroupLayout,
SetLineGizmoBindGroup, LINE_JOINT_SHADER_HANDLE, LINE_SHADER_HANDLE,
DrawLineJointGizmo, GizmoRenderSystem, GpuLineGizmo, LineGizmo,
LineGizmoUniformBindgroupLayout, SetLineGizmoBindGroup, LINE_JOINT_SHADER_HANDLE,
LINE_SHADER_HANDLE,
};
use bevy_app::{App, Plugin};
use bevy_asset::Handle;
Expand Down Expand Up @@ -52,7 +53,7 @@ impl Plugin for LineGizmo2dPlugin {
Render,
(queue_line_gizmos_2d, queue_line_joint_gizmos_2d)
.in_set(GizmoRenderSystem::QueueLineGizmos2d)
.after(prepare_assets::<LineGizmo>),
.after(prepare_assets::<GpuLineGizmo>),
);
}

Expand Down Expand Up @@ -253,7 +254,7 @@ fn queue_line_gizmos_2d(
pipeline_cache: Res<PipelineCache>,
msaa: Res<Msaa>,
line_gizmos: Query<(Entity, &Handle<LineGizmo>, &GizmoMeshConfig)>,
line_gizmo_assets: Res<RenderAssets<LineGizmo>>,
line_gizmo_assets: Res<RenderAssets<GpuLineGizmo>>,
mut views: Query<(
&ExtractedView,
&mut SortedRenderPhase<Transparent2d>,
Expand Down Expand Up @@ -306,7 +307,7 @@ fn queue_line_joint_gizmos_2d(
pipeline_cache: Res<PipelineCache>,
msaa: Res<Msaa>,
line_gizmos: Query<(Entity, &Handle<LineGizmo>, &GizmoMeshConfig)>,
line_gizmo_assets: Res<RenderAssets<LineGizmo>>,
line_gizmo_assets: Res<RenderAssets<GpuLineGizmo>>,
mut views: Query<(
&ExtractedView,
&mut SortedRenderPhase<Transparent2d>,
Expand Down
11 changes: 6 additions & 5 deletions crates/bevy_gizmos/src/pipeline_3d.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::{
config::{GizmoLineJoint, GizmoLineStyle, GizmoMeshConfig},
line_gizmo_vertex_buffer_layouts, line_joint_gizmo_vertex_buffer_layouts, DrawLineGizmo,
DrawLineJointGizmo, GizmoRenderSystem, LineGizmo, LineGizmoUniformBindgroupLayout,
SetLineGizmoBindGroup, LINE_JOINT_SHADER_HANDLE, LINE_SHADER_HANDLE,
DrawLineJointGizmo, GizmoRenderSystem, GpuLineGizmo, LineGizmo,
LineGizmoUniformBindgroupLayout, SetLineGizmoBindGroup, LINE_JOINT_SHADER_HANDLE,
LINE_SHADER_HANDLE,
};
use bevy_app::{App, Plugin};
use bevy_asset::Handle;
Expand Down Expand Up @@ -51,7 +52,7 @@ impl Plugin for LineGizmo3dPlugin {
Render,
(queue_line_gizmos_3d, queue_line_joint_gizmos_3d)
.in_set(GizmoRenderSystem::QueueLineGizmos3d)
.after(prepare_assets::<LineGizmo>),
.after(prepare_assets::<GpuLineGizmo>),
);
}

Expand Down Expand Up @@ -278,7 +279,7 @@ fn queue_line_gizmos_3d(
pipeline_cache: Res<PipelineCache>,
msaa: Res<Msaa>,
line_gizmos: Query<(Entity, &Handle<LineGizmo>, &GizmoMeshConfig)>,
line_gizmo_assets: Res<RenderAssets<LineGizmo>>,
line_gizmo_assets: Res<RenderAssets<GpuLineGizmo>>,
mut views: Query<(
&ExtractedView,
&mut SortedRenderPhase<Transparent3d>,
Expand Down Expand Up @@ -361,7 +362,7 @@ fn queue_line_joint_gizmos_3d(
pipeline_cache: Res<PipelineCache>,
msaa: Res<Msaa>,
line_gizmos: Query<(Entity, &Handle<LineGizmo>, &GizmoMeshConfig)>,
line_gizmo_assets: Res<RenderAssets<LineGizmo>>,
line_gizmo_assets: Res<RenderAssets<GpuLineGizmo>>,
mut views: Query<(
&ExtractedView,
&mut SortedRenderPhase<Transparent3d>,
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/extended_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bevy_render::{
ShaderRef, SpecializedMeshPipelineError, UnpreparedBindGroup,
},
renderer::RenderDevice,
texture::{FallbackImage, Image},
texture::{FallbackImage, GpuImage},
};

use crate::{Material, MaterialPipeline, MaterialPipelineKey, MeshPipeline, MeshPipelineKey};
Expand Down Expand Up @@ -139,7 +139,7 @@ impl<B: Material, E: MaterialExtension> AsBindGroup for ExtendedMaterial<B, E> {
&self,
layout: &BindGroupLayout,
render_device: &RenderDevice,
images: &RenderAssets<Image>,
images: &RenderAssets<GpuImage>,
fallback_image: &FallbackImage,
) -> Result<UnpreparedBindGroup<Self::Data>, AsBindGroupError> {
// add together the bindings of the base material and the user material
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ use bevy_render::{
render_asset::prepare_assets,
render_graph::RenderGraph,
render_resource::Shader,
texture::Image,
texture::{GpuImage, Image},
view::VisibilitySystems,
ExtractSchedule, Render, RenderApp, RenderSet,
};
Expand Down Expand Up @@ -375,7 +375,7 @@ impl Plugin for PbrPlugin {
(
prepare_lights
.in_set(RenderSet::ManageViews)
.after(prepare_assets::<Image>),
.after(prepare_assets::<GpuImage>),
prepare_clusters.in_set(RenderSet::PrepareResources),
),
)
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_pbr/src/light_probe/environment_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use bevy_render::{
TextureSampleType, TextureView,
},
renderer::RenderDevice,
texture::{FallbackImage, Image},
texture::{FallbackImage, GpuImage, Image},
};

use std::num::NonZeroU32;
Expand Down Expand Up @@ -213,7 +213,7 @@ impl<'a> RenderViewEnvironmentMapBindGroupEntries<'a> {
/// specular binding arrays respectively, as well as the sampler.
pub(crate) fn get(
render_view_environment_maps: Option<&RenderViewLightProbes<EnvironmentMapLight>>,
images: &'a RenderAssets<Image>,
images: &'a RenderAssets<GpuImage>,
fallback_image: &'a FallbackImage,
render_device: &RenderDevice,
) -> RenderViewEnvironmentMapBindGroupEntries<'a> {
Expand Down Expand Up @@ -283,7 +283,7 @@ impl LightProbeComponent for EnvironmentMapLight {
// view.
type ViewLightProbeInfo = EnvironmentMapViewLightProbeInfo;

fn id(&self, image_assets: &RenderAssets<Image>) -> Option<Self::AssetId> {
fn id(&self, image_assets: &RenderAssets<GpuImage>) -> Option<Self::AssetId> {
if image_assets.get(&self.diffuse_map).is_none()
|| image_assets.get(&self.specular_map).is_none()
{
Expand All @@ -302,7 +302,7 @@ impl LightProbeComponent for EnvironmentMapLight {

fn create_render_view_light_probes(
view_component: Option<&EnvironmentMapLight>,
image_assets: &RenderAssets<Image>,
image_assets: &RenderAssets<GpuImage>,
) -> RenderViewLightProbes<Self> {
let mut render_view_light_probes = RenderViewLightProbes::new();

Expand Down
12 changes: 6 additions & 6 deletions crates/bevy_pbr/src/light_probe/irradiance_volume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ use bevy_render::{
TextureSampleType, TextureView,
},
renderer::RenderDevice,
texture::{FallbackImage, Image},
texture::{FallbackImage, GpuImage, Image},
};
use std::{num::NonZeroU32, ops::Deref};

Expand Down Expand Up @@ -212,7 +212,7 @@ impl<'a> RenderViewIrradianceVolumeBindGroupEntries<'a> {
/// the view, as well as the sampler.
pub(crate) fn get(
render_view_irradiance_volumes: Option<&RenderViewLightProbes<IrradianceVolume>>,
images: &'a RenderAssets<Image>,
images: &'a RenderAssets<GpuImage>,
fallback_image: &'a FallbackImage,
render_device: &RenderDevice,
) -> RenderViewIrradianceVolumeBindGroupEntries<'a> {
Expand All @@ -236,7 +236,7 @@ impl<'a> RenderViewIrradianceVolumeBindGroupEntries<'a> {
/// arrays are available on the current platform.
fn get_multiple(
render_view_irradiance_volumes: Option<&RenderViewLightProbes<IrradianceVolume>>,
images: &'a RenderAssets<Image>,
images: &'a RenderAssets<GpuImage>,
fallback_image: &'a FallbackImage,
) -> RenderViewIrradianceVolumeBindGroupEntries<'a> {
let mut texture_views = vec![];
Expand Down Expand Up @@ -269,7 +269,7 @@ impl<'a> RenderViewIrradianceVolumeBindGroupEntries<'a> {
/// arrays aren't available on the current platform.
fn get_single(
render_view_irradiance_volumes: Option<&RenderViewLightProbes<IrradianceVolume>>,
images: &'a RenderAssets<Image>,
images: &'a RenderAssets<GpuImage>,
fallback_image: &'a FallbackImage,
) -> RenderViewIrradianceVolumeBindGroupEntries<'a> {
if let Some(irradiance_volumes) = render_view_irradiance_volumes {
Expand Down Expand Up @@ -322,7 +322,7 @@ impl LightProbeComponent for IrradianceVolume {
// here.
type ViewLightProbeInfo = ();

fn id(&self, image_assets: &RenderAssets<Image>) -> Option<Self::AssetId> {
fn id(&self, image_assets: &RenderAssets<GpuImage>) -> Option<Self::AssetId> {
if image_assets.get(&self.voxels).is_none() {
None
} else {
Expand All @@ -336,7 +336,7 @@ impl LightProbeComponent for IrradianceVolume {

fn create_render_view_light_probes(
_: Option<&Self>,
_: &RenderAssets<Image>,
_: &RenderAssets<GpuImage>,
) -> RenderViewLightProbes<Self> {
RenderViewLightProbes::new()
}
Expand Down
12 changes: 6 additions & 6 deletions crates/bevy_pbr/src/light_probe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use bevy_render::{
render_resource::{DynamicUniformBuffer, Sampler, Shader, ShaderType, TextureView},
renderer::{RenderDevice, RenderQueue},
settings::WgpuFeatures,
texture::{FallbackImage, Image},
texture::{FallbackImage, GpuImage, Image},
view::ExtractedView,
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
};
Expand Down Expand Up @@ -270,7 +270,7 @@ pub trait LightProbeComponent: Send + Sync + Component + Sized {

/// Returns the asset ID or asset IDs of the texture or textures referenced
/// by this light probe.
fn id(&self, image_assets: &RenderAssets<Image>) -> Option<Self::AssetId>;
fn id(&self, image_assets: &RenderAssets<GpuImage>) -> Option<Self::AssetId>;

/// Returns the intensity of this light probe.
///
Expand All @@ -284,7 +284,7 @@ pub trait LightProbeComponent: Send + Sync + Component + Sized {
/// This is called for every light probe in view every frame.
fn create_render_view_light_probes(
view_component: Option<&Self>,
image_assets: &RenderAssets<Image>,
image_assets: &RenderAssets<GpuImage>,
) -> RenderViewLightProbes<Self>;
}

Expand Down Expand Up @@ -342,7 +342,7 @@ impl Plugin for LightProbePlugin {
/// Gathers up all light probes of a single type in the scene and assigns them
/// to views, performing frustum culling and distance sorting in the process.
fn gather_light_probes<C>(
image_assets: Res<RenderAssets<Image>>,
image_assets: Res<RenderAssets<GpuImage>>,
light_probe_query: Extract<Query<(&GlobalTransform, &C), With<LightProbe>>>,
view_query: Extract<Query<(Entity, &GlobalTransform, &Frustum, Option<&C>), With<Camera3d>>>,
mut reflection_probes: Local<Vec<LightProbeInfo<C>>>,
Expand Down Expand Up @@ -505,7 +505,7 @@ where
/// every frame.
fn new(
(light_probe_transform, environment_map): (&GlobalTransform, &C),
image_assets: &RenderAssets<Image>,
image_assets: &RenderAssets<GpuImage>,
) -> Option<LightProbeInfo<C>> {
environment_map.id(image_assets).map(|id| LightProbeInfo {
affine_transform: light_probe_transform.affine(),
Expand Down Expand Up @@ -634,7 +634,7 @@ pub(crate) fn add_cubemap_texture_view<'a>(
texture_views: &mut Vec<&'a <TextureView as Deref>::Target>,
sampler: &mut Option<&'a Sampler>,
image_id: AssetId<Image>,
images: &'a RenderAssets<Image>,
images: &'a RenderAssets<GpuImage>,
fallback_image: &'a FallbackImage,
) {
match images.get(image_id) {
Expand Down

0 comments on commit ab7cbfa

Please sign in to comment.