From a86bdc2c44b41cfefd99fe053337dda953900811 Mon Sep 17 00:00:00 2001 From: Sascha Willems Date: Thu, 28 Jan 2021 22:04:25 +0100 Subject: [PATCH] Finished descriptor indexing example --- .../descriptorindexing.frag.spv | Bin 856 -> 868 bytes .../descriptorindexing.vert | 3 - .../descriptorindexing.vert.spv | Bin 2104 -> 1864 bytes .../descriptorindexing/descriptorindexing.cpp | 367 ++++++++---------- 4 files changed, 169 insertions(+), 201 deletions(-) diff --git a/data/shaders/glsl/descriptorindexing/descriptorindexing.frag.spv b/data/shaders/glsl/descriptorindexing/descriptorindexing.frag.spv index 3f3e184ed3a430d0f3cb69374f792f079f5427ae..1e2499ad6e667c4740f11b47f30eaa71845fb2bd 100644 GIT binary patch delta 25 gcmcb?_Joa-nMs+Qfq{{MYa?efBctf%M#k%m07In(Z2$lO delta 23 ecmaFDc7u(RnMs+Qfq{{MeIsWx3FM3Z8INe#+Z)3h72rfi#btHe+I1^r3> zD!-WcK4*7o^_a=b`_ARe&YWT)Uz>6+=Vsird*aeF@A6`tJM99m8~X?Q^E^rgvVk6Uq6aPC8I%@(&7tlQMvuzPIQC2`cwB~GaMX-gG7aN zf{e$;*BeGj)QiPV`FOt7^HH1}MBhT<%*Qv$_o^H9Uq{2F6}Q8&k4fPhb-QC9{V@*? z-?c9Nwv`N`c1VlL4Lpb4AM`$lZQqL6G?rezMB!Kb#?ix3uM>8qsd)=LQL~ZFw=A9m zX1-wRHPjRPn)P9 zw=s&iS}Bk9#Ncz~sG@SS`5!^SFC54@};P#PZ-QkDloZj`=YE zH3`RXHHB_LQN&f1!*^pZ$qJ+^l4a#n7mTkWJS|Nu{L>8cdx670Bb}3&A8d19l>e-c zOJj&<&*Z{W56u3p9y6q#`N8~_;plHcn*P`q^)5(rH$46&Y5Z1iSvI-w+y~|j!EFc= zdr9Kl$qV|uDyst2!Ec!ORpF{MITcA+7PGdV@FDb7lcop!+zaNn2S;7L@4AGuB78%d zI(%0-duwne)VwLoUe+bl1ha43)$PnC$L_c*J2!zJWHHoxESr9~cUyN-hx<867bWcAovH`YmorXk%&le|y<6Os zjHAz{I-qa<7LZRoes;;7^onm+cQBU?3A1A6^udhoNZ>7|E}Ix;W-<3;#`lbxIXvVI zx`GcRX2=bAyQuH|R-6FNN%|#2l1q}yl53J#NkN}p>EB8&xuQO&d0=wW!|daZG?+SZ z;-PTK*OdQ5bhMf5MMq-Fevdd2l^u4XoEBg4`+jbJ+Dy7xE7>&xt%J-%nAH0q8 zUQT@6B)-FT`@~0o+!c#6bLsC|Sts6&Xi>Rk&tV^Q(l61jZ$)e#OD|vJ=o^0H=wUzI zi`vrEd0>#pv<7i=NB#*!OH6n7lQK z<-z@*$KGf2z~miEEDz4|=$RhixG&~0Enyk1<`fGOm2;g7H;_ z`=p75UkaGt799Tb(mfLMgYDi&<-g$L@)+WoJGt=G1M?o>WGtS@mv2_Ws?g}Juq(!j(&)}E^+R7mGj(`RRwC{2j7wq z&wjV1?G7h}XN0Li%#jPhFuz^63E@Z5l}ET09N9J}iU+4OGsWr(5Pnr!xG-v#YS9rkCz{}IDmWiRfT|3c2bEQ}tI@D^4S z&6@#Z4@om);+X4*gqfNTY`#(1%-4Kie9UW7!mQq_3+|Zz1vr**H)O+cM&qi1<2+4u z%K2&%@` textures; vks::Buffer vertexBuffer; @@ -45,25 +39,27 @@ public: VkPhysicalDeviceDescriptorIndexingFeaturesEXT physicalDeviceDescriptorIndexingFeatures{}; + struct Vertex { + float pos[3]; + float uv[2]; + int32_t textureIndex; + }; + VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { title = "Descriptor indexing"; settings.overlay = true; camera.type = Camera::CameraType::lookat; - camera.setPosition(glm::vec3(0.0f, 0.0f, -7.5f)); + camera.setPosition(glm::vec3(0.0f, 0.0f, -10.0f)); camera.setRotation(glm::vec3(-35.0f, 0.0f, 0.0f)); camera.setPerspective(45.0f, (float)width / (float)height, 0.1f, 256.0f); - /* - [POI] Enable required extensions - */ + // [POI] Enable required extensions enabledInstanceExtensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME); enabledDeviceExtensions.push_back(VK_KHR_MAINTENANCE3_EXTENSION_NAME); enabledDeviceExtensions.push_back(VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME); - /* - [POI] Enable required extension features - */ + // [POI] Enable required extension features physicalDeviceDescriptorIndexingFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_FEATURES_EXT; physicalDeviceDescriptorIndexingFeatures.shaderSampledImageArrayNonUniformIndexing = VK_TRUE; physicalDeviceDescriptorIndexingFeatures.runtimeDescriptorArray = VK_TRUE; @@ -74,10 +70,9 @@ public: ~VulkanExample() { - //vkDestroyImageView(device, textureArray.view, nullptr); - //vkDestroyImage(device, textureArray.image, nullptr); - //vkDestroySampler(device, textureArray.sampler, nullptr); - //vkFreeMemory(device, textureArray.deviceMemory, nullptr); + for (auto &texture : textures) { + texture.destroy(); + } vkDestroyPipeline(device, pipeline, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); @@ -86,136 +81,103 @@ public: uniformBufferVS.destroy(); } - void loadAssets() + struct V { + uint8_t r; + uint8_t g; + uint8_t b; + uint8_t a; + }; + + // Generate some random textures + void generateTextures() { - textures.resize(7); - for (uint32_t i = 0; i < 7; i++) { - textures[i].loadFromFile(getAssetPath() + "textures/array/" + std::to_string(i) + ".ktx", VK_FORMAT_R8G8B8A8_UNORM, vulkanDevice, queue); + textures.resize(32); + for (size_t i = 0; i < textures.size(); i++) { + std::random_device rndDevice; + std::default_random_engine rndEngine(rndDevice()); + std::uniform_int_distribution rndDist(50, 255); + const int32_t dim = 3; + const size_t bufferSize = dim * dim * 4; + std::vector texture(bufferSize); + for (size_t i = 0; i < dim * dim; i++) { + texture[i * 4] = rndDist(rndEngine); + texture[i * 4 + 1] = rndDist(rndEngine); + texture[i * 4 + 2] = rndDist(rndEngine); + texture[i * 4 + 3] = 255; + } + textures[i].fromBuffer(texture.data(), bufferSize, VK_FORMAT_R8G8B8A8_UNORM, dim, dim, vulkanDevice, queue, VK_FILTER_NEAREST); } } - void buildCommandBuffers() - { - VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo(); - - VkClearValue clearValues[2]; - clearValues[0].color = defaultClearColor; - clearValues[1].depthStencil = { 1.0f, 0 }; - - VkRenderPassBeginInfo renderPassBeginInfo = vks::initializers::renderPassBeginInfo(); - renderPassBeginInfo.renderPass = renderPass; - renderPassBeginInfo.renderArea.offset.x = 0; - renderPassBeginInfo.renderArea.offset.y = 0; - renderPassBeginInfo.renderArea.extent.width = width; - renderPassBeginInfo.renderArea.extent.height = height; - renderPassBeginInfo.clearValueCount = 2; - renderPassBeginInfo.pClearValues = clearValues; - - for (int32_t i = 0; i < drawCmdBuffers.size(); ++i) - { - renderPassBeginInfo.framebuffer = frameBuffers[i]; - - VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo)); - - vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE); - - VkViewport viewport = vks::initializers::viewport((float)width, (float)height, 0.0f, 1.0f); - vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport); - - VkRect2D scissor = vks::initializers::rect2D(width, height, 0, 0); - vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor); - - vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL); - vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline); - - VkDeviceSize offsets[1] = { 0 }; - vkCmdBindVertexBuffers(drawCmdBuffers[i], 0, 1, &vertexBuffer.buffer, offsets); - vkCmdBindIndexBuffer(drawCmdBuffers[i], indexBuffer.buffer, 0, VK_INDEX_TYPE_UINT32); - - vkCmdDrawIndexed(drawCmdBuffers[i], indexCount, layerCount, 0, 0, 0); - - drawUI(drawCmdBuffers[i]); - - vkCmdEndRenderPass(drawCmdBuffers[i]); - - VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i])); - } - } - - void generateCube(glm::vec3 pos, float scale, std::vector &vertices, std::vector &indices) - { - // Generate random per-face texture indices - std::random_device mch; - std::default_random_engine generator(mch()); - std::uniform_int_distribution rndTextureIndex(0, layerCount - 1); - - std::vector faceTextureIndex(6); - for (auto &textureIndex : faceTextureIndex) { - textureIndex = rndTextureIndex(generator); - } - - std::cout << faceTextureIndex[0] << std::endl; - - std::vector cubeVertices = { - { { -scale, -scale, scale }, { 0.0f, 0.0f }, faceTextureIndex[0] }, - { { scale, -scale, scale }, { 1.0f, 0.0f }, faceTextureIndex[0] }, - { { scale, scale, scale }, { 1.0f, 1.0f }, faceTextureIndex[0] }, - { { -scale, scale, scale }, { 0.0f, 1.0f }, faceTextureIndex[0] }, - - { { scale, scale, scale }, { 0.0f, 0.0f }, faceTextureIndex[1] }, - { { scale, scale, -scale }, { 1.0f, 0.0f }, faceTextureIndex[1] }, - { { scale, -scale, -scale }, { 1.0f, 1.0f }, faceTextureIndex[1] }, - { { scale, -scale, scale }, { 0.0f, 1.0f }, faceTextureIndex[1] }, - - { { -scale, -scale, -scale }, { 0.0f, 0.0f }, faceTextureIndex[2] }, - { { scale, -scale, -scale }, { 1.0f, 0.0f }, faceTextureIndex[2] }, - { { scale, scale, -scale }, { 1.0f, 1.0f }, faceTextureIndex[2] }, - { { -scale, scale, -scale }, { 0.0f, 1.0f }, faceTextureIndex[2] }, - - { { -scale, -scale, -scale }, { 0.0f, 0.0f }, faceTextureIndex[3] }, - { { -scale, -scale, scale }, { 1.0f, 0.0f }, faceTextureIndex[3] }, - { { -scale, scale, scale }, { 1.0f, 1.0f }, faceTextureIndex[3] }, - { { -scale, scale, -scale }, { 0.0f, 1.0f }, faceTextureIndex[3] }, - - { { scale, scale, scale }, { 0.0f, 0.0f }, faceTextureIndex[4] }, - { { -scale, scale, scale }, { 1.0f, 0.0f }, faceTextureIndex[4] }, - { { -scale, scale, -scale }, { 1.0f, 1.0f }, faceTextureIndex[4] }, - { { scale, scale, -scale }, { 0.0f, 1.0f }, faceTextureIndex[4] }, - - { { -scale, -scale, -scale }, { 0.0f, 0.0f }, faceTextureIndex[5] }, - { { scale, -scale, -scale }, { 1.0f, 0.0f }, faceTextureIndex[5] }, - { { scale, -scale, scale }, { 1.0f, 1.0f }, faceTextureIndex[5] }, - { { -scale, -scale, scale }, { 0.0f, 1.0f }, faceTextureIndex[5] }, - - }; - for (auto &vertex : cubeVertices) { - vertex.pos[0] += pos.x; - vertex.pos[1] += pos.y; - vertex.pos[2] += pos.z; - } - - std::vector cubeIndices = { - 0,1,2, 0,2,3, 4,5,6, 4,6,7, 8,9,10, 8,10,11, 12,13,14, 12,14,15, 16,17,18, 16,18,19, 20,21,22, 20,22,23 - }; - for (auto &index : cubeIndices) { - index += vertices.size(); - } - - vertices.insert(vertices.end(), cubeVertices.begin(), cubeVertices.end()); - indices.insert(indices.end(), cubeIndices.begin(), cubeIndices.end()); - } - - void generateBuffers() + // Generates a line of cubes with randomized per-face texture indices + void generateCubes() { std::vector vertices; std::vector indices; - float offset = -1.5f; - float center = (layerCount * offset) / 2.0f - (offset * 0.5f); - //for (uint32_t i = 0; i < 7; i++) { - //generateCube(glm::vec3(i * offset - center, 0.0f, 0.0f), 0.5f, vertices, indices); - //} - generateCube(glm::vec3(0.0f, 0.0f, 0.0f), 0.5f, vertices, indices); + // Generate random per-face texture indices + std::random_device rndDevice; + std::default_random_engine rndEngine(rndDevice()); + std::uniform_int_distribution rndDist(0, static_cast(textures.size()) - 1); + + // Generate cubes with random per-face texture indices + const uint32_t count = 6; + for (uint32_t i = 0; i < count; i++) { + // Get random per-Face texture indices that the shader will sample from + int32_t textureIndices[6]; + for (uint32_t j = 0; j < 6; j++) { + textureIndices[j] = rndDist(rndEngine); + } + // Push vertices to buffer + float pos = 2.5f * i - (count * 2.5f / 2.0f); + const std::vector cube = { + { { -1.0f + pos, -1.0f, 1.0f }, { 0.0f, 0.0f }, textureIndices[0] }, + { { 1.0f + pos, -1.0f, 1.0f }, { 1.0f, 0.0f }, textureIndices[0] }, + { { 1.0f + pos, 1.0f, 1.0f }, { 1.0f, 1.0f }, textureIndices[0] }, + { { -1.0f + pos, 1.0f, 1.0f }, { 0.0f, 1.0f }, textureIndices[0] }, + + { { 1.0f + pos, 1.0f, 1.0f }, { 0.0f, 0.0f }, textureIndices[1] }, + { { 1.0f + pos, 1.0f, -1.0f }, { 1.0f, 0.0f }, textureIndices[1] }, + { { 1.0f + pos, -1.0f, -1.0f }, { 1.0f, 1.0f }, textureIndices[1] }, + { { 1.0f + pos, -1.0f, 1.0f }, { 0.0f, 1.0f }, textureIndices[1] }, + + { { -1.0f + pos, -1.0f, -1.0f }, { 0.0f, 0.0f }, textureIndices[2] }, + { { 1.0f + pos, -1.0f, -1.0f }, { 1.0f, 0.0f }, textureIndices[2] }, + { { 1.0f + pos, 1.0f, -1.0f }, { 1.0f, 1.0f }, textureIndices[2] }, + { { -1.0f + pos, 1.0f, -1.0f }, { 0.0f, 1.0f }, textureIndices[2] }, + + { { -1.0f + pos, -1.0f, -1.0f }, { 0.0f, 0.0f }, textureIndices[3] }, + { { -1.0f + pos, -1.0f, 1.0f }, { 1.0f, 0.0f }, textureIndices[3] }, + { { -1.0f + pos, 1.0f, 1.0f }, { 1.0f, 1.0f }, textureIndices[3] }, + { { -1.0f + pos, 1.0f, -1.0f }, { 0.0f, 1.0f }, textureIndices[3] }, + + { { 1.0f + pos, 1.0f, 1.0f }, { 0.0f, 0.0f }, textureIndices[4] }, + { { -1.0f + pos, 1.0f, 1.0f }, { 1.0f, 0.0f }, textureIndices[4] }, + { { -1.0f + pos, 1.0f, -1.0f }, { 1.0f, 1.0f }, textureIndices[4] }, + { { 1.0f + pos, 1.0f, -1.0f }, { 0.0f, 1.0f }, textureIndices[4] }, + + { { -1.0f + pos, -1.0f, -1.0f }, { 0.0f, 0.0f }, textureIndices[5] }, + { { 1.0f + pos, -1.0f, -1.0f }, { 1.0f, 0.0f }, textureIndices[5] }, + { { 1.0f + pos, -1.0f, 1.0f }, { 1.0f, 1.0f }, textureIndices[5] }, + { { -1.0f + pos, -1.0f, 1.0f }, { 0.0f, 1.0f }, textureIndices[5] }, + }; + for (auto& vertex : cube) { + vertices.push_back(vertex); + } + // Push indices to buffer + const std::vector cubeIndices = { + 0,1,2,0,2,3, + 4,5,6,4,6,7, + 8,9,10,8,10,11, + 12,13,14,12,14,15, + 16,17,18,16,18,19, + 20,21,22,20,22,23 + }; + for (auto& index : cubeIndices) { + indices.push_back(index + static_cast(vertices.size())); + } + } + indexCount = static_cast(indices.size()); // For the sake of simplicity we won't stage the vertex data to the gpu memory @@ -233,37 +195,30 @@ public: indices.data())); } - /* - [POI] Set up descriptor sets and set layout - */ + // [POI] Set up descriptor sets and set layout void setupDescriptorSets() { - /* - Descriptor set layout - */ + // Descriptor pool + std::vector poolSizes = { + vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1), + vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, static_cast(textures.size())) + }; + VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(poolSizes, 2); + VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool)); + // Descriptor set layout std::vector setLayoutBindings = { // Binding 0 : Vertex shader uniform buffer vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0), - /* - [POI] - - Binding 1 contains a texture array that is dynamically non-uniform sampled from - - FS: - outFragColor = texture(textures[nonuniformEXT(inTexIndex)], inUV); - */ + // [POI] Binding 1 contains a texture array that is dynamically non-uniform sampled from + // In the fragment shader: + // outFragColor = texture(textures[nonuniformEXT(inTexIndex)], inUV); vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 1, static_cast(textures.size())) }; - /* - [POI] - - The fragment shader will be using an unsized array, which has to be marked with a certain flag - - FS: - layout (set = 0, binding = 1) uniform sampler2D textures[]; - */ + // [POI] The fragment shader will be using an unsized array of samplers, which has to be marked with the VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT + // In the fragment shader: + // layout (set = 0, binding = 1) uniform sampler2D textures[]; VkDescriptorSetLayoutBindingFlagsCreateInfoEXT setLayoutBindingFlags{}; setLayoutBindingFlags.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT; setLayoutBindingFlags.bindingCount = 2; @@ -273,28 +228,14 @@ public: }; setLayoutBindingFlags.pBindingFlags = descriptorBindingFlags.data(); - VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCI = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings.data(), setLayoutBindings.size()); + VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCI = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings); descriptorSetLayoutCI.pNext = &setLayoutBindingFlags; VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorSetLayout)); - /* - Descriptor pool - */ - - std::vector poolSizes = { - vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1), - vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, textures.size()) - }; - VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(poolSizes.size(), poolSizes.data(), 2); - VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool)); - - /* - Descriptor sets - */ - + // Descriptor sets VkDescriptorSetVariableDescriptorCountAllocateInfoEXT variableDescriptorCountAllocInfo = {}; - uint32_t variableDescCounts[] = {textures.size()}; + uint32_t variableDescCounts[] = { static_cast(textures.size())}; variableDescriptorCountAllocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO_EXT; variableDescriptorCountAllocInfo.descriptorSetCount = 1; @@ -317,13 +258,8 @@ public: textureDescriptors[i].imageView = textures[i].view; } - /* - [POI] - - Second and final descriptor is a texture array - - Unlike an array texture, these are adressed like typical arrays - */ + // [POI] Second and final descriptor is a texture array + // Unlike an array texture, these are adressed like typical arrays writeDescriptorSets[1] = {}; writeDescriptorSets[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; writeDescriptorSets[1].dstBinding = 1; @@ -334,7 +270,7 @@ public: writeDescriptorSets[1].dstSet = descriptorSet; writeDescriptorSets[1].pImageInfo = textureDescriptors.data(); - vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL); + vkUpdateDescriptorSets(device, static_cast(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, nullptr); } void preparePipelines() @@ -351,7 +287,7 @@ public: VkPipelineViewportStateCreateInfo viewportStateCI = vks::initializers::pipelineViewportStateCreateInfo(1, 1, 0); VkPipelineMultisampleStateCreateInfo multisampleStateCI = vks::initializers::pipelineMultisampleStateCreateInfo(VK_SAMPLE_COUNT_1_BIT, 0); std::vector dynamicStateEnables = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR }; - VkPipelineDynamicStateCreateInfo dynamicStateCI = vks::initializers::pipelineDynamicStateCreateInfo(dynamicStateEnables.data(), dynamicStateEnables.size(), 0); + VkPipelineDynamicStateCreateInfo dynamicStateCI = vks::initializers::pipelineDynamicStateCreateInfo(dynamicStateEnables); // Vertex bindings and attributes VkVertexInputBindingDescription vertexInputBinding = { 0, sizeof(Vertex), VK_VERTEX_INPUT_RATE_VERTEX }; @@ -370,6 +306,7 @@ public: std::array shaderStages; shaderStages[0] = loadShader(getShadersPath() + "descriptorindexing/descriptorindexing.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); + // [POI] The fragment shader does non-uniform access into our sampler array, so we need to use nonuniformEXT: texture(textures[nonuniformEXT(inTexIndex)], inUV) shaderStages[1] = loadShader(getShadersPath() + "descriptorindexing/descriptorindexing.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); VkGraphicsPipelineCreateInfo pipelineCI = vks::initializers::pipelineCreateInfo(pipelineLayout, renderPass, 0); @@ -381,7 +318,7 @@ public: pipelineCI.pViewportState = &viewportStateCI; pipelineCI.pDepthStencilState = &depthStencilStateCI; pipelineCI.pDynamicState = &dynamicStateCI; - pipelineCI.stageCount = shaderStages.size(); + pipelineCI.stageCount = static_cast(shaderStages.size()); pipelineCI.pStages = shaderStages.data(); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipeline)); @@ -406,23 +343,57 @@ public: memcpy(uniformBufferVS.mapped, &uboVS, sizeof(uboVS)); } + void buildCommandBuffers() + { + VkClearValue clearValues[2]; + clearValues[0].color = defaultClearColor; + clearValues[1].depthStencil = { 1.0f, 0 }; + + VkRenderPassBeginInfo renderPassBeginInfo = vks::initializers::renderPassBeginInfo(); + renderPassBeginInfo.renderPass = renderPass; + renderPassBeginInfo.renderArea.offset.x = 0; + renderPassBeginInfo.renderArea.offset.y = 0; + renderPassBeginInfo.renderArea.extent.width = width; + renderPassBeginInfo.renderArea.extent.height = height; + renderPassBeginInfo.clearValueCount = 2; + renderPassBeginInfo.pClearValues = clearValues; + + for (int32_t i = 0; i < drawCmdBuffers.size(); ++i) + { + renderPassBeginInfo.framebuffer = frameBuffers[i]; + VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo(); + VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo)); + vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE); + VkViewport viewport = vks::initializers::viewport((float)width, (float)height, 0.0f, 1.0f); + vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport); + VkRect2D scissor = vks::initializers::rect2D(width, height, 0, 0); + vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor); + vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL); + vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline); + VkDeviceSize offsets[1] = { 0 }; + vkCmdBindVertexBuffers(drawCmdBuffers[i], 0, 1, &vertexBuffer.buffer, offsets); + vkCmdBindIndexBuffer(drawCmdBuffers[i], indexBuffer.buffer, 0, VK_INDEX_TYPE_UINT32); + vkCmdDrawIndexed(drawCmdBuffers[i], indexCount, 1, 0, 0, 0); + drawUI(drawCmdBuffers[i]); + vkCmdEndRenderPass(drawCmdBuffers[i]); + VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i])); + } + } + void draw() { VulkanExampleBase::prepareFrame(); - submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer]; VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE)); - VulkanExampleBase::submitFrame(); } void prepare() { VulkanExampleBase::prepare(); - loadAssets(); - layerCount = textures.size(); - generateBuffers(); + generateTextures(); + generateCubes(); prepareUniformBuffers(); setupDescriptorSets(); preparePipelines();