Skip to content

Commit

Permalink
add support for using Vulkan-Hpp C++20 module
Browse files Browse the repository at this point in the history
  • Loading branch information
qbojj committed Jul 10, 2024
1 parent 8335d9d commit bfb0e4b
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 146 deletions.
20 changes: 13 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
list(APPEND CMAKE_PREFIX_PATH ./VulkanMemoryAllocator/install)
cmake_minimum_required(VERSION 3.6 FATAL_ERROR)

project(
Expand Down Expand Up @@ -25,9 +26,14 @@ target_include_directories(VulkanMemoryAllocator-Hpp
target_link_libraries(VulkanMemoryAllocator-Hpp
INTERFACE
Vulkan::Vulkan
VulkanMemoryAllocator::VulkanMemoryAllocator
GPUOpen::VulkanMemoryAllocator
)

option(VMA_BUILD_CXX_MODULE OFF)
if (${VMA_BUILD_CXX_MODULE})
add_subdirectory(CppModule)
endif()

install(
FILES
${CMAKE_CURRENT_SOURCE_DIR}/include/vk_mem_alloc_enums.hpp
Expand All @@ -51,9 +57,9 @@ install(
EXPORT VulkanMemoryAllocator-HppTargets
)

install(
EXPORT VulkanMemoryAllocator-HppTargets
FILE VulkanMemoryAllocator-HppTargets.cmake
NAMESPACE VulkanMemoryAllocator-Hpp::
DESTINATION lib/cmake/VulkanMemoryAllocator-Hpp
)
# install(
# EXPORT VulkanMemoryAllocator-HppTargets
# FILE VulkanMemoryAllocator-HppTargets.cmake
# NAMESPACE VulkanMemoryAllocator-Hpp::
# DESTINATION lib/cmake/VulkanMemoryAllocator-Hpp
# )
64 changes: 32 additions & 32 deletions Generate.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ record Entry(String name, String originalName) {}

content.append(processTemplate(ifdef, typedefMatcher.start(), """
namespace VMA_HPP_NAMESPACE {
VMA_EXPORT namespace VMA_HPP_NAMESPACE {
enum class $0$1 {
{{{e${name} = ${originalName}${,$}}}}
Expand All @@ -265,15 +265,15 @@ enum class $0$1 {
if (flagBits) {
content.append(processTemplate(ifdef, typedefMatcher.start(), """
namespace VULKAN_HPP_NAMESPACE {
VMA_EXPORT namespace VULKAN_HPP_NAMESPACE {
template<> struct FlagTraits<VMA_HPP_NAMESPACE::$0> {
static VULKAN_HPP_CONST_OR_CONSTEXPR bool isBitmask = true;
static VULKAN_HPP_CONST_OR_CONSTEXPR Flags<VMA_HPP_NAMESPACE::$0> allFlags =
{{{${ ^|} VMA_HPP_NAMESPACE::$0::e${name}${$;}}}}
};
}
namespace VMA_HPP_NAMESPACE {
VMA_EXPORT namespace VMA_HPP_NAMESPACE {
using $1 = VULKAN_HPP_NAMESPACE::Flags<$0>;
Expand Down Expand Up @@ -481,7 +481,7 @@ static List<String> generateStructs(String orig, Ifdef.Range ifdef) throws IOExc
#ifndef VULKAN_MEMORY_ALLOCATOR_STRUCTS_HPP
#define VULKAN_MEMORY_ALLOCATOR_STRUCTS_HPP
namespace VMA_HPP_NAMESPACE {
VMA_EXPORT namespace VMA_HPP_NAMESPACE {
$0
}
#endif
Expand Down Expand Up @@ -518,7 +518,7 @@ String getLowerName() {

String generateClass() {
return processTemplate("""
namespace VMA_HPP_NAMESPACE {
VMA_EXPORT namespace VMA_HPP_NAMESPACE {
class $0 {
public:
using CType = Vma$0;
Expand Down Expand Up @@ -568,21 +568,21 @@ explicit operator bool() const VULKAN_HPP_NOEXCEPT {
}
""" + (hasUniqueVariant() ? """
#ifndef VULKAN_HPP_NO_SMART_HANDLE
namespace VULKAN_HPP_NAMESPACE {
VMA_EXPORT namespace VULKAN_HPP_NAMESPACE {
template<> class UniqueHandleTraits<VMA_HPP_NAMESPACE::$0, VMA_HPP_NAMESPACE::Dispatcher> {
public:
using deleter = VMA_HPP_NAMESPACE::Deleter<VMA_HPP_NAMESPACE::$0, $3>;
};
}
namespace VMA_HPP_NAMESPACE { using Unique$0 = VULKAN_HPP_NAMESPACE::UniqueHandle<$0, Dispatcher>; }
VMA_EXPORT namespace VMA_HPP_NAMESPACE { using Unique$0 = VULKAN_HPP_NAMESPACE::UniqueHandle<$0, Dispatcher>; }
#endif
""" : ""),
name, getLowerName(), declarations.toString().indent(4),
owner == null ? "void" : "VMA_HPP_NAMESPACE::" + owner.name);
}
String generateNamespace() {
return processTemplate("""
namespace VMA_HPP_NAMESPACE {
VMA_EXPORT namespace VMA_HPP_NAMESPACE {
$0
}
""", declarations.toString().indent(2));
Expand Down Expand Up @@ -881,7 +881,7 @@ String generate(boolean definition, boolean uniqueHandle, boolean customVectorAl
#ifndef VULKAN_MEMORY_ALLOCATOR_HANDLES_HPP
#define VULKAN_MEMORY_ALLOCATOR_HANDLES_HPP
namespace VMA_HPP_NAMESPACE {
VMA_EXPORT namespace VMA_HPP_NAMESPACE {
$0
}
Expand All @@ -893,7 +893,7 @@ String generate(boolean definition, boolean uniqueHandle, boolean customVectorAl
#ifndef VULKAN_MEMORY_ALLOCATOR_FUNCS_HPP
#define VULKAN_MEMORY_ALLOCATOR_FUNCS_HPP
namespace VMA_HPP_NAMESPACE {
VMA_EXPORT namespace VMA_HPP_NAMESPACE {
$0
}
#endif
Expand All @@ -902,32 +902,32 @@ String generate(boolean definition, boolean uniqueHandle, boolean customVectorAl
}

static void generateModule(List<String> enums, List<String> structs, List<Handle> handles) throws IOException {
Files.writeString(Path.of("src/vk_mem_alloc.cppm"), processTemplate("""
Files.writeString(Path.of("src/vk_mem_alloc.cppm"), """
module;
#define VMA_BUILD_MODULE
#ifndef VMA_USE_VULKAN_HPP_MODULE
#define VMA_USE_VULKAN_HPP_MODULE 0
#endif
#define VMA_IMPLEMENTATION
#include <vk_mem_alloc.hpp>
#include <vk_mem_alloc.h>
#if !VMA_USE_VULKAN_HPP_MODULE
#include <vulkan/vulkan.hpp>
#endif // !VMA_USE_VULKAN_HPP_MODULE
export module vk_mem_alloc_hpp;
export namespace VMA_HPP_NAMESPACE {
using VMA_HPP_NAMESPACE::operator|;
using VMA_HPP_NAMESPACE::operator&;
using VMA_HPP_NAMESPACE::operator^;
using VMA_HPP_NAMESPACE::operator~;
using VMA_HPP_NAMESPACE::to_string;
using VMA_HPP_NAMESPACE::functionsFromDispatcher;
{{{using VMA_HPP_NAMESPACE::${toString};}}}
}
""", Stream.concat(Stream.concat(enums.stream(), structs.stream()), handles.stream()
.flatMap(h -> h.name == null ? h.methods.stream() : Stream.of(h.name)))) +
processTemplate("""
#ifndef VULKAN_HPP_NO_SMART_HANDLE
export namespace VMA_HPP_NAMESPACE {
using VMA_HPP_NAMESPACE::UniqueBuffer;
using VMA_HPP_NAMESPACE::UniqueImage;
{{{using VMA_HPP_NAMESPACE::Unique${name};}}}
}
#if VMA_USE_VULKAN_HPP_MODULE
import vulkan_hpp;
#endif
#include "vk_mem_alloc.hpp"
""" +
processTemplate("""
module : private;
Expand Down
2 changes: 1 addition & 1 deletion Vulkan-Hpp
2 changes: 1 addition & 1 deletion VulkanMemoryAllocator
15 changes: 11 additions & 4 deletions include/vk_mem_alloc.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
#ifndef VULKAN_MEMORY_ALLOCATOR_HPP
#define VULKAN_MEMORY_ALLOCATOR_HPP

#ifndef VMA_BUILD_MODULE
#define VMA_EXPORT

#if !defined(AMD_VULKAN_MEMORY_ALLOCATOR_H)
#include <vk_mem_alloc.h>
#endif

#include <vulkan/vulkan.hpp>

#else // VMA_BUILD_MODULE
#define VMA_EXPORT export
#endif // VMA_BUILD_MODULE

#if !defined(VMA_HPP_NAMESPACE)
#define VMA_HPP_NAMESPACE vma
#endif

#define VMA_HPP_NAMESPACE_STRING VULKAN_HPP_STRINGIFY(VMA_HPP_NAMESPACE)

#ifndef VULKAN_HPP_NO_SMART_HANDLE
namespace VMA_HPP_NAMESPACE {
VMA_EXPORT namespace VMA_HPP_NAMESPACE {
struct Dispatcher {}; // VMA uses function pointers from VmaAllocator instead
class Allocator;

Expand Down Expand Up @@ -58,7 +65,7 @@ namespace VMA_HPP_NAMESPACE {
void destroy(const T& t) VULKAN_HPP_NOEXCEPT { t.destroy(); }
};
}
namespace VULKAN_HPP_NAMESPACE {
VMA_EXPORT namespace VULKAN_HPP_NAMESPACE {
template<> class UniqueHandleTraits<Buffer, VMA_HPP_NAMESPACE::Dispatcher> {
public:
using deleter = VMA_HPP_NAMESPACE::Deleter<Buffer, VMA_HPP_NAMESPACE::Allocator>;
Expand All @@ -68,7 +75,7 @@ namespace VULKAN_HPP_NAMESPACE {
using deleter = VMA_HPP_NAMESPACE::Deleter<Image, VMA_HPP_NAMESPACE::Allocator>;
};
}
namespace VMA_HPP_NAMESPACE {
VMA_EXPORT namespace VMA_HPP_NAMESPACE {
using UniqueBuffer = VULKAN_HPP_NAMESPACE::UniqueHandle<VULKAN_HPP_NAMESPACE::Buffer, Dispatcher>;
using UniqueImage = VULKAN_HPP_NAMESPACE::UniqueHandle<VULKAN_HPP_NAMESPACE::Image, Dispatcher>;
}
Expand All @@ -79,7 +86,7 @@ namespace VMA_HPP_NAMESPACE {
#include "vk_mem_alloc_structs.hpp"
#include "vk_mem_alloc_funcs.hpp"

namespace VMA_HPP_NAMESPACE {
VMA_EXPORT namespace VMA_HPP_NAMESPACE {

#ifndef VULKAN_HPP_NO_SMART_HANDLE
# define VMA_HPP_DESTROY_IMPL(NAME) \
Expand Down
Loading

0 comments on commit bfb0e4b

Please sign in to comment.