This commit is contained in:
Hane 2024-11-12 21:36:03 +01:00
commit e80932e261
3 changed files with 188 additions and 72 deletions

View file

@ -23,6 +23,15 @@
#define NUM_VIEWPORTS 1 #define NUM_VIEWPORTS 1
#define NUM_SCISSORS NUM_VIEWPORTS #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 <excpt.h> #include <excpt.h>
//#include <eh.h> //#include <eh.h>
#include <cstdint> #include <cstdint>
@ -57,6 +66,10 @@ typedef struct StateInfo {
* return state; * return state;
* } * }
*/ */
VkQueue presentQueue;
VkPresentInfoKHR presentInfo;
bool rendered = false;
void render();
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
stif *stateInfo; stif *stateInfo;
@ -69,13 +82,15 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
stateInfo = (stif*)ptr; stateInfo = (stif*)ptr;
switch (uMsg) { switch (uMsg) {
case WM_PAINT: case WM_PAINT:
{ /**/ {
HGDIOBJ brush = GetStockObject(BLACK_BRUSH); render();
PAINTSTRUCT ps; /**/
HDC hdc = BeginPaint(hwnd, &ps); //HGDIOBJ brush = GetStockObject(BLACK_BRUSH);
//PAINTSTRUCT ps;
//HDC hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH) brush ); //FillRect(hdc, &ps.rcPaint, (HBRUSH) brush );
EndPaint(hwnd, &ps); //EndPaint(hwnd, &ps);
return 0; return 0;
} }
case WM_DESTROY: case WM_DESTROY:
@ -292,6 +307,11 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n
VkDevice device; VkDevice device;
result = vkCreateDevice(gpus[0], &deviceInfo, NULL, &device); //U_ASSERT_ONLY result = vkCreateDevice(gpus[0], &deviceInfo, NULL, &device); //U_ASSERT_ONLY
assert(result == VK_SUCCESS); 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 //Creating command buffer pool & command buffer
VkCommandPool cmdPool; VkCommandPool cmdPool;
@ -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 */ /* We cannot bind the vertex buffer until we begin a renderpass */
VkClearValue clear_values[2]; VkClearValue clear_values[2];
clear_values[0].color.float32[0] = 0.2f; clear_values[0].color.float32[0] = 0.7f;
clear_values[0].color.float32[1] = 0.2f; clear_values[0].color.float32[1] = 0.7f;
clear_values[0].color.float32[2] = 0.2f; clear_values[0].color.float32[2] = 0.3f;
clear_values[0].color.float32[3] = 0.2f; clear_values[0].color.float32[3] = 0.2f;
clear_values[1].depthStencil.depth = 1.0f; clear_values[1].depthStencil.depth = 1.0f;
clear_values[1].depthStencil.stencil = 0; 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); 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 */ 1, /* Binding Count */
&vertexData.buf, /* pBuffers */ &vertexData.buf, /* pBuffers */
offsets); /* pOffsets */ offsets); /* pOffsets */
//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); vkCmdEndRenderPass(cmd);
//U_ASSERT_ONLY result
result = vkEndCommandBuffer(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 = &currentBuffer;
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); assert(result == VK_SUCCESS);
//Window show and event loop //Window show and event loop
ShowWindow(hwnd, nShowCmd); ShowWindow(hwnd, nShowCmd);
MSG msg = { }; MSG msg = { };
@ -1293,6 +1376,7 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n
DispatchMessage(&msg); DispatchMessage(&msg);
} }
vkDestroyFence(device, drawFence, NULL);
vkDestroyPipeline(device, pipeline, NULL); vkDestroyPipeline(device, pipeline, NULL);
vkDestroyBuffer(device, vertexData.buf, NULL); vkDestroyBuffer(device, vertexData.buf, NULL);
vkFreeMemory(device, vertexData.mem, NULL); vkFreeMemory(device, vertexData.mem, NULL);
@ -1316,3 +1400,21 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n
vkDestroyInstance(inst, NULL); vkDestroyInstance(inst, NULL);
return 0; 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;
}
}

View file

@ -1,19 +1,33 @@
#version 400 #version 400
#extension GL_ARB_separate_shader_objects : enable #extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable #extension GL_ARB_shading_language_420pack : enable
layout (std140, binding = 0) uniform bufferVals {
//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; mat4 mvp;
} ubuf; } myBufferVals;
layout (location = 0) in vec4 pos; layout (location = 0) in vec4 pos;
layout (location = 1) in vec2 inTexCoords; layout (location = 1) in vec4 inColor;
layout (location = 0) out vec2 texcoord; layout (location = 0) out vec4 outColor;
void main() { void main() {
texcoord = inTexCoords; outColor = inColor;
gl_Position = ubuf.mvp * pos; 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;
// }

View file

@ -36,47 +36,47 @@ static const Vertex g_vbData[] = {
static const Vertex g_vb_solid_face_colors_Data[] = { static const Vertex g_vb_solid_face_colors_Data[] = {
// red face // 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)},
{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 //// green face
{XYZ1(-1, -1, -1), XYZ1(0.f, 1.f, 0.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(1.f, 0.5f, 1.f)},
{XYZ1(-1, 1, -1), XYZ1(0.f, 1.f, 0.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)}, //{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 // 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)},
{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 //// 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)},
{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 //// 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)},
{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 //// 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, 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[] = { static const VertexUV g_vb_texture_Data[] = {