From 1a7779e75b1978488edd84912c0684b171ea371c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20S=C3=BC=C3=9Fenbach?= Date: Mon, 15 Apr 2019 10:18:58 +0200 Subject: [PATCH] Add samples InstanceExtensionProperties, InstanceLayerExtensionProperties, InstanceLayerProperties, MultipleSets (#320) + slightly adjust some other files. --- .../09_InitDescriptorSet.cpp | 2 +- samples/12_InitFrameBuffers/CMakeLists.txt | 4 +- samples/13_InitVertexBuffer/CMakeLists.txt | 4 +- samples/15_DrawCube/15_DrawCube.cpp | 4 +- samples/16_Vulkan_1_1/CMakeLists.txt | 4 +- samples/CopyBlitImage/CMakeLists.txt | 4 +- .../CreateDebugReportCallback/CMakeLists.txt | 4 +- samples/DrawTexturedCube/CMakeLists.txt | 3 +- samples/DrawTexturedCube/DrawTexturedCube.cpp | 6 +- samples/DynamicUniform/CMakeLists.txt | 3 +- samples/DynamicUniform/DynamicUniform.cpp | 4 +- .../CMakeLists.txt | 4 +- .../EnumerateDevicesAdvanced/CMakeLists.txt | 2 +- samples/Events/CMakeLists.txt | 4 +- samples/Events/Events.cpp | 1 - samples/ImmutableSampler/CMakeLists.txt | 3 +- samples/ImmutableSampler/ImmutableSampler.cpp | 6 +- samples/InitTexture/CMakeLists.txt | 3 +- samples/InputAttachment/CMakeLists.txt | 3 +- samples/InputAttachment/InputAttachment.cpp | 2 +- .../CMakeLists.txt | 35 +++ .../InstanceExtensionProperties.cpp | 59 ++++ .../CMakeLists.txt | 35 +++ .../InstanceLayerExtensionProperties.cpp | 102 +++++++ .../InstanceLayerProperties/CMakeLists.txt | 36 +++ .../InstanceLayerProperties.cpp | 73 +++++ samples/MultipleSets/CMakeLists.txt | 44 +++ samples/MultipleSets/MultipleSets.cpp | 274 ++++++++++++++++++ samples/utils/math.cpp | 10 +- samples/utils/math.hpp | 3 +- samples/utils/utils.cpp | 115 +++----- samples/utils/utils.hpp | 39 ++- 32 files changed, 771 insertions(+), 124 deletions(-) create mode 100644 samples/InstanceExtensionProperties/CMakeLists.txt create mode 100644 samples/InstanceExtensionProperties/InstanceExtensionProperties.cpp create mode 100644 samples/InstanceLayerExtensionProperties/CMakeLists.txt create mode 100644 samples/InstanceLayerExtensionProperties/InstanceLayerExtensionProperties.cpp create mode 100644 samples/InstanceLayerProperties/CMakeLists.txt create mode 100644 samples/InstanceLayerProperties/InstanceLayerProperties.cpp create mode 100644 samples/MultipleSets/CMakeLists.txt create mode 100644 samples/MultipleSets/MultipleSets.cpp diff --git a/samples/09_InitDescriptorSet/09_InitDescriptorSet.cpp b/samples/09_InitDescriptorSet/09_InitDescriptorSet.cpp index 1df411b..c1bf2c9 100644 --- a/samples/09_InitDescriptorSet/09_InitDescriptorSet.cpp +++ b/samples/09_InitDescriptorSet/09_InitDescriptorSet.cpp @@ -42,7 +42,7 @@ int main(int /*argc*/, char ** /*argv*/) vk::UniqueDevice device = vk::su::createDevice(physicalDevices[0], vk::su::findGraphicsQueueFamilyIndex(physicalDevices[0].getQueueFamilyProperties())); vk::su::BufferData uniformBufferData(physicalDevices[0], device, sizeof(glm::mat4x4), vk::BufferUsageFlagBits::eUniformBuffer); - vk::su::copyToDevice(device, uniformBufferData.deviceMemory, vk::su::createModelViewProjectionClipMatrix()); + vk::su::copyToDevice(device, uniformBufferData.deviceMemory, vk::su::createModelViewProjectionClipMatrix(vk::Extent2D(0, 0))); vk::UniqueDescriptorSetLayout descriptorSetLayout = vk::su::createDescriptorSetLayout(device); diff --git a/samples/12_InitFrameBuffers/CMakeLists.txt b/samples/12_InitFrameBuffers/CMakeLists.txt index c313c0e..bc0f9f7 100644 --- a/samples/12_InitFrameBuffers/CMakeLists.txt +++ b/samples/12_InitFrameBuffers/CMakeLists.txt @@ -34,6 +34,4 @@ add_executable(12_InitFrameBuffers ) set_target_properties(12_InitFrameBuffers PROPERTIES FOLDER "Samples") -target_include_directories(12_InitFrameBuffers PUBLIC ${CMAKE_SOURCE_DIR}/glslang) -target_link_libraries(12_InitFrameBuffers PUBLIC glslang SPIRV "${Vulkan_LIBRARIES}" -) +target_link_libraries(12_InitFrameBuffers PUBLIC "${Vulkan_LIBRARIES}") diff --git a/samples/13_InitVertexBuffer/CMakeLists.txt b/samples/13_InitVertexBuffer/CMakeLists.txt index 176a379..7a34254 100644 --- a/samples/13_InitVertexBuffer/CMakeLists.txt +++ b/samples/13_InitVertexBuffer/CMakeLists.txt @@ -35,6 +35,4 @@ add_executable(13_InitVertexBuffer ) set_target_properties(13_InitVertexBuffer PROPERTIES FOLDER "Samples") -target_include_directories(13_InitVertexBuffer PUBLIC ${CMAKE_SOURCE_DIR}/glslang) -target_link_libraries(13_InitVertexBuffer PUBLIC glslang SPIRV "${Vulkan_LIBRARIES}" -) +target_link_libraries(13_InitVertexBuffer PUBLIC "${Vulkan_LIBRARIES}") diff --git a/samples/15_DrawCube/15_DrawCube.cpp b/samples/15_DrawCube/15_DrawCube.cpp index cb335d0..7a2e5c2 100644 --- a/samples/15_DrawCube/15_DrawCube.cpp +++ b/samples/15_DrawCube/15_DrawCube.cpp @@ -55,7 +55,7 @@ int main(int /*argc*/, char ** /*argv*/) vk::su::DepthBufferData depthBufferData(physicalDevices[0], device, vk::Format::eD16Unorm, surfaceData.extent); vk::su::BufferData uniformBufferData(physicalDevices[0], device, sizeof(glm::mat4x4), vk::BufferUsageFlagBits::eUniformBuffer); - vk::su::copyToDevice(device, uniformBufferData.deviceMemory, vk::su::createModelViewProjectionClipMatrix()); + vk::su::copyToDevice(device, uniformBufferData.deviceMemory, vk::su::createModelViewProjectionClipMatrix(surfaceData.extent)); vk::UniqueDescriptorSetLayout descriptorSetLayout = vk::su::createDescriptorSetLayout(device); vk::UniquePipelineLayout pipelineLayout = device->createPipelineLayoutUnique(vk::PipelineLayoutCreateInfo(vk::PipelineLayoutCreateFlags(), 1, &descriptorSetLayout.get())); @@ -79,7 +79,7 @@ int main(int /*argc*/, char ** /*argv*/) vk::su::updateDescriptorSets(device, descriptorSets[0], vk::DescriptorType::eUniformBuffer, &descriptorBufferInfo); vk::UniquePipelineCache pipelineCache = device->createPipelineCacheUnique(vk::PipelineCacheCreateInfo()); - vk::UniquePipeline graphicsPipeline = vk::su::createGraphicsPipeline(device, pipelineCache, vertexShaderModule, fragmentShaderModule, sizeof(coloredCubeData[0]), pipelineLayout, renderPass); + vk::UniquePipeline graphicsPipeline = vk::su::createGraphicsPipeline(device, pipelineCache, vertexShaderModule, fragmentShaderModule, sizeof(coloredCubeData[0]), true, pipelineLayout, renderPass); /* VULKAN_KEY_START */ diff --git a/samples/16_Vulkan_1_1/CMakeLists.txt b/samples/16_Vulkan_1_1/CMakeLists.txt index 924707c..674933b 100644 --- a/samples/16_Vulkan_1_1/CMakeLists.txt +++ b/samples/16_Vulkan_1_1/CMakeLists.txt @@ -34,6 +34,4 @@ add_executable(16_Vulkan_1_1 ) set_target_properties(16_Vulkan_1_1 PROPERTIES FOLDER "Samples") -target_include_directories(16_Vulkan_1_1 PUBLIC ${CMAKE_SOURCE_DIR}/glslang) -target_link_libraries(16_Vulkan_1_1 PUBLIC glslang SPIRV "$ENV{VULKAN_SDK}/Lib/vulkan-1.lib" -) +target_link_libraries(16_Vulkan_1_1 PUBLIC "${Vulkan_LIBRARIES}") diff --git a/samples/CopyBlitImage/CMakeLists.txt b/samples/CopyBlitImage/CMakeLists.txt index 5bc473c..6704745 100644 --- a/samples/CopyBlitImage/CMakeLists.txt +++ b/samples/CopyBlitImage/CMakeLists.txt @@ -34,6 +34,4 @@ add_executable(CopyBlitImage ) set_target_properties(CopyBlitImage PROPERTIES FOLDER "Samples") -target_include_directories(CopyBlitImage PUBLIC ${CMAKE_SOURCE_DIR}/glslang) -target_link_libraries(CopyBlitImage PUBLIC glslang SPIRV "$ENV{VULKAN_SDK}/Lib/vulkan-1.lib" -) +target_link_libraries(CopyBlitImage PUBLIC "${Vulkan_LIBRARIES}") diff --git a/samples/CreateDebugReportCallback/CMakeLists.txt b/samples/CreateDebugReportCallback/CMakeLists.txt index 0fb2b23..21be8ba 100644 --- a/samples/CreateDebugReportCallback/CMakeLists.txt +++ b/samples/CreateDebugReportCallback/CMakeLists.txt @@ -32,6 +32,4 @@ add_executable(CreateDebugReportCallback ) set_target_properties(CreateDebugReportCallback PROPERTIES FOLDER "Samples") -target_include_directories(CreateDebugReportCallback PUBLIC ${CMAKE_SOURCE_DIR}/glslang) -target_link_libraries(CreateDebugReportCallback PUBLIC glslang SPIRV "$ENV{VULKAN_SDK}/Lib/vulkan-1.lib" -) +target_link_libraries(CreateDebugReportCallback PUBLIC "${Vulkan_LIBRARIES}") diff --git a/samples/DrawTexturedCube/CMakeLists.txt b/samples/DrawTexturedCube/CMakeLists.txt index a23009c..062c72c 100644 --- a/samples/DrawTexturedCube/CMakeLists.txt +++ b/samples/DrawTexturedCube/CMakeLists.txt @@ -41,5 +41,4 @@ add_executable(DrawTexturedCube set_target_properties(DrawTexturedCube PROPERTIES FOLDER "Samples") target_include_directories(DrawTexturedCube PUBLIC ${CMAKE_SOURCE_DIR}/glslang) -target_link_libraries(DrawTexturedCube PUBLIC glslang SPIRV "$ENV{VULKAN_SDK}/Lib/vulkan-1.lib" -) +target_link_libraries(DrawTexturedCube PUBLIC glslang SPIRV "${Vulkan_LIBRARIES}") diff --git a/samples/DrawTexturedCube/DrawTexturedCube.cpp b/samples/DrawTexturedCube/DrawTexturedCube.cpp index 5d4025f..420daea 100644 --- a/samples/DrawTexturedCube/DrawTexturedCube.cpp +++ b/samples/DrawTexturedCube/DrawTexturedCube.cpp @@ -59,10 +59,10 @@ int main(int /*argc*/, char ** /*argv*/) vk::su::TextureData textureData(physicalDevices[0], device); commandBuffers[0]->begin(vk::CommandBufferBeginInfo()); - textureData.setCheckerboardTexture(device, commandBuffers[0]); + textureData.setTexture(device, commandBuffers[0], vk::su::CheckerboardTextureCreator()); vk::su::BufferData uniformBufferData(physicalDevices[0], device, sizeof(glm::mat4x4), vk::BufferUsageFlagBits::eUniformBuffer); - vk::su::copyToDevice(device, uniformBufferData.deviceMemory, vk::su::createModelViewProjectionClipMatrix()); + vk::su::copyToDevice(device, uniformBufferData.deviceMemory, vk::su::createModelViewProjectionClipMatrix(surfaceData.extent)); vk::UniqueDescriptorSetLayout descriptorSetLayout = vk::su::createDescriptorSetLayout(device, vk::DescriptorType::eUniformBuffer, textured); vk::UniquePipelineLayout pipelineLayout = device->createPipelineLayoutUnique(vk::PipelineLayoutCreateInfo(vk::PipelineLayoutCreateFlags(), 1, &descriptorSetLayout.get())); @@ -87,7 +87,7 @@ int main(int /*argc*/, char ** /*argv*/) vk::su::updateDescriptorSets(device, descriptorSets[0], vk::DescriptorType::eUniformBuffer, &bufferInfo, &imageInfo); vk::UniquePipelineCache pipelineCache = device->createPipelineCacheUnique(vk::PipelineCacheCreateInfo()); - vk::UniquePipeline graphicsPipeline = vk::su::createGraphicsPipeline(device, pipelineCache, vertexShaderModule, fragmentShaderModule, sizeof(texturedCubeData[0]), pipelineLayout, renderPass); + vk::UniquePipeline graphicsPipeline = vk::su::createGraphicsPipeline(device, pipelineCache, vertexShaderModule, fragmentShaderModule, sizeof(texturedCubeData[0]), true, pipelineLayout, renderPass); /* VULKAN_KEY_START */ diff --git a/samples/DynamicUniform/CMakeLists.txt b/samples/DynamicUniform/CMakeLists.txt index cec6b22..2f7f721 100644 --- a/samples/DynamicUniform/CMakeLists.txt +++ b/samples/DynamicUniform/CMakeLists.txt @@ -41,5 +41,4 @@ add_executable(DynamicUniform set_target_properties(DynamicUniform PROPERTIES FOLDER "Samples") target_include_directories(DynamicUniform PUBLIC ${CMAKE_SOURCE_DIR}/glslang) -target_link_libraries(DynamicUniform PUBLIC glslang SPIRV "$ENV{VULKAN_SDK}/Lib/vulkan-1.lib" -) +target_link_libraries(DynamicUniform PUBLIC glslang SPIRV "${Vulkan_LIBRARIES}") diff --git a/samples/DynamicUniform/DynamicUniform.cpp b/samples/DynamicUniform/DynamicUniform.cpp index f614b7c..777659c 100644 --- a/samples/DynamicUniform/DynamicUniform.cpp +++ b/samples/DynamicUniform/DynamicUniform.cpp @@ -83,7 +83,7 @@ int main(int /*argc*/, char ** /*argv*/) glm::mat4x4 clip = glm::mat4x4(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 1.0f); // vulkan clip space has inverted y and half z ! mvpcs[0] = clip * projection * view * model; - model = glm::translate(model, glm::vec3(-1.5f, -1.5f, -1.5f)); + model = glm::translate(model, glm::vec3(-1.5f, 1.5f, -1.5f)); mvpcs[1] = clip * projection * view * model; VkDeviceSize bufferSize = sizeof(glm::mat4x4); @@ -107,7 +107,7 @@ int main(int /*argc*/, char ** /*argv*/) vk::su::updateDescriptorSets(device, descriptorSets[0], vk::DescriptorType::eUniformBufferDynamic, &descriptorBufferInfo); vk::UniquePipelineCache pipelineCache = device->createPipelineCacheUnique(vk::PipelineCacheCreateInfo()); - vk::UniquePipeline graphicsPipeline = vk::su::createGraphicsPipeline(device, pipelineCache, vertexShaderModule, fragmentShaderModule, sizeof(coloredCubeData[0]), pipelineLayout, renderPass); + vk::UniquePipeline graphicsPipeline = vk::su::createGraphicsPipeline(device, pipelineCache, vertexShaderModule, fragmentShaderModule, sizeof(coloredCubeData[0]), true, pipelineLayout, renderPass); // Get the index of the next available swapchain image: vk::UniqueSemaphore imageAcquiredSemaphore = device->createSemaphoreUnique(vk::SemaphoreCreateInfo()); diff --git a/samples/EnableValidationWithCallback/CMakeLists.txt b/samples/EnableValidationWithCallback/CMakeLists.txt index 3dc4146..1415ebf 100644 --- a/samples/EnableValidationWithCallback/CMakeLists.txt +++ b/samples/EnableValidationWithCallback/CMakeLists.txt @@ -32,6 +32,4 @@ add_executable(EnableValidationWithCallback ) set_target_properties(EnableValidationWithCallback PROPERTIES FOLDER "Samples") -target_include_directories(EnableValidationWithCallback PUBLIC ${CMAKE_SOURCE_DIR}/glslang) -target_link_libraries(EnableValidationWithCallback PUBLIC glslang SPIRV "$ENV{VULKAN_SDK}/Lib/vulkan-1.lib" -) +target_link_libraries(EnableValidationWithCallback PUBLIC "${Vulkan_LIBRARIES}") diff --git a/samples/EnumerateDevicesAdvanced/CMakeLists.txt b/samples/EnumerateDevicesAdvanced/CMakeLists.txt index b23a4ce..c057ece 100644 --- a/samples/EnumerateDevicesAdvanced/CMakeLists.txt +++ b/samples/EnumerateDevicesAdvanced/CMakeLists.txt @@ -34,4 +34,4 @@ add_executable(EnumerateDevicesAdvanced ) set_target_properties(EnumerateDevicesAdvanced PROPERTIES FOLDER "Samples") -target_link_libraries(EnumerateDevicesAdvanced "$ENV{VULKAN_SDK}/Lib/vulkan-1.lib") +target_link_libraries(EnumerateDevicesAdvanced "${Vulkan_LIBRARIES}") diff --git a/samples/Events/CMakeLists.txt b/samples/Events/CMakeLists.txt index baed87d..6c57fcf 100644 --- a/samples/Events/CMakeLists.txt +++ b/samples/Events/CMakeLists.txt @@ -34,6 +34,4 @@ add_executable(Events ) set_target_properties(Events PROPERTIES FOLDER "Samples") -target_include_directories(Events PUBLIC ${CMAKE_SOURCE_DIR}/glslang) -target_link_libraries(Events PUBLIC glslang SPIRV "$ENV{VULKAN_SDK}/Lib/vulkan-1.lib" -) +target_link_libraries(Events PUBLIC "${Vulkan_LIBRARIES}") diff --git a/samples/Events/Events.cpp b/samples/Events/Events.cpp index ad29720..65c2c24 100644 --- a/samples/Events/Events.cpp +++ b/samples/Events/Events.cpp @@ -17,7 +17,6 @@ #include "../utils/utils.hpp" #include "vulkan/vulkan.hpp" -#include "SPIRV/GlslangToSpv.h" #include static char const* AppName = "Events"; diff --git a/samples/ImmutableSampler/CMakeLists.txt b/samples/ImmutableSampler/CMakeLists.txt index ce7e445..0550be0 100644 --- a/samples/ImmutableSampler/CMakeLists.txt +++ b/samples/ImmutableSampler/CMakeLists.txt @@ -41,5 +41,4 @@ add_executable(ImmutableSampler set_target_properties(ImmutableSampler PROPERTIES FOLDER "Samples") target_include_directories(ImmutableSampler PUBLIC ${CMAKE_SOURCE_DIR}/glslang) -target_link_libraries(ImmutableSampler PUBLIC glslang SPIRV "$ENV{VULKAN_SDK}/Lib/vulkan-1.lib" -) +target_link_libraries(ImmutableSampler PUBLIC glslang SPIRV "${Vulkan_LIBRARIES}") diff --git a/samples/ImmutableSampler/ImmutableSampler.cpp b/samples/ImmutableSampler/ImmutableSampler.cpp index 2be1172..ca50e0d 100644 --- a/samples/ImmutableSampler/ImmutableSampler.cpp +++ b/samples/ImmutableSampler/ImmutableSampler.cpp @@ -55,7 +55,7 @@ int main(int /*argc*/, char ** /*argv*/) vk::su::DepthBufferData depthBufferData(physicalDevices[0], device, vk::Format::eD16Unorm, surfaceData.extent); vk::su::BufferData uniformBufferData(physicalDevices[0], device, sizeof(glm::mat4x4), vk::BufferUsageFlagBits::eUniformBuffer); - vk::su::copyToDevice(device, uniformBufferData.deviceMemory, vk::su::createModelViewProjectionClipMatrix()); + vk::su::copyToDevice(device, uniformBufferData.deviceMemory, vk::su::createModelViewProjectionClipMatrix(surfaceData.extent)); vk::UniqueRenderPass renderPass = vk::su::createRenderPass(device, vk::su::pickColorFormat(physicalDevices[0].getSurfaceFormatsKHR(surfaceData.surface.get())), depthBufferData.format); @@ -74,7 +74,7 @@ int main(int /*argc*/, char ** /*argv*/) vk::su::TextureData textureData(physicalDevices[0], device); commandBuffers[0]->begin(vk::CommandBufferBeginInfo()); - textureData.setCheckerboardTexture(device, commandBuffers[0]); + textureData.setTexture(device, commandBuffers[0], vk::su::CheckerboardTextureCreator()); vk::DescriptorSetLayoutBinding bindings[2] = { @@ -109,7 +109,7 @@ int main(int /*argc*/, char ** /*argv*/) /* VULKAN_KEY_END */ vk::UniquePipelineCache pipelineCache = device->createPipelineCacheUnique(vk::PipelineCacheCreateInfo()); - vk::UniquePipeline graphicsPipeline = vk::su::createGraphicsPipeline(device, pipelineCache, vertexShaderModule, fragmentShaderModule, sizeof(texturedCubeData[0]), pipelineLayout, renderPass); + vk::UniquePipeline graphicsPipeline = vk::su::createGraphicsPipeline(device, pipelineCache, vertexShaderModule, fragmentShaderModule, sizeof(texturedCubeData[0]), true, pipelineLayout, renderPass); vk::UniqueSemaphore imageAcquiredSemaphore = device->createSemaphoreUnique(vk::SemaphoreCreateInfo()); vk::ResultValue currentBuffer = device->acquireNextImageKHR(swapChainData.swapChain.get(), vk::su::FenceTimeout, imageAcquiredSemaphore.get(), nullptr); diff --git a/samples/InitTexture/CMakeLists.txt b/samples/InitTexture/CMakeLists.txt index 4b533c1..9de257d 100644 --- a/samples/InitTexture/CMakeLists.txt +++ b/samples/InitTexture/CMakeLists.txt @@ -41,5 +41,4 @@ add_executable(InitTexture set_target_properties(InitTexture PROPERTIES FOLDER "Samples") target_include_directories(InitTexture PUBLIC ${CMAKE_SOURCE_DIR}/glslang) -target_link_libraries(InitTexture PUBLIC glslang SPIRV "$ENV{VULKAN_SDK}/Lib/vulkan-1.lib" -) +target_link_libraries(InitTexture PUBLIC glslang SPIRV "${Vulkan_LIBRARIES}") diff --git a/samples/InputAttachment/CMakeLists.txt b/samples/InputAttachment/CMakeLists.txt index 85e6349..0098df8 100644 --- a/samples/InputAttachment/CMakeLists.txt +++ b/samples/InputAttachment/CMakeLists.txt @@ -41,5 +41,4 @@ add_executable(InputAttachment set_target_properties(InputAttachment PROPERTIES FOLDER "Samples") target_include_directories(InputAttachment PUBLIC ${CMAKE_SOURCE_DIR}/glslang) -target_link_libraries(InputAttachment PUBLIC glslang SPIRV "$ENV{VULKAN_SDK}/Lib/vulkan-1.lib" -) +target_link_libraries(InputAttachment PUBLIC glslang SPIRV "${Vulkan_LIBRARIES}") diff --git a/samples/InputAttachment/InputAttachment.cpp b/samples/InputAttachment/InputAttachment.cpp index 4c03662..b99f963 100644 --- a/samples/InputAttachment/InputAttachment.cpp +++ b/samples/InputAttachment/InputAttachment.cpp @@ -155,7 +155,7 @@ int main(int /*argc*/, char ** /*argv*/) device->updateDescriptorSets(vk::ArrayProxy(1, &writeDescriptorSet), nullptr); vk::UniquePipelineCache pipelineCache = device->createPipelineCacheUnique(vk::PipelineCacheCreateInfo()); - vk::UniquePipeline graphicsPipeline = vk::su::createGraphicsPipeline(device, pipelineCache, vertexShaderModule, fragmentShaderModule, 0, pipelineLayout, renderPass); + vk::UniquePipeline graphicsPipeline = vk::su::createGraphicsPipeline(device, pipelineCache, vertexShaderModule, fragmentShaderModule, 0, false, pipelineLayout, renderPass); vk::UniqueSemaphore imageAcquiredSemaphore = device->createSemaphoreUnique(vk::SemaphoreCreateInfo()); diff --git a/samples/InstanceExtensionProperties/CMakeLists.txt b/samples/InstanceExtensionProperties/CMakeLists.txt new file mode 100644 index 0000000..474703f --- /dev/null +++ b/samples/InstanceExtensionProperties/CMakeLists.txt @@ -0,0 +1,35 @@ +# Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +cmake_minimum_required(VERSION 3.2) + +project(InstanceExtensionProperties) + +set(HEADERS +) + +set(SOURCES + InstanceExtensionProperties.cpp +) + +source_group(headers FILES ${HEADERS}) +source_group(sources FILES ${SOURCES}) + +add_executable(InstanceExtensionProperties + ${HEADERS} + ${SOURCES} +) + +set_target_properties(InstanceExtensionProperties PROPERTIES FOLDER "Samples") +target_link_libraries(InstanceExtensionProperties PUBLIC "${Vulkan_LIBRARIES}") diff --git a/samples/InstanceExtensionProperties/InstanceExtensionProperties.cpp b/samples/InstanceExtensionProperties/InstanceExtensionProperties.cpp new file mode 100644 index 0000000..a32bcb7 --- /dev/null +++ b/samples/InstanceExtensionProperties/InstanceExtensionProperties.cpp @@ -0,0 +1,59 @@ +// Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// VulkanHpp Samples : InstanceExtensionProperties +// Get global extension properties to know what extension are available to enable at CreateInstance time. + +#include "vulkan/vulkan.hpp" +#include +#include + +static char const* AppName = "InstanceExtensionProperties"; +static char const* EngineName = "Vulkan.hpp"; + +int main(int /*argc*/, char ** /*argv*/) +{ + try + { + /* VULKAN_KEY_START */ + + std::vector instanceExtensionProperties = vk::enumerateInstanceExtensionProperties(); + + std::cout << "Instance Extensions:" << std::endl; + for (auto const& instanceExtensionProperty : instanceExtensionProperties) + { + std::cout << instanceExtensionProperty.extensionName << ":" << std::endl; + std::cout << "\tVersion: " << instanceExtensionProperty.specVersion << std::endl; + std::cout << std::endl; + } + + /* VULKAN_KEY_END */ + } + catch (vk::SystemError err) + { + std::cout << "vk::SystemError: " << err.what() << std::endl; + exit(-1); + } + catch (std::runtime_error err) + { + std::cout << "std::runtime_error: " << err.what() << std::endl; + exit(-1); + } + catch (...) + { + std::cout << "unknown error\n"; + exit(-1); + } + return 0; +} diff --git a/samples/InstanceLayerExtensionProperties/CMakeLists.txt b/samples/InstanceLayerExtensionProperties/CMakeLists.txt new file mode 100644 index 0000000..12990cf --- /dev/null +++ b/samples/InstanceLayerExtensionProperties/CMakeLists.txt @@ -0,0 +1,35 @@ +# Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +cmake_minimum_required(VERSION 3.2) + +project(InstanceLayerExtensionProperties) + +set(HEADERS +) + +set(SOURCES + InstanceLayerExtensionProperties.cpp +) + +source_group(headers FILES ${HEADERS}) +source_group(sources FILES ${SOURCES}) + +add_executable(InstanceLayerExtensionProperties + ${HEADERS} + ${SOURCES} +) + +set_target_properties(InstanceLayerExtensionProperties PROPERTIES FOLDER "Samples") +target_link_libraries(InstanceLayerExtensionProperties PUBLIC "${Vulkan_LIBRARIES}") diff --git a/samples/InstanceLayerExtensionProperties/InstanceLayerExtensionProperties.cpp b/samples/InstanceLayerExtensionProperties/InstanceLayerExtensionProperties.cpp new file mode 100644 index 0000000..1116926 --- /dev/null +++ b/samples/InstanceLayerExtensionProperties/InstanceLayerExtensionProperties.cpp @@ -0,0 +1,102 @@ +// Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// VulkanHpp Samples : InstanceLayerExtensionProperties +// Get list of global layers and their associated extensions, if any. + +#include "vulkan/vulkan.hpp" +#include +#include +#include + +static char const* AppName = "InstanceLayerExtensionProperties"; +static char const* EngineName = "Vulkan.hpp"; + +struct PropertyData +{ + PropertyData(vk::LayerProperties const& layerProperties_, std::vector const& extensionProperties_) + : layerProperties(layerProperties_) + , extensionProperties(extensionProperties_) + {} + + vk::LayerProperties layerProperties; + std::vector extensionProperties; +}; + +int main(int /*argc*/, char ** /*argv*/) +{ + try + { + std::vector layerProperties = vk::enumerateInstanceLayerProperties(); + + /* VULKAN_KEY_START */ + + std::vector propertyData; + propertyData.reserve(layerProperties.size()); + + for (auto const& layerProperty : layerProperties) + { + std::vector extensionProperties = vk::enumerateInstanceExtensionProperties(std::string(layerProperty.layerName)); + propertyData.push_back(PropertyData(layerProperty, extensionProperties)); + } + + /* VULKAN_KEY_END */ + + std::cout << "Instance Layers:" << std::endl; + if (propertyData.empty()) + { + std::cout << "Set the environment variable VK_LAYER_PATH to point to the location of your layers" << std::endl; + } + else + { + for (auto const& pd : propertyData) + { + std::cout << pd.layerProperties.layerName << std::endl; + if (pd.extensionProperties.empty()) + { + std::cout << "Layer Extension: None"; + } + else + { + for (auto it = pd.extensionProperties.begin(); it != pd.extensionProperties.end(); ++it) + { + if (it != pd.extensionProperties.begin()) + { + std::cout << ", "; + } + std::cout << it->extensionName << " Version " << it->specVersion; + } + } + std::cout << std::endl << std::endl; + } + } + std::cout << std::endl; + } + catch (vk::SystemError err) + { + std::cout << "vk::SystemError: " << err.what() << std::endl; + exit(-1); + } + catch (std::runtime_error err) + { + std::cout << "std::runtime_error: " << err.what() << std::endl; + exit(-1); + } + catch (...) + { + std::cout << "unknown error\n"; + exit(-1); + } + return 0; +} diff --git a/samples/InstanceLayerProperties/CMakeLists.txt b/samples/InstanceLayerProperties/CMakeLists.txt new file mode 100644 index 0000000..6a3205f --- /dev/null +++ b/samples/InstanceLayerProperties/CMakeLists.txt @@ -0,0 +1,36 @@ +# Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +cmake_minimum_required(VERSION 3.2) + +project(InstanceLayerProperties) + +set(HEADERS +) + +set(SOURCES + InstanceLayerProperties.cpp +) + +source_group(headers FILES ${HEADERS}) +source_group(sources FILES ${SOURCES}) + +add_executable(InstanceLayerProperties + ${HEADERS} + ${SOURCES} +) + +set_target_properties(InstanceLayerProperties PROPERTIES FOLDER "Samples") +target_link_libraries(InstanceLayerProperties PUBLIC "$ENV{VULKAN_SDK}/Lib/vulkan-1.lib" +) diff --git a/samples/InstanceLayerProperties/InstanceLayerProperties.cpp b/samples/InstanceLayerProperties/InstanceLayerProperties.cpp new file mode 100644 index 0000000..7c134ac --- /dev/null +++ b/samples/InstanceLayerProperties/InstanceLayerProperties.cpp @@ -0,0 +1,73 @@ +// Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// VulkanHpp Samples : InstanceLayerProperties +// Get global layer properties to know what layers are available to enable at CreateInstance time. + + +#include "vulkan/vulkan.hpp" +#include +#include +#include + +static char const* AppName = "InstanceLayerProperties"; +static char const* EngineName = "Vulkan.hpp"; + +void extract_version(uint32_t version, uint32_t &major, uint32_t &minor, uint32_t &patch) { + major = version >> 22; + minor = (version >> 12) & 0x3ff; + patch = version & 0xfff; +} + +int main(int /*argc*/, char ** /*argv*/) +{ + try + { + /* VULKAN_KEY_START */ + + std::vector layerProperties = vk::enumerateInstanceLayerProperties(); + + std::cout << "Instance Layers:" << std::endl; + if (layerProperties.empty()) + { + std::cout << "Set the environment variable VK_LAYER_PATH to point to the location of your layers" << std::endl; + } + for (auto const& lp : layerProperties) + { + std::cout << lp.layerName << ":" << std::endl; + std::cout << "\tVersion: " << lp.implementationVersion << std::endl; + std::cout << "\tAPI Version: (" << (lp.specVersion >> 22) << "." << ((lp.specVersion >> 12) & 0x03FF) << "." << (lp.specVersion & 0xFFF) << ")" << std::endl; + std::cout << "\tDescription: " << lp.description << std::endl; + std::cout << std::endl; + } + + /* VULKAN_KEY_END */ + } + catch (vk::SystemError err) + { + std::cout << "vk::SystemError: " << err.what() << std::endl; + exit(-1); + } + catch (std::runtime_error err) + { + std::cout << "std::runtime_error: " << err.what() << std::endl; + exit(-1); + } + catch (...) + { + std::cout << "unknown error\n"; + exit(-1); + } + return 0; +} diff --git a/samples/MultipleSets/CMakeLists.txt b/samples/MultipleSets/CMakeLists.txt new file mode 100644 index 0000000..c9b6f35 --- /dev/null +++ b/samples/MultipleSets/CMakeLists.txt @@ -0,0 +1,44 @@ +# Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +cmake_minimum_required(VERSION 3.2) + +project(MultipleSets) + +set(HEADERS + ../utils/geometries.hpp + ../utils/math.hpp + ../utils/shaders.hpp + ../utils/utils.hpp +) + +set(SOURCES + MultipleSets.cpp + ../utils/math.cpp + ../utils/shaders.cpp + ../utils/utils.cpp + ../../glslang/StandAlone/ResourceLimits.cpp +) + +source_group(headers FILES ${HEADERS}) +source_group(sources FILES ${SOURCES}) + +add_executable(MultipleSets + ${HEADERS} + ${SOURCES} +) + +set_target_properties(MultipleSets PROPERTIES FOLDER "Samples") +target_include_directories(MultipleSets PUBLIC ${CMAKE_SOURCE_DIR}/glslang) +target_link_libraries(MultipleSets PUBLIC glslang SPIRV "${Vulkan_LIBRARIES}") diff --git a/samples/MultipleSets/MultipleSets.cpp b/samples/MultipleSets/MultipleSets.cpp new file mode 100644 index 0000000..1df7b66 --- /dev/null +++ b/samples/MultipleSets/MultipleSets.cpp @@ -0,0 +1,274 @@ +// Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// VulkanHpp Samples : MultipleSets +// Use multiple descriptor sets to draw a textured cube. + +#include "../utils/geometries.hpp" +#include "../utils/math.hpp" +#include "../utils/shaders.hpp" +#include "../utils/utils.hpp" +#include "vulkan/vulkan.hpp" +#include "SPIRV/GlslangToSpv.h" +#include + +static char const* AppName = "MultipleSets"; +static char const* EngineName = "Vulkan.hpp"; + +const std::string vertexShaderText = R"( +#version 400 + +#extension GL_ARB_separate_shader_objects : enable +#extension GL_ARB_shading_language_420pack : enable + +layout (std140, set = 0, binding = 0) uniform buffer +{ + mat4 mvp; +} uniformBuffer; + +layout (set = 1, binding = 0) uniform sampler2D surface; + +layout (location = 0) in vec4 pos; +layout (location = 1) in vec2 inTexCoord; + +layout (location = 0) out vec4 outColor; +layout (location = 1) out vec2 outTexCoord; + +void main() +{ + outColor = texture(surface, vec2(0.0f)); + outTexCoord = inTexCoord; + gl_Position = uniformBuffer.mvp * pos; +} +)"; + +const char *fragShaderText = +"#version 400\n" +"#extension GL_ARB_separate_shader_objects : enable\n" +"#extension GL_ARB_shading_language_420pack : enable\n" +"layout (location = 0) in vec4 inColor;\n" +"layout (location = 1) in vec2 inTexCoords;\n" +"layout (location = 0) out vec4 outColor;\n" +"void main() {\n" +" vec4 resColor = inColor;\n" +// Create a border to see the cube more easily +" if (inTexCoords.x < 0.01 || inTexCoords.x > 0.99)\n" +" resColor *= vec4(0.1, 0.1, 0.1, 1.0);\n" +" if (inTexCoords.y < 0.01 || inTexCoords.y > 0.99)\n" +" resColor *= vec4(0.1, 0.1, 0.1, 1.0);\n" +" outColor = resColor;\n" +"}\n"; + +const std::string fragmentShaderText = R"( +#version 400 + +#extension GL_ARB_separate_shader_objects : enable +#extension GL_ARB_shading_language_420pack : enable + +layout (location = 0) in vec4 inColor; +layout (location = 1) in vec2 inTexCoord; + +layout (location = 0) out vec4 outColor; + +void main() +{ + outColor = inColor; + + // create a border to see the cube more easily + if ((inTexCoord.x < 0.01f) || (0.99f < inTexCoord.x) || (inTexCoord.y < 0.01f) || (0.99f < inTexCoord.y)) + { + outColor *= vec4(0.1f, 0.1f, 0.1f, 1.0f); + } +} +)"; + +class MonochromeTextureGenerator +{ +public: + MonochromeTextureGenerator(std::array const& rgb_) + : rgb(rgb_) + {} + + void operator()(void* data, vk::Extent2D &extent) const + { + // fill in with the monochrome color + unsigned char *pImageMemory = static_cast(data); + for (uint32_t row = 0; row < extent.height; row++) + { + for (uint32_t col = 0; col < extent.width; col++) + { + pImageMemory[0] = rgb[0]; + pImageMemory[1] = rgb[1]; + pImageMemory[2] = rgb[2]; + pImageMemory[3] = 255; + pImageMemory += 4; + } + } + } + +private: + std::array const& rgb; +}; + +int main(int /*argc*/, char ** /*argv*/) +{ + try + { + vk::UniqueInstance instance = vk::su::createInstance(AppName, EngineName, vk::su::getInstanceExtensions()); +#if !defined(NDEBUG) + vk::UniqueDebugReportCallbackEXT debugReportCallback = vk::su::createDebugReportCallback(instance); +#endif + + std::vector physicalDevices = instance->enumeratePhysicalDevices(); + assert(!physicalDevices.empty()); + + vk::su::SurfaceData surfaceData(instance, AppName, AppName, vk::Extent2D(500, 500)); + + std::pair graphicsAndPresentQueueFamilyIndex = vk::su::findGraphicsAndPresentQueueFamilyIndex(physicalDevices[0], surfaceData.surface); + vk::UniqueDevice device = vk::su::createDevice(physicalDevices[0], graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions()); + + vk::UniqueCommandPool commandPool = vk::su::createCommandPool(device, graphicsAndPresentQueueFamilyIndex.first); + std::vector commandBuffers = device->allocateCommandBuffersUnique(vk::CommandBufferAllocateInfo(commandPool.get(), vk::CommandBufferLevel::ePrimary, 1)); + + vk::Queue graphicsQueue = device->getQueue(graphicsAndPresentQueueFamilyIndex.first, 0); + vk::Queue presentQueue = device->getQueue(graphicsAndPresentQueueFamilyIndex.second, 0); + + vk::su::SwapChainData swapChainData(physicalDevices[0], device, surfaceData.surface, surfaceData.extent, vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eTransferSrc + , graphicsAndPresentQueueFamilyIndex.first, graphicsAndPresentQueueFamilyIndex.second); + + vk::su::DepthBufferData depthBufferData(physicalDevices[0], device, vk::Format::eD16Unorm, surfaceData.extent); + + vk::su::TextureData textureData(physicalDevices[0], device); + + commandBuffers[0]->begin(vk::CommandBufferBeginInfo()); + textureData.setTexture(device, commandBuffers[0], MonochromeTextureGenerator({ 118, 185, 0 })); + + vk::su::BufferData uniformBufferData(physicalDevices[0], device, sizeof(glm::mat4x4), vk::BufferUsageFlagBits::eUniformBuffer); + vk::su::copyToDevice(device, uniformBufferData.deviceMemory, vk::su::createModelViewProjectionClipMatrix(surfaceData.extent)); + + vk::UniqueRenderPass renderPass = vk::su::createRenderPass(device, vk::su::pickColorFormat(physicalDevices[0].getSurfaceFormatsKHR(surfaceData.surface.get())), depthBufferData.format); + + glslang::InitializeProcess(); + vk::UniqueShaderModule vertexShaderModule = vk::su::createShaderModule(device, vk::ShaderStageFlagBits::eVertex, vertexShaderText); + vk::UniqueShaderModule fragmentShaderModule = vk::su::createShaderModule(device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText); + glslang::FinalizeProcess(); + + std::vector framebuffers = vk::su::createFramebuffers(device, renderPass, swapChainData.imageViews, depthBufferData.imageView, surfaceData.extent); + + vk::su::BufferData vertexBufferData(physicalDevices[0], device, sizeof(texturedCubeData), vk::BufferUsageFlagBits::eVertexBuffer); + vk::su::copyToDevice(device, vertexBufferData.deviceMemory, texturedCubeData, sizeof(texturedCubeData) / sizeof(texturedCubeData[0])); + + /* VULKAN_KEY_START */ + + // Create first layout to contain uniform buffer data + vk::DescriptorSetLayoutBinding uniformBinding(0, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex); + vk::UniqueDescriptorSetLayout uniformLayout = device->createDescriptorSetLayoutUnique(vk::DescriptorSetLayoutCreateInfo(vk::DescriptorSetLayoutCreateFlags(), 1, &uniformBinding)); + + // Create second layout containing combined sampler/image data + vk::DescriptorSetLayoutBinding sampler2DBinding(0, vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eVertex); + vk::UniqueDescriptorSetLayout samplerLayout = device->createDescriptorSetLayoutUnique(vk::DescriptorSetLayoutCreateInfo(vk::DescriptorSetLayoutCreateFlags(), 1, &sampler2DBinding)); + + // Create pipeline layout with multiple descriptor sets + std::array descriptorSetLayouts = { uniformLayout.get(), samplerLayout.get() }; + vk::UniquePipelineLayout pipelineLayout = device->createPipelineLayoutUnique(vk::PipelineLayoutCreateInfo(vk::PipelineLayoutCreateFlags(), 2, descriptorSetLayouts.data())); + + // Create a single pool to contain data for our two descriptor sets + vk::DescriptorPoolSize poolSizes[2] = + { + vk::DescriptorPoolSize(vk::DescriptorType::eUniformBuffer, 1), + vk::DescriptorPoolSize(vk::DescriptorType::eCombinedImageSampler, 1) + }; + vk::UniqueDescriptorPool descriptorPool = device->createDescriptorPoolUnique(vk::DescriptorPoolCreateInfo(vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, 2, 2, poolSizes)); + + // Populate descriptor sets + std::vector descriptorSets = device->allocateDescriptorSetsUnique(vk::DescriptorSetAllocateInfo(descriptorPool.get(), 2, descriptorSetLayouts.data())); + + // Populate with info about our uniform buffer + vk::DescriptorBufferInfo uniformBufferInfo(uniformBufferData.buffer.get(), 0, sizeof(glm::mat4x4)); + vk::DescriptorImageInfo textureImageInfo(textureData.textureSampler.get(), textureData.imageData->imageView.get(), vk::ImageLayout::eShaderReadOnlyOptimal); + std::array writeDescriptorSets = + { + vk::WriteDescriptorSet(descriptorSets[0].get(), 0, 0, 1, vk::DescriptorType::eUniformBuffer, nullptr, &uniformBufferInfo), + vk::WriteDescriptorSet(descriptorSets[1].get(), 0, 0, 1, vk::DescriptorType::eCombinedImageSampler, &textureImageInfo) + }; + device->updateDescriptorSets(writeDescriptorSets, nullptr); + + /* VULKAN_KEY_END */ + + vk::UniquePipelineCache pipelineCache = device->createPipelineCacheUnique(vk::PipelineCacheCreateInfo()); + vk::UniquePipeline graphicsPipeline = vk::su::createGraphicsPipeline(device, pipelineCache, vertexShaderModule, fragmentShaderModule, sizeof(texturedCubeData[0]), true, pipelineLayout, renderPass); + + // Get the index of the next available swapchain image: + vk::UniqueSemaphore imageAcquiredSemaphore = device->createSemaphoreUnique(vk::SemaphoreCreateInfo()); + vk::ResultValue currentBuffer = device->acquireNextImageKHR(swapChainData.swapChain.get(), vk::su::FenceTimeout, imageAcquiredSemaphore.get(), nullptr); + assert(currentBuffer.result == vk::Result::eSuccess); + assert(currentBuffer.value < framebuffers.size()); + + vk::ClearValue clearValues[2]; + clearValues[0].color = vk::ClearColorValue(std::array({ 0.2f, 0.2f, 0.2f, 0.2f })); + clearValues[1].depthStencil = vk::ClearDepthStencilValue(1.0f, 0); + vk::RenderPassBeginInfo renderPassBeginInfo(renderPass.get(), framebuffers[currentBuffer.value].get(), vk::Rect2D(vk::Offset2D(0, 0), surfaceData.extent), 2, clearValues); + commandBuffers[0]->beginRenderPass(renderPassBeginInfo, vk::SubpassContents::eInline); + commandBuffers[0]->bindPipeline(vk::PipelineBindPoint::eGraphics, graphicsPipeline.get()); + commandBuffers[0]->bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipelineLayout.get(), 0, { descriptorSets[0].get(), descriptorSets[1].get() }, nullptr); + + vk::DeviceSize offset = 0; + commandBuffers[0]->bindVertexBuffers(0, vertexBufferData.buffer.get(), offset); + + vk::Viewport viewport(0.0f, 0.0f, static_cast(surfaceData.extent.width), static_cast(surfaceData.extent.height), 0.0f, 1.0f); + commandBuffers[0]->setViewport(0, viewport); + + vk::Rect2D scissor(vk::Offset2D(0, 0), surfaceData.extent); + commandBuffers[0]->setScissor(0, scissor); + + commandBuffers[0]->draw(12 * 3, 1, 0, 0); + commandBuffers[0]->endRenderPass(); + commandBuffers[0]->end(); + + vk::UniqueFence drawFence = device->createFenceUnique(vk::FenceCreateInfo()); + + vk::PipelineStageFlags waitDestinationStageMask(vk::PipelineStageFlagBits::eColorAttachmentOutput); + vk::SubmitInfo submitInfo(1, &imageAcquiredSemaphore.get(), &waitDestinationStageMask, 1, &commandBuffers[0].get()); + graphicsQueue.submit(submitInfo, drawFence.get()); + + while (vk::Result::eTimeout == device->waitForFences(drawFence.get(), VK_TRUE, vk::su::FenceTimeout)) + ; + + presentQueue.presentKHR(vk::PresentInfoKHR(0, nullptr, 1, &swapChainData.swapChain.get(), ¤tBuffer.value)); + Sleep(1000); + + device->waitIdle(); +#if defined(VK_USE_PLATFORM_WIN32_KHR) + DestroyWindow(surfaceData.window); +#else +#pragma error "unhandled platform" +#endif + } + catch (vk::SystemError err) + { + std::cout << "vk::SystemError: " << err.what() << std::endl; + exit(-1); + } + catch (std::runtime_error err) + { + std::cout << "std::runtime_error: " << err.what() << std::endl; + exit(-1); + } + catch (...) + { + std::cout << "unknown error\n"; + exit(-1); + } + return 0; +} diff --git a/samples/utils/math.cpp b/samples/utils/math.cpp index 1b9daaf..613a439 100644 --- a/samples/utils/math.cpp +++ b/samples/utils/math.cpp @@ -19,11 +19,17 @@ namespace vk { namespace su { - glm::mat4x4 createModelViewProjectionClipMatrix() + glm::mat4x4 createModelViewProjectionClipMatrix(vk::Extent2D const& extent) { + float fov = glm::radians(45.0f); + if (extent.width > extent.height) + { + fov *= static_cast(extent.height) / static_cast(extent.width); + } + glm::mat4x4 model = glm::mat4x4(1.0f); glm::mat4x4 view = glm::lookAt(glm::vec3(-5.0f, 3.0f, -10.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)); - glm::mat4x4 projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f); + glm::mat4x4 projection = glm::perspective(fov, 1.0f, 0.1f, 100.0f); glm::mat4x4 clip = glm::mat4x4(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 1.0f); // vulkan clip space has inverted y and half z ! return clip * projection * view * model; } diff --git a/samples/utils/math.hpp b/samples/utils/math.hpp index 24f1207..42a726b 100644 --- a/samples/utils/math.hpp +++ b/samples/utils/math.hpp @@ -13,6 +13,7 @@ // limitations under the License. // +#include #define GLM_FORCE_RADIANS #pragma warning(disable:4201) // disable warning C4201: nonstandard extension used: nameless struct/union; needed to get glm/detail/type_vec?.hpp without warnings #include @@ -21,6 +22,6 @@ namespace vk { namespace su { - glm::mat4x4 createModelViewProjectionClipMatrix(); + glm::mat4x4 createModelViewProjectionClipMatrix(vk::Extent2D const& extent); } } diff --git a/samples/utils/utils.cpp b/samples/utils/utils.cpp index 00f614b..40fdd67 100644 --- a/samples/utils/utils.cpp +++ b/samples/utils/utils.cpp @@ -35,21 +35,7 @@ namespace vk { vk::UniqueDeviceMemory allocateMemory(vk::UniqueDevice &device, vk::PhysicalDeviceMemoryProperties const& memoryProperties, vk::MemoryRequirements const& memoryRequirements, vk::MemoryPropertyFlags memoryPropertyFlags) { - uint32_t memoryTypeBits = memoryRequirements.memoryTypeBits; - uint32_t memoryTypeIndex = static_cast(~0); - for (uint32_t i = 0; i < memoryProperties.memoryTypeCount; i++) - { - if ((memoryTypeBits & 1) == 1) - { - if ((memoryProperties.memoryTypes[i].propertyFlags & memoryPropertyFlags) == memoryPropertyFlags) - { - memoryTypeIndex = i; - break; - } - } - memoryTypeBits >>= 1; - } - assert(memoryTypeIndex != ~0); + uint32_t memoryTypeIndex = findMemoryType(memoryProperties, memoryRequirements.memoryTypeBits, memoryPropertyFlags); return device->allocateMemoryUnique(vk::MemoryAllocateInfo(memoryRequirements.size, memoryTypeIndex)); } @@ -106,7 +92,7 @@ namespace vk return physicalDevice.createDeviceUnique(deviceCreateInfo); } - std::vector createFramebuffers(vk::UniqueDevice &device, vk::UniqueRenderPass &renderPass, std::vector const& imageViews, vk::UniqueImageView &depthImageView, vk::Extent2D const& extent) + std::vector createFramebuffers(vk::UniqueDevice &device, vk::UniqueRenderPass &renderPass, std::vector const& imageViews, vk::UniqueImageView const& depthImageView, vk::Extent2D const& extent) { vk::ImageView attachments[2]; attachments[1] = depthImageView.get(); @@ -116,13 +102,13 @@ namespace vk for (auto const& view : imageViews) { attachments[0] = view.get(); - framebuffers.push_back(device->createFramebufferUnique(vk::FramebufferCreateInfo(vk::FramebufferCreateFlags(), renderPass.get(), 2, attachments, extent.width, extent.height, 1))); + framebuffers.push_back(device->createFramebufferUnique(vk::FramebufferCreateInfo(vk::FramebufferCreateFlags(), renderPass.get(), depthImageView ? 2 : 1, attachments, extent.width, extent.height, 1))); } return framebuffers; } - vk::UniquePipeline createGraphicsPipeline(vk::UniqueDevice &device, vk::UniquePipelineCache &pipelineCache, vk::UniqueShaderModule &vertexShaderModule, vk::UniqueShaderModule &fragmentShaderModule, uint32_t vertexStride, vk::UniquePipelineLayout &pipelineLayout, vk::UniqueRenderPass &renderPass) + vk::UniquePipeline createGraphicsPipeline(vk::UniqueDevice &device, vk::UniquePipelineCache &pipelineCache, vk::UniqueShaderModule &vertexShaderModule, vk::UniqueShaderModule &fragmentShaderModule, uint32_t vertexStride, bool depthBuffered, vk::UniquePipelineLayout &pipelineLayout, vk::UniqueRenderPass &renderPass) { vk::PipelineShaderStageCreateInfo pipelineShaderStageCreateInfos[2] = { @@ -154,7 +140,7 @@ namespace vk vk::PipelineMultisampleStateCreateInfo pipelineMultisampleStateCreateInfo; vk::StencilOpState stencilOpState(vk::StencilOp::eKeep, vk::StencilOp::eKeep, vk::StencilOp::eKeep, vk::CompareOp::eAlways); - vk::PipelineDepthStencilStateCreateInfo pipelineDepthStencilStateCreateInfo(vk::PipelineDepthStencilStateCreateFlags(), true, true, vk::CompareOp::eLessOrEqual, false, false, stencilOpState, stencilOpState); + vk::PipelineDepthStencilStateCreateInfo pipelineDepthStencilStateCreateInfo(vk::PipelineDepthStencilStateCreateFlags(), depthBuffered, depthBuffered, vk::CompareOp::eLessOrEqual, false, false, stencilOpState, stencilOpState); vk::ColorComponentFlags colorComponentFlags(vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA); vk::PipelineColorBlendAttachmentState pipelineColorBlendAttachmentState(false, vk::BlendFactor::eZero, vk::BlendFactor::eZero, vk::BlendOp::eAdd, vk::BlendFactor::eZero, vk::BlendFactor::eZero, vk::BlendOp::eAdd, colorComponentFlags); @@ -208,17 +194,21 @@ namespace vk return instance; } - vk::UniqueRenderPass createRenderPass(vk::UniqueDevice &device, vk::Format colorFormat, vk::Format depthFormat) + vk::UniqueRenderPass createRenderPass(vk::UniqueDevice &device, vk::Format colorFormat, vk::Format depthFormat, vk::AttachmentLoadOp loadOp, vk::ImageLayout colorFinalLayout) { - vk::AttachmentDescription attachmentDescriptions[2] = + std::vector attachmentDescriptions; + assert(colorFormat != vk::Format::eUndefined); + attachmentDescriptions.push_back(vk::AttachmentDescription(vk::AttachmentDescriptionFlags(), colorFormat, vk::SampleCountFlagBits::e1, loadOp, vk::AttachmentStoreOp::eStore, + vk::AttachmentLoadOp::eDontCare, vk::AttachmentStoreOp::eDontCare, vk::ImageLayout::eUndefined, colorFinalLayout)); + if (depthFormat != vk::Format::eUndefined) { - vk::AttachmentDescription(vk::AttachmentDescriptionFlags(), colorFormat, vk::SampleCountFlagBits::e1, vk::AttachmentLoadOp::eClear, vk::AttachmentStoreOp::eStore, vk::AttachmentLoadOp::eDontCare, vk::AttachmentStoreOp::eDontCare, vk::ImageLayout::eUndefined, vk::ImageLayout::ePresentSrcKHR), - vk::AttachmentDescription(vk::AttachmentDescriptionFlags(), depthFormat, vk::SampleCountFlagBits::e1, vk::AttachmentLoadOp::eClear, vk::AttachmentStoreOp::eStore, vk::AttachmentLoadOp::eLoad, vk::AttachmentStoreOp::eStore, vk::ImageLayout::eUndefined, vk::ImageLayout::eDepthStencilAttachmentOptimal) - }; + attachmentDescriptions.push_back(vk::AttachmentDescription(vk::AttachmentDescriptionFlags(), depthFormat, vk::SampleCountFlagBits::e1, loadOp, vk::AttachmentStoreOp::eStore, + vk::AttachmentLoadOp::eLoad, vk::AttachmentStoreOp::eStore, vk::ImageLayout::eUndefined, vk::ImageLayout::eDepthStencilAttachmentOptimal)); + } vk::AttachmentReference colorAttachment(0, vk::ImageLayout::eColorAttachmentOptimal); vk::AttachmentReference depthAttachment(1, vk::ImageLayout::eDepthStencilAttachmentOptimal); - vk::SubpassDescription subpassDescription(vk::SubpassDescriptionFlags(), vk::PipelineBindPoint::eGraphics, 0, nullptr, 1, &colorAttachment, nullptr, &depthAttachment); - return device->createRenderPassUnique(vk::RenderPassCreateInfo(vk::RenderPassCreateFlags(), 2, attachmentDescriptions, 1, &subpassDescription)); + vk::SubpassDescription subpassDescription(vk::SubpassDescriptionFlags(), vk::PipelineBindPoint::eGraphics, 0, nullptr, 1, &colorAttachment, nullptr, (depthFormat != vk::Format::eUndefined) ? &depthAttachment : nullptr); + return device->createRenderPassUnique(vk::RenderPassCreateInfo(vk::RenderPassCreateFlags(), static_cast(attachmentDescriptions.size()), attachmentDescriptions.data(), 1, &subpassDescription)); } VkBool32 debugReportCallback(VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT /*objectType*/, uint64_t /*object*/, size_t /*location*/, int32_t /*messageCode*/, const char* /*pLayerPrefix*/, const char* pMessage, void* /*pUserData*/) @@ -425,7 +415,7 @@ namespace vk ImageData::ImageData(vk::PhysicalDevice &physicalDevice, vk::UniqueDevice & device, vk::Format format_, vk::Extent2D const& extent, vk::ImageTiling tiling, vk::ImageUsageFlags usage, vk::ImageLayout initialLayout, vk::MemoryPropertyFlags memoryProperties, vk::ImageAspectFlags aspectMask) : format(format_) { - vk::ImageCreateInfo imageCreateInfo(vk::ImageCreateFlags(), vk::ImageType::e2D, format, vk::Extent3D(extent, 1), 1, 1, vk::SampleCountFlagBits::e1, tiling, usage, vk::SharingMode::eExclusive, 0, nullptr, initialLayout); + vk::ImageCreateInfo imageCreateInfo(vk::ImageCreateFlags(), vk::ImageType::e2D, format, vk::Extent3D(extent, 1), 1, 1, vk::SampleCountFlagBits::e1, tiling, usage | vk::ImageUsageFlagBits::eSampled, vk::SharingMode::eExclusive, 0, nullptr, initialLayout); image = device->createImageUnique(imageCreateInfo); deviceMemory = vk::su::allocateMemory(device, physicalDevice.getMemoryProperties(), device->getImageMemoryRequirements(image.get()), memoryProperties); @@ -495,42 +485,8 @@ namespace vk } } - TextureData::TextureData(vk::PhysicalDevice &physicalDevice, vk::UniqueDevice &device) - : format(vk::Format::eR8G8B8A8Unorm) - , extent(256, 256) + void CheckerboardTextureCreator::operator()(void* data, vk::Extent2D &extent) const { - vk::PhysicalDeviceMemoryProperties memoryProperties = physicalDevice.getMemoryProperties(); - vk::FormatProperties formatProperties = physicalDevice.getFormatProperties(format); - - needsStaging = (formatProperties.linearTilingFeatures & vk::FormatFeatureFlagBits::eSampledImage) != vk::FormatFeatureFlagBits::eSampledImage; - vk::ImageTiling imageTiling; - vk::ImageUsageFlags usageFlags(vk::ImageUsageFlagBits::eSampled); - vk::ImageLayout initialLayout; - if (needsStaging) - { - bufferData = std::make_unique(physicalDevice, device, extent.width * extent.height * 4, vk::BufferUsageFlagBits::eTransferSrc); - imageTiling = vk::ImageTiling::eOptimal; - usageFlags |= vk::ImageUsageFlagBits::eTransferDst; - initialLayout = vk::ImageLayout::eUndefined; - } - else - { - imageTiling = vk::ImageTiling::eLinear; - initialLayout = vk::ImageLayout::ePreinitialized; - } - imageData = std::make_unique(physicalDevice, device, format, extent, imageTiling, usageFlags, initialLayout - , vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostVisible, vk::ImageAspectFlagBits::eColor); - - textureSampler = device->createSamplerUnique(vk::SamplerCreateInfo(vk::SamplerCreateFlags(), vk::Filter::eNearest, vk::Filter::eNearest, vk::SamplerMipmapMode::eNearest, - vk::SamplerAddressMode::eClampToEdge, vk::SamplerAddressMode::eClampToEdge, vk::SamplerAddressMode::eClampToEdge, 0.0f, false, 1.0f, false, vk::CompareOp::eNever, 0.0f, 0.0f - , vk::BorderColor::eFloatOpaqueWhite)); - } - - void TextureData::setCheckerboardTexture(vk::UniqueDevice &device, vk::UniqueCommandBuffer &commandBuffer) - { - void* data = needsStaging - ? device->mapMemory(bufferData->deviceMemory.get(), 0, device->getBufferMemoryRequirements(bufferData->buffer.get()).size) - : device->mapMemory(imageData->deviceMemory.get(), 0, device->getImageMemoryRequirements(imageData->image.get()).size); // Checkerboard of 16x16 pixel squares unsigned char *pImageMemory = static_cast(data); for (uint32_t row = 0; row < extent.height; row++) @@ -545,22 +501,39 @@ namespace vk pImageMemory += 4; } } - device->unmapMemory(needsStaging ? bufferData->deviceMemory.get() : imageData->deviceMemory.get()); + } + TextureData::TextureData(vk::PhysicalDevice &physicalDevice, vk::UniqueDevice &device, vk::ImageUsageFlags usageFlags, vk::FormatFeatureFlags formatFeatureFlags) + : format(vk::Format::eR8G8B8A8Unorm) + , extent(256, 256) + { + vk::PhysicalDeviceMemoryProperties memoryProperties = physicalDevice.getMemoryProperties(); + vk::FormatProperties formatProperties = physicalDevice.getFormatProperties(format); + + formatFeatureFlags |= vk::FormatFeatureFlagBits::eSampledImage; + needsStaging = (formatProperties.linearTilingFeatures & formatFeatureFlags) != formatFeatureFlags; + vk::ImageTiling imageTiling; + vk::ImageLayout initialLayout; + vk::MemoryPropertyFlags requirements; if (needsStaging) { - // Since we're going to blit to the texture image, set its layout to eTransferDstOptimal - vk::su::setImageLayout(commandBuffer, imageData->image.get(), vk::ImageAspectFlagBits::eColor, vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal, vk::PipelineStageFlagBits::eTopOfPipe, vk::PipelineStageFlagBits::eTransfer); - vk::BufferImageCopy copyRegion(0, extent.width, extent.height, vk::ImageSubresourceLayers(vk::ImageAspectFlagBits::eColor, 0, 0, 1), vk::Offset3D(0, 0, 0), vk::Extent3D(extent, 1)); - commandBuffer->copyBufferToImage(bufferData->buffer.get(), imageData->image.get(), vk::ImageLayout::eTransferDstOptimal, copyRegion); - // Set the layout for the texture image from eTransferDstOptimal to SHADER_READ_ONLY - vk::su::setImageLayout(commandBuffer, imageData->image.get(), vk::ImageAspectFlagBits::eColor, vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eShaderReadOnlyOptimal, vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eFragmentShader); + assert((formatProperties.optimalTilingFeatures & formatFeatureFlags) == formatFeatureFlags); + bufferData = std::make_unique(physicalDevice, device, extent.width * extent.height * 4, vk::BufferUsageFlagBits::eTransferSrc); + imageTiling = vk::ImageTiling::eOptimal; + usageFlags |= vk::ImageUsageFlagBits::eTransferDst; + initialLayout = vk::ImageLayout::eUndefined; } else { - // If we can use the linear tiled image as a texture, just do it - vk::su::setImageLayout(commandBuffer, imageData->image.get(), vk::ImageAspectFlagBits::eColor, vk::ImageLayout::ePreinitialized, vk::ImageLayout::eShaderReadOnlyOptimal, vk::PipelineStageFlagBits::eHost, vk::PipelineStageFlagBits::eFragmentShader); + imageTiling = vk::ImageTiling::eLinear; + initialLayout = vk::ImageLayout::ePreinitialized; + requirements = vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostVisible; } + imageData = std::make_unique(physicalDevice, device, format, extent, imageTiling, usageFlags | vk::ImageUsageFlagBits::eSampled, initialLayout, requirements, vk::ImageAspectFlagBits::eColor); + + textureSampler = device->createSamplerUnique(vk::SamplerCreateInfo(vk::SamplerCreateFlags(), vk::Filter::eNearest, vk::Filter::eNearest, vk::SamplerMipmapMode::eNearest, + vk::SamplerAddressMode::eClampToEdge, vk::SamplerAddressMode::eClampToEdge, vk::SamplerAddressMode::eClampToEdge, 0.0f, false, 1.0f, false, vk::CompareOp::eNever, 0.0f, 0.0f + , vk::BorderColor::eFloatOpaqueWhite)); } #if defined(VK_USE_PLATFORM_WIN32_KHR) diff --git a/samples/utils/utils.hpp b/samples/utils/utils.hpp index 82a0a11..156da1f 100644 --- a/samples/utils/utils.hpp +++ b/samples/utils/utils.hpp @@ -65,11 +65,40 @@ namespace vk std::vector imageViews; }; + class CheckerboardTextureCreator + { + public: + void operator()(void* data, vk::Extent2D &extent) const; + }; + struct TextureData { - TextureData(vk::PhysicalDevice &physicalDevice, vk::UniqueDevice &device); + TextureData(vk::PhysicalDevice &physicalDevice, vk::UniqueDevice &device, vk::ImageUsageFlags usageFlags = {}, vk::FormatFeatureFlags formatFeatureFlags = {}); - void setCheckerboardTexture(vk::UniqueDevice &device, vk::UniqueCommandBuffer &commandBuffer); + template + void setTexture(vk::UniqueDevice &device, vk::UniqueCommandBuffer &commandBuffer, TextureCreator const& textureCreator) + { + void* data = needsStaging + ? device->mapMemory(bufferData->deviceMemory.get(), 0, device->getBufferMemoryRequirements(bufferData->buffer.get()).size) + : device->mapMemory(imageData->deviceMemory.get(), 0, device->getImageMemoryRequirements(imageData->image.get()).size); + textureCreator(data, extent); + device->unmapMemory(needsStaging ? bufferData->deviceMemory.get() : imageData->deviceMemory.get()); + + if (needsStaging) + { + // Since we're going to blit to the texture image, set its layout to eTransferDstOptimal + vk::su::setImageLayout(commandBuffer, imageData->image.get(), vk::ImageAspectFlagBits::eColor, vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal, vk::PipelineStageFlagBits::eTopOfPipe, vk::PipelineStageFlagBits::eTransfer); + vk::BufferImageCopy copyRegion(0, extent.width, extent.height, vk::ImageSubresourceLayers(vk::ImageAspectFlagBits::eColor, 0, 0, 1), vk::Offset3D(0, 0, 0), vk::Extent3D(extent, 1)); + commandBuffer->copyBufferToImage(bufferData->buffer.get(), imageData->image.get(), vk::ImageLayout::eTransferDstOptimal, copyRegion); + // Set the layout for the texture image from eTransferDstOptimal to SHADER_READ_ONLY + vk::su::setImageLayout(commandBuffer, imageData->image.get(), vk::ImageAspectFlagBits::eColor, vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eShaderReadOnlyOptimal, vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eFragmentShader); + } + else + { + // If we can use the linear tiled image as a texture, just do it + vk::su::setImageLayout(commandBuffer, imageData->image.get(), vk::ImageAspectFlagBits::eColor, vk::ImageLayout::ePreinitialized, vk::ImageLayout::eShaderReadOnlyOptimal, vk::PipelineStageFlagBits::eHost, vk::PipelineStageFlagBits::eFragmentShader); + } + } vk::Format format; vk::Extent2D extent; @@ -128,10 +157,10 @@ namespace vk vk::UniqueDescriptorPool createDescriptorPool(vk::UniqueDevice &device, vk::DescriptorType descriptorType = vk::DescriptorType::eUniformBuffer, bool textured = false); vk::UniqueDescriptorSetLayout createDescriptorSetLayout(vk::UniqueDevice &device, vk::DescriptorType = vk::DescriptorType::eUniformBuffer, bool textured = false); vk::UniqueDevice createDevice(vk::PhysicalDevice physicalDevice, uint32_t queueFamilyIndex, std::vector const& extensions = {}); - std::vector createFramebuffers(vk::UniqueDevice &device, vk::UniqueRenderPass &renderPass, std::vector const& imageViews, vk::UniqueImageView &depthImageView, vk::Extent2D const& extent); - vk::UniquePipeline createGraphicsPipeline(vk::UniqueDevice &device, vk::UniquePipelineCache &pipelineCache, vk::UniqueShaderModule &vertexShaderModule, vk::UniqueShaderModule &fragmentShaderModule, uint32_t vertexStride, vk::UniquePipelineLayout &pipelineLayout, vk::UniqueRenderPass &renderPass); + std::vector createFramebuffers(vk::UniqueDevice &device, vk::UniqueRenderPass &renderPass, std::vector const& imageViews, vk::UniqueImageView const& depthImageView, vk::Extent2D const& extent); + vk::UniquePipeline createGraphicsPipeline(vk::UniqueDevice &device, vk::UniquePipelineCache &pipelineCache, vk::UniqueShaderModule &vertexShaderModule, vk::UniqueShaderModule &fragmentShaderModule, uint32_t vertexStride, bool depthBuffered, vk::UniquePipelineLayout &pipelineLayout, vk::UniqueRenderPass &renderPass); vk::UniqueInstance createInstance(std::string const& appName, std::string const& engineName, std::vector const& extensions = {}, uint32_t apiVersion = VK_API_VERSION_1_0); - vk::UniqueRenderPass createRenderPass(vk::UniqueDevice &device, vk::Format colorFormat, vk::Format depthFormat); + vk::UniqueRenderPass createRenderPass(vk::UniqueDevice &device, vk::Format colorFormat, vk::Format depthFormat, vk::AttachmentLoadOp loadOp = vk::AttachmentLoadOp::eClear, vk::ImageLayout colorFinalLayout = vk::ImageLayout::ePresentSrcKHR); VkBool32 debugReportCallback(VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT objectType, uint64_t object, size_t location, int32_t messageCode, const char* pLayerPrefix, const char* pMessage, void* pUserData); uint32_t findGraphicsQueueFamilyIndex(std::vector const& queueFamilyProperties); std::pair findGraphicsAndPresentQueueFamilyIndex(vk::PhysicalDevice physicalDevice, vk::UniqueSurfaceKHR & surface);