Skip to content

Commit

Permalink
Declare YUV formats
Browse files Browse the repository at this point in the history
  • Loading branch information
corporateshark committed Jul 2, 2024
1 parent 2c4be77 commit 27484fd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lvk/LVK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ namespace {
struct TextureFormatProperties {
const lvk::Format format = lvk::Format_Invalid;
const uint8_t bytesPerBlock : 5 = 1;
const uint8_t blockWidth : 4 = 1;
const uint8_t blockHeight : 4 = 1;
const uint8_t blockWidth : 3 = 1;
const uint8_t blockHeight : 3 = 1;
const uint8_t minBlocksX : 2 = 1;
const uint8_t minBlocksY : 2 = 1;
const bool depth : 1 = false;
const bool stencil : 1 = false;
const bool compressed : 1 = false;
const uint8_t numPlanes : 2 = 1;
};

#define PROPS(fmt, bpb, ...) \
Expand Down Expand Up @@ -81,6 +82,8 @@ static constexpr TextureFormatProperties properties[] = {
PROPS(Z_F32, 4, .depth = true),
PROPS(Z_UN24_S_UI8, 4, .depth = true, .stencil = true),
PROPS(Z_F32_S_UI8, 5, .depth = true, .stencil = true),
PROPS(YUV_NV12, 24, .blockWidth = 4, .blockHeight = 4, .compressed = true, .numPlanes = 2), // Subsampled 420
PROPS(YUV_420p, 24, .blockWidth = 4, .blockHeight = 4, .compressed = true, .numPlanes = 3), // Subsampled 420
};

bool initVulkanContextWithSwapchain(std::unique_ptr<lvk::VulkanContext>& ctx,
Expand Down Expand Up @@ -128,7 +131,7 @@ void* createCocoaWindowView(GLFWwindow* window);
#endif

static_assert(sizeof(TextureFormatProperties) <= sizeof(uint32_t));
static_assert(LVK_ARRAY_NUM_ELEMENTS(properties) == lvk::Format_Z_F32_S_UI8 + 1);
static_assert(LVK_ARRAY_NUM_ELEMENTS(properties) == lvk::Format_YUV_420p + 1);

bool lvk::isDepthOrStencilFormat(lvk::Format format) {
return properties[format].depth || properties[format].stencil;
Expand Down Expand Up @@ -173,7 +176,7 @@ uint32_t lvk::getTextureBytesPerLayer(uint32_t width, uint32_t height, lvk::Form
const uint32_t levelWidth = std::max(width >> level, 1u);
const uint32_t levelHeight = std::max(height >> level, 1u);

const auto props = properties[format];
const TextureFormatProperties props = properties[format];

if (!props.compressed) {
return props.bytesPerBlock * levelWidth * levelHeight;
Expand Down
3 changes: 3 additions & 0 deletions lvk/LVK.h
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,9 @@ enum Format : uint8_t {
Format_Z_F32,
Format_Z_UN24_S_UI8,
Format_Z_F32_S_UI8,

Format_YUV_NV12,
Format_YUV_420p,
};

enum LoadOp : uint8_t {
Expand Down
8 changes: 8 additions & 0 deletions lvk/vulkan/VulkanUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ VkFormat lvk::formatToVkFormat(lvk::Format format) {
return VK_FORMAT_D24_UNORM_S8_UINT;
case lvk::Format_Z_F32_S_UI8:
return VK_FORMAT_D32_SFLOAT_S8_UINT;
case lvk::Format_YUV_NV12:
return VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;
case lvk::Format_YUV_420p:
return VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM;
}
#if defined(_MSC_VER)
LVK_ASSERT_MSG(false, "TextureFormat value not handled: %d", (int)format);
Expand Down Expand Up @@ -239,6 +243,10 @@ lvk::Format lvk::vkFormatToFormat(VkFormat format) {
return Format_Z_F32;
case VK_FORMAT_D32_SFLOAT_S8_UINT:
return Format_Z_F32_S_UI8;
case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
return Format_YUV_NV12;
case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
return Format_YUV_420p;
default:;
}
LVK_ASSERT_MSG(false, "VkFormat value not handled: %d", (int)format);
Expand Down

0 comments on commit 27484fd

Please sign in to comment.