| 1 | package com.mojang.renderpearl.backend.vulkan;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.api.pipeline.BindGroupLayout;
|
| 4 | import com.mojang.renderpearl.api.pipeline.BlendFunction;
|
| 5 | import com.mojang.renderpearl.api.pipeline.ColorTargetState;
|
| 6 | import com.mojang.renderpearl.api.pipeline.DepthStencilState;
|
| 7 | import com.mojang.renderpearl.backend.api.BackendRenderPipeline;
|
| 8 | import com.mojang.renderpearl.backend.api.BackendRenderPipeline.CreateInfo.AttribBinding;
|
| 9 | import com.mojang.renderpearl.backend.api.BackendRenderPipeline.CreateInfo.Shader;
|
| 10 | import com.mojang.renderpearl.backend.api.BackendRenderPipeline.CreateInfo.VertexBuffer;
|
| 11 | import it.unimi.dsi.fastutil.longs.LongArrayList;
|
| 12 | import it.unimi.dsi.fastutil.longs.LongList;
|
| 13 | import java.nio.ByteBuffer;
|
| 14 | import java.nio.IntBuffer;
|
| 15 | import java.nio.LongBuffer;
|
| 16 | import java.util.List;
|
| 17 | import net.fabricmc.api.EnvType;
|
| 18 | import net.fabricmc.api.Environment;
|
| 19 | import org.lwjgl.system.MemoryStack;
|
| 20 | import org.lwjgl.vulkan.VK12;
|
| 21 | import org.lwjgl.vulkan.VkDescriptorSetLayoutBinding;
|
| 22 | import org.lwjgl.vulkan.VkDescriptorSetLayoutCreateInfo;
|
| 23 | import org.lwjgl.vulkan.VkGraphicsPipelineCreateInfo;
|
| 24 | import org.lwjgl.vulkan.VkPipelineColorBlendAttachmentState;
|
| 25 | import org.lwjgl.vulkan.VkPipelineColorBlendStateCreateInfo;
|
| 26 | import org.lwjgl.vulkan.VkPipelineDepthStencilStateCreateInfo;
|
| 27 | import org.lwjgl.vulkan.VkPipelineDynamicStateCreateInfo;
|
| 28 | import org.lwjgl.vulkan.VkPipelineInputAssemblyStateCreateInfo;
|
| 29 | import org.lwjgl.vulkan.VkPipelineLayoutCreateInfo;
|
| 30 | import org.lwjgl.vulkan.VkPipelineMultisampleStateCreateInfo;
|
| 31 | import org.lwjgl.vulkan.VkPipelineRasterizationStateCreateInfo;
|
| 32 | import org.lwjgl.vulkan.VkPipelineRenderingCreateInfoKHR;
|
| 33 | import org.lwjgl.vulkan.VkPipelineShaderStageCreateInfo;
|
| 34 | import org.lwjgl.vulkan.VkPipelineVertexInputDivisorStateCreateInfoEXT;
|
| 35 | import org.lwjgl.vulkan.VkPipelineVertexInputStateCreateInfo;
|
| 36 | import org.lwjgl.vulkan.VkPipelineViewportStateCreateInfo;
|
| 37 | import org.lwjgl.vulkan.VkPushConstantRange;
|
| 38 | import org.lwjgl.vulkan.VkShaderModuleCreateInfo;
|
| 39 | import org.lwjgl.vulkan.VkVertexInputAttributeDescription;
|
| 40 | import org.lwjgl.vulkan.VkVertexInputBindingDescription;
|
| 41 | import org.lwjgl.vulkan.VkVertexInputBindingDivisorDescriptionEXT;
|
| 42 | import org.lwjgl.vulkan.VkDescriptorSetLayoutBinding.Buffer;
|
| 43 |
|
| 44 | @Environment(EnvType.CLIENT)
|
| 45 | public final class VulkanRenderPipeline implements BackendRenderPipeline, Destroyable {
|
| 46 | private final VulkanDevice device;
|
| 47 | private final long withDepthPipeline;
|
| 48 | private final long withoutDepthPipeline;
|
| 49 | private final long pipelineLayout;
|
| 50 | private final long descriptorSetLayout;
|
| 51 | private final LongList shaderModules;
|
| 52 | private final List<BindGroupLayout.UniformDescription> uniforms;
|
| 53 | private boolean closed;
|
| 54 |
|
| 55 | public VulkanRenderPipeline(
|
| 56 | final VulkanDevice device,
|
| 57 | final long withDepthPipeline,
|
| 58 | final long withoutDepthPipeline,
|
| 59 | final long pipelineLayout,
|
| 60 | final long descriptorSetLayout,
|
| 61 | final LongList shaderModules,
|
| 62 | final List<BindGroupLayout.UniformDescription> uniforms
|
| 63 | ) {
|
| 64 | this.device = device;
|
| 65 | this.withDepthPipeline = withDepthPipeline;
|
| 66 | this.withoutDepthPipeline = withoutDepthPipeline;
|
| 67 | this.pipelineLayout = pipelineLayout;
|
| 68 | this.descriptorSetLayout = descriptorSetLayout;
|
| 69 | this.shaderModules = shaderModules;
|
| 70 | this.uniforms = uniforms;
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public boolean isClosed() {
|
| 75 | return this.closed;
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public void close() {
|
| 80 | if (!this.closed) {
|
| 81 | this.closed = true;
|
| 82 | this.device.createCommandEncoder().queueForDestroy(this);
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | public static VulkanRenderPipeline compile(final VulkanDevice device, final BackendRenderPipeline.CreateInfo pipelineCreateInfo) {
|
| 87 | long descriptorSetLayout;
|
| 88 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 89 | int descriptorCount = pipelineCreateInfo.uniforms().size();
|
| 90 | Buffer bindings = VkDescriptorSetLayoutBinding.calloc(descriptorCount, stack);
|
| 91 |
|
| 92 | for (int i = 0; i < descriptorCount; i++) {
|
| 93 | VkDescriptorSetLayoutBinding binding = VkDescriptorSetLayoutBinding.calloc(stack);
|
| 94 |
|
| 95 | binding.descriptorType(switch (pipelineCreateInfo.uniforms().get(i).type()) {
|
| 96 | case UNIFORM_BUFFER -> 6;
|
| 97 | case COMBINED_IMAGE_SAMPLER -> 1;
|
| 98 | case TEXEL_BUFFER -> 4;
|
| 99 | });
|
| 100 | binding.descriptorCount(1);
|
| 101 | binding.binding(i);
|
| 102 | binding.stageFlags(17);
|
| 103 | bindings.put(binding);
|
| 104 | }
|
| 105 |
|
| 106 | bindings.flip();
|
| 107 | VkDescriptorSetLayoutCreateInfo setCreateInfo = VkDescriptorSetLayoutCreateInfo.calloc(stack).sType$Default();
|
| 108 | setCreateInfo.flags(1);
|
| 109 | setCreateInfo.pBindings(bindings);
|
| 110 | LongBuffer pointer = stack.callocLong(1);
|
| 111 | VulkanUtils.crashIfFailure(
|
| 112 | device,
|
| 113 | VK12.vkCreateDescriptorSetLayout(device.vkDevice(), setCreateInfo, null, pointer),
|
| 114 | "Can't create descriptor set layout for " + pipelineCreateInfo.name()
|
| 115 | );
|
| 116 | descriptorSetLayout = pointer.get(0);
|
| 117 | }
|
| 118 |
|
| 119 | long pipelineLayout;
|
| 120 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 121 | VkPipelineLayoutCreateInfo createInfo = VkPipelineLayoutCreateInfo.calloc(stack).sType$Default();
|
| 122 | if (pipelineCreateInfo.pushConstantsSize() != 0) {
|
| 123 | org.lwjgl.vulkan.VkPushConstantRange.Buffer range = VkPushConstantRange.calloc(1, stack);
|
| 124 | range.stageFlags(Integer.MAX_VALUE);
|
| 125 | range.offset(0);
|
| 126 | range.size(pipelineCreateInfo.pushConstantsSize());
|
| 127 | createInfo.pPushConstantRanges(range);
|
| 128 | }
|
| 129 |
|
| 130 | createInfo.pSetLayouts(stack.longs(descriptorSetLayout));
|
| 131 | LongBuffer pointer = stack.callocLong(1);
|
| 132 | VulkanUtils.crashIfFailure(
|
| 133 | device, VK12.vkCreatePipelineLayout(device.vkDevice(), createInfo, null, pointer), "Can't create pipeline for " + pipelineCreateInfo.name()
|
| 134 | );
|
| 135 | pipelineLayout = pointer.get(0);
|
| 136 | device.instance().debug().setObjectName(device.vkDevice(), 17, pipelineLayout, () -> "Pipeline layout for " + pipelineCreateInfo.name());
|
| 137 | }
|
| 138 |
|
| 139 | LongList compiledShaderModules = new LongArrayList();
|
| 140 |
|
| 141 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 142 | org.lwjgl.vulkan.VkPipelineShaderStageCreateInfo.Buffer shaderStages = VkPipelineShaderStageCreateInfo.calloc(pipelineCreateInfo.shaders().size(), stack);
|
| 143 |
|
| 144 | for (Shader shader : pipelineCreateInfo.shaders()) {
|
| 145 | long module;
|
| 146 | try (MemoryStack vertexInputDivisorState = stack.push()) {
|
| 147 | VkShaderModuleCreateInfo info = VkShaderModuleCreateInfo.calloc(stack).sType$Default().pCode(shader.module().spv());
|
| 148 | LongBuffer pointer = stack.callocLong(1);
|
| 149 | VulkanUtils.crashIfFailure(
|
| 150 | device,
|
| 151 | VK12.vkCreateShaderModule(device.vkDevice(), info, null, pointer),
|
| 152 | "Can't compile " + shader.name() + " (" + shader.module().type() + ") for pipeline " + pipelineCreateInfo.name()
|
| 153 | );
|
| 154 | device.instance().debug().setObjectName(device.vkDevice(), 15, pointer.get(0), pipelineCreateInfo::name);
|
| 155 | module = pointer.get(0);
|
| 156 | compiledShaderModules.add(module);
|
| 157 | }
|
| 158 |
|
| 159 | ByteBuffer entryPoint = stack.UTF8(shader.entryPoint());
|
| 160 | VkPipelineShaderStageCreateInfo stage = VkPipelineShaderStageCreateInfo.calloc(stack)
|
| 161 | .sType$Default()
|
| 162 | .stage(VulkanConst.toVk(shader.module().type()))
|
| 163 | .module(module)
|
| 164 | .pName(entryPoint);
|
| 165 | shaderStages.put(stage);
|
| 166 | }
|
| 167 |
|
| 168 | shaderStages.flip();
|
| 169 | List<VertexBuffer> vertexBindings = pipelineCreateInfo.vertexBuffers();
|
| 170 | org.lwjgl.vulkan.VkVertexInputBindingDescription.Buffer vertexBindingDescriptions = VkVertexInputBindingDescription.calloc(vertexBindings.size(), stack);
|
| 171 | org.lwjgl.vulkan.VkVertexInputBindingDivisorDescriptionEXT.Buffer vertexBindingDivisorDescriptions = VkVertexInputBindingDivisorDescriptionEXT.calloc(
|
| 172 | vertexBindings.size(), stack
|
| 173 | );
|
| 174 |
|
| 175 | for (VertexBuffer vertexBinding : vertexBindings) {
|
| 176 | VkVertexInputBindingDescription bindingDescription = VkVertexInputBindingDescription.calloc(stack)
|
| 177 | .binding(vertexBinding.bufferSlot())
|
| 178 | .stride(vertexBinding.stride())
|
| 179 | .inputRate(vertexBinding.stepRate() > 0 ? 1 : 0);
|
| 180 | vertexBindingDescriptions.put(bindingDescription);
|
| 181 | if (vertexBinding.stepRate() > 0) {
|
| 182 | VkVertexInputBindingDivisorDescriptionEXT divisorBinding = VkVertexInputBindingDivisorDescriptionEXT.calloc(stack)
|
| 183 | .binding(vertexBinding.bufferSlot())
|
| 184 | .divisor(vertexBinding.stepRate());
|
| 185 | vertexBindingDivisorDescriptions.put(divisorBinding);
|
| 186 | }
|
| 187 | }
|
| 188 |
|
| 189 | vertexBindingDescriptions.flip();
|
| 190 | vertexBindingDivisorDescriptions.flip();
|
| 191 | org.lwjgl.vulkan.VkVertexInputAttributeDescription.Buffer vertexAttributeDescriptions = VkVertexInputAttributeDescription.calloc(
|
| 192 | pipelineCreateInfo.attribBindings().size(), stack
|
| 193 | );
|
| 194 |
|
| 195 | for (AttribBinding attribBinding : pipelineCreateInfo.attribBindings()) {
|
| 196 | VkVertexInputAttributeDescription attributeDescription = VkVertexInputAttributeDescription.calloc(stack)
|
| 197 | .location(attribBinding.location())
|
| 198 | .binding(attribBinding.bufferSlot())
|
| 199 | .offset(attribBinding.offset())
|
| 200 | .format(VulkanConst.toVk(attribBinding.format()));
|
| 201 | vertexAttributeDescriptions.put(attributeDescription);
|
| 202 | }
|
| 203 |
|
| 204 | vertexAttributeDescriptions.flip();
|
| 205 | VkPipelineVertexInputDivisorStateCreateInfoEXT vertexInputDivisorState = VkPipelineVertexInputDivisorStateCreateInfoEXT.calloc(stack)
|
| 206 | .sType$Default()
|
| 207 | .pVertexBindingDivisors(vertexBindingDivisorDescriptions);
|
| 208 | VkPipelineVertexInputStateCreateInfo vertexInputState = VkPipelineVertexInputStateCreateInfo.calloc(stack)
|
| 209 | .sType$Default()
|
| 210 | .pVertexAttributeDescriptions(vertexAttributeDescriptions)
|
| 211 | .pVertexBindingDescriptions(vertexBindingDescriptions);
|
| 212 | if (vertexInputDivisorState.vertexBindingDivisorCount() > 0) {
|
| 213 | vertexInputState.pNext(vertexInputDivisorState);
|
| 214 | }
|
| 215 |
|
| 216 | VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = VkPipelineInputAssemblyStateCreateInfo.calloc(stack)
|
| 217 | .sType$Default()
|
| 218 | .topology(VulkanConst.toVk(pipelineCreateInfo.primitiveTopology()));
|
| 219 | VkPipelineRasterizationStateCreateInfo rasterizationState = VkPipelineRasterizationStateCreateInfo.calloc(stack)
|
| 220 | .sType$Default()
|
| 221 | .polygonMode(VulkanConst.toVk(pipelineCreateInfo.polygonMode()))
|
| 222 | .cullMode(pipelineCreateInfo.cull() ? 2 : 0)
|
| 223 | .frontFace(1)
|
| 224 | .lineWidth(1.0F);
|
| 225 | VkPipelineDepthStencilStateCreateInfo vkDepthStencilState = VkPipelineDepthStencilStateCreateInfo.calloc(stack).sType$Default();
|
| 226 | DepthStencilState depthStencilState = pipelineCreateInfo.depthStencilState();
|
| 227 | if (depthStencilState != null) {
|
| 228 | rasterizationState.depthBiasEnable(depthStencilState.depthBiasConstant() != 0.0F || depthStencilState.depthBiasScaleFactor() != 0.0F);
|
| 229 | rasterizationState.depthBiasConstantFactor(depthStencilState.depthBiasConstant());
|
| 230 | rasterizationState.depthBiasSlopeFactor(depthStencilState.depthBiasScaleFactor());
|
| 231 | vkDepthStencilState.depthTestEnable(true);
|
| 232 | vkDepthStencilState.depthWriteEnable(depthStencilState.writeDepth());
|
| 233 | vkDepthStencilState.depthCompareOp(VulkanConst.toVk(depthStencilState.depthTest()));
|
| 234 | }
|
| 235 |
|
| 236 | List<ColorTargetState> colorTargetStates = pipelineCreateInfo.colorTargetStates();
|
| 237 | org.lwjgl.vulkan.VkPipelineColorBlendAttachmentState.Buffer blendAttachments = VkPipelineColorBlendAttachmentState.calloc(colorTargetStates.size(), stack);
|
| 238 |
|
| 239 | for (ColorTargetState colorTargetState : colorTargetStates) {
|
| 240 | blendAttachments.colorWriteMask(colorTargetState != null ? VulkanConst.toVk(colorTargetState) : 0);
|
| 241 | if (colorTargetState != null && colorTargetState.blendFunction().isPresent()) {
|
| 242 | applyBlendInformation(blendAttachments, colorTargetState.blendFunction().get());
|
| 243 | }
|
| 244 |
|
| 245 | blendAttachments.position(blendAttachments.position() + 1);
|
| 246 | }
|
| 247 |
|
| 248 | blendAttachments.position(0);
|
| 249 | VkPipelineColorBlendStateCreateInfo colorBlendState = VkPipelineColorBlendStateCreateInfo.calloc(stack).sType$Default().pAttachments(blendAttachments);
|
| 250 | VkPipelineViewportStateCreateInfo viewportState = VkPipelineViewportStateCreateInfo.calloc(stack).sType$Default().scissorCount(1).viewportCount(1);
|
| 251 | VkPipelineMultisampleStateCreateInfo multisampleState = VkPipelineMultisampleStateCreateInfo.calloc(stack)
|
| 252 | .sType$Default()
|
| 253 | .rasterizationSamples(1)
|
| 254 | .sampleShadingEnable(false);
|
| 255 | VkPipelineDynamicStateCreateInfo dynamicStateInfo = VkPipelineDynamicStateCreateInfo.calloc(stack).sType$Default().pDynamicStates(stack.ints(1, 0));
|
| 256 | VkPipelineRenderingCreateInfoKHR renderingInfo = VkPipelineRenderingCreateInfoKHR.calloc(stack).sType$Default();
|
| 257 | IntBuffer colorAttachmentFormats = stack.mallocInt(colorTargetStates.size());
|
| 258 |
|
| 259 | for (int i = 0; i < colorTargetStates.size(); i++) {
|
| 260 | ColorTargetState colorTargetState = colorTargetStates.get(i);
|
| 261 | colorAttachmentFormats.put(i, colorTargetState != null ? VulkanConst.toVk(colorTargetState.format()) : 0);
|
| 262 | }
|
| 263 |
|
| 264 | renderingInfo.pColorAttachmentFormats(colorAttachmentFormats);
|
| 265 | renderingInfo.depthAttachmentFormat(126);
|
| 266 | org.lwjgl.vulkan.VkGraphicsPipelineCreateInfo.Buffer createInfo = VkGraphicsPipelineCreateInfo.calloc(1, stack)
|
| 267 | .sType$Default()
|
| 268 | .flags(0)
|
| 269 | .pStages(shaderStages)
|
| 270 | .pVertexInputState(vertexInputState)
|
| 271 | .pInputAssemblyState(inputAssemblyState)
|
| 272 | .pRasterizationState(rasterizationState)
|
| 273 | .pDepthStencilState(vkDepthStencilState)
|
| 274 | .pColorBlendState(colorBlendState)
|
| 275 | .pViewportState(viewportState)
|
| 276 | .pMultisampleState(multisampleState)
|
| 277 | .pDynamicState(dynamicStateInfo)
|
| 278 | .layout(pipelineLayout)
|
| 279 | .pNext(renderingInfo);
|
| 280 | LongBuffer pointer = stack.callocLong(1);
|
| 281 | VulkanUtils.crashIfFailure(
|
| 282 | device, VK12.vkCreateGraphicsPipelines(device.vkDevice(), 0L, createInfo, null, pointer), "Can't compile pipeline " + pipelineCreateInfo.name()
|
| 283 | );
|
| 284 | long withDepthPipeline = pointer.get(0);
|
| 285 | device.instance().debug().setObjectName(device.vkDevice(), 19, withDepthPipeline, () -> "Pipeline " + pipelineCreateInfo.name());
|
| 286 | long withoutDepthPipeline;
|
| 287 | if (depthStencilState == null) {
|
| 288 | renderingInfo.depthAttachmentFormat(0);
|
| 289 | VulkanUtils.crashIfFailure(
|
| 290 | device, VK12.vkCreateGraphicsPipelines(device.vkDevice(), 0L, createInfo, null, pointer), "Can't compile pipeline " + pipelineCreateInfo.name()
|
| 291 | );
|
| 292 | withoutDepthPipeline = pointer.get(0);
|
| 293 | device.instance().debug().setObjectName(device.vkDevice(), 19, withoutDepthPipeline, () -> "Pipeline " + pipelineCreateInfo.name());
|
| 294 | } else {
|
| 295 | withoutDepthPipeline = 0L;
|
| 296 | }
|
| 297 |
|
| 298 | return new VulkanRenderPipeline(
|
| 299 | device, withDepthPipeline, withoutDepthPipeline, pipelineLayout, descriptorSetLayout, compiledShaderModules, pipelineCreateInfo.uniforms()
|
| 300 | );
|
| 301 | }
|
| 302 | }
|
| 303 |
|
| 304 | @Override
|
| 305 | public void destroy() {
|
| 306 | if (this.withDepthPipeline != 0L) {
|
| 307 | VK12.vkDestroyPipeline(this.device.vkDevice(), this.withoutDepthPipeline, null);
|
| 308 | VK12.vkDestroyPipeline(this.device.vkDevice(), this.withDepthPipeline, null);
|
| 309 | VK12.vkDestroyPipelineLayout(this.device.vkDevice(), this.pipelineLayout, null);
|
| 310 | VK12.vkDestroyDescriptorSetLayout(this.device.vkDevice(), this.descriptorSetLayout, null);
|
| 311 |
|
| 312 | for (int i = 0; i < this.shaderModules.size(); i++) {
|
| 313 | VK12.vkDestroyShaderModule(this.device.vkDevice(), this.shaderModules.getLong(i), null);
|
| 314 | }
|
| 315 | }
|
| 316 | }
|
| 317 |
|
| 318 | private static void applyBlendInformation(
|
| 319 | final org.lwjgl.vulkan.VkPipelineColorBlendAttachmentState.Buffer blendAttachments, final BlendFunction blendFunction
|
| 320 | ) {
|
| 321 | blendAttachments.blendEnable(true)
|
| 322 | .colorBlendOp(VulkanConst.toVk(blendFunction.color().op()))
|
| 323 | .alphaBlendOp(VulkanConst.toVk(blendFunction.alpha().op()))
|
| 324 | .dstAlphaBlendFactor(VulkanConst.toVk(blendFunction.alpha().destFactor()))
|
| 325 | .dstColorBlendFactor(VulkanConst.toVk(blendFunction.color().destFactor()))
|
| 326 | .srcAlphaBlendFactor(VulkanConst.toVk(blendFunction.alpha().sourceFactor()))
|
| 327 | .srcColorBlendFactor(VulkanConst.toVk(blendFunction.color().sourceFactor()));
|
| 328 | }
|
| 329 |
|
| 330 | public VulkanDevice device() {
|
| 331 | return this.device;
|
| 332 | }
|
| 333 |
|
| 334 | public long withDepthPipeline() {
|
| 335 | return this.withDepthPipeline;
|
| 336 | }
|
| 337 |
|
| 338 | public long withoutDepthPipeline() {
|
| 339 | return this.withoutDepthPipeline;
|
| 340 | }
|
| 341 |
|
| 342 | public long pipelineLayout() {
|
| 343 | return this.pipelineLayout;
|
| 344 | }
|
| 345 |
|
| 346 | public List<BindGroupLayout.UniformDescription> uniforms() {
|
| 347 | return this.uniforms;
|
| 348 | }
|
| 349 | }
|
| 350 |
|