Skip to content

Commit 9279252

Browse files
authored
Fix Texture Generator with 3D Textures (#3632)
* Fix Texture.Read having wrong strides depending on texture format https://files.facepunch.com/sampavlovic/1b1011b1/qrenderdoc_fXecqvuzKo.png https://files.facepunch.com/sampavlovic/1b1011b1/qrenderdoc_zHXXguaCkG.png Makes texture reads and reuploads work correctly * Fix TextureCompiler not compiling 3D textures properly, make Texture.GetBitmap work with 3D textures * format
1 parent ac30658 commit 9279252

2 files changed

Lines changed: 44 additions & 9 deletions

File tree

engine/Sandbox.Engine/Resources/Textures/Texture.Read.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

engine/Sandbox.Tools/Assets/ResourceCompile/TextureCompiler.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,31 @@ override protected async Task<bool> Compile()
5959
writer.Header.Depth = (ushort)depth;
6060
writer.Header.MipCount = (byte)mipCount;
6161

62+
var desc = texture.Desc;
63+
var flags = VTexWriter.VTEX_Flags_t.NONE;
64+
65+
if ( desc.m_nFlags.HasFlag( NativeEngine.RuntimeTextureSpecificationFlags.TSPEC_CUBE_TEXTURE ) )
66+
{
67+
flags |= VTexWriter.VTEX_Flags_t.VTEX_FLAG_CUBE_TEXTURE;
68+
}
69+
70+
if ( desc.m_nFlags.HasFlag( NativeEngine.RuntimeTextureSpecificationFlags.TSPEC_VOLUME_TEXTURE ) )
71+
{
72+
flags |= VTexWriter.VTEX_Flags_t.VTEX_FLAG_VOLUME_TEXTURE;
73+
}
74+
75+
if ( desc.m_nFlags.HasFlag( NativeEngine.RuntimeTextureSpecificationFlags.TSPEC_TEXTURE_ARRAY ) )
76+
{
77+
flags |= VTexWriter.VTEX_Flags_t.VTEX_FLAG_TEXTURE_ARRAY;
78+
}
79+
80+
if ( desc.m_nFlags.HasFlag( NativeEngine.RuntimeTextureSpecificationFlags.TSPEC_NO_LOD ) )
81+
{
82+
flags |= VTexWriter.VTEX_Flags_t.VTEX_FLAG_NO_LOD;
83+
}
84+
85+
writer.Header.Flags = flags;
86+
6287
writer.CalculateFormat();
6388

6489
Context.Data.Write( writer.GetData() );

0 commit comments

Comments
 (0)