Skip to content

Commit

Permalink
Merge branch 'yuv'
Browse files Browse the repository at this point in the history
  • Loading branch information
corporateshark committed Jul 4, 2024
2 parents a7015c6 + b194171 commit e8fe61a
Show file tree
Hide file tree
Showing 9 changed files with 794 additions and 48 deletions.
31 changes: 27 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,12 +131,16 @@ 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;
}

uint32_t lvk::getNumImagePlanes(lvk::Format format) {
return properties[format].numPlanes;
}

uint32_t lvk::getVertexFormatSize(lvk::VertexFormat format) {
// clang-format off
#define SIZE4(LVKBaseType, BaseType) \
Expand Down Expand Up @@ -173,7 +180,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 All @@ -186,6 +193,22 @@ uint32_t lvk::getTextureBytesPerLayer(uint32_t width, uint32_t height, lvk::Form
return widthInBlocks * heightInBlocks * props.bytesPerBlock;
}

uint32_t lvk::getTextureBytesPerPlane(uint32_t width, uint32_t height, lvk::Format format, uint32_t plane) {
const TextureFormatProperties props = properties[format];

LVK_ASSERT(plane < props.numPlanes);

switch (format) {
case Format_YUV_NV12:
return width * height / (plane + 1);
case Format_YUV_420p:
return width * height / (plane ? 4 : 1);
default:;
}

return getTextureBytesPerLayer(width, height, format, 0);
}

uint32_t lvk::calcNumMipLevels(uint32_t width, uint32_t height) {
assert(width > 0);
assert(height > 0);
Expand Down
5 changes: 5 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 Expand Up @@ -934,8 +937,10 @@ struct ContextConfig {
};

[[nodiscard]] bool isDepthOrStencilFormat(lvk::Format format);
[[nodiscard]] uint32_t getNumImagePlanes(lvk::Format format);
[[nodiscard]] uint32_t calcNumMipLevels(uint32_t width, uint32_t height);
[[nodiscard]] uint32_t getTextureBytesPerLayer(uint32_t width, uint32_t height, lvk::Format format, uint32_t level);
[[nodiscard]] uint32_t getTextureBytesPerPlane(uint32_t width, uint32_t height, lvk::Format format, uint32_t plane);
[[nodiscard]] uint32_t getVertexFormatSize(lvk::VertexFormat format);
void logShaderSource(const char* text);

Expand Down
Loading

0 comments on commit e8fe61a

Please sign in to comment.