Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shader Translation #31

Closed
hlolli opened this issue Sep 14, 2015 · 8 comments
Closed

Shader Translation #31

hlolli opened this issue Sep 14, 2015 · 8 comments

Comments

@hlolli
Copy link
Member

hlolli commented Sep 14, 2015

This is not really an issue or error, just a minor bug. I was trying out the glsl translate functions and noticed how some of the parenthesis are translated wrongly. Maybe there's nothing wrong with this and my code is just wrong. I'll just post the code and maybe you can tell me what's wrong, translation or not.

So I wanted to take this shadertoy shader https://www.shadertoy.com/view/ldBGRR

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    vec2 p = -1.0 + 2.0 * fragCoord.xy / iResolution.xy;

// main code, *original shader by: 'Plasma' by Viktor Korsun (2011)
float x = p.x;
float y = p.y;
float mov0 = x+y+cos(sin(iGlobalTime)*2.0)*100.+sin(x/100.)*1000.;
float mov1 = y / 0.9 +  iGlobalTime;
float mov2 = x / 0.2;
float c1 = abs(sin(mov1+iGlobalTime)/2.+mov2/2.-mov1-mov2+iGlobalTime);
float c2 = abs(sin(c1+sin(mov0/1000.+iGlobalTime)+sin(y/40.+iGlobalTime)+sin((x+y)/100.)*3.));
float c3 = abs(sin(c2+cos(mov1+mov2+c2)+cos(mov2)+sin(x/1000.)));
fragColor = vec4(c1,c2,c3,1);

}   

And make the following translation function

(trans/defshader simple2
  '((uniform vec3 iResolution)
    (uniform float iGlobalTime)
    (defn void main []
      (setq vec2 p (- 1.0 (/ (* 2. gl_FragCoord.xy) iResolution.xy)))
      (setq float x p.x)
      (setq float y p.y)
      (setq float mov0 (+ x y (* 100. (cos (* 2.0 (sin iGlobalTime))))
                          (* 1000. (sin (/ x 100.)))))
      (setq float mov1 (+ iGlobalTime (/ y 0.9)))
      (setq float mov2 (/ x 0.2))
      (setq float c1 (abs (sin (+ (/ (+ mov1 iGlobalTime) 2.)
                                  (/ mov2 2.)
                                  (- mov1)
                                  (- mov2)
                                  iGlobalTime))))
      (setq float c2 (abs (sin (+ (+ c1 (sin (/ mov0 (+ iGlobalTime 1000.))))
                                  (sin (/ y (+ 40. iGlobalTime)))
                                  (sin (* (/ (+ x y) 100.) 3.0))))))
      (setq float c3 (abs (sin (+ c2
                                  (cos (+ mov1 mov2 c2))
                                  (cos mov2)
                                  (sin (/ x 1000.))))))
      (setq gl_FragColor (vec4 c1 c2 c3 1.0))))) 

It will output a shader, but not like the one on shadertoy, they look different. Also the print function prints out the following, making all these extra parenthesis suspicious.

uniform vec3 iResolution;
uniform float iGlobalTime;
void main(void) {
vec2 p = (1.0 - ((2.0 * gl_FragCoord.xy) / iResolution.xy));
float x = p.x;
float y = p.y;
float mov0 = (x + y + (100.0 * cos((2.0 * sin(iGlobalTime)))) + (1000.0 * sin((x / 100.0))));
float mov1 = (iGlobalTime + (y / 0.9));
float mov2 = (x / 0.2);
float c1 = abs(sin((((mov1 + iGlobalTime) / 2.0) + (mov2 / 2.0) + (mov1) + (mov2) + iGlobalTime)));
float c2 = abs(sin(((c1 + sin((mov0 / (iGlobalTime + 1000.0)))) + sin((y / (40.0 + iGlobalTime))) + sin((((x + y) / 100.0) * 3.0)))));
float c3 = abs(sin((c2 + cos((mov1 + mov2 + c2)) + cos(mov2) + sin((x / 1000.0)))));
gl_FragColor = vec4(c1,c2,c3,1.0);
}
@rogerallen
Copy link
Member

I'm not sure I understand. Is the issue basically that

float mov0 = x+y+cos(sin(iGlobalTime)*2.0)*100.+sin(x/100.)*1000.;

translates to

float mov0 = (x + y + (100.0 * cos((2.0 * sin(iGlobalTime)))) + (1000.0 * sin((x / 100.0))));

which is functionally the same, but has more parenthesis?

@hlolli
Copy link
Member Author

hlolli commented Sep 15, 2015

If this is functionally the same, I just must have written this shader wrongly somewhere in the code. And remind myself not to file a bug being hang over on sundays :)

@rogerallen
Copy link
Member

Okay. Examining the original code it is clear that shadertoy.com has moved on to a new format that doesn't match with what shadertone expects. Sigh...

If you try the code it will report this in stderr

shadertone.core=> (t/start "examples/bug.glsl")
nil
shadertone.core=> Loading shader from file: examples/bug.glsl
ERROR: Linking Shaders:
ERROR: No definition of main in fragment shader

Exception in thread "Thread-3111" java.lang.Exception: OpenGL Error(1282):GL_INVALID_OPERATION: @ end of let init-shaders
at shadertone.shader$except_gl_errors.invoke(shader.clj:115)
at shadertone.shader$init_shaders.invoke(shader.clj:287)
at shadertone.shader$init_gl.invoke(shader.clj:467)
at shadertone.shader$run_thread.invoke(shader.clj:725)
at shadertone.shader$start_shader_display$fn__10365.invoke(shader.clj:935)
at clojure.lang.AFn.run(AFn.java:24)
at java.lang.Thread.run(Thread.java:745)

@rogerallen
Copy link
Member

Changing the code to match expectations for the version of GLSL shadertoy expects, it seems to work just fine.

void main(void)
{
    vec2 p = -1.0 + 2.0 * gl_FragCoord.xy / iResolution.xy;

    // main code, *original shader by: 'Plasma' by Viktor Korsun (2011)
    float x = p.x;
    float y = p.y;
    float mov0 = x+y+cos(sin(iGlobalTime)*2.0)*100.+sin(x/100.)*1000.;
    float mov1 = y / 0.9 +  iGlobalTime;
    float mov2 = x / 0.2;
    float c1 = abs(sin(mov1+iGlobalTime)/2.+mov2/2.-mov1-mov2+iGlobalTime);
    float c2 = abs(sin(c1+sin(mov0/1000.+iGlobalTime)+sin(y/40.+iGlobalTime)+sin((x+y)/100.)*3.));
    float c3 = abs(sin(c2+cos(mov1+mov2+c2)+cos(mov2)+sin(x/1000.)));
    gl_FragColor = vec4(c1,c2,c3,1);

}

@rogerallen
Copy link
Member

Okay apparently you already figured this out when you did the translation since it matches what I wrote...

Alright, the real issue is that there is no unary negate in my translator. Converting (- mov1) to `(* -1. mov1) made it better. I also got it to work after adjusting the code for various issues in translation. This version seems to work for me:

(trans/defshader simple2
  '((uniform vec3 iResolution)
    (uniform float iGlobalTime)
    (defn void main []
      (setq vec2 p (- (/ (* 2. gl_FragCoord.xy) iResolution.xy) 1.0))
      (setq float x p.x)
      (setq float y p.y)
      (setq float mov0 (+ x y (* 100. (cos (* 2.0 (sin iGlobalTime))))
                          (* 1000. (sin (/ x 100.)))))
      (setq float mov1 (+ iGlobalTime (/ y 0.9)))
      (setq float mov2 (/ x 0.2))
      (setq float c1 (abs (+ (/ (sin (+ mov1 iGlobalTime) ) 2.)
                             (/ mov2 2.)
                             (* -1. mov1)
                             (* -1. mov2)
                             iGlobalTime)))
      (setq float c2 (abs (sin (+ c1
                                  (sin (+ (/ mov0 1000.) iGlobalTime))
                                  (sin (+ (/ y 40.) iGlobalTime))
                                  (* (sin (/ (+ x y) 100.)) 3.0)))))
      (setq float c3 (abs (sin (+ c2
                                  (cos (+ mov1 mov2 c2))
                                  (cos mov2)
                                  (sin (/ x 1000.))))))
      (setq gl_FragColor (vec4 c1 c2 c3 1.0)))))

(t/start (atom simple2))

@rogerallen
Copy link
Member

So, I think what does need to happen is:

  1. at least document the current translation steps necessary for shadertoy.com -> shadertone.
  2. handle or at least emit an error on unary negation.

rogerallen added a commit that referenced this issue Sep 21, 2015
Added unary math handling & unit test.  Adjusted README to note the
shadertoy.com syntax change.  Prep for a 0.2.6 release.
@rogerallen
Copy link
Member

I've updated the code. Let me know if I can do anything else.

@hlolli
Copy link
Member Author

hlolli commented Oct 10, 2015

it works perfectly now! Thanks.

mihetare pushed a commit to Viritystila/shadertone that referenced this issue Sep 30, 2017
Added unary math handling & unit test.  Adjusted README to note the
shadertoy.com syntax change.  Prep for a 0.2.6 release.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants