From 13112b4532d656df2862a0c1c109c09a79d3412d Mon Sep 17 00:00:00 2001 From: javasabr Date: Tue, 6 Feb 2018 22:04:32 +0300 Subject: [PATCH] added new var types for shaders. --- .../java/com/jme3/renderer/opengl/GL3.java | 48 +++++++++++++ .../java/com/jme3/renderer/opengl/GL4.java | 10 +++ .../jme3/renderer/opengl/GLDebugDesktop.java | 13 ++++ .../com/jme3/renderer/opengl/GLRenderer.java | 70 ++++++++++++++----- .../main/java/com/jme3/shader/VarType.java | 4 +- .../java/com/jme3/renderer/jogl/JoglGL.java | 15 +++- .../java/com/jme3/renderer/lwjgl/LwjglGL.java | 18 +++-- .../java/com/jme3/renderer/lwjgl/LwjglGL.java | 10 +++ 8 files changed, 159 insertions(+), 29 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/renderer/opengl/GL3.java b/jme3-core/src/main/java/com/jme3/renderer/opengl/GL3.java index 595f5ff85..605e13111 100644 --- a/jme3-core/src/main/java/com/jme3/renderer/opengl/GL3.java +++ b/jme3-core/src/main/java/com/jme3/renderer/opengl/GL3.java @@ -83,6 +83,31 @@ public interface GL3 extends GL2 { public static final int GL_RGB_INTEGER = 36248; public static final int GL_RGBA_INTEGER = 36249; + public static final int GL_UNIFORM_OFFSET = 0x8A3B; + + /** + * Accepted by the {@code target} parameters of BindBuffer, BufferData, BufferSubData, MapBuffer, UnmapBuffer, GetBufferSubData, and GetBufferPointerv. + */ + public static final int GL_UNIFORM_BUFFER = 0x8A11; + + /** + * Accepted by the {@code pname} parameter of GetActiveUniformBlockiv. + */ + public static final int GL_UNIFORM_BLOCK_BINDING = 0x8A3F; + public static final int GL_UNIFORM_BLOCK_DATA_SIZE = 0x8A40; + public static final int GL_UNIFORM_BLOCK_NAME_LENGTH = 0x8A41; + public static final int GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS = 0x8A42; + public static final int GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES = 0x8A43; + public static final int GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER = 0x8A44; + public static final int GL_UNIFORM_BLOCK_REFERENCED_BY_GEOMETRY_SHADER = 0x8A45; + public static final int GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER = 0x8A46; + + /** + * Accepted by the {@code target} parameters of BindBuffer, BufferData, BufferSubData, MapBuffer, UnmapBuffer, GetBufferSubData, GetBufferPointerv, + * BindBufferRange, BindBufferOffset and BindBufferBase. + */ + public static final int GL_TRANSFORM_FEEDBACK_BUFFER = 0x8C8E; + /** *

Reference Page

*

@@ -128,4 +153,27 @@ public interface GL3 extends GL2 { * @param index the index of the particular element being queried. */ public String glGetString(int name, int index); /// GL3+ + + + /** + *

Reference Page

+ * + * Retrieves the index of a named uniform block. + * + * @param program the name of a program containing the uniform block. + * @param uniformBlockName an array of characters to containing the name of the uniform block whose index to retrieve. + * @return the block index. + */ + public int glGetUniformBlockIndex(int program, String uniformBlockName); + + /** + *

Reference Page

+ * + * Binds a buffer object to an indexed buffer target. + * + * @param target the target of the bind operation. One of:
{@link #GL_TRANSFORM_FEEDBACK_BUFFER TRANSFORM_FEEDBACK_BUFFER}{@link #GL_UNIFORM_BUFFER UNIFORM_BUFFER}{@link GL4#GL_ATOMIC_COUNTER_BUFFER ATOMIC_COUNTER_BUFFER}{@link GL4#GL_SHADER_STORAGE_BUFFER SHADER_STORAGE_BUFFER}
+ * @param index the index of the binding point within the array specified by {@code target} + * @param buffer a buffer object to bind to the specified binding point + */ + public void glBindBufferBase(int target, int index, int buffer); } diff --git a/jme3-core/src/main/java/com/jme3/renderer/opengl/GL4.java b/jme3-core/src/main/java/com/jme3/renderer/opengl/GL4.java index 9afe4755b..b6d644ba4 100644 --- a/jme3-core/src/main/java/com/jme3/renderer/opengl/GL4.java +++ b/jme3-core/src/main/java/com/jme3/renderer/opengl/GL4.java @@ -42,6 +42,16 @@ public interface GL4 extends GL3 { public static final int GL_TESS_EVALUATION_SHADER = 0x8E87; public static final int GL_PATCHES = 0xE; + /** + * Accepted by the {@code target} parameter of BindBufferBase and BindBufferRange. + */ + public static final int GL_ATOMIC_COUNTER_BUFFER = 0x92C0; + + /** + * Accepted by the {@code target} parameters of BindBuffer, BufferData, BufferSubData, MapBuffer, UnmapBuffer, GetBufferSubData, and GetBufferPointerv. + */ + public static final int GL_SHADER_STORAGE_BUFFER = 0x90D2; + /** *

Reference Page

*

diff --git a/jme3-core/src/main/java/com/jme3/renderer/opengl/GLDebugDesktop.java b/jme3-core/src/main/java/com/jme3/renderer/opengl/GLDebugDesktop.java index a945a1d85..8f1cb535e 100644 --- a/jme3-core/src/main/java/com/jme3/renderer/opengl/GLDebugDesktop.java +++ b/jme3-core/src/main/java/com/jme3/renderer/opengl/GLDebugDesktop.java @@ -83,6 +83,19 @@ public class GLDebugDesktop extends GLDebugES implements GL2, GL3, GL4 { return result; } + @Override + public int glGetUniformBlockIndex(final int program, final String uniformBlockName) { + final int result = gl3.glGetUniformBlockIndex(program, uniformBlockName); + checkError(); + return result; + } + + @Override + public void glBindBufferBase(final int target, final int index, final int buffer) { + gl3.glBindBufferBase(target, index, buffer); + checkError(); + } + @Override public void glDeleteVertexArrays(IntBuffer arrays) { gl3.glDeleteVertexArrays(arrays); diff --git a/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java b/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java index 746db4084..76b16c692 100644 --- a/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java +++ b/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java @@ -61,17 +61,17 @@ import com.jme3.util.BufferUtils; import com.jme3.util.ListMap; 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 jme3tools.shader.ShaderDebug; + +import java.nio.ByteBuffer; +import java.nio.FloatBuffer; +import java.nio.IntBuffer; +import java.nio.ShortBuffer; +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; public final class GLRenderer implements Renderer { @@ -1056,6 +1056,18 @@ public final class GLRenderer implements Renderer { gl.resetStats(); } + protected void bindProgram(Shader shader) { + int shaderId = shader.getId(); + if (context.boundShaderProgram != shaderId) { + gl.glUseProgram(shaderId); + statistics.onShaderUse(shader, true); + context.boundShader = shader; + context.boundShaderProgram = shaderId; + } else { + statistics.onShaderUse(shader, false); + } + } + /*********************************************************************\ |* Shaders *| \*********************************************************************/ @@ -1070,18 +1082,6 @@ public final class GLRenderer implements Renderer { } } - protected void bindProgram(Shader shader) { - int shaderId = shader.getId(); - if (context.boundShaderProgram != shaderId) { - gl.glUseProgram(shaderId); - statistics.onShaderUse(shader, true); - context.boundShader = shader; - context.boundShaderProgram = shaderId; - } else { - statistics.onShaderUse(shader, false); - } - } - protected void updateUniform(Shader shader, Uniform uniform) { int shaderId = shader.getId(); @@ -1182,6 +1182,38 @@ public final class GLRenderer implements Renderer { Integer i = (Integer) uniform.getValue(); gl.glUniform1i(loc, i.intValue()); break; + case Struct: { + + final ByteBuffer data = (ByteBuffer) uniform.getValue(); + + intBuf1.clear(); + gl3.glGenBuffers(intBuf1); + + final int bufferAddress = intBuf1.get(); + final int blockIndex = gl3.glGetUniformBlockIndex(shaderId, uniform.getName()); + + gl3.glBindBuffer(GL3.GL_UNIFORM_BUFFER, bufferAddress); + gl3.glBufferData(GL3.GL_UNIFORM_BUFFER, data, GL.GL_DYNAMIC_DRAW); + gl3.glBindBufferBase(GL3.GL_UNIFORM_BUFFER, blockIndex, bufferAddress); + break; + } + case StructArray: { + + //TODO + final ByteBuffer data = (ByteBuffer) uniform.getValue(); + + intBuf1.clear(); + gl3.glGenBuffers(intBuf1); + + final int bufferAddress = intBuf1.get(); + final int blockIndex = gl3.glGetUniformBlockIndex(shaderId, uniform.getName()); + + gl3.glBindBuffer(GL3.GL_UNIFORM_BUFFER, bufferAddress); + gl3.glBufferData(GL3.GL_UNIFORM_BUFFER, data, GL.GL_DYNAMIC_DRAW); + gl3.glBindBufferBase(GL3.GL_UNIFORM_BUFFER, blockIndex, bufferAddress); + break; + } + default: throw new UnsupportedOperationException("Unsupported uniform type: " + uniform.getVarType()); } diff --git a/jme3-core/src/main/java/com/jme3/shader/VarType.java b/jme3-core/src/main/java/com/jme3/shader/VarType.java index 7300294d4..6e33eb5b3 100644 --- a/jme3-core/src/main/java/com/jme3/shader/VarType.java +++ b/jme3-core/src/main/java/com/jme3/shader/VarType.java @@ -57,7 +57,9 @@ public enum VarType { Texture3D(false,true,"sampler3D"), TextureArray(false,true,"sampler2DArray|sampler2DArrayShadow"), TextureCubeMap(false,true,"samplerCube"), - Int("int"); + Int("int"), + Struct(false, false, "dynamic"), + StructArray(false, false, "dynamic"); private boolean usesMultiData = false; private boolean textureType = false; diff --git a/jme3-jogl/src/main/java/com/jme3/renderer/jogl/JoglGL.java b/jme3-jogl/src/main/java/com/jme3/renderer/jogl/JoglGL.java index e6435e5c5..bb9797cf9 100644 --- a/jme3-jogl/src/main/java/com/jme3/renderer/jogl/JoglGL.java +++ b/jme3-jogl/src/main/java/com/jme3/renderer/jogl/JoglGL.java @@ -4,12 +4,11 @@ import com.jme3.renderer.RendererException; import com.jme3.renderer.opengl.GL; import com.jme3.renderer.opengl.GL2; import com.jme3.renderer.opengl.GL3; - -import java.nio.*; - import com.jme3.renderer.opengl.GL4; import com.jogamp.opengl.GLContext; +import java.nio.*; + public class JoglGL implements GL, GL2, GL3, GL4 { private static int getLimitBytes(ByteBuffer buffer) { @@ -634,4 +633,14 @@ public class JoglGL implements GL, GL2, GL3, GL4 { checkLimit(arrays); GLContext.getCurrentGL().getGL2ES3().glDeleteVertexArrays(arrays.limit(), arrays); } + + @Override + public int glGetUniformBlockIndex(final int program, final String uniformBlockName) { + return GLContext.getCurrentGL().getGL3bc().glGetUniformBlockIndex(program, uniformBlockName); + } + + @Override + public void glBindBufferBase(final int target, final int index, final int buffer) { + GLContext.getCurrentGL().getGL3bc().glBindBufferBase(target, index, buffer); + } } diff --git a/jme3-lwjgl/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java b/jme3-lwjgl/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java index 5a74e06f0..f040d39ca 100644 --- a/jme3-lwjgl/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java +++ b/jme3-lwjgl/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java @@ -4,16 +4,12 @@ import com.jme3.renderer.RendererException; import com.jme3.renderer.opengl.GL; import com.jme3.renderer.opengl.GL2; import com.jme3.renderer.opengl.GL3; -import java.nio.Buffer; -import java.nio.ByteBuffer; -import java.nio.FloatBuffer; -import java.nio.IntBuffer; -import java.nio.ShortBuffer; - import com.jme3.renderer.opengl.GL4; import com.jme3.util.BufferUtils; import org.lwjgl.opengl.*; +import java.nio.*; + public final class LwjglGL implements GL, GL2, GL3, GL4 { IntBuffer tmpBuff = BufferUtils.createIntBuffer(1); @@ -493,4 +489,14 @@ public final class LwjglGL implements GL, GL2, GL3, GL4 { checkLimit(arrays); ARBVertexArrayObject.glDeleteVertexArrays(arrays); } + + @Override + public int glGetUniformBlockIndex(final int program, final String uniformBlockName) { + return GL31.glGetUniformBlockIndex(program, uniformBlockName); + } + + @Override + public void glBindBufferBase(final int target, final int index, final int buffer) { + GL30.glBindBufferBase(target, index, buffer); + } } diff --git a/jme3-lwjgl3/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java b/jme3-lwjgl3/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java index e87bdf20a..d8540e39a 100644 --- a/jme3-lwjgl3/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java +++ b/jme3-lwjgl3/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java @@ -621,4 +621,14 @@ public class LwjglGL extends LwjglRender implements GL, GL2, GL3, GL4 { checkLimit(arrays); ARBVertexArrayObject.glDeleteVertexArrays(arrays); } + + @Override + public int glGetUniformBlockIndex(final int program, final String uniformBlockName) { + return GL31.glGetUniformBlockIndex(program, uniformBlockName); + } + + @Override + public void glBindBufferBase(final int target, final int index, final int buffer) { + GL30.glBindBufferBase(target, index, buffer); + } }