Add constants over hardcoded values

This commit is contained in:
Georg Hagen
2024-12-30 21:18:53 +01:00
parent a290bcf11b
commit c858744d44

View File

@@ -12,11 +12,19 @@
namespace OpenVulkano::Vulkan namespace OpenVulkano::Vulkan
{ {
namespace
{
constexpr uint32_t SAMPLE_SIZE_WIDTH = 5;
constexpr uint32_t SAMPLE_SIZE_HEIGHT = 5;
constexpr uint32_t SAMPLE_SIZE = SAMPLE_SIZE_WIDTH * SAMPLE_SIZE_HEIGHT;
constexpr uint32_t SAMPLE_CENTER = SAMPLE_SIZE / 2;
}
void DepthBufferQuery::Init() void DepthBufferQuery::Init()
{ {
auto device = renderer.GetContext().device->device; auto device = renderer.GetContext().device->device;
vk::BufferCreateInfo bufferInfo = { {}, 25 * sizeof(float), vk::BufferUsageFlagBits::eTransferDst }; vk::BufferCreateInfo bufferInfo = { {}, SAMPLE_SIZE * sizeof(float), vk::BufferUsageFlagBits::eTransferDst };
bufferDepth = device.createBuffer(bufferInfo); bufferDepth = device.createBuffer(bufferInfo);
const vk::MemoryRequirements memRequirements = device.getBufferMemoryRequirements(bufferDepth); const vk::MemoryRequirements memRequirements = device.getBufferMemoryRequirements(bufferDepth);
size_t size = memRequirements.size; size_t size = memRequirements.size;
@@ -45,20 +53,24 @@ namespace OpenVulkano::Vulkan
float DepthBufferQuery::GetQueriedValue() const float DepthBufferQuery::GetQueriedValue() const
{ {
if (cpuDepthBuffer[0] == -2) return -2; if (cpuDepthBuffer[0] == -2) return -2;
if (cpuDepthBuffer[12] > 0 && cpuDepthBuffer[12] < 1) return cpuDepthBuffer[12]; if (cpuDepthBuffer[SAMPLE_CENTER] > 0 && cpuDepthBuffer[SAMPLE_CENTER] < 1) return cpuDepthBuffer[SAMPLE_CENTER];
double val = 0; std::sort(cpuDepthBuffer, cpuDepthBuffer + SAMPLE_SIZE);
int validCount = 0; Logger::APP->info("Depth Values: ({}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {})",
for (int i = 0; i < 25; i++) cpuDepthBuffer[0], cpuDepthBuffer[1], cpuDepthBuffer[2], cpuDepthBuffer[3], cpuDepthBuffer[4],
cpuDepthBuffer[5], cpuDepthBuffer[6], cpuDepthBuffer[7], cpuDepthBuffer[8], cpuDepthBuffer[9],
cpuDepthBuffer[10], cpuDepthBuffer[11], cpuDepthBuffer[12], cpuDepthBuffer[13], cpuDepthBuffer[14],
cpuDepthBuffer[15], cpuDepthBuffer[16], cpuDepthBuffer[17], cpuDepthBuffer[18], cpuDepthBuffer[19],
cpuDepthBuffer[20], cpuDepthBuffer[21], cpuDepthBuffer[22], cpuDepthBuffer[23], cpuDepthBuffer[24]);
uint32_t start = UINT32_MAX, end = UINT32_MAX;
for (uint32_t i = 0; i < SAMPLE_SIZE; i++)
{ {
float f = cpuDepthBuffer[i]; float f = cpuDepthBuffer[i];
if (f > 0 && f < 1) if (f > 0 && start == UINT32_MAX) start = i;
{ if (f < 1 && end == UINT32_MAX) end = i;
val += f; else if (f >= 1) break;
validCount++;
} }
} if (start == UINT32_MAX || end == UINT32_MAX) return cpuDepthBuffer[SAMPLE_CENTER];
if (validCount == 0) return 1; // Prevent divide by 0 return cpuDepthBuffer[(start + end) / 2];
return val / validCount;
} }
vk::Offset3D DepthBufferQuery::GetCopyOffset() const vk::Offset3D DepthBufferQuery::GetCopyOffset() const
@@ -66,11 +78,11 @@ namespace OpenVulkano::Vulkan
vk::Extent3D depthExtent = renderer.GetContext().swapChain.GetCurrentDepthBuffer().extent; vk::Extent3D depthExtent = renderer.GetContext().swapChain.GetCurrentDepthBuffer().extent;
int32_t x = static_cast<int32_t>(depthQueryCoordinates.x * depthExtent.width); int32_t x = static_cast<int32_t>(depthQueryCoordinates.x * depthExtent.width);
x = std::min<int32_t>(depthExtent.width - 5, std::max(0, x)); x = std::min<int32_t>(depthExtent.width - SAMPLE_SIZE_WIDTH, std::max(0, x));
int32_t y = static_cast<int32_t>(depthQueryCoordinates.y * depthExtent.height); int32_t y = static_cast<int32_t>(depthQueryCoordinates.y * depthExtent.height);
y = std::min<int32_t>(depthExtent.height - 5, std::max(0, y));
y = std::min<int32_t>(depthExtent.height - SAMPLE_SIZE_HEIGHT, std::max(0, y));
return { x, y, 0 }; return { x, y, 0 };
} }
@@ -78,11 +90,11 @@ namespace OpenVulkano::Vulkan
{ {
if (!copyDepthBuffer) return; if (!copyDepthBuffer) return;
copyDepthBuffer = false; copyDepthBuffer = false;
std::fill(cpuDepthBuffer, cpuDepthBuffer + 25, -2.0f); // Invalidate data in buffer to allow detecting if copy is done std::fill(cpuDepthBuffer, cpuDepthBuffer + SAMPLE_SIZE, -2.0f); // Invalidate data in buffer to allow detecting if copy is done
const vk::ImageAspectFlags aspectMask = vk::ImageAspectFlagBits::eDepth | vk::ImageAspectFlagBits::eStencil; const vk::ImageAspectFlags aspectMask = vk::ImageAspectFlagBits::eDepth | vk::ImageAspectFlagBits::eStencil;
const Image& depthBufferImage = renderer.GetContext().swapChain.GetCurrentDepthBuffer(); const Image& depthBufferImage = renderer.GetContext().swapChain.GetCurrentDepthBuffer();
constexpr vk::Extent3D copySize = { 5, 5, 1 }; constexpr vk::Extent3D copySize = { SAMPLE_SIZE_WIDTH, SAMPLE_SIZE_HEIGHT, 1 };
const vk::ImageSubresourceLayers layout = { vk::ImageAspectFlagBits::eDepth, 0, 0, 1 }; const vk::ImageSubresourceLayers layout = { vk::ImageAspectFlagBits::eDepth, 0, 0, 1 };
vk::BufferImageCopy imgCopy = { 0, 5, 5, layout, GetCopyOffset(), copySize }; vk::BufferImageCopy imgCopy = { 0, 5, 5, layout, GetCopyOffset(), copySize };