blob: 9fca6c9ece0c904990c769036924cead693dfc5c [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 "GrVkResource.h"
Greg Daniel7e000222018-12-03 10:08:21 -050012#include "GrVkSamplerYcbcrConversion.h"
13#include "SkOpts.h"
14#include "vk/GrVkTypes.h"
Mike Klein0ec1c572018-12-04 11:52:51 -050015#include <atomic>
Greg Daniel164a9f02016-02-22 09:56:40 -050016
Brian Salomon2bbdcc42017-09-07 12:36:34 -040017class GrSamplerState;
Greg Daniel164a9f02016-02-22 09:56:40 -050018class GrVkGpu;
19
Greg Daniel164a9f02016-02-22 09:56:40 -050020class GrVkSampler : public GrVkResource {
21public:
Greg Daniel7e000222018-12-03 10:08:21 -050022 static GrVkSampler* Create(GrVkGpu* gpu, const GrSamplerState&, const GrVkYcbcrConversionInfo&);
Greg Daniel164a9f02016-02-22 09:56:40 -050023
24 VkSampler sampler() const { return fSampler; }
Greg Daniel7a82edf2018-12-04 10:54:34 -050025 const VkSampler* samplerPtr() const { return &fSampler; }
Greg Daniel164a9f02016-02-22 09:56:40 -050026
Greg Daniel7e000222018-12-03 10:08:21 -050027 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 Daniel6cd74902018-11-29 13:30:08 -050037
Greg Daniel7e000222018-12-03 10:08:21 -050038 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 }
jvanverth7ec92412016-07-06 09:24:57 -070051
Greg Daniel7a82edf2018-12-04 10:54:34 -050052 uint32_t uniqueID() const { return fUniqueID; }
53
jvanverth7ec92412016-07-06 09:24:57 -070054#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 Daniel164a9f02016-02-22 09:56:40 -050060private:
Greg Daniel7e000222018-12-03 10:08:21 -050061 GrVkSampler(VkSampler sampler, GrVkSamplerYcbcrConversion* ycbcrConversion, Key key)
Greg Daniel7a82edf2018-12-04 10:54:34 -050062 : INHERITED()
63 , fSampler(sampler)
64 , fYcbcrConversion(ycbcrConversion)
65 , fKey(key)
66 , fUniqueID(GenID()) {}
Greg Daniel164a9f02016-02-22 09:56:40 -050067
Ethan Nicholas8e265a72018-12-12 16:22:40 -050068 void freeGPUData(GrVkGpu* gpu) const override;
Greg Daniel7e000222018-12-03 10:08:21 -050069 void abandonGPUData() const override;
Greg Daniel164a9f02016-02-22 09:56:40 -050070
Greg Daniel7a82edf2018-12-04 10:54:34 -050071 static uint32_t GenID() {
Mike Klein0ec1c572018-12-04 11:52:51 -050072 static std::atomic<uint32_t> nextID{1};
Greg Daniel7a82edf2018-12-04 10:54:34 -050073 uint32_t id;
74 do {
Mike Klein0ec1c572018-12-04 11:52:51 -050075 id = nextID++;
Greg Daniel7a82edf2018-12-04 10:54:34 -050076 } while (id == SK_InvalidUniqueID);
77 return id;
78 }
79
Greg Daniel7e000222018-12-03 10:08:21 -050080 VkSampler fSampler;
81 GrVkSamplerYcbcrConversion* fYcbcrConversion;
82 Key fKey;
Greg Daniel7a82edf2018-12-04 10:54:34 -050083 uint32_t fUniqueID;
Greg Daniel164a9f02016-02-22 09:56:40 -050084
85 typedef GrVkResource INHERITED;
86};
87
egdaniel8b6394c2016-03-04 07:35:10 -080088#endif