Skip to content

Commit

Permalink
src: Add vk_format_utils.h to UtilityHeaders
Browse files Browse the repository at this point in the history
The library originates from Vulkan-ValidationLayers, but is being moved
into this repo to make it easier for others to use it.

The library has also been modified to be header only and C compatible,
which allows more developers to be able to use it. This does require some
changes, but only affects the vkuFormatElementSize and vkuFormatTexelSize
functions which used default parameters. Two new functions,
vkuFormatElementSizeWithAspect and vkuFormatTexelSizeWithAspect have been
added to handle the non-default image aspect case (the default was COLOR_BIT).

Renaming was done using the following convention:
* public header files begin with `vk_`
* enums begin with VKU_FORMAT_
* functions begin with vku
  • Loading branch information
charles-lunarg authored and juan-lunarg committed Sep 11, 2023
1 parent 2f464cf commit c903525
Show file tree
Hide file tree
Showing 12 changed files with 3,660 additions and 0 deletions.
1 change: 1 addition & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ static_library("vulkan_layer_settings") {
"include/vulkan/layer/vk_layer_settings.hpp",
"include/vulkan/layer/vk_layer_settings_ext.h",
"include/vulkan/utility/vk_dispatch_table.h",
"include/vulkan/utility/vk_format_utils.h",
"include/vulkan/vk_enum_string_helper.h",
"src/layer/layer_settings_manager.cpp",
"src/layer/layer_settings_manager.hpp",
Expand Down
2 changes: 2 additions & 0 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.19")
target_sources(VulkanUtilityHeaders PRIVATE
vulkan/utility/vk_dispatch_table.h
vulkan/vk_enum_string_helper.h
vulkan/utility/vk_format_utils.h
)
endif()

Expand All @@ -31,3 +32,4 @@ endif()
target_link_Libraries(VulkanUtilityHeaders INTERFACE Vulkan::Headers)

target_include_directories(VulkanUtilityHeaders INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)

2,264 changes: 2,264 additions & 0 deletions include/vulkan/utility/vk_format_utils.h

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions scripts/generate_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def RunGenerators(api: str, registry: str, targetFilter: str) -> None:
from generators.base_generator import BaseGeneratorOptions
from generators.dispatch_table_generator import DispatchTableOutputGenerator
from generators.enum_string_helper_generator import EnumStringHelperOutputGenerator
from generators.format_utils_generator import FormatUtilsOutputGenerator

# Build up a list of all generators and custom options
generators = {
Expand All @@ -35,6 +36,10 @@ def RunGenerators(api: str, registry: str, targetFilter: str) -> None:
'generator' : EnumStringHelperOutputGenerator,
'directory' : 'include/vulkan',
},
'vk_format_utils.h' : {
'generator' : FormatUtilsOutputGenerator,
'directory' : 'include/vulkan/utility',
},
}

if (targetFilter and targetFilter not in generators.keys()):
Expand Down
662 changes: 662 additions & 0 deletions scripts/generators/format_utils_generator.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
add_subdirectory(layer)
add_subdirectory(generated)
add_subdirectory(vk_dispatch_table)
add_subdirectory(format_utils)
2 changes: 2 additions & 0 deletions tests/add_subdirectory/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ target_sources(add_subdirectory_example PRIVATE
vk_dispatch_table.c
vk_enum_string_helper.c
vk_layer_settings.c
vk_format_utils.c
vk_format_utils_2.c # Need two translation units to test if header file behaves correctly.
)

target_link_libraries(add_subdirectory_example PRIVATE
Expand Down
17 changes: 17 additions & 0 deletions tests/add_subdirectory/vk_format_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2023 The Khronos Group Inc.
// Copyright 2023 Valve Corporation
// Copyright 2023 LunarG, Inc.
//
// SPDX-License-Identifier: Apache-2.0
#include <vulkan/utility/vk_format_utils.h>

bool check_format_utils() {
vkuGetPlaneIndex(VK_IMAGE_ASPECT_PLANE_1_BIT);
vkuFormatHasGreen(VK_FORMAT_R8G8B8A8_UNORM);
vkuFormatElementSizeWithAspect(VK_FORMAT_ASTC_5x4_SRGB_BLOCK, VK_IMAGE_ASPECT_STENCIL_BIT);
struct VKU_FORMAT_INFO f = vkuGetFormatInfo(VK_FORMAT_R8G8B8A8_SRGB);
if (f.component_count != 4) {
return false;
}
return true;
}
18 changes: 18 additions & 0 deletions tests/add_subdirectory/vk_format_utils_2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2023 The Khronos Group Inc.
// Copyright 2023 Valve Corporation
// Copyright 2023 LunarG, Inc.
//
// SPDX-License-Identifier: Apache-2.0
#include <vulkan/utility/vk_format_utils.h>

// Need two translation units to test if header file behaves correctly.
bool check_format_utils_2() {
vkuGetPlaneIndex(VK_IMAGE_ASPECT_PLANE_1_BIT);
vkuFormatHasGreen(VK_FORMAT_R8G8B8A8_UNORM);
vkuFormatElementSizeWithAspect(VK_FORMAT_ASTC_5x4_SRGB_BLOCK, VK_IMAGE_ASPECT_STENCIL_BIT);
struct VKU_FORMAT_INFO f = vkuGetFormatInfo(VK_FORMAT_R8G8B8A8_SRGB);
if (f.component_count != 4) {
return false;
}
return true;
}
1 change: 1 addition & 0 deletions tests/find_package/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ target_sources(find_package_example PRIVATE
${CMAKE_CURRENT_LIST_DIR}/../add_subdirectory/vk_layer_settings.c
${CMAKE_CURRENT_LIST_DIR}/../add_subdirectory/vk_enum_string_helper.c
${CMAKE_CURRENT_LIST_DIR}/../add_subdirectory/vk_dispatch_table.c
${CMAKE_CURRENT_LIST_DIR}/../add_subdirectory/vk_format_utils.c
)

# NOTE: Because VulkanHeaders is a PUBLIC dependency it needs to be found prior to VulkanUtilityLibraries
Expand Down
25 changes: 25 additions & 0 deletions tests/format_utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2023 The Khronos Group Inc.
# Copyright 2023 Valve Corporation
# Copyright 2023 LunarG, Inc.
#
# SPDX-License-Identifier: Apache-2.0

find_package(GTest REQUIRED CONFIG)
find_package(magic_enum REQUIRED CONFIG)

include(GoogleTest)

add_executable(test_format_utils test_formats.cpp)

target_link_libraries(test_format_utils PRIVATE
GTest::gtest
GTest::gtest_main
magic_enum::magic_enum
Vulkan::UtilityHeaders
)

if(${CMAKE_C_COMPILER_ID} MATCHES "(GNU|Clang)")
add_compile_options(-Wpedantic -Wall -Wextra -Werror)
endif()

gtest_discover_tests(test_format_utils)
Loading

0 comments on commit c903525

Please sign in to comment.