blob: 9d74d8f40bf49dd8a5cc390d1e00bdfeae2d16e5 [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
Greg Daniel7e000222018-12-03 10:08:21 -05002 * 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 Daniel164a9f02016-02-22 09:56:40 -05007
8#ifndef GrVkSampler_DEFINED
9#define GrVkSampler_DEFINED
10
Greg Daniel54bfb182018-11-20 17:12:36 -050011#include "GrVkVulkan.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050012
Greg Daniel54bfb182018-11-20 17:12:36 -050013#include "GrVkResource.h"
Greg Daniel7e000222018-12-03 10:08:21 -050014#include "GrVkSamplerYcbcrConversion.h"
Greg Daniel7a82edf2018-12-04 10:54:34 -050015#include "SkAtomics.h"
Greg Daniel7e000222018-12-03 10:08:21 -050016#include "SkOpts.h"
17#include "vk/GrVkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050018
Brian Salomon2bbdcc42017-09-07 12:36:34 -040019class GrSamplerState;
Greg Daniel164a9f02016-02-22 09:56:40 -050020class GrVkGpu;
21
Greg Daniel164a9f02016-02-22 09:56:40 -050022class GrVkSampler : public GrVkResource {
23public:
Greg Daniel7e000222018-12-03 10:08:21 -050024 static GrVkSampler* Create(GrVkGpu* gpu, const GrSamplerState&, const GrVkYcbcrConversionInfo&);
Greg Daniel164a9f02016-02-22 09:56:40 -050025
26 VkSampler sampler() const { return fSampler; }
Greg Daniel7a82edf2018-12-04 10:54:34 -050027 const VkSampler* samplerPtr() const { return &fSampler; }
Greg Daniel164a9f02016-02-22 09:56:40 -050028
Greg Daniel7e000222018-12-03 10:08:21 -050029 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 Daniel6cd74902018-11-29 13:30:08 -050039
Greg Daniel7e000222018-12-03 10:08:21 -050040 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 }
jvanverth7ec92412016-07-06 09:24:57 -070053
Greg Daniel7a82edf2018-12-04 10:54:34 -050054 uint32_t uniqueID() const { return fUniqueID; }
55
jvanverth7ec92412016-07-06 09:24:57 -070056#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 Daniel164a9f02016-02-22 09:56:40 -050062private:
Greg Daniel7e000222018-12-03 10:08:21 -050063 GrVkSampler(VkSampler sampler, GrVkSamplerYcbcrConversion* ycbcrConversion, Key key)
Greg Daniel7a82edf2018-12-04 10:54:34 -050064 : INHERITED()
65 , fSampler(sampler)
66 , fYcbcrConversion(ycbcrConversion)
67 , fKey(key)
68 , fUniqueID(GenID()) {}
Greg Daniel164a9f02016-02-22 09:56:40 -050069
70 void freeGPUData(const GrVkGpu* gpu) const override;
Greg Daniel7e000222018-12-03 10:08:21 -050071 void abandonGPUData() const override;
Greg Daniel164a9f02016-02-22 09:56:40 -050072
Greg Daniel7a82edf2018-12-04 10:54:34 -050073 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 Daniel7e000222018-12-03 10:08:21 -050082 VkSampler fSampler;
83 GrVkSamplerYcbcrConversion* fYcbcrConversion;
84 Key fKey;
Greg Daniel7a82edf2018-12-04 10:54:34 -050085 uint32_t fUniqueID;
Greg Daniel164a9f02016-02-22 09:56:40 -050086
87 typedef GrVkResource INHERITED;
88};
89
egdaniel8b6394c2016-03-04 07:35:10 -080090#endif