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

@ -13,8 +13,8 @@
Swapchain::Swapchain(const Window *window, const Device *device, NameString &&name) Swapchain::Swapchain(const Window *window, const Device *device, NameString &&name)
: m_Device(device) : m_Device(device)
, m_Name(std::move(name)) , m_Name(std::move(name))
, m_Format(vk::Format::eUndefined) , m_Format(vk::Format::eUndefined)
{ {
this->Create(window); this->Create(window);
} }
@ -26,12 +26,12 @@ Swapchain::~Swapchain()
Swapchain::Swapchain(Swapchain &&other) noexcept Swapchain::Swapchain(Swapchain &&other) noexcept
: m_Device(other.m_Device) : m_Device(other.m_Device)
, m_Swapchain(Take(other.m_Swapchain)) , m_Swapchain(Take(other.m_Swapchain))
, m_Name(std::move(other.m_Name)) , m_Name(std::move(other.m_Name))
, m_Extent(other.m_Extent) , m_Extent(other.m_Extent)
, m_Format(other.m_Format) , m_Format(other.m_Format)
, m_Images(std::move(other.m_Images)) , m_Images(std::move(other.m_Images))
, m_ImageViews(std::move(other.m_ImageViews)) , m_ImageViews(std::move(other.m_ImageViews))
{ {
} }

View File

@ -23,7 +23,7 @@ TextureHandle
TextureManager::Commit(Texture *texture) TextureManager::Commit(Texture *texture)
{ {
ERROR_IF(!texture->IsValid() || !texture->IsOwned(), "Buffer must be valid and owned for commital") ERROR_IF(!texture->IsValid() || !texture->IsOwned(), "Buffer must be valid and owned for commital")
THEN_ABORT(-1); THEN_ABORT(-1);
if (m_FreeHead != GpuResourceHandle::INVALID_HANDLE) if (m_FreeHead != GpuResourceHandle::INVALID_HANDLE)
{ {
@ -165,7 +165,7 @@ BufferManager::Release(const Device *device, const BufferHandle handle)
void void
BufferManager::Destroy(const Device *device) BufferManager::Destroy(const Device *device)
{ {
for (auto& buffer : m_Buffers) for (auto &buffer : m_Buffers)
{ {
buffer.Destroy(device); buffer.Destroy(device);
} }
@ -275,7 +275,7 @@ GpuResourceManager::Release(TextureHandle handle)
} }
TextureHandle TextureHandle
GpuResourceManager::Commit(Texture* texture) GpuResourceManager::Commit(Texture *texture)
{ {
TextureHandle handle = m_TextureManager.Commit(texture); TextureHandle handle = m_TextureManager.Commit(texture);
@ -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);
@ -63,9 +62,8 @@ 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;
@ -98,7 +96,7 @@ private:
void EraseWrites(u32 handleIndex, HandleType handleType); void EraseWrites(u32 handleIndex, HandleType handleType);
public: public:
const Device *m_Device; const Device *m_Device;
constexpr static u32 BUFFER_BINDING_INDEX = 0; constexpr static u32 BUFFER_BINDING_INDEX = 0;

View File

@ -14,8 +14,8 @@ struct Light
vec3 um_Position; vec3 um_Position;
vec3 um_Direction; vec3 um_Direction;
}; };
f32 m_Range; // < 0.0 for invalid f32 m_Range; // < 0.0 for invalid
u32 m_Color_; // LSB is used for flags. (R G B Flags) u32 m_Color_; // LSB is used for flags. (R G B Flags)
f32 m_Intensity; f32 m_Intensity;
constexpr static u32 MAX_GEN = 0x40; constexpr static u32 MAX_GEN = 0x40;
@ -150,7 +150,7 @@ LightManager::AddDirectional(const vec3 &direction, const vec3 &color)
m_Lights.push_back(); m_Lights.push_back();
m_Lights.push_back(); m_Lights.push_back();
if (m_MetaInfo.m_PointLightMaxCount > 0) // Edge Case: nullptr at size 0 if (m_MetaInfo.m_PointLightMaxCount > 0) // Edge Case: nullptr at size 0
{ {
Light *oldPointStart = m_Lights.data() + oldPointLightOffset; Light *oldPointStart = m_Lights.data() + oldPointLightOffset;
Light *newPointStart = oldPointStart + 2; Light *newPointStart = oldPointStart + 2;
@ -175,7 +175,7 @@ LightManager::AddDirectional(const vec3 &direction, const vec3 &color)
} }
LightHandle LightHandle
LightManager::AddPoint(const vec3 & position, const vec3 &color, const f32 radius) LightManager::AddPoint(const vec3 &position, const vec3 &color, const f32 radius)
{ {
assert(m_PointLightCount <= m_MetaInfo.m_PointLightMaxCount); assert(m_PointLightCount <= m_MetaInfo.m_PointLightMaxCount);
assert(radius >= 0.0f); assert(radius >= 0.0f);
@ -206,7 +206,7 @@ LightManager::AddPoint(const vec3 & position, const vec3 &color, const f32 radiu
const u16 index = m_PointLightCount; const u16 index = m_PointLightCount;
Light *light = &m_Lights[index + m_MetaInfo.m_PointLightOffset]; Light *light = &m_Lights[index + m_MetaInfo.m_PointLightOffset];
constexpr u8 gen = 0; // New light constexpr u8 gen = 0; // New light
light->m_Color_ = (ToColor32(color) & Light::COLOR_MASK) | Light::TYPE_POINT | gen; light->m_Color_ = (ToColor32(color) & Light::COLOR_MASK) | Light::TYPE_POINT | gen;
light->m_Range = radius; light->m_Range = radius;
@ -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

@ -44,11 +44,11 @@ struct LightManager
// We can use that with Offset = 0, and point light at further offsets. // We can use that with Offset = 0, and point light at further offsets.
// This way we don't need to move point lights often. // This way we don't need to move point lights often.
BufferHandle m_LightBuffer; // 04 04 BufferHandle m_LightBuffer; // 04 04
u16 m_PointLightMaxCount; // 02 06 u16 m_PointLightMaxCount; // 02 06
u16 m_PointLightOffset; // 02 08 u16 m_PointLightOffset; // 02 08
u16 m_DirectionalLightMaxCount; // 02 10 u16 m_DirectionalLightMaxCount; // 02 10
u16 m_UnusedPadding0 = 0; // 02 12 u16 m_UnusedPadding0 = 0; // 02 12
}; };
GpuResourceManager *m_ResourceManager; GpuResourceManager *m_ResourceManager;
@ -69,7 +69,7 @@ struct LightManager
constexpr static u16 CAPACITY_MASK = ~(UPDATE_REQUIRED_BIT); constexpr static u16 CAPACITY_MASK = ~(UPDATE_REQUIRED_BIT);
LightHandle AddDirectional(const vec3 &direction, const vec3 &color); LightHandle AddDirectional(const vec3 &direction, const vec3 &color);
LightHandle AddPoint(const vec3& position, const vec3 &color, f32 radius); LightHandle AddPoint(const vec3 &position, const vec3 &color, f32 radius);
void Update(); void Update();
void RemoveLight(LightHandle handle); void RemoveLight(LightHandle handle);

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,17 +63,17 @@ 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 =
{ {
.aspectMask = vk::ImageAspectFlagBits::eColor, .aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0, .baseMipLevel = 0,
.levelCount = texture.GetMipLevels(), .levelCount = texture.GetMipLevels(),
.baseArrayLayer = 0, .baseArrayLayer = 0,
.layerCount = 1, .layerCount = 1,
}, },
}; };
vk::ImageMemoryBarrier nextMipBarrier = { vk::ImageMemoryBarrier nextMipBarrier = {
@ -82,17 +81,17 @@ 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 =
{ {
.aspectMask = vk::ImageAspectFlagBits::eColor, .aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0, .baseMipLevel = 0,
.levelCount = 1, .levelCount = 1,
.baseArrayLayer = 0, .baseArrayLayer = 0,
.layerCount = 1, .layerCount = 1,
}, },
}; };
vk::ImageMemoryBarrier imageReadyBarrier = { vk::ImageMemoryBarrier imageReadyBarrier = {
@ -104,13 +103,13 @@ ModelLoader::LoadImage(vk::CommandBuffer commandBuffer, StagingBuffer *stagingBu
.dstQueueFamilyIndex = m_GraphicsQueueIndex, .dstQueueFamilyIndex = m_GraphicsQueueIndex,
.image = texture.m_Image, .image = texture.m_Image,
.subresourceRange = .subresourceRange =
{ {
.aspectMask = vk::ImageAspectFlagBits::eColor, .aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0, .baseMipLevel = 0,
.levelCount = texture.GetMipLevels(), .levelCount = texture.GetMipLevels(),
.baseArrayLayer = 0, .baseArrayLayer = 0,
.layerCount = 1, .layerCount = 1,
}, },
}; };
vk::BufferImageCopy imageCopy = { vk::BufferImageCopy imageCopy = {
@ -118,12 +117,12 @@ ModelLoader::LoadImage(vk::CommandBuffer commandBuffer, StagingBuffer *stagingBu
.bufferRowLength = Cast<u32>(image->width), .bufferRowLength = Cast<u32>(image->width),
.bufferImageHeight = Cast<u32>(image->height), .bufferImageHeight = Cast<u32>(image->height),
.imageSubresource = .imageSubresource =
{ {
.aspectMask = vk::ImageAspectFlagBits::eColor, .aspectMask = vk::ImageAspectFlagBits::eColor,
.mipLevel = 0, .mipLevel = 0,
.baseArrayLayer = 0, .baseArrayLayer = 0,
.layerCount = 1, .layerCount = 1,
}, },
.imageOffset = {}, .imageOffset = {},
.imageExtent = texture.m_Extent, .imageExtent = texture.m_Extent,
}; };
@ -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);
@ -151,27 +148,29 @@ ModelLoader::LoadImage(vk::CommandBuffer commandBuffer, StagingBuffer *stagingBu
vk::ImageBlit blitRegion = { vk::ImageBlit blitRegion = {
.srcSubresource = .srcSubresource =
{ {
.aspectMask = vk::ImageAspectFlagBits::eColor, .aspectMask = vk::ImageAspectFlagBits::eColor,
.mipLevel = prevMipLevel, .mipLevel = prevMipLevel,
.baseArrayLayer = 0, .baseArrayLayer = 0,
.layerCount = 1, .layerCount = 1,
}, },
.srcOffsets = std::array{ .srcOffsets =
vk::Offset3D{0, 0, 0}, std::array{
vk::Offset3D{prevMipWidth, prevMipHeight, 1}, vk::Offset3D{0, 0, 0},
}, vk::Offset3D{prevMipWidth, prevMipHeight, 1},
},
.dstSubresource = .dstSubresource =
{ {
.aspectMask = vk::ImageAspectFlagBits::eColor, .aspectMask = vk::ImageAspectFlagBits::eColor,
.mipLevel = currentMipLevel, .mipLevel = currentMipLevel,
.baseArrayLayer = 0, .baseArrayLayer = 0,
.layerCount = 1, .layerCount = 1,
}, },
.dstOffsets = std::array{ .dstOffsets =
vk::Offset3D{0, 0, 0}, std::array{
vk::Offset3D{currentMipWidth, currentMipHeight, 1}, vk::Offset3D{0, 0, 0},
}, vk::Offset3D{currentMipWidth, currentMipHeight, 1},
},
}; };
nextMipBarrier.subresourceRange.baseMipLevel = currentMipLevel; nextMipBarrier.subresourceRange.baseMipLevel = currentMipLevel;
@ -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());
@ -407,7 +407,7 @@ ModelLoader::LoadModel(cstr path, cstr name, bool batched)
usize byteOffset = (uvAccessor->byteOffset + uvBufferView->byteOffset); usize byteOffset = (uvAccessor->byteOffset + uvBufferView->byteOffset);
assert(uvAccessor->type == TINYGLTF_TYPE_VEC2 && assert(uvAccessor->type == TINYGLTF_TYPE_VEC2 &&
uvAccessor->componentType == TINYGLTF_COMPONENT_TYPE_FLOAT); uvAccessor->componentType == TINYGLTF_COMPONENT_TYPE_FLOAT);
{ {
vec2 *data = Recast<vec2 *>(uvBuffer->data.data() + byteOffset); vec2 *data = Recast<vec2 *>(uvBuffer->data.data() + byteOffset);
vec2 *end = data + vertexCount; vec2 *end = data + vertexCount;
@ -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

@ -30,15 +30,15 @@ struct MeshPrimitive
struct Material struct Material
{ {
vec4 m_AlbedoFactor; // 16 16 vec4 m_AlbedoFactor; // 16 16
vec3 m_EmissionFactor; // 12 28 vec3 m_EmissionFactor; // 12 28
f32 m_MetalFactor; // 04 32 f32 m_MetalFactor; // 04 32
f32 m_RoughFactor; // 04 36 f32 m_RoughFactor; // 04 36
TextureHandle m_AlbedoTex; // 04 40 TextureHandle m_AlbedoTex; // 04 40
TextureHandle m_NormalTex; // 04 44 TextureHandle m_NormalTex; // 04 44
TextureHandle m_MetalRoughTex; // 04 48 TextureHandle m_MetalRoughTex; // 04 48
TextureHandle m_OcclusionTex; // 04 52 TextureHandle m_OcclusionTex; // 04 52
TextureHandle m_EmissionTex; // 04 56 TextureHandle m_EmissionTex; // 04 56
}; };
struct VertexData struct VertexData
@ -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>
@ -53,18 +53,18 @@ main(int, char **)
Features enabledDeviceFeatures = { Features enabledDeviceFeatures = {
.m_Vulkan10Features = {.samplerAnisotropy = true}, .m_Vulkan10Features = {.samplerAnisotropy = true},
.m_Vulkan12Features = .m_Vulkan12Features =
{ {
.descriptorIndexing = true, .descriptorIndexing = true,
.shaderSampledImageArrayNonUniformIndexing = true, .shaderSampledImageArrayNonUniformIndexing = true,
.shaderStorageBufferArrayNonUniformIndexing = true, .shaderStorageBufferArrayNonUniformIndexing = true,
.shaderStorageImageArrayNonUniformIndexing = true, .shaderStorageImageArrayNonUniformIndexing = true,
.descriptorBindingUniformBufferUpdateAfterBind = true, // Not related to Bindless .descriptorBindingUniformBufferUpdateAfterBind = true, // Not related to Bindless
.descriptorBindingSampledImageUpdateAfterBind = true, .descriptorBindingSampledImageUpdateAfterBind = true,
.descriptorBindingStorageImageUpdateAfterBind = true, .descriptorBindingStorageImageUpdateAfterBind = true,
.descriptorBindingStorageBufferUpdateAfterBind = true, .descriptorBindingStorageBufferUpdateAfterBind = true,
.descriptorBindingPartiallyBound = true, .descriptorBindingPartiallyBound = true,
.runtimeDescriptorArray = true, .runtimeDescriptorArray = true,
}, },
.m_Vulkan13Features = {.dynamicRendering = true}, .m_Vulkan13Features = {.dynamicRendering = true},
}; };
@ -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);