Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GL debug capabilities #1790

Merged
merged 3 commits into from
May 24, 2022
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
7 changes: 6 additions & 1 deletion jme3-core/src/main/java/com/jme3/renderer/Caps.java
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,12 @@ public enum Caps {
/**
* Supports unpack row length (stride).
*/
UnpackRowLength
UnpackRowLength,

/**
* Supports debugging capabilities
*/
GLDebug
;

/**
Expand Down
11 changes: 10 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 @@ -622,6 +622,7 @@ public void updateUniformBindings(Shader shader) {
* @see com.jme3.material.Material#render(com.jme3.scene.Geometry, com.jme3.renderer.RenderManager)
*/
public void renderGeometry(Geometry geom) {
this.renderer.pushDebugGroup(geom.getName());
if (geom.isIgnoreTransform()) {
setWorldMatrix(Matrix4f.IDENTITY);
} else {
Expand Down Expand Up @@ -680,6 +681,7 @@ public void renderGeometry(Geometry geom) {
} else {
material.render(geom, lightList, this);
}
this.renderer.popDebugGroup();
}

/**
Expand Down Expand Up @@ -955,15 +957,19 @@ public void renderViewPortQueues(ViewPort vp, boolean flush) {
if (prof != null) {
prof.vpStep(VpStep.RenderBucket, vp, Bucket.Opaque);
}
this.renderer.pushDebugGroup(Bucket.Opaque.name());
rq.renderQueue(Bucket.Opaque, this, cam, flush);
this.renderer.popDebugGroup();

// render the sky, with depth range set to the farthest
if (!rq.isQueueEmpty(Bucket.Sky)) {
if (prof != null) {
prof.vpStep(VpStep.RenderBucket, vp, Bucket.Sky);
}
renderer.setDepthRange(1, 1);
this.renderer.pushDebugGroup(Bucket.Sky.name());
rq.renderQueue(Bucket.Sky, this, cam, flush);
this.renderer.popDebugGroup();
depthRangeChanged = true;
}

Expand All @@ -979,8 +985,9 @@ public void renderViewPortQueues(ViewPort vp, boolean flush) {
renderer.setDepthRange(0, 1);
depthRangeChanged = false;
}

this.renderer.pushDebugGroup(Bucket.Transparent.name());
rq.renderQueue(Bucket.Transparent, this, cam, flush);
this.renderer.popDebugGroup();
}

if (!rq.isQueueEmpty(Bucket.Gui)) {
Expand All @@ -989,7 +996,9 @@ public void renderViewPortQueues(ViewPort vp, boolean flush) {
}
renderer.setDepthRange(0, 0);
setCamera(cam, true);
this.renderer.pushDebugGroup(Bucket.Gui.name());
rq.renderQueue(Bucket.Gui, this, cam, flush);
this.renderer.popDebugGroup();
setCamera(cam, false);
depthRangeChanged = true;
}
Expand Down
9 changes: 9 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 @@ -519,4 +519,13 @@ public void setTexture(int unit, Texture tex)
* @return true for conversion, false for no conversion
*/
public boolean isMainFrameBufferSrgb();

public default void popDebugGroup() {

}

public default void pushDebugGroup(String name) {

}

}
5 changes: 5 additions & 0 deletions jme3-core/src/main/java/com/jme3/renderer/opengl/GL3.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ public interface GL3 extends GL2 {
*/
public static final int GL_TRANSFORM_FEEDBACK_BUFFER = 0x8C8E;


public static final int GL_FRAMEBUFFER = 0x8D40;
public static final int GL_READ_FRAMEBUFFER = 0x8CA8;
public static final int GL_DRAW_FRAMEBUFFER = 0x8CA9;

/**
* <p><a target="_blank" href="http://docs.gl/gl4/glBindFragDataLocation">Reference Page</a></p>
* <p>
Expand Down
24 changes: 24 additions & 0 deletions jme3-core/src/main/java/com/jme3/renderer/opengl/GLExt.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@ public interface GLExt {
public static final int GL_COMPRESSED_RGBA_BPTC_UNORM = 0x8E8C;
public static final int GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM = 0x8E8D;

public static final int GL_DEBUG_SOURCE_API = 0x8246;
public static final int GL_DEBUG_SOURCE_WINDOW_SYSTEM = 0x8247;
public static final int GL_DEBUG_SOURCE_SHADER_COMPILER = 0x8248;
public static final int GL_DEBUG_SOURCE_THIRD_PARTY = 0x8249;
public static final int GL_DEBUG_SOURCE_APPLICATION = 0x824A;
public static final int GL_DEBUG_SOURCE_OTHER = 0x824B;

public static final int GL_BUFFER = 0x82E0;
public static final int GL_SHADER = 0x82E1;
public static final int GL_PROGRAM = 0x82E2;
public static final int GL_QUERY = 0x82E3;
public static final int GL_PROGRAM_PIPELINE = 0x82E4;
public static final int GL_SAMPLER = 0x82E6;
public static final int GL_DISPLAY_LIST = 0x82E7;

/**
* <p><a target="_blank" href="http://docs.gl/gl4/glBufferData">Reference Page</a></p>
*
Expand Down Expand Up @@ -239,4 +254,13 @@ public void glTexImage2DMultisample(int target, int samples, int internalFormat,
* @param divisor the divisor value.
*/
public void glVertexAttribDivisorARB(int index, int divisor);

public default void glPushDebugGroup(int source, int id, String message) {
}

public default void glPopDebugGroup() {
}

public default void glObjectLabel(int identifier, int id, String label){
}
}
42 changes: 42 additions & 0 deletions jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ public final class GLRenderer implements Renderer {
private final GLExt glext;
private final GLFbo glfbo;
private final TextureUtil texUtil;
private boolean debug = false;
private int debugGroupId = 0;


public GLRenderer(GL gl, GLExt glext, GLFbo glfbo) {
this.gl = gl;
Expand All @@ -128,6 +131,29 @@ public void setGenerateMipmapsForFrameBuffer(boolean v) {
generateMipmapsForFramebuffers = v;
}

public void setDebugEnabled(boolean v) {
debug = v;
}

@Override
public void popDebugGroup() {
if (debug && caps.contains(Caps.GLDebug)) {
glext.glPopDebugGroup();
debugGroupId--;
}
}

@Override
public void pushDebugGroup(String name) {
if (debug && caps.contains(Caps.GLDebug)) {
if (name == null) name = "Group " + debugGroupId;
glext.glPushDebugGroup(GLExt.GL_DEBUG_SOURCE_APPLICATION, debugGroupId, name);
debugGroupId++;
}
}



@Override
public Statistics getStatistics() {
return statistics;
Expand Down Expand Up @@ -574,6 +600,10 @@ private void loadCapabilitiesCommon() {
caps.add(Caps.UnpackRowLength);
}

if (caps.contains(Caps.OpenGL43) || hasExtension("GL_KHR_debug")) {
caps.add(Caps.GLDebug);
}

// Print context information
logger.log(Level.INFO, "OpenGL Renderer Information\n" +
" * Vendor: {0}\n" +
Expand Down Expand Up @@ -1431,6 +1461,9 @@ public void updateShaderSourceData(ShaderSource source) {
}

source.setId(id);
if (debug && caps.contains(Caps.GLDebug)) {
if(source.getName() != null) glext.glObjectLabel(GLExt.GL_SHADER, id, source.getName());
}
} else {
throw new RendererException("Cannot recompile shader source");
}
Expand Down Expand Up @@ -2103,6 +2136,9 @@ public void setFrameBuffer(FrameBuffer fb) {
assert context.boundFBO == fb.getId();

context.boundFB = fb;
if (debug && caps.contains(Caps.GLDebug)) {
if (fb.getName() != null) glext.glObjectLabel(GL3.GL_FRAMEBUFFER, fb.getId(), fb.getName());
}
}
}

Expand Down Expand Up @@ -2626,6 +2662,9 @@ public void setTexture(int unit, Texture tex) throws TextureUnitException {
assert texId != -1;

setupTextureParams(unit, tex);
if (debug && caps.contains(Caps.GLDebug)) {
if (tex.getName() != null) glext.glObjectLabel(GL.GL_TEXTURE, tex.getImage().getId(), tex.getName());
}
}


Expand Down Expand Up @@ -2995,6 +3034,9 @@ public void setVertexAttrib(VertexBuffer vb, VertexBuffer idb) {
attribs[slot] = vb.getWeakRef();
}
}
if (debug && caps.contains(Caps.GLDebug)) {
if (vb.getName() != null) glext.glObjectLabel(GLExt.GL_BUFFER, vb.getId(), vb.getName());
Copy link
Contributor

@Trass3r Trass3r Apr 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It suffices to do this once right after creating the GL object (glGenBuffers) if the name doesn't change over time.
In general updateBufferData seems to be a better place for this, also to cover more cases like index buffers.
If needed setName could set updateNeeded to get updates too I guess.

}
}

public void setVertexAttrib(VertexBuffer vb) {
Expand Down
12 changes: 12 additions & 0 deletions jme3-core/src/main/java/com/jme3/scene/VertexBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ public int getComponentSize() {
protected boolean normalized = false;
protected int instanceSpan = 0;
protected transient boolean dataSizeChanged = false;
protected String name;

/**
* Creates an empty, uninitialized buffer.
Expand Down Expand Up @@ -1189,4 +1190,15 @@ public void read(JmeImporter im) throws IOException {
throw new IOException("Unsupported import buffer format: " + format);
}
}

public String getName() {
if (name == null) {
name = getClass().getSimpleName() + "(" + getBufferType().name() + ")";
}
return name;
}

public void setName(String name) {
this.name = name;
}
}
6 changes: 5 additions & 1 deletion jme3-core/src/main/java/com/jme3/system/AppSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,9 @@ public String getOpenCLPlatformChooser() {
* Determine if the renderer will be run in Graphics Debug mode, which means every openGL call is checked and
* if it returns an error code, throw a {@link com.jme3.renderer.RendererException}.<br>
* Without this, many openGL calls might fail without notice, so turning it on is recommended for development.
*
* Graphics Debug mode will also label native objects and group calls on supported renderers. Compatible
* graphics debuggers will be able to use this data to show a better outlook of your application
*
* @return whether the context will be run in Graphics Debug Mode or not
* @see #setGraphicsDebug(boolean)
*/
Expand All @@ -1311,6 +1313,8 @@ public boolean isGraphicsDebug() {
* Set whether the renderer will be run in Graphics Debug mode, which means every openGL call is checked and
* if it returns an error code, throw a {@link com.jme3.renderer.RendererException}.<br>
* Without this, many openGL calls might fail without notice, so turning it on is recommended for development.
* Graphics Debug mode will also label native objects and group calls on supported renderers. Compatible
* graphics debuggers will be able to use this data to show a better outlook of your application
*
* @param debug whether the context will be run in Graphics Debug Mode or not
* @see #isGraphicsDebug()
Expand Down
8 changes: 8 additions & 0 deletions jme3-core/src/main/java/com/jme3/texture/FrameBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public class FrameBuffer extends NativeObject {
private RenderBuffer depthBuf = null;
private int colorBufIndex = 0;
private boolean srgb;
private String name;
private Boolean mipMapsGenerationHint = null;

/**
Expand Down Expand Up @@ -844,6 +845,13 @@ public boolean isSrgb() {
return srgb;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

/**
* Hints the renderer to generate mipmaps for this framebuffer if necessary
Expand Down
16 changes: 16 additions & 0 deletions jme3-lwjgl3/src/main/java/com/jme3/renderer/lwjgl/LwjglGLExt.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,20 @@ public int glClientWaitSync(final Object sync, final int flags, final long timeo
public void glDeleteSync(final Object sync) {
ARBSync.glDeleteSync((Long) sync);
}

@Override
public void glPushDebugGroup(int source, int id, String message) {
KHRDebug.glPushDebugGroup(source, id, message);
}

@Override
public void glPopDebugGroup() {
KHRDebug.glPopDebugGroup();
}

@Override
public void glObjectLabel(int identifier, int id, String label) {
assert label != null;
KHRDebug.glObjectLabel(identifier, id, label);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ private void initContext(boolean first) {
}

this.renderer = new GLRenderer(gl, glext, glfbo);
if (this.settings.isGraphicsDebug()) ((GLRenderer)this.renderer).setDebugEnabled(true);
}
this.renderer.initialize();

Expand Down