Skip to content

Commit

Permalink
glsl-out: push constants use anonymous uniforms
Browse files Browse the repository at this point in the history
Previously this was done with UBOs but this posed some problems when
integrating with wgpu (see gfx-rs/wgpu#2400)
  • Loading branch information
JCapucho authored and kvark committed Jan 22, 2022
1 parent 8647e06 commit 81dc674
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 29 deletions.
31 changes: 8 additions & 23 deletions src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ impl crate::AtomicFunction {
impl crate::StorageClass {
fn is_buffer(&self) -> bool {
match *self {
crate::StorageClass::PushConstant
| crate::StorageClass::Uniform
| crate::StorageClass::Storage { .. } => true,
crate::StorageClass::Uniform | crate::StorageClass::Storage { .. } => true,
_ => false,
}
}
Expand Down Expand Up @@ -230,8 +228,6 @@ pub struct Options {
pub writer_flags: WriterFlags,
/// Map of resources association to binding locations.
pub binding_map: BindingMap,
/// The binding to give the push constant buffer if it's present
pub push_constant_binding: u32,
}

impl Default for Options {
Expand All @@ -240,7 +236,6 @@ impl Default for Options {
version: Version::Embedded(310),
writer_flags: WriterFlags::ADJUST_COORDINATE_SPACE,
binding_map: BindingMap::default(),
push_constant_binding: 0,
}
}
}
Expand All @@ -263,7 +258,6 @@ pub struct PipelineOptions {
pub struct ReflectionInfo {
pub texture_mapping: crate::FastHashMap<String, TextureMapping>,
pub uniforms: crate::FastHashMap<Handle<crate::GlobalVariable>, String>,
pub push_constant: Option<String>,
}

/// Structure that connects a texture to a sampler or not
Expand Down Expand Up @@ -894,13 +888,7 @@ impl<'a, W: Write> Writer<'a, W> {
global: &crate::GlobalVariable,
) -> BackendResult {
if self.options.version.supports_explicit_locations() {
if let crate::StorageClass::PushConstant = global.class {
write!(
self.out,
"layout(std140, binding = {}) ",
self.options.push_constant_binding
)?
} else if let Some(ref br) = global.binding {
if let Some(ref br) = global.binding {
match self.options.binding_map.get(br) {
Some(binding) => {
let layout = match global.class {
Expand All @@ -911,9 +899,7 @@ impl<'a, W: Write> Writer<'a, W> {
"std140, "
}
}
crate::StorageClass::Uniform | crate::StorageClass::PushConstant => {
"std140, "
}
crate::StorageClass::Uniform => "std140, ",
_ => "",
};
write!(self.out, "layout({}binding = {}) ", layout, binding)?
Expand Down Expand Up @@ -977,6 +963,11 @@ impl<'a, W: Write> Writer<'a, W> {
false
};

if let crate::StorageClass::PushConstant = global.class {
let global_name = self.get_global_name(handle, global);
self.reflection_names_globals.insert(handle, global_name);
}

// Finally write the global name and end the global with a `;` and a newline
// Leading space is important
write!(self.out, " ")?;
Expand Down Expand Up @@ -2878,7 +2869,6 @@ impl<'a, W: Write> Writer<'a, W> {
let info = self.info.get_entry_point(self.entry_point_idx as usize);
let mut texture_mapping = crate::FastHashMap::default();
let mut uniforms = crate::FastHashMap::default();
let mut push_constant = None;

for sampling in info.sampling_set.iter() {
let tex_name = self.reflection_names_globals[&sampling.image].clone();
Expand Down Expand Up @@ -2909,10 +2899,6 @@ impl<'a, W: Write> Writer<'a, W> {
let name = self.reflection_names_globals[&handle].clone();
uniforms.insert(handle, name);
}
crate::StorageClass::PushConstant => {
let name = self.reflection_names_globals[&handle].clone();
push_constant = Some(name)
}
_ => (),
},
crate::TypeInner::Image { .. } => {
Expand All @@ -2936,7 +2922,6 @@ impl<'a, W: Write> Writer<'a, W> {
Ok(ReflectionInfo {
texture_mapping,
uniforms,
push_constant,
})
}
}
Expand Down
1 change: 0 additions & 1 deletion tests/in/functions-webgl.param.ron
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
version: Embedded(300),
writer_flags: (bits: 0),
binding_map: {},
push_constant_binding: 0,
),
)
1 change: 0 additions & 1 deletion tests/in/interpolate.param.ron
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@
version: Desktop(400),
writer_flags: (bits: 0),
binding_map: {},
push_constant_binding: 0,
),
)
1 change: 0 additions & 1 deletion tests/in/push-constants.param.ron
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
version: Embedded(320),
writer_flags: (bits: 0),
binding_map: {},
push_constant_binding: 4,
),
)
1 change: 0 additions & 1 deletion tests/in/quad.param.ron
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
version: Embedded(300),
writer_flags: (bits: 0),
binding_map: {},
push_constant_binding: 0,
),
)
1 change: 0 additions & 1 deletion tests/in/skybox.param.ron
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
(group: 0, binding: 0): 0,
(group: 0, binding: 1): 0,
},
push_constant_binding: 0,
),
hlsl: (
shader_model: V5_1,
Expand Down
2 changes: 1 addition & 1 deletion tests/out/glsl/push-constants.main.Fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct PushConstants {
struct FragmentIn {
vec4 color;
};
layout(std140, binding = 4) uniform PushConstants_block_0Fragment { PushConstants pc; };
uniform PushConstants pc;

layout(location = 0) smooth in vec4 _vs2fs_location0;
layout(location = 0) out vec4 _fs2p_location0;
Expand Down

0 comments on commit 81dc674

Please sign in to comment.