diff --git a/texture/texture.cpp b/texture/texture.cpp index 5040b999..acd6b7fd 100644 --- a/texture/texture.cpp +++ b/texture/texture.cpp @@ -45,7 +45,6 @@ public: VkImageLayout imageLayout; VkDeviceMemory deviceMemory; VkImageView view; - VkDescriptorImageInfo descriptor; uint32_t width, height; uint32_t mipLevels; } texture; @@ -474,7 +473,6 @@ public: // are abstracted by image views containing additional // information and sub resource ranges VkImageViewCreateInfo view = vkTools::initializers::imageViewCreateInfo(); - view.image = VK_NULL_HANDLE; view.viewType = VK_IMAGE_VIEW_TYPE_2D; view.format = format; view.components = { VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A }; @@ -487,13 +485,9 @@ public: // Linear tiling usually won't support mip maps // Only set mip map count if optimal tiling is used view.subresourceRange.levelCount = (useStaging) ? texture.mipLevels : 1; + // The view will be based on the texture's image view.image = texture.image; VK_CHECK_RESULT(vkCreateImageView(device, &view, nullptr, &texture.view)); - - // Fill image descriptor image info that can be used during the descriptor set setup - texture.descriptor.imageLayout = VK_IMAGE_LAYOUT_GENERAL; - texture.descriptor.imageView = texture.view; - texture.descriptor.sampler = texture.sampler; } // Free all Vulkan resources used a texture object @@ -700,6 +694,12 @@ public: VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet)); + // Setup a descriptor image info for the current texture to be used as a combined image sampler + VkDescriptorImageInfo textureDescriptor; + textureDescriptor.imageView = texture.view; // The image's view (images are never directly accessed by the shader, but rather through views defining subresources) + textureDescriptor.sampler = texture.sampler; // The sampler (Telling the pipeline how to sample the texture, including repeat, border, etc.) + textureDescriptor.imageLayout = texture.imageLayout; // The current layout of the image (Note: Should always fit the actual use, e.g. shader read) + std::vector writeDescriptorSets = { // Binding 0 : Vertex shader uniform buffer @@ -709,11 +709,12 @@ public: 0, &uniformBufferVS.descriptor), // Binding 1 : Fragment shader texture sampler + // Fragment shader: layout (binding = 1) uniform sampler2D samplerColor; vkTools::initializers::writeDescriptorSet( - descriptorSet, - VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, - 1, - &texture.descriptor) + descriptorSet, + VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, // The descriptor set will use a combined image sampler (sampler and image could be split) + 1, // Shader binding point 1 + &textureDescriptor) // Pointer to the descriptor image for our texture }; vkUpdateDescriptorSets(device, static_cast(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL); diff --git a/texturearray/texturearray.cpp b/texturearray/texturearray.cpp index 560a42c7..2bc891b0 100644 --- a/texturearray/texturearray.cpp +++ b/texturearray/texturearray.cpp @@ -489,11 +489,11 @@ public: VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet)); // Image descriptor for the texture array - VkDescriptorImageInfo texArrayDescriptor = + VkDescriptorImageInfo textureDescriptor = vkTools::initializers::descriptorImageInfo( textureArray.sampler, textureArray.view, - VK_IMAGE_LAYOUT_GENERAL); + textureArray.imageLayout); std::vector writeDescriptorSets = { @@ -508,7 +508,7 @@ public: descriptorSet, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, - &texArrayDescriptor) + &textureDescriptor) }; vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL); diff --git a/texturecubemap/texturecubemap.cpp b/texturecubemap/texturecubemap.cpp index efaea310..264df3d9 100644 --- a/texturecubemap/texturecubemap.cpp +++ b/texturecubemap/texturecubemap.cpp @@ -470,11 +470,11 @@ public: void setupDescriptorSets() { // Image descriptor for the cube map texture - VkDescriptorImageInfo cubeMapDescriptor = + VkDescriptorImageInfo textureDescriptor = vkTools::initializers::descriptorImageInfo( cubeMap.sampler, cubeMap.view, - VK_IMAGE_LAYOUT_GENERAL); + cubeMap.imageLayout); VkDescriptorSetAllocateInfo allocInfo = vkTools::initializers::descriptorSetAllocateInfo( @@ -498,7 +498,7 @@ public: descriptorSets.object, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, - &cubeMapDescriptor) + &textureDescriptor) }; vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL); @@ -518,7 +518,7 @@ public: descriptorSets.skybox, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, - &cubeMapDescriptor) + &textureDescriptor) }; vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL); } diff --git a/texturemipmapgen/texturemipmapgen.cpp b/texturemipmapgen/texturemipmapgen.cpp index 371cad37..ab4e0088 100644 --- a/texturemipmapgen/texturemipmapgen.cpp +++ b/texturemipmapgen/texturemipmapgen.cpp @@ -547,12 +547,16 @@ public: &uniformBufferVS.descriptor)); // Binding 1: Sampled image - VkDescriptorImageInfo texDescriptor = vkTools::initializers::descriptorImageInfo(VK_NULL_HANDLE, texture.view, VK_IMAGE_LAYOUT_GENERAL); + VkDescriptorImageInfo textureDescriptor = + vkTools::initializers::descriptorImageInfo( + VK_NULL_HANDLE, + texture.view, + texture.imageLayout); writeDescriptorSets.push_back(vkTools::initializers::writeDescriptorSet( descriptorSet, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1, - &texDescriptor)); + &textureDescriptor)); // Binding 2: Sampler array std::vector samplerDescriptors;