Skip to content

Commit

Permalink
Merge pull request KhronosGroup#729 from KhronosGroup/fix-728
Browse files Browse the repository at this point in the history
HLSL: Use same logic as GLSL for picking cbuffer block name.
  • Loading branch information
HansKristian-Work authored Oct 23, 2018
2 parents a8e5a0b + 226d837 commit bfeb388
Show file tree
Hide file tree
Showing 25 changed files with 74 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
cbuffer _5 : register(b0)
cbuffer _4_5 : register(b0)
{
column_major float2x4 _5_m0 : packoffset(c0);
float4 _5_m1 : packoffset(c4);
};


static float2 _3;

struct SPIRV_Cross_Output
Expand Down
3 changes: 2 additions & 1 deletion reference/opt/shaders-hlsl/asm/frag/pass-by-value.asm.frag
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
cbuffer registers
cbuffer Registers
{
float registers_foo : packoffset(c0);
};


static float FragColor;

struct SPIRV_Cross_Output
Expand Down
5 changes: 3 additions & 2 deletions reference/opt/shaders-hlsl/comp/num-workgroups-alone.comp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
RWByteAddressBuffer _10 : register(u0);
cbuffer SPIRV_Cross_NumWorkgroups : register(b0)
{
uint3 SPIRV_Cross_NumWorkgroups_count : packoffset(c0);
uint3 SPIRV_Cross_NumWorkgroups_1_count : packoffset(c0);
};


void comp_main()
{
_10.Store3(0, SPIRV_Cross_NumWorkgroups_count);
_10.Store3(0, SPIRV_Cross_NumWorkgroups_1_count);
}

[numthreads(1, 1, 1)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
RWByteAddressBuffer _10 : register(u0);
cbuffer SPIRV_Cross_NumWorkgroups : register(b0)
{
uint3 SPIRV_Cross_NumWorkgroups_count : packoffset(c0);
uint3 SPIRV_Cross_NumWorkgroups_1_count : packoffset(c0);
};


static uint3 gl_WorkGroupID;
struct SPIRV_Cross_Input
{
Expand All @@ -12,7 +13,7 @@ struct SPIRV_Cross_Input

void comp_main()
{
_10.Store3(0, SPIRV_Cross_NumWorkgroups_count + gl_WorkGroupID);
_10.Store3(0, SPIRV_Cross_NumWorkgroups_1_count + gl_WorkGroupID);
}

[numthreads(1, 1, 1)]
Expand Down
3 changes: 2 additions & 1 deletion reference/opt/shaders-hlsl/comp/rwbuffer-matrix.comp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
RWByteAddressBuffer _28 : register(u0);
cbuffer _68 : register(b1)
cbuffer UBO : register(b1)
{
int _68_index0 : packoffset(c0);
int _68_index1 : packoffset(c0.y);
};


void comp_main()
{
float4x4 _253 = asfloat(uint4x4(_28.Load(64), _28.Load(80), _28.Load(96), _28.Load(112), _28.Load(68), _28.Load(84), _28.Load(100), _28.Load(116), _28.Load(72), _28.Load(88), _28.Load(104), _28.Load(120), _28.Load(76), _28.Load(92), _28.Load(108), _28.Load(124)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ struct CBO_1
};

ConstantBuffer<CBO_1> cbo[2][4] : register(b4, space0);
cbuffer push
cbuffer PushMe
{
float4 push_a : packoffset(c0);
float4 push_b : packoffset(c1);
float4 push_c : packoffset(c2);
float4 push_d : packoffset(c3);
};


static float4 FragColor;

struct SPIRV_Cross_Output
Expand Down
6 changes: 4 additions & 2 deletions reference/opt/shaders-hlsl/frag/resources.frag
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
cbuffer cbuf : register(b3)
cbuffer CBuffer : register(b3)
{
float4 cbuf_a : packoffset(c0);
};
cbuffer registers

cbuffer PushMe
{
float4 registers_d : packoffset(c0);
};

Texture2D<float4> uSampledImage : register(t4);
SamplerState _uSampledImage_sampler : register(s4);
Texture2D<float4> uTexture : register(t5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ struct Foo
row_major float4x4 w;
};

cbuffer _17 : register(b0)
cbuffer UBO : register(b0)
{
Foo _17_foo : packoffset(c0);
};


static float4 FragColor;
static float4 vUV;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
static const int Value = 2;

cbuffer _15 : register(b0)
cbuffer SpecConstArray : register(b0)
{
float4 _15_samples[Value] : packoffset(c0);
};


static float4 FragColor;
static int Index;

Expand Down
3 changes: 2 additions & 1 deletion reference/opt/shaders-hlsl/vert/basic.vert
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
cbuffer _16
cbuffer UBO
{
row_major float4x4 _16_uMVP : packoffset(c0);
};


static float4 gl_Position;
static float4 aVertex;
static float3 vNormal;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
cbuffer _104 : register(b0)
cbuffer Block : register(b0)
{
column_major float2x3 _104_var[3][4] : packoffset(c0);
};


static float4 gl_Position;
static float4 a_position;
static float v_vtxResult;
Expand Down
3 changes: 2 additions & 1 deletion reference/shaders-hlsl/asm/frag/cbuffer-stripped.asm.frag
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
cbuffer _5 : register(b0)
cbuffer _4_5 : register(b0)
{
column_major float2x4 _5_m0 : packoffset(c0);
float4 _5_m1 : packoffset(c4);
};


static float2 _3;

struct SPIRV_Cross_Output
Expand Down
3 changes: 2 additions & 1 deletion reference/shaders-hlsl/asm/frag/pass-by-value.asm.frag
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
cbuffer registers
cbuffer Registers
{
float registers_foo : packoffset(c0);
};


static float FragColor;

struct SPIRV_Cross_Output
Expand Down
5 changes: 3 additions & 2 deletions reference/shaders-hlsl/comp/num-workgroups-alone.comp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
RWByteAddressBuffer _10 : register(u0);
cbuffer SPIRV_Cross_NumWorkgroups : register(b0)
{
uint3 SPIRV_Cross_NumWorkgroups_count : packoffset(c0);
uint3 SPIRV_Cross_NumWorkgroups_1_count : packoffset(c0);
};


void comp_main()
{
_10.Store3(0, SPIRV_Cross_NumWorkgroups_count);
_10.Store3(0, SPIRV_Cross_NumWorkgroups_1_count);
}

[numthreads(1, 1, 1)]
Expand Down
5 changes: 3 additions & 2 deletions reference/shaders-hlsl/comp/num-workgroups-with-builtins.comp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
RWByteAddressBuffer _10 : register(u0);
cbuffer SPIRV_Cross_NumWorkgroups : register(b0)
{
uint3 SPIRV_Cross_NumWorkgroups_count : packoffset(c0);
uint3 SPIRV_Cross_NumWorkgroups_1_count : packoffset(c0);
};


static uint3 gl_WorkGroupID;
struct SPIRV_Cross_Input
{
Expand All @@ -12,7 +13,7 @@ struct SPIRV_Cross_Input

void comp_main()
{
_10.Store3(0, SPIRV_Cross_NumWorkgroups_count + gl_WorkGroupID);
_10.Store3(0, SPIRV_Cross_NumWorkgroups_1_count + gl_WorkGroupID);
}

[numthreads(1, 1, 1)]
Expand Down
3 changes: 2 additions & 1 deletion reference/shaders-hlsl/comp/rwbuffer-matrix.comp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
RWByteAddressBuffer _28 : register(u0);
cbuffer _68 : register(b1)
cbuffer UBO : register(b1)
{
int _68_index0 : packoffset(c0);
int _68_index1 : packoffset(c0.y);
};


void row_to_col()
{
float4x4 _55 = asfloat(uint4x4(_28.Load(64), _28.Load(80), _28.Load(96), _28.Load(112), _28.Load(68), _28.Load(84), _28.Load(100), _28.Load(116), _28.Load(72), _28.Load(88), _28.Load(104), _28.Load(120), _28.Load(76), _28.Load(92), _28.Load(108), _28.Load(124)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ struct CBO_1
};

ConstantBuffer<CBO_1> cbo[2][4] : register(b4, space0);
cbuffer push
cbuffer PushMe
{
float4 push_a : packoffset(c0);
float4 push_b : packoffset(c1);
float4 push_c : packoffset(c2);
float4 push_d : packoffset(c3);
};


static float4 FragColor;

struct SPIRV_Cross_Output
Expand Down
3 changes: 2 additions & 1 deletion reference/shaders-hlsl/frag/partial-write-preserve.frag
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ struct B

static const B _80 = { 10.0f, 20.0f };

cbuffer _42 : register(b0)
cbuffer UBO : register(b0)
{
int _42_some_value : packoffset(c0);
};


void partial_inout(inout float4 x)
{
x.x = 10.0f;
Expand Down
6 changes: 4 additions & 2 deletions reference/shaders-hlsl/frag/resources.frag
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
cbuffer cbuf : register(b3)
cbuffer CBuffer : register(b3)
{
float4 cbuf_a : packoffset(c0);
};
cbuffer registers

cbuffer PushMe
{
float4 registers_d : packoffset(c0);
};

Texture2D<float4> uSampledImage : register(t4);
SamplerState _uSampledImage_sampler : register(s4);
Texture2D<float4> uTexture : register(t5);
Expand Down
3 changes: 2 additions & 1 deletion reference/shaders-hlsl/frag/row-major-layout-in-struct.frag
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ struct Foo
row_major float4x4 w;
};

cbuffer _17 : register(b0)
cbuffer UBO : register(b0)
{
Foo _17_foo : packoffset(c0);
};


static float4 FragColor;
static float4 vUV;

Expand Down
3 changes: 2 additions & 1 deletion reference/shaders-hlsl/frag/spec-constant-block-size.frag
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
static const int Value = 2;

cbuffer _15 : register(b0)
cbuffer SpecConstArray : register(b0)
{
float4 _15_samples[Value] : packoffset(c0);
};


static float4 FragColor;
static int Index;

Expand Down
3 changes: 2 additions & 1 deletion reference/shaders-hlsl/vert/basic.vert
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
cbuffer _16
cbuffer UBO
{
row_major float4x4 _16_uMVP : packoffset(c0);
};


static float4 gl_Position;
static float4 aVertex;
static float3 vNormal;
Expand Down
3 changes: 2 additions & 1 deletion reference/shaders-hlsl/vert/read-from-row-major-array.vert
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
cbuffer _104 : register(b0)
cbuffer Block : register(b0)
{
column_major float2x3 _104_var[3][4] : packoffset(c0);
};


static float4 gl_Position;
static float4 a_position;
static float v_vtxResult;
Expand Down
2 changes: 2 additions & 0 deletions spirv_cross.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,8 @@ class Compiler
// For other names like remapped names for variables, etc, it's generally enough to query the name of the variables
// after compiling, block names are an exception to this rule.
// ID is the name of a variable as returned by Resource::id, and must be a variable with a Block-like type.
//
// This also applies to HLSL cbuffers.
std::string get_remapped_declared_block_name(uint32_t id) const;

// For buffer block variables, get the decorations for that variable.
Expand Down
19 changes: 18 additions & 1 deletion spirv_hlsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1858,9 +1858,25 @@ void CompilerHLSL::emit_buffer_block(const SPIRVariable &var)
// this restriction is similar to GLSL where layout(offset) is not possible on sub-structs.
flattened_structs.insert(var.self);

// Prefer the block name if possible.
auto buffer_name = to_name(type.self, false);
if (ir.meta[type.self].decoration.alias.empty() || resource_names.find(buffer_name) != end(resource_names))
buffer_name = get_block_fallback_name(var.self);
add_variable(resource_names, buffer_name);

// If for some reason buffer_name is an illegal name, make a final fallback to a workaround name.
// This cannot conflict with anything else, so we're safe now.
if (buffer_name.empty())
buffer_name = join("_", get<SPIRType>(var.basetype).self, "_", var.self);

resource_names.insert(buffer_name);

// Save for post-reflection later.
declared_block_names[var.self] = buffer_name;

type.member_name_cache.clear();
add_resource_name(var.self);
statement("cbuffer ", to_name(var.self), to_resource_binding(var));
statement("cbuffer ", buffer_name, to_resource_binding(var));
begin_scope();

uint32_t i = 0;
Expand All @@ -1876,6 +1892,7 @@ void CompilerHLSL::emit_buffer_block(const SPIRVariable &var)
}

end_scope_decl();
statement("");
}
else
{
Expand Down

0 comments on commit bfeb388

Please sign in to comment.