use crate::define_atomic_id;
use crate::renderer::WgpuWrapper;
use alloc::borrow::Cow;
use bevy_asset::Handle;
use bevy_mesh::VertexBufferLayout;
use bevy_shader::{Shader, ShaderDefVal};
use core::iter;
use core::ops::Deref;
use thiserror::Error;
use wgpu::{
BindGroupLayoutEntry, ColorTargetState, DepthStencilState, MultisampleState, PrimitiveState,
PushConstantRange,
};
define_atomic_id!(RenderPipelineId);
#[derive(Clone, Debug)]
pub struct RenderPipeline {
id: RenderPipelineId,
value: WgpuWrapper<wgpu::RenderPipeline>,
}
impl RenderPipeline {
#[inline]
pub fn id(&self) -> RenderPipelineId {
self.id
}
}
impl From<wgpu::RenderPipeline> for RenderPipeline {
fn from(value: wgpu::RenderPipeline) -> Self {
RenderPipeline {
id: RenderPipelineId::new(),
value: WgpuWrapper::new(value),
}
}
}
impl Deref for RenderPipeline {
type Target = wgpu::RenderPipeline;
#[inline]
fn deref(&self) -> &Self::Target {
&self.value
}
}
define_atomic_id!(ComputePipelineId);
#[derive(Clone, Debug)]
pub struct ComputePipeline {
id: ComputePipelineId,
value: WgpuWrapper<wgpu::ComputePipeline>,
}
impl ComputePipeline {
#[inline]
pub fn id(&self) -> ComputePipelineId {
self.id
}
}
impl From<wgpu::ComputePipeline> for ComputePipeline {
fn from(value: wgpu::ComputePipeline) -> Self {
ComputePipeline {
id: ComputePipelineId::new(),
value: WgpuWrapper::new(value),
}
}
}
impl Deref for ComputePipeline {
type Target = wgpu::ComputePipeline;
#[inline]
fn deref(&self) -> &Self::Target {
&self.value
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)]
pub struct BindGroupLayoutDescriptor {
pub label: Cow<'static, str>,
pub entries: Vec<BindGroupLayoutEntry>,
}
impl BindGroupLayoutDescriptor {
pub fn new(label: impl Into<Cow<'static, str>>, entries: &[BindGroupLayoutEntry]) -> Self {
Self {
label: label.into(),
entries: entries.into(),
}
}
}
#[derive(Clone, Debug, PartialEq, Default)]
pub struct RenderPipelineDescriptor {
pub label: Option<Cow<'static, str>>,
pub layout: Vec<BindGroupLayoutDescriptor>,
pub push_constant_ranges: Vec<PushConstantRange>,
pub vertex: VertexState,
pub primitive: PrimitiveState,
pub depth_stencil: Option<DepthStencilState>,
pub multisample: MultisampleState,
pub fragment: Option<FragmentState>,
pub zero_initialize_workgroup_memory: bool,
}
#[derive(Copy, Clone, Debug, Error)]
#[error("RenderPipelineDescriptor has no FragmentState configured")]
pub struct NoFragmentStateError;
impl RenderPipelineDescriptor {
pub fn fragment_mut(&mut self) -> Result<&mut FragmentState, NoFragmentStateError> {
self.fragment.as_mut().ok_or(NoFragmentStateError)
}
pub fn set_layout(&mut self, index: usize, layout: BindGroupLayoutDescriptor) {
filling_set_at(&mut self.layout, index, bevy_utils::default(), layout);
}
}
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct VertexState {
pub shader: Handle<Shader>,
pub shader_defs: Vec<ShaderDefVal>,
pub entry_point: Option<Cow<'static, str>>,
pub buffers: Vec<VertexBufferLayout>,
}
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub struct FragmentState {
pub shader: Handle<Shader>,
pub shader_defs: Vec<ShaderDefVal>,
pub entry_point: Option<Cow<'static, str>>,
pub targets: Vec<Option<ColorTargetState>>,
}
impl FragmentState {
pub fn set_target(&mut self, index: usize, target: ColorTargetState) {
filling_set_at(&mut self.targets, index, None, Some(target));
}
}
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub struct ComputePipelineDescriptor {
pub label: Option<Cow<'static, str>>,
pub layout: Vec<BindGroupLayoutDescriptor>,
pub push_constant_ranges: Vec<PushConstantRange>,
pub shader: Handle<Shader>,
pub shader_defs: Vec<ShaderDefVal>,
pub entry_point: Option<Cow<'static, str>>,
pub zero_initialize_workgroup_memory: bool,
}
fn filling_set_at<T: Clone>(vec: &mut Vec<T>, index: usize, filler: T, value: T) {
let num_to_fill = (index + 1).saturating_sub(vec.len());
vec.extend(iter::repeat_n(filler, num_to_fill));
vec[index] = value;
}