@@ -41,15 +41,15 @@ getDFDComponentInfoUnpacked(const uint32_t* DFD, uint32_t* numComponents,
4141{
4242 const uint32_t * BDFDB = DFD + 1 ;
4343 uint32_t numSamples = KHR_DFDSAMPLECOUNT (BDFDB );
44- uint32_t sampleCounter ;
44+ uint32_t sampleNumber ;
4545 uint32_t currentChannel = ~0U ; /* Don't start matched. */
4646
4747 /* This is specifically for unpacked formats which means the size of */
4848 /* each component is the same. */
4949 * numComponents = 0 ;
50- for (sampleCounter = 0 ; sampleCounter < numSamples ; ++ sampleCounter ) {
51- uint32_t sampleByteLength = (KHR_DFDSVAL (BDFDB , sampleCounter , BITLENGTH ) + 1 ) >> 3U ;
52- uint32_t sampleChannel = KHR_DFDSVAL (BDFDB , sampleCounter , CHANNELID );
50+ for (sampleNumber = 0 ; sampleNumber < numSamples ; ++ sampleNumber ) {
51+ uint32_t sampleByteLength = (KHR_DFDSVAL (BDFDB , sampleNumber , BITLENGTH ) + 1 ) >> 3U ;
52+ uint32_t sampleChannel = KHR_DFDSVAL (BDFDB , sampleNumber , CHANNELID );
5353
5454 if (sampleChannel == currentChannel ) {
5555 /* Continuation of the same channel. */
@@ -85,10 +85,10 @@ uint32_t getDFDNumComponents(const uint32_t* DFD)
8585 uint32_t currentChannel = ~0U ; /* Don't start matched. */
8686 uint32_t numComponents = 0 ;
8787 uint32_t numSamples = KHR_DFDSAMPLECOUNT (BDFDB );
88- uint32_t sampleCounter ;
88+ uint32_t sampleNumber ;
8989
90- for (sampleCounter = 0 ; sampleCounter < numSamples ; ++ sampleCounter ) {
91- uint32_t sampleChannel = KHR_DFDSVAL (BDFDB , sampleCounter , CHANNELID );
90+ for (sampleNumber = 0 ; sampleNumber < numSamples ; ++ sampleNumber ) {
91+ uint32_t sampleChannel = KHR_DFDSVAL (BDFDB , sampleNumber , CHANNELID );
9292 if (sampleChannel != currentChannel ) {
9393 numComponents ++ ;
9494 currentChannel = sampleChannel ;
@@ -97,50 +97,83 @@ uint32_t getDFDNumComponents(const uint32_t* DFD)
9797 return numComponents ;
9898}
9999
100+
100101/**
101102 * @~English
102- * @brief Recreate the value of bytesPlane0 from sample info.
103+ * @brief Reconstruct the value of bytesPlane0 from sample info.
103104 *
104- * This can be use to recreate the value of bytesPlane0 for data that
105- * has been variable-rate compressed so has bytesPlane0 = 0. For DFDs
106- * that are valid for KTX files. Little-endian data only and no multi-plane
107- * formats.
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.
108108 *
109109 * @param DFD Pointer to a Data Format Descriptor for which,
110110 * described as 32-bit words in native endianness.
111111 * Note that this is the whole descriptor, not just
112112 * the basic descriptor block.
113- * @param bytesPlane0 pointer to a 32-bit word in which the recreated
114- * value of bytesPlane0 will be written.
115113 */
116- void
117- recreateBytesPlane0FromSampleInfo (const uint32_t * DFD , uint32_t * bytesPlane0 )
114+ uint32_t
115+ reconstructDFDBytesPlane0FromSamples (const uint32_t * DFD )
118116{
119117 const uint32_t * BDFDB = DFD + 1 ;
120118 uint32_t numSamples = KHR_DFDSAMPLECOUNT (BDFDB );
121- uint32_t sampleCounter ;
119+ uint32_t sampleNumber ;
122120
123121 uint32_t bitsPlane0 = 0 ;
124- uint32_t * bitOffsets = malloc (sizeof (uint32_t ) * numSamples );
125- memset (bitOffsets , -1 , sizeof (uint32_t ) * numSamples );
126- for (sampleCounter = 0 ; sampleCounter < numSamples ; ++ sampleCounter ) {
127- uint32_t sampleBitOffset = KHR_DFDSVAL (BDFDB , sampleCounter , BITOFFSET );
128- /* The sample bitLength field stores the bit length - 1. */
129- uint32_t sampleBitLength = KHR_DFDSVAL (BDFDB , sampleCounter , BITLENGTH ) + 1 ;
130- uint32_t i ;
131- for (i = 0 ; i < numSamples ; i ++ ) {
132- if (sampleBitOffset == bitOffsets [i ]) {
133- // This sample is being repeated as in e.g. RGB9E5.
134- break ;
122+ int32_t largestOffset = 0 ;
123+ uint32_t sampleNumberWithLargestOffset = 0 ;
124+
125+ // Special case these depth{,-stencil} formats. The unused bits are
126+ // in the MSBs so have no visibility in the DFD therefore the max offset
127+ // algorithm below returns a value that is too small.
128+ if (KHR_DFDSVAL (BDFDB , 0 , CHANNELID ) == KHR_DF_CHANNEL_COMMON_DEPTH ) {
129+ if (numSamples == 1 ) {
130+ if (KHR_DFDSVAL (BDFDB , 0 , BITLENGTH ) + 1 == 24 ) {
131+ // X8_D24_UNORM_PACK32,
132+ return 4 ;
133+ }
134+ } else if (numSamples == 2 ) {
135+ if (KHR_DFDSVAL (BDFDB , 0 , BITLENGTH ) + 1 == 16 ) {
136+ // D16_UNORM_S8_UINT
137+ return 4 ;
138+ }
139+ if (KHR_DFDSVAL (BDFDB , 0 , BITLENGTH ) + 1 == 32
140+ && KHR_DFDSVAL (BDFDB , 1 , CHANNELID ) == KHR_DF_CHANNEL_COMMON_STENCIL ) {
141+ // D32_SFLOAT_S8_UINT
142+ return 8 ;
135143 }
136144 }
137- if (i == numSamples ) {
138- // Previously unseen bitOffset. Bump size.
139- bitsPlane0 += sampleBitLength ;
140- bitOffsets [sampleCounter ] = sampleBitOffset ;
145+ }
146+ for (sampleNumber = 0 ; sampleNumber < numSamples ; ++ sampleNumber ) {
147+ int32_t sampleBitOffset = KHR_DFDSVAL (BDFDB , sampleNumber , BITOFFSET );
148+ if (sampleBitOffset > largestOffset ) {
149+ largestOffset = sampleBitOffset ;
150+ sampleNumberWithLargestOffset = sampleNumber ;
141151 }
142152 }
143- free (bitOffsets );
144- * bytesPlane0 = bitsPlane0 >> 3U ;
153+
154+ /* The sample bitLength field stores the bit length - 1. */
155+ uint32_t sampleBitLength = KHR_DFDSVAL (BDFDB , sampleNumberWithLargestOffset , BITLENGTH ) + 1 ;
156+ bitsPlane0 = largestOffset + sampleBitLength ;
157+ return bitsPlane0 >> 3U ;
145158}
146159
160+ /**
161+ * @~English
162+ * @brief Reconstruct the value of bytesPlane0 from sample info.
163+ *
164+ * @see reconstructDFDBytesPlane0FromSamples for details.
165+ * @deprecated For backward comparibility only. Use
166+ * reconstructDFDBytesPlane0FromSamples.
167+ *
168+ * @param DFD Pointer to a Data Format Descriptor for which,
169+ * described as 32-bit words in native endianness.
170+ * Note that this is the whole descriptor, not just
171+ * the basic descriptor block.
172+ * @param bytesPlane0 pointer to a 32-bit word in which the recreated
173+ * value of bytesPlane0 will be written.
174+ */
175+ void
176+ recreateBytesPlane0FromSampleInfo (const uint32_t * DFD , uint32_t * bytesPlane0 )
177+ {
178+ * bytesPlane0 = reconstructDFDBytesPlane0FromSamples (DFD );
179+ }
0 commit comments