Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RenderContext usage cleanup #4446

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 18 additions & 43 deletions crates/re_renderer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
renderer::Renderer,
resource_managers::{MeshManager, TextureManager2D},
wgpu_resources::{GpuRenderPipelinePoolMoveAccessor, WgpuResourcePools},
FileResolver, FileServer, FileSystem, RecommendedFileResolver,
FileServer, RecommendedFileResolver,
};

/// Any resource involving wgpu rendering which can be re-used across different scenes.
Expand All @@ -19,7 +19,12 @@ pub struct RenderContext {
pub device: Arc<wgpu::Device>,
pub queue: Arc<wgpu::Queue>,

pub(crate) shared_renderer_data: SharedRendererData,
pub(crate) config: RenderContextConfig,

/// Global bindings, always bound to 0 bind group slot zero.
/// [`Renderer`] are not allowed to use bind group 0 themselves!
pub(crate) global_bindings: GlobalBindings,

renderers: RwLock<Renderers>,
pub(crate) resolver: RecommendedFileResolver,
#[cfg(all(not(target_arch = "wasm32"), debug_assertions))] // native debug build
Expand All @@ -41,32 +46,20 @@ pub struct RenderContext {
pub gpu_resources: WgpuResourcePools, // Last due to drop order.
}

/// Immutable data that is shared between all [`Renderer`]
pub struct SharedRendererData {
pub(crate) config: RenderContextConfig,

/// Global bindings, always bound to 0 bind group slot zero.
/// [`Renderer`] are not allowed to use bind group 0 themselves!
pub(crate) global_bindings: GlobalBindings,
}

/// Struct owning *all* [`Renderer`].
/// [`Renderer`] are created lazily and stay around indefinitely.
pub(crate) struct Renderers {
renderers: concurrent::TypeMap,
}

impl Renderers {
pub fn get_or_create<Fs: FileSystem, R: 'static + Renderer + Send + Sync>(
pub fn get_or_create<R: 'static + Renderer + Send + Sync>(
&mut self,
shared_data: &SharedRendererData,
resource_pools: &WgpuResourcePools,
device: &wgpu::Device,
resolver: &FileResolver<Fs>,
ctx: &RenderContext,
) -> &R {
self.renderers.entry().or_insert_with(|| {
re_tracing::profile_scope!("create_renderer", std::any::type_name::<R>());
R::create_renderer(shared_data, resource_pools, device, resolver)
R::create_renderer(ctx)
})
}

Expand Down Expand Up @@ -165,22 +158,8 @@ impl RenderContext {
err_tracker
};

let shared_renderer_data = SharedRendererData {
config,
global_bindings,
};

let resolver = crate::new_recommended_file_resolver();
let mut renderers = RwLock::new(Renderers {
renderers: TypeMap::new(),
});

let mesh_manager = RwLock::new(MeshManager::new(renderers.get_mut().get_or_create(
&shared_renderer_data,
&gpu_resources,
&device,
&resolver,
)));
let mesh_manager = RwLock::new(MeshManager::new());
let texture_manager_2d =
TextureManager2D::new(device.clone(), queue.clone(), &gpu_resources.textures);

Expand Down Expand Up @@ -209,9 +188,12 @@ impl RenderContext {
device,
queue,

shared_renderer_data,
config,
global_bindings,

renderers,
renderers: RwLock::new(Renderers {
renderers: TypeMap::new(),
}),

gpu_resources,

Expand Down Expand Up @@ -250,9 +232,7 @@ impl RenderContext {
// knowing that we're not _actually_ blocking.
//
// For more details check https://github.com/gfx-rs/wgpu/issues/3601
if cfg!(target_arch = "wasm32")
&& self.shared_renderer_data.config.device_caps.tier == DeviceTier::Gles
{
if cfg!(target_arch = "wasm32") && self.config.device_caps.tier == DeviceTier::Gles {
self.device.poll(wgpu::Maintain::Wait);
return;
}
Expand Down Expand Up @@ -405,12 +385,7 @@ impl RenderContext {
// If it wasn't there we have to add it.
// This path is rare since it happens only once per renderer type in the lifetime of the ctx.
// (we don't discard renderers ever)
self.renderers.write().get_or_create::<_, R>(
&self.shared_renderer_data,
&self.gpu_resources,
&self.device,
&self.resolver,
);
self.renderers.write().get_or_create::<R>(self);

// Release write lock again and only take a read lock.
// safe to unwrap since we just created it and nobody removes elements from the renderer.
Expand Down
38 changes: 14 additions & 24 deletions crates/re_renderer/src/draw_phases/outlines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ impl OutlineMaskProcessor {
// ------------- Textures -------------
let texture_pool = &ctx.gpu_resources.textures;

let mask_sample_count =
Self::mask_sample_count(&ctx.shared_renderer_data.config.device_caps);
let mask_sample_count = Self::mask_sample_count(&ctx.config.device_caps);
let mask_texture_desc = crate::wgpu_resources::TextureDesc {
label: format!("{instance_label}::mask_texture").into(),
size: wgpu::Extent3d {
Expand Down Expand Up @@ -264,8 +263,7 @@ impl OutlineMaskProcessor {

// ------------- Render Pipelines -------------

let screen_triangle_vertex_shader =
screen_triangle_vertex_shader(&ctx.gpu_resources, &ctx.device, &ctx.resolver);
let screen_triangle_vertex_shader = screen_triangle_vertex_shader(ctx);
let jumpflooding_init_shader_module = if mask_sample_count == 1 {
include_shader_module!("../../shader/outlines/jumpflooding_init.wgsl")
} else {
Expand All @@ -274,54 +272,46 @@ impl OutlineMaskProcessor {
let jumpflooding_init_desc = RenderPipelineDesc {
label: "OutlineMaskProcessor::jumpflooding_init".into(),
pipeline_layout: ctx.gpu_resources.pipeline_layouts.get_or_create(
&ctx.device,
ctx,
&PipelineLayoutDesc {
label: "OutlineMaskProcessor::jumpflooding_init".into(),
entries: vec![bind_group_layout_jumpflooding_init],
},
&ctx.gpu_resources.bind_group_layouts,
),
vertex_entrypoint: "main".into(),
vertex_handle: screen_triangle_vertex_shader,
fragment_entrypoint: "main".into(),
fragment_handle: ctx.gpu_resources.shader_modules.get_or_create(
&ctx.device,
&ctx.resolver,
&jumpflooding_init_shader_module,
),
fragment_handle: ctx
.gpu_resources
.shader_modules
.get_or_create(ctx, &jumpflooding_init_shader_module),
vertex_buffers: smallvec![],
render_targets: smallvec![Some(Self::VORONOI_FORMAT.into())],
primitive: wgpu::PrimitiveState::default(),
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
};
let render_pipeline_jumpflooding_init = ctx.gpu_resources.render_pipelines.get_or_create(
&ctx.device,
&jumpflooding_init_desc,
&ctx.gpu_resources.pipeline_layouts,
&ctx.gpu_resources.shader_modules,
);
let render_pipeline_jumpflooding_init = ctx
.gpu_resources
.render_pipelines
.get_or_create(ctx, &jumpflooding_init_desc);
let render_pipeline_jumpflooding_step = ctx.gpu_resources.render_pipelines.get_or_create(
&ctx.device,
ctx,
&RenderPipelineDesc {
label: "OutlineMaskProcessor::jumpflooding_step".into(),
pipeline_layout: ctx.gpu_resources.pipeline_layouts.get_or_create(
&ctx.device,
ctx,
&PipelineLayoutDesc {
label: "OutlineMaskProcessor::jumpflooding_step".into(),
entries: vec![bind_group_layout_jumpflooding_step],
},
&ctx.gpu_resources.bind_group_layouts,
),
fragment_handle: ctx.gpu_resources.shader_modules.get_or_create(
&ctx.device,
&ctx.resolver,
ctx,
&include_shader_module!("../../shader/outlines/jumpflooding_step.wgsl"),
),
..jumpflooding_init_desc
},
&ctx.gpu_resources.pipeline_layouts,
&ctx.gpu_resources.shader_modules,
);

Self {
Expand Down
26 changes: 7 additions & 19 deletions crates/re_renderer/src/draw_phases/picking_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,7 @@ impl PickingLayerProcessor {
},
);

let direct_depth_readback = ctx
.shared_renderer_data
.config
.device_caps
.support_depth_readback();
let direct_depth_readback = ctx.config.device_caps.support_depth_readback();

let picking_depth_target = ctx.gpu_resources.textures.alloc(
&ctx.device,
Expand Down Expand Up @@ -259,7 +255,7 @@ impl PickingLayerProcessor {
frame_uniform_buffer_content,
);

let bind_group_0 = ctx.shared_renderer_data.global_bindings.create_bind_group(
let bind_group_0 = ctx.global_bindings.create_bind_group(
&ctx.gpu_resources,
&ctx.device,
frame_uniform_buffer,
Expand Down Expand Up @@ -535,30 +531,24 @@ impl DepthReadbackWorkaround {
);

let render_pipeline = ctx.gpu_resources.render_pipelines.get_or_create(
&ctx.device,
ctx,
&RenderPipelineDesc {
label: "DepthCopyWorkaround::render_pipeline".into(),
pipeline_layout: ctx.gpu_resources.pipeline_layouts.get_or_create(
&ctx.device,
ctx,
&PipelineLayoutDesc {
label: "DepthCopyWorkaround::render_pipeline".into(),
entries: vec![
ctx.shared_renderer_data.global_bindings.layout,
bind_group_layout,
],
entries: vec![ctx.global_bindings.layout, bind_group_layout],
},
&ctx.gpu_resources.bind_group_layouts,
),
vertex_entrypoint: "main".into(),
vertex_handle: ctx.gpu_resources.shader_modules.get_or_create(
&ctx.device,
&ctx.resolver,
ctx,
&include_shader_module!("../../shader/screen_triangle.wgsl"),
),
fragment_entrypoint: "main".into(),
fragment_handle: ctx.gpu_resources.shader_modules.get_or_create(
&ctx.device,
&ctx.resolver,
ctx,
&include_shader_module!("../../shader/copy_texture.wgsl"),
),
vertex_buffers: smallvec![],
Expand All @@ -571,8 +561,6 @@ impl DepthReadbackWorkaround {
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
},
&ctx.gpu_resources.pipeline_layouts,
&ctx.gpu_resources.shader_modules,
);

Self {
Expand Down
52 changes: 20 additions & 32 deletions crates/re_renderer/src/renderer/compositor.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use crate::{
allocator::create_and_fill_uniform_buffer,
context::SharedRendererData,
include_shader_module,
renderer::{screen_triangle_vertex_shader, DrawData, DrawError, Renderer},
view_builder::ViewBuilder,
wgpu_resources::{
BindGroupDesc, BindGroupEntry, BindGroupLayoutDesc, GpuBindGroup, GpuBindGroupLayoutHandle,
GpuRenderPipelineHandle, GpuRenderPipelinePoolAccessor, GpuTexture, PipelineLayoutDesc,
RenderPipelineDesc, WgpuResourcePools,
RenderPipelineDesc,
},
OutlineConfig, Rgba,
};

use crate::{DrawPhase, FileResolver, FileSystem, RenderContext};
use crate::{DrawPhase, RenderContext};

use smallvec::smallvec;

Expand Down Expand Up @@ -99,14 +98,9 @@ impl CompositorDrawData {
impl Renderer for Compositor {
type RendererDrawData = CompositorDrawData;

fn create_renderer<Fs: FileSystem>(
shared_data: &SharedRendererData,
pools: &WgpuResourcePools,
device: &wgpu::Device,
resolver: &FileResolver<Fs>,
) -> Self {
let bind_group_layout = pools.bind_group_layouts.get_or_create(
device,
fn create_renderer(ctx: &RenderContext) -> Self {
let bind_group_layout = ctx.gpu_resources.bind_group_layouts.get_or_create(
&ctx.device,
&BindGroupLayoutDesc {
label: "Compositor::bind_group_layout".into(),
entries: vec![
Expand Down Expand Up @@ -148,47 +142,41 @@ impl Renderer for Compositor {
},
);

let vertex_handle = screen_triangle_vertex_shader(pools, device, resolver);
let vertex_handle = screen_triangle_vertex_shader(ctx);

let render_pipeline_descriptor = RenderPipelineDesc {
label: "CompositorDrawData::render_pipeline_regular".into(),
pipeline_layout: pools.pipeline_layouts.get_or_create(
device,
pipeline_layout: ctx.gpu_resources.pipeline_layouts.get_or_create(
ctx,
&PipelineLayoutDesc {
label: "compositor".into(),
entries: vec![shared_data.global_bindings.layout, bind_group_layout],
entries: vec![ctx.global_bindings.layout, bind_group_layout],
},
&pools.bind_group_layouts,
),
vertex_entrypoint: "main".into(),
vertex_handle,
fragment_entrypoint: "main".into(),
fragment_handle: pools.shader_modules.get_or_create(
device,
resolver,
&include_shader_module!("../../shader/composite.wgsl"),
),
fragment_handle: ctx
.gpu_resources
.shader_modules
.get_or_create(ctx, &include_shader_module!("../../shader/composite.wgsl")),
vertex_buffers: smallvec![],
render_targets: smallvec![Some(shared_data.config.output_format_color.into())],
render_targets: smallvec![Some(ctx.config.output_format_color.into())],
primitive: wgpu::PrimitiveState::default(),
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
};
let render_pipeline_regular = pools.render_pipelines.get_or_create(
device,
&render_pipeline_descriptor,
&pools.pipeline_layouts,
&pools.shader_modules,
);
let render_pipeline_screenshot = pools.render_pipelines.get_or_create(
device,
let render_pipeline_regular = ctx
.gpu_resources
.render_pipelines
.get_or_create(ctx, &render_pipeline_descriptor);
let render_pipeline_screenshot = ctx.gpu_resources.render_pipelines.get_or_create(
ctx,
&RenderPipelineDesc {
label: "CompositorDrawData::render_pipeline_screenshot".into(),
render_targets: smallvec![Some(ViewBuilder::SCREENSHOT_COLOR_FORMAT.into())],
..render_pipeline_descriptor
},
&pools.pipeline_layouts,
&pools.shader_modules,
);

Compositor {
Expand Down
Loading
Loading