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/GrVkResource.h" 14 #include "src/gpu/vk/GrVkSamplerYcbcrConversion.h" 15 #include <atomic> 16 17 class GrSamplerState; 18 class GrVkGpu; 19 20 class GrVkSampler : public GrVkResource { 21 public: 22 static GrVkSampler* Create(GrVkGpu* gpu, GrSamplerState, const GrVkYcbcrConversionInfo&); 23 sampler()24 VkSampler sampler() const { return fSampler; } samplerPtr()25 const VkSampler* samplerPtr() const { return &fSampler; } 26 27 struct Key { KeyKey28 Key(uint8_t samplerKey, const GrVkSamplerYcbcrConversion::Key& ycbcrKey) { 29 // We must memset here since the GrVkSamplerYcbcrConversion has a 64 bit value which may 30 // force alignment padding to occur in the middle of the Key struct. 31 memset(this, 0, sizeof(Key)); 32 fSamplerKey = samplerKey; 33 fYcbcrKey = ycbcrKey; 34 } 35 uint8_t fSamplerKey; 36 GrVkSamplerYcbcrConversion::Key fYcbcrKey; 37 38 bool operator==(const Key& that) const { 39 return this->fSamplerKey == that.fSamplerKey && 40 this->fYcbcrKey == that.fYcbcrKey; 41 } 42 }; 43 44 // Helpers for hashing GrVkSampler 45 static Key GenerateKey(GrSamplerState, const GrVkYcbcrConversionInfo&); 46 GetKey(const GrVkSampler & sampler)47 static const Key& GetKey(const GrVkSampler& sampler) { return sampler.fKey; } Hash(const Key & key)48 static uint32_t Hash(const Key& key) { 49 return SkOpts::hash(reinterpret_cast<const uint32_t*>(&key), sizeof(Key)); 50 } 51 uniqueID()52 uint32_t uniqueID() const { return fUniqueID; } 53 54 #ifdef SK_TRACE_VK_RESOURCES dumpInfo()55 void dumpInfo() const override { 56 SkDebugf("GrVkSampler: %d (%d refs)\n", fSampler, this->getRefCnt()); 57 } 58 #endif 59 60 private: GrVkSampler(VkSampler sampler,GrVkSamplerYcbcrConversion * ycbcrConversion,Key key)61 GrVkSampler(VkSampler sampler, GrVkSamplerYcbcrConversion* ycbcrConversion, Key key) 62 : INHERITED() 63 , fSampler(sampler) 64 , fYcbcrConversion(ycbcrConversion) 65 , fKey(key) 66 , fUniqueID(GenID()) {} 67 68 void freeGPUData(GrVkGpu* gpu) const override; 69 GenID()70 static uint32_t GenID() { 71 static std::atomic<uint32_t> nextID{1}; 72 uint32_t id; 73 do { 74 id = nextID++; 75 } while (id == SK_InvalidUniqueID); 76 return id; 77 } 78 79 VkSampler fSampler; 80 GrVkSamplerYcbcrConversion* fYcbcrConversion; 81 Key fKey; 82 uint32_t fUniqueID; 83 84 typedef GrVkResource INHERITED; 85 }; 86 87 #endif 88