Effects: Coding Convolutions & Shaders in general?

Forum for plugin, effect and behavior programmers. Please use the Help & Support forum for help using Construct.

Post » Tue Aug 24, 2010 1:06 am

I have a few question regarding effects coding, (which I'll number for ease of reference).

[color=#000080:1l6pu9nq]Q1: How much do I need to know about the pixel shader 2.0 limitations to code shaders that rely on convolutions?[/color:1l6pu9nq]

[color=#000080:1l6pu9nq]Q2: Is there any good online reference for what you can and can't do in pixel shader 2.0. [/color:1l6pu9nq]

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.

[color=#000080:1l6pu9nq]Q3: Is DirectX's "HLSL" language what construct shaders are coded in? (i.e. Will this Wikipedia page "[url:1l6pu9nq]http://en.wikipedia.org/wiki/HLSL[/url:1l6pu9nq]" help me at all?)[/color:1l6pu9nq]

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.

[color=#000080:1l6pu9nq]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?[/color:1l6pu9nq]
B
21
S
13
G
8
Posts: 306
Reputation: 5,458

Post » Tue Aug 24, 2010 4:27 am

I don't know very much on the subject, but i'm pretty sure you can't do multiple passes.
B
25
S
3
G
6
Posts: 1,197
Reputation: 5,620

Post » Tue Aug 24, 2010 4:33 am

[quote="fisholith":2jmdq3ko][color=#000080:2jmdq3ko]Q1: How much do I need to know about the pixel shader 2.0 limitations to code shaders that rely on convolutions?[/color:2jmdq3ko][/quote:2jmdq3ko]
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.

[quote="fisholith":2jmdq3ko][color=#000080:2jmdq3ko]Q2: Is there any good online reference for what you can and can't do in pixel shader 2.0. [/color:2jmdq3ko][/quote:2jmdq3ko]
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 ;)

[quote="fisholith":2jmdq3ko][color=#000080:2jmdq3ko]Q3: Is DirectX's "HLSL" language what construct shaders are coded in? [/color:2jmdq3ko][/quote:2jmdq3ko]
See above :)

[quote="fisholith":2jmdq3ko][color=#000080:2jmdq3ko]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?[/color:2jmdq3ko][/quote:2jmdq3ko]
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
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Tue Aug 24, 2010 12:14 pm

Thanks! :D
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. :D
B
21
S
13
G
8
Posts: 306
Reputation: 5,458

Post » Tue Aug 24, 2010 6:47 pm

[quote="fisholith":1qvuvolp]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.[/quote:1qvuvolp]
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.

[code:1qvuvolp]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.

for-statement reference
if-statement reference
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242


Return to Construct engineering

Who is online

Users browsing this forum: No registered users and 0 guests