Effects: Coding Convolutions & Shaders in general?

This forum is currently in read-only mode.
From the Asset Store
5 levels with simple coding Source-code (.c3p) + HTML5 Exported
  • I have a few question regarding effects coding, (which I'll number for ease of reference).

    Q1: How much do I need to know about the pixel shader 2.0 limitations to code shaders that rely on convolutions?

    Q2: Is there any good online reference for what you can and can't do in pixel shader 2.0.

    I've read through the Construct Wiki page on shaders, and that helped with the Construct specific stuff, but it doesn't mention much about the actual language.

    Q3: Is DirectX's "HLSL" language what construct shaders are coded in? (i.e. Will this Wikipedia page "http://en.wikipedia.org/wiki/HLSL" help me at all?)

    While I'm familiar with a variety of image compositing and processing algorithms, I have no background in coding pixel shaders. I'm not even sure if "pixel shader 2.0" is a language or a target machine code standard, or what.

    There are a number of effects I'm interested in building, and several rely on various forms of convolution, and particularly blurs.

    After spending just a little time coding effects, I've run into a number of errors resulting from registry allocation, overflows, and illegal computation complexity. Which reminds me of my last question.

    Q4: Is it possible to make shaders that distribute their computation complexity over multiple passes, if the algorithm is to complex for a single pass?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I don't know very much on the subject, but i'm pretty sure you can't do multiple passes.

  • Q1: How much do I need to know about the pixel shader 2.0 limitations to code shaders that rely on convolutions?

    Basically, the only limitation you need to be aware of is that you only have 64 instruction slots. Esp for convolution effects it will limit you too much. You may get away with a 3x3 matrix, although I doubt it.

    Q2: Is there any good online reference for what you can and can't do in pixel shader 2.0.

    I suggest you use the official HLSL reference. You just need to accept that with Construct's implementation not all intrinsic functions work, even if they are said to work under ps 2.0 (e.g noise). You won't get any error messages then, it seems as if they just got lost in the dark universe of dead bits

    Q3: Is DirectX's "HLSL" language what construct shaders are coded in?

    See above

    Q4: Is it possible to make shaders that distribute their computation complexity over multiple passes, if the algorithm is to complex for a single pass?

    No, Construct limits you to single pass.

    EDIT: 1 addition, "pixel shader 2.0" is a profile, this links explains what it means: http://msdn.microsoft.com/en-us/library ... 85%29.aspx

  • Thanks!

    I've been looking over the Microsoft HLSL reference site you mentioned.

    I'm glad I asked before investing a lot of time in trying to build a bunch of convolution stuff.

    I've built a few shaders that are a bit more straightforward thus far, and I'm getting a feel for HLSL. (albeit rather slowly)

    Do you know off hand if loops are unrolled at compilation? Or, likewise, if all the instructions in the branches of an "if" statement count as taking up instruction slots? It seems like they must, but I'm not sure what kind of optimization happens at compile time.

    Thanks again.

  • Do you know off hand if loops are unrolled at compilation? Or, likewise, if all the instructions in the branches of an "if" statement count as taking up instruction slots? It seems like they must, but I'm not sure what kind of optimization happens at compile time.

    There is also a time factor. If a loop uses only the allowed amount of instruction slots but is too time expensive, you will get an error too.

    If-statements count for every branch. A loop is one instruction plus the ones you enclose with the loop.

    float4 EffectProcess( float2 Tex : TEXCOORD0 ) : COLOR0
    {
        float4 color = tex2D(foreground, Tex);
    	for (int i = 0; i < 1023; i++)
    	{
    		color *= 0.9999;
    	}
    	
        return color;
    }[/code:1qvuvolp]
    This code uses 3 instruction slots, being a loop with 1024 iterations.
    
    But as soon as you insert a simple if-statement you are using 3 instruction slots per iteration (the if-branch, the calculation and the not typed but still existent else-branch)
    [code:1qvuvolp]float4 EffectProcess( float2 Tex : TEXCOORD0 ) : COLOR0
    {
        float4 color = tex2D(foreground, Tex);
    	for (int i = 0; i < 21; i++)
    	{
    		if (color.a != 0)
    		{
    			color *= 0.9999;
    		}
    	}
    	
        return color;
    }[/code:1qvuvolp]
    See? You now have to cap the loop to 21, which equals 21 * 3 = 63 instruction slots (plus the color assignment one line above)
    
    But be careful with extensive loops. You will use too much RAM. Better don't try the following example (I used the task manager to exit Construct after about 2 GB of RAM were assigned, and it took a very long time to get there):
    [code:1qvuvolp]float4 EffectProcess( float2 Tex : TEXCOORD0 ) : COLOR0
    {
        float4 color = tex2D(foreground, Tex);
    	for (int i = 0; i < 1023; i++)
    	{
    		for (int j = 0; j < 64; j++)
    		{
    			color *= 0.9995;
    			color /= 0.9995;
    		}
    	}
    	
        return color;
    }[/code:1qvuvolp]
    
    I'm not sure if attribute assignment is supported in Construct, I never tried it. HLSL generally allows you to add attributes to for- or if-statements (like [unroll] for for-statements or [flatten] for if-statements) to control compilation, but maybe Construct is taken care of that. You would need to try I'm afraid.
    
    [url=http://msdn.microsoft.com/en-us/library/bb509602%28v=VS.85%29.aspx]for-statement reference[/url]
    [url=http://msdn.microsoft.com/en-us/library/bb509610%28v=VS.85%29.aspx]if-statement reference[/url]
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)