Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimization of HLSL shader
    text
    copied!<p>I have the problem that the below pixel shader (HLSL) compiles to <strong>68</strong> instructions (with the below suggested optimizations). However, I would like to use it with shader model 2 and therefore unfortunately I can only use up to <strong>64</strong> instructions. Does anyone see any possible optimizations without changing the result of the shader?</p> <p>The shader transforms a more-or-less spherical region of the screen (with sinus-shaped borders) from RGB to a gradient of white -> red -> black with some additional brightness etc. modifications.</p> <p>The shader code is: </p> <pre><code>// Normalized timefactor (1 = fully enabled) float timeFactor; // Center of "light" float x; float y; // Size of "light" float viewsizeQ; float fadesizeQ; // Rotational shift float angleShift; // Resolution float screenResolutionWidth; float screenResolutionHeight; float screenZoomQTimesX; // Texture sampler sampler TextureSampler : register(s0); float4 method(float2 texCoord : TEXCOORD0) : COLOR0 { // New color after transformation float4 newColor; // Look up the texture color. float4 color = tex2D(TextureSampler, texCoord); // Calculate distance float2 delta = (float2(x, y) - texCoord.xy) * float2(screenResolutionWidth, screenResolutionHeight); // Get angle from center float distQ = dot(delta, delta) - sin((atan2(delta.x, delta.y) + angleShift) * 13) * screenZoomQTimesX; // Within fadeSize if (distQ &lt; fadesizeQ) { // Make greyscale float grey = dot(color.rgb, float3(0.3, 0.59, 0.11)); // Increase contrast by applying a color transformation based on a quasi-sigmoid gamma curve grey = 1 / (1 + pow(1.25-grey/2, 16) ); // Transform Black/White color range to Black/Red/White color range // 1 -&gt; 0.5f ... White -&gt; Red if (grey &gt;= 0.75) { newColor.r = 0.7 + 0.3 * color.r; grey = (grey - 0.75) * 4; newColor.gb = 0.7 * grey + 0.3 * color.gb; } else // 0.5f -&gt; 0 ... Red -&gt; Black { newColor.r = 1.5 * 0.7 * grey + 0.3 * color.r; newColor.gb = 0.3 * color.gb ; } // Within viewSize (Full transformation, only blend with timefactor) if (distQ &lt; viewsizeQ) { color.rgb = lerp(newColor.rgb, color.rgb, timeFactor); } // Outside viewSize but still in fadeSize (Spatial fade-out but also with timefactor) else { float factor = timeFactor * (1 - (distQ - viewsizeQ) / (fadesizeQ - viewsizeQ)); color.rgb = lerp(newColor.rgb, color.rgb, factor); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload