From e80932e2611fc60cf2a59dc9c999ccbbffbead06 Mon Sep 17 00:00:00 2001 From: Hane Date: Tue, 12 Nov 2024 21:36:03 +0100 Subject: [PATCH] tri! --- src/main.cpp | 136 +++++++++++++++++++++++++++++++++++++++++------ src/shaderv.vert | 44 +++++++++------ src/triangle.h | 80 ++++++++++++++-------------- 3 files changed, 188 insertions(+), 72 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 9cacfc8..f5cabf4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,6 +23,15 @@ #define NUM_VIEWPORTS 1 #define NUM_SCISSORS NUM_VIEWPORTS +/* Amount of time, in nanoseconds, to wait for a command buffer to complete */ +#define FENCE_TIMEOUT 100000000 + +#if defined(NDEBUG) && defined(__GNUC__) +#define U_ASSERT_ONLY __attribute__((unused)) +#else +#define U_ASSERT_ONLY +#endif + #include //#include #include @@ -57,6 +66,10 @@ typedef struct StateInfo { * return state; * } */ +VkQueue presentQueue; +VkPresentInfoKHR presentInfo; +bool rendered = false; +void render(); LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { stif *stateInfo; @@ -69,13 +82,15 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) stateInfo = (stif*)ptr; switch (uMsg) { case WM_PAINT: - { /**/ - HGDIOBJ brush = GetStockObject(BLACK_BRUSH); - PAINTSTRUCT ps; - HDC hdc = BeginPaint(hwnd, &ps); + { + render(); + /**/ + //HGDIOBJ brush = GetStockObject(BLACK_BRUSH); + //PAINTSTRUCT ps; + //HDC hdc = BeginPaint(hwnd, &ps); - FillRect(hdc, &ps.rcPaint, (HBRUSH) brush ); - EndPaint(hwnd, &ps); + //FillRect(hdc, &ps.rcPaint, (HBRUSH) brush ); + //EndPaint(hwnd, &ps); return 0; } case WM_DESTROY: @@ -292,7 +307,12 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n VkDevice device; result = vkCreateDevice(gpus[0], &deviceInfo, NULL, &device); //U_ASSERT_ONLY assert(result == VK_SUCCESS); - + //Retrieving handles to created queues + VkQueue graphicsQueue; + //VkQueue presentQueue; + vkGetDeviceQueue(device, graphicsQueueFamilyIndex, 0, &graphicsQueue); + vkGetDeviceQueue(device, presentQueueFamilyIndex, 0, &presentQueue); + //Creating command buffer pool & command buffer VkCommandPool cmdPool; VkCommandPoolCreateInfo cmdPoolInfo = {}; @@ -1089,9 +1109,9 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n /* We cannot bind the vertex buffer until we begin a renderpass */ VkClearValue clear_values[2]; - clear_values[0].color.float32[0] = 0.2f; - clear_values[0].color.float32[1] = 0.2f; - clear_values[0].color.float32[2] = 0.2f; + clear_values[0].color.float32[0] = 0.7f; + clear_values[0].color.float32[1] = 0.7f; + clear_values[0].color.float32[2] = 0.3f; clear_values[0].color.float32[3] = 0.2f; clear_values[1].depthStencil.depth = 1.0f; clear_values[1].depthStencil.stencil = 0; @@ -1271,20 +1291,83 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n vkCmdBeginRenderPass(cmd, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE); - vkCmdBindVertexBuffers(cmd, 0, /* Start Binding */ + vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline); + vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, NUM_DESCRIPTOR_SETS, + descriptorSets.data(), 0, NULL); + vkCmdBindVertexBuffers(cmd, 0, /* Start Binding w/ offset */ 1, /* Binding Count */ &vertexData.buf, /* pBuffers */ offsets); /* pOffsets */ - vkCmdEndRenderPass(cmd); - //U_ASSERT_ONLY result - result = vkEndCommandBuffer(cmd); + //Viewport & scissor setup + VkViewport viewport; + viewport.height = (float)swapchainExtent.height; + viewport.width = (float)swapchainExtent.height; + viewport.minDepth = (float)0.0f; + viewport.maxDepth = (float)1.0f; + viewport.x = 0; + viewport.y = 0; + vkCmdSetViewport(cmd, 0, NUM_VIEWPORTS, &viewport); + + VkRect2D scissor; + scissor.extent.width = swapchainExtent.width; + scissor.extent.height = swapchainExtent.height; + scissor.offset.x = 0; + scissor.offset.y = 0; + vkCmdSetScissor(cmd, 0, NUM_SCISSORS, &scissor); + + vkCmdDraw(cmd, 12 * 3, 1, 0, 0); + vkCmdEndRenderPass(cmd); + + result = vkEndCommandBuffer(cmd); //todo: bool U_ASSERT_ONLY result assert(result == VK_SUCCESS); - - + + //Fence + VkFenceCreateInfo fenceInfo; + VkFence drawFence; + fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; + fenceInfo.pNext = NULL; + fenceInfo.flags = 0; + vkCreateFence(device, &fenceInfo, NULL, &drawFence); + + VkPipelineStageFlags pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; + VkSubmitInfo submit_info[1] = {}; + submit_info[0].pNext = NULL; + submit_info[0].sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; + submit_info[0].waitSemaphoreCount = 1; + submit_info[0].pWaitSemaphores = &imageAcquiredSemaphore; + submit_info[0].pWaitDstStageMask = &pipe_stage_flags; + submit_info[0].commandBufferCount = 1; + submit_info[0].pCommandBuffers = &cmd; + submit_info[0].signalSemaphoreCount = 0; + submit_info[0].pSignalSemaphores = NULL; + + /* Queue the command buffer for execution */ + result = vkQueueSubmit(graphicsQueue, 1, submit_info, drawFence); + assert(result == VK_SUCCESS); + + /* Now present the image in the window */ + + //VkPresentInfoKHR presentInfo; + presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; + presentInfo.pNext = NULL; + presentInfo.swapchainCount = 1; + presentInfo.pSwapchains = &swapchain; + presentInfo.pImageIndices = ¤tBuffer; + presentInfo.pWaitSemaphores = NULL; + presentInfo.waitSemaphoreCount = 0; + presentInfo.pResults = NULL; + + /* Make sure command buffer is finished before presenting */ + do { + result = vkWaitForFences(device, 1, &drawFence, VK_TRUE, FENCE_TIMEOUT); + } while (result == VK_TIMEOUT); + + assert(result == VK_SUCCESS); + + //Window show and event loop - ShowWindow(hwnd, nShowCmd); MSG msg = { }; @@ -1293,6 +1376,7 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n DispatchMessage(&msg); } + vkDestroyFence(device, drawFence, NULL); vkDestroyPipeline(device, pipeline, NULL); vkDestroyBuffer(device, vertexData.buf, NULL); vkFreeMemory(device, vertexData.mem, NULL); @@ -1316,3 +1400,21 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n vkDestroyInstance(inst, NULL); return 0; } + +void render() { + VkResult U_ASSERT_ONLY result; + if(!rendered) { + result = vkQueuePresentKHR(presentQueue, &presentInfo); + assert(result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR); + #ifdef WIN32 + Sleep(1 * 1000); + #elif defined(__ANDROID__) + sleep(1); + #else + sleep(1); + #endif + rendered = true; +} +} + + diff --git a/src/shaderv.vert b/src/shaderv.vert index 96062f3..df76a76 100644 --- a/src/shaderv.vert +++ b/src/shaderv.vert @@ -1,19 +1,33 @@ -#version 400 +#version 400 #extension GL_ARB_separate_shader_objects : enable #extension GL_ARB_shading_language_420pack : enable - -//set = desc set layout -//binding = desc set binding -//var[I] = descriptor set pos in array -layout (std140, set = 0, binding = 0) uniform buf { - mat4 mvp; -} ubuf; - -layout (location = 0) in vec4 pos; -layout (location = 1) in vec2 inTexCoords; -layout (location = 0) out vec2 texcoord; - +layout (std140, binding = 0) uniform bufferVals { + mat4 mvp; +} myBufferVals; +layout (location = 0) in vec4 pos; +layout (location = 1) in vec4 inColor; +layout (location = 0) out vec4 outColor; void main() { - texcoord = inTexCoords; - gl_Position = ubuf.mvp * pos; + outColor = inColor; + gl_Position = myBufferVals.mvp * pos; } + +// #version 400 +// #extension GL_ARB_separate_shader_objects : enable +// #extension GL_ARB_shading_language_420pack : enable + +// //set = desc set layout +// //binding = desc set binding +// //var[I] = descriptor set pos in array +// layout (std140, set = 0, binding = 0) uniform buf { +// mat4 mvp; +// } ubuf; + +// layout (location = 0) in vec4 pos; +// layout (location = 1) in vec2 inTexCoords; +// layout (location = 0) out vec2 texcoord; + +// void main() { +// texcoord = inTexCoords; +// gl_Position = ubuf.mvp * pos; +// } diff --git a/src/triangle.h b/src/triangle.h index 05e36d4..804246e 100644 --- a/src/triangle.h +++ b/src/triangle.h @@ -36,47 +36,47 @@ static const Vertex g_vbData[] = { static const Vertex g_vb_solid_face_colors_Data[] = { // red face - {XYZ1(-1, -1, 1), XYZ1(1.f, 0.f, 0.f)}, - {XYZ1(-1, 1, 1), XYZ1(1.f, 0.f, 0.f)}, - {XYZ1(1, -1, 1), XYZ1(1.f, 0.f, 0.f)}, - {XYZ1(1, -1, 1), XYZ1(1.f, 0.f, 0.f)}, - {XYZ1(-1, 1, 1), XYZ1(1.f, 0.f, 0.f)}, - {XYZ1(1, 1, 1), XYZ1(1.f, 0.f, 0.f)}, - // green face - {XYZ1(-1, -1, -1), XYZ1(0.f, 1.f, 0.f)}, - {XYZ1(1, -1, -1), XYZ1(0.f, 1.f, 0.f)}, - {XYZ1(-1, 1, -1), XYZ1(0.f, 1.f, 0.f)}, - {XYZ1(-1, 1, -1), XYZ1(0.f, 1.f, 0.f)}, - {XYZ1(1, -1, -1), XYZ1(0.f, 1.f, 0.f)}, - {XYZ1(1, 1, -1), XYZ1(0.f, 1.f, 0.f)}, + //{XYZ1(-1, -1, 1), XYZ1(1.f, 0.f, 0.f)}, + //{XYZ1(-1, 1, 1), XYZ1(1.f, 0.f, 0.f)}, + //{XYZ1(1, -1, 1), XYZ1(1.f, 0.f, 0.f)}, + //{XYZ1(1, -1, 1), XYZ1(1.f, 0.f, 0.f)}, + //{XYZ1(-1, 1, 1), XYZ1(1.f, 0.f, 0.f)}, + //{XYZ1(1, 1, 1), XYZ1(1.f, 0.f, 0.f)}, + //// green face + {XYZ1(-1, -1, -1), XYZ1(1.f, 0.5f, 1.f)}, + {XYZ1(1, -1, -1), XYZ1(1.f, 0.5f, 1.f)}, + {XYZ1(-1, 1, -1), XYZ1(1.f, 0.5f, 1.f)}, + //{XYZ1(-1, 1, -1), XYZ1(0.f, 1.f, 0.f)}, + //{XYZ1(1, -1, -1), XYZ1(0.f, 1.f, 0.f)}, + //{XYZ1(1, 1, -1), XYZ1(0.f, 1.f, 0.f)}, // blue face - {XYZ1(-1, 1, 1), XYZ1(0.f, 0.f, 1.f)}, - {XYZ1(-1, -1, 1), XYZ1(0.f, 0.f, 1.f)}, - {XYZ1(-1, 1, -1), XYZ1(0.f, 0.f, 1.f)}, - {XYZ1(-1, 1, -1), XYZ1(0.f, 0.f, 1.f)}, - {XYZ1(-1, -1, 1), XYZ1(0.f, 0.f, 1.f)}, - {XYZ1(-1, -1, -1), XYZ1(0.f, 0.f, 1.f)}, - // yellow face - {XYZ1(1, 1, 1), XYZ1(1.f, 1.f, 0.f)}, - {XYZ1(1, 1, -1), XYZ1(1.f, 1.f, 0.f)}, - {XYZ1(1, -1, 1), XYZ1(1.f, 1.f, 0.f)}, - {XYZ1(1, -1, 1), XYZ1(1.f, 1.f, 0.f)}, - {XYZ1(1, 1, -1), XYZ1(1.f, 1.f, 0.f)}, - {XYZ1(1, -1, -1), XYZ1(1.f, 1.f, 0.f)}, - // magenta face - {XYZ1(1, 1, 1), XYZ1(1.f, 0.f, 1.f)}, - {XYZ1(-1, 1, 1), XYZ1(1.f, 0.f, 1.f)}, - {XYZ1(1, 1, -1), XYZ1(1.f, 0.f, 1.f)}, - {XYZ1(1, 1, -1), XYZ1(1.f, 0.f, 1.f)}, - {XYZ1(-1, 1, 1), XYZ1(1.f, 0.f, 1.f)}, - {XYZ1(-1, 1, -1), XYZ1(1.f, 0.f, 1.f)}, - // cyan face - {XYZ1(1, -1, 1), XYZ1(0.f, 1.f, 1.f)}, - {XYZ1(1, -1, -1), XYZ1(0.f, 1.f, 1.f)}, - {XYZ1(-1, -1, 1), XYZ1(0.f, 1.f, 1.f)}, - {XYZ1(-1, -1, 1), XYZ1(0.f, 1.f, 1.f)}, - {XYZ1(1, -1, -1), XYZ1(0.f, 1.f, 1.f)}, - {XYZ1(-1, -1, -1), XYZ1(0.f, 1.f, 1.f)}, + //{XYZ1(-1, 1, 1), XYZ1(0.f, 0.f, 1.f)}, + //{XYZ1(-1, -1, 1), XYZ1(0.f, 0.f, 1.f)}, + //{XYZ1(-1, 1, -1), XYZ1(0.f, 0.f, 1.f)}, + //{XYZ1(-1, 1, -1), XYZ1(0.f, 0.f, 1.f)}, + //{XYZ1(-1, -1, 1), XYZ1(0.f, 0.f, 1.f)}, + //{XYZ1(-1, -1, -1), XYZ1(0.f, 0.f, 1.f)}, + //// yellow face + //{XYZ1(1, 1, 1), XYZ1(1.f, 1.f, 0.f)}, + //{XYZ1(1, 1, -1), XYZ1(1.f, 1.f, 0.f)}, + //{XYZ1(1, -1, 1), XYZ1(1.f, 1.f, 0.f)}, + //{XYZ1(1, -1, 1), XYZ1(1.f, 1.f, 0.f)}, + //{XYZ1(1, 1, -1), XYZ1(1.f, 1.f, 0.f)}, + //{XYZ1(1, -1, -1), XYZ1(1.f, 1.f, 0.f)}, + //// magenta face + //{XYZ1(1, 1, 1), XYZ1(1.f, 0.f, 1.f)}, + //{XYZ1(-1, 1, 1), XYZ1(1.f, 0.f, 1.f)}, + //{XYZ1(1, 1, -1), XYZ1(1.f, 0.f, 1.f)}, + //{XYZ1(1, 1, -1), XYZ1(1.f, 0.f, 1.f)}, + //{XYZ1(-1, 1, 1), XYZ1(1.f, 0.f, 1.f)}, + //{XYZ1(-1, 1, -1), XYZ1(1.f, 0.f, 1.f)}, + //// cyan face + //{XYZ1(1, -1, 1), XYZ1(0.f, 1.f, 1.f)}, + //{XYZ1(1, -1, -1), XYZ1(0.f, 1.f, 1.f)}, + //{XYZ1(-1, -1, 1), XYZ1(0.f, 1.f, 1.f)}, + //{XYZ1(-1, -1, 1), XYZ1(0.f, 1.f, 1.f)}, + //{XYZ1(1, -1, -1), XYZ1(0.f, 1.f, 1.f)}, + //{XYZ1(-1, -1, -1), XYZ1(0.f, 1.f, 1.f)}, }; static const VertexUV g_vb_texture_Data[] = {