2026-02-21 19:41:21 -08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <vulkan/vulkan.h>
|
|
|
|
|
#include <vk_mem_alloc.h>
|
|
|
|
|
#include <cstdint>
|
2026-03-11 11:36:06 -07:00
|
|
|
#include <limits>
|
|
|
|
|
#include <cstdlib>
|
2026-02-21 19:41:21 -08:00
|
|
|
|
|
|
|
|
namespace wowee {
|
|
|
|
|
namespace rendering {
|
|
|
|
|
|
|
|
|
|
class VkContext;
|
|
|
|
|
|
|
|
|
|
struct AllocatedBuffer {
|
|
|
|
|
VkBuffer buffer = VK_NULL_HANDLE;
|
|
|
|
|
VmaAllocation allocation = VK_NULL_HANDLE;
|
|
|
|
|
VmaAllocationInfo info{};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct AllocatedImage {
|
|
|
|
|
VkImage image = VK_NULL_HANDLE;
|
|
|
|
|
VmaAllocation allocation = VK_NULL_HANDLE;
|
|
|
|
|
VkImageView imageView = VK_NULL_HANDLE;
|
|
|
|
|
VkExtent2D extent{};
|
|
|
|
|
VkFormat format = VK_FORMAT_UNDEFINED;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Buffer creation
|
|
|
|
|
AllocatedBuffer createBuffer(VmaAllocator allocator, VkDeviceSize size,
|
|
|
|
|
VkBufferUsageFlags usage, VmaMemoryUsage memoryUsage);
|
|
|
|
|
|
|
|
|
|
void destroyBuffer(VmaAllocator allocator, AllocatedBuffer& buffer);
|
|
|
|
|
|
|
|
|
|
// Image creation
|
|
|
|
|
AllocatedImage createImage(VkDevice device, VmaAllocator allocator,
|
|
|
|
|
uint32_t width, uint32_t height, VkFormat format,
|
|
|
|
|
VkImageUsageFlags usage, VkSampleCountFlagBits samples = VK_SAMPLE_COUNT_1_BIT,
|
|
|
|
|
uint32_t mipLevels = 1);
|
|
|
|
|
|
|
|
|
|
void destroyImage(VkDevice device, VmaAllocator allocator, AllocatedImage& image);
|
|
|
|
|
|
|
|
|
|
// Image layout transitions
|
|
|
|
|
void transitionImageLayout(VkCommandBuffer cmd, VkImage image,
|
|
|
|
|
VkImageLayout oldLayout, VkImageLayout newLayout,
|
|
|
|
|
VkPipelineStageFlags srcStage = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
|
|
|
|
|
VkPipelineStageFlags dstStage = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
|
|
|
|
|
|
|
|
|
|
// Staging upload helper — copies CPU data to a GPU-local buffer
|
|
|
|
|
AllocatedBuffer uploadBuffer(VkContext& ctx, const void* data, VkDeviceSize size,
|
|
|
|
|
VkBufferUsageFlags usage);
|
|
|
|
|
|
|
|
|
|
// Check VkResult and log on failure
|
2026-02-23 19:16:47 -08:00
|
|
|
inline bool vkCheck(VkResult result, [[maybe_unused]] const char* msg) {
|
2026-02-21 19:41:21 -08:00
|
|
|
if (result != VK_SUCCESS) {
|
|
|
|
|
// Caller should log the message
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-11 11:36:06 -07:00
|
|
|
// Environment variable utility functions
|
|
|
|
|
inline size_t envSizeMBOrDefault(const char* name, size_t defMb) {
|
|
|
|
|
const char* v = std::getenv(name);
|
|
|
|
|
if (!v || !*v) return defMb;
|
|
|
|
|
char* end = nullptr;
|
|
|
|
|
unsigned long long mb = std::strtoull(v, &end, 10);
|
|
|
|
|
if (end == v || mb == 0) return defMb;
|
|
|
|
|
if (mb > (std::numeric_limits<size_t>::max() / (1024ull * 1024ull))) return defMb;
|
|
|
|
|
return static_cast<size_t>(mb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline size_t envSizeOrDefault(const char* name, size_t defValue) {
|
|
|
|
|
const char* v = std::getenv(name);
|
|
|
|
|
if (!v || !*v) return defValue;
|
|
|
|
|
char* end = nullptr;
|
|
|
|
|
unsigned long long n = std::strtoull(v, &end, 10);
|
|
|
|
|
if (end == v || n == 0) return defValue;
|
|
|
|
|
return static_cast<size_t>(n);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-21 19:41:21 -08:00
|
|
|
} // namespace rendering
|
|
|
|
|
} // namespace wowee
|