@@ -206,34 +206,17 @@ public void SaveToDDS(Stream stream, bool keepOpen, int? mips, uint? width = nul
206206 // Console.Out.WriteLine($"{mips ?? Header.MipCount} {width ?? Header.Width} {height ?? Header.Height} {surfaces ?? Header.Surfaces}");
207207
208208 var targetMips = mips ?? Header . MipCount ;
209- uint saveMipCount = Header . MipCount ;
210- int savePayloadCount = 0 ;
211- if ( PayloadRequired ) {
212- /*Console.Out.Write($"-- MIP DEBUG\n {Header.MipCount} ");
213- foreach (var payload in Payloads)
214- {
215- if (payload == null)
216- {
217- Console.Out.WriteLine("MISSING!!");
218- continue;
219- }
220- Console.Out.Write($"{payload.Header.Mips} ");
221- }*/
222-
223- var payloadIdx = Payloads . Length - 1 ;
224- while ( payloadIdx >= 0 )
225- {
226- var mipLimitIncludedByThisPayload = Header . MipCount - Payloads [ payloadIdx -- ] . Header . Mips ;
227- saveMipCount = mipLimitIncludedByThisPayload ;
228- savePayloadCount ++ ;
229-
230- if ( mipLimitIncludedByThisPayload >= targetMips ) break ;
231- }
232209
233- //Console.Out.WriteLine($"---- {targetMips} {saveMipCount} {payloadIdx} {Payloads.Length} {savePayloadCount} -----");
210+ int savePayloadCount ;
211+ if ( targetMips == 1 ) {
212+ // optimization for general case
213+ // we don't need to serialize mips if we only want highest
214+ savePayloadCount = 1 ;
215+ } else {
216+ savePayloadCount = Header . PayloadCount ;
234217 }
235218
236- TextureTypes . DDSHeader dds = Header . ToDDSHeader ( ( int ) saveMipCount , width ?? Header . Width , height ?? Header . Height , surfaces ?? Header . Surfaces ) ;
219+ TextureTypes . DDSHeader dds = Header . ToDDSHeader ( targetMips , width ?? Header . Width , height ?? Header . Height , surfaces ?? Header . Surfaces ) ;
237220 ddsWriter . Write ( dds ) ;
238221 if ( dds . Format . FourCC == 0x30315844 ) {
239222 var dimension = TextureTypes . D3D10_RESOURCE_DIMENSION . UNKNOWN ;
0 commit comments