Skip to content

Commit

Permalink
GL ES 3.0 megapatch (#2061)
Browse files Browse the repository at this point in the history
* Extend GL ES 3.0 support. Enable CoreProfile capability when using GL ES 3.0. Add WebGL caps.

* Fix contrast adjustment matdef

* Improve GL ->GLES compatibility: Detect if RGB float attachment is supported, implement correct glDrawBuffer shim, add explicit GLES 300 support in all materials

* Allow setReadDrawBuffers in GLES 30+, add DepthTexture capability in GLES 30+

* Make passing boundDrawBufferId to shaders toggleable (default true). Disable in tests

* Revert "Allow setReadDrawBuffers in GLES 30+, add DepthTexture capability in GLES 30+"

This reverts commit d8c7e96.

* Fix tests

* Set default precision in GLSLCompat only if supported

* Set highp for samplerCube and int. Do not check for GL_FRAGMENT_PRECISION_HIGH as it is not always declared despite the specs.

* Fix GLES formats declaration
  • Loading branch information
riccardobl committed Sep 2, 2023
1 parent e883dd7 commit 4e851bf
Show file tree
Hide file tree
Showing 75 changed files with 568 additions and 343 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
public class AndroidGL implements GL, GL2, GLES_30, GLExt, GLFbo {

IntBuffer tmpBuff = BufferUtils.createIntBuffer(1);
IntBuffer tmpBuff16 = BufferUtils.createIntBuffer(16);

@Override
public void resetStats() {
Expand Down Expand Up @@ -694,10 +695,17 @@ public void glPolygonMode(int face, int mode) {
// Wrapper to DrawBuffers as there's no DrawBuffer method in GLES
@Override
public void glDrawBuffer(int mode) {
tmpBuff.clear();
tmpBuff.put(0, mode);
tmpBuff.rewind();
glDrawBuffers(tmpBuff);
int nBuffers = (mode - GLFbo.GL_COLOR_ATTACHMENT0_EXT) + 1;
if (nBuffers <= 0 || nBuffers > 16) {
throw new IllegalArgumentException("Draw buffer outside range: " + Integer.toHexString(mode));
}
tmpBuff16.clear();
for (int i = 0; i < nBuffers - 1; i++) {
tmpBuff16.put(GL.GL_NONE);
}
tmpBuff16.put(mode);
tmpBuff16.flip();
glDrawBuffers(tmpBuff16);
}

@Override
Expand Down Expand Up @@ -729,5 +737,21 @@ public void glTexSubImage3D(int target, int level, int xoffset, int yoffset, int
GLES30.glTexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, data);
}

@Override
public void glBindVertexArray(int array) {
GLES30.glBindVertexArray(array);
}

@Override
public void glDeleteVertexArrays(IntBuffer arrays) {
GLES30.glDeleteVertexArrays(arrays.limit(),arrays);
}

@Override
public void glGenVertexArrays(IntBuffer arrays) {
GLES30.glGenVertexArrays(arrays.limit(),arrays);

}

}

Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,12 @@ public void initialize(RenderManager rm, ViewPort vp) {
fsQuad.setHeight(1);

if (!renderer.getCaps().contains(Caps.PackedFloatTexture)) {
if (!renderer.getCaps().contains(Caps.FloatTexture)) {
fbFormat = Format.RGB8;
} else {
if(renderer.getCaps().contains(Caps.FloatColorBufferRGB)){
fbFormat = Format.RGB16F;
} else if(renderer.getCaps().contains(Caps.FloatColorBufferRGBA)){
fbFormat = Format.RGBA16F;
} else {
fbFormat = Format.RGB8;
}
}

Expand Down
6 changes: 4 additions & 2 deletions jme3-core/src/main/java/com/jme3/post/HDRRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public class HDRRenderer implements SceneProcessor {
private float whiteLevel = 100f;
private float throttle = -1;
private int maxIterations = -1;
private Image.Format bufFormat = Format.RGB16F;
private Image.Format bufFormat = Format.RGB8;

private MinFilter fbMinFilter = MinFilter.BilinearNoMipMaps;
private MagFilter fbMagFilter = MagFilter.Bilinear;
Expand All @@ -106,8 +106,10 @@ public HDRRenderer(AssetManager manager, Renderer renderer) {
Collection<Caps> caps = renderer.getCaps();
if (caps.contains(Caps.PackedFloatColorBuffer))
bufFormat = Format.RGB111110F;
else if (caps.contains(Caps.FloatColorBuffer))
else if (caps.contains(Caps.FloatColorBufferRGB))
bufFormat = Format.RGB16F;
else if (caps.contains(Caps.FloatColorBufferRGBA))
bufFormat = Format.RGBA16F;
else {
enabled = false;
return;
Expand Down
16 changes: 16 additions & 0 deletions jme3-core/src/main/java/com/jme3/renderer/Caps.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ public enum Caps {
*/
FloatTexture,

/**
* Supports rendering on RGB floating point textures
*/
FloatColorBufferRGB,

/**
* Supports rendering on RGBA floating point textures
*/
FloatColorBufferRGBA,

/**
* Supports integer textures.
*/
Expand All @@ -236,6 +246,7 @@ public enum Caps {
* Supports floating point FBO color buffers (Format.RGB16F).
*/
FloatColorBuffer,


/**
* Supports floating point depth buffer.
Expand Down Expand Up @@ -341,6 +352,11 @@ public enum Caps {
*/
OpenGLES20,

/**
* Supports WebGL
*/
WebGL,

/**
* Supports RGB8 / RGBA8 textures.
*/
Expand Down
40 changes: 39 additions & 1 deletion jme3-core/src/main/java/com/jme3/renderer/RenderManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@
import com.jme3.shader.Shader;
import com.jme3.shader.UniformBinding;
import com.jme3.shader.UniformBindingManager;
import com.jme3.shader.VarType;
import com.jme3.system.NullRenderer;
import com.jme3.system.Timer;
import com.jme3.texture.FrameBuffer;
import com.jme3.util.SafeArrayList;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -101,6 +103,7 @@ public class RenderManager {
private LightFilter lightFilter = new DefaultLightFilter();
private TechniqueDef.LightMode preferredLightMode = TechniqueDef.LightMode.MultiPass;
private int singlePassLightBatchSize = 1;
private MatParamOverride boundDrawBufferId=new MatParamOverride(VarType.Int,"BoundDrawBuffer",0);


/**
Expand All @@ -111,6 +114,7 @@ public class RenderManager {
*/
public RenderManager(Renderer renderer) {
this.renderer = renderer;
this.forcedOverrides.add(boundDrawBufferId);
}

/**
Expand Down Expand Up @@ -629,6 +633,12 @@ public void renderGeometry(Geometry geom) {
setWorldMatrix(geom.getWorldMatrix());
}

// Use material override to pass the current target index (used in api such as GL ES that do not support glDrawBuffer)
FrameBuffer currentFb = this.renderer.getCurrentFrameBuffer();
if (currentFb != null && !currentFb.isMultiTarget()) {
this.boundDrawBufferId.setValue(currentFb.getTargetIndex());
}

// Perform light filtering if we have a light filter.
LightList lightList = geom.getWorldLightList();

Expand All @@ -639,7 +649,7 @@ public void renderGeometry(Geometry geom) {
}

Material material = geom.getMaterial();

// If forcedTechnique exists, we try to force it for the render.
// If it does not exist in the mat def, we check for forcedMaterial and render the geom if not null.
// Otherwise, the geometry is not rendered.
Expand Down Expand Up @@ -1298,4 +1308,32 @@ public void render(float tpf, boolean mainFrameBufferActive) {
}
}
}


/**
* Returns true if the draw buffer target id is passed to the shader.
*
* @return True if the draw buffer target id is passed to the shaders.
*/
public boolean getPassDrawBufferTargetIdToShaders() {
return this.forcedOverrides.contains(boundDrawBufferId);
}

/**
* Enable or disable passing the draw buffer target id to the shaders. This
* is needed to handle FrameBuffer.setTargetIndex correctly in some
* backends.
*
* @param v
* True to enable, false to disable (default is true)
*/
public void setPassDrawBufferTargetIdToShaders(boolean v) {
if (v) {
if (!this.forcedOverrides.contains(boundDrawBufferId)) {
this.forcedOverrides.add(boundDrawBufferId);
}
} else {
this.forcedOverrides.remove(boundDrawBufferId);
}
}
}
6 changes: 6 additions & 0 deletions jme3-core/src/main/java/com/jme3/renderer/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -527,4 +527,10 @@ public default void pushDebugGroup(String name) {

}

/**
* Returns the current FrameBuffer that is being rendered to.
* @return the FrameBuffer or null if rendering to the screen.
*/
public FrameBuffer getCurrentFrameBuffer();

}
7 changes: 7 additions & 0 deletions jme3-core/src/main/java/com/jme3/renderer/opengl/GLES_30.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
*/
package com.jme3.renderer.opengl;

import java.nio.IntBuffer;

/**
* GL functions and constants only available on vanilla OpenGL ES 3.0.
*
Expand All @@ -40,5 +42,10 @@ public interface GLES_30 extends GL {

public static final int GL_RGB10_A2 = 0x8059;
public static final int GL_UNSIGNED_INT_2_10_10_10_REV = 0x8368;

public void glBindVertexArray(int array);

public void glDeleteVertexArrays(IntBuffer arrays);

public void glGenVertexArrays(IntBuffer arrays);
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static GLImageFormat[][] getFormatsForCaps(EnumSet<Caps> caps) {
formatSrgbSwiz(formatToGL, Format.Luminance8Alpha8, GLExt.GL_SRGB8_ALPHA8_EXT, GL3.GL_RG, GL.GL_UNSIGNED_BYTE);
}

if (caps.contains(Caps.OpenGL20)) {
if (caps.contains(Caps.OpenGL20)||caps.contains(Caps.OpenGLES30)) {
if (!caps.contains(Caps.CoreProfile)) {
format(formatToGL, Format.Alpha8, GL2.GL_ALPHA8, GL.GL_ALPHA, GL.GL_UNSIGNED_BYTE);
format(formatToGL, Format.Luminance8, GL2.GL_LUMINANCE8, GL.GL_LUMINANCE, GL.GL_UNSIGNED_BYTE);
Expand Down
Loading

0 comments on commit 4e851bf

Please sign in to comment.