diff --git a/README.md b/README.md index 969c7d9f..058db89f 100644 --- a/README.md +++ b/README.md @@ -402,6 +402,28 @@ Y-flipping of gl_Position and similar is also supported. The use of this is discouraged, because relying on vertex shader Y-flipping tends to get quite messy. To enable this, set `CompilerGLSL::Options.vertex.flip_vert_y` or `--flip-vert-y` in CLI. +#### Reserved identifiers + +When cross-compiling, certain identifiers are considered to be reserved by the implementation. +Code generated by SPIRV-Cross cannot emit these identifiers as they are reserved and used for various internal purposes, +and such variables will typically show up as `_RESERVED_IDENTIFIER_FIXUP_` +or some similar name to make it more obvious that an identifier has been renamed. + +Reflection output will follow the exact name specified in the SPIR-V module. It might not be a valid identifier in the C sense, +as it may contain non-alphanumeric/non-underscore characters. + +Reserved identifiers currently assumed by the implementation are (in pseudo-regex): + +- _$digit+, e.g. `_100`, `_2` +- _$digit+_.+, e.g. `_100_tmp`, `_2_foobar`. `_2Bar` is **not** reserved. +- gl_- prefix +- spv- prefix +- SPIRV_Cross prefix +- Double underscores (reserved by all target languages). + +Members of structs also have a reserved identifier: +- _m$digit+$END, e.g. `_m20` and `_m40` are reserved, but not `_m40Foobar`. + ## Contributing Contributions to SPIRV-Cross are welcome. See Testing and Licensing sections for details. diff --git a/reference/opt/shaders-msl/asm/comp/vector-builtin-type-cast-func.asm.comp b/reference/opt/shaders-msl/asm/comp/vector-builtin-type-cast-func.asm.comp index 36600038..afbcadd0 100644 --- a/reference/opt/shaders-msl/asm/comp/vector-builtin-type-cast-func.asm.comp +++ b/reference/opt/shaders-msl/asm/comp/vector-builtin-type-cast-func.asm.comp @@ -5,7 +5,7 @@ using namespace metal; struct cb1_struct { - float4 _m0[1]; + float4 _RESERVED_IDENTIFIER_FIXUP_m0[1]; }; constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(16u, 16u, 1u); @@ -19,7 +19,7 @@ kernel void main0(constant cb1_struct& cb0_1 [[buffer(0)]], texture2d= spvStageInputSize)) return; uint gl_VertexIndex = gl_GlobalInvocationID.x + spvDispatchBase.x; - _12._m0[int(gl_VertexIndex)] = in.m_19; + _RESERVED_IDENTIFIER_FIXUP_12._RESERVED_IDENTIFIER_FIXUP_m0[int(gl_VertexIndex)] = in._RESERVED_IDENTIFIER_FIXUP_19; } diff --git a/reference/opt/shaders-msl/vert/no_stage_out.vert b/reference/opt/shaders-msl/vert/no_stage_out.vert index 30613cc4..e804da67 100644 --- a/reference/opt/shaders-msl/vert/no_stage_out.vert +++ b/reference/opt/shaders-msl/vert/no_stage_out.vert @@ -3,18 +3,18 @@ using namespace metal; -struct _10 +struct _RESERVED_IDENTIFIER_FIXUP_10_12 { - uint4 _m0[1024]; + uint4 _RESERVED_IDENTIFIER_FIXUP_m0[1024]; }; struct main0_in { - uint4 m_19 [[attribute(0)]]; + uint4 _RESERVED_IDENTIFIER_FIXUP_19 [[attribute(0)]]; }; -vertex void main0(main0_in in [[stage_in]], device _10& _12 [[buffer(0)]], uint gl_VertexIndex [[vertex_id]]) +vertex void main0(main0_in in [[stage_in]], device _RESERVED_IDENTIFIER_FIXUP_10_12& _RESERVED_IDENTIFIER_FIXUP_12 [[buffer(0)]], uint gl_VertexIndex [[vertex_id]]) { - _12._m0[int(gl_VertexIndex)] = in.m_19; + _RESERVED_IDENTIFIER_FIXUP_12._RESERVED_IDENTIFIER_FIXUP_m0[int(gl_VertexIndex)] = in._RESERVED_IDENTIFIER_FIXUP_19; } diff --git a/reference/opt/shaders-msl/vert/no_stage_out.write_buff.vert b/reference/opt/shaders-msl/vert/no_stage_out.write_buff.vert index d5d31f44..296293aa 100644 --- a/reference/opt/shaders-msl/vert/no_stage_out.write_buff.vert +++ b/reference/opt/shaders-msl/vert/no_stage_out.write_buff.vert @@ -3,14 +3,14 @@ using namespace metal; -struct _35 +struct _RESERVED_IDENTIFIER_FIXUP_33_35 { - uint4 _m0[1024]; + uint4 _RESERVED_IDENTIFIER_FIXUP_m0[1024]; }; -struct _40 +struct _RESERVED_IDENTIFIER_FIXUP_38_40 { - uint4 _m0[1024]; + uint4 _RESERVED_IDENTIFIER_FIXUP_m0[1024]; }; struct main0_out @@ -20,16 +20,16 @@ struct main0_out struct main0_in { - float4 m_17 [[attribute(0)]]; + float4 _RESERVED_IDENTIFIER_FIXUP_14 [[attribute(0)]]; }; -vertex void main0(main0_in in [[stage_in]], device _35& _37 [[buffer(0)]], constant _40& _42 [[buffer(1)]]) +vertex void main0(main0_in in [[stage_in]], device _RESERVED_IDENTIFIER_FIXUP_33_35& _RESERVED_IDENTIFIER_FIXUP_35 [[buffer(0)]], constant _RESERVED_IDENTIFIER_FIXUP_38_40& _RESERVED_IDENTIFIER_FIXUP_40 [[buffer(1)]]) { main0_out out = {}; - out.gl_Position = in.m_17; + out.gl_Position = in._RESERVED_IDENTIFIER_FIXUP_14; for (int _52 = 0; _52 < 1024; ) { - _37._m0[_52] = _42._m0[_52]; + _RESERVED_IDENTIFIER_FIXUP_35._RESERVED_IDENTIFIER_FIXUP_m0[_52] = _RESERVED_IDENTIFIER_FIXUP_40._RESERVED_IDENTIFIER_FIXUP_m0[_52]; _52++; continue; } diff --git a/reference/opt/shaders-msl/vert/no_stage_out.write_buff_atomic.vert b/reference/opt/shaders-msl/vert/no_stage_out.write_buff_atomic.vert index e2f38878..92fbf555 100644 --- a/reference/opt/shaders-msl/vert/no_stage_out.write_buff_atomic.vert +++ b/reference/opt/shaders-msl/vert/no_stage_out.write_buff_atomic.vert @@ -6,9 +6,9 @@ using namespace metal; -struct _23 +struct _RESERVED_IDENTIFIER_FIXUP_19_21 { - uint _m0; + uint _RESERVED_IDENTIFIER_FIXUP_m0; }; struct main0_out @@ -18,13 +18,13 @@ struct main0_out struct main0_in { - float4 m_17 [[attribute(0)]]; + float4 _RESERVED_IDENTIFIER_FIXUP_14 [[attribute(0)]]; }; -vertex void main0(main0_in in [[stage_in]], volatile device _23& _25 [[buffer(0)]]) +vertex void main0(main0_in in [[stage_in]], volatile device _RESERVED_IDENTIFIER_FIXUP_19_21& _RESERVED_IDENTIFIER_FIXUP_21 [[buffer(0)]]) { main0_out out = {}; - out.gl_Position = in.m_17; - uint _29 = atomic_fetch_add_explicit((volatile device atomic_uint*)&_25._m0, 1u, memory_order_relaxed); + out.gl_Position = in._RESERVED_IDENTIFIER_FIXUP_14; + uint _29 = atomic_fetch_add_explicit((volatile device atomic_uint*)&_RESERVED_IDENTIFIER_FIXUP_21._RESERVED_IDENTIFIER_FIXUP_m0, 1u, memory_order_relaxed); } diff --git a/reference/opt/shaders-msl/vert/no_stage_out.write_tex.vert b/reference/opt/shaders-msl/vert/no_stage_out.write_tex.vert index ddad934e..9d87efe1 100644 --- a/reference/opt/shaders-msl/vert/no_stage_out.write_tex.vert +++ b/reference/opt/shaders-msl/vert/no_stage_out.write_tex.vert @@ -10,16 +10,16 @@ struct main0_out struct main0_in { - float4 m_17 [[attribute(0)]]; + float4 _RESERVED_IDENTIFIER_FIXUP_14 [[attribute(0)]]; }; -vertex void main0(main0_in in [[stage_in]], texture1d _34 [[texture(0)]], texture1d _37 [[texture(1)]]) +vertex void main0(main0_in in [[stage_in]], texture1d _RESERVED_IDENTIFIER_FIXUP_32 [[texture(0)]], texture1d _RESERVED_IDENTIFIER_FIXUP_35 [[texture(1)]]) { main0_out out = {}; - out.gl_Position = in.m_17; + out.gl_Position = in._RESERVED_IDENTIFIER_FIXUP_14; for (int _45 = 0; _45 < 128; ) { - _34.write(_37.read(uint(_45)), uint(_45)); + _RESERVED_IDENTIFIER_FIXUP_32.write(_RESERVED_IDENTIFIER_FIXUP_35.read(uint(_45)), uint(_45)); _45++; continue; } diff --git a/reference/opt/shaders-msl/vert/packed_matrix.vert b/reference/opt/shaders-msl/vert/packed_matrix.vert index 8ca4d6ca..74b2c5fc 100644 --- a/reference/opt/shaders-msl/vert/packed_matrix.vert +++ b/reference/opt/shaders-msl/vert/packed_matrix.vert @@ -3,43 +3,43 @@ using namespace metal; -struct _15 +struct _RESERVED_IDENTIFIER_FIXUP_1365_18812 { - float3x4 _m0; - float3x4 _m1; + float3x4 _RESERVED_IDENTIFIER_FIXUP_m0; + float3x4 _RESERVED_IDENTIFIER_FIXUP_m1; }; -struct _42 +struct _RESERVED_IDENTIFIER_FIXUP_1126_22044 { - float4x4 _m0; - float4x4 _m1; - float _m2; + float4x4 _RESERVED_IDENTIFIER_FIXUP_m0; + float4x4 _RESERVED_IDENTIFIER_FIXUP_m1; + float _RESERVED_IDENTIFIER_FIXUP_m9; char _m3_pad[12]; - packed_float3 _m3; - float _m4; - packed_float3 _m5; - float _m6; - float _m7; - float _m8; - float2 _m9; + packed_float3 _RESERVED_IDENTIFIER_FIXUP_m10; + float _RESERVED_IDENTIFIER_FIXUP_m11; + packed_float3 _RESERVED_IDENTIFIER_FIXUP_m12; + float _RESERVED_IDENTIFIER_FIXUP_m17; + float _RESERVED_IDENTIFIER_FIXUP_m18; + float _RESERVED_IDENTIFIER_FIXUP_m19; + float2 _RESERVED_IDENTIFIER_FIXUP_m20; }; struct main0_out { - float3 m_72 [[user(locn0)]]; + float3 _RESERVED_IDENTIFIER_FIXUP_3976 [[user(locn0)]]; float4 gl_Position [[position]]; }; struct main0_in { - float4 m_25 [[attribute(0)]]; + float4 _RESERVED_IDENTIFIER_FIXUP_5275 [[attribute(0)]]; }; -vertex main0_out main0(main0_in in [[stage_in]], constant _15& _17 [[buffer(0)]], constant _42& _44 [[buffer(1)]]) +vertex main0_out main0(main0_in in [[stage_in]], constant _RESERVED_IDENTIFIER_FIXUP_1365_18812& _RESERVED_IDENTIFIER_FIXUP_18812 [[buffer(0)]], constant _RESERVED_IDENTIFIER_FIXUP_1126_22044& _RESERVED_IDENTIFIER_FIXUP_22044 [[buffer(1)]]) { main0_out out = {}; - float4 _70 = _44._m0 * float4(float3(_44._m3) + (in.m_25.xyz * (_44._m6 + _44._m7)), 1.0); - out.m_72 = normalize(float4(in.m_25.xyz, 0.0) * _17._m1); + float4 _70 = _RESERVED_IDENTIFIER_FIXUP_22044._RESERVED_IDENTIFIER_FIXUP_m0 * float4(float3(_RESERVED_IDENTIFIER_FIXUP_22044._RESERVED_IDENTIFIER_FIXUP_m10) + (in._RESERVED_IDENTIFIER_FIXUP_5275.xyz * (_RESERVED_IDENTIFIER_FIXUP_22044._RESERVED_IDENTIFIER_FIXUP_m17 + _RESERVED_IDENTIFIER_FIXUP_22044._RESERVED_IDENTIFIER_FIXUP_m18)), 1.0); + out._RESERVED_IDENTIFIER_FIXUP_3976 = normalize(float4(in._RESERVED_IDENTIFIER_FIXUP_5275.xyz, 0.0) * _RESERVED_IDENTIFIER_FIXUP_18812._RESERVED_IDENTIFIER_FIXUP_m1); float4 _94 = _70; _94.y = -_70.y; out.gl_Position = _94; diff --git a/reference/opt/shaders-ue4/asm/frag/subpass-input.ios.framebuffer-fetch.asm.frag b/reference/opt/shaders-ue4/asm/frag/subpass-input.ios.framebuffer-fetch.asm.frag index 3dfcf8af..817b1cff 100644 --- a/reference/opt/shaders-ue4/asm/frag/subpass-input.ios.framebuffer-fetch.asm.frag +++ b/reference/opt/shaders-ue4/asm/frag/subpass-input.ios.framebuffer-fetch.asm.frag @@ -197,10 +197,10 @@ struct main0_out float4 out_var_SV_Target0 [[color(0)]]; }; -fragment main0_out main0(constant type_View& View [[buffer(0)]], constant type_Globals& _Globals [[buffer(1)]], float4 _gl_LastFragData [[color(0)]], texture2d ShadowDepthTexture [[texture(0)]], sampler ShadowDepthTextureSampler [[sampler(0)]], float4 gl_FragCoord [[position]]) +fragment main0_out main0(constant type_View& View [[buffer(0)]], constant type_Globals& _Globals [[buffer(1)]], float4 _RESERVED_IDENTIFIER_FIXUP_gl_LastFragData [[color(0)]], texture2d ShadowDepthTexture [[texture(0)]], sampler ShadowDepthTextureSampler [[sampler(0)]], float4 gl_FragCoord [[position]]) { main0_out out = {}; - float4 _67 = _gl_LastFragData; + float4 _67 = _RESERVED_IDENTIFIER_FIXUP_gl_LastFragData; float _68 = _67.w; float4 _82 = _Globals.ScreenToShadowMatrix * float4((((gl_FragCoord.xy * View.View_BufferSizeAndInvSize.zw) - View.View_ScreenPositionScaleBias.wz) / View.View_ScreenPositionScaleBias.xy) * float2(_68), _68, 1.0); float _118 = fast::clamp(((fast::clamp((ShadowDepthTexture.sample(ShadowDepthTextureSampler, (((_82.xyz / float3(_82.w)).xy * _Globals.ShadowTileOffsetAndSize.zw).xy + _Globals.ShadowTileOffsetAndSize.xy).xy, level(0.0)).xxx * float3(_Globals.SoftTransitionScale.z)) - float3((fast::min(_82.z, 0.999989986419677734375) * _Globals.SoftTransitionScale.z) - 1.0), float3(0.0), float3(1.0)).x - 0.5) * _Globals.ShadowSharpen) + 0.5, 0.0, 1.0); diff --git a/reference/shaders-msl/asm/comp/vector-builtin-type-cast-func.asm.comp b/reference/shaders-msl/asm/comp/vector-builtin-type-cast-func.asm.comp index 93e14e86..9fb68a63 100644 --- a/reference/shaders-msl/asm/comp/vector-builtin-type-cast-func.asm.comp +++ b/reference/shaders-msl/asm/comp/vector-builtin-type-cast-func.asm.comp @@ -7,7 +7,7 @@ using namespace metal; struct cb1_struct { - float4 _m0[1]; + float4 _RESERVED_IDENTIFIER_FIXUP_m0[1]; }; constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(16u, 16u, 1u); @@ -27,7 +27,7 @@ kernel void main0(constant cb1_struct& cb0_1 [[buffer(0)]], texture2d= spvStageInputSize)) return; uint gl_VertexIndex = gl_GlobalInvocationID.x + spvDispatchBase.x; - _12._m0[int(gl_VertexIndex)] = in.m_19; + _RESERVED_IDENTIFIER_FIXUP_12._RESERVED_IDENTIFIER_FIXUP_m0[int(gl_VertexIndex)] = in._RESERVED_IDENTIFIER_FIXUP_19; } diff --git a/reference/shaders-msl/vert/no_stage_out.vert b/reference/shaders-msl/vert/no_stage_out.vert index 30613cc4..e804da67 100644 --- a/reference/shaders-msl/vert/no_stage_out.vert +++ b/reference/shaders-msl/vert/no_stage_out.vert @@ -3,18 +3,18 @@ using namespace metal; -struct _10 +struct _RESERVED_IDENTIFIER_FIXUP_10_12 { - uint4 _m0[1024]; + uint4 _RESERVED_IDENTIFIER_FIXUP_m0[1024]; }; struct main0_in { - uint4 m_19 [[attribute(0)]]; + uint4 _RESERVED_IDENTIFIER_FIXUP_19 [[attribute(0)]]; }; -vertex void main0(main0_in in [[stage_in]], device _10& _12 [[buffer(0)]], uint gl_VertexIndex [[vertex_id]]) +vertex void main0(main0_in in [[stage_in]], device _RESERVED_IDENTIFIER_FIXUP_10_12& _RESERVED_IDENTIFIER_FIXUP_12 [[buffer(0)]], uint gl_VertexIndex [[vertex_id]]) { - _12._m0[int(gl_VertexIndex)] = in.m_19; + _RESERVED_IDENTIFIER_FIXUP_12._RESERVED_IDENTIFIER_FIXUP_m0[int(gl_VertexIndex)] = in._RESERVED_IDENTIFIER_FIXUP_19; } diff --git a/reference/shaders-msl/vert/no_stage_out.write_buff.vert b/reference/shaders-msl/vert/no_stage_out.write_buff.vert index 23fa0817..fb8060f0 100644 --- a/reference/shaders-msl/vert/no_stage_out.write_buff.vert +++ b/reference/shaders-msl/vert/no_stage_out.write_buff.vert @@ -3,14 +3,14 @@ using namespace metal; -struct _35 +struct _RESERVED_IDENTIFIER_FIXUP_33_35 { - uint4 _m0[1024]; + uint4 _RESERVED_IDENTIFIER_FIXUP_m0[1024]; }; -struct _40 +struct _RESERVED_IDENTIFIER_FIXUP_38_40 { - uint4 _m0[1024]; + uint4 _RESERVED_IDENTIFIER_FIXUP_m0[1024]; }; struct main0_out @@ -20,16 +20,16 @@ struct main0_out struct main0_in { - float4 m_17 [[attribute(0)]]; + float4 _RESERVED_IDENTIFIER_FIXUP_14 [[attribute(0)]]; }; -vertex void main0(main0_in in [[stage_in]], device _35& _37 [[buffer(0)]], constant _40& _42 [[buffer(1)]]) +vertex void main0(main0_in in [[stage_in]], device _RESERVED_IDENTIFIER_FIXUP_33_35& _RESERVED_IDENTIFIER_FIXUP_35 [[buffer(0)]], constant _RESERVED_IDENTIFIER_FIXUP_38_40& _RESERVED_IDENTIFIER_FIXUP_40 [[buffer(1)]]) { main0_out out = {}; - out.gl_Position = in.m_17; - for (int _22 = 0; _22 < 1024; _22++) + out.gl_Position = in._RESERVED_IDENTIFIER_FIXUP_14; + for (int _RESERVED_IDENTIFIER_FIXUP_19 = 0; _RESERVED_IDENTIFIER_FIXUP_19 < 1024; _RESERVED_IDENTIFIER_FIXUP_19++) { - _37._m0[_22] = _42._m0[_22]; + _RESERVED_IDENTIFIER_FIXUP_35._RESERVED_IDENTIFIER_FIXUP_m0[_RESERVED_IDENTIFIER_FIXUP_19] = _RESERVED_IDENTIFIER_FIXUP_40._RESERVED_IDENTIFIER_FIXUP_m0[_RESERVED_IDENTIFIER_FIXUP_19]; } } diff --git a/reference/shaders-msl/vert/no_stage_out.write_buff_atomic.vert b/reference/shaders-msl/vert/no_stage_out.write_buff_atomic.vert index cb4a2195..68c649ed 100644 --- a/reference/shaders-msl/vert/no_stage_out.write_buff_atomic.vert +++ b/reference/shaders-msl/vert/no_stage_out.write_buff_atomic.vert @@ -6,9 +6,9 @@ using namespace metal; -struct _23 +struct _RESERVED_IDENTIFIER_FIXUP_19_21 { - uint _m0; + uint _RESERVED_IDENTIFIER_FIXUP_m0; }; struct main0_out @@ -18,14 +18,14 @@ struct main0_out struct main0_in { - float4 m_17 [[attribute(0)]]; + float4 _RESERVED_IDENTIFIER_FIXUP_14 [[attribute(0)]]; }; -vertex void main0(main0_in in [[stage_in]], volatile device _23& _25 [[buffer(0)]]) +vertex void main0(main0_in in [[stage_in]], volatile device _RESERVED_IDENTIFIER_FIXUP_19_21& _RESERVED_IDENTIFIER_FIXUP_21 [[buffer(0)]]) { main0_out out = {}; - out.gl_Position = in.m_17; - uint _29 = atomic_fetch_add_explicit((volatile device atomic_uint*)&_25._m0, 1u, memory_order_relaxed); - uint _22 = _29; + out.gl_Position = in._RESERVED_IDENTIFIER_FIXUP_14; + uint _29 = atomic_fetch_add_explicit((volatile device atomic_uint*)&_RESERVED_IDENTIFIER_FIXUP_21._RESERVED_IDENTIFIER_FIXUP_m0, 1u, memory_order_relaxed); + uint _RESERVED_IDENTIFIER_FIXUP_26 = _29; } diff --git a/reference/shaders-msl/vert/no_stage_out.write_tex.vert b/reference/shaders-msl/vert/no_stage_out.write_tex.vert index 76eb6f24..dfe1c328 100644 --- a/reference/shaders-msl/vert/no_stage_out.write_tex.vert +++ b/reference/shaders-msl/vert/no_stage_out.write_tex.vert @@ -10,16 +10,16 @@ struct main0_out struct main0_in { - float4 m_17 [[attribute(0)]]; + float4 _RESERVED_IDENTIFIER_FIXUP_14 [[attribute(0)]]; }; -vertex void main0(main0_in in [[stage_in]], texture1d _34 [[texture(0)]], texture1d _37 [[texture(1)]]) +vertex void main0(main0_in in [[stage_in]], texture1d _RESERVED_IDENTIFIER_FIXUP_32 [[texture(0)]], texture1d _RESERVED_IDENTIFIER_FIXUP_35 [[texture(1)]]) { main0_out out = {}; - out.gl_Position = in.m_17; - for (int _22 = 0; _22 < 128; _22++) + out.gl_Position = in._RESERVED_IDENTIFIER_FIXUP_14; + for (int _RESERVED_IDENTIFIER_FIXUP_19 = 0; _RESERVED_IDENTIFIER_FIXUP_19 < 128; _RESERVED_IDENTIFIER_FIXUP_19++) { - _34.write(_37.read(uint(_22)), uint(_22)); + _RESERVED_IDENTIFIER_FIXUP_32.write(_RESERVED_IDENTIFIER_FIXUP_35.read(uint(_RESERVED_IDENTIFIER_FIXUP_19)), uint(_RESERVED_IDENTIFIER_FIXUP_19)); } } diff --git a/reference/shaders-msl/vert/packed_matrix.vert b/reference/shaders-msl/vert/packed_matrix.vert index 9d901d5b..9cc416a3 100644 --- a/reference/shaders-msl/vert/packed_matrix.vert +++ b/reference/shaders-msl/vert/packed_matrix.vert @@ -3,53 +3,53 @@ using namespace metal; -struct _15 +struct _RESERVED_IDENTIFIER_FIXUP_1365_18812 { - float3x4 _m0; - float3x4 _m1; + float3x4 _RESERVED_IDENTIFIER_FIXUP_m0; + float3x4 _RESERVED_IDENTIFIER_FIXUP_m1; }; -struct _42 +struct _RESERVED_IDENTIFIER_FIXUP_1126_22044 { - float4x4 _m0; - float4x4 _m1; - float _m2; + float4x4 _RESERVED_IDENTIFIER_FIXUP_m0; + float4x4 _RESERVED_IDENTIFIER_FIXUP_m1; + float _RESERVED_IDENTIFIER_FIXUP_m9; char _m3_pad[12]; - packed_float3 _m3; - float _m4; - packed_float3 _m5; - float _m6; - float _m7; - float _m8; - float2 _m9; + packed_float3 _RESERVED_IDENTIFIER_FIXUP_m10; + float _RESERVED_IDENTIFIER_FIXUP_m11; + packed_float3 _RESERVED_IDENTIFIER_FIXUP_m12; + float _RESERVED_IDENTIFIER_FIXUP_m17; + float _RESERVED_IDENTIFIER_FIXUP_m18; + float _RESERVED_IDENTIFIER_FIXUP_m19; + float2 _RESERVED_IDENTIFIER_FIXUP_m20; }; struct main0_out { - float3 m_72 [[user(locn0)]]; + float3 _RESERVED_IDENTIFIER_FIXUP_3976 [[user(locn0)]]; float4 gl_Position [[position]]; }; struct main0_in { - float4 m_25 [[attribute(0)]]; + float4 _RESERVED_IDENTIFIER_FIXUP_5275 [[attribute(0)]]; }; -vertex main0_out main0(main0_in in [[stage_in]], constant _15& _17 [[buffer(0)]], constant _42& _44 [[buffer(1)]]) +vertex main0_out main0(main0_in in [[stage_in]], constant _RESERVED_IDENTIFIER_FIXUP_1365_18812& _RESERVED_IDENTIFIER_FIXUP_18812 [[buffer(0)]], constant _RESERVED_IDENTIFIER_FIXUP_1126_22044& _RESERVED_IDENTIFIER_FIXUP_22044 [[buffer(1)]]) { main0_out out = {}; - float3 _91; - float3 _13; + float3 _RESERVED_IDENTIFIER_FIXUP_2; + float3 _RESERVED_IDENTIFIER_FIXUP_23783; for (;;) { - _13 = normalize(float4(in.m_25.xyz, 0.0) * _17._m1); + _RESERVED_IDENTIFIER_FIXUP_23783 = normalize(float4(in._RESERVED_IDENTIFIER_FIXUP_5275.xyz, 0.0) * _RESERVED_IDENTIFIER_FIXUP_18812._RESERVED_IDENTIFIER_FIXUP_m1); break; } - float4 _39 = _44._m0 * float4(float3(_44._m3) + (in.m_25.xyz * (_44._m6 + _44._m7)), 1.0); - out.m_72 = _13; - float4 _74 = _39; - _74.y = -_39.y; - out.gl_Position = _74; + float4 _RESERVED_IDENTIFIER_FIXUP_14995 = _RESERVED_IDENTIFIER_FIXUP_22044._RESERVED_IDENTIFIER_FIXUP_m0 * float4(float3(_RESERVED_IDENTIFIER_FIXUP_22044._RESERVED_IDENTIFIER_FIXUP_m10) + (in._RESERVED_IDENTIFIER_FIXUP_5275.xyz * (_RESERVED_IDENTIFIER_FIXUP_22044._RESERVED_IDENTIFIER_FIXUP_m17 + _RESERVED_IDENTIFIER_FIXUP_22044._RESERVED_IDENTIFIER_FIXUP_m18)), 1.0); + out._RESERVED_IDENTIFIER_FIXUP_3976 = _RESERVED_IDENTIFIER_FIXUP_23783; + float4 _RESERVED_IDENTIFIER_FIXUP_6282 = _RESERVED_IDENTIFIER_FIXUP_14995; + _RESERVED_IDENTIFIER_FIXUP_6282.y = -_RESERVED_IDENTIFIER_FIXUP_14995.y; + out.gl_Position = _RESERVED_IDENTIFIER_FIXUP_6282; return out; } diff --git a/reference/shaders-no-opt/asm/frag/reserved-identifiers.asm.frag b/reference/shaders-no-opt/asm/frag/reserved-identifiers.asm.frag new file mode 100644 index 00000000..5d75a44a --- /dev/null +++ b/reference/shaders-no-opt/asm/frag/reserved-identifiers.asm.frag @@ -0,0 +1,17 @@ +#version 450 + +layout(location = 0) out vec4 _RESERVED_IDENTIFIER_FIXUP_spvFoo; +layout(location = 1) out vec4 SPIRV_Cross_blah; +layout(location = 2) out vec4 _40Bar; +layout(location = 3) out vec4 _m40; +layout(location = 4) out vec4 _underscore_foo_bar_meep_; + +void main() +{ + _RESERVED_IDENTIFIER_FIXUP_spvFoo = vec4(0.0); + SPIRV_Cross_blah = vec4(1.0); + _40Bar = vec4(2.0); + _m40 = vec4(3.0); + _underscore_foo_bar_meep_ = vec4(4.0); +} + diff --git a/reference/shaders-reflection/asm/op-source-hlsl-uav-1.asm.comp.json b/reference/shaders-reflection/asm/op-source-hlsl-uav-1.asm.comp.json index 19feacc4..b34f85bb 100644 --- a/reference/shaders-reflection/asm/op-source-hlsl-uav-1.asm.comp.json +++ b/reference/shaders-reflection/asm/op-source-hlsl-uav-1.asm.comp.json @@ -20,7 +20,7 @@ "name" : "UAV0", "members" : [ { - "name" : "_data", + "name" : "@data", "type" : "vec4", "array" : [ 0 diff --git a/reference/shaders-reflection/asm/op-source-hlsl-uav-2.asm.comp.json b/reference/shaders-reflection/asm/op-source-hlsl-uav-2.asm.comp.json index 6d0b685c..052e3ba8 100644 --- a/reference/shaders-reflection/asm/op-source-hlsl-uav-2.asm.comp.json +++ b/reference/shaders-reflection/asm/op-source-hlsl-uav-2.asm.comp.json @@ -20,7 +20,7 @@ "name" : "UAV0", "members" : [ { - "name" : "_data", + "name" : "@data", "type" : "vec4", "array" : [ 0 diff --git a/reference/shaders-reflection/asm/op-source-none-uav-1.asm.comp.json b/reference/shaders-reflection/asm/op-source-none-uav-1.asm.comp.json index 19feacc4..b34f85bb 100644 --- a/reference/shaders-reflection/asm/op-source-none-uav-1.asm.comp.json +++ b/reference/shaders-reflection/asm/op-source-none-uav-1.asm.comp.json @@ -20,7 +20,7 @@ "name" : "UAV0", "members" : [ { - "name" : "_data", + "name" : "@data", "type" : "vec4", "array" : [ 0 diff --git a/reference/shaders-reflection/asm/op-source-none-uav-2.asm.comp.json b/reference/shaders-reflection/asm/op-source-none-uav-2.asm.comp.json index 6d0b685c..052e3ba8 100644 --- a/reference/shaders-reflection/asm/op-source-none-uav-2.asm.comp.json +++ b/reference/shaders-reflection/asm/op-source-none-uav-2.asm.comp.json @@ -20,7 +20,7 @@ "name" : "UAV0", "members" : [ { - "name" : "_data", + "name" : "@data", "type" : "vec4", "array" : [ 0 diff --git a/reference/shaders-reflection/frag/image-load-store-uint-coord.asm.frag.json b/reference/shaders-reflection/frag/image-load-store-uint-coord.asm.frag.json index 527ea2bf..c239527c 100644 --- a/reference/shaders-reflection/frag/image-load-store-uint-coord.asm.frag.json +++ b/reference/shaders-reflection/frag/image-load-store-uint-coord.asm.frag.json @@ -8,7 +8,7 @@ "outputs" : [ { "type" : "vec4", - "name" : "_entryPointOutput", + "name" : "@entryPointOutput", "location" : 0 } ], diff --git a/reference/shaders-ue4/asm/frag/subpass-input.ios.framebuffer-fetch.asm.frag b/reference/shaders-ue4/asm/frag/subpass-input.ios.framebuffer-fetch.asm.frag index 3dfcf8af..817b1cff 100644 --- a/reference/shaders-ue4/asm/frag/subpass-input.ios.framebuffer-fetch.asm.frag +++ b/reference/shaders-ue4/asm/frag/subpass-input.ios.framebuffer-fetch.asm.frag @@ -197,10 +197,10 @@ struct main0_out float4 out_var_SV_Target0 [[color(0)]]; }; -fragment main0_out main0(constant type_View& View [[buffer(0)]], constant type_Globals& _Globals [[buffer(1)]], float4 _gl_LastFragData [[color(0)]], texture2d ShadowDepthTexture [[texture(0)]], sampler ShadowDepthTextureSampler [[sampler(0)]], float4 gl_FragCoord [[position]]) +fragment main0_out main0(constant type_View& View [[buffer(0)]], constant type_Globals& _Globals [[buffer(1)]], float4 _RESERVED_IDENTIFIER_FIXUP_gl_LastFragData [[color(0)]], texture2d ShadowDepthTexture [[texture(0)]], sampler ShadowDepthTextureSampler [[sampler(0)]], float4 gl_FragCoord [[position]]) { main0_out out = {}; - float4 _67 = _gl_LastFragData; + float4 _67 = _RESERVED_IDENTIFIER_FIXUP_gl_LastFragData; float _68 = _67.w; float4 _82 = _Globals.ScreenToShadowMatrix * float4((((gl_FragCoord.xy * View.View_BufferSizeAndInvSize.zw) - View.View_ScreenPositionScaleBias.wz) / View.View_ScreenPositionScaleBias.xy) * float2(_68), _68, 1.0); float _118 = fast::clamp(((fast::clamp((ShadowDepthTexture.sample(ShadowDepthTextureSampler, (((_82.xyz / float3(_82.w)).xy * _Globals.ShadowTileOffsetAndSize.zw).xy + _Globals.ShadowTileOffsetAndSize.xy).xy, level(0.0)).xxx * float3(_Globals.SoftTransitionScale.z)) - float3((fast::min(_82.z, 0.999989986419677734375) * _Globals.SoftTransitionScale.z) - 1.0), float3(0.0), float3(1.0)).x - 0.5) * _Globals.ShadowSharpen) + 0.5, 0.0, 1.0); diff --git a/shaders-no-opt/asm/frag/reserved-identifiers.asm.frag b/shaders-no-opt/asm/frag/reserved-identifiers.asm.frag new file mode 100644 index 00000000..5015cef8 --- /dev/null +++ b/shaders-no-opt/asm/frag/reserved-identifiers.asm.frag @@ -0,0 +1,51 @@ +; SPIR-V +; Version: 1.0 +; Generator: Khronos Glslang Reference Front End; 10 +; Bound: 24 +; Schema: 0 + OpCapability Shader + %1 = OpExtInstImport "GLSL.std.450" + OpMemoryModel Logical GLSL450 + OpEntryPoint Fragment %main "main" %spvFoo %SPIRV_Cross_blah %_40 %_m40 %_underscore_foo_bar_meep + OpExecutionMode %main OriginUpperLeft + OpSource GLSL 450 + OpName %main "main" + OpName %spvFoo "spvFoo" + OpName %SPIRV_Cross_blah "SPIRV_Cross_blah" + OpName %_40 "_40Bar" + OpName %_m40 "_m40" + OpName %_underscore_foo_bar_meep "__underscore_foo__bar_meep__" + OpDecorate %spvFoo Location 0 + OpDecorate %SPIRV_Cross_blah Location 1 + OpDecorate %_40 Location 2 + OpDecorate %_m40 Location 3 + OpDecorate %_underscore_foo_bar_meep Location 4 + %void = OpTypeVoid + %3 = OpTypeFunction %void + %float = OpTypeFloat 32 + %v4float = OpTypeVector %float 4 +%_ptr_Output_v4float = OpTypePointer Output %v4float + %spvFoo = OpVariable %_ptr_Output_v4float Output + %float_0 = OpConstant %float 0 + %11 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0 +%SPIRV_Cross_blah = OpVariable %_ptr_Output_v4float Output + %float_1 = OpConstant %float 1 + %14 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1 + %_40 = OpVariable %_ptr_Output_v4float Output + %float_2 = OpConstant %float 2 + %17 = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2 + %_m40 = OpVariable %_ptr_Output_v4float Output + %float_3 = OpConstant %float 3 + %20 = OpConstantComposite %v4float %float_3 %float_3 %float_3 %float_3 +%_underscore_foo_bar_meep = OpVariable %_ptr_Output_v4float Output + %float_4 = OpConstant %float 4 + %23 = OpConstantComposite %v4float %float_4 %float_4 %float_4 %float_4 + %main = OpFunction %void None %3 + %5 = OpLabel + OpStore %spvFoo %11 + OpStore %SPIRV_Cross_blah %14 + OpStore %_40 %17 + OpStore %_m40 %20 + OpStore %_underscore_foo_bar_meep %23 + OpReturn + OpFunctionEnd diff --git a/spirv_cpp.cpp b/spirv_cpp.cpp index 53094d4e..d13d6006 100644 --- a/spirv_cpp.cpp +++ b/spirv_cpp.cpp @@ -306,6 +306,8 @@ void CompilerCPP::emit_resources() string CompilerCPP::compile() { + ir.fixup_reserved_names(); + // Do not deal with ES-isms like precision, older extensions and such. options.es = false; options.version = 450; diff --git a/spirv_cross_parsed_ir.cpp b/spirv_cross_parsed_ir.cpp index 655713f8..d7b82fbf 100644 --- a/spirv_cross_parsed_ir.cpp +++ b/spirv_cross_parsed_ir.cpp @@ -74,6 +74,8 @@ ParsedIR &ParsedIR::operator=(ParsedIR &&other) SPIRV_CROSS_NOEXCEPT source = other.source; loop_iteration_depth_hard = other.loop_iteration_depth_hard; loop_iteration_depth_soft = other.loop_iteration_depth_soft; + + meta_needing_name_fixup = std::move(other.meta_needing_name_fixup); } return *this; } @@ -106,6 +108,8 @@ ParsedIR &ParsedIR::operator=(const ParsedIR &other) addressing_model = other.addressing_model; memory_model = other.memory_model; + meta_needing_name_fixup = other.meta_needing_name_fixup; + // Very deliberate copying of IDs. There is no default copy constructor, nor a simple default constructor. // Construct object first so we have the correct allocator set-up, then we can copy object into our new pool group. ids.clear(); @@ -134,42 +138,146 @@ static bool is_alpha(char c) return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } -static bool is_alphanumeric(char c) +static bool is_numeric(char c) { - return is_alpha(c) || (c >= '0' && c <= '9'); + return c >= '0' && c <= '9'; } -static string ensure_valid_identifier(const string &name, bool member) +static bool is_alphanumeric(char c) +{ + return is_alpha(c) || is_numeric(c); +} + +static bool is_valid_identifier(const string &name) +{ + if (name.empty()) + return true; + + if (is_numeric(name[0])) + return false; + + for (auto c : name) + if (!is_alphanumeric(c) && c != '_') + return false; + + bool saw_underscore = false; + // Two underscores in a row is not a valid identifier either. + // Technically reserved, but it's easier to treat it as invalid. + for (auto c : name) + { + bool is_underscore = c == '_'; + if (is_underscore && saw_underscore) + return false; + saw_underscore = is_underscore; + } + + return true; +} + +static bool is_reserved_prefix(const string &name) +{ + // Generic reserved identifiers used by the implementation. + return name.compare(0, 3, "gl_", 3) == 0 || + // Ignore this case for now, might rewrite internal code to always use spv prefix. + //name.compare(0, 11, "SPIRV_Cross", 11) == 0 || + name.compare(0, 3, "spv", 3) == 0; +} + +static bool is_reserved_identifier(const string &name, bool member, bool allow_reserved_prefixes) +{ + if (!allow_reserved_prefixes && is_reserved_prefix(name)) + return true; + + if (member) + { + // Reserved member identifiers come in one form: + // _m[0-9]+$. + if (name.size() < 3) + return false; + + if (name.compare(0, 2, "_m", 2) != 0) + return false; + + size_t index = 2; + while (index < name.size() && is_numeric(name[index])) + index++; + + return index == name.size(); + } + else + { + // Reserved non-member identifiers come in two forms: + // _[0-9]+$, used for temporaries which map directly to a SPIR-V ID. + // _[0-9]+_, used for auxillary temporaries which derived from a SPIR-V ID. + if (name.size() < 2) + return false; + + if (name[0] != '_' || !is_numeric(name[1])) + return false; + + size_t index = 2; + while (index < name.size() && is_numeric(name[index])) + index++; + + return index == name.size() || (index < name.size() && name[index] == '_'); + } +} + +bool ParsedIR::is_globally_reserved_identifier(std::string &str, bool allow_reserved_prefixes) +{ + return is_reserved_identifier(str, false, allow_reserved_prefixes); +} + +static string make_unreserved_identifier(const string &name) +{ + if (is_reserved_prefix(name)) + return "_RESERVED_IDENTIFIER_FIXUP_" + name; + else + return "_RESERVED_IDENTIFIER_FIXUP" + name; +} + +void ParsedIR::sanitize_underscores(std::string &str) +{ + // Compact adjacent underscores to make it valid. + auto dst = str.begin(); + auto src = dst; + bool saw_underscore = false; + while (src != str.end()) + { + bool is_underscore = *src == '_'; + if (saw_underscore && is_underscore) + { + src++; + } + else + { + if (dst != src) + *dst = *src; + dst++; + src++; + saw_underscore = is_underscore; + } + } + str.erase(dst, str.end()); +} + +static string ensure_valid_identifier(const string &name) { // Functions in glslangValidator are mangled with name( stuff. // Normally, we would never see '(' in any legal identifiers, so just strip them out. auto str = name.substr(0, name.find('(')); - for (uint32_t i = 0; i < str.size(); i++) - { - auto &c = str[i]; + if (str.empty()) + return str; - if (member) - { - // _m variables are reserved by the internal implementation, - // otherwise, make sure the name is a valid identifier. - if (i == 0) - c = is_alpha(c) ? c : '_'; - else if (i == 2 && str[0] == '_' && str[1] == 'm') - c = is_alpha(c) ? c : '_'; - else - c = is_alphanumeric(c) ? c : '_'; - } - else - { - // _ variables are reserved by the internal implementation, - // otherwise, make sure the name is a valid identifier. - if (i == 0 || (str[0] == '_' && i == 1)) - c = is_alpha(c) ? c : '_'; - else - c = is_alphanumeric(c) ? c : '_'; - } - } + if (is_numeric(str[0])) + str[0] = '_'; + + for (auto &c : str) + if (!is_alphanumeric(c) && c != '_') + c = '_'; + + ParsedIR::sanitize_underscores(str); return str; } @@ -195,35 +303,41 @@ const string &ParsedIR::get_member_name(TypeID id, uint32_t index) const return empty_string; } +void ParsedIR::sanitize_identifier(std::string &name, bool member, bool allow_reserved_prefixes) +{ + if (!is_valid_identifier(name)) + name = ensure_valid_identifier(name); + if (is_reserved_identifier(name, member, allow_reserved_prefixes)) + name = make_unreserved_identifier(name); +} + +void ParsedIR::fixup_reserved_names() +{ + for (uint32_t id : meta_needing_name_fixup) + { + auto &m = meta[id]; + sanitize_identifier(m.decoration.alias, false, false); + for (auto &memb : m.members) + sanitize_identifier(memb.alias, true, false); + } + meta_needing_name_fixup.clear(); +} + void ParsedIR::set_name(ID id, const string &name) { - auto &str = meta[id].decoration.alias; - str.clear(); - - if (name.empty()) - return; - - // Reserved for temporaries. - if (name[0] == '_' && name.size() >= 2 && isdigit(name[1])) - return; - - str = ensure_valid_identifier(name, false); + auto &m = meta[id]; + m.decoration.alias = name; + if (!is_valid_identifier(name) || is_reserved_identifier(name, false, false)) + meta_needing_name_fixup.insert(id); } void ParsedIR::set_member_name(TypeID id, uint32_t index, const string &name) { - meta[id].members.resize(max(meta[id].members.size(), size_t(index) + 1)); - - auto &str = meta[id].members[index].alias; - str.clear(); - if (name.empty()) - return; - - // Reserved for unnamed members. - if (name[0] == '_' && name.size() >= 3 && name[1] == 'm' && isdigit(name[2])) - return; - - str = ensure_valid_identifier(name, true); + auto &m = meta[id]; + m.members.resize(max(meta[id].members.size(), size_t(index) + 1)); + m.members[index].alias = name; + if (!is_valid_identifier(name) || is_reserved_identifier(name, true, false)) + meta_needing_name_fixup.insert(id); } void ParsedIR::set_decoration_string(ID id, Decoration decoration, const string &argument) diff --git a/spirv_cross_parsed_ir.hpp b/spirv_cross_parsed_ir.hpp index 4880c841..36d6ac7b 100644 --- a/spirv_cross_parsed_ir.hpp +++ b/spirv_cross_parsed_ir.hpp @@ -208,6 +208,12 @@ public: void make_constant_null(uint32_t id, uint32_t type, bool add_to_typed_id_set); + void fixup_reserved_names(); + + static void sanitize_underscores(std::string &str); + static void sanitize_identifier(std::string &str, bool member, bool allow_reserved_prefixes); + static bool is_globally_reserved_identifier(std::string &str, bool allow_reserved_prefixes); + private: template T &get(uint32_t id) @@ -225,6 +231,8 @@ private: mutable uint32_t loop_iteration_depth_soft = 0; std::string empty_string; Bitset cleared_bitset; + + std::unordered_set meta_needing_name_fixup; }; } // namespace SPIRV_CROSS_NAMESPACE diff --git a/spirv_glsl.cpp b/spirv_glsl.cpp index 2002b9e1..ef4a705f 100644 --- a/spirv_glsl.cpp +++ b/spirv_glsl.cpp @@ -145,32 +145,6 @@ static BufferPackingStandard packing_to_substruct_packing(BufferPackingStandard } } -// Sanitizes underscores for GLSL where multiple underscores in a row are not allowed. -string CompilerGLSL::sanitize_underscores(const string &str) -{ - string res; - res.reserve(str.size()); - - bool last_underscore = false; - for (auto c : str) - { - if (c == '_') - { - if (last_underscore) - continue; - - res += c; - last_underscore = true; - } - else - { - res += c; - last_underscore = false; - } - } - return res; -} - void CompilerGLSL::init() { if (ir.source.known) @@ -529,6 +503,8 @@ void CompilerGLSL::find_static_extensions() string CompilerGLSL::compile() { + ir.fixup_reserved_names(); + if (options.vulkan_semantics) backend.allow_precision_qualifiers = true; else @@ -2150,7 +2126,7 @@ void CompilerGLSL::emit_flattened_io_block_member(const std::string &basename, c // Sanitize underscores because joining the two identifiers might create more than 1 underscore in a row, // which is not allowed. - flattened_name = sanitize_underscores(flattened_name); + ParsedIR::sanitize_underscores(flattened_name); uint32_t last_index = indices.back(); @@ -2693,6 +2669,23 @@ bool CompilerGLSL::should_force_emit_builtin_block(StorageClass storage) return should_force; } +void CompilerGLSL::fixup_implicit_builtin_block_names() +{ + ir.for_each_typed_id([&](uint32_t, SPIRVariable &var) { + auto &type = this->get(var.basetype); + bool block = has_decoration(type.self, DecorationBlock); + if ((var.storage == StorageClassOutput || var.storage == StorageClassInput) && block && + is_builtin_variable(var)) + { + // Make sure the array has a supported name in the code. + if (var.storage == StorageClassOutput) + set_name(var.self, "gl_out"); + else if (var.storage == StorageClassInput) + set_name(var.self, "gl_in"); + } + }); +} + void CompilerGLSL::emit_declared_builtin_block(StorageClass storage, ExecutionModel model) { Bitset emitted_builtins; @@ -2874,12 +2867,6 @@ void CompilerGLSL::emit_declared_builtin_block(StorageClass storage, ExecutionMo if (builtin_array) { - // Make sure the array has a supported name in the code. - if (storage == StorageClassOutput) - set_name(block_var->self, "gl_out"); - else if (storage == StorageClassInput) - set_name(block_var->self, "gl_in"); - if (model == ExecutionModelTessellationControl && storage == StorageClassOutput) end_scope_decl(join(to_name(block_var->self), "[", get_entry_point().output_vertices, "]")); else @@ -2936,6 +2923,18 @@ void CompilerGLSL::emit_resources() if (!pls_inputs.empty() || !pls_outputs.empty()) emit_pls(); + switch (execution.model) + { + case ExecutionModelGeometry: + case ExecutionModelTessellationControl: + case ExecutionModelTessellationEvaluation: + fixup_implicit_builtin_block_names(); + break; + + default: + break; + } + // Emit custom gl_PerVertex for SSO compatibility. if (options.separate_shader_objects && !options.es && execution.model != ExecutionModelFragment) { @@ -7793,7 +7792,9 @@ void CompilerGLSL::prepare_access_chain_for_scalar_access(std::string &, const S string CompilerGLSL::to_flattened_struct_member(const string &basename, const SPIRType &type, uint32_t index) { - return sanitize_underscores(join(basename, "_", to_member_name(type, index))); + auto ret = join(basename, "_", to_member_name(type, index)); + ParsedIR::sanitize_underscores(ret); + return ret; } string CompilerGLSL::access_chain(uint32_t base, const uint32_t *indices, uint32_t count, const SPIRType &target_type, @@ -7837,7 +7838,9 @@ string CompilerGLSL::access_chain(uint32_t base, const uint32_t *indices, uint32 } auto basename = to_flattened_access_chain_expression(base); - return sanitize_underscores(join(basename, "_", chain)); + auto ret = join(basename, "_", chain); + ParsedIR::sanitize_underscores(ret); + return ret; } else { @@ -7895,7 +7898,8 @@ void CompilerGLSL::store_flattened_struct(const string &basename, uint32_t rhs_i for (uint32_t i = 0; i < uint32_t(member_type->member_types.size()); i++) { sub_indices.back() = i; - auto lhs = sanitize_underscores(join(basename, "_", to_member_name(*member_type, i))); + auto lhs = join(basename, "_", to_member_name(*member_type, i)); + ParsedIR::sanitize_underscores(lhs); if (get(member_type->member_types[i]).basetype == SPIRType::Struct) { @@ -11435,13 +11439,7 @@ void CompilerGLSL::add_member_name(SPIRType &type, uint32_t index) if (name.empty()) return; - // Reserved for temporaries. - if (name[0] == '_' && name.size() >= 2 && isdigit(name[1])) - { - name.clear(); - return; - } - + ParsedIR::sanitize_identifier(name, true, true); update_name_cache(type.member_name_cache, name); } } @@ -12167,16 +12165,13 @@ void CompilerGLSL::add_variable(unordered_set &variables_primary, if (name.empty()) return; - // Reserved for temporaries. - if (name[0] == '_' && name.size() >= 2 && isdigit(name[1])) + ParsedIR::sanitize_underscores(name); + if (ParsedIR::is_globally_reserved_identifier(name, true)) { name.clear(); return; } - // Avoid double underscores. - name = sanitize_underscores(name); - update_name_cache(variables_primary, variables_secondary, name); } diff --git a/spirv_glsl.hpp b/spirv_glsl.hpp index c3837434..79f6582c 100644 --- a/spirv_glsl.hpp +++ b/spirv_glsl.hpp @@ -487,6 +487,7 @@ protected: void emit_buffer_reference_block(SPIRType &type, bool forward_declaration); void emit_buffer_block_legacy(const SPIRVariable &var); void emit_buffer_block_flattened(const SPIRVariable &type); + void fixup_implicit_builtin_block_names(); void emit_declared_builtin_block(spv::StorageClass storage, spv::ExecutionModel model); bool should_force_emit_builtin_block(spv::StorageClass storage); void emit_push_constant_block_vulkan(const SPIRVariable &var); @@ -757,8 +758,6 @@ protected: virtual void declare_undefined_values(); - static std::string sanitize_underscores(const std::string &str); - bool can_use_io_location(spv::StorageClass storage, bool block); const Instruction *get_next_instruction_in_block(const Instruction &instr); static uint32_t mask_relevant_memory_semantics(uint32_t semantics); diff --git a/spirv_hlsl.cpp b/spirv_hlsl.cpp index 51b3dd48..9f6a9f1b 100644 --- a/spirv_hlsl.cpp +++ b/spirv_hlsl.cpp @@ -971,7 +971,9 @@ std::string CompilerHLSL::builtin_to_glsl(spv::BuiltIn builtin, spv::StorageClas auto &var = get(num_workgroups_builtin); auto &type = get(var.basetype); - return sanitize_underscores(join(to_name(num_workgroups_builtin), "_", get_member_name(type.self, 0))); + auto ret = join(to_name(num_workgroups_builtin), "_", get_member_name(type.self, 0)); + ParsedIR::sanitize_underscores(ret); + return ret; } case BuiltInPointCoord: // Crude hack, but there is no real alternative. This path is only enabled if point_coord_compat is set. @@ -2076,7 +2078,9 @@ void CompilerHLSL::emit_buffer_block(const SPIRVariable &var) add_member_name(type, i); auto backup_name = get_member_name(type.self, i); auto member_name = to_member_name(type, i); - set_member_name(type.self, i, sanitize_underscores(join(to_name(var.self), "_", member_name))); + member_name = join(to_name(var.self), "_", member_name); + ParsedIR::sanitize_underscores(member_name); + set_member_name(type.self, i, member_name); emit_struct_member(type, member, i, ""); set_member_name(type.self, i, backup_name); i++; @@ -2157,8 +2161,9 @@ void CompilerHLSL::emit_push_constant_block(const SPIRVariable &var) add_member_name(type, constant_index); auto backup_name = get_member_name(type.self, i); auto member_name = to_member_name(type, i); - set_member_name(type.self, constant_index, - sanitize_underscores(join(to_name(var.self), "_", member_name))); + member_name = join(to_name(var.self), "_", member_name); + ParsedIR::sanitize_underscores(member_name); + set_member_name(type.self, constant_index, member_name); emit_struct_member(type, member, i, "", layout.start); set_member_name(type.self, constant_index, backup_name); @@ -5590,6 +5595,8 @@ void CompilerHLSL::validate_shader_model() string CompilerHLSL::compile() { + ir.fixup_reserved_names(); + // Do not deal with ES-isms like precision, older extensions and such. options.es = false; options.version = 450; diff --git a/spirv_msl.cpp b/spirv_msl.cpp index 9edad29a..c435af86 100644 --- a/spirv_msl.cpp +++ b/spirv_msl.cpp @@ -589,7 +589,7 @@ void CompilerMSL::build_implicit_builtins() uint_type_ptr_out.pointer = true; uint_type_ptr_out.parent_type = get_uint_type_id(); uint_type_ptr_out.storage = StorageClassOutput; - + auto &ptr_out_type = set(offset, uint_type_ptr_out); ptr_out_type.self = get_uint_type_id(); set(var_id, offset, StorageClassOutput); @@ -1028,6 +1028,8 @@ void CompilerMSL::emit_entry_point_declarations() string CompilerMSL::compile() { + ir.fixup_reserved_names(); + // Do not deal with GLES-isms like precision, older extensions and such. options.vulkan_semantics = true; options.es = false;