VulkanShader_1.21.10-0.0.4-alpha.jar

Download file
    package net.vulkanmod.vulkan;

import com.mojang.blaze3d.opengl.GlStateManager;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import net.minecraft.class_310;
import net.vulkanmod.Initializer;
import net.vulkanmod.gl.VkGlFramebuffer;
import net.vulkanmod.mixin.window.WindowAccessor;
import net.vulkanmod.render.PipelineManager;
import net.vulkanmod.render.chunk.WorldRenderer;
import net.vulkanmod.render.chunk.buffer.UploadManager;
import net.vulkanmod.render.profiling.Profiler;
import net.vulkanmod.render.texture.ImageUploadHelper;
import net.vulkanmod.vulkan.device.DeviceManager;
import net.vulkanmod.vulkan.framebuffer.Framebuffer;
import net.vulkanmod.vulkan.framebuffer.RenderPass;
import net.vulkanmod.vulkan.framebuffer.SwapChain;
import net.vulkanmod.vulkan.memory.MemoryManager;
import net.vulkanmod.vulkan.pass.DefaultMainPass;
import net.vulkanmod.vulkan.pass.MainPass;
import net.vulkanmod.vulkan.pass.ShadowPass;
import net.vulkanmod.vulkan.queue.CommandPool;
import net.vulkanmod.vulkan.queue.TransferQueue;
import net.vulkanmod.vulkan.shader.GraphicsPipeline;
import net.vulkanmod.vulkan.shader.Pipeline;
import net.vulkanmod.vulkan.shader.PipelineState;
import net.vulkanmod.vulkan.shader.Uniforms;
import net.vulkanmod.vulkan.shader.layout.PushConstants;
import net.vulkanmod.vulkan.texture.VTextureSelector;
import net.vulkanmod.vulkan.util.VkResult;
import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;
import org.lwjgl.vulkan.KHRSwapchain;
import org.lwjgl.vulkan.VK10;
import org.lwjgl.vulkan.VkAllocationCallbacks;
import org.lwjgl.vulkan.VkClearAttachment;
import org.lwjgl.vulkan.VkClearRect;
import org.lwjgl.vulkan.VkClearValue;
import org.lwjgl.vulkan.VkCommandBuffer;
import org.lwjgl.vulkan.VkCommandBufferAllocateInfo;
import org.lwjgl.vulkan.VkCommandBufferBeginInfo;
import org.lwjgl.vulkan.VkDevice;
import org.lwjgl.vulkan.VkFenceCreateInfo;
import org.lwjgl.vulkan.VkPresentInfoKHR;
import org.lwjgl.vulkan.VkRect2D;
import org.lwjgl.vulkan.VkSemaphoreCreateInfo;
import org.lwjgl.vulkan.VkSubmitInfo;
import org.lwjgl.vulkan.VkViewport;

public class Renderer {
   private static Renderer INSTANCE;
   private static VkDevice device;
   private static boolean swapChainUpdate = false;
   public static boolean skipRendering = false;
   private final Set<Pipeline> usedPipelines = new ObjectOpenHashSet();
   private Pipeline boundPipeline;
   private long boundPipelineHandle;
   private Drawer drawer;
   private SwapChain swapChain;
   private int framesNum;
   private List<VkCommandBuffer> mainCommandBuffers;
   private ArrayList<Long> imageAvailableSemaphores;
   private ArrayList<Long> renderFinishedSemaphores;
   private ArrayList<Long> inFlightFences;
   private List<CommandPool.CommandBuffer> transferCbs;
   private Framebuffer boundFramebuffer;
   private RenderPass boundRenderPass;
   private static int currentFrame = 0;
   private static int imageIndex;
   private static int lastReset = -1;
   private VkCommandBuffer currentCmdBuffer;
   private boolean recordingCmds = false;
   int recursion = 0;
   MainPass mainPass;
   ShadowPass shadowPass;
   private final List<Runnable> onResizeCallbacks = new ObjectArrayList();

   public static void initRenderer() {
      INSTANCE = new Renderer();
      INSTANCE.init();
   }

   public static Renderer getInstance() {
      return INSTANCE;
   }

   public static Drawer getDrawer() {
      return INSTANCE.drawer;
   }

   public static int getCurrentFrame() {
      return currentFrame;
   }

   public static int getCurrentImage() {
      return imageIndex;
   }

   public Renderer() {
      device = Vulkan.getVkDevice();
      this.framesNum = Initializer.CONFIG.frameQueueSize;
   }

   public static void setLineWidth(float width) {
      if (INSTANCE.boundFramebuffer != null) {
         VK10.vkCmdSetLineWidth(INSTANCE.currentCmdBuffer, width);
      }
   }

   private void init() {
      MemoryManager.createInstance(getFramesNum());
      Vulkan.createStagingBuffers();
      this.swapChain = new SwapChain();
      this.mainPass = DefaultMainPass.create();
      this.shadowPass = ShadowPass.create();
      this.drawer = new Drawer();
      this.drawer.createResources(this.framesNum);
      Uniforms.setupDefaultUniforms();
      PipelineManager.init();
      UploadManager.createInstance();
      this.allocateCommandBuffers();
      this.createSyncObjects();
   }

   private void allocateCommandBuffers() {
      if (this.mainCommandBuffers != null) {
         this.mainCommandBuffers.forEach((commandBuffer) -> VK10.vkFreeCommandBuffers(device, Vulkan.getCommandPool(), commandBuffer));
      }

      this.mainCommandBuffers = new ArrayList(this.framesNum);
      MemoryStack stack = MemoryStack.stackPush();

      try {
         VkCommandBufferAllocateInfo allocInfo = VkCommandBufferAllocateInfo.calloc(stack);
         allocInfo.sType(40);
         allocInfo.commandPool(Vulkan.getCommandPool());
         allocInfo.level(0);
         allocInfo.commandBufferCount(this.framesNum);
         PointerBuffer pCommandBuffers = stack.mallocPointer(this.framesNum);
         int vkResult = VK10.vkAllocateCommandBuffers(device, allocInfo, pCommandBuffers);
         if (vkResult != 0) {
            throw new RuntimeException("Failed to allocate command buffers: %s".formatted(VkResult.decode(vkResult)));
         }

         for(int i = 0; i < this.framesNum; ++i) {
            this.mainCommandBuffers.add(new VkCommandBuffer(pCommandBuffers.get(i), device));
         }
      } catch (Throwable var7) {
         if (stack != null) {
            try {
               stack.close();
            } catch (Throwable var6) {
               var7.addSuppressed(var6);
            }
         }

         throw var7;
      }

      if (stack != null) {
         stack.close();
      }

      if (this.transferCbs != null) {
         this.transferCbs.forEach((commandBuffer) -> {
            VK10.vkResetCommandBuffer(commandBuffer.handle, 0);
            commandBuffer.reset();
         });
      }

      this.transferCbs = new ArrayList(this.framesNum);

      for(int i = 0; i < this.framesNum; ++i) {
         this.transferCbs.add(DeviceManager.getTransferQueue().getCommandPool().getCommandBuffer());
      }

   }

   private void createSyncObjects() {
      this.imageAvailableSemaphores = new ArrayList(this.framesNum);
      this.renderFinishedSemaphores = new ArrayList(this.framesNum);
      this.inFlightFences = new ArrayList(this.framesNum);
      MemoryStack stack = MemoryStack.stackPush();

      try {
         VkSemaphoreCreateInfo semaphoreInfo = VkSemaphoreCreateInfo.calloc(stack);
         semaphoreInfo.sType(9);
         VkFenceCreateInfo fenceInfo = VkFenceCreateInfo.calloc(stack);
         fenceInfo.sType(8);
         fenceInfo.flags(1);
         LongBuffer pImageAvailableSemaphore = stack.mallocLong(1);
         LongBuffer pRenderFinishedSemaphore = stack.mallocLong(1);
         LongBuffer pFence = stack.mallocLong(1);

         for(int i = 0; i < this.framesNum; ++i) {
            if (VK10.vkCreateSemaphore(device, semaphoreInfo, (VkAllocationCallbacks)null, pImageAvailableSemaphore) != 0 || VK10.vkCreateSemaphore(device, semaphoreInfo, (VkAllocationCallbacks)null, pRenderFinishedSemaphore) != 0 || VK10.vkCreateFence(device, fenceInfo, (VkAllocationCallbacks)null, pFence) != 0) {
               throw new RuntimeException("Failed to create synchronization objects for the frame: " + i);
            }

            this.imageAvailableSemaphores.add(pImageAvailableSemaphore.get(0));
            this.renderFinishedSemaphores.add(pRenderFinishedSemaphore.get(0));
            this.inFlightFences.add(pFence.get(0));
         }
      } catch (Throwable var9) {
         if (stack != null) {
            try {
               stack.close();
            } catch (Throwable var8) {
               var9.addSuppressed(var8);
            }
         }

         throw var9;
      }

      if (stack != null) {
         stack.close();
      }

   }

   public void preInitFrame() {
      Profiler p = Profiler.getMainProfiler();
      p.pop();
      p.round();
      p.push("Frame_ops");
      if (lastReset == currentFrame) {
         this.submitUploads();
         this.waitFences();
      }

      lastReset = currentFrame;
      this.drawer.resetBuffers(currentFrame);
      WorldRenderer.getInstance().uploadSections();
      UploadManager.INSTANCE.submitUploads();
   }

   public void beginFrame() {
      if (swapChainUpdate) {
         this.recreateSwapChain();
         swapChainUpdate = false;
         if (this.getSwapChain().getWidth() == 0 && this.getSwapChain().getHeight() == 0) {
            skipRendering = true;
            class_310.method_1551().field_1743 = true;
         } else {
            skipRendering = false;
            class_310.method_1551().field_1743 = false;
         }
      }

      if (!skipRendering) {
         ++this.recursion;
         if (this.recursion > 1) {
            this.endFrame();
         }

         this.preInitFrame();
         Profiler p = Profiler.getMainProfiler();
         p.pop();
         p.push("Frame_fence");
         VK10.vkWaitForFences(device, (Long)this.inFlightFences.get(currentFrame), true, -1L);
         p.pop();
         p.push("Begin_rendering");
         MemoryManager.getInstance().initFrame(currentFrame);
         this.drawer.setCurrentFrame(currentFrame);
         this.resetDescriptors();
         this.currentCmdBuffer = (VkCommandBuffer)this.mainCommandBuffers.get(currentFrame);
         VK10.vkResetCommandBuffer(this.currentCmdBuffer, 0);
         MemoryStack stack = MemoryStack.stackPush();

         label70: {
            try {
               IntBuffer pImageIndex = stack.mallocInt(1);
               int vkResult = KHRSwapchain.vkAcquireNextImageKHR(device, this.swapChain.getId(), -1L, (Long)this.imageAvailableSemaphores.get(currentFrame), 0L, pImageIndex);
               if (vkResult != 1000001003 && vkResult != -1000001004 && !swapChainUpdate) {
                  if (vkResult != 0) {
                     throw new RuntimeException("Cannot acquire next swap chain image: %s".formatted(VkResult.decode(vkResult)));
                  }

                  imageIndex = pImageIndex.get(0);
                  this.beginMainRenderPass(stack);
                  break label70;
               }

               swapChainUpdate = true;
               skipRendering = true;
               this.beginFrame();
            } catch (Throwable var6) {
               if (stack != null) {
                  try {
                     stack.close();
                  } catch (Throwable var5) {
                     var6.addSuppressed(var5);
                  }
               }

               throw var6;
            }

            if (stack != null) {
               stack.close();
            }

            return;
         }

         if (stack != null) {
            stack.close();
         }

         p.pop();
      }
   }

   private void beginMainRenderPass(MemoryStack stack) {
      VkCommandBufferBeginInfo beginInfo = VkCommandBufferBeginInfo.calloc(stack);
      beginInfo.sType(42);
      beginInfo.flags(1);
      VkCommandBuffer commandBuffer = this.currentCmdBuffer;
      int vkResult = VK10.vkBeginCommandBuffer(commandBuffer, beginInfo);
      if (vkResult != 0) {
         throw new RuntimeException("Failed to begin recording command buffer: %s".formatted(VkResult.decode(vkResult)));
      } else {
         this.recordingCmds = true;
         this.mainPass.begin(commandBuffer, stack);
         resetDynamicState(commandBuffer);
      }
   }

   public void endFrame() {
      if (!skipRendering && this.recordingCmds) {
         if (this.recursion != 0) {
            --this.recursion;
            Profiler p = Profiler.getMainProfiler();
            p.push("End_rendering");
            this.mainPass.end(this.currentCmdBuffer);
            this.submitUploads();
            this.waitFences();
            this.submitFrame();
            this.recordingCmds = false;
            p.pop();
            p.push("Post_rendering");
         }
      }
   }

   private void submitFrame() {
      if (!swapChainUpdate) {
         MemoryStack stack = MemoryStack.stackPush();

         label68: {
            try {
               VkSubmitInfo submitInfo = VkSubmitInfo.calloc(stack);
               submitInfo.sType(4);
               Synchronization.INSTANCE.addWaitSemaphore((Long)this.imageAvailableSemaphores.get(currentFrame));
               LongBuffer waitSemaphores = Synchronization.INSTANCE.getWaitSemaphores(stack);
               int waitSemaphoreCount = waitSemaphores.limit();
               IntBuffer waitDstStageMask = stack.mallocInt(waitSemaphoreCount);

               for(int i = 0; i < waitSemaphoreCount - 1; ++i) {
                  waitDstStageMask.put(i, 1);
               }

               waitDstStageMask.put(waitSemaphoreCount - 1, 1024);
               submitInfo.pWaitSemaphores(waitSemaphores);
               submitInfo.waitSemaphoreCount(waitSemaphores.limit());
               submitInfo.pWaitDstStageMask(waitDstStageMask);
               submitInfo.pSignalSemaphores(stack.longs((Long)this.renderFinishedSemaphores.get(currentFrame)));
               submitInfo.pCommandBuffers(stack.pointers(this.currentCmdBuffer));
               VK10.vkResetFences(device, (Long)this.inFlightFences.get(currentFrame));
               int vkResult;
               if ((vkResult = VK10.vkQueueSubmit(DeviceManager.getGraphicsQueue().vkQueue(), submitInfo, (Long)this.inFlightFences.get(currentFrame))) != 0) {
                  VK10.vkResetFences(device, (Long)this.inFlightFences.get(currentFrame));
                  throw new RuntimeException("Failed to submit draw command buffer: %s".formatted(VkResult.decode(vkResult)));
               }

               VkPresentInfoKHR presentInfo = VkPresentInfoKHR.calloc(stack);
               presentInfo.sType(1000001001);
               presentInfo.pWaitSemaphores(stack.longs((Long)this.renderFinishedSemaphores.get(currentFrame)));
               presentInfo.swapchainCount(1);
               presentInfo.pSwapchains(stack.longs(this.swapChain.getId()));
               presentInfo.pImageIndices(stack.ints(imageIndex));
               vkResult = KHRSwapchain.vkQueuePresentKHR(DeviceManager.getPresentQueue().vkQueue(), presentInfo);
               if (vkResult != -1000001004 && vkResult != 1000001003 && !swapChainUpdate) {
                  if (vkResult != 0) {
                     throw new RuntimeException("Failed to present rendered frame: %s".formatted(VkResult.decode(vkResult)));
                  }

                  Synchronization.INSTANCE.scheduleCbReset();
                  currentFrame = (currentFrame + 1) % this.framesNum;
                  break label68;
               }

               swapChainUpdate = true;
            } catch (Throwable var9) {
               if (stack != null) {
                  try {
                     stack.close();
                  } catch (Throwable var8) {
                     var9.addSuppressed(var8);
                  }
               }

               throw var9;
            }

            if (stack != null) {
               stack.close();
            }

            return;
         }

         if (stack != null) {
            stack.close();
         }

      }
   }

   public void flushCmds() {
      if (this.recordingCmds) {
         MemoryStack stack = MemoryStack.stackPush();

         try {
            this.endRenderPass(this.currentCmdBuffer);
            VK10.vkEndCommandBuffer(this.currentCmdBuffer);
            VkSubmitInfo submitInfo = VkSubmitInfo.calloc(stack);
            submitInfo.sType(4);
            submitInfo.pCommandBuffers(stack.pointers(this.currentCmdBuffer));
            VK10.vkResetFences(device, (Long)this.inFlightFences.get(currentFrame));
            this.submitUploads();
            this.waitFences();
            int vkResult;
            if ((vkResult = VK10.vkQueueSubmit(DeviceManager.getGraphicsQueue().vkQueue(), submitInfo, (Long)this.inFlightFences.get(currentFrame))) != 0) {
               VK10.vkResetFences(device, (Long)this.inFlightFences.get(currentFrame));
               throw new RuntimeException("Failed to submit draw command buffer: %s".formatted(VkResult.decode(vkResult)));
            }

            VK10.vkWaitForFences(device, (Long)this.inFlightFences.get(currentFrame), true, -1L);
            this.beginMainRenderPass(stack);
         } catch (Throwable var5) {
            if (stack != null) {
               try {
                  stack.close();
               } catch (Throwable var4) {
                  var5.addSuppressed(var4);
               }
            }

            throw var5;
         }

         if (stack != null) {
            stack.close();
         }

      }
   }

   public void submitUploads() {
      CommandPool.CommandBuffer transferCb = (CommandPool.CommandBuffer)this.transferCbs.get(currentFrame);
      if (transferCb.isRecording()) {
         TransferQueue transferQueue = DeviceManager.getTransferQueue();
         MemoryStack stack = MemoryStack.stackPush();

         try {
            transferCb.submitCommands(stack, transferQueue.vkQueue(), true);
         } catch (Throwable var7) {
            if (stack != null) {
               try {
                  stack.close();
               } catch (Throwable var6) {
                  var7.addSuppressed(var6);
               }
            }

            throw var7;
         }

         if (stack != null) {
            stack.close();
         }

         Synchronization.INSTANCE.addCommandBuffer(transferCb, true);
         this.transferCbs.set(currentFrame, transferQueue.getCommandPool().getCommandBuffer());
      }

      ImageUploadHelper.INSTANCE.submitCommands();
   }

   public void endRenderPass() {
      this.endRenderPass(this.currentCmdBuffer);
   }

   public void endRenderPass(VkCommandBuffer commandBuffer) {
      if (!skipRendering && this.recordingCmds && this.boundFramebuffer != null) {
         this.boundRenderPass.endRenderPass(commandBuffer);
         this.boundRenderPass = null;
         this.boundFramebuffer = null;
         VkGlFramebuffer.resetBoundFramebuffer();
      }
   }

   public boolean beginRenderPass(RenderPass renderPass, Framebuffer framebuffer) {
      if (skipRendering) {
         return false;
      } else {
         if (!this.recordingCmds) {
            this.beginFrame();
            this.recordingCmds = true;
         }

         if (this.boundFramebuffer != framebuffer) {
            this.endRenderPass(this.currentCmdBuffer);
            MemoryStack stack = MemoryStack.stackPush();

            try {
               framebuffer.beginRenderPass(this.currentCmdBuffer, renderPass, stack);
            } catch (Throwable var7) {
               if (stack != null) {
                  try {
                     stack.close();
                  } catch (Throwable var6) {
                     var7.addSuppressed(var6);
                  }
               }

               throw var7;
            }

            if (stack != null) {
               stack.close();
            }

            this.boundFramebuffer = framebuffer;
            this.boundRenderPass = renderPass;
            setViewportState(0, 0, framebuffer.getWidth(), framebuffer.getHeight());
            setScissor(0, 0, framebuffer.getWidth(), framebuffer.getHeight());
         }

         return true;
      }
   }

   public void addUsedPipeline(Pipeline pipeline) {
      this.usedPipelines.add(pipeline);
   }

   public void removeUsedPipeline(Pipeline pipeline) {
      this.usedPipelines.remove(pipeline);
   }

   private void waitFences() {
      Synchronization.INSTANCE.waitFences();
      Vulkan.getStagingBuffer().reset();
   }

   private void resetDescriptors() {
      for(Pipeline pipeline : this.usedPipelines) {
         pipeline.resetDescriptorPool(currentFrame);
      }

      this.usedPipelines.clear();
      this.boundPipeline = null;
      this.boundPipelineHandle = 0L;
   }

   void waitForSwapChain() {
      VK10.vkResetFences(device, (Long)this.inFlightFences.get(currentFrame));
      MemoryStack stack = MemoryStack.stackPush();

      try {
         VkSubmitInfo info = VkSubmitInfo.calloc(stack).sType$Default().pWaitSemaphores(stack.longs((Long)this.imageAvailableSemaphores.get(currentFrame))).pWaitDstStageMask(stack.ints(65536));
         VK10.vkQueueSubmit(DeviceManager.getGraphicsQueue().vkQueue(), info, (Long)this.inFlightFences.get(currentFrame));
         VK10.vkWaitForFences(device, (Long)this.inFlightFences.get(currentFrame), true, -1L);
      } catch (Throwable var5) {
         if (stack != null) {
            try {
               stack.close();
            } catch (Throwable var4) {
               var5.addSuppressed(var4);
            }
         }

         throw var5;
      }

      if (stack != null) {
         stack.close();
      }

   }

   private void recreateSwapChain() {
      this.submitUploads();
      this.waitFences();
      Vulkan.waitIdle();
      this.mainCommandBuffers.forEach((commandBuffer) -> VK10.vkResetCommandBuffer(commandBuffer, 0));
      this.recordingCmds = false;
      this.swapChain.recreate();
      this.destroySyncObjects();
      int newFramesNum = Initializer.CONFIG.frameQueueSize;
      if (this.framesNum != newFramesNum) {
         UploadManager.INSTANCE.submitUploads();
         this.framesNum = newFramesNum;
         MemoryManager.getInstance().freeAllBuffers();
         MemoryManager.createInstance(newFramesNum);
         Vulkan.createStagingBuffers();
         this.allocateCommandBuffers();
         Pipeline.recreateDescriptorSets(this.framesNum);
         this.drawer.createResources(this.framesNum);
      }

      this.createSyncObjects();
      this.mainPass.onResize();
      VRenderSystem.createSceneColorImage(this.swapChain.getWidth(), this.swapChain.getHeight(), this.swapChain.getFormat());
      this.onResizeCallbacks.forEach(Runnable::run);
      ((WindowAccessor)class_310.method_1551().method_22683()).getEventHandler().method_15993();
      currentFrame = 0;
   }

   public void cleanUpResources() {
      WorldRenderer.getInstance().cleanUp();
      this.destroySyncObjects();
      this.drawer.cleanUpResources();
      this.shadowPass.cleanUp();
      this.mainPass.cleanUp();
      this.swapChain.cleanUp();
      PipelineManager.destroyPipelines();
      VTextureSelector.getWhiteTexture().free();
      if (VRenderSystem.sceneColorImage != null) {
         VRenderSystem.sceneColorImage.free();
      }

   }

   private void destroySyncObjects() {
      for(int i = 0; i < this.framesNum; ++i) {
         VK10.vkDestroyFence(device, (Long)this.inFlightFences.get(i), (VkAllocationCallbacks)null);
         VK10.vkDestroySemaphore(device, (Long)this.imageAvailableSemaphores.get(i), (VkAllocationCallbacks)null);
         VK10.vkDestroySemaphore(device, (Long)this.renderFinishedSemaphores.get(i), (VkAllocationCallbacks)null);
      }

   }

   public void addOnResizeCallback(Runnable runnable) {
      this.onResizeCallbacks.add(runnable);
   }

   public void bindGraphicsPipeline(GraphicsPipeline pipeline) {
      VkCommandBuffer commandBuffer = this.currentCmdBuffer;
      PipelineState currentState = PipelineState.getCurrentPipelineState(this.boundRenderPass);
      long handle = pipeline.getHandle(currentState);
      if (this.boundPipelineHandle != handle) {
         VK10.vkCmdBindPipeline(commandBuffer, 0, handle);
         this.boundPipelineHandle = handle;
         this.boundPipeline = pipeline;
         this.addUsedPipeline(pipeline);
      }
   }

   public void uploadAndBindUBOs(Pipeline pipeline) {
      VkCommandBuffer commandBuffer = this.currentCmdBuffer;
      pipeline.bindDescriptorSets(commandBuffer, currentFrame);
   }

   public void pushConstants(Pipeline pipeline) {
      VkCommandBuffer commandBuffer = this.currentCmdBuffer;
      PushConstants pushConstants = pipeline.getPushConstants();
      MemoryStack stack = MemoryStack.stackPush();

      try {
         ByteBuffer buffer = stack.malloc(pushConstants.getSize());
         long ptr = MemoryUtil.memAddress0(buffer);
         pushConstants.update(ptr);
         VK10.nvkCmdPushConstants(commandBuffer, pipeline.getLayout(), 1, 0, pushConstants.getSize(), ptr);
      } catch (Throwable var9) {
         if (stack != null) {
            try {
               stack.close();
            } catch (Throwable var8) {
               var9.addSuppressed(var8);
            }
         }

         throw var9;
      }

      if (stack != null) {
         stack.close();
      }

   }

   public Pipeline getBoundPipeline() {
      return this.boundPipeline;
   }

   public void setBoundFramebuffer(Framebuffer framebuffer) {
      this.boundFramebuffer = framebuffer;
   }

   public Framebuffer getBoundFramebuffer() {
      return this.boundFramebuffer;
   }

   public void setBoundRenderPass(RenderPass boundRenderPass) {
      this.boundRenderPass = boundRenderPass;
   }

   public RenderPass getBoundRenderPass() {
      return this.boundRenderPass;
   }

   public void setMainPass(MainPass mainPass) {
      this.mainPass = mainPass;
   }

   public MainPass getMainPass() {
      return this.mainPass;
   }

   public ShadowPass getShadowPass() {
      return this.shadowPass;
   }

   public SwapChain getSwapChain() {
      return this.swapChain;
   }

   public CommandPool.CommandBuffer getTransferCb() {
      return (CommandPool.CommandBuffer)this.transferCbs.get(currentFrame);
   }

   private static void resetDynamicState(VkCommandBuffer commandBuffer) {
      VK10.vkCmdSetDepthBias(commandBuffer, 0.0F, 0.0F, 0.0F);
      VK10.vkCmdSetLineWidth(commandBuffer, 1.0F);
   }

   public static void setDepthBias(float constant, float slope) {
      VkCommandBuffer commandBuffer = INSTANCE.currentCmdBuffer;
      VK10.vkCmdSetDepthBias(commandBuffer, constant, 0.0F, slope);
   }

   public static void clearAttachments(int attachments) {
      clearAttachments(INSTANCE.currentCmdBuffer, attachments);
   }

   public static void clearAttachments(VkCommandBuffer commandBuffer, int attachments) {
      Framebuffer framebuffer = getInstance().boundFramebuffer;
      if (framebuffer != null) {
         clearAttachments(commandBuffer, attachments, framebuffer.getWidth(), framebuffer.getHeight());
      }
   }

   public static void clearAttachments(int attachments, int width, int height) {
      clearAttachments(INSTANCE.currentCmdBuffer, attachments, width, height);
   }

   public static void clearAttachments(int attachments, int x, int y, int width, int height) {
      clearAttachments(INSTANCE.currentCmdBuffer, attachments, x, y, width, height);
   }

   public static void clearAttachments(VkCommandBuffer commandBuffer, int attachments, int width, int height) {
      clearAttachments(commandBuffer, attachments, 0, 0, width, height);
   }

   public static void clearAttachments(VkCommandBuffer commandBuffer, int attachments, int x, int y, int width, int height) {
      if (!skipRendering) {
         MemoryStack stack = MemoryStack.stackPush();

         try {
            VkClearValue colorValue = VkClearValue.calloc(stack);
            colorValue.color().float32(VRenderSystem.clearColor);
            VkClearValue depthValue = VkClearValue.calloc(stack);
            depthValue.depthStencil().set(VRenderSystem.clearDepthValue, 0);
            int attachmentsCount = attachments == 16640 ? 2 : 1;
            VkClearAttachment.Buffer pAttachments = VkClearAttachment.malloc(attachmentsCount, stack);
            switch (attachments) {
               case 256:
                  VkClearAttachment clearDepth = (VkClearAttachment)pAttachments.get(0);
                  clearDepth.aspectMask(2);
                  clearDepth.colorAttachment(0);
                  clearDepth.clearValue(depthValue);
                  break;
               case 16384:
                  VkClearAttachment clearColor = (VkClearAttachment)pAttachments.get(0);
                  clearColor.aspectMask(1);
                  clearColor.colorAttachment(0);
                  clearColor.clearValue(colorValue);
                  break;
               case 16640:
                  VkClearAttachment clearColor = (VkClearAttachment)pAttachments.get(0);
                  clearColor.aspectMask(1);
                  clearColor.colorAttachment(0);
                  clearColor.clearValue(colorValue);
                  VkClearAttachment clearDepth = (VkClearAttachment)pAttachments.get(1);
                  clearDepth.aspectMask(2);
                  clearDepth.colorAttachment(0);
                  clearDepth.clearValue(depthValue);
                  break;
               default:
                  throw new RuntimeException("unexpected value");
            }

            VkRect2D renderArea = VkRect2D.malloc(stack);
            renderArea.offset().set(x, y);
            renderArea.extent().set(width, height);
            VkClearRect.Buffer pRect = VkClearRect.malloc(1, stack);
            pRect.rect(renderArea);
            pRect.baseArrayLayer(0);
            pRect.layerCount(1);
            VK10.vkCmdClearAttachments(commandBuffer, pAttachments, pRect);
         } catch (Throwable var14) {
            if (stack != null) {
               try {
                  stack.close();
               } catch (Throwable var13) {
                  var14.addSuppressed(var13);
               }
            }

            throw var14;
         }

         if (stack != null) {
            stack.close();
         }

      }
   }

   public static void setInvertedViewport(int x, int y, int width, int height) {
      setViewportState(x, y + height, width, -height);
   }

   public static void resetViewport() {
      int width = INSTANCE.getSwapChain().getWidth();
      int height = INSTANCE.getSwapChain().getHeight();
      setViewportState(0, 0, width, height);
   }

   public static void setViewportState(int x, int y, int width, int height) {
      GlStateManager._viewport(x, y, width, height);
   }

   public static void setViewport(int x, int y, int width, int height) {
      MemoryStack stack = MemoryStack.stackPush();

      try {
         setViewport(x, y, width, height, stack);
      } catch (Throwable var8) {
         if (stack != null) {
            try {
               stack.close();
            } catch (Throwable var7) {
               var8.addSuppressed(var7);
            }
         }

         throw var8;
      }

      if (stack != null) {
         stack.close();
      }

   }

   public static void setViewport(int x, int y, int width, int height, MemoryStack stack) {
      if (INSTANCE.recordingCmds) {
         VkViewport.Buffer viewport = VkViewport.malloc(1, stack);
         viewport.x((float)x);
         viewport.y((float)(height + y));
         viewport.width((float)width);
         viewport.height((float)(-height));
         viewport.minDepth(0.0F);
         viewport.maxDepth(1.0F);
         VK10.vkCmdSetViewport(INSTANCE.currentCmdBuffer, 0, viewport);
      }
   }

   public static void setScissor(int x, int y, int width, int height) {
      if (INSTANCE.recordingCmds && INSTANCE.boundFramebuffer != null) {
         MemoryStack stack = MemoryStack.stackPush();

         try {
            int framebufferHeight = INSTANCE.boundFramebuffer.getHeight();
            x = Math.max(0, x);
            VkRect2D.Buffer scissor = VkRect2D.malloc(1, stack);
            scissor.offset().set(x, framebufferHeight - (y + height));
            scissor.extent().set(width, height);
            VK10.vkCmdSetScissor(INSTANCE.currentCmdBuffer, 0, scissor);
         } catch (Throwable var8) {
            if (stack != null) {
               try {
                  stack.close();
               } catch (Throwable var7) {
                  var8.addSuppressed(var7);
               }
            }

            throw var8;
         }

         if (stack != null) {
            stack.close();
         }

      }
   }

   public static void resetScissor() {
      if (INSTANCE.boundFramebuffer != null) {
         MemoryStack stack = MemoryStack.stackPush();

         try {
            VkRect2D.Buffer scissor = INSTANCE.boundFramebuffer.scissor(stack);
            VK10.vkCmdSetScissor(INSTANCE.currentCmdBuffer, 0, scissor);
         } catch (Throwable var4) {
            if (stack != null) {
               try {
                  stack.close();
               } catch (Throwable var3) {
                  var4.addSuppressed(var3);
               }
            }

            throw var4;
         }

         if (stack != null) {
            stack.close();
         }

      }
   }

   public static void pushDebugSection(String s) {
   }

   public static void popDebugSection() {
   }

   public static void popPushDebugSection(String s) {
      popDebugSection();
      pushDebugSection(s);
   }

   public static int getFramesNum() {
      return INSTANCE.framesNum;
   }

   public static VkCommandBuffer getCommandBuffer() {
      return INSTANCE.currentCmdBuffer;
   }

   public static boolean isRecording() {
      return INSTANCE.recordingCmds;
   }

   public static void scheduleSwapChainUpdate() {
      swapChainUpdate = true;
   }
}
    
Download file