shadow texture array

This commit is contained in:
Zack Buhman 2026-04-18 12:22:50 -05:00
parent 1ae766c648
commit 440272ad7a
2 changed files with 18 additions and 24 deletions

View File

@ -62,7 +62,7 @@ struct MaterialColorImage
// set 1: constant // set 1: constant
[[vk::binding(0, 1)]] StructuredBuffer<MaterialColorImage> MaterialColorImages; [[vk::binding(0, 1)]] StructuredBuffer<MaterialColorImage> MaterialColorImages;
[[vk::binding(1, 1)]] SamplerState LinearSampler; [[vk::binding(1, 1)]] SamplerState LinearSampler;
[[vk::binding(2, 1)]] Texture2D ShadowTexture; [[vk::binding(2, 1)]] Texture2DArray ShadowTexture;
[[vk::binding(3, 1)]] Texture2D SceneTexture[]; [[vk::binding(3, 1)]] Texture2D SceneTexture[];
struct PushConstant { struct PushConstant {
@ -103,7 +103,7 @@ VSOutput VSMain(VSInput input)
float Shadow(float3 position, float bias) float Shadow(float3 position, float bias)
{ {
float sampledDepth = ShadowTexture.Sample(LinearSampler, position.xy).x; float sampledDepth = ShadowTexture.Sample(LinearSampler, float3(position.xy, 0)).x;
float shadow = (position.z - bias) > sampledDepth ? 0.1 : 1.0; float shadow = (position.z - bias) > sampledDepth ? 0.1 : 1.0;
return shadow; return shadow;
} }
@ -111,7 +111,8 @@ float Shadow(float3 position, float bias)
float ShadowPCF(float3 position, float bias) float ShadowPCF(float3 position, float bias)
{ {
float2 dimensions; float2 dimensions;
ShadowTexture.GetDimensions(dimensions.x, dimensions.y); float elements;
ShadowTexture.GetDimensions(dimensions.x, dimensions.y, elements);
float2 texelSize = 1.0 / dimensions; float2 texelSize = 1.0 / dimensions;
float shadow = 0.0; float shadow = 0.0;

View File

@ -34,6 +34,7 @@ VkImage depthImage{ VK_NULL_HANDLE };
VkImageView depthImageView{ VK_NULL_HANDLE }; VkImageView depthImageView{ VK_NULL_HANDLE };
VkDeviceMemory depthMemory{ VK_NULL_HANDLE }; VkDeviceMemory depthMemory{ VK_NULL_HANDLE };
uint32_t shadowArrayLayers{ 6 };
VkImage shadowDepthImage{ VK_NULL_HANDLE }; VkImage shadowDepthImage{ VK_NULL_HANDLE };
VkImageView shadowDepthImageView{ VK_NULL_HANDLE }; VkImageView shadowDepthImageView{ VK_NULL_HANDLE };
VkImageView shadowDepthImageViewDepth{ VK_NULL_HANDLE }; VkImageView shadowDepthImageViewDepth{ VK_NULL_HANDLE };
@ -102,6 +103,7 @@ void createDepth(VkDeviceSize nonCoherentAtomSize,
uint32_t height, uint32_t height,
VkFormat format, VkFormat format,
VkImageUsageFlags usage, VkImageUsageFlags usage,
uint32_t arrayLayers,
VkImage * image, VkImage * image,
VkDeviceMemory * memory, VkDeviceMemory * memory,
VkImageView * imageView) VkImageView * imageView)
@ -116,7 +118,7 @@ void createDepth(VkDeviceSize nonCoherentAtomSize,
.depth = 1, .depth = 1,
}, },
.mipLevels = 1, .mipLevels = 1,
.arrayLayers = 1, .arrayLayers = arrayLayers,
.samples = VK_SAMPLE_COUNT_1_BIT, .samples = VK_SAMPLE_COUNT_1_BIT,
.tiling = VK_IMAGE_TILING_OPTIMAL, .tiling = VK_IMAGE_TILING_OPTIMAL,
.usage = usage, .usage = usage,
@ -140,33 +142,21 @@ void createDepth(VkDeviceSize nonCoherentAtomSize,
&stride); &stride);
VK_CHECK(vkBindImageMemory(device, *image, *memory, 0)); VK_CHECK(vkBindImageMemory(device, *image, *memory, 0));
VkImageViewType viewType{ (arrayLayers == 1) ? VK_IMAGE_VIEW_TYPE_2D : VK_IMAGE_VIEW_TYPE_2D_ARRAY };
VkImageViewCreateInfo imageViewCreateInfo{ VkImageViewCreateInfo imageViewCreateInfo{
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
.image = *image, .image = *image,
.viewType = VK_IMAGE_VIEW_TYPE_2D, .viewType = viewType,
.format = format, .format = format,
.subresourceRange{ .subresourceRange{
.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT, .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT,
.levelCount = 1, .levelCount = 1,
.layerCount = 1 .layerCount = arrayLayers
} }
}; };
VK_CHECK(vkCreateImageView(device, &imageViewCreateInfo, nullptr, imageView)); VK_CHECK(vkCreateImageView(device, &imageViewCreateInfo, nullptr, imageView));
} }
void createCubeDepth(VkDeviceSize nonCoherentAtomSize,
VkPhysicalDeviceMemoryProperties const & physicalDeviceMemoryProperties,
uint32_t width,
uint32_t height,
VkFormat format,
VkImageUsageFlags usage,
VkImage * image,
VkDeviceMemory * memory,
VkImageView * imageView)
{
}
void recreateSwapchain(VkSurfaceFormatKHR surfaceFormat, void recreateSwapchain(VkSurfaceFormatKHR surfaceFormat,
VkFormat depthFormat, VkFormat depthFormat,
VkDeviceSize nonCoherentAtomSize, VkDeviceSize nonCoherentAtomSize,
@ -267,12 +257,14 @@ void recreateSwapchain(VkSurfaceFormatKHR surfaceFormat,
vkDestroyImageView(device, depthImageView, nullptr); vkDestroyImageView(device, depthImageView, nullptr);
} }
uint32_t arrayLayers{ 1 };
createDepth(nonCoherentAtomSize, createDepth(nonCoherentAtomSize,
physicalDeviceMemoryProperties, physicalDeviceMemoryProperties,
imageExtent.width, imageExtent.width,
imageExtent.height, imageExtent.height,
depthFormat, depthFormat,
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
arrayLayers,
&depthImage, &depthImage,
&depthMemory, &depthMemory,
&depthImageView); &depthImageView);
@ -490,6 +482,7 @@ int main()
1024, 1024,
depthFormat, depthFormat,
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
shadowArrayLayers,
&shadowDepthImage, &shadowDepthImage,
&shadowDepthMemory, &shadowDepthMemory,
&shadowDepthImageView); &shadowDepthImageView);
@ -498,12 +491,12 @@ int main()
VkImageViewCreateInfo imageViewCreateInfo{ VkImageViewCreateInfo imageViewCreateInfo{
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
.image = shadowDepthImage, .image = shadowDepthImage,
.viewType = VK_IMAGE_VIEW_TYPE_2D, .viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY,
.format = depthFormat, .format = depthFormat,
.subresourceRange{ .subresourceRange{
.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
.levelCount = 1, .levelCount = 1,
.layerCount = 1 .layerCount = shadowArrayLayers
} }
}; };
VK_CHECK(vkCreateImageView(device, &imageViewCreateInfo, nullptr, &shadowDepthImageViewDepth)); VK_CHECK(vkCreateImageView(device, &imageViewCreateInfo, nullptr, &shadowDepthImageViewDepth));
@ -695,7 +688,7 @@ int main()
.subresourceRange{ .subresourceRange{
.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT, .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT,
.levelCount = 1, .levelCount = 1,
.layerCount = 1 .layerCount = shadowArrayLayers
} }
} }
}; };
@ -720,7 +713,7 @@ int main()
VkRenderingInfo shadowRenderingInfo{ VkRenderingInfo shadowRenderingInfo{
.sType = VK_STRUCTURE_TYPE_RENDERING_INFO, .sType = VK_STRUCTURE_TYPE_RENDERING_INFO,
.renderArea{ .extent{ .width = 1024, .height = 1024 } }, .renderArea{ .extent{ .width = 1024, .height = 1024 } },
.layerCount = 1, .layerCount = 6,
.colorAttachmentCount = 0, .colorAttachmentCount = 0,
.pDepthAttachment = &shadowDepthRenderingAttachmentInfo, .pDepthAttachment = &shadowDepthRenderingAttachmentInfo,
.pStencilAttachment = &shadowDepthRenderingAttachmentInfo, .pStencilAttachment = &shadowDepthRenderingAttachmentInfo,
@ -794,7 +787,7 @@ int main()
.subresourceRange{ .subresourceRange{
.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT, .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT,
.levelCount = 1, .levelCount = 1,
.layerCount = 1 .layerCount = shadowArrayLayers
} }
} }
}; };