Skip to content

Commit

Permalink
rebased onto #6968
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtkuehnert committed Dec 27, 2022
1 parent d8b5527 commit a650d23
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 59 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/core_2d/main_pass_2d_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bevy_gpu::{
use bevy_render::{
camera::ExtractedCamera,
render_graph::{Node, NodeRunError, RenderGraphContext, SlotInfo, SlotType},
render_phase::{DrawFunctions, RenderPhase, TrackedRenderPass},
render_phase::RenderPhase,
view::{ExtractedView, ViewTarget},
};
#[cfg(feature = "trace")]
Expand Down
44 changes: 15 additions & 29 deletions crates/bevy_core_pipeline/src/core_3d/main_pass_3d_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bevy_gpu::{
use bevy_render::{
camera::ExtractedCamera,
render_graph::{Node, NodeRunError, RenderGraphContext, SlotInfo, SlotType},
render_phase::{DrawFunctions, RenderPhase, TrackedRenderPass},
render_phase::RenderPhase,
view::{ExtractedView, ViewDepthTexture, ViewTarget},
};
#[cfg(feature = "trace")]
Expand Down Expand Up @@ -97,20 +97,13 @@ impl Node for MainPass3dNode {
}),
};

let draw_functions = world.resource::<DrawFunctions<Opaque3d>>();

let render_pass = render_context
.command_encoder
.begin_render_pass(&pass_descriptor);
let mut draw_functions = draw_functions.write();
let mut tracked_pass = TrackedRenderPass::new(render_pass);
if let Some(viewport) = camera.viewport.as_ref() {
tracked_pass.set_camera_viewport(viewport);
}
for item in &opaque_phase.items {
let draw_function = draw_functions.get_mut(item.draw_function).unwrap();
draw_function.draw(world, &mut tracked_pass, view_entity, item);
}
opaque_phase.render(
world,
gpu_context,
view_entity,
camera.viewport.as_ref(),
pass_descriptor,
);
}

if !alpha_mask_phase.items.is_empty() {
Expand All @@ -136,20 +129,13 @@ impl Node for MainPass3dNode {
}),
};

let draw_functions = world.resource::<DrawFunctions<AlphaMask3d>>();

let render_pass = render_context
.command_encoder
.begin_render_pass(&pass_descriptor);
let mut draw_functions = draw_functions.write();
let mut tracked_pass = TrackedRenderPass::new(render_pass);
if let Some(viewport) = camera.viewport.as_ref() {
tracked_pass.set_camera_viewport(viewport);
}
for item in &alpha_mask_phase.items {
let draw_function = draw_functions.get_mut(item.draw_function).unwrap();
draw_function.draw(world, &mut tracked_pass, view_entity, item);
}
alpha_mask_phase.render(
world,
gpu_context,
view_entity,
camera.viewport.as_ref(),
pass_descriptor,
);
}

if !transparent_phase.items.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/tonemapping/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl Node for TonemappingNode {
};

let mut render_pass = gpu_context
.command_encoder
.gpu_command_encoder
.begin_render_pass(&pass_descriptor);

render_pass.set_pipeline(pipeline);
Expand Down
10 changes: 3 additions & 7 deletions crates/bevy_gpu/src/gpu_resource/bind_group.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use crate::gpu_resource::resource_macros::*;
use bevy_reflect::Uuid;
use crate::{define_atomic_id, gpu_resource::resource_macros::*};
use std::ops::Deref;

define_atomic_id!(BindGroupId);
gpu_resource_wrapper!(ErasedBindGroup, wgpu::BindGroup);

/// A [`BindGroup`] identifier.
#[derive(Copy, Clone, Hash, Eq, PartialEq, Debug)]
pub struct BindGroupId(Uuid);

/// Bind groups are responsible for binding render resources (e.g. buffers, textures, samplers)
/// to a render pass.
/// This makes them accessible in the pipeline (shaders) as uniforms.
Expand All @@ -31,7 +27,7 @@ impl BindGroup {
impl From<wgpu::BindGroup> for BindGroup {
fn from(value: wgpu::BindGroup) -> Self {
BindGroup {
id: BindGroupId(Uuid::new_v4()),
id: BindGroupId::new(),
value: ErasedBindGroup::new(value),
}
}
Expand Down
5 changes: 2 additions & 3 deletions crates/bevy_gpu/src/gpu_resource/bind_group_layout.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::gpu_resource::{define_atomic_id, resource_macros::*};
use bevy_reflect::Uuid;
use crate::{define_atomic_id, gpu_resource::resource_macros::*};
use std::ops::Deref;

define_atomic_id!(BindGroupLayoutId);
Expand Down Expand Up @@ -32,7 +31,7 @@ impl BindGroupLayout {
impl From<wgpu::BindGroupLayout> for BindGroupLayout {
fn from(value: wgpu::BindGroupLayout) -> Self {
BindGroupLayout {
id: BindGroupLayoutId(Uuid::new_v4()),
id: BindGroupLayoutId::new(),
value: ErasedBindGroupLayout::new(value),
}
}
Expand Down
6 changes: 1 addition & 5 deletions crates/bevy_gpu/src/gpu_resource/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use crate::gpu_resource::{define_atomic_id, resource_macros::*};
use bevy_utils::Uuid;
use crate::{define_atomic_id, gpu_resource::resource_macros::*};
use std::ops::{Bound, Deref, RangeBounds};

#[derive(Copy, Clone, Hash, Eq, PartialEq, Debug)]
pub struct BufferId(Uuid);

define_atomic_id!(BufferId);
gpu_resource_wrapper!(ErasedBuffer, wgpu::Buffer);

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_gpu/src/gpu_resource/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub use wgpu::{
ImageCopyTexture, ImageCopyTextureBase, ImageDataLayout, ImageSubresourceRange, IndexFormat,
Limits as WgpuLimits, LoadOp, MapMode, MultisampleState, Operations, Origin3d, PipelineLayout,
PipelineLayoutDescriptor, PolygonMode, PresentMode as WgpuPresentMode, PrimitiveState,
PrimitiveTopology, RenderPass as WgpuRenderPass, RenderPassColorAttachment,
PrimitiveTopology, RenderPass as WgpuRenderPass, RenderPass, RenderPassColorAttachment,
RenderPassDepthStencilAttachment, RenderPassDescriptor,
RenderPipelineDescriptor as RawRenderPipelineDescriptor, SamplerBindingType, SamplerDescriptor,
ShaderModule, ShaderModuleDescriptor, ShaderSource, ShaderStages, StencilFaceState,
Expand Down
7 changes: 5 additions & 2 deletions crates/bevy_gpu/src/gpu_resource/pipeline.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{define_atomic_id, gpu_resource::{resource_macros::*, BindGroupLayout, Shader, ShaderDefVal}};
use crate::{
define_atomic_id,
gpu_resource::{resource_macros::*, BindGroupLayout, Shader, ShaderDefVal},
};
use bevy_asset::Handle;
use std::{borrow::Cow, ops::Deref};
use wgpu::{
Expand All @@ -7,7 +10,7 @@ use wgpu::{
};

define_atomic_id!(RenderPipelineId);
render_resource_wrapper!(ErasedRenderPipeline, wgpu::RenderPipeline);
gpu_resource_wrapper!(ErasedRenderPipeline, wgpu::RenderPipeline);

/// A [`RenderPipeline`] represents a graphics pipeline and its stages (shaders), bindings and vertex buffers.
///
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_gpu/src/gpu_resource/resource_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,4 @@ macro_rules! define_atomic_id {
};
}


pub use gpu_resource_wrapper;
2 changes: 1 addition & 1 deletion crates/bevy_gpu/src/gpu_resource/texture.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::define_atomic_id;
use std::ops::Deref;
use crate::gpu_resource::resource_macros::*;
use std::ops::Deref;

define_atomic_id!(TextureId);
gpu_resource_wrapper!(ErasedTexture, wgpu::Texture);
Expand Down
14 changes: 11 additions & 3 deletions crates/bevy_gpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,21 @@ pub struct GpuAdapterInfo(pub AdapterInfo);
#[derive(Resource, Clone, Deref, DerefMut)]
pub struct GpuQueue(pub Arc<Queue>);

/// Encodes a series of GPU operations.
///
/// A command encoder can record [`RenderPass`]es,
/// [`ComputePass`]es, and transfer operations between
/// driver-managed resources like [`Buffer`]s and
/// [`Texture`]s.
pub type GpuCommandEncoder = CommandEncoder;

/// The context with all information required to interact with the GPU.
///
/// The [`GpuDevice`] is used to create render resources and the
/// the [`CommandEncoder`] is used to record a series of GPU operations.
/// The [`GpuDevice`] is used to create gpu resources (buffers, bind groups, pipelines, etc.) and
/// the [`GpuCommandEncoder`] is used to record a series of GPU operations.
pub struct GpuContext {
pub gpu_device: GpuDevice,
pub command_encoder: CommandEncoder,
pub gpu_command_encoder: GpuCommandEncoder,
}

gpu_resource_wrapper!(ErasedGpuDevice, wgpu::Device);
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_render/src/render_graph/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use crate::render_graph::{
SlotInfo, SlotInfos, SlotType, SlotValue,
};
use bevy_ecs::world::World;
use bevy_utils::Uuid;
use bevy_gpu::{define_atomic_id, GpuContext};
use downcast_rs::{impl_downcast, Downcast};
use std::{borrow::Cow, fmt::Debug};
use thiserror::Error;
use bevy_gpu::{define_atomic_id};

define_atomic_id!(NodeId);

Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_render/src/render_phase/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ mod draw_state;
pub use draw::*;
pub use draw_state::*;

use crate::{camera::Viewport, renderer::GpuContext};
use crate::camera::Viewport;
use bevy_ecs::{
entity::Entity,
prelude::{Component, Query},
world::World,
};
use wgpu::RenderPassDescriptor;
use bevy_gpu::{gpu_resource::RenderPassDescriptor, GpuContext};

/// A resource to collect and sort draw requests for specific [`PhaseItems`](PhaseItem).
#[derive(Component)]
Expand Down
Empty file.
1 change: 1 addition & 0 deletions crates/bevy_render/src/renderer/gpu_device.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 1 addition & 1 deletion crates/bevy_render/src/renderer/graph_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl RenderGraphRunner {
{
#[cfg(feature = "trace")]
let _span = info_span!("submit_graph_commands").entered();
queue.submit(vec![render_context.command_encoder.finish()]);
queue.submit(vec![gpu_context.gpu_command_encoder.finish()]);
}
Ok(())
}
Expand Down

0 comments on commit a650d23

Please sign in to comment.