mirror of
https://github.com/KytyPS5/KytyPS5.git
synced 2026-10-28 00:12:40 +00:00
graphics: refactoring #2
This commit is contained in:
1 parent
b51d4322cc
commit
0a5f78c727
137 files changed
+10230
-10893
No files matched your search
@@ -138,16 +138,15 @@ static PreparedFramePool* GetPreparedFramePool() {
|
||||
return pool;
|
||||
}
|
||||
|
||||
static void ConfigurePreparedFrame(PreparedFrame* frame, vk::Extent2D extent, vk::Format format) {
|
||||
EXIT_IF(frame == nullptr);
|
||||
static void ConfigurePreparedFrame(PreparedFrame& frame, vk::Extent2D extent, vk::Format format) {
|
||||
EXIT_IF(g_window_ctx == nullptr);
|
||||
if (extent.width == 0 || extent.height == 0 || format == vk::Format::eUndefined) {
|
||||
EXIT("unsupported prepared frame, extent=%ux%u format=%d\n", extent.width, extent.height,
|
||||
static_cast<int>(format));
|
||||
}
|
||||
|
||||
auto* ctx = &g_window_ctx->graphic_ctx;
|
||||
auto& dst = frame->image;
|
||||
auto& graphics = g_window_ctx->graphic_ctx;
|
||||
auto& dst = frame.image;
|
||||
const bool compatible = dst.image != nullptr && dst.extent.width == extent.width &&
|
||||
dst.extent.height == extent.height && dst.format == format;
|
||||
if (compatible) {
|
||||
@@ -155,7 +154,7 @@ static void ConfigurePreparedFrame(PreparedFrame* frame, vk::Extent2D extent, vk
|
||||
}
|
||||
if (dst.image != nullptr) {
|
||||
EXIT_IF(!dst.view_cache.views.empty());
|
||||
VulkanDeleteImage(ctx, &dst);
|
||||
graphics.DeleteImage(dst);
|
||||
dst.memory = {};
|
||||
}
|
||||
|
||||
@@ -178,7 +177,7 @@ static void ConfigurePreparedFrame(PreparedFrame* frame, vk::Extent2D extent, vk
|
||||
create.usage = vk::ImageUsageFlagBits::eTransferSrc | vk::ImageUsageFlagBits::eTransferDst;
|
||||
create.sharingMode = vk::SharingMode::eExclusive;
|
||||
create.samples = vk::SampleCountFlagBits::e1;
|
||||
if (!VulkanCreateImage(ctx, create, &dst)) {
|
||||
if (!graphics.CreateImage(create, dst)) {
|
||||
EXIT("failed to allocate prepared presentation image, extent=%ux%u format=%d\n",
|
||||
dst.extent.width, dst.extent.height, static_cast<int>(dst.format));
|
||||
}
|
||||
@@ -188,28 +187,22 @@ VulkanSwapchain::~VulkanSwapchain() = default;
|
||||
|
||||
[[maybe_unused]] static vk::SwapchainKHR VulkanCreateSwapchainInternal(
|
||||
vk::Device device, vk::SurfaceKHR surface, uint32_t width, uint32_t height,
|
||||
uint32_t image_count, SurfaceCapabilities* r, vk::Format* swapchain_format,
|
||||
vk::Extent2D* swapchain_extent, std::unique_ptr<vk::Image[]>* swapchain_images,
|
||||
std::unique_ptr<vk::ImageView[]>* swapchain_image_views, uint32_t* swapchain_images_count) {
|
||||
uint32_t image_count, SurfaceCapabilities& r, vk::Format& swapchain_format,
|
||||
vk::Extent2D& swapchain_extent, std::unique_ptr<vk::Image[]>& swapchain_images,
|
||||
std::unique_ptr<vk::ImageView[]>& swapchain_image_views, uint32_t& swapchain_images_count) {
|
||||
EXIT_IF(device == nullptr);
|
||||
EXIT_IF(surface == nullptr);
|
||||
EXIT_IF(r == nullptr);
|
||||
EXIT_IF(swapchain_format == nullptr);
|
||||
EXIT_IF(swapchain_extent == nullptr);
|
||||
EXIT_IF(swapchain_images == nullptr);
|
||||
EXIT_IF(swapchain_image_views == nullptr);
|
||||
EXIT_IF(swapchain_images_count == nullptr);
|
||||
|
||||
EXIT_NOT_IMPLEMENTED(r->formats.empty());
|
||||
EXIT_NOT_IMPLEMENTED(r.formats.empty());
|
||||
|
||||
vk::Extent2D extent {};
|
||||
extent.width = std::clamp(width, r->capabilities.minImageExtent.width,
|
||||
r->capabilities.maxImageExtent.width);
|
||||
extent.height = std::clamp(height, r->capabilities.minImageExtent.height,
|
||||
r->capabilities.maxImageExtent.height);
|
||||
extent.width =
|
||||
std::clamp(width, r.capabilities.minImageExtent.width, r.capabilities.maxImageExtent.width);
|
||||
extent.height = std::clamp(height, r.capabilities.minImageExtent.height,
|
||||
r.capabilities.maxImageExtent.height);
|
||||
|
||||
image_count =
|
||||
std::clamp(image_count, r->capabilities.minImageCount, r->capabilities.maxImageCount);
|
||||
std::clamp(image_count, r.capabilities.minImageCount, r.capabilities.maxImageCount);
|
||||
|
||||
vk::SwapchainCreateInfoKHR create_info {};
|
||||
create_info.sType = vk::StructureType::eSwapchainCreateInfoKHR;
|
||||
@@ -218,14 +211,14 @@ VulkanSwapchain::~VulkanSwapchain() = default;
|
||||
create_info.surface = surface;
|
||||
create_info.minImageCount = image_count;
|
||||
|
||||
if (r->format_unorm_bgra32) {
|
||||
if (r.format_unorm_bgra32) {
|
||||
create_info.imageFormat = vk::Format::eB8G8R8A8Unorm;
|
||||
create_info.imageColorSpace = vk::ColorSpaceKHR::eSrgbNonlinear;
|
||||
} else if (r->format_srgb_bgra32) {
|
||||
} else if (r.format_srgb_bgra32) {
|
||||
create_info.imageFormat = vk::Format::eB8G8R8A8Srgb;
|
||||
create_info.imageColorSpace = vk::ColorSpaceKHR::eSrgbNonlinear;
|
||||
} else {
|
||||
const auto& format = r->formats[0];
|
||||
const auto& format = r.formats[0];
|
||||
create_info.imageFormat = format.format;
|
||||
create_info.imageColorSpace = format.colorSpace;
|
||||
}
|
||||
@@ -237,14 +230,14 @@ VulkanSwapchain::~VulkanSwapchain() = default;
|
||||
create_info.imageSharingMode = vk::SharingMode::eExclusive;
|
||||
create_info.queueFamilyIndexCount = 0;
|
||||
create_info.pQueueFamilyIndices = nullptr;
|
||||
create_info.preTransform = r->capabilities.currentTransform;
|
||||
create_info.preTransform = r.capabilities.currentTransform;
|
||||
create_info.compositeAlpha = vk::CompositeAlphaFlagBitsKHR::eOpaque;
|
||||
create_info.presentMode = vk::PresentModeKHR::eFifo;
|
||||
create_info.clipped = VK_TRUE;
|
||||
create_info.oldSwapchain = nullptr;
|
||||
|
||||
*swapchain_format = create_info.imageFormat;
|
||||
*swapchain_extent = extent;
|
||||
swapchain_format = create_info.imageFormat;
|
||||
swapchain_extent = extent;
|
||||
|
||||
vk::SwapchainKHR swapchain = nullptr;
|
||||
|
||||
@@ -258,19 +251,19 @@ VulkanSwapchain::~VulkanSwapchain() = default;
|
||||
});
|
||||
EXIT_NOT_IMPLEMENTED(images.empty());
|
||||
|
||||
*swapchain_images_count = static_cast<uint32_t>(images.size());
|
||||
*swapchain_images = std::make_unique<vk::Image[]>(images.size());
|
||||
std::copy(images.begin(), images.end(), swapchain_images->get());
|
||||
swapchain_images_count = static_cast<uint32_t>(images.size());
|
||||
swapchain_images = std::make_unique<vk::Image[]>(images.size());
|
||||
std::copy(images.begin(), images.end(), swapchain_images.get());
|
||||
|
||||
*swapchain_image_views = std::make_unique<vk::ImageView[]>(*swapchain_images_count);
|
||||
for (uint32_t i = 0; i < *swapchain_images_count; i++) {
|
||||
swapchain_image_views = std::make_unique<vk::ImageView[]>(swapchain_images_count);
|
||||
for (uint32_t i = 0; i < swapchain_images_count; i++) {
|
||||
vk::ImageViewCreateInfo create_info {};
|
||||
create_info.sType = vk::StructureType::eImageViewCreateInfo;
|
||||
create_info.pNext = nullptr;
|
||||
create_info.flags = {};
|
||||
create_info.image = (*swapchain_images)[i];
|
||||
create_info.image = (swapchain_images)[i];
|
||||
create_info.viewType = vk::ImageViewType::e2D;
|
||||
create_info.format = *swapchain_format;
|
||||
create_info.format = swapchain_format;
|
||||
create_info.components.r = vk::ComponentSwizzle::eIdentity;
|
||||
create_info.components.g = vk::ComponentSwizzle::eIdentity;
|
||||
create_info.components.b = vk::ComponentSwizzle::eIdentity;
|
||||
@@ -282,19 +275,19 @@ VulkanSwapchain::~VulkanSwapchain() = default;
|
||||
create_info.subresourceRange.levelCount = 1;
|
||||
|
||||
RequireVulkanSuccess(
|
||||
device.createImageView(&create_info, nullptr, &((*swapchain_image_views)[i])),
|
||||
device.createImageView(&create_info, nullptr, &((swapchain_image_views)[i])),
|
||||
"vkCreateImageView");
|
||||
EXIT_IF((*swapchain_image_views)[i] == nullptr);
|
||||
EXIT_IF((swapchain_image_views)[i] == nullptr);
|
||||
}
|
||||
|
||||
return swapchain;
|
||||
}
|
||||
|
||||
VulkanSwapchain* VulkanCreateSwapchain(GraphicContext* ctx, uint32_t image_count) {
|
||||
VulkanSwapchain* VulkanCreateSwapchain(uint32_t image_count) {
|
||||
EXIT_IF(g_window_ctx == nullptr);
|
||||
EXIT_IF(ctx == nullptr);
|
||||
EXIT_IF(ctx->screen_width == 0);
|
||||
EXIT_IF(ctx->screen_height == 0);
|
||||
auto& graphics = g_window_ctx->graphic_ctx;
|
||||
EXIT_IF(graphics.screen_width == 0);
|
||||
EXIT_IF(graphics.screen_height == 0);
|
||||
|
||||
Common::LockGuard lock(g_window_ctx->mutex);
|
||||
|
||||
@@ -302,9 +295,9 @@ VulkanSwapchain* VulkanCreateSwapchain(GraphicContext* ctx, uint32_t image_count
|
||||
auto* s = swapchain_owner.get();
|
||||
|
||||
s->swapchain = VulkanCreateSwapchainInternal(
|
||||
ctx->device, g_window_ctx->surface, ctx->screen_width, ctx->screen_height, image_count,
|
||||
g_window_ctx->surface_capabilities, &s->swapchain_format, &s->swapchain_extent,
|
||||
&s->swapchain_images, &s->swapchain_image_views, &s->swapchain_images_count);
|
||||
graphics.device, g_window_ctx->surface, graphics.screen_width, graphics.screen_height,
|
||||
image_count, *g_window_ctx->surface_capabilities, s->swapchain_format, s->swapchain_extent,
|
||||
s->swapchain_images, s->swapchain_image_views, s->swapchain_images_count);
|
||||
if (s->swapchain == nullptr) {
|
||||
EXIT("Could not create swapchain");
|
||||
}
|
||||
@@ -320,12 +313,12 @@ VulkanSwapchain* VulkanCreateSwapchain(GraphicContext* ctx, uint32_t image_count
|
||||
s->image_acquired_semaphores = std::make_unique<vk::Semaphore[]>(s->swapchain_images_count);
|
||||
s->render_complete_semaphores = std::make_unique<vk::Semaphore[]>(s->swapchain_images_count);
|
||||
for (uint32_t i = 0; i < s->swapchain_images_count; i++) {
|
||||
auto result =
|
||||
ctx->device.createSemaphore(&semaphore_info, nullptr, &s->image_acquired_semaphores[i]);
|
||||
auto result = graphics.device.createSemaphore(&semaphore_info, nullptr,
|
||||
&s->image_acquired_semaphores[i]);
|
||||
EXIT_NOT_IMPLEMENTED(result != vk::Result::eSuccess);
|
||||
|
||||
result = ctx->device.createSemaphore(&semaphore_info, nullptr,
|
||||
&s->render_complete_semaphores[i]);
|
||||
result = graphics.device.createSemaphore(&semaphore_info, nullptr,
|
||||
&s->render_complete_semaphores[i]);
|
||||
EXIT_NOT_IMPLEMENTED(result != vk::Result::eSuccess);
|
||||
}
|
||||
GetPreparedFramePool()->EnsureCapacity(s->swapchain_images_count, s->swapchain_format);
|
||||
@@ -333,38 +326,39 @@ VulkanSwapchain* VulkanCreateSwapchain(GraphicContext* ctx, uint32_t image_count
|
||||
return swapchain_owner.release();
|
||||
}
|
||||
|
||||
static void VulkanDeleteSwapchain(GraphicContext* ctx, VulkanSwapchain* s) {
|
||||
if (ctx == nullptr || s == nullptr) {
|
||||
static void VulkanDeleteSwapchain(VulkanSwapchain* s) {
|
||||
if (s == nullptr) {
|
||||
return;
|
||||
}
|
||||
auto swapchain_owner = std::unique_ptr<VulkanSwapchain>(s);
|
||||
auto swapchain_owner = std::unique_ptr<VulkanSwapchain>(s);
|
||||
auto& graphics = g_window_ctx->graphic_ctx;
|
||||
|
||||
Transfer::WaitForGraphicsIdle(ctx);
|
||||
Transfer::WaitForGraphicsIdle();
|
||||
|
||||
if (s->image_acquired_semaphores != nullptr) {
|
||||
for (uint32_t i = 0; i < s->swapchain_images_count; i++) {
|
||||
if (s->image_acquired_semaphores[i] != nullptr) {
|
||||
ctx->device.destroySemaphore(s->image_acquired_semaphores[i], nullptr);
|
||||
graphics.device.destroySemaphore(s->image_acquired_semaphores[i], nullptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (s->render_complete_semaphores != nullptr) {
|
||||
for (uint32_t i = 0; i < s->swapchain_images_count; i++) {
|
||||
if (s->render_complete_semaphores[i] != nullptr) {
|
||||
ctx->device.destroySemaphore(s->render_complete_semaphores[i], nullptr);
|
||||
graphics.device.destroySemaphore(s->render_complete_semaphores[i], nullptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (s->swapchain_image_views != nullptr) {
|
||||
for (uint32_t i = 0; i < s->swapchain_images_count; i++) {
|
||||
if (s->swapchain_image_views[i] != nullptr) {
|
||||
ctx->device.destroyImageView(s->swapchain_image_views[i], nullptr);
|
||||
graphics.device.destroyImageView(s->swapchain_image_views[i], nullptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (s->swapchain != nullptr) {
|
||||
ctx->device.destroySwapchainKHR(s->swapchain, nullptr);
|
||||
graphics.device.destroySwapchainKHR(s->swapchain, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -382,7 +376,7 @@ static void VulkanRefreshSurfaceSize() {
|
||||
}
|
||||
|
||||
VulkanGetSurfaceCapabilities(g_window_ctx->graphic_ctx.physical_device, g_window_ctx->surface,
|
||||
g_window_ctx->surface_capabilities);
|
||||
*g_window_ctx->surface_capabilities);
|
||||
}
|
||||
|
||||
static void VulkanRecreateSwapchain() {
|
||||
@@ -390,61 +384,60 @@ static void VulkanRecreateSwapchain() {
|
||||
|
||||
LOGF("Recreating Vulkan swapchain\n");
|
||||
VulkanRefreshSurfaceSize();
|
||||
VulkanDeleteSwapchain(&g_window_ctx->graphic_ctx, g_window_ctx->swapchain);
|
||||
g_window_ctx->swapchain = VulkanCreateSwapchain(&g_window_ctx->graphic_ctx, 2);
|
||||
VulkanDeleteSwapchain(g_window_ctx->swapchain);
|
||||
g_window_ctx->swapchain = VulkanCreateSwapchain(2);
|
||||
}
|
||||
|
||||
static void ValidatePreparedCommand(CommandBuffer* buffer) {
|
||||
if (buffer == nullptr || buffer->IsInvalid() ||
|
||||
buffer->GetQueue() != GraphicContext::QUEUE_GFX) {
|
||||
static void ValidatePreparedCommand(CommandBuffer& buffer) {
|
||||
if (buffer.IsInvalid() || buffer.GetQueue() != GraphicContext::QUEUE_GFX) {
|
||||
EXIT("prepared frames must be recorded on the graphics queue\n");
|
||||
}
|
||||
EXIT_IF(g_render_ctx == nullptr);
|
||||
}
|
||||
|
||||
PreparedFrame* WindowPrepareFrame(CommandBuffer* buffer, VideoOutVulkanImage* image) {
|
||||
PreparedFrame& WindowPrepareFrame(CommandBuffer& buffer, VideoOutVulkanImage& image) {
|
||||
KYTY_PROFILER_FUNCTION();
|
||||
ValidatePreparedCommand(buffer);
|
||||
if (image->format == vk::Format::eUndefined) {
|
||||
EXIT("unsupported presentation source, image=%p\n", static_cast<const void*>(image));
|
||||
if (image.format == vk::Format::eUndefined) {
|
||||
EXIT("unsupported presentation source, image=%p\n", static_cast<const void*>(&image));
|
||||
}
|
||||
|
||||
auto* frame = GetPreparedFramePool()->Acquire();
|
||||
Common::LockGuard render_lock(g_render_ctx->GetMutex());
|
||||
g_render_ctx->GetTextureCache()->RefreshVideoOut(image);
|
||||
ConfigurePreparedFrame(frame, image->extent, image->format);
|
||||
const std::array copies {ImageImageCopy {
|
||||
.src_image = image, .width = image->extent.width, .height = image->extent.height}};
|
||||
Transfer::CopyImage(buffer, copies, &frame->image, vk::ImageLayout::eTransferSrcOptimal);
|
||||
return frame;
|
||||
Common::LockGuard render_lock(GetRenderContext().GetMutex());
|
||||
GetRenderContext().GetTextureCache().RefreshVideoOut(image);
|
||||
ConfigurePreparedFrame(*frame, image.extent, image.format);
|
||||
ImageImageCopy copy {image};
|
||||
copy.width = image.extent.width;
|
||||
copy.height = image.extent.height;
|
||||
const std::array copies {copy};
|
||||
Transfer::CopyImage(buffer, copies, frame->image, vk::ImageLayout::eTransferSrcOptimal);
|
||||
return *frame;
|
||||
}
|
||||
|
||||
PreparedFrame* WindowPrepareBlankFrame(CommandBuffer* buffer, uint32_t width, uint32_t height,
|
||||
PreparedFrame& WindowPrepareBlankFrame(CommandBuffer& buffer, uint32_t width, uint32_t height,
|
||||
bool opaque) {
|
||||
KYTY_PROFILER_FUNCTION();
|
||||
ValidatePreparedCommand(buffer);
|
||||
auto* pool = GetPreparedFramePool();
|
||||
auto format = pool->GetFormat();
|
||||
auto* frame = pool->Acquire();
|
||||
Common::LockGuard render_lock(g_render_ctx->GetMutex());
|
||||
ConfigurePreparedFrame(frame, {width, height}, format);
|
||||
Common::LockGuard render_lock(GetRenderContext().GetMutex());
|
||||
ConfigurePreparedFrame(*frame, {width, height}, format);
|
||||
vk::ClearColorValue clear {};
|
||||
clear.float32[3] = opaque ? 1.0f : 0.0f;
|
||||
Transfer::ClearColorImage(buffer, &frame->image, clear);
|
||||
return frame;
|
||||
Transfer::ClearColorImage(buffer, frame->image, clear);
|
||||
return *frame;
|
||||
}
|
||||
|
||||
void WindowPresentFrame(PreparedFrame* frame) {
|
||||
void WindowPresentFrame(PreparedFrame& frame) {
|
||||
KYTY_PROFILER_FUNCTION();
|
||||
|
||||
EXIT_IF(frame == nullptr);
|
||||
EXIT_IF(g_window_ctx == nullptr);
|
||||
EXIT_IF(g_window_ctx->swapchain == nullptr);
|
||||
struct ReleaseScope {
|
||||
PreparedFrame* frame;
|
||||
~ReleaseScope() { GetPreparedFramePool()->Release(frame); }
|
||||
};
|
||||
ReleaseScope release {frame};
|
||||
ReleaseScope release {&frame};
|
||||
|
||||
if (g_window_ctx->window_hidden) {
|
||||
WindowUpdateIcon();
|
||||
@@ -478,17 +471,17 @@ void WindowPresentFrame(PreparedFrame* frame) {
|
||||
default: EXIT("vkAcquireNextImageKHR failed: %s\n", VulkanToString(result).c_str());
|
||||
}
|
||||
EXIT_NOT_IMPLEMENTED(swapchain->current_index == static_cast<uint32_t>(-1));
|
||||
if (frame->present_commands == nullptr) {
|
||||
frame->present_commands = std::make_unique<CommandBuffer>(GraphicContext::QUEUE_GFX);
|
||||
if (frame.present_commands == nullptr) {
|
||||
frame.present_commands = std::make_unique<CommandBuffer>(GraphicContext::QUEUE_GFX);
|
||||
}
|
||||
frame->present_commands->WaitForFenceAndReset();
|
||||
auto& buffer = *frame->present_commands;
|
||||
frame.present_commands->WaitForFenceAndReset();
|
||||
auto& buffer = *frame.present_commands;
|
||||
|
||||
auto vk_buffer = buffer.Handle();
|
||||
|
||||
buffer.Begin();
|
||||
|
||||
Transfer::BlitToSwapchain(&buffer, &frame->image, swapchain);
|
||||
Transfer::BlitToSwapchain(buffer, frame.image, *swapchain);
|
||||
|
||||
vk::ImageMemoryBarrier pre_present_barrier {};
|
||||
pre_present_barrier.sType = vk::StructureType::eImageMemoryBarrier;
|
||||
|
||||
@@ -80,35 +80,35 @@ static bool HasLayer(const std::vector<vk::LayerProperties>& layers, const char*
|
||||
}
|
||||
|
||||
void VulkanGetSurfaceCapabilities(vk::PhysicalDevice physical_device, vk::SurfaceKHR surface,
|
||||
SurfaceCapabilities* r) {
|
||||
RequireVulkanSuccess(physical_device.getSurfaceCapabilitiesKHR(surface, &r->capabilities),
|
||||
SurfaceCapabilities& r) {
|
||||
RequireVulkanSuccess(physical_device.getSurfaceCapabilitiesKHR(surface, &r.capabilities),
|
||||
"vkGetPhysicalDeviceSurfaceCapabilitiesKHR");
|
||||
|
||||
r->formats = EnumerateVulkan<vk::SurfaceFormatKHR>( // @suppress("Ambiguous problem")
|
||||
r.formats = EnumerateVulkan<vk::SurfaceFormatKHR>( // @suppress("Ambiguous problem")
|
||||
"vkGetPhysicalDeviceSurfaceFormatsKHR", [&](uint32_t* count, vk::SurfaceFormatKHR* values) {
|
||||
return physical_device.getSurfaceFormatsKHR(surface, count, values);
|
||||
});
|
||||
EXIT_NOT_IMPLEMENTED(r->formats.empty());
|
||||
EXIT_NOT_IMPLEMENTED(r.formats.empty());
|
||||
|
||||
r->present_modes = EnumerateVulkan<vk::PresentModeKHR>( // @suppress("Ambiguous problem")
|
||||
r.present_modes = EnumerateVulkan<vk::PresentModeKHR>( // @suppress("Ambiguous problem")
|
||||
"vkGetPhysicalDeviceSurfacePresentModesKHR",
|
||||
[&](uint32_t* count, vk::PresentModeKHR* values) {
|
||||
return physical_device.getSurfacePresentModesKHR(surface, count, values);
|
||||
});
|
||||
EXIT_NOT_IMPLEMENTED(r->present_modes.empty());
|
||||
EXIT_NOT_IMPLEMENTED(r.present_modes.empty());
|
||||
|
||||
r->format_srgb_bgra32 = false;
|
||||
r->format_unorm_bgra32 = false;
|
||||
for (const auto& f: r->formats) {
|
||||
r.format_srgb_bgra32 = false;
|
||||
r.format_unorm_bgra32 = false;
|
||||
for (const auto& f: r.formats) {
|
||||
if (f.format == vk::Format::eB8G8R8A8Srgb &&
|
||||
f.colorSpace == vk::ColorSpaceKHR::eSrgbNonlinear) {
|
||||
r->format_srgb_bgra32 = true;
|
||||
r.format_srgb_bgra32 = true;
|
||||
}
|
||||
if (f.format == vk::Format::eB8G8R8A8Unorm &&
|
||||
f.colorSpace == vk::ColorSpaceKHR::eSrgbNonlinear) {
|
||||
r->format_unorm_bgra32 = true;
|
||||
r.format_unorm_bgra32 = true;
|
||||
}
|
||||
if (r->format_srgb_bgra32 && r->format_unorm_bgra32) {
|
||||
if (r.format_srgb_bgra32 && r.format_unorm_bgra32) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -244,13 +244,10 @@ static VulkanQueues VulkanFindQueues(vk::PhysicalDevice device, vk::SurfaceKHR s
|
||||
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
|
||||
static void VulkanFindPhysicalDevice(vk::Instance instance, vk::SurfaceKHR surface,
|
||||
const std::vector<const char*>& device_extensions,
|
||||
SurfaceCapabilities* out_capabilities,
|
||||
vk::PhysicalDevice* out_device, VulkanQueues* out_queues) {
|
||||
SurfaceCapabilities& out_capabilities,
|
||||
vk::PhysicalDevice& out_device, VulkanQueues& out_queues) {
|
||||
EXIT_IF(instance == nullptr);
|
||||
EXIT_IF(surface == nullptr);
|
||||
EXIT_IF(out_capabilities == nullptr);
|
||||
EXIT_IF(out_device == nullptr);
|
||||
EXIT_IF(out_queues == nullptr);
|
||||
|
||||
auto devices = EnumerateVulkan<vk::PhysicalDevice>(
|
||||
"vkEnumeratePhysicalDevices", [&](uint32_t* count, vk::PhysicalDevice* values) {
|
||||
@@ -408,7 +405,7 @@ static void VulkanFindPhysicalDevice(vk::Instance instance, vk::SurfaceKHR surfa
|
||||
|
||||
SurfaceCapabilities candidate_capabilities;
|
||||
if (!skip_device) {
|
||||
VulkanGetSurfaceCapabilities(device, surface, &candidate_capabilities);
|
||||
VulkanGetSurfaceCapabilities(device, surface, candidate_capabilities);
|
||||
|
||||
if (!(candidate_capabilities.capabilities.supportedUsageFlags &
|
||||
vk::ImageUsageFlagBits::eTransferDst)) {
|
||||
@@ -512,16 +509,16 @@ static void VulkanFindPhysicalDevice(vk::Instance instance, vk::SurfaceKHR surfa
|
||||
}
|
||||
}
|
||||
|
||||
*out_device = best_device;
|
||||
*out_queues = best_queues;
|
||||
out_device = best_device;
|
||||
out_queues = best_queues;
|
||||
if (best_device != nullptr) {
|
||||
*out_capabilities = std::move(best_capabilities);
|
||||
out_capabilities = std::move(best_capabilities);
|
||||
}
|
||||
}
|
||||
|
||||
static void VulkanInitSubgroupSizeControl(vk::PhysicalDevice physical_device, GraphicContext* ctx) {
|
||||
static void VulkanInitSubgroupSizeControl(vk::PhysicalDevice physical_device) {
|
||||
EXIT_IF(physical_device == nullptr);
|
||||
EXIT_IF(ctx == nullptr);
|
||||
auto& graphics = g_window_ctx->graphic_ctx;
|
||||
|
||||
vk::PhysicalDeviceSubgroupSizeControlProperties subgroup_size_control {};
|
||||
subgroup_size_control.sType = vk::StructureType::ePhysicalDeviceSubgroupSizeControlProperties;
|
||||
@@ -547,28 +544,26 @@ static void VulkanInitSubgroupSizeControl(vk::PhysicalDevice physical_device, Gr
|
||||
|
||||
physical_device.getFeatures2(&features2);
|
||||
|
||||
ctx->subgroup_size = properties11.subgroupSize;
|
||||
ctx->min_subgroup_size = subgroup_size_control.minSubgroupSize;
|
||||
ctx->max_subgroup_size = subgroup_size_control.maxSubgroupSize;
|
||||
ctx->required_subgroup_size_stages = subgroup_size_control.requiredSubgroupSizeStages;
|
||||
ctx->subgroup_size_control_enabled = features13.subgroupSizeControl == VK_TRUE &&
|
||||
subgroup_size_control.minSubgroupSize <= 64 &&
|
||||
subgroup_size_control.maxSubgroupSize >= 64;
|
||||
graphics.subgroup_size = properties11.subgroupSize;
|
||||
graphics.min_subgroup_size = subgroup_size_control.minSubgroupSize;
|
||||
graphics.max_subgroup_size = subgroup_size_control.maxSubgroupSize;
|
||||
graphics.required_subgroup_size_stages = subgroup_size_control.requiredSubgroupSizeStages;
|
||||
graphics.subgroup_size_control_enabled = features13.subgroupSizeControl == VK_TRUE &&
|
||||
subgroup_size_control.minSubgroupSize <= 64 &&
|
||||
subgroup_size_control.maxSubgroupSize >= 64;
|
||||
|
||||
LOGF("Vulkan subgroup: default=%u min=%u max=%u stages=0x%08x size_control=%s\n",
|
||||
ctx->subgroup_size, ctx->min_subgroup_size, ctx->max_subgroup_size,
|
||||
static_cast<vk::ShaderStageFlags::MaskType>(ctx->required_subgroup_size_stages),
|
||||
ctx->subgroup_size_control_enabled ? "true" : "false");
|
||||
graphics.subgroup_size, graphics.min_subgroup_size, graphics.max_subgroup_size,
|
||||
static_cast<vk::ShaderStageFlags::MaskType>(graphics.required_subgroup_size_stages),
|
||||
graphics.subgroup_size_control_enabled ? "true" : "false");
|
||||
}
|
||||
|
||||
static vk::Device VulkanCreateDevice(vk::PhysicalDevice physical_device, vk::SurfaceKHR surface,
|
||||
const VulkanExtensions* r, const VulkanQueues& queues,
|
||||
const std::vector<const char*>& device_extensions,
|
||||
GraphicContext* ctx) {
|
||||
const VulkanExtensions& r, const VulkanQueues& queues,
|
||||
const std::vector<const char*>& device_extensions) {
|
||||
EXIT_IF(physical_device == nullptr);
|
||||
EXIT_IF(r == nullptr);
|
||||
auto& graphics = g_window_ctx->graphic_ctx;
|
||||
EXIT_IF(surface == nullptr);
|
||||
EXIT_IF(ctx == nullptr);
|
||||
|
||||
std::vector<vk::DeviceQueueCreateInfo> queue_create_info(queues.family_count);
|
||||
std::vector<std::vector<float>> queue_priority(queues.family_count);
|
||||
@@ -599,8 +594,8 @@ static vk::Device VulkanCreateDevice(vk::PhysicalDevice physical_device, vk::Sur
|
||||
color_write_ext.colorWriteEnable = VK_TRUE;
|
||||
|
||||
vk::PhysicalDeviceDepthClipEnableFeaturesEXT depth_clip_enable {};
|
||||
depth_clip_enable.sType = vk::StructureType::ePhysicalDeviceDepthClipEnableFeaturesEXT;
|
||||
depth_clip_enable.pNext = &color_write_ext;
|
||||
depth_clip_enable.sType = vk::StructureType::ePhysicalDeviceDepthClipEnableFeaturesEXT;
|
||||
depth_clip_enable.pNext = &color_write_ext;
|
||||
depth_clip_enable.depthClipEnable = VK_TRUE;
|
||||
|
||||
vk::PhysicalDeviceDepthClipControlFeaturesEXT depth_clip_control {};
|
||||
@@ -646,12 +641,13 @@ static vk::Device VulkanCreateDevice(vk::PhysicalDevice physical_device, vk::Sur
|
||||
device_features.shaderImageGatherExtended = VK_TRUE;
|
||||
device_features.independentBlend = VK_TRUE;
|
||||
device_features.tessellationShader = VK_TRUE;
|
||||
device_features.sampleRateShading = supported_features2.features.sampleRateShading;
|
||||
ctx->sample_rate_shading_enabled = device_features.sampleRateShading == VK_TRUE;
|
||||
device_features.sampleRateShading = supported_features2.features.sampleRateShading;
|
||||
graphics.sample_rate_shading_enabled = device_features.sampleRateShading == VK_TRUE;
|
||||
device_features.vertexPipelineStoresAndAtomics =
|
||||
supported_features2.features.vertexPipelineStoresAndAtomics;
|
||||
|
||||
if (ctx->subgroup_size_control_enabled && supported_features13.subgroupSizeControl == VK_TRUE) {
|
||||
if (graphics.subgroup_size_control_enabled &&
|
||||
supported_features13.subgroupSizeControl == VK_TRUE) {
|
||||
subgroup_size_control.subgroupSizeControl = VK_TRUE;
|
||||
}
|
||||
|
||||
@@ -685,9 +681,9 @@ static vk::Device VulkanCreateDevice(vk::PhysicalDevice physical_device, vk::Sur
|
||||
create_info.pQueueCreateInfos = queue_create_info.data();
|
||||
create_info.queueCreateInfoCount = queue_create_info_num;
|
||||
create_info.enabledLayerCount =
|
||||
(r->enable_validation_layers ? static_cast<uint32_t>(r->required_layers.size()) : 0);
|
||||
(r.enable_validation_layers ? static_cast<uint32_t>(r.required_layers.size()) : 0);
|
||||
create_info.ppEnabledLayerNames =
|
||||
(r->enable_validation_layers ? r->required_layers.data() : nullptr);
|
||||
(r.enable_validation_layers ? r.required_layers.data() : nullptr);
|
||||
create_info.enabledExtensionCount = static_cast<uint32_t>(device_extensions.size());
|
||||
create_info.ppEnabledExtensionNames = device_extensions.data();
|
||||
create_info.pEnabledFeatures = &device_features;
|
||||
@@ -703,9 +699,8 @@ static vk::Device VulkanCreateDevice(vk::PhysicalDevice physical_device, vk::Sur
|
||||
return device;
|
||||
}
|
||||
|
||||
static void VulkanGetExtensions(SDL_Window* window, VulkanExtensions* r) {
|
||||
static void VulkanGetExtensions(SDL_Window* window, VulkanExtensions& r) {
|
||||
EXIT_IF(window == nullptr);
|
||||
EXIT_IF(r == nullptr);
|
||||
|
||||
uint32_t required_extensions_count = 0;
|
||||
|
||||
@@ -714,65 +709,65 @@ static void VulkanGetExtensions(SDL_Window* window, VulkanExtensions* r) {
|
||||
EXIT_NOT_IMPLEMENTED(sdl_result == SDL_FALSE);
|
||||
EXIT_NOT_IMPLEMENTED(required_extensions_count == 0);
|
||||
|
||||
r->required_extensions =
|
||||
r.required_extensions =
|
||||
std::vector<const char*>(required_extensions_count); // @suppress("Ambiguous problem")
|
||||
std::memset(r->required_extensions.data(), 0,
|
||||
sizeof(const char*) * r->required_extensions.size());
|
||||
std::memset(r.required_extensions.data(), 0,
|
||||
sizeof(const char*) * r.required_extensions.size());
|
||||
|
||||
sdl_result = SDL_Vulkan_GetInstanceExtensions(window, &required_extensions_count,
|
||||
r->required_extensions.data());
|
||||
r.required_extensions.data());
|
||||
|
||||
EXIT_NOT_IMPLEMENTED(sdl_result == SDL_FALSE);
|
||||
EXIT_NOT_IMPLEMENTED(required_extensions_count == 0);
|
||||
EXIT_NOT_IMPLEMENTED(required_extensions_count != r->required_extensions.size());
|
||||
EXIT_NOT_IMPLEMENTED(required_extensions_count != r.required_extensions.size());
|
||||
|
||||
r->available_extensions =
|
||||
r.available_extensions =
|
||||
EnumerateVulkan<vk::ExtensionProperties>( // @suppress("Ambiguous problem")
|
||||
"vkEnumerateInstanceExtensionProperties",
|
||||
[](uint32_t* count, vk::ExtensionProperties* values) {
|
||||
return vk::enumerateInstanceExtensionProperties(nullptr, count, values);
|
||||
});
|
||||
|
||||
r->enable_validation_layers = Config::VulkanValidationEnabled();
|
||||
r.enable_validation_layers = Config::VulkanValidationEnabled();
|
||||
|
||||
if (HasExtension(r->available_extensions, VK_EXT_DEBUG_UTILS_EXTENSION_NAME)) {
|
||||
r->required_extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
|
||||
if (HasExtension(r.available_extensions, VK_EXT_DEBUG_UTILS_EXTENSION_NAME)) {
|
||||
r.required_extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
|
||||
} else {
|
||||
r->enable_validation_layers = false;
|
||||
r.enable_validation_layers = false;
|
||||
}
|
||||
|
||||
for (const char* ext: r->required_extensions) {
|
||||
for (const char* ext: r.required_extensions) {
|
||||
LOGF("Vulkan required extension: %s\n", ext);
|
||||
}
|
||||
|
||||
for (const auto& ext: r->available_extensions) {
|
||||
for (const auto& ext: r.available_extensions) {
|
||||
LOGF("Vulkan available extension: %s, version = %u\n", ext.extensionName.data(),
|
||||
ext.specVersion);
|
||||
}
|
||||
|
||||
r->available_layers = EnumerateVulkan<vk::LayerProperties>( // @suppress("Ambiguous problem")
|
||||
r.available_layers = EnumerateVulkan<vk::LayerProperties>( // @suppress("Ambiguous problem")
|
||||
"vkEnumerateInstanceLayerProperties", [](uint32_t* count, vk::LayerProperties* values) {
|
||||
return vk::enumerateInstanceLayerProperties(count, values);
|
||||
});
|
||||
|
||||
for (const auto& l: r->available_layers) {
|
||||
for (const auto& l: r.available_layers) {
|
||||
LOGF("Vulkan available layer: %s, specVersion = %u, implVersion = %u, %s\n",
|
||||
l.layerName.data(), l.specVersion, l.implementationVersion, l.description.data());
|
||||
}
|
||||
|
||||
r->required_layers = {"VK_LAYER_KHRONOS_validation"};
|
||||
r.required_layers = {"VK_LAYER_KHRONOS_validation"};
|
||||
|
||||
if (r->enable_validation_layers) {
|
||||
for (const char* l: r->required_layers) {
|
||||
if (!HasLayer(r->available_layers, l)) {
|
||||
if (r.enable_validation_layers) {
|
||||
for (const char* l: r.required_layers) {
|
||||
if (!HasLayer(r.available_layers, l)) {
|
||||
LOGF("no validation layer: %s\n", l);
|
||||
r->enable_validation_layers = false;
|
||||
r.enable_validation_layers = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (r->enable_validation_layers) {
|
||||
if (r.enable_validation_layers) {
|
||||
auto available_extensions = EnumerateVulkan<vk::ExtensionProperties>(
|
||||
"vkEnumerateInstanceExtensionProperties",
|
||||
[](uint32_t* count, vk::ExtensionProperties* values) {
|
||||
@@ -786,9 +781,9 @@ static void VulkanGetExtensions(SDL_Window* window, VulkanExtensions* r) {
|
||||
}
|
||||
|
||||
if (HasExtension(available_extensions, VK_EXT_VALIDATION_FEATURES_EXTENSION_NAME)) {
|
||||
r->required_extensions.push_back(VK_EXT_VALIDATION_FEATURES_EXTENSION_NAME);
|
||||
r.required_extensions.push_back(VK_EXT_VALIDATION_FEATURES_EXTENSION_NAME);
|
||||
} else {
|
||||
r->enable_validation_layers = false;
|
||||
r.enable_validation_layers = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -870,28 +865,30 @@ static VKAPI_ATTR vk::Result VKAPI_CALL VulkanCreateDebugUtilsMessengerEXT(
|
||||
return vk::Result::eErrorExtensionNotPresent;
|
||||
}
|
||||
|
||||
static void VulkanCreateQueues(GraphicContext* ctx, const VulkanQueues& queues) {
|
||||
EXIT_IF(ctx == nullptr);
|
||||
EXIT_IF(ctx->device == nullptr);
|
||||
static void VulkanCreateQueues(const VulkanQueues& queues) {
|
||||
auto& graphics = g_window_ctx->graphic_ctx;
|
||||
EXIT_IF(graphics.device == nullptr);
|
||||
EXIT_IF(queues.graphics.size() != 1);
|
||||
EXIT_IF(queues.present.size() != 1);
|
||||
EXIT_IF(!(queues.compute.size() >= 1 &&
|
||||
queues.compute.size() <= GraphicContext::QUEUE_COMPUTE_NUM));
|
||||
|
||||
auto get_queue = [ctx](int id, const QueueInfo& info) {
|
||||
ctx->queues[id].family = info.family;
|
||||
ctx->queues[id].index = info.index;
|
||||
EXIT_IF(ctx->queues[id].vk_queue != nullptr);
|
||||
ctx->device.getQueue(ctx->queues[id].family, ctx->queues[id].index,
|
||||
&ctx->queues[id].vk_queue);
|
||||
EXIT_NOT_IMPLEMENTED(ctx->queues[id].vk_queue == nullptr);
|
||||
auto get_queue = [&graphics](int id, const QueueInfo& info) {
|
||||
graphics.queues[id].family = info.family;
|
||||
graphics.queues[id].index = info.index;
|
||||
EXIT_IF(graphics.queues[id].vk_queue != nullptr);
|
||||
graphics.device.getQueue(graphics.queues[id].family, graphics.queues[id].index,
|
||||
&graphics.queues[id].vk_queue);
|
||||
EXIT_NOT_IMPLEMENTED(graphics.queues[id].vk_queue == nullptr);
|
||||
};
|
||||
|
||||
get_queue(GraphicContext::QUEUE_GFX, queues.graphics[0]);
|
||||
ctx->queues[GraphicContext::QUEUE_UTIL].family = ctx->queues[GraphicContext::QUEUE_GFX].family;
|
||||
ctx->queues[GraphicContext::QUEUE_UTIL].index = ctx->queues[GraphicContext::QUEUE_GFX].index;
|
||||
ctx->queues[GraphicContext::QUEUE_UTIL].vk_queue =
|
||||
ctx->queues[GraphicContext::QUEUE_GFX].vk_queue;
|
||||
graphics.queues[GraphicContext::QUEUE_UTIL].family =
|
||||
graphics.queues[GraphicContext::QUEUE_GFX].family;
|
||||
graphics.queues[GraphicContext::QUEUE_UTIL].index =
|
||||
graphics.queues[GraphicContext::QUEUE_GFX].index;
|
||||
graphics.queues[GraphicContext::QUEUE_UTIL].vk_queue =
|
||||
graphics.queues[GraphicContext::QUEUE_GFX].vk_queue;
|
||||
LOGF("Vulkan queue: using graphics queue for utility submissions to preserve resource "
|
||||
"ordering\n");
|
||||
get_queue(GraphicContext::QUEUE_PRESENT, queues.present[0]);
|
||||
@@ -902,14 +899,14 @@ static void VulkanCreateQueues(GraphicContext* ctx, const VulkanQueues& queues)
|
||||
}
|
||||
|
||||
for (int id = 0; id < GraphicContext::QUEUES_NUM; id++) {
|
||||
auto& queue = ctx->queues[id];
|
||||
auto& queue = graphics.queues[id];
|
||||
if (queue.vk_queue == nullptr) {
|
||||
continue;
|
||||
}
|
||||
EXIT_IF(queue.mutex != nullptr);
|
||||
|
||||
for (int other_id = 0; other_id < id; other_id++) {
|
||||
auto& other = ctx->queues[other_id];
|
||||
auto& other = graphics.queues[other_id];
|
||||
if (other.vk_queue != queue.vk_queue) {
|
||||
continue;
|
||||
}
|
||||
@@ -921,7 +918,7 @@ static void VulkanCreateQueues(GraphicContext* ctx, const VulkanQueues& queues)
|
||||
}
|
||||
|
||||
if (queue.mutex == nullptr) {
|
||||
queue.mutex = &ctx->queue_mutexes[id];
|
||||
queue.mutex = &graphics.queue_mutexes[id];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -946,12 +943,12 @@ static void VulkanCheckInstanceVersion() {
|
||||
}
|
||||
}
|
||||
|
||||
void VulkanCreate(WindowContext* ctx) {
|
||||
EXIT_IF(ctx->window == nullptr);
|
||||
EXIT_IF(ctx->graphic_ctx.instance != nullptr);
|
||||
EXIT_IF(ctx->graphic_ctx.physical_device != nullptr);
|
||||
EXIT_IF(ctx->graphic_ctx.device != nullptr);
|
||||
EXIT_IF(ctx->surface_capabilities != nullptr);
|
||||
void VulkanCreate(WindowContext& window) {
|
||||
EXIT_IF(window.window == nullptr);
|
||||
EXIT_IF(window.graphic_ctx.instance != nullptr);
|
||||
EXIT_IF(window.graphic_ctx.physical_device != nullptr);
|
||||
EXIT_IF(window.graphic_ctx.device != nullptr);
|
||||
EXIT_IF(window.surface_capabilities != nullptr);
|
||||
|
||||
auto get_instance_proc_addr =
|
||||
reinterpret_cast<PFN_vkGetInstanceProcAddr>(SDL_Vulkan_GetVkGetInstanceProcAddr());
|
||||
@@ -961,7 +958,7 @@ void VulkanCreate(WindowContext* ctx) {
|
||||
VULKAN_HPP_DEFAULT_DISPATCHER.init(get_instance_proc_addr);
|
||||
|
||||
VulkanExtensions r;
|
||||
VulkanGetExtensions(ctx->window, &r);
|
||||
VulkanGetExtensions(window.window, r);
|
||||
VulkanCheckInstanceVersion();
|
||||
|
||||
vk::ApplicationInfo app_info {};
|
||||
@@ -1025,31 +1022,31 @@ void VulkanCreate(WindowContext* ctx) {
|
||||
inst_info.ppEnabledLayerNames =
|
||||
(r.enable_validation_layers ? r.required_layers.data() : nullptr);
|
||||
|
||||
const vk::Result result = vk::createInstance(&inst_info, nullptr, &ctx->graphic_ctx.instance);
|
||||
const vk::Result result = vk::createInstance(&inst_info, nullptr, &window.graphic_ctx.instance);
|
||||
switch (result) {
|
||||
case vk::Result::eSuccess: break;
|
||||
case vk::Result::eErrorIncompatibleDriver:
|
||||
EXIT("Unable to find a compatible Vulkan Driver");
|
||||
default: EXIT("Could not create a Vulkan instance (for unknown reasons)");
|
||||
}
|
||||
VULKAN_HPP_DEFAULT_DISPATCHER.init(ctx->graphic_ctx.instance);
|
||||
VULKAN_HPP_DEFAULT_DISPATCHER.init(window.graphic_ctx.instance);
|
||||
|
||||
if (r.enable_validation_layers) {
|
||||
dbg_create_info.pNext = nullptr;
|
||||
if (VulkanCreateDebugUtilsMessengerEXT(ctx->graphic_ctx.instance, &dbg_create_info, nullptr,
|
||||
&ctx->graphic_ctx.debug_messenger) !=
|
||||
if (VulkanCreateDebugUtilsMessengerEXT(window.graphic_ctx.instance, &dbg_create_info,
|
||||
nullptr, &window.graphic_ctx.debug_messenger) !=
|
||||
vk::Result::eSuccess) {
|
||||
EXIT("Could not create debug messenger");
|
||||
}
|
||||
}
|
||||
|
||||
vk::SurfaceKHR::CType native_surface = VK_NULL_HANDLE;
|
||||
if (SDL_Vulkan_CreateSurface(ctx->window,
|
||||
static_cast<vk::Instance::CType>(ctx->graphic_ctx.instance),
|
||||
if (SDL_Vulkan_CreateSurface(window.window,
|
||||
static_cast<vk::Instance::CType>(window.graphic_ctx.instance),
|
||||
&native_surface) == SDL_FALSE) {
|
||||
EXIT("Could not create a Vulkan surface");
|
||||
}
|
||||
ctx->surface = native_surface;
|
||||
window.surface = native_surface;
|
||||
|
||||
std::vector<const char*> device_extensions = {
|
||||
VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_EXT_DEPTH_CLIP_ENABLE_EXTENSION_NAME,
|
||||
@@ -1062,21 +1059,23 @@ void VulkanCreate(WindowContext* ctx) {
|
||||
}
|
||||
#endif
|
||||
|
||||
ctx->surface_capabilities = new SurfaceCapabilities {};
|
||||
window.surface_capabilities = new SurfaceCapabilities {};
|
||||
|
||||
VulkanQueues queues;
|
||||
|
||||
VulkanFindPhysicalDevice(ctx->graphic_ctx.instance, ctx->surface, device_extensions,
|
||||
ctx->surface_capabilities, &ctx->graphic_ctx.physical_device, &queues);
|
||||
VulkanFindPhysicalDevice(window.graphic_ctx.instance, window.surface, device_extensions,
|
||||
*window.surface_capabilities, window.graphic_ctx.physical_device,
|
||||
queues);
|
||||
|
||||
if (ctx->graphic_ctx.physical_device == nullptr) {
|
||||
if (window.graphic_ctx.physical_device == nullptr) {
|
||||
EXIT("Could not find suitable device");
|
||||
}
|
||||
|
||||
ctx->graphic_ctx.physical_device.getProperties(&ctx->graphic_ctx.physical_device_properties);
|
||||
ctx->graphic_ctx.physical_device.getMemoryProperties(
|
||||
&ctx->graphic_ctx.physical_device_memory_properties);
|
||||
const auto& device_properties = ctx->graphic_ctx.GetPhysicalDeviceProperties();
|
||||
window.graphic_ctx.physical_device.getProperties(
|
||||
&window.graphic_ctx.physical_device_properties);
|
||||
window.graphic_ctx.physical_device.getMemoryProperties(
|
||||
&window.graphic_ctx.physical_device_memory_properties);
|
||||
const auto& device_properties = window.graphic_ctx.GetPhysicalDeviceProperties();
|
||||
|
||||
LOGF("Select device: %s\n", device_properties.deviceName.data());
|
||||
|
||||
@@ -1084,40 +1083,40 @@ void VulkanCreate(WindowContext* ctx) {
|
||||
auto available_extensions = EnumerateVulkan<vk::ExtensionProperties>(
|
||||
"vkEnumerateDeviceExtensionProperties",
|
||||
[&](uint32_t* count, vk::ExtensionProperties* values) {
|
||||
return ctx->graphic_ctx.physical_device.enumerateDeviceExtensionProperties(
|
||||
return window.graphic_ctx.physical_device.enumerateDeviceExtensionProperties(
|
||||
nullptr, count, values);
|
||||
});
|
||||
|
||||
if (HasExtension(available_extensions, VK_EXT_MEMORY_BUDGET_EXTENSION_NAME)) {
|
||||
device_extensions.push_back(VK_EXT_MEMORY_BUDGET_EXTENSION_NAME);
|
||||
ctx->graphic_ctx.memory_budget_ext_enabled = true;
|
||||
window.graphic_ctx.memory_budget_ext_enabled = true;
|
||||
}
|
||||
if (HasExtension(available_extensions, VK_EXT_ROBUSTNESS_2_EXTENSION_NAME)) {
|
||||
device_extensions.push_back(VK_EXT_ROBUSTNESS_2_EXTENSION_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(ctx->device_name, device_properties.deviceName, sizeof(ctx->device_name));
|
||||
std::snprintf(ctx->processor_name, sizeof(ctx->processor_name), "%s",
|
||||
memcpy(window.device_name, device_properties.deviceName, sizeof(window.device_name));
|
||||
std::snprintf(window.processor_name, sizeof(window.processor_name), "%s",
|
||||
Common::GetSystemInfo().ProcessorName.c_str());
|
||||
|
||||
VulkanInitSubgroupSizeControl(ctx->graphic_ctx.physical_device, &ctx->graphic_ctx);
|
||||
VulkanInitSubgroupSizeControl(window.graphic_ctx.physical_device);
|
||||
|
||||
ctx->graphic_ctx.device = VulkanCreateDevice(ctx->graphic_ctx.physical_device, ctx->surface, &r,
|
||||
queues, device_extensions, &ctx->graphic_ctx);
|
||||
if (ctx->graphic_ctx.device == nullptr) {
|
||||
window.graphic_ctx.device = VulkanCreateDevice(window.graphic_ctx.physical_device,
|
||||
window.surface, r, queues, device_extensions);
|
||||
if (window.graphic_ctx.device == nullptr) {
|
||||
EXIT("Could not create device");
|
||||
}
|
||||
VULKAN_HPP_DEFAULT_DISPATCHER.init(ctx->graphic_ctx.device);
|
||||
VULKAN_HPP_DEFAULT_DISPATCHER.init(window.graphic_ctx.device);
|
||||
|
||||
if (!VulkanCreateAllocator(&ctx->graphic_ctx)) {
|
||||
if (!window.graphic_ctx.CreateAllocator()) {
|
||||
EXIT("Could not create Vulkan memory allocator");
|
||||
}
|
||||
|
||||
VulkanCreateQueues(&ctx->graphic_ctx, queues);
|
||||
VulkanCreateQueues(queues);
|
||||
|
||||
ctx->swapchain = VulkanCreateSwapchain(&ctx->graphic_ctx, 2);
|
||||
RenderDocSetActiveWindow(ctx->graphic_ctx.instance, ctx->window);
|
||||
window.swapchain = VulkanCreateSwapchain(2);
|
||||
RenderDocSetActiveWindow(window.graphic_ctx.instance, window.window);
|
||||
}
|
||||
|
||||
} // namespace Libs::Graphics
|
||||
@@ -234,11 +234,11 @@ struct WindowGame {
|
||||
};
|
||||
|
||||
struct WindowGamePrivate {
|
||||
WindowGamePrivate() = default;
|
||||
explicit WindowGamePrivate(GraphicContext& graphics): graphics(graphics) {}
|
||||
|
||||
Common::Mutex mutex;
|
||||
int skip_frames = 0;
|
||||
GraphicContext* ctx = nullptr;
|
||||
GraphicContext& graphics;
|
||||
};
|
||||
|
||||
WindowContext* g_window_ctx = nullptr;
|
||||
@@ -248,58 +248,58 @@ constexpr uint32_t KYTY_SDL_WINDOW_FLAGS =
|
||||
(static_cast<uint32_t>(SDL_WINDOW_HIDDEN) | static_cast<uint32_t>(SDL_WINDOW_VULKAN));
|
||||
constexpr int KYTY_SDL_WINDOWPOS_CENTERED = SDL_WINDOWPOS_CENTERED; /*NOLINT(hicpp-signed-bitwise)*/
|
||||
|
||||
static void CalcFrameTime(WindowGame* game, double game_time_s) {
|
||||
game->m_previous_time_seconds = game->m_current_time_seconds;
|
||||
game->m_current_time_seconds = game_time_s;
|
||||
static void CalcFrameTime(WindowGame& game, double game_time_s) {
|
||||
game.m_previous_time_seconds = game.m_current_time_seconds;
|
||||
game.m_current_time_seconds = game_time_s;
|
||||
|
||||
game->m_frame_num++;
|
||||
game->m_fps_frames_num++;
|
||||
game.m_frame_num++;
|
||||
game.m_fps_frames_num++;
|
||||
|
||||
const auto fps_time = game->m_current_time_seconds - game->m_fps_start_time;
|
||||
const auto fps_time = game.m_current_time_seconds - game.m_fps_start_time;
|
||||
if (fps_time > FPS_UPDATE_TIME) {
|
||||
game->m_current_fps = static_cast<double>(game->m_fps_frames_num) / fps_time;
|
||||
game->m_fps_frames_num = 0;
|
||||
game->m_fps_start_time = game->m_current_time_seconds;
|
||||
game.m_current_fps = static_cast<double>(game.m_fps_frames_num) / fps_time;
|
||||
game.m_fps_frames_num = 0;
|
||||
game.m_fps_start_time = game.m_current_time_seconds;
|
||||
}
|
||||
}
|
||||
|
||||
static bool Init(WindowGame* /*game*/) {
|
||||
static bool Init(WindowGame& /*game*/) {
|
||||
return true;
|
||||
}
|
||||
static bool Update(WindowGame* /*game*/) {
|
||||
static bool Update(WindowGame& /*game*/) {
|
||||
return true;
|
||||
}
|
||||
static bool Render(WindowGame* /*game*/) {
|
||||
static bool Render(WindowGame& /*game*/) {
|
||||
return true;
|
||||
}
|
||||
static bool Close(WindowGame* /*game*/) {
|
||||
static bool Close(WindowGame& /*game*/) {
|
||||
return true;
|
||||
}
|
||||
static void SetPause(WindowGame* game, bool flag) {
|
||||
static void SetPause(WindowGame& game, bool flag) {
|
||||
LOGF("Pause: %s\n", flag ? "true" : "false");
|
||||
|
||||
game->m_game_is_paused = flag;
|
||||
game.m_game_is_paused = flag;
|
||||
}
|
||||
|
||||
static bool RenderAndUpdate(WindowGame* game) {
|
||||
static bool RenderAndUpdate(WindowGame& game) {
|
||||
static double lag = 0.0;
|
||||
|
||||
lag += game->m_current_time_seconds - game->m_previous_time_seconds;
|
||||
lag += game.m_current_time_seconds - game.m_previous_time_seconds;
|
||||
|
||||
int num = 0;
|
||||
|
||||
bool ok = true;
|
||||
|
||||
while (lag >= game->m_update_fixed_time) {
|
||||
if (num < game->m_max_updates_per_frame) {
|
||||
while (lag >= game.m_update_fixed_time) {
|
||||
if (num < game.m_max_updates_per_frame) {
|
||||
ok = ok && Update(game);
|
||||
|
||||
game->m_update_num++;
|
||||
game.m_update_num++;
|
||||
num++;
|
||||
game->m_update_time_seconds = game->m_update_num * game->m_update_fixed_time;
|
||||
game.m_update_time_seconds = game.m_update_num * game.m_update_fixed_time;
|
||||
}
|
||||
|
||||
lag -= game->m_update_fixed_time;
|
||||
lag -= game.m_update_fixed_time;
|
||||
}
|
||||
|
||||
ok = ok && Render(game);
|
||||
@@ -307,44 +307,36 @@ static bool RenderAndUpdate(WindowGame* game) {
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool GameInit(WindowGame* game, const Common::Timer& timer, void* data) {
|
||||
EXIT_IF(game == nullptr);
|
||||
EXIT_IF(data == nullptr);
|
||||
EXIT_IF(game->private_data || game->event);
|
||||
bool GameInit(WindowGame& game, const Common::Timer& timer) {
|
||||
EXIT_IF(game.private_data || game.event);
|
||||
auto& graphics = WindowGetGraphicContext();
|
||||
|
||||
auto* ctx = static_cast<GraphicContext*>(data);
|
||||
EXIT_IF(graphics.screen_width == 0 || graphics.screen_height == 0);
|
||||
|
||||
EXIT_IF(ctx->screen_width == 0 || ctx->screen_height == 0);
|
||||
auto* pdata = new WindowGamePrivate(graphics);
|
||||
|
||||
auto* pdata = new WindowGamePrivate;
|
||||
pdata->ctx = ctx;
|
||||
game.private_data = pdata;
|
||||
game.event = new SDL_Event;
|
||||
|
||||
game->private_data = pdata;
|
||||
game->event = new SDL_Event;
|
||||
|
||||
game->m_screen_width = ctx->screen_width;
|
||||
game->m_screen_height = ctx->screen_height;
|
||||
game.m_screen_width = graphics.screen_width;
|
||||
game.m_screen_height = graphics.screen_height;
|
||||
|
||||
CalcFrameTime(game, timer.GetTimeS());
|
||||
|
||||
return Init(game);
|
||||
}
|
||||
|
||||
bool GameClose(WindowGame* game) {
|
||||
EXIT_IF(!game);
|
||||
bool GameClose(WindowGame& game) {
|
||||
EXIT_IF(!game.private_data || !game.event);
|
||||
|
||||
EXIT_IF(!game->private_data || !game->event);
|
||||
|
||||
delete (static_cast<WindowGamePrivate*>(game->private_data));
|
||||
delete (static_cast<SDL_Event*>(game->event));
|
||||
delete (static_cast<WindowGamePrivate*>(game.private_data));
|
||||
delete (static_cast<SDL_Event*>(game.event));
|
||||
|
||||
return Close(game);
|
||||
}
|
||||
|
||||
void GameShowWindow(WindowGame* game, const Common::Timer& timer) {
|
||||
EXIT_IF(!game);
|
||||
|
||||
auto* p = static_cast<WindowGamePrivate*>(game->private_data);
|
||||
void GameShowWindow(WindowGame& game, const Common::Timer& timer) {
|
||||
auto* p = static_cast<WindowGamePrivate*>(game.private_data);
|
||||
|
||||
EXIT_IF(!p);
|
||||
|
||||
@@ -364,33 +356,33 @@ void GameShowWindow(WindowGame* game, const Common::Timer& timer) {
|
||||
p->mutex.Unlock();
|
||||
}
|
||||
|
||||
void GameEventQuit(WindowGame* game) {
|
||||
void GameEventQuit(WindowGame& game) {
|
||||
LOGF("Event: quit\n");
|
||||
|
||||
game->m_game_need_exit = true;
|
||||
game.m_game_need_exit = true;
|
||||
}
|
||||
|
||||
void GameEventTerminate(WindowGame* game) {
|
||||
void GameEventTerminate(WindowGame& game) {
|
||||
LOGF("Event: terminate\n");
|
||||
|
||||
game->m_game_need_exit = true;
|
||||
game.m_game_need_exit = true;
|
||||
}
|
||||
|
||||
void GameEventKeyboard(WindowGame* game, const EventKeyboard* key) {
|
||||
void GameEventKeyboard(WindowGame& game, const EventKeyboard& key) {
|
||||
#ifdef KYTY_DBG_INPUT
|
||||
LOGF("Key: time = %.04f, %s%s, %s%s, %s, scan = %d, key = %d, mod = %04" PRIx16 "\n",
|
||||
key->timestamp_seconds, (key->down ? "down" : ""), (key->up ? "up" : ""),
|
||||
(key->pressed ? "pressed" : ""), (key->released ? "released" : ""),
|
||||
(key->repeat ? "repeat" : ""), key->scan_code, key->key_code, key->mod);
|
||||
key.timestamp_seconds, (key.down ? "down" : ""), (key.up ? "up" : ""),
|
||||
(key.pressed ? "pressed" : ""), (key.released ? "released" : ""),
|
||||
(key.repeat ? "repeat" : ""), key.scan_code, key.key_code, key.mod);
|
||||
#endif
|
||||
|
||||
#if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS || KYTY_PLATFORM == KYTY_PLATFORM_LINUX
|
||||
if (key->down) {
|
||||
switch (key->key_code) {
|
||||
case SDLK_ESCAPE: game->m_game_need_exit = true; break;
|
||||
case SDLK_SPACE: SetPause(game, !game->m_game_is_paused); break;
|
||||
if (key.down) {
|
||||
switch (key.key_code) {
|
||||
case SDLK_ESCAPE: game.m_game_need_exit = true; break;
|
||||
case SDLK_SPACE: SetPause(game, !game.m_game_is_paused); break;
|
||||
case SDLK_F1:
|
||||
if (!key->repeat) {
|
||||
if (!key.repeat) {
|
||||
RenderDocRequestCapture();
|
||||
}
|
||||
break;
|
||||
@@ -398,159 +390,153 @@ void GameEventKeyboard(WindowGame* game, const EventKeyboard* key) {
|
||||
}
|
||||
}
|
||||
|
||||
const auto button = KeyboardKeyToPadButton(key->key_code);
|
||||
if (button != 0 && (key->down || key->up) && !key->repeat) {
|
||||
const auto button = KeyboardKeyToPadButton(key.key_code);
|
||||
if (button != 0 && (key.down || key.up) && !key.repeat) {
|
||||
static bool keyboard_connected = false;
|
||||
if (!keyboard_connected) {
|
||||
Controller::ControllerConnect(KEYBOARD_CONTROLLER_ID);
|
||||
keyboard_connected = true;
|
||||
}
|
||||
Controller::ControllerButton(KEYBOARD_CONTROLLER_ID, button, key->down);
|
||||
Controller::ControllerButton(KEYBOARD_CONTROLLER_ID, button, key.down);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void GameEventMouse([[maybe_unused]] WindowGame* game, [[maybe_unused]] const EventMouse* mb) {
|
||||
void GameEventMouse([[maybe_unused]] WindowGame& game, [[maybe_unused]] const EventMouse& mb) {
|
||||
#ifdef KYTY_DBG_INPUT
|
||||
if (mb->wheel) {
|
||||
LOGF("Mouse wheel: time = %.04f, %s[%d, %d]\n", mb->timestamp_seconds,
|
||||
(mb->touch ? "touch, " : ""), mb->x, mb->y);
|
||||
} else if (mb->motion) {
|
||||
LOGF("Mouse motion: time = %.04f, %s%s%s%s%s%s, [%d, %d], (%d, %d)\n",
|
||||
mb->timestamp_seconds, (mb->left ? "left" : ""), (mb->middle ? "middle" : ""),
|
||||
(mb->right ? "right" : ""), (mb->x1 ? "x1" : ""), (mb->x2 ? "x2" : ""),
|
||||
(mb->touch ? "_touch" : ""), mb->x, mb->y, mb->motion_x, mb->motion_y);
|
||||
if (mb.wheel) {
|
||||
LOGF("Mouse wheel: time = %.04f, %s[%d, %d]\n", mb.timestamp_seconds,
|
||||
(mb.touch ? "touch, " : ""), mb.x, mb.y);
|
||||
} else if (mb.motion) {
|
||||
LOGF("Mouse motion: time = %.04f, %s%s%s%s%s%s, [%d, %d], (%d, %d)\n", mb.timestamp_seconds,
|
||||
(mb.left ? "left" : ""), (mb.middle ? "middle" : ""), (mb.right ? "right" : ""),
|
||||
(mb.x1 ? "x1" : ""), (mb.x2 ? "x2" : ""), (mb.touch ? "_touch" : ""), mb.x, mb.y,
|
||||
mb.motion_x, mb.motion_y);
|
||||
} else {
|
||||
LOGF("Mouse click: time = %.04f, %d, %s%s%s%s%s%s, %s%s, %s%s, [%d, %d]\n",
|
||||
mb->timestamp_seconds, mb->num_of_clicks, (mb->left ? "left" : ""),
|
||||
(mb->middle ? "middle" : ""), (mb->right ? "right" : ""), (mb->x1 ? "x1" : ""),
|
||||
(mb->x2 ? "x2" : ""), (mb->touch ? "_touch" : ""), (mb->down ? "down" : ""),
|
||||
(mb->up ? "up" : ""), (mb->pressed ? "pressed" : ""), (mb->released ? "released" : ""),
|
||||
mb->x, mb->y);
|
||||
mb.timestamp_seconds, mb.num_of_clicks, (mb.left ? "left" : ""),
|
||||
(mb.middle ? "middle" : ""), (mb.right ? "right" : ""), (mb.x1 ? "x1" : ""),
|
||||
(mb.x2 ? "x2" : ""), (mb.touch ? "_touch" : ""), (mb.down ? "down" : ""),
|
||||
(mb.up ? "up" : ""), (mb.pressed ? "pressed" : ""), (mb.released ? "released" : ""),
|
||||
mb.x, mb.y);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void GameEventFinger([[maybe_unused]] WindowGame* game, [[maybe_unused]] const EventFinger* f) {
|
||||
void GameEventFinger([[maybe_unused]] WindowGame& game, [[maybe_unused]] const EventFinger& f) {
|
||||
#ifdef KYTY_DBG_INPUT
|
||||
if (f->motion) {
|
||||
if (f.motion) {
|
||||
LOGF("Finger motion: time = %.04f, %d, %d, (x,y) = [%f, %f], (dx,dy) = [%f, %f], pressure "
|
||||
"= %f\n",
|
||||
f->timestamp_seconds, f->touch_id, f->finger_id, f->x, f->y, f->dx, f->dy,
|
||||
f->pressure);
|
||||
f.timestamp_seconds, f.touch_id, f.finger_id, f.x, f.y, f.dx, f.dy, f.pressure);
|
||||
} else {
|
||||
LOGF("Finger press: time = %.04f, %d, %d, %s%s, (x,y) = [%f, %f], (dx,dy) = [%f, %f], "
|
||||
"pressure = %f\n",
|
||||
f->timestamp_seconds, f->touch_id, f->finger_id, (f->down ? "down" : ""),
|
||||
(f->up ? "up" : ""), f->x, f->y, f->dx, f->dy, f->pressure);
|
||||
f.timestamp_seconds, f.touch_id, f.finger_id, (f.down ? "down" : ""),
|
||||
(f.up ? "up" : ""), f.x, f.y, f.dx, f.dy, f.pressure);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void GameEventController([[maybe_unused]] WindowGame* game,
|
||||
[[maybe_unused]] const EventController* f) {
|
||||
EXIT_NOT_IMPLEMENTED(f->remapped);
|
||||
void GameEventController([[maybe_unused]] WindowGame& game,
|
||||
[[maybe_unused]] const EventController& f) {
|
||||
EXIT_NOT_IMPLEMENTED(f.remapped);
|
||||
|
||||
#ifdef KYTY_DBG_INPUT
|
||||
if (f->added || f->removed) {
|
||||
LOGF("Controller %s: %d, time = %.04f\n", (f->added ? "added" : "removed"), f->id,
|
||||
f->timestamp_seconds);
|
||||
} else if (f->axis) {
|
||||
LOGF("Controller axis: %d, axis = %d, value = %d, time = %.04f\n", f->id, f->axis_id,
|
||||
f->axis_value, f->timestamp_seconds);
|
||||
if (f.added || f.removed) {
|
||||
LOGF("Controller %s: %d, time = %.04f\n", (f.added ? "added" : "removed"), f.id,
|
||||
f.timestamp_seconds);
|
||||
} else if (f.axis) {
|
||||
LOGF("Controller axis: %d, axis = %d, value = %d, time = %.04f\n", f.id, f.axis_id,
|
||||
f.axis_value, f.timestamp_seconds);
|
||||
} else {
|
||||
LOGF("Controller button: "
|
||||
"%d, %s%s, %s%s, button = %d, time = %.04f\n",
|
||||
f->id, (f->down ? "down" : ""), (f->up ? "up" : ""), (f->pressed ? "pressed" : ""),
|
||||
(f->released ? "released" : ""), f->button, f->timestamp_seconds);
|
||||
f.id, (f.down ? "down" : ""), (f.up ? "up" : ""), (f.pressed ? "pressed" : ""),
|
||||
(f.released ? "released" : ""), f.button, f.timestamp_seconds);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (f->added) {
|
||||
auto* pad = SDL_GameControllerOpen(f->id);
|
||||
if (f.added) {
|
||||
auto* pad = SDL_GameControllerOpen(f.id);
|
||||
EXIT_NOT_IMPLEMENTED(pad == nullptr);
|
||||
int id = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(pad));
|
||||
Controller::ControllerConnect(id);
|
||||
}
|
||||
|
||||
if (f->removed) {
|
||||
Controller::ControllerDisconnect(f->id);
|
||||
SDL_GameControllerClose(SDL_GameControllerFromInstanceID(f->id));
|
||||
if (f.removed) {
|
||||
Controller::ControllerDisconnect(f.id);
|
||||
SDL_GameControllerClose(SDL_GameControllerFromInstanceID(f.id));
|
||||
}
|
||||
|
||||
if (f->down || f->up) {
|
||||
const auto button = ControllerButtonToPadButton(f->button);
|
||||
if (f.down || f.up) {
|
||||
const auto button = ControllerButtonToPadButton(f.button);
|
||||
if (button != 0) {
|
||||
Controller::ControllerButton(f->id, button, f->down);
|
||||
Controller::ControllerButton(f.id, button, f.down);
|
||||
}
|
||||
}
|
||||
|
||||
if (f->axis) {
|
||||
const auto axis = ControllerAxisFromSdl(f->axis_id);
|
||||
if (f.axis) {
|
||||
const auto axis = ControllerAxisFromSdl(f.axis_id);
|
||||
if (axis != Controller::Axis::AxisMax) {
|
||||
Controller::ControllerAxis(f->id, axis,
|
||||
ControllerAxisValueFromSdl(f->axis_id, f->axis_value));
|
||||
Controller::ControllerAxis(f.id, axis,
|
||||
ControllerAxisValueFromSdl(f.axis_id, f.axis_value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GameEventDisplay([[maybe_unused]] WindowGame* game, [[maybe_unused]] const EventDisplay* d) {
|
||||
auto* p = static_cast<WindowGamePrivate*>(game->private_data);
|
||||
auto* ctx = static_cast<GraphicContext*>(p->ctx);
|
||||
void GameEventDisplay([[maybe_unused]] WindowGame& game) {
|
||||
auto* p = static_cast<WindowGamePrivate*>(game.private_data);
|
||||
|
||||
p->mutex.Lock();
|
||||
game->m_screen_width = ctx->screen_width;
|
||||
game->m_screen_height = ctx->screen_height;
|
||||
game.m_screen_width = p->graphics.screen_width;
|
||||
game.m_screen_height = p->graphics.screen_height;
|
||||
p->mutex.Unlock();
|
||||
}
|
||||
|
||||
void GameEventLowMemory(WindowGame* /*game*/) {
|
||||
void GameEventLowMemory(WindowGame& /*game*/) {
|
||||
LOGF("Event: low_memory\n");
|
||||
}
|
||||
|
||||
void GameEventWillEnterBackground(WindowGame* game) {
|
||||
void GameEventWillEnterBackground(WindowGame& game) {
|
||||
LOGF("Event: will_enter_background\n");
|
||||
|
||||
SetPause(game, true);
|
||||
}
|
||||
|
||||
void GameEventDidEnterBackground(WindowGame* /*game*/) {
|
||||
void GameEventDidEnterBackground(WindowGame& /*game*/) {
|
||||
LOGF("Event: did_enter_background\n");
|
||||
}
|
||||
|
||||
void GameEventWillEnterForeground(WindowGame* /*game*/) {
|
||||
void GameEventWillEnterForeground(WindowGame& /*game*/) {
|
||||
LOGF("Event: will_enter_foreground\n");
|
||||
}
|
||||
|
||||
void GameEventDidEnterForeground(WindowGame* game) {
|
||||
void GameEventDidEnterForeground(WindowGame& game) {
|
||||
LOGF("Event: did_enter_foreground\n");
|
||||
|
||||
SetPause(game, false);
|
||||
}
|
||||
|
||||
void GameEventResize(WindowGame* game, uint32_t new_width, uint32_t new_height) {
|
||||
void GameEventResize(WindowGame& game, uint32_t new_width, uint32_t new_height) {
|
||||
EXIT_IF(new_width == 0 || new_height == 0);
|
||||
EXIT_IF(!game);
|
||||
|
||||
auto* p = static_cast<WindowGamePrivate*>(game->private_data);
|
||||
auto* p = static_cast<WindowGamePrivate*>(game.private_data);
|
||||
EXIT_IF(p == nullptr);
|
||||
|
||||
auto* ctx = static_cast<GraphicContext*>(p->ctx);
|
||||
EXIT_IF(ctx == nullptr);
|
||||
|
||||
p->mutex.Lock();
|
||||
{
|
||||
p->skip_frames++;
|
||||
ctx->screen_width = new_width;
|
||||
ctx->screen_height = new_height;
|
||||
p->graphics.screen_width = new_width;
|
||||
p->graphics.screen_height = new_height;
|
||||
|
||||
game->m_screen_width = ctx->screen_width;
|
||||
game->m_screen_height = ctx->screen_height;
|
||||
game.m_screen_width = p->graphics.screen_width;
|
||||
game.m_screen_height = p->graphics.screen_height;
|
||||
}
|
||||
p->mutex.Unlock();
|
||||
}
|
||||
|
||||
static void ProcessWindowEvent(WindowGame* game, SDL_WindowEvent window) {
|
||||
static void ProcessWindowEvent(WindowGame& game, SDL_WindowEvent window) {
|
||||
switch (window.event) {
|
||||
case SDL_WINDOWEVENT_SHOWN: LOGF("Window %" PRIu32 " shown\n", window.windowID); break;
|
||||
|
||||
@@ -608,7 +594,7 @@ static void ProcessWindowEvent(WindowGame* game, SDL_WindowEvent window) {
|
||||
}
|
||||
}
|
||||
|
||||
static void ProcessDisplayEvent(WindowGame* game, SDL_DisplayEvent display) {
|
||||
static void ProcessDisplayEvent(WindowGame& game, SDL_DisplayEvent display) {
|
||||
bool sdl = false;
|
||||
|
||||
switch (display.event) {
|
||||
@@ -617,32 +603,17 @@ static void ProcessDisplayEvent(WindowGame* game, SDL_DisplayEvent display) {
|
||||
LOGF("Display %" PRIu32 "[%s] changed orientation to %d - ", display.display,
|
||||
sdl ? "SDL" : "Kyty", static_cast<int>(display.data1));
|
||||
|
||||
EventDisplay d {};
|
||||
d.orientation = DisplayOrientation::Unknown;
|
||||
|
||||
switch (display.data1) {
|
||||
case SDL_ORIENTATION_UNKNOWN: LOGF("UNKNOWN\n"); break;
|
||||
case SDL_ORIENTATION_LANDSCAPE:
|
||||
LOGF("LANDSCAPE\n");
|
||||
d.orientation = DisplayOrientation::Landscape;
|
||||
break;
|
||||
case SDL_ORIENTATION_LANDSCAPE_FLIPPED:
|
||||
LOGF("LANDSCAPE_FLIPPED\n");
|
||||
d.orientation = DisplayOrientation::LandscapeFlipped;
|
||||
break;
|
||||
case SDL_ORIENTATION_PORTRAIT:
|
||||
LOGF("PORTRAIT\n");
|
||||
d.orientation = DisplayOrientation::Portrait;
|
||||
break;
|
||||
case SDL_ORIENTATION_PORTRAIT_FLIPPED:
|
||||
d.orientation = DisplayOrientation::PortraitFlipped;
|
||||
LOGF("PORTRAIT_FLIPPED\n");
|
||||
break;
|
||||
case SDL_ORIENTATION_LANDSCAPE: LOGF("LANDSCAPE\n"); break;
|
||||
case SDL_ORIENTATION_LANDSCAPE_FLIPPED: LOGF("LANDSCAPE_FLIPPED\n"); break;
|
||||
case SDL_ORIENTATION_PORTRAIT: LOGF("PORTRAIT\n"); break;
|
||||
case SDL_ORIENTATION_PORTRAIT_FLIPPED: LOGF("PORTRAIT_FLIPPED\n"); break;
|
||||
default: LOGF("???\n");
|
||||
}
|
||||
|
||||
if (!sdl) {
|
||||
GameEventDisplay(game, &d);
|
||||
GameEventDisplay(game);
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -654,30 +625,24 @@ static void ProcessDisplayEvent(WindowGame* game, SDL_DisplayEvent display) {
|
||||
}
|
||||
}
|
||||
|
||||
int GamePollEvent(WindowGame* game) {
|
||||
EXIT_IF(!game);
|
||||
|
||||
auto* event = static_cast<SDL_Event*>(game->event);
|
||||
int GamePollEvent(WindowGame& game) {
|
||||
auto* event = static_cast<SDL_Event*>(game.event);
|
||||
|
||||
EXIT_IF(!event);
|
||||
|
||||
return SDL_PollEvent(event);
|
||||
}
|
||||
|
||||
int GameWaitEvent(WindowGame* game) {
|
||||
EXIT_IF(!game);
|
||||
|
||||
auto* event = static_cast<SDL_Event*>(game->event);
|
||||
int GameWaitEvent(WindowGame& game) {
|
||||
auto* event = static_cast<SDL_Event*>(game.event);
|
||||
|
||||
EXIT_IF(!event);
|
||||
|
||||
return SDL_WaitEvent(event);
|
||||
}
|
||||
|
||||
void GameProcessEvent(WindowGame* game, double time_s) {
|
||||
EXIT_IF(!game);
|
||||
|
||||
auto* event = static_cast<SDL_Event*>(game->event);
|
||||
void GameProcessEvent(WindowGame& game, double time_s) {
|
||||
auto* event = static_cast<SDL_Event*>(game.event);
|
||||
|
||||
EXIT_IF(!event);
|
||||
|
||||
@@ -712,7 +677,7 @@ void GameProcessEvent(WindowGame* game, double time_s) {
|
||||
key.mod = event->key.keysym.mod;
|
||||
key.timestamp_seconds = time_s;
|
||||
|
||||
GameEventKeyboard(game, &key);
|
||||
GameEventKeyboard(game, key);
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -744,7 +709,7 @@ void GameProcessEvent(WindowGame* game, double time_s) {
|
||||
mb.motion_y = 0;
|
||||
mb.timestamp_seconds = time_s;
|
||||
|
||||
GameEventMouse(game, &mb);
|
||||
GameEventMouse(game, mb);
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -771,7 +736,7 @@ void GameProcessEvent(WindowGame* game, double time_s) {
|
||||
mb.motion_y = 0;
|
||||
mb.timestamp_seconds = time_s;
|
||||
|
||||
GameEventMouse(game, &mb);
|
||||
GameEventMouse(game, mb);
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -798,7 +763,7 @@ void GameProcessEvent(WindowGame* game, double time_s) {
|
||||
mb.motion_y = event->motion.yrel;
|
||||
mb.timestamp_seconds = time_s;
|
||||
|
||||
GameEventMouse(game, &mb);
|
||||
GameEventMouse(game, mb);
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -820,7 +785,7 @@ void GameProcessEvent(WindowGame* game, double time_s) {
|
||||
f.pressure = event->tfinger.pressure;
|
||||
f.timestamp_seconds = time_s;
|
||||
|
||||
GameEventFinger(game, &f);
|
||||
GameEventFinger(game, f);
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -842,7 +807,7 @@ void GameProcessEvent(WindowGame* game, double time_s) {
|
||||
c.released = false;
|
||||
c.timestamp_seconds = time_s;
|
||||
|
||||
GameEventController(game, &c);
|
||||
GameEventController(game, c);
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -865,7 +830,7 @@ void GameProcessEvent(WindowGame* game, double time_s) {
|
||||
c.released = (event->cbutton.state == SDL_RELEASED);
|
||||
c.timestamp_seconds = time_s;
|
||||
|
||||
GameEventController(game, &c);
|
||||
GameEventController(game, c);
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -889,20 +854,20 @@ void GameProcessEvent(WindowGame* game, double time_s) {
|
||||
c.released = false;
|
||||
c.timestamp_seconds = time_s;
|
||||
|
||||
GameEventController(game, &c);
|
||||
GameEventController(game, c);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GameMainLoop(WindowGame* game, void* data) {
|
||||
void GameMainLoop(WindowGame& game) {
|
||||
bool need_exit = false;
|
||||
|
||||
Common::Timer timer;
|
||||
timer.Start();
|
||||
|
||||
if (!GameInit(game, timer, data)) {
|
||||
if (!GameInit(game, timer)) {
|
||||
need_exit = true;
|
||||
}
|
||||
|
||||
@@ -916,7 +881,7 @@ void GameMainLoop(WindowGame* game, void* data) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (game->m_game_is_paused) {
|
||||
if (game.m_game_is_paused) {
|
||||
if (!timer.IsPaused()) {
|
||||
timer.Pause();
|
||||
}
|
||||
@@ -924,13 +889,13 @@ void GameMainLoop(WindowGame* game, void* data) {
|
||||
GameWaitEvent(game);
|
||||
|
||||
GameProcessEvent(game, timer.GetTimeS());
|
||||
need_exit = game->m_game_need_exit;
|
||||
need_exit = game.m_game_need_exit;
|
||||
continue;
|
||||
}
|
||||
|
||||
need_exit = game->m_game_need_exit;
|
||||
need_exit = game.m_game_need_exit;
|
||||
|
||||
if (game->m_game_is_paused) {
|
||||
if (game.m_game_is_paused) {
|
||||
if (!timer.IsPaused()) {
|
||||
timer.Pause();
|
||||
}
|
||||
@@ -952,14 +917,13 @@ void GameMainLoop(WindowGame* game, void* data) {
|
||||
GameClose(game);
|
||||
}
|
||||
|
||||
static void WindowCreate(WindowContext* ctx) {
|
||||
EXIT_IF(ctx == nullptr);
|
||||
EXIT_IF(ctx->window != nullptr);
|
||||
EXIT_IF(ctx->graphic_ctx.screen_width == 0);
|
||||
EXIT_IF(ctx->graphic_ctx.screen_height == 0);
|
||||
static void WindowCreate(WindowContext& context) {
|
||||
EXIT_IF(context.window != nullptr);
|
||||
EXIT_IF(context.graphic_ctx.screen_width == 0);
|
||||
EXIT_IF(context.graphic_ctx.screen_height == 0);
|
||||
|
||||
int width = static_cast<int>(ctx->graphic_ctx.screen_width);
|
||||
int height = static_cast<int>(ctx->graphic_ctx.screen_height);
|
||||
int width = static_cast<int>(context.graphic_ctx.screen_width);
|
||||
int height = static_cast<int>(context.graphic_ctx.screen_height);
|
||||
|
||||
#if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
|
||||
SDL_SetHint(SDL_HINT_WINDOWS_DPI_SCALING, "0");
|
||||
@@ -971,17 +935,17 @@ static void WindowCreate(WindowContext* ctx) {
|
||||
|
||||
LOGF("WindowCreate(): width = %d, height = %d\n", width, height);
|
||||
|
||||
ctx->window =
|
||||
context.window =
|
||||
SDL_CreateWindow(KYTY_SDL_WINDOW_CAPTION, KYTY_SDL_WINDOWPOS_CENTERED,
|
||||
KYTY_SDL_WINDOWPOS_CENTERED, width, height, KYTY_SDL_WINDOW_FLAGS);
|
||||
|
||||
ctx->window_hidden = true;
|
||||
context.window_hidden = true;
|
||||
|
||||
if (ctx->window == nullptr) {
|
||||
if (context.window == nullptr) {
|
||||
EXIT("%s\n", SDL_GetError());
|
||||
}
|
||||
|
||||
SDL_SetWindowResizable(ctx->window, SDL_FALSE);
|
||||
SDL_SetWindowResizable(context.window, SDL_FALSE);
|
||||
}
|
||||
|
||||
void WindowInit(uint32_t width, uint32_t height) {
|
||||
@@ -1015,19 +979,21 @@ void WindowRun() {
|
||||
{
|
||||
EXIT_IF(g_window_ctx->graphic_initialized);
|
||||
|
||||
WindowCreate(g_window_ctx);
|
||||
VulkanCreate(g_window_ctx);
|
||||
WindowCreate(*g_window_ctx);
|
||||
VulkanCreate(*g_window_ctx);
|
||||
|
||||
g_window_ctx->game = &game;
|
||||
|
||||
g_window_ctx->graphic_initialized = true;
|
||||
g_window_ctx->graphic_initialized_condvar.Signal();
|
||||
}
|
||||
g_window_ctx->mutex.Unlock();
|
||||
|
||||
GraphicsRenderCreateContext();
|
||||
|
||||
GameMainLoop(&game, &g_window_ctx->graphic_ctx);
|
||||
g_window_ctx->mutex.Lock();
|
||||
g_window_ctx->graphic_initialized = true;
|
||||
g_window_ctx->graphic_initialized_condvar.Signal();
|
||||
g_window_ctx->mutex.Unlock();
|
||||
|
||||
GameMainLoop(game);
|
||||
|
||||
// TODO: replace std::_Exit shutdown with full Vulkan teardown, then destroy
|
||||
// the VMA allocator immediately before vkDestroyDevice.
|
||||
@@ -1043,12 +1009,12 @@ vk::SurfaceCapabilitiesKHR* VulkanGetSurfaceCapabilities() {
|
||||
return &g_window_ctx->surface_capabilities->capabilities;
|
||||
}
|
||||
|
||||
GraphicContext* WindowGetGraphicContext() {
|
||||
GraphicContext& WindowGetGraphicContext() {
|
||||
EXIT_IF(g_window_ctx == nullptr);
|
||||
|
||||
Common::LockGuard lock(g_window_ctx->mutex);
|
||||
|
||||
return &g_window_ctx->graphic_ctx;
|
||||
return g_window_ctx->graphic_ctx;
|
||||
}
|
||||
|
||||
static int WindowIconRead(void* user, char* data, int size) {
|
||||
|
||||
@@ -41,9 +41,9 @@ struct WindowContext {
|
||||
extern WindowContext* g_window_ctx;
|
||||
|
||||
void VulkanGetSurfaceCapabilities(vk::PhysicalDevice physical_device, vk::SurfaceKHR surface,
|
||||
SurfaceCapabilities* capabilities);
|
||||
VulkanSwapchain* VulkanCreateSwapchain(GraphicContext* ctx, uint32_t image_count);
|
||||
void VulkanCreate(WindowContext* ctx);
|
||||
SurfaceCapabilities& capabilities);
|
||||
VulkanSwapchain* VulkanCreateSwapchain(uint32_t image_count);
|
||||
void VulkanCreate(WindowContext& window);
|
||||
|
||||
void WindowUpdateIcon();
|
||||
void WindowUpdateTitle();
|
||||
|
||||
Reference in new issue
Block a user