Compare commits
3 Commits
0799a11fef
...
e7d74e6b0f
| Author | SHA1 | Date |
|---|---|---|
|
|
e7d74e6b0f | |
|
|
35347d28e0 | |
|
|
1e00847f90 |
|
|
@ -0,0 +1,64 @@
|
||||||
|
#include "AppState.h"
|
||||||
|
|
||||||
|
#include <SDL3/SDL_log.h>
|
||||||
|
|
||||||
|
#include "GlobalMemory.h"
|
||||||
|
#include "RenderDevice.h"
|
||||||
|
#include "MiscData.h"
|
||||||
|
|
||||||
|
bool AppState::isInit() const
|
||||||
|
{
|
||||||
|
return window and renderDevice and renderDevice->isInit();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AppState::cleanup()
|
||||||
|
{
|
||||||
|
if (!isInit()) return;
|
||||||
|
|
||||||
|
renderDevice->waitIdle();
|
||||||
|
|
||||||
|
Take(miscData)->cleanup(*renderDevice);
|
||||||
|
|
||||||
|
Take(renderDevice)->cleanup();
|
||||||
|
SDL_DestroyWindow(Take(window));
|
||||||
|
}
|
||||||
|
|
||||||
|
AppState::AppState(SDL_Window* window, RenderDevice* renderDevice, MiscData* miscData): window{ window }
|
||||||
|
, renderDevice{ renderDevice }
|
||||||
|
, miscData{ miscData }
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
AppState* CreateAppState(GlobalMemory* memory, uint32_t const width, uint32_t const height)
|
||||||
|
{
|
||||||
|
SDL_Window* window = SDL_CreateWindow("Blaze Test", static_cast<int>(width), static_cast<int>(height), SDL_WINDOW_VULKAN);
|
||||||
|
if (!window)
|
||||||
|
{
|
||||||
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s", SDL_GetError());
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto state = memory->getState();
|
||||||
|
RenderDevice* renderDevice = CreateRenderDevice(memory, { .window = window });
|
||||||
|
if (!renderDevice->isInit())
|
||||||
|
{
|
||||||
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "RenderDevice failed to init");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
(void)state;
|
||||||
|
|
||||||
|
auto* miscDataAllocation = memory->allocate(sizeof(MiscData));
|
||||||
|
|
||||||
|
MiscData* miscData = new (miscDataAllocation) MiscData{};
|
||||||
|
miscData->init(*renderDevice);
|
||||||
|
|
||||||
|
auto* allocation = memory->allocate(sizeof(AppState));
|
||||||
|
AppState* appState = new (allocation) AppState{ window, renderDevice, miscData };
|
||||||
|
|
||||||
|
return appState;
|
||||||
|
}
|
||||||
|
|
||||||
|
AppState::~AppState()
|
||||||
|
{
|
||||||
|
cleanup();
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
struct GlobalMemory;
|
||||||
|
struct SDL_Window;
|
||||||
|
struct RenderDevice;
|
||||||
|
struct MiscData;
|
||||||
|
|
||||||
|
struct AppState
|
||||||
|
{
|
||||||
|
SDL_Window* window;
|
||||||
|
RenderDevice* renderDevice;
|
||||||
|
MiscData* miscData;
|
||||||
|
|
||||||
|
[[nodiscard]] bool isInit() const;
|
||||||
|
void cleanup();
|
||||||
|
|
||||||
|
AppState(SDL_Window* window, RenderDevice* renderDevice, MiscData* miscData);
|
||||||
|
|
||||||
|
AppState(AppState const& other) = delete;
|
||||||
|
AppState(AppState&& other) noexcept = delete;
|
||||||
|
|
||||||
|
AppState& operator=(AppState const& other) = delete;
|
||||||
|
AppState& operator=(AppState&& other) noexcept = delete;
|
||||||
|
|
||||||
|
~AppState();
|
||||||
|
};
|
||||||
|
|
||||||
|
AppState* CreateAppState(GlobalMemory* memory, uint32_t const width, uint32_t const height);
|
||||||
795
Blaze.cpp
795
Blaze.cpp
|
|
@ -1,697 +1,94 @@
|
||||||
// Blaze.cpp : This file contains the 'main' function. Program execution begins and ends there.
|
// Blaze.cpp : This file contains the 'main' function. Program execution begins and ends there.
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <array>
|
|
||||||
#include <fmt/format.h>
|
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <span>
|
#include <span>
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include <volk.h>
|
#include <volk.h>
|
||||||
|
|
||||||
|
#define SDL_MAIN_USE_CALLBACKS 1
|
||||||
|
#include <memory>
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
|
#include <SDL3/SDL_main.h>
|
||||||
#include <SDL3/SDL_filesystem.h>
|
#include <SDL3/SDL_filesystem.h>
|
||||||
#include <SDL3/SDL_vulkan.h>
|
#include <SDL3/SDL_vulkan.h>
|
||||||
|
|
||||||
#define ASSERT(COND) assert((COND))
|
#include "AppState.h"
|
||||||
|
#include "Frame.h"
|
||||||
#define VK_CHECK(RESULT) ASSERT((RESULT) == VK_SUCCESS)
|
#include "GlobalMemory.h"
|
||||||
|
#include "MacroUtils.h"
|
||||||
|
#include "MathUtil.h"
|
||||||
|
#include "RenderDevice.h"
|
||||||
|
#include "MiscData.h"
|
||||||
|
|
||||||
constexpr uint32_t WIDTH = 1280;
|
constexpr uint32_t WIDTH = 1280;
|
||||||
constexpr uint32_t HEIGHT = 720;
|
constexpr uint32_t HEIGHT = 720;
|
||||||
constexpr uint32_t NUM_FRAMES = 3;
|
constexpr uint32_t NUM_FRAMES = 3;
|
||||||
|
|
||||||
uint32_t Clamp(uint32_t const val, uint32_t const minVal, uint32_t const maxVal)
|
using Byte = uint8_t;
|
||||||
|
|
||||||
|
constexpr size_t operator ""_KiB(size_t const value)
|
||||||
{
|
{
|
||||||
return std::min(maxVal, std::max(val, minVal));
|
return value * 1024;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
constexpr size_t operator ""_MiB(size_t const value)
|
||||||
{
|
{
|
||||||
volkInitialize();
|
return value * 1024_KiB;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr size_t operator ""_GiB(size_t const value)
|
||||||
|
{
|
||||||
|
return value * 1024_MiB;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace Blaze::Global
|
||||||
|
{
|
||||||
|
GlobalMemory g_Memory;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_AppResult SDL_AppInit(void** pAppState, int, char**)
|
||||||
|
{
|
||||||
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
|
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
|
||||||
|
|
||||||
SDL_Window* window = SDL_CreateWindow("Blaze Test", WIDTH, HEIGHT, SDL_WINDOW_VULKAN);
|
Blaze::Global::g_Memory.init(128_MiB);
|
||||||
|
|
||||||
VkApplicationInfo constexpr applicationInfo = {
|
*pAppState = CreateAppState(&Blaze::Global::g_Memory, WIDTH, HEIGHT);
|
||||||
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
|
if (!*pAppState)
|
||||||
.pNext = nullptr,
|
return SDL_APP_FAILURE;
|
||||||
.pApplicationName = "Test",
|
|
||||||
.applicationVersion = VK_MAKE_API_VERSION(0, 0, 1, 0),
|
|
||||||
.pEngineName = "Blaze",
|
|
||||||
.engineVersion = VK_MAKE_API_VERSION(0, 0, 1, 0),
|
|
||||||
.apiVersion = VK_API_VERSION_1_3,
|
|
||||||
};
|
|
||||||
|
|
||||||
uint32_t instanceExtensionCount;
|
AppState& appState = *static_cast<AppState*>(*pAppState);
|
||||||
char const* const* instanceExtensions = SDL_Vulkan_GetInstanceExtensions(&instanceExtensionCount);
|
|
||||||
|
|
||||||
VkInstanceCreateInfo const instanceCreateInfo = {
|
if (!appState.isInit())
|
||||||
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0,
|
|
||||||
.pApplicationInfo = &applicationInfo,
|
|
||||||
.enabledLayerCount = 0,
|
|
||||||
.ppEnabledLayerNames = nullptr,
|
|
||||||
.enabledExtensionCount = instanceExtensionCount,
|
|
||||||
.ppEnabledExtensionNames = instanceExtensions,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkInstance instance;
|
|
||||||
VK_CHECK(vkCreateInstance(&instanceCreateInfo, nullptr, &instance));
|
|
||||||
volkLoadInstance(instance);
|
|
||||||
|
|
||||||
VkSurfaceKHR surface;
|
|
||||||
ASSERT(SDL_Vulkan_CreateSurface(window, instance, nullptr, &surface));
|
|
||||||
|
|
||||||
VkPhysicalDevice physicalDeviceInUse = nullptr;
|
|
||||||
VkDevice device = nullptr;
|
|
||||||
uint32_t directQueueFamilyIndex = 0;
|
|
||||||
VkQueue directQueue;
|
|
||||||
{
|
{
|
||||||
uint32_t physicalDeviceCount;
|
return SDL_APP_FAILURE;
|
||||||
VK_CHECK(vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, nullptr));
|
}
|
||||||
SDL_Log("Found %u GPUs", physicalDeviceCount);
|
|
||||||
|
|
||||||
std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
|
return SDL_APP_CONTINUE;
|
||||||
VK_CHECK(vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, physicalDevices.data()));
|
}
|
||||||
|
|
||||||
|
SDL_AppResult SDL_AppIterate(void* appstate)
|
||||||
std::vector<VkQueueFamilyProperties> queueFamilyProperties;
|
|
||||||
for (VkPhysicalDevice const physicalDevice : physicalDevices)
|
|
||||||
{
|
{
|
||||||
VkPhysicalDeviceProperties properties;
|
AppState& appState = *static_cast<AppState*>(appstate);
|
||||||
vkGetPhysicalDeviceProperties(physicalDevice, &properties);
|
RenderDevice& renderDevice = *appState.renderDevice;
|
||||||
|
MiscData& misc = *appState.miscData;
|
||||||
|
Frame& currentFrame = renderDevice.frames[renderDevice.frameIndex];
|
||||||
|
|
||||||
SDL_Log("GPU: %s", properties.deviceName);
|
VK_CHECK(vkWaitForFences(renderDevice.device, 1, ¤tFrame.frameReadyToReuse, VK_TRUE, std::numeric_limits<uint32_t>::max()));
|
||||||
|
|
||||||
SDL_Log("- API Version %d.%d.%d",
|
|
||||||
VK_API_VERSION_MAJOR(properties.apiVersion),
|
|
||||||
VK_API_VERSION_MINOR(properties.apiVersion),
|
|
||||||
VK_API_VERSION_PATCH(properties.apiVersion));
|
|
||||||
|
|
||||||
constexpr static uint32_t API_PATCH_BITS = 0xFFF;
|
|
||||||
if ((properties.apiVersion & (~API_PATCH_BITS)) < VK_API_VERSION_1_3)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t queueFamilyCount;
|
|
||||||
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, nullptr);
|
|
||||||
queueFamilyProperties.resize(queueFamilyCount);
|
|
||||||
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, queueFamilyProperties.data());
|
|
||||||
|
|
||||||
for (uint32_t queueFamilyIndex = 0; VkQueueFamilyProperties const qfp : queueFamilyProperties)
|
|
||||||
{
|
|
||||||
bool hasGraphicsSupport = false;
|
|
||||||
bool hasComputeSupport = false;
|
|
||||||
bool hasTransferSupport = false;
|
|
||||||
bool hasPresentSupport = false;
|
|
||||||
|
|
||||||
SDL_Log("- Queue [%d]", queueFamilyIndex);
|
|
||||||
if (qfp.queueFlags & VK_QUEUE_GRAPHICS_BIT)
|
|
||||||
{
|
|
||||||
hasGraphicsSupport = true;
|
|
||||||
SDL_Log("-- Graphic");
|
|
||||||
}
|
|
||||||
if (qfp.queueFlags & VK_QUEUE_COMPUTE_BIT)
|
|
||||||
{
|
|
||||||
hasComputeSupport = true;
|
|
||||||
SDL_Log("-- Compute");
|
|
||||||
}
|
|
||||||
if (qfp.queueFlags & VK_QUEUE_TRANSFER_BIT)
|
|
||||||
{
|
|
||||||
hasTransferSupport = true;
|
|
||||||
SDL_Log("-- Transfer");
|
|
||||||
}
|
|
||||||
|
|
||||||
VkBool32 isSurfaceSupported;
|
|
||||||
VK_CHECK(vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, queueFamilyIndex, surface, &isSurfaceSupported));
|
|
||||||
|
|
||||||
if (isSurfaceSupported)
|
|
||||||
{
|
|
||||||
hasPresentSupport = true;
|
|
||||||
SDL_Log("-- Present");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hasGraphicsSupport and hasComputeSupport and hasTransferSupport and hasPresentSupport)
|
|
||||||
{
|
|
||||||
physicalDeviceInUse = physicalDevice;
|
|
||||||
directQueueFamilyIndex = queueFamilyIndex;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
++queueFamilyIndex;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ASSERT(physicalDeviceInUse);
|
|
||||||
|
|
||||||
float priority = 1.0f;
|
|
||||||
VkDeviceQueueCreateInfo queueCreateInfo = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0,
|
|
||||||
.queueFamilyIndex = directQueueFamilyIndex,
|
|
||||||
.queueCount = 1,
|
|
||||||
.pQueuePriorities = &priority,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPhysicalDeviceVulkan13Features constexpr features13 = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.synchronization2 = true,
|
|
||||||
.dynamicRendering = true,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPhysicalDeviceFeatures features = {
|
|
||||||
.depthClamp = true,
|
|
||||||
.samplerAnisotropy = true,
|
|
||||||
};
|
|
||||||
|
|
||||||
std::array enabledDeviceExtensions = {
|
|
||||||
VK_KHR_SWAPCHAIN_EXTENSION_NAME
|
|
||||||
};
|
|
||||||
|
|
||||||
VkDeviceCreateInfo const deviceCreateInfo = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
|
|
||||||
.pNext = &features13,
|
|
||||||
.flags = 0,
|
|
||||||
.queueCreateInfoCount = 1,
|
|
||||||
.pQueueCreateInfos = &queueCreateInfo,
|
|
||||||
.enabledLayerCount = 0,
|
|
||||||
.ppEnabledLayerNames = nullptr,
|
|
||||||
.enabledExtensionCount = enabledDeviceExtensions.size(),
|
|
||||||
.ppEnabledExtensionNames = enabledDeviceExtensions.data(),
|
|
||||||
.pEnabledFeatures = &features,
|
|
||||||
};
|
|
||||||
|
|
||||||
VK_CHECK(vkCreateDevice(physicalDeviceInUse, &deviceCreateInfo, nullptr, &device));
|
|
||||||
vkGetDeviceQueue(device, directQueueFamilyIndex, 0, &directQueue);
|
|
||||||
}
|
|
||||||
|
|
||||||
VkFormat swapchainFormat;
|
|
||||||
VkExtent2D swapchainExtent = { WIDTH, HEIGHT };
|
|
||||||
VkSwapchainKHR swapchain;
|
|
||||||
std::vector<VkImage> swapchainImages;
|
|
||||||
std::vector<VkImageView> swapchainViews;
|
|
||||||
{
|
|
||||||
VkSurfaceCapabilitiesKHR capabilities;
|
|
||||||
VK_CHECK(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDeviceInUse, surface, &capabilities));
|
|
||||||
|
|
||||||
// Image Count Calculation
|
|
||||||
uint32_t swapchainImageCount = NUM_FRAMES;
|
|
||||||
if (capabilities.maxImageCount > 0)
|
|
||||||
{
|
|
||||||
swapchainImageCount = std::min(swapchainImageCount, capabilities.maxImageCount);
|
|
||||||
}
|
|
||||||
swapchainImageCount = std::max(swapchainImageCount, capabilities.minImageCount);
|
|
||||||
|
|
||||||
// Image Size calculation
|
|
||||||
{
|
|
||||||
auto [minWidth, minHeight] = capabilities.minImageExtent;
|
|
||||||
auto [maxWidth, maxHeight] = capabilities.maxImageExtent;
|
|
||||||
swapchainExtent.width = Clamp(swapchainExtent.width, minWidth, maxWidth);
|
|
||||||
swapchainExtent.height = Clamp(swapchainExtent.height, minHeight, maxHeight);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t surfaceFormatCount;
|
|
||||||
vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDeviceInUse, surface, &surfaceFormatCount, nullptr);
|
|
||||||
std::vector<VkSurfaceFormatKHR> surfaceFormats(surfaceFormatCount);
|
|
||||||
vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDeviceInUse, surface, &surfaceFormatCount, surfaceFormats.data());
|
|
||||||
|
|
||||||
VkSurfaceFormatKHR format;
|
|
||||||
format.format = VK_FORMAT_UNDEFINED;
|
|
||||||
for (auto& surfaceFormat : surfaceFormats)
|
|
||||||
{
|
|
||||||
if (surfaceFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
|
|
||||||
{
|
|
||||||
SDL_Log("Color Space SRGB Found %d", surfaceFormat.format);
|
|
||||||
if (surfaceFormat.format == VK_FORMAT_R8G8B8A8_SRGB) {
|
|
||||||
format = surfaceFormat;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (surfaceFormat.format == VK_FORMAT_B8G8R8A8_SRGB) {
|
|
||||||
format = surfaceFormat;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (surfaceFormat.format == VK_FORMAT_R8G8B8A8_UNORM) {
|
|
||||||
format = surfaceFormat;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ASSERT(format.format != VK_FORMAT_UNDEFINED);
|
|
||||||
swapchainFormat = format.format;
|
|
||||||
|
|
||||||
uint32_t presentModeCount;
|
|
||||||
vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDeviceInUse, surface, &presentModeCount, nullptr);
|
|
||||||
std::vector<VkPresentModeKHR> presentModes(presentModeCount);
|
|
||||||
vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDeviceInUse, surface, &presentModeCount, presentModes.data());
|
|
||||||
|
|
||||||
VkPresentModeKHR presentMode = VK_PRESENT_MODE_FIFO_KHR;
|
|
||||||
for (VkPresentModeKHR presentModeIter : presentModes)
|
|
||||||
{
|
|
||||||
if (presentModeIter == VK_PRESENT_MODE_FIFO_RELAXED_KHR)
|
|
||||||
{
|
|
||||||
presentMode = presentModeIter;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (presentModeIter == VK_PRESENT_MODE_MAILBOX_KHR)
|
|
||||||
{
|
|
||||||
presentMode = presentModeIter;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
VkSwapchainCreateInfoKHR const swapchainCreateInfo = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0,
|
|
||||||
.surface = surface,
|
|
||||||
.minImageCount = swapchainImageCount,
|
|
||||||
.imageFormat = format.format,
|
|
||||||
.imageColorSpace = format.colorSpace,
|
|
||||||
.imageExtent = swapchainExtent,
|
|
||||||
.imageArrayLayers = 1,
|
|
||||||
.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
|
|
||||||
.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
|
|
||||||
.queueFamilyIndexCount = 0,
|
|
||||||
.pQueueFamilyIndices = nullptr,
|
|
||||||
.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR,
|
|
||||||
.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
|
|
||||||
.presentMode = presentMode,
|
|
||||||
.clipped = false,
|
|
||||||
.oldSwapchain = nullptr,
|
|
||||||
};
|
|
||||||
|
|
||||||
VK_CHECK(vkCreateSwapchainKHR(device, &swapchainCreateInfo, nullptr, &swapchain));
|
|
||||||
|
|
||||||
swapchainImageCount = 0;
|
|
||||||
vkGetSwapchainImagesKHR(device, swapchain, &swapchainImageCount, nullptr);
|
|
||||||
swapchainImages.resize(swapchainImageCount);
|
|
||||||
vkGetSwapchainImagesKHR(device, swapchain, &swapchainImageCount, swapchainImages.data());
|
|
||||||
|
|
||||||
for (VkImage image : swapchainImages) {
|
|
||||||
|
|
||||||
VkImageViewCreateInfo const viewCreateInfo = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0,
|
|
||||||
.image = image,
|
|
||||||
.viewType = VK_IMAGE_VIEW_TYPE_2D,
|
|
||||||
.format = format.format,
|
|
||||||
.components = {
|
|
||||||
VK_COMPONENT_SWIZZLE_IDENTITY,
|
|
||||||
VK_COMPONENT_SWIZZLE_IDENTITY,
|
|
||||||
VK_COMPONENT_SWIZZLE_IDENTITY,
|
|
||||||
VK_COMPONENT_SWIZZLE_IDENTITY
|
|
||||||
},
|
|
||||||
.subresourceRange = {
|
|
||||||
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
|
|
||||||
.baseMipLevel = 0,
|
|
||||||
.levelCount = 1,
|
|
||||||
.baseArrayLayer = 0,
|
|
||||||
.layerCount = 1,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
VkImageView view;
|
|
||||||
VK_CHECK(vkCreateImageView(device, &viewCreateInfo, nullptr, &view));
|
|
||||||
|
|
||||||
swapchainViews.push_back(view);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
VkPipelineLayout pipelineLayout;
|
|
||||||
VkPipeline trianglePipeline;
|
|
||||||
{
|
|
||||||
size_t dataSize;
|
|
||||||
void* rawData = SDL_LoadFile("Triangle.spv", &dataSize);
|
|
||||||
ASSERT(dataSize % 4 == 0);
|
|
||||||
|
|
||||||
if (not rawData)
|
|
||||||
{
|
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "%s", SDL_GetError());
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
auto data = static_cast<uint32_t const*>(rawData);
|
|
||||||
|
|
||||||
VkShaderModuleCreateInfo const shaderModuleCreateInfo = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0,
|
|
||||||
.codeSize = dataSize,
|
|
||||||
.pCode = data,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkShaderModule shaderModule;
|
|
||||||
VK_CHECK(vkCreateShaderModule(device, &shaderModuleCreateInfo, nullptr, &shaderModule));
|
|
||||||
|
|
||||||
VkPipelineLayoutCreateInfo constexpr pipelineLayoutCreateInfo = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0,
|
|
||||||
.setLayoutCount = 0,
|
|
||||||
.pSetLayouts = nullptr,
|
|
||||||
.pushConstantRangeCount = 0,
|
|
||||||
.pPushConstantRanges = nullptr,
|
|
||||||
};
|
|
||||||
VK_CHECK(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &pipelineLayout));
|
|
||||||
|
|
||||||
std::array stages = {
|
|
||||||
VkPipelineShaderStageCreateInfo{
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0,
|
|
||||||
.stage = VK_SHADER_STAGE_VERTEX_BIT,
|
|
||||||
.module = shaderModule,
|
|
||||||
.pName = "VertexMain",
|
|
||||||
.pSpecializationInfo = nullptr,
|
|
||||||
},
|
|
||||||
VkPipelineShaderStageCreateInfo{
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0,
|
|
||||||
.stage = VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
||||||
.module = shaderModule,
|
|
||||||
.pName = "FragmentMain",
|
|
||||||
.pSpecializationInfo = nullptr,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPipelineVertexInputStateCreateInfo constexpr vertexInputState = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0,
|
|
||||||
.vertexBindingDescriptionCount = 0,
|
|
||||||
.pVertexBindingDescriptions = nullptr,
|
|
||||||
.vertexAttributeDescriptionCount = 0,
|
|
||||||
.pVertexAttributeDescriptions = nullptr,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPipelineInputAssemblyStateCreateInfo constexpr inputAssembly = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0,
|
|
||||||
.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
|
|
||||||
.primitiveRestartEnable = VK_FALSE,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPipelineTessellationStateCreateInfo constexpr tessellationState = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0,
|
|
||||||
.patchControlPoints = 0,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPipelineViewportStateCreateInfo constexpr viewportState = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0,
|
|
||||||
.viewportCount = 1,
|
|
||||||
.pViewports = nullptr,
|
|
||||||
.scissorCount = 1,
|
|
||||||
.pScissors = nullptr,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPipelineRasterizationStateCreateInfo constexpr rasterizationState = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0,
|
|
||||||
.depthClampEnable = VK_TRUE,
|
|
||||||
.rasterizerDiscardEnable = VK_FALSE,
|
|
||||||
.polygonMode = VK_POLYGON_MODE_FILL,
|
|
||||||
.cullMode = VK_CULL_MODE_NONE,
|
|
||||||
.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE,
|
|
||||||
.depthBiasEnable = VK_FALSE,
|
|
||||||
.depthBiasConstantFactor = 0.0f,
|
|
||||||
.depthBiasClamp = 0.0f,
|
|
||||||
.depthBiasSlopeFactor = 0.0f,
|
|
||||||
.lineWidth = 1.0f,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPipelineMultisampleStateCreateInfo constexpr multisampleState = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0,
|
|
||||||
.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT,
|
|
||||||
.sampleShadingEnable = VK_FALSE,
|
|
||||||
.minSampleShading = 0.0f,
|
|
||||||
.pSampleMask = nullptr,
|
|
||||||
.alphaToCoverageEnable = VK_FALSE,
|
|
||||||
.alphaToOneEnable = VK_FALSE,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPipelineDepthStencilStateCreateInfo constexpr depthStencilState = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0,
|
|
||||||
.depthTestEnable = VK_FALSE,
|
|
||||||
.depthWriteEnable = VK_FALSE,
|
|
||||||
.depthCompareOp = VK_COMPARE_OP_ALWAYS,
|
|
||||||
.depthBoundsTestEnable = VK_FALSE,
|
|
||||||
.stencilTestEnable = VK_FALSE,
|
|
||||||
.front = {},
|
|
||||||
.back = {},
|
|
||||||
.minDepthBounds = 0.0f,
|
|
||||||
.maxDepthBounds = 1.0f,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPipelineColorBlendAttachmentState constexpr colorBlendAttachmentState = {
|
|
||||||
.blendEnable = VK_FALSE,
|
|
||||||
.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA,
|
|
||||||
.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
|
|
||||||
.colorBlendOp = VK_BLEND_OP_ADD,
|
|
||||||
.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE,
|
|
||||||
.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO,
|
|
||||||
.alphaBlendOp = VK_BLEND_OP_ADD,
|
|
||||||
.colorWriteMask = VK_COLOR_COMPONENT_R_BIT
|
|
||||||
| VK_COLOR_COMPONENT_G_BIT
|
|
||||||
| VK_COLOR_COMPONENT_B_BIT
|
|
||||||
| VK_COLOR_COMPONENT_A_BIT,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPipelineColorBlendStateCreateInfo const colorBlendState = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0,
|
|
||||||
.logicOpEnable = VK_FALSE,
|
|
||||||
.logicOp = VK_LOGIC_OP_COPY,
|
|
||||||
.attachmentCount = 1,
|
|
||||||
.pAttachments = &colorBlendAttachmentState,
|
|
||||||
.blendConstants = {0.0f, 0.0f, 0.0f, 0.0f},
|
|
||||||
};
|
|
||||||
|
|
||||||
std::array constexpr dynamicStates = {
|
|
||||||
VK_DYNAMIC_STATE_VIEWPORT,
|
|
||||||
VK_DYNAMIC_STATE_SCISSOR
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPipelineDynamicStateCreateInfo const dynamicStateCreateInfo = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0,
|
|
||||||
.dynamicStateCount = static_cast<uint32_t>(dynamicStates.size()),
|
|
||||||
.pDynamicStates = dynamicStates.data()
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPipelineRenderingCreateInfoKHR const renderingCreateInfo = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR,
|
|
||||||
.colorAttachmentCount = 1,
|
|
||||||
.pColorAttachmentFormats = &swapchainFormat,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkGraphicsPipelineCreateInfo const graphicsPipelineCreateInfo = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
|
|
||||||
.pNext = &renderingCreateInfo,
|
|
||||||
.flags = 0,
|
|
||||||
.stageCount = static_cast<uint32_t>(stages.size()),
|
|
||||||
.pStages = stages.data(),
|
|
||||||
.pVertexInputState = &vertexInputState,
|
|
||||||
.pInputAssemblyState = &inputAssembly,
|
|
||||||
.pTessellationState = &tessellationState,
|
|
||||||
.pViewportState = &viewportState,
|
|
||||||
.pRasterizationState = &rasterizationState,
|
|
||||||
.pMultisampleState = &multisampleState,
|
|
||||||
.pDepthStencilState = &depthStencilState,
|
|
||||||
.pColorBlendState = &colorBlendState,
|
|
||||||
.pDynamicState = &dynamicStateCreateInfo,
|
|
||||||
.layout = pipelineLayout,
|
|
||||||
.renderPass = nullptr,
|
|
||||||
.subpass = 0,
|
|
||||||
.basePipelineHandle = nullptr,
|
|
||||||
.basePipelineIndex = 0,
|
|
||||||
};
|
|
||||||
|
|
||||||
VK_CHECK(vkCreateGraphicsPipelines(device, nullptr, 1, &graphicsPipelineCreateInfo, nullptr, &trianglePipeline));
|
|
||||||
|
|
||||||
vkDestroyShaderModule(device, shaderModule, nullptr);
|
|
||||||
|
|
||||||
SDL_free(rawData);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create 1 command pool per frame.
|
|
||||||
std::array<VkCommandPool, NUM_FRAMES> commandPools;
|
|
||||||
{
|
|
||||||
VkCommandPoolCreateInfo const commandPoolCreateInfo = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT,
|
|
||||||
.queueFamilyIndex = directQueueFamilyIndex,
|
|
||||||
};
|
|
||||||
for (auto& commandPool : commandPools) {
|
|
||||||
VK_CHECK(vkCreateCommandPool(device, &commandPoolCreateInfo, nullptr, &commandPool));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::array<VkCommandBuffer, NUM_FRAMES> commandBuffers;
|
|
||||||
{
|
|
||||||
uint32_t index = 0;
|
|
||||||
for (auto& commandPool : commandPools) {
|
|
||||||
VkCommandBufferAllocateInfo const commandBufferAllocateInfo = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.commandPool = commandPool,
|
|
||||||
.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
|
|
||||||
.commandBufferCount = 1,
|
|
||||||
};
|
|
||||||
VK_CHECK(vkAllocateCommandBuffers(device, &commandBufferAllocateInfo, &commandBuffers[index]));
|
|
||||||
|
|
||||||
++index;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::array<VkSemaphore, NUM_FRAMES> imageAcquiredSemaphores;
|
|
||||||
std::array<VkSemaphore, NUM_FRAMES> renderFinishedSemaphores;
|
|
||||||
std::array<VkFence, NUM_FRAMES> frameInUseFences;
|
|
||||||
{
|
|
||||||
VkSemaphoreCreateInfo constexpr semaphoreCreateInfo = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0
|
|
||||||
};
|
|
||||||
for (auto& semaphore : imageAcquiredSemaphores) {
|
|
||||||
VK_CHECK(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &semaphore));
|
|
||||||
}
|
|
||||||
for (auto& semaphore : renderFinishedSemaphores) {
|
|
||||||
VK_CHECK(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &semaphore));
|
|
||||||
}
|
|
||||||
|
|
||||||
VkFenceCreateInfo constexpr fenceCreateInfo = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = VK_FENCE_CREATE_SIGNALED_BIT,
|
|
||||||
};
|
|
||||||
for (auto& fence : frameInUseFences) {
|
|
||||||
VK_CHECK(vkCreateFence(device, &fenceCreateInfo, nullptr, &fence));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
VkImageMemoryBarrier2 acquireToRenderBarrier = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.srcStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT,
|
|
||||||
.srcAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT,
|
|
||||||
.dstStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT,
|
|
||||||
.dstAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT,
|
|
||||||
.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
|
|
||||||
.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
|
||||||
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
||||||
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
||||||
.subresourceRange = {
|
|
||||||
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
|
|
||||||
.baseMipLevel = 0,
|
|
||||||
.levelCount = 1,
|
|
||||||
.baseArrayLayer = 0,
|
|
||||||
.layerCount = 1,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
VkDependencyInfo acquireToRenderDependency = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.dependencyFlags = 0,
|
|
||||||
.memoryBarrierCount = 0,
|
|
||||||
.pMemoryBarriers = nullptr,
|
|
||||||
.bufferMemoryBarrierCount = 0,
|
|
||||||
.pBufferMemoryBarriers = nullptr,
|
|
||||||
.imageMemoryBarrierCount = 1,
|
|
||||||
.pImageMemoryBarriers = &acquireToRenderBarrier,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkImageMemoryBarrier2 renderToPresentBarrier = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.srcStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT,
|
|
||||||
.srcAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT,
|
|
||||||
.dstStageMask = VK_PIPELINE_STAGE_2_BOTTOM_OF_PIPE_BIT,
|
|
||||||
.dstAccessMask = VK_ACCESS_2_NONE,
|
|
||||||
.oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
|
||||||
.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
|
|
||||||
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
||||||
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
||||||
.subresourceRange = {
|
|
||||||
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
|
|
||||||
.baseMipLevel = 0,
|
|
||||||
.levelCount = 1,
|
|
||||||
.baseArrayLayer = 0,
|
|
||||||
.layerCount = 1,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
VkDependencyInfo renderToPresentDependency = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.dependencyFlags = 0,
|
|
||||||
.memoryBarrierCount = 0,
|
|
||||||
.pMemoryBarriers = nullptr,
|
|
||||||
.bufferMemoryBarrierCount = 0,
|
|
||||||
.pBufferMemoryBarriers = nullptr,
|
|
||||||
.imageMemoryBarrierCount = 1,
|
|
||||||
.pImageMemoryBarriers = &renderToPresentBarrier,
|
|
||||||
};
|
|
||||||
|
|
||||||
uint32_t frameIndex = 0;
|
|
||||||
bool isRunning = true;
|
|
||||||
while (isRunning)
|
|
||||||
{
|
|
||||||
SDL_PumpEvents();
|
|
||||||
|
|
||||||
SDL_Event event;
|
|
||||||
while (SDL_PollEvent(&event))
|
|
||||||
{
|
|
||||||
switch (event.type)
|
|
||||||
{
|
|
||||||
case SDL_EVENT_QUIT:
|
|
||||||
isRunning = false;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
VkFence fence = frameInUseFences[frameIndex];
|
|
||||||
|
|
||||||
VK_CHECK(vkWaitForFences(device, 1, &fence, VK_TRUE, std::numeric_limits<uint32_t>::max()));
|
|
||||||
// All resources of frame 'frameIndex' are free.
|
// All resources of frame 'frameIndex' are free.
|
||||||
|
|
||||||
VkSemaphore imageAcquiredSemaphore = imageAcquiredSemaphores[frameIndex];
|
|
||||||
|
|
||||||
uint32_t currentImageIndex;
|
uint32_t currentImageIndex;
|
||||||
VK_CHECK(vkAcquireNextImageKHR(device, swapchain, std::numeric_limits<uint32_t>::max(), imageAcquiredSemaphore, nullptr, ¤tImageIndex));
|
VK_CHECK(vkAcquireNextImageKHR(renderDevice.device, renderDevice.swapchain, std::numeric_limits<uint32_t>::max(), currentFrame.imageAcquiredSemaphore, nullptr, ¤tImageIndex));
|
||||||
|
|
||||||
VK_CHECK(vkResetFences(device, 1, &fence));
|
VK_CHECK(vkResetFences(renderDevice.device, 1, ¤tFrame.frameReadyToReuse));
|
||||||
VK_CHECK(vkResetCommandPool(device, commandPools[frameIndex], 0));
|
VK_CHECK(vkResetCommandPool(renderDevice.device, currentFrame.commandPool, 0));
|
||||||
|
|
||||||
acquireToRenderBarrier.image = swapchainImages[currentImageIndex];
|
misc.acquireToRenderBarrier.image = renderDevice.swapchainImages[currentImageIndex];
|
||||||
renderToPresentBarrier.image = swapchainImages[currentImageIndex];
|
misc.renderToPresentBarrier.image = renderDevice.swapchainImages[currentImageIndex];
|
||||||
|
|
||||||
VkCommandBuffer cmd = commandBuffers[frameIndex];
|
VkCommandBuffer cmd = currentFrame.commandBuffer;
|
||||||
|
|
||||||
VkCommandBufferBeginInfo constexpr beginInfo = {
|
VkCommandBufferBeginInfo constexpr beginInfo = {
|
||||||
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
|
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
|
||||||
|
|
@ -709,7 +106,7 @@ int main()
|
||||||
VkRenderingAttachmentInfo const attachmentInfo = {
|
VkRenderingAttachmentInfo const attachmentInfo = {
|
||||||
.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO,
|
.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO,
|
||||||
.pNext = nullptr,
|
.pNext = nullptr,
|
||||||
.imageView = swapchainViews[currentImageIndex],
|
.imageView = renderDevice.swapchainViews[currentImageIndex],
|
||||||
.imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
.imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
||||||
.resolveMode = VK_RESOLVE_MODE_NONE,
|
.resolveMode = VK_RESOLVE_MODE_NONE,
|
||||||
.resolveImageView = nullptr,
|
.resolveImageView = nullptr,
|
||||||
|
|
@ -723,7 +120,7 @@ int main()
|
||||||
.sType = VK_STRUCTURE_TYPE_RENDERING_INFO,
|
.sType = VK_STRUCTURE_TYPE_RENDERING_INFO,
|
||||||
.pNext = nullptr,
|
.pNext = nullptr,
|
||||||
.flags = 0,
|
.flags = 0,
|
||||||
.renderArea = {.offset = {0,0}, .extent = swapchainExtent},
|
.renderArea = {.offset = {0,0}, .extent = renderDevice.swapchainExtent},
|
||||||
.layerCount = 1,
|
.layerCount = 1,
|
||||||
.viewMask = 0,
|
.viewMask = 0,
|
||||||
.colorAttachmentCount = 1,
|
.colorAttachmentCount = 1,
|
||||||
|
|
@ -732,107 +129,81 @@ int main()
|
||||||
.pStencilAttachment = nullptr,
|
.pStencilAttachment = nullptr,
|
||||||
};
|
};
|
||||||
|
|
||||||
vkCmdPipelineBarrier2(cmd, &acquireToRenderDependency);
|
vkCmdPipelineBarrier2(cmd, &misc.acquireToRenderDependency);
|
||||||
vkCmdBeginRendering(cmd, &renderingInfo);
|
vkCmdBeginRendering(cmd, &renderingInfo);
|
||||||
{
|
{
|
||||||
VkViewport viewport = {
|
VkViewport viewport = {
|
||||||
.x = 0,
|
.x = 0,
|
||||||
.y = static_cast<float>(swapchainExtent.height),
|
.y = static_cast<float>(renderDevice.swapchainExtent.height),
|
||||||
.width = static_cast<float>(swapchainExtent.width),
|
.width = static_cast<float>(renderDevice.swapchainExtent.width),
|
||||||
.height = -static_cast<float>(swapchainExtent.height),
|
.height = -static_cast<float>(renderDevice.swapchainExtent.height),
|
||||||
.minDepth = 0.0f,
|
.minDepth = 0.0f,
|
||||||
.maxDepth = 1.0f,
|
.maxDepth = 1.0f,
|
||||||
};
|
};
|
||||||
vkCmdSetViewport(cmd, 0, 1, &viewport);
|
vkCmdSetViewport(cmd, 0, 1, &viewport);
|
||||||
VkRect2D scissor = {
|
VkRect2D scissor = {
|
||||||
.offset = {0, 0},
|
.offset = {0, 0},
|
||||||
.extent = swapchainExtent,
|
.extent = renderDevice.swapchainExtent,
|
||||||
};
|
};
|
||||||
vkCmdSetScissor(cmd, 0, 1, &scissor);
|
vkCmdSetScissor(cmd, 0, 1, &scissor);
|
||||||
|
|
||||||
// Render Something?
|
// Render Something?
|
||||||
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, trianglePipeline);
|
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, misc.trianglePipeline);
|
||||||
vkCmdDraw(cmd, 3, 1, 0, 0);
|
vkCmdDraw(cmd, 3, 1, 0, 0);
|
||||||
|
|
||||||
}
|
}
|
||||||
vkCmdEndRendering(cmd);
|
vkCmdEndRendering(cmd);
|
||||||
vkCmdPipelineBarrier2(cmd, &renderToPresentDependency);
|
vkCmdPipelineBarrier2(cmd, &misc.renderToPresentDependency);
|
||||||
}
|
}
|
||||||
VK_CHECK(vkEndCommandBuffer(cmd));
|
VK_CHECK(vkEndCommandBuffer(cmd));
|
||||||
|
|
||||||
VkSemaphore renderingFinishedSemaphore = renderFinishedSemaphores[frameIndex];
|
|
||||||
|
|
||||||
VkPipelineStageFlags stageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
VkPipelineStageFlags stageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
||||||
VkSubmitInfo const submitInfo = {
|
VkSubmitInfo const submitInfo = {
|
||||||
.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
|
.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
|
||||||
.pNext = nullptr,
|
.pNext = nullptr,
|
||||||
.waitSemaphoreCount = 1,
|
.waitSemaphoreCount = 1,
|
||||||
.pWaitSemaphores = &imageAcquiredSemaphore,
|
.pWaitSemaphores = ¤tFrame.imageAcquiredSemaphore,
|
||||||
.pWaitDstStageMask = &stageMask,
|
.pWaitDstStageMask = &stageMask,
|
||||||
.commandBufferCount = 1,
|
.commandBufferCount = 1,
|
||||||
.pCommandBuffers = &cmd,
|
.pCommandBuffers = &cmd,
|
||||||
.signalSemaphoreCount = 1,
|
.signalSemaphoreCount = 1,
|
||||||
.pSignalSemaphores = &renderingFinishedSemaphore,
|
.pSignalSemaphores = ¤tFrame.renderFinishedSemaphore,
|
||||||
};
|
};
|
||||||
VK_CHECK(vkQueueSubmit(directQueue, 1, &submitInfo, fence));
|
VK_CHECK(vkQueueSubmit(renderDevice.directQueue, 1, &submitInfo, currentFrame.frameReadyToReuse));
|
||||||
|
|
||||||
VkPresentInfoKHR const presentInfo = {
|
VkPresentInfoKHR const presentInfo = {
|
||||||
.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
|
.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
|
||||||
.pNext = nullptr,
|
.pNext = nullptr,
|
||||||
.waitSemaphoreCount = 1,
|
.waitSemaphoreCount = 1,
|
||||||
.pWaitSemaphores = &renderingFinishedSemaphore,
|
.pWaitSemaphores = ¤tFrame.renderFinishedSemaphore,
|
||||||
.swapchainCount = 1,
|
.swapchainCount = 1,
|
||||||
.pSwapchains = &swapchain,
|
.pSwapchains = &renderDevice.swapchain,
|
||||||
.pImageIndices = ¤tImageIndex,
|
.pImageIndices = ¤tImageIndex,
|
||||||
.pResults = nullptr,
|
.pResults = nullptr,
|
||||||
};
|
};
|
||||||
|
|
||||||
VK_CHECK(vkQueuePresentKHR(directQueue, &presentInfo));
|
VK_CHECK(vkQueuePresentKHR(renderDevice.directQueue, &presentInfo));
|
||||||
|
|
||||||
frameIndex = (frameIndex + 1) % NUM_FRAMES;
|
renderDevice.frameIndex = (renderDevice.frameIndex + 1) % NUM_FRAMES;
|
||||||
|
|
||||||
|
return SDL_APP_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
VK_CHECK(vkDeviceWaitIdle(device));
|
SDL_AppResult SDL_AppEvent(void*, SDL_Event* event)
|
||||||
|
|
||||||
for (VkFence fence : frameInUseFences)
|
|
||||||
{
|
{
|
||||||
vkDestroyFence(device, fence, nullptr);
|
if (event->type == SDL_EVENT_QUIT)
|
||||||
}
|
|
||||||
|
|
||||||
for (VkSemaphore semaphore : imageAcquiredSemaphores)
|
|
||||||
{
|
{
|
||||||
vkDestroySemaphore(device, semaphore, nullptr);
|
return SDL_APP_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (VkSemaphore semaphore : renderFinishedSemaphores)
|
return SDL_APP_CONTINUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SDL_AppQuit(void* appstate, SDL_AppResult)
|
||||||
{
|
{
|
||||||
vkDestroySemaphore(device, semaphore, nullptr);
|
AppState* appState = static_cast<AppState*> (appstate);
|
||||||
}
|
|
||||||
|
appState->cleanup();
|
||||||
for (VkCommandPool commandPool : commandPools)
|
|
||||||
{
|
Blaze::Global::g_Memory.destroy();
|
||||||
vkDestroyCommandPool(device, commandPool, nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (VkImageView view : swapchainViews)
|
|
||||||
{
|
|
||||||
vkDestroyImageView(device, view, nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
vkDestroyPipeline(device, trianglePipeline, nullptr);
|
|
||||||
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
|
||||||
|
|
||||||
vkDestroySwapchainKHR(device, swapchain, nullptr);
|
|
||||||
|
|
||||||
vkDestroyDevice(device, nullptr);
|
|
||||||
|
|
||||||
SDL_Vulkan_DestroySurface(instance, surface, nullptr);
|
|
||||||
|
|
||||||
vkDestroyInstance(instance, nullptr);
|
|
||||||
|
|
||||||
volkFinalize();
|
|
||||||
|
|
||||||
SDL_DestroyWindow(window);
|
|
||||||
|
|
||||||
SDL_Quit();
|
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CppNaming/Rules/=Class_0020and_0020struct_0020methods/@EntryIndexedValue"><NamingElement Priority="10"><Descriptor Static="Indeterminate" Constexpr="Indeterminate" Const="Indeterminate" Volatile="Indeterminate" Accessibility="NOT_APPLICABLE"><type Name="member function" /></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb"><ExtraRule Prefix="" Suffix="" Style="aa_bb" /></Policy></NamingElement></s:String></wpf:ResourceDictionary>
|
||||||
|
|
@ -148,7 +148,13 @@
|
||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="AppState.cpp" />
|
||||||
<ClCompile Include="Blaze.cpp" />
|
<ClCompile Include="Blaze.cpp" />
|
||||||
|
<ClCompile Include="Frame.cpp" />
|
||||||
|
<ClCompile Include="GlobalMemory.cpp" />
|
||||||
|
<ClCompile Include="MiscData.cpp" />
|
||||||
|
<ClCompile Include="RenderDevice.cpp" />
|
||||||
|
<ClCompile Include="VmaImpl.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include=".gitignore" />
|
<None Include=".gitignore" />
|
||||||
|
|
@ -173,6 +179,16 @@
|
||||||
<None Include="vcpkg-configuration.json" />
|
<None Include="vcpkg-configuration.json" />
|
||||||
<None Include="vcpkg.json" />
|
<None Include="vcpkg.json" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClInclude Include="AppState.h" />
|
||||||
|
<ClInclude Include="Frame.h" />
|
||||||
|
<ClInclude Include="GlobalMemory.h" />
|
||||||
|
<ClInclude Include="MemoryUtils.h" />
|
||||||
|
<ClInclude Include="MacroUtils.h" />
|
||||||
|
<ClInclude Include="MathUtil.h" />
|
||||||
|
<ClInclude Include="MiscData.h" />
|
||||||
|
<ClInclude Include="RenderDevice.h" />
|
||||||
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
</ImportGroup>
|
</ImportGroup>
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,24 @@
|
||||||
<ClCompile Include="Blaze.cpp">
|
<ClCompile Include="Blaze.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Frame.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="RenderDevice.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="AppState.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="MiscData.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="GlobalMemory.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="VmaImpl.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="vcpkg.json">
|
<None Include="vcpkg.json">
|
||||||
|
|
@ -44,4 +62,30 @@
|
||||||
<Filter>Shader Files</Filter>
|
<Filter>Shader Files</Filter>
|
||||||
</CustomBuild>
|
</CustomBuild>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClInclude Include="Frame.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="RenderDevice.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="MacroUtils.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="MathUtil.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="MemoryUtils.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="AppState.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="MiscData.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="GlobalMemory.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|
@ -0,0 +1,64 @@
|
||||||
|
#include "Frame.h"
|
||||||
|
|
||||||
|
#include <SDL3/SDL_log.h>
|
||||||
|
|
||||||
|
#include "MacroUtils.h"
|
||||||
|
#include "RenderDevice.h"
|
||||||
|
|
||||||
|
bool Frame::isInit() const
|
||||||
|
{
|
||||||
|
return static_cast<bool>(commandPool);
|
||||||
|
}
|
||||||
|
|
||||||
|
Frame::Frame(VkDevice const device, uint32_t const directQueueFamilyIndex)
|
||||||
|
{
|
||||||
|
VkCommandPoolCreateInfo const commandPoolCreateInfo = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT,
|
||||||
|
.queueFamilyIndex = directQueueFamilyIndex,
|
||||||
|
};
|
||||||
|
VK_CHECK(vkCreateCommandPool(device, &commandPoolCreateInfo, nullptr, &commandPool));
|
||||||
|
|
||||||
|
VkCommandBufferAllocateInfo const commandBufferAllocateInfo = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.commandPool = commandPool,
|
||||||
|
.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
|
||||||
|
.commandBufferCount = 1,
|
||||||
|
};
|
||||||
|
VK_CHECK(vkAllocateCommandBuffers(device, &commandBufferAllocateInfo, &commandBuffer));
|
||||||
|
|
||||||
|
VkSemaphoreCreateInfo constexpr semaphoreCreateInfo = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = 0
|
||||||
|
};
|
||||||
|
VK_CHECK(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &imageAcquiredSemaphore));
|
||||||
|
VK_CHECK(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &renderFinishedSemaphore));
|
||||||
|
|
||||||
|
VkFenceCreateInfo constexpr fenceCreateInfo = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = VK_FENCE_CREATE_SIGNALED_BIT,
|
||||||
|
};
|
||||||
|
VK_CHECK(vkCreateFence(device, &fenceCreateInfo, nullptr, &frameReadyToReuse));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Frame::cleanup(RenderDevice const& renderDevice)
|
||||||
|
{
|
||||||
|
if (!isInit()) return;
|
||||||
|
|
||||||
|
VkDevice const device = renderDevice.device;
|
||||||
|
|
||||||
|
vkDestroyCommandPool(device, Take(commandPool), nullptr);
|
||||||
|
vkDestroyFence(device, Take(frameReadyToReuse), nullptr);
|
||||||
|
vkDestroySemaphore(device, Take(imageAcquiredSemaphore), nullptr);
|
||||||
|
vkDestroySemaphore(device, Take(renderFinishedSemaphore), nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
Frame::~Frame()
|
||||||
|
{
|
||||||
|
// Manual Cleanup Required.
|
||||||
|
ASSERT(not isInit());
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
#include <volk.h>
|
||||||
|
|
||||||
|
struct RenderDevice;
|
||||||
|
|
||||||
|
struct Frame
|
||||||
|
{
|
||||||
|
VkCommandPool commandPool;
|
||||||
|
VkCommandBuffer commandBuffer;
|
||||||
|
VkSemaphore imageAcquiredSemaphore;
|
||||||
|
VkSemaphore renderFinishedSemaphore;
|
||||||
|
VkFence frameReadyToReuse;
|
||||||
|
|
||||||
|
[[nodiscard]] bool isInit() const;
|
||||||
|
|
||||||
|
Frame(VkDevice device, uint32_t directQueueFamilyIndex);
|
||||||
|
|
||||||
|
void cleanup(RenderDevice const& renderDevice);
|
||||||
|
|
||||||
|
~Frame();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
@ -0,0 +1,65 @@
|
||||||
|
#include "GlobalMemory.h"
|
||||||
|
|
||||||
|
#include <SDL3/SDL_log.h>
|
||||||
|
|
||||||
|
void GlobalMemory::init(size_t const size)
|
||||||
|
{
|
||||||
|
memory = new Byte[size];
|
||||||
|
capacity = size;
|
||||||
|
available = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GlobalMemory::destroy()
|
||||||
|
{
|
||||||
|
Byte const* originalMemory = memory - (capacity - available);
|
||||||
|
delete[] originalMemory;
|
||||||
|
memory = nullptr;
|
||||||
|
available = 0;
|
||||||
|
capacity = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Byte* GlobalMemory::allocate(size_t const size)
|
||||||
|
{
|
||||||
|
assert(size <= available && "No enough space available");
|
||||||
|
|
||||||
|
Byte* retVal = memory;
|
||||||
|
memory += size;
|
||||||
|
available -= size;
|
||||||
|
SDL_LogInfo(SDL_LOG_CATEGORY_SYSTEM, "ALLOC: %p -> %p (%llu) (avail: %llu)", reinterpret_cast<void*>(retVal), reinterpret_cast<void*>(memory), size, available);
|
||||||
|
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
Byte* GlobalMemory::allocate(size_t const size, size_t const alignment)
|
||||||
|
{
|
||||||
|
uintptr_t const addr = reinterpret_cast<uintptr_t>(memory);
|
||||||
|
uintptr_t const foundOffset = addr % alignment;
|
||||||
|
|
||||||
|
if (foundOffset == 0)
|
||||||
|
{
|
||||||
|
return allocate(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
uintptr_t const offset = alignment - foundOffset;
|
||||||
|
size_t const allocationSize = size + offset;
|
||||||
|
|
||||||
|
return offset + allocate(allocationSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
GlobalMemory::State GlobalMemory::getState() const
|
||||||
|
{
|
||||||
|
SDL_LogInfo(SDL_LOG_CATEGORY_SYSTEM, "TEMP: %p %llu", reinterpret_cast<void*>(memory), available);
|
||||||
|
return {
|
||||||
|
.memory = memory,
|
||||||
|
.available = available,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
void GlobalMemory::restoreState(State const& state)
|
||||||
|
{
|
||||||
|
assert(memory >= state.memory); //< Behind top of allocator
|
||||||
|
assert(memory - (capacity - available) <= state.memory); //< Ahead of start of allocator
|
||||||
|
SDL_LogInfo(SDL_LOG_CATEGORY_SYSTEM, "RESTORE: %p %llu", reinterpret_cast<void*>(memory), available);
|
||||||
|
memory = state.memory;
|
||||||
|
available = state.available;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include "MacroUtils.h"
|
||||||
|
|
||||||
|
using Byte = uint8_t;
|
||||||
|
|
||||||
|
struct GlobalMemory
|
||||||
|
{
|
||||||
|
struct State
|
||||||
|
{
|
||||||
|
Byte* memory;
|
||||||
|
size_t available;
|
||||||
|
};
|
||||||
|
|
||||||
|
Byte* memory;
|
||||||
|
size_t available;
|
||||||
|
size_t capacity;
|
||||||
|
|
||||||
|
void init(size_t const size);
|
||||||
|
|
||||||
|
void destroy();
|
||||||
|
|
||||||
|
Byte* allocate(size_t const size);
|
||||||
|
|
||||||
|
Byte* allocate(size_t const size, size_t const alignment);
|
||||||
|
|
||||||
|
// Do not do any permanent allocations after calling this.
|
||||||
|
[[nodiscard]] State getState() const;
|
||||||
|
|
||||||
|
// Call this before permanent allocations.
|
||||||
|
void restoreState(State const& state);
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#define G_ASSERT(COND) \
|
||||||
|
do { \
|
||||||
|
auto _result = (COND); \
|
||||||
|
if (not _result) { \
|
||||||
|
__debugbreak(); \
|
||||||
|
assert(_result && #COND); \
|
||||||
|
} \
|
||||||
|
} while(false)
|
||||||
|
|
||||||
|
#define ASSERT(COND) G_ASSERT(COND)
|
||||||
|
|
||||||
|
#define VK_CHECK(RESULT) \
|
||||||
|
do { \
|
||||||
|
auto _result = (RESULT); \
|
||||||
|
if (_result != VK_SUCCESS) { \
|
||||||
|
SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, \
|
||||||
|
"" #RESULT " failed with %d at %s:%d", \
|
||||||
|
_result, __FILE__, __LINE__); \
|
||||||
|
__debugbreak(); \
|
||||||
|
assert(_result == VK_SUCCESS); \
|
||||||
|
} \
|
||||||
|
} while(false)
|
||||||
|
|
||||||
|
#define Take(OBJ) std::exchange(OBJ, {})
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
template <std::totally_ordered T>
|
||||||
|
T Clamp(T const val, T const minVal, T const maxVal)
|
||||||
|
{
|
||||||
|
return std::min(maxVal, std::max(val, minVal));
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <foonathan/memory/memory_arena.hpp>
|
||||||
|
#include <foonathan/memory/memory_stack.hpp>
|
||||||
|
#include <foonathan/memory/temporary_allocator.hpp>
|
||||||
|
|
||||||
|
using global_allocator_t = foonathan::memory::memory_stack<foonathan::memory::virtual_block_allocator>;
|
||||||
|
|
||||||
|
using subsystem_allocator_t = foonathan::memory::memory_stack<foonathan::memory::fixed_block_allocator<global_allocator_t>>;
|
||||||
|
using temporary_allocator_t = foonathan::memory::temporary_allocator;
|
||||||
|
|
@ -0,0 +1,289 @@
|
||||||
|
#include "MiscData.h"
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <SDL3/SDL_log.h>
|
||||||
|
|
||||||
|
#include "MacroUtils.h"
|
||||||
|
#include "RenderDevice.h"
|
||||||
|
|
||||||
|
|
||||||
|
void MiscData::init(RenderDevice const& renderDevice)
|
||||||
|
{
|
||||||
|
VkDevice const device = renderDevice.device;
|
||||||
|
{
|
||||||
|
size_t dataSize;
|
||||||
|
void* rawData = SDL_LoadFile("Triangle.spv", &dataSize);
|
||||||
|
ASSERT(dataSize % 4 == 0);
|
||||||
|
|
||||||
|
if (not rawData)
|
||||||
|
{
|
||||||
|
SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "%s", SDL_GetError());
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto data = static_cast<uint32_t const*>(rawData);
|
||||||
|
|
||||||
|
VkShaderModuleCreateInfo const shaderModuleCreateInfo = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = 0,
|
||||||
|
.codeSize = dataSize,
|
||||||
|
.pCode = data,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkShaderModule shaderModule;
|
||||||
|
VK_CHECK(vkCreateShaderModule(device, &shaderModuleCreateInfo, nullptr, &shaderModule));
|
||||||
|
|
||||||
|
VkPipelineLayoutCreateInfo constexpr pipelineLayoutCreateInfo = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = 0,
|
||||||
|
.setLayoutCount = 0,
|
||||||
|
.pSetLayouts = nullptr,
|
||||||
|
.pushConstantRangeCount = 0,
|
||||||
|
.pPushConstantRanges = nullptr,
|
||||||
|
};
|
||||||
|
VK_CHECK(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &pipelineLayout));
|
||||||
|
|
||||||
|
std::array stages = {
|
||||||
|
VkPipelineShaderStageCreateInfo{
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = 0,
|
||||||
|
.stage = VK_SHADER_STAGE_VERTEX_BIT,
|
||||||
|
.module = shaderModule,
|
||||||
|
.pName = "VertexMain",
|
||||||
|
.pSpecializationInfo = nullptr,
|
||||||
|
},
|
||||||
|
VkPipelineShaderStageCreateInfo{
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = 0,
|
||||||
|
.stage = VK_SHADER_STAGE_FRAGMENT_BIT,
|
||||||
|
.module = shaderModule,
|
||||||
|
.pName = "FragmentMain",
|
||||||
|
.pSpecializationInfo = nullptr,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPipelineVertexInputStateCreateInfo constexpr vertexInputState = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = 0,
|
||||||
|
.vertexBindingDescriptionCount = 0,
|
||||||
|
.pVertexBindingDescriptions = nullptr,
|
||||||
|
.vertexAttributeDescriptionCount = 0,
|
||||||
|
.pVertexAttributeDescriptions = nullptr,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPipelineInputAssemblyStateCreateInfo constexpr inputAssembly = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = 0,
|
||||||
|
.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
|
||||||
|
.primitiveRestartEnable = VK_FALSE,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPipelineTessellationStateCreateInfo constexpr tessellationState = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = 0,
|
||||||
|
.patchControlPoints = 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPipelineViewportStateCreateInfo constexpr viewportState = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = 0,
|
||||||
|
.viewportCount = 1,
|
||||||
|
.pViewports = nullptr,
|
||||||
|
.scissorCount = 1,
|
||||||
|
.pScissors = nullptr,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPipelineRasterizationStateCreateInfo constexpr rasterizationState = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = 0,
|
||||||
|
.depthClampEnable = VK_TRUE,
|
||||||
|
.rasterizerDiscardEnable = VK_FALSE,
|
||||||
|
.polygonMode = VK_POLYGON_MODE_FILL,
|
||||||
|
.cullMode = VK_CULL_MODE_NONE,
|
||||||
|
.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE,
|
||||||
|
.depthBiasEnable = VK_FALSE,
|
||||||
|
.depthBiasConstantFactor = 0.0f,
|
||||||
|
.depthBiasClamp = 0.0f,
|
||||||
|
.depthBiasSlopeFactor = 0.0f,
|
||||||
|
.lineWidth = 1.0f,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPipelineMultisampleStateCreateInfo constexpr multisampleState = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = 0,
|
||||||
|
.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT,
|
||||||
|
.sampleShadingEnable = VK_FALSE,
|
||||||
|
.minSampleShading = 0.0f,
|
||||||
|
.pSampleMask = nullptr,
|
||||||
|
.alphaToCoverageEnable = VK_FALSE,
|
||||||
|
.alphaToOneEnable = VK_FALSE,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPipelineDepthStencilStateCreateInfo constexpr depthStencilState = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = 0,
|
||||||
|
.depthTestEnable = VK_FALSE,
|
||||||
|
.depthWriteEnable = VK_FALSE,
|
||||||
|
.depthCompareOp = VK_COMPARE_OP_ALWAYS,
|
||||||
|
.depthBoundsTestEnable = VK_FALSE,
|
||||||
|
.stencilTestEnable = VK_FALSE,
|
||||||
|
.front = {},
|
||||||
|
.back = {},
|
||||||
|
.minDepthBounds = 0.0f,
|
||||||
|
.maxDepthBounds = 1.0f,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPipelineColorBlendAttachmentState constexpr colorBlendAttachmentState = {
|
||||||
|
.blendEnable = VK_FALSE,
|
||||||
|
.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA,
|
||||||
|
.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
|
||||||
|
.colorBlendOp = VK_BLEND_OP_ADD,
|
||||||
|
.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE,
|
||||||
|
.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO,
|
||||||
|
.alphaBlendOp = VK_BLEND_OP_ADD,
|
||||||
|
.colorWriteMask = VK_COLOR_COMPONENT_R_BIT
|
||||||
|
| VK_COLOR_COMPONENT_G_BIT
|
||||||
|
| VK_COLOR_COMPONENT_B_BIT
|
||||||
|
| VK_COLOR_COMPONENT_A_BIT,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPipelineColorBlendStateCreateInfo const colorBlendState = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = 0,
|
||||||
|
.logicOpEnable = VK_FALSE,
|
||||||
|
.logicOp = VK_LOGIC_OP_COPY,
|
||||||
|
.attachmentCount = 1,
|
||||||
|
.pAttachments = &colorBlendAttachmentState,
|
||||||
|
.blendConstants = {0.0f, 0.0f, 0.0f, 0.0f},
|
||||||
|
};
|
||||||
|
|
||||||
|
std::array constexpr dynamicStates = {
|
||||||
|
VK_DYNAMIC_STATE_VIEWPORT,
|
||||||
|
VK_DYNAMIC_STATE_SCISSOR
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPipelineDynamicStateCreateInfo const dynamicStateCreateInfo = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = 0,
|
||||||
|
.dynamicStateCount = static_cast<uint32_t>(dynamicStates.size()),
|
||||||
|
.pDynamicStates = dynamicStates.data()
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPipelineRenderingCreateInfoKHR const renderingCreateInfo = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR,
|
||||||
|
.colorAttachmentCount = 1,
|
||||||
|
.pColorAttachmentFormats = &renderDevice.swapchainFormat,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkGraphicsPipelineCreateInfo const graphicsPipelineCreateInfo = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
|
||||||
|
.pNext = &renderingCreateInfo,
|
||||||
|
.flags = 0,
|
||||||
|
.stageCount = static_cast<uint32_t>(stages.size()),
|
||||||
|
.pStages = stages.data(),
|
||||||
|
.pVertexInputState = &vertexInputState,
|
||||||
|
.pInputAssemblyState = &inputAssembly,
|
||||||
|
.pTessellationState = &tessellationState,
|
||||||
|
.pViewportState = &viewportState,
|
||||||
|
.pRasterizationState = &rasterizationState,
|
||||||
|
.pMultisampleState = &multisampleState,
|
||||||
|
.pDepthStencilState = &depthStencilState,
|
||||||
|
.pColorBlendState = &colorBlendState,
|
||||||
|
.pDynamicState = &dynamicStateCreateInfo,
|
||||||
|
.layout = pipelineLayout,
|
||||||
|
.renderPass = nullptr,
|
||||||
|
.subpass = 0,
|
||||||
|
.basePipelineHandle = nullptr,
|
||||||
|
.basePipelineIndex = 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
VK_CHECK(vkCreateGraphicsPipelines(device, nullptr, 1, &graphicsPipelineCreateInfo, nullptr, &trianglePipeline));
|
||||||
|
|
||||||
|
vkDestroyShaderModule(device, shaderModule, nullptr);
|
||||||
|
|
||||||
|
SDL_free(rawData);
|
||||||
|
}
|
||||||
|
|
||||||
|
acquireToRenderBarrier = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.srcStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT,
|
||||||
|
.srcAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT,
|
||||||
|
.dstStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT,
|
||||||
|
.dstAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT,
|
||||||
|
.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
|
||||||
|
.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
||||||
|
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||||
|
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||||
|
.subresourceRange = {
|
||||||
|
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
|
||||||
|
.baseMipLevel = 0,
|
||||||
|
.levelCount = 1,
|
||||||
|
.baseArrayLayer = 0,
|
||||||
|
.layerCount = 1,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
acquireToRenderDependency = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.dependencyFlags = 0,
|
||||||
|
.memoryBarrierCount = 0,
|
||||||
|
.pMemoryBarriers = nullptr,
|
||||||
|
.bufferMemoryBarrierCount = 0,
|
||||||
|
.pBufferMemoryBarriers = nullptr,
|
||||||
|
.imageMemoryBarrierCount = 1,
|
||||||
|
.pImageMemoryBarriers = &acquireToRenderBarrier,
|
||||||
|
};
|
||||||
|
|
||||||
|
renderToPresentBarrier = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.srcStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT,
|
||||||
|
.srcAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT,
|
||||||
|
.dstStageMask = VK_PIPELINE_STAGE_2_BOTTOM_OF_PIPE_BIT,
|
||||||
|
.dstAccessMask = VK_ACCESS_2_NONE,
|
||||||
|
.oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
||||||
|
.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
|
||||||
|
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||||
|
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||||
|
.subresourceRange = {
|
||||||
|
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
|
||||||
|
.baseMipLevel = 0,
|
||||||
|
.levelCount = 1,
|
||||||
|
.baseArrayLayer = 0,
|
||||||
|
.layerCount = 1,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
renderToPresentDependency = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.dependencyFlags = 0,
|
||||||
|
.memoryBarrierCount = 0,
|
||||||
|
.pMemoryBarriers = nullptr,
|
||||||
|
.bufferMemoryBarrierCount = 0,
|
||||||
|
.pBufferMemoryBarriers = nullptr,
|
||||||
|
.imageMemoryBarrierCount = 1,
|
||||||
|
.pImageMemoryBarriers = &renderToPresentBarrier,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
void MiscData::cleanup(RenderDevice const& renderDevice)
|
||||||
|
{
|
||||||
|
VkDevice const device = renderDevice.device;
|
||||||
|
|
||||||
|
vkDestroyPipeline(device, trianglePipeline, nullptr);
|
||||||
|
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <volk.h>
|
||||||
|
|
||||||
|
struct RenderDevice;
|
||||||
|
|
||||||
|
struct MiscData
|
||||||
|
{
|
||||||
|
VkPipelineLayout pipelineLayout;
|
||||||
|
VkPipeline trianglePipeline;
|
||||||
|
|
||||||
|
VkImageMemoryBarrier2 acquireToRenderBarrier;
|
||||||
|
VkDependencyInfo acquireToRenderDependency;
|
||||||
|
VkImageMemoryBarrier2 renderToPresentBarrier;
|
||||||
|
VkDependencyInfo renderToPresentDependency;
|
||||||
|
|
||||||
|
void init(RenderDevice const& renderDevice);
|
||||||
|
void cleanup(RenderDevice const& renderDevice);
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,439 @@
|
||||||
|
#include "RenderDevice.h"
|
||||||
|
|
||||||
|
#include "MacroUtils.h"
|
||||||
|
|
||||||
|
#include <SDL3/SDL_log.h>
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <optional>
|
||||||
|
#include <span>
|
||||||
|
|
||||||
|
#include "Frame.h"
|
||||||
|
#include "GlobalMemory.h"
|
||||||
|
#include "MathUtil.h"
|
||||||
|
|
||||||
|
RenderDevice::~RenderDevice()
|
||||||
|
{
|
||||||
|
ASSERT(!isInit());
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Failure Handling
|
||||||
|
RenderDevice* CreateRenderDevice(GlobalMemory* mem, RenderDevice::CreateInfo const& createInfo)
|
||||||
|
{
|
||||||
|
ASSERT(createInfo.window);
|
||||||
|
|
||||||
|
volkInitialize();
|
||||||
|
|
||||||
|
VkInstance instance;
|
||||||
|
// Create Instance
|
||||||
|
{
|
||||||
|
VkApplicationInfo constexpr applicationInfo = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.pApplicationName = "Test",
|
||||||
|
.applicationVersion = VK_MAKE_API_VERSION(0, 0, 1, 0),
|
||||||
|
.pEngineName = "Blaze",
|
||||||
|
.engineVersion = VK_MAKE_API_VERSION(0, 0, 1, 0),
|
||||||
|
.apiVersion = VK_API_VERSION_1_3,
|
||||||
|
};
|
||||||
|
|
||||||
|
uint32_t instanceExtensionCount;
|
||||||
|
char const* const* instanceExtensions = SDL_Vulkan_GetInstanceExtensions(&instanceExtensionCount);
|
||||||
|
|
||||||
|
VkInstanceCreateInfo const instanceCreateInfo = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = 0,
|
||||||
|
.pApplicationInfo = &applicationInfo,
|
||||||
|
.enabledLayerCount = 0,
|
||||||
|
.ppEnabledLayerNames = nullptr,
|
||||||
|
.enabledExtensionCount = instanceExtensionCount,
|
||||||
|
.ppEnabledExtensionNames = instanceExtensions,
|
||||||
|
};
|
||||||
|
|
||||||
|
VK_CHECK(vkCreateInstance(&instanceCreateInfo, nullptr, &instance));
|
||||||
|
volkLoadInstance(instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
VkSurfaceKHR surface;
|
||||||
|
// Create Surface
|
||||||
|
ASSERT(SDL_Vulkan_CreateSurface(createInfo.window, instance, nullptr, &surface));
|
||||||
|
|
||||||
|
VkPhysicalDevice physicalDeviceInUse = nullptr;
|
||||||
|
VkDevice device = nullptr;
|
||||||
|
VmaAllocator gpuAllocator = nullptr;
|
||||||
|
std::optional<uint32_t> directQueueFamilyIndex;
|
||||||
|
VkQueue directQueue = nullptr;
|
||||||
|
// Create Device and Queue
|
||||||
|
{
|
||||||
|
auto tempAllocStart = mem->getState();
|
||||||
|
|
||||||
|
uint32_t physicalDeviceCount;
|
||||||
|
VK_CHECK(vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, nullptr));
|
||||||
|
SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "Found %u GPUs", physicalDeviceCount);
|
||||||
|
|
||||||
|
VkPhysicalDevice* physicalDevices = reinterpret_cast<VkPhysicalDevice*>(mem->allocate(sizeof(VkPhysicalDevice) * physicalDeviceCount));
|
||||||
|
VK_CHECK(vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, physicalDevices));
|
||||||
|
|
||||||
|
for (VkPhysicalDevice const physicalDevice : std::span{ physicalDevices, physicalDeviceCount })
|
||||||
|
{
|
||||||
|
auto tempAllocQueueProperties = mem->getState();
|
||||||
|
|
||||||
|
VkPhysicalDeviceProperties properties;
|
||||||
|
vkGetPhysicalDeviceProperties(physicalDevice, &properties);
|
||||||
|
|
||||||
|
SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "GPU: %s", properties.deviceName);
|
||||||
|
|
||||||
|
SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "- API Version %d.%d.%d",
|
||||||
|
VK_API_VERSION_MAJOR(properties.apiVersion),
|
||||||
|
VK_API_VERSION_MINOR(properties.apiVersion),
|
||||||
|
VK_API_VERSION_PATCH(properties.apiVersion));
|
||||||
|
|
||||||
|
constexpr static uint32_t API_PATCH_BITS = 0xFFF;
|
||||||
|
if ((properties.apiVersion & (~API_PATCH_BITS)) < VK_API_VERSION_1_3)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t queueFamilyCount;
|
||||||
|
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, nullptr);
|
||||||
|
VkQueueFamilyProperties* queueFamilyProperties = reinterpret_cast<VkQueueFamilyProperties*>(mem->allocate(sizeof(VkQueueFamilyProperties) * queueFamilyCount));
|
||||||
|
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, queueFamilyProperties);
|
||||||
|
|
||||||
|
for (uint32_t queueFamilyIndex = 0; queueFamilyIndex != queueFamilyCount;
|
||||||
|
++queueFamilyIndex)
|
||||||
|
{
|
||||||
|
VkQueueFamilyProperties const& qfp = queueFamilyProperties[queueFamilyIndex];
|
||||||
|
|
||||||
|
bool hasGraphicsSupport = false;
|
||||||
|
bool hasComputeSupport = false;
|
||||||
|
bool hasTransferSupport = false;
|
||||||
|
bool hasPresentSupport = false;
|
||||||
|
|
||||||
|
SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "- Queue [%d]", queueFamilyIndex);
|
||||||
|
if (qfp.queueFlags & VK_QUEUE_GRAPHICS_BIT)
|
||||||
|
{
|
||||||
|
hasGraphicsSupport = true;
|
||||||
|
SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "-- Graphic");
|
||||||
|
}
|
||||||
|
if (qfp.queueFlags & VK_QUEUE_COMPUTE_BIT)
|
||||||
|
{
|
||||||
|
hasComputeSupport = true;
|
||||||
|
SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "-- Compute");
|
||||||
|
}
|
||||||
|
if (qfp.queueFlags & VK_QUEUE_TRANSFER_BIT)
|
||||||
|
{
|
||||||
|
hasTransferSupport = true;
|
||||||
|
SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "-- Transfer");
|
||||||
|
}
|
||||||
|
|
||||||
|
VkBool32 isSurfaceSupported;
|
||||||
|
VK_CHECK(vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, queueFamilyIndex, surface, &isSurfaceSupported));
|
||||||
|
|
||||||
|
if (isSurfaceSupported)
|
||||||
|
{
|
||||||
|
hasPresentSupport = true;
|
||||||
|
SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "-- Present");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasGraphicsSupport and hasComputeSupport and hasTransferSupport and hasPresentSupport)
|
||||||
|
{
|
||||||
|
physicalDeviceInUse = physicalDevice;
|
||||||
|
directQueueFamilyIndex = queueFamilyIndex;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
mem->restoreState(tempAllocQueueProperties);
|
||||||
|
}
|
||||||
|
|
||||||
|
ASSERT(physicalDeviceInUse);
|
||||||
|
ASSERT(directQueueFamilyIndex.has_value());
|
||||||
|
|
||||||
|
float priority = 1.0f;
|
||||||
|
VkDeviceQueueCreateInfo queueCreateInfo = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = 0,
|
||||||
|
.queueFamilyIndex = directQueueFamilyIndex.value(),
|
||||||
|
.queueCount = 1,
|
||||||
|
.pQueuePriorities = &priority,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPhysicalDeviceVulkan13Features constexpr features13 = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.synchronization2 = true,
|
||||||
|
.dynamicRendering = true,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPhysicalDeviceFeatures features = {
|
||||||
|
.depthClamp = true,
|
||||||
|
.samplerAnisotropy = true,
|
||||||
|
};
|
||||||
|
|
||||||
|
std::array enabledDeviceExtensions = {
|
||||||
|
VK_KHR_SWAPCHAIN_EXTENSION_NAME
|
||||||
|
};
|
||||||
|
|
||||||
|
VkDeviceCreateInfo const deviceCreateInfo = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
|
||||||
|
.pNext = &features13,
|
||||||
|
.flags = 0,
|
||||||
|
.queueCreateInfoCount = 1,
|
||||||
|
.pQueueCreateInfos = &queueCreateInfo,
|
||||||
|
.enabledLayerCount = 0,
|
||||||
|
.ppEnabledLayerNames = nullptr,
|
||||||
|
.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size()),
|
||||||
|
.ppEnabledExtensionNames = enabledDeviceExtensions.data(),
|
||||||
|
.pEnabledFeatures = &features,
|
||||||
|
};
|
||||||
|
|
||||||
|
VK_CHECK(vkCreateDevice(physicalDeviceInUse, &deviceCreateInfo, nullptr, &device));
|
||||||
|
volkLoadDevice(device);
|
||||||
|
|
||||||
|
VmaAllocatorCreateInfo allocatorCreateInfo = {
|
||||||
|
.flags = 0,
|
||||||
|
.physicalDevice = physicalDeviceInUse,
|
||||||
|
.device = device,
|
||||||
|
.preferredLargeHeapBlockSize = 0,
|
||||||
|
.pAllocationCallbacks = nullptr,
|
||||||
|
.pDeviceMemoryCallbacks = nullptr,
|
||||||
|
.pHeapSizeLimit = nullptr,
|
||||||
|
.pVulkanFunctions = nullptr,
|
||||||
|
.instance = instance,
|
||||||
|
.vulkanApiVersion = VK_API_VERSION_1_3,
|
||||||
|
.pTypeExternalMemoryHandleTypes = nullptr,
|
||||||
|
};
|
||||||
|
|
||||||
|
VmaVulkanFunctions vkFunctions;
|
||||||
|
VK_CHECK(vmaImportVulkanFunctionsFromVolk(&allocatorCreateInfo, &vkFunctions));
|
||||||
|
allocatorCreateInfo.pVulkanFunctions = &vkFunctions;
|
||||||
|
|
||||||
|
VK_CHECK(vmaCreateAllocator(&allocatorCreateInfo, &gpuAllocator));
|
||||||
|
|
||||||
|
vkGetDeviceQueue(device, directQueueFamilyIndex.value(), 0, &directQueue);
|
||||||
|
|
||||||
|
mem->restoreState(tempAllocStart);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Swapchain creation
|
||||||
|
VkExtent2D swapchainExtent = { createInfo.width, createInfo.height };
|
||||||
|
VkFormat swapchainFormat = VK_FORMAT_UNDEFINED;
|
||||||
|
VkSwapchainKHR swapchain;
|
||||||
|
VkImage* swapchainImages;
|
||||||
|
VkImageView* swapchainViews;
|
||||||
|
uint32_t swapchainImageCount;
|
||||||
|
{
|
||||||
|
auto tempAllocStart = mem->getState();
|
||||||
|
|
||||||
|
VkSurfaceCapabilitiesKHR capabilities;
|
||||||
|
VK_CHECK(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDeviceInUse, surface, &capabilities));
|
||||||
|
|
||||||
|
// Image Count Calculation
|
||||||
|
swapchainImageCount = 3;
|
||||||
|
if (capabilities.maxImageCount > 0)
|
||||||
|
{
|
||||||
|
swapchainImageCount = std::min(swapchainImageCount, capabilities.maxImageCount);
|
||||||
|
}
|
||||||
|
swapchainImageCount = std::max(swapchainImageCount, capabilities.minImageCount + 1);
|
||||||
|
|
||||||
|
// Image Size calculation
|
||||||
|
{
|
||||||
|
auto [minWidth, minHeight] = capabilities.minImageExtent;
|
||||||
|
auto [maxWidth, maxHeight] = capabilities.maxImageExtent;
|
||||||
|
swapchainExtent.width = Clamp(swapchainExtent.width, minWidth, maxWidth);
|
||||||
|
swapchainExtent.height = Clamp(swapchainExtent.height, minHeight, maxHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t surfaceFormatCount;
|
||||||
|
vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDeviceInUse, surface, &surfaceFormatCount, nullptr);
|
||||||
|
VkSurfaceFormatKHR* surfaceFormats = reinterpret_cast<VkSurfaceFormatKHR*>(mem->allocate(sizeof(VkSurfaceFormatKHR*) * surfaceFormatCount));
|
||||||
|
vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDeviceInUse, surface, &surfaceFormatCount, surfaceFormats);
|
||||||
|
|
||||||
|
VkSurfaceFormatKHR format = {
|
||||||
|
.format = VK_FORMAT_UNDEFINED,
|
||||||
|
.colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR,
|
||||||
|
};
|
||||||
|
for (auto& surfaceFormat : std::span{ surfaceFormats, surfaceFormatCount })
|
||||||
|
{
|
||||||
|
if (surfaceFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
|
||||||
|
{
|
||||||
|
SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "Color Space SRGB Found %d", surfaceFormat.format);
|
||||||
|
if (surfaceFormat.format == VK_FORMAT_R8G8B8A8_SRGB) {
|
||||||
|
format = surfaceFormat;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (surfaceFormat.format == VK_FORMAT_B8G8R8A8_SRGB) {
|
||||||
|
format = surfaceFormat;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (surfaceFormat.format == VK_FORMAT_R8G8B8A8_UNORM) {
|
||||||
|
format = surfaceFormat;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ASSERT(format.format != VK_FORMAT_UNDEFINED);
|
||||||
|
swapchainFormat = format.format;
|
||||||
|
|
||||||
|
uint32_t presentModeCount;
|
||||||
|
vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDeviceInUse, surface, &presentModeCount, nullptr);
|
||||||
|
VkPresentModeKHR* presentModes = reinterpret_cast<VkPresentModeKHR*>(mem->allocate(sizeof(VkPresentModeKHR*) * presentModeCount));
|
||||||
|
vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDeviceInUse, surface, &presentModeCount, presentModes);
|
||||||
|
|
||||||
|
VkPresentModeKHR presentMode = VK_PRESENT_MODE_FIFO_KHR;
|
||||||
|
for (VkPresentModeKHR presentModeIter : std::span{ presentModes, presentModeCount })
|
||||||
|
{
|
||||||
|
if (presentModeIter == VK_PRESENT_MODE_FIFO_RELAXED_KHR)
|
||||||
|
{
|
||||||
|
presentMode = presentModeIter;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (presentModeIter == VK_PRESENT_MODE_MAILBOX_KHR)
|
||||||
|
{
|
||||||
|
presentMode = presentModeIter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mem->restoreState(tempAllocStart);
|
||||||
|
|
||||||
|
VkSwapchainCreateInfoKHR const swapchainCreateInfo = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = 0,
|
||||||
|
.surface = surface,
|
||||||
|
.minImageCount = swapchainImageCount,
|
||||||
|
.imageFormat = format.format,
|
||||||
|
.imageColorSpace = format.colorSpace,
|
||||||
|
.imageExtent = swapchainExtent,
|
||||||
|
.imageArrayLayers = 1,
|
||||||
|
.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
|
||||||
|
.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
|
||||||
|
.queueFamilyIndexCount = 0,
|
||||||
|
.pQueueFamilyIndices = nullptr,
|
||||||
|
.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR,
|
||||||
|
.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
|
||||||
|
.presentMode = presentMode,
|
||||||
|
.clipped = false,
|
||||||
|
.oldSwapchain = nullptr,
|
||||||
|
};
|
||||||
|
|
||||||
|
VK_CHECK(vkCreateSwapchainKHR(device, &swapchainCreateInfo, nullptr, &swapchain));
|
||||||
|
|
||||||
|
swapchainImageCount = 0;
|
||||||
|
vkGetSwapchainImagesKHR(device, swapchain, &swapchainImageCount, nullptr);
|
||||||
|
swapchainImages = reinterpret_cast<VkImage*>(mem->allocate(sizeof(VkImage) * swapchainImageCount));
|
||||||
|
vkGetSwapchainImagesKHR(device, swapchain, &swapchainImageCount, swapchainImages);
|
||||||
|
|
||||||
|
swapchainViews = reinterpret_cast<VkImageView*>(mem->allocate(sizeof(VkImageView) * swapchainImageCount));
|
||||||
|
for (uint32_t i = 0; i != swapchainImageCount; ++i) {
|
||||||
|
VkImageViewCreateInfo const viewCreateInfo = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = 0,
|
||||||
|
.image = swapchainImages[i],
|
||||||
|
.viewType = VK_IMAGE_VIEW_TYPE_2D,
|
||||||
|
.format = format.format,
|
||||||
|
.components = {
|
||||||
|
VK_COMPONENT_SWIZZLE_IDENTITY,
|
||||||
|
VK_COMPONENT_SWIZZLE_IDENTITY,
|
||||||
|
VK_COMPONENT_SWIZZLE_IDENTITY,
|
||||||
|
VK_COMPONENT_SWIZZLE_IDENTITY
|
||||||
|
},
|
||||||
|
.subresourceRange = {
|
||||||
|
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
|
||||||
|
.baseMipLevel = 0,
|
||||||
|
.levelCount = 1,
|
||||||
|
.baseArrayLayer = 0,
|
||||||
|
.layerCount = 1,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
VK_CHECK(vkCreateImageView(device, &viewCreateInfo, nullptr, &swapchainViews[i]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init frames.
|
||||||
|
|
||||||
|
Frame* frames = reinterpret_cast<Frame*>(mem->allocate(sizeof(Frame) * swapchainImageCount));
|
||||||
|
for (uint32_t i = 0; i != swapchainImageCount; ++i)
|
||||||
|
{
|
||||||
|
new (frames + i) Frame(device, directQueueFamilyIndex.value());
|
||||||
|
}
|
||||||
|
|
||||||
|
Byte* allocation = mem->allocate(sizeof(RenderDevice), alignof(RenderDevice));
|
||||||
|
return new (allocation) RenderDevice{
|
||||||
|
instance, surface, physicalDeviceInUse,
|
||||||
|
device, gpuAllocator, directQueue, directQueueFamilyIndex.value(),
|
||||||
|
swapchainFormat, swapchainExtent, swapchain, swapchainImages, swapchainViews, frames, swapchainImageCount,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool RenderDevice::isInit() const
|
||||||
|
{
|
||||||
|
return instance and device;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RenderDevice::cleanup()
|
||||||
|
{
|
||||||
|
if (not isInit())
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (Frame& frame : std::span{ frames, swapchainImageCount })
|
||||||
|
{
|
||||||
|
frame.cleanup(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto const& view : std::span{ swapchainViews, swapchainImageCount })
|
||||||
|
{
|
||||||
|
vkDestroyImageView(device, view, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
vkDestroySwapchainKHR(device, Take(swapchain), nullptr);
|
||||||
|
|
||||||
|
vkDestroyDevice(Take(device), nullptr);
|
||||||
|
|
||||||
|
SDL_Vulkan_DestroySurface(instance, Take(surface), nullptr);
|
||||||
|
|
||||||
|
vkDestroyInstance(Take(instance), nullptr);
|
||||||
|
|
||||||
|
volkFinalize();
|
||||||
|
}
|
||||||
|
|
||||||
|
void RenderDevice::waitIdle() const
|
||||||
|
{
|
||||||
|
VK_CHECK(vkDeviceWaitIdle(device));
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t RenderDevice::getNumFrames() const
|
||||||
|
{
|
||||||
|
return swapchainImageCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
RenderDevice::RenderDevice(VkInstance const instance, VkSurfaceKHR const surface, VkPhysicalDevice const physicalDeviceInUse,
|
||||||
|
VkDevice const device, VmaAllocator gpuAllocator, VkQueue const directQueue, uint32_t const directQueueFamilyIndex,
|
||||||
|
VkFormat const swapchainFormat, VkExtent2D const swapchainExtent, VkSwapchainKHR const swapchain, VkImage* swapchainImages,
|
||||||
|
VkImageView* swapchainViews, Frame* frames, uint32_t const swapchainImageCount)
|
||||||
|
: instance{ instance }
|
||||||
|
, surface{ surface }
|
||||||
|
, physicalDeviceInUse{ physicalDeviceInUse }
|
||||||
|
, device{ device }
|
||||||
|
, gpuAllocator{ gpuAllocator }
|
||||||
|
, directQueue{ directQueue }
|
||||||
|
, directQueueFamilyIndex{ directQueueFamilyIndex }
|
||||||
|
, swapchainFormat{ swapchainFormat }
|
||||||
|
, swapchainExtent{ swapchainExtent }
|
||||||
|
, swapchain{ swapchain }
|
||||||
|
, swapchainImages{ swapchainImages }
|
||||||
|
, swapchainViews{ swapchainViews }
|
||||||
|
, frames{ frames }
|
||||||
|
, swapchainImageCount{ swapchainImageCount }
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,80 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <volk.h>
|
||||||
|
|
||||||
|
#define VMA_STATIC_VULKAN_FUNCTIONS 0
|
||||||
|
#define VMA_DYNAMIC_VULKAN_FUNCTIONS 0
|
||||||
|
#include <vma/vk_mem_alloc.h>
|
||||||
|
|
||||||
|
#include <SDL3/SDL_video.h>
|
||||||
|
#include <SDL3/SDL_vulkan.h>
|
||||||
|
|
||||||
|
struct GlobalMemory;
|
||||||
|
struct Frame;
|
||||||
|
|
||||||
|
/// The Rendering backend abstraction
|
||||||
|
/// If this fails to initialize, we crash
|
||||||
|
///
|
||||||
|
/// TODO: Fail elegantly.
|
||||||
|
struct RenderDevice
|
||||||
|
{
|
||||||
|
struct CreateInfo
|
||||||
|
{
|
||||||
|
SDL_Window* window = nullptr;
|
||||||
|
uint32_t width = 640;
|
||||||
|
uint32_t height = 480;
|
||||||
|
};
|
||||||
|
|
||||||
|
VkInstance instance;
|
||||||
|
VkSurfaceKHR surface;
|
||||||
|
VkPhysicalDevice physicalDeviceInUse;
|
||||||
|
|
||||||
|
VkDevice device;
|
||||||
|
VmaAllocator gpuAllocator;
|
||||||
|
VkQueue directQueue;
|
||||||
|
uint32_t directQueueFamilyIndex;
|
||||||
|
|
||||||
|
VkFormat swapchainFormat;
|
||||||
|
VkExtent2D swapchainExtent;
|
||||||
|
VkSwapchainKHR swapchain;
|
||||||
|
VkImage* swapchainImages;
|
||||||
|
VkImageView* swapchainViews;
|
||||||
|
Frame* frames;
|
||||||
|
uint32_t swapchainImageCount;
|
||||||
|
uint32_t frameIndex = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] bool isInit() const;
|
||||||
|
void cleanup();
|
||||||
|
void waitIdle() const;
|
||||||
|
[[nodiscard]] uint32_t getNumFrames() const;
|
||||||
|
|
||||||
|
RenderDevice(
|
||||||
|
VkInstance instance,
|
||||||
|
VkSurfaceKHR surface,
|
||||||
|
VkPhysicalDevice physicalDeviceInUse,
|
||||||
|
VkDevice device,
|
||||||
|
VmaAllocator gpuAllocator,
|
||||||
|
VkQueue directQueue,
|
||||||
|
uint32_t directQueueFamilyIndex,
|
||||||
|
// TODO: Pack?
|
||||||
|
|
||||||
|
VkFormat swapchainFormat,
|
||||||
|
VkExtent2D swapchainExtent,
|
||||||
|
VkSwapchainKHR swapchain,
|
||||||
|
|
||||||
|
VkImage* swapchainImages,
|
||||||
|
VkImageView* swapchainViews,
|
||||||
|
Frame* frames,
|
||||||
|
uint32_t swapchainImageCount
|
||||||
|
);
|
||||||
|
|
||||||
|
RenderDevice(RenderDevice const&) = delete;
|
||||||
|
RenderDevice& operator=(RenderDevice const&) = delete;
|
||||||
|
|
||||||
|
RenderDevice(RenderDevice&&) noexcept = delete;
|
||||||
|
RenderDevice& operator=(RenderDevice&&) noexcept = delete;
|
||||||
|
|
||||||
|
~RenderDevice();
|
||||||
|
};
|
||||||
|
|
||||||
|
RenderDevice* CreateRenderDevice(GlobalMemory* mem, RenderDevice::CreateInfo const& createInfo);
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
#include <volk.h>
|
||||||
|
|
||||||
|
#pragma warning(push, 1)
|
||||||
|
#pragma warning(disable : 5045)
|
||||||
|
#define VMA_IMPLEMENTATION
|
||||||
|
#include <vma/vk_mem_alloc.h>
|
||||||
|
#pragma warning(pop)
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
{
|
{
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
"fmt",
|
|
||||||
"volk",
|
"volk",
|
||||||
"shader-slang",
|
"shader-slang",
|
||||||
|
"vulkan-memory-allocator",
|
||||||
{
|
{
|
||||||
"name": "sdl3",
|
"name": "sdl3",
|
||||||
"features": [ "vulkan" ]
|
"features": [ "vulkan" ]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue