Consolidate sampler->index/key conversion

Share code between GL and other backends.

Prepare for separate GrSamplerState field to control mip mapping.

Bug: skia:10344

Change-Id: I16adae3512e11971f98c63ae08fb8cde31332520
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/302910
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
This commit is contained in:
Brian Salomon 2020-07-15 13:25:30 -04:00 committed by Skia Commit-Bot
parent 268e48b938
commit a7d9f30f4a
7 changed files with 25 additions and 38 deletions

View File

@ -9,6 +9,7 @@
#define GrSamplerState_DEFINED
#include "include/gpu/GrTypes.h"
#include <limits>
/**
* Represents the filtering and tile modes used to access a texture.
@ -61,25 +62,28 @@ public:
constexpr bool operator!=(const GrSamplerState& that) const { return !(*this == that); }
constexpr static uint8_t GenerateKey(GrSamplerState samplerState) {
const int kTileModeXShift = 2;
const int kTileModeYShift = 4;
SkASSERT(static_cast<int>(samplerState.filter()) <= 3);
uint8_t key = static_cast<uint8_t>(samplerState.filter());
SkASSERT(static_cast<int>(samplerState.wrapModeX()) <= 3);
key |= (static_cast<uint8_t>(samplerState.wrapModeX()) << kTileModeXShift);
SkASSERT(static_cast<int>(samplerState.wrapModeY()) <= 3);
key |= (static_cast<uint8_t>(samplerState.wrapModeY()) << kTileModeYShift);
return key;
/**
* Turn the sampler state into an integer from a tightly packed range for use as an index
* (or key)
*/
constexpr uint8_t asIndex() const {
constexpr int kNumWraps = static_cast<int>(WrapMode::kLast) + 1;
int result = static_cast<int>(fWrapModes[0])*1
+ static_cast<int>(fWrapModes[1])*kNumWraps
+ static_cast<int>(fFilter) *kNumWraps*kNumWraps;
SkASSERT(result <= kNumUniqueSamplers);
return static_cast<uint8_t>(result);
}
static constexpr int kNumUniqueSamplers = (static_cast<int>(WrapMode::kLast) + 1)
* (static_cast<int>(WrapMode::kLast) + 1)
* (static_cast<int>(Filter::kLast ) + 1);
private:
WrapMode fWrapModes[2] = {WrapMode::kClamp, WrapMode::kClamp};
Filter fFilter = GrSamplerState::Filter::kNearest;
};
static_assert(GrSamplerState::kNumUniqueSamplers <=
std::numeric_limits<decltype(GrSamplerState{}.asIndex())>::max());
#endif

View File

@ -126,7 +126,7 @@ static D3D12_TEXTURE_ADDRESS_MODE wrap_mode_to_d3d_address_mode(GrSamplerState::
D3D12_CPU_DESCRIPTOR_HANDLE GrD3DResourceProvider::findOrCreateCompatibleSampler(
const GrSamplerState& params) {
uint32_t key = GrSamplerState::GenerateKey(params);
uint32_t key = params.asIndex();
D3D12_CPU_DESCRIPTOR_HANDLE* samplerPtr = fSamplers.find(key);
if (samplerPtr) {
return *samplerPtr;

View File

@ -81,9 +81,7 @@ private:
void removeFinishIdleProcs();
struct SamplerHash {
uint32_t operator()(GrSamplerState state) const {
return GrSamplerState::GenerateKey(state);
}
uint32_t operator()(GrSamplerState state) const { return state.asIndex(); }
};
GrD3DDescriptorHeap::CPUHandle fShaderResourceView;

View File

@ -232,7 +232,7 @@ public:
}
void bindSampler(int unitIdx, GrSamplerState state) {
int index = StateToIndex(state);
int index = state.asIndex();
if (!fSamplers[index]) {
GrGLuint s;
GR_GL_CALL(fGpu->glInterface(), GenSamplers(1, &s));
@ -280,20 +280,8 @@ public:
}
private:
static int StateToIndex(GrSamplerState state) {
int filter = static_cast<int>(state.filter());
SkASSERT(filter >= 0 && filter < 3);
int wrapX = static_cast<int>(state.wrapModeX());
SkASSERT(wrapX >= 0 && wrapX < 4);
int wrapY = static_cast<int>(state.wrapModeY());
SkASSERT(wrapY >= 0 && wrapY < 4);
int idx = 16 * filter + 4 * wrapX + wrapY;
SkASSERT(idx < kNumSamplers);
return idx;
}
static constexpr int kNumSamplers = GrSamplerState::kNumUniqueSamplers;
GrGLGpu* fGpu;
static constexpr int kNumSamplers = 48;
std::unique_ptr<GrGLuint[]> fHWBoundSamplers;
GrGLuint fSamplers[kNumSamplers];
int fNumTextureUnits;

View File

@ -74,5 +74,5 @@ GrMtlSampler* GrMtlSampler::Create(const GrMtlGpu* gpu, GrSamplerState samplerSt
}
GrMtlSampler::Key GrMtlSampler::GenerateKey(GrSamplerState samplerState) {
return GrSamplerState::GenerateKey(samplerState);
return samplerState.asIndex();
}

View File

@ -122,6 +122,5 @@ void GrVkSampler::freeGPUData() const {
GrVkSampler::Key GrVkSampler::GenerateKey(GrSamplerState samplerState,
const GrVkYcbcrConversionInfo& ycbcrInfo) {
return { GrSamplerState::GenerateKey(samplerState),
GrVkSamplerYcbcrConversion::GenerateKey(ycbcrInfo) };
return {samplerState.asIndex(), GrVkSamplerYcbcrConversion::GenerateKey(ycbcrInfo)};
}

View File

@ -97,9 +97,7 @@ private:
const GrVkImageView* fTextureView;
struct SamplerHash {
uint32_t operator()(GrSamplerState state) const {
return GrSamplerState::GenerateKey(state);
}
uint32_t operator()(GrSamplerState state) const { return state.asIndex(); }
};
struct DescriptorCacheEntry;
SkLRUCache<const GrSamplerState, std::unique_ptr<DescriptorCacheEntry>, SamplerHash>