Skip to content

Commit

Permalink
Merge pull request #1409 from KhronosGroup/fix-1405
Browse files Browse the repository at this point in the history
Improve expression usage tracking
  • Loading branch information
HansKristian-Work authored Jun 29, 2020
2 parents 2e7a562 + 3afbfdb commit 42096ca
Show file tree
Hide file tree
Showing 17 changed files with 189 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ layout(location = 0) out vec4 _entryPointOutput;

void main()
{
vec2 _45 = vec2(0.0, _8.CB1.TextureSize.w);
vec4 _49 = texture(SPIRV_Cross_CombinedmapTexturemapSampler, IN_uv);
float _50 = _49.y;
float _53 = clamp(_50 * 0.06399999558925628662109375, 7.999999797903001308441162109375e-05, 0.008000000379979610443115234375);
float _55;
float _58;
_55 = 0.0;
_58 = 0.0;
for (int _60 = -3; _60 <= 3; )
{
float _64 = float(_60);
vec4 _72 = texture(SPIRV_Cross_CombinedmapTexturemapSampler, IN_uv + (vec2(0.0, _8.CB1.TextureSize.w) * _64));
float _78 = exp(((-_64) * _64) * 0.2222220003604888916015625) * float(abs(_72.y - _50) < clamp(_50 * 0.06399999558925628662109375, 7.999999797903001308441162109375e-05, 0.008000000379979610443115234375));
vec4 _72 = texture(SPIRV_Cross_CombinedmapTexturemapSampler, IN_uv + (_45 * _64));
float _78 = exp(((-_64) * _64) * 0.2222220003604888916015625) * float(abs(_72.y - _50) < _53);
_55 += (_72.x * _78);
_58 += _78;
_60++;
Expand Down
29 changes: 29 additions & 0 deletions reference/opt/shaders/frag/avoid-expression-lowering-to-loop.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#version 310 es
precision mediump float;
precision highp int;

layout(binding = 1, std140) uniform Count
{
float count;
} _44;

layout(binding = 0) uniform mediump sampler2D tex;

layout(location = 0) in highp vec4 vertex;
layout(location = 0) out vec4 fragColor;

void main()
{
highp float _24 = 1.0 / float(textureSize(tex, 0).x);
highp float _34 = dFdx(vertex.x);
float _62;
_62 = 0.0;
for (float _61 = 0.0; _61 < _44.count; )
{
_62 += (_24 * _34);
_61 += 1.0;
continue;
}
fragColor = vec4(_62);
}

3 changes: 2 additions & 1 deletion reference/shaders-msl/asm/comp/variable-pointers-2.asm.comp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ kernel void main0(device foo& buf [[buffer(0)]], constant bar& cb [[buffer(1)]],
{
device foo* _46 = select_buffer(buf, cb);
device foo* _45 = _46;
thread uint3* _47 = select_input(gl_GlobalInvocationID, gl_LocalInvocationID, cb);
device foo* _48 = _45;
device int* _52;
device int* _55;
Expand All @@ -46,7 +47,7 @@ kernel void main0(device foo& buf [[buffer(0)]], constant bar& cb [[buffer(1)]],
_58 = *_55;
if (_57 != _58)
{
int _66 = (_57 + _58) + int((*select_input(gl_GlobalInvocationID, gl_LocalInvocationID, cb)).x);
int _66 = (_57 + _58) + int((*_47).x);
*_52 = _66;
*_55 = _66;
_52 = &_52[1u];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ void main()
_151.UvStuds = IN_UvStuds_EdgeDistance2.xy;
SurfaceInput _156 = _151;
_156.UvStuds.y = (fract(_151.UvStuds.y) + IN_studIndex) * 0.25;
float _160 = clamp(1.0 - (_146.View_Depth.w * 0.00333332992158830165863037109375), 0.0, 1.0);
float _163 = _146.View_Depth.w * _19.CB0.RefractionBias_FadeDistance_GlowFactor.y;
float _165 = clamp(1.0 - _163, 0.0, 1.0);
vec2 _166 = IN_Uv_EdgeDistance1.xy * 1.0;
Expand All @@ -141,7 +142,7 @@ void main()
else
{
float _180 = 1.0 / (1.0 - 0.0);
_193 = mix(texture(SPIRV_Cross_CombinedDiffuseMapTextureDiffuseMapSampler, _166 * 0.25), texture(SPIRV_Cross_CombinedDiffuseMapTextureDiffuseMapSampler, _166), vec4(clamp((clamp(1.0 - (_146.View_Depth.w * 0.00333332992158830165863037109375), 0.0, 1.0) * _180) - (0.0 * _180), 0.0, 1.0)));
_193 = mix(texture(SPIRV_Cross_CombinedDiffuseMapTextureDiffuseMapSampler, _166 * 0.25), texture(SPIRV_Cross_CombinedDiffuseMapTextureDiffuseMapSampler, _166), vec4(clamp((_160 * _180) - (0.0 * _180), 0.0, 1.0)));
break;
}
_193 = _192;
Expand All @@ -167,12 +168,14 @@ void main()
vec2 _223 = vec2(1.0);
vec2 _224 = (_220.wy * 2.0) - _223;
vec3 _232 = vec3(_224, sqrt(clamp(1.0 + dot(-_224, _224), 0.0, 1.0)));
vec2 _240 = (texture(SPIRV_Cross_CombinedNormalDetailMapTextureNormalDetailMapSampler, _166 * 0.0).wy * 2.0) - _223;
vec4 _237 = texture(SPIRV_Cross_CombinedNormalDetailMapTextureNormalDetailMapSampler, _166 * 0.0);
vec2 _240 = (_237.wy * 2.0) - _223;
vec2 _252 = _232.xy + (vec3(_240, sqrt(clamp(1.0 + dot(-_240, _240), 0.0, 1.0))).xy * 0.0);
vec3 _253 = vec3(_252.x, _252.y, _232.z);
vec2 _255 = _253.xy * _165;
vec3 _256 = vec3(_255.x, _255.y, _253.z);
vec3 _271 = ((IN_Color.xyz * (_193 * 1.0).xyz) * (1.0 + (_256.x * 0.300000011920928955078125))) * (texture(SPIRV_Cross_CombinedStudsMapTextureStudsMapSampler, _156.UvStuds).x * 2.0);
vec4 _268 = texture(SPIRV_Cross_CombinedStudsMapTextureStudsMapSampler, _156.UvStuds);
vec3 _271 = ((IN_Color.xyz * (_193 * 1.0).xyz) * (1.0 + (_256.x * 0.300000011920928955078125))) * (_268.x * 2.0);
vec4 _298;
for (;;)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,14 @@ fragment main0_out main0(main0_in in [[stage_in]], constant type_View& View [[bu
{
main0_out out = {};
float4 _177 = float4((((gl_FragCoord.xy - View.View_ViewRectMin.xy) * View.View_ViewSizeAndInvSize.zw) - float2(0.5)) * float2(2.0, -2.0), _138, 1.0) * float4(gl_FragCoord.w);
float3 _179 = in.in_var_TEXCOORD8.xyz - float3(View.View_PreViewTranslation);
float3 _181 = normalize(-in.in_var_TEXCOORD8.xyz);
float2 _190 = (Material_Texture2D_0.sample(Material_Texture2D_0Sampler, (in.in_var_TEXCOORD0 * float2(10.0))).xy * float2(2.0)) - float2(1.0);
float4 _187 = Material_Texture2D_0.sample(Material_Texture2D_0Sampler, (in.in_var_TEXCOORD0 * float2(10.0)));
float2 _190 = (_187.xy * float2(2.0)) - float2(1.0);
float3 _206 = normalize(float3x3(float3(1.0, 0.0, 0.0), float3(0.0, 1.0, 0.0), float3(0.0, 0.0, 1.0)) * (((float4(_190, sqrt(fast::clamp(1.0 - dot(_190, _190), 0.0, 1.0)), 1.0).xyz * float3(0.300000011920928955078125, 0.300000011920928955078125, 1.0)) * float3(View.View_NormalOverrideParameter.w)) + View.View_NormalOverrideParameter.xyz));
float _208 = dot(_206, _181);
float _219 = mix(0.4000000059604644775390625, 1.0, Material_Texture2D_1.sample(Material_Texture2D_1Sampler, (in.in_var_TEXCOORD0 * float2(20.0))).x);
float4 _217 = Material_Texture2D_1.sample(Material_Texture2D_1Sampler, (in.in_var_TEXCOORD0 * float2(20.0)));
float _219 = mix(0.4000000059604644775390625, 1.0, _217.x);
float4 _223 = Material_Texture2D_1.sample(Material_Texture2D_1Sampler, (in.in_var_TEXCOORD0 * float2(5.0)));
float _224 = _177.w;
float _228 = fast::min(fast::max((_224 - 24.0) * 0.000666666659526526927947998046875, 0.0), 1.0);
Expand Down Expand Up @@ -328,7 +331,7 @@ fragment main0_out main0(main0_in in [[stage_in]], constant type_View& View [[bu
{
if (_491 < _Globals.NumDynamicPointLights)
{
float3 _501 = _Globals.LightPositionAndInvRadius[_491].xyz - (in.in_var_TEXCOORD8.xyz - float3(View.View_PreViewTranslation));
float3 _501 = _Globals.LightPositionAndInvRadius[_491].xyz - _179;
float _502 = dot(_501, _501);
float3 _505 = _501 * float3(rsqrt(_502));
_507 = normalize(_181 + _505);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,11 @@ fragment main0_out main0(main0_in in [[stage_in]], constant type_View& View [[bu
float3 _151 = normalize(-_148);
float3 _152 = _151 * float3x3(in.in_var_TEXCOORD10_centroid.xyz, cross(in.in_var_TEXCOORD11_centroid.xyz, in.in_var_TEXCOORD10_centroid.xyz) * float3(in.in_var_TEXCOORD11_centroid.w), in.in_var_TEXCOORD11_centroid.xyz);
float _170 = mix(Material.Material_ScalarExpressions[0].y, Material.Material_ScalarExpressions[0].z, fast::min(fast::max(abs(dot(_151, in.in_var_TEXCOORD11_centroid.xyz)), 0.0), 1.0));
float _171 = floor(_170);
float _172 = 1.0 / _170;
float2 _174 = (float2(Material.Material_ScalarExpressions[0].x) * ((_152.xy * float2(-1.0)) / float2(_152.z))) * float2(_172);
float2 _175 = dfdx(float2(in_var_TEXCOORD0[0].x, in_var_TEXCOORD0[0].y));
float2 _176 = dfdy(float2(in_var_TEXCOORD0[0].x, in_var_TEXCOORD0[0].y));
float _180_copy;
float2 _183;
_183 = float2(0.0);
Expand All @@ -288,9 +291,9 @@ fragment main0_out main0(main0_in in [[stage_in]], constant type_View& View [[bu
float _189 = 1.0;
for (;;)
{
if (float(_185) < (floor(_170) + 2.0))
if (float(_185) < (_171 + 2.0))
{
_188 = Material_Texture2D_0.sample(Material_Texture2D_0Sampler, (float2(in_var_TEXCOORD0[0].x, in_var_TEXCOORD0[0].y) + _183), gradient2d(dfdx(float2(in_var_TEXCOORD0[0].x, in_var_TEXCOORD0[0].y)), dfdy(float2(in_var_TEXCOORD0[0].x, in_var_TEXCOORD0[0].y)))).y;
_188 = Material_Texture2D_0.sample(Material_Texture2D_0Sampler, (float2(in_var_TEXCOORD0[0].x, in_var_TEXCOORD0[0].y) + _183), gradient2d(_175, _176)).y;
if (_180 < _188)
{
float _201 = _188 - _180;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,11 @@ fragment main0_out main0(main0_in in [[stage_in]], constant type_View& View [[bu
float3 _151 = normalize(-_148);
float3 _152 = _151 * float3x3(in.in_var_TEXCOORD10_centroid.xyz, cross(in.in_var_TEXCOORD11_centroid.xyz, in.in_var_TEXCOORD10_centroid.xyz) * float3(in.in_var_TEXCOORD11_centroid.w), in.in_var_TEXCOORD11_centroid.xyz);
float _170 = mix(Material.Material_ScalarExpressions[0].y, Material.Material_ScalarExpressions[0].z, fast::min(fast::max(abs(dot(_151, in.in_var_TEXCOORD11_centroid.xyz)), 0.0), 1.0));
float _171 = floor(_170);
float _172 = 1.0 / _170;
float2 _174 = (float2(Material.Material_ScalarExpressions[0].x) * ((_152.xy * float2(-1.0)) / float2(_152.z))) * float2(_172);
float2 _175 = dfdx(float2(in_var_TEXCOORD0[0].x, in_var_TEXCOORD0[0].y));
float2 _176 = dfdy(float2(in_var_TEXCOORD0[0].x, in_var_TEXCOORD0[0].y));
float _180_copy;
float2 _183;
_183 = float2(0.0);
Expand All @@ -288,9 +291,9 @@ fragment main0_out main0(main0_in in [[stage_in]], constant type_View& View [[bu
float _189 = 1.0;
for (;;)
{
if (float(_185) < (floor(_170) + 2.0))
if (float(_185) < (_171 + 2.0))
{
_188 = Material_Texture2D_0.sample(Material_Texture2D_0Sampler, (float2(in_var_TEXCOORD0[0].x, in_var_TEXCOORD0[0].y) + _183), gradient2d(dfdx(float2(in_var_TEXCOORD0[0].x, in_var_TEXCOORD0[0].y)), dfdy(float2(in_var_TEXCOORD0[0].x, in_var_TEXCOORD0[0].y)))).y;
_188 = Material_Texture2D_0.sample(Material_Texture2D_0Sampler, (float2(in_var_TEXCOORD0[0].x, in_var_TEXCOORD0[0].y) + _183), gradient2d(_175, _176)).y;
if (_180 < _188)
{
float _201 = _188 - _180;
Expand Down
3 changes: 2 additions & 1 deletion reference/shaders-ue4/asm/frag/texture-atomics.asm.frag
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ fragment main0_out main0(main0_in in [[stage_in]], const device type_StructuredB
uint _107 = _103 + 1u;
if (all(CulledObjectBoxBounds._m0[_107].xy > _96.xy) && all(CulledObjectBoxBounds._m0[_103].xyz < _102))
{
float3 _121 = float3(0.5) * (CulledObjectBoxBounds._m0[_103].xyz + CulledObjectBoxBounds._m0[_107].xyz);
float _122 = _96.x;
float _123 = _96.y;
spvUnsafeArray<float3, 8> _73;
Expand All @@ -103,7 +104,7 @@ fragment main0_out main0(main0_in in [[stage_in]], const device type_StructuredB
_158 = float3(500000.0);
for (int _160 = 0; _160 < 8; )
{
float3 _166 = _73[_160] - (float3(0.5) * (CulledObjectBoxBounds._m0[_103].xyz + CulledObjectBoxBounds._m0[_107].xyz));
float3 _166 = _73[_160] - _121;
float3 _170 = float3(dot(_166, CulledObjectBoxBounds._m0[_103 + 2u].xyz), dot(_166, CulledObjectBoxBounds._m0[_103 + 3u].xyz), dot(_166, CulledObjectBoxBounds._m0[_103 + 4u].xyz));
_155 = fast::max(_155, _170);
_158 = fast::min(_158, _170);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ fragment main0_out main0(main0_in in [[stage_in]], const device type_StructuredB
uint _107 = _103 + 1u;
if (all(CulledObjectBoxBounds._m0[_107].xy > _96.xy) && all(CulledObjectBoxBounds._m0[_103].xyz < _102))
{
float3 _121 = float3(0.5) * (CulledObjectBoxBounds._m0[_103].xyz + CulledObjectBoxBounds._m0[_107].xyz);
float _122 = _96.x;
float _123 = _96.y;
spvUnsafeArray<float3, 8> _73;
Expand All @@ -103,7 +104,7 @@ fragment main0_out main0(main0_in in [[stage_in]], const device type_StructuredB
_158 = float3(500000.0);
for (int _160 = 0; _160 < 8; )
{
float3 _166 = _73[_160] - (float3(0.5) * (CulledObjectBoxBounds._m0[_103].xyz + CulledObjectBoxBounds._m0[_107].xyz));
float3 _166 = _73[_160] - _121;
float3 _170 = float3(dot(_166, CulledObjectBoxBounds._m0[_103 + 2u].xyz), dot(_166, CulledObjectBoxBounds._m0[_103 + 3u].xyz), dot(_166, CulledObjectBoxBounds._m0[_103 + 4u].xyz));
_155 = fast::max(_155, _170);
_158 = fast::min(_158, _170);
Expand Down
6 changes: 4 additions & 2 deletions reference/shaders/asm/frag/loop-header-to-continue.asm.frag
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ layout(location = 0) out vec4 _entryPointOutput;

void main()
{
vec2 _45 = vec2(0.0, _8.CB1.TextureSize.w);
vec4 _49 = texture(SPIRV_Cross_CombinedmapTexturemapSampler, IN_uv);
float _50 = _49.y;
float _53 = clamp((_50 * 80.0) * 0.0007999999797903001308441162109375, 7.999999797903001308441162109375e-05, 0.008000000379979610443115234375);
float _55;
float _58;
_55 = 0.0;
_58 = 0.0;
for (int _60 = -3; _60 <= 3; )
{
float _64 = float(_60);
vec4 _72 = texture(SPIRV_Cross_CombinedmapTexturemapSampler, IN_uv + (vec2(0.0, _8.CB1.TextureSize.w) * _64));
float _78 = exp(((-_64) * _64) * 0.2222220003604888916015625) * float(abs(_72.y - _50) < clamp((_50 * 80.0) * 0.0007999999797903001308441162109375, 7.999999797903001308441162109375e-05, 0.008000000379979610443115234375));
vec4 _72 = texture(SPIRV_Cross_CombinedmapTexturemapSampler, IN_uv + (_45 * _64));
float _78 = exp(((-_64) * _64) * 0.2222220003604888916015625) * float(abs(_72.y - _50) < _53);
_55 += (_72.x * _78);
_58 += _78;
_60++;
Expand Down
26 changes: 26 additions & 0 deletions reference/shaders/frag/avoid-expression-lowering-to-loop.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#version 310 es
precision mediump float;
precision highp int;

layout(binding = 1, std140) uniform Count
{
float count;
} _44;

layout(binding = 0) uniform mediump sampler2D tex;

layout(location = 0) in highp vec4 vertex;
layout(location = 0) out vec4 fragColor;

void main()
{
highp float size = 1.0 / float(textureSize(tex, 0).x);
float r = 0.0;
float d = dFdx(vertex.x);
for (float i = 0.0; i < _44.count; i += 1.0)
{
r += (size * d);
}
fragColor = vec4(r);
}

23 changes: 23 additions & 0 deletions shaders/frag/avoid-expression-lowering-to-loop.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#version 310 es
precision mediump float;
precision mediump int;

layout(binding = 0) uniform mediump sampler2D tex;
layout(binding = 1) uniform Count
{
float count;
};

layout(location = 0) in highp vec4 vertex;
layout(location = 0) out vec4 fragColor;

void main() {

highp float size = 1.0 / float(textureSize(tex, 0).x);
float r = 0.0;
float d = dFdx(vertex.x);
for (float i = 0.0; i < count ; i += 1.0)
r += size * d;

fragColor = vec4(r);
}
26 changes: 26 additions & 0 deletions spirv_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,29 @@ inline std::string convert_to_string(double t, char locale_radix_point)
return buf;
}

template <typename T>
struct ValueSaver
{
explicit ValueSaver(T &current_)
: current(current_)
, saved(current_)
{
}

void release()
{
current = saved;
}

~ValueSaver()
{
release();
}

T &current;
T saved;
};

#if defined(__clang__) || defined(__GNUC__)
#pragma GCC diagnostic pop
#elif defined(_MSC_VER)
Expand Down Expand Up @@ -699,6 +722,9 @@ struct SPIRExpression : IVariant
// Used by access chain Store and Load since we read multiple expressions in this case.
SmallVector<ID> implied_read_expressions;

// The expression was emitted at a certain scope. Lets us track when an expression read means multiple reads.
uint32_t emitted_loop_level = 0;

SPIRV_CROSS_DECLARE_CLONE(SPIRExpression)
};

Expand Down
5 changes: 5 additions & 0 deletions spirv_cross.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4674,3 +4674,8 @@ bool Compiler::flush_phi_required(BlockID from, BlockID to) const
return true;
return false;
}

void Compiler::add_loop_level()
{
current_loop_level++;
}
14 changes: 14 additions & 0 deletions spirv_cross.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,22 @@ class Compiler

SPIRFunction *current_function = nullptr;
SPIRBlock *current_block = nullptr;
uint32_t current_loop_level = 0;
std::unordered_set<VariableID> active_interface_variables;
bool check_active_interface_variables = false;

void add_loop_level();

void set_initializers(SPIRExpression &e)
{
e.emitted_loop_level = current_loop_level;
}

template <typename T>
void set_initializers(const T &)
{
}

// If our IDs are out of range here as part of opcodes, throw instead of
// undefined behavior.
template <typename T, typename... P>
Expand All @@ -524,6 +537,7 @@ class Compiler
ir.add_typed_id(static_cast<Types>(T::type), id);
auto &var = variant_set<T>(ir.ids[id], std::forward<P>(args)...);
var.self = id;
set_initializers(var);
return var;
}

Expand Down
Loading

0 comments on commit 42096ca

Please sign in to comment.