| 1 | package thunder.hack.utility.render.compat;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.PrimitiveTopology;
|
| 4 | import com.mojang.blaze3d.pipeline.RenderPipeline;
|
| 5 | import com.mojang.blaze3d.buffers.GpuBufferSlice;
|
| 6 | import com.mojang.blaze3d.platform.GlStateManager;
|
| 7 | import com.mojang.blaze3d.systems.GpuDevice;
|
| 8 | import com.mojang.blaze3d.systems.RenderSystem.AutoStorageIndexBuffer;
|
| 9 | import com.mojang.blaze3d.textures.GpuTexture;
|
| 10 | import com.mojang.blaze3d.textures.GpuSampler;
|
| 11 | import com.mojang.blaze3d.textures.GpuTextureView;
|
| 12 | import net.minecraft.client.renderer.DynamicUniforms;
|
| 13 | import org.joml.Matrix4f;
|
| 14 | import org.joml.Matrix4fStack;
|
| 15 | import org.lwjgl.opengl.GL11;
|
| 16 |
|
| 17 | import java.util.LinkedHashMap;
|
| 18 | import java.util.Map;
|
| 19 | import net.minecraft.client.Minecraft;
|
| 20 | import net.minecraft.core.ClientAsset;
|
| 21 | import net.minecraft.resources.Identifier;
|
| 22 |
|
| 23 | public final class RenderSystem {
|
| 24 | public enum BlendMode {
|
| 25 | DEFAULT,
|
| 26 | ADDITIVE,
|
| 27 | DST_ALPHA,
|
| 28 | CUSTOM
|
| 29 | }
|
| 30 |
|
| 31 | private static RenderPipeline currentPipeline;
|
| 32 | private static final Map<String, Object> currentUniforms = new LinkedHashMap<>();
|
| 33 | private static final GpuTextureView[] shaderTextures = new GpuTextureView[12];
|
| 34 | private static final GpuSampler[] shaderSamplers = new GpuSampler[12];
|
| 35 | private static BlendMode blendMode = BlendMode.DEFAULT;
|
| 36 | private static boolean scissorEnabled;
|
| 37 | private static final float[] shaderColor = new float[]{1f, 1f, 1f, 1f};
|
| 38 | private static int scissorX;
|
| 39 | private static int scissorY;
|
| 40 | private static int scissorWidth;
|
| 41 | private static int scissorHeight;
|
| 42 | private static boolean depthTest = true;
|
| 43 | private static int worldDrawingDepth;
|
| 44 | private static Runnable pendingCapture;
|
| 45 |
|
| 46 | private RenderSystem() {
|
| 47 | }
|
| 48 |
|
| 49 | /**
|
| 50 | * World space geometry (ESP, chams, 3D text, …) has to be projected with the level
|
| 51 | * perspective projection, while screens and the HUD use an orthographic GUI projection.
|
| 52 | * Minecraft 26.2 only has one projection per draw call, so call sites that draw world
|
| 53 | * geometry announce themselves here.
|
| 54 | */
|
| 55 | public static void beginWorldDrawing() {
|
| 56 | worldDrawingDepth++;
|
| 57 | }
|
| 58 |
|
| 59 | public static void endWorldDrawing() {
|
| 60 | if (worldDrawingDepth > 0) {
|
| 61 | worldDrawingDepth--;
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | public static boolean isWorldProjection() {
|
| 66 | return worldDrawingDepth > 0;
|
| 67 | }
|
| 68 |
|
| 69 | public static boolean isDepthTestEnabled() {
|
| 70 | return depthTest;
|
| 71 | }
|
| 72 |
|
| 73 | public static boolean isScissorEnabled() {
|
| 74 | return scissorEnabled;
|
| 75 | }
|
| 76 |
|
| 77 | public static int getScissorX() {
|
| 78 | return scissorX;
|
| 79 | }
|
| 80 |
|
| 81 | public static int getScissorY() {
|
| 82 | return scissorY;
|
| 83 | }
|
| 84 |
|
| 85 | public static int getScissorWidth() {
|
| 86 | return scissorWidth;
|
| 87 | }
|
| 88 |
|
| 89 | public static int getScissorHeight() {
|
| 90 | return scissorHeight;
|
| 91 | }
|
| 92 |
|
| 93 | /**
|
| 94 | * Screen captures such as MelaHack's blur have to read the main render target while it still
|
| 95 | * holds the frame they belong to. Because those draws may be replayed later, the capture is
|
| 96 | * deferred as well.
|
| 97 | */
|
| 98 | public static void scheduleCapture(Runnable capture) {
|
| 99 | pendingCapture = capture;
|
| 100 | }
|
| 101 |
|
| 102 | public static Runnable takePendingCapture() {
|
| 103 | Runnable capture = pendingCapture;
|
| 104 | pendingCapture = null;
|
| 105 | return capture;
|
| 106 | }
|
| 107 |
|
| 108 | public static GpuDevice getDevice() {
|
| 109 | return com.mojang.blaze3d.systems.RenderSystem.getDevice();
|
| 110 | }
|
| 111 |
|
| 112 | public static DynamicUniforms getDynamicUniforms() {
|
| 113 | return com.mojang.blaze3d.systems.RenderSystem.getDynamicUniforms();
|
| 114 | }
|
| 115 |
|
| 116 | public static AutoStorageIndexBuffer getSequentialBuffer(PrimitiveTopology topology) {
|
| 117 | return com.mojang.blaze3d.systems.RenderSystem.getSequentialBuffer(topology);
|
| 118 | }
|
| 119 |
|
| 120 | public static GpuBufferSlice getProjectionMatrixBuffer() {
|
| 121 | return com.mojang.blaze3d.systems.RenderSystem.getProjectionMatrixBuffer();
|
| 122 | }
|
| 123 |
|
| 124 | public static final int PROJECTION_MATRIX_UBO_SIZE =
|
| 125 | com.mojang.blaze3d.systems.RenderSystem.PROJECTION_MATRIX_UBO_SIZE;
|
| 126 |
|
| 127 | public static RenderPipeline getCurrentPipeline() {
|
| 128 | return currentPipeline;
|
| 129 | }
|
| 130 |
|
| 131 | public static Map<String, Object> getCurrentUniforms() {
|
| 132 | return currentUniforms;
|
| 133 | }
|
| 134 |
|
| 135 | public static BlendMode getBlendMode() {
|
| 136 | return blendMode;
|
| 137 | }
|
| 138 |
|
| 139 | public static void setShader(RenderPipeline pipeline) {
|
| 140 | if (currentPipeline != pipeline) {
|
| 141 | currentUniforms.clear();
|
| 142 | }
|
| 143 | currentPipeline = pipeline;
|
| 144 | }
|
| 145 |
|
| 146 | public static void setShader(Object ignored) {
|
| 147 | if (ignored instanceof RenderPipeline pipeline) {
|
| 148 | setShader(pipeline);
|
| 149 | } else {
|
| 150 | currentPipeline = null;
|
| 151 | currentUniforms.clear();
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | public static void setShaderUniform(String name, float... values) {
|
| 156 | currentUniforms.put(name, values);
|
| 157 | }
|
| 158 |
|
| 159 | public static void setShaderUniform(String name, int... values) {
|
| 160 | currentUniforms.put(name, values);
|
| 161 | }
|
| 162 |
|
| 163 | public static void setShaderUniform(String name, Matrix4f value) {
|
| 164 | currentUniforms.put(name, value);
|
| 165 | }
|
| 166 |
|
| 167 | public static void enableBlend() {
|
| 168 | com.mojang.blaze3d.opengl.GlStateManager._enableBlend(0);
|
| 169 | }
|
| 170 |
|
| 171 | public static void disableBlend() {
|
| 172 | com.mojang.blaze3d.opengl.GlStateManager._disableBlend(0);
|
| 173 | }
|
| 174 |
|
| 175 | public static void defaultBlendFunc() {
|
| 176 | com.mojang.blaze3d.opengl.GlStateManager._blendFuncSeparate(
|
| 177 | GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
|
| 178 | blendMode = BlendMode.DEFAULT;
|
| 179 | }
|
| 180 |
|
| 181 | public static void blendFunc(GlStateManager.SrcFactor src, GlStateManager.DstFactor dst) {
|
| 182 | blendFunc(src.value, dst.value);
|
| 183 | }
|
| 184 |
|
| 185 | public static void blendFunc(int src, int dst) {
|
| 186 | com.mojang.blaze3d.opengl.GlStateManager._blendFuncSeparate(src, dst, GL11.GL_ONE, GL11.GL_ZERO);
|
| 187 | blendMode = classifyBlend(src, dst, GL11.GL_ONE, GL11.GL_ZERO);
|
| 188 | }
|
| 189 |
|
| 190 | public static void blendFuncSeparate(
|
| 191 | GlStateManager.SrcFactor srcRgb,
|
| 192 | GlStateManager.DstFactor dstRgb,
|
| 193 | GlStateManager.SrcFactor srcAlpha,
|
| 194 | GlStateManager.DstFactor dstAlpha
|
| 195 | ) {
|
| 196 | com.mojang.blaze3d.opengl.GlStateManager._blendFuncSeparate(
|
| 197 | srcRgb.value, dstRgb.value, srcAlpha.value, dstAlpha.value);
|
| 198 | blendMode = classifyBlend(srcRgb.value, dstRgb.value, srcAlpha.value, dstAlpha.value);
|
| 199 | }
|
| 200 |
|
| 201 | private static BlendMode classifyBlend(int srcRgb, int dstRgb, int srcAlpha, int dstAlpha) {
|
| 202 | if (srcRgb == GL11.GL_SRC_ALPHA
|
| 203 | && dstRgb == GL11.GL_ONE
|
| 204 | && srcAlpha == GL11.GL_ONE
|
| 205 | && dstAlpha == GL11.GL_ZERO) {
|
| 206 | return BlendMode.ADDITIVE;
|
| 207 | }
|
| 208 |
|
| 209 | if (srcRgb == GL11.GL_SRC_ALPHA
|
| 210 | && dstRgb == GL11.GL_ONE_MINUS_SRC_ALPHA
|
| 211 | && srcAlpha == GL11.GL_ONE
|
| 212 | && dstAlpha == GL11.GL_ZERO) {
|
| 213 | return BlendMode.DEFAULT;
|
| 214 | }
|
| 215 |
|
| 216 | if (srcRgb == GL11.GL_DST_ALPHA
|
| 217 | && dstRgb == GL11.GL_ONE_MINUS_DST_ALPHA
|
| 218 | && srcAlpha == GL11.GL_ONE
|
| 219 | && dstAlpha == GL11.GL_ZERO) {
|
| 220 | return BlendMode.DST_ALPHA;
|
| 221 | }
|
| 222 |
|
| 223 | return BlendMode.CUSTOM;
|
| 224 | }
|
| 225 |
|
| 226 | public static void enableDepthTest() {
|
| 227 | depthTest = true;
|
| 228 | }
|
| 229 |
|
| 230 | public static void disableDepthTest() {
|
| 231 | depthTest = false;
|
| 232 | }
|
| 233 |
|
| 234 | public static void depthMask(boolean mask) {
|
| 235 | com.mojang.blaze3d.opengl.GlStateManager._depthMask(mask);
|
| 236 | }
|
| 237 |
|
| 238 | public static void depthFunc(int func) {
|
| 239 | com.mojang.blaze3d.opengl.GlStateManager._depthFunc(func);
|
| 240 | }
|
| 241 |
|
| 242 | public static void enableCull() {
|
| 243 | com.mojang.blaze3d.opengl.GlStateManager._enableCull();
|
| 244 | }
|
| 245 |
|
| 246 | public static void disableCull() {
|
| 247 | com.mojang.blaze3d.opengl.GlStateManager._disableCull();
|
| 248 | }
|
| 249 |
|
| 250 | public static void colorMask(boolean red, boolean green, boolean blue, boolean alpha) {
|
| 251 | com.mojang.blaze3d.opengl.GlStateManager._colorMask((red ? 1 : 0) | (green ? 2 : 0) | (blue ? 4 : 0) | (alpha ? 8 : 0));
|
| 252 | }
|
| 253 |
|
| 254 | public static void lineWidth(float width) {
|
| 255 | // Line width is encoded by modern render pipelines rather than mutable global state.
|
| 256 | }
|
| 257 |
|
| 258 | public static void setShaderColor(float red, float green, float blue, float alpha) {
|
| 259 | shaderColor[0] = red;
|
| 260 | shaderColor[1] = green;
|
| 261 | shaderColor[2] = blue;
|
| 262 | shaderColor[3] = alpha;
|
| 263 | }
|
| 264 |
|
| 265 | public static float[] getShaderColor() {
|
| 266 | return shaderColor;
|
| 267 | }
|
| 268 |
|
| 269 | public static void setShaderTexture(int slot, Identifier id) {
|
| 270 | var texture = Minecraft.getInstance().getTextureManager().getTexture(id);
|
| 271 | rememberTexture(slot, texture.getTextureView(), texture.getSampler());
|
| 272 | }
|
| 273 |
|
| 274 | public static void setShaderTexture(int slot, ClientAsset.Texture texture) {
|
| 275 | setShaderTexture(slot, texture.texturePath());
|
| 276 | }
|
| 277 |
|
| 278 | public static void setShaderTexture(int slot, GpuTexture texture) {
|
| 279 | // Callers with a raw texture must provide a view on modern Blaze3D.
|
| 280 | }
|
| 281 |
|
| 282 | public static void setShaderTexture(int slot, GpuTextureView texture) {
|
| 283 | rememberTexture(slot, texture, com.mojang.blaze3d.systems.RenderSystem.getSamplerCache()
|
| 284 | .getClampToEdge(com.mojang.blaze3d.textures.FilterMode.LINEAR));
|
| 285 | }
|
| 286 |
|
| 287 | private static void rememberTexture(int slot, GpuTextureView texture, GpuSampler sampler) {
|
| 288 | if (slot >= 0 && slot < shaderTextures.length) {
|
| 289 | shaderTextures[slot] = texture;
|
| 290 | shaderSamplers[slot] = sampler;
|
| 291 | }
|
| 292 | }
|
| 293 |
|
| 294 | public static GpuTextureView getShaderTexture(int slot) {
|
| 295 | return slot >= 0 && slot < shaderTextures.length ? shaderTextures[slot] : null;
|
| 296 | }
|
| 297 |
|
| 298 | public static GpuSampler getShaderSampler(int slot) {
|
| 299 | return slot >= 0 && slot < shaderSamplers.length ? shaderSamplers[slot] : null;
|
| 300 | }
|
| 301 |
|
| 302 | public static void clearColor(float red, float green, float blue, float alpha) {
|
| 303 | GL11.glClearColor(red, green, blue, alpha);
|
| 304 | }
|
| 305 |
|
| 306 | public static void clear(int mask) {
|
| 307 | com.mojang.blaze3d.opengl.GlStateManager._clear(mask);
|
| 308 | }
|
| 309 |
|
| 310 | public static void texParameter(int target, int pname, int param) {
|
| 311 | com.mojang.blaze3d.opengl.GlStateManager._texParameter(target, pname, param);
|
| 312 | }
|
| 313 |
|
| 314 | public static void enableScissor(int x, int y, int width, int height) {
|
| 315 | scissorEnabled = true;
|
| 316 | scissorX = x;
|
| 317 | scissorY = y;
|
| 318 | scissorWidth = width;
|
| 319 | scissorHeight = height;
|
| 320 | com.mojang.blaze3d.systems.RenderSystem.enableScissorForRenderTypeDraws(x, y, width, height);
|
| 321 | }
|
| 322 |
|
| 323 | public static void disableScissor() {
|
| 324 | scissorEnabled = false;
|
| 325 | com.mojang.blaze3d.systems.RenderSystem.disableScissorForRenderTypeDraws();
|
| 326 | }
|
| 327 |
|
| 328 | public static void applyScissor(com.mojang.blaze3d.systems.RenderPass pass) {
|
| 329 | if (scissorEnabled) {
|
| 330 | pass.enableScissor(scissorX, scissorY, scissorWidth, scissorHeight);
|
| 331 | } else {
|
| 332 | pass.disableScissor();
|
| 333 | }
|
| 334 | }
|
| 335 |
|
| 336 | public static void backupProjectionMatrix() {
|
| 337 | com.mojang.blaze3d.systems.RenderSystem.backupProjectionMatrix();
|
| 338 | }
|
| 339 |
|
| 340 | public static void restoreProjectionMatrix() {
|
| 341 | com.mojang.blaze3d.systems.RenderSystem.restoreProjectionMatrix();
|
| 342 | }
|
| 343 |
|
| 344 | public static Matrix4f getProjectionMatrix() {
|
| 345 | return new Matrix4f();
|
| 346 | }
|
| 347 |
|
| 348 | public static Matrix4f getModelViewMatrix() {
|
| 349 | return com.mojang.blaze3d.systems.RenderSystem.getModelViewMatrixCopy();
|
| 350 | }
|
| 351 |
|
| 352 | public static Matrix4fStack getModelViewStack() {
|
| 353 | return com.mojang.blaze3d.systems.RenderSystem.getModelViewStack();
|
| 354 | }
|
| 355 |
|
| 356 | public static void resetTextureMatrix() {
|
| 357 | // Texture transforms are supplied through pipeline uniforms in modern Blaze3D.
|
| 358 | }
|
| 359 |
|
| 360 | public static GpuBufferSlice getShaderFog() {
|
| 361 | return com.mojang.blaze3d.systems.RenderSystem.getShaderFog();
|
| 362 | }
|
| 363 |
|
| 364 | public static void setShaderFog(GpuBufferSlice fog) {
|
| 365 | com.mojang.blaze3d.systems.RenderSystem.setShaderFog(fog);
|
| 366 | }
|
| 367 |
|
| 368 | public static boolean isOnRenderThread() {
|
| 369 | return com.mojang.blaze3d.systems.RenderSystem.isOnRenderThread();
|
| 370 | }
|
| 371 |
|
| 372 | public static void assertOnRenderThreadOrInit() {
|
| 373 | if (!isOnRenderThread()) {
|
| 374 | com.mojang.blaze3d.systems.RenderSystem.assertOnRenderThread();
|
| 375 | }
|
| 376 | }
|
| 377 |
|
| 378 | public static void assertInInitPhase() {
|
| 379 | }
|
| 380 |
|
| 381 | public static void recordRenderCall(Runnable runnable) {
|
| 382 | runnable.run();
|
| 383 | }
|
| 384 | }
|
| 385 |
|