VulkanShader_1.21.10-0.0.4-alpha.jar

Download file
    package net.vulkanmod.vulkan;

import com.mojang.blaze3d.buffers.GpuBufferSlice;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import net.minecraft.class_1041;
import net.minecraft.class_243;
import net.minecraft.class_310;
import net.minecraft.class_4184;
import net.minecraft.class_7285;
import net.vulkanmod.render.engine.VkGpuBuffer;
import net.vulkanmod.vulkan.device.DeviceManager;
import net.vulkanmod.vulkan.shader.PipelineState;
import net.vulkanmod.vulkan.texture.VulkanImage;
import net.vulkanmod.vulkan.util.ColorUtil;
import net.vulkanmod.vulkan.util.MappedBuffer;
import net.vulkanmod.vulkan.util.VUtil;
import org.joml.Matrix4f;
import org.lwjgl.system.MemoryUtil;

public abstract class VRenderSystem {
   private static final float DEFAULT_DEPTH_VALUE = 1.0F;
   private static long window;
   public static boolean depthTest = true;
   public static boolean depthMask = true;
   public static int depthFun = 515;
   public static int topology = 3;
   public static int polygonMode = 0;
   public static boolean canSetLineWidth = false;
   public static int colorMask = PipelineState.ColorMask.getColorMask(true, true, true, true);
   public static boolean cull = true;
   public static boolean logicOp = false;
   public static int logicOpFun = 0;
   public static float clearDepthValue = 1.0F;
   public static FloatBuffer clearColor = MemoryUtil.memCallocFloat(4);
   public static MappedBuffer modelViewMatrix = new MappedBuffer(64);
   public static MappedBuffer projectionMatrix = new MappedBuffer(64);
   public static MappedBuffer TextureMatrix = new MappedBuffer(64);
   public static MappedBuffer MVP = new MappedBuffer(64);
   public static MappedBuffer modelOffset = new MappedBuffer(12);
   public static MappedBuffer lightDirection0 = new MappedBuffer(16);
   public static MappedBuffer lightDirection1 = new MappedBuffer(16);
   public static MappedBuffer lightSpaceMatrix = new MappedBuffer(64);
   public static MappedBuffer inverseProjectionMatrix = new MappedBuffer(64);
   public static MappedBuffer inverseViewMatrix = new MappedBuffer(64);
   public static MappedBuffer lightSpaceViewMatrix = new MappedBuffer(64);
   public static MappedBuffer cameraWorldPos = new MappedBuffer(12);
   private static final long START_TIME_MS = System.currentTimeMillis();
   public static MappedBuffer shaderColor;
   public static MappedBuffer shaderFogColor;
   public static class_7285 fogData;
   public static MappedBuffer screenSize;
   public static VulkanImage sceneColorImage;
   public static float alphaCutout;
   private static boolean depthBiasEnabled;
   private static float depthBiasConstant;
   private static float depthBiasSlope;

   public static MappedBuffer getCameraWorldPos() {
      class_4184 cam = class_310.method_1551().field_1773.method_19418();
      class_243 pos = cam.method_19326();
      VUtil.UNSAFE.putFloat(cameraWorldPos.ptr, (float)pos.field_1352);
      VUtil.UNSAFE.putFloat(cameraWorldPos.ptr + 4L, (float)pos.field_1351);
      VUtil.UNSAFE.putFloat(cameraWorldPos.ptr + 8L, (float)pos.field_1350);
      return cameraWorldPos;
   }

   public static float getTime() {
      return (float)(System.currentTimeMillis() - START_TIME_MS) / 1000.0F;
   }

   public static void createSceneColorImage(int width, int height, int format) {
      if (sceneColorImage != null) {
         sceneColorImage.free();
      }

      sceneColorImage = VulkanImage.builder(width, height).setName("SceneColor").setFormat(format).setUsage(6).setLinearFiltering(true).setClamp(true).createVulkanImage();
   }

   public static void initRenderer() {
      Vulkan.initVulkan(window);
      setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
   }

   public static MappedBuffer getScreenSize() {
      updateScreenSize();
      return screenSize;
   }

   public static void updateScreenSize() {
      class_1041 window = class_310.method_1551().method_22683();
      screenSize.putFloat(0, (float)window.method_4489());
      screenSize.putFloat(4, (float)window.method_4506());
   }

   public static void setWindow(long window) {
      VRenderSystem.window = window;
   }

   public static ByteBuffer getModelOffset() {
      return modelOffset.buffer;
   }

   public static int maxSupportedTextureSize() {
      return DeviceManager.deviceProperties.limits().maxImageDimension2D();
   }

   public static void applyMVP(Matrix4f MV, Matrix4f P) {
      applyModelViewMatrix(MV);
      applyProjectionMatrix(P);
      calculateMVP();
   }

   public static void applyModelViewMatrix(Matrix4f mat) {
      mat.get(modelViewMatrix.buffer.asFloatBuffer());
   }

   public static void applyProjectionMatrix(Matrix4f mat) {
      mat.get(projectionMatrix.buffer.asFloatBuffer());
      mat.invert(new Matrix4f()).get(inverseProjectionMatrix.buffer.asFloatBuffer());
   }

   public static void applyProjectionMatrix(GpuBufferSlice bufferSlice) {
      long ptr = ((VkGpuBuffer)bufferSlice.buffer()).getBuffer().getDataPtr();
      ByteBuffer byteBuffer = MemoryUtil.memByteBuffer(ptr + (long)bufferSlice.offset(), bufferSlice.length());
      Matrix4f matrix4f = (new Matrix4f()).set(byteBuffer);
      matrix4f.get(projectionMatrix.buffer.asFloatBuffer());
      matrix4f.invert(new Matrix4f()).get(inverseProjectionMatrix.buffer.asFloatBuffer());
   }

   public static MappedBuffer getInverseProjectionMatrix() {
      return inverseProjectionMatrix;
   }

   public static void calculateMVP() {
      Matrix4f MV = new Matrix4f(modelViewMatrix.buffer.asFloatBuffer());
      Matrix4f P = new Matrix4f(projectionMatrix.buffer.asFloatBuffer());
      P.mul(MV).get(MVP.buffer);
   }

   public static void setTextureMatrix(Matrix4f mat) {
      mat.get(TextureMatrix.buffer.asFloatBuffer());
   }

   public static MappedBuffer getTextureMatrix() {
      return TextureMatrix;
   }

   public static MappedBuffer getModelViewMatrix() {
      return modelViewMatrix;
   }

   public static MappedBuffer getProjectionMatrix() {
      return projectionMatrix;
   }

   public static MappedBuffer getMVP() {
      return MVP;
   }

   public static MappedBuffer getLightSpaceMatrix() {
      return lightSpaceMatrix;
   }

   public static void setCameraViewMatrix(Matrix4f viewMat) {
      viewMat.invert(new Matrix4f()).get(inverseViewMatrix.buffer.asFloatBuffer());
      recomputeLightSpaceViewMatrix();
   }

   public static void recomputeLightSpaceViewMatrix() {
      Matrix4f lsm = (new Matrix4f()).set(lightSpaceMatrix.buffer.asFloatBuffer());
      Matrix4f ivm = (new Matrix4f()).set(inverseViewMatrix.buffer.asFloatBuffer());
      lsm.mul(ivm).get(lightSpaceViewMatrix.buffer.asFloatBuffer());
   }

   public static MappedBuffer getLightSpaceViewMatrix() {
      return lightSpaceViewMatrix;
   }

   public static void setShaderLightDir(int index, float x, float y, float z) {
      long ptr = (index == 0 ? lightDirection0 : lightDirection1).ptr;
      VUtil.UNSAFE.putFloat(ptr, x);
      VUtil.UNSAFE.putFloat(ptr + 4L, y);
      VUtil.UNSAFE.putFloat(ptr + 8L, z);
      VUtil.UNSAFE.putFloat(ptr + 12L, 0.0F);
   }

   public static void setModelOffset(float x, float y, float z) {
      long ptr = modelOffset.ptr;
      VUtil.UNSAFE.putFloat(ptr, x);
      VUtil.UNSAFE.putFloat(ptr + 4L, y);
      VUtil.UNSAFE.putFloat(ptr + 8L, z);
   }

   public static void setShaderColor(float f1, float f2, float f3, float f4) {
      ColorUtil.setRGBA_Buffer(shaderColor, f1, f2, f3, f4);
   }

   public static void setShaderFogColor(float f1, float f2, float f3, float f4) {
      ColorUtil.setRGBA_Buffer(shaderFogColor, f1, f2, f3, f4);
   }

   public static MappedBuffer getShaderColor() {
      return shaderColor;
   }

   public static MappedBuffer getShaderFogColor() {
      return shaderFogColor;
   }

   public static class_7285 getFogData() {
      return fogData;
   }

   public static void setClearColor(float f1, float f2, float f3, float f4) {
      ColorUtil.setRGBA_Buffer(clearColor, f1, f2, f3, f4);
   }

   public static void clear(int mask) {
      Renderer.clearAttachments(mask);
   }

   public static void clearDepth(double depth) {
      clearDepthValue = (float)depth;
   }

   public static void disableDepthTest() {
      depthTest = false;
   }

   public static void depthMask(boolean b) {
      depthMask = b;
   }

   public static void setPrimitiveTopologyGL(int mode) {
      byte var10000;
      switch (mode) {
         case 1:
         case 3:
            var10000 = 1;
            break;
         case 2:
         default:
            throw new RuntimeException(String.format("Unknown GL primitive topology: %s", mode));
         case 4:
         case 5:
         case 6:
            var10000 = 3;
      }

      topology = var10000;
   }

   public static void setPolygonModeGL(int mode) {
      byte var10000;
      switch (mode) {
         case 6912 -> var10000 = 2;
         case 6913 -> var10000 = 1;
         case 6914 -> var10000 = 0;
         default -> throw new RuntimeException(String.format("Unknown GL polygon mode: %s", mode));
      }

      polygonMode = var10000;
   }

   public static void setLineWidth(float width) {
      if (canSetLineWidth) {
         Renderer.setLineWidth(width);
      }

   }

   public static void colorMask(boolean b, boolean b1, boolean b2, boolean b3) {
      colorMask = PipelineState.ColorMask.getColorMask(b, b1, b2, b3);
   }

   public static int getColorMask() {
      return colorMask;
   }

   public static void enableDepthTest() {
      depthTest = true;
   }

   public static void enableCull() {
      cull = true;
   }

   public static void disableCull() {
      cull = false;
   }

   public static void depthFunc(int depthFun) {
      VRenderSystem.depthFun = depthFun;
   }

   public static void enableBlend() {
      PipelineState.blendInfo.enabled = true;
   }

   public static void disableBlend() {
      PipelineState.blendInfo.enabled = false;
   }

   public static void blendFunc(int srcFactor, int dstFactor) {
      PipelineState.blendInfo.setBlendFunction(srcFactor, dstFactor);
   }

   public static void blendFuncSeparate(int srcFactorRGB, int dstFactorRGB, int srcFactorAlpha, int dstFactorAlpha) {
      PipelineState.blendInfo.setBlendFuncSeparate(srcFactorRGB, dstFactorRGB, srcFactorAlpha, dstFactorAlpha);
   }

   public static void blendOp(int op) {
      PipelineState.blendInfo.setBlendOp(op);
   }

   public static void enableColorLogicOp() {
      logicOp = true;
   }

   public static void disableColorLogicOp() {
      logicOp = false;
   }

   public static void logicOp(int glLogicOp) {
      logicOpFun = glLogicOp;
   }

   public static void polygonOffset(float slope, float biasConstant) {
      if (depthBiasConstant != biasConstant || depthBiasSlope != slope) {
         depthBiasConstant = biasConstant;
         depthBiasSlope = slope;
         Renderer.setDepthBias(depthBiasConstant, depthBiasSlope);
      }

   }

   public static void enablePolygonOffset() {
      if (!depthBiasEnabled) {
         Renderer.setDepthBias(depthBiasConstant, depthBiasSlope);
         depthBiasEnabled = true;
      }

   }

   public static void disablePolygonOffset() {
      if (depthBiasEnabled) {
         Renderer.setDepthBias(0.0F, 0.0F);
         depthBiasEnabled = false;
      }

   }

   static {
      (new Matrix4f()).identity().get(lightSpaceMatrix.buffer.asFloatBuffer());
      shaderColor = new MappedBuffer(16);
      shaderFogColor = new MappedBuffer(16);
      screenSize = new MappedBuffer(8);
      alphaCutout = 0.0F;
      depthBiasEnabled = false;
      depthBiasConstant = 0.0F;
      depthBiasSlope = 0.0F;
   }
}
    
Download file