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