Skip to content

Commit c4e78e4

Browse files
Optiminze renders
1 parent 58e7bda commit c4e78e4

8 files changed

Lines changed: 105 additions & 53 deletions

File tree

res/layout/main.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
width="200dp"
3333
height="200dp"
3434
background="?colorPrimary"
35+
rotation="45"
36+
alpha="0.2"
3537
src="@drawable/panda"/>
3638
<View width="10dp" height="10dp"/>
3739
<Switch/>

res/raw/view_renderer.frag

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
precision mediump float;
2+
3+
#define ALPHA float(params[1])/255.0
4+
5+
uniform int[8] params;
6+
7+
uniform sampler2D texture;
8+
9+
varying vec2 uv;
10+
11+
void main() {
12+
vec4 color = texture2D(texture, uv);
13+
14+
gl_FragColor = color;
15+
}

res/raw/view_renderer.vert

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
attribute vec4 vPos;
2+
attribute vec2 vTexCoords;
3+
4+
varying vec2 uv;
5+
6+
void main() {
7+
gl_Position = vec4(vPos.xy, 0.0, 1.0);
8+
uv = vTexCoords;
9+
}

src/br/nullexcept/mux/core/texel/GLProgram.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public void build(){
2929
glUseProgram(0);
3030
}
3131

32+
protected int id() {
33+
return program;
34+
}
35+
3236
protected abstract void preload(int program);
3337

3438
public void dispose(){
Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,56 @@
11
package br.nullexcept.mux.core.texel;
22

3+
import br.nullexcept.mux.hardware.GLES;
4+
import br.nullexcept.mux.utils.BufferUtils;
5+
36
class GLProgramView extends GLProgram {
7+
public int texture;
8+
public int position;
9+
public int uv;
10+
public int params;
11+
412
private final String fragment;
513
private final String vertex;
614

715
GLProgramView(){
8-
fragment =
9-
"precision mediump float;\n" +
10-
"\n" +
11-
"uniform float alpha;\n" +
12-
"uniform float time;\n" +
13-
"uniform sampler2D __texture__;\n" +
14-
"varying vec2 xuv;\n" +
15-
"\n" +
16-
"void main(){\n" +
17-
" vec4 pixel = texture2D(__texture__, xuv);\n" +
18-
" pixel.a *= alpha;\n" +
19-
" if(pixel.a < 0.001) discard; " +
20-
" gl_FragColor = pixel;\n" +
21-
"}";
22-
vertex =
23-
"attribute vec4 __position__;\n" +
24-
"attribute vec2 __uv__;\n" +
25-
"\n" +
26-
"varying vec2 xuv;\n" +
27-
"\n" +
28-
"void main(){\n" +
29-
" gl_Position = vec4(__position__.xy,0.0, 1.0);\n" +
30-
" xuv = __uv__ ;\n" +
31-
"}";
16+
fragment = BufferUtils.utfFromStream(getClass().getResourceAsStream("/res/raw/view_renderer.frag"));
17+
vertex = BufferUtils.utfFromStream(getClass().getResourceAsStream("/res/raw/view_renderer.vert"));
18+
}
19+
20+
@Override
21+
public void build() {
22+
super.build();
23+
24+
bind();
25+
position = GLES.glGetAttribLocation(id(), "vPos");
26+
uv = GLES.glGetAttribLocation(id(), "vTexCoords");
27+
texture = GLES.glGetUniformLocation(id(), "texture");
28+
params = GLES.glGetUniformLocation(id(), "params");
29+
30+
unbind();
31+
}
32+
33+
@Override
34+
protected void preload(int program) {
35+
3236
}
3337

3438
@Override
3539
protected String fragment() {
36-
return fragment.replaceAll("__texture__", UNIFORM_TEXTURE);
40+
return fragment;
3741
}
3842

3943
@Override
4044
protected String vertex() {
41-
return vertex.replaceAll("__position__", ATTRIBUTE_POSITION)
42-
.replaceAll("__uv__", ATTRIBUTE_UV);
45+
return vertex;
4346
}
4447

4548
@Override
46-
protected void preload(int program) {
49+
public String toString() {
50+
return "GLProgramView{" +
51+
"texture=" + texture +
52+
", position=" + position +
53+
", uv=" + uv+
54+
'}';
4755
}
4856
}

src/br/nullexcept/mux/core/texel/GLShaderList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class GLShaderList {
66
private static ArrayList<GLProgram> list = new ArrayList<>();
77

88
public static final GLProgram TEXTURE = register(new GLProgramTexture());
9-
public static final GLProgram VIEW = register(new GLProgramView());
9+
public static final GLProgramView VIEW = register(new GLProgramView());
1010

1111
public static <T extends GLProgram> T register(T value){
1212
list.add(value);

src/br/nullexcept/mux/core/texel/GLTexel.java

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import br.nullexcept.mux.graphics.Color;
44
import org.lwjgl.BufferUtils;
5+
import org.lwjgl.opengles.OESDrawBuffersIndexed;
56

67
import java.nio.FloatBuffer;
78

@@ -54,42 +55,57 @@ public static void drawTexture(float x, float y, float width, float height, GLTe
5455

5556
public static void drawViewLayers(float[][] vertices, int[] textures, float[] alphas){
5657
glEnable(GL_BLEND);
57-
glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
58-
GLProgram program = GLShaderList.VIEW;
58+
/**
59+
* X = FUNC SRC
60+
* Y = FUNC DST
61+
*
62+
* dstColor = src.rgb * X + dest.rgb * Y;
63+
*/
64+
glBlendFuncSeparate(
65+
GL_ONE,
66+
GL_ONE_MINUS_SRC_ALPHA,
67+
GL_ONE,
68+
GL_ONE_MINUS_SRC_ALPHA
69+
);
70+
71+
72+
GLProgramView program = GLShaderList.VIEW;
5973

6074
program.bind();
61-
62-
int vPosition = program.attribute(GLProgram.ATTRIBUTE_POSITION);
63-
int vTextureCoords = program.attribute(GLProgram.ATTRIBUTE_UV);
64-
int uTexture = program.uniform(GLProgram.UNIFORM_TEXTURE);
65-
int uAlpha = program.uniform("alpha");
66-
int uTime = program.uniform("time");
67-
68-
glEnableVertexAttribArray(vPosition);
69-
glEnableVertexAttribArray(vTextureCoords);
75+
glEnableVertexAttribArray(program.position);
76+
glEnableVertexAttribArray(program.uv);
7077

7178
for (int i = 0; i < vertices.length; i++) {
7279
bufferRect.put(vertices[i]);
7380
bufferRect.position(0);
7481

7582
glBindTexture(GL_TEXTURE_2D, textures[i]);
76-
glVertexAttribPointer(vPosition, 2, GL_FLOAT, false, 0, bufferRect);
77-
glVertexAttribPointer(vTextureCoords, 2, GL_FLOAT, false, 0, bufferUV);
7883

79-
glUniform1i(uTexture, GL_TEXTURE_2D);
80-
glUniform1f(uAlpha, alphas[i]);
81-
glUniform1f(uTime, (System.currentTimeMillis() & 1000)/1000.0f);
84+
glVertexAttribPointer(program.position, 2, GL_FLOAT, false, 0, bufferRect);
85+
glVertexAttribPointer(program.uv, 2, GL_FLOAT, false, 0, bufferUV);
86+
87+
glUniform1iv(program.params, new int[]{});
88+
89+
glUniform1i(program.texture, GL_TEXTURE_2D);
8290
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
83-
glDisable(GL_DEPTH_TEST);
91+
glFinish();
8492
}
8593

8694
program.unbind();
8795
glBindTexture(GL_TEXTURE_2D, 0);
8896
}
8997

98+
99+
private static FloatBuffer allocateVertices(float[][] vectors) {
100+
FloatBuffer vertx = BufferUtils.createFloatBuffer(vectors.length*vectors[0].length);
101+
vertx.position(0);
102+
for (int i = 0; i< vectors.length; i++)
103+
vertx.put(vectors[i]);
104+
vertx.position(0);
105+
return vertx;
106+
}
107+
90108
public static void drawTexture(float x, float y, float width, float height, GLProgram program, int texture){
91-
glEnable(GL_BLEND);
92-
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
93109
prepareRect(x, y, width, height);
94110
//bufferColor.put(new float[]{ Color.red(color)/255.0f, Color.green(color)/255.0f, Color.blue(color)/255.0f, Color.alpha(color)/255.0f });
95111
program.bind();
@@ -113,6 +129,5 @@ public static void drawTexture(float x, float y, float width, float height, GLPr
113129
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
114130
program.unbind();
115131
glBindTexture(GL_TEXTURE_2D,0);
116-
glDisable(GL_DEPTH_TEST);
117132
}
118133
}

src/br/nullexcept/mux/core/texel/ViewRenderer.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public void drawInternal(CanvasTexel canvas, View view){
2626
canvas.getFramebuffer().resize(bounds.width(), bounds.height());
2727
}
2828
canvas.begin();
29-
canvas.reset();
3029
canvas.alpha(view.getAlpha());
30+
canvas.reset();
3131
view.onDraw(canvas);
3232
if (view instanceof ViewGroup){
3333
canvas.end();
@@ -52,10 +52,9 @@ public void drawInternal(CanvasTexel canvas, View view){
5252
index++;
5353
}
5454
}
55-
canvas.getFramebuffer().bind();
56-
GLTexel.drawViewLayers(borders,textures, alphas);
57-
canvas.getFramebuffer().unbind();
55+
5856
canvas.begin();
57+
GLTexel.drawViewLayers(borders,textures, alphas);
5958
}
6059
view.onDrawForeground(canvas);
6160
view.subFlag(FLAG_REQUIRES_DRAW);

0 commit comments

Comments
 (0)