Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit f22c57d

Browse files
rphilliSkia Commit-Bot
authored andcommitted
Centralize GrSamplerState key creation
This also changes how the Metal backend computes its key (removing the # of mipMap levels) and removes the use of the # of mipMap levels when setting up the MTLSamplerDescriptor Change-Id: I29fedc7c7a12773f3efeea824bca98cbcde98be8 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/248565 Reviewed-by: Jim Van Verth <jvanverth@google.com> Commit-Queue: Robert Phillips <robertphillips@google.com>
1 parent 55f9cba commit f22c57d

File tree

8 files changed

+33
-48
lines changed

8 files changed

+33
-48
lines changed

src/gpu/GrSamplerState.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ class GrSamplerState {
6262

6363
bool operator!=(const GrSamplerState& that) const { return !(*this == that); }
6464

65+
static uint8_t GenerateKey(const GrSamplerState& samplerState) {
66+
const int kTileModeXShift = 2;
67+
const int kTileModeYShift = 4;
68+
69+
SkASSERT(static_cast<int>(samplerState.filter()) <= 3);
70+
uint8_t key = static_cast<uint8_t>(samplerState.filter());
71+
72+
SkASSERT(static_cast<int>(samplerState.wrapModeX()) <= 3);
73+
key |= (static_cast<uint8_t>(samplerState.wrapModeX()) << kTileModeXShift);
74+
75+
SkASSERT(static_cast<int>(samplerState.wrapModeY()) <= 3);
76+
key |= (static_cast<uint8_t>(samplerState.wrapModeY()) << kTileModeYShift);
77+
78+
return key;
79+
}
80+
6581
private:
6682
WrapMode fWrapModes[2];
6783
Filter fFilter;

src/gpu/mtl/GrMtlPipelineState.mm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
GrTexture* texture,
2929
GrMtlGpu* gpu)
3030
: fTexture(static_cast<GrMtlTexture*>(texture)->mtlTexture()) {
31-
uint32_t maxMipMapLevel = texture->texturePriv().maxMipMapLevel();
32-
fSampler = gpu->resourceProvider().findOrCreateCompatibleSampler(state, maxMipMapLevel);
31+
fSampler = gpu->resourceProvider().findOrCreateCompatibleSampler(state);
3332
}
3433

3534
GrMtlPipelineState::GrMtlPipelineState(

src/gpu/mtl/GrMtlResourceProvider.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class GrMtlResourceProvider {
3333
GrSurfaceOrigin);
3434

3535
// Finds or creates a compatible MTLSamplerState based on the GrSamplerState.
36-
GrMtlSampler* findOrCreateCompatibleSampler(const GrSamplerState&, uint32_t maxMipLevel);
36+
GrMtlSampler* findOrCreateCompatibleSampler(const GrSamplerState&);
3737

3838
id<MTLBuffer> getDynamicBuffer(size_t size, size_t* offset);
3939
void addBufferCompletionHandler(GrMtlCommandBuffer* cmdBuffer);

src/gpu/mtl/GrMtlResourceProvider.mm

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,11 @@
5959
return depthStencilState;
6060
}
6161

62-
GrMtlSampler* GrMtlResourceProvider::findOrCreateCompatibleSampler(const GrSamplerState& params,
63-
uint32_t maxMipLevel) {
62+
GrMtlSampler* GrMtlResourceProvider::findOrCreateCompatibleSampler(const GrSamplerState& params) {
6463
GrMtlSampler* sampler;
65-
sampler = fSamplers.find(GrMtlSampler::GenerateKey(params, maxMipLevel));
64+
sampler = fSamplers.find(GrMtlSampler::GenerateKey(params));
6665
if (!sampler) {
67-
sampler = GrMtlSampler::Create(fGpu, params, maxMipLevel);
66+
sampler = GrMtlSampler::Create(fGpu, params);
6867
fSamplers.add(sampler);
6968
}
7069
SkASSERT(sampler);

src/gpu/mtl/GrMtlSampler.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ class GrMtlGpu;
1919
// A wrapper for a MTLSamplerState object with caching support.
2020
class GrMtlSampler : public SkRefCnt {
2121
public:
22-
static GrMtlSampler* Create(const GrMtlGpu* gpu, const GrSamplerState&, uint32_t maxMipLevel);
22+
static GrMtlSampler* Create(const GrMtlGpu* gpu, const GrSamplerState&);
2323
~GrMtlSampler() { fMtlSamplerState = nil; }
2424

2525
id<MTLSamplerState> mtlSampler() const { return fMtlSamplerState; }
2626

2727
typedef uint32_t Key;
2828

2929
// Helpers for hashing GrMtlSampler
30-
static Key GenerateKey(const GrSamplerState&, uint32_t maxMipLevel);
30+
static Key GenerateKey(const GrSamplerState&);
3131

3232
static const Key& GetKey(const GrMtlSampler& sampler) { return sampler.fKey; }
3333
static uint32_t Hash(const Key& key) {

src/gpu/mtl/GrMtlSampler.mm

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ static inline MTLSamplerAddressMode wrap_mode_to_mtl_sampler_address(
4040
SK_ABORT("Unknown wrap mode.");
4141
}
4242

43-
GrMtlSampler* GrMtlSampler::Create(const GrMtlGpu* gpu, const GrSamplerState& samplerState,
44-
uint32_t maxMipLevel) {
43+
GrMtlSampler* GrMtlSampler::Create(const GrMtlGpu* gpu, const GrSamplerState& samplerState) {
4544
static MTLSamplerMinMagFilter mtlMinMagFilterModes[] = {
4645
MTLSamplerMinMagFilterNearest,
4746
MTLSamplerMinMagFilterLinear,
@@ -62,35 +61,18 @@ static inline MTLSamplerAddressMode wrap_mode_to_mtl_sampler_address(
6261
samplerDesc.minFilter = mtlMinMagFilterModes[static_cast<int>(samplerState.filter())];
6362
samplerDesc.mipFilter = MTLSamplerMipFilterLinear;
6463
samplerDesc.lodMinClamp = 0.0f;
65-
bool useMipMaps = GrSamplerState::Filter::kMipMap == samplerState.filter() && maxMipLevel > 0;
66-
samplerDesc.lodMaxClamp = !useMipMaps ? 0.0f : (float)(maxMipLevel);
64+
bool useMipMaps = GrSamplerState::Filter::kMipMap == samplerState.filter();
65+
samplerDesc.lodMaxClamp = !useMipMaps ? 0.0f : 10000.0f;
6766
samplerDesc.maxAnisotropy = 1.0f;
6867
samplerDesc.normalizedCoordinates = true;
6968
if (@available(macOS 10.11, iOS 9.0, *)) {
7069
samplerDesc.compareFunction = MTLCompareFunctionNever;
7170
}
7271

7372
return new GrMtlSampler([gpu->device() newSamplerStateWithDescriptor: samplerDesc],
74-
GenerateKey(samplerState, maxMipLevel));
73+
GenerateKey(samplerState));
7574
}
7675

77-
GrMtlSampler::Key GrMtlSampler::GenerateKey(const GrSamplerState& samplerState,
78-
uint32_t maxMipLevel) {
79-
const int kTileModeXShift = 2;
80-
const int kTileModeYShift = 4;
81-
const int kMipLevelShift = 6;
82-
83-
SkASSERT(static_cast<int>(samplerState.filter()) <= 3);
84-
Key samplerKey = static_cast<uint16_t>(samplerState.filter());
85-
86-
SkASSERT(static_cast<int>(samplerState.wrapModeX()) <= 3);
87-
samplerKey |= (static_cast<uint16_t>(samplerState.wrapModeX()) << kTileModeXShift);
88-
89-
SkASSERT(static_cast<int>(samplerState.wrapModeY()) <= 3);
90-
samplerKey |= (static_cast<uint16_t>(samplerState.wrapModeY()) << kTileModeYShift);
91-
92-
bool useMipMaps = GrSamplerState::Filter::kMipMap == samplerState.filter() && maxMipLevel > 0;
93-
samplerKey |= (!useMipMaps ? 0 : (uint16_t) maxMipLevel) << kMipLevelShift;
94-
95-
return samplerKey;
76+
GrMtlSampler::Key GrMtlSampler::GenerateKey(const GrSamplerState& samplerState) {
77+
return GrSamplerState::GenerateKey(samplerState);
9678
}

src/gpu/vk/GrVkSampler.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,7 @@ void GrVkSampler::abandonGPUData() const {
126126

127127
GrVkSampler::Key GrVkSampler::GenerateKey(const GrSamplerState& samplerState,
128128
const GrVkYcbcrConversionInfo& ycbcrInfo) {
129-
const int kTileModeXShift = 2;
130-
const int kTileModeYShift = 4;
131-
132-
SkASSERT(static_cast<int>(samplerState.filter()) <= 3);
133-
uint8_t samplerKey = static_cast<uint16_t>(samplerState.filter());
134-
135-
SkASSERT(static_cast<int>(samplerState.wrapModeX()) <= 3);
136-
samplerKey |= (static_cast<uint8_t>(samplerState.wrapModeX()) << kTileModeXShift);
137-
138-
SkASSERT(static_cast<int>(samplerState.wrapModeY()) <= 3);
139-
samplerKey |= (static_cast<uint8_t>(samplerState.wrapModeY()) << kTileModeYShift);
140-
141-
return {samplerKey, GrVkSamplerYcbcrConversion::GenerateKey(ycbcrInfo)};
129+
return { GrSamplerState::GenerateKey(samplerState),
130+
GrVkSamplerYcbcrConversion::GenerateKey(ycbcrInfo) };
142131
}
143132

src/gpu/vk/GrVkSampler.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ class GrVkSampler : public GrVkResource {
2525
const VkSampler* samplerPtr() const { return &fSampler; }
2626

2727
struct Key {
28-
Key(uint16_t samplerKey, const GrVkSamplerYcbcrConversion::Key& ycbcrKey) {
28+
Key(uint8_t samplerKey, const GrVkSamplerYcbcrConversion::Key& ycbcrKey) {
2929
// We must memset here since the GrVkSamplerYcbcrConversion has a 64 bit value which may
3030
// force alignment padding to occur in the middle of the Key struct.
3131
memset(this, 0, sizeof(Key));
3232
fSamplerKey = samplerKey;
3333
fYcbcrKey = ycbcrKey;
3434
}
35-
uint16_t fSamplerKey;
35+
uint8_t fSamplerKey;
3636
GrVkSamplerYcbcrConversion::Key fYcbcrKey;
3737

3838
bool operator==(const Key& that) const {

0 commit comments

Comments
 (0)