Skip to content

Commit

Permalink
wgpu: Cache Program3D bind group layout
Browse files Browse the repository at this point in the history
The bind group layout only depends on the texture registers
(and 2D/cubemap type) accessed by the fragment shader, not on
the runtime texture bound with Context3D. This means that we can
build and cache it when we compile the AGAL program to a Naga
module.

Since the bind group layout is used for the overall pipeline, I've
refactored the shader caching code into `ShaderPairAgal`, which
holds both the vertex and fragment shader bytecode, and compiles
both in the `compile` function.
  • Loading branch information
Aaron1011 committed Jul 9, 2023
1 parent a0d6389 commit b6fae8e
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 281 deletions.
20 changes: 5 additions & 15 deletions core/src/avm2/object/context3d_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,8 @@ impl<'gc> Context3DObject<'gc> {
.unwrap()
.process_command(
Context3DCommand::UploadShaders {
vertex_shader: program.vertex_shader_handle(),
module: program.shader_module_handle(),
vertex_shader_agal,
fragment_shader: program.fragment_shader_handle(),
fragment_shader_agal,
},
activation.context.gc_context,
Expand All @@ -263,15 +262,9 @@ impl<'gc> Context3DObject<'gc> {
activation: &mut Activation<'_, 'gc>,
program: Option<Program3DObject<'gc>>,
) {
let (vertex_shader, fragment_shader) = match program {
Some(program) => (
program.vertex_shader_handle(),
program.fragment_shader_handle(),
),
None => (
GcCell::allocate(activation.context.gc_context, None),
GcCell::allocate(activation.context.gc_context, None),
),
let module = match program {
Some(program) => program.shader_module_handle(),
None => GcCell::allocate(activation.context.gc_context, None),
};

self.0
Expand All @@ -280,10 +273,7 @@ impl<'gc> Context3DObject<'gc> {
.as_mut()
.unwrap()
.process_command(
Context3DCommand::SetShaders {
vertex_shader,
fragment_shader,
},
Context3DCommand::SetShaders { module },
activation.context.gc_context,
);
}
Expand Down
15 changes: 4 additions & 11 deletions core/src/avm2/object/program_3d_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ impl<'gc> Program3DObject<'gc> {
Program3DObjectData {
base,
context3d,
vertex_shader_handle: GcCell::allocate(activation.context.gc_context, None),
fragment_shader_handle: GcCell::allocate(activation.context.gc_context, None),
shader_module_handle: GcCell::allocate(activation.context.gc_context, None),
},
))
.into();
Expand All @@ -45,12 +44,8 @@ impl<'gc> Program3DObject<'gc> {
Ok(this)
}

pub fn vertex_shader_handle(&self) -> GcCell<'gc, Option<Rc<dyn ShaderModule>>> {
self.0.read().vertex_shader_handle
}

pub fn fragment_shader_handle(&self) -> GcCell<'gc, Option<Rc<dyn ShaderModule>>> {
self.0.read().fragment_shader_handle
pub fn shader_module_handle(&self) -> GcCell<'gc, Option<Rc<dyn ShaderModule>>> {
self.0.read().shader_module_handle
}

pub fn context3d(&self) -> Context3DObject<'gc> {
Expand All @@ -66,9 +61,7 @@ pub struct Program3DObjectData<'gc> {

context3d: Context3DObject<'gc>,

vertex_shader_handle: GcCell<'gc, Option<Rc<dyn ShaderModule>>>,

fragment_shader_handle: GcCell<'gc, Option<Rc<dyn ShaderModule>>>,
shader_module_handle: GcCell<'gc, Option<Rc<dyn ShaderModule>>>,
}

impl<'gc> TObject<'gc> for Program3DObject<'gc> {
Expand Down
6 changes: 2 additions & 4 deletions render/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,15 +429,13 @@ pub enum Context3DCommand<'a, 'gc> {
},

UploadShaders {
vertex_shader: GcCell<'gc, Option<Rc<dyn ShaderModule>>>,
module: GcCell<'gc, Option<Rc<dyn ShaderModule>>>,
vertex_shader_agal: Vec<u8>,
fragment_shader: GcCell<'gc, Option<Rc<dyn ShaderModule>>>,
fragment_shader_agal: Vec<u8>,
},

SetShaders {
vertex_shader: GcCell<'gc, Option<Rc<dyn ShaderModule>>>,
fragment_shader: GcCell<'gc, Option<Rc<dyn ShaderModule>>>,
module: GcCell<'gc, Option<Rc<dyn ShaderModule>>>,
},
SetProgramConstantsFromVector {
program_type: ProgramType,
Expand Down
Loading

0 comments on commit b6fae8e

Please sign in to comment.