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

wgpu: Don't use textures for shader cache keys #17014

Merged
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
42 changes: 13 additions & 29 deletions render/wgpu/src/context3d/current_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ use wgpu::{Buffer, DepthStencilState, StencilFaceState};
use wgpu::{ColorTargetState, RenderPipelineDescriptor, TextureFormat, VertexState};

use std::cell::Cell;
use std::hash::{Hash, Hasher};
use std::num::NonZeroU64;
use std::rc::Rc;

use crate::bitmaps::WgpuSamplerConfig;
use crate::context3d::shader_pair::ShaderCompileData;
use crate::context3d::shader_pair::{ShaderCompileData, ShaderTextureInfo};
use crate::context3d::VertexBufferWrapper;
use crate::descriptors::Descriptors;

Expand Down Expand Up @@ -93,32 +92,6 @@ pub struct BoundTextureData {
pub cube: bool,
}

impl Hash for BoundTextureData {
fn hash<H: Hasher>(&self, state: &mut H) {
// We can't hash 'view', but we can hash the pointer of the 'Rc<dyn Texture>',
// which is unique to the TextureView
let BoundTextureData { id, cube, view: _ } = self;
(Rc::as_ptr(id) as *const ()).hash(state);
cube.hash(state);
}
}

impl PartialEq for BoundTextureData {
fn eq(&self, other: &Self) -> bool {
let BoundTextureData { id, cube, view: _ } = self;
let BoundTextureData {
id: other_id,
cube: other_cube,
view: _,
} = other;
std::ptr::eq(
Rc::as_ptr(id) as *const (),
Rc::as_ptr(other_id) as *const (),
) && cube == other_cube
}
}
impl Eq for BoundTextureData {}

impl CurrentPipeline {
pub fn new(descriptors: &Descriptors) -> Self {
let vertex_shader_uniforms = descriptors.device.create_buffer(&BufferDescriptor {
Expand Down Expand Up @@ -345,12 +318,23 @@ impl CurrentPipeline {
})
});

let mut texture_infos = [None; 8];
for (i, bound_texture) in self.bound_textures.iter().enumerate() {
if let Some(bound_texture) = bound_texture {
if bound_texture.cube {
texture_infos[i] = Some(ShaderTextureInfo::Cube);
} else {
texture_infos[i] = Some(ShaderTextureInfo::D2);
}
}
}

let compiled_shaders = self.shaders.as_ref().expect("Missing shaders!").compile(
descriptors,
ShaderCompileData {
vertex_attributes: agal_attributes,
sampler_configs: self.sampler_configs,
bound_textures: self.bound_textures.clone(),
texture_infos,
},
);

Expand Down
21 changes: 13 additions & 8 deletions render/wgpu/src/context3d/shader_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
};
use wgpu::SamplerBindingType;

use super::{current_pipeline::BoundTextureData, MAX_VERTEX_ATTRIBUTES};
use super::MAX_VERTEX_ATTRIBUTES;

use crate::descriptors::Descriptors;

Expand Down Expand Up @@ -110,12 +110,11 @@ impl ShaderPairAgal {
},
];

for (i, bound_texture) in data.bound_textures.iter().enumerate() {
if let Some(bound_texture) = bound_texture {
let dimension = if bound_texture.cube {
wgpu::TextureViewDimension::Cube
} else {
wgpu::TextureViewDimension::D2
for (i, texture_info) in data.texture_infos.iter().enumerate() {
if let Some(texture_info) = texture_info {
let dimension = match texture_info {
ShaderTextureInfo::D2 => wgpu::TextureViewDimension::D2,
ShaderTextureInfo::Cube => wgpu::TextureViewDimension::Cube,
};
layout_entries.push(wgpu::BindGroupLayoutEntry {
binding: naga_agal::TEXTURE_START_BIND_INDEX + i as u32,
Expand Down Expand Up @@ -155,9 +154,15 @@ impl ShaderPairAgal {
}
}

#[derive(Hash, PartialEq, Eq, Clone, Copy)]
pub enum ShaderTextureInfo {
D2,
Cube,
}

#[derive(Hash, Eq, PartialEq, Clone)]
pub struct ShaderCompileData {
pub sampler_configs: [SamplerConfig; 8],
pub vertex_attributes: [Option<VertexAttributeFormat>; MAX_VERTEX_ATTRIBUTES],
pub bound_textures: [Option<BoundTextureData>; 8],
pub texture_infos: [Option<ShaderTextureInfo>; 8],
}
Loading