Skip to content

Commit 2ffab22

Browse files
committed
Volk
1 parent 2e269ec commit 2ffab22

8 files changed

Lines changed: 56 additions & 29 deletions

File tree

cmake/findPackages.cmake

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ set(SYSTEM_DIRECTORY_FLAG $<IF:$<OR:$<COMPILE_LANG_AND_ID:C,MSVC>,$<COMPILE_LANG
66
macro(getLatestPackageVersion gitRepo versionSplat)
77
find_package(Git 2.18 REQUIRED)
88
if (WIN32)
9-
execute_process(COMMAND powershell -command "((& '${GIT_EXECUTABLE}' -c 'versionsort.suffix=-' ls-remote --exit-code --refs --sort=version:refname --tags ${gitRepo} '${versionSplat}' | Select-Object -Last 1) -Split '/')[2]" OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE LATEST_RELEASE)
9+
execute_process(COMMAND powershell -command "((& '${GIT_EXECUTABLE}' -c 'versionsort.suffix=-' ls-remote --exit-code --refs --sort=version:refname --tags ${gitRepo} '${versionSplat}' | Select-Object -Last 1) -Split '/')[2]" OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE _LUNA_PACKAGE_LATEST_RELEASE_VERSION)
1010
else ()
11-
execute_process(COMMAND ${GIT_EXECUTABLE} -c "versionsort.suffix=-" ls-remote --exit-code --refs --sort=version:refname --tags ${gitRepo} "${versionSplat}" COMMAND tail --lines=1 COMMAND cut --delimiter=/ --fields=3 COMMAND tr -d "\n" OUTPUT_VARIABLE LATEST_RELEASE)
11+
execute_process(COMMAND ${GIT_EXECUTABLE} -c "versionsort.suffix=-" ls-remote --exit-code --refs --sort=version:refname --tags ${gitRepo} "${versionSplat}" COMMAND tail --lines=1 COMMAND cut --delimiter=/ --fields=3 COMMAND tr -d "\n" OUTPUT_VARIABLE _LUNA_PACKAGE_LATEST_RELEASE_VERSION)
1212
endif ()
1313
endmacro()
1414

@@ -18,7 +18,7 @@ macro(makePackageAvailable gitRepo versionSplat packageName)
1818
FetchContent_Declare(
1919
${packageName}
2020
GIT_REPOSITORY ${gitRepo}
21-
GIT_TAG ${LATEST_RELEASE}
21+
GIT_TAG ${_LUNA_PACKAGE_LATEST_RELEASE_VERSION}
2222
GIT_SHALLOW TRUE
2323
GIT_PROGRESS TRUE
2424
EXCLUDE_FROM_ALL
@@ -28,50 +28,41 @@ macro(makePackageAvailable gitRepo versionSplat packageName)
2828
FetchContent_MakeAvailable(${packageName})
2929
endmacro()
3030

31-
macro(findVulkan)
32-
find_package(Vulkan QUIET)
31+
function(findVulkan)
32+
makePackageAvailable(https://github.com/zeux/volk.git vulkan-sdk-1.4.*.* Vulkan COMPONENTS volk QUIET)
3333
add_library(VulkanLibrary INTERFACE)
34-
if (NOT Vulkan_FOUND)
35-
makePackageAvailable(https://github.com/KhronosGroup/Vulkan-Headers.git v1.4.* Headers)
36-
makePackageAvailable(https://github.com/KhronosGroup/Vulkan-Loader.git v1.4.* Loader)
34+
target_link_libraries(VulkanLibrary INTERFACE $<IF:$<BOOL:${Vulkan_volk_FOUND}>,Vulkan,volk>::volk)
35+
target_compile_options(VulkanLibrary INTERFACE $<$<BOOL:${LUNA_DEFINE_VK_NO_PROTOTYPES}>:$<IF:$<OR:$<COMPILE_LANG_AND_ID:C,MSVC>,$<COMPILE_LANG_AND_ID:CXX,MSVC>>,/DVK_NO_PROTOTYPES,-DVK_NO_PROTOTYPES>>)
36+
endfunction()
3737

38-
target_compile_options(VulkanLibrary INTERFACE "SHELL:${SYSTEM_DIRECTORY_FLAG} ${VULKAN_LOADER_SOURCE_DIR}/loader")
39-
target_link_libraries(VulkanLibrary INTERFACE Vulkan::Loader)
40-
target_include_directories(VulkanLibrary INTERFACE ${VULKAN_HEADERS_SOURCE_DIR}/include ${VULKAN_LOADER_SOURCE_DIR}/loader)
41-
else ()
42-
target_link_libraries(VulkanLibrary INTERFACE Vulkan::Vulkan)
43-
target_include_directories(VulkanLibrary INTERFACE ${Vulkan_INCLUDE_DIRS})
44-
endif ()
45-
endmacro()
46-
47-
macro(findSDL3)
38+
function(findSDL3)
4839
makePackageAvailable(https://github.com/libsdl-org/SDL.git release-3.*.* SDL3 CONFIG)
49-
endmacro()
40+
endfunction()
5041

51-
macro(fetchVMA)
42+
function(fetchVMA)
5243
check_include_file("vk_mem_alloc.h" VMA_FOUND)
5344
add_library(VMA INTERFACE)
5445
if (NOT VMA_FOUND)
5546
makePackageAvailable(https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator.git v3.*.* vma)
5647
target_compile_options(VMA INTERFACE "SHELL:${SYSTEM_DIRECTORY_FLAG} ${vma_SOURCE_DIR}/include")
5748
target_include_directories(VMA INTERFACE "${vma_SOURCE_DIR}/include")
5849
endif ()
59-
endmacro()
50+
endfunction()
6051

61-
macro(fetchCglm)
52+
function(fetchCglm)
6253
check_include_file("cglm/cglm.h" cglm_FOUND)
6354
if (NOT cglm_FOUND)
6455
makePackageAvailable(https://github.com/recp/cglm.git v0.*.* cglm)
6556
else ()
6657
add_library(cglm INTERFACE)
6758
endif ()
68-
endmacro()
59+
endfunction()
6960

70-
macro(fetchLodePNG)
61+
function(fetchLodePNG)
7162
file(DOWNLOAD https://raw.githubusercontent.com/lvandeve/lodepng/refs/heads/master/lodepng.cpp ${CMAKE_BINARY_DIR}/_deps/lodepng/lodepng.c)
7263
file(DOWNLOAD https://raw.githubusercontent.com/lvandeve/lodepng/refs/heads/master/lodepng.h ${CMAKE_BINARY_DIR}/_deps/lodepng/lodepng.h)
7364
add_library(LodePNG INTERFACE)
7465
target_compile_options(LodePNG INTERFACE "SHELL:${SYSTEM_DIRECTORY_FLAG} ${CMAKE_BINARY_DIR}/_deps/lodepng")
7566
target_sources(LodePNG INTERFACE "${CMAKE_BINARY_DIR}/_deps/lodepng/lodepng.c")
7667
target_include_directories(LodePNG INTERFACE "${CMAKE_BINARY_DIR}/_deps/lodepng")
77-
endmacro()
68+
endfunction()

cmake/options.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ option(LUNA_ENABLE_LTO "Enable LTO on release builds, which can increase perform
33

44
option(LUNA_WARNINGS_ARE_FATAL "Treat warnings as errors, causing the build to fail if any warnings are present" ON)
55

6+
option(LUNA_DEFINE_VK_NO_PROTOTYPES "Define the `VK_NO_PROTOTYPES` macro, which allows the application to include <vulkan/vulkan_core.h> instead of <volk.h>" ON)
7+
68
option(LUNA_EXAMPLES "Enable building of example projects" OFF)
79
option(LUNA_EXAMPLE_ALL "Enable all example project targets" ON)
810
option(LUNA_EXAMPLE_HelloTriangle "Enable the HelloTriangle example project target" ON)

include/luna/lunaTypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#ifndef LUNATYPES_H
66
#define LUNATYPES_H
77

8-
#include <vulkan/vulkan.h>
8+
#include <volk.h>
99

1010
#ifdef __cplusplus
1111
extern "C"

src/headers/luna/core/Fence.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#pragma once
66

7-
#include <vulkan/vulkan_core.h>
7+
#include <volk.h>
88

99
namespace luna::core
1010
{

src/headers/luna/core/Luna.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include <atomic>
88
#include <vector>
9-
#include <vulkan/vulkan.h>
9+
#include <volk.h>
1010

1111
namespace luna::helpers
1212
{

src/headers/luna/core/Semaphore.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#pragma once
66

7-
#include <vulkan/vulkan_core.h>
7+
#include <volk.h>
88

99
namespace luna::core
1010
{

src/luna/core/Device.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,46 @@ Device::Device(const LunaDeviceCreationInfo2 &creationInfo)
150150
.pEnabledFeatures = &creationInfo.requiredFeatures.features,
151151
};
152152
CHECK_RESULT_THROW(vkCreateDevice(physicalDevice_, &createInfo, nullptr, &logicalDevice_));
153+
volkLoadDevice(logicalDevice_);
153154

154155
vkGetDeviceQueue(logicalDevice_, familyIndices_.graphics, 0, &familyQueues_.graphics);
155156
vkGetDeviceQueue(logicalDevice_, familyIndices_.transfer, 0, &familyQueues_.transfer);
156157
vkGetDeviceQueue(logicalDevice_, familyIndices_.presentation, 0, &familyQueues_.presentation);
157158

159+
const VmaVulkanFunctions vmaVulkanFunctions = {
160+
.vkGetInstanceProcAddr = vkGetInstanceProcAddr,
161+
.vkGetDeviceProcAddr = vkGetDeviceProcAddr,
162+
.vkGetPhysicalDeviceProperties = vkGetPhysicalDeviceProperties,
163+
.vkGetPhysicalDeviceMemoryProperties = vkGetPhysicalDeviceMemoryProperties,
164+
.vkAllocateMemory = vkAllocateMemory,
165+
.vkFreeMemory = vkFreeMemory,
166+
.vkMapMemory = vkMapMemory,
167+
.vkUnmapMemory = vkUnmapMemory,
168+
.vkFlushMappedMemoryRanges = vkFlushMappedMemoryRanges,
169+
.vkInvalidateMappedMemoryRanges = vkInvalidateMappedMemoryRanges,
170+
.vkBindBufferMemory = vkBindBufferMemory,
171+
.vkBindImageMemory = vkBindImageMemory,
172+
.vkGetBufferMemoryRequirements = vkGetBufferMemoryRequirements,
173+
.vkGetImageMemoryRequirements = vkGetImageMemoryRequirements,
174+
.vkCreateBuffer = vkCreateBuffer,
175+
.vkDestroyBuffer = vkDestroyBuffer,
176+
.vkCreateImage = vkCreateImage,
177+
.vkDestroyImage = vkDestroyImage,
178+
.vkCmdCopyBuffer = vkCmdCopyBuffer,
179+
.vkGetBufferMemoryRequirements2KHR = vkGetBufferMemoryRequirements2KHR,
180+
.vkGetImageMemoryRequirements2KHR = vkGetImageMemoryRequirements2KHR,
181+
.vkBindBufferMemory2KHR = vkBindBufferMemory2KHR,
182+
.vkBindImageMemory2KHR = vkBindImageMemory2KHR,
183+
.vkGetPhysicalDeviceMemoryProperties2KHR = vkGetPhysicalDeviceMemoryProperties2KHR,
184+
.vkGetDeviceBufferMemoryRequirements = vkGetDeviceBufferMemoryRequirements,
185+
.vkGetDeviceImageMemoryRequirements = vkGetDeviceImageMemoryRequirements,
186+
// .vkGetMemoryWin32HandleKHR = vkGetMemoryWin32HandleKHR,
187+
};
158188
const VmaAllocatorCreateInfo allocationCreateInfo = {
159189
.flags = VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT,
160190
.physicalDevice = physicalDevice_,
161191
.device = logicalDevice_,
192+
.pVulkanFunctions = &vmaVulkanFunctions,
162193
.instance = instance,
163194
.vulkanApiVersion = apiVersion,
164195
};

src/luna/core/Instance.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ VkResult lunaCreateInstance(const LunaInstanceCreationInfo *creationInfo)
238238
enabledLayers.emplace_back("VK_LAYER_KHRONOS_validation");
239239
}
240240

241+
CHECK_RESULT_RETURN(volkInitialize());
242+
241243
const VkApplicationInfo vulkanApplicationInfo = {
242244
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
243245
.apiVersion = creationInfo->apiVersion,
@@ -251,6 +253,7 @@ VkResult lunaCreateInstance(const LunaInstanceCreationInfo *creationInfo)
251253
.ppEnabledExtensionNames = creationInfo->extensionNames,
252254
};
253255
CHECK_RESULT_RETURN(vkCreateInstance(&createInfo, nullptr, &luna::core::instance));
256+
volkLoadInstanceOnly(luna::core::instance);
254257
return VK_SUCCESS;
255258
}
256259
VkResult lunaDestroyInstance()

0 commit comments

Comments
 (0)