summaryrefslogtreecommitdiffstats
path: root/src/demos/es2/perftst/PerfUniLoad.java
blob: ae5e98aa2a53d7bef99113bab7dff2e1d22c8711 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package demos.es2.perftst;

import com.jogamp.common.nio.Buffers;
import java.nio.*;
import com.jogamp.opengl.*;
import com.jogamp.opengl.util.*;
import com.jogamp.opengl.util.glsl.ShaderState;


public class PerfUniLoad extends PerfModule {
    static final int MAX_ARRAYS = 12;
    static final int MAX_ARRAY_ELEM = 16;

    GLUniformData[] dummyA, dummyB, dummyC;
    final int dataType=GL.GL_FLOAT;
    
    public PerfUniLoad() {
    }

    public ShaderState initShaderState(GL2ES2 gl) {
        return initShaderState(gl, "uni-vert-col", "fcolor");
    }

    protected void runOneSet(GLAutoDrawable drawable, int numObjs, int numArrayElem, int loops) {
        GL2ES2 gl = drawable.getGL().getGL2ES2();

        // 
        // Vertices Data setup
        //

        if(numObjs>MAX_ARRAYS) {
            throw new GLException("numObjs must be within 0.."+MAX_ARRAYS);
        }

        if(numArrayElem>MAX_ARRAY_ELEM) {
            throw new GLException("numArrayElem must be within 0.."+MAX_ARRAY_ELEM);
        }

        st.useProgram(gl, true);

        GLArrayDataServer vertices = GLArrayDataServer.createGLSL("mgl_Vertex", 3, GL.GL_FLOAT, true, 4, GL.GL_STATIC_DRAW);
        {
            FloatBuffer vb = (FloatBuffer)vertices.getBuffer();
            vb.put(0f); vb.put(0f); vb.put(0f);
            vb.put(1f); vb.put(0f); vb.put(0f);
            vb.put(0f); vb.put(1f); vb.put(0f);
            vb.put(1f); vb.put(1f); vb.put(0f);
        }
        vertices.seal(gl, true);

        GLArrayDataServer colors = GLArrayDataServer.createGLSL("mgl_Color",  4, GL.GL_FLOAT, true, 4, GL.GL_STATIC_DRAW);
        {
            FloatBuffer cb = (FloatBuffer)colors.getBuffer();
            cb.put(0f); cb.put(0f); cb.put(0f); cb.put(1f);
            cb.put(1f); cb.put(0f); cb.put(0f); cb.put(1f);
            cb.put(0f); cb.put(1f); cb.put(0f); cb.put(1f);
            cb.put(0f); cb.put(0f); cb.put(1f); cb.put(1f);
        }
        colors.seal(gl, true);

        //
        // Uniform Data setup
        //

        GLUniformData[] dummyUni = new GLUniformData[numObjs];

        float x=0f, y=0f, z=0f, w=0f;

        for(int i=0; i<numObjs; i++) {
            FloatBuffer fb = Buffers.newDirectFloatBuffer(4*numArrayElem);

            for(int j=0; j<numArrayElem; j++) {
                // Fill them up
                fb.put(x);
                fb.put(y);
                fb.put(z);
                fb.put(w);
                if(x==0f) x=1f;
                else if(x==1f) { x=0f; y+=0.01f; }
                if(y>1f) { x=0f; y=0f; z+=0.01f; }
            }
            fb.flip();

            dummyUni[i] = new GLUniformData("mgl_Dummy"+i, 4, fb);
        }

        // 
        // run loops
        //

        long dtC, dt, dt2, dt3, dtF, dtS, dtT;
        long[] tC = new long[loops];
        long[] t0 = new long[loops];
        long[][] t1 = new long[loops][numObjs];
        long[][] t2 = new long[loops][numObjs];
        long[] tF = new long[loops];
        long[] tS = new long[loops];

        for(int i=0; i<loops; i++) {
            tC[i] = System.currentTimeMillis();

            gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);

            t0[i] = System.currentTimeMillis();

            for(int j=0; j<numObjs; j++) {
                st.uniform(gl, dummyUni[j]);

                t1[i][j] = System.currentTimeMillis();

                gl.glDrawArrays(GL.GL_LINE_STRIP, 0, vertices.getElementCount());

                t2[i][j] = System.currentTimeMillis();
            }

            gl.glFinish();

            tF[i] = System.currentTimeMillis();

            drawable.swapBuffers();

            tS[i] = System.currentTimeMillis();
        }

        int uniElements = numObjs * numArrayElem ;
        int uniBytes    = uniElements * Buffers.SIZEOF_FLOAT;

        dt = 0;
        for(int i=1; i<loops; i++) {
            dt += tS[i] - tC[i];
        }

        System.out.println("");
        System.out.println("Loops "+loops+", uniform arrays "+dummyUni.length+", type FLOAT"+
                           ", uniforms array size "+numArrayElem+
                           ",\n total elements "+uniElements+
                           ", total bytes "+uniBytes+", total time: "+dt +
                           "ms, fps(-1): "+(((loops-1)*1000)/dt)+
                           ",\n uni elem/s: " + ((double)(loops*uniElements)/((double)dt/1000.0)));

        for(int i=0; i<loops; i++) {
            dtC= t0[i] - tC[i];
            dtF= tF[i] - t2[i][dummyUni.length-1];
            dtS= tS[i] - tF[i];
            dtT= tS[i] - tC[i];
            if(dtT<=0) dtT=1;
            System.out.println("\tloop "+i+": clear "+dtC+"ms, finish "+dtF+", swap "+dtS+"ms, total: "+ dtT+"ms, fps "+1000/dtT);
            /*
            for(int j=0; j<dummyUni.length; j++) {
                dt = t1[i][j] - t0[i];
                dt2= t2[i][j] - t1[i][j];
                dtT= dt+dt2;
                System.out.println("\t\tobj "+j+": uniform "+dt +"ms, draw "+dt2+"ms, total: "+ dtT);
            } */
        }
        System.out.println("*****************************************************************");


        st.useProgram(gl, false);

        try {
            Thread.sleep(100);
        } catch (Exception e) {}
    }

    public void run(GLAutoDrawable drawable, int loops) {
        runOneSet(drawable, 1,    1, loops);

        runOneSet(drawable,  4,    1, loops);
        runOneSet(drawable,  1,    4, loops);

        runOneSet(drawable,  8,    1, loops);
        runOneSet(drawable,  1,    8, loops);

        if(MAX_ARRAYS>8) {
            runOneSet(drawable, MAX_ARRAYS,         1, loops);
            runOneSet(drawable, 1,         MAX_ARRAYS, loops);
        }
        runOneSet(drawable,  1,   16, loops);

        runOneSet(drawable,  2,   16, loops);
        runOneSet(drawable,  4,   16, loops);
        runOneSet(drawable,  8,   16, loops);
        if(MAX_ARRAYS>8) {
            runOneSet(drawable, MAX_ARRAYS,   16, loops);
        }
    }

}