From 9ffc7dd9ca6fedef5516661764fb8833030ba909 Mon Sep 17 00:00:00 2001 From: Hane Date: Tue, 12 Nov 2024 19:40:23 +0100 Subject: [PATCH] lunarg 14 --- src/main.cpp | 188 +++++++++++++++++++++++++++++++++++++++++++---- src/shaderf.frag | 19 +++-- 2 files changed, 188 insertions(+), 19 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 9c3e90d..9cacfc8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -579,12 +579,22 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n /* Note that when we start using textures, this is where our sampler will * need to be specified */ - VkDescriptorSetLayoutBinding descriptorSetLayoutBinding = {}; - descriptorSetLayoutBinding.binding = 0; - descriptorSetLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - descriptorSetLayoutBinding.descriptorCount = 1; - descriptorSetLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; - descriptorSetLayoutBinding.pImmutableSamplers = NULL; + + VkDescriptorSetLayoutBinding descriptorSetLayoutBindings[1]; + //Uniform buffer layout binding + descriptorSetLayoutBindings[0].binding = 0; + descriptorSetLayoutBindings[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + descriptorSetLayoutBindings[0].descriptorCount = 1; + descriptorSetLayoutBindings[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT; + descriptorSetLayoutBindings[0].pImmutableSamplers = NULL; + //Texture sampler layout binding + /* + * descriptorSetLayoutBindings[1].binding = 1; + * descriptorSetLayoutBindings[1].descriptorCount = 1; + * descriptorSetLayoutBindings[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; + * descriptorSetLayoutBindings[1].pImmutableSamplers = nullptr; + * descriptorSetLayoutBindings[1].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; + */ /* Next take layout bindings and use them to create a descriptor set layout */ @@ -593,7 +603,7 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n descriptorSetInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; descriptorSetInfo.pNext = NULL; descriptorSetInfo.bindingCount = 1; - descriptorSetInfo.pBindings = &descriptorSetLayoutBinding; + descriptorSetInfo.pBindings = descriptorSetLayoutBindings; descriptorSetLayouts.resize(NUM_DESCRIPTOR_SETS); result = vkCreateDescriptorSetLayout(device, &descriptorSetInfo, NULL, descriptorSetLayouts.data()); @@ -619,13 +629,17 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n VkDescriptorPoolSize typeCount[1]; typeCount[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; typeCount[0].descriptorCount = 1; + /* + * typeCount[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; + * typeCount[1].descriptorCount = 1; + */ VkDescriptorPool descriptorPool; VkDescriptorPoolCreateInfo descriptorPoolInfo = {}; descriptorPoolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; descriptorPoolInfo.pNext = NULL; descriptorPoolInfo.maxSets = 1; - descriptorPoolInfo.poolSizeCount = 1; + descriptorPoolInfo.poolSizeCount = 1; //descriptorPoolSize size descriptorPoolInfo.pPoolSizes = typeCount; result = vkCreateDescriptorPool(device, &descriptorPoolInfo, NULL, &descriptorPool); @@ -643,7 +657,7 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n result = vkAllocateDescriptorSets(device, descriptorSetAllocInfo, descriptorSets.data()); assert(result == VK_SUCCESS); - VkWriteDescriptorSet writes[1]; + VkWriteDescriptorSet writes[2]; writes[0] = {}; writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; writes[0].pNext = NULL; @@ -653,6 +667,16 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n writes[0].pBufferInfo = &uniformData.descriptorBufferInfo; writes[0].dstArrayElement = 0; writes[0].dstBinding = 0; + /* + * writes[1] = {}; + * writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + * writes[1].dstSet = descriptorSets[0]; + * writes[1].dstBinding = 1; + * writes[1].descriptorCount = 1; + * writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; + * writes[1].pImageInfo = &info.texture_data.image_info; + * writes[1].dstArrayElement = 0; + */ vkUpdateDescriptorSets(device, 1, writes, 0, NULL); @@ -1064,13 +1088,13 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n const VkDeviceSize offsets[1] = {0}; /* We cannot bind the vertex buffer until we begin a renderpass */ - VkClearValue clear_values[1]; + 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[3] = 0.2f; - //clear_values[1].depthStencil.depth = 1.0f; - //clear_values[1].depthStencil.stencil = 0; + clear_values[1].depthStencil.depth = 1.0f; + clear_values[1].depthStencil.stencil = 0; /* I think repe and will repe later? * VkSemaphore imageAcquiredSemaphore; @@ -1090,6 +1114,140 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n * assert(res == VK_SUCCESS); */ + /* Pipeline setup */ + + VkDynamicState dynamicStateEnables[2]; // Viewport + Scissor + VkPipelineDynamicStateCreateInfo dynamicStateInfo = {}; + memset(dynamicStateEnables, 0, sizeof dynamicStateEnables); + dynamicStateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; + dynamicStateInfo.pNext = NULL; + dynamicStateInfo.pDynamicStates = dynamicStateEnables; + dynamicStateInfo.dynamicStateCount = 0; + + VkPipelineVertexInputStateCreateInfo pipelineVertexInputStateInfo; + pipelineVertexInputStateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; + pipelineVertexInputStateInfo.pNext = NULL; + pipelineVertexInputStateInfo.flags = 0; + pipelineVertexInputStateInfo.vertexBindingDescriptionCount = 1; + pipelineVertexInputStateInfo.pVertexBindingDescriptions = &vertexInputBindingDescription; + pipelineVertexInputStateInfo.vertexAttributeDescriptionCount = 2; + pipelineVertexInputStateInfo.pVertexAttributeDescriptions = vertexInputBindingAttributes; + + VkPipelineInputAssemblyStateCreateInfo pipelineInputAssemblyStateInfo; + pipelineInputAssemblyStateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; + pipelineInputAssemblyStateInfo.pNext = NULL; + pipelineInputAssemblyStateInfo.flags = 0; + pipelineInputAssemblyStateInfo.primitiveRestartEnable = VK_FALSE; + pipelineInputAssemblyStateInfo.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; + + VkPipelineRasterizationStateCreateInfo pipelineRasterizationStateInfo; + pipelineRasterizationStateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; + pipelineRasterizationStateInfo.pNext = NULL; + pipelineRasterizationStateInfo.flags = 0; + pipelineRasterizationStateInfo.polygonMode = VK_POLYGON_MODE_FILL; + pipelineRasterizationStateInfo.cullMode = VK_CULL_MODE_BACK_BIT; + pipelineRasterizationStateInfo.frontFace = VK_FRONT_FACE_CLOCKWISE; + pipelineRasterizationStateInfo.depthClampEnable = VK_FALSE; + pipelineRasterizationStateInfo.rasterizerDiscardEnable = VK_FALSE; + pipelineRasterizationStateInfo.depthBiasEnable = VK_FALSE; + pipelineRasterizationStateInfo.depthBiasConstantFactor = 0; + pipelineRasterizationStateInfo.depthBiasClamp = 0; + pipelineRasterizationStateInfo.depthBiasSlopeFactor = 0; + pipelineRasterizationStateInfo.lineWidth = 1.0f; + + VkPipelineColorBlendStateCreateInfo pipelineColorBlendStateInfo; + pipelineColorBlendStateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; + pipelineColorBlendStateInfo.pNext = NULL; + pipelineColorBlendStateInfo.flags = 0; + VkPipelineColorBlendAttachmentState pipelineColorBlendAttachmentsState[1]; + pipelineColorBlendAttachmentsState[0].colorWriteMask = 0xf; + pipelineColorBlendAttachmentsState[0].blendEnable = VK_FALSE; + pipelineColorBlendAttachmentsState[0].alphaBlendOp = VK_BLEND_OP_ADD; + pipelineColorBlendAttachmentsState[0].colorBlendOp = VK_BLEND_OP_ADD; + pipelineColorBlendAttachmentsState[0].srcColorBlendFactor = VK_BLEND_FACTOR_ZERO; + pipelineColorBlendAttachmentsState[0].dstColorBlendFactor = VK_BLEND_FACTOR_ZERO; + pipelineColorBlendAttachmentsState[0].srcAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; + pipelineColorBlendAttachmentsState[0].dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; + pipelineColorBlendStateInfo.attachmentCount = 1; + pipelineColorBlendStateInfo.pAttachments = pipelineColorBlendAttachmentsState; + pipelineColorBlendStateInfo.logicOpEnable = VK_FALSE; + pipelineColorBlendStateInfo.logicOp = VK_LOGIC_OP_NO_OP; + pipelineColorBlendStateInfo.blendConstants[0] = 1.0f; + pipelineColorBlendStateInfo.blendConstants[1] = 1.0f; + pipelineColorBlendStateInfo.blendConstants[2] = 1.0f; + pipelineColorBlendStateInfo.blendConstants[3] = 1.0f; + + VkPipelineViewportStateCreateInfo pipelineViewportStateInfo = {}; + pipelineViewportStateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; + pipelineViewportStateInfo.pNext = NULL; + pipelineViewportStateInfo.flags = 0; + pipelineViewportStateInfo.viewportCount = NUM_VIEWPORTS; + dynamicStateEnables[dynamicStateInfo.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT; + pipelineViewportStateInfo.scissorCount = NUM_SCISSORS; + dynamicStateEnables[dynamicStateInfo.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR; + pipelineViewportStateInfo.pScissors = NULL; + pipelineViewportStateInfo.pViewports = NULL; + + + VkPipelineDepthStencilStateCreateInfo pipelineDepthStencilStateInfo; + pipelineDepthStencilStateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; + pipelineDepthStencilStateInfo.pNext = NULL; + pipelineDepthStencilStateInfo.flags = 0; + pipelineDepthStencilStateInfo.depthTestEnable = VK_FALSE;//VK_TRUE; + pipelineDepthStencilStateInfo.depthWriteEnable = VK_FALSE;//VK_TRUE; + pipelineDepthStencilStateInfo.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL; + pipelineDepthStencilStateInfo.depthBoundsTestEnable = VK_FALSE; + pipelineDepthStencilStateInfo.minDepthBounds = 0; + pipelineDepthStencilStateInfo.maxDepthBounds = 0; + pipelineDepthStencilStateInfo.stencilTestEnable = VK_FALSE; + pipelineDepthStencilStateInfo.back.failOp = VK_STENCIL_OP_KEEP; + pipelineDepthStencilStateInfo.back.passOp = VK_STENCIL_OP_KEEP; + pipelineDepthStencilStateInfo.back.compareOp = VK_COMPARE_OP_ALWAYS; + pipelineDepthStencilStateInfo.back.compareMask = 0; + pipelineDepthStencilStateInfo.back.reference = 0; + pipelineDepthStencilStateInfo.back.depthFailOp = VK_STENCIL_OP_KEEP; + pipelineDepthStencilStateInfo.back.writeMask = 0; + pipelineDepthStencilStateInfo.front = pipelineDepthStencilStateInfo.back; + + + VkPipelineMultisampleStateCreateInfo pipelineMultisampleStateInfo; + pipelineMultisampleStateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; + pipelineMultisampleStateInfo.pNext = NULL; + pipelineMultisampleStateInfo.flags = 0; + pipelineMultisampleStateInfo.pSampleMask = NULL; + pipelineMultisampleStateInfo.rasterizationSamples = NUM_SAMPLES; + pipelineMultisampleStateInfo.sampleShadingEnable = VK_FALSE; + pipelineMultisampleStateInfo.alphaToCoverageEnable = VK_FALSE; + pipelineMultisampleStateInfo.alphaToOneEnable = VK_FALSE; + pipelineMultisampleStateInfo.minSampleShading = 0.0; + + VkGraphicsPipelineCreateInfo pipelineInfo; + pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; + pipelineInfo.pNext = NULL; + pipelineInfo.layout = pipelineLayout; + pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; + pipelineInfo.basePipelineIndex = 0; + pipelineInfo.flags = 0; + pipelineInfo.pVertexInputState = &pipelineVertexInputStateInfo; + pipelineInfo.pInputAssemblyState = &pipelineInputAssemblyStateInfo; + pipelineInfo.pRasterizationState = &pipelineRasterizationStateInfo; + pipelineInfo.pColorBlendState = &pipelineColorBlendStateInfo; + pipelineInfo.pTessellationState = NULL; + pipelineInfo.pMultisampleState = &pipelineMultisampleStateInfo; + pipelineInfo.pDynamicState = &dynamicStateInfo; + pipelineInfo.pViewportState = &pipelineViewportStateInfo; + pipelineInfo.pDepthStencilState = &pipelineDepthStencilStateInfo; + pipelineInfo.pStages = shaderStages; + pipelineInfo.stageCount = 2; + pipelineInfo.renderPass = renderPass; + pipelineInfo.subpass = 0; + + VkPipeline pipeline; + result = vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, NULL, &pipeline); + assert(result == VK_SUCCESS); + + /* Command buffer and render pass. Rendering now?!?! */ + VkCommandBufferBeginInfo cmdBufferBeginInfo = {}; cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; cmdBufferBeginInfo.pNext = NULL; @@ -1108,7 +1266,7 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n renderPassBeginInfo.renderArea.offset.y = 0; renderPassBeginInfo.renderArea.extent.width = swapchainExtent.width; renderPassBeginInfo.renderArea.extent.height = swapchainExtent.height; - renderPassBeginInfo.clearValueCount = 1; + renderPassBeginInfo.clearValueCount = 2; renderPassBeginInfo.pClearValues = clear_values; vkCmdBeginRenderPass(cmd, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE); @@ -1135,7 +1293,9 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n DispatchMessage(&msg); } - + vkDestroyPipeline(device, pipeline, NULL); + vkDestroyBuffer(device, vertexData.buf, NULL); + vkFreeMemory(device, vertexData.mem, NULL); for (i = 0; i < swapchainImageCount; i++) { vkDestroyFramebuffer(device, framebuffers[i], NULL); } diff --git a/src/shaderf.frag b/src/shaderf.frag index 5bbc26b..78cdce8 100644 --- a/src/shaderf.frag +++ b/src/shaderf.frag @@ -1,11 +1,20 @@ #version 400 #extension GL_ARB_separate_shader_objects : enable #extension GL_ARB_shading_language_420pack : enable - -layout (binding = 1) uniform sampler2D tex; -layout (location = 0) in vec2 texcoord; +layout (location = 0) in vec4 color; layout (location = 0) out vec4 outColor; - void main() { - outColor = textureLod(tex, texcoord, 0.0); + outColor = color; } + +// #version 400 +// #extension GL_ARB_separate_shader_objects : enable +// #extension GL_ARB_shading_language_420pack : enable + +// /* layout (binding = 1) uniform sampler2D tex; */ +// layout (location = 0) in vec2 texcoord; +// layout (location = 0) out vec4 outColor; + +// void main() { +// outColor = textureLod(tex, texcoord, 0.0); +//} \ No newline at end of file