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!!
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 \$\endgroup\$ – Folkert Hoogenraad Dec 16 '16 at 14:43