refactoring.

JavaSaBr-added_new_var_type
JavaSaBr 7 years ago committed by Nehon
parent b342b1ede1
commit a71b418c52
  1. 16
      jme3-core/src/main/java/com/jme3/material/Material.java
  2. 7
      jme3-core/src/main/java/com/jme3/renderer/Renderer.java
  3. 20
      jme3-core/src/main/java/com/jme3/renderer/opengl/GL3.java
  4. 6
      jme3-core/src/main/java/com/jme3/renderer/opengl/GLDebugDesktop.java
  5. 45
      jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java
  6. 52
      jme3-core/src/main/java/com/jme3/shader/Shader.java
  7. 20
      jme3-core/src/main/java/com/jme3/shader/ShaderBufferBlock.java
  8. 7
      jme3-core/src/main/java/com/jme3/system/NullRenderer.java
  9. 21
      jme3-core/src/test/java/com/jme3/SetupTest.java
  10. 5
      jme3-jogl/src/main/java/com/jme3/renderer/jogl/JoglGL.java
  11. 5
      jme3-lwjgl/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java
  12. 5
      jme3-lwjgl3/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java

@ -822,10 +822,10 @@ public class Material implements CloneableSmartAsset, Cloneable, Savable {
MatParam param = paramValues.getValue(i); MatParam param = paramValues.getValue(i);
VarType type = param.getVarType(); VarType type = param.getVarType();
if(type == VarType.ShaderStorageBufferObject) { if(isBO(type)) {
final StorageBlock storageBlock = shader.getStorageBlock(param.getPrefixedName()); final ShaderBufferBlock bufferBlock = shader.getBufferBlock(param.getPrefixedName());
storageBlock.setStorageData(param.getValue()); bufferBlock.setBufferObject((BufferObject) param.getValue());
} else { } else {
@ -848,6 +848,16 @@ public class Material implements CloneableSmartAsset, Cloneable, Savable {
return unit; return unit;
} }
/**
* Returns true if the type is Buffer Object's type.
*
* @param type the material parameter type.
* @return true if the type is Buffer Object's type.
*/
private boolean isBO(final VarType type) {
return type == VarType.ShaderStorageBufferObject || type == VarType.UniformBufferObject;
}
private void updateRenderState(RenderManager renderManager, Renderer renderer, TechniqueDef techniqueDef) { private void updateRenderState(RenderManager renderManager, Renderer renderer, TechniqueDef techniqueDef) {
if (renderManager.getForcedRenderState() != null) { if (renderManager.getForcedRenderState() != null) {
renderer.applyRenderState(renderManager.getForcedRenderState()); renderer.applyRenderState(renderManager.getForcedRenderState());

@ -270,13 +270,6 @@ public interface Renderer {
*/ */
public void updateBufferData(VertexBuffer vb); public void updateBufferData(VertexBuffer vb);
/**
* Uploads a shader storage buffer object to the GPU.
*
* @param ssbo the shader storage buffer object to upload.
*/
public void updateBufferData(ShaderStorageBufferObject ssbo);
/** /**
* Uploads data of the buffer object on the GPU. * Uploads data of the buffer object on the GPU.
* *

@ -191,4 +191,24 @@ public interface GL3 extends GL2 {
* @param buffer a buffer object to bind to the specified binding point * @param buffer a buffer object to bind to the specified binding point
*/ */
public void glBindBufferBase(int target, int index, int buffer); public void glBindBufferBase(int target, int index, int buffer);
/**
* Binding points for active uniform blocks are assigned using glUniformBlockBinding. Each of a program's active
* uniform blocks has a corresponding uniform buffer binding point. program is the name of a program object for
* which the command glLinkProgram has been issued in the past.
* <p>
* If successful, glUniformBlockBinding specifies that program will use the data store of the buffer object bound
* to the binding point uniformBlockBinding to extract the values of the uniforms in the uniform block identified
* by uniformBlockIndex.
* <p>
* When a program object is linked or re-linked, the uniform buffer object binding point assigned to each of its
* active uniform blocks is reset to zero.
*
* @param program The name of a program object containing the active uniform block whose binding to
* assign.
* @param uniformBlockIndex The index of the active uniform block within program whose binding to assign.
* @param uniformBlockBinding Specifies the binding point to which to bind the uniform block with index
* uniformBlockIndex within program.
*/
public void glUniformBlockBinding(int program, int uniformBlockIndex, int uniformBlockBinding);
} }

@ -125,4 +125,10 @@ public class GLDebugDesktop extends GLDebugES implements GL2, GL3, GL4 {
gl.glBlendEquationSeparate(colorMode, alphaMode); gl.glBlendEquationSeparate(colorMode, alphaMode);
checkError(); checkError();
} }
@Override
public void glUniformBlockBinding(final int program, final int uniformBlockIndex, final int uniformBlockBinding) {
gl3.glUniformBlockBinding(program, uniformBlockIndex, uniformBlockBinding);
checkError();
}
} }

@ -1207,33 +1207,40 @@ public final class GLRenderer implements Renderer {
} }
/** /**
* Update the storage block for the shader. * Updates the buffer block for the shader.
* *
* @param shader the shader. * @param shader the shader.
* @param storageBlock the storage block. * @param bufferBlock the storage block.
*/ */
protected void updateShaderStorageBlock(final Shader shader, final StorageBlock storageBlock) { protected void updateShaderBufferBlock(final Shader shader, final ShaderBufferBlock bufferBlock) {
assert storageBlock.getName() != null; assert bufferBlock.getName() != null;
assert shader.getId() > 0; assert shader.getId() > 0;
final ShaderStorageBufferObject storageData = (ShaderStorageBufferObject) storageBlock.getStorageData(); final BufferObject bufferObject = bufferBlock.getBufferObject();
if (storageData.getUniqueId() == -1 || storageData.isUpdateNeeded()) { if (bufferObject.getUniqueId() == -1 || bufferObject.isUpdateNeeded()) {
updateBufferData(storageData); updateBufferData(bufferObject);
} }
if (storageBlock.isUpdateNeeded()) { if (!bufferBlock.isUpdateNeeded()) {
return;
}
bindProgram(shader); bindProgram(shader);
final int shaderId = shader.getId(); final int shaderId = shader.getId();
final int blockIndex = gl4.glGetProgramResourceIndex(shaderId, GL4.GL_SHADER_STORAGE_BLOCK, storageBlock.getName());
gl4.glShaderStorageBlockBinding(shaderId, blockIndex, storageData.getBinding()); if (bufferObject instanceof ShaderStorageBufferObject) {
gl4.glBindBufferBase(GL4.GL_SHADER_STORAGE_BUFFER, storageData.getBinding(), storageData.getId()); final int blockIndex = gl4.glGetProgramResourceIndex(shaderId, GL4.GL_SHADER_STORAGE_BLOCK, bufferBlock.getName());
gl4.glShaderStorageBlockBinding(shaderId, blockIndex, bufferObject.getBinding());
storageBlock.clearUpdateNeeded(); gl4.glBindBufferBase(GL4.GL_SHADER_STORAGE_BUFFER, bufferObject.getBinding(), bufferObject.getId());
} else if (bufferObject instanceof UniformBufferObject) {
final int blockIndex = gl3.glGetUniformBlockIndex(shaderId, bufferBlock.getName());
gl3.glBindBufferBase(GL3.GL_UNIFORM_BUFFER, bufferObject.getBinding(), bufferObject.getId());
gl3.glUniformBlockBinding(GL3.GL_UNIFORM_BUFFER, blockIndex, bufferObject.getBinding());
} }
bufferBlock.clearUpdateNeeded();
} }
protected void updateShaderUniforms(Shader shader) { protected void updateShaderUniforms(Shader shader) {
@ -1247,14 +1254,14 @@ public final class GLRenderer implements Renderer {
} }
/** /**
* Updates all shader's storage blocks. * Updates all shader's buffer blocks.
* *
* @param shader the shader. * @param shader the shader.
*/ */
protected void updateShaderStorageBlocks(final Shader shader) { protected void updateShaderBufferBlocks(final Shader shader) {
final ListMap<String, StorageBlock> storageBlocks = shader.getStorageBlockMap(); final ListMap<String, ShaderBufferBlock> bufferBlocks = shader.getBufferBlockMap();
for (int i = 0; i < storageBlocks.size(); i++) { for (int i = 0; i < bufferBlocks.size(); i++) {
updateShaderStorageBlock(shader, storageBlocks.getValue(i)); updateShaderBufferBlock(shader, bufferBlocks.getValue(i));
} }
} }
@ -1476,7 +1483,7 @@ public final class GLRenderer implements Renderer {
assert shader.getId() > 0; assert shader.getId() > 0;
updateShaderUniforms(shader); updateShaderUniforms(shader);
updateShaderStorageBlocks(shader); updateShaderBufferBlocks(shader);
bindProgram(shader); bindProgram(shader);
} }
} }

@ -53,9 +53,9 @@ public final class Shader extends NativeObject {
private final ListMap<String, Uniform> uniforms; private final ListMap<String, Uniform> uniforms;
/** /**
* Maps storage block name to the storage block variable. * Maps storage block name to the buffer block variables.
*/ */
private final ListMap<String, StorageBlock> storageBlocks; private final ListMap<String, ShaderBufferBlock> bufferBlocks;
/** /**
* Uniforms bound to {@link UniformBinding}s. * Uniforms bound to {@link UniformBinding}s.
@ -227,7 +227,7 @@ public final class Shader extends NativeObject {
super(); super();
shaderSourceList = new ArrayList<>(); shaderSourceList = new ArrayList<>();
uniforms = new ListMap<>(); uniforms = new ListMap<>();
storageBlocks = new ListMap<>(); bufferBlocks = new ListMap<>();
attribs = new IntMap<>(); attribs = new IntMap<>();
boundUniforms = new ArrayList<>(); boundUniforms = new ArrayList<>();
} }
@ -246,7 +246,7 @@ public final class Shader extends NativeObject {
} }
uniforms = null; uniforms = null;
storageBlocks = null; bufferBlocks = null;
boundUniforms = null; boundUniforms = null;
attribs = null; attribs = null;
} }
@ -296,24 +296,24 @@ public final class Shader extends NativeObject {
} }
/** /**
* Get or create a storage block by the name. * Gets or creates a buffer block by the name.
* *
* @param name the storage block's name. * @param name the buffer block's name.
* @return the storage block. * @return the buffer block.
*/ */
public StorageBlock getStorageBlock(final String name) { public ShaderBufferBlock getBufferBlock(final String name) {
assert name.startsWith("m_"); assert name.startsWith("m_");
StorageBlock storageBlock = storageBlocks.get(name); ShaderBufferBlock block = bufferBlocks.get(name);
if (storageBlock == null) { if (block == null) {
storageBlock = new StorageBlock(); block = new ShaderBufferBlock();
storageBlock.name = name; block.name = name;
storageBlocks.put(name, storageBlock); bufferBlocks.put(name, block);
} }
return storageBlock; return block;
} }
public void removeUniform(String name){ public void removeUniform(String name){
@ -321,12 +321,12 @@ public final class Shader extends NativeObject {
} }
/** /**
* Remove a storage block by the name. * Removes a buffer block by the name.
* *
* @param name the storage block's name. * @param name the buffer block's name.
*/ */
public void removeStorageBlock(final String name){ public void removeBufferBlock(final String name){
storageBlocks.remove(name); bufferBlocks.remove(name);
} }
public Attribute getAttribute(VertexBuffer.Type attribType){ public Attribute getAttribute(VertexBuffer.Type attribType){
@ -345,12 +345,12 @@ public final class Shader extends NativeObject {
} }
/** /**
* Get the storage blocks map. * Get the buffer blocks map.
* *
* @return the storage blocks map. * @return the buffer blocks map.
*/ */
public ListMap<String, StorageBlock> getStorageBlockMap() { public ListMap<String, ShaderBufferBlock> getBufferBlockMap() {
return storageBlocks; return bufferBlocks;
} }
public ArrayList<Uniform> getBoundUniforms() { public ArrayList<Uniform> getBoundUniforms() {
@ -366,7 +366,7 @@ public final class Shader extends NativeObject {
return getClass().getSimpleName() + return getClass().getSimpleName() +
"[numSources=" + shaderSourceList.size() + "[numSources=" + shaderSourceList.size() +
", numUniforms=" + uniforms.size() + ", numUniforms=" + uniforms.size() +
", numStorageBlocks=" + storageBlocks.size() + ", numBufferBlocks=" + bufferBlocks.size() +
", shaderSources=" + getSources() + "]"; ", shaderSources=" + getSources() + "]";
} }
@ -413,9 +413,9 @@ public final class Shader extends NativeObject {
uniform.reset(); // fixes issue with re-initialization uniform.reset(); // fixes issue with re-initialization
} }
} }
if (storageBlocks != null) { if (bufferBlocks != null) {
for (StorageBlock storageBlock : storageBlocks.values()) { for (ShaderBufferBlock shaderBufferBlock : bufferBlocks.values()) {
storageBlock.reset(); shaderBufferBlock.reset();
} }
} }
if (attribs != null) { if (attribs != null) {

@ -32,29 +32,29 @@
package com.jme3.shader; package com.jme3.shader;
/** /**
* Implementation of shader's storage block. * Implementation of shader's buffer block.
* *
* @author JavaSaBr * @author JavaSaBr
*/ */
public class StorageBlock extends ShaderVariable { public class ShaderBufferBlock extends ShaderVariable {
/** /**
* Current used buffer object. * Current used buffer object.
*/ */
protected Object storageData; protected BufferObject bufferObject;
/** /**
* Set the new storage data. * Set the new buffer object.
* *
* @param storageData the new storage data * @param bufferObject the new buffer object.
*/ */
public void setStorageData(final Object storageData) { public void setBufferObject(final BufferObject bufferObject) {
if (storageData == null) { if (bufferObject == null) {
throw new IllegalArgumentException("for storage block " + name + ": storageData cannot be null"); throw new IllegalArgumentException("for storage block " + name + ": storageData cannot be null");
} }
this.storageData = storageData; this.bufferObject = bufferObject;
updateNeeded = true; updateNeeded = true;
} }
@ -87,7 +87,7 @@ public class StorageBlock extends ShaderVariable {
* *
* @return the current storage data. * @return the current storage data.
*/ */
public Object getStorageData() { public BufferObject getBufferObject() {
return storageData; return bufferObject;
} }
} }

@ -44,6 +44,7 @@ import com.jme3.renderer.Renderer;
import com.jme3.renderer.Statistics; import com.jme3.renderer.Statistics;
import com.jme3.scene.Mesh; import com.jme3.scene.Mesh;
import com.jme3.scene.VertexBuffer; import com.jme3.scene.VertexBuffer;
import com.jme3.shader.BufferObject;
import com.jme3.shader.Shader; import com.jme3.shader.Shader;
import com.jme3.shader.Shader.ShaderSource; import com.jme3.shader.Shader.ShaderSource;
import com.jme3.shader.ShaderStorageBufferObject; import com.jme3.shader.ShaderStorageBufferObject;
@ -150,14 +151,14 @@ public class NullRenderer implements Renderer {
} }
@Override @Override
public void updateBufferData(ShaderStorageBufferObject ssbo) { public void updateBufferData(BufferObject bo) {
} }
public void deleteBuffer(VertexBuffer vb) { public void deleteBuffer(VertexBuffer vb) {
} }
@Override @Override
public void deleteBuffer(ShaderStorageBufferObject ssbo) { public void deleteBuffer(BufferObject bo) {
} }
public void renderMesh(Mesh mesh, int lod, int count, VertexBuffer[] instanceData) { public void renderMesh(Mesh mesh, int lod, int count, VertexBuffer[] instanceData) {

@ -31,6 +31,13 @@
*/ */
package com.jme3; package com.jme3;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.shader.BufferObject;
import com.jme3.shader.BufferObject.Layout;
import com.jme3.shader.BufferObjectField;
import com.jme3.shader.UniformBufferObject;
import com.jme3.shader.VarType;
import org.junit.Test; import org.junit.Test;
/** /**
@ -41,6 +48,20 @@ public class SetupTest {
@Test(expected=AssertionError.class) @Test(expected=AssertionError.class)
public void testAssertionEnabled() { public void testAssertionEnabled() {
Material material;
final UniformBufferObject ubo = new UniformBufferObject(3, Layout.std140,
new BufferObjectField("light_1", VarType.Vector4),
new BufferObjectField("light_2", VarType.Vector4),
new BufferObjectField("array", VarType.FloatArray)
);
ubo.setValue("light_1", ColorRGBA.Black);
ubo.setValue("light_2", ColorRGBA.Gray);
ubo.setValue("array", new float[] {1F, 2F, 3F});
material.setBufferObject("uboTest", ubo);
assert false; assert false;
} }
} }

@ -654,4 +654,9 @@ public class JoglGL implements GL, GL2, GL3, GL4 {
public void glShaderStorageBlockBinding(final int program, final int storageBlockIndex, final int storageBlockBinding) { public void glShaderStorageBlockBinding(final int program, final int storageBlockIndex, final int storageBlockBinding) {
GLContext.getCurrentGL().getGL4bc().glShaderStorageBlockBinding(program, storageBlockIndex, storageBlockBinding); GLContext.getCurrentGL().getGL4bc().glShaderStorageBlockBinding(program, storageBlockIndex, storageBlockBinding);
} }
@Override
public void glUniformBlockBinding(final int program, final int uniformBlockIndex, final int uniformBlockBinding) {
GLContext.getCurrentGL().getGL3bc().glUniformBlockBinding(program, uniformBlockIndex, uniformBlockBinding);
}
} }

@ -509,4 +509,9 @@ public final class LwjglGL implements GL, GL2, GL3, GL4 {
public void glBindBufferBase(final int target, final int index, final int buffer) { public void glBindBufferBase(final int target, final int index, final int buffer) {
GL30.glBindBufferBase(target, index, buffer); GL30.glBindBufferBase(target, index, buffer);
} }
@Override
public void glUniformBlockBinding(final int program, final int uniformBlockIndex, final int uniformBlockBinding) {
GL31.glUniformBlockBinding(program, uniformBlockIndex, uniformBlockBinding);
}
} }

@ -641,4 +641,9 @@ public class LwjglGL extends LwjglRender implements GL, GL2, GL3, GL4 {
public void glBindBufferBase(final int target, final int index, final int buffer) { public void glBindBufferBase(final int target, final int index, final int buffer) {
GL30.glBindBufferBase(target, index, buffer); GL30.glBindBufferBase(target, index, buffer);
} }
@Override
public void glUniformBlockBinding(final int program, final int uniformBlockIndex, final int uniformBlockBinding) {
GL31.glUniformBlockBinding(program, uniformBlockIndex, uniformBlockBinding);
}
} }

Loading…
Cancel
Save