rhi_helpers.cpp

cross platform rendering playground

src/backend/vulkan/rhi_helpers.cpp

2.5 KB
#include <volk.h>

#include <ktx.h>

#include "backend/vulkan/vk_api.h"
#include "backend/vulkan/vk_conversion.h"

namespace vk {
bool format_supports_sampling(rhi::Device &device, VkFormat format) {
    VkFormatProperties props{};
    vkGetPhysicalDeviceFormatProperties(device.physical, format, &props);
    return (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) != 0;
}
} // namespace vk

namespace rhi {
using namespace vk;

uintptr_t image_id(const Image &img) {
    return reinterpret_cast<uintptr_t>(img._handle);
}

uintptr_t buffer_id(const Buffer &buffer) {
    return reinterpret_cast<uintptr_t>(buffer._handle);
}

uintptr_t sampler_id(const Sampler &sampler) {
    return reinterpret_cast<uintptr_t>(sampler._handle);
}
uintptr_t accel_id(const AccelStruct &as) {
    return reinterpret_cast<uintptr_t>(as.handle);
}

bool valid(const Image &img) {
    return img._handle != VK_NULL_HANDLE;
}
bool valid(const Buffer &buffer) {
    return buffer._handle != VK_NULL_HANDLE;
}
bool valid(const CmdBuffer &cmd) {
    return cmd.handle != VK_NULL_HANDLE;
}
bool valid(const CmdPool &pool) {
    return pool.handle != VK_NULL_HANDLE;
}
bool valid(const RenderPipeline &pipeline) {
    return pipeline._handle != VK_NULL_HANDLE;
}
bool valid(const ComputePipeline &pipeline) {
    return pipeline._handle != VK_NULL_HANDLE;
}
bool valid(const Sync *sync) {
    return sync != nullptr && sync->timeline != VK_NULL_HANDLE;
}
bool valid(const AccelStruct &as) {
    return as.handle != VK_NULL_HANDLE;
}
bool valid(const Sampler &sampler) {
    return sampler._handle != VK_NULL_HANDLE;
}
bool valid(const QueryPool &pool) {
    return pool._handle != VK_NULL_HANDLE;
}
bool valid(const Swapchain &swapchain) {
    return swapchain.handle != VK_NULL_HANDLE;
}

bool valid(const ImageView &view) {
    return view._handle != VK_NULL_HANDLE && view.slot != UINT64_MAX;
}

bool valid(const BufferView &view) {
    return view.buffer != nullptr && view.slot != UINT64_MAX;
}

rhi::ImageFormat image_format_from_vk(u32 format) {
    return vk::image_format_from_vk(static_cast<VkFormat>(format));
}

rhi::ImageFormat preferred_compressed_format(Device &device, bool srgb) {
    if (srgb) {
        if (format_supports_sampling(device, VK_FORMAT_BC7_SRGB_BLOCK)) {
            return ImageFormat::BC7_SRGB;
        }
        return ImageFormat::RGBA8_SRGB;
    }
    if (format_supports_sampling(device, VK_FORMAT_BC7_UNORM_BLOCK)) {
        return ImageFormat::BC7_UNORM;
    }
    return ImageFormat::RGBA8_UNORM;
}

} // namespace rhi