| 1 | package net.minecraft.client.renderer;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.google.common.collect.ImmutableMap.Builder;
|
| 6 | import com.google.gson.JsonArray;
|
| 7 | import com.google.gson.JsonObject;
|
| 8 | import com.google.gson.JsonSyntaxException;
|
| 9 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 10 | import com.mojang.logging.LogUtils;
|
| 11 | import com.mojang.renderpearl.api.device.DeviceInfo;
|
| 12 | import com.mojang.renderpearl.api.device.GpuDevice;
|
| 13 | import java.io.IOException;
|
| 14 | import java.io.Reader;
|
| 15 | import java.util.List;
|
| 16 | import java.util.regex.Matcher;
|
| 17 | import java.util.regex.Pattern;
|
| 18 | import net.fabricmc.api.EnvType;
|
| 19 | import net.fabricmc.api.Environment;
|
| 20 | import net.minecraft.resources.Identifier;
|
| 21 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 22 | import net.minecraft.server.packs.resources.SimplePreparableReloadListener;
|
| 23 | import net.minecraft.util.StrictJsonParser;
|
| 24 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 25 | import net.minecraft.util.profiling.Zone;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 | import org.slf4j.Logger;
|
| 28 |
|
| 29 | @Environment(EnvType.CLIENT)
|
| 30 | public class GpuWarnlistManager extends SimplePreparableReloadListener<GpuWarnlistManager.Preparations> {
|
| 31 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 32 | private static final Identifier GPU_WARNLIST_LOCATION = Identifier.withDefaultNamespace("gpu_warnlist.json");
|
| 33 | private ImmutableMap<String, String> warnings = ImmutableMap.of();
|
| 34 | private boolean showWarning;
|
| 35 | private boolean warningDismissed;
|
| 36 |
|
| 37 | public boolean hasWarnings() {
|
| 38 | return !this.warnings.isEmpty();
|
| 39 | }
|
| 40 |
|
| 41 | public boolean willShowWarning() {
|
| 42 | return this.hasWarnings() && !this.warningDismissed;
|
| 43 | }
|
| 44 |
|
| 45 | public void showWarning() {
|
| 46 | this.showWarning = true;
|
| 47 | }
|
| 48 |
|
| 49 | public void dismissWarning() {
|
| 50 | this.warningDismissed = true;
|
| 51 | }
|
| 52 |
|
| 53 | public boolean isShowingWarning() {
|
| 54 | return this.showWarning && !this.warningDismissed;
|
| 55 | }
|
| 56 |
|
| 57 | public void resetWarnings() {
|
| 58 | this.showWarning = false;
|
| 59 | this.warningDismissed = false;
|
| 60 | }
|
| 61 |
|
| 62 | public @Nullable String getRendererWarnings() {
|
| 63 | return this.warnings.get("renderer");
|
| 64 | }
|
| 65 |
|
| 66 | public @Nullable String getVersionWarnings() {
|
| 67 | return this.warnings.get("version");
|
| 68 | }
|
| 69 |
|
| 70 | public @Nullable String getVendorWarnings() {
|
| 71 | return this.warnings.get("vendor");
|
| 72 | }
|
| 73 |
|
| 74 | public @Nullable String getAllWarnings() {
|
| 75 | StringBuilder sb = new StringBuilder();
|
| 76 | this.warnings.forEach((k, v) -> sb.append(k).append(": ").append(v));
|
| 77 | return sb.isEmpty() ? null : sb.toString();
|
| 78 | }
|
| 79 |
|
| 80 | protected GpuWarnlistManager.Preparations prepare(final ResourceManager manager, final ProfilerFiller profiler) {
|
| 81 | List<Pattern> rendererPatterns = Lists.newArrayList();
|
| 82 | List<Pattern> versionPatterns = Lists.newArrayList();
|
| 83 | List<Pattern> vendorPatterns = Lists.newArrayList();
|
| 84 | JsonObject root = parseJson(manager, profiler);
|
| 85 | if (root != null) {
|
| 86 | try (Zone ignored = profiler.zone("compile_regex")) {
|
| 87 | compilePatterns(root.getAsJsonArray("renderer"), rendererPatterns);
|
| 88 | compilePatterns(root.getAsJsonArray("version"), versionPatterns);
|
| 89 | compilePatterns(root.getAsJsonArray("vendor"), vendorPatterns);
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | return new GpuWarnlistManager.Preparations(rendererPatterns, versionPatterns, vendorPatterns);
|
| 94 | }
|
| 95 |
|
| 96 | protected void apply(final GpuWarnlistManager.Preparations preparations, final ResourceManager manager, final ProfilerFiller profiler) {
|
| 97 | this.warnings = preparations.apply();
|
| 98 | }
|
| 99 |
|
| 100 | private static void compilePatterns(final JsonArray jsonArray, final List<Pattern> patternList) {
|
| 101 | jsonArray.forEach(e -> patternList.add(Pattern.compile(e.getAsString(), 2)));
|
| 102 | }
|
| 103 |
|
| 104 | private static @Nullable JsonObject parseJson(final ResourceManager manager, final ProfilerFiller profiler) {
|
| 105 | try (
|
| 106 | Zone ignored = profiler.zone("parse_json");
|
| 107 | Reader resource = manager.openAsReader(GPU_WARNLIST_LOCATION);
|
| 108 | ) {
|
| 109 | return StrictJsonParser.parse(resource).getAsJsonObject();
|
| 110 | } catch (IOException | JsonSyntaxException e) {
|
| 111 | LOGGER.warn("Failed to load GPU warnlist", e);
|
| 112 | return null;
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | @Environment(EnvType.CLIENT)
|
| 117 | protected static final class Preparations {
|
| 118 | private final List<Pattern> rendererPatterns;
|
| 119 | private final List<Pattern> versionPatterns;
|
| 120 | private final List<Pattern> vendorPatterns;
|
| 121 |
|
| 122 | private Preparations(final List<Pattern> rendererPatterns, final List<Pattern> versionPatterns, final List<Pattern> vendorPatterns) {
|
| 123 | this.rendererPatterns = rendererPatterns;
|
| 124 | this.versionPatterns = versionPatterns;
|
| 125 | this.vendorPatterns = vendorPatterns;
|
| 126 | }
|
| 127 |
|
| 128 | private static String matchAny(final List<Pattern> patterns, final String input) {
|
| 129 | List<String> allMatches = Lists.newArrayList();
|
| 130 |
|
| 131 | for (Pattern pattern : patterns) {
|
| 132 | Matcher matcher = pattern.matcher(input);
|
| 133 |
|
| 134 | while (matcher.find()) {
|
| 135 | allMatches.add(matcher.group());
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | return String.join(", ", allMatches);
|
| 140 | }
|
| 141 |
|
| 142 | private ImmutableMap<String, String> apply() {
|
| 143 | Builder<String, String> map = new Builder<>();
|
| 144 | GpuDevice device = RenderSystem.getDevice();
|
| 145 | DeviceInfo deviceInfo = device.getDeviceInfo();
|
| 146 | if (deviceInfo.backendName().equals("OpenGL")) {
|
| 147 | String rendererFails = matchAny(this.rendererPatterns, deviceInfo.name());
|
| 148 | if (!rendererFails.isEmpty()) {
|
| 149 | map.put("renderer", rendererFails);
|
| 150 | }
|
| 151 |
|
| 152 | String versionFails = matchAny(this.versionPatterns, deviceInfo.driverInfo());
|
| 153 | if (!versionFails.isEmpty()) {
|
| 154 | map.put("version", versionFails);
|
| 155 | }
|
| 156 |
|
| 157 | String vendorFails = matchAny(this.vendorPatterns, deviceInfo.vendorName());
|
| 158 | if (!vendorFails.isEmpty()) {
|
| 159 | map.put("vendor", vendorFails);
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | return map.build();
|
| 164 | }
|
| 165 | }
|
| 166 | }
|
| 167 |
|