Add GrCaps call to get swizzle from BackendFormat and ColorType.

This CL just adds the caps function. Follow on CL will use it thoughout
the gpu backend to remove GrPixelConfig dependency for swizzle.

Bug: skia:6718
Change-Id: Id299c72a9dc9f33411d8fd9c806158da20372474
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/214188
Commit-Queue: Greg Daniel <egdaniel@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
This commit is contained in:
Greg Daniel 2019-05-16 16:52:55 -04:00 committed by Skia Commit-Bot
parent 4e93feba40
commit eb4a827d5b
9 changed files with 309 additions and 4 deletions

View File

@ -346,6 +346,18 @@ public:
*/
bool clampToBorderSupport() const { return fClampToBorderSupport; }
/**
* Returns the GrSwizzle to use when sampling from a texture with the passed in GrBackendFormat
* and GrColorType.
*/
virtual GrSwizzle getTextureSwizzle(const GrBackendFormat&, GrColorType) const = 0;
/**
* Returns the GrSwizzle to use when outputting to a render target with the passed in
* GrBackendFormat and GrColorType.
*/
virtual GrSwizzle getOutputSwizzle(const GrBackendFormat&, GrColorType) const = 0;
const GrDriverBugWorkarounds& workarounds() const { return fDriverBugWorkarounds; }
protected:

View File

@ -96,7 +96,6 @@ public:
static constexpr GrSwizzle RRRR() { return GrSwizzle("rrrr"); }
static constexpr GrSwizzle RRRA() { return GrSwizzle("rrra"); }
static constexpr GrSwizzle BGRA() { return GrSwizzle("bgra"); }
static constexpr GrSwizzle RGRG() { return GrSwizzle("rgrg"); }
static constexpr GrSwizzle RGB1() { return GrSwizzle("rgb1"); }
private:

View File

@ -1625,7 +1625,7 @@ void GrGLCaps::initConfigTable(const GrContextOptions& contextOptions,
} else {
fConfigTable[kRG_88_GrPixelConfig].fFlags = 0;
}
fConfigTable[kRG_88_GrPixelConfig].fSwizzle = GrSwizzle::RGRG();
fConfigTable[kRG_88_GrPixelConfig].fSwizzle = GrSwizzle::RGBA();
fConfigTable[kBGRA_8888_GrPixelConfig].fFormats.fExternalFormat[kReadPixels_ExternalFormatUsage] =
GR_GL_BGRA;
@ -3270,6 +3270,100 @@ GrBackendFormat GrGLCaps::getBackendFormatFromGrColorType(GrColorType ct,
return GrBackendFormat::MakeGL(this->configSizedInternalFormat(config), GR_GL_TEXTURE_2D);
}
#ifdef SK_DEBUG
static bool format_color_type_valid_pair(GrGLenum format, GrColorType colorType) {
switch (colorType) {
case GrColorType::kUnknown:
return false;
case GrColorType::kAlpha_8:
return GR_GL_ALPHA8 == format || GR_GL_R8 == format;
case GrColorType::kRGB_565:
return GR_GL_RGB565 == format;
case GrColorType::kABGR_4444:
return GR_GL_RGBA4 == format;
case GrColorType::kRGBA_8888:
return GR_GL_RGBA8 == format || GR_GL_SRGB8_ALPHA8 == format;
case GrColorType::kRGB_888x:
return GR_GL_RGB8 == format || GR_GL_RGBA8 == format;
case GrColorType::kRG_88:
return GR_GL_RG8 == format;
case GrColorType::kBGRA_8888:
return GR_GL_RGBA8 == format || GR_GL_BGRA8 == format || GR_GL_SRGB8_ALPHA8 == format;
case GrColorType::kRGBA_1010102:
return GR_GL_RGB10_A2 == format;
case GrColorType::kGray_8:
return GR_GL_LUMINANCE8 == format || GR_GL_R8 == format;
case GrColorType::kAlpha_F16:
return GR_GL_R16F == format;
case GrColorType::kRGBA_F16:
return GR_GL_RGBA16F == format;
case GrColorType::kRGBA_F16_Clamped:
return GR_GL_RGBA16F == format;
case GrColorType::kRG_F32:
return GR_GL_RG32F == format;
case GrColorType::kRGBA_F32:
return GR_GL_RGBA32F == format;
case GrColorType::kRGB_ETC1:
return GR_GL_COMPRESSED_RGB8_ETC2 == format || GR_GL_COMPRESSED_ETC1_RGB8 == format;
}
}
#endif
static GrSwizzle get_swizzle(const GrBackendFormat& format, GrColorType colorType,
bool forOutput) {
SkASSERT(format.getGLFormat());
GrGLenum glFormat = *format.getGLFormat();
SkASSERT(format_color_type_valid_pair(glFormat, colorType));
// When picking a swizzle for output, we will pick to use RGBA if possible so that it creates
// less variations of shaders and those shaders can be used by different configs.
switch (colorType) {
case GrColorType::kAlpha_8:
if (glFormat == GR_GL_ALPHA8) {
if (!forOutput) {
return GrSwizzle::AAAA();
}
} else {
SkASSERT(glFormat == GR_GL_R8);
if (forOutput) {
return GrSwizzle::AAAA();
} else {
return GrSwizzle::RRRR();
}
}
break;
case GrColorType::kAlpha_F16:
SkASSERT(glFormat == GR_GL_R16F);
if (forOutput) {
return GrSwizzle::AAAA();
} else {
return GrSwizzle::RRRR();
}
case GrColorType::kGray_8:
if (glFormat == GR_GL_R8) {
if (!forOutput) {
return GrSwizzle::RRRA();
}
}
break;
case GrColorType::kRGB_888x:
if (!forOutput) {
return GrSwizzle::RGB1();
}
default:
return GrSwizzle::RGBA();
}
return GrSwizzle::RGBA();
}
GrSwizzle GrGLCaps::getTextureSwizzle(const GrBackendFormat& format, GrColorType colorType) const {
return get_swizzle(format, colorType, false);
}
GrSwizzle GrGLCaps::getOutputSwizzle(const GrBackendFormat& format, GrColorType colorType) const {
return get_swizzle(format, colorType, true);
}
size_t GrGLCaps::onTransferFromOffsetAlignment(GrColorType bufferColorType) const {
// This implementation is highly coupled with decisions made in initConfigTable and GrGLGpu's
// read pixels implementation and may not be correct for all types. TODO(bsalomon): This will be

View File

@ -428,6 +428,9 @@ public:
GrBackendFormat getBackendFormatFromGrColorType(GrColorType ct,
GrSRGBEncoded srgbEncoded) const override;
GrSwizzle getTextureSwizzle(const GrBackendFormat&, GrColorType) const override;
GrSwizzle getOutputSwizzle(const GrBackendFormat&, GrColorType) const override;
#if GR_TEST_UTILS
GrGLStandard standard() const { return fStandard; }
#endif

View File

@ -106,6 +106,13 @@ public:
return GrBackendFormat::MakeMock(config);
}
GrSwizzle getTextureSwizzle(const GrBackendFormat&, GrColorType) const override {
return GrSwizzle();
}
GrSwizzle getOutputSwizzle(const GrBackendFormat&, GrColorType) const override {
return GrSwizzle();
}
private:
bool onSurfaceSupportsWritePixels(const GrSurface*) const override { return true; }
bool onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,

View File

@ -72,6 +72,9 @@ public:
GrBackendFormat getBackendFormatFromGrColorType(GrColorType ct,
GrSRGBEncoded srgbEncoded) const override;
GrSwizzle getTextureSwizzle(const GrBackendFormat&, GrColorType) const override;
GrSwizzle getOutputSwizzle(const GrBackendFormat&, GrColorType) const override;
private:
void initFeatureSet(MTLFeatureSet featureSet);

View File

@ -601,3 +601,102 @@ GrBackendFormat GrMtlCaps::getBackendFormatFromGrColorType(GrColorType ct,
return GrBackendFormat::MakeMtl(format);
}
#ifdef SK_DEBUG
static bool format_color_type_valid_pair(MTLPixelFormat format, GrColorType colorType) {
switch (colorType) {
case GrColorType::kUnknown:
return false;
case GrColorType::kAlpha_8:
return MTLPixelFormatA8Unorm == format || MTLPixelFormatR8Unorm == format;
case GrColorType::kRGB_565:
#ifdef SK_BUILD_FOR_MAC
return false;
#else
return MTLPixelFormatB5G6R5Unorm == format;
#endif
case GrColorType::kABGR_4444:
#ifdef SK_BUILD_FOR_MAC
return false;
#else
return MTLPixelFormatABGR4Unorm == format;
#endif
case GrColorType::kRGBA_8888:
return MTLPixelFormatRGBA8Unorm == format || MTLPixelFormatRGBA8Unorm_sRGB == format;
case GrColorType::kRGB_888x:
return MTLPixelFormatRGBA8Unorm == format;
case GrColorType::kRG_88:
return MTLPixelFormatRG8Unorm == format;
case GrColorType::kBGRA_8888:
return MTLPixelFormatBGRA8Unorm == format || MTLPixelFormatBGRA8Unorm_sRGB == format;
case GrColorType::kRGBA_1010102:
return MTLPixelFormatRGB10A2Unorm == format;
case GrColorType::kGray_8:
return MTLPixelFormatR8Unorm == format;
case GrColorType::kAlpha_F16:
return MTLPixelFormatR16Float == format;
case GrColorType::kRGBA_F16:
return MTLPixelFormatRGBA16Float == format;
case GrColorType::kRGBA_F16_Clamped:
return MTLPixelFormatRGBA16Float == format;
case GrColorType::kRG_F32:
return MTLPixelFormatRG32Float == format;
case GrColorType::kRGBA_F32:
return MTLPixelFormatRGBA32Float == format;
case GrColorType::kRGB_ETC1:
#ifdef SK_BUILD_FOR_MAC
return false;
#else
return MTLPixelFormatETC2_RGB8 == format;
#endif
}
}
#endif
static GrSwizzle get_swizzle(const GrBackendFormat& format, GrColorType colorType,
bool forOutput) {
SkASSERT(format.getMtlFormat());
MTLPixelFormat mtlFormat = static_cast<MTLPixelFormat>(*format.getVkFormat());
SkASSERT(format_color_type_valid_pair(mtlFormat, colorType));
switch (colorType) {
case GrColorType::kAlpha_8:
if (mtlFormat == MTLPixelFormatA8Unorm) {
if (!forOutput) {
return GrSwizzle::AAAA();
}
} else {
SkASSERT(mtlFormat == MTLPixelFormatR8Unorm);
if (forOutput) {
return GrSwizzle::AAAA();
} else {
return GrSwizzle::RRRR();
}
}
break;
case GrColorType::kAlpha_F16:
if (forOutput) {
return GrSwizzle::AAAA();
} else {
return GrSwizzle::RRRR();
}
case GrColorType::kGray_8:
if (!forOutput) {
return GrSwizzle::RRRA();
}
break;
case GrColorType::kRGB_888x:
return GrSwizzle::RGB1();
default:
return GrSwizzle::RGBA();
}
return GrSwizzle::RGBA();
}
GrSwizzle GrMtlCaps::getTextureSwizzle(const GrBackendFormat& format, GrColorType colorType) const {
return get_swizzle(format, colorType, false);
}
GrSwizzle GrMtlCaps::getOutputSwizzle(const GrBackendFormat& format, GrColorType colorType) const {
return get_swizzle(format, colorType, true);
}

View File

@ -706,6 +706,7 @@ static bool format_is_srgb(VkFormat format) {
case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
case VK_FORMAT_R5G6B5_UNORM_PACK16:
case VK_FORMAT_B4G4R4A4_UNORM_PACK16:
case VK_FORMAT_R4G4B4A4_UNORM_PACK16:
case VK_FORMAT_R8_UNORM:
case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:
case VK_FORMAT_R32G32B32A32_SFLOAT:
@ -733,6 +734,7 @@ static constexpr VkFormat kVkFormats[] = {
VK_FORMAT_R8G8_UNORM,
VK_FORMAT_A2B10G10R10_UNORM_PACK32,
VK_FORMAT_B4G4R4A4_UNORM_PACK16,
VK_FORMAT_R4G4B4A4_UNORM_PACK16,
VK_FORMAT_R32G32B32A32_SFLOAT,
VK_FORMAT_R32G32_SFLOAT,
VK_FORMAT_R8G8B8A8_SRGB,
@ -984,7 +986,8 @@ static GrPixelConfig validate_image_info(VkFormat format, SkColorType ct, bool h
}
break;
case kARGB_4444_SkColorType:
if (VK_FORMAT_B4G4R4A4_UNORM_PACK16 == format) {
if (VK_FORMAT_B4G4R4A4_UNORM_PACK16 == format ||
VK_FORMAT_R4G4B4A4_UNORM_PACK16 == format) {
return kRGBA_4444_GrPixelConfig;
}
break;
@ -1101,6 +1104,88 @@ GrBackendFormat GrVkCaps::getBackendFormatFromGrColorType(GrColorType ct,
return GrBackendFormat::MakeVk(format);
}
#ifdef SK_DEBUG
static bool format_color_type_valid_pair(VkFormat vkFormat, GrColorType colorType) {
switch (colorType) {
case GrColorType::kUnknown:
return false;
case GrColorType::kAlpha_8:
return VK_FORMAT_R8_UNORM == vkFormat;
case GrColorType::kRGB_565:
return VK_FORMAT_R5G6B5_UNORM_PACK16 == vkFormat;
case GrColorType::kABGR_4444:
return VK_FORMAT_B4G4R4A4_UNORM_PACK16 == vkFormat ||
VK_FORMAT_R4G4B4A4_UNORM_PACK16 == vkFormat;
case GrColorType::kRGBA_8888:
return VK_FORMAT_R8G8B8A8_UNORM == vkFormat || VK_FORMAT_R8G8B8A8_SRGB == vkFormat;
case GrColorType::kRGB_888x:
return VK_FORMAT_R8G8B8_UNORM == vkFormat || VK_FORMAT_R8G8B8A8_UNORM == vkFormat;
case GrColorType::kRG_88:
return VK_FORMAT_R8G8_UNORM == vkFormat;
case GrColorType::kBGRA_8888:
return VK_FORMAT_B8G8R8A8_UNORM == vkFormat || VK_FORMAT_B8G8R8A8_SRGB == vkFormat;
case GrColorType::kRGBA_1010102:
return VK_FORMAT_A2B10G10R10_UNORM_PACK32 == vkFormat;
case GrColorType::kGray_8:
return VK_FORMAT_R8_UNORM == vkFormat;
case GrColorType::kAlpha_F16:
return VK_FORMAT_R16_SFLOAT == vkFormat;
case GrColorType::kRGBA_F16:
return VK_FORMAT_R16G16B16A16_SFLOAT == vkFormat;
case GrColorType::kRGBA_F16_Clamped:
return VK_FORMAT_R16G16B16A16_SFLOAT == vkFormat;
case GrColorType::kRG_F32:
return VK_FORMAT_R32G32_SFLOAT == vkFormat;
case GrColorType::kRGBA_F32:
return VK_FORMAT_R32G32B32A32_SFLOAT == vkFormat;
case GrColorType::kRGB_ETC1:
return VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK == vkFormat;
}
}
#endif
static GrSwizzle get_swizzle(const GrBackendFormat& format, GrColorType colorType,
bool forOutput) {
SkASSERT(format.getVkFormat());
VkFormat vkFormat = *format.getVkFormat();
SkASSERT(format_color_type_valid_pair(vkFormat, colorType));
switch (colorType) {
case GrColorType::kAlpha_8: // fall through
case GrColorType::kAlpha_F16:
if (forOutput) {
return GrSwizzle::AAAA();
} else {
return GrSwizzle::RRRR();
}
case GrColorType::kGray_8:
if (!forOutput) {
return GrSwizzle::RRRA();
}
break;
case GrColorType::kABGR_4444:
if (VK_FORMAT_B4G4R4A4_UNORM_PACK16 == vkFormat) {
return GrSwizzle::BGRA();
}
break;
case GrColorType::kRGB_888x:
if (!forOutput) {
return GrSwizzle::RGB1();
}
default:
return GrSwizzle::RGBA();
}
return GrSwizzle::RGBA();
}
GrSwizzle GrVkCaps::getTextureSwizzle(const GrBackendFormat& format, GrColorType colorType) const {
return get_swizzle(format, colorType, false);
}
GrSwizzle GrVkCaps::getOutputSwizzle(const GrBackendFormat& format, GrColorType colorType) const {
return get_swizzle(format, colorType, true);
}
size_t GrVkCaps::onTransferFromOffsetAlignment(GrColorType bufferColorType) const {
// This GrColorType has 32 bpp but the Vulkan pixel format we use for with may have 24bpp
// (VK_FORMAT_R8G8B8_...) or may be 32 bpp. We don't support post transforming the pixel data

View File

@ -166,6 +166,9 @@ public:
GrBackendFormat getBackendFormatFromGrColorType(GrColorType ct,
GrSRGBEncoded srgbEncoded) const override;
GrSwizzle getTextureSwizzle(const GrBackendFormat&, GrColorType) const override;
GrSwizzle getOutputSwizzle(const GrBackendFormat&, GrColorType) const override;
private:
enum VkVendor {
kAMD_VkVendor = 4098,
@ -220,7 +223,7 @@ private:
SkTDArray<int> fColorSampleCounts;
};
static const size_t kNumVkFormats = 16;
static const size_t kNumVkFormats = 17;
FormatInfo fFormatTable[kNumVkFormats];
const FormatInfo& getFormatInfo(VkFormat) const;