Corrected descriptor flags for Bindless.

Plus Reformat.
This commit is contained in:
Anish Bhobe 2024-07-23 12:13:40 +02:00
parent 870b18b1fa
commit 44121f1930
11 changed files with 181 additions and 155 deletions

View File

@ -57,26 +57,22 @@ Recast(TFrom &&in)
return reinterpret_cast<TType>(std::forward<TFrom>(in)); return reinterpret_cast<TType>(std::forward<TFrom>(in));
} }
constexpr f32 constexpr f32 operator""_deg(long double degrees)
operator""_deg(long double degrees)
{ {
return glm::radians<f32>(Cast<f32>(degrees)); return glm::radians<f32>(Cast<f32>(degrees));
} }
constexpr f32 constexpr f32 operator""_deg(unsigned long long int degrees)
operator""_deg(unsigned long long int degrees)
{ {
return glm::radians<f32>(Cast<f32>(degrees)); return glm::radians<f32>(Cast<f32>(degrees));
} }
constexpr f32 constexpr f32 operator""_rad(long double rads)
operator""_rad(long double rads)
{ {
return Cast<f32>(rads); return Cast<f32>(rads);
} }
constexpr f32 constexpr f32 operator""_rad(unsigned long long int rads)
operator""_rad(unsigned long long int rads)
{ {
return Cast<f32>(rads); return Cast<f32>(rads);
} }
@ -84,10 +80,10 @@ operator""_rad(unsigned long long int rads)
using glm::ivec2; using glm::ivec2;
using glm::ivec3; using glm::ivec3;
using glm::ivec4; using glm::ivec4;
using glm::quat;
using glm::vec2; using glm::vec2;
using glm::vec3; using glm::vec3;
using glm::vec4; using glm::vec4;
using glm::quat;
using glm::mat2; using glm::mat2;
using glm::mat3; using glm::mat3;

View File

@ -338,7 +338,7 @@ GpuResourceManager::GpuResourceManager(const Device *device, u16 maxSize)
.maxAnisotropy = properties.limits.maxSamplerAnisotropy, .maxAnisotropy = properties.limits.maxSamplerAnisotropy,
.compareEnable = false, .compareEnable = false,
.minLod = 0, .minLod = 0,
.maxLod = vk::LodClampNone, .maxLod = VK_LOD_CLAMP_NONE,
.borderColor = vk::BorderColor::eFloatOpaqueBlack, .borderColor = vk::BorderColor::eFloatOpaqueBlack,
.unnormalizedCoordinates = false, .unnormalizedCoordinates = false,
}; };
@ -368,6 +368,18 @@ GpuResourceManager::GpuResourceManager(const Device *device, u16 maxSize)
}; };
AbortIfFailed(device->m_Device.createDescriptorPool(&poolCreateInfo, nullptr, &m_DescriptorPool)); AbortIfFailed(device->m_Device.createDescriptorPool(&poolCreateInfo, nullptr, &m_DescriptorPool));
vk::DescriptorBindingFlags bindingFlags =
vk::DescriptorBindingFlagBits::ePartiallyBound | vk::DescriptorBindingFlagBits::eUpdateAfterBind;
eastl::array layoutBindingFlags = {
bindingFlags,
bindingFlags,
};
vk::DescriptorSetLayoutBindingFlagsCreateInfo bindingFlagsCreateInfo = {
.bindingCount = Cast<u32>(layoutBindingFlags.size()),
.pBindingFlags = layoutBindingFlags.data(),
};
eastl::vector immutableSamplers(texturesCount, m_ImmutableSampler); eastl::vector immutableSamplers(texturesCount, m_ImmutableSampler);
eastl::array descriptorLayoutBindings = { eastl::array descriptorLayoutBindings = {
vk::DescriptorSetLayoutBinding{ vk::DescriptorSetLayoutBinding{
@ -384,7 +396,9 @@ GpuResourceManager::GpuResourceManager(const Device *device, u16 maxSize)
.pImmutableSamplers = immutableSamplers.data(), .pImmutableSamplers = immutableSamplers.data(),
}, },
}; };
static_assert(layoutBindingFlags.size() == descriptorLayoutBindings.size());
const vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo = { const vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo = {
.pNext = &bindingFlagsCreateInfo,
.flags = vk::DescriptorSetLayoutCreateFlagBits::eUpdateAfterBindPool, .flags = vk::DescriptorSetLayoutCreateFlagBits::eUpdateAfterBindPool,
.bindingCount = Cast<u32>(descriptorLayoutBindings.size()), .bindingCount = Cast<u32>(descriptorLayoutBindings.size()),
.pBindings = descriptorLayoutBindings.data(), .pBindings = descriptorLayoutBindings.data(),
@ -420,18 +434,18 @@ GpuResourceManager::~GpuResourceManager()
} }
GpuResourceManager::GpuResourceManager(GpuResourceManager &&other) noexcept GpuResourceManager::GpuResourceManager(GpuResourceManager &&other) noexcept
: m_WriteInfos(std::move(other.m_WriteInfos)), : m_WriteInfos(std::move(other.m_WriteInfos))
m_Writes(std::move(other.m_Writes)), , m_Writes(std::move(other.m_Writes))
m_WriteOwner(std::move(other.m_WriteOwner)), , m_WriteOwner(std::move(other.m_WriteOwner))
m_ImmutableSampler(other.m_ImmutableSampler), , m_ImmutableSampler(other.m_ImmutableSampler)
m_BufferManager(std::move(other.m_BufferManager)), , m_BufferManager(std::move(other.m_BufferManager))
m_TextureManager(std::move(other.m_TextureManager)), , m_TextureManager(std::move(other.m_TextureManager))
m_Device(Take(other.m_Device)), , m_Device(Take(other.m_Device))
m_DescriptorPool(other.m_DescriptorPool), , m_DescriptorPool(other.m_DescriptorPool)
m_SetLayout(other.m_SetLayout), , m_SetLayout(other.m_SetLayout)
m_DescriptorSet(other.m_DescriptorSet), , m_DescriptorSet(other.m_DescriptorSet)
m_CommitedBufferCount(other.m_CommitedBufferCount), , m_CommitedBufferCount(other.m_CommitedBufferCount)
m_CommitedTextureCount(other.m_CommitedTextureCount) , m_CommitedTextureCount(other.m_CommitedTextureCount)
{ {
assert(!other.m_Device); assert(!other.m_Device);
} }

View File

@ -40,8 +40,7 @@ struct TextureManager
u32 m_MaxCapacity; u32 m_MaxCapacity;
u32 m_FreeHead; u32 m_FreeHead;
void void Init(u32 maxCapacity);
Init(u32 maxCapacity);
TextureHandle Commit(Texture *texture); TextureHandle Commit(Texture *texture);
Texture *Fetch(TextureHandle handle); Texture *Fetch(TextureHandle handle);
void Release(const Device *device, TextureHandle handle); void Release(const Device *device, TextureHandle handle);
@ -64,8 +63,7 @@ struct BufferManager
struct GpuResourceManager struct GpuResourceManager
{ {
private: private:
union WriteInfo union WriteInfo {
{
vk::DescriptorBufferInfo uBufferInfo; vk::DescriptorBufferInfo uBufferInfo;
vk::DescriptorImageInfo uImageInfo; vk::DescriptorImageInfo uImageInfo;
vk::BufferView uBufferView; vk::BufferView uBufferView;

View File

@ -244,7 +244,6 @@ LightManager::RemoveLight(const LightHandle handle)
{ {
const u8 handleGen = handle.m_Generation; const u8 handleGen = handle.m_Generation;
if (handle.m_Type == Light::TYPE_DIRECTIONAL) if (handle.m_Type == Light::TYPE_DIRECTIONAL)
{ {
Light *lightSlot = &m_Lights[handle.m_Index]; Light *lightSlot = &m_Lights[handle.m_Index];

View File

@ -14,12 +14,12 @@
#include "buffer.h" #include "buffer.h"
#include "device.h" #include "device.h"
#include "gpu_resource_manager.h"
#include "helpers.h" #include "helpers.h"
#include "image.h" #include "image.h"
#include "gpu_resource_manager.h"
#include <glm/gtc/type_ptr.hpp>
#include <EASTL/array.h> #include <EASTL/array.h>
#include <glm/gtc/type_ptr.hpp>
vec4 vec4
VectorToVec4(const std::vector<double> &vec) VectorToVec4(const std::vector<double> &vec)
@ -46,8 +46,7 @@ VectorToVec3(const std::vector<double> &vec)
} }
TextureHandle TextureHandle
ModelLoader::LoadImage(vk::CommandBuffer commandBuffer, StagingBuffer *stagingBuffer, ModelLoader::LoadImage(vk::CommandBuffer commandBuffer, StagingBuffer *stagingBuffer, tinygltf::Image *image) const
tinygltf::Image *image) const
{ {
assert(image->component == 4); assert(image->component == 4);
@ -64,8 +63,8 @@ ModelLoader::LoadImage(vk::CommandBuffer commandBuffer, StagingBuffer *stagingBu
.dstAccessMask = vk::AccessFlagBits::eTransferWrite, .dstAccessMask = vk::AccessFlagBits::eTransferWrite,
.oldLayout = vk::ImageLayout::eUndefined, .oldLayout = vk::ImageLayout::eUndefined,
.newLayout = vk::ImageLayout::eTransferDstOptimal, .newLayout = vk::ImageLayout::eTransferDstOptimal,
.srcQueueFamilyIndex = vk::QueueFamilyIgnored, .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = vk::QueueFamilyIgnored, .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = texture.m_Image, .image = texture.m_Image,
.subresourceRange = .subresourceRange =
{ {
@ -82,8 +81,8 @@ ModelLoader::LoadImage(vk::CommandBuffer commandBuffer, StagingBuffer *stagingBu
.dstAccessMask = vk::AccessFlagBits::eTransferRead, .dstAccessMask = vk::AccessFlagBits::eTransferRead,
.oldLayout = vk::ImageLayout::eTransferDstOptimal, .oldLayout = vk::ImageLayout::eTransferDstOptimal,
.newLayout = vk::ImageLayout::eTransferSrcOptimal, .newLayout = vk::ImageLayout::eTransferSrcOptimal,
.srcQueueFamilyIndex = vk::QueueFamilyIgnored, .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = vk::QueueFamilyIgnored, .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = texture.m_Image, .image = texture.m_Image,
.subresourceRange = .subresourceRange =
{ {
@ -135,9 +134,7 @@ ModelLoader::LoadImage(vk::CommandBuffer commandBuffer, StagingBuffer *stagingBu
commandBuffer.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eTransfer, {}, 0, commandBuffer.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eTransfer, {}, 0,
nullptr, 0, nullptr, 1, &nextMipBarrier); nullptr, 0, nullptr, 1, &nextMipBarrier);
auto calcNextMip = [](i32 prev) { auto calcNextMip = [](i32 prev) { return eastl::max(prev / 2, 1); };
return eastl::max(prev / 2, 1);
};
i32 prevMipWidth = Cast<i32>(texture.m_Extent.width); i32 prevMipWidth = Cast<i32>(texture.m_Extent.width);
i32 prevMipHeight = Cast<i32>(texture.m_Extent.height); i32 prevMipHeight = Cast<i32>(texture.m_Extent.height);
@ -157,7 +154,8 @@ ModelLoader::LoadImage(vk::CommandBuffer commandBuffer, StagingBuffer *stagingBu
.baseArrayLayer = 0, .baseArrayLayer = 0,
.layerCount = 1, .layerCount = 1,
}, },
.srcOffsets = std::array{ .srcOffsets =
std::array{
vk::Offset3D{0, 0, 0}, vk::Offset3D{0, 0, 0},
vk::Offset3D{prevMipWidth, prevMipHeight, 1}, vk::Offset3D{prevMipWidth, prevMipHeight, 1},
}, },
@ -168,7 +166,8 @@ ModelLoader::LoadImage(vk::CommandBuffer commandBuffer, StagingBuffer *stagingBu
.baseArrayLayer = 0, .baseArrayLayer = 0,
.layerCount = 1, .layerCount = 1,
}, },
.dstOffsets = std::array{ .dstOffsets =
std::array{
vk::Offset3D{0, 0, 0}, vk::Offset3D{0, 0, 0},
vk::Offset3D{currentMipWidth, currentMipHeight, 1}, vk::Offset3D{currentMipWidth, currentMipHeight, 1},
}, },
@ -230,6 +229,7 @@ ModelLoader::LoadModel(cstr path, cstr name, bool batched)
eastl::vector<StagingBuffer> stagingBuffers; eastl::vector<StagingBuffer> stagingBuffers;
eastl::vector<TextureHandle> textureHandles; eastl::vector<TextureHandle> textureHandles;
// TODO: MetalRough and Occlusion Textures are non-sRGB
if (!model.images.empty()) if (!model.images.empty())
{ {
u32 numImages = Cast<u32>(model.images.size()); u32 numImages = Cast<u32>(model.images.size());
@ -513,7 +513,8 @@ ModelLoader::LoadModel(cstr path, cstr name, bool batched)
{ {
if (model.defaultScene >= 0) if (model.defaultScene >= 0)
{ {
eastl::function<void(i32,i32)> processNode = [&processNode, &model, &nodes, &meshPrimRanges, &meshPrimitives](i32 idx, i32 parent) -> void { eastl::function<void(i32, i32)> processNode = [&processNode, &model, &nodes, &meshPrimRanges,
&meshPrimitives](i32 idx, i32 parent) -> void {
const auto *node = &model.nodes[idx]; const auto *node = &model.nodes[idx];
vec3 nodeTranslation = vec3{0.0f}; vec3 nodeTranslation = vec3{0.0f};
@ -537,8 +538,8 @@ ModelLoader::LoadModel(cstr path, cstr name, bool batched)
{ {
nodeMatrix = glm::make_mat4(node->matrix.data()); nodeMatrix = glm::make_mat4(node->matrix.data());
} }
const mat4 transform = const mat4 transform = translate(mat4(1.0f), nodeTranslation) * mat4_cast(nodeRotation) *
translate(mat4(1.0f), nodeTranslation) * mat4_cast(nodeRotation) * scale(mat4(1.0f), nodeScale) * nodeMatrix; scale(mat4(1.0f), nodeScale) * nodeMatrix;
const i32 nodeArrayIndex = Cast<i32>(nodes.Add(transform, parent)); const i32 nodeArrayIndex = Cast<i32>(nodes.Add(transform, parent));
if (node->mesh >= 0) if (node->mesh >= 0)
@ -633,12 +634,14 @@ ModelLoader::LoadModel(cstr path, cstr name, bool batched)
.m_NodeHandle = nodeHandle, .m_NodeHandle = nodeHandle,
}; };
return Model{m_ResourceManager, std::move(textureHandles), std::move(nodes), return Model{
handles, indexBuffer, meshPrimitives, }; m_ResourceManager, std::move(textureHandles), std::move(nodes), handles, indexBuffer, meshPrimitives,
};
} }
Model::Model(GpuResourceManager *resourceManager, eastl::vector<TextureHandle> &&textureHandles, Nodes &&nodes, Model::Model(GpuResourceManager *resourceManager, eastl::vector<TextureHandle> &&textureHandles, Nodes &&nodes,
const ModelHandles &handles, const IndexBuffer &indexBuffer, const eastl::vector<MeshPrimitive> &meshPrimitives) const ModelHandles &handles, const IndexBuffer &indexBuffer,
const eastl::vector<MeshPrimitive> &meshPrimitives)
: m_ResourceManager(resourceManager) : m_ResourceManager(resourceManager)
, m_TextureHandles(std::move(textureHandles)) , m_TextureHandles(std::move(textureHandles))
, m_Nodes(std::move(nodes)) , m_Nodes(std::move(nodes))
@ -741,12 +744,13 @@ ModelLoader::~ModelLoader()
} }
} }
ModelLoader::ModelLoader(ModelLoader &&other) noexcept: m_ResourceManager(Take(other.m_ResourceManager)), ModelLoader::ModelLoader(ModelLoader &&other) noexcept
m_CommandPool(other.m_CommandPool), : m_ResourceManager(Take(other.m_ResourceManager))
m_CommandBuffer(other.m_CommandBuffer), , m_CommandPool(other.m_CommandPool)
m_TransferQueue(other.m_TransferQueue), , m_CommandBuffer(other.m_CommandBuffer)
m_TransferQueueIndex(other.m_TransferQueueIndex), , m_TransferQueue(other.m_TransferQueue)
m_GraphicsQueueIndex(other.m_GraphicsQueueIndex) , m_TransferQueueIndex(other.m_TransferQueueIndex)
, m_GraphicsQueueIndex(other.m_GraphicsQueueIndex)
{ {
} }

View File

@ -72,7 +72,8 @@ struct Model
void Update(); void Update();
Model(GpuResourceManager *resourceManager, eastl::vector<TextureHandle> &&textureHandles, Nodes &&nodes, Model(GpuResourceManager *resourceManager, eastl::vector<TextureHandle> &&textureHandles, Nodes &&nodes,
const ModelHandles& handles, const IndexBuffer &indexBuffer, const eastl::vector<MeshPrimitive> &meshPrimitives); const ModelHandles &handles, const IndexBuffer &indexBuffer,
const eastl::vector<MeshPrimitive> &meshPrimitives);
~Model(); ~Model();
Model(Model &&other) noexcept; Model(Model &&other) noexcept;
@ -91,8 +92,8 @@ struct ModelLoader
u32 m_TransferQueueIndex; u32 m_TransferQueueIndex;
u32 m_GraphicsQueueIndex; u32 m_GraphicsQueueIndex;
TextureHandle LoadImage(vk::CommandBuffer commandBuffer, StagingBuffer *stagingBuffer, TextureHandle
tinygltf::Image *image) const; LoadImage(vk::CommandBuffer commandBuffer, StagingBuffer *stagingBuffer, tinygltf::Image *image) const;
Model LoadModel(cstr path, cstr name = nullptr, bool batched = false); Model LoadModel(cstr path, cstr name = nullptr, bool batched = false);
constexpr static auto ANormal = "NORMAL"; constexpr static auto ANormal = "NORMAL";

View File

@ -18,9 +18,9 @@
#include "helpers.h" #include "helpers.h"
#include "light_manager.h" #include "light_manager.h"
#include "gpu_resource_manager.h"
#include "model_loader.h" #include "model_loader.h"
#include "pipeline_utils.h" #include "pipeline_utils.h"
#include "gpu_resource_manager.h"
#include <EASTL/array.h> #include <EASTL/array.h>
#include <stb_image.h> #include <stb_image.h>
@ -285,11 +285,11 @@ main(int, char **)
for (auto &prim : model.m_MeshPrimitives) for (auto &prim : model.m_MeshPrimitives)
{ {
cmd.pushConstants(pipeline.m_Layout, vk::ShaderStageFlagBits::eAll, pcbOffset, cmd.pushConstants(pipeline.m_Layout, vk::ShaderStageFlagBits::eAll, pcbOffset, sizeof prim.m_MaterialIdx,
sizeof prim.m_MaterialIdx, &prim.m_MaterialIdx); &prim.m_MaterialIdx);
pcbOffset += sizeof prim.m_MaterialIdx; pcbOffset += sizeof prim.m_MaterialIdx;
cmd.pushConstants(pipeline.m_Layout, vk::ShaderStageFlagBits::eAll, pcbOffset, cmd.pushConstants(pipeline.m_Layout, vk::ShaderStageFlagBits::eAll, pcbOffset, sizeof prim.m_TransformIdx,
sizeof prim.m_TransformIdx, &prim.m_TransformIdx); &prim.m_TransformIdx);
pcbOffset += sizeof prim.m_TransformIdx; pcbOffset += sizeof prim.m_TransformIdx;
cmd.drawIndexed(prim.m_IndexCount, 1, prim.m_FirstIndex, Cast<i32>(prim.m_VertexOffset), 0); cmd.drawIndexed(prim.m_IndexCount, 1, prim.m_FirstIndex, Cast<i32>(prim.m_VertexOffset), 0);
} }

View File

@ -6,8 +6,8 @@
#include "pipeline_utils.h" #include "pipeline_utils.h"
#include "device.h" #include "device.h"
#include "helpers.h"
#include "gpu_resource_manager.h" #include "gpu_resource_manager.h"
#include "helpers.h"
#include "swapchain.h" #include "swapchain.h"
#include <EASTL/array.h> #include <EASTL/array.h>

View File

@ -15,14 +15,27 @@ struct FS_Output
float4 GetAlbedo(int MaterialIdx, float2 UV) float4 GetAlbedo(int MaterialIdx, float2 UV)
{ {
uint albedoTexId = MaterialsBuffer[PushConstant.MaterialBufferHandle][MaterialIdx].AlbedoTex; uint AlbedoTexId = MaterialsBuffer[PushConstant.MaterialBufferHandle][MaterialIdx].AlbedoTex;
if (albedoTexId == INVALID_HANDLE) if (AlbedoTexId == INVALID_HANDLE)
{ {
return (float4) MaterialsBuffer[PushConstant.MaterialBufferHandle][MaterialIdx].AlbedoFactor; return (float4) MaterialsBuffer[PushConstant.MaterialBufferHandle][MaterialIdx].AlbedoFactor;
} }
else else
{ {
return Textures[albedoTexId].Sample(ImmutableSamplers[albedoTexId], UV); return Textures[AlbedoTexId].Sample(ImmutableSamplers[AlbedoTexId], UV);
}
}
float2 GetMetalRough(int MaterialIdx, float2 UV)
{
uint MetalRoughTexId = MaterialsBuffer[PushConstant.MaterialBufferHandle][MaterialIdx].MetalRoughTex;
if (MetalRoughTexId == INVALID_HANDLE)
{
return float2(MaterialsBuffer[PushConstant.MaterialBufferHandle][MaterialIdx].MetalFactor, MaterialsBuffer[PushConstant.MaterialBufferHandle][MaterialIdx].RoughFactor);
}
else
{
return Textures[MetalRoughTexId].Sample(ImmutableSamplers[MetalRoughTexId], UV).bg; // Metal in Blue, Roughness in Green
} }
} }
@ -91,6 +104,7 @@ FS_Output main(FS_Input StageInput)
float3 Position = StageInput.InPosition.xyz; float3 Position = StageInput.InPosition.xyz;
float4 ObjColor = PushConstant.MaterialIdx < 0 ? StageInput.InColor : GetAlbedo(PushConstant.MaterialIdx, StageInput.InUV0); float4 ObjColor = PushConstant.MaterialIdx < 0 ? StageInput.InColor : GetAlbedo(PushConstant.MaterialIdx, StageInput.InUV0);
float2 MetalRough = PushConstant.MaterialIdx < 0 ? float2(0.0f, 0.0f) : GetMetalRough(PushConstant.MaterialIdx, StageInput.InUV0);
float3 Diffuse = GetDirectionalLightInfluence(Normal) + GetPointLightInfluence(Position, Normal); float3 Diffuse = GetDirectionalLightInfluence(Normal) + GetPointLightInfluence(Position, Normal);