1 /* 2 * Copyright 2016 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef GrVkSampler_DEFINED 9 #define GrVkSampler_DEFINED 10 11 #include "include/gpu/vk/GrVkTypes.h" 12 #include "src/core/SkOpts.h" 13 #include "src/gpu/vk/GrVkManagedResource.h" 14 #include "src/gpu/vk/GrVkSamplerYcbcrConversion.h" 15 16 #include <atomic> 17 #include <cinttypes> 18 19 class GrSamplerState; 20 class GrVkGpu; 21 22 class GrVkSampler : public GrVkManagedResource { 23 public: 24 static GrVkSampler* Create(GrVkGpu* gpu, GrSamplerState, const GrVkYcbcrConversionInfo&); 25 sampler()26 VkSampler sampler() const { return fSampler; } samplerPtr()27 const VkSampler* samplerPtr() const { return &fSampler; } 28 29 struct Key { KeyKey30 Key(uint8_t samplerKey, const GrVkSamplerYcbcrConversion::Key& ycbcrKey) { 31 // We must memset here since the GrVkSamplerYcbcrConversion has a 64 bit value which may 32 // force alignment padding to occur in the middle of the Key struct. 33 memset(this, 0, sizeof(Key)); 34 fSamplerKey = samplerKey; 35 fYcbcrKey = ycbcrKey; 36 } 37 uint8_t fSamplerKey; 38 GrVkSamplerYcbcrConversion::Key fYcbcrKey; 39 40 bool operator==(const Key& that) const { 41 return this->fSamplerKey == that.fSamplerKey && 42 this->fYcbcrKey == that.fYcbcrKey; 43 } 44 }; 45 46 // Helpers for hashing GrVkSampler 47 static Key GenerateKey(GrSamplerState, const GrVkYcbcrConversionInfo&); 48 GetKey(const GrVkSampler & sampler)49 static const Key& GetKey(const GrVkSampler& sampler) { return sampler.fKey; } Hash(const Key & key)50 static uint32_t Hash(const Key& key) { 51 return SkOpts::hash(reinterpret_cast<const uint32_t*>(&key), sizeof(Key)); 52 } 53 uniqueID()54 uint32_t uniqueID() const { return fUniqueID; } 55 56 #ifdef SK_TRACE_MANAGED_RESOURCES dumpInfo()57 void dumpInfo() const override { 58 SkDebugf("GrVkSampler: %" PRIdPTR " (%d refs)\n", (intptr_t)fSampler, this->getRefCnt()); 59 } 60 #endif 61 62 private: GrVkSampler(const GrVkGpu * gpu,VkSampler sampler,GrVkSamplerYcbcrConversion * ycbcrConversion,Key key)63 GrVkSampler(const GrVkGpu* gpu, VkSampler sampler, 64 GrVkSamplerYcbcrConversion* ycbcrConversion, Key key) 65 : INHERITED(gpu) 66 , fSampler(sampler) 67 , fYcbcrConversion(ycbcrConversion) 68 , fKey(key) 69 , fUniqueID(GenID()) {} 70 71 void freeGPUData() const override; 72 GenID()73 static uint32_t GenID() { 74 static std::atomic<uint32_t> nextID{1}; 75 uint32_t id; 76 do { 77 id = nextID++; 78 } while (id == SK_InvalidUniqueID); 79 return id; 80 } 81 82 VkSampler fSampler; 83 GrVkSamplerYcbcrConversion* fYcbcrConversion; 84 Key fKey; 85 uint32_t fUniqueID; 86 87 using INHERITED = GrVkManagedResource; 88 }; 89 90 #endif 91