GLProxy reformat

This commit is contained in:
James Seibel
2023-09-23 11:25:12 -05:00
parent 9d5bfc4f16
commit 03efa3c191
@@ -61,7 +61,6 @@ import java.util.concurrent.TimeUnit;
* https://stackoverflow.com/questions/63509735/massive-performance-loss-with-glmapbuffer <br><br> * https://stackoverflow.com/questions/63509735/massive-performance-loss-with-glmapbuffer <br><br>
* *
* @author James Seibel * @author James Seibel
* @version 2022-10-1
*/ */
public class GLProxy public class GLProxy
{ {
@@ -102,6 +101,10 @@ public class GLProxy
//=============//
// constructor //
//=============//
private GLProxy() throws IllegalStateException private GLProxy() throws IllegalStateException
{ {
// this must be created on minecraft's render context to work correctly // this must be created on minecraft's render context to work correctly
@@ -123,13 +126,13 @@ public class GLProxy
//============================// //============================//
// get Minecraft's context // get Minecraft's context
minecraftGlContext = GLFW.glfwGetCurrentContext(); this.minecraftGlContext = GLFW.glfwGetCurrentContext();
minecraftGlCapabilities = GL.getCapabilities(); this.minecraftGlCapabilities = GL.getCapabilities();
// crash the game if the GPU doesn't support OpenGL 3.2 // crash the game if the GPU doesn't support OpenGL 3.2
if (!minecraftGlCapabilities.OpenGL32) if (!this.minecraftGlCapabilities.OpenGL32)
{ {
String supportedVersionInfo = getFailedVersionInfo(minecraftGlCapabilities); String supportedVersionInfo = this.getFailedVersionInfo(this.minecraftGlCapabilities);
// See full requirement at above. // See full requirement at above.
String errorMessage = ModInfo.READABLE_NAME + " was initializing " + GLProxy.class.getSimpleName() String errorMessage = ModInfo.READABLE_NAME + " was initializing " + GLProxy.class.getSimpleName()
@@ -137,11 +140,11 @@ public class GLProxy
"Additional info:\n" + supportedVersionInfo; "Additional info:\n" + supportedVersionInfo;
MC.crashMinecraft(errorMessage, new UnsupportedOperationException("Distant Horizon OpenGL requirements not met")); MC.crashMinecraft(errorMessage, new UnsupportedOperationException("Distant Horizon OpenGL requirements not met"));
} }
GL_LOGGER.info("minecraftGlCapabilities:\n" + getVersionInfo(minecraftGlCapabilities)); GL_LOGGER.info("minecraftGlCapabilities:\n" + this.getVersionInfo(this.minecraftGlCapabilities));
if (Config.Client.Advanced.Debugging.overrideVanillaGLLogger.get()) if (Config.Client.Advanced.Debugging.overrideVanillaGLLogger.get())
{ {
GLUtil.setupDebugMessageCallback(new PrintStream(new GLMessageOutputStream(GLProxy::logMessage, vanillaDebugMessageBuilder), true)); GLUtil.setupDebugMessageCallback(new PrintStream(new GLMessageOutputStream(GLProxy::logMessage, this.vanillaDebugMessageBuilder), true));
} }
@@ -183,20 +186,20 @@ public class GLProxy
} }
// create the Lod Builder context // create the Lod Builder context
lodBuilderGlContext = GLFW.glfwCreateWindow(64, 48, "LOD Builder Window", 0L, minecraftGlContext); this.lodBuilderGlContext = GLFW.glfwCreateWindow(64, 48, "LOD Builder Window", 0L, this.minecraftGlContext);
if (lodBuilderGlContext == 0) if (this.lodBuilderGlContext == 0)
{ {
GL_LOGGER.error("ERROR: Failed to create GLFW context for OpenGL 3.2 with" GL_LOGGER.error("ERROR: Failed to create GLFW context for OpenGL 3.2 with"
+ " Forward Compat Core Profile! Your OS may have not been able to support it!"); + " Forward Compat Core Profile! Your OS may have not been able to support it!");
throw new UnsupportedOperationException("Forward Compat Core Profile 3.2 creation failure"); throw new UnsupportedOperationException("Forward Compat Core Profile 3.2 creation failure");
} }
// create the window // create the window
GLFW.glfwMakeContextCurrent(lodBuilderGlContext); GLFW.glfwMakeContextCurrent(this.lodBuilderGlContext);
// set and log the capabilities // set and log the capabilities
lodBuilderGlCapabilities = GL.createCapabilities(); this.lodBuilderGlCapabilities = GL.createCapabilities();
GL_LOGGER.info("lodBuilderGlCapabilities:\n" + getVersionInfo(lodBuilderGlCapabilities)); GL_LOGGER.info("lodBuilderGlCapabilities:\n" + this.getVersionInfo(this.lodBuilderGlCapabilities));
// override the GL logger // override the GL logger
GLUtil.setupDebugMessageCallback(new PrintStream(new GLMessageOutputStream(GLProxy::logMessage, lodBuilderDebugMessageBuilder), true)); GLUtil.setupDebugMessageCallback(new PrintStream(new GLMessageOutputStream(GLProxy::logMessage, this.lodBuilderDebugMessageBuilder), true));
// clear the context for the next stage // clear the context for the next stage
GLFW.glfwMakeContextCurrent(0L); GLFW.glfwMakeContextCurrent(0L);
@@ -207,20 +210,20 @@ public class GLProxy
//=================================// //=================================//
// create the proxyWorker's context // create the proxyWorker's context
proxyWorkerGlContext = GLFW.glfwCreateWindow(64, 48, "LOD proxy worker Window", 0L, minecraftGlContext); this.proxyWorkerGlContext = GLFW.glfwCreateWindow(64, 48, "LOD proxy worker Window", 0L, this.minecraftGlContext);
if (proxyWorkerGlContext == 0) if (this.proxyWorkerGlContext == 0)
{ {
GL_LOGGER.error("ERROR: Failed to create GLFW context for OpenGL 3.2 with" GL_LOGGER.error("ERROR: Failed to create GLFW context for OpenGL 3.2 with"
+ " Forward Compat Core Profile! Your OS may have not been able to support it!"); + " Forward Compat Core Profile! Your OS may have not been able to support it!");
throw new UnsupportedOperationException("Forward Compat Core Profile 3.2 creation failure"); throw new UnsupportedOperationException("Forward Compat Core Profile 3.2 creation failure");
} }
// create the window // create the window
GLFW.glfwMakeContextCurrent(proxyWorkerGlContext); GLFW.glfwMakeContextCurrent(this.proxyWorkerGlContext);
// set and log the capabilities // set and log the capabilities
proxyWorkerGlCapabilities = GL.createCapabilities(); this.proxyWorkerGlCapabilities = GL.createCapabilities();
GL_LOGGER.info("proxyWorkerGlCapabilities:\n" + getVersionInfo(lodBuilderGlCapabilities)); GL_LOGGER.info("proxyWorkerGlCapabilities:\n" + this.getVersionInfo(this.lodBuilderGlCapabilities));
// override the GL logger // override the GL logger
GLUtil.setupDebugMessageCallback(new PrintStream(new GLMessageOutputStream(GLProxy::logMessage, proxyWorkerDebugMessageBuilder), true)); GLUtil.setupDebugMessageCallback(new PrintStream(new GLMessageOutputStream(GLProxy::logMessage, this.proxyWorkerDebugMessageBuilder), true));
// clear the context for the next stage // clear the context for the next stage
GLFW.glfwMakeContextCurrent(0L); GLFW.glfwMakeContextCurrent(0L);
@@ -231,19 +234,19 @@ public class GLProxy
//======================// //======================//
// get capabilities from a context we use // get capabilities from a context we use
setGlContext(EGLProxyContext.LOD_BUILDER); this.setGlContext(EGLProxyContext.LOD_BUILDER);
// Check if we can use the make-over version of Vertex Attribute, which is available in GL4.3 or after // Check if we can use the make-over version of Vertex Attribute, which is available in GL4.3 or after
VertexAttributeBufferBindingSupported = minecraftGlCapabilities.glBindVertexBuffer != 0L; // Nullptr this.VertexAttributeBufferBindingSupported = this.minecraftGlCapabilities.glBindVertexBuffer != 0L; // Nullptr
// UNUSED currently // UNUSED currently
// Check if we can use the named version of all calls, which is available in GL4.5 or after // Check if we can use the named version of all calls, which is available in GL4.5 or after
namedObjectSupported = minecraftGlCapabilities.glNamedBufferData != 0L; //Nullptr this.namedObjectSupported = this.minecraftGlCapabilities.glNamedBufferData != 0L; //Nullptr
// get specific capabilities // get specific capabilities
// Check if we can use the Buffer Storage, which is available in GL4.4 or after // Check if we can use the Buffer Storage, which is available in GL4.4 or after
bufferStorageSupported = minecraftGlCapabilities.glBufferStorage != 0L && lodBuilderGlCapabilities.glBufferStorage != 0L; // Nullptr this.bufferStorageSupported = this.minecraftGlCapabilities.glBufferStorage != 0L && this.lodBuilderGlCapabilities.glBufferStorage != 0L; // Nullptr
if (!bufferStorageSupported) if (!this.bufferStorageSupported)
{ {
GL_LOGGER.warn("This GPU doesn't support Buffer Storage (OpenGL 4.4), falling back to using other methods."); GL_LOGGER.warn("This GPU doesn't support Buffer Storage (OpenGL 4.4), falling back to using other methods.");
} }
@@ -253,14 +256,14 @@ public class GLProxy
if (vendor.contains("NVIDIA") || vendor.contains("GEFORCE")) if (vendor.contains("NVIDIA") || vendor.contains("GEFORCE"))
{ {
// NVIDIA card // NVIDIA card
preferredUploadMethod = bufferStorageSupported ? EGpuUploadMethod.BUFFER_STORAGE : EGpuUploadMethod.SUB_DATA; this.preferredUploadMethod = this.bufferStorageSupported ? EGpuUploadMethod.BUFFER_STORAGE : EGpuUploadMethod.SUB_DATA;
} }
else else
{ {
// AMD or Intel card // AMD or Intel card
preferredUploadMethod = bufferStorageSupported ? EGpuUploadMethod.BUFFER_STORAGE : EGpuUploadMethod.DATA; this.preferredUploadMethod = this.bufferStorageSupported ? EGpuUploadMethod.BUFFER_STORAGE : EGpuUploadMethod.DATA;
} }
GL_LOGGER.info("GPU Vendor [" + vendor + "], Preferred upload method is [" + preferredUploadMethod + "]."); GL_LOGGER.info("GPU Vendor [" + vendor + "], Preferred upload method is [" + this.preferredUploadMethod + "].");
@@ -269,7 +272,7 @@ public class GLProxy
//==========// //==========//
// Since this is created on the render thread, make sure the Minecraft context is used in the end // Since this is created on the render thread, make sure the Minecraft context is used in the end
setGlContext(EGLProxyContext.MINECRAFT); this.setGlContext(EGLProxyContext.MINECRAFT);
// GLProxy creation success // GLProxy creation success
GL_LOGGER.info(GLProxy.class.getSimpleName() + " creation successful. OpenGL smiles upon you this day."); GL_LOGGER.info(GLProxy.class.getSimpleName() + " creation successful. OpenGL smiles upon you this day.");
@@ -281,7 +284,7 @@ public class GLProxy
*/ */
public void setGlContext(EGLProxyContext newContext) public void setGlContext(EGLProxyContext newContext)
{ {
EGLProxyContext currentContext = getGlContext(); EGLProxyContext currentContext = this.getGlContext();
// we don't have to change the context, we are already there. // we don't have to change the context, we are already there.
if (currentContext == newContext) if (currentContext == newContext)
@@ -294,18 +297,18 @@ public class GLProxy
switch (newContext) switch (newContext)
{ {
case LOD_BUILDER: case LOD_BUILDER:
contextPointer = lodBuilderGlContext; contextPointer = this.lodBuilderGlContext;
newGlCapabilities = lodBuilderGlCapabilities; newGlCapabilities = this.lodBuilderGlCapabilities;
break; break;
case MINECRAFT: case MINECRAFT:
contextPointer = minecraftGlContext; contextPointer = this.minecraftGlContext;
newGlCapabilities = minecraftGlCapabilities; newGlCapabilities = this.minecraftGlCapabilities;
break; break;
case PROXY_WORKER: case PROXY_WORKER:
contextPointer = proxyWorkerGlContext; contextPointer = this.proxyWorkerGlContext;
newGlCapabilities = proxyWorkerGlCapabilities; newGlCapabilities = this.proxyWorkerGlCapabilities;
break; break;
default: // default should never happen, it is just here to make the compiler happy default: // default should never happen, it is just here to make the compiler happy
@@ -325,15 +328,15 @@ public class GLProxy
long currentContext = GLFW.glfwGetCurrentContext(); long currentContext = GLFW.glfwGetCurrentContext();
if (currentContext == lodBuilderGlContext) if (currentContext == this.lodBuilderGlContext)
{ {
return EGLProxyContext.LOD_BUILDER; return EGLProxyContext.LOD_BUILDER;
} }
else if (currentContext == minecraftGlContext) else if (currentContext == this.minecraftGlContext)
{ {
return EGLProxyContext.MINECRAFT; return EGLProxyContext.MINECRAFT;
} }
else if (currentContext == proxyWorkerGlContext) else if (currentContext == this.proxyWorkerGlContext)
{ {
return EGLProxyContext.PROXY_WORKER; return EGLProxyContext.PROXY_WORKER;
} }
@@ -346,9 +349,9 @@ public class GLProxy
// hopefully this shouldn't happen // hopefully this shouldn't happen
throw new IllegalStateException(Thread.currentThread().getName() + throw new IllegalStateException(Thread.currentThread().getName() +
" has a unknown OpenGl context: [" + currentContext + "]. " " has a unknown OpenGl context: [" + currentContext + "]. "
+ "Minecraft context [" + minecraftGlContext + "], " + "Minecraft context [" + this.minecraftGlContext + "], "
+ "LodBuilder context [" + lodBuilderGlContext + "], " + "LodBuilder context [" + this.lodBuilderGlContext + "], "
+ "ProxyWorker context [" + proxyWorkerGlContext + "], " + "ProxyWorker context [" + this.proxyWorkerGlContext + "], "
+ "no context [0]."); + "no context [0].");
} }
} }
@@ -368,13 +371,13 @@ public class GLProxy
public EGpuUploadMethod getGpuUploadMethod() public EGpuUploadMethod getGpuUploadMethod()
{ {
EGpuUploadMethod method = Config.Client.Advanced.GpuBuffers.gpuUploadMethod.get(); EGpuUploadMethod method = Config.Client.Advanced.GpuBuffers.gpuUploadMethod.get();
if (!bufferStorageSupported && method == EGpuUploadMethod.BUFFER_STORAGE) if (!this.bufferStorageSupported && method == EGpuUploadMethod.BUFFER_STORAGE)
{ {
// if buffer storage isn't supported // if buffer storage isn't supported
// default to DATA since that is the most compatible // default to DATA since that is the most compatible
method = EGpuUploadMethod.DATA; method = EGpuUploadMethod.DATA;
} }
return method == EGpuUploadMethod.AUTO ? preferredUploadMethod : method; return method == EGpuUploadMethod.AUTO ? this.preferredUploadMethod : method;
} }
/** /**
@@ -386,14 +389,14 @@ public class GLProxy
public void recordOpenGlCall(Runnable renderCall) public void recordOpenGlCall(Runnable renderCall)
{ {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
workerThread.execute(() -> runnableContainer(renderCall, stackTrace)); this.workerThread.execute(() -> this.runnableContainer(renderCall, stackTrace));
} }
private void runnableContainer(Runnable renderCall, StackTraceElement[] stackTrace) private void runnableContainer(Runnable renderCall, StackTraceElement[] stackTrace)
{ {
try try
{ {
// set up the context... // set up the context...
setGlContext(EGLProxyContext.PROXY_WORKER); this.setGlContext(EGLProxyContext.PROXY_WORKER);
// ...run the actual code... // ...run the actual code...
renderCall.run(); renderCall.run();
} }
@@ -406,7 +409,7 @@ public class GLProxy
finally finally
{ {
// ...and make sure the context is released when the thread finishes // ...and make sure the context is released when the thread finishes
setGlContext(EGLProxyContext.NONE); this.setGlContext(EGLProxyContext.NONE);
} }
} }
@@ -460,9 +463,9 @@ public class GLProxy
return false; return false;
} }
namedObjectSupported = c.glNamedBufferStorage != 0; this.namedObjectSupported = c.glNamedBufferStorage != 0;
bufferStorageSupported = c.glBufferStorage != 0; this.bufferStorageSupported = c.glBufferStorage != 0;
VertexAttributeBufferBindingSupported = c.glVertexAttribBinding != 0; this.VertexAttributeBufferBindingSupported = c.glVertexAttribBinding != 0;
return true; return true;
} }