Compare commits
2 Commits
76a7927643
...
eaf4556bad
| Author | SHA1 | Date |
|---|---|---|
|
|
eaf4556bad | |
|
|
c6ff2f4f76 |
|
|
@ -1,3 +1,4 @@
|
|||
.idea/
|
||||
.cache/
|
||||
build/
|
||||
.vs/
|
||||
|
|
@ -8,7 +8,15 @@ set(CMAKE_CXX_STANDARD 20)
|
|||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "-Wall -fno-rtti -fno-exceptions")
|
||||
if (MSVC)
|
||||
set(CMAKE_CXX_FLAGS "/W4 /GR- /Zi")
|
||||
add_compile_definitions(_HAS_EXCEPTIONS=1)
|
||||
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
|
||||
else ()
|
||||
set(CMAKE_CXX_FLAGS "-Wall -fno-rtti -fno-exceptions")
|
||||
endif ()
|
||||
|
||||
include(add_shader.cmake)
|
||||
|
||||
add_subdirectory("aster")
|
||||
add_subdirectory("triangle")
|
||||
|
|
|
|||
|
|
@ -12,6 +12,16 @@
|
|||
"CMAKE_CXX_COMPILER": "/usr/bin/clang++",
|
||||
"CMAKE_TOOLCHAIN_FILE": "${sourceDir}/vcpkg/scripts/buildsystems/vcpkg.cmake"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "windows",
|
||||
"generator": "Ninja",
|
||||
"binaryDir": "${sourceDir}/build",
|
||||
"cacheVariables": {
|
||||
"CMAKE_EXPORT_COMPILE_COMMANDS": true,
|
||||
"CMAKE_MAKE_PROGRAM": "ninja",
|
||||
"CMAKE_TOOLCHAIN_FILE": "${sourceDir}/vcpkg/scripts/buildsystems/vcpkg.cmake"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
function(add_shader TARGET SHADER)
|
||||
find_package(Vulkan REQUIRED COMPONENTS dxc)
|
||||
|
||||
get_filename_component(shader-ext ${SHADER} LAST_EXT)
|
||||
|
||||
set(current-shader-path ${CMAKE_CURRENT_SOURCE_DIR}/${SHADER})
|
||||
set(current-output-path ${CMAKE_CURRENT_BINARY_DIR}/${SHADER}.spv)
|
||||
|
||||
get_filename_component(current-output-dir ${current-output-path} DIRECTORY)
|
||||
file(MAKE_DIRECTORY ${current-output-dir})
|
||||
|
||||
if (Vulkan_dxc_exe_FOUND AND ${shader-ext} STREQUAL ".hlsl")
|
||||
message("Marked as hlsl file. ${current-output-path}")
|
||||
add_custom_command(
|
||||
OUTPUT ${current-output-path}
|
||||
COMMAND Vulkan::dxc_exe -spirv -T vs_6_0 -E main ${current-shader-path} -Fo ${current-output-path}
|
||||
DEPENDS ${current-shader-path}
|
||||
IMPLICIT_DEPENDS CXX ${current-shader-path}
|
||||
VERBATIM)
|
||||
elseif (Vulkan_glslc_FOUND AND ${shader-ext} STREQUAL ".glsl")
|
||||
message("Marked as glsl file. ${current-output-path}")
|
||||
add_custom_command(
|
||||
OUTPUT ${current-output-path}
|
||||
COMMAND Vulkan::glslc -o ${current-output-path} ${current-shader-path}
|
||||
DEPENDS ${current-shader-path}
|
||||
IMPLICIT_DEPENDS CXX ${current-shader-path}
|
||||
VERBATIM)
|
||||
endif ()
|
||||
|
||||
# Make sure our build depends on this output.
|
||||
set_source_files_properties(${current-output-path} PROPERTIES GENERATED TRUE)
|
||||
target_sources(${TARGET} PRIVATE ${current-output-path})
|
||||
endfunction(add_shader)
|
||||
|
|
@ -11,10 +11,12 @@
|
|||
#include <EASTL/array.h>
|
||||
#include <EASTL/fixed_vector.h>
|
||||
|
||||
constexpr eastl::array DEVICE_EXTENSIONS = {VK_KHR_SWAPCHAIN_EXTENSION_NAME};
|
||||
// TODO: This will need to be flexible for devices that don't support some of the extensions.
|
||||
constexpr eastl::array DEVICE_EXTENSIONS = {
|
||||
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
|
||||
};
|
||||
|
||||
Device::Device(const Context *context, PhysicalDevice *physicalDevice,
|
||||
const vk::PhysicalDeviceFeatures *enabledFeatures,
|
||||
Device::Device(const Context *context, PhysicalDevice *physicalDevice, Features *enabledFeatures,
|
||||
const eastl::vector<QueueAllocation> &queueAllocations, NameString &&name)
|
||||
: m_Name(std::move(name))
|
||||
, m_PhysicalDevice(physicalDevice->m_PhysicalDevice)
|
||||
|
|
@ -40,12 +42,21 @@ Device::Device(const Context *context, PhysicalDevice *physicalDevice,
|
|||
});
|
||||
}
|
||||
|
||||
vk::PhysicalDeviceFeatures *deviceFeatures = &enabledFeatures->m_Vulkan10Features;
|
||||
vk::PhysicalDeviceVulkan11Features *vulkan11Features = &enabledFeatures->m_Vulkan11Features;
|
||||
vk::PhysicalDeviceVulkan12Features *vulkan12Features = &enabledFeatures->m_Vulkan12Features;
|
||||
vk::PhysicalDeviceVulkan13Features *vulkan13Features = &enabledFeatures->m_Vulkan13Features;
|
||||
|
||||
vulkan11Features->pNext = vulkan12Features;
|
||||
vulkan12Features->pNext = vulkan13Features;
|
||||
|
||||
vk::DeviceCreateInfo deviceCreateInfo = {
|
||||
.pNext = vulkan11Features,
|
||||
.queueCreateInfoCount = Cast<u32>(deviceQueueCreateInfos.size()),
|
||||
.pQueueCreateInfos = deviceQueueCreateInfos.data(),
|
||||
.enabledExtensionCount = Cast<u32>(DEVICE_EXTENSIONS.size()),
|
||||
.ppEnabledExtensionNames = DEVICE_EXTENSIONS.data(),
|
||||
.pEnabledFeatures = enabledFeatures,
|
||||
.pEnabledFeatures = deviceFeatures,
|
||||
};
|
||||
|
||||
vk::Result result = m_PhysicalDevice.createDevice(&deviceCreateInfo, nullptr, &m_Device);
|
||||
|
|
@ -53,6 +64,8 @@ Device::Device(const Context *context, PhysicalDevice *physicalDevice,
|
|||
THEN_ABORT(result)
|
||||
ELSE_DEBUG("{} ({}) Initialized.", m_Name, physicalDevice->m_DeviceProperties.deviceName.data());
|
||||
|
||||
SetName(m_Device, m_Name.data());
|
||||
|
||||
VmaVulkanFunctions vmaVulkanFunctions = {
|
||||
.vkGetInstanceProcAddr = vk::defaultDispatchLoaderDynamic.vkGetInstanceProcAddr,
|
||||
.vkGetDeviceProcAddr = vk::defaultDispatchLoaderDynamic.vkGetDeviceProcAddr,
|
||||
|
|
@ -87,3 +100,11 @@ Device::~Device()
|
|||
DEBUG("Device '{}' Destroyed", m_Name);
|
||||
m_PhysicalDevice = nullptr;
|
||||
}
|
||||
|
||||
vk::Queue
|
||||
Device::GetQueue(const u32 familyIndex, const u32 queueIndex) const
|
||||
{
|
||||
vk::Queue queue;
|
||||
m_Device.getQueue(familyIndex, queueIndex, &queue);
|
||||
return queue;
|
||||
}
|
||||
|
|
@ -18,6 +18,14 @@ struct QueueAllocation
|
|||
u32 m_Count;
|
||||
};
|
||||
|
||||
struct Features
|
||||
{
|
||||
vk::PhysicalDeviceFeatures m_Vulkan10Features;
|
||||
vk::PhysicalDeviceVulkan11Features m_Vulkan11Features;
|
||||
vk::PhysicalDeviceVulkan12Features m_Vulkan12Features;
|
||||
vk::PhysicalDeviceVulkan13Features m_Vulkan13Features;
|
||||
};
|
||||
|
||||
struct Device final
|
||||
{
|
||||
NameString m_Name;
|
||||
|
|
@ -25,7 +33,28 @@ struct Device final
|
|||
vk::Device m_Device = nullptr;
|
||||
VmaAllocator m_Allocator = nullptr;
|
||||
|
||||
Device(const Context *context, PhysicalDevice *physicalDevice, const vk::PhysicalDeviceFeatures *enabledFeatures,
|
||||
Device(const Context *context, PhysicalDevice *physicalDevice, Features *enabledFeatures,
|
||||
const eastl::vector<QueueAllocation> &queueAllocations, NameString &&name);
|
||||
~Device();
|
||||
|
||||
template <typename T>
|
||||
requires vk::isVulkanHandleType<T>::value
|
||||
void SetName(const T &object, cstr name) const;
|
||||
[[nodiscard]] vk::Queue GetQueue(u32 familyIndex, u32 queueIndex) const;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
requires vk::isVulkanHandleType<T>::value
|
||||
void
|
||||
Device::SetName(const T &object, cstr name) const
|
||||
{
|
||||
auto handle = Recast<u64>(Cast<typename T::NativeType>(object));
|
||||
const vk::DebugUtilsObjectNameInfoEXT objectNameInfo = {
|
||||
.objectType = object.objectType,
|
||||
.objectHandle = handle,
|
||||
.pObjectName = name,
|
||||
};
|
||||
vk::Result result = m_Device.setDebugUtilsObjectNameEXT(&objectNameInfo);
|
||||
WARN_IF(Failed(result), "Could not name {:x}: {} as {}. Cause: {}", handle, to_string(object.objectType), name,
|
||||
result);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,14 +15,15 @@
|
|||
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
|
||||
|
||||
void *
|
||||
operator new[](size_t size, const char *pName, int flags, unsigned debugFlags, const char *file, int line)
|
||||
operator new[](size_t size, const char * /*pName*/, int /*flags*/, unsigned /*debugFlags*/, const char * /*file*/,
|
||||
int /*line*/)
|
||||
{
|
||||
return new u8[size];
|
||||
}
|
||||
|
||||
void *
|
||||
operator new[](size_t size, size_t alignment, size_t alignmentOffset, const char *pName, int flags, unsigned debugFlags,
|
||||
const char *file, int line)
|
||||
operator new[](size_t size, size_t /*alignment*/, size_t /*alignmentOffset*/, const char * /*pName*/, int /*flags*/,
|
||||
unsigned /*debugFlags*/, const char * /*file*/, int /*line*/)
|
||||
{
|
||||
return new u8[size];
|
||||
}
|
||||
|
|
@ -34,6 +34,8 @@ constexpr u32 ASTER_API_VERSION = vk::ApiVersion13;
|
|||
|
||||
#define CODE_LOC " @ " __FILE__ ":" VULKAN_HPP_STRINGIFY(__LINE__)
|
||||
|
||||
#define FORCE_TODO static_assert(false)
|
||||
|
||||
[[nodiscard]] inline bool
|
||||
Failed(const vk::Result result)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -42,7 +42,6 @@ struct Logger
|
|||
return "[DEBUG]:";
|
||||
if constexpr (TLogLevel == LogType::eVerbose)
|
||||
return "[VERB]: ";
|
||||
return "";
|
||||
}
|
||||
|
||||
template <LogType TLogLevel>
|
||||
|
|
@ -59,7 +58,6 @@ struct Logger
|
|||
return ansi_color::White;
|
||||
if constexpr (TLogLevel == LogType::eVerbose)
|
||||
return ansi_color::Blue;
|
||||
return ansi_color::White;
|
||||
}
|
||||
|
||||
template <LogType TLogLevel>
|
||||
|
|
|
|||
|
|
@ -18,12 +18,7 @@ Swapchain::Swapchain(const Window *window, const Device *device, NameString &&na
|
|||
|
||||
Swapchain::~Swapchain()
|
||||
{
|
||||
if (m_Swapchain)
|
||||
{
|
||||
m_Device->m_Device.destroy(m_Swapchain, nullptr);
|
||||
m_Swapchain = nullptr;
|
||||
DEBUG("Swapchain '{}' destroyed.", m_Name);
|
||||
}
|
||||
this->Cleanup();
|
||||
}
|
||||
|
||||
void
|
||||
|
|
@ -33,21 +28,21 @@ Swapchain::Create(const Window *window)
|
|||
auto surfaceFormats = GetSurfaceFormats(m_Device->m_PhysicalDevice, window->m_Surface);
|
||||
auto presentModes = GetSurfacePresentModes(m_Device->m_PhysicalDevice, window->m_Surface);
|
||||
|
||||
vk::Format swapchainFormat = vk::Format::eUndefined;
|
||||
m_Format = vk::Format::eUndefined;
|
||||
vk::ColorSpaceKHR swapchainColorSpace = vk::ColorSpaceKHR::eSrgbNonlinear;
|
||||
for (auto [format, colorSpace] : surfaceFormats)
|
||||
{
|
||||
if (format == vk::Format::eB8G8R8A8Srgb && colorSpace == vk::ColorSpaceKHR::eSrgbNonlinear)
|
||||
{
|
||||
swapchainFormat = format;
|
||||
m_Format = format;
|
||||
swapchainColorSpace = colorSpace;
|
||||
}
|
||||
}
|
||||
if (swapchainFormat == vk::Format::eUndefined)
|
||||
if (m_Format == vk::Format::eUndefined)
|
||||
{
|
||||
WARN("Preferred Swapchain format not found. Falling back.");
|
||||
auto [format, colorSpace] = surfaceFormats.front();
|
||||
swapchainFormat = format;
|
||||
m_Format = format;
|
||||
swapchainColorSpace = colorSpace;
|
||||
}
|
||||
|
||||
|
|
@ -61,10 +56,9 @@ Swapchain::Create(const Window *window)
|
|||
}
|
||||
}
|
||||
|
||||
vk::Extent2D swapchainExtent;
|
||||
if (surfaceCapabilities.currentExtent.width != MaxValue<u32>)
|
||||
{
|
||||
swapchainExtent = surfaceCapabilities.currentExtent;
|
||||
m_Extent = surfaceCapabilities.currentExtent;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -72,8 +66,8 @@ Swapchain::Create(const Window *window)
|
|||
auto [minWidth, minHeight] = surfaceCapabilities.minImageExtent;
|
||||
auto [maxWidth, maxHeight] = surfaceCapabilities.maxImageExtent;
|
||||
|
||||
swapchainExtent.width = glm::clamp(width, minWidth, maxWidth);
|
||||
swapchainExtent.height = glm::clamp(height, minHeight, maxHeight);
|
||||
m_Extent.width = glm::clamp(width, minWidth, maxWidth);
|
||||
m_Extent.height = glm::clamp(height, minHeight, maxHeight);
|
||||
}
|
||||
|
||||
u32 swapchainImageCount = 3;
|
||||
|
|
@ -88,9 +82,9 @@ Swapchain::Create(const Window *window)
|
|||
const vk::SwapchainCreateInfoKHR swapchainCreateInfo = {
|
||||
.surface = window->m_Surface,
|
||||
.minImageCount = swapchainImageCount,
|
||||
.imageFormat = swapchainFormat,
|
||||
.imageFormat = m_Format,
|
||||
.imageColorSpace = swapchainColorSpace,
|
||||
.imageExtent = swapchainExtent,
|
||||
.imageExtent = m_Extent,
|
||||
.imageArrayLayers = 1,
|
||||
.imageUsage = vk::ImageUsageFlagBits::eColorAttachment,
|
||||
.imageSharingMode = vk::SharingMode::eExclusive,
|
||||
|
|
@ -101,11 +95,77 @@ Swapchain::Create(const Window *window)
|
|||
.oldSwapchain = m_Swapchain,
|
||||
};
|
||||
|
||||
vk::Device device = m_Device->m_Device;
|
||||
|
||||
vk::SwapchainKHR swapchain;
|
||||
vk::Result result = m_Device->m_Device.createSwapchainKHR(&swapchainCreateInfo, nullptr, &swapchain);
|
||||
vk::Result result = device.createSwapchainKHR(&swapchainCreateInfo, nullptr, &swapchain);
|
||||
ERROR_IF(Failed(result), "Swapchain {} creation failed. Cause {}", m_Name, result)
|
||||
THEN_ABORT(result)
|
||||
ELSE_DEBUG("Created Swapchain '{}'", m_Name);
|
||||
|
||||
// Irrelevant on the first run. Required for re-creation.
|
||||
Cleanup();
|
||||
|
||||
m_Swapchain = swapchain;
|
||||
|
||||
m_Device->SetName(m_Swapchain, m_Name.data());
|
||||
|
||||
result = device.getSwapchainImagesKHR(m_Swapchain, &swapchainImageCount, nullptr);
|
||||
ERROR_IF(Failed(result), "Failed getting swapchain {}'s images. Cause {}", m_Name, result)
|
||||
THEN_ABORT(result);
|
||||
|
||||
// Managed by the Swapchain.
|
||||
m_Images.resize(swapchainImageCount);
|
||||
result = device.getSwapchainImagesKHR(m_Swapchain, &swapchainImageCount, m_Images.data());
|
||||
ERROR_IF(Failed(result), "Failed getting swapchain {}'s images. Cause {}", m_Name, result)
|
||||
THEN_ABORT(result);
|
||||
|
||||
vk::ImageViewCreateInfo viewCreateInfo = {
|
||||
.viewType = vk::ImageViewType::e2D,
|
||||
.format = m_Format,
|
||||
.components = {.r = vk::ComponentSwizzle::eIdentity,
|
||||
.g = vk::ComponentSwizzle::eIdentity,
|
||||
.b = vk::ComponentSwizzle::eIdentity,
|
||||
.a = vk::ComponentSwizzle::eIdentity},
|
||||
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1},
|
||||
};
|
||||
|
||||
u32 index = 0;
|
||||
for (auto image : m_Images)
|
||||
{
|
||||
viewCreateInfo.image = image;
|
||||
|
||||
vk::ImageView imageView;
|
||||
result = device.createImageView(&viewCreateInfo, nullptr, &imageView);
|
||||
ERROR_IF(Failed(result), "Failed creating swapchain {}'s image view [{}]. Cause {}", m_Name, index, result)
|
||||
THEN_ABORT(result);
|
||||
|
||||
m_ImageViews.push_back(imageView);
|
||||
|
||||
++index;
|
||||
}
|
||||
|
||||
DEBUG("Swapchain {} Image Views created.", m_Name);
|
||||
}
|
||||
|
||||
void
|
||||
Swapchain::Cleanup()
|
||||
{
|
||||
if (!m_ImageViews.empty()) // Don't want the condition in the logs.
|
||||
DEBUG("Swapchain {} Image Views destroyed.", m_Name);
|
||||
for (const auto imageView : m_ImageViews)
|
||||
{
|
||||
m_Device->m_Device.destroy(imageView, nullptr);
|
||||
}
|
||||
m_ImageViews.clear();
|
||||
if (m_Swapchain)
|
||||
{
|
||||
m_Device->m_Device.destroy(m_Swapchain, nullptr);
|
||||
m_Swapchain = nullptr;
|
||||
DEBUG("Swapchain '{}' destroyed.", m_Name);
|
||||
}
|
||||
}
|
||||
|
|
@ -7,6 +7,8 @@
|
|||
|
||||
#include "global.h"
|
||||
|
||||
#include <EASTL/fixed_vector.h>
|
||||
|
||||
struct PhysicalDevice;
|
||||
struct Window;
|
||||
struct Device;
|
||||
|
|
@ -16,8 +18,15 @@ struct Swapchain final
|
|||
const Device *m_Device;
|
||||
vk::SwapchainKHR m_Swapchain;
|
||||
NameString m_Name;
|
||||
vk::Extent2D m_Extent;
|
||||
vk::Format m_Format;
|
||||
eastl::fixed_vector<vk::Image, 4> m_Images;
|
||||
eastl::fixed_vector<vk::ImageView, 4> m_ImageViews;
|
||||
|
||||
Swapchain(const Window *window, const Device *device, NameString &&name);
|
||||
~Swapchain();
|
||||
void Create(const Window *window);
|
||||
|
||||
private:
|
||||
void Cleanup();
|
||||
};
|
||||
|
|
|
|||
|
|
@ -2,7 +2,10 @@
|
|||
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
|
||||
add_executable(aster_exe "aster.cpp")
|
||||
add_dependencies(aster_exe aster_core)
|
||||
add_executable(triangle "triangle.cpp")
|
||||
add_dependencies(triangle aster_core)
|
||||
|
||||
target_link_libraries(aster_exe PRIVATE aster_core)
|
||||
add_shader(triangle shader/triangle.vs.hlsl)
|
||||
add_shader(triangle shader/white.frag.glsl)
|
||||
|
||||
target_link_libraries(triangle PRIVATE aster_core)
|
||||
|
|
|
|||
|
|
@ -1,90 +0,0 @@
|
|||
#include "aster/constants.h"
|
||||
#include "aster/context.h"
|
||||
#include "aster/device.h"
|
||||
#include "aster/glfw_context.h"
|
||||
#include "aster/physical_device.h"
|
||||
#include "aster/window.h"
|
||||
|
||||
#include "aster/global.h"
|
||||
#include "aster/swapchain.h"
|
||||
|
||||
constexpr QueueSupportFlags REQUIRED_QUEUE_SUPPORT = QueueSupportFlags{} | QueueSupportFlagBits::eGraphics |
|
||||
QueueSupportFlagBits::eCompute | QueueSupportFlagBits::ePresent |
|
||||
QueueSupportFlagBits::eTransfer;
|
||||
|
||||
[[nodiscard]] bool
|
||||
IsSuitableDevice(const PhysicalDevice *physicalDevice)
|
||||
{
|
||||
const bool hasAllRequiredQueues =
|
||||
std::ranges::any_of(physicalDevice->m_QueueFamilies, [](const auto &queueFamilyProp) {
|
||||
return (queueFamilyProp.m_Support & REQUIRED_QUEUE_SUPPORT) == REQUIRED_QUEUE_SUPPORT;
|
||||
});
|
||||
|
||||
const bool isNotCpu = physicalDevice->m_DeviceProperties.deviceType != vk::PhysicalDeviceType::eCpu;
|
||||
|
||||
const bool hasPresentMode = !physicalDevice->m_PresentModes.empty();
|
||||
|
||||
const bool hasSurfaceFormat = !physicalDevice->m_SurfaceFormats.empty();
|
||||
|
||||
return hasSurfaceFormat && hasPresentMode && isNotCpu && hasAllRequiredQueues;
|
||||
}
|
||||
|
||||
PhysicalDevice
|
||||
FindSuitableDevice(const PhysicalDevices &physicalDevices)
|
||||
{
|
||||
for (auto &physicalDevice : physicalDevices)
|
||||
{
|
||||
if (IsSuitableDevice(&physicalDevice))
|
||||
{
|
||||
return physicalDevice;
|
||||
}
|
||||
}
|
||||
|
||||
ERROR("No suitable GPU available on the system.")
|
||||
THEN_ABORT(vk::Result::eErrorUnknown);
|
||||
}
|
||||
|
||||
QueueAllocation
|
||||
FindAppropriateQueueAllocation(const PhysicalDevice *physicalDevice)
|
||||
{
|
||||
for (auto &queueFamilyInfo : physicalDevice->m_QueueFamilies)
|
||||
{
|
||||
if ((queueFamilyInfo.m_Support & REQUIRED_QUEUE_SUPPORT) == REQUIRED_QUEUE_SUPPORT)
|
||||
{
|
||||
return {
|
||||
.m_Family = queueFamilyInfo.m_Index,
|
||||
.m_Count = queueFamilyInfo.m_Count,
|
||||
};
|
||||
}
|
||||
}
|
||||
ERROR("No suitable queue family on the GPU.")
|
||||
THEN_ABORT(vk::Result::eErrorUnknown);
|
||||
}
|
||||
|
||||
int
|
||||
main(int, char **)
|
||||
{
|
||||
MIN_LOG_LEVEL(Logger::LogType::eDebug);
|
||||
|
||||
GlfwContext glfwContext = {};
|
||||
Context context = {"Aster", VERSION};
|
||||
Window window = {"Aster1", &context, {640, 480}};
|
||||
|
||||
PhysicalDevices physicalDevices = {&window, &context};
|
||||
PhysicalDevice deviceToUse = FindSuitableDevice(physicalDevices);
|
||||
|
||||
INFO("Using {} as the primary device.", deviceToUse.m_DeviceProperties.deviceName.data());
|
||||
|
||||
vk::PhysicalDeviceFeatures features = {};
|
||||
QueueAllocation queueAllocation = FindAppropriateQueueAllocation(&deviceToUse);
|
||||
|
||||
Device device = {&context, &deviceToUse, &features, {queueAllocation}, "Primary Device"};
|
||||
|
||||
Swapchain swapchain = {&window, &device, "Primary Chain"};
|
||||
|
||||
while (window.Poll())
|
||||
{
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
struct VSIn {
|
||||
int idx : SV_VERTEXID;
|
||||
};
|
||||
|
||||
struct VSOut
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
[[vk::location(0)]] float3 Color : COLOR0;
|
||||
};
|
||||
|
||||
VSOut main(VSIn input) {
|
||||
float3 points[] = {
|
||||
float3(-0.5f, -0.5f, 0.0f),
|
||||
float3(0.5f, -0.5f, 0.0f),
|
||||
float3(0.0f, 0.5f, 0.0f)
|
||||
};
|
||||
float3 colors[] = {
|
||||
float3( 1.0f, 0.0f, 0.0f ),
|
||||
float3( 0.0f, 1.0f, 0.0f ),
|
||||
float3( 0.0f, 0.0f, 1.0f ),
|
||||
};
|
||||
|
||||
VSOut output;
|
||||
output.Pos = float4(points[input.idx], 1.0f);
|
||||
output.Color = colors[input.idx];
|
||||
|
||||
return output;
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
#version 450
|
||||
#pragma shader_stage(fragment)
|
||||
|
||||
layout (location = 0) in vec3 inColor;
|
||||
layout (location = 0) out vec4 outColor;
|
||||
|
||||
void main() {
|
||||
outColor = vec4(inColor, 1.0);
|
||||
}
|
||||
|
|
@ -0,0 +1,521 @@
|
|||
#include "aster/constants.h"
|
||||
#include "aster/context.h"
|
||||
#include "aster/device.h"
|
||||
#include "aster/glfw_context.h"
|
||||
#include "aster/physical_device.h"
|
||||
#include "aster/window.h"
|
||||
|
||||
#include "aster/global.h"
|
||||
#include "aster/swapchain.h"
|
||||
|
||||
#include <EASTL/array.h>
|
||||
#include <EASTL/optional.h>
|
||||
|
||||
constexpr u32 MAX_FRAMES_IN_FLIGHT = 3;
|
||||
|
||||
bool IsSuitableDevice(const PhysicalDevice *physicalDevice);
|
||||
PhysicalDevice FindSuitableDevice(const PhysicalDevices &physicalDevices);
|
||||
QueueAllocation FindAppropriateQueueAllocation(const PhysicalDevice *physicalDevice);
|
||||
eastl::optional<eastl::vector<u32>> ReadFile(cstr fileName);
|
||||
vk::ShaderModule CreateShader(const Device *device, cstr shaderFile);
|
||||
|
||||
struct Frame
|
||||
{
|
||||
const Device *m_Device;
|
||||
vk::CommandPool m_Pool;
|
||||
vk::Fence m_FrameAvailableFence;
|
||||
vk::Semaphore m_ImageAcquireSem;
|
||||
vk::Semaphore m_RenderFinishSem;
|
||||
|
||||
Frame(const Device *device, u32 queueFamilyIndex, u32 frameCount);
|
||||
~Frame();
|
||||
|
||||
[[nodiscard]] vk::CommandBuffer AllocateCommandBuffer() const;
|
||||
};
|
||||
|
||||
struct Shader
|
||||
{
|
||||
vk::ShaderModule m_ShaderModule;
|
||||
vk::ShaderStageFlags m_Stage;
|
||||
};
|
||||
|
||||
int
|
||||
main(int, char **)
|
||||
{
|
||||
MIN_LOG_LEVEL(Logger::LogType::eVerbose);
|
||||
|
||||
GlfwContext glfwContext = {};
|
||||
Context context = {"Aster", VERSION};
|
||||
Window window = {"Aster1", &context, {640, 480}};
|
||||
|
||||
PhysicalDevices physicalDevices = {&window, &context};
|
||||
PhysicalDevice deviceToUse = FindSuitableDevice(physicalDevices);
|
||||
|
||||
INFO("Using {} as the primary device.", deviceToUse.m_DeviceProperties.deviceName.data());
|
||||
|
||||
Features enabledDeviceFeatures = {.m_Vulkan13Features = {.dynamicRendering = vk::True}};
|
||||
QueueAllocation queueAllocation = FindAppropriateQueueAllocation(&deviceToUse);
|
||||
Device device = {&context, &deviceToUse, &enabledDeviceFeatures, {queueAllocation}, "Primary Device"};
|
||||
|
||||
vk::Queue commandQueue = device.GetQueue(queueAllocation.m_Family, 1);
|
||||
|
||||
Swapchain swapchain = {&window, &device, "Primary Chain"};
|
||||
|
||||
auto vertexShaderModule = CreateShader(&device, "shader/triangle.vs.hlsl.spv");
|
||||
auto fragmentShaderModule = CreateShader(&device, "shader/white.frag.glsl.spv");
|
||||
|
||||
eastl::array<vk::PipelineShaderStageCreateInfo, 2> shaderStages = {{
|
||||
{
|
||||
.stage = vk::ShaderStageFlagBits::eVertex,
|
||||
.module = vertexShaderModule,
|
||||
.pName = "main",
|
||||
},
|
||||
{
|
||||
.stage = vk::ShaderStageFlagBits::eFragment,
|
||||
.module = fragmentShaderModule,
|
||||
.pName = "main",
|
||||
},
|
||||
}};
|
||||
|
||||
vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo = {
|
||||
.setLayoutCount = 0,
|
||||
.pSetLayouts = nullptr,
|
||||
.pushConstantRangeCount = 0,
|
||||
.pPushConstantRanges = nullptr,
|
||||
};
|
||||
vk::PipelineLayout pipelineLayout;
|
||||
vk::Result result = device.m_Device.createPipelineLayout(&pipelineLayoutCreateInfo, nullptr, &pipelineLayout);
|
||||
ERROR_IF(Failed(result), "Could not create a pipeline layout. Cause: {}", result) THEN_ABORT(result);
|
||||
device.SetName(pipelineLayout, "Triangle Layout");
|
||||
|
||||
vk::PipelineVertexInputStateCreateInfo vertexInputStateCreateInfo = {
|
||||
.vertexBindingDescriptionCount = 0,
|
||||
.pVertexBindingDescriptions = nullptr,
|
||||
.vertexAttributeDescriptionCount = 0,
|
||||
.pVertexAttributeDescriptions = nullptr,
|
||||
};
|
||||
vk::PipelineInputAssemblyStateCreateInfo inputAssemblyStateCreateInfo = {
|
||||
.topology = vk::PrimitiveTopology::eTriangleList,
|
||||
.primitiveRestartEnable = vk::False,
|
||||
};
|
||||
|
||||
vk::PipelineViewportStateCreateInfo viewportStateCreateInfo = {
|
||||
.viewportCount = 1,
|
||||
.scissorCount = 1,
|
||||
};
|
||||
|
||||
vk::PipelineRasterizationStateCreateInfo rasterizationStateCreateInfo = {
|
||||
.depthClampEnable = vk::False,
|
||||
.rasterizerDiscardEnable = vk::False,
|
||||
.polygonMode = vk::PolygonMode::eFill,
|
||||
.cullMode = vk::CullModeFlagBits::eNone,
|
||||
.frontFace = vk::FrontFace::eCounterClockwise,
|
||||
.depthBiasEnable = vk::False,
|
||||
.lineWidth = 1.0,
|
||||
};
|
||||
vk::PipelineMultisampleStateCreateInfo multisampleStateCreateInfo = {
|
||||
.rasterizationSamples = vk::SampleCountFlagBits::e1,
|
||||
.sampleShadingEnable = vk::False,
|
||||
};
|
||||
vk::PipelineDepthStencilStateCreateInfo depthStencilStateCreateInfo = {
|
||||
.depthTestEnable = vk::False,
|
||||
.depthWriteEnable = vk::False,
|
||||
};
|
||||
vk::PipelineColorBlendAttachmentState colorBlendAttachmentState = {
|
||||
.blendEnable = vk::False,
|
||||
.srcColorBlendFactor = vk::BlendFactor::eSrcColor,
|
||||
.dstColorBlendFactor = vk::BlendFactor::eOneMinusSrcColor,
|
||||
.colorBlendOp = vk::BlendOp::eAdd,
|
||||
.srcAlphaBlendFactor = vk::BlendFactor::eSrcAlpha,
|
||||
.dstAlphaBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha,
|
||||
.alphaBlendOp = vk::BlendOp::eAdd,
|
||||
.colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG |
|
||||
vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA,
|
||||
};
|
||||
vk::PipelineColorBlendStateCreateInfo colorBlendStateCreateInfo = {
|
||||
.logicOpEnable = vk::False,
|
||||
.attachmentCount = 1,
|
||||
.pAttachments = &colorBlendAttachmentState,
|
||||
};
|
||||
|
||||
eastl::array dynamicStates = {
|
||||
vk::DynamicState::eScissor,
|
||||
vk::DynamicState::eViewport,
|
||||
};
|
||||
|
||||
vk::PipelineDynamicStateCreateInfo dynamicStateCreateInfo = {
|
||||
.dynamicStateCount = Cast<u32>(dynamicStates.size()),
|
||||
.pDynamicStates = dynamicStates.data(),
|
||||
};
|
||||
|
||||
vk::PipelineRenderingCreateInfo renderingCreateInfo = {
|
||||
.viewMask = 0,
|
||||
.colorAttachmentCount = 1,
|
||||
.pColorAttachmentFormats = &swapchain.m_Format,
|
||||
};
|
||||
|
||||
vk::GraphicsPipelineCreateInfo pipelineCreateInfo = {
|
||||
.pNext = &renderingCreateInfo,
|
||||
.stageCount = Cast<u32>(shaderStages.size()),
|
||||
.pStages = shaderStages.data(),
|
||||
.pVertexInputState = &vertexInputStateCreateInfo,
|
||||
.pInputAssemblyState = &inputAssemblyStateCreateInfo,
|
||||
.pViewportState = &viewportStateCreateInfo,
|
||||
.pRasterizationState = &rasterizationStateCreateInfo,
|
||||
.pMultisampleState = &multisampleStateCreateInfo,
|
||||
.pDepthStencilState = &depthStencilStateCreateInfo,
|
||||
.pColorBlendState = &colorBlendStateCreateInfo,
|
||||
.pDynamicState = &dynamicStateCreateInfo,
|
||||
.layout = pipelineLayout,
|
||||
};
|
||||
vk::Pipeline pipeline;
|
||||
result = device.m_Device.createGraphicsPipelines(nullptr, 1, &pipelineCreateInfo, nullptr, &pipeline);
|
||||
ERROR_IF(Failed(result), "Could not create a graphics pipeline. Cause: {}", result) THEN_ABORT(result);
|
||||
device.SetName(pipelineLayout, "Triangle Pipeline");
|
||||
|
||||
// Frames
|
||||
eastl::fixed_vector<Frame, MAX_FRAMES_IN_FLIGHT> frames;
|
||||
for (int i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i)
|
||||
{
|
||||
frames.emplace_back(&device, queueAllocation.m_Family, i);
|
||||
}
|
||||
|
||||
vk::Viewport viewport = {
|
||||
.x = 0,
|
||||
.y = Cast<f32>(swapchain.m_Extent.height),
|
||||
.width = Cast<f32>(swapchain.m_Extent.width),
|
||||
.height = -Cast<f32>(swapchain.m_Extent.height),
|
||||
.minDepth = 0.0,
|
||||
.maxDepth = 1.0,
|
||||
};
|
||||
|
||||
vk::Rect2D scissor = {
|
||||
.offset = {0, 0},
|
||||
.extent = swapchain.m_Extent,
|
||||
};
|
||||
|
||||
u32 frameIndex = 0;
|
||||
while (window.Poll())
|
||||
{
|
||||
Frame *currentFrame = &frames[frameIndex];
|
||||
|
||||
result = device.m_Device.waitForFences(1, ¤tFrame->m_FrameAvailableFence, vk::True, MaxValue<u64>);
|
||||
ERROR_IF(Failed(result), "Waiting for fence {} failed. Cause: {}", frameIndex, result)
|
||||
THEN_ABORT(result);
|
||||
|
||||
u32 imageIndex;
|
||||
result = device.m_Device.acquireNextImageKHR(swapchain.m_Swapchain, MaxValue<u64>,
|
||||
currentFrame->m_ImageAcquireSem, nullptr, &imageIndex);
|
||||
if (Failed(result))
|
||||
{
|
||||
switch (result)
|
||||
{
|
||||
case vk::Result::eErrorOutOfDateKHR:
|
||||
case vk::Result::eSuboptimalKHR:
|
||||
INFO("Recreating Swapchain. Cause: {}", result);
|
||||
swapchain.Create(&window);
|
||||
viewport.y = Cast<f32>(swapchain.m_Extent.height);
|
||||
viewport.width = Cast<f32>(swapchain.m_Extent.width);
|
||||
viewport.height = -Cast<f32>(swapchain.m_Extent.height);
|
||||
scissor.extent = swapchain.m_Extent;
|
||||
break;
|
||||
default:
|
||||
ERROR("Waiting for swapchain image {} failed. Cause: {}", frameIndex, result)
|
||||
THEN_ABORT(result);
|
||||
}
|
||||
}
|
||||
|
||||
result = device.m_Device.resetFences(1, ¤tFrame->m_FrameAvailableFence);
|
||||
ERROR_IF(Failed(result), "Fence {} reset failed. Cause: {}", frameIndex, result)
|
||||
THEN_ABORT(result);
|
||||
|
||||
result = device.m_Device.resetCommandPool(currentFrame->m_Pool, {});
|
||||
ERROR_IF(Failed(result), "Command pool {} reset failed. Cause: {}", frameIndex, result)
|
||||
THEN_ABORT(result);
|
||||
|
||||
vk::ImageView currentImageView = swapchain.m_ImageViews[imageIndex];
|
||||
vk::Image currentImage = swapchain.m_Images[imageIndex];
|
||||
vk::CommandBuffer cmd = currentFrame->AllocateCommandBuffer();
|
||||
|
||||
vk::ImageMemoryBarrier topOfThePipeBarrier = {
|
||||
.oldLayout = vk::ImageLayout::eUndefined,
|
||||
.newLayout = vk::ImageLayout::eColorAttachmentOptimal,
|
||||
.srcQueueFamilyIndex = queueAllocation.m_Family,
|
||||
.dstQueueFamilyIndex = queueAllocation.m_Family,
|
||||
.image = currentImage,
|
||||
.subresourceRange =
|
||||
{
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
};
|
||||
vk::ImageMemoryBarrier renderToPresentBarrier = {
|
||||
.oldLayout = vk::ImageLayout::eColorAttachmentOptimal,
|
||||
.newLayout = vk::ImageLayout::ePresentSrcKHR,
|
||||
.srcQueueFamilyIndex = queueAllocation.m_Family,
|
||||
.dstQueueFamilyIndex = queueAllocation.m_Family,
|
||||
.image = currentImage,
|
||||
.subresourceRange =
|
||||
{
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
};
|
||||
|
||||
vk::CommandBufferBeginInfo beginInfo = {.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit};
|
||||
result = cmd.begin(&beginInfo);
|
||||
ERROR_IF(Failed(result), "Command buffer begin failed. Cause: {}", result)
|
||||
THEN_ABORT(result);
|
||||
|
||||
cmd.pipelineBarrier(vk::PipelineStageFlagBits::eTopOfPipe, vk::PipelineStageFlagBits::eColorAttachmentOutput,
|
||||
{}, 0, nullptr, 0, nullptr, 1, &topOfThePipeBarrier);
|
||||
|
||||
// Render
|
||||
vk::RenderingAttachmentInfo attachmentInfo = {
|
||||
.imageView = currentImageView,
|
||||
.imageLayout = vk::ImageLayout::eColorAttachmentOptimal,
|
||||
.resolveMode = vk::ResolveModeFlagBits::eNone,
|
||||
.loadOp = vk::AttachmentLoadOp::eClear,
|
||||
.storeOp = vk::AttachmentStoreOp::eStore,
|
||||
.clearValue = vk::ClearColorValue{0.0f, 0.0f, 0.0f, 1.0f},
|
||||
};
|
||||
|
||||
vk::RenderingInfo renderingInfo = {
|
||||
.renderArea = {.extent = swapchain.m_Extent},
|
||||
.layerCount = 1,
|
||||
.colorAttachmentCount = 1,
|
||||
.pColorAttachments = &attachmentInfo,
|
||||
};
|
||||
|
||||
cmd.beginRendering(&renderingInfo);
|
||||
|
||||
cmd.setViewport(0, 1, &viewport);
|
||||
cmd.setScissor(0, 1, &scissor);
|
||||
cmd.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline);
|
||||
cmd.draw(3, 1, 0, 0);
|
||||
|
||||
cmd.endRendering();
|
||||
|
||||
cmd.pipelineBarrier(vk::PipelineStageFlagBits::eColorAttachmentOutput, vk::PipelineStageFlagBits::eBottomOfPipe,
|
||||
{}, 0, nullptr, 0, nullptr, 1, &renderToPresentBarrier);
|
||||
|
||||
result = cmd.end();
|
||||
ERROR_IF(Failed(result), "Command buffer end failed. Cause: {}", result)
|
||||
THEN_ABORT(result);
|
||||
|
||||
vk::PipelineStageFlags waitDstStage = vk::PipelineStageFlagBits::eColorAttachmentOutput;
|
||||
vk::SubmitInfo submitInfo = {
|
||||
.waitSemaphoreCount = 1,
|
||||
.pWaitSemaphores = ¤tFrame->m_ImageAcquireSem,
|
||||
.pWaitDstStageMask = &waitDstStage,
|
||||
.commandBufferCount = 1,
|
||||
.pCommandBuffers = &cmd,
|
||||
.signalSemaphoreCount = 1,
|
||||
.pSignalSemaphores = ¤tFrame->m_RenderFinishSem,
|
||||
};
|
||||
result = commandQueue.submit(1, &submitInfo, currentFrame->m_FrameAvailableFence);
|
||||
ERROR_IF(Failed(result), "Command queue submit failed. Cause: {}", result)
|
||||
THEN_ABORT(result);
|
||||
|
||||
vk::PresentInfoKHR presentInfo = {
|
||||
.waitSemaphoreCount = 1,
|
||||
.pWaitSemaphores = ¤tFrame->m_RenderFinishSem,
|
||||
.swapchainCount = 1,
|
||||
.pSwapchains = &swapchain.m_Swapchain,
|
||||
.pImageIndices = &imageIndex,
|
||||
.pResults = nullptr,
|
||||
};
|
||||
result = commandQueue.presentKHR(&presentInfo);
|
||||
if (Failed(result))
|
||||
{
|
||||
switch (result)
|
||||
{
|
||||
case vk::Result::eErrorOutOfDateKHR:
|
||||
case vk::Result::eSuboptimalKHR:
|
||||
INFO("Recreating Swapchain. Cause: {}", result);
|
||||
swapchain.Create(&window);
|
||||
viewport.y = Cast<f32>(swapchain.m_Extent.height);
|
||||
viewport.width = Cast<f32>(swapchain.m_Extent.width);
|
||||
viewport.height = -Cast<f32>(swapchain.m_Extent.height);
|
||||
scissor.extent = swapchain.m_Extent;
|
||||
break;
|
||||
default:
|
||||
ERROR("Command queue present failed. Cause: {}", result)
|
||||
THEN_ABORT(result);
|
||||
}
|
||||
}
|
||||
frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT;
|
||||
}
|
||||
|
||||
result = device.m_Device.waitIdle();
|
||||
ERROR_IF(Failed(result), "Wait idle failed. Cause: {}", result);
|
||||
|
||||
device.m_Device.destroy(pipeline, nullptr);
|
||||
device.m_Device.destroy(pipelineLayout, nullptr);
|
||||
device.m_Device.destroy(vertexShaderModule, nullptr);
|
||||
device.m_Device.destroy(fragmentShaderModule, nullptr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
constexpr QueueSupportFlags REQUIRED_QUEUE_SUPPORT = QueueSupportFlags{} | QueueSupportFlagBits::eGraphics |
|
||||
QueueSupportFlagBits::eCompute | QueueSupportFlagBits::ePresent |
|
||||
QueueSupportFlagBits::eTransfer;
|
||||
|
||||
PhysicalDevice
|
||||
FindSuitableDevice(const PhysicalDevices &physicalDevices)
|
||||
{
|
||||
for (auto &physicalDevice : physicalDevices)
|
||||
{
|
||||
if (IsSuitableDevice(&physicalDevice))
|
||||
{
|
||||
return physicalDevice;
|
||||
}
|
||||
}
|
||||
|
||||
ERROR("No suitable GPU available on the system.")
|
||||
THEN_ABORT(vk::Result::eErrorUnknown);
|
||||
}
|
||||
|
||||
QueueAllocation
|
||||
FindAppropriateQueueAllocation(const PhysicalDevice *physicalDevice)
|
||||
{
|
||||
for (auto &queueFamilyInfo : physicalDevice->m_QueueFamilies)
|
||||
{
|
||||
if ((queueFamilyInfo.m_Support & REQUIRED_QUEUE_SUPPORT) == REQUIRED_QUEUE_SUPPORT)
|
||||
{
|
||||
return {
|
||||
.m_Family = queueFamilyInfo.m_Index,
|
||||
.m_Count = queueFamilyInfo.m_Count,
|
||||
};
|
||||
}
|
||||
}
|
||||
ERROR("No suitable queue family on the GPU.")
|
||||
THEN_ABORT(vk::Result::eErrorUnknown);
|
||||
}
|
||||
eastl::optional<eastl::vector<u32>>
|
||||
ReadFile(cstr fileName)
|
||||
{
|
||||
FILE *filePtr = fopen(fileName, "rb");
|
||||
|
||||
if (!filePtr)
|
||||
{
|
||||
ERROR("Invalid read of {}", fileName);
|
||||
return eastl::nullopt;
|
||||
}
|
||||
|
||||
eastl::vector<u32> outputVec;
|
||||
eastl::array<u32, 1024> buffer{};
|
||||
usize totalRead = 0;
|
||||
usize readCount;
|
||||
do
|
||||
{
|
||||
readCount = fread(buffer.data(), sizeof(u32), buffer.size(), filePtr);
|
||||
const auto nextSize = totalRead + readCount;
|
||||
outputVec.resize(nextSize);
|
||||
memcpy(outputVec.data() + totalRead, buffer.data(), readCount * sizeof *buffer.data());
|
||||
totalRead = nextSize;
|
||||
} while (readCount == 1024);
|
||||
|
||||
return outputVec;
|
||||
}
|
||||
|
||||
Frame::Frame(const Device *device, const u32 queueFamilyIndex, const u32 frameCount)
|
||||
{
|
||||
m_Device = device;
|
||||
|
||||
const vk::CommandPoolCreateInfo commandPoolCreateInfo = {
|
||||
.flags = vk::CommandPoolCreateFlagBits::eTransient,
|
||||
.queueFamilyIndex = queueFamilyIndex,
|
||||
};
|
||||
vk::Result result = device->m_Device.createCommandPool(&commandPoolCreateInfo, nullptr, &m_Pool);
|
||||
ERROR_IF(Failed(result), "Could not command pool for frame {}. Cause: {}", frameCount, result)
|
||||
THEN_ABORT(result);
|
||||
|
||||
constexpr vk::FenceCreateInfo fenceCreateInfo = {.flags = vk::FenceCreateFlagBits::eSignaled};
|
||||
result = device->m_Device.createFence(&fenceCreateInfo, nullptr, &m_FrameAvailableFence);
|
||||
ERROR_IF(Failed(result), "Could not create a fence for frame {}. Cause: {}", frameCount, result)
|
||||
THEN_ABORT(result);
|
||||
|
||||
constexpr vk::SemaphoreCreateInfo semaphoreCreateInfo = {};
|
||||
result = device->m_Device.createSemaphore(&semaphoreCreateInfo, nullptr, &m_ImageAcquireSem);
|
||||
ERROR_IF(Failed(result), "Could not create IA semaphore for frame {}. Cause: {}", frameCount, result)
|
||||
THEN_ABORT(result);
|
||||
result = device->m_Device.createSemaphore(&semaphoreCreateInfo, nullptr, &m_RenderFinishSem);
|
||||
ERROR_IF(Failed(result), "Could not create RF semaphore for frame {}. Cause: {}", frameCount, result)
|
||||
THEN_ABORT(result);
|
||||
|
||||
DEBUG("Frame {} created successfully.", frameCount);
|
||||
}
|
||||
|
||||
vk::CommandBuffer
|
||||
Frame::AllocateCommandBuffer() const
|
||||
{
|
||||
const vk::CommandBufferAllocateInfo allocateInfo = {
|
||||
.commandPool = m_Pool, .level = vk::CommandBufferLevel::ePrimary, .commandBufferCount = 1};
|
||||
|
||||
vk::CommandBuffer commandBuffer;
|
||||
vk::Result result = m_Device->m_Device.allocateCommandBuffers(&allocateInfo, &commandBuffer);
|
||||
ERROR_IF(Failed(result), "Command buffer allocation failed. Cause: {}", result)
|
||||
THEN_ABORT(result);
|
||||
|
||||
return commandBuffer;
|
||||
}
|
||||
|
||||
vk::ShaderModule
|
||||
CreateShader(const Device *device, cstr shaderFile)
|
||||
{
|
||||
eastl::vector<u32> shaderCode;
|
||||
if (auto res = ReadFile(shaderFile))
|
||||
{
|
||||
shaderCode = res.value();
|
||||
}
|
||||
else
|
||||
{
|
||||
ERROR("Could not open {}.", shaderFile) THEN_ABORT(-1);
|
||||
}
|
||||
|
||||
const vk::ShaderModuleCreateInfo shaderModuleCreateInfo = {
|
||||
.codeSize = shaderCode.size() * sizeof(u32),
|
||||
.pCode = shaderCode.data(),
|
||||
};
|
||||
vk::ShaderModule shaderModule;
|
||||
|
||||
vk::Result result = device->m_Device.createShaderModule(&shaderModuleCreateInfo, nullptr, &shaderModule);
|
||||
ERROR_IF(Failed(result), "Shader {} could not be created. Cause: {}", shaderFile, result)
|
||||
THEN_ABORT(result);
|
||||
return shaderModule;
|
||||
}
|
||||
|
||||
Frame::~Frame()
|
||||
{
|
||||
m_Device->m_Device.destroy(m_RenderFinishSem, nullptr);
|
||||
m_Device->m_Device.destroy(m_ImageAcquireSem, nullptr);
|
||||
m_Device->m_Device.destroy(m_FrameAvailableFence, nullptr);
|
||||
m_Device->m_Device.destroy(m_Pool, nullptr);
|
||||
|
||||
DEBUG("Destoryed Frame");
|
||||
}
|
||||
|
||||
bool
|
||||
IsSuitableDevice(const PhysicalDevice *physicalDevice)
|
||||
{
|
||||
const bool hasAllRequiredQueues =
|
||||
std::ranges::any_of(physicalDevice->m_QueueFamilies, [](const auto &queueFamilyProp) {
|
||||
return (queueFamilyProp.m_Support & REQUIRED_QUEUE_SUPPORT) == REQUIRED_QUEUE_SUPPORT;
|
||||
});
|
||||
|
||||
const bool isNotCpu = physicalDevice->m_DeviceProperties.deviceType != vk::PhysicalDeviceType::eCpu;
|
||||
|
||||
const bool hasPresentMode = !physicalDevice->m_PresentModes.empty();
|
||||
|
||||
const bool hasSurfaceFormat = !physicalDevice->m_SurfaceFormats.empty();
|
||||
|
||||
return hasSurfaceFormat && hasPresentMode && isNotCpu && hasAllRequiredQueues;
|
||||
}
|
||||
Loading…
Reference in New Issue