Merge pull request #729 from KhronosGroup/fix-728

HLSL: Use same logic as GLSL for picking cbuffer block name.
diff --git a/reference/opt/shaders-hlsl/asm/frag/cbuffer-stripped.asm.frag b/reference/opt/shaders-hlsl/asm/frag/cbuffer-stripped.asm.frag
index 25eee2d..b410010 100644
--- a/reference/opt/shaders-hlsl/asm/frag/cbuffer-stripped.asm.frag
+++ b/reference/opt/shaders-hlsl/asm/frag/cbuffer-stripped.asm.frag
@@ -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
diff --git a/reference/opt/shaders-hlsl/asm/frag/pass-by-value.asm.frag b/reference/opt/shaders-hlsl/asm/frag/pass-by-value.asm.frag
index 4fe2a19..56ac1f2 100644
--- a/reference/opt/shaders-hlsl/asm/frag/pass-by-value.asm.frag
+++ b/reference/opt/shaders-hlsl/asm/frag/pass-by-value.asm.frag
@@ -1,8 +1,9 @@
-cbuffer registers
+cbuffer Registers
 {
     float registers_foo : packoffset(c0);
 };
 
+
 static float FragColor;
 
 struct SPIRV_Cross_Output
diff --git a/reference/opt/shaders-hlsl/comp/num-workgroups-alone.comp b/reference/opt/shaders-hlsl/comp/num-workgroups-alone.comp
index 1e7dd54..dee39e3 100644
--- a/reference/opt/shaders-hlsl/comp/num-workgroups-alone.comp
+++ b/reference/opt/shaders-hlsl/comp/num-workgroups-alone.comp
@@ -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)]
diff --git a/reference/opt/shaders-hlsl/comp/num-workgroups-with-builtins.comp b/reference/opt/shaders-hlsl/comp/num-workgroups-with-builtins.comp
index f44754e..1c98e5e 100644
--- a/reference/opt/shaders-hlsl/comp/num-workgroups-with-builtins.comp
+++ b/reference/opt/shaders-hlsl/comp/num-workgroups-with-builtins.comp
@@ -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
 {
@@ -12,7 +13,7 @@
 
 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)]
diff --git a/reference/opt/shaders-hlsl/comp/rwbuffer-matrix.comp b/reference/opt/shaders-hlsl/comp/rwbuffer-matrix.comp
index c05f8ff..42103c2 100644
--- a/reference/opt/shaders-hlsl/comp/rwbuffer-matrix.comp
+++ b/reference/opt/shaders-hlsl/comp/rwbuffer-matrix.comp
@@ -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)));
diff --git a/reference/opt/shaders-hlsl/frag/constant-buffer-array.invalid.sm51.frag b/reference/opt/shaders-hlsl/frag/constant-buffer-array.invalid.sm51.frag
index 7e613da..d330706 100644
--- a/reference/opt/shaders-hlsl/frag/constant-buffer-array.invalid.sm51.frag
+++ b/reference/opt/shaders-hlsl/frag/constant-buffer-array.invalid.sm51.frag
@@ -7,7 +7,7 @@
 };
 
 ConstantBuffer<CBO_1> cbo[2][4] : register(b4, space0);
-cbuffer push
+cbuffer PushMe
 {
     float4 push_a : packoffset(c0);
     float4 push_b : packoffset(c1);
@@ -15,6 +15,7 @@
     float4 push_d : packoffset(c3);
 };
 
+
 static float4 FragColor;
 
 struct SPIRV_Cross_Output
diff --git a/reference/opt/shaders-hlsl/frag/resources.frag b/reference/opt/shaders-hlsl/frag/resources.frag
index 24b93c2..aac0d53 100644
--- a/reference/opt/shaders-hlsl/frag/resources.frag
+++ b/reference/opt/shaders-hlsl/frag/resources.frag
@@ -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);
diff --git a/reference/opt/shaders-hlsl/frag/row-major-layout-in-struct.frag b/reference/opt/shaders-hlsl/frag/row-major-layout-in-struct.frag
index 8576b0f..7df0fd9 100644
--- a/reference/opt/shaders-hlsl/frag/row-major-layout-in-struct.frag
+++ b/reference/opt/shaders-hlsl/frag/row-major-layout-in-struct.frag
@@ -4,11 +4,12 @@
     row_major float4x4 w;
 };
 
-cbuffer _17 : register(b0)
+cbuffer UBO : register(b0)
 {
     Foo _17_foo : packoffset(c0);
 };
 
+
 static float4 FragColor;
 static float4 vUV;
 
diff --git a/reference/opt/shaders-hlsl/frag/spec-constant-block-size.frag b/reference/opt/shaders-hlsl/frag/spec-constant-block-size.frag
index 094d509..2a3f08d 100644
--- a/reference/opt/shaders-hlsl/frag/spec-constant-block-size.frag
+++ b/reference/opt/shaders-hlsl/frag/spec-constant-block-size.frag
@@ -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;
 
diff --git a/reference/opt/shaders-hlsl/vert/basic.vert b/reference/opt/shaders-hlsl/vert/basic.vert
index 5f0a5f3..f357d90 100644
--- a/reference/opt/shaders-hlsl/vert/basic.vert
+++ b/reference/opt/shaders-hlsl/vert/basic.vert
@@ -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;
diff --git a/reference/opt/shaders-hlsl/vert/read-from-row-major-array.vert b/reference/opt/shaders-hlsl/vert/read-from-row-major-array.vert
index dde648e..03fa4f3 100644
--- a/reference/opt/shaders-hlsl/vert/read-from-row-major-array.vert
+++ b/reference/opt/shaders-hlsl/vert/read-from-row-major-array.vert
@@ -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;
diff --git a/reference/shaders-hlsl/asm/frag/cbuffer-stripped.asm.frag b/reference/shaders-hlsl/asm/frag/cbuffer-stripped.asm.frag
index 851dfa1..3d5d628 100644
--- a/reference/shaders-hlsl/asm/frag/cbuffer-stripped.asm.frag
+++ b/reference/shaders-hlsl/asm/frag/cbuffer-stripped.asm.frag
@@ -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
diff --git a/reference/shaders-hlsl/asm/frag/pass-by-value.asm.frag b/reference/shaders-hlsl/asm/frag/pass-by-value.asm.frag
index 4bc02d5..ab0471a 100644
--- a/reference/shaders-hlsl/asm/frag/pass-by-value.asm.frag
+++ b/reference/shaders-hlsl/asm/frag/pass-by-value.asm.frag
@@ -1,8 +1,9 @@
-cbuffer registers
+cbuffer Registers
 {
     float registers_foo : packoffset(c0);
 };
 
+
 static float FragColor;
 
 struct SPIRV_Cross_Output
diff --git a/reference/shaders-hlsl/comp/num-workgroups-alone.comp b/reference/shaders-hlsl/comp/num-workgroups-alone.comp
index 1e7dd54..dee39e3 100644
--- a/reference/shaders-hlsl/comp/num-workgroups-alone.comp
+++ b/reference/shaders-hlsl/comp/num-workgroups-alone.comp
@@ -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)]
diff --git a/reference/shaders-hlsl/comp/num-workgroups-with-builtins.comp b/reference/shaders-hlsl/comp/num-workgroups-with-builtins.comp
index f44754e..1c98e5e 100644
--- a/reference/shaders-hlsl/comp/num-workgroups-with-builtins.comp
+++ b/reference/shaders-hlsl/comp/num-workgroups-with-builtins.comp
@@ -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
 {
@@ -12,7 +13,7 @@
 
 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)]
diff --git a/reference/shaders-hlsl/comp/rwbuffer-matrix.comp b/reference/shaders-hlsl/comp/rwbuffer-matrix.comp
index 0e352f1..e798292 100644
--- a/reference/shaders-hlsl/comp/rwbuffer-matrix.comp
+++ b/reference/shaders-hlsl/comp/rwbuffer-matrix.comp
@@ -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)));
diff --git a/reference/shaders-hlsl/frag/constant-buffer-array.invalid.sm51.frag b/reference/shaders-hlsl/frag/constant-buffer-array.invalid.sm51.frag
index 7e613da..d330706 100644
--- a/reference/shaders-hlsl/frag/constant-buffer-array.invalid.sm51.frag
+++ b/reference/shaders-hlsl/frag/constant-buffer-array.invalid.sm51.frag
@@ -7,7 +7,7 @@
 };
 
 ConstantBuffer<CBO_1> cbo[2][4] : register(b4, space0);
-cbuffer push
+cbuffer PushMe
 {
     float4 push_a : packoffset(c0);
     float4 push_b : packoffset(c1);
@@ -15,6 +15,7 @@
     float4 push_d : packoffset(c3);
 };
 
+
 static float4 FragColor;
 
 struct SPIRV_Cross_Output
diff --git a/reference/shaders-hlsl/frag/partial-write-preserve.frag b/reference/shaders-hlsl/frag/partial-write-preserve.frag
index 402c810..bb5b905 100644
--- a/reference/shaders-hlsl/frag/partial-write-preserve.frag
+++ b/reference/shaders-hlsl/frag/partial-write-preserve.frag
@@ -6,11 +6,12 @@
 
 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;
diff --git a/reference/shaders-hlsl/frag/resources.frag b/reference/shaders-hlsl/frag/resources.frag
index 60b709e..2a3b2be 100644
--- a/reference/shaders-hlsl/frag/resources.frag
+++ b/reference/shaders-hlsl/frag/resources.frag
@@ -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);
diff --git a/reference/shaders-hlsl/frag/row-major-layout-in-struct.frag b/reference/shaders-hlsl/frag/row-major-layout-in-struct.frag
index f97e217..5fc45b2 100644
--- a/reference/shaders-hlsl/frag/row-major-layout-in-struct.frag
+++ b/reference/shaders-hlsl/frag/row-major-layout-in-struct.frag
@@ -10,11 +10,12 @@
     row_major float4x4 w;
 };
 
-cbuffer _17 : register(b0)
+cbuffer UBO : register(b0)
 {
     Foo _17_foo : packoffset(c0);
 };
 
+
 static float4 FragColor;
 static float4 vUV;
 
diff --git a/reference/shaders-hlsl/frag/spec-constant-block-size.frag b/reference/shaders-hlsl/frag/spec-constant-block-size.frag
index 094d509..2a3f08d 100644
--- a/reference/shaders-hlsl/frag/spec-constant-block-size.frag
+++ b/reference/shaders-hlsl/frag/spec-constant-block-size.frag
@@ -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;
 
diff --git a/reference/shaders-hlsl/vert/basic.vert b/reference/shaders-hlsl/vert/basic.vert
index 5f0a5f3..f357d90 100644
--- a/reference/shaders-hlsl/vert/basic.vert
+++ b/reference/shaders-hlsl/vert/basic.vert
@@ -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;
diff --git a/reference/shaders-hlsl/vert/read-from-row-major-array.vert b/reference/shaders-hlsl/vert/read-from-row-major-array.vert
index 79758a4..f656c0a 100644
--- a/reference/shaders-hlsl/vert/read-from-row-major-array.vert
+++ b/reference/shaders-hlsl/vert/read-from-row-major-array.vert
@@ -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;
diff --git a/spirv_cross.hpp b/spirv_cross.hpp
index 82ff041..0767956 100644
--- a/spirv_cross.hpp
+++ b/spirv_cross.hpp
@@ -512,6 +512,8 @@
 	// 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.
diff --git a/spirv_hlsl.cpp b/spirv_hlsl.cpp
index 7120824..8e4b440 100644
--- a/spirv_hlsl.cpp
+++ b/spirv_hlsl.cpp
@@ -1858,9 +1858,25 @@
 			// 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;
@@ -1876,6 +1892,7 @@
 			}
 
 			end_scope_decl();
+			statement("");
 		}
 		else
 		{