Formatted EVERYTHING
This commit is contained in:
@@ -1,81 +1,64 @@
|
||||
#include "Window.h"
|
||||
#include "Resources.h"
|
||||
#include "Graphics.h"
|
||||
#include "Enums.h"
|
||||
#include "Command.h"
|
||||
#include "Enums.h"
|
||||
#include "Graphics.h"
|
||||
#include "Resources.h"
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
|
||||
using namespace Seele;
|
||||
using namespace Seele::Vulkan;
|
||||
|
||||
double currentFrameDelta = 0;
|
||||
double Gfx::getCurrentFrameDelta()
|
||||
{
|
||||
return currentFrameDelta;
|
||||
}
|
||||
double Gfx::getCurrentFrameDelta() { return currentFrameDelta; }
|
||||
|
||||
uint32 currentFrameIndex = 0;
|
||||
uint32 Gfx::getCurrentFrameIndex()
|
||||
{
|
||||
return currentFrameIndex;
|
||||
}
|
||||
uint32 Gfx::getCurrentFrameIndex() { return currentFrameIndex; }
|
||||
|
||||
void glfwKeyCallback(GLFWwindow* handle, int key, int, int action, int modifier)
|
||||
{
|
||||
if (key == -1)
|
||||
{
|
||||
void glfwKeyCallback(GLFWwindow* handle, int key, int, int action, int modifier) {
|
||||
if (key == -1) {
|
||||
return;
|
||||
}
|
||||
Window* window = (Window*)glfwGetWindowUserPointer(handle);
|
||||
window->keyPress((KeyCode)key, (InputAction)action, (KeyModifier)modifier);
|
||||
}
|
||||
|
||||
void glfwMouseMoveCallback(GLFWwindow* handle, double xpos, double ypos)
|
||||
{
|
||||
void glfwMouseMoveCallback(GLFWwindow* handle, double xpos, double ypos) {
|
||||
Window* window = (Window*)glfwGetWindowUserPointer(handle);
|
||||
window->mouseMove(xpos, ypos);
|
||||
}
|
||||
|
||||
void glfwMouseButtonCallback(GLFWwindow* handle, int button, int action, int modifier)
|
||||
{
|
||||
void glfwMouseButtonCallback(GLFWwindow* handle, int button, int action, int modifier) {
|
||||
Window* window = (Window*)glfwGetWindowUserPointer(handle);
|
||||
window->mouseButton((MouseButton)button, (InputAction)action, (KeyModifier)modifier);
|
||||
}
|
||||
|
||||
void glfwScrollCallback(GLFWwindow* handle, double xoffset, double yoffset)
|
||||
{
|
||||
void glfwScrollCallback(GLFWwindow* handle, double xoffset, double yoffset) {
|
||||
Window* window = (Window*)glfwGetWindowUserPointer(handle);
|
||||
window->scroll(xoffset, yoffset);
|
||||
}
|
||||
|
||||
void glfwFileCallback(GLFWwindow* handle, int count, const char** paths)
|
||||
{
|
||||
void glfwFileCallback(GLFWwindow* handle, int count, const char** paths) {
|
||||
Window* window = (Window*)glfwGetWindowUserPointer(handle);
|
||||
window->fileDrop(count, paths);
|
||||
}
|
||||
|
||||
void glfwCloseCallback(GLFWwindow* handle)
|
||||
{
|
||||
void glfwCloseCallback(GLFWwindow* handle) {
|
||||
Window* window = (Window*)glfwGetWindowUserPointer(handle);
|
||||
window->close();
|
||||
}
|
||||
|
||||
void glfwFramebufferResizeCallback(GLFWwindow* handle, int width, int height)
|
||||
{
|
||||
void glfwFramebufferResizeCallback(GLFWwindow* handle, int width, int height) {
|
||||
Window* window = (Window*)glfwGetWindowUserPointer(handle);
|
||||
window->resize(width, height);
|
||||
}
|
||||
|
||||
Window::Window(PGraphics graphics, const WindowCreateInfo &createInfo)
|
||||
: graphics(graphics)
|
||||
, preferences(createInfo)
|
||||
, instance(graphics->getInstance())
|
||||
, swapchain(VK_NULL_HANDLE)
|
||||
{
|
||||
Window::Window(PGraphics graphics, const WindowCreateInfo& createInfo)
|
||||
: graphics(graphics), preferences(createInfo), instance(graphics->getInstance()), swapchain(VK_NULL_HANDLE) {
|
||||
float xscale, yscale;
|
||||
glfwGetMonitorContentScale(glfwGetPrimaryMonitor(), &xscale, &yscale);
|
||||
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
||||
GLFWwindow *handle = glfwCreateWindow(createInfo.width / xscale, createInfo.height / yscale, createInfo.title, nullptr, nullptr);
|
||||
GLFWwindow* handle = glfwCreateWindow(createInfo.width / xscale, createInfo.height / yscale, createInfo.title, nullptr, nullptr);
|
||||
windowHandle = handle;
|
||||
glfwSetWindowUserPointer(handle, this);
|
||||
|
||||
@@ -86,10 +69,10 @@ Window::Window(PGraphics graphics, const WindowCreateInfo &createInfo)
|
||||
glfwSetDropCallback(handle, &glfwFileCallback);
|
||||
glfwSetWindowCloseCallback(handle, &glfwCloseCallback);
|
||||
glfwSetFramebufferSizeCallback(handle, &glfwFramebufferResizeCallback);
|
||||
//glfwSetWindowSizeCallback(handle, &glfwResizeCallback);
|
||||
// glfwSetWindowSizeCallback(handle, &glfwResizeCallback);
|
||||
|
||||
glfwCreateWindowSurface(instance, handle, nullptr, &surface);
|
||||
|
||||
|
||||
querySurface();
|
||||
chooseSwapSurfaceFormat();
|
||||
framebufferFormat = cast(format.format);
|
||||
@@ -100,31 +83,28 @@ Window::Window(PGraphics graphics, const WindowCreateInfo &createInfo)
|
||||
createSwapChain();
|
||||
}
|
||||
|
||||
Window::~Window()
|
||||
{
|
||||
Window::~Window() {
|
||||
vkDestroySwapchainKHR(graphics->getDevice(), swapchain, nullptr);
|
||||
vkDestroySurfaceKHR(instance, surface, nullptr);
|
||||
glfwDestroyWindow(static_cast<GLFWwindow *>(windowHandle));
|
||||
glfwDestroyWindow(static_cast<GLFWwindow*>(windowHandle));
|
||||
}
|
||||
|
||||
void Window::pollInput()
|
||||
{
|
||||
glfwPollEvents();
|
||||
}
|
||||
void Window::pollInput() { glfwPollEvents(); }
|
||||
|
||||
void Window::beginFrame()
|
||||
{
|
||||
void Window::beginFrame() {
|
||||
imageAvailableFences[currentSemaphoreIndex]->reset();
|
||||
vkAcquireNextImageKHR(graphics->getDevice(), swapchain, std::numeric_limits<uint64>::max(), imageAvailableSemaphores[currentSemaphoreIndex]->getHandle(), imageAvailableFences[currentSemaphoreIndex]->getHandle(), ¤tImageIndex);
|
||||
vkAcquireNextImageKHR(graphics->getDevice(), swapchain, std::numeric_limits<uint64>::max(),
|
||||
imageAvailableSemaphores[currentSemaphoreIndex]->getHandle(),
|
||||
imageAvailableFences[currentSemaphoreIndex]->getHandle(), ¤tImageIndex);
|
||||
imageAvailableFences[currentSemaphoreIndex]->submit();
|
||||
graphics->getGraphicsCommands()->getCommands()->waitForSemaphore(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, imageAvailableSemaphores[currentSemaphoreIndex]);
|
||||
graphics->getGraphicsCommands()->getCommands()->waitForSemaphore(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
|
||||
imageAvailableSemaphores[currentSemaphoreIndex]);
|
||||
}
|
||||
|
||||
void Window::endFrame()
|
||||
{
|
||||
swapChainTextures[currentImageIndex]->changeLayout(Gfx::SE_IMAGE_LAYOUT_PRESENT_SRC_KHR,
|
||||
Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
|
||||
Gfx::SE_ACCESS_MEMORY_READ_BIT, Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
|
||||
void Window::endFrame() {
|
||||
swapChainTextures[currentImageIndex]->changeLayout(Gfx::SE_IMAGE_LAYOUT_PRESENT_SRC_KHR, Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
|
||||
Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, Gfx::SE_ACCESS_MEMORY_READ_BIT,
|
||||
Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
|
||||
graphics->getGraphicsCommands()->submitCommands(renderingDoneSemaphores[currentSemaphoreIndex]);
|
||||
VkSemaphore renderDoneHandle = renderingDoneSemaphores[currentSemaphoreIndex]->getHandle();
|
||||
VkPresentInfoKHR presentInfo = {
|
||||
@@ -138,99 +118,49 @@ void Window::endFrame()
|
||||
.pResults = nullptr,
|
||||
};
|
||||
VkResult r = vkQueuePresentKHR(graphics->getGraphicsCommands()->getQueue()->getHandle(), &presentInfo);
|
||||
if(r == VK_SUCCESS)
|
||||
{ }
|
||||
else if (r == VK_ERROR_OUT_OF_DATE_KHR || r == VK_SUBOPTIMAL_KHR)
|
||||
{
|
||||
if (r == VK_SUCCESS) {
|
||||
} else if (r == VK_ERROR_OUT_OF_DATE_KHR || r == VK_SUBOPTIMAL_KHR) {
|
||||
createSwapChain();
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
VK_CHECK(r);
|
||||
}
|
||||
currentSemaphoreIndex = (currentSemaphoreIndex + 1) % Gfx::numFramesBuffered;
|
||||
currentFrameIndex = currentSemaphoreIndex;
|
||||
//graphics->waitDeviceIdle();
|
||||
// graphics->waitDeviceIdle();
|
||||
}
|
||||
|
||||
void Window::onWindowCloseEvent()
|
||||
{
|
||||
}
|
||||
void Window::onWindowCloseEvent() {}
|
||||
|
||||
Gfx::PTexture2D Window::getBackBuffer() const
|
||||
{
|
||||
return PTexture2D(swapChainTextures[currentImageIndex]);
|
||||
}
|
||||
Gfx::PTexture2D Window::getBackBuffer() const { return PTexture2D(swapChainTextures[currentImageIndex]); }
|
||||
|
||||
void Window::setKeyCallback(std::function<void(KeyCode, InputAction, KeyModifier)> callback)
|
||||
{
|
||||
keyCallback = callback;
|
||||
}
|
||||
void Window::setKeyCallback(std::function<void(KeyCode, InputAction, KeyModifier)> callback) { keyCallback = callback; }
|
||||
|
||||
void Window::setMouseMoveCallback(std::function<void(double, double)> callback)
|
||||
{
|
||||
mouseMoveCallback = callback;
|
||||
}
|
||||
void Window::setMouseMoveCallback(std::function<void(double, double)> callback) { mouseMoveCallback = callback; }
|
||||
|
||||
void Window::setMouseButtonCallback(std::function<void(MouseButton, InputAction, KeyModifier)> callback)
|
||||
{
|
||||
mouseButtonCallback = callback;
|
||||
}
|
||||
void Window::setMouseButtonCallback(std::function<void(MouseButton, InputAction, KeyModifier)> callback) { mouseButtonCallback = callback; }
|
||||
|
||||
void Window::setScrollCallback(std::function<void(double, double)> callback)
|
||||
{
|
||||
scrollCallback = callback;
|
||||
}
|
||||
void Window::setScrollCallback(std::function<void(double, double)> callback) { scrollCallback = callback; }
|
||||
|
||||
void Window::setFileCallback(std::function<void(int, const char**)> callback)
|
||||
{
|
||||
fileCallback = callback;
|
||||
}
|
||||
void Window::setFileCallback(std::function<void(int, const char**)> callback) { fileCallback = callback; }
|
||||
|
||||
void Window::setCloseCallback(std::function<void()> callback)
|
||||
{
|
||||
closeCallback = callback;
|
||||
}
|
||||
void Window::setCloseCallback(std::function<void()> callback) { closeCallback = callback; }
|
||||
|
||||
void Window::setResizeCallback(std::function<void(uint32, uint32)> callback)
|
||||
{
|
||||
resizeCallback = callback;
|
||||
}
|
||||
void Window::setResizeCallback(std::function<void(uint32, uint32)> callback) { resizeCallback = callback; }
|
||||
|
||||
void Window::keyPress(KeyCode code, InputAction action, KeyModifier modifier)
|
||||
{
|
||||
keyCallback(code, action, modifier);
|
||||
}
|
||||
void Window::keyPress(KeyCode code, InputAction action, KeyModifier modifier) { keyCallback(code, action, modifier); }
|
||||
|
||||
void Window::mouseMove(double x, double y)
|
||||
{
|
||||
mouseMoveCallback(x, y);
|
||||
}
|
||||
void Window::mouseMove(double x, double y) { mouseMoveCallback(x, y); }
|
||||
|
||||
void Window::mouseButton(MouseButton button, InputAction action, KeyModifier modifier)
|
||||
{
|
||||
mouseButtonCallback(button, action, modifier);
|
||||
}
|
||||
void Window::mouseButton(MouseButton button, InputAction action, KeyModifier modifier) { mouseButtonCallback(button, action, modifier); }
|
||||
|
||||
void Window::scroll(double x, double y)
|
||||
{
|
||||
scrollCallback(x, y);
|
||||
}
|
||||
void Window::scroll(double x, double y) { scrollCallback(x, y); }
|
||||
|
||||
void Window::fileDrop(int num, const char** files)
|
||||
{
|
||||
fileCallback(num, files);
|
||||
}
|
||||
void Window::fileDrop(int num, const char** files) { fileCallback(num, files); }
|
||||
|
||||
void Window::close()
|
||||
{
|
||||
closeCallback();
|
||||
}
|
||||
void Window::close() { closeCallback(); }
|
||||
|
||||
void Window::resize(int width, int height)
|
||||
{
|
||||
if (width == 0 || height == 0)
|
||||
{
|
||||
void Window::resize(int width, int height) {
|
||||
if (width == 0 || height == 0) {
|
||||
paused = true;
|
||||
return;
|
||||
}
|
||||
@@ -246,8 +176,7 @@ void Window::resize(int width, int height)
|
||||
resizeCallback(width, height);
|
||||
}
|
||||
|
||||
void Window::querySurface()
|
||||
{
|
||||
void Window::querySurface() {
|
||||
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(graphics->getPhysicalDevice(), surface, &capabilities);
|
||||
|
||||
uint32 numFormats;
|
||||
@@ -261,20 +190,15 @@ void Window::querySurface()
|
||||
vkGetPhysicalDeviceSurfacePresentModesKHR(graphics->getPhysicalDevice(), surface, &numPresentModes, supportedPresentModes.data());
|
||||
}
|
||||
|
||||
void Window::chooseSwapSurfaceFormat()
|
||||
{
|
||||
for (const auto& supportedFormat : supportedFormats)
|
||||
{
|
||||
if (supportedFormat.format == cast(preferences.preferredFormat))
|
||||
{
|
||||
void Window::chooseSwapSurfaceFormat() {
|
||||
for (const auto& supportedFormat : supportedFormats) {
|
||||
if (supportedFormat.format == cast(preferences.preferredFormat)) {
|
||||
format = supportedFormat;
|
||||
return;
|
||||
}
|
||||
}
|
||||
for (const auto& supportedFormat : supportedFormats)
|
||||
{
|
||||
if (supportedFormat.format == VK_FORMAT_R8G8B8A8_SRGB && supportedFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
|
||||
{
|
||||
for (const auto& supportedFormat : supportedFormats) {
|
||||
if (supportedFormat.format == VK_FORMAT_R8G8B8A8_SRGB && supportedFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
|
||||
format = supportedFormat;
|
||||
return;
|
||||
}
|
||||
@@ -282,12 +206,9 @@ void Window::chooseSwapSurfaceFormat()
|
||||
format = supportedFormats[0];
|
||||
}
|
||||
|
||||
void Window::chooseSwapPresentMode()
|
||||
{
|
||||
for (const auto& supportedPresentMode : supportedPresentModes)
|
||||
{
|
||||
if (supportedPresentMode == VK_PRESENT_MODE_MAILBOX_KHR)
|
||||
{
|
||||
void Window::chooseSwapPresentMode() {
|
||||
for (const auto& supportedPresentMode : supportedPresentModes) {
|
||||
if (supportedPresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
|
||||
presentMode = supportedPresentMode;
|
||||
return;
|
||||
}
|
||||
@@ -295,8 +216,7 @@ void Window::chooseSwapPresentMode()
|
||||
presentMode = VK_PRESENT_MODE_FIFO_KHR;
|
||||
}
|
||||
|
||||
void Window::chooseSwapExtent()
|
||||
{
|
||||
void Window::chooseSwapExtent() {
|
||||
if (capabilities.currentExtent.width != std::numeric_limits<uint32>::max()) {
|
||||
extent = capabilities.currentExtent;
|
||||
return;
|
||||
@@ -312,8 +232,7 @@ void Window::chooseSwapExtent()
|
||||
extent.height = std::clamp(extent.height, capabilities.minImageExtent.height, capabilities.maxImageExtent.height);
|
||||
}
|
||||
|
||||
void Window::createSwapChain()
|
||||
{
|
||||
void Window::createSwapChain() {
|
||||
uint32 imageCount = Gfx::numFramesBuffered;
|
||||
VkSwapchainCreateInfoKHR createInfo = {
|
||||
.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
|
||||
@@ -336,21 +255,21 @@ void Window::createSwapChain()
|
||||
|
||||
VK_CHECK(vkGetSwapchainImagesKHR(graphics->getDevice(), swapchain, &imageCount, swapChainImages.data()));
|
||||
|
||||
for (uint32 i = 0; i < imageCount; ++i)
|
||||
{
|
||||
swapChainTextures[i] = new Texture2D(graphics, TextureCreateInfo{
|
||||
.format = cast(format.format),
|
||||
.width = extent.width,
|
||||
.height = extent.height,
|
||||
.depth = 1,
|
||||
.mipLevels = 1,
|
||||
.layers = 1,
|
||||
.elements = 1,
|
||||
.samples = 1,
|
||||
.usage = Gfx::SE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | Gfx::SE_IMAGE_USAGE_TRANSFER_DST_BIT,
|
||||
}, swapChainImages[i]);
|
||||
if(imageAvailableFences[i] != nullptr)
|
||||
{
|
||||
for (uint32 i = 0; i < imageCount; ++i) {
|
||||
swapChainTextures[i] = new Texture2D(graphics,
|
||||
TextureCreateInfo{
|
||||
.format = cast(format.format),
|
||||
.width = extent.width,
|
||||
.height = extent.height,
|
||||
.depth = 1,
|
||||
.mipLevels = 1,
|
||||
.layers = 1,
|
||||
.elements = 1,
|
||||
.samples = 1,
|
||||
.usage = Gfx::SE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | Gfx::SE_IMAGE_USAGE_TRANSFER_DST_BIT,
|
||||
},
|
||||
swapChainImages[i]);
|
||||
if (imageAvailableFences[i] != nullptr) {
|
||||
imageAvailableFences[i]->wait(100000);
|
||||
imageAvailableFences[i]->reset();
|
||||
}
|
||||
@@ -360,24 +279,19 @@ void Window::createSwapChain()
|
||||
}
|
||||
}
|
||||
|
||||
Viewport::Viewport(PWindow owner, const ViewportCreateInfo &viewportInfo)
|
||||
: Gfx::Viewport(owner, viewportInfo)
|
||||
{
|
||||
Viewport::Viewport(PWindow owner, const ViewportCreateInfo& viewportInfo) : Gfx::Viewport(owner, viewportInfo) {
|
||||
handle.width = static_cast<float>(sizeX);
|
||||
handle.height = static_cast<float>(sizeY);
|
||||
handle.x = static_cast<float>(offsetX);
|
||||
handle.y = static_cast<float>(offsetY) + handle.height;
|
||||
handle.height = -handle.height;
|
||||
handle.minDepth = 1.f;
|
||||
handle.maxDepth = 0.f;
|
||||
handle.minDepth = 1.f;
|
||||
handle.maxDepth = 0.f;
|
||||
}
|
||||
|
||||
Viewport::~Viewport()
|
||||
{
|
||||
}
|
||||
Viewport::~Viewport() {}
|
||||
|
||||
void Viewport::resize(uint32 newX, uint32 newY)
|
||||
{
|
||||
void Viewport::resize(uint32 newX, uint32 newY) {
|
||||
sizeX = newX;
|
||||
sizeY = newY;
|
||||
handle.width = static_cast<float>(sizeX);
|
||||
@@ -385,8 +299,7 @@ void Viewport::resize(uint32 newX, uint32 newY)
|
||||
handle.height = -static_cast<float>(sizeY);
|
||||
}
|
||||
|
||||
void Viewport::move(uint32 newOffsetX, uint32 newOffsetY)
|
||||
{
|
||||
void Viewport::move(uint32 newOffsetX, uint32 newOffsetY) {
|
||||
offsetX = newOffsetX;
|
||||
offsetY = newOffsetY;
|
||||
handle.x = static_cast<float>(offsetX);
|
||||
|
||||
Reference in New Issue
Block a user