Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions jme3-core/src/main/java/com/jme3/renderer/RenderContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,15 @@ private static <T> WeakReference<T>[] newWeakReferenceArray(int size) {


/**
* Current bound buffer object IDs for each buffer object unit.
* Current uniform-buffer bindings for each indexed binding point.
*
* @see Renderer#setUniformBufferObject(int, com.jme3.shader.BufferObject)
* @see Renderer#setShaderStorageBufferObject(int, com.jme3.shader.BufferObject)
*/
public final WeakReference<BufferObject>[] boundBO = newWeakReferenceArray(maxBufferObjectUnits);

/** Shader-storage indexed bindings, independent of uniform-buffer bindings. */
public final WeakReference<BufferObject>[] boundSsbo = newWeakReferenceArray(maxBufferObjectUnits);

/**
* IDList for texture units.
*
Expand Down Expand Up @@ -465,6 +467,11 @@ public void reset() {

textureIndexList.reset();

for (int i = 0; i < boundBO.length; i++) {
boundBO[i] = null;
boundSsbo[i] = null;
}

for (int i = 0; i < boundAttribs.length; i++) {
boundAttribs[i] = null;
boundAttribDivisors[i] = 0;
Expand Down
58 changes: 55 additions & 3 deletions jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2108,8 +2108,15 @@ public void deleteShader(Shader shader) {
}
}

if (context.boundShaderProgram == shader.getId()) {
// Deleting a current program alone merely flags it for deletion.
gl.glUseProgram(0);
context.boundShaderProgram = 0;
context.boundShader = null;
}
gl.glDeleteProgram(shader.getId());
statistics.onDeleteShader();
objManager.unregisterObject(shader);
shader.resetObject();
}

Expand Down Expand Up @@ -3472,10 +3479,11 @@ public void setShaderStorageBufferObject(int bindingPoint, BufferObject bufferOb
if (bufferObject.isUpdateNeeded()) {
updateShaderStorageBufferObjectData(bufferObject);
}
if (context.boundBO[bindingPoint] == null || context.boundBO[bindingPoint].get() != bufferObject) {
if (context.boundSsbo[bindingPoint] == null
|| context.boundSsbo[bindingPoint].get() != bufferObject) {
bindShaderStorageBufferBase(bindingPoint, bufferObject.getId());
bufferObject.setBinding(bindingPoint);
context.boundBO[bindingPoint] = bufferObject.getWeakRef();
context.boundSsbo[bindingPoint] = bufferObject.getWeakRef();
}
bufferObject.setBinding(bindingPoint);

Expand Down Expand Up @@ -3768,6 +3776,8 @@ public void deleteBuffer(VertexBuffer vb) {
intBuf1.put(0, bufId);
intBuf1.position(0).limit(1);
gl.glDeleteBuffers(intBuf1);
invalidateBufferBindings(bufId, true);
objManager.unregisterObject(vb);
vb.resetObject();

//statistics.onDeleteVertexBuffer();
Expand All @@ -3787,10 +3797,51 @@ public void deleteBuffer(final BufferObject bo) {
intBuf1.flip();

gl.glDeleteBuffers(intBuf1);

invalidateBufferBindings(bufferId, false);
objManager.unregisterObject(bo);
bo.resetObject();
}

private void invalidateBufferBindings(int bufferId, boolean clearAttributes) {
if (context.boundArrayVBO == bufferId) {
context.boundArrayVBO = 0;
}
if (context.boundElementArrayVBO == bufferId) {
context.boundElementArrayVBO = 0;
}
if (context.boundPixelPackPBO == bufferId) {
context.boundPixelPackPBO = 0;
}
for (int i = 0; i < context.boundBO.length; i++) {
WeakReference<BufferObject> uniform = context.boundBO[i];
BufferObject uniformBuffer = uniform == null ? null : uniform.get();
if (uniform != null && (uniformBuffer == null || uniformBuffer.getId() == bufferId)) {
context.boundBO[i] = null;
}
WeakReference<BufferObject> storage = context.boundSsbo[i];
BufferObject storageBuffer = storage == null ? null : storage.get();
if (storage != null && (storageBuffer == null || storageBuffer.getId() == bufferId)) {
context.boundSsbo[i] = null;
}
}
if (clearAttributes) {
// Interleaved attributes retain their logical VB, not the backing
// buffer ID. Invalidate the finite attribute cache conservatively.
// Enabled arrays stay enabled; the next draw refreshes pointers.
for (int i = 0; i < context.boundAttribs.length; i++) {
WeakReference<VertexBuffer> ref = context.boundAttribs[i];
// A GC cleanup clone need not retain instance metadata, and
// the cached weak referent may already have been collected.
if (ref != null && caps.contains(Caps.MeshInstancing)) {
glext.glVertexAttribDivisorARB(i, 0);
context.boundAttribDivisors[i] = 0;
vertexAttribDivisorsInvalid[i] = false;
}
context.boundAttribs[i] = null;
}
}
}

public void clearVertexAttribs() {
IDList attribList = context.attribIndexList;
for (int i = 0; i < attribList.oldLen; i++) {
Expand Down Expand Up @@ -4325,6 +4376,7 @@ public GL4 getGl4(){
public void deleteFence(GLFence fence) {
if(gl4 != null && fence.getId() != NativeObject.INVALID_ID){
gl4.glDeleteSync(fence);
objManager.unregisterObject(fence);
fence.resetObject();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public void resetRetainsExistingCleanupForOtherCaches() {
context.attribIndexList.copyNewToOld();
WeakReference<BufferObject> previousBuffer = new WeakReference<>(null);
context.boundBO[0] = previousBuffer;
context.boundSsbo[0] = previousBuffer;

context.reset();

Expand All @@ -119,8 +120,8 @@ public void resetRetainsExistingCleanupForOtherCaches() {
assertEquals(0, context.textureIndexList.newLen);
assertEquals(0, context.attribIndexList.oldLen);
assertEquals(0, context.attribIndexList.newLen);
// The legacy reset did not change boundBO; this revision preserves that scope.
assertSame(previousBuffer, context.boundBO[0]);
assertNull(context.boundBO[0]);
assertNull(context.boundSsbo[0]);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright (c) 2009-2026 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.renderer.opengl;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.jme3.renderer.Caps;
import com.jme3.shader.bufferobject.BufferObject;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import org.junit.jupiter.api.Test;

class IndexedBufferBindingTest {
@Test
void contextResetClearsIndexedBufferBindings() throws Exception {
RecordingGl gl = new RecordingGl();
GLRenderer renderer = gl.renderer();
renderer.getCaps().add(Caps.UniformBufferObject);
renderer.getCaps().add(Caps.ShaderStorageBufferObject);
BufferObject buffer = new BufferObject();
buffer.setId(71);
buffer.clearUpdateNeeded();

renderer.setUniformBufferObject(3, buffer);
renderer.setShaderStorageBufferObject(3, buffer);
assertEquals(2, gl.bufferBaseCalls);
renderer.invalidateState();

renderer.setUniformBufferObject(3, buffer);
renderer.setShaderStorageBufferObject(3, buffer);
assertEquals(4, gl.bufferBaseCalls);
}

@Test
void uniformAndStorageBuffersUseIndependentIndexedBindingCaches() throws Exception {
RecordingGl gl = new RecordingGl();
GLRenderer renderer = gl.renderer();
renderer.getCaps().add(Caps.UniformBufferObject);
renderer.getCaps().add(Caps.ShaderStorageBufferObject);
BufferObject buffer = new BufferObject();
buffer.setId(72);
buffer.clearUpdateNeeded();
renderer.registerNativeObject(buffer);

renderer.setUniformBufferObject(4, buffer);
renderer.setShaderStorageBufferObject(4, buffer);
assertEquals(2, gl.bufferBaseCalls);
BufferObject other = new BufferObject();
other.setId(73);
other.clearUpdateNeeded();
renderer.setUniformBufferObject(4, other);
renderer.setShaderStorageBufferObject(4, buffer);
assertEquals(3, gl.bufferBaseCalls, "changing a UBO must preserve the SSBO cache");
}

private static final class RecordingGl implements InvocationHandler {
int bufferBaseCalls;

GLRenderer renderer() {
GL4 gl = (GL4) Proxy.newProxyInstance(GL4.class.getClassLoader(),
new Class<?>[]{GL4.class}, this);
GLExt extension = (GLExt) Proxy.newProxyInstance(GLExt.class.getClassLoader(),
new Class<?>[]{GLExt.class}, this);
return new GLRenderer(gl, extension, null);
}

@Override
public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] arguments) {
if (method.getName().equals("glBindBufferBase")) {
bufferBaseCalls++;
}
return defaultValue(method.getReturnType());
}

private static Object defaultValue(Class<?> type) {
if (!type.isPrimitive() || type == void.class) {
return null;
}
if (type == boolean.class) {
return false;
}
if (type == char.class) {
return '\0';
}
if (type == byte.class) {
return (byte) 0;
}
if (type == short.class) {
return (short) 0;
}
if (type == int.class) {
return 0;
}
if (type == long.class) {
return 0L;
}
if (type == float.class) {
return 0f;
}
if (type == double.class) {
return 0d;
}
throw new AssertionError(type);
}
}
}
Loading
Loading