-1

I would like to have a very simple explanation WHY these variables doesn't work on "Android Studio" and how to solve my problem(some work on "TheeBookOfShaders", some work on "Atom", others work on both, some work only on "ShaderToy" and some only work on "Android Studio").


* To really understand, this is a sample (from a "fragment.glsl" file) *

uniform vec2 resolution;     // [-] work on...
uniform vec2 uresolution;    // [-] work on...
uniform vec2 iresolution;    // [Y] work only on "ShaderToy"
uniform vec2 u_resolution;   // [Y] work on "Atom" and "WebGL"

i.e. * Sample Conversion FROM "ShaderToy" TO "Atom" (live coding)*

uniform vec2 iresolution;    // is used on: "ShaderToy"
uniform vec2 u_resolution;   // is used on: "Atom", "WebGL", etc.

so: [iresolution = u_resolution] * OK It works *


* Well, now, why in "Android Studio" (java code + fragment.glsl) no one of these it works? *

   uniform vec2 resolution;     // doesn't work on "Android Studio"
   uniform vec2 uresolution;    // doesn't work on "Android Studio"
   uniform vec2 iresolution;    // doesn't work on "Android Studio"
   uniform vec2 u_resolution;   // doesn't work on "Android Studio"
   uniform vec2 vresolution;    // doesn't work on "Android Studio"
   uniform vec2 v_resolution;   // doesn't work on "Android Studio"

and obviously:

   vec2 A = (gl_FragCoord.xy / u_resolution);     // doesn't work on "Android Studio"
   vec2 A = (gl_FragCoord.xy / uresolution);      // doesn't work on "Android Studio"
   vec2 A = (gl_FragCoord.xy / *SOME*resolution); // doesn't work on "Android Studio"

etc.


Same situation about the time var: time, utime, u_time, itime, vtime, v_time, globalTime, etc.


* Where do I can find the exact keyword to use RESOLUTION/TIME/others system-var in "Android Studio" GLSL shader file? *

  • "resolution" is there a currently defined reference table to understand how to convert system variables?

  • "resolution" is it a system-lib variable or not?

  • "Xresolution" is there a simple final real scheme to understand something in this confusion?


in this sample, we can see the ONLY work version of Xresolution - try at home

in this other sample, we can see the ALL THE OTHERS yellow-failure versions of Xresolution - try at home


The "fragment.glsl" test-file work 100% on Atom-Editor (try at home please)

#ifdef GL_ES
    precision highp float;
#endif

uniform vec2 resolution;    // not-system var
uniform vec2 uresolution;   // not-system var
uniform vec2 iResolution;   // system-var WORK 100% on ShaderToy
uniform vec2 vresolution;   // not-system var
uniform vec2 u_resolution;  // system-var WORK 100% on Atom-Editor but NOT on Android Studio
uniform vec2 i_resolution;  // not-system var
uniform vec2 v_resolution;  // not-system var

void main()
{
    vec2 A = (gl_FragCoord.xy / u_resolution);

    gl_FragColor = vec4(A.x, A.y, 0.0, 1.0);
}

* SOLUTION | WORK 100% ONLY ON ANDROID STUDIO *

#ifdef GL_ES
    precision highp float;
#endif

uniform vec2 u_resolution; // note: you can name it also "Pacman"...
                           // this mode let you can to create your
                           // personal var-name to access to windows view-port

void main()
{
    // ---------------------------------------------------------------------------------
    u_resolution = vec2(1920, 1080); // this assignment work 100% ONLY on Android Studio
    // ---------------------------------------------------------------------------------

    // --------------------------------------------------------------------------
    vec2 A = (gl_FragCoord.xy / u_resolution);                      // solution 1
    vec2 A = (gl_FragCoord.xy / vec2(1920, 1080));                  // solution 2
    vec2 A = (vec2(gl_FragCoord.x / 1920, gl_FragCoord.y / 1080));  // solution 3
    // --------------------------------------------------------------------------

    gl_FragColor = vec4(A.x, A.y, 0.0, 1.0);
}

Finally we found the solution, always before our eyes. We start from a window of which we have the dimensions of X and Y set to 1920x1080 (in our case we do not need anything else) and I point out 3 modes of setting the variable "u_resolution". WARNING - this feature works ONLY in Android Studio and is able to answer my questions above. The problem has been solved. Felipe showed his commitment to solving the problem by getting involved. Of course we can also set this value from the main-code via Java or C ++ or other; but to us, in this post, it was only interesting to set/retrieve these "u_resolution" directly via/from GLSL.


The solution adopted perfectly meets the needs of departure, and I hope it will be helpful to all those who come after me. The 3 line solution are equivalent: choose your preferred


A special thank to @felipe-gutierrez for his kind cooperation.

M.C.
  • 37
  • 9
  • I haven't used Android Studio, but uniforms are your way of communicating your host (CPU part of your app) with the GPU, you have to explicitly declare them according to your own needs but as I learnt shader programming from shadertoy as well I keep the same variable-uniform names you can see some C++ that shouldn't be so hard to port to Java I guess? https://github.com/felipunky/NavierStokes/tree/master/NavierStokes/LearnOpenGL – Felipe Gutierrez Dec 04 '18 at 00:33
  • Hello, well indeed the word is different. I'll explain: in ShaderToy you use the variable "iResolution" and think it's like in C ++: where if you want to create a variable seriously, you must first reserve some space in memory, then state it by giving it a high-level name, then initialize it finally you can use it. In GLSL the thing is not the same. Always speaking from the profane of GLSL, declare a line of code "uniform vec3 iResolution;" it means that we are using a token called "iResolution" within the preloaded system LIB. – M.C. Dec 04 '18 at 00:58
  • In practice it is like declaring that you want to use the "printf" command before you can use it, and you have to do it correctly. In fact, in ShaderToy, "iResolution" is pre-registered in its command library, but not in ours that we use for example Atom-Editor. In fact "iResolution" is directly connected to the retrieve of the management of the screen coordinates. So in theory it could also be called "whatAmother" but if I do not declare it - that is, if I do not say I want to use it - the program will not allow me to access this var. – M.C. Dec 04 '18 at 00:59
  • Do you understand why there is confusion? Because thanks to this system every one uses his token and I'm a little lost, because I can not find in my system the word related to "iResolution" that allows me to get the retrieve of the coordinates of the screen. Because GLSL is not a simple language like the wonderful C ++, but uses a different paradigm of approach to pierce the graphic pipeline ... anyway thanks for intervening with your answer, you're kind. – M.C. Dec 04 '18 at 00:59
  • Exactly! The important thing is that you don't abuse the bus traffic from CPU to GPU or you will loose performance so just use it for input that you can't get otherwise or if you wan't to save up some computations of some heavy function as in this example https://www.shadertoy.com/view/lsyfWy – Felipe Gutierrez Dec 04 '18 at 01:06
  • it's the same in C++ write a code like this: "iWantUseThis printf = null;" - then you can start to use it :) – M.C. Dec 04 '18 at 01:12
  • Ok, someone have the right command line to start to use that "Xresolution" in my Android Studio, please? – M.C. Dec 04 '18 at 01:14

2 Answers2

3

NONE of the GLSL variables you mentioned are system vars

They are user made up variables.

uniform vec2 resolution;

has absolutely no more meaning than;

uniform vec2 foobar;

Those are variables chosen by you.

You set them by looking up their location

In WebGL/JavaScript

const resolutionLocation = gl.getUniformLocation(someProgram, "resolution");
const foobarLocation = gl.getUniformLocation(someProgram, "foobar");

In Java

int resolutionLocation = GLES20.glGetUniformLocation(mProgram, "resolution");
int foobarLocation = GLES20.glGetUniformLocation(mProgram, "foobar");

You set them in WebGL/JavaScript

gl.useProgram(someProgram);  
gl.uniform2f(resolutionLocation, yourVariableForResolutionX, yourVariableForResolutionY);
gl.uniform2f(foobarLocation, yourVariableForFoobarX, yourVariableForFoobarY);

or Java

GLES20.glUseProgram(someProgram);  
GLES20.glUniform2f(resolutionLocation, yourVariableForResolutionX, yourVariableForResolutionY);
GLES20.glUniform2f(foobarLocation, yourVariableForFoobarX, yourVariableForFoobarY);

There is no magic system vars, they are 100% your app's variables. iResolution is a variable that the programmers of ShaderToy made up. u_resolution is a variable that some plugin author for Atom made up. They could have just as easily chosen renderSize or gamenHirosa (japanese for screen width), or anything. Again, they are not system vars, they are variables chosen by the programmer. In your app you also make up your own variables.

I suggest you read some tutorials on WebGL

gman
  • 100,619
  • 31
  • 269
  • 393
  • Thank you for participating @gman, all your information has already been evaluated before creating this post. The solution, which responds exactly to the needs of the moment, was included at the end of the post. – M.C. Dec 04 '18 at 18:44
1

According to the Khronos site: "A uniform is a global GLSLvariable declared with the "uniform" storage qualifier. These act as parameters that the user of a shader program can pass to that program. They are stored in a program object. Uniforms are named so because they do not change from one execution of a shader program to the next within a particular rendering call. This makes them unlike shader stage inputs and outputs, which are often different for each invocation of a program stage." So in other words it's a variable that you create in your host that you can access in your OpenGL program (Vertex and Fragment Shaders) but that you can't modify directly, so for example you get the resolution of your window in Java or C++ or Javascript or *** then you input it in Shadertoy's convention as iResolution, or your mouse position and left click (iMouse.xyz) and you pass it as a Uniform to your fragment shader. They are useful for input that isn't too heavy, as you may have seen in Shadertoy, videos are pased as textures, like your webcam or Van Dammes' clip, you can even pass sound as input, for more advanced effects you can pass one shader program into another for things like additive blending or Ping-Pong as BufferA or B or C or D in Shadertoy.

You can see what they stand for from the inputs to the shader on the top part of the editor on the shadertoy site, Shadertoy Inputs and here you can check how I got many of the same inputs that shadertoy uses in C++ and unfortunately not plain Java but Processing

If you want to test that you have the correct iResolution uniform then you can type:

void main()
{

    vec2 uv = gl_FragCoord.xy/u_resolution;

    vec3 col = vec3( smoothstep( 0.1, 0.1 - 0.005, length( uv - 0.5 ) ) );

    gl_FragColor = vec4( col, 1 );

}

And you should see the ellipse at the center of the screen. Proper Input

Felipe Gutierrez
  • 675
  • 6
  • 17
  • Hi Felipe, are you able to tell me the GLSL equivalent of a file "fragment.glsl" that can use as i want from C++ or Java, from this ShaderToy's line of code? ----------------------------------------------------------------- "vec2 AB = (gl_FragCoord.xy / iResolution);" == "vec2 AB = (gl_FragCoord.xy / ?????????);" ----------------------------------------------------------------- What kind of "xResolution" you say i should to do? p.s. your info and your images is not new for me. I came here from there and others like that... – M.C. Dec 04 '18 at 01:06
  • *gl_FragCoord — contains the window-relative coordinates of the current fragment* to normalize the coords from 0 to 1 we do: gl_FragCoord.xy / iResolution where iResolution.x is the screen's width and iResolution.y is the screen's height. In that way we can get a screen that is not dependant on the screen's size. How to set the vec2 from your Android Studio: https://stackoverflow.com/questions/35780980/getting-the-actual-screen-height-android – Felipe Gutierrez Dec 04 '18 at 01:26
  • This method seems convenient but as I stated I have not tried myself: int width=Resources.getSystem().getDisplayMetrics().widthPixels; int height=Resources.getSystem().getDisplayMetrics().heightPixels; – Felipe Gutierrez Dec 04 '18 at 01:31
  • Then you input as uniform as: float[] myVector = {f1,f2,....,f(N*2)}; GLES20.glUniform2f(myVariablePosition,N,myVector); – Felipe Gutierrez Dec 04 '18 at 01:34
  • Very interesting method Felipe, thanks, but is not what i need, because you reach the GLSL from your JAVA file, and i don't want this only for this simple retrieve. As you cann see from the 2 pictures i've added right now, only within "u_resolution" in your Atom-Editor of any other editors or WebGl, you can pierce the graphic pipeline in the right mode. All the others generate only a simple yellow result from a simple var-name... not a system-lib token var name. Is THAT what i need. Only in Android Studio doens't work - Tonight maybe i will shoot to my keyboard... – M.C. Dec 04 '18 at 01:45
  • i've added also a work code to better understand what i mean... (thanks for your cooperation) – M.C. Dec 04 '18 at 01:52
  • the unique problem is that THIS work with all the systems but not within Android Studio *.jar, *.apk file – M.C. Dec 04 '18 at 01:54
  • Let me get my hands on Android Studio, I am downloading it already. You welcome. – Felipe Gutierrez Dec 04 '18 at 02:03
  • I've tested on shadertoy: u_resolution is the correct vec2 uniform. – Felipe Gutierrez Dec 04 '18 at 02:16
  • I've updated the answer, so that you can verify but I'm pretty sure that *u_resolution* is the uniform you want. – Felipe Gutierrez Dec 04 '18 at 02:25
  • I'm almost sure that "u_resolution"(lower) is the uniform name also on Android Studio but still doesn't work... and i've serched for that token in the win system: it doesn't match. ABOLUTELY ABSURD my friend... absurd! – M.C. Dec 04 '18 at 02:36
  • Android Studio doesn't recognize "u_resolution" like as internal token system uniform word! But only as a simple personal var like "banana" or "michaelJackson" - and the screen appear yellow just like as image i posted up there. Try to use my simple "fragment.glsl" file within u_resolution uniform... if you will have YELLOW result, you will have the same problem – M.C. Dec 04 '18 at 02:40
  • Mmm let me dig around Android Studio, I though that the picture you uploaded of the gradient was using u_resolution and the same code gave me the same output as your pic in shadertoy so I guessed it was the one – Felipe Gutierrez Dec 04 '18 at 02:47
  • I've tried your code above and the result is the same: doesn't work with Android Studio... (it work only with the rest of live-editors) - Yes, try to exchange the uniform's vars in your [vec2 A] assignment to reply the problem on your systems to better understand why only Android Studio don't recognize that system-var... – M.C. Dec 04 '18 at 03:09
  • Yes as soon as I get my hands on AStudio I will. I found no documentation online for the shading pipeline. – Felipe Gutierrez Dec 04 '18 at 03:18
  • The solution, which responds exactly to the needs of the moment and work 100% just only in Android Studio, was included at the end of the post, thanks for your intense cooperation! – M.C. Dec 04 '18 at 18:54