@@ -53,18 +53,18 @@ public unsafe Bitmap GetBitmap( int mip )
5353 var height = desc . m_nHeight / d ;
5454 var depth = desc . m_nDepth ;
5555 var outputFormat = floatingPoint ? ImageFormat . RGBA16161616F : ImageFormat . RGBA8888 ;
56- var targetMemoryRequired = NativeEngine . ImageLoader . GetMemRequired ( width , height , 1 , 1 , outputFormat ) ;
56+ var targetMemoryRequired = NativeEngine . ImageLoader . GetMemRequired ( width , height , depth , 1 , outputFormat ) ;
5757
5858 if ( targetMemoryRequired <= 0 )
5959 {
6060 //
6161 // If desc.m_nWidth and height are 0, this is probably the rendersystemempty, which is obviously a disaster
6262 //
6363
64- throw new System . Exception ( $ "targetMemoryRequired <= 0 ({ width } x{ height } { outputFormat } )" ) ;
64+ throw new System . Exception ( $ "targetMemoryRequired <= 0 ({ width } x{ height } x { depth } { outputFormat } )" ) ;
6565 }
6666
67- var bitmap = new Bitmap ( width , height , floatingPoint ) ;
67+ var bitmap = new Bitmap ( width , height * depth , floatingPoint ) ;
6868 var data = bitmap . GetBuffer ( ) ;
6969
7070 if ( data . Length != targetMemoryRequired )
@@ -74,10 +74,18 @@ public unsafe Bitmap GetBitmap( int mip )
7474
7575 fixed ( byte * pData = data )
7676 {
77- var rect = new NativeRect ( 0 , 0 , width , height ) ;
77+ if ( depth > 1 )
78+ {
79+ GetPixels3D ( ( 0 , 0 , 0 , width , height , depth ) , mip , data , outputFormat ) ;
80+ }
81+ else
82+ {
83+ var rect = new NativeRect ( 0 , 0 , width , height ) ;
84+
85+ if ( ! g_pRenderDevice . ReadTexturePixels ( native , ref rect , 0 , mip , ref rect , ( IntPtr ) pData , outputFormat , 0 ) )
86+ return null ;
87+ }
7888
79- if ( ! g_pRenderDevice . ReadTexturePixels ( native , ref rect , 0 , mip , ref rect , ( IntPtr ) pData , outputFormat , 0 ) )
80- return null ;
8189 }
8290
8391 return bitmap ;
@@ -149,8 +157,10 @@ private static int GetImageFormatSize( ImageFormat format, int width, int height
149157 throw new ArgumentException ( $ "{ nameof ( srcRect ) } out of range" ) ;
150158 }
151159
152- var dstStride = ( long ) dstSize . X * pixelSize ;
153- if ( dstStride > int . MaxValue || ( ( long ) dstSize . Y * pixelSize ) > int . MaxValue )
160+ var dstStrideBytes = GetImageFormatSize ( dstFormat , dstSize . X , 1 ) ;
161+ if ( dstStrideBytes <= 0 )
162+ throw new ArgumentException ( $ "{ nameof ( dstSize ) } invalid" ) ;
163+ if ( dstStrideBytes > int . MaxValue )
154164 throw new ArgumentException ( $ "{ nameof ( dstSize ) } too large" ) ;
155165
156166 var sliceSize = GetImageFormatSize ( dstFormat , dstSize . X , dstSize . Y ) ;
@@ -173,7 +183,7 @@ private static int GetImageFormatSize( ImageFormat format, int width, int height
173183
174184 fixed ( T * dataPtr = dstData )
175185 {
176- if ( ! g_pRenderDevice . ReadTexturePixels ( native , ref nativeSrcRect , slice , mip , ref nativeDstRect , ( IntPtr ) dataPtr , dstFormat , ( int ) dstStride ) )
186+ if ( ! g_pRenderDevice . ReadTexturePixels ( native , ref nativeSrcRect , slice , mip , ref nativeDstRect , ( IntPtr ) dataPtr , dstFormat , dstStrideBytes ) )
177187 throw new Exception ( "Unable to read texture pixels" ) ;
178188 }
179189 }
0 commit comments