Commit 1c29477a by Nicolas Capens Committed by Nicolas Capens

Don't expose the sampling routine cache's mutex

The getOrCreate() method takes care of the mutex locking and unlocking, and takes the function for creating the routine as a callback argument. Bug: b/131246679 Change-Id: I0873f6be94c92a11181ce575d1b44ed040177d43 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/42869 Presubmit-Ready: Nicolas Capens <nicolascapens@google.com> Kokoro-Presubmit: kokoro <noreply+kokoro@google.com> Tested-by: 's avatarNicolas Capens <nicolascapens@google.com> Reviewed-by: 's avatarBen Clayton <bclayton@google.com>
parent 13943bad
...@@ -45,58 +45,54 @@ SpirvShader::ImageSampler *SpirvShader::getImageSampler(uint32_t inst, vk::Sampl ...@@ -45,58 +45,54 @@ SpirvShader::ImageSampler *SpirvShader::getImageSampler(uint32_t inst, vk::Sampl
return (ImageSampler *)(routine->getEntry()); return (ImageSampler *)(routine->getEntry());
} }
std::unique_lock<std::mutex> lock(imageDescriptor->device->getSamplingRoutineCacheMutex());
vk::Device::SamplingRoutineCache *cache = imageDescriptor->device->getSamplingRoutineCache(); vk::Device::SamplingRoutineCache *cache = imageDescriptor->device->getSamplingRoutineCache();
auto routine = cache->query(key); auto createSamplingRoutine = [&](const vk::Device::SamplingRoutineCache::Key &key) {
if(routine) auto type = imageDescriptor->type;
{
return (ImageSampler *)(routine->getEntry());
}
auto type = imageDescriptor->type; Sampler samplerState = {};
samplerState.textureType = type;
samplerState.textureFormat = imageDescriptor->format;
Sampler samplerState = {}; samplerState.addressingModeU = convertAddressingMode(0, sampler, type);
samplerState.textureType = type; samplerState.addressingModeV = convertAddressingMode(1, sampler, type);
samplerState.textureFormat = imageDescriptor->format; samplerState.addressingModeW = convertAddressingMode(2, sampler, type);
samplerState.addressingModeY = convertAddressingMode(3, sampler, type);
samplerState.addressingModeU = convertAddressingMode(0, sampler, type); samplerState.mipmapFilter = convertMipmapMode(sampler);
samplerState.addressingModeV = convertAddressingMode(1, sampler, type); samplerState.swizzle = imageDescriptor->swizzle;
samplerState.addressingModeW = convertAddressingMode(2, sampler, type); samplerState.gatherComponent = instruction.gatherComponent;
samplerState.addressingModeY = convertAddressingMode(3, sampler, type); samplerState.highPrecisionFiltering = false;
samplerState.largeTexture = (imageDescriptor->extent.width > SHRT_MAX) ||
samplerState.mipmapFilter = convertMipmapMode(sampler); (imageDescriptor->extent.height > SHRT_MAX) ||
samplerState.swizzle = imageDescriptor->swizzle; (imageDescriptor->extent.depth > SHRT_MAX);
samplerState.gatherComponent = instruction.gatherComponent;
samplerState.highPrecisionFiltering = false; if(sampler)
samplerState.largeTexture = (imageDescriptor->extent.width > SHRT_MAX) || {
(imageDescriptor->extent.height > SHRT_MAX) || samplerState.textureFilter = (instruction.samplerMethod == Gather) ? FILTER_GATHER : convertFilterMode(sampler);
(imageDescriptor->extent.depth > SHRT_MAX); samplerState.border = sampler->borderColor;
if(sampler) samplerState.mipmapFilter = convertMipmapMode(sampler);
{
samplerState.textureFilter = (instruction.samplerMethod == Gather) ? FILTER_GATHER : convertFilterMode(sampler);
samplerState.border = sampler->borderColor;
samplerState.mipmapFilter = convertMipmapMode(sampler); samplerState.compareEnable = (sampler->compareEnable != VK_FALSE);
samplerState.compareOp = sampler->compareOp;
samplerState.unnormalizedCoordinates = (sampler->unnormalizedCoordinates != VK_FALSE);
samplerState.compareEnable = (sampler->compareEnable != VK_FALSE); samplerState.ycbcrModel = sampler->ycbcrModel;
samplerState.compareOp = sampler->compareOp; samplerState.studioSwing = sampler->studioSwing;
samplerState.unnormalizedCoordinates = (sampler->unnormalizedCoordinates != VK_FALSE); samplerState.swappedChroma = sampler->swappedChroma;
samplerState.ycbcrModel = sampler->ycbcrModel; samplerState.mipLodBias = sampler->mipLodBias;
samplerState.studioSwing = sampler->studioSwing; samplerState.maxAnisotropy = sampler->maxAnisotropy;
samplerState.swappedChroma = sampler->swappedChroma; samplerState.minLod = sampler->minLod;
samplerState.maxLod = sampler->maxLod;
}
samplerState.mipLodBias = sampler->mipLodBias; return emitSamplerRoutine(instruction, samplerState);
samplerState.maxAnisotropy = sampler->maxAnisotropy; };
samplerState.minLod = sampler->minLod;
samplerState.maxLod = sampler->maxLod;
}
routine = emitSamplerRoutine(instruction, samplerState); auto routine = cache->getOrCreate(key, createSamplingRoutine);
cache->add(key, routine);
return (ImageSampler *)(routine->getEntry()); return (ImageSampler *)(routine->getEntry());
} }
......
...@@ -14,11 +14,6 @@ ...@@ -14,11 +14,6 @@
#include "Debug.hpp" #include "Debug.hpp"
#include <atomic>
#include <cstdarg>
#include <cstdio>
#include <string>
#if __ANDROID__ #if __ANDROID__
# include <android/log.h> # include <android/log.h>
#endif #endif
...@@ -34,6 +29,11 @@ ...@@ -34,6 +29,11 @@
# include <unistd.h> # include <unistd.h>
#endif #endif
#include <atomic>
#include <cstdarg>
#include <cstdio>
#include <string>
#ifdef ERROR #ifdef ERROR
# undef ERROR // b/127920555 # undef ERROR // b/127920555
#endif #endif
......
...@@ -38,17 +38,6 @@ std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> now ...@@ -38,17 +38,6 @@ std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> now
namespace vk { namespace vk {
std::shared_ptr<rr::Routine> Device::SamplingRoutineCache::query(const vk::Device::SamplingRoutineCache::Key &key) const
{
return cache.query(key);
}
void Device::SamplingRoutineCache::add(const vk::Device::SamplingRoutineCache::Key &key, const std::shared_ptr<rr::Routine> &routine)
{
ASSERT(routine);
cache.add(key, routine);
}
rr::Routine *Device::SamplingRoutineCache::querySnapshot(const vk::Device::SamplingRoutineCache::Key &key) const rr::Routine *Device::SamplingRoutineCache::querySnapshot(const vk::Device::SamplingRoutineCache::Key &key) const
{ {
return cache.querySnapshot(key).get(); return cache.querySnapshot(key).get();
...@@ -56,6 +45,8 @@ rr::Routine *Device::SamplingRoutineCache::querySnapshot(const vk::Device::Sampl ...@@ -56,6 +45,8 @@ rr::Routine *Device::SamplingRoutineCache::querySnapshot(const vk::Device::Sampl
void Device::SamplingRoutineCache::updateSnapshot() void Device::SamplingRoutineCache::updateSnapshot()
{ {
std::lock_guard<std::mutex> lock(mutex);
cache.updateSnapshot(); cache.updateSnapshot();
} }
...@@ -309,15 +300,9 @@ rr::Routine *Device::querySnapshotCache(const SamplingRoutineCache::Key &key) co ...@@ -309,15 +300,9 @@ rr::Routine *Device::querySnapshotCache(const SamplingRoutineCache::Key &key) co
void Device::updateSamplingRoutineSnapshotCache() void Device::updateSamplingRoutineSnapshotCache()
{ {
std::unique_lock<std::mutex> lock(samplingRoutineCacheMutex);
samplingRoutineCache->updateSnapshot(); samplingRoutineCache->updateSnapshot();
} }
std::mutex &Device::getSamplingRoutineCacheMutex()
{
return samplingRoutineCacheMutex;
}
uint32_t Device::indexSampler(const SamplerState &samplerState) uint32_t Device::indexSampler(const SamplerState &samplerState)
{ {
return samplerIndexer->index(samplerState); return samplerIndexer->index(samplerState);
......
...@@ -86,18 +86,31 @@ public: ...@@ -86,18 +86,31 @@ public:
}; };
}; };
std::shared_ptr<rr::Routine> query(const Key &key) const; template<typename Function>
void add(const Key &key, const std::shared_ptr<rr::Routine> &routine); std::shared_ptr<rr::Routine> getOrCreate(const Key &key, Function createRoutine)
{
std::lock_guard<std::mutex> lock(mutex);
if(auto existingRoutine = cache.query(key))
{
return existingRoutine;
}
std::shared_ptr<rr::Routine> newRoutine = createRoutine(key);
cache.add(key, newRoutine);
return newRoutine;
}
rr::Routine *querySnapshot(const Key &key) const; rr::Routine *querySnapshot(const Key &key) const;
void updateSnapshot(); void updateSnapshot();
private: private:
sw::LRUSnapshotCache<Key, std::shared_ptr<rr::Routine>, Key::Hash> cache; sw::LRUSnapshotCache<Key, std::shared_ptr<rr::Routine>, Key::Hash> cache; // guarded by mutex
std::mutex mutex;
}; };
SamplingRoutineCache *getSamplingRoutineCache() const; SamplingRoutineCache *getSamplingRoutineCache() const;
std::mutex &getSamplingRoutineCacheMutex();
rr::Routine *querySnapshotCache(const SamplingRoutineCache::Key &key) const; rr::Routine *querySnapshotCache(const SamplingRoutineCache::Key &key) const;
void updateSamplingRoutineSnapshotCache(); void updateSamplingRoutineSnapshotCache();
...@@ -139,14 +152,13 @@ private: ...@@ -139,14 +152,13 @@ private:
Queue *const queues = nullptr; Queue *const queues = nullptr;
uint32_t queueCount = 0; uint32_t queueCount = 0;
std::unique_ptr<sw::Blitter> blitter; std::unique_ptr<sw::Blitter> blitter;
std::unique_ptr<SamplingRoutineCache> samplingRoutineCache;
std::mutex samplingRoutineCacheMutex;
uint32_t enabledExtensionCount = 0; uint32_t enabledExtensionCount = 0;
typedef char ExtensionName[VK_MAX_EXTENSION_NAME_SIZE]; typedef char ExtensionName[VK_MAX_EXTENSION_NAME_SIZE];
ExtensionName *extensions = nullptr; ExtensionName *extensions = nullptr;
const VkPhysicalDeviceFeatures enabledFeatures = {}; const VkPhysicalDeviceFeatures enabledFeatures = {};
std::shared_ptr<marl::Scheduler> scheduler; std::shared_ptr<marl::Scheduler> scheduler;
std::unique_ptr<SamplingRoutineCache> samplingRoutineCache;
std::unique_ptr<SamplerIndexer> samplerIndexer; std::unique_ptr<SamplerIndexer> samplerIndexer;
#ifdef ENABLE_VK_DEBUGGER #ifdef ENABLE_VK_DEBUGGER
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment