Skip to content

Commit d4cad5f

Browse files
authored
Set bytes planes to non-zero for supercompressed formats. (#988)
* Changes ktxTexture2 reader to reconstruct the bytes planes values on initial construction, if they are zero, and removes post inflation reconstruction. * Changes deflators to keep original bytesPlanes. * Changes BasisLZ/ETC1S encoder to set bytesPlanes values in newly encoded texture. * Changes validator to check bytesPlanes for all formats, including supercompressed, and to issue a warning if bytes planes are zero (unsized). The PR includes a hack command for `ktx` that aided in patching the vast number of test files. This will not be built, in the normal course of development, or released but is being included in the repo to keep for possible future use.
1 parent e0cf193 commit d4cad5f

49 files changed

Lines changed: 689 additions & 148 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

external/dfdutils/dfd.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,21 +194,21 @@ void printDFDJSON(uint32_t *DFD, uint32_t dataSize, uint32_t base_indent, uint32
194194
/* Get the number of components & component size from a DFD for an
195195
* unpacked format.
196196
*/
197-
void
198-
getDFDComponentInfoUnpacked(const uint32_t* DFD, uint32_t* numComponents,
199-
uint32_t* componentByteLength);
197+
void getDFDComponentInfoUnpacked(const uint32_t* DFD, uint32_t* numComponents,
198+
uint32_t* componentByteLength);
200199

201200
/* Return the number of components described by a DFD. */
202201
uint32_t getDFDNumComponents(const uint32_t* DFD);
203202

204-
/* Reconstruct and return the value of bytesPlane0 as it should be for the data
205-
* post-inflation from variable-rate compression.
203+
/* Reconstruct and update the bytesPlane[0-4] fields of an unsized DFD to what
204+
* they were before supercompression.
206205
*/
207-
uint32_t
208-
reconstructDFDBytesPlane0FromSamples(const uint32_t* DFD);
206+
void reconstructDFDBytesPlanesFromSamples(uint32_t* DFD);
209207
/* Deprecated. For backward compatibility. */
210-
void
211-
recreateBytesPlane0FromSampleInfo(const uint32_t* DFD, uint32_t* bytesPlane0);
208+
uint32_t reconstructDFDBytesPlane0FromSamples(const uint32_t* DFD);
209+
/* Deprecated. For backward compatibility. */
210+
void recreateBytesPlane0FromSampleInfo(const uint32_t* DFD,
211+
uint32_t* bytesPlane0);
212212

213213
/** @brief Colourspace primaries information.
214214
*

external/dfdutils/queries.c

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,23 @@ uint32_t getDFDNumComponents(const uint32_t* DFD)
9797
return numComponents;
9898
}
9999

100-
101100
/**
102101
* @~English
103102
* @brief Reconstruct the value of bytesPlane0 from sample info.
104103
*
105-
* Reconstruct the value for data that has been variable-rate compressed so
106-
* has bytesPlane0 = 0. For DFDs that are valid for KTX files. Little-endian
107-
* data only and no multi-plane formats.
104+
* @deprecated Use reconstructDFDBytesPlanesFromSamples. This does not handle
105+
* the possible second plane of the ETC1S model.
108106
*
109-
* @param DFD Pointer to a Data Format Descriptor for which,
110-
* described as 32-bit words in native endianness.
107+
* Reconstruct the value for data that has been variable-rate compressed
108+
* and and whose bytesPlane0 value has been set to 0. For DFDs that
109+
* are valid for KTX files. Little-endian data only and no multi-plane models
110+
* except ETC1S.
111+
*
112+
* @param DFD Pointer to the Data Format Descriptor for which to provide
113+
* the value described as 32-bit words in native endianness.
111114
* Note that this is the whole descriptor, not just
112115
* the basic descriptor block.
116+
* @return The number of bytes a pixel occupies in bytesPlane0.
113117
*/
114118
uint32_t
115119
reconstructDFDBytesPlane0FromSamples(const uint32_t* DFD)
@@ -143,6 +147,10 @@ reconstructDFDBytesPlane0FromSamples(const uint32_t* DFD)
143147
}
144148
}
145149
}
150+
if (KHR_DFDVAL(BDFDB, MODEL) == KHR_DF_MODEL_ETC1S) {
151+
// Size of the first plane.
152+
return 8;
153+
}
146154
for (sampleNumber = 0; sampleNumber < numSamples; ++sampleNumber) {
147155
int32_t sampleBitOffset = KHR_DFDSVAL(BDFDB, sampleNumber, BITOFFSET);
148156
if (sampleBitOffset > largestOffset) {
@@ -157,16 +165,44 @@ reconstructDFDBytesPlane0FromSamples(const uint32_t* DFD)
157165
return bitsPlane0 >> 3U;
158166
}
159167

168+
/**
169+
* @~English
170+
* @brief Reconstruct the values of bytesPlane[01] from sample info.
171+
*
172+
* Reconstruct the values for data that has been variable-rate compressed
173+
* and whose bytesPlane[01] values have been set to 0 and update the
174+
* fields of the target DFD. For DFDs that are valid for KTX files.
175+
* Little-endian data only and no multi-plane models except ETC1S hence
176+
* only looking at bytesPlane0 abd bytesPlane1.
177+
*
178+
* @param DFD Pointer to a Data Format Descriptor for which,
179+
* described as 32-bit words in native endianness.
180+
* Note that this is the whole descriptor, not just
181+
* the basic descriptor block.
182+
*/
183+
184+
void
185+
reconstructDFDBytesPlanesFromSamples(uint32_t* DFD)
186+
{
187+
uint32_t *BDFDB = DFD+1;
188+
189+
KHR_DFDSETVAL(BDFDB, BYTESPLANE0, reconstructDFDBytesPlane0FromSamples(DFD));
190+
if (KHR_DFDVAL(BDFDB, MODEL) == KHR_DF_MODEL_ETC1S) {
191+
if (KHR_DFDSAMPLECOUNT(BDFDB) == 2)
192+
KHR_DFDSETVAL(BDFDB, BYTESPLANE1, 8);
193+
}
194+
}
195+
160196
/**
161197
* @~English
162198
* @brief Reconstruct the value of bytesPlane0 from sample info.
163199
*
164200
* @see reconstructDFDBytesPlane0FromSamples for details.
165201
* @deprecated For backward comparibility only. Use
166-
* reconstructDFDBytesPlane0FromSamples.
202+
* reconstructDFDBytesPlanesFromSamples.
167203
*
168-
* @param DFD Pointer to a Data Format Descriptor for which,
169-
* described as 32-bit words in native endianness.
204+
* @param DFD Pointer to the Data Format Descriptor for which to provide
205+
* the value described as 32-bit words in native endianness.
170206
* Note that this is the whole descriptor, not just
171207
* the basic descriptor block.
172208
* @param bytesPlane0 pointer to a 32-bit word in which the recreated

include/ktx.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,9 @@ ktxTexture1_Destroy(ktxTexture1* This);
10361036
KTX_API ktx_bool_t KTX_APIENTRY
10371037
ktxTexture1_NeedsTranscoding(ktxTexture1* This);
10381038

1039+
KTX_API ktx_error_code_e KTX_APIENTRY
1040+
ktxTexture1_LoadImageData(ktxTexture1* This, ktx_uint8_t* pBuffer, ktx_size_t bufSize);
1041+
10391042
/*
10401043
* These four write a ktxTexture1 object to a KTX v1 file.
10411044
*/
@@ -1131,7 +1134,7 @@ ktxTexture2_GetNumComponents(ktxTexture2* This);
11311134

11321135
KTX_API khr_df_transfer_e KTX_APIENTRY
11331136
ktxTexture2_GetTransferFunction_e(ktxTexture2* This);
1134-
// For backward compatibility
1137+
/* For backward compatibility. */
11351138
KTX_API khr_df_transfer_e KTX_APIENTRY
11361139
ktxTexture2_GetOETF_e(ktxTexture2* This);
11371140
KTX_API ktx_uint32_t KTX_APIENTRY
@@ -1150,14 +1153,23 @@ KTX_API ktx_bool_t KTX_APIENTRY
11501153
ktxTexture2_NeedsTranscoding(ktxTexture2* This);
11511154

11521155
KTX_API ktx_error_code_e KTX_APIENTRY
1153-
ktxTexture2_SeTransferFunction(ktxTexture2* This, khr_df_transfer_e oetf);
1154-
// For backward compatibility
1156+
ktxTexture2_SetTransferFunction(ktxTexture2* This, khr_df_transfer_e tf);
1157+
/* For backward compatibility. */
11551158
KTX_API ktx_error_code_e KTX_APIENTRY
11561159
ktxTexture2_SetOETF(ktxTexture2* This, khr_df_transfer_e oetf);
11571160

11581161
KTX_API ktx_error_code_e KTX_APIENTRY
11591162
ktxTexture2_SetPrimaries(ktxTexture2* This, khr_df_primaries_e primaries);
11601163

1164+
KTX_API ktx_error_code_e KTX_APIENTRY
1165+
ktxTexture2_LoadImageData(ktxTexture2* This, ktx_uint8_t* pBuffer, ktx_size_t bufSize);
1166+
/*
1167+
* For rare testing scenarios. Use ktxTexture2_LoadImageData.
1168+
*/
1169+
KTX_API ktx_error_code_e KTX_APIENTRY
1170+
ktxTexture2_LoadDeflatedImageData(ktxTexture2* This,
1171+
ktx_uint8_t* pBuffer, ktx_size_t bufSize);
1172+
11611173
/*
11621174
* These four write a ktxTexture2 object to a KTX v2 file.
11631175
*/

lib/astc_codec.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pthread_create(pthread_t* thread, const pthread_attr_t* attribs,
5353
#pragma clang diagnostic push
5454
#pragma clang diagnostic ignored "-Wcast-function-type-mismatch"
5555
#endif
56-
LPTHREAD_START_ROUTINE func = reinterpret_cast<LPTHREAD_START_ROUTINE>(threadfunc);
56+
LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)(threadfunc);
5757
#ifdef __clang__
5858
#pragma clang diagnostic pop
5959
#endif

lib/basis_encode.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,10 @@ ktxTexture2_rewriteDfd4BasisLzETC1S(ktxTexture2* This,
264264

265265
nbdb[KHR_DF_WORD_TEXELBLOCKDIMENSION0] =
266266
3 | (3 << KHR_DF_SHIFT_TEXELBLOCKDIMENSION1);
267-
// Show it describes an unsized format.
268267
nbdb[KHR_DF_WORD_BYTESPLANE0] = 0; /* bytesPlane3..0 = 0 */
268+
KHR_DFDSETVAL(nbdb, BYTESPLANE0, 8);
269+
if (alphaContent != eNone)
270+
KHR_DFDSETVAL(nbdb, BYTESPLANE1, 8);
269271
nbdb[KHR_DF_WORD_BYTESPLANE4] = 0; /* bytesPlane7..5 = 0 */
270272

271273
for (uint32_t sample = 0; sample < newSampleCount; sample++) {

lib/gl_funcs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ ktxFindOpenGL() {
155155
#endif
156156
pfnWglGetProcAddress =
157157
(PFNGLGETPROCADDRESS)GetProcAddress(module,
158-
"wglGetProcAddress");
158+
"wglGetProcAddress");
159159
#ifdef __clang__
160160
#pragma clang diagnostic pop
161161
#endif

lib/internalexport.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ EXPORTS
4343
ktxTexture2_GetImageOffset
4444
ktxTexture2_calcLevelOffset
4545
ktxTexture2_destruct
46-
reconstructDFDBytesPlane0FromSamples
46+
reconstructDFDBytesPlanesFromSamples
4747
stringToVkFormat
4848
vk2dfd
4949
vkFormatString

lib/internalexport_mingw.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ EXPORTS
4343
ktxTexture2_GetImageOffset
4444
ktxTexture2_calcLevelOffset
4545
ktxTexture2_destruct
46-
reconstructDFDBytesPlane0FromSamples
46+
reconstructDFDBytesPlanesFromSamples
4747
stringToVkFormat
4848
vk2dfd
4949
vkFormatString

0 commit comments

Comments
 (0)