Skip to main content
GameDev.net gamedev.net
🔒 Locked

How to understand this GLSL shader?

Started by tracegame Mar 8, 2019 at 7:52 AM 5 replies 3.7k views
Original Post
tracegame
tracegame

https://glslfan.com/?channel=-L_NAXqFFl9RAZ2eBIaH

This is a web GLSL shader effect.

Maybe can't be loaded on some browser or old ones.

Anyway if your browser support compiled, it will show.

Full code here


#version 300 es
// - glslfan.com --------------------------------------------------------------
// Ctrl + s or Command + s: compile shader
// Ctrl + m or Command + m: toggle visibility for codepane
// ----------------------------------------------------------------------------
precision mediump float;
uniform vec2  resolution;     // resolution (width, height)
uniform vec2  mouse;          // mouse      (0.0 ~ 1.0)
uniform float time;           // time       (1second == 1.0)
uniform sampler2D backbuffer; // previous scene

out vec4 fragColor;

void main()
{
    int t = int(time*12.0);
	vec2 uv = (gl_FragCoord.xy*2.0-resolution.xy)/resolution.y;
	ivec2 p = ivec2(pow(abs(uv*32.0),vec2(0.4))*16.0);
	int ptn = int(time*0.1)%3; 
    int q = int[](p.x|p.y, p.x&p.y, p.x^p.y)[ptn];
    t = (t&63^t<<q*(t%9)) |(t&63|t<<q*(t&7)) ^ (t*2*(t&63|t<<q*(t&15)));
    float x = float((t>>0&15)+(t>>4&15)+(t>>8&15))*0.1;
    vec3 col = vec3(0.7,0.2,0.1)*fwidth(x);
    fragColor = vec4(col,1);
}

My question is there is a line.


int q = int[](p.x|p.y, p.x&p.y, p.x^p.y)[ptn];

Looks like dynamic array,but what does the content inside () mean?

Is it init list or something else?

And this code compile successfuly.

So I think the grammar is correct,but I never seen or learn anything about something like this.

Could someone explain why this is correct and what does it mean?

And is there some document or example teach or show you that you can use it like this?

Need more example



dave j
dave j
1 hour ago, tracegame said:

My question is there is a line.



int q = int[](p.x|p.y, p.x&p.y, p.x^p.y)[ptn];

Looks like dynamic array,but what does the content inside () mean?

Is it init list or something else?

It's an array constructor so the contents of the () are just the elements of the array.

The [ptn] at the end is accessing one of the elements of the array which is why q is an int.

Irusan, son of Arusan
Irusan, son of Arusan
1 hour ago, tracegame said:


int q = int[](p.x|p.y, p.x&p.y, p.x^p.y)[ptn];

Looks like dynamic array,but what does the content inside () mean?

It's equivalent to code that looks like this, but without branching:


int q;
if (ptn == 0)
  q = p.x | p.y;
else if (ptn == 1)
  q = p.x & p.y;
else if (ptn == 2)
  q = p.x ^ p.y;

So q is getting set to either the boolean OR of p.x and p.y, the boolean AND of p.x and p.y, or the boolean XOR of p.x and p.y depending on the value of ptn.

evelyn4you
evelyn4you
2 hours ago, Irusan, son of Arusan said:

It's equivalent to code that looks like this, but without branching:



int q;
if (ptn == 0)
  q = p.x | p.y;
else if (ptn == 1)
  q = p.x & p.y;
else if (ptn == 2)
  q = p.x ^ p.y;

So q is getting set to either the boolean OR of p.x and p.y, the boolean AND of p.x and p.y, or the boolean XOR of p.x and p.y depending on the value of ptn.


Hi Irusan,

"It's equivalent to code that looks like this, but without branching:"

Are you really shure ? I was supposed that dynamic arrays in shader code will internally alway be treated like your displayed code.
=> bad performance ??


Irusan, son of Arusan
Irusan, son of Arusan
57 minutes ago, evelyn4you said:

Are you really shure ? I was supposed that dynamic arrays in shader code will internally alway be treated like your displayed code.

You'd have to check with the output of the compiler, which is - IIRC - implemented by the graphics card driver and so could do anything it likes. But my assumption (ass, you, me, etc.) is that it would calculate the three values put them away and access them like an array - which involves no branch. As always whether it's faster or slower cannot be truly determined except by measurement on the target hardware. For Vulkan you could look at the bytecode produced by the compilation step into SPIR-V and see what it is doing. If you were so inclined.

tracegame
tracegame

Thank you all,with document I could use this happily and no worry.

Thank you all,with document I could use this happily and no worry.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.