Skip to content

Commit

Permalink
Reworked TextureFormat properties; removed CMake target IGLLibrary
Browse files Browse the repository at this point in the history
  • Loading branch information
corporateshark committed Jul 13, 2023
1 parent 8051261 commit 1faabca
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 220 deletions.
21 changes: 11 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ endif()
find_package(Vulkan REQUIRED)

include_directories(.)
include_directories(src)

add_subdirectory(src/igl)
add_subdirectory(lvk)

if(IGL_DEPLOY_DEPS)
add_dependencies(IGLLibrary IGLDependencies)
add_dependencies(LVKLibrary IGLDependencies)
endif()

add_library(IGLstb third-party/deps/patches/stb_impl/stb_image.c third-party/deps/patches/stb_impl/stb_image_resize.c
Expand All @@ -97,8 +98,8 @@ set(MINILOG_RAW_OUTPUT ON CACHE BOOL "")
add_subdirectory(third-party/deps/src/minilog)
igl_set_folder(minilog "third-party")

target_link_libraries(IGLLibrary PUBLIC minilog)
target_include_directories(IGLLibrary PUBLIC "third-party/deps/src")
target_link_libraries(LVKLibrary PUBLIC minilog)
target_include_directories(LVKLibrary PUBLIC "third-party/deps/src")

if(IGL_WITH_SAMPLES)
include_directories("third-party/deps/src")
Expand Down Expand Up @@ -135,13 +136,13 @@ if(IGL_WITH_SAMPLES)
endif()

if(IGL_WITH_TRACY)
target_link_libraries(IGLLibrary PUBLIC TracyClient)
target_link_libraries(LVKLibrary PUBLIC TracyClient)
endif()

# ImGui
target_sources(IGLLibrary PRIVATE "${IGL_ROOT_DIR}/third-party/deps/src/imgui/imgui.cpp")
target_sources(IGLLibrary PRIVATE "${IGL_ROOT_DIR}/third-party/deps/src/imgui/imgui_demo.cpp")
target_sources(IGLLibrary PRIVATE "${IGL_ROOT_DIR}/third-party/deps/src/imgui/imgui_draw.cpp")
target_sources(IGLLibrary PRIVATE "${IGL_ROOT_DIR}/third-party/deps/src/imgui/imgui_tables.cpp")
target_sources(IGLLibrary PRIVATE "${IGL_ROOT_DIR}/third-party/deps/src/imgui/imgui_widgets.cpp")
target_include_directories(IGLLibrary PUBLIC "${IGL_ROOT_DIR}/third-party/deps/src/imgui")
target_sources(LVKLibrary PRIVATE "${IGL_ROOT_DIR}/third-party/deps/src/imgui/imgui.cpp")
target_sources(LVKLibrary PRIVATE "${IGL_ROOT_DIR}/third-party/deps/src/imgui/imgui_demo.cpp")
target_sources(LVKLibrary PRIVATE "${IGL_ROOT_DIR}/third-party/deps/src/imgui/imgui_draw.cpp")
target_sources(LVKLibrary PRIVATE "${IGL_ROOT_DIR}/third-party/deps/src/imgui/imgui_tables.cpp")
target_sources(LVKLibrary PRIVATE "${IGL_ROOT_DIR}/third-party/deps/src/imgui/imgui_widgets.cpp")
target_include_directories(LVKLibrary PUBLIC "${IGL_ROOT_DIR}/third-party/deps/src/imgui")
2 changes: 1 addition & 1 deletion lvk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ add_subdirectory(vulkan)
target_link_libraries(LVKLibrary PUBLIC LVKVulkan)

# temporary
target_link_libraries(LVKLibrary PUBLIC IGLLibrary)
target_link_libraries(LVKLibrary PUBLIC IGLVulkan)
109 changes: 109 additions & 0 deletions lvk/LVK.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* LightweightVK
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include "LVK.h"

namespace {

struct TextureFormatProperties {
const igl::TextureFormat format = igl::TextureFormat::Invalid;
const uint8_t bytesPerBlock : 5 = 1;
const uint8_t blockWidth : 4 = 1;
const uint8_t blockHeight : 4 = 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;
};

#define PROPS(fmt, bpb, ...) \
TextureFormatProperties{ .format = igl::TextureFormat::fmt, .bytesPerBlock = bpb, ##__VA_ARGS__ }

static constexpr TextureFormatProperties properties[] = {
PROPS(Invalid, 1),
PROPS(R_UN8, 1),
PROPS(R_UI16, 2),
PROPS(R_UN16, 2),
PROPS(R_F16, 2),
PROPS(R_F32, 4),
PROPS(RG_UN8, 2),
PROPS(RG_UI16, 4),
PROPS(RG_UN16, 4),
PROPS(RG_F16, 4),
PROPS(RG_F32, 8),
PROPS(RGBA_UN8, 4),
PROPS(RGBA_UI32, 16),
PROPS(RGBA_F16, 8),
PROPS(RGBA_F32, 16),
PROPS(RGBA_SRGB8, 4),
PROPS(BGRA_UN8, 4),
PROPS(BGRA_SRGB8, 4),
PROPS(ETC2_RGB8, 8, .blockWidth = 4, .blockHeight = 4, .compressed = true),
PROPS(ETC2_SRGB8, 8, .blockWidth = 4, .blockHeight = 4, .compressed = true),
PROPS(BC7_RGBA, 16, .blockWidth = 4, .blockHeight = 4, .compressed = true),
PROPS(Z_UN16, 2, .depth = true),
PROPS(Z_UN24, 3, .depth = true),
PROPS(Z_F32, 4, .depth = true),
PROPS(Z_UN24_S_UI8, 4, .depth = true, .stencil = true),
};

} // namespace

static_assert(sizeof(TextureFormatProperties) <= sizeof(uint32_t));
static_assert(IGL_ARRAY_NUM_ELEMENTS(properties) == igl::TextureFormat::Z_UN24_S_UI8 + 1);

bool lvk::isDepthOrStencilFormat(igl::TextureFormat format) {
return properties[format].depth || properties[format].stencil;
}

uint32_t lvk::getTextureBytesPerLayer(uint32_t width,
uint32_t height,
igl::TextureFormat format,
uint32_t level) {
const uint32_t levelWidth = std::max(width >> level, 1u);
const uint32_t levelHeight = std::max(height >> level, 1u);

const auto props = properties[format];

if (!props.compressed) {
return props.bytesPerBlock * levelWidth * levelHeight;
}

const uint32_t blockWidth = std::max((uint32_t)props.blockWidth, 1u);
const uint32_t blockHeight = std::max((uint32_t)props.blockHeight, 1u);
const uint32_t widthInBlocks = (levelWidth + props.blockWidth - 1) / props.blockWidth;
const uint32_t heightInBlocks = (levelHeight + props.blockHeight - 1) / props.blockHeight;
return widthInBlocks * heightInBlocks * props.bytesPerBlock;
}

uint32_t lvk::calcNumMipLevels(uint32_t width, uint32_t height) {
assert(width > 0);
assert(height > 0);

uint32_t levels = 1;

while ((width | height) >> levels) {
levels++;
}

return levels;
}

bool lvk::Assert(bool cond, const char* file, int line, const char* format, ...) {
if (!cond) {
va_list ap;
va_start(ap, format);
LLOGW("[LVK] Assertion failed in %s:%d: ", file, line);
LLOGW(format, ap);
va_end(ap);
assert(false);
}
return cond;
}
33 changes: 18 additions & 15 deletions lvk/LVK.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@

#define IGL_ARRAY_NUM_ELEMENTS(x) (sizeof(x) / sizeof((x)[0]))

namespace igl {
namespace lvk {

bool _IGLVerify(bool cond, const char* file, int line, const char* format, ...);
bool Assert(bool cond, const char* file, int line, const char* format, ...);

} // namespace igl
} // namespace lvk

// clang-format off
#if !defined(NDEBUG) && (defined(DEBUG) || defined(_DEBUG) || defined(__DEBUG))
#define IGL_VERIFY(cond) ::igl::_IGLVerify((cond), __FILE__, __LINE__, #cond)
#define IGL_VERIFY(cond) ::lvk::Assert((cond), __FILE__, __LINE__, #cond)
#define IGL_ASSERT(cond) (void)IGL_VERIFY(cond)
#define IGL_ASSERT_MSG(cond, format, ...) (void)::igl::_IGLVerify((cond), __FILE__, __LINE__, (format), ##__VA_ARGS__)
#define IGL_ASSERT_MSG(cond, format, ...) (void)::lvk::Assert((cond), __FILE__, __LINE__, (format), ##__VA_ARGS__)
#else
#define IGL_VERIFY(cond) (cond)
#define IGL_ASSERT(cond)
Expand Down Expand Up @@ -751,21 +751,24 @@ class IDevice {
const char* fs,
const char* debugNameFS,
Result* outResult = nullptr) {
auto VS = createShaderModule(igl::ShaderModuleDesc(vs, Stage_Vertex, debugNameVS), outResult);
auto FS = createShaderModule(igl::ShaderModuleDesc(fs, Stage_Fragment, debugNameFS), outResult);
return igl::ShaderStages(VS, FS);
auto VS = createShaderModule(ShaderModuleDesc(vs, Stage_Vertex, debugNameVS), outResult);
auto FS = createShaderModule(ShaderModuleDesc(fs, Stage_Fragment, debugNameFS), outResult);
return ShaderStages(VS, FS);
}

protected:
IDevice() = default;
};

bool isDepthOrStencilFormat(TextureFormat format);
} // namespace igl

namespace lvk {

bool isDepthOrStencilFormat(igl::TextureFormat format);
uint32_t calcNumMipLevels(uint32_t width, uint32_t height);
uint32_t getTextureBytesPerLayer(uint32_t texWidth,
uint32_t texHeight,
uint32_t texDepth,
TextureFormat texFormat,
uint32_t mipLevel);
uint32_t getTextureBytesPerLayer(uint32_t width,
uint32_t height,
igl::TextureFormat format,
uint32_t level);

} // namespace igl
} // namespace lvk
2 changes: 1 addition & 1 deletion lvk/vulkan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ lvk_set_folder(glslang-default-resource-limits "third-party/glslang")

find_package(Vulkan REQUIRED)

target_link_libraries(LVKVulkan PRIVATE IGLLibrary)
target_link_libraries(LVKVulkan PRIVATE LVKLibrary)
target_link_libraries(LVKVulkan PUBLIC glslang SPIRV glslang-default-resource-limits)
target_link_libraries(LVKVulkan PUBLIC Vulkan::Vulkan)

Expand Down
2 changes: 1 addition & 1 deletion samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ macro(ADD_DEMO app)
add_executable(${app} "${app}.cpp")
igl_set_cxxstd(${app} 20)
igl_set_folder(${app} ${PROJECT_NAME})
target_link_libraries(${app} PRIVATE IGLLibrary)
target_link_libraries(${app} PRIVATE LVKLibrary)
target_link_libraries(${app} PRIVATE bc7enc)
target_link_libraries(${app} PRIVATE meshoptimizer)
target_link_libraries(${app} PRIVATE tinyobjloader)
Expand Down
16 changes: 8 additions & 8 deletions samples/Tiny_MeshLarge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ void createShadowMap() {
.width = w,
.height = h,
.usage = igl::TextureUsageBits_Attachment | igl::TextureUsageBits_Sampled,
.numMipLevels = igl::calcNumMipLevels(w, h),
.numMipLevels = lvk::calcNumMipLevels(w, h),
.debugName = "Shadow map",
};
fbShadowMap_ = {
Expand All @@ -1055,7 +1055,7 @@ void createOffscreenFramebuffer() {
.width = w,
.height = h,
.usage = igl::TextureUsageBits_Attachment | igl::TextureUsageBits_Sampled,
.numMipLevels = igl::calcNumMipLevels(w, h),
.numMipLevels = lvk::calcNumMipLevels(w, h),
.debugName = "Offscreen framebuffer (d)",
};
if (kNumSamplesMSAA > 1) {
Expand All @@ -1075,7 +1075,7 @@ void createOffscreenFramebuffer() {
.width = w,
.height = h,
.usage = usage,
.numMipLevels = igl::calcNumMipLevels(w, h),
.numMipLevels = lvk::calcNumMipLevels(w, h),
.debugName = "Offscreen framebuffer (c)",
};
if (kNumSamplesMSAA > 1) {
Expand Down Expand Up @@ -1290,7 +1290,7 @@ void generateCompressedTexture(LoadedImage img) {

printf("...compressing texture to %s\n", img.compressedFileName.c_str());

const auto mipmapLevelCount = igl::calcNumMipLevels(img.w, img.h);
const auto mipmapLevelCount = lvk::calcNumMipLevels(img.w, img.h);

// Go over all generated mipmap and create a compressed texture
gli::texture2d::extent_type extents;
Expand Down Expand Up @@ -1464,7 +1464,7 @@ void loadCubemapTexture(const std::string& fileNameKTX, std::shared_ptr<ITexture
.width = width,
.height = height,
.usage = igl::TextureUsageBits_Sampled,
.numMipLevels = igl::calcNumMipLevels(texRef.extent().x, texRef.extent().y),
.numMipLevels = lvk::calcNumMipLevels(texRef.extent().x, texRef.extent().y),
.debugName = fileNameKTX.c_str(),
},
nullptr);
Expand All @@ -1485,7 +1485,7 @@ void loadCubemapTexture(const std::string& fileNameKTX, std::shared_ptr<ITexture
.height = height,
.numLayers = 6,
// if compression is enabled, upload all mip-levels
.numMipLevels = kEnableCompression ? igl::calcNumMipLevels(width, height) : 1u,
.numMipLevels = kEnableCompression ? lvk::calcNumMipLevels(width, height) : 1u,
};
tex->upload(texRefRange, data);

Expand All @@ -1504,7 +1504,7 @@ gli::texture_cube gliToCube(Bitmap& bmp) {

const gli::texture_cube::extent_type extents{w, h};

const auto miplevels = igl::calcNumMipLevels(w, h);
const uint32_t miplevels = lvk::calcNumMipLevels(w, h);

gli::texture_cube gliTexCube =
gli::texture_cube(gli::FORMAT_RGBA32_SFLOAT_PACK32, extents, miplevels);
Expand Down Expand Up @@ -1650,7 +1650,7 @@ std::shared_ptr<ITexture> createTexture(const LoadedImage& img) {
.width = img.w,
.height = img.h,
.usage = igl::TextureUsageBits_Sampled,
.numMipLevels = igl::calcNumMipLevels(img.w, img.h),
.numMipLevels = lvk::calcNumMipLevels(img.w, img.h),
.debugName = img.debugName.c_str(),
};
auto tex = device_->createTexture(desc, nullptr);
Expand Down
15 changes: 1 addition & 14 deletions src/igl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,6 @@

cmake_minimum_required(VERSION 3.16)

project(IGLLibrary CXX C)

file(GLOB SRC_FILES LIST_DIRECTORIES false RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
file(GLOB HEADER_FILES LIST_DIRECTORIES false RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.h)

add_library(IGLLibrary ${SRC_FILES} ${HEADER_FILES})

target_include_directories(IGLLibrary PUBLIC "${IGL_ROOT_DIR}/src")

igl_set_cxxstd(IGLLibrary 20)
igl_set_folder(IGLLibrary "IGL")
project(LVKLibrary CXX C)

add_subdirectory(vulkan)

target_link_libraries(IGLLibrary PUBLIC IGLVulkan)
target_link_libraries(IGLLibrary PRIVATE LVKLibrary)
Loading

0 comments on commit 1faabca

Please sign in to comment.