Workaround broken generated structure type wrappers, rewrite vkgraphics vulkan calls
This commit is contained in:
parent
5b80993b28
commit
7c8ee5f384
2 changed files with 75 additions and 98 deletions
126
vkgraphics.cpp
126
vkgraphics.cpp
|
@ -2,6 +2,7 @@
|
|||
#include "vulkan_utl.h"
|
||||
#include "vulkan_framebuffer_utl.h"
|
||||
#include "vulkan_texture_utl.h"
|
||||
#include "vulkan_contructors.h"
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
#include <vulkan/vulkan_xlib.h>
|
||||
|
@ -89,56 +90,21 @@ struct GraphicsApplication
|
|||
//VkFence swapchainFence;
|
||||
void CreateWindow(const char* windowTitle, int& outWidth, int& outHeight, bool resizable)
|
||||
{
|
||||
if (!glfwInit()) {
|
||||
if (!glfwInit())
|
||||
return;
|
||||
}
|
||||
|
||||
const bool wantsWholeArea = outWidth <= 0 || outHeight <= 0;
|
||||
|
||||
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
||||
glfwWindowHint(GLFW_RESIZABLE, wantsWholeArea || !resizable ? GLFW_FALSE : GLFW_TRUE);
|
||||
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
|
||||
|
||||
// render full screen without overlapping taskbar
|
||||
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
|
||||
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
int w = outWidth;
|
||||
int h = outHeight;
|
||||
|
||||
if (wantsWholeArea) {
|
||||
int areaW = 0;
|
||||
int areaH = 0;
|
||||
glfwGetMonitorWorkarea(monitor, &x, &y, &areaW, &areaH);
|
||||
auto getPercent = [](int value, int percent) {
|
||||
assert(percent > 0 && percent <= 100);
|
||||
return static_cast<int>(static_cast<float>(value) * static_cast<float>(percent) / 100.0f);
|
||||
};
|
||||
if (outWidth < 0) {
|
||||
w = getPercent(areaW, -outWidth);
|
||||
x = (areaW - w) / 2;
|
||||
} else {
|
||||
w = areaW;
|
||||
}
|
||||
if (outHeight < 0) {
|
||||
h = getPercent(areaH, -outHeight);
|
||||
y = (areaH - h) / 2;
|
||||
} else {
|
||||
h = areaH;
|
||||
}
|
||||
}
|
||||
|
||||
window = glfwCreateWindow(w, h, windowTitle, nullptr, nullptr);
|
||||
window = glfwCreateWindow(outWidth, outHeight, windowTitle, nullptr, nullptr);
|
||||
|
||||
if (!window) {
|
||||
glfwTerminate();
|
||||
return;
|
||||
}
|
||||
|
||||
if (wantsWholeArea) {
|
||||
glfwSetWindowPos(window, x, y);
|
||||
}
|
||||
|
||||
glfwGetWindowSize(window, &outWidth, &outHeight);
|
||||
|
||||
glfwSetKeyCallback(window, [](GLFWwindow* window, int key, int, int action, int) {
|
||||
|
@ -148,7 +114,6 @@ struct GraphicsApplication
|
|||
});
|
||||
|
||||
glfwSetErrorCallback([](int error, const char* description) { printf("GLFW Error (%i): %s\n", error, description); });
|
||||
//glfwShowWindow(window);
|
||||
}
|
||||
void DestroyWindow()
|
||||
{
|
||||
|
@ -158,33 +123,22 @@ struct GraphicsApplication
|
|||
}
|
||||
void CreateSwapchain(uint32_t width, uint32_t height)
|
||||
{
|
||||
const VkXlibSurfaceCreateInfoKHR surfaceinfo = {
|
||||
.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR,
|
||||
.flags = 0,
|
||||
.dpy = glfwGetX11Display(),
|
||||
.window = glfwGetX11Window(window),
|
||||
};
|
||||
VK_CHECK_RESULT(vkCreateXlibSurfaceKHR(context.instance, &surfaceinfo, nullptr, &surface));
|
||||
const VkSwapchainCreateInfoKHR swapchain_create_info = {
|
||||
.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
|
||||
.surface = surface,
|
||||
.minImageCount = 4,
|
||||
// todo: guess format
|
||||
.imageFormat = VK_FORMAT_B8G8R8A8_UNORM,
|
||||
.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR,
|
||||
.imageExtent = {.width = width, .height = height},
|
||||
.imageArrayLayers = 1,
|
||||
.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
|
||||
.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
|
||||
.queueFamilyIndexCount = 1,
|
||||
.pQueueFamilyIndices = &dev.defaultFamilyIndex,
|
||||
.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR,
|
||||
.compositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
|
||||
.presentMode = VK_PRESENT_MODE_FIFO_KHR,
|
||||
.clipped = VK_TRUE,
|
||||
.oldSwapchain = VK_NULL_HANDLE,
|
||||
};
|
||||
vkCreateSwapchainKHR(dev.device, &swapchain_create_info, nullptr, &swapchain);
|
||||
VK_CHECK_RESULT(CallWith($M(VkXlibSurfaceCreateInfoKHR{VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR},
|
||||
$(dpy) = glfwGetX11Display(), $(window) = glfwGetX11Window(window)),
|
||||
vkCreateXlibSurfaceKHR(context.instance, &ref, nullptr, &surface)));
|
||||
CallWith($M(
|
||||
VkSwapchainCreateInfoKHR{VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR},
|
||||
$(surface) = surface, $(minImageCount) = 4,
|
||||
$(imageFormat) = VK_FORMAT_B8G8R8A8_UNORM,
|
||||
$(imageColorSpace) = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR,
|
||||
$(imageExtent) = VkExtent2D{width, height},
|
||||
$(imageArrayLayers),$(queueFamilyIndexCount),$(clipped),
|
||||
$(imageUsage) = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
|
||||
$(pQueueFamilyIndices) = &dev.defaultFamilyIndex,
|
||||
$(preTransform) = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR,
|
||||
$(compositeAlpha) = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
|
||||
$(presentMode) = VK_PRESENT_MODE_FIFO_KHR),
|
||||
vkCreateSwapchainKHR(dev.device, &ref, nullptr, &swapchain));
|
||||
|
||||
numSwapchainImages = MAX_SWAPCHAIN_IMAGES;
|
||||
|
||||
|
@ -198,9 +152,6 @@ struct GraphicsApplication
|
|||
VK_CHECK_RESULT(vkCreateSemaphore(dev.device, &semaphoreCreateInfo, nullptr, &swapchainPresentSemaphore[i]));
|
||||
VK_CHECK_RESULT(vkCreateSemaphore(dev.device, &semaphoreCreateInfo, nullptr, &swapchainRenderSemaphore[i]));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
unsigned int AcquireImage(int sem_index, VkFence fence = NULL)
|
||||
{
|
||||
|
@ -210,13 +161,8 @@ struct GraphicsApplication
|
|||
}
|
||||
void PresentImage(unsigned int index)
|
||||
{
|
||||
VkPresentInfoKHR presentInfo = {VK_STRUCTURE_TYPE_PRESENT_INFO_KHR};
|
||||
presentInfo.swapchainCount = 1;
|
||||
presentInfo.pSwapchains = &swapchain;
|
||||
presentInfo.pImageIndices = &index;
|
||||
presentInfo.pWaitSemaphores = &swapchainPresentSemaphore[index];
|
||||
presentInfo.waitSemaphoreCount = 1;
|
||||
vkQueuePresentKHR(dev.defautQueue, &presentInfo);
|
||||
CallWith(PresentInfo(&swapchain, &index, &swapchainPresentSemaphore[index]),
|
||||
vkQueuePresentKHR(dev.defautQueue, &ref));
|
||||
}
|
||||
void DestroySwapchain()
|
||||
{
|
||||
|
@ -228,8 +174,6 @@ struct GraphicsApplication
|
|||
}
|
||||
vkDestroySwapchainKHR(dev.device, swapchain, NULL);
|
||||
vkDestroySurfaceKHR(context.instance,surface,NULL);
|
||||
|
||||
|
||||
}
|
||||
|
||||
// todo: NUKE THIS!!!
|
||||
|
@ -304,10 +248,8 @@ struct GraphicsApplication
|
|||
vkCmdEndRenderPass(commandBuffers[i]);
|
||||
VulkanTexture::SetImageLayout(commandBuffers[i], swapchainImages[i], VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 });
|
||||
vkEndCommandBuffer(commandBuffers[i]);
|
||||
VkFenceCreateInfo fenceCreateInfo = {};
|
||||
fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
|
||||
fenceCreateInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
|
||||
VK_CHECK_RESULT(vkCreateFence(dev.device, &fenceCreateInfo, NULL, &chainFences[i]));
|
||||
VK_CHECK_RESULT(CallWith(FenceInfo(VK_FENCE_CREATE_SIGNALED_BIT),
|
||||
vkCreateFence(dev.device, &ref, NULL, &chainFences[i])));
|
||||
}
|
||||
uint32_t idx = 0;
|
||||
int sem_idx = 0;
|
||||
|
@ -317,20 +259,15 @@ struct GraphicsApplication
|
|||
sem_idx = (sem_idx + 1) % numSwapchainImages;
|
||||
waitFence(sem_idx);
|
||||
idx = AcquireImage(sem_idx);
|
||||
VkSubmitInfo submitInfo = {VK_STRUCTURE_TYPE_SUBMIT_INFO};
|
||||
|
||||
submitInfo.pWaitSemaphores = &swapchainRenderSemaphore[sem_idx];
|
||||
submitInfo.waitSemaphoreCount = 1;
|
||||
submitInfo.pSignalSemaphores = &swapchainPresentSemaphore[idx];
|
||||
submitInfo.signalSemaphoreCount = 1;
|
||||
|
||||
submitInfo.commandBufferCount = 1;
|
||||
submitInfo.pCommandBuffers = &commandBuffers[idx];
|
||||
VkPipelineStageFlags waitDstStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
|
||||
submitInfo.pWaitDstStageMask = &waitDstStageMask;
|
||||
|
||||
vkResetFences(dev.device,1, &chainFences[sem_idx]);
|
||||
vkQueueSubmit(dev.defautQueue, 1, &submitInfo, chainFences[sem_idx]);
|
||||
CallWith(
|
||||
SubmitInfo(
|
||||
commandBuffers[idx],$(waitSemaphoreCount), $(signalSemaphoreCount),
|
||||
$(pWaitSemaphores) = &swapchainRenderSemaphore[sem_idx],
|
||||
$(pSignalSemaphores) = &swapchainPresentSemaphore[idx],
|
||||
$(pWaitDstStageMask) = &waitDstStageMask),
|
||||
vkQueueSubmit(dev.defautQueue, 1, &ref, chainFences[sem_idx]));
|
||||
PresentImage(idx);
|
||||
}
|
||||
vkDeviceWaitIdle(dev.device);
|
||||
|
@ -343,7 +280,6 @@ struct GraphicsApplication
|
|||
dev.Destroy();
|
||||
context.Destroy();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
int main()
|
||||
|
|
|
@ -26,10 +26,8 @@
|
|||
#define VULKAN_CONTRUCTORS_H
|
||||
#include <vulkan/vulkan.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef VK_CHECK_RESULT
|
||||
#define VK_CHECK_RESULT(f) \
|
||||
{ \
|
||||
|
@ -87,6 +85,17 @@ VkSubmitInfo SubmitInfo(const VkCommandBuffer &commandBuffer, const Args&... arg
|
|||
return $M(VkSubmitInfo{VK_STRUCTURE_TYPE_SUBMIT_INFO},
|
||||
$(commandBufferCount), $(pCommandBuffers) &= commandBuffer, arguments...);
|
||||
}
|
||||
template <typename... Args>
|
||||
VkPresentInfoKHR PresentInfo(const VkSwapchainKHR *swapchain, const unsigned int *index, const VkSemaphore *semaphore = NULL, const Args&... arguments )
|
||||
{
|
||||
return $M(VkPresentInfoKHR{VK_STRUCTURE_TYPE_PRESENT_INFO_KHR},
|
||||
$(swapchainCount),
|
||||
$(pSwapchains) = swapchain,
|
||||
$(pImageIndices) = index,
|
||||
$(waitSemaphoreCount) = (semaphore != NULL),
|
||||
$(pWaitSemaphores) = semaphore,
|
||||
arguments...);
|
||||
}
|
||||
|
||||
// wrapper for chains with pNext
|
||||
template <typename T, typename... Ts>
|
||||
|
@ -104,6 +113,12 @@ struct $Sc<T> : T
|
|||
$Sc(const T &t) : T(t){}
|
||||
};
|
||||
|
||||
#define CallWith(y, x)[&](const auto &ref){return x;}(y)
|
||||
|
||||
#define DISABLE_STRUCTURE_MAP
|
||||
/* generated files missing, downloaded fails to compile
|
||||
* also LvlInitStruct template disappeared!
|
||||
* khronos making something useless again! */
|
||||
#ifndef DISABLE_STRUCTURE_MAP
|
||||
#include <vulkan/generated/vk_typemap_helper.h>
|
||||
template <typename T, typename... Ts>
|
||||
|
@ -113,5 +128,31 @@ T $Vk(const Ts&... ts)
|
|||
$F(t, ts...);
|
||||
return t;
|
||||
}
|
||||
#else
|
||||
template <typename T> struct MyLvlTypeMap {};
|
||||
#define VK_STRUCT_MAP(x,y) template <> struct MyLvlTypeMap<x> { \
|
||||
static const VkStructureType kSType = y;\
|
||||
}
|
||||
|
||||
VK_STRUCT_MAP(VkMemoryFdPropertiesKHR, VK_STRUCTURE_TYPE_MEMORY_FD_PROPERTIES_KHR);
|
||||
VK_STRUCT_MAP(VkExternalMemoryImageCreateInfo,VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO);
|
||||
VK_STRUCT_MAP(VkImageDrmFormatModifierExplicitCreateInfoEXT,VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT);
|
||||
VK_STRUCT_MAP(VkImportMemoryFdInfoKHR,VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR);
|
||||
|
||||
template <typename T> T MyLvlInitStruct() {
|
||||
T out = {};
|
||||
out.sType = MyLvlTypeMap<T>::kSType;
|
||||
return out;
|
||||
}
|
||||
|
||||
template <typename T, typename... Ts>
|
||||
T $Vk(const Ts&... ts)
|
||||
{
|
||||
T t = MyLvlInitStruct<T>();
|
||||
$F(t, ts...);
|
||||
return t;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif // VULKAN_CONTRUCTORS_H
|
||||
|
|
Loading…
Reference in a new issue