working mesh shader

This commit is contained in:
Jorijn van der Graaf 2025-04-26 20:49:56 +02:00
commit 27ba32cdf5
11 changed files with 175 additions and 30 deletions

View file

@ -13,6 +13,7 @@ module;
#include <glm/gtc/type_ptr.hpp>
#include "VulkanBuffer.h"
#include "camera.hpp"
#include <unordered_map>
export module Crafter.Graphics:VulkanPipeline;
import :VulkanDevice;
@ -20,8 +21,88 @@ import :VulkanShader;
import :WindowWaylandVulkan;
namespace Crafter {
struct DescriptorEntry {
VkDescriptorType type;
std::uint32_t occurrences = 0;
};
export template <typename MeshShader, typename FragmentShader>
class VulkanPipeline {
private:
constexpr static std::uint32_t totalDescriptorCount = MeshShader::descriptorCount+FragmentShader::descriptorCount;
consteval static std::uint32_t GetUniqueDiscriptorCount() {
DescriptorEntry types[] = {{VK_DESCRIPTOR_TYPE_SAMPLER, 0},{VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0},{VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 0},{VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 0},{VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 0},{VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 0},{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0},{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 0},{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 0},{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 0},{VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 0},{VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK, 0},{VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, 0},{VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV, 0},{VK_DESCRIPTOR_TYPE_SAMPLE_WEIGHT_IMAGE_QCOM, 0},{VK_DESCRIPTOR_TYPE_BLOCK_MATCH_IMAGE_QCOM, 0},{VK_DESCRIPTOR_TYPE_MUTABLE_EXT, 0},{VK_DESCRIPTOR_TYPE_PARTITIONED_ACCELERATION_STRUCTURE_NV, 0},{VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT, 0},{VK_DESCRIPTOR_TYPE_MUTABLE_VALVE, 0}};
for(const DescriptorBinding& binding : MeshShader::descriptors) {
for(DescriptorEntry& type : types) {
if(type.type == binding.type) {
type.occurrences++;
}
}
}
for(const DescriptorBinding& binding : FragmentShader::descriptors) {
for(DescriptorEntry& type : types) {
if(type.type == binding.type) {
type.occurrences++;
}
}
}
std::uint32_t size = 0;
for(DescriptorEntry& type : types) {
size+=type.occurrences;
}
return size;
}
constexpr static std::uint32_t uniqueDescriptorCount = GetUniqueDiscriptorCount();
consteval static std::array<VkDescriptorPoolSize, uniqueDescriptorCount> GetPoolSizes() {
std::array<VkDescriptorPoolSize, uniqueDescriptorCount> types = {};
std::uint32_t i = 0;
for(const DescriptorBinding& binding : MeshShader::descriptors) {
for(VkDescriptorPoolSize& type : types) {
if(type.type == binding.type) {
type.descriptorCount++;
goto next;
}
}
types[i].type = binding.type;
types[i].descriptorCount = 1;
next:;
}
for(const DescriptorBinding& binding : FragmentShader::descriptors) {
for(VkDescriptorPoolSize& type : types) {
if(type.type == binding.type) {
type.descriptorCount++;
goto next2;
}
}
types[i].type = binding.type;
types[i].descriptorCount = 1;
next2:;
}
return types;
}
consteval static std::array<VkDescriptorSetLayoutBinding, totalDescriptorCount> GetDescriptorSet() {
std::array<VkDescriptorSetLayoutBinding, totalDescriptorCount> set;
std::uint32_t i = 0;
for(const DescriptorBinding& binding : MeshShader::descriptors) {
set[i] = {binding.slot, binding.type, 1, VK_SHADER_STAGE_MESH_BIT_EXT, nullptr};
i++;
}
for(const DescriptorBinding& binding : FragmentShader::descriptors) {
set[i] = {binding.slot, binding.type, 1, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr};
i++;
}
return set;
}
public:
inline static VkPipeline pipeline;
inline static VkPipelineLayout pipelineLayout;
@ -51,34 +132,31 @@ namespace Crafter {
memcpy(uniformBuffer.mapped, &uniformData, sizeof(UniformData));
// Pool
std::vector<VkDescriptorPoolSize> poolSizes = {
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1),
};
VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(static_cast<uint32_t>(poolSizes.size()), poolSizes.data(), 1);
std::array<VkDescriptorPoolSize, uniqueDescriptorCount> poolSizes = GetPoolSizes();
VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(uniqueDescriptorCount, poolSizes.data(), 1);
VulkanDevice::CHECK_VK_RESULT(vkCreateDescriptorPool(VulkanDevice::device, &descriptorPoolInfo, nullptr, &descriptorPool));
// Layout
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_MESH_BIT_EXT, 0),
};
VkDescriptorSetLayoutCreateInfo descriptorLayoutInfo = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);
constexpr std::array<VkDescriptorSetLayoutBinding, totalDescriptorCount> setLayoutBindings = GetDescriptorSet();
VkDescriptorSetLayoutCreateInfo descriptorLayoutInfo = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings.data(), totalDescriptorCount);
VulkanDevice::CHECK_VK_RESULT(vkCreateDescriptorSetLayout(VulkanDevice::device, &descriptorLayoutInfo, nullptr, &descriptorSetLayout));
// Set
VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1);
VulkanDevice::CHECK_VK_RESULT(vkAllocateDescriptorSets(VulkanDevice::device, &allocInfo, &descriptorSet));
std::vector<VkWriteDescriptorSet> modelWriteDescriptorSets = {
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffer.descriptor),
};
vkUpdateDescriptorSets(VulkanDevice::device, static_cast<uint32_t>(modelWriteDescriptorSets.size()), modelWriteDescriptorSets.data(), 0, nullptr);
// Layout
VkPipelineLayoutCreateInfo pipelineLayoutInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayout, 1);
VulkanDevice::CHECK_VK_RESULT(vkCreatePipelineLayout(VulkanDevice::device, &pipelineLayoutInfo, nullptr, &pipelineLayout));
// Pipeline
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE);
VkPipelineRasterizationStateCreateInfo rasterizationState = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_NONE, VK_FRONT_FACE_CLOCKWISE, 0);
VkPipelineRasterizationStateCreateInfo rasterizationState = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0);
VkPipelineColorBlendAttachmentState blendAttachmentState = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE);
VkPipelineColorBlendStateCreateInfo colorBlendState = vks::initializers::pipelineColorBlendStateCreateInfo(1, &blendAttachmentState);
VkPipelineDepthStencilStateCreateInfo depthStencilState = vks::initializers::pipelineDepthStencilStateCreateInfo(VK_TRUE, VK_TRUE, VK_COMPARE_OP_LESS_OR_EQUAL);