Skip to content

Commit

Permalink
More tests after loadCapabilities refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kristinfjola committed Apr 3, 2016
2 parents 1eaab97 + f8682a3 commit 0e2c744
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 75 deletions.
97 changes: 48 additions & 49 deletions jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,15 @@
import com.jme3.util.MipMapGenerator;
import com.jme3.util.NativeObjectManager;
import java.nio.*;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jme3tools.shader.ShaderDebug;

import static java.util.Arrays.asList;

public class GLRenderer implements Renderer {

private static final Logger logger = Logger.getLogger(GLRenderer.class.getName());
Expand Down Expand Up @@ -138,7 +136,7 @@ protected HashSet<String> loadExtensions() {
extensionSet.add(extension);
}
} else {
extensionSet.addAll(Arrays.asList(gl.glGetString(GL.GL_EXTENSIONS).split(" ")));
extensionSet.addAll(asList(gl.glGetString(GL.GL_EXTENSIONS).split(" ")));
}
return extensionSet;
}
Expand All @@ -163,40 +161,38 @@ private boolean hasExtension(String extensionName) {
return extensions.contains(extensionName);
}

/**
* Load Embedded System Capabilities.
* Important: Do not add OpenGL20 - that's the desktop capability!
*/
private void loadCapabilitiesES() {
caps.add(Caps.GLSL100);
caps.add(Caps.OpenGLES20);

// Important: Do not add OpenGL20 - that's the desktop capability!
}

protected void loadCapabilitiesGL2() {
int oglVer = extractVersion(gl.glGetString(GL.GL_VERSION));

if (oglVer >= 200) {
caps.add(Caps.OpenGL20);
if (oglVer >= 210) {
caps.add(Caps.OpenGL21);
if (oglVer >= 300) {
caps.add(Caps.OpenGL30);
if (oglVer >= 310) {
caps.add(Caps.OpenGL31);
if (oglVer >= 320) {
caps.add(Caps.OpenGL32);
}
if (oglVer >= 330) {
caps.add(Caps.OpenGL33);
caps.add(Caps.GeometryShader);
}
if (oglVer >= 400) {
caps.add(Caps.OpenGL40);
caps.add(Caps.TesselationShader);
}
}
Map<Integer, List<Caps>> versionCapMap = getVersionCapabilityMapping();

for (Map.Entry<Integer, List<Caps>> entry : versionCapMap.entrySet()) {
if (oglVer >= entry.getKey()) {
for (Caps cap : entry.getValue()) {
caps.add(cap);
}
}
}

// Fix issue in TestRenderToMemory when GL.GL_FRONT is the main
// buffer being used.
context.initialDrawBuf = getInteger(GL2.GL_DRAW_BUFFER);
context.initialReadBuf = getInteger(GL2.GL_READ_BUFFER);
}

/**
* Load GL Shading Language capabilities
*/
protected void loadCapabilitiesGLSL() {
int glslVer = extractVersion(gl.glGetString(GL.GL_SHADING_LANGUAGE_VERSION));

switch (glslVer) {
Expand Down Expand Up @@ -224,25 +220,18 @@ protected void loadCapabilitiesGL2() {
caps.add(Caps.GLSL100);
break;
}
}

// Workaround, always assume we support GLSL100 & GLSL110
// Supporting OpenGL 2.0 means supporting GLSL 1.10.
/**
* Workaround, always assume we support GLSL100 & GLSL110
* Supporting OpenGL 2.0 means supporting GLSL 1.10.
*/
protected void loadCapabilitiesGLSL1Support() {
caps.add(Caps.GLSL110);
caps.add(Caps.GLSL100);

// Fix issue in TestRenderToMemory when GL.GL_FRONT is the main
// buffer being used.
context.initialDrawBuf = getInteger(GL2.GL_DRAW_BUFFER);
context.initialReadBuf = getInteger(GL2.GL_READ_BUFFER);

// XXX: This has to be GL.GL_BACK for canvas on Mac
// Since initialDrawBuf is GL.GL_FRONT for pbuffer, gotta
// change this value later on ...
// initialDrawBuf = GL.GL_BACK;
// initialReadBuf = GL.GL_BACK;
}

private void loadCapabilitiesCommon() {
protected void loadCapabilitiesCommon() {
extensions = loadExtensions();

limits.put(Limits.VertexTextureUnits, getInteger(GL.GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS));
Expand All @@ -252,13 +241,6 @@ private void loadCapabilitiesCommon() {

limits.put(Limits.FragmentTextureUnits, getInteger(GL.GL_MAX_TEXTURE_IMAGE_UNITS));

// gl.glGetInteger(GL.GL_MAX_VERTEX_UNIFORM_COMPONENTS, intBuf16);
// vertexUniforms = intBuf16.get(0);
// logger.log(Level.FINER, "Vertex Uniforms: {0}", vertexUniforms);
//
// gl.glGetInteger(GL.GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, intBuf16);
// fragUniforms = intBuf16.get(0);
// logger.log(Level.FINER, "Fragment Uniforms: {0}", fragUniforms);
if (caps.contains(Caps.OpenGLES20)) {
limits.put(Limits.VertexUniformVectors, getInteger(GL.GL_MAX_VERTEX_UNIFORM_VECTORS));
} else {
Expand Down Expand Up @@ -483,6 +465,8 @@ private void loadCapabilitiesCommon() {
protected void loadCapabilities() {
if (gl2 != null) {
loadCapabilitiesGL2();
loadCapabilitiesGLSL();
loadCapabilitiesGLSL1Support();
} else {
loadCapabilitiesES();
}
Expand Down Expand Up @@ -2729,4 +2713,19 @@ public void setLinearizeSrgbImages(boolean linearize) {
linearizeSrgbImages = linearize;
}
}

public Map<Integer,List<Caps>> getVersionCapabilityMapping() {

Map<Integer,List<Caps>> map = new TreeMap<>();

map.put(200, Collections.singletonList(Caps.OpenGL20));
map.put(210, Collections.singletonList(Caps.OpenGL21));
map.put(300, Collections.singletonList(Caps.OpenGL30));
map.put(310, Collections.singletonList(Caps.OpenGL31));
map.put(320, Collections.singletonList(Caps.OpenGL32));
map.put(330, asList(Caps.OpenGL33, Caps.GeometryShader));
map.put(400, asList(Caps.OpenGL40, Caps.TesselationShader));

return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ public void setUp() throws Exception {
initMocks(this);
r = spy(new GLRenderer(gl, glExt, glFbo));


doReturn(new HashSet<String>()).when(r).loadExtensions();
}

/**
* Tests initialize with a GL2 version
*/
@Test
public void initialize() {
doReturn(310).when(r).extractVersion(null);
Expand All @@ -39,68 +41,87 @@ public void initialize() {

verify(r).loadCapabilities();
verify(r).loadCapabilitiesGL2();
verify(r).loadCapabilitiesGLSL();
verify(r).loadCapabilitiesGLSL1Support();
verify(r).loadCapabilitiesCommon();
}


@Test
public void loadCapabilitiesGL2Common() throws Exception {
public void loadCapabilitiesGL2RealVersion() throws Exception {
doReturn(310).when(r).extractVersion(null);

r.initialize();

assertTrue(r.caps.contains(Caps.GLSL110));
assertTrue(r.caps.contains(Caps.GLSL100));
assertTrue(r.caps.contains(Caps.OpenGL20));
assertTrue(r.caps.contains(Caps.OpenGL21));
assertTrue(r.caps.contains(Caps.OpenGL30));
assertTrue(r.caps.contains(Caps.OpenGL31));
assertFalse(r.caps.contains(Caps.OpenGL32));
assertFalse(r.caps.contains(Caps.OpenGL33));
assertFalse(r.caps.contains(Caps.OpenGL40));

assertNotNull(r.context.initialDrawBuf);
assertNotNull(r.context.initialReadBuf);
}

@Test
public void loadCapabilitiesGL2oglVer() throws Exception {
doReturn(310).when(r).extractVersion(null);
public void loadCapabilitiesGL2NotRealVersion() throws Exception {
doReturn(213).when(r).extractVersion(null);

r.initialize();

assertTrue(r.caps.contains(Caps.OpenGL20));
assertTrue(r.caps.contains(Caps.OpenGL21));
assertTrue(r.caps.contains(Caps.OpenGL30));
assertTrue(r.caps.contains(Caps.OpenGL31));
assertFalse(r.caps.contains(Caps.OpenGL30));
assertFalse(r.caps.contains(Caps.OpenGL31));
assertFalse(r.caps.contains(Caps.OpenGL32));
assertFalse(r.caps.contains(Caps.OpenGL33));
assertFalse(r.caps.contains(Caps.OpenGL40));

assertNotNull(r.context.initialDrawBuf);
assertNotNull(r.context.initialReadBuf);
}

@Test
public void loadCapabilitiesGL2glslVer() throws Exception {
public void loadCapabilitiesGLSLRealVersion() throws Exception {
// a number that is a real version
doReturn(150).when(r).extractVersion(null);

r.initialize();

assertFalse(r.caps.contains(Caps.GLSL400));
assertFalse(r.caps.contains(Caps.GLSL330));
assertTrue(r.caps.contains(Caps.GLSL150));
assertTrue(r.caps.contains(Caps.GLSL140));
assertTrue(r.caps.contains(Caps.GLSL130));
assertTrue(r.caps.contains(Caps.GLSL120));
assertTrue(r.caps.contains(Caps.GLSL110));
assertTrue(r.caps.contains(Caps.GLSL100));
}

@Test
public void loadCapabilitiesGLSLNotRealVersion() throws Exception {
doReturn(124).when(r).extractVersion(null);

r.initialize();

assertFalse(r.caps.contains(Caps.GLSL400));
assertFalse(r.caps.contains(Caps.GLSL330));
assertFalse(r.caps.contains(Caps.GLSL150));
assertFalse(r.caps.contains(Caps.GLSL140));
assertFalse(r.caps.contains(Caps.GLSL130));
assertTrue(r.caps.contains(Caps.GLSL120));
assertTrue(r.caps.contains(Caps.GLSL110));
assertTrue(r.caps.contains(Caps.GLSL100));
}

public class TestGLRenderer extends GLRenderer {

public TestGLRenderer(GL gl, GLExt glext, GLFbo glfbo) {
super(gl, glext, glfbo);
}
/*
@Override
public int extractVersion(String version) {
return 200;
}
*/

@Override
protected HashSet<String> loadExtensions() {
return new HashSet<String>();
}
@Test
public void loadCapabilitiesGLSL1Support() throws Exception {
doReturn(310).when(r).extractVersion(null);

r.initialize();

assertTrue(r.caps.contains(Caps.GLSL110));
assertTrue(r.caps.contains(Caps.GLSL100));
}
}

0 comments on commit 0e2c744

Please sign in to comment.