private void draw(GL2ES2 gl) { // gl.glClearColor(0.5f, 0.1f, 0.1f, 1); // gl.glClear(GL2ES2.GL_COLOR_BUFFER_BIT); shaderState.useProgram(gl, true); time.setData((System.currentTimeMillis() - millisOffset) / 1000.0f); shaderState.uniform(gl, time); vertices.enableBuffer(gl, true); gl.glDrawArrays(GL2ES2.GL_TRIANGLE_STRIP, 0, 4); vertices.enableBuffer(gl, false); shaderState.useProgram(gl, false); // Compute current framerate and printout. frameCount++; fcount += 1; int m = (int) (System.currentTimeMillis() - millisOffset); if (m - lastm > 1000 * fint) { frameRate = (float) (fcount) / fint; fcount = 0; lastm = m; } if (frameCount % TARGET_FPS == 0) { System.out.println("FrameCount: " + frameCount + " - " + "FrameRate: " + frameRate); } }
private void linkData(final GL2ES2 gl, final ShaderProgram sp) { if (null == iVBO) return; if (0 > vboPos.setLocation(gl, sp.program())) { throw new GLException("Couldn't locate " + vboPos); } if (0 > vboParams.setLocation(gl, sp.program())) { throw new GLException("Couldn't locate " + vboParams); } if (0 > vboTexCoordsR.setLocation(gl, sp.program())) { throw new GLException("Couldn't locate " + vboTexCoordsR); } if (StereoUtil.usesChromaticDistortion(distortionBits)) { if (0 > vboTexCoordsG.setLocation(gl, sp.program())) { throw new GLException("Couldn't locate " + vboTexCoordsG); } if (0 > vboTexCoordsB.setLocation(gl, sp.program())) { throw new GLException("Couldn't locate " + vboTexCoordsB); } } if (0 > eyeToSourceUVScale.setLocation(gl, sp.program())) { throw new GLException("Couldn't locate " + eyeToSourceUVScale); } if (0 > eyeToSourceUVOffset.setLocation(gl, sp.program())) { throw new GLException("Couldn't locate " + eyeToSourceUVOffset); } if (StereoUtil.usesTimewarpDistortion(distortionBits)) { if (0 > eyeRotationStart.setLocation(gl, sp.program())) { throw new GLException("Couldn't locate " + eyeRotationStart); } if (0 > eyeRotationEnd.setLocation(gl, sp.program())) { throw new GLException("Couldn't locate " + eyeRotationEnd); } } iVBO.seal(gl, true); iVBO.enableBuffer(gl, false); indices.seal(gl, true); indices.enableBuffer(gl, false); }
@Override public void display(final GLAutoDrawable drawable) { final GL2ES2 gl = drawable.getGL().getGL2ES2(); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); st.useProgram(gl, true); interleavedVBO.enableBuffer(gl, true); if (!keepTextureBound && null != texture) { gl.glActiveTexture(GL.GL_TEXTURE0 + textureUnit); texture.enable(gl); texture.bind(gl); } gl.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, 4); if (!keepTextureBound && null != texture) { texture.disable(gl); } interleavedVBO.enableBuffer(gl, false); st.useProgram(gl, false); }
private void setup(GL2ES2 gl) { if (60 < TARGET_FPS) { // Disables vsync gl.setSwapInterval(0); } glu = new GLU(); vertShader = ShaderCode.create( gl, GL2ES2.GL_VERTEX_SHADER, LandscapeES2.class, "shader", "shader/bin", "landscape", true); fragShader = ShaderCode.create( gl, GL2ES2.GL_FRAGMENT_SHADER, LandscapeES2.class, "shader", "shader/bin", "landscape", true); vertShader.defaultShaderCustomization(gl, true, true); fragShader.defaultShaderCustomization(gl, true, true); shaderProg = new ShaderProgram(); shaderProg.add(gl, vertShader, System.err); shaderProg.add(gl, fragShader, System.err); shaderState = new ShaderState(); shaderState.attachShaderProgram(gl, shaderProg, true); resolution = new GLUniformData("iResolution", 3, FloatBuffer.wrap(new float[] {width, height, 0})); shaderState.ownUniform(resolution); shaderState.uniform(gl, resolution); time = new GLUniformData("iGlobalTime", 0.0f); shaderState.ownUniform(time); vertices = GLArrayDataServer.createGLSL("inVertex", 2, GL.GL_FLOAT, false, 4, GL.GL_STATIC_DRAW); vertices.putf(-1.0f); vertices.putf(-1.0f); vertices.putf(+1.0f); vertices.putf(-1.0f); vertices.putf(-1.0f); vertices.putf(+1.0f); vertices.putf(+1.0f); vertices.putf(+1.0f); vertices.seal(gl, true); shaderState.ownAttribute(vertices, true); shaderState.useProgram(gl, false); doneSetup = true; }
@Override public void display(final GLAutoDrawable glad) { final long t1 = System.currentTimeMillis(); final GL2ES2 gl = glad.getGL().getGL2ES2(); if (clearBuffers) { if (null != tileRendererInUse) { gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f); } else { gl.glClearColor(0, 0, 0, 0); } gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); } if (!gl.hasGLSL()) { return; } st.useProgram(gl, true); // One rotation every four seconds pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW); pmvMatrix.glLoadIdentity(); pmvMatrix.glTranslatef(0, 0, -10); if (doRotate) { final float ang = ((t1 - t0) * 360.0F) / 4000.0F; pmvMatrix.glRotatef(ang, 0, 0, 1); pmvMatrix.glRotatef(ang, 0, 1, 0); } st.uniform(gl, pmvMatrixUniform); // Draw a square vertices.enableBuffer(gl, true); colors.enableBuffer(gl, true); gl.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, 4); vertices.enableBuffer(gl, false); colors.enableBuffer(gl, false); st.useProgram(gl, false); }
/* pp */ GenericEye( final GenericStereoDevice device, final int distortionBits, final float[] eyePositionOffset, final EyeParameter eyeParam, final DimensionImmutable textureSize, final RectangleImmutable eyeViewport) { this.eyeName = eyeParam.number; this.distortionBits = distortionBits; this.viewport = eyeViewport; final boolean usePP = null != device.config.distortionMeshProducer && 0 != distortionBits; final boolean usesTimewarp = usePP && StereoUtil.usesTimewarpDistortion(distortionBits); final FloatBuffer fstash = Buffers.newDirectFloatBuffer(2 + 2 + (usesTimewarp ? 16 + 16 : 0)); if (usePP) { eyeToSourceUVScale = new GLUniformData("svr_EyeToSourceUVScale", 2, Buffers.slice2Float(fstash, 0, 2)); eyeToSourceUVOffset = new GLUniformData("svr_EyeToSourceUVOffset", 2, Buffers.slice2Float(fstash, 2, 2)); } else { eyeToSourceUVScale = null; eyeToSourceUVOffset = null; } if (usesTimewarp) { eyeRotationStart = new GLUniformData("svr_EyeRotationStart", 4, 4, Buffers.slice2Float(fstash, 4, 16)); eyeRotationEnd = new GLUniformData("svr_EyeRotationEnd", 4, 4, Buffers.slice2Float(fstash, 20, 16)); } else { eyeRotationStart = null; eyeRotationEnd = null; } this.eyeParameter = eyeParam; // Setup: eyeToSourceUVScale, eyeToSourceUVOffset if (usePP) { final ScaleAndOffset2D textureScaleAndOffset = new ScaleAndOffset2D(eyeParam.fovhv, textureSize, eyeViewport); if (StereoDevice.DEBUG) { System.err.println("XXX." + eyeName + ": eyeParam " + eyeParam); System.err.println("XXX." + eyeName + ": uvScaleOffset " + textureScaleAndOffset); System.err.println("XXX." + eyeName + ": textureSize " + textureSize); System.err.println("XXX." + eyeName + ": viewport " + eyeViewport); } final FloatBuffer eyeToSourceUVScaleFB = eyeToSourceUVScale.floatBufferValue(); eyeToSourceUVScaleFB.put(0, textureScaleAndOffset.scale[0]); eyeToSourceUVScaleFB.put(1, textureScaleAndOffset.scale[1]); final FloatBuffer eyeToSourceUVOffsetFB = eyeToSourceUVOffset.floatBufferValue(); eyeToSourceUVOffsetFB.put(0, textureScaleAndOffset.offset[0]); eyeToSourceUVOffsetFB.put(1, textureScaleAndOffset.offset[1]); } else { vertexCount = 0; indexCount = 0; iVBO = null; vboPos = null; vboParams = null; vboTexCoordsR = null; vboTexCoordsG = null; vboTexCoordsB = null; indices = null; if (StereoDevice.DEBUG) { System.err.println("XXX." + eyeName + ": " + this); } return; } final DistortionMesh meshData = device.config.distortionMeshProducer.create(eyeParam, distortionBits); if (null == meshData) { throw new GLException( "Failed to create meshData for eye " + eyeParam + ", and " + StereoUtil.distortionBitsToString(distortionBits)); } vertexCount = meshData.vertexCount; indexCount = meshData.indexCount; /** * 2+2+2+2+2: { vec2 position, vec2 color, vec2 texCoordR, vec2 texCoordG, vec2 texCoordB } */ final boolean useChromatic = StereoUtil.usesChromaticDistortion(distortionBits); final boolean useVignette = StereoUtil.usesVignetteDistortion(distortionBits); final int compsPerElement = 2 + 2 + 2 + (useChromatic ? 2 + 2 /* texCoordG + texCoordB */ : 0); iVBO = GLArrayDataServer.createGLSLInterleaved( compsPerElement, GL.GL_FLOAT, false, vertexCount, GL.GL_STATIC_DRAW); vboPos = iVBO.addGLSLSubArray("svr_Position", 2, GL.GL_ARRAY_BUFFER); vboParams = iVBO.addGLSLSubArray("svr_Params", 2, GL.GL_ARRAY_BUFFER); vboTexCoordsR = iVBO.addGLSLSubArray("svr_TexCoordR", 2, GL.GL_ARRAY_BUFFER); if (useChromatic) { vboTexCoordsG = iVBO.addGLSLSubArray("svr_TexCoordG", 2, GL.GL_ARRAY_BUFFER); vboTexCoordsB = iVBO.addGLSLSubArray("svr_TexCoordB", 2, GL.GL_ARRAY_BUFFER); } else { vboTexCoordsG = null; vboTexCoordsB = null; } indices = GLArrayDataServer.createData( 1, GL.GL_SHORT, indexCount, GL.GL_STATIC_DRAW, GL.GL_ELEMENT_ARRAY_BUFFER); /** * 2+2+2+2+2: { vec2 position, vec2 color, vec2 texCoordR, vec2 texCoordG, vec2 texCoordB } */ final FloatBuffer iVBOFB = (FloatBuffer) iVBO.getBuffer(); for (int vertNum = 0; vertNum < vertexCount; vertNum++) { final DistortionMesh.DistortionVertex v = meshData.vertices[vertNum]; int dataIdx = 0; if (StereoDevice.DUMP_DATA) { System.err.println("XXX." + eyeName + ": START VERTEX " + vertNum + " / " + vertexCount); } // pos if (v.pos_size >= 2) { if (StereoDevice.DUMP_DATA) { System.err.println( "XXX." + eyeName + ": pos [" + v.data[dataIdx] + ", " + v.data[dataIdx + 1] + "]"); } iVBOFB.put(v.data[dataIdx]); iVBOFB.put(v.data[dataIdx + 1]); } else { iVBOFB.put(0f); iVBOFB.put(0f); } dataIdx += v.pos_size; // params if (v.vignetteFactor_size >= 1 && useVignette) { if (StereoDevice.DUMP_DATA) { System.err.println("XXX." + eyeName + ": vignette " + v.data[dataIdx]); } iVBOFB.put(v.data[dataIdx]); } else { iVBOFB.put(1.0f); } dataIdx += v.vignetteFactor_size; if (v.timewarpFactor_size >= 1) { if (StereoDevice.DUMP_DATA) { System.err.println("XXX." + eyeName + ": timewarp " + v.data[dataIdx]); } iVBOFB.put(v.data[dataIdx]); } else { iVBOFB.put(1.0f); } dataIdx += v.timewarpFactor_size; // texCoordR if (v.texR_size >= 2) { if (StereoDevice.DUMP_DATA) { System.err.println( "XXX." + eyeName + ": texR [" + v.data[dataIdx] + ", " + v.data[dataIdx + 1] + "]"); } iVBOFB.put(v.data[dataIdx]); iVBOFB.put(v.data[dataIdx + 1]); } else { iVBOFB.put(1f); iVBOFB.put(1f); } dataIdx += v.texR_size; if (useChromatic) { // texCoordG if (v.texG_size >= 2) { if (StereoDevice.DUMP_DATA) { System.err.println( "XXX." + eyeName + ": texG [" + v.data[dataIdx] + ", " + v.data[dataIdx + 1] + "]"); } iVBOFB.put(v.data[dataIdx]); iVBOFB.put(v.data[dataIdx + 1]); } else { iVBOFB.put(1f); iVBOFB.put(1f); } dataIdx += v.texG_size; // texCoordB if (v.texB_size >= 2) { if (StereoDevice.DUMP_DATA) { System.err.println( "XXX." + eyeName + ": texB [" + v.data[dataIdx] + ", " + v.data[dataIdx + 1] + "]"); } iVBOFB.put(v.data[dataIdx]); iVBOFB.put(v.data[dataIdx + 1]); } else { iVBOFB.put(1f); iVBOFB.put(1f); } dataIdx += v.texB_size; } else { dataIdx += v.texG_size; dataIdx += v.texB_size; } } if (StereoDevice.DUMP_DATA) { System.err.println("XXX." + eyeName + ": iVBO " + iVBO); } { if (StereoDevice.DUMP_DATA) { System.err.println("XXX." + eyeName + ": idx " + indices + ", count " + indexCount); for (int i = 0; i < indexCount; i++) { if (0 == i % 16) { System.err.printf("%n%5d: ", i); } System.err.printf("%5d, ", (int) meshData.indices[i]); } System.err.println(); } final ShortBuffer out = (ShortBuffer) indices.getBuffer(); out.put(meshData.indices, 0, meshData.indexCount); } if (StereoDevice.DEBUG) { System.err.println("XXX." + eyeName + ": " + this); } }
/* pp */ void enableVBO(final GL2ES2 gl, final boolean enable) { if (null == iVBO) return; iVBO.enableBuffer(gl, enable); indices.bindBuffer(gl, enable); // keeps VBO binding if enable:=true }
/* pp */ void dispose(final GL2ES2 gl) { if (null == iVBO) return; iVBO.destroy(gl); indices.destroy(gl); }
@Test public void testShaderState01Validation() throws InterruptedException { // preset .. final NEWTGLContext.WindowContext winctx = NEWTGLContext.createOnscreenWindow(GLProfile.getGL2ES2(), 480, 480, true); final GLDrawable drawable = winctx.context.getGLDrawable(); final GL2ES2 gl = winctx.context.getGL().getGL2ES2(); System.err.println(winctx.context); Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); // test code .. final ShaderState st = new ShaderState(); final ShaderCode rsVp = ShaderCode.create( gl, GL2ES2.GL_VERTEX_SHADER, 1, RedSquare0.class, "shader", "shader/bin", "RedSquareShader"); final ShaderCode rsFp = ShaderCode.create( gl, GL2ES2.GL_FRAGMENT_SHADER, 1, RedSquare0.class, "shader", "shader/bin", "RedSquareShader"); final ShaderProgram sp = new ShaderProgram(); Assert.assertTrue(0 > sp.program()); sp.add(gl, rsVp, System.err); sp.add(gl, rsFp, System.err); Assert.assertTrue(0 <= sp.program()); Assert.assertTrue(!sp.inUse()); Assert.assertTrue(!sp.linked()); Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); st.attachShaderProgram(gl, sp); // Allocate Vertex Array0 final GLArrayDataServer vertices0 = GLSLMiscHelper.createRSVertices0(gl, st, vertices0_loc); System.err.println("vertices0: " + vertices0); vertices0.enableBuffer(gl, false); Assert.assertEquals(vertices0_loc, vertices0.getLocation()); st.ownAttribute(vertices0, true); // Allocate Color Array0 final GLArrayDataServer colors0 = GLSLMiscHelper.createRSColors0(gl, st, colors0_loc); System.err.println("colors0: " + colors0); colors0.enableBuffer(gl, false); Assert.assertEquals(colors0_loc, colors0.getLocation()); st.ownAttribute(colors0, true); Assert.assertTrue(sp.link(gl, System.err)); Assert.assertTrue(sp.linked()); Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); Assert.assertEquals(vertices0_loc, vertices0.getLocation()); GLSLMiscHelper.validateGLArrayDataServerState(gl, st, vertices0); Assert.assertEquals(colors0_loc, colors0.getLocation()); GLSLMiscHelper.validateGLArrayDataServerState(gl, st, colors0); Assert.assertEquals(null, ShaderState.getShaderState(gl)); st.useProgram(gl, true); Assert.assertTrue(sp.inUse()); Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); Assert.assertEquals(st, ShaderState.getShaderState(gl)); // setup mgl_PMVMatrix final PMVMatrix pmvMatrix = new PMVMatrix(); final GLUniformData pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.glGetPMvMatrixf()); Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); st.ownUniform(pmvMatrixUniform); st.uniform(gl, pmvMatrixUniform); Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); Assert.assertEquals(pmvMatrixUniform, st.getUniform("mgl_PMVMatrix")); // Allocate Vertex Array1 final GLArrayDataServer vertices1 = GLSLMiscHelper.createRSVertices1(gl, st); System.err.println("vertices1: " + vertices1); vertices1.enableBuffer(gl, false); GLSLMiscHelper.validateGLArrayDataServerState(gl, st, vertices1); st.ownAttribute(vertices1, true); // Allocate Color Array1 final GLArrayDataServer colors1 = GLSLMiscHelper.createRSColors1(gl, st); System.err.println("colors1: " + colors1); colors1.enableBuffer(gl, false); GLSLMiscHelper.validateGLArrayDataServerState(gl, st, colors1); st.ownAttribute(colors0, true); // misc GL setup gl.glClearColor(0, 0, 0, 1); gl.glEnable(GL2ES2.GL_DEPTH_TEST); Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); // reshape pmvMatrix.glMatrixMode(PMVMatrix.GL_PROJECTION); pmvMatrix.glLoadIdentity(); pmvMatrix.gluPerspective( 45.0F, (float) drawable.getWidth() / (float) drawable.getHeight(), 1.0F, 100.0F); pmvMatrix.glMatrixMode(PMVMatrix.GL_MODELVIEW); pmvMatrix.glLoadIdentity(); pmvMatrix.glTranslatef(0, 0, -10); st.uniform(gl, pmvMatrixUniform); gl.glViewport(0, 0, drawable.getWidth(), drawable.getHeight()); Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); // display #1 vertices0 / colors0 (post-disable) GLSLMiscHelper.displayVCArrays( drawable, gl, st, true, vertices0, colors0, true, 1, durationPerTest); // display #2 #1 vertices1 / colors1 (post-disable) GLSLMiscHelper.displayVCArrays( drawable, gl, st, true, vertices1, colors1, true, 2, durationPerTest); // display #3 vertices0 / colors0 (post-disable) GLSLMiscHelper.displayVCArrays( drawable, gl, st, true, vertices0, colors0, true, 3, durationPerTest); // cleanup st.destroy(gl); NEWTGLContext.destroyWindow(winctx); }
@Test(timeout = 120000) public void testShaderState01PerformanceDouble() throws InterruptedException { // preset .. final NEWTGLContext.WindowContext winctx = NEWTGLContext.createOnscreenWindow(GLProfile.getGL2ES2(), 480, 480, false); final GLDrawable drawable = winctx.context.getGLDrawable(); final GL2ES2 gl = winctx.context.getGL().getGL2ES2(); System.err.println(winctx.context); gl.setSwapInterval(0); Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); // test code .. final ShaderState st = new ShaderState(); final ShaderCode rsVp = ShaderCode.create( gl, GL2ES2.GL_VERTEX_SHADER, 1, RedSquare0.class, "shader", "shader/bin", "RedSquareShader"); final ShaderCode rsFp = ShaderCode.create( gl, GL2ES2.GL_FRAGMENT_SHADER, 1, RedSquare0.class, "shader", "shader/bin", "RedSquareShader"); final ShaderProgram sp = new ShaderProgram(); sp.add(rsVp); sp.add(rsFp); sp.init(gl); Assert.assertTrue(sp.link(gl, System.err)); st.attachShaderProgram(gl, sp); st.useProgram(gl, true); // setup mgl_PMVMatrix final PMVMatrix pmvMatrix = new PMVMatrix(); final GLUniformData pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.glGetPMvMatrixf()); st.ownUniform(pmvMatrixUniform); st.uniform(gl, pmvMatrixUniform); // Allocate Vertex Array0 final GLArrayDataServer vertices0 = GLSLMiscHelper.createRSVertices0(gl, st, -1); st.ownAttribute(vertices0, true); vertices0.enableBuffer(gl, false); // Allocate Vertex Array1 final GLArrayDataServer vertices1 = GLSLMiscHelper.createRSVertices1(gl, st); st.ownAttribute(vertices1, true); vertices1.enableBuffer(gl, false); // Allocate Color Array0 final GLArrayDataServer colors0 = GLSLMiscHelper.createRSColors0(gl, st, -1); st.ownAttribute(colors0, true); colors0.enableBuffer(gl, false); // Allocate Color Array1 final GLArrayDataServer colors1 = GLSLMiscHelper.createRSColors1(gl, st); st.ownAttribute(colors1, true); colors1.enableBuffer(gl, false); // misc GL setup gl.glClearColor(0, 0, 0, 1); gl.glEnable(GL2ES2.GL_DEPTH_TEST); // reshape pmvMatrix.glMatrixMode(PMVMatrix.GL_PROJECTION); pmvMatrix.glLoadIdentity(); pmvMatrix.gluPerspective( 45.0F, (float) drawable.getWidth() / (float) drawable.getHeight(), 1.0F, 100.0F); pmvMatrix.glMatrixMode(PMVMatrix.GL_MODELVIEW); pmvMatrix.glLoadIdentity(); pmvMatrix.glTranslatef(0, 0, -10); st.uniform(gl, pmvMatrixUniform); gl.glViewport(0, 0, drawable.getWidth(), drawable.getHeight()); gl.setSwapInterval(0); // validation .. GLSLMiscHelper.displayVCArrays(drawable, gl, st, true, vertices0, colors0, true, 1, 0); GLSLMiscHelper.displayVCArrays(drawable, gl, st, true, vertices1, colors1, true, 2, 0); // warmup .. for (int frames = 0; frames < GLSLMiscHelper.frames_warmup; frames += 2) { GLSLMiscHelper.displayVCArraysNoChecks(drawable, gl, true, vertices0, colors0, true); GLSLMiscHelper.displayVCArraysNoChecks(drawable, gl, true, vertices1, colors1, true); } // measure .. long t0 = System.currentTimeMillis(); int frames; for (frames = 0; frames < GLSLMiscHelper.frames_perftest; frames += 2) { GLSLMiscHelper.displayVCArraysNoChecks(drawable, gl, true, vertices0, colors0, true); GLSLMiscHelper.displayVCArraysNoChecks(drawable, gl, true, vertices1, colors1, true); } final long t1 = System.currentTimeMillis(); final long dt = t1 - t0; final double fps = (frames * 1000.0) / (double) dt; final String fpsS = String.valueOf(fps); final int fpsSp = fpsS.indexOf('.'); System.err.println( "testShaderState01PerformanceDouble: " + dt / 1000.0 + "s: " + frames + "f, " + fpsS.substring(0, fpsSp + 2) + " fps, " + dt / frames + " ms/f"); // cleanup st.destroy(gl); NEWTGLContext.destroyWindow(winctx); }
@Override public void init(final GLAutoDrawable glad) { if (null != textureData) { texture = TextureIO.newTexture(glad.getGL(), textureData); } final GL2ES2 gl = glad.getGL().getGL2ES2(); initShader(gl, true); // setup mgl_PMVMatrix pmvMatrix = new PMVMatrix(); pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION); pmvMatrix.glLoadIdentity(); pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW); pmvMatrix.glLoadIdentity(); pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.glGetPMvMatrixf()); // P, Mv st.ownUniform(pmvMatrixUniform); if (!st.uniform(gl, pmvMatrixUniform)) { throw new GLException("Error setting PMVMatrix in shader: " + st); } if (!st.uniform(gl, new GLUniformData("mgl_ActiveTexture", textureUnit))) { throw new GLException("Error setting mgl_ActiveTexture in shader: " + st); } if (null != texture) { // fetch the flipped texture coordinates texture.getImageTexCoords().getST_LB_RB_LT_RT(s_quadTexCoords, 0, 1f, 1f); } interleavedVBO = GLArrayDataServer.createGLSLInterleaved( 3 + 4 + 2, GL.GL_FLOAT, false, 3 * 4, GL.GL_STATIC_DRAW); { interleavedVBO.addGLSLSubArray("mgl_Vertex", 3, GL.GL_ARRAY_BUFFER); interleavedVBO.addGLSLSubArray("mgl_Color", 4, GL.GL_ARRAY_BUFFER); // interleavedVBO.addGLSLSubArray("mgl_Normal", 3, GL.GL_ARRAY_BUFFER); interleavedVBO.addGLSLSubArray("mgl_MultiTexCoord", 2, GL.GL_ARRAY_BUFFER); final FloatBuffer ib = (FloatBuffer) interleavedVBO.getBuffer(); for (int i = 0; i < 4; i++) { ib.put(s_quadVertices, i * 3, 3); ib.put(s_quadColors, i * 4, 4); // ib.put(s_cubeNormals, i*3, 3); ib.put(s_quadTexCoords, i * 2, 2); } } interleavedVBO.seal(gl, true); interleavedVBO.enableBuffer(gl, false); st.ownAttribute(interleavedVBO, true); // OpenGL Render Settings gl.glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]); gl.glEnable(GL.GL_DEPTH_TEST); if (keepTextureBound && null != texture) { gl.glActiveTexture(GL.GL_TEXTURE0 + textureUnit); texture.enable(gl); texture.bind(gl); } st.useProgram(gl, false); }
protected void runOneSet( GLAutoDrawable drawable, String textBaseName, int numObjs, int numTextures, int loops) { GL2ES2 gl = drawable.getGL().getGL2ES2(); if (numTextures > MAX_TEXTURE_ENGINES) { throw new GLException("numTextures must be within 1.." + MAX_TEXTURE_ENGINES); } String textName = null; textDatas = new TextureData[numObjs]; textures = new Texture[numTextures]; try { for (int i = 0; i < numObjs; i++) { textName = "data/" + textBaseName + "." + (i + 1) + ".tga"; URL urlText = IOUtil.getResource(Perftst.class, textName); if (urlText == null) { throw new RuntimeException("couldn't fetch " + textName); } textDatas[i] = TextureIO.newTextureData(gl.getGLProfile(), urlText.openStream(), false, TextureIO.TGA); System.out.println(textBaseName + ": " + textDatas[i]); } for (int i = 0; i < numTextures; i++) { gl.glActiveTexture(i); textures[i] = new Texture(GL.GL_TEXTURE_2D); } } catch (IOException ioe) { System.err.println("couldn't fetch " + textName); throw new RuntimeException(ioe); } // // Vertices Data setup // st.useProgram(gl, true); GLArrayDataServer vertices = GLArrayDataServer.createGLSL("mgl_Vertex", 2, GL.GL_FLOAT, true, 4, GL.GL_STATIC_DRAW); { FloatBuffer vb = (FloatBuffer) vertices.getBuffer(); vb.put(0f); vb.put(0f); vb.put(1f); vb.put(0f); vb.put(0f); vb.put(1f); vb.put(1f); vb.put(1f); } vertices.seal(gl, true); GLArrayDataServer texCoords = GLArrayDataServer.createGLSL( "mgl_MultiTexCoord0", 2, GL.GL_FLOAT, true, 4, GL.GL_STATIC_DRAW); { FloatBuffer cb = (FloatBuffer) texCoords.getBuffer(); cb.put(0f); cb.put(0f); cb.put(1f); cb.put(0f); cb.put(0f); cb.put(1f); cb.put(1f); cb.put(1f); } texCoords.seal(gl, true); // // texture setup // long[] tU = new long[numObjs + 1]; tU[0] = System.currentTimeMillis(); for (int j = 0; j < numTextures; j++) { gl.glActiveTexture(j); textures[j].updateImage(gl, textDatas[0]); tU[j + 1] = System.currentTimeMillis(); } GLUniformData activeTexture = new GLUniformData("mgl_ActiveTexture", 0); st.uniform(gl, activeTexture); // // run loops // long dtC, dt, dt2, dt3, dtF, dtS, dtT; long[][] tC = new long[loops][numObjs]; long[][] t0 = new long[loops][numObjs]; long[][][] t1 = new long[loops][numObjs][numTextures]; long[][][] t2 = new long[loops][numObjs][numTextures]; long[][][] t3 = new long[loops][numObjs][numTextures]; long[][] tF = new long[loops][numObjs]; long[][] tS = new long[loops][numObjs]; for (int i = 0; i < loops; i++) { for (int j = 0; j < numObjs; j++) { tC[i][j] = System.currentTimeMillis(); gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT); t0[i][j] = System.currentTimeMillis(); for (int k = 0; k < numTextures; k++) { gl.glActiveTexture(GL.GL_TEXTURE0 + k); textures[k].enable(gl); textures[k].bind(gl); activeTexture.setData(k); st.uniform(gl, activeTexture); t1[i][j][k] = System.currentTimeMillis(); textures[k].updateSubImage(gl, textDatas[j], 0, 0, 0); t2[i][j][k] = System.currentTimeMillis(); gl.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, vertices.getElementCount()); t3[i][j][k] = System.currentTimeMillis(); } gl.glFinish(); tF[i][j] = System.currentTimeMillis(); drawable.swapBuffers(); tS[i][j] = System.currentTimeMillis(); /*try { Thread.sleep(100); } catch (Exception e) {} */ } } int textBytes = 0; for (int j = 0; j < numObjs; j++) { textBytes += textDatas[j].getEstimatedMemorySize(); } textBytes *= numTextures; dt = 0; for (int i = 1; i < loops; i++) { for (int j = 0; j < numObjs; j++) { dt += tS[i][j] - tC[i][j]; } } System.out.println(""); System.out.println( "Texture " + textBaseName + ", loops " + loops + ", textures " + numTextures + ", objects " + numObjs + ", total bytes " + textBytes + ", total time: " + dt + "ms, fps(-1): " + (((loops - 1) * numObjs * 1000) / dt) + ",\n text kB/s: " + (((double) (loops * textBytes) / 1024.0) / ((double) dt / 1000.0))); for (int i = 0; i < loops; i++) { dtC = 0; dtF = 0; dtS = 0; dtT = 0; for (int j = 0; j < numObjs; j++) { dtC += t0[i][j] - tC[i][j]; dtF += tF[i][j] - t3[i][j][numTextures - 1]; dtS += tS[i][j] - tF[i][j]; dtT += tS[i][j] - tC[i][j]; } if (dtT <= 0) dtT = 1; System.out.println( "\tloop " + i + ": clear " + dtC + "ms, finish " + dtF + ", swap " + dtS + "ms, total: " + dtT + "ms, fps " + (numObjs * 1000) / dtT); /* for(int j=0; j<dummyUni.length; j++) { dt = t1[i][j] - t0[i]; dt2= t2[i][j] - t1[i][j]; dt3= t3[i][j] - t2[i][j]; dtT= dt+dt2+dt3; System.out.println("\t\tobj "+j+": setup "+dt +"ms, update "+dt2 +"ms, draw "+dt3+"ms, total: "+ dtT); } */ } System.out.println("*****************************************************************"); st.useProgram(gl, false); for (int i = 0; i < numTextures; i++) { textures[i].disable(gl); textures[i].destroy(gl); textures[i] = null; } for (int i = 0; i < numObjs; i++) { textDatas[i] = null; } textures = null; textDatas = null; System.gc(); try { Thread.sleep(100); } catch (Exception e) { } System.gc(); }
@Override public void init(final GLAutoDrawable glad) { System.err.println( Thread.currentThread() + " RedSquareES2.init: tileRendererInUse " + tileRendererInUse); final GL2ES2 gl = glad.getGL().getGL2ES2(); System.err.println("RedSquareES2 init on " + Thread.currentThread()); System.err.println("Chosen GLCapabilities: " + glad.getChosenGLCapabilities()); System.err.println("INIT GL IS: " + gl.getClass().getName()); System.err.println(JoglVersion.getGLStrings(gl, null, false).toString()); if (!gl.hasGLSL()) { System.err.println("No GLSL available, no rendering."); return; } st = new ShaderState(); st.setVerbose(true); final ShaderCode vp0 = ShaderCode.create( gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(), "shader", "shader/bin", "RedSquareShader", true); final ShaderCode fp0 = ShaderCode.create( gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(), "shader", "shader/bin", "RedSquareShader", true); vp0.defaultShaderCustomization(gl, true, true); fp0.defaultShaderCustomization(gl, true, true); final ShaderProgram sp0 = new ShaderProgram(); sp0.add(gl, vp0, System.err); sp0.add(gl, fp0, System.err); st.attachShaderProgram(gl, sp0, true); // setup mgl_PMVMatrix pmvMatrix = new PMVMatrix(); pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION); pmvMatrix.glLoadIdentity(); pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW); pmvMatrix.glLoadIdentity(); pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.glGetPMvMatrixf()); // P, Mv st.ownUniform(pmvMatrixUniform); st.uniform(gl, pmvMatrixUniform); // Allocate Vertex Array vertices = GLArrayDataServer.createGLSL("mgl_Vertex", 3, GL.GL_FLOAT, false, 4, GL.GL_STATIC_DRAW); vertices.putf(-2); vertices.putf(2); vertices.putf(0); vertices.putf(2); vertices.putf(2); vertices.putf(0); vertices.putf(-2); vertices.putf(-2); vertices.putf(0); vertices.putf(2); vertices.putf(-2); vertices.putf(0); vertices.seal(gl, true); st.ownAttribute(vertices, true); vertices.enableBuffer(gl, false); // Allocate Color Array colors = GLArrayDataServer.createGLSL("mgl_Color", 4, GL.GL_FLOAT, false, 4, GL.GL_STATIC_DRAW); colors.putf(1); colors.putf(0); colors.putf(0); colors.putf(1); colors.putf(0); colors.putf(0); colors.putf(1); colors.putf(1); colors.putf(1); colors.putf(0); colors.putf(0); colors.putf(1); colors.putf(1); colors.putf(0); colors.putf(0); colors.putf(1); colors.seal(gl, true); st.ownAttribute(colors, true); colors.enableBuffer(gl, false); // OpenGL Render Settings gl.glEnable(GL.GL_DEPTH_TEST); st.useProgram(gl, false); t0 = System.currentTimeMillis(); System.err.println(Thread.currentThread() + " RedSquareES2.init FIN"); }