public void snapshot(int sn, GL gl, String fileSuffix, String destPath) {
      final GLDrawable drawable = gl.getContext().getGLReadDrawable();
      final String postSNDetail = String.format("awt-usr%03d", textRendererGLEL.userCounter);
      final String filenameAWT =
          getSnapshotFilename(
              sn,
              postSNDetail,
              drawable.getChosenGLCapabilities(),
              drawable.getWidth(),
              drawable.getHeight(),
              glReadBufferUtil.hasAlpha(),
              fileSuffix,
              destPath);
      if (swapBuffersBeforeRead) {
        drawable.swapBuffers();
        // Just to test whether we use the right buffer,
        // i.e. back-buffer shall no more be required ..
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
      } else {
        gl.glFinish(); // just make sure rendering finished ..
      }

      final boolean awtOrientation = drawable.isGLOriented();
      System.err.println(
          Thread.currentThread().getName()
              + ": ** screenshot: awtOrient/v-flip "
              + awtOrientation
              + ", swapBuffersBeforeRead "
              + swapBuffersBeforeRead
              + ", "
              + filenameAWT);

      final BufferedImage image = glReadBufferUtil.readPixelsToBufferedImage(gl, awtOrientation);
      final File fout = new File(filenameAWT);
      try {
        ImageIO.write(image, "png", fout);
      } catch (IOException e) {
        e.printStackTrace();
      }
      /**
       * final String filenameJGL = getSnapshotFilename(sn, "jgl",
       * drawable.getChosenGLCapabilities(), drawable.getWidth(), drawable.getHeight(),
       * glReadBufferUtil.hasAlpha(), fileSuffix, destPath); glReadBufferUtil.write(new
       * File(filenameJGL));
       */
    }
Exemple #2
0
 @Override
 public final void setRealized(boolean realized) {
   final RecursiveLock _lock = getLock();
   _lock.lock();
   try {
     final GLDrawable _drawable = drawable;
     if (null == _drawable
         || realized && (0 >= _drawable.getWidth() || 0 >= _drawable.getHeight())) {
       return;
     }
     _drawable.setRealized(realized);
     if (realized && _drawable.isRealized()) {
       sendReshape = true; // ensure a reshape is being send ..
     }
   } finally {
     _lock.unlock();
   }
 }
  @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);
  }
Exemple #5
0
 @Override
 public int getWidth() {
   final GLDrawable _drawable = drawable;
   return null != _drawable ? _drawable.getWidth() : 0;
 }
Exemple #6
0
  /**
   * Takes a snapshot of the drawable's current front framebuffer. Example filenames:
   *
   * <pre>
   * TestGLDrawableAutoDelegateOnOffscrnCapsNEWT.testES2OffScreenFBOSglBuf____-n0001-msaa0-GLES2_-sw-fbobject-Bdbl-Frgb__Irgb_-S00_default-0400x0300.png
   * TestGLDrawableAutoDelegateOnOffscrnCapsNEWT.testES2OffScreenPbufferDblBuf-n0003-msaa0-GLES2_-sw-pbuffer_-Bdbl-Frgb__Irgb_-S00_default-0200x0150.png
   * TestGLDrawableAutoDelegateOnOffscrnCapsNEWT.testGL2OffScreenPbufferSglBuf-n0003-msaa0-GL2___-hw-pbuffer_-Bone-Frgb__Irgb_-S00_default-0200x0150.png
   * </pre>
   *
   * @param sn sequential number
   * @param postSNDetail optional detail to be added to the filename after <code>sn</code>
   * @param gl the current GL context object. It's read drawable is being used as the pixel source
   *     and to gather some details which will end up in the filename.
   * @param readBufferUtil the {@link GLReadBufferUtil} to be used to read the pixels for the
   *     screenshot.
   * @param fileSuffix Optional file suffix without a <i>dot</i> defining the file type, i.e. <code>
   *     "png"</code>. If <code>null</code> the <code>"png"</code> as defined in {@link
   *     TextureIO#PNG} is being used.
   * @param destPath Optional platform dependent file path. It shall use {@link File#separatorChar}
   *     as is directory separator. It shall not end with a directory separator, {@link
   *     File#separatorChar}. If <code>null</code> the current working directory is being used.
   */
  public void snapshot(
      int sn,
      String postSNDetail,
      GL gl,
      GLReadBufferUtil readBufferUtil,
      String fileSuffix,
      String destPath) {
    if (null == fileSuffix) {
      fileSuffix = TextureIO.PNG;
    }
    final int maxSimpleTestNameLen = getMaxTestNameLen() + getClass().getSimpleName().length() + 1;
    final String simpleTestName = this.getSimpleTestName(".");
    final String filenameBaseName;
    {
      final GLDrawable drawable = gl.getContext().getGLReadDrawable();
      final GLCapabilitiesImmutable caps = drawable.getChosenGLCapabilities();
      final String accel = caps.getHardwareAccelerated() ? "hw" : "sw";
      final String scrnm;
      if (caps.isOnscreen()) {
        scrnm = "onscreen";
      } else if (caps.isFBO()) {
        scrnm = "fbobject";
      } else if (caps.isPBuffer()) {
        scrnm = "pbuffer_";
      } else if (caps.isBitmap()) {
        scrnm = "bitmap__";
      } else {
        scrnm = "unknown_";
      }
      final String dblb = caps.getDoubleBuffered() ? "dbl" : "one";
      final String F_pfmt = readBufferUtil.hasAlpha() ? "rgba" : "rgb_";
      final String pfmt = caps.getAlphaBits() > 0 ? "rgba" : "rgb_";
      final int samples = caps.getNumSamples();
      final String aaext = caps.getSampleExtension();
      postSNDetail = null != postSNDetail ? "-" + postSNDetail : "";

      filenameBaseName =
          String.format(
                  "%-"
                      + maxSimpleTestNameLen
                      + "s-n%04d%s-%-6s-%s-%s-B%s-F%s_I%s-S%02d_%s-%04dx%04d.%s",
                  simpleTestName,
                  sn,
                  postSNDetail,
                  drawable.getGLProfile().getName(),
                  accel,
                  scrnm,
                  dblb,
                  F_pfmt,
                  pfmt,
                  samples,
                  aaext,
                  drawable.getWidth(),
                  drawable.getHeight(),
                  fileSuffix)
              .replace(' ', '_');
    }
    final String filename =
        null != destPath ? destPath + File.separator + filenameBaseName : filenameBaseName;
    System.err.println(
        Thread.currentThread().getName()
            + ": ** screenshot: "
            + filename
            + ", maxTestNameLen "
            + maxSimpleTestNameLen
            + ", <"
            + simpleTestName
            + ">");
    gl.glFinish(); // just make sure rendering finished ..
    if (readBufferUtil.readPixels(gl, false)) {
      readBufferUtil.write(new File(filename));
    }
  }