GrVkSampler.h 2.6 KB

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