The following HLSL shader (without preprocessing) results in 65 arithmetic operations (see below). Can the same effect achieved with <=64 operations?
// Center of "light"
float x;
float y;
// Size of "light"
float viewsizeQ;
float fadesizeQ;
// Rotational shift
float angleShift;
// Resolution
float screenResolutionWidth;
float screenResolutionHeight;
float screenZoom;
// Texture sampler
sampler TextureSampler : register(s0);
float4 main(float2 texCoord : TEXCOORD0) : COLOR0
{
// Look up the texture color.
float4 color = tex2D(TextureSampler, texCoord);
// Calculate distance
float dx = (x-texCoord.x) * screenResolutionWidth;
float dy = (y-texCoord.y) * screenResolutionHeight;
// Get angle from center
// Added into next line to save that variable
//float angleFactor = sin((atan2(dx, dy) + angleShift) * 12) * 13000 * screenZoom * screenZoom;
float distQ = dx*dx + dy*dy - /*angleFactor*/ sin((atan2(dx, dy) + angleShift) * 12) * 13000 * screenZoom * screenZoom;
// Get greyscale
float greyscale = dot(color.rgb, float3(0.3, 0.59, 0.11));
// Within fadeSize
if (distQ < fadesizeQ)
{
// Within viewSize
if (distQ < viewsizeQ)
{
// Make blue-greyscale
color.r = 0.3f * greyscale;
color.g = 0.5f * greyscale;
color.b = 1.0f * greyscale;
}
// Outside viewSize but still in fadeSize
else
{
// Make
float factor = 1.0f - ((distQ - viewsizeQ) / (fadesizeQ - viewsizeQ));
color.r = factor * 0.3f * greyscale + (1-factor) * 0.2f * greyscale;
color.g = factor * 0.5f * greyscale + (1-factor) * 0.25f * greyscale;
color.b = factor * 1.0f * greyscale + (1-factor) * 0.2f * greyscale;
}
}
// Outside fadeSize (just show grey terrain)
else
{
color.rb = 0.2f * greyscale;
color.g = 0.25f * greyscale;
}
return color;
}
technique Thermal
{
pass Pass1
{
PixelShader = compile ps_2_0 main();
}
}
The following output is created by the Microsoft Shader Compiler:
C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Utilities\bin\x86>fxc 07_EffectThermalVisionLevel.fx /T ps_2_a /Zi
Microsoft (R) Direct3D Shader Compiler 9.29.952.3111
Copyright (C) Microsoft Corporation 2002-2009. All rights reserved.
//
// Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111
//
// fxc 07_EffectThermalVisionLevel.fx /T ps_2_a /Zi
//
//
// Parameters:
//
// sampler2D TextureSampler;
// float angleShift;
// float fadesizeQ;
// float screenResolutionHeight;
// float screenResolutionWidth;
// float screenZoom;
// float viewsizeQ;
// float x;
// float y;
//
//
// Registers:
//
// Name Reg Size
// ---------------------- ----- ----
// x c0 1
// y c1 1
// viewsizeQ c2 1
// fadesizeQ c3 1
// angleShift c4 1
// screenResolutionWidth c5 1
// screenResolutionHeight c6 1
// screenZoom c7 1
// TextureSampler s0 1
//
ps_2_x
def c8, 0.0208350997, -0.0851330012, 0.180141002, -0.330299497
def c9, 0.999866009, 0, 1, 3.14159274
def c10, -2, 1.57079637, 1.9098593, 0.5
def c11, 6.28318548, -3.14159274, 13000, 0
def c12, 0.300000012, 0.589999974, 0.109999999, 0.5
def c13, 0.200000003, 0.25, 0, 0
def c14, -1.55009923e-006, -2.17013894e-005, 0.00260416674, 0.00026041668
def c15, -0.020833334, -0.125, 1, 0.5
dcl t0.xy // texCoord<0,1>
dcl_2d s0
#line 26 "C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Utilities\bin\x86\07_EffectThermalVisionLevel.fx"
add r0.w, -t0.x, c0.x
mul r0.x, r0.w, c5.x // ::dx<0>
add r0.z, -t0.y, c1.x
mul r0.z, r0.z, c6.x // ::dy<0>
#line 33
abs r0.yw, r0.xxzz
max r1.w, r0.y, r0.w
rcp r1.x, r1.w
min r1.y, r0.w, r0.y
add r0.y, -r0.y, r0.w
cmp r0.y, r0.y, c9.y, c9.z
mul r0.w, r1.x, r1.y
mul r1.x, r0.w, r0.w
mad r1.y, r1.x, c8.x, c8.y
mad r1.y, r1.x, r1.y, c8.z
mad r1.y, r1.x, r1.y, c8.w
mad r1.x, r1.x, r1.y, c9.x
mul r0.w, r0.w, r1.x
mad r1.x, r0.w, c10.x, c10.y
mad r0.y, r1.x, r0.y, r0.w
cmp r0.w, r0.z, -c9.y, -c9.w
add r0.y, r0.w, r0.y
add r0.w, r0.y, r0.y
max r1.x, r0.x, r0.z
cmp r1.x, r1.x, c9.z, c9.y
min r1.y, r0.z, r0.x
mul r0.z, r0.z, r0.z
mad r0.x, r0.x, r0.x, r0.z
cmp r0.z, r1.y, c9.y, r1.x
mad r0.y, r0.z, -r0.w, r0.y
add r0.y, r0.y, c4.x
mad r0.y, r0.y, c10.z, c10.w
frc r0.y, r0.y
mad r0.y, r0.y, c11.x, c11.y
sincos r1.y, r0.y, c14, c15
mul r0.y, c7.x, c7.x
mul r0.y, r1.y, r0.y
mad r0.x, r0.y, -c11.z, r0.x // ::distQ<0>
#line 39
add r0.y, r0.x, -c3.x
#line 42
add r0.x, r0.x, -c2.x
#line 53
mov r1.x, c3.x // ::fadesizeQ<0>
add r0.z, r1.x, -c2.x
rcp r0.z, r0.z
mad r0.z, r0.x, -r0.z, c9.z // ::factor<0>
#line 23
texld r1, t0, s0 // ::color<0,1,2,3>
#line 36
dp3 r2.z, r1, c12 // ::greyscale<0>
#line 54
mul r0.w, r0.z, r2.z
add r2.w, -r0.z, c9.z
mul r2.w, r2.z, r2.w
mul r3.xy, r2.w, c13
mad r4.xy, r0.w, c12.xwzw, r3 // ::color<0,1>
mad r4.z, r0.z, r2.z, r3.x // ::color<2>
#line 42
mul r2.xy, r2.z, c12.xwzw
cmp r0.xzw, r0.x, r4.xyyz, r2.xyyz // ::color<0,1,2>
#line 63
mul r2.xyz, r2.z, c13.xyxw // ::color<0,1,0>
#line 39
cmp r1.xyz, r0.y, r2, r0.xzww // ::color<0,1,2>
mov oC0, r1 // ::main<0,1,2,3>