Skip to content

Commit

Permalink
Create separate shader pipeline for each use case (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechBarczynski authored Dec 19, 2023
1 parent 5cf42ab commit 926030d
Show file tree
Hide file tree
Showing 38 changed files with 686 additions and 848 deletions.
3 changes: 2 additions & 1 deletion compositor_render/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::transformations::web_renderer::CreateWebRendererError;
use crate::wgpu::common_pipeline::CreateShaderError;
use crate::wgpu::CreateWgpuCtxError;
use crate::{
registry,
scene::SceneError,
transformations::{
image_renderer::ImageError, web_renderer::chromium_context::WebRendererContextError,
},
wgpu::{shader::CreateShaderError, CreateWgpuCtxError},
};
use crate::{OutputId, RendererId};

Expand Down
2 changes: 1 addition & 1 deletion compositor_render/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::sync::Arc;
use std::time::Duration;

use crate::transformations::image_renderer::Image;
use crate::transformations::shader::validation::error::ParametersValidationError;
use crate::transformations::shader::Shader;
use crate::transformations::text_renderer::TextRenderParams;
use crate::transformations::web_renderer::WebRenderer;
use crate::wgpu::validation::ParametersValidationError;
use crate::{InputId, OutputId, RendererId, Resolution};

use self::image_component::StatefulImageComponent;
Expand Down
2 changes: 1 addition & 1 deletion compositor_render/src/state/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl InnerRenderNode {

match self {
InnerRenderNode::Shader(ref shader) => {
shader.render(sources, target, pts);
shader.render(ctx.wgpu_ctx, sources, target, pts);
}
InnerRenderNode::Web(renderer) => renderer.render(ctx, sources, target),
InnerRenderNode::Text(renderer) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn vs_main(input: VertexInput) -> VertexOutput {
fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
let current_layout = layouts[layout_id];

// sampling can't be conditional, so in case of texture_id == -1
// sampling can't be conditional, so in case of plane_id == -1
// sample textures[0], but ignore the result.
if (current_layout.is_texture == 0u) {
return current_layout.color;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use crate::wgpu::{shader::CreateShaderError, WgpuCtx};
use crate::wgpu::{common_pipeline::CreateShaderError, WgpuCtx};

use super::shader::LayoutShader;

Expand Down
2 changes: 1 addition & 1 deletion compositor_render/src/transformations/layout/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl ParamsBuffer {
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("params bind group"),
layout: &wgpu_ctx.shader_parameters_bind_group_layout,
layout: &wgpu_ctx.uniform_bgl,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: buffer.as_entire_binding(),
Expand Down
87 changes: 35 additions & 52 deletions compositor_render/src/transformations/layout/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use std::sync::Arc;
use wgpu::ShaderStages;

use crate::wgpu::{
common_pipeline::Sampler,
shader::{pipeline, CreateShaderError},
texture::{NodeTexture, NodeTextureState, Texture},
common_pipeline::{self, CreateShaderError, Sampler},
texture::{NodeTexture, NodeTextureState},
WgpuCtx, WgpuErrorScope,
};

Expand All @@ -14,8 +13,6 @@ pub struct LayoutShader {
pipeline: wgpu::RenderPipeline,
sampler: Sampler,
texture_bgl: wgpu::BindGroupLayout,

empty_texture: Texture,
}

impl LayoutShader {
Expand All @@ -38,22 +35,7 @@ impl LayoutShader {
) -> Result<Self, CreateShaderError> {
let sampler = Sampler::new(&wgpu_ctx.device);

let texture_bgl =
wgpu_ctx
.device
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("Layout texture bgl"),
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
count: None,
visibility: wgpu::ShaderStages::FRAGMENT | wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Texture {
multisampled: false,
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2,
},
}],
});
let texture_bgl = common_pipeline::create_single_texture_bgl(&wgpu_ctx.device);

let pipeline_layout =
wgpu_ctx
Expand All @@ -62,7 +44,7 @@ impl LayoutShader {
label: Some("shader transformation pipeline layout"),
bind_group_layouts: &[
&texture_bgl,
&wgpu_ctx.shader_parameters_bind_group_layout,
&wgpu_ctx.uniform_bgl,
&sampler.bind_group_layout,
],
push_constant_ranges: &[wgpu::PushConstantRange {
Expand All @@ -71,19 +53,16 @@ impl LayoutShader {
}],
});

let pipeline = pipeline::Pipeline::create_render_pipeline(
let pipeline = common_pipeline::create_render_pipeline(
&wgpu_ctx.device,
&pipeline_layout,
&shader_module,
);

let empty_texture = Texture::empty(wgpu_ctx);

Ok(Self {
pipeline,
sampler,
texture_bgl,
empty_texture,
})
}

Expand All @@ -94,27 +73,7 @@ impl LayoutShader {
textures: &[Option<&NodeTexture>],
target: &NodeTextureState,
) {
let input_texture_bgs: Vec<wgpu::BindGroup> = textures
.iter()
.map(|texture| {
texture
.and_then(|texture| texture.state())
.map(|state| &state.rgba_texture().texture().view)
.unwrap_or(&self.empty_texture.view)
})
.map(|view| {
wgpu_ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &self.texture_bgl,
label: None,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(view),
}],
})
})
.collect();
let input_texture_bgs: Vec<wgpu::BindGroup> = self.input_textures_bg(wgpu_ctx, textures);

let mut encoder = wgpu_ctx.device.create_command_encoder(&Default::default());
{
Expand Down Expand Up @@ -145,13 +104,37 @@ impl LayoutShader {
render_pass.set_bind_group(1, params, &[]);
render_pass.set_bind_group(2, &self.sampler.bind_group, &[]);

wgpu_ctx
.plane_cache
.plane(layout_id as i32)
.unwrap()
.draw(&mut render_pass);
wgpu_ctx.plane.draw(&mut render_pass);
}
}
wgpu_ctx.queue.submit(Some(encoder.finish()));
}

fn input_textures_bg(
&self,
wgpu_ctx: &Arc<WgpuCtx>,
textures: &[Option<&NodeTexture>],
) -> Vec<wgpu::BindGroup> {
textures
.iter()
.map(|texture| {
texture
.and_then(|texture| texture.state())
.map(|state| &state.rgba_texture().texture().view)
.unwrap_or(&wgpu_ctx.empty_texture.view)
})
.map(|view| {
wgpu_ctx
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &self.texture_bgl,
label: None,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(view),
}],
})
})
.collect()
}
}
22 changes: 13 additions & 9 deletions compositor_render/src/transformations/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ use std::sync::Arc;

use crate::{
scene::ShaderParam,
wgpu::{
shader::{CreateShaderError, WgpuShader},
validation::ParametersValidationError,
WgpuCtx,
},
wgpu::{common_pipeline::CreateShaderError, WgpuCtx},
FallbackStrategy, RendererId,
};

use self::{pipeline::ShaderPipeline, validation::error::ParametersValidationError};

mod base_params;
pub mod node;
mod pipeline;
pub mod validation;

const SHADER_INPUT_TEXTURES_AMOUNT: u32 = 16;

#[derive(Debug)]
pub struct Shader {
wgpu_shader: WgpuShader,
pipeline: ShaderPipeline,
fallback_strategy: FallbackStrategy,
clear_color: Option<wgpu::Color>,
}
Expand All @@ -30,10 +33,11 @@ impl Shader {
pub fn new(wgpu_ctx: &Arc<WgpuCtx>, spec: ShaderSpec) -> Result<Self, CreateShaderError> {
let fallback_strategy = spec.fallback_strategy;
let clear_color = None;
let wgpu_shader = WgpuShader::new(wgpu_ctx, spec.source)?;
// let wgpu_shader = WgpuShader::new(wgpu_ctx, spec.source)?;
let pipeline = ShaderPipeline::new(wgpu_ctx, &spec.source)?;

Ok(Self {
wgpu_shader,
pipeline,
fallback_strategy,
clear_color,
})
Expand All @@ -43,6 +47,6 @@ impl Shader {
&self,
params: &ShaderParam,
) -> Result<(), ParametersValidationError> {
self.wgpu_shader.validate_params(params)
self.pipeline.validate_params(params)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,33 @@ use crate::Resolution;

#[repr(C)]
#[derive(Debug, bytemuck::Pod, bytemuck::Zeroable, Clone, Copy)]
pub struct CommonShaderParameters {
pub struct BaseShaderParameters {
plane_id: i32,
time: f32,
pub texture_count: u32,
texture_count: u32,
output_resolution: [u32; 2],
}

impl CommonShaderParameters {
pub fn new(time: Duration, texture_count: u32, output_resolution: Resolution) -> Self {
impl BaseShaderParameters {
pub fn new(
plane_id: i32,
time: Duration,
texture_count: u32,
output_resolution: Resolution,
) -> Self {
Self {
time: time.as_secs_f32(),
texture_count,
output_resolution: [
output_resolution.width as u32,
output_resolution.height as u32,
],
plane_id,
}
}

pub fn push_constant_size() -> u32 {
let size = std::mem::size_of::<CommonShaderParameters>() as u32;
let size = std::mem::size_of::<BaseShaderParameters>() as u32;
match size % 4 {
0 => size,
rest => size + (4 - rest),
Expand Down
9 changes: 6 additions & 3 deletions compositor_render/src/transformations/shader/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl ShaderNode {
fn new_params_bind_group(ctx: &WgpuCtx, buffer: &wgpu::Buffer) -> wgpu::BindGroup {
ctx.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("shader node params bind group"),
layout: &ctx.shader_parameters_bind_group_layout,
layout: &ctx.uniform_bgl,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: buffer.as_entire_binding(),
Expand All @@ -74,12 +74,15 @@ impl ShaderNode {

pub fn render(
&self,
wgpu_ctx: &Arc<WgpuCtx>,
sources: &[(&NodeId, &NodeTexture)],
target: &mut NodeTexture,
pts: Duration,
) {
let target = target.ensure_size(&self.shader.wgpu_shader.wgpu_ctx, self.resolution);
self.shader.wgpu_shader.render(
let target = target.ensure_size(wgpu_ctx, self.resolution);

self.shader.pipeline.render(
wgpu_ctx,
&self.params_bind_group,
sources,
target,
Expand Down
Loading

0 comments on commit 926030d

Please sign in to comment.