Page 1 of 4

Shader Help Thread

PostPosted: Mon Jul 24, 2017 3:28 am
by Nash
Beginner shader tutorial: https://gamedevelopment.tutsplus.com/tu ... -cms-23313

//////////////////////////////

Spoiler:

Re: Shader Help Thread

PostPosted: Mon Jul 24, 2017 4:49 am
by dpJudas
Texture sampling isn't that slow. SSAO samples 8/16/32 times per pixel depending on which quality you pick. After that it also blurs the entire thing, which requires even more sampling.

The expensive part is all the math it is doing. There are a few for loops: 8 iterations in turbulence, 2 iterations in fBM. That means the snoise function is called 16 times. snoise is an absolute monster doing 14 dot products and tons of other math operations too. That's 224 dot products right there.

You can't make this shader faster without simplifying the noise math. You could lower the number of iterations it is using, but that would make it look less good I'm sure. It might been possible to put the noise math into some kind of texture and sample the value from there, but that would require a deeper understanding of what snoise is doing.

If you had bigger control over the PP pipeline you could have generated a lower resolution texture with the result of this shader, and then used that as input to a second shader that combined it with the InputTexture contents. Unfortunately that option is out as you can't specify such a complex setup in the GLDEFS file today.

Re: Shader Help Thread

PostPosted: Mon Jul 24, 2017 5:08 am
by Nash
Understood. I suspected as much. The calculations are just too much, as nice as it looks. I've found a much cheaper, but also nice-looking water shader, so that's all good.

Re: Shader Help Thread

PostPosted: Mon Jul 24, 2017 11:13 am
by Rachael
I unstickied my thread. This thread is much more suitable for asking general shader questions, as is suggested by the title.

Re: Shader Help Thread

PostPosted: Mon Jul 24, 2017 4:07 pm
by Talon1024
Is there a way to use post-processing shaders on textures instead of the screen?

Re: Shader Help Thread

PostPosted: Mon Jul 24, 2017 4:35 pm
by Rachael
No.

You can, however, use fragment shaders on textures, but you won't have access to the uniform system.

Re: Shader Help Thread

PostPosted: Tue Jul 25, 2017 10:52 am
by Major Cooke
Nash wrote:These two shaders look pretty close to that special effect:

https://www.shadertoy.com/view/XlXXR4
https://www.shadertoy.com/view/llj3Dz


Thanks! I'll give these a try, though I'm still pouring over how to properly implement them.

Re: Shader Help Thread

PostPosted: Tue Jul 25, 2017 11:09 am
by Major Cooke
Another curiosity, would it be possible to have multiple of the same image created? Because similarly to water effects, or resembling that of stun effects I might want to try having the scene duplicated a couple of times each with their own transparency.

Also can someone be so kind as to update the wiki with the documentation on how to set this all up? (That is, for GZDoom itself and adding any notes upon what to do -- I am too thoroughly dumbfounded right now, so I am disqualified at the moment).

Putting this down here because it's important:

Code: Select allExpand view
void main()
{
    vec4 c = texture(InputTexture, TexCoord);
// INSERT CODE HERE.
    FragColor = c;
}


dpJudas wrote:you have to assign the 'c' variable to FragColor to have it output something
and strictly speaking you don't have to read from the input texture, but it sure will be a lame PP effect if it doesn't use any data from the scene texture

there are two varying in/out variables always: in vec2 TexCoord and out vec4 FragColor

Re: Shader Help Thread

PostPosted: Tue Jul 25, 2017 8:13 pm
by Major Cooke
So I'm trying to make a simple shader where all it does is double the RGB values of the screen on the right hand side (took this idea from this guide), but download the attached file and try to run it. It uh... royally fucks up by only showing one pixel.

Help?

Re: Shader Help Thread

PostPosted: Wed Jul 26, 2017 1:22 am
by Nash
Two things:

1) The wrong shader was called in your ZScript. Your shader's name is "colour", but in your ZScript it's calling the nonexistent shader "distort".
2) Change line 6 in your colour shader to vec2 xy = TexCoord; Explanation from dpJudas:

"where fragCoord contains the pixel coordinates for which the shader needs to compute a color. The coordinates are in pixel units, ranging from 0.5 to resolution-0.5, over the rendering surface, where the resolution is passed to the shader through the iResolution uniform (see below)."
that's from https://www.shadertoy.com/howto
however, TexCoord is in 0-1 range
you don't have to "vec2 uv = TexCoord.xy / texSize.xy;" because TexCoord is already scaled to 0-1


ShaderToy tip: Generally when you see something assigning something to fragCoord / [something], it's safe to omit the division and just replace fragCoord with TexCoord (because GZDoom doesn't recognize fragCoord). Also generally for this step, people seem to like naming the vec2 uv, so keep a look out for that.

Re: Shader Help Thread

PostPosted: Wed Jul 26, 2017 7:44 am
by Major Cooke
Wait a second, is distort already the name of an internal shader? That might explain why it wasn't working when I originally had it named "distort"...

Well I managed to get that working, so thanks Nash!

Also started working on the distortion shader... Not sure what else needs converting at this point.

Re: Shader Help Thread

PostPosted: Wed Jul 26, 2017 8:10 am
by dpJudas
No, there's no internal shader with that name. What Nash meant is that the name you give the shader in the GLDEFS file must match the shader name you pass into the ZScript SetUniform functions. If the name doesn't match it will not be setting the uniforms.

Re: Shader Help Thread

PostPosted: Wed Jul 26, 2017 12:10 pm
by Major Cooke
Well... Partially works.


Oh and Nash, I did disable the y flip. Still does that.

Re: Shader Help Thread

PostPosted: Wed Jul 26, 2017 4:50 pm
by Major Cooke
ZZYZX fixed it.

He really knows his stuff when converting from shadertoy to gzdoom. :mrgreen:

Re: Shader Help Thread

PostPosted: Wed Sep 06, 2017 2:36 pm
by Amuscaria
Has this been implemented into GZdoom yet? If so, how might I make use of it? Been looking for something likes this got a long time in Doom, now. :D