summaryrefslogtreecommitdiff
path: root/main.cpp
diff options
context:
space:
mode:
authorMiguel <m.i@gmx.at>2025-01-18 23:42:15 +0100
committerMiguel <m.i@gmx.at>2025-01-18 23:42:15 +0100
commitcb1ad9f907a879a4402bcd121502f5e74aedd2f9 (patch)
tree18f1838b1c93b0728c2e13025c8929efdf9c856c /main.cpp
parent950f6305fa70bd8c8e6acbac55cb33d23954207d (diff)
cleanup and some tiny experiments
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp41
1 files changed, 24 insertions, 17 deletions
diff --git a/main.cpp b/main.cpp
index b57659b..e098ea8 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,5 +1,11 @@
// Coding along: https://docs.vulkan.org/tutorial/
+// comment and simplify and abstract away
+// make pure/static as much as possible
+// build with cmake for: linux/freebsd/mac/win/android/ios?
+// raymarch game
+
+
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
@@ -30,6 +36,7 @@ public:
private:
+ static const int MAX_FRAMES_IN_FLIGHT = 2;
static const uint32_t WIDTH = 800;
static const uint32_t HEIGHT = 600;
@@ -220,7 +227,8 @@ private:
scissor.extent = swapChainExtent;
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
- vkCmdDraw(commandBuffer, 3, 1, 0, 0);
+ // verex count, instance count, first vertex, first instance
+ vkCmdDraw(commandBuffer, 6, 1, 0, 0);
vkCmdEndRenderPass(commandBuffer);
if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS) {
@@ -297,10 +305,10 @@ private:
subpass.pColorAttachments = &colorAttachmentRef;
VkSubpassDependency dependency{};
- dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
- dependency.dstSubpass = 0;
+ dependency.srcSubpass = VK_SUBPASS_EXTERNAL; // implicit subpass before OR after render pass (depends if defined on src or dst)
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.srcAccessMask = 0;
+ dependency.dstSubpass = 0; // our one and only pass
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
@@ -363,7 +371,7 @@ private:
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
inputAssembly.primitiveRestartEnable = VK_FALSE;
-
+/*
VkViewport viewport{};
viewport.x = 0.0f;
viewport.y = 0.0f;
@@ -375,8 +383,9 @@ private:
VkRect2D scissor{};
scissor.offset = {0, 0};
scissor.extent = swapChainExtent;
+*/
-// DYN STATE?
+// DYN STATE
VkPipelineViewportStateCreateInfo viewportState{};
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportState.viewportCount = 1;
@@ -397,7 +406,6 @@ private:
rasterizer.lineWidth = 1.0f;
rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
rasterizer.frontFace = VK_FRONT_FACE_CLOCKWISE;
-
rasterizer.depthBiasEnable = VK_FALSE;
rasterizer.depthBiasConstantFactor = 0.0f; // Optional
rasterizer.depthBiasClamp = 0.0f; // Optional
@@ -534,6 +542,7 @@ private:
uint32_t queueFamilyIndices[] = {indices.graphicsFamily.value(), indices.presentFamily.value()};
if (indices.graphicsFamily != indices.presentFamily) {
+ // handle swap chain across different queues
createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
createInfo.queueFamilyIndexCount = 2;
createInfo.pQueueFamilyIndices = queueFamilyIndices;
@@ -542,10 +551,11 @@ private:
createInfo.queueFamilyIndexCount = 0; // Optional
createInfo.pQueueFamilyIndices = nullptr; // Optional
}
+
createInfo.preTransform = swapChainSupport.capabilities.currentTransform;
createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
createInfo.presentMode = presentMode;
- createInfo.clipped = VK_TRUE;
+ createInfo.clipped = VK_TRUE; // don't care about pixels obscured by other windows (we do not need to read them back)
createInfo.oldSwapchain = VK_NULL_HANDLE;
if (vkCreateSwapchainKHR(device, &createInfo, nullptr, &swapChain) != VK_SUCCESS) {
@@ -621,7 +631,7 @@ private:
vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
for (const auto& device : devices)
- if (isDeviceSuitable(device)) { physicalDevice = device; break; }
+ if (isDeviceSuitable(device)) { if (physicalDevice == VK_NULL_HANDLE) physicalDevice=device; }
if (physicalDevice == VK_NULL_HANDLE) throw std::runtime_error("failed to find a suitable GPU!");
}
@@ -684,6 +694,7 @@ private:
vkWaitForFences(device, 1, &inFlightFence, VK_TRUE, UINT64_MAX);
vkResetFences(device, 1, &inFlightFence);
+
uint32_t imageIndex;
vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, imageAvailableSemaphore, VK_NULL_HANDLE, &imageIndex);
vkResetCommandBuffer(commandBuffer, 0);
@@ -691,30 +702,26 @@ private:
VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
-
VkSemaphore waitSemaphores[] = {imageAvailableSemaphore};
+ VkSemaphore signalSemaphores[] = {renderFinishedSemaphore};
VkPipelineStageFlags waitStages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT};
submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = waitSemaphores;
submitInfo.pWaitDstStageMask = waitStages;
-
- submitInfo.commandBufferCount = 1;
- submitInfo.pCommandBuffers = &commandBuffer;
-
- VkSemaphore signalSemaphores[] = {renderFinishedSemaphore};
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = signalSemaphores;
+ submitInfo.commandBufferCount = 1;
+ submitInfo.pCommandBuffers = &commandBuffer;
if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFence) != VK_SUCCESS) {
throw std::runtime_error("failed to submit draw command buffer!");
}
VkPresentInfoKHR presentInfo{};
+ VkSwapchainKHR swapChains[] = {swapChain};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = signalSemaphores;
-
- VkSwapchainKHR swapChains[] = {swapChain};
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = swapChains;
presentInfo.pImageIndices = &imageIndex;
@@ -778,7 +785,7 @@ private:
return true;
}
- std::vector<const char*> getRequiredExtensions() {
+ static std::vector<const char*> getRequiredExtensions() {
uint32_t glfwExtensionCount = 0;
const char** glfwExtensions;
glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount); //e.g VK_KHR_surface