Jump to content
  • Advertisement
tracegame

OpenGL ES How to understand this GLSL shader?

Recommended Posts

Posted (edited)

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

 

 

Edited by tracegame

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ??

 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!