Compare commits

...

2 Commits

Author SHA1 Message Date
Anish Bhobe 7dd46caac8 Moved Per-Frame data into Descriptors. 2024-08-09 20:00:34 +02:00
Anish Bhobe 7d5b4034ca Mipmap barrier fix. 2024-08-01 23:44:13 +02:00
10 changed files with 223 additions and 114 deletions

View File

@ -27,8 +27,8 @@
"CMAKE_EXPORT_COMPILE_COMMANDS": true,
"CMAKE_MAKE_PROGRAM": "ninja",
"CMAKE_TOOLCHAIN_FILE": "${sourceDir}/vcpkg/scripts/buildsystems/vcpkg.cmake",
"DXC_SHADER_FLAGS": "-Zi",
"GLSLC_SHADER_FLAGS": "-g"
"DXC_SHADER_FLAGS": "-Zi;-D_DEBUG",
"GLSLC_SHADER_FLAGS": "-g;-D_DEBUG"
},
"condition": {
"type": "equals",
@ -45,8 +45,8 @@
"CMAKE_EXPORT_COMPILE_COMMANDS": true,
"CMAKE_MAKE_PROGRAM": "ninja",
"CMAKE_TOOLCHAIN_FILE": "${sourceDir}/vcpkg/scripts/buildsystems/vcpkg.cmake",
"DXC_SHADER_FLAGS": "-Zi",
"GLSLC_SHADER_FLAGS": "-g"
"DXC_SHADER_FLAGS": "-Zi;-D_DEBUG",
"GLSLC_SHADER_FLAGS": "-g;-D_DEBUG"
},
"condition": {
"type": "equals",
@ -63,8 +63,8 @@
"CMAKE_EXPORT_COMPILE_COMMANDS": true,
"CMAKE_MAKE_PROGRAM": "ninja",
"CMAKE_TOOLCHAIN_FILE": "${sourceDir}/vcpkg/scripts/buildsystems/vcpkg.cmake",
"DXC_SHADER_FLAGS": "-Zi",
"GLSLC_SHADER_FLAGS": "-g",
"DXC_SHADER_FLAGS": "-Zi;-D_DEBUG",
"GLSLC_SHADER_FLAGS": "-g;-D_DEBUG",
"MSVC_DEFINES": "ASTER_NO_BREAK"
},
"condition": {

View File

@ -12,13 +12,13 @@ Image::Destroy(const Device *device)
{
if (!IsValid() || !IsOwned())
{
m_MipLevels_ = 0;
m_Flags_ = 0;
return;
}
device->m_Device.destroy(m_View, nullptr);
vmaDestroyImage(device->m_Allocator, m_Image, m_Allocation);
m_MipLevels_ = 0;
m_Flags_ = 0;
}
void
@ -28,8 +28,7 @@ Texture::Init(const Device *device, const vk::Extent2D extent, vk::Format imageF
WARN_IF(!IsPowerOfTwo(extent.width) || !IsPowerOfTwo(extent.width), "Image {2} is {0}x{1} (Non Power of Two)",
extent.width, extent.height, name ? name : "<unnamed>");
const u32 mipLevels = isMipMapped ? 1 + Cast<u32>(floor(log2(eastl::max(extent.width, extent.height)))) : 1;
assert(mipLevels <= MIP_MASK);
const u8 mipLevels = isMipMapped ? 1 + Cast<u8>(floor(log2(eastl::max(extent.width, extent.height)))) : 1;
auto usage = vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eTransferDst;
if (isMipMapped)
@ -82,7 +81,9 @@ Texture::Init(const Device *device, const vk::Extent2D extent, vk::Format imageF
m_View = view;
m_Allocation = allocation;
m_Extent = imageCreateInfo.extent;
m_MipLevels_ = mipLevels | OWNED_BIT | VALID_BIT;
m_Flags_ = OWNED_BIT | VALID_BIT;
m_LayerCount = 1;
m_MipLevels = mipLevels;
device->SetName(m_Image, name);
}
@ -110,8 +111,7 @@ TextureCube::Init(const Device *device, u32 cubeSide, vk::Format imageFormat, bo
{
WARN_IF(!IsPowerOfTwo(cubeSide), "Image {1} is {0}x{0} (Non Power of Two)", cubeSide, name ? name : "<unnamed>");
const u32 mipLevels = isMipMapped ? 1 + Cast<u32>(floor(log2(cubeSide))) : 1;
assert(mipLevels <= MIP_MASK);
const u8 mipLevels = isMipMapped ? 1 + Cast<u8>(floor(log2(cubeSide))) : 1;
auto usage = vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eTransferDst;
if (isMipMapped)
@ -167,7 +167,9 @@ TextureCube::Init(const Device *device, u32 cubeSide, vk::Format imageFormat, bo
m_View = view;
m_Allocation = allocation;
m_Extent = extent;
m_MipLevels_ = mipLevels | OWNED_BIT | VALID_BIT;
m_MipLevels = mipLevels;
m_Flags_ = OWNED_BIT | VALID_BIT;
m_LayerCount = 6;
device->SetName(m_Image, name);
}
@ -220,7 +222,9 @@ AttachmentImage::Init(const Device *device, vk::Extent2D extent, vk::Format imag
m_View = view;
m_Allocation = allocation;
m_Extent = imageCreateInfo.extent;
m_MipLevels_ = 1 | OWNED_BIT | VALID_BIT;
m_MipLevels = 1;
m_Flags_ = OWNED_BIT | VALID_BIT;
m_LayerCount = 1;
device->SetName(m_Image, name);
}
@ -274,7 +278,9 @@ DepthImage::Init(const Device *device, vk::Extent2D extent, cstr name)
m_View = view;
m_Allocation = allocation;
m_Extent = imageCreateInfo.extent;
m_MipLevels_ = 1 | OWNED_BIT | VALID_BIT;
m_MipLevels = 1;
m_Flags_ = OWNED_BIT | VALID_BIT;
m_LayerCount = 1;
device->SetName(m_Image, name);
}
@ -340,13 +346,16 @@ StorageTexture::Init(const Device *device, vk::Extent2D extent, const vk::Format
m_View = view;
m_Allocation = allocation;
m_Extent = imageCreateInfo.extent;
m_MipLevels_ = 1 | OWNED_BIT | VALID_BIT;
m_MipLevels = 1;
m_Flags_ = OWNED_BIT | VALID_BIT;
m_LayerCount = 1;
device->SetName(m_Image, name);
}
void
StorageTextureCube::Init(const Device* device, u32 cubeSide, vk::Format imageFormat, bool isSampled, bool isMipMapped, cstr name)
StorageTextureCube::Init(const Device *device, u32 cubeSide, vk::Format imageFormat, bool isSampled, bool isMipMapped,
cstr name)
{
// Reasoning:
// Transfer Src and Dst to copy to and from the buffer since Storage will often be loaded with info, and read for
@ -355,13 +364,12 @@ StorageTextureCube::Init(const Device* device, u32 cubeSide, vk::Format imageFor
vk::ImageUsageFlagBits::eStorage | vk::ImageUsageFlagBits::eTransferSrc | vk::ImageUsageFlagBits::eTransferDst;
if (isSampled)
{
WARN_IF(!IsPowerOfTwo(cubeSide), "Image {1} is {0}x{0} (Non Power of Two)",
cubeSide, name ? name : "<unnamed>");
WARN_IF(!IsPowerOfTwo(cubeSide), "Image {1} is {0}x{0} (Non Power of Two)", cubeSide,
name ? name : "<unnamed>");
usage |= vk::ImageUsageFlagBits::eSampled;
}
const u32 mipLevels = isMipMapped ? 1 + Cast<u32>(floor(log2(cubeSide))) : 1;
assert(mipLevels <= MIP_MASK);
const u8 mipLevels = isMipMapped ? 1 + Cast<u8>(floor(log2(cubeSide))) : 1;
vk::ImageCreateInfo imageCreateInfo = {
.flags = vk::ImageCreateFlagBits::eCubeCompatible,
@ -409,7 +417,9 @@ StorageTextureCube::Init(const Device* device, u32 cubeSide, vk::Format imageFor
m_View = view;
m_Allocation = allocation;
m_Extent = imageCreateInfo.extent;
m_MipLevels_ = mipLevels | OWNED_BIT | VALID_BIT;
m_MipLevels = mipLevels;
m_Flags_ = OWNED_BIT | VALID_BIT;
m_LayerCount = 6;
device->SetName(m_Image, name);
}

View File

@ -41,7 +41,10 @@ struct Image
vk::Extent3D m_Extent;
// Image.m_MipLevels_ is used for bookkeeping
// If the image is Invalid, the remaining data in Image is used intrusively by `GpuResourceManager`.
u32 m_MipLevels_ = 0;
u8 m_EmptyPadding_ = 0;
u8 m_Flags_ = 0;
u8 m_LayerCount = 0;
u8 m_MipLevels = 0;
[[nodiscard]] bool IsValid() const;
[[nodiscard]] bool IsOwned() const;
@ -49,9 +52,8 @@ struct Image
void Destroy(const Device *device);
constexpr static u32 VALID_BIT = 1u << 31;
constexpr static u32 OWNED_BIT = 1u << 30;
constexpr static u32 MIP_MASK = ~(VALID_BIT | OWNED_BIT);
constexpr static u8 VALID_BIT = 1u << 7;
constexpr static u8 OWNED_BIT = 1u << 6;
};
struct Texture : Image
@ -101,17 +103,17 @@ static_assert(sizeof(StorageTextureCube) == sizeof(Image));
inline bool
Image::IsValid() const
{
return m_MipLevels_ & VALID_BIT;
return m_Flags_ & VALID_BIT;
}
inline bool
Image::IsOwned() const
{
return m_MipLevels_ & OWNED_BIT;
return m_Flags_ & OWNED_BIT;
}
inline u32
Image::GetMipLevels() const
{
return m_MipLevels_ & MIP_MASK;
return m_MipLevels;
}

View File

@ -39,7 +39,7 @@ TextureManager::Commit(Texture *texture)
*allocatedTexture = *texture;
// Take ownership of the texture.
texture->m_MipLevels_ &= ~Texture::OWNED_BIT;
texture->m_Flags_ &= ~Texture::OWNED_BIT;
return {index};
}
@ -53,7 +53,7 @@ TextureManager::Commit(Texture *texture)
static_assert(std::is_trivially_copyable_v<Texture>);
*allocatedTexture = *texture;
texture->m_MipLevels_ &= ~Texture::OWNED_BIT;
texture->m_Flags_ &= ~Texture::OWNED_BIT;
return {index};
}
@ -390,7 +390,7 @@ GpuResourceManager::Release(Texture *texture, TextureHandle handle)
Texture *internal = m_TextureManager.Fetch(handle);
*texture = *internal;
internal->m_MipLevels_ &= ~Texture::OWNED_BIT;
internal->m_Flags_ &= ~Texture::OWNED_BIT;
Release(handle);
}
@ -480,7 +480,7 @@ GpuResourceManager::Release(StorageTexture *texture, const StorageTextureHandle
StorageTexture *internal = m_StorageTextureManager.Fetch(handle);
*texture = *internal;
internal->m_MipLevels_ &= ~StorageTexture::OWNED_BIT;
internal->m_Flags_ &= ~StorageTexture::OWNED_BIT;
Release(handle);
}

View File

@ -218,7 +218,7 @@ GenerateMipMaps(vk::CommandBuffer commandBuffer, Texture *texture, vk::ImageLayo
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
.layerCount = texture->m_LayerCount,
},
};
vk::ImageMemoryBarrier2 mipsStartBarrier = imageStartBarrier;
@ -230,7 +230,7 @@ GenerateMipMaps(vk::CommandBuffer commandBuffer, Texture *texture, vk::ImageLayo
.baseMipLevel = 1,
.levelCount = texture->GetMipLevels() - 1,
.baseArrayLayer = 0,
.layerCount = 1,
.layerCount = texture->m_LayerCount,
};
eastl::fixed_vector<vk::ImageMemoryBarrier2, 2> startBarriers = {
mipsStartBarrier,
@ -261,7 +261,7 @@ GenerateMipMaps(vk::CommandBuffer commandBuffer, Texture *texture, vk::ImageLayo
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
.layerCount = texture->m_LayerCount,
},
};
vk::DependencyInfo interMipDependency = {
@ -285,7 +285,7 @@ GenerateMipMaps(vk::CommandBuffer commandBuffer, Texture *texture, vk::ImageLayo
.baseMipLevel = 0,
.levelCount = texture->GetMipLevels(),
.baseArrayLayer = 0,
.layerCount = 1,
.layerCount = texture->m_LayerCount,
},
};
vk::DependencyInfo imageReadyDependency = {
@ -298,13 +298,13 @@ GenerateMipMaps(vk::CommandBuffer commandBuffer, Texture *texture, vk::ImageLayo
{
.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseArrayLayer = 0,
.layerCount = 1,
.layerCount = texture->m_LayerCount,
},
.dstSubresource =
{
.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseArrayLayer = 0,
.layerCount = 1,
.layerCount = texture->m_LayerCount,
},
};

View File

@ -139,6 +139,8 @@ main(int, char **)
PhysicalDevices physicalDevices = {&window, &context};
PhysicalDevice deviceToUse = FindSuitableDevice(physicalDevices);
usize physicalDeviceOffsetAlignment = deviceToUse.m_DeviceProperties.limits.minUniformBufferOffsetAlignment;
INFO("Using {} as the primary device.", deviceToUse.m_DeviceProperties.deviceName.data());
Features enabledDeviceFeatures = {
@ -197,14 +199,14 @@ main(int, char **)
lightManager.Update();
vk::DescriptorPool descriptorPool;
vk::DescriptorSet descriptorSet;
vk::DescriptorSet perFrameDescriptor;
{
vk::DescriptorSetLayout descriptorSetLayout = pipeline.m_SetLayouts[1];
eastl::array poolSizes = {
vk::DescriptorPoolSize{
.type = vk::DescriptorType::eUniformBuffer,
.descriptorCount = 1,
.descriptorCount = 3,
},
};
vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo = {
@ -216,7 +218,7 @@ main(int, char **)
.descriptorSetCount = 1,
.pSetLayouts = &descriptorSetLayout,
};
AbortIfFailed(device.m_Device.allocateDescriptorSets(&descriptorSetAllocateInfo, &descriptorSet));
AbortIfFailed(device.m_Device.allocateDescriptorSets(&descriptorSetAllocateInfo, &perFrameDescriptor));
}
vk::Extent2D internalResolution = {1920, 1080};
@ -224,22 +226,50 @@ main(int, char **)
CameraController cameraController = {vec3{0.0f, 0.0f, 2.0f}, vec3{0.0f}, 70_deg,
Cast<f32>(swapchain.m_Extent.width) / Cast<f32>(swapchain.m_Extent.height)};
usize uboSize = 0;
usize cameraSize = sizeof cameraController.m_Camera;
uboSize += ClosestMultiple(cameraSize, physicalDeviceOffsetAlignment);
usize lightOffset = uboSize;
usize lightingSize = sizeof environment + sizeof lightManager.m_MetaInfo;
uboSize += ClosestMultiple(lightingSize, physicalDeviceOffsetAlignment);
u8 *data = new u8[uboSize];
memcpy(data, &cameraController.m_Camera, cameraSize);
memcpy(data + lightOffset, &environment, sizeof environment);
memcpy(data + lightOffset + sizeof environment, &lightManager.m_MetaInfo, sizeof lightManager.m_MetaInfo);
UniformBuffer ubo;
ubo.Init(&device, sizeof cameraController.m_Camera, "Camera UBO");
ubo.Write(&device, 0, sizeof cameraController.m_Camera, &cameraController.m_Camera);
vk::DescriptorBufferInfo descriptorBufferInfo = {
ubo.Init(&device, uboSize, "Desc1 UBO");
ubo.Write(&device, 0, ubo.GetSize(), data);
delete[] data;
vk::DescriptorBufferInfo cameraBufferInfo = {
.buffer = ubo.m_Buffer,
.offset = 0,
.range = ubo.GetSize(),
.range = cameraSize,
};
vk::DescriptorBufferInfo lightingBufferInfo = {
.buffer = ubo.m_Buffer,
.offset = lightOffset,
.range = lightingSize,
};
eastl::array writeDescriptors = {
vk::WriteDescriptorSet{
.dstSet = descriptorSet,
.dstSet = perFrameDescriptor,
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = vk::DescriptorType::eUniformBuffer,
.pBufferInfo = &descriptorBufferInfo,
.pBufferInfo = &cameraBufferInfo,
},
vk::WriteDescriptorSet{
.dstSet = perFrameDescriptor,
.dstBinding = 1,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = vk::DescriptorType::eUniformBuffer,
.pBufferInfo = &lightingBufferInfo,
},
};
device.m_Device.updateDescriptorSets(Cast<u32>(writeDescriptors.size()), writeDescriptors.data(), 0, nullptr);
@ -376,6 +406,11 @@ main(int, char **)
bool showPrefilter = false;
bool useSpecular = true;
constexpr u32 USE_DIFFUSE_BIT = 1;
constexpr u32 USE_SPECULAR_BIT = 1 << 1;
constexpr u32 SHOW_DIFFUSE_BIT = 1 << 2;
constexpr u32 SHOW_PREFILTER_BIT = 1 << 3;
i32 height = Cast<i32>(internalResolution.height);
f32 camPitch = glm::degrees(cameraController.m_Pitch);
f32 camYaw = glm::degrees(cameraController.m_Yaw);
@ -548,45 +583,40 @@ main(int, char **)
cmd.setScissor(0, 1, &scissor);
cmd.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipeline.m_Layout, 0, 1,
&resourceManager.m_DescriptorSet, 0, nullptr);
cmd.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipeline.m_Layout, 1, 1, &descriptorSet, 0, nullptr);
cmd.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipeline.m_Layout, 1, 1, &perFrameDescriptor, 0,
nullptr);
cmd.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipeline.m_Layout, 1, 1, &perFrameDescriptor, 0,
nullptr);
cmd.bindIndexBuffer(model.m_IndexBuffer.m_Buffer, 0, vk::IndexType::eUint32);
cmd.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline.m_Pipeline);
u32 flags = 0;
if (useSpecular)
{
flags |= USE_SPECULAR_BIT;
}
if (useDiffuse)
{
flags |= USE_DIFFUSE_BIT;
}
if (showPrefilter)
{
flags |= SHOW_PREFILTER_BIT;
}
if (showDiffuse)
{
flags |= SHOW_DIFFUSE_BIT;
}
u32 pcbOffset = 0;
cmd.pushConstants(pipeline.m_Layout, vk::ShaderStageFlagBits::eAll, pcbOffset, sizeof model.m_Handles,
&model.m_Handles);
pcbOffset += sizeof model.m_Handles;
Environment thisFrameEnvBuffers = environment;
if (showDiffuse)
{
thisFrameEnvBuffers.m_Skybox = environment.m_Diffuse;
}
else if (showPrefilter)
{
thisFrameEnvBuffers.m_Skybox = environment.m_Prefilter;
}
if (!useDiffuse)
{
thisFrameEnvBuffers.m_Diffuse = {};
}
if (!useSpecular)
{
thisFrameEnvBuffers.m_BrdfLut = {};
thisFrameEnvBuffers.m_Prefilter = {};
}
assert(pcbOffset == 16);
cmd.pushConstants(pipeline.m_Layout, vk::ShaderStageFlagBits::eAll, pcbOffset, sizeof thisFrameEnvBuffers,
&thisFrameEnvBuffers);
pcbOffset += sizeof thisFrameEnvBuffers;
cmd.pushConstants(pipeline.m_Layout, vk::ShaderStageFlagBits::eAll, pcbOffset, sizeof lightManager.m_MetaInfo,
&lightManager.m_MetaInfo);
pcbOffset += sizeof lightManager.m_MetaInfo;
cmd.pushConstants(pipeline.m_Layout, vk::ShaderStageFlagBits::eAll, pcbOffset, sizeof flags, &flags);
pcbOffset += sizeof flags;
for (auto &prim : model.m_MeshPrimitives)
{

View File

@ -43,6 +43,18 @@ CreatePipeline(const Device *device, vk::Format attachmentFormat, const GpuResou
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eAll,
},
vk::DescriptorSetLayoutBinding{
.binding = 1,
.descriptorType = vk::DescriptorType::eUniformBuffer,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eAll,
},
vk::DescriptorSetLayoutBinding{
.binding = 2,
.descriptorType = vk::DescriptorType::eUniformBuffer,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eAll,
},
};
vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo = {
.bindingCount = Cast<u32>(descriptorSetLayoutBindings.size()),
@ -57,7 +69,7 @@ CreatePipeline(const Device *device, vk::Format attachmentFormat, const GpuResou
vk::PushConstantRange pushConstantRange = {
.stageFlags = vk::ShaderStageFlagBits::eAll,
.offset = 0,
.size = 52,
.size = 32,
};
vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo = {
@ -192,6 +204,18 @@ CreateBackgroundPipeline(const Device *device, vk::Format attachmentFormat, cons
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eAll,
},
vk::DescriptorSetLayoutBinding{
.binding = 1,
.descriptorType = vk::DescriptorType::eUniformBuffer,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eAll,
},
vk::DescriptorSetLayoutBinding{
.binding = 2,
.descriptorType = vk::DescriptorType::eUniformBuffer,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eAll,
},
};
vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo = {
.bindingCount = Cast<u32>(descriptorSetLayoutBindings.size()),
@ -206,7 +230,7 @@ CreateBackgroundPipeline(const Device *device, vk::Format attachmentFormat, cons
vk::PushConstantRange pushConstantRange = {
.stageFlags = vk::ShaderStageFlagBits::eAll,
.offset = 0,
.size = 52,
.size = 32,
};
vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo = {

View File

@ -15,7 +15,23 @@ PS_Output main(PS_Input StageInput)
PS_Output StageOutput;
float3 Direction = normalize(StageInput.WorldPosition - Camera.Position.xyz);
float4 Color = TextureCubes[PushConstant.EnvCubeHandle].Sample(ImmutableSamplers[PushConstant.EnvCubeHandle], Direction);
#ifndef _DEBUG
float4 Color = TextureCubes[Lights.EnvCubeHandle].Sample(ImmutableSamplers[Lights.EnvCubeHandle], Direction);
#else
float4 Color;
if ((PushConstant.DebugFlags & SHOW_DIFFUSE_BIT) > 0)
{
Color = TextureCubes[Lights.DiffuseIrradianceHandle].Sample(ImmutableSamplers[Lights.DiffuseIrradianceHandle], Direction);
}
else if ((PushConstant.DebugFlags & SHOW_PREFILTER_BIT) > 0)
{
Color = TextureCubes[Lights.PrefilterHandle].Sample(ImmutableSamplers[Lights.PrefilterHandle], Direction);
}
else
{
Color = TextureCubes[Lights.EnvCubeHandle].Sample(ImmutableSamplers[Lights.EnvCubeHandle], Direction);
}
#endif
StageOutput.Color = float4(Uncharted2Tonemap(Color.rgb), Color.a);
return StageOutput;

View File

@ -2,34 +2,53 @@
struct CameraData
{
float4x4 View;
float4x4 Projection;
float4x4 InvView;
float4x4 InvProjection;
float4 Position;
float4x4 View; // 64
float4x4 Projection; // 128
float4x4 InvView; // 192
float4x4 InvProjection; // 256
float4 Position; // 272
};
struct Lighting
{
uint EnvCubeHandle; // 4
uint DiffuseIrradianceHandle; // 8
uint PrefilterHandle; // 12
uint BrdfLutHandle; // 16
uint LightHandle; // 20
uint PointLightIndexer; // 24
uint DirectionalLightIndexer; // 28
};
struct ModelHandles
{
uint VertexBufferHandle; // 4
uint VertexDataHandle; // 8
uint MaterialBufferHandle; // 12
uint NodeBufferHandle; // 16
};
struct Block
{
uint VertexBufferHandle;
uint VertexDataHandle;
uint MaterialBufferHandle;
uint NodeBufferHandle;
uint VertexBufferHandle; // 4
uint VertexDataHandle; // 8
uint MaterialBufferHandle; // 12
uint NodeBufferHandle; // 16
uint EnvCubeHandle;
uint DiffuseIrradianceHandle;
uint PrefilterHandle;
uint BrdfLutHandle;
uint DebugFlags; // 20
uint LightHandle;
uint PointLightIndexer;
uint DirectionalLightIndexer;
int MaterialIdx;
uint NodeIdx;
int MaterialIdx; // 24
uint NodeIdx; // 28
};
static const uint USE_DIFFUSE_BIT = 1;
static const uint USE_SPECULAR_BIT = 2;
static const uint SHOW_DIFFUSE_BIT = 4;
static const uint SHOW_PREFILTER_BIT = 8;
[[vk::binding(0, 1)]] ConstantBuffer<CameraData> Camera;
[[vk::binding(1, 1)]] ConstantBuffer<Lighting> Lights;
[[vk::binding(2, 1)]] ConstantBuffer<ModelHandles> Model;
[[vk::push_constant]]
Block PushConstant;

View File

@ -70,9 +70,9 @@ float2 GetMetalRough(float2 UV)
float3 SampleIrradiance(float3 Direction)
{
if (PushConstant.DiffuseIrradianceHandle != INVALID_HANDLE)
if (Lights.DiffuseIrradianceHandle != INVALID_HANDLE)
{
return TextureCubes[PushConstant.DiffuseIrradianceHandle].Sample(ImmutableSamplers[PushConstant.DiffuseIrradianceHandle], Direction).rgb;
return TextureCubes[Lights.DiffuseIrradianceHandle].Sample(ImmutableSamplers[Lights.DiffuseIrradianceHandle], Direction).rgb;
}
return 0.04f.xxx;
}
@ -81,18 +81,18 @@ float3 SamplePrefiltered(float3 Direction, float Roughness)
{
const float MAX_MIP_LEVEL = 5.0f;
float Mip = MAX_MIP_LEVEL * Roughness;
if (PushConstant.PrefilterHandle != INVALID_HANDLE)
if (Lights.PrefilterHandle != INVALID_HANDLE)
{
return TextureCubes[PushConstant.PrefilterHandle].SampleLevel(ImmutableSamplers[PushConstant.PrefilterHandle], Direction, Mip).rgb;
return TextureCubes[Lights.PrefilterHandle].SampleLevel(ImmutableSamplers[Lights.PrefilterHandle], Direction, Mip).rgb;
}
return 0.0f.xxx;
}
float2 SampleBrdfLut(float NdotV, float Roughness)
{
if (PushConstant.BrdfLutHandle != INVALID_HANDLE)
if (Lights.BrdfLutHandle != INVALID_HANDLE)
{
return TexturesRG[PushConstant.BrdfLutHandle].Sample(ImmutableSamplers[PushConstant.BrdfLutHandle], float2(NdotV, Roughness));
return TexturesRG[Lights.BrdfLutHandle].Sample(ImmutableSamplers[Lights.BrdfLutHandle], float2(NdotV, Roughness));
}
return 0.0f.xx;
}
@ -171,11 +171,11 @@ float3 GetPBRContrib(float3 Albedo, float3 LightColor, float3 ViewDir, float3 No
float3 GetPointLightInfluence(float3 Albedo, float2 MetalRough, float3 Position, float3 Normal)
{
if (PushConstant.LightHandle == INVALID_HANDLE)
if (Lights.LightHandle == INVALID_HANDLE)
return 0.0f.xxx;
uint Offset = IndexerOffset(PushConstant.PointLightIndexer);
uint Count = IndexerCount(PushConstant.PointLightIndexer);
uint Offset = IndexerOffset(Lights.PointLightIndexer);
uint Count = IndexerCount(Lights.PointLightIndexer);
float3 ViewDir = normalize(Camera.Position.xyz - Position);
@ -190,7 +190,7 @@ float3 GetPointLightInfluence(float3 Albedo, float2 MetalRough, float3 Position,
float3 Contrib = 0.0f;
for (uint i = 0; i < Count; ++i)
{
PointLight Light = PointLightBuffer[PushConstant.LightHandle][i + Offset];
PointLight Light = PointLightBuffer[Lights.LightHandle][i + Offset];
if (Light.Range < 0.0f)
continue;
@ -218,10 +218,10 @@ float3 GetPointLightInfluence(float3 Albedo, float2 MetalRough, float3 Position,
float3 GetDirectionalLightInfluence(float3 Albedo, float2 MetalRough, float3 Position, float3 Normal)
{
if (PushConstant.LightHandle == INVALID_HANDLE)
if (Lights.LightHandle == INVALID_HANDLE)
return 0.0f.xxx;
uint Count = IndexerCount(PushConstant.DirectionalLightIndexer);
uint Count = IndexerCount(Lights.DirectionalLightIndexer);
float3 ViewDir = normalize(Camera.Position.xyz - Position);
@ -236,7 +236,7 @@ float3 GetDirectionalLightInfluence(float3 Albedo, float2 MetalRough, float3 Pos
float3 Contrib = 0.0f;
for (uint i = 0; i < Count; ++i)
{
DirectionalLight Light = DirectionalLightBuffer[PushConstant.LightHandle][i];
DirectionalLight Light = DirectionalLightBuffer[Lights.LightHandle][i];
if (Light.Validity_ < 0.0f)
continue;
@ -279,6 +279,14 @@ float3 GetAmbientInfluence(float3 Albedo, float2 MetalRough, float3 Position, fl
float3 Specular = PrefilteredColor * (K_Specular * EnvBRDF.x + EnvBRDF.y);
float3 DiffuseIrradiance = Albedo * SampleIrradiance(Normal);
#ifdef _DEBUG
if ((PushConstant.DebugFlags & USE_DIFFUSE_BIT) == 0) {
DiffuseIrradiance = 0.0f.xxx;
}
if ((PushConstant.DebugFlags & USE_SPECULAR_BIT) == 0) {
Specular = 0.0f.xxx;
}
#endif
return (K_Diffuse * DiffuseIrradiance + Specular) * Occlusion;
}