From 7dff066bb823dddb5d6e0e7672f5599afa5a43b9 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Wed, 31 Oct 2012 15:16:56 +0100 Subject: Add OSX CALayer OpenGL 3 (core) support: Derive pixelformat from parent (GL3), use GL3.2 compatible shader; Use VBO in general. Covered by: Auto unit tests: TestOffscreenLayer01GLCanvasAWT, TestOffscreenLayer02NewtCanvasAWT Manual: TestGearsES2AWT '-gl3 -layered' --- .../jogamp/opengl/macosx/cgl/MacOSXCGLContext.java | 76 ++++++++++++++++++++-- .../classes/jogamp/opengl/shader/texture01_xxx.fp | 19 ++++++ .../classes/jogamp/opengl/shader/texture01_xxx.vp | 19 ++++++ 3 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 src/jogl/classes/jogamp/opengl/shader/texture01_xxx.fp create mode 100644 src/jogl/classes/jogamp/opengl/shader/texture01_xxx.vp (limited to 'src/jogl/classes') diff --git a/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLContext.java b/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLContext.java index f6338b04c..518d97067 100644 --- a/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLContext.java +++ b/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLContext.java @@ -51,11 +51,14 @@ import javax.media.nativewindow.NativeWindowFactory; import javax.media.nativewindow.OffscreenLayerSurface; import javax.media.nativewindow.ProxySurface; import javax.media.opengl.GL; +import javax.media.opengl.GL2ES2; +import javax.media.opengl.GL3; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLCapabilitiesImmutable; import javax.media.opengl.GLContext; import javax.media.opengl.GLException; import javax.media.opengl.GLProfile; +import javax.media.opengl.GLUniformData; import jogamp.nativewindow.macosx.OSXUtil; import jogamp.opengl.GLContextImpl; @@ -71,6 +74,9 @@ import com.jogamp.common.util.VersionNumber; import com.jogamp.gluegen.runtime.ProcAddressTable; import com.jogamp.gluegen.runtime.opengl.GLProcAddressResolver; import com.jogamp.opengl.GLExtensions; +import com.jogamp.opengl.util.PMVMatrix; +import com.jogamp.opengl.util.glsl.ShaderCode; +import com.jogamp.opengl.util.glsl.ShaderProgram; public abstract class MacOSXCGLContext extends GLContextImpl { @@ -132,6 +138,52 @@ public abstract class MacOSXCGLContext extends GLContextImpl } } + /** Static instances of GL3 core shader code, initialized lazy when required - never destroyed. */ + private static Object gl3ShaderLock = new Object(); + private static volatile boolean gl3VertexShaderInitialized = false; + private static ShaderCode gl3VertexShader = null; + private static ShaderCode gl3FragmentShader = null; + + private static ShaderProgram createCALayerShader(GL3 gl) { + // Create vertex & fragment shader code objects + if( !gl3VertexShaderInitialized ) { // volatile OK + synchronized( gl3ShaderLock ) { + if( !gl3VertexShaderInitialized ) { + final String shaderBasename = "texture01_xxx"; + gl3VertexShader = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, MacOSXCGLContext.class, + "../../shader", "../../shader/bin", shaderBasename, true); + gl3FragmentShader = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, MacOSXCGLContext.class, + "../../shader", "../../shader/bin", shaderBasename, true); + gl3VertexShader.defaultShaderCustomization(gl, true, ShaderCode.es2_default_precision_vp); + gl3FragmentShader.defaultShaderCustomization(gl, true, ShaderCode.es2_default_precision_fp); + gl3VertexShaderInitialized = true; + } + } + } + // Create & Link the shader program + final ShaderProgram sp = new ShaderProgram(); + sp.add(gl3VertexShader); + sp.add(gl3FragmentShader); + if(!sp.link(gl, System.err)) { + throw new GLException("Couldn't link program: "+sp); + } + sp.useProgram(gl, true); + + // setup mgl_PMVMatrix + final PMVMatrix pmvMatrix = new PMVMatrix(); + pmvMatrix.glMatrixMode(PMVMatrix.GL_PROJECTION); + pmvMatrix.glLoadIdentity(); + pmvMatrix.glMatrixMode(PMVMatrix.GL_MODELVIEW); + pmvMatrix.glLoadIdentity(); + final GLUniformData pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.glGetPMvMatrixf()); // P, Mv + pmvMatrixUniform.setLocation( gl.glGetUniformLocation( sp.program(), pmvMatrixUniform.getName() ) ); + gl.glUniform(pmvMatrixUniform); + + sp.useProgram(gl, false); + return sp; + } + + private boolean haveSetOpenGLMode = false; private GLBackendType openGLMode = GLBackendType.NSOPENGL; @@ -441,7 +493,8 @@ public abstract class MacOSXCGLContext extends GLContextImpl private int vsyncTimeout; // microSec - for nsOpenGLLayer mode private int lastWidth=0, lastHeight=0; // allowing to detect size change private boolean needsSetContextPBuffer = false; - + private ShaderProgram gl3ShaderProgram = null; + @Override public boolean isNSContext() { return true; } @@ -611,7 +664,16 @@ public abstract class MacOSXCGLContext extends GLContextImpl if(0>=lastWidth || 0>=lastHeight || !drawable.isRealized()) { throw new GLException("Drawable not realized yet or invalid texture size, texSize "+lastWidth+"x"+lastHeight+", "+drawable); } - nsOpenGLLayer = CGL.createNSOpenGLLayer(ctx, /* MacOSXCGLContext.this.isGL3(), */ nsOpenGLLayerPFmt, pbufferHandle, texID, chosenCaps.isBackgroundOpaque(), lastWidth, lastHeight); + final int gl3ShaderProgramName; + if( MacOSXCGLContext.this.isGL3core() ) { + if( null == gl3ShaderProgram) { + gl3ShaderProgram = createCALayerShader(MacOSXCGLContext.this.gl.getGL3()); + } + gl3ShaderProgramName = gl3ShaderProgram.program(); + } else { + gl3ShaderProgramName = 0; + } + nsOpenGLLayer = CGL.createNSOpenGLLayer(ctx, gl3ShaderProgramName, nsOpenGLLayerPFmt, pbufferHandle, texID, chosenCaps.isBackgroundOpaque(), lastWidth, lastHeight); if (DEBUG) { System.err.println("NS create nsOpenGLLayer "+toHexString(nsOpenGLLayer)+" w/ pbuffer "+toHexString(pbufferHandle)+", texID "+texID+", texSize "+lastWidth+"x"+lastHeight+", "+drawable); } @@ -632,7 +694,11 @@ public abstract class MacOSXCGLContext extends GLContextImpl // still having a valid OLS attached to surface (parent OLS could have been removed) ols.detachSurfaceLayer(); } - CGL.releaseNSOpenGLLayer(nsOpenGLLayer); + CGL.releaseNSOpenGLLayer(nsOpenGLLayer); + if( null != gl3ShaderProgram ) { + gl3ShaderProgram.destroy(MacOSXCGLContext.this.gl.getGL3()); + gl3ShaderProgram = null; + } nsOpenGLLayer = 0; } if(0 != nsOpenGLLayerPFmt) { @@ -767,10 +833,10 @@ public abstract class MacOSXCGLContext extends GLContextImpl if(res) { if(isFBO) { // trigger CALayer to update incl. possible surface change (texture) - CGL.setNSOpenGLLayerNeedsDisplayFBO(nsOpenGLLayer, texID, lastWidth, lastHeight); + CGL.setNSOpenGLLayerNeedsDisplayFBO(nsOpenGLLayer, texID); } else { // trigger CALayer to update incl. possible surface change (new pbuffer handle) - CGL.setNSOpenGLLayerNeedsDisplayPBuffer(nsOpenGLLayer, drawable.getHandle(), lastWidth, lastHeight); + CGL.setNSOpenGLLayerNeedsDisplayPBuffer(nsOpenGLLayer, drawable.getHandle()); } } } else { diff --git a/src/jogl/classes/jogamp/opengl/shader/texture01_xxx.fp b/src/jogl/classes/jogamp/opengl/shader/texture01_xxx.fp new file mode 100644 index 000000000..8b30b65f9 --- /dev/null +++ b/src/jogl/classes/jogamp/opengl/shader/texture01_xxx.fp @@ -0,0 +1,19 @@ +// Copyright 2012 JogAmp Community. All rights reserved. + +#if __VERSION__ >= 130 + #define varying in + out vec4 mgl_FragColor; + #define texture2D texture +#else + #define mgl_FragColor gl_FragColor +#endif + +varying vec2 mgl_texCoord; + +uniform sampler2D mgl_Texture0; + +void main (void) +{ + mgl_FragColor = texture2D(mgl_Texture0, mgl_texCoord); +} + diff --git a/src/jogl/classes/jogamp/opengl/shader/texture01_xxx.vp b/src/jogl/classes/jogamp/opengl/shader/texture01_xxx.vp new file mode 100644 index 000000000..d9ef6b493 --- /dev/null +++ b/src/jogl/classes/jogamp/opengl/shader/texture01_xxx.vp @@ -0,0 +1,19 @@ +// Copyright 2012 JogAmp Community. All rights reserved. + +#if __VERSION__ >= 130 + #define attribute in + #define varying out +#endif + +uniform mat4 mgl_PMVMatrix[2]; + +attribute vec4 mgl_Vertex; +attribute vec4 mgl_MultiTexCoord; + +varying vec2 mgl_texCoord; + +void main(void) +{ + mgl_texCoord = mgl_MultiTexCoord.st; + gl_Position = mgl_PMVMatrix[0] * mgl_PMVMatrix[1] * mgl_Vertex; +} -- cgit v1.2.3