use super::*;
use crate::wgpu;
use wgpu::*;
impl WgpuRenderer {
pub(super) fn create_render_pipeline(
&mut self,
backend_data: &mut WgpuBackendData,
) -> RendererResult<()> {
let device = &backend_data.device;
let common_layout = backend_data
.render_resources
.common_bind_group_layout()
.ok_or_else(|| {
RendererError::InvalidRenderState(
"Common bind group layout not initialized".to_string(),
)
})?;
let image_layout = backend_data
.render_resources
.image_bind_group_layout()
.ok_or_else(|| {
RendererError::InvalidRenderState(
"Image bind group layout not initialized".to_string(),
)
})?;
#[cfg(any(feature = "wgpu-29", feature = "wgpu-30"))]
let pipeline_layout = device.create_pipeline_layout(&PipelineLayoutDescriptor {
label: Some("Dear ImGui Pipeline Layout"),
bind_group_layouts: &[Some(common_layout), Some(image_layout)],
immediate_size: 0,
});
#[cfg(any(feature = "wgpu-27", feature = "wgpu-28"))]
let pipeline_layout = device.create_pipeline_layout(&PipelineLayoutDescriptor {
label: Some("Dear ImGui Pipeline Layout"),
bind_group_layouts: &[common_layout, image_layout],
#[cfg(feature = "wgpu-28")]
immediate_size: 0,
#[cfg(feature = "wgpu-27")]
push_constant_ranges: &[],
});
let shader_module = self.shader_manager.get_shader_module()?;
let vertex_buffer_layout = crate::shaders::create_vertex_buffer_layout();
#[cfg(feature = "wgpu-30")]
let vertex_buffer_layouts = [Some(vertex_buffer_layout)];
#[cfg(not(feature = "wgpu-30"))]
let vertex_buffer_layouts = [vertex_buffer_layout];
let vertex_state =
crate::shaders::create_vertex_state(shader_module, &vertex_buffer_layouts);
let color_target = ColorTargetState {
format: backend_data.render_target_format,
blend: Some(BlendState {
color: BlendComponent {
src_factor: BlendFactor::SrcAlpha,
dst_factor: BlendFactor::OneMinusSrcAlpha,
operation: BlendOperation::Add,
},
alpha: BlendComponent {
src_factor: BlendFactor::One,
dst_factor: BlendFactor::OneMinusSrcAlpha,
operation: BlendOperation::Add,
},
}),
write_mask: ColorWrites::ALL,
};
let use_gamma_correction = matches!(
backend_data.render_target_format,
TextureFormat::Rgba8UnormSrgb
| TextureFormat::Bgra8UnormSrgb
| TextureFormat::Bc1RgbaUnormSrgb
| TextureFormat::Bc2RgbaUnormSrgb
| TextureFormat::Bc3RgbaUnormSrgb
| TextureFormat::Bc7RgbaUnormSrgb
);
let color_targets = [Some(color_target)];
let fragment_state = crate::shaders::create_fragment_state(
shader_module,
&color_targets,
use_gamma_correction,
);
let depth_stencil = backend_data.depth_stencil_format.map(|format| {
#[cfg(any(feature = "wgpu-29", feature = "wgpu-30"))]
let state = DepthStencilState {
format,
depth_write_enabled: Some(false), depth_compare: Some(CompareFunction::Always), stencil: StencilState {
front: StencilFaceState {
compare: CompareFunction::Always, fail_op: StencilOperation::Keep, depth_fail_op: StencilOperation::Keep, pass_op: StencilOperation::Keep, },
back: StencilFaceState {
compare: CompareFunction::Always, fail_op: StencilOperation::Keep, depth_fail_op: StencilOperation::Keep, pass_op: StencilOperation::Keep, },
read_mask: 0xff, write_mask: 0xff, },
bias: DepthBiasState::default(),
};
#[cfg(any(feature = "wgpu-27", feature = "wgpu-28"))]
let state = DepthStencilState {
format,
depth_write_enabled: false, depth_compare: CompareFunction::Always, stencil: StencilState {
front: StencilFaceState {
compare: CompareFunction::Always, fail_op: StencilOperation::Keep, depth_fail_op: StencilOperation::Keep, pass_op: StencilOperation::Keep, },
back: StencilFaceState {
compare: CompareFunction::Always, fail_op: StencilOperation::Keep, depth_fail_op: StencilOperation::Keep, pass_op: StencilOperation::Keep, },
read_mask: 0xff, write_mask: 0xff, },
bias: DepthBiasState::default(),
};
state
});
let pipeline = device.create_render_pipeline(&RenderPipelineDescriptor {
label: Some("Dear ImGui Render Pipeline"),
layout: Some(&pipeline_layout),
vertex: vertex_state,
primitive: PrimitiveState {
topology: PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: FrontFace::Cw,
cull_mode: None,
polygon_mode: PolygonMode::Fill,
unclipped_depth: false,
conservative: false,
},
depth_stencil,
multisample: backend_data.init_info.pipeline_multisample_state,
fragment: Some(fragment_state),
#[cfg(any(feature = "wgpu-28", feature = "wgpu-29", feature = "wgpu-30"))]
multiview_mask: None,
#[cfg(feature = "wgpu-27")]
multiview: None,
cache: None,
});
backend_data.pipeline_state = Some(pipeline);
Ok(())
}
}