SpirvShaderSampling: Fix flaky tests.
The cache keys were colliding, causing mayhem.
Add 32-bit unique keys to ImageViews and Samplers, combine them into a unique 64 bit key.
Bug: b/131246679
Change-Id: I74c86a80ee5fec1c97eda720bd0797bff4a1a7e0
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/29934
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
Reviewed-by: Chris Forbes <chrisforbes@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Tested-by: Ben Clayton <bclayton@google.com>
diff --git a/build/Visual Studio 15 2017 Win64/libvk_swiftshader.vcxproj b/build/Visual Studio 15 2017 Win64/libvk_swiftshader.vcxproj
index a27ae0c..11e83a8 100644
--- a/build/Visual Studio 15 2017 Win64/libvk_swiftshader.vcxproj
+++ b/build/Visual Studio 15 2017 Win64/libvk_swiftshader.vcxproj
@@ -322,6 +322,7 @@
<ClInclude Include="$(SolutionDir)src\Vulkan\VkQueue.hpp" />
<ClCompile Include="$(SolutionDir)src\Vulkan\VkRenderPass.cpp" />
<ClInclude Include="$(SolutionDir)src\Vulkan\VkRenderPass.hpp" />
+ <ClInclude Include="$(SolutionDir)src\Vulkan\VkSampler.cpp" />
<ClInclude Include="$(SolutionDir)src\Vulkan\VkSampler.hpp" />
<ClInclude Include="$(SolutionDir)src\Vulkan\VkSemaphore.hpp" />
<ClCompile Include="$(SolutionDir)src\Vulkan\VkShaderModule.cpp" />
diff --git a/build/Visual Studio 15 2017 Win64/libvk_swiftshader.vcxproj.filters b/build/Visual Studio 15 2017 Win64/libvk_swiftshader.vcxproj.filters
index bc7a75c..532901d 100644
--- a/build/Visual Studio 15 2017 Win64/libvk_swiftshader.vcxproj.filters
+++ b/build/Visual Studio 15 2017 Win64/libvk_swiftshader.vcxproj.filters
@@ -190,6 +190,9 @@
<ClCompile Include="$(SolutionDir)src\Vulkan\VkRenderPass.cpp">
<Filter>src\Vulkan</Filter>
</ClCompile>
+ <ClCompile Include="$(SolutionDir)src\Vulkan\VkSampler.cpp">
+ <Filter>src\Vulkan</Filter>
+ </ClCompile>
<ClCompile Include="$(SolutionDir)src\Vulkan\VkShaderModule.cpp">
<Filter>src\Vulkan</Filter>
</ClCompile>
diff --git a/src/Pipeline/SpirvShaderSampling.cpp b/src/Pipeline/SpirvShaderSampling.cpp
index 5d978d2..ea4a802 100644
--- a/src/Pipeline/SpirvShaderSampling.cpp
+++ b/src/Pipeline/SpirvShaderSampling.cpp
@@ -51,12 +51,11 @@
SpirvShader::ImageSampler *SpirvShader::getImageSampler(SamplerMethod samplerMethod, const vk::ImageView *imageView, const vk::Sampler *sampler)
{
// TODO(b/129523279): Move somewhere sensible.
- static std::unordered_map<uintptr_t, ImageSampler*> cache;
+ static std::unordered_map<uint64_t, ImageSampler*> cache;
static std::mutex mutex;
- // FIXME(b/129523279): Don't use pointers: they can be deleted and reused. Instead combine some two unique ids.
// FIXME(b/129523279): Take instruction opcode and optional parameters into acount (SamplerMethod / SamplerOption).
- auto key = reinterpret_cast<uintptr_t>(imageView) ^ reinterpret_cast<uintptr_t>(sampler);
+ auto key = (static_cast<uint64_t>(imageView->id) << 32) | static_cast<uint64_t>(sampler->id);
std::unique_lock<std::mutex> lock(mutex);
auto it = cache.find(key);
@@ -79,7 +78,7 @@
const vk::ImageView *imageView, const vk::Sampler *sampler,
Pointer<Byte> image, Pointer<SIMD::Float> in, Pointer<Byte> out, Pointer<Byte> constants)
{
- Sampler::State samplerState;
+ Sampler::State samplerState = {};
samplerState.textureType = convertTextureType(imageView->getType());
samplerState.textureFormat = imageView->getFormat();
samplerState.textureFilter = convertFilterMode(sampler);
diff --git a/src/Vulkan/VkImageView.cpp b/src/Vulkan/VkImageView.cpp
index 6104adb..65e1245 100644
--- a/src/Vulkan/VkImageView.cpp
+++ b/src/Vulkan/VkImageView.cpp
@@ -42,6 +42,8 @@
namespace vk
{
+std::atomic<uint32_t> ImageView::nextID(1);
+
ImageView::ImageView(const VkImageViewCreateInfo* pCreateInfo, void* mem) :
image(Cast(pCreateInfo->image)), viewType(pCreateInfo->viewType), format(pCreateInfo->format),
components(ResolveIdentityMapping(pCreateInfo->components)),
diff --git a/src/Vulkan/VkImageView.hpp b/src/Vulkan/VkImageView.hpp
index 048af64..96d7760 100644
--- a/src/Vulkan/VkImageView.hpp
+++ b/src/Vulkan/VkImageView.hpp
@@ -20,6 +20,8 @@
#include "VkObject.hpp"
#include "VkImage.hpp"
+#include <atomic>
+
namespace vk
{
@@ -52,7 +54,10 @@
const VkImageSubresourceRange &getSubresourceRange() const { return subresourceRange; }
size_t getImageSizeInBytes() const { return image->getMemoryRequirements().size; }
+ const uint32_t id = nextID++;
private:
+ static std::atomic<uint32_t> nextID;
+
bool imageTypesMatch(VkImageType imageType) const;
Image *const image = nullptr;
diff --git a/src/Vulkan/VkSampler.cpp b/src/Vulkan/VkSampler.cpp
new file mode 100644
index 0000000..d80f699
--- /dev/null
+++ b/src/Vulkan/VkSampler.cpp
@@ -0,0 +1,23 @@
+// 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 "VkSampler.hpp"
+
+namespace vk
+{
+
+std::atomic<uint32_t> Sampler::nextID(1);
+
+} // namespace vk
diff --git a/src/Vulkan/VkSampler.hpp b/src/Vulkan/VkSampler.hpp
index 5fa0b4e..09acf4b 100644
--- a/src/Vulkan/VkSampler.hpp
+++ b/src/Vulkan/VkSampler.hpp
@@ -17,6 +17,8 @@
#include "VkDevice.hpp"
+#include <atomic>
+
namespace vk
{
@@ -49,6 +51,7 @@
return 0;
}
+ const uint32_t id = nextID++;
const VkFilter magFilter = VK_FILTER_NEAREST;
const VkFilter minFilter = VK_FILTER_NEAREST;
const VkSamplerMipmapMode mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;
@@ -64,6 +67,9 @@
const float maxLod = 0.0f;
const VkBorderColor borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
const VkBool32 unnormalizedCoordinates = VK_FALSE;
+
+private:
+ static std::atomic<uint32_t> nextID;
};
static inline Sampler* Cast(VkSampler object)
diff --git a/src/Vulkan/vulkan.vcxproj b/src/Vulkan/vulkan.vcxproj
index fa3a517..53aa1b9 100644
--- a/src/Vulkan/vulkan.vcxproj
+++ b/src/Vulkan/vulkan.vcxproj
@@ -134,6 +134,7 @@
<ClCompile Include="VkQueryPool.cpp" />
<ClCompile Include="VkQueue.cpp" />
<ClCompile Include="VkRenderPass.cpp" />
+ <ClCompile Include="VkSampler.cpp" />
<ClCompile Include="VkShaderModule.cpp" />
<ClCompile Include="..\Device\Blitter.cpp" />
<ClCompile Include="..\Device\Clipper.cpp" />
diff --git a/src/Vulkan/vulkan.vcxproj.filters b/src/Vulkan/vulkan.vcxproj.filters
index c95a986..f43aabe 100644
--- a/src/Vulkan/vulkan.vcxproj.filters
+++ b/src/Vulkan/vulkan.vcxproj.filters
@@ -246,6 +246,9 @@
<ClCompile Include="VkRenderPass.cpp">
<Filter>Source Files\Vulkan</Filter>
</ClCompile>
+ <ClCompile Include="VkSampler.cpp">
+ <Filter>Source Files\Vulkan</Filter>
+ </ClCompile>
<ClCompile Include="VkShaderModule.cpp">
<Filter>Source Files\Vulkan</Filter>
</ClCompile>