Can now configure Vulkan with JSON. ENJOY!

This commit is contained in:
Zed A. Shaw 2025-12-06 13:04:57 -05:00
parent a996440c61
commit 883f683ce4
6 changed files with 96 additions and 15 deletions

34
json_mods.hpp Normal file
View file

@ -0,0 +1,34 @@
#pragma once
#include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp>
#include <optional>
#define ENROLL_COMPONENT(COMPONENT, ...) \
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(COMPONENT, __VA_ARGS__); \
template <> struct NameOf<COMPONENT> { \
static constexpr const char *name = #COMPONENT; \
};
// partial specialization (full specialization works too)
namespace nlohmann {
template <typename T>
struct adl_serializer<std::optional<T>> {
static void to_json(json& j, const std::optional<T>& opt) {
if (opt == std::nullopt) {
j = nullptr;
} else {
j = *opt; // this will call adl_serializer<T>::to_json which will
// find the free function to_json in T's namespace!
}
}
static void from_json(const json& j, std::optional<T>& opt) {
if (j.is_null() || j == false) {
opt = std::nullopt;
} else {
opt = std::make_optional<T>(j.template get<T>());
// same as above, but with adl_serializer<T>::from_json
}
}
};
}

View file

@ -104,6 +104,7 @@ else
})
endif
json = subproject('nlohmann_json').get_variable('nlohmann_json_dep')
vkbootstrap_proj = cmake.subproject('vk-bootstrap', options: vk_opts)
vkbootstrap = vkbootstrap_proj.get_variable('vk_bootstrap_dep')
@ -113,6 +114,7 @@ dependencies += [
glm,
imgui,
sdl2,
json,
]
sources = [

8
trash_test.hpp Normal file
View file

@ -0,0 +1,8 @@
#include "vk_types.h"
#include "json_mods.hpp"
template <typename T> struct NameOf;
ENROLL_COMPONENT(VkSemaphoreCreateInfo, sType);
ENROLL_COMPONENT(VkFenceCreateInfo, sType);
ENROLL_COMPONENT(VkImageViewCreateInfo, sType, viewType, subresourceRange.baseMipLevel, subresourceRange.levelCount, subresourceRange.baseArrayLayer, subresourceRange.layerCount);

View file

@ -19,7 +19,7 @@
#define VMA_IMPLEMENTATION
#include "vk_mem_alloc.h"
constexpr bool bUseValidationLayers = true;
constexpr bool bUseValidationLayers = false;
VulkanEngine* loadedEngine = nullptr;

View file

@ -1,4 +1,40 @@
#include <vk_initializers.h>
#include <vulkan/vk_enum_string_helper.h>
#include "trash_test.hpp"
nlohmann::json DATA = nlohmann::json::parse(R"(
{
"VkSemaphoreCreateInfo": {
"draw": {
"sType": 9
}
},
"VkFenceCreateInfo": {
"draw": {
"sType": 8
}
},
"VkImageViewCreateInfo": {
"draw": {
"sType": 15,
"viewType": 1,
"subresourceRange.baseMipLevel": 0,
"subresourceRange.levelCount": 1,
"subresourceRange.baseArrayLayer": 0,
"subresourceRange.layerCount": 1
}
}
}
)");
template<typename COMPONENT> COMPONENT load(nlohmann::json &data, const std::string& profile) {
COMPONENT result{};
auto& type_stuff = data[NameOf<COMPONENT>::name];
auto& profile_data = type_stuff[profile];
nlohmann::from_json(profile_data, result);
return result;
}
VkCommandPoolCreateInfo vkinit::command_pool_create_info(uint32_t queueFamilyIndex,
VkCommandPoolCreateFlags flags /*= 0*/)
@ -27,19 +63,16 @@ VkCommandBufferAllocateInfo vkinit::command_buffer_allocate_info(
VkFenceCreateInfo vkinit::fence_create_info(VkFenceCreateFlags flags /*=0*/)
{
VkFenceCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
info.pNext = nullptr;
auto info = load<VkFenceCreateInfo>(DATA, "draw");
info.flags = flags;
return info;
}
VkSemaphoreCreateInfo vkinit::semaphore_create_info(VkSemaphoreCreateFlags flags/*=0*/)
{
VkSemaphoreCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
info.pNext = nullptr;
auto info = load<VkSemaphoreCreateInfo>(DATA, "draw");
info.flags = flags;
return info;
}
@ -133,17 +166,10 @@ VkImageCreateInfo vkinit::image_create_info(VkFormat format, VkImageUsageFlags u
VkImageViewCreateInfo vkinit::imageview_create_info(VkFormat format, VkImage image, VkImageAspectFlags aspectFlags)
{
VkImageViewCreateInfo info{};
info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
info.pNext = nullptr;
auto info = load<VkImageViewCreateInfo>(DATA, "draw");
info.viewType = VK_IMAGE_VIEW_TYPE_2D;
info.image = image;
info.format = format;
info.subresourceRange.baseMipLevel = 0;
info.subresourceRange.levelCount = 1;
info.subresourceRange.baseArrayLayer = 0;
info.subresourceRange.layerCount = 1;
info.subresourceRange.aspectMask = aspectFlags;
return info;

11
wraps/nlohmann_json.wrap Normal file
View file

@ -0,0 +1,11 @@
[wrap-file]
directory = nlohmann_json-3.11.3
lead_directory_missing = true
source_url = https://github.com/nlohmann/json/releases/download/v3.11.3/include.zip
source_filename = nlohmann_json-3.11.3.zip
source_hash = a22461d13119ac5c78f205d3df1db13403e58ce1bb1794edc9313677313f4a9d
source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/nlohmann_json_3.11.3-1/nlohmann_json-3.11.3.zip
wrapdb_version = 3.11.3-1
[provide]
nlohmann_json = nlohmann_json_dep