SPIRV-Cross/reference/shaders-hlsl/frag/complex-expression-in-access-chain.frag
Hans-Kristian Arntzen acae607703 Register implied expression reads in OpLoad/OpAccessChain.
This is required to avoid relying on complex sub-expression elimination
in compilers, and generates cleaner code.

The problem case is if a complex expression is used in an access chain,
like:

Composite comp = buffer[texture(...)];
vec4 a = comp.a + comp.b + comp.c;

Before, we did not have common subexpression tracking for
OpLoad/OpAccessChain, so we easily ended up with code like:

vec4 a = buffer[texture(...)].a + buffer[texture(...)].b + buffer[texture(...)].c;

A good compiler will optimize this, but we should not rely on it, and
forcing texture(...) to a temporary also looks better.

The solution is to add a vector "implied_expression_reads", which works
similarly to expression_dependencies. We also need an extra mechanism in
to_expression which lets us skip expression read checking and do it
later. E.g. for expr -> access chain -> load, we should only trigger
a read of expr when using the loaded expression.
2019-01-04 14:56:12 +01:00

41 lines
966 B
GLSL

RWByteAddressBuffer _34 : register(u0);
Texture2D<int4> Buf : register(t1);
SamplerState _Buf_sampler : register(s1);
static float4 gl_FragCoord;
static int vIn;
static int vIn2;
static float4 FragColor;
struct SPIRV_Cross_Input
{
nointerpolation int vIn : TEXCOORD0;
nointerpolation int vIn2 : TEXCOORD1;
float4 gl_FragCoord : SV_Position;
};
struct SPIRV_Cross_Output
{
float4 FragColor : SV_Target0;
};
void frag_main()
{
int4 coords = Buf.Load(int3(int2(gl_FragCoord.xy), 0));
float4 foo = asfloat(_34.Load4((coords.x % 16) * 16 + 0));
int c = vIn * vIn;
int d = vIn2 * vIn2;
FragColor = (foo + foo) + asfloat(_34.Load4((c + d) * 16 + 0));
}
SPIRV_Cross_Output main(SPIRV_Cross_Input stage_input)
{
gl_FragCoord = stage_input.gl_FragCoord;
vIn = stage_input.vIn;
vIn2 = stage_input.vIn2;
frag_main();
SPIRV_Cross_Output stage_output;
stage_output.FragColor = FragColor;
return stage_output;
}