Skip to content

Commit

Permalink
rt: pass device context to uniform binder if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
chyyran committed Mar 4, 2024
1 parent 3a11ff4 commit 1cffa3d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 59 deletions.
10 changes: 6 additions & 4 deletions librashader-runtime-gl/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ impl GlUniformScalar for u32 {
}

pub(crate) struct GlUniformBinder;
impl<T> BindUniform<VariableLocation, T> for GlUniformBinder
impl<T> BindUniform<VariableLocation, T, ()> for GlUniformBinder
where
T: GlUniformScalar,
{
fn bind_uniform(block: UniformMemberBlock, value: T, location: VariableLocation) -> Option<()> {
fn bind_uniform(block: UniformMemberBlock, value: T, location: VariableLocation, _: &()) -> Option<()> {
if let Some(location) = location.location(block)
&& location.bindable()
{
Expand All @@ -84,11 +84,12 @@ where
}
}

impl BindUniform<VariableLocation, &[f32; 4]> for GlUniformBinder {
impl BindUniform<VariableLocation, &[f32; 4], ()> for GlUniformBinder {
fn bind_uniform(
block: UniformMemberBlock,
vec4: &[f32; 4],
location: VariableLocation,
_: &()
) -> Option<()> {
if let Some(location) = location.location(block)
&& location.bindable()
Expand All @@ -108,11 +109,12 @@ impl BindUniform<VariableLocation, &[f32; 4]> for GlUniformBinder {
}
}

impl BindUniform<VariableLocation, &[f32; 16]> for GlUniformBinder {
impl BindUniform<VariableLocation, &[f32; 16], ()> for GlUniformBinder {
fn bind_uniform(
block: UniformMemberBlock,
mat4: &[f32; 16],
location: VariableLocation,
_: &()
) -> Option<()> {
if let Some(location) = location.location(block)
&& location.bindable()
Expand Down
2 changes: 1 addition & 1 deletion librashader-runtime-vk/src/filter_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::sync::Arc;

pub struct FilterPass {
pub reflection: ShaderReflection,
pub(crate) uniform_storage: UniformStorage<NoUniformBinder, Option<()>, RawVulkanBuffer>,
pub(crate) uniform_storage: UniformStorage<NoUniformBinder, Option<()>, RawVulkanBuffer, Box<[u8]>, Arc<ash::Device>>,
pub uniform_bindings: FastHashMap<UniformBinding, MemberOffset>,
pub source: ShaderSource,
pub config: ShaderPassConfig,
Expand Down
2 changes: 1 addition & 1 deletion librashader-runtime-wgpu/src/filter_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct FilterPass {
pub device: Arc<wgpu::Device>,
pub reflection: ShaderReflection,
pub(crate) uniform_storage:
UniformStorage<NoUniformBinder, Option<()>, WgpuStagedBuffer, WgpuStagedBuffer>,
UniformStorage<NoUniformBinder, Option<()>, WgpuStagedBuffer, WgpuStagedBuffer, Arc<wgpu::Device>>,
pub uniform_bindings: FastHashMap<UniformBinding, MemberOffset>,
pub source: ShaderSource,
pub config: ShaderPassConfig,
Expand Down
64 changes: 35 additions & 29 deletions librashader-runtime/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ pub trait TextureInput {
}

/// A uniform member offset with context that needs to be resolved.
pub trait ContextOffset<H, C>
pub trait ContextOffset<H, C, D=()>
where
H: BindUniform<C, f32>,
H: BindUniform<C, u32>,
H: BindUniform<C, i32>,
H: for<'a> BindUniform<C, &'a [f32; 4]>,
H: for<'a> BindUniform<C, &'a [f32; 16]>,
H: BindUniform<C, f32, D>,
H: BindUniform<C, u32, D>,
H: BindUniform<C, i32, D>,
H: for<'a> BindUniform<C, &'a [f32; 4], D>,
H: for<'a> BindUniform<C, &'a [f32; 16], D>,
{
/// Gets the `MemberOffset` part of the offset.
fn offset(&self) -> MemberOffset;
Expand All @@ -30,13 +30,13 @@ where
fn context(&self) -> C;
}

impl<H> ContextOffset<H, Option<()>> for MemberOffset
impl<D, H> ContextOffset<H, Option<()>, D> for MemberOffset
where
H: BindUniform<Option<()>, f32>,
H: BindUniform<Option<()>, u32>,
H: BindUniform<Option<()>, i32>,
H: for<'a> BindUniform<Option<()>, &'a [f32; 4]>,
H: for<'a> BindUniform<Option<()>, &'a [f32; 16]>,
H: BindUniform<Option<()>, f32, D>,
H: BindUniform<Option<()>, u32, D>,
H: BindUniform<Option<()>, i32, D>,
H: for<'a> BindUniform<Option<()>, &'a [f32; 4], D>,
H: for<'a> BindUniform<Option<()>, &'a [f32; 16], D>,
{
fn offset(&self) -> MemberOffset {
*self
Expand Down Expand Up @@ -73,11 +73,11 @@ where
C: Copy,
U: Deref<Target = [u8]> + DerefMut,
P: Deref<Target = [u8]> + DerefMut,
H: BindUniform<C, f32>,
H: BindUniform<C, u32>,
H: BindUniform<C, i32>,
H: for<'b> BindUniform<C, &'b [f32; 4]>,
H: for<'b> BindUniform<C, &'b [f32; 16]>,
H: BindUniform<C, f32, Self::DeviceContext>,
H: BindUniform<C, u32, Self::DeviceContext>,
H: BindUniform<C, i32, Self::DeviceContext>,
H: for<'b> BindUniform<C, &'b [f32; 4], Self::DeviceContext>,
H: for<'b> BindUniform<C, &'b [f32; 16], Self::DeviceContext>,
{
/// The type of the input texture used for semantic binding.
type InputTexture: TextureInput;
Expand All @@ -92,7 +92,7 @@ where
type DeviceContext;

/// The type of uniform offsets to use.
type UniformOffset: ContextOffset<H, C>;
type UniformOffset: ContextOffset<H, C, Self::DeviceContext>;

/// Bind a texture to the input descriptor set
fn bind_texture<'a>(
Expand All @@ -108,7 +108,7 @@ where
fn bind_semantics<'a>(
device: &Self::DeviceContext,
sampler_set: &Self::SamplerSet,
uniform_storage: &mut UniformStorage<H, C, U, P>,
uniform_storage: &mut UniformStorage<H, C, U, P, Self::DeviceContext>,
descriptor_set: &mut Self::DescriptorSet<'a>,
uniform_inputs: UniformInputs<'_>,
original: &Self::InputTexture,
Expand All @@ -124,7 +124,7 @@ where
) {
// Bind MVP
if let Some(offset) = uniform_bindings.get(&UniqueSemantics::MVP.into()) {
uniform_storage.bind_mat4(offset.offset(), uniform_inputs.mvp, offset.context());
uniform_storage.bind_mat4(offset.offset(), uniform_inputs.mvp, offset.context(), device);
}

// Bind OutputSize
Expand All @@ -133,6 +133,7 @@ where
offset.offset(),
uniform_inputs.framebuffer_size,
offset.context(),
device
);
}

Expand All @@ -142,6 +143,7 @@ where
offset.offset(),
uniform_inputs.viewport_size,
offset.context(),
device
);
}

Expand All @@ -151,6 +153,7 @@ where
offset.offset(),
uniform_inputs.frame_count,
offset.context(),
device
);
}

Expand All @@ -160,12 +163,13 @@ where
offset.offset(),
uniform_inputs.frame_direction,
offset.context(),
device
);
}

// bind Rotation
if let Some(offset) = uniform_bindings.get(&UniqueSemantics::Rotation.into()) {
uniform_storage.bind_scalar(offset.offset(), uniform_inputs.rotation, offset.context());
uniform_storage.bind_scalar(offset.offset(), uniform_inputs.rotation, offset.context(), device);
}

// bind TotalSubFrames
Expand All @@ -174,6 +178,7 @@ where
offset.offset(),
uniform_inputs.total_subframes,
offset.context(),
device
);
}

Expand All @@ -183,6 +188,7 @@ where
offset.offset(),
uniform_inputs.current_subframe,
offset.context(),
device
);
}

Expand All @@ -194,7 +200,7 @@ where
// bind OriginalSize
if let Some(offset) = uniform_bindings.get(&TextureSemantics::Original.semantics(0).into())
{
uniform_storage.bind_vec4(offset.offset(), original.size(), offset.context());
uniform_storage.bind_vec4(offset.offset(), original.size(), offset.context(), device);
}

// bind Source sampler
Expand All @@ -204,7 +210,7 @@ where

// bind SourceSize
if let Some(offset) = uniform_bindings.get(&TextureSemantics::Source.semantics(0).into()) {
uniform_storage.bind_vec4(offset.offset(), source.size(), offset.context());
uniform_storage.bind_vec4(offset.offset(), source.size(), offset.context(), device);
}

// OriginalHistory0 aliases OriginalHistory
Expand All @@ -218,7 +224,7 @@ where
if let Some(offset) =
uniform_bindings.get(&TextureSemantics::OriginalHistory.semantics(0).into())
{
uniform_storage.bind_vec4(offset.offset(), original.size(), offset.context());
uniform_storage.bind_vec4(offset.offset(), original.size(), offset.context(), device);
}

// bind OriginalHistory1-..
Expand All @@ -240,7 +246,7 @@ where
.semantics(index + 1)
.into(),
) {
uniform_storage.bind_vec4(offset.offset(), history.size(), offset.context());
uniform_storage.bind_vec4(offset.offset(), history.size(), offset.context(), device);
}
}

Expand All @@ -262,7 +268,7 @@ where
if let Some(offset) =
uniform_bindings.get(&TextureSemantics::PassOutput.semantics(index).into())
{
uniform_storage.bind_vec4(offset.offset(), output.size(), offset.context());
uniform_storage.bind_vec4(offset.offset(), output.size(), offset.context(), device);
}
}

Expand All @@ -283,7 +289,7 @@ where
if let Some(offset) =
uniform_bindings.get(&TextureSemantics::PassFeedback.semantics(index).into())
{
uniform_storage.bind_vec4(offset.offset(), feedback.size(), offset.context());
uniform_storage.bind_vec4(offset.offset(), feedback.size(), offset.context(), device);
}
}

Expand All @@ -301,7 +307,7 @@ where

let value = *runtime_parameters.get(id).unwrap_or(&default);

uniform_storage.bind_scalar(offset.offset(), value, offset.context());
uniform_storage.bind_scalar(offset.offset(), value, offset.context(), device);
}

// bind luts
Expand All @@ -314,7 +320,7 @@ where
if let Some(offset) =
uniform_bindings.get(&TextureSemantics::User.semantics(index).into())
{
uniform_storage.bind_vec4(offset.offset(), lut.size(), offset.context());
uniform_storage.bind_vec4(offset.offset(), lut.size(), offset.context(), device);
}
}
}
Expand Down
Loading

0 comments on commit 1cffa3d

Please sign in to comment.