| 1 | package com.mojang.renderpearl.backend.opengl;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.renderpearl.api.device.DeviceFeatures;
|
| 5 | import com.mojang.renderpearl.api.device.DeviceInfo;
|
| 6 | import com.mojang.renderpearl.api.device.DeviceLimits;
|
| 7 | import com.mojang.renderpearl.api.device.DeviceType;
|
| 8 | import com.mojang.renderpearl.api.device.HintsAndWorkarounds;
|
| 9 | import java.util.Collections;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Locale;
|
| 12 | import java.util.Set;
|
| 13 | import net.fabricmc.api.EnvType;
|
| 14 | import net.fabricmc.api.Environment;
|
| 15 | import net.minecraft.util.Util;
|
| 16 | import net.minecraft.util.Util.OS;
|
| 17 | import org.lwjgl.opengl.GL33C;
|
| 18 | import org.lwjgl.opengl.GLCapabilities;
|
| 19 | import org.slf4j.Logger;
|
| 20 |
|
| 21 | @Environment(EnvType.CLIENT)
|
| 22 | public class GlHeuristics {
|
| 23 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 24 | private static final List<String> DEVICE_NAMES_THAT_IMPLY_CPU = List.of("mesa offscreen", "llvmpipe");
|
| 25 | private static final List<String> DEVICE_NAMES_THAT_IMPLY_VIRTUAL = List.of("virtgl");
|
| 26 | private final boolean isGlOnDx12;
|
| 27 | private final boolean isAmd;
|
| 28 | private final boolean isNvidia;
|
| 29 | private final boolean couldBeIntelGen7;
|
| 30 |
|
| 31 | GlHeuristics(final String deviceName, final String vendor) {
|
| 32 | this.isGlOnDx12 = isGlOnDx12(deviceName);
|
| 33 | this.isAmd = isAmd(deviceName);
|
| 34 | this.isNvidia = isNvidia(deviceName);
|
| 35 | this.couldBeIntelGen7 = couldBeIntelGen7(deviceName.toLowerCase(Locale.ROOT), vendor.toLowerCase(Locale.ROOT));
|
| 36 | }
|
| 37 |
|
| 38 | public boolean isGlOnDx12() {
|
| 39 | return this.isGlOnDx12;
|
| 40 | }
|
| 41 |
|
| 42 | public boolean isAmd() {
|
| 43 | return this.isAmd;
|
| 44 | }
|
| 45 |
|
| 46 | public boolean isNvidia() {
|
| 47 | return this.isNvidia;
|
| 48 | }
|
| 49 |
|
| 50 | public boolean couldBeIntelGen7() {
|
| 51 | return this.couldBeIntelGen7;
|
| 52 | }
|
| 53 |
|
| 54 | private static boolean isGlOnDx12(final String deviceName) {
|
| 55 | boolean isWindowsArm64 = Util.getPlatform() == OS.WINDOWS && Util.isAarch64();
|
| 56 | return isWindowsArm64 || deviceName.startsWith("D3D12");
|
| 57 | }
|
| 58 |
|
| 59 | private static boolean isAmd(final String deviceName) {
|
| 60 | return deviceName.contains("AMD");
|
| 61 | }
|
| 62 |
|
| 63 | private static boolean isNvidia(final String deviceName) {
|
| 64 | return deviceName.toLowerCase(Locale.ROOT).contains("nvidia");
|
| 65 | }
|
| 66 |
|
| 67 | private static int getMaxSupportedTextureSize() {
|
| 68 | int maxReported = GlStateManager._getInteger(3379);
|
| 69 |
|
| 70 | for (int texSize = Math.max(32768, maxReported); texSize >= 1024; texSize >>= 1) {
|
| 71 | GlStateManager._texImage2D(32868, 0, 6408, texSize, texSize, 0, 6408, 5121, null);
|
| 72 | int width = GlStateManager._getTexLevelParameter(32868, 0, 4096);
|
| 73 | if (width != 0) {
|
| 74 | return texSize;
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | int maxSupportedTextureSize = Math.max(maxReported, 1024);
|
| 79 | LOGGER.info("Failed to determine maximum texture size by probing, trying GL_MAX_TEXTURE_SIZE = {}", maxSupportedTextureSize);
|
| 80 | return maxSupportedTextureSize;
|
| 81 | }
|
| 82 |
|
| 83 | public DeviceInfo createDeviceInfo(final GLCapabilities capabilities, final int maxSupportedAnisotropy, final Set<String> enabledExtensions) {
|
| 84 | String renderer = GlStateManager._getString(7937);
|
| 85 | String vendor = GlStateManager._getString(7936);
|
| 86 | String rendererLowerCase = renderer.toLowerCase(Locale.ROOT);
|
| 87 | String vendorLowerCase = vendor.toLowerCase(Locale.ROOT);
|
| 88 | int drawIndirectCount = enabledExtensions.contains("GL_ARB_multi_draw_indirect")
|
| 89 | ? Integer.MAX_VALUE
|
| 90 | : (enabledExtensions.contains("GL_ARB_draw_indirect") ? 1 : 0);
|
| 91 | return new DeviceInfo(
|
| 92 | renderer,
|
| 93 | vendor,
|
| 94 | GlStateManager._getString(7938),
|
| 95 | capabilities.GL_ARB_clip_control,
|
| 96 | "OpenGL",
|
| 97 | 1.0F,
|
| 98 | new DeviceLimits(
|
| 99 | maxSupportedAnisotropy, GL33C.glGetInteger(35380), getMaxSupportedTextureSize(), Long.MAX_VALUE, 0, GL33C.glGetInteger(34852), drawIndirectCount
|
| 100 | ),
|
| 101 | new DeviceFeatures(
|
| 102 | true,
|
| 103 | enabledExtensions.contains("GL_ARB_shader_draw_parameters"),
|
| 104 | false,
|
| 105 | true,
|
| 106 | enabledExtensions.contains("GL_ARB_multi_draw_indirect"),
|
| 107 | enabledExtensions.contains("GL_ARB_draw_indirect"),
|
| 108 | enabledExtensions.contains("GL_ARB_base_instance"),
|
| 109 | enabledExtensions.contains("GL_ARB_buffer_storage")
|
| 110 | ),
|
| 111 | Collections.unmodifiableSet(enabledExtensions),
|
| 112 | new HintsAndWorkarounds(
|
| 113 | this.isGlOnDx12(), this.isAmd(), Util.isAppleSiliconMac(renderer), vendorLowerCase.contains("intel") && !rendererLowerCase.contains("arc")
|
| 114 | ),
|
| 115 | this.guessDeviceType(rendererLowerCase, vendorLowerCase)
|
| 116 | );
|
| 117 | }
|
| 118 |
|
| 119 | private static boolean couldBeIntelGen7(final String renderer, final String vendor) {
|
| 120 | if (!vendor.contains("intel")) {
|
| 121 | return false;
|
| 122 | } else if (renderer.contains("2500")) {
|
| 123 | return true;
|
| 124 | } else if (renderer.contains("4000")) {
|
| 125 | return true;
|
| 126 | } else {
|
| 127 | return renderer.contains("hd graphics (byt)") ? true : renderer.endsWith("hd graphics");
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | private DeviceType guessDeviceType(final String renderer, final String vendor) {
|
| 132 | if (vendor.contains("intel")) {
|
| 133 | return renderer.contains("arc") ? DeviceType.DISCRETE : DeviceType.INTEGRATED;
|
| 134 | }
|
| 135 |
|
| 136 | for (String string : DEVICE_NAMES_THAT_IMPLY_CPU) {
|
| 137 | if (renderer.contains(string)) {
|
| 138 | return DeviceType.CPU;
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | for (String string : DEVICE_NAMES_THAT_IMPLY_VIRTUAL) {
|
| 143 | if (renderer.contains(string)) {
|
| 144 | return DeviceType.VIRTUAL;
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | return DeviceType.OTHER;
|
| 149 | }
|
| 150 | }
|
| 151 |
|