Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1 | /* |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 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 | */ |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 7 | |
| 8 | #ifndef GrVkSampler_DEFINED |
| 9 | #define GrVkSampler_DEFINED |
| 10 | |
Greg Daniel | 54bfb18 | 2018-11-20 17:12:36 -0500 | [diff] [blame] | 11 | #include "GrVkVulkan.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 12 | |
Greg Daniel | 54bfb18 | 2018-11-20 17:12:36 -0500 | [diff] [blame] | 13 | #include "GrVkResource.h" |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 14 | #include "GrVkSamplerYcbcrConversion.h" |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame^] | 15 | #include "SkAtomics.h" |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 16 | #include "SkOpts.h" |
| 17 | #include "vk/GrVkTypes.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 18 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 19 | class GrSamplerState; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 20 | class GrVkGpu; |
| 21 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 22 | class GrVkSampler : public GrVkResource { |
| 23 | public: |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 24 | static GrVkSampler* Create(GrVkGpu* gpu, const GrSamplerState&, const GrVkYcbcrConversionInfo&); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 25 | |
| 26 | VkSampler sampler() const { return fSampler; } |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame^] | 27 | const VkSampler* samplerPtr() const { return &fSampler; } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 28 | |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 29 | struct Key { |
| 30 | Key(uint16_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 | uint16_t fSamplerKey; |
| 38 | GrVkSamplerYcbcrConversion::Key fYcbcrKey; |
Greg Daniel | 6cd7490 | 2018-11-29 13:30:08 -0500 | [diff] [blame] | 39 | |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 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(const GrSamplerState&, const GrVkYcbcrConversionInfo&); |
| 48 | |
| 49 | static const Key& GetKey(const GrVkSampler& sampler) { return sampler.fKey; } |
| 50 | static uint32_t Hash(const Key& key) { |
| 51 | return SkOpts::hash(reinterpret_cast<const uint32_t*>(&key), sizeof(Key)); |
| 52 | } |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 53 | |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame^] | 54 | uint32_t uniqueID() const { return fUniqueID; } |
| 55 | |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 56 | #ifdef SK_TRACE_VK_RESOURCES |
| 57 | void dumpInfo() const override { |
| 58 | SkDebugf("GrVkSampler: %d (%d refs)\n", fSampler, this->getRefCnt()); |
| 59 | } |
| 60 | #endif |
| 61 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 62 | private: |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 63 | GrVkSampler(VkSampler sampler, GrVkSamplerYcbcrConversion* ycbcrConversion, Key key) |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame^] | 64 | : INHERITED() |
| 65 | , fSampler(sampler) |
| 66 | , fYcbcrConversion(ycbcrConversion) |
| 67 | , fKey(key) |
| 68 | , fUniqueID(GenID()) {} |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 69 | |
| 70 | void freeGPUData(const GrVkGpu* gpu) const override; |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 71 | void abandonGPUData() const override; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 72 | |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame^] | 73 | static uint32_t GenID() { |
| 74 | static int32_t gUniqueID = SK_InvalidUniqueID; |
| 75 | uint32_t id; |
| 76 | do { |
| 77 | id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1); |
| 78 | } while (id == SK_InvalidUniqueID); |
| 79 | return id; |
| 80 | } |
| 81 | |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 82 | VkSampler fSampler; |
| 83 | GrVkSamplerYcbcrConversion* fYcbcrConversion; |
| 84 | Key fKey; |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame^] | 85 | uint32_t fUniqueID; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 86 | |
| 87 | typedef GrVkResource INHERITED; |
| 88 | }; |
| 89 | |
egdaniel | 8b6394c | 2016-03-04 07:35:10 -0800 | [diff] [blame] | 90 | #endif |