Files
sbox-public/engine/Sandbox.Tools/Assets/ResourceCompile/TextureCompiler.cs
Sam Pavlovic 1e0d0ea828 Texture Cube Fixes (#3713)
* Take texture desc from actual texture instead of managed for texturecompiler, we assume 6 slices for cubemaps when vtex writes it as 1 depth

* Create TextureCube properly with mips from C# part

* Prototype envmap probe caching

* Simplify, get memory size right when we want a specific number of mips

* GetBitmap works correctly with cubemaps

* Cubemap mip writes goes from highest to lowest mip

* Native can load multiple mipmaps into cubemap texture

* Revert Envmap caching stuff, will make a [Cached] attribute, leave cubemap fixes on branch

* Format
2026-01-12 10:13:54 -03:00

96 lines
2.6 KiB
C#

using Sandbox.Resources;
namespace Editor;
[Expose]
[ResourceIdentity( "texture" )]
[ResourceIdentity( "vtex" )]
public class TextureResourceCompiler : ResourceCompiler
{
public TextureResourceCompiler()
{
}
/// <summary>
/// We found an embedded resource definition.
/// 1. Find the TextureGenerator
/// 2. Create a child texture resource with a deterministic name
/// 3. Put the provided compile data in that and let it compile
/// 4. Store a reference to the compiled version in the json
/// </summary>
protected override bool CompileEmbedded( ref EmbeddedResource embed )
{
return CompileEmbeddedResource<Texture>( ref embed, "textures", "vtex", FileSystem.Transient );
}
override protected async Task<bool> Compile()
{
if ( !TryParseEmbeddedResource( out var serialized ) || !serialized.HasValue )
return false;
var generator = ResourceGenerator.Create<Texture>( serialized.Value );
if ( generator is null || !generator.CacheToDisk ) return false;
var texture = await generator.CreateAsync( new ResourceGenerator.Options { ForDisk = true, Compiler = this }, default );
if ( texture is null ) return false;
Context.ResourceVersion = 1;
var desc = texture.Desc;
//
// Note: mipmaps don't work with PNG format ya doink
//
int width = desc.m_nWidth;
int height = desc.m_nHeight;
int depth = desc.m_nDepth;
int mipCount = desc.m_nNumMipLevels;
var writer = new VTexWriter();
for ( var mip = 0; mip < mipCount; mip++ )
{
var bitmap = texture.GetBitmap( mip );
writer.SetTexture( bitmap, mip );
}
writer.Header.Width = (ushort)width;
writer.Header.Height = (ushort)height;
writer.Header.Depth = (ushort)depth;
writer.Header.MipCount = (byte)mipCount;
var flags = VTexWriter.VTEX_Flags_t.NONE;
if ( desc.m_nFlags.HasFlag( NativeEngine.RuntimeTextureSpecificationFlags.TSPEC_CUBE_TEXTURE ) )
{
flags |= VTexWriter.VTEX_Flags_t.VTEX_FLAG_CUBE_TEXTURE;
}
if ( desc.m_nFlags.HasFlag( NativeEngine.RuntimeTextureSpecificationFlags.TSPEC_VOLUME_TEXTURE ) )
{
flags |= VTexWriter.VTEX_Flags_t.VTEX_FLAG_VOLUME_TEXTURE;
}
if ( desc.m_nFlags.HasFlag( NativeEngine.RuntimeTextureSpecificationFlags.TSPEC_TEXTURE_ARRAY ) )
{
flags |= VTexWriter.VTEX_Flags_t.VTEX_FLAG_TEXTURE_ARRAY;
}
if ( desc.m_nFlags.HasFlag( NativeEngine.RuntimeTextureSpecificationFlags.TSPEC_NO_LOD ) )
{
flags |= VTexWriter.VTEX_Flags_t.VTEX_FLAG_NO_LOD;
}
writer.Header.Flags = flags;
writer.CalculateFormat();
Context.Data.Write( writer.GetData() );
Context.StreamingData.Write( writer.GetStreamingData() );
return true;
}
}