Skip to content

Commit

Permalink
hlsl-out: implement constant buffer support
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Jul 18, 2021
1 parent 16206f2 commit 39eaa57
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
27 changes: 18 additions & 9 deletions src/back/hlsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ impl<'a, W: Write> Writer<'a, W> {
crate::StorageClass::Function => unreachable!("Function storage class"),
crate::StorageClass::Private => ("static ", ""),
crate::StorageClass::WorkGroup => ("groupshared ", ""),
crate::StorageClass::Uniform => ("", "b"),
crate::StorageClass::Uniform => ("cbuffer", "b"),
crate::StorageClass::Storage | crate::StorageClass::Handle => {
if let TypeInner::Sampler { .. } = *inner {
("", "s")
Expand All @@ -358,12 +358,13 @@ impl<'a, W: Write> Writer<'a, W> {
};

write!(self.out, "{}", storage)?;
self.write_type(module, global.ty)?;
write!(
self.out,
" {}",
&self.names[&NameKey::GlobalVariable(handle)]
)?;
// constant buffer declarations are expected to be inlined, e.g.
// cbuffer foo: register(b0) { field1: type1; };
if global.class != crate::StorageClass::Uniform {
self.write_type(module, global.ty)?;
}
let name = &self.names[&NameKey::GlobalVariable(handle)];
write!(self.out, " {}", name)?;
if let TypeInner::Array { size, .. } = module.types[global.ty].inner {
self.write_array_size(module, size)?;
}
Expand All @@ -375,14 +376,22 @@ impl<'a, W: Write> Writer<'a, W> {
if self.options.shader_model > super::ShaderModel::V5_0 {
write!(self.out, ", space{}", bt.space)?;
}
writeln!(self.out, ");")?;
} else {
write!(self.out, ")")?;
} else if global.class == crate::StorageClass::Private {
write!(self.out, " = ")?;
if let Some(init) = global.init {
self.write_constant(module, init)?;
} else {
self.write_default_init(module, global.ty)?;
}
}

if global.class == crate::StorageClass::Uniform {
write!(self.out, " {{ ")?;
self.write_type(module, global.ty)?;
let name = &self.names[&NameKey::GlobalVariable(handle)];
writeln!(self.out, " {}; }}", name)?;
} else {
writeln!(self.out, ";")?;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/out/hlsl/globals.hlsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
static const bool Foo = true;

groupshared float wg[10] = (float[10])0;
groupshared float wg[10];

[numthreads(1, 1, 1)]
void main()
Expand Down
2 changes: 1 addition & 1 deletion tests/out/hlsl/shadow.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct Lights {
Light data[1];
};

Globals u_globals : register(b0);
cbuffer u_globals : register(b0) { Globals u_globals; }
Lights s_lights : register(t1);
Texture2DArray t_shadow : register(t2);
SamplerComparisonState sampler_shadow : register(s3);
Expand Down
2 changes: 1 addition & 1 deletion tests/out/hlsl/skybox.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct Data {
float4x4 view;
};

Data r_data : register(b0, space0);
cbuffer r_data : register(b0, space0) { Data r_data; }
TextureCube<float4> r_texture : register(t0, space0);
SamplerState r_sampler : register(s0, space1);

Expand Down

0 comments on commit 39eaa57

Please sign in to comment.