シェーダの最適化を楽しむ

 id:ABAさんのシェーダについて、使用スロット数上の最適化を行ってみました。

struct PS_INPUT {
	float2 DiffuseDepth	: TEXCOORD1;
	float2 Texcoord		: TEXCOORD0;
};
struct PS_OUTPUT {
	float4 Color		: COLOR0;
};

float4 Color;

PS_OUTPUT AddEdgeAndLighting(PS_INPUT Input)
{
    PS_OUTPUT Output;
    float onLight = (Input.DiffuseDepth.x < 0.5)? 0.0:1.0;
    float btx = frac(Input.Texcoord.x);
    float bty = frac(Input.Texcoord.y);
    float hasBoard = (btx < 0.1 || btx > 0.9 || bty < 0.1 || bty > 0.9)? 0.0:1.0;
    float edgeThreshold = 0.001 * (100 + Input.DiffuseDepth.y);
    float cr = hasBoard * onLight * 0.5;
    cr = (btx < (0.1 + edgeThreshold))? cr * 1.3:cr;
    cr = (bty < (0.1 + edgeThreshold))? cr * 1.5:cr;
    cr = (btx > (0.9 - edgeThreshold))? cr * 0.7:cr;
    cr = (bty > (0.9 - edgeThreshold))? cr * 0.5:cr;
    Output.Color = Color * cr;
    Output.Color.a = Color.a;
    return Output;
}

PS_OUTPUT AddEdgeAndLightingX(PS_INPUT Input)
{
    PS_OUTPUT Output;
    float light = (Input.DiffuseDepth.x >= 0.5) * 0.5;
    float2 bt = frac(Input.Texcoord);
    float2 bound = (bt >= 0.1) * (bt <= 0.9);
    float edgeThreshold = 0.001 * (100 + Input.DiffuseDepth.y);
    float2 i;
    i  = (bt < (0.1 + edgeThreshold))? float2(1.3,1.5):float2(1,1);
    i *= (bt > (0.9 - edgeThreshold))? float2(0.7,0.5):float2(1,1);
    i *= bound;
    Output.Color.rgb = Color.rgb * ((i.x * i.y) * light);
    Output.Color.a = Color.a;
    return Output;
}

technique T {
	pass Original {
		// original   / ps_3_0: 28-slots | ps_2_0: 38-slots @ D3D10 Shader Compiler 9.19.949.0046
		PixelShader = compile ps_3_0 AddEdgeAndLighting();
	}
	pass XELFMod {
		// XELF's mod / ps_3_0: 20-slots | ps_2_0: 23-slots @ D3D10 Shader Compiler 9.19.949.0046
		PixelShader = compile ps_3_0 AddEdgeAndLightingX();
	}
}