# OpenGLES texture coordinates in shader

Currently I'm doing an android 2D game using OpenGLES 2.0 and I noticed that my UVs never change in all of textures when rendering, like this in Java code:

    public static FloatBuffer vertexUv;

public static void setUpUV()
{
float uvs[] = {0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f
};
ByteBuffer bb = ByteBuffer.allocateDirect(uvs.length*4);
bb.order(ByteOrder.nativeOrder());
vertexUv = bb.asFloatBuffer();
vertexUv.put(uvs);
vertexUv.position(0);
}
...
//UV sending in render
int mTexCoordLoc = glGetAttribLocation(program, "a_texCoord");
glEnableVertexAttribArray(mTexCoordLoc);
glVertexAttribPointer(mTexCoordLoc, 2, GL_FLOAT, false, 0, vertexUv);
...


The problem is: even the UVs never change, I always sends via Java code to the GPU causing waste of CPU using VBOs or not(I tested it using OpenGLES tracer). All textures of my game is in 2D mode so I believe that I don't need send it every texture rendering.

What I need to do? I tried to put the UVs in the shader code (I believe that is the way) but without success... more or less like this: (I tested more alternatives in the shader/fragment shader too... below is only a example of my idea to solve this problem)

    //Vertex shader
var v_texCoord= var(0.0, 0.0,
0.0, 1.0,
1.0, 1.0,
1.0, 0.0
); //Could I init this in the first vertex processing?
varying vec2 v_texCoord;
attribute vec4 vPosition;
void main()
{
gl_Position = vPosition;"
v_texCoord  = v_texCoord;"   //To fragment shader
}


Question: is possible put statically UVs in shader?

Performance:
Each blue region represents ONE texture rendering from a FRAME. Maybe its a minimal and unnecessary optimization...

Thank you!!

• I'm currently not at home so I can't write a full answer but: make sure to call setUpUV only once. And instead of glVertexAttribPointer(mTexCoordLoc, 2, GL_FLOAT, false, 0, vertexUv); you could (and should) upload your buffer to a VBO.More info here. Also, you probably shouldn't worry too much about 16 floats being sent per drawcall, premature optimisation is the root of all evil, but take this with a grain of salt – Folkert Hoogenraad Dec 16 '16 at 14:43
• Hi! The method setUpUV() is called only one time. I will change my buffers to VBO! I edited my question with the screenshot that show the performance log. – Bruno Gonçalves Dec 16 '16 at 16:47

Usually, the fragment shader takes the UV value for the current fragment as an input from the vertex shader:

out vec2 UV;

void main(){
...

UV = vertexUV;
}


in vec2 UV;

uniform sampler2D myTextureSampler;

void main(){

color = texture( myTextureSampler, UV ).rgb;
}


even the UVs never change

They do not change from drawcall to drawcall. But in the end the rasterizer interpolates the UV value for the current fragment from the UV values of the vertices of the current triangle, following the information provided by the vertex shader. And those values can change from vertex to vertex .

I don't see how you would be able to deal with that in the fragment shader alone. You provide 4 UVs and look at 3 vertices for each triangle. How would you figure out what UVs to use and how would you compute the value for the current fragment?? In the end that is what the vertex shader and rasterizer combination is for, isn't it?

Also, why do you want to do that at all? The amount of data that are being transferred here seem too small to me to be worth the effort...

• Sorry for all that editing, deleting, undeleting. The more I thought of my answer, the more i realized how false it was. – Djindjidj Dec 16 '16 at 15:02
• Hi. Thank for you reply. I forget that UVs change in fragment shader on the rasterization! My example of my shader is really non sense hehehehehe. Anyway, can I put the fixed UVs in the vertex shader? I will edit the post changing fragment to vertex shader in my example and a screenshot where I got the performance log. – Bruno Gonçalves Dec 16 '16 at 16:45