2023-05-02 08:43:01 +02:00
|
|
|
package com.jozufozu.flywheel.gl;
|
2021-02-21 00:22:07 +01:00
|
|
|
|
2021-04-08 19:22:11 +02:00
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
2021-04-01 05:53:02 +02:00
|
|
|
import org.lwjgl.PointerBuffer;
|
2022-01-13 06:25:03 +01:00
|
|
|
import org.lwjgl.opengl.GL;
|
2021-04-01 05:53:02 +02:00
|
|
|
import org.lwjgl.opengl.GL20C;
|
2021-02-21 00:22:07 +01:00
|
|
|
import org.lwjgl.opengl.GLCapabilities;
|
2021-04-01 05:53:02 +02:00
|
|
|
import org.lwjgl.system.MemoryStack;
|
2021-02-21 00:22:07 +01:00
|
|
|
|
2021-12-14 04:55:50 +01:00
|
|
|
import net.minecraft.Util;
|
|
|
|
|
2021-02-21 00:22:07 +01:00
|
|
|
/**
|
2021-06-07 00:46:16 +02:00
|
|
|
* An instance of this class stores information about what OpenGL features are available.
|
|
|
|
* <br>
|
|
|
|
* Each field stores an enum variant that provides access to the most appropriate version of a feature for the current
|
|
|
|
* system.
|
2021-02-21 00:22:07 +01:00
|
|
|
*/
|
2021-04-01 05:53:02 +02:00
|
|
|
public class GlCompat {
|
2023-05-07 01:14:30 +02:00
|
|
|
public static final boolean ALLOW_DSA = true;
|
2023-05-02 08:43:01 +02:00
|
|
|
public static final GLCapabilities CAPABILITIES = GL.createCapabilities();
|
|
|
|
private static final boolean amd = _decideIfWeAreAMDWindows();
|
|
|
|
private static final boolean supportsIndirect = _decideIfWeSupportIndirect();
|
2021-12-14 04:55:50 +01:00
|
|
|
|
2023-04-25 06:54:59 +02:00
|
|
|
private GlCompat() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean onAMDWindows() {
|
2021-12-14 04:55:50 +01:00
|
|
|
return amd;
|
2021-04-18 03:54:57 +02:00
|
|
|
}
|
|
|
|
|
2023-04-25 06:54:59 +02:00
|
|
|
public static boolean supportsInstancing() {
|
2023-05-07 01:14:30 +02:00
|
|
|
return true;
|
2023-04-25 06:54:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean supportsIndirect() {
|
|
|
|
return supportsIndirect;
|
2021-04-18 03:54:57 +02:00
|
|
|
}
|
|
|
|
|
2023-04-30 06:53:56 +02:00
|
|
|
private static boolean _decideIfWeSupportIndirect() {
|
2023-05-02 08:43:01 +02:00
|
|
|
return CAPABILITIES.OpenGL46 || (CAPABILITIES.GL_ARB_compute_shader && CAPABILITIES.GL_ARB_shader_draw_parameters && CAPABILITIES.GL_ARB_base_instance && CAPABILITIES.GL_ARB_multi_draw_indirect && CAPABILITIES.GL_ARB_direct_state_access);
|
2021-08-15 00:57:52 +02:00
|
|
|
}
|
|
|
|
|
2021-04-18 03:54:57 +02:00
|
|
|
/**
|
2022-08-11 05:47:53 +02:00
|
|
|
* Modified from:
|
2022-06-09 21:44:52 +02:00
|
|
|
* <br> <a href="https://github.com/grondag/canvas/commit/820bf754092ccaf8d0c169620c2ff575722d7d96">canvas</a>
|
2021-04-30 02:19:08 +02:00
|
|
|
*
|
|
|
|
* <p>Identical in function to {@link GL20C#glShaderSource(int, CharSequence)} but
|
|
|
|
* passes a null pointer for string length to force the driver to rely on the null
|
|
|
|
* terminator for string length. This is a workaround for an apparent flaw with some
|
|
|
|
* AMD drivers that don't receive or interpret the length correctly, resulting in
|
|
|
|
* an access violation when the driver tries to read past the string memory.
|
|
|
|
*
|
|
|
|
* <p>Hat tip to fewizz for the find and the fix.
|
|
|
|
*/
|
|
|
|
public static void safeShaderSource(int glId, CharSequence source) {
|
2022-08-11 05:47:53 +02:00
|
|
|
try (MemoryStack stack = MemoryStack.stackPush()) {
|
|
|
|
final ByteBuffer sourceBuffer = stack.UTF8(source, true);
|
2021-04-30 02:19:08 +02:00
|
|
|
final PointerBuffer pointers = stack.mallocPointer(1);
|
|
|
|
pointers.put(sourceBuffer);
|
|
|
|
GL20C.nglShaderSource(glId, 1, pointers.address0(), 0);
|
|
|
|
}
|
|
|
|
}
|
2022-06-09 21:44:52 +02:00
|
|
|
|
2023-04-30 06:53:56 +02:00
|
|
|
private static boolean _decideIfWeAreAMDWindows() {
|
2022-06-09 21:44:52 +02:00
|
|
|
if (Util.getPlatform() != Util.OS.WINDOWS) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
String vendor = GL20C.glGetString(GL20C.GL_VENDOR);
|
|
|
|
|
|
|
|
if (vendor == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// vendor string I got was "ATI Technologies Inc."
|
|
|
|
return vendor.contains("ATI") || vendor.contains("AMD");
|
|
|
|
}
|
2021-02-21 00:22:07 +01:00
|
|
|
}
|
|
|
|
|