ContentTools

Chuck Smith edited this page 2026-11-25 01:11:13 +00:00 · 1 commit

ContentTools on Linux (#12)

Written 2026-11-23 for issue #12 (ContentTools builds and makes the same assets on Linux), after #38 (ufbx). Geometry processing and FBX import already build on Linux (the CMake static library ContentTools). This note covers the rest: textures, IBL, primitive meshes and the shared library the editor loads. Section 2 holds the user's decisions, section 4 the plan, section 5 the smaller decisions, section 6 what was built, tested and measured.

1. What is there today

File Lines Windows-only parts
TextureImporter.cpp 1,066 DirectXTex (vcpkg), WIC loading, DXGI adapter list + D3D11 device (LoadLibrary of dxgi.dll/d3d11.dll), GPU BC6H/BC7 compression, WRL ComPtr
EnvMapProcessing.cpp 963 D3D11 compute for equirectangular → cube map, diffuse and specular prefiltering, the BRDF LUT; a CPU equirectangular path on 5 raw std::threads + the caller
EnvMapProcessing.hlsl 356 none: four compute shaders (cs_5_0, fxc through CompileShaders.cmd, embedded as x64\Compiled\*.inc)
NormalMapIdentification.cpp 157 DirectXTex only
PrimitiveMesh.cpp, ContentTools.cpp 399 none found (not built on Linux yet)

The CPU fallback for BC compression already exists: compress_image() uses DirectXTex's CPU encoders (TEX_COMPRESS_PARALLEL) when there is no D3D11 device or the format isn't BC6H/BC7.

2. Decisions (user, 2026-11-23)

  1. DirectXTex stays, on both OSes, downloaded and hash-pinned like ufbx. On Linux it has no WIC, so PNG and JPEG load through stb_image; DDS, TGA and HDR use DirectXTex's own readers on both. Replacing DirectXTex is investigated in #81 (Unreal uses Oodle Texture, Unity ISPC-based encoders and bc7e).
  2. IBL runs on the GPU, through Vulkan compute, on both OSes. The HLSL stays (no C++ port); DXC compiles it to SPIR-V. Unit tests can't check it, GraphicsTests can (lavapipe in CI).
  3. No D3D11 at all. The D3D11 IBL code, CompileShaders.cmd and the DXGI/D3D11 device go. BC6H/BC7 compression uses DirectXTex's CPU encoders on both OSes until #81 decides on a custom (GPU) encoder.

3. Design

  • Textures. TextureImporter.cpp loses everything D3D11/DXGI/WRL. load_from_file() picks the reader by extension: DDS/TGA/HDR → DirectXTex, everything else → WIC on Windows and stb_image on Linux (stb's output copied into a ScratchImage, 8-bit RGBA or float RGBA for 16-bit/HDR sources; sRGB as WIC decides today: the import setting ignore_srgb is honored the same way). Compression is always the CPU path.
  • IBL on Vulkan. A new ContentTools/VulkanCompute.{h,cpp}: a headless Vulkan 1.3 device (no surface, one compute queue, the most capable GPU as the D3D11 code picked it, lavapipe as a last resort), made at the first IBL call and destroyed in ShutDownContentTools(). Each pass uploads its input, dispatches the same entry points with the same constants, and downloads the result into the ScratchImage the rest of the code already uses. Validation in Debug like the engine (PRIMAL_VULKAN_VALIDATION). Functions come through the engine's loader pattern (VulkanLoader.cpp compiled into ContentTools as well: its globals are separate per binary), memory through VMA (already a dependency).
  • Shaders. ContentTools/ContentTools.shaders, a shader pack manifest with the four compute groups, compiled at build time by PrimalShaderCompiler --pack ... vulkan into x64/<Config>/shaders/ContentTools.shaderpack (both build systems, SPIR-V only), and read with primal.shader_pack next to the library. The HLSL gets explicit [[vk::binding]] so ContentTools doesn't depend on the engine's register shifts.
  • The CPU equirectangular path runs its six faces with jobs::parallel_for(6, 1, ...). ContentTools links the engine library on both OSes: in the editor's process it shares the job pool EngineDLL starts; without a pool the jobs run on the caller.
  • The library. CMake builds the sources once (an object library) into the static ContentTools that UnitTests links and a shared libContentTools.so with the EDITOR_INTERFACE exports for the Linux editor (#13; the C# "ContentTools.dll" name is #13's to resolve).

4. Plan

  1. Dependencies. DirectXTex may2026 and stb (stb_image.h, pinned to a commit) downloaded in cmake/PrimalDependencies.cmake and ContentTools/ThirdParty.targets, same SHA-256. On Linux DirectXTex also needs DirectX-Headers (dxgiformat.h and the WSL stubs; pinned v1.721.2). vcpkg goes from ContentTools.
  2. Textures on Linux. TextureImporter.cpp and NormalMapIdentification.cpp without D3D11/WRL, stb_image on Linux, D3D11 BC compression removed. PrimitiveMesh.cpp and ContentTools.cpp in the CMake build. Unit tests: loading each source format from small files in UnitTests/Data/, BC1/BC3/BC4/BC5/BC6H/BC7 round trips (PSNR above a floor), mip counts, normal map detection, cube maps from six images, primitive meshes.
  3. IBL on Vulkan. The manifest and pack, VulkanCompute.*, the four passes, the D3D11 code and CompileShaders.cmd removed, the CPU equirectangular path on jobs. GraphicsTests: GPU equirectangular → cube map against the CPU path, the BRDF LUT against reference values, specular mip 0 against its input, diffuse of a constant environment = that constant; validation clean.
  4. The shared library and its exports on Linux; ci.sh builds it.
  5. Windows. ContentTools.vcxproj: the DirectXTex sources, stb not used, the pack build, the engine library, the Vulkan headers. Checked in the VM: build, UnitTests, GraphicsTests (IBL on lavapipe), and the outputs compared with C:\winvm\baseline\ (x64/*.texture from the SDK-era DLL; IBL within a tolerance, the rest identical). The editor imports a texture and an HDR environment map.
  6. Measure an HDR environment map import before (D3D11, the VM's baseline or the desktop) and after (Vulkan), and the CPU BC7/BC6H cost for #81.

5. Smaller decisions (user, 2026-11-23: every recommendation)

  1. DirectXTex on Windows: both build systems compile the downloaded DirectXTex sources (without its D3D11 GPU files) into ContentTools; vcpkg goes.
  2. Vulkan headers on Windows: ContentTools needs the Vulkan SDK, like the engine's Vulkan backend; without it the build fails with a message.
  3. No usable GPU: the IBL exports set ImportError and log why; the rest of texture import still works.

6. Progress (2026-11-23)

Steps 1-4 are done on Linux, step 5 in the VM except the comparison with the baseline DLL, step 6 measured on Linux.

What was built

  • Dependencies: DirectXTex may2026 and DirectX-Headers v1.721.2 (CMake, .tar.gz), and on Windows DirectXTex's .zip (ThirdParty.targets: MSBuild unzips but can't untar, so its hash differs). stb_image is pinned to commit 6b5799b and used on Linux only (Windows keeps WIC). DirectXTex's CPU and WIC sources are compiled into ContentTools at -O2//O2 in every configuration. vcpkg is gone (vcpkg.json, vcpkg-configuration.json). The SAL stub has the macros DirectXTex needs.

  • Textures: TextureImporter.cpp has no D3D11, DXGI or WRL. Its structs moved to TextureImporter.h. Block compression runs in strips of 64 rows on jobs::parallel_for (the output is bit-identical to one Compress() call, which a test checks). ImageLoading.* loads BMP/GIF/JPEG/PNG with stb_image and decides sRGB like WIC does (PNG sRGB chunk or gAMA 45455, JPEG EXIF ColorSpace 1). Import() initializes COM on its thread once (WIC needs it; never undone, because DirectXTex keeps its WIC factory). Editor memory goes through editor_realloc() (CoTaskMemRealloc/realloc).

  • IBL: VulkanCompute.* is a headless Vulkan 1.3 compute device of ContentTools' own:

    • its own function table (vulkan-1.dll / libvulkan.so.1 opened at run time);
    • push descriptors and push constants;
    • one memory allocation per resource, with readback from host-cached memory;
    • validation in Debug.

    The device ranks discrete > integrated > virtual > CPU (lavapipe), is made on first use, and is destroyed by ShutDownContentTools() or a lost device. EnvMapProcessing.hlsl uses Vulkan bindings and push constants. It is compiled into shaders/ContentTools.shaderpack (manifest ContentTools.shaders, SPIR-V only):

    • by ShaderCompilerTool in Debug/Release (both build systems);
    • by EngineDLL's InitializeEngine() in the editor configurations, which don't build the tool. A failure there is a warning: only environment map import needs the pack.

    CompileShaders.cmd and the D3D11 code are gone. Without a device or the pack, the IBL exports return ImportError and log why. The CPU equirectangular path (nearest texel) remains as the fallback for the cube map only.

  • Library: CMake's object library ContentToolsObjects goes into the static ContentTools (UnitTests, GraphicsTests) and the shared x64/<Config>/libContentTools.so. Hidden visibility and --exclude-libs,ALL leave exactly the 8 EDITOR_INTERFACE functions exported. On Windows, ContentTools.vcxproj links PrimalEngine.lib, imports Vulkan.props + Modules.props, and fails without the Vulkan SDK. UnitTests' and GraphicsTests' ContentTools sources are conditional on PrimalVulkan, so the CI build without Vulkan still builds.

Tests

  • UnitTests "texture import" (12 cases) + "primitive meshes": sRGB detection, PNG (8- and 16-bit), JPEG, BMP, TGA, HDR, DDS, a missing file, mip chains, strips = one go (BC1/BC3/BC7), the output format by content (BC5 normals, sRGB BC formats), Decompress, cube maps from six images, the CPU cube map's faces, every primitive. Test images: UnitTests/Data/Textures/ (make_files.py).
  • GraphicsTests "image based lighting" (6 cases): the GPU cube map against the CPU one (per face and channel), an 8-bit input, prefiltering a constant environment (diffuse and specular stay constant, specular mip 0 = the source), refused inputs, the BRDF LUT against a C++ copy of IntegrateBRDF(), the device made again after shutdown; validation clean in Debug.
  • Checked on Linux: clang and GCC, clang-tidy, ci.sh checks, ASan/UBSan, TSan (UnitTests 256, GraphicsTests 28). UBSan is off for third-party code that breaks it on purpose or by upstream bug: DirectXTex (unaligned pixel access in TGA; its BC7 encoder casts negative floats to uint8_t) and MikkTSpace's shift check (a 32-bit rotate by 32).
  • Checked in the VM: all four configurations with 0 warnings, UnitTests Debug + Release (also the 13 texture cases on WIC), GraphicsTests on D3D12 and Vulkan (28 each; IBL on lavapipe, also in Release), the editor tests in both editor configurations (42, ContentToolsTests.cs through ContentTools.dll), and the editor's start, which compiled x64\DebugEditor\shaders\ContentTools.shaderpack.

Measured (Release, Linux, RX 6800 XT / 24 threads; benchmark: test cases)

Step Time
2048x1024 environment map to a 1024 cube map, GPU (upload, dispatch, readback) 42.6 ms (463 ms with uncached readback)
the same on the CPU (nearest texel, jobs) 58 ms
diffuse prefilter (256 cube map → 32, 1024 samples) 62 ms
specular prefilter (256 cube map, 5 mips, 1024 samples) 7.9 ms
BRDF LUT 256x256, 1024 samples 0.9 ms
2048x2048 BC1, 24 threads 60 ms
2048x2048 BC7, 24 threads (DirectXTex default) 86 s
2048x2048 BC6H, 24 threads 17 s
2048x512 BC7, one thread: default / TEX_COMPRESS_BC7_QUICK (mode 6 only) 215.5 s / 7.9 s

The CPU BC7 and BC6H encoders are the cost of dropping the D3D11 GPU compressor: an import of a 2048x2048 BC7 texture with mips took about two minutes. Decided (user, 2026-11-23): the importer uses TEX_COMPRESS_BC7_QUICK, 27x faster at a lower quality (2048x2048 BC7 on 24 threads: 3.2 s), and #81 (an encoder of our own or a library) comes right after #13. BC6H has no quick mode (17 s).

Open

  • Comparing outputs with C:\winvm\baseline\'s SDK-era ContentTools.dll: needs a texture version of DumpImport.exe.
  • The desktop: plan doc A58 (the editor imports a texture, a cube map and an HDR environment map; the D3D11-era timing for comparison).
  • The Linux editor's library name ("ContentTools.dll" in C#) belongs to #13.

Pages 19

View all

Clone this repository