use crate::anti_alias::AntiAliasing;
use crate::dynamic_materials::BucketEntry;
use crate::error::Result;
use crate::pipeline_layouts::PipelineLayoutCacheKey;
use crate::pipelines::compute_pipeline::ComputePipelineCacheKey;
use crate::render_passes::material_classify::{
bind_group::MaterialClassifyBindGroups, shader::cache_key::ShaderCacheKeyMaterialClassify,
};
use crate::render_passes::RenderPassInitContext;
use crate::shaders::ShaderCacheKey;
pub struct MaterialClassifyPipelines;
pub struct MaterialClassifyPrewarmDescriptors {
pub pipeline_cache_keys: Vec<ComputePipelineCacheKey>,
pub slot_msaa: Vec<Option<u32>>,
}
impl MaterialClassifyPipelines {
pub async fn warm_pool(
ctx: &mut RenderPassInitContext<'_>,
bind_groups: &MaterialClassifyBindGroups,
bucket_entries: &[BucketEntry],
) -> Result<()> {
ctx.shaders
.ensure_keys(
ctx.gpu,
Self::shader_cache_keys(ctx.gpu, bucket_entries, ctx.anti_aliasing),
)
.await?;
let descs = Self::build_descriptors(ctx, bind_groups, bucket_entries).await?;
ctx.pipelines
.compute
.ensure_keys(
ctx.gpu,
ctx.shaders,
ctx.pipeline_layouts,
descs.pipeline_cache_keys.clone(),
)
.await?;
Ok(())
}
pub fn shader_cache_keys(
gpu: &awsm_renderer_core::renderer::AwsmRendererWebGpu,
bucket_entries: &[BucketEntry],
anti_aliasing: &AntiAliasing,
) -> Vec<ShaderCacheKey> {
let active_msaa = match anti_aliasing.msaa_sample_count {
Some(4) => Some(4),
_ => None,
};
vec![ShaderCacheKey::from(ShaderCacheKeyMaterialClassify {
msaa_sample_count: active_msaa,
bucket_count: bucket_entries.len() as u32,
emit_edge_data: active_msaa.is_some() && crate::edge_resolve_supported(gpu),
})]
}
pub async fn build_descriptors(
ctx: &mut RenderPassInitContext<'_>,
bind_groups: &MaterialClassifyBindGroups,
bucket_entries: &[BucketEntry],
) -> Result<MaterialClassifyPrewarmDescriptors> {
Self::build_descriptors_for_config(
ctx.gpu,
ctx.bind_group_layouts,
ctx.pipeline_layouts,
ctx.shaders,
bind_groups,
bucket_entries,
ctx.anti_aliasing,
)
.await
}
pub async fn build_descriptors_for_config(
gpu: &awsm_renderer_core::renderer::AwsmRendererWebGpu,
bind_group_layouts: &mut crate::bind_group_layout::BindGroupLayouts,
pipeline_layouts: &mut crate::pipeline_layouts::PipelineLayouts,
shaders: &mut crate::shaders::Shaders,
bind_groups: &MaterialClassifyBindGroups,
bucket_entries: &[BucketEntry],
anti_aliasing: &AntiAliasing,
) -> Result<MaterialClassifyPrewarmDescriptors> {
let (active_msaa, bgl_key) = match anti_aliasing.msaa_sample_count {
Some(4) => (Some(4), bind_groups.multisampled_bind_group_layout_key),
_ => (None, bind_groups.singlesampled_bind_group_layout_key),
};
let pipeline_layout_key = pipeline_layouts.get_key(
gpu,
bind_group_layouts,
PipelineLayoutCacheKey::new(vec![bgl_key]),
)?;
let shader_key = shaders
.get_key(
gpu,
ShaderCacheKeyMaterialClassify {
msaa_sample_count: active_msaa,
bucket_count: bucket_entries.len() as u32,
emit_edge_data: active_msaa.is_some() && crate::edge_resolve_supported(gpu),
},
)
.await?;
Ok(MaterialClassifyPrewarmDescriptors {
pipeline_cache_keys: vec![ComputePipelineCacheKey::new(
shader_key,
pipeline_layout_key,
)],
slot_msaa: vec![active_msaa],
})
}
}