OSDN Git Service

VkCreateSwapchainKHR and VkDestroySwapchainKHR
authorHernan Liatis <hliatis@google.com>
Tue, 26 Feb 2019 03:29:54 +0000 (19:29 -0800)
committerHernan Liatis <hliatis@google.com>
Mon, 4 Mar 2019 19:27:21 +0000 (19:27 +0000)
Bare bones implementation of a creatable and
destroyable swapchain

Bug: b/124265819
Change-Id: Ie8277184863ab6b7204b6c8f6fc2b2f86ad787c9
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/25509
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Tested-by: Hernan Liatis <hliatis@google.com>
CMakeLists.txt
build/Visual Studio 15 2017 Win64/libvk_swiftshader.vcxproj
src/Vulkan/VkDestroy.h
src/Vulkan/VkGetProcAddress.cpp
src/Vulkan/libVulkan.cpp
src/Vulkan/vulkan.vcxproj
src/WSI/VkSwapchainKHR.cpp [new file with mode: 0644]
src/WSI/VkSwapchainKHR.hpp [new file with mode: 0644]

index 4a9640c..965f7dd 100644 (file)
@@ -2061,6 +2061,8 @@ file(GLOB_RECURSE VULKAN_LIST
     ${SOURCE_DIR}/Pipeline/*.hpp
     ${SOURCE_DIR}/WSI/VkSurfaceKHR.cpp
     ${SOURCE_DIR}/WSI/VkSurfaceKHR.hpp
+    ${SOURCE_DIR}/WSI/VkSwapchainKHR.cpp
+    ${SOURCE_DIR}/WSI/VkSwapchainKHR.hpp
     ${CMAKE_CURRENT_SOURCE_DIR}/include/vulkan/*.h}
 )
 
index 0808ce4..9052ac6 100644 (file)
@@ -318,6 +318,10 @@ if %errorlevel% neq 0 goto :VCEnd</Command>
     <ClInclude Include="$(SolutionDir)src\Pipeline\SpirvShader.hpp" />\r
     <ClInclude Include="$(SolutionDir)src\Pipeline\VertexProgram.hpp" />\r
     <ClInclude Include="$(SolutionDir)src\Pipeline\VertexRoutine.hpp" />\r
+    <ClCompile Include="$(SolutionDir)src\WSI\VkSurfaceKHR.cpp" />\r
+    <ClInclude Include="$(SolutionDir)src\WSI\VkSurfaceKHR.hpp" />\r
+    <ClCompile Include="$(SolutionDir)src\WSI\VkSwapchainKHR.cpp" />\r
+    <ClInclude Include="$(SolutionDir)src\WSI\VkSwapchainKHR.hpp" />\r
   </ItemGroup>\r
   <ItemGroup>\r
     <ProjectReference Include="$(SolutionDir)build\Visual Studio 15 2017 Win64\ReactorLLVM.vcxproj">\r
index 2bdad4e..a8367c6 100644 (file)
@@ -35,6 +35,7 @@
 #include "VkShaderModule.hpp"
 #include "VkRenderPass.hpp"
 #include "WSI/VkSurfaceKHR.hpp"
+#include "WSI/VkSwapchainKHR.hpp"
 
 namespace vk
 {
index 1c2ddd6..059dddb 100644 (file)
@@ -248,6 +248,8 @@ static const std::unordered_map<std::string, PFN_vkVoidFunction> deviceFunctionP
        MAKE_VULKAN_DEVICE_ENTRY(vkGetImageSparseMemoryRequirements2KHR),
        // VK_KHR_maintenance3
        MAKE_VULKAN_DEVICE_ENTRY(vkGetDescriptorSetLayoutSupportKHR),
+       MAKE_VULKAN_DEVICE_ENTRY(vkCreateSwapchainKHR),
+       MAKE_VULKAN_DEVICE_ENTRY(vkDestroySwapchainKHR),
 };
 #undef MAKE_VULKAN_DEVICE_ENTRY
 
index 1caab01..5220f22 100644 (file)
@@ -45,6 +45,8 @@
 #include "WSI/XlibSurfaceKHR.hpp"
 #endif
 
+#include "WSI/VkSwapchainKHR.hpp"
+
 #include <algorithm>
 #include <cstring>
 #include <string>
@@ -1238,9 +1240,9 @@ VKAPI_ATTR void VKAPI_CALL vkDestroyRenderPass(VkDevice device, VkRenderPass ren
 
 VKAPI_ATTR void VKAPI_CALL vkGetRenderAreaGranularity(VkDevice device, VkRenderPass renderPass, VkExtent2D* pGranularity)
 {
-       TRACE("(VkDevice device = 0x%X, VkRenderPass renderPass = 0x%X, VkExtent2D* pGranularity = 0x%X)",\r
-             device, renderPass, pGranularity);\r
-\r
+       TRACE("(VkDevice device = 0x%X, VkRenderPass renderPass = 0x%X, VkExtent2D* pGranularity = 0x%X)",
+             device, renderPass, pGranularity);
+
        vk::Cast(renderPass)->getRenderAreaGranularity(pGranularity);
 }
 
@@ -2141,4 +2143,35 @@ VKAPI_ATTR VkResult VKAPI_CALL vkGetPhysicalDeviceSurfacePresentModesKHR(VkPhysi
        return vk::Cast(surface)->getPresentModes(pPresentModeCount, pPresentModes);
 }
 
+VKAPI_ATTR VkResult VKAPI_CALL vkCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain)
+{
+       TRACE("(VkDevice device = 0x%X, const VkSwapchainCreateInfoKHR* pCreateInfo = 0x%X, const VkAllocationCallbacks* pAllocator = 0x%X, VkSwapchainKHR* pSwapchain = 0x%X)",
+                       device, pCreateInfo, pAllocator, pSwapchain);
+
+       VkResult status = vk::SwapchainKHR::Create(pAllocator, pCreateInfo, pSwapchain);
+
+       if(status != VK_SUCCESS)
+       {
+               return status;
+       }
+
+       status = vk::Cast(*pSwapchain)->createImages(device);
+
+       if(status != VK_SUCCESS)
+       {
+               vk::destroy(*pSwapchain, pAllocator);
+               return status;
+       }
+
+       return VK_SUCCESS;
+}
+
+VKAPI_ATTR void VKAPI_CALL vkDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks* pAllocator)
+{
+       TRACE("(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks* pAllocator)",
+                       device, swapchain, pAllocator);
+
+       vk::destroy(swapchain, pAllocator);
+}
+
 }
index ec6a1ae..65dad9e 100644 (file)
@@ -169,6 +169,7 @@ copy "$(OutDir)vk_swiftshader.dll" "$(SolutionDir)out\$(Configuration)_$(Platfor
     <ClCompile Include="..\System\Thread.cpp" />\r
     <ClCompile Include="..\System\Timer.cpp" />\r
     <ClCompile Include="..\WSI\VkSurfaceKHR.cpp" />\r
+    <ClCompile Include="..\WSI\VkSwapchainKHR.cpp" />\r
     <ClCompile Include="..\WSI\FrameBuffer.cpp" />\r
     <ClCompile Include="..\WSI\FrameBufferAndroid.cpp">\r
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>\r
@@ -281,6 +282,7 @@ copy "$(OutDir)vk_swiftshader.dll" "$(SolutionDir)out\$(Configuration)_$(Platfor
     <ClInclude Include="..\System\Timer.hpp" />\r
     <ClInclude Include="..\System\Types.hpp" />\r
     <ClInclude Include="..\WSI\VkSurfaceKHR.hpp" />\r
+    <ClInclude Include="..\WSI\VkSwapchainKHR.hpp" />\r
     <ClInclude Include="..\WSI\FrameBuffer.hpp" />\r
     <ClInclude Include="..\WSI\FrameBufferAndroid.hpp">\r
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>\r
diff --git a/src/WSI/VkSwapchainKHR.cpp b/src/WSI/VkSwapchainKHR.cpp
new file mode 100644 (file)
index 0000000..43cfc12
--- /dev/null
@@ -0,0 +1,123 @@
+// Copyright 2019 The SwiftShader Authors. 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.
+
+#include "VkSwapchainKHR.hpp"
+
+#include "Vulkan/VkImage.hpp"
+#include "Vulkan/VkDeviceMemory.hpp"
+#include "Vulkan/VkDestroy.h"
+
+namespace vk
+{
+
+SwapchainKHR::SwapchainKHR(const VkSwapchainCreateInfoKHR *pCreateInfo, void *mem) :
+       createInfo(*pCreateInfo),
+       imageCount(0)
+{
+       images.resize(pCreateInfo->minImageCount);
+       resetImages();
+}
+
+void SwapchainKHR::destroy(const VkAllocationCallbacks *pAllocator)
+{
+       for(auto& currentImage : images)
+       {
+               if (currentImage.imageStatus != NONEXISTENT)
+               {
+                       vk::destroy(currentImage.imageMemory, pAllocator);
+                       vk::destroy(currentImage.image, pAllocator);
+
+                       currentImage.imageStatus = NONEXISTENT;
+               }
+       }
+}
+
+size_t SwapchainKHR::ComputeRequiredAllocationSize(const VkSwapchainCreateInfoKHR *pCreateInfo)
+{
+       return 0;
+}
+
+void SwapchainKHR::resetImages()
+{
+       for(auto& currentImage : images)
+       {
+               currentImage.image = VK_NULL_HANDLE;
+               currentImage.imageMemory = VK_NULL_HANDLE;
+               currentImage.imageStatus = NONEXISTENT;
+       }
+}
+
+VkResult SwapchainKHR::createImages(VkDevice device)
+{
+       resetImages();
+
+       VkImageCreateInfo imageInfo = {};
+       imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
+
+       if(createInfo.flags & VK_SWAPCHAIN_CREATE_SPLIT_INSTANCE_BIND_REGIONS_BIT_KHR)
+       {
+               imageInfo.flags |= VK_IMAGE_CREATE_SPLIT_INSTANCE_BIND_REGIONS_BIT;
+       }
+
+       if(createInfo.flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR)
+       {
+               imageInfo.flags |= VK_IMAGE_CREATE_PROTECTED_BIT;
+       }
+
+       imageInfo.imageType = VK_IMAGE_TYPE_2D;
+       imageInfo.format = createInfo.imageFormat;
+       imageInfo.extent.height = createInfo.imageExtent.height;
+       imageInfo.extent.width = createInfo.imageExtent.width;
+       imageInfo.extent.depth = 1;
+       imageInfo.mipLevels = 1;
+       imageInfo.arrayLayers = createInfo.imageArrayLayers;
+       imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
+       imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
+       imageInfo.usage = createInfo.imageUsage;
+       imageInfo.sharingMode = createInfo.imageSharingMode;
+       imageInfo.pQueueFamilyIndices = createInfo.pQueueFamilyIndices;
+       imageInfo.queueFamilyIndexCount = createInfo.queueFamilyIndexCount;
+       imageInfo.initialLayout = VK_IMAGE_LAYOUT_GENERAL;
+
+       VkResult status;
+       for(auto& currentImage : images)
+       {
+               status = vkCreateImage(device, &imageInfo, nullptr, &currentImage.image);
+               if(status != VK_SUCCESS)
+               {
+                       return status;
+               }
+
+               VkMemoryRequirements memRequirements = vk::Cast(currentImage.image)->getMemoryRequirements();
+
+               VkMemoryAllocateInfo allocInfo = {};
+               allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
+               allocInfo.allocationSize = memRequirements.size;
+               allocInfo.memoryTypeIndex = 0;
+
+               status = vkAllocateMemory(device, &allocInfo, nullptr, &currentImage.imageMemory);
+               if(status != VK_SUCCESS)
+               {
+                       return status;
+               }
+
+               vkBindImageMemory(device, currentImage.image, currentImage.imageMemory, 0);
+
+               currentImage.imageStatus = AVAILABLE;
+       }
+
+       return VK_SUCCESS;
+}
+
+}
\ No newline at end of file
diff --git a/src/WSI/VkSwapchainKHR.hpp b/src/WSI/VkSwapchainKHR.hpp
new file mode 100644 (file)
index 0000000..96f9d2a
--- /dev/null
@@ -0,0 +1,67 @@
+// Copyright 2019 The SwiftShader Authors. 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.
+
+#ifndef SWIFTSHADER_VKSWAPCHAINKHR_HPP
+#define SWIFTSHADER_VKSWAPCHAINKHR_HPP
+
+#include "Vulkan/VkObject.hpp"
+
+#include <vector>
+
+namespace vk
+{
+
+enum PresentImageStatus
+{
+       NONEXISTENT, //  Image wasn't made
+       AVAILABLE,
+       DRAWING,
+       PRESENTING,
+};
+
+struct PresentImage
+{
+       VkImage image;
+       VkDeviceMemory imageMemory;
+       PresentImageStatus imageStatus;
+};
+
+class SwapchainKHR : public Object<SwapchainKHR, VkSwapchainKHR>
+{
+public:
+       SwapchainKHR(const VkSwapchainCreateInfoKHR* pCreateInfo, void* mem);
+       ~SwapchainKHR() = delete;
+
+       void destroy(const VkAllocationCallbacks* pAllocator);
+
+       static size_t ComputeRequiredAllocationSize(const VkSwapchainCreateInfoKHR* pCreateInfo);
+
+       VkResult createImages(VkDevice device);
+
+private:
+       VkSwapchainCreateInfoKHR createInfo;
+       std::vector<PresentImage> images;
+       uint32_t imageCount;
+
+       void resetImages();
+};
+
+static inline SwapchainKHR* Cast(VkSwapchainKHR object)
+{
+       return reinterpret_cast<SwapchainKHR*>(object);
+}
+
+}
+
+#endif //SWIFTSHADER_VKSWAPCHAINKHR_HPP