Compare commits

..

No commits in common. "7dd46caac86b3d865f0cebcb3cf1dec9d2eaef8b" and "e82b37b2d9a0a2d9e9eff8971a1e534626f5ffe5" have entirely different histories.

10 changed files with 114 additions and 223 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;-D_DEBUG",
"GLSLC_SHADER_FLAGS": "-g;-D_DEBUG"
"DXC_SHADER_FLAGS": "-Zi",
"GLSLC_SHADER_FLAGS": "-g"
},
"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;-D_DEBUG",
"GLSLC_SHADER_FLAGS": "-g;-D_DEBUG"
"DXC_SHADER_FLAGS": "-Zi",
"GLSLC_SHADER_FLAGS": "-g"
},
"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;-D_DEBUG",
"GLSLC_SHADER_FLAGS": "-g;-D_DEBUG",
"DXC_SHADER_FLAGS": "-Zi",
"GLSLC_SHADER_FLAGS": "-g",
"MSVC_DEFINES": "ASTER_NO_BREAK"
},
"condition": {

View File

@ -12,13 +12,13 @@ Image::Destroy(const Device *device)
{
if (!IsValid() || !IsOwned())
{
m_Flags_ = 0;
m_MipLevels_ = 0;
return;
}
device->m_Device.destroy(m_View, nullptr);
vmaDestroyImage(device->m_Allocator, m_Image, m_Allocation);
m_Flags_ = 0;
m_MipLevels_ = 0;
}
void
@ -28,7 +28,8 @@ 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 u8 mipLevels = isMipMapped ? 1 + Cast<u8>(floor(log2(eastl::max(extent.width, extent.height)))) : 1;
const u32 mipLevels = isMipMapped ? 1 + Cast<u32>(floor(log2(eastl::max(extent.width, extent.height)))) : 1;
assert(mipLevels <= MIP_MASK);
auto usage = vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eTransferDst;
if (isMipMapped)
@ -81,9 +82,7 @@ Texture::Init(const Device *device, const vk::Extent2D extent, vk::Format imageF
m_View = view;
m_Allocation = allocation;
m_Extent = imageCreateInfo.extent;
m_Flags_ = OWNED_BIT | VALID_BIT;
m_LayerCount = 1;
m_MipLevels = mipLevels;
m_MipLevels_ = mipLevels | OWNED_BIT | VALID_BIT;
device->SetName(m_Image, name);
}
@ -111,7 +110,8 @@ 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 u8 mipLevels = isMipMapped ? 1 + Cast<u8>(floor(log2(cubeSide))) : 1;
const u32 mipLevels = isMipMapped ? 1 + Cast<u32>(floor(log2(cubeSide))) : 1;
assert(mipLevels <= MIP_MASK);
auto usage = vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eTransferDst;
if (isMipMapped)
@ -167,9 +167,7 @@ TextureCube::Init(const Device *device, u32 cubeSide, vk::Format imageFormat, bo
m_View = view;
m_Allocation = allocation;
m_Extent = extent;
m_MipLevels = mipLevels;
m_Flags_ = OWNED_BIT | VALID_BIT;
m_LayerCount = 6;
m_MipLevels_ = mipLevels | OWNED_BIT | VALID_BIT;
device->SetName(m_Image, name);
}
@ -222,9 +220,7 @@ AttachmentImage::Init(const Device *device, vk::Extent2D extent, vk::Format imag
m_View = view;
m_Allocation = allocation;
m_Extent = imageCreateInfo.extent;
m_MipLevels = 1;
m_Flags_ = OWNED_BIT | VALID_BIT;
m_LayerCount = 1;
m_MipLevels_ = 1 | OWNED_BIT | VALID_BIT;
device->SetName(m_Image, name);
}
@ -278,9 +274,7 @@ DepthImage::Init(const Device *device, vk::Extent2D extent, cstr name)
m_View = view;
m_Allocation = allocation;
m_Extent = imageCreateInfo.extent;
m_MipLevels = 1;
m_Flags_ = OWNED_BIT | VALID_BIT;
m_LayerCount = 1;
m_MipLevels_ = 1 | OWNED_BIT | VALID_BIT;
device->SetName(m_Image, name);
}
@ -346,16 +340,13 @@ StorageTexture::Init(const Device *device, vk::Extent2D extent, const vk::Format
m_View = view;
m_Allocation = allocation;
m_Extent = imageCreateInfo.extent;
m_MipLevels = 1;
m_Flags_ = OWNED_BIT | VALID_BIT;
m_LayerCount = 1;
m_MipLevels_ = 1 | OWNED_BIT | VALID_BIT;
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
@ -364,12 +355,13 @@ 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 u8 mipLevels = isMipMapped ? 1 + Cast<u8>(floor(log2(cubeSide))) : 1;
const u32 mipLevels = isMipMapped ? 1 + Cast<u32>(floor(log2(cubeSide))) : 1;
assert(mipLevels <= MIP_MASK);
vk::ImageCreateInfo imageCreateInfo = {
.flags = vk::ImageCreateFlagBits::eCubeCompatible,
@ -417,9 +409,7 @@ StorageTextureCube::Init(const Device *device, u32 cubeSide, vk::Format imageFor
m_View = view;
m_Allocation = allocation;
m_Extent = imageCreateInfo.extent;
m_MipLevels = mipLevels;
m_Flags_ = OWNED_BIT | VALID_BIT;
m_LayerCount = 6;
m_MipLevels_ = mipLevels | OWNED_BIT | VALID_BIT;
device->SetName(m_Image, name);
}

View File

@ -41,10 +41,7 @@ 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`.
u8 m_EmptyPadding_ = 0;
u8 m_Flags_ = 0;
u8 m_LayerCount = 0;
u8 m_MipLevels = 0;
u32 m_MipLevels_ = 0;
[[nodiscard]] bool IsValid() const;
[[nodiscard]] bool IsOwned() const;
@ -52,8 +49,9 @@ struct Image
void Destroy(const Device *device);
constexpr static u8 VALID_BIT = 1u << 7;
constexpr static u8 OWNED_BIT = 1u << 6;
constexpr static u32 VALID_BIT = 1u << 31;
constexpr static u32 OWNED_BIT = 1u << 30;
constexpr static u32 MIP_MASK = ~(VALID_BIT | OWNED_BIT);
};
struct Texture : Image
@ -103,17 +101,17 @@ static_assert(sizeof(StorageTextureCube) == sizeof(Image));
inline bool
Image::IsValid() const
{
return m_Flags_ & VALID_BIT;
return m_MipLevels_ & VALID_BIT;
}
inline bool
Image::IsOwned() const
{
return m_Flags_ & OWNED_BIT;
return m_MipLevels_ & OWNED_BIT;
}
inline u32
Image::GetMipLevels() const
{
return m_MipLevels;
return m_MipLevels_ & MIP_MASK;
}

View File

@ -39,7 +39,7 @@ TextureManager::Commit(Texture *texture)
*allocatedTexture = *texture;
// Take ownership of the texture.
texture->m_Flags_ &= ~Texture::OWNED_BIT;
texture->m_MipLevels_ &= ~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_Flags_ &= ~Texture::OWNED_BIT;
texture->m_MipLevels_ &= ~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_Flags_ &= ~Texture::OWNED_BIT;
internal->m_MipLevels_ &= ~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_Flags_ &= ~StorageTexture::OWNED_BIT;
internal->m_MipLevels_ &= ~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 = texture->m_LayerCount,
.layerCount = 1,
},
};
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 = texture->m_LayerCount,
.layerCount = 1,
};
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 = texture->m_LayerCount,
.layerCount = 1,
},
};
vk::DependencyInfo interMipDependency = {
@ -285,7 +285,7 @@ GenerateMipMaps(vk::CommandBuffer commandBuffer, Texture *texture, vk::ImageLayo
.baseMipLevel = 0,
.levelCount = texture->GetMipLevels(),
.baseArrayLayer = 0,
.layerCount = texture->m_LayerCount,
.layerCount = 1,
},
};
vk::DependencyInfo imageReadyDependency = {
@ -298,13 +298,13 @@ GenerateMipMaps(vk::CommandBuffer commandBuffer, Texture *texture, vk::ImageLayo
{
.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseArrayLayer = 0,
.layerCount = texture->m_LayerCount,
.layerCount = 1,
},
.dstSubresource =
{
.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseArrayLayer = 0,
.layerCount = texture->m_LayerCount,
.layerCount = 1,
},
};

View File

@ -139,8 +139,6 @@ 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 = {
@ -199,14 +197,14 @@ main(int, char **)
lightManager.Update();
vk::DescriptorPool descriptorPool;
vk::DescriptorSet perFrameDescriptor;
vk::DescriptorSet descriptorSet;
{
vk::DescriptorSetLayout descriptorSetLayout = pipeline.m_SetLayouts[1];
eastl::array poolSizes = {
vk::DescriptorPoolSize{
.type = vk::DescriptorType::eUniformBuffer,
.descriptorCount = 3,
.descriptorCount = 1,
},
};
vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo = {
@ -218,7 +216,7 @@ main(int, char **)
.descriptorSetCount = 1,
.pSetLayouts = &descriptorSetLayout,
};
AbortIfFailed(device.m_Device.allocateDescriptorSets(&descriptorSetAllocateInfo, &perFrameDescriptor));
AbortIfFailed(device.m_Device.allocateDescriptorSets(&descriptorSetAllocateInfo, &descriptorSet));
}
vk::Extent2D internalResolution = {1920, 1080};
@ -226,50 +224,22 @@ 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, uboSize, "Desc1 UBO");
ubo.Write(&device, 0, ubo.GetSize(), data);
delete[] data;
vk::DescriptorBufferInfo cameraBufferInfo = {
ubo.Init(&device, sizeof cameraController.m_Camera, "Camera UBO");
ubo.Write(&device, 0, sizeof cameraController.m_Camera, &cameraController.m_Camera);
vk::DescriptorBufferInfo descriptorBufferInfo = {
.buffer = ubo.m_Buffer,
.offset = 0,
.range = cameraSize,
};
vk::DescriptorBufferInfo lightingBufferInfo = {
.buffer = ubo.m_Buffer,
.offset = lightOffset,
.range = lightingSize,
.range = ubo.GetSize(),
};
eastl::array writeDescriptors = {
vk::WriteDescriptorSet{
.dstSet = perFrameDescriptor,
.dstSet = descriptorSet,
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = vk::DescriptorType::eUniformBuffer,
.pBufferInfo = &cameraBufferInfo,
},
vk::WriteDescriptorSet{
.dstSet = perFrameDescriptor,
.dstBinding = 1,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = vk::DescriptorType::eUniformBuffer,
.pBufferInfo = &lightingBufferInfo,
.pBufferInfo = &descriptorBufferInfo,
},
};
device.m_Device.updateDescriptorSets(Cast<u32>(writeDescriptors.size()), writeDescriptors.data(), 0, nullptr);
@ -406,11 +376,6 @@ 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);
@ -583,40 +548,45 @@ 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, &perFrameDescriptor, 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, &descriptorSet, 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;
cmd.pushConstants(pipeline.m_Layout, vk::ShaderStageFlagBits::eAll, pcbOffset, sizeof flags, &flags);
pcbOffset += sizeof flags;
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;
for (auto &prim : model.m_MeshPrimitives)
{

View File

@ -43,18 +43,6 @@ 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()),
@ -69,7 +57,7 @@ CreatePipeline(const Device *device, vk::Format attachmentFormat, const GpuResou
vk::PushConstantRange pushConstantRange = {
.stageFlags = vk::ShaderStageFlagBits::eAll,
.offset = 0,
.size = 32,
.size = 52,
};
vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo = {
@ -204,18 +192,6 @@ 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()),
@ -230,7 +206,7 @@ CreateBackgroundPipeline(const Device *device, vk::Format attachmentFormat, cons
vk::PushConstantRange pushConstantRange = {
.stageFlags = vk::ShaderStageFlagBits::eAll,
.offset = 0,
.size = 32,
.size = 52,
};
vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo = {

View File

@ -15,23 +15,7 @@ PS_Output main(PS_Input StageInput)
PS_Output StageOutput;
float3 Direction = normalize(StageInput.WorldPosition - Camera.Position.xyz);
#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
float4 Color = TextureCubes[PushConstant.EnvCubeHandle].Sample(ImmutableSamplers[PushConstant.EnvCubeHandle], Direction);
StageOutput.Color = float4(Uncharted2Tonemap(Color.rgb), Color.a);
return StageOutput;

View File

@ -2,53 +2,34 @@
struct CameraData
{
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
float4x4 View;
float4x4 Projection;
float4x4 InvView;
float4x4 InvProjection;
float4 Position;
};
struct Block
{
uint VertexBufferHandle; // 4
uint VertexDataHandle; // 8
uint MaterialBufferHandle; // 12
uint NodeBufferHandle; // 16
uint VertexBufferHandle;
uint VertexDataHandle;
uint MaterialBufferHandle;
uint NodeBufferHandle;
uint DebugFlags; // 20
uint EnvCubeHandle;
uint DiffuseIrradianceHandle;
uint PrefilterHandle;
uint BrdfLutHandle;
int MaterialIdx; // 24
uint NodeIdx; // 28
uint LightHandle;
uint PointLightIndexer;
uint DirectionalLightIndexer;
int MaterialIdx;
uint NodeIdx;
};
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 (Lights.DiffuseIrradianceHandle != INVALID_HANDLE)
if (PushConstant.DiffuseIrradianceHandle != INVALID_HANDLE)
{
return TextureCubes[Lights.DiffuseIrradianceHandle].Sample(ImmutableSamplers[Lights.DiffuseIrradianceHandle], Direction).rgb;
return TextureCubes[PushConstant.DiffuseIrradianceHandle].Sample(ImmutableSamplers[PushConstant.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 (Lights.PrefilterHandle != INVALID_HANDLE)
if (PushConstant.PrefilterHandle != INVALID_HANDLE)
{
return TextureCubes[Lights.PrefilterHandle].SampleLevel(ImmutableSamplers[Lights.PrefilterHandle], Direction, Mip).rgb;
return TextureCubes[PushConstant.PrefilterHandle].SampleLevel(ImmutableSamplers[PushConstant.PrefilterHandle], Direction, Mip).rgb;
}
return 0.0f.xxx;
}
float2 SampleBrdfLut(float NdotV, float Roughness)
{
if (Lights.BrdfLutHandle != INVALID_HANDLE)
if (PushConstant.BrdfLutHandle != INVALID_HANDLE)
{
return TexturesRG[Lights.BrdfLutHandle].Sample(ImmutableSamplers[Lights.BrdfLutHandle], float2(NdotV, Roughness));
return TexturesRG[PushConstant.BrdfLutHandle].Sample(ImmutableSamplers[PushConstant.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 (Lights.LightHandle == INVALID_HANDLE)
if (PushConstant.LightHandle == INVALID_HANDLE)
return 0.0f.xxx;
uint Offset = IndexerOffset(Lights.PointLightIndexer);
uint Count = IndexerCount(Lights.PointLightIndexer);
uint Offset = IndexerOffset(PushConstant.PointLightIndexer);
uint Count = IndexerCount(PushConstant.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[Lights.LightHandle][i + Offset];
PointLight Light = PointLightBuffer[PushConstant.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 (Lights.LightHandle == INVALID_HANDLE)
if (PushConstant.LightHandle == INVALID_HANDLE)
return 0.0f.xxx;
uint Count = IndexerCount(Lights.DirectionalLightIndexer);
uint Count = IndexerCount(PushConstant.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[Lights.LightHandle][i];
DirectionalLight Light = DirectionalLightBuffer[PushConstant.LightHandle][i];
if (Light.Validity_ < 0.0f)
continue;
@ -279,14 +279,6 @@ 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;
}