@@ -198,8 +198,7 @@ impl Texture {
198198 & mut unswizzled,
199199 texture. width as usize ,
200200 texture. height as usize ,
201- texture. format . block_size ( ) ,
202- texture. format . pixel_block_size ( ) ,
201+ texture. format ,
203202 ) ;
204203 Ok ( ( texture, unswizzled, comment) )
205204 } else {
@@ -571,25 +570,37 @@ mod swizzle {
571570 }
572571
573572 pub ( crate ) mod ps4 {
573+ use crate :: gui:: dxgi:: GcnSurfaceFormat ;
574+
574575 // https://github.com/tge-was-taken/GFD-Studio/blob/dad6c2183a6ec0716c3943b71991733bfbd4649d/GFDLibrary/Textures/Swizzle/PS4SwizzleAlgorithm.cs#L20
575576 fn do_swizzle (
576577 source : & [ u8 ] ,
577578 destination : & mut [ u8 ] ,
578579 width : usize ,
579580 height : usize ,
580- block_size : usize ,
581- pixel_block_size : usize ,
581+ format : GcnSurfaceFormat ,
582582 unswizzle : bool ,
583583 ) {
584- let width_pow2 = width. next_power_of_two ( ) ;
585- let height_pow2 = height. next_power_of_two ( ) ;
584+ let pixel_block_size = format. pixel_block_size ( ) ;
585+ let block_size = format. block_size ( ) ;
586+
587+ let width_src = if format. is_compressed ( ) {
588+ width. next_power_of_two ( )
589+ } else {
590+ width
591+ } ;
592+ let height_src = if format. is_compressed ( ) {
593+ height. next_power_of_two ( )
594+ } else {
595+ height
596+ } ;
586597
587598 let width_texels_dest = width / pixel_block_size;
588599 let height_texels_dest = height / pixel_block_size;
589600
590- let width_texels = width_pow2 / pixel_block_size;
601+ let width_texels = width_src / pixel_block_size;
591602 let width_texels_aligned = ( width_texels + 7 ) / 8 ;
592- let height_texels = height_pow2 / pixel_block_size;
603+ let height_texels = height_src / pixel_block_size;
593604 let height_texels_aligned = ( height_texels + 7 ) / 8 ;
594605 let mut data_index = 0 ;
595606
@@ -611,8 +622,12 @@ mod swizzle {
611622 ( dest_index, data_index)
612623 } ;
613624
614- destination[ dst..dst + block_size]
615- . copy_from_slice ( & source[ src..src + block_size] ) ;
625+ if ( src + block_size) < source. len ( )
626+ && ( dst + block_size) < destination. len ( )
627+ {
628+ destination[ dst..dst + block_size]
629+ . copy_from_slice ( & source[ src..src + block_size] ) ;
630+ }
616631 }
617632
618633 data_index += block_size;
@@ -626,19 +641,10 @@ mod swizzle {
626641 destination : & mut Vec < u8 > ,
627642 width : usize ,
628643 height : usize ,
629- block_size : usize ,
630- pixel_block_size : usize ,
644+ format : GcnSurfaceFormat ,
631645 ) {
632646 destination. resize ( source. len ( ) , 0 ) ;
633- do_swizzle (
634- source,
635- destination,
636- width,
637- height,
638- block_size,
639- pixel_block_size,
640- true ,
641- ) ;
647+ do_swizzle ( source, destination, width, height, format, true ) ;
642648 }
643649 }
644650}
0 commit comments