Add clamp to border wrap mode to gpu
Bug: skia: Change-Id: I286163cdea4fa8f7e6a876baaa11aa3dacd4f2ff Reviewed-on: https://skia-review.googlesource.com/c/175244 Reviewed-by: Brian Salomon <bsalomon@google.com> Reviewed-by: Greg Daniel <egdaniel@google.com> Commit-Queue: Michael Ludwig <michaelludwig@google.com>
This commit is contained in:
parent
e954b9df38
commit
f49a5785f3
@ -16,7 +16,7 @@
|
||||
class GrSamplerState {
|
||||
public:
|
||||
enum class Filter : uint8_t { kNearest, kBilerp, kMipMap };
|
||||
enum class WrapMode : uint8_t { kClamp, kRepeat, kMirrorRepeat };
|
||||
enum class WrapMode : uint8_t { kClamp, kRepeat, kMirrorRepeat, kClampToBorder };
|
||||
|
||||
static constexpr GrSamplerState ClampNearest() { return GrSamplerState(); }
|
||||
static constexpr GrSamplerState ClampBilerp() {
|
||||
@ -51,7 +51,8 @@ public:
|
||||
WrapMode wrapModeY() const { return fWrapModes[1]; }
|
||||
|
||||
bool isRepeated() const {
|
||||
return WrapMode::kClamp != fWrapModes[0] || WrapMode::kClamp != fWrapModes[1];
|
||||
return (WrapMode::kClamp != fWrapModes[0] && WrapMode::kClampToBorder != fWrapModes[0]) ||
|
||||
(WrapMode::kClamp != fWrapModes[1] && WrapMode::kClampToBorder != fWrapModes[1]);
|
||||
}
|
||||
|
||||
bool operator==(const GrSamplerState& that) const {
|
||||
|
@ -72,6 +72,9 @@ GrCaps::GrCaps(const GrContextOptions& options) {
|
||||
|
||||
fPreferVRAMUseOverFlushes = true;
|
||||
|
||||
// Default to true, allow older versions of OpenGL to disable explicitly
|
||||
fClampToBorderSupport = true;
|
||||
|
||||
fDriverBugWorkarounds = options.fDriverBugWorkarounds;
|
||||
}
|
||||
|
||||
|
@ -311,6 +311,12 @@ public:
|
||||
*/
|
||||
GrBackendFormat createFormatFromBackendTexture(const GrBackendTexture&) const;
|
||||
|
||||
/**
|
||||
* The CLAMP_TO_BORDER wrap mode for texture coordinates was added to desktop GL in 1.3, and
|
||||
* GLES 3.2, but is also available in extensions. Vulkan and Metal always have support.
|
||||
*/
|
||||
bool clampToBorderSupport() const { return fClampToBorderSupport; }
|
||||
|
||||
const GrDriverBugWorkarounds& workarounds() const { return fDriverBugWorkarounds; }
|
||||
|
||||
protected:
|
||||
@ -347,6 +353,7 @@ protected:
|
||||
bool fMustClearUploadedBufferData : 1;
|
||||
bool fSupportsAHardwareBufferImages : 1;
|
||||
bool fHalfFloatVertexAttributeSupport : 1;
|
||||
bool fClampToBorderSupport : 1;
|
||||
|
||||
// Driver workaround
|
||||
bool fBlacklistCoverageCounting : 1;
|
||||
|
@ -235,6 +235,21 @@ void GrGLCaps::init(const GrContextOptions& contextOptions,
|
||||
// lacks TexImage2D support and ANGLE lacks GL ES 3.0 support.
|
||||
}
|
||||
|
||||
// GrCaps defaults fClampToBorderSupport to true, so disable when unsupported
|
||||
if (kGL_GrGLStandard == standard) {
|
||||
// Clamp to border added in 1.3
|
||||
if (version < GR_GL_VER(1, 3) && !ctxInfo.hasExtension("GL_ARB_texture_border_clamp")) {
|
||||
fClampToBorderSupport = false;
|
||||
}
|
||||
} else if (kGLES_GrGLStandard == standard) {
|
||||
// GLES didn't have clamp to border until 3.2, but provides several alternative extensions
|
||||
if (version < GR_GL_VER(3, 2) && !ctxInfo.hasExtension("GL_EXT_texture_border_clamp") &&
|
||||
!ctxInfo.hasExtension("GL_NV_texture_border_clamp") &&
|
||||
!ctxInfo.hasExtension("GL_OES_texture_border_clamp")) {
|
||||
fClampToBorderSupport = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (kGL_GrGLStandard == standard) {
|
||||
if (version >= GR_GL_VER(3,3) || ctxInfo.hasExtension("GL_ARB_texture_swizzle")) {
|
||||
fTextureSwizzleSupport = true;
|
||||
|
@ -670,6 +670,7 @@
|
||||
#define GR_GL_REPEAT 0x2901
|
||||
#define GR_GL_CLAMP_TO_EDGE 0x812F
|
||||
#define GR_GL_MIRRORED_REPEAT 0x8370
|
||||
#define GR_GL_CLAMP_TO_BORDER 0x812D
|
||||
|
||||
/* Texture Swizzle */
|
||||
#define GR_GL_TEXTURE_SWIZZLE_R 0x8E42
|
||||
|
@ -202,11 +202,16 @@ static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode) {
|
||||
static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode,
|
||||
const GrCaps& caps) {
|
||||
switch (wrapMode) {
|
||||
case GrSamplerState::WrapMode::kClamp: return GR_GL_CLAMP_TO_EDGE;
|
||||
case GrSamplerState::WrapMode::kRepeat: return GR_GL_REPEAT;
|
||||
case GrSamplerState::WrapMode::kMirrorRepeat: return GR_GL_MIRRORED_REPEAT;
|
||||
case GrSamplerState::WrapMode::kClampToBorder:
|
||||
// May not be supported but should have been caught earlier
|
||||
SkASSERT(caps.clampToBorderSupport());
|
||||
return GR_GL_CLAMP_TO_BORDER;
|
||||
}
|
||||
SK_ABORT("Unknown wrap mode");
|
||||
return 0;
|
||||
@ -242,8 +247,8 @@ public:
|
||||
fSamplers[index] = s;
|
||||
auto minFilter = filter_to_gl_min_filter(state.filter());
|
||||
auto magFilter = filter_to_gl_mag_filter(state.filter());
|
||||
auto wrapX = wrap_mode_to_gl_wrap(state.wrapModeX());
|
||||
auto wrapY = wrap_mode_to_gl_wrap(state.wrapModeY());
|
||||
auto wrapX = wrap_mode_to_gl_wrap(state.wrapModeX(), fGpu->glCaps());
|
||||
auto wrapY = wrap_mode_to_gl_wrap(state.wrapModeY(), fGpu->glCaps());
|
||||
GR_GL_CALL(fGpu->glInterface(),
|
||||
SamplerParameteri(s, GR_GL_TEXTURE_MIN_FILTER, minFilter));
|
||||
GR_GL_CALL(fGpu->glInterface(),
|
||||
@ -284,16 +289,16 @@ private:
|
||||
int filter = static_cast<int>(state.filter());
|
||||
SkASSERT(filter >= 0 && filter < 3);
|
||||
int wrapX = static_cast<int>(state.wrapModeX());
|
||||
SkASSERT(wrapX >= 0 && wrapX < 3);
|
||||
SkASSERT(wrapX >= 0 && wrapX < 4);
|
||||
int wrapY = static_cast<int>(state.wrapModeY());
|
||||
SkASSERT(wrapY >= 0 && wrapY < 3);
|
||||
int idx = 9 * filter + 3 * wrapX + wrapY;
|
||||
SkASSERT(wrapY >= 0 && wrapY < 4);
|
||||
int idx = 16 * filter + 4 * wrapX + wrapY;
|
||||
SkASSERT(idx < kNumSamplers);
|
||||
return idx;
|
||||
}
|
||||
|
||||
GrGLGpu* fGpu;
|
||||
static constexpr int kNumSamplers = 27;
|
||||
static constexpr int kNumSamplers = 48;
|
||||
std::unique_ptr<GrGLuint[]> fHWBoundSamplers;
|
||||
GrGLuint fSamplers[kNumSamplers];
|
||||
int fNumTextureUnits;
|
||||
@ -2854,8 +2859,8 @@ void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, GrGLTexture*
|
||||
newSamplerParams.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
|
||||
newSamplerParams.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
|
||||
|
||||
newSamplerParams.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX());
|
||||
newSamplerParams.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY());
|
||||
newSamplerParams.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
|
||||
newSamplerParams.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
|
||||
|
||||
// These are the OpenGL default values.
|
||||
newSamplerParams.fMinLOD = -1000;
|
||||
@ -2889,6 +2894,17 @@ void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, GrGLTexture*
|
||||
this->setTextureUnit(unitIdx);
|
||||
GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerParams.fWrapT));
|
||||
}
|
||||
if (this->glCaps().clampToBorderSupport()) {
|
||||
// Make sure the border color is transparent black (the default)
|
||||
if (setAll || oldSamplerParams.fBorderColorInvalid) {
|
||||
this->setTextureUnit(unitIdx);
|
||||
// Specify the transparent black as normalized signed integers. The conversion is
|
||||
// defined as (2c+1)/(2^32-1), which makes it impossible to map a signed in to
|
||||
// exactly 0.f. But 1/(2^32-1) is close enough.
|
||||
static const GrGLint kTransparentBlack[4] = {0, 0, 0, 0};
|
||||
GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
|
||||
}
|
||||
}
|
||||
}
|
||||
GrGLTexture::NonSamplerParams newNonSamplerParams;
|
||||
newNonSamplerParams.fBaseMipMapLevel = 0;
|
||||
|
@ -27,6 +27,10 @@ public:
|
||||
GrGLenum fWrapT = GR_GL_REPEAT;
|
||||
GrGLfloat fMinLOD = -1000.f;
|
||||
GrGLfloat fMaxLOD = 1000.f;
|
||||
// We always want the border color to be transparent black, so no need to store 4 floats.
|
||||
// Just track if it's been invalidated and no longer the default
|
||||
bool fBorderColorInvalid = false;
|
||||
|
||||
void invalidate() {
|
||||
fMinFilter = ~0U;
|
||||
fMagFilter = ~0U;
|
||||
@ -34,6 +38,7 @@ public:
|
||||
fWrapT = ~0U;
|
||||
fMinLOD = SK_ScalarNaN;
|
||||
fMaxLOD = SK_ScalarNaN;
|
||||
fBorderColorInvalid = true;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -18,6 +18,8 @@ static inline MTLSamplerAddressMode wrap_mode_to_mtl_sampler_address(
|
||||
return MTLSamplerAddressModeRepeat;
|
||||
case GrSamplerState::WrapMode::kMirrorRepeat:
|
||||
return MTLSamplerAddressModeMirrorRepeat;
|
||||
case GrSamplerState::WrapMode::kClampToBorder:
|
||||
return MTLSamplerAddressModeClampToBorderColor;
|
||||
}
|
||||
SK_ABORT("Unknown wrap mode.");
|
||||
return MTLSamplerAddressModeClampToEdge;
|
||||
|
@ -19,6 +19,8 @@ static inline VkSamplerAddressMode wrap_mode_to_vk_sampler_address(
|
||||
return VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
||||
case GrSamplerState::WrapMode::kMirrorRepeat:
|
||||
return VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT;
|
||||
case GrSamplerState::WrapMode::kClampToBorder:
|
||||
return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
|
||||
}
|
||||
SK_ABORT("Unknown wrap mode.");
|
||||
return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
||||
|
@ -171,8 +171,7 @@ static GrSamplerState::WrapMode tile_mode_to_wrap_mode(const SkShader::TileMode
|
||||
case SkShader::TileMode::kMirror_TileMode:
|
||||
return GrSamplerState::WrapMode::kMirrorRepeat;
|
||||
case SkShader::kDecal_TileMode:
|
||||
// TODO: depending on caps, we should extend WrapMode for decal...
|
||||
return GrSamplerState::WrapMode::kClamp;
|
||||
return GrSamplerState::WrapMode::kClampToBorder;
|
||||
}
|
||||
SK_ABORT("Unknown tile mode.");
|
||||
return GrSamplerState::WrapMode::kClamp;
|
||||
@ -188,6 +187,19 @@ std::unique_ptr<GrFragmentProcessor> SkImageShader::asFragmentProcessor(
|
||||
|
||||
GrSamplerState::WrapMode wrapModes[] = {tile_mode_to_wrap_mode(fTileModeX),
|
||||
tile_mode_to_wrap_mode(fTileModeY)};
|
||||
if ((wrapModes[0] == GrSamplerState::WrapMode::kClampToBorder ||
|
||||
wrapModes[1] == GrSamplerState::WrapMode::kClampToBorder) &&
|
||||
!args.fContext->contextPriv().caps()->clampToBorderSupport()) {
|
||||
// HW clamp to border is unavailable, so fall back to clamp for now
|
||||
// TODO(michaelludwig): If clamp-to-border is selected but is unsupported, the texture
|
||||
// domain effect could be used to emulate the decal effect.
|
||||
if (wrapModes[0] == GrSamplerState::WrapMode::kClampToBorder) {
|
||||
wrapModes[0] = GrSamplerState::WrapMode::kClamp;
|
||||
}
|
||||
if (wrapModes[1] == GrSamplerState::WrapMode::kClampToBorder) {
|
||||
wrapModes[1] = GrSamplerState::WrapMode::kClamp;
|
||||
}
|
||||
}
|
||||
|
||||
// Must set wrap and filter on the sampler before requesting a texture. In two places below
|
||||
// we check the matrix scale factors to determine how to interpret the filter quality setting.
|
||||
|
Loading…
Reference in New Issue
Block a user