Skip to content

Commit a7f69c2

Browse files
egdanielSkia Commit-Bot
authored and
Skia Commit-Bot
committed
Add stencil formats to all the backends.
Currently there is nothing using these formats, but this change just adds them along with a quaries about their properties. They will be used in a follow up change. Bug: skia:10727 Change-Id: Iaaf13baf372799d47c65bd974fd204a32be57617 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/322622 Commit-Queue: Greg Daniel <[email protected]> Reviewed-by: Brian Salomon <[email protected]>
1 parent c021360 commit a7f69c2

18 files changed

+283
-28
lines changed

include/gpu/GrBackendSurface.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ class SK_API GrBackendFormat {
104104
}
105105
#endif
106106

107-
static GrBackendFormat MakeMock(GrColorType colorType, SkImage::CompressionType compression);
107+
static GrBackendFormat MakeMock(GrColorType colorType, SkImage::CompressionType compression,
108+
bool isStencilFormat = false);
108109

109110
bool operator==(const GrBackendFormat& that) const;
110111
bool operator!=(const GrBackendFormat& that) const { return !(*this == that); }
@@ -157,12 +158,13 @@ class SK_API GrBackendFormat {
157158
#endif
158159

159160
/**
160-
* If the backend API is not Mock these two calls will return kUnknown and kNone, respectively.
161-
* Otherwise, if the compression type is kNone then the GrColorType will be valid. If the
162-
* compression type is anything other then kNone than the GrColorType will be kUnknown.
161+
* If the backend API is not Mock these three calls will return kUnknown, kNone or false,
162+
* respectively. Otherwise, only one of the following can be true. The GrColorType is not
163+
* kUnknown, the compression type is not kNone, or this is a mock stencil format.
163164
*/
164165
GrColorType asMockColorType() const;
165166
SkImage::CompressionType asMockCompressionType() const;
167+
bool isMockStencilFormat() const;
166168

167169
// If possible, copies the GrBackendFormat and forces the texture type to be Texture2D. If the
168170
// GrBackendFormat was for Vulkan and it originally had a GrVkYcbcrConversionInfo, we will
@@ -193,17 +195,21 @@ class SK_API GrBackendFormat {
193195
GrBackendFormat(DXGI_FORMAT dxgiFormat);
194196
#endif
195197

196-
GrBackendFormat(GrColorType, SkImage::CompressionType);
198+
GrBackendFormat(GrColorType, SkImage::CompressionType, bool isStencilFormat);
199+
200+
#ifdef SK_DEBUG
201+
bool validateMock() const;
202+
#endif
197203

198204
GrBackendApi fBackend = GrBackendApi::kMock;
199205
bool fValid = false;
200206

201207
union {
202-
GrGLenum fGLFormat; // the sized, internal format of the GL resource
208+
GrGLenum fGLFormat; // the sized, internal format of the GL resource
203209
struct {
204210
VkFormat fFormat;
205211
GrVkYcbcrConversionInfo fYcbcrConversionInfo;
206-
} fVk;
212+
} fVk;
207213
#ifdef SK_DAWN
208214
wgpu::TextureFormat fDawnFormat;
209215
#endif
@@ -218,7 +224,8 @@ class SK_API GrBackendFormat {
218224
struct {
219225
GrColorType fColorType;
220226
SkImage::CompressionType fCompressionType;
221-
} fMock;
227+
bool fIsStencilFormat;
228+
} fMock;
222229
};
223230
GrTextureType fTextureType = GrTextureType::kNone;
224231
};

include/gpu/gl/GrGLTypes.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,14 @@ enum class GrGLFormat {
8181
kRG16F,
8282
kLUMINANCE16F,
8383

84-
kLast = kLUMINANCE16F
84+
kLastColorFormat = kLUMINANCE16F,
85+
86+
// Depth/Stencil formats
87+
kSTENCIL_INDEX8,
88+
kSTENCIL_INDEX16,
89+
kDEPTH24_STENCIL8,
90+
91+
kLast = kDEPTH24_STENCIL8
8592
};
8693

8794
///////////////////////////////////////////////////////////////////////////////

include/private/GrGLTypesPriv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#ifndef GrGLTypesPriv_DEFINED
1212
#define GrGLTypesPriv_DEFINED
1313

14-
static constexpr int kGrGLFormatCount = static_cast<int>(GrGLFormat::kLast) + 1;
14+
static constexpr int kGrGLColorFormatCount = static_cast<int>(GrGLFormat::kLastColorFormat) + 1;
1515

1616
class GrGLTextureParameters : public SkNVRefCnt<GrGLTextureParameters> {
1717
public:

src/gpu/GrBackendSurface.cpp

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,15 @@ bool GrBackendFormat::asDxgiFormat(DXGI_FORMAT* dxgiFormat) const {
199199
}
200200
#endif
201201

202-
GrBackendFormat::GrBackendFormat(GrColorType colorType, SkImage::CompressionType compression)
202+
GrBackendFormat::GrBackendFormat(GrColorType colorType, SkImage::CompressionType compression,
203+
bool isStencilFormat)
203204
: fBackend(GrBackendApi::kMock)
204205
, fValid(true)
205206
, fTextureType(GrTextureType::k2D) {
206207
fMock.fColorType = colorType;
207208
fMock.fCompressionType = compression;
209+
fMock.fIsStencilFormat = isStencilFormat;
210+
SkASSERT(this->validateMock());
208211
}
209212

210213
uint32_t GrBackendFormat::channelMask() const {
@@ -240,11 +243,25 @@ uint32_t GrBackendFormat::channelMask() const {
240243
}
241244
}
242245

246+
#ifdef SK_DEBUG
247+
bool GrBackendFormat::validateMock() const {
248+
int trueStates = 0;
249+
if (fMock.fCompressionType != SkImage::CompressionType::kNone) {
250+
trueStates++;
251+
}
252+
if (fMock.fColorType != GrColorType::kUnknown) {
253+
trueStates++;
254+
}
255+
if (fMock.fIsStencilFormat) {
256+
trueStates++;
257+
}
258+
return trueStates == 1;
259+
}
260+
#endif
261+
243262
GrColorType GrBackendFormat::asMockColorType() const {
244263
if (this->isValid() && GrBackendApi::kMock == fBackend) {
245-
SkASSERT(fMock.fCompressionType == SkImage::CompressionType::kNone ||
246-
fMock.fColorType == GrColorType::kUnknown);
247-
264+
SkASSERT(this->validateMock());
248265
return fMock.fColorType;
249266
}
250267

@@ -253,15 +270,21 @@ GrColorType GrBackendFormat::asMockColorType() const {
253270

254271
SkImage::CompressionType GrBackendFormat::asMockCompressionType() const {
255272
if (this->isValid() && GrBackendApi::kMock == fBackend) {
256-
SkASSERT(fMock.fCompressionType == SkImage::CompressionType::kNone ||
257-
fMock.fColorType == GrColorType::kUnknown);
258-
273+
SkASSERT(this->validateMock());
259274
return fMock.fCompressionType;
260275
}
261276

262277
return SkImage::CompressionType::kNone;
263278
}
264279

280+
bool GrBackendFormat::isMockStencilFormat() const {
281+
if (this->isValid() && GrBackendApi::kMock == fBackend) {
282+
SkASSERT(this->validateMock());
283+
return fMock.fIsStencilFormat;
284+
}
285+
286+
return false;
287+
}
265288

266289
GrBackendFormat GrBackendFormat::makeTexture2D() const {
267290
GrBackendFormat copy = *this;
@@ -279,8 +302,9 @@ GrBackendFormat GrBackendFormat::makeTexture2D() const {
279302
}
280303

281304
GrBackendFormat GrBackendFormat::MakeMock(GrColorType colorType,
282-
SkImage::CompressionType compression) {
283-
return GrBackendFormat(colorType, compression);
305+
SkImage::CompressionType compression,
306+
bool isStencilFormat) {
307+
return GrBackendFormat(colorType, compression, isStencilFormat);
284308
}
285309

286310
bool GrBackendFormat::operator==(const GrBackendFormat& that) const {

src/gpu/GrBackendUtils.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ size_t GrBackendFormatBytesPerBlock(const GrBackendFormat& format) {
137137
SkImage::CompressionType compression = format.asMockCompressionType();
138138
if (compression != SkImage::CompressionType::kNone) {
139139
return GrCompressedRowBytes(compression, 1);
140+
} else if (format.isMockStencilFormat()) {
141+
static constexpr int kMockStencilSize = 4;
142+
return kMockStencilSize;
140143
}
141144
return GrColorTypeBytesPerPixel(format.asMockColorType());
142145
}
@@ -150,3 +153,52 @@ size_t GrBackendFormatBytesPerPixel(const GrBackendFormat& format) {
150153
}
151154
return GrBackendFormatBytesPerBlock(format);
152155
}
156+
157+
int GrBackendFormatStencilBits(const GrBackendFormat& format) {
158+
switch (format.backend()) {
159+
case GrBackendApi::kOpenGL: {
160+
#ifdef SK_GL
161+
GrGLFormat glFormat = format.asGLFormat();
162+
return GrGLFormatStencilBits(glFormat);
163+
#endif
164+
break;
165+
}
166+
case GrBackendApi::kVulkan: {
167+
#ifdef SK_VULKAN
168+
VkFormat vkFormat;
169+
SkAssertResult(format.asVkFormat(&vkFormat));
170+
return GrVkFormatStencilBits(vkFormat);
171+
#endif
172+
break;
173+
}
174+
case GrBackendApi::kMetal: {
175+
#ifdef SK_METAL
176+
return GrMtlBackendFormatStencilBits(format);
177+
#endif
178+
break;
179+
}
180+
case GrBackendApi::kDirect3D: {
181+
#ifdef SK_DIRECT3D
182+
DXGI_FORMAT dxgiFormat;
183+
SkAssertResult(format.asDxgiFormat(&dxgiFormat));
184+
return GrDxgiFormatStencilBits(dxgiFormat);
185+
#endif
186+
break;
187+
}
188+
case GrBackendApi::kDawn: {
189+
#ifdef SK_DAWN
190+
wgpu::TextureFormat dawnFormat;
191+
SkAssertResult(format.asDawnFormat(&dawnFormat));
192+
return GrDawnFormatStencilBits(dawnFormat);
193+
#endif
194+
break;
195+
}
196+
case GrBackendApi::kMock: {
197+
if (format.isMockStencilFormat()) {
198+
static constexpr int kMockStencilBits = 8;
199+
return kMockStencilBits;
200+
}
201+
}
202+
}
203+
return 0;
204+
}

src/gpu/GrBackendUtils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ size_t GrBackendFormatBytesPerBlock(const GrBackendFormat& format);
2121
// Returns the number of bytes per pixel for the given format. All compressed formats will return 0.
2222
size_t GrBackendFormatBytesPerPixel(const GrBackendFormat& format);
2323

24+
int GrBackendFormatStencilBits(const GrBackendFormat& format);
25+
2426
#endif

src/gpu/d3d/GrD3DUtil.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ static constexpr uint32_t GrDxgiFormatChannels(DXGI_FORMAT format) {
4949
case DXGI_FORMAT_R16G16_UNORM: return kRG_SkColorChannelFlags;
5050
case DXGI_FORMAT_R16G16B16A16_UNORM: return kRGBA_SkColorChannelFlags;
5151
case DXGI_FORMAT_R16G16_FLOAT: return kRG_SkColorChannelFlags;
52+
case DXGI_FORMAT_D24_UNORM_S8_UINT: return 0;
53+
case DXGI_FORMAT_D32_FLOAT_S8X24_UINT: return 0;
5254

5355
default: return 0;
5456
}
@@ -71,11 +73,24 @@ static constexpr size_t GrDxgiFormatBytesPerBlock(DXGI_FORMAT format) {
7173
case DXGI_FORMAT_R16G16_UNORM: return 4;
7274
case DXGI_FORMAT_R16G16B16A16_UNORM: return 8;
7375
case DXGI_FORMAT_R16G16_FLOAT: return 4;
76+
case DXGI_FORMAT_D24_UNORM_S8_UINT: return 4;
77+
case DXGI_FORMAT_D32_FLOAT_S8X24_UINT: return 8;
7478

7579
default: return 0;
7680
}
7781
}
7882

83+
static constexpr int GrDxgiFormatStencilBits(DXGI_FORMAT format) {
84+
switch (format) {
85+
case DXGI_FORMAT_D24_UNORM_S8_UINT:
86+
return 8;
87+
case DXGI_FORMAT_D32_FLOAT_S8X24_UINT:
88+
return 8;
89+
default:
90+
return 0;
91+
}
92+
}
93+
7994
#if defined(SK_DEBUG) || GR_TEST_UTILS
8095
static constexpr const char* GrDxgiFormatToStr(DXGI_FORMAT dxgiFormat) {
8196
switch (dxgiFormat) {
@@ -95,6 +110,8 @@ static constexpr const char* GrDxgiFormatToStr(DXGI_FORMAT dxgiFormat) {
95110
case DXGI_FORMAT_R16G16_UNORM: return "R16G16_UNORM";
96111
case DXGI_FORMAT_R16G16B16A16_UNORM: return "R16G16B16A16_UNORM";
97112
case DXGI_FORMAT_R16G16_FLOAT: return "R16G16_FLOAT";
113+
case DXGI_FORMAT_D24_UNORM_S8_UINT: return "D24_UNORM_S8_UINT";
114+
case DXGI_FORMAT_D32_FLOAT_S8X24_UINT: return "D32_FLOAT_S8X24_UINT";
98115

99116
default: return "Unknown";
100117
}

src/gpu/dawn/GrDawnUtil.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,20 @@ size_t GrDawnBytesPerBlock(wgpu::TextureFormat format) {
1717
case wgpu::TextureFormat::Depth24PlusStencil8:
1818
return 4;
1919
default:
20-
SkASSERT(false);
21-
return 4;
20+
SkUNREACHABLE;
21+
}
22+
}
23+
24+
int GrDawnFormatStencilBits(wgpu::TextureFormat format) {
25+
switch (format) {
26+
case wgpu::TextureFormat::RGBA8Unorm:
27+
case wgpu::TextureFormat::BGRA8Unorm:
28+
case wgpu::TextureFormat::R8Unorm:
29+
return 0;
30+
case wgpu::TextureFormat::Depth24PlusStencil8:
31+
return 8;
32+
default:
33+
SkUNREACHABLE;
2234
}
2335
}
2436

@@ -78,8 +90,7 @@ const char* GrDawnFormatToStr(wgpu::TextureFormat format) {
7890
case wgpu::TextureFormat::Depth24PlusStencil8:
7991
return "Depth24PlusStencil8";
8092
default:
81-
SkASSERT(false);
82-
return "Unknown";
93+
SkUNREACHABLE;
8394
}
8495
}
8596
#endif

src/gpu/dawn/GrDawnUtil.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "dawn/webgpu_cpp.h"
1313

1414
size_t GrDawnBytesPerBlock(wgpu::TextureFormat format);
15+
int GrDawnFormatStencilBits(wgpu::TextureFormat format);
1516
bool GrDawnFormatIsRenderable(wgpu::TextureFormat format);
1617
bool GrColorTypeToDawnFormat(GrColorType colorType, wgpu::TextureFormat* format);
1718
bool GrDawnFormatToGrColorType(wgpu::TextureFormat format, GrColorType* colorType);

src/gpu/gl/GrGLCaps.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ void GrGLCaps::onDumpJSON(SkJSONWriter* writer) const {
12311231

12321232
writer->beginArray("formats");
12331233

1234-
for (int i = 0; i < kGrGLFormatCount; ++i) {
1234+
for (int i = 0; i < kGrGLColorFormatCount; ++i) {
12351235
writer->beginObject(nullptr, false);
12361236
writer->appendHexU32("flags", fFormatTable[i].fFlags);
12371237
writer->appendHexU32("f_type", (uint32_t)fFormatTable[i].fFormatType);
@@ -3073,7 +3073,7 @@ void GrGLCaps::initFormatTable(const GrGLContextInfo& ctxInfo, const GrGLInterfa
30733073
this->setupSampleCounts(ctxInfo, gli);
30743074

30753075
#ifdef SK_DEBUG
3076-
for (int i = 0; i < kGrGLFormatCount; ++i) {
3076+
for (int i = 0; i < kGrGLColorFormatCount; ++i) {
30773077
if (GrGLFormat::kUnknown == static_cast<GrGLFormat>(i)) {
30783078
continue;
30793079
}
@@ -3107,7 +3107,7 @@ void GrGLCaps::setupSampleCounts(const GrGLContextInfo& ctxInfo, const GrGLInter
31073107
sk_ignore_unused_variable(standard);
31083108
GrGLVersion version = ctxInfo.version();
31093109

3110-
for (int i = 0; i < kGrGLFormatCount; ++i) {
3110+
for (int i = 0; i < kGrGLColorFormatCount; ++i) {
31113111
if (FormatInfo::kFBOColorAttachmentWithMSAA_Flag & fFormatTable[i].fFlags) {
31123112
// We assume that MSAA rendering is supported only if we support non-MSAA rendering.
31133113
SkASSERT(FormatInfo::kFBOColorAttachment_Flag & fFormatTable[i].fFlags);

src/gpu/gl/GrGLCaps.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ class GrGLCaps : public GrCaps {
735735
int fColorTypeInfoCount = 0;
736736
};
737737

738-
FormatInfo fFormatTable[kGrGLFormatCount];
738+
FormatInfo fFormatTable[kGrGLColorFormatCount];
739739

740740
FormatInfo& getFormatInfo(GrGLFormat format) { return fFormatTable[static_cast<int>(format)]; }
741741
const FormatInfo& getFormatInfo(GrGLFormat format) const {

src/gpu/gl/GrGLUtil.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,9 @@ bool GrGLFormatIsCompressed(GrGLFormat format) {
643643
case GrGLFormat::kRG16:
644644
case GrGLFormat::kRGBA16:
645645
case GrGLFormat::kRG16F:
646+
case GrGLFormat::kSTENCIL_INDEX8:
647+
case GrGLFormat::kSTENCIL_INDEX16:
648+
case GrGLFormat::kDEPTH24_STENCIL8:
646649
case GrGLFormat::kUnknown:
647650
return false;
648651
}

0 commit comments

Comments
 (0)