The asset system is designed to:
- make ownership explicit
- separate CPU and GPU lifetime
- work identically in runtime and editor
- avoid hidden memory management
1. What Is an Asset?
In GFX-Next, an asset is any reusable resource that may:
- be shared across multiple objects
- live longer than a single scene
- own GPU and/or CPU memory
Examples:
- Mesh
- StaticMeshModel
- SkinnedMeshModel
- Material
- Shader
- Texture
- ComputeShader
Assets are not scene-owned.
They are managed globally by the AssetManager.
2. The AssetManager
The AssetManager is responsible for:
- loading assets from disk
- storing assets by type and identifier
- initializing GPU resources
- freeing CPU-side data
- disposing GPU resources
AssetManager assets = new AssetManager();
Assets can be:
- loaded from disk
- created procedurally
- reused and shared freely
3. Loading Assets
Assets are usually loaded during LoadContent.
_lionModel = assets.Load<StaticMeshModel>("Ressources/Lion/Lion.gltf");
Key properties:
- assets are cached automatically
- loading the same asset twice returns the same instance
- the asset manager owns the instance
This makes asset usage cheap and predictable.
4. Procedural Assets
Not all assets come from disk.
Procedural assets are created in code and registered manually.
_cubeMesh = assets.Add<Mesh>("CubeMesh", Cube.GetMesh(defaultMaterial));
This is commonly used for:
- primitives
- debug geometry
- editor-only helpers
- generated meshes
5. Asset Initialization (GPU Upload)
Assets that interact with the GPU implement IGraphicsResource.
assets.InitializeAssets(RenderDevice);
This step:
- uploads buffers
- creates textures
- compiles shaders
- prepares render data
Important
Assets exist before GPU initialization.
This allows:
- editor previews
- asset inspection
- serialization without a graphics context
6. CPU vs GPU Lifetime
GFX-Next explicitly separates:
- CPU-side data
- GPU-side data
After GPU upload, CPU data may no longer be needed.
assets.FreeCPUResources();
Examples of CPU data:
- vertex arrays
- index buffers
- animation data
- imported mesh data
Why this matters
- reduces memory usage
- improves cache behavior
- makes lifetime decisions explicit
The engine never frees CPU memory automatically.
This is a conscious design decision.
7. User-Controlled Resource Policy
Whether CPU memory is freed is entirely up to the user. The Game class contains the following property:
public bool FreeCPUResources { get; set; } = true;
This allows different workflows:
There is no hidden behavior.
8. Selective CPU Memory Freeing
The asset manager supports type-based CPU cleanup.
assets.FreeCPUResources<StaticMeshModel>();
assets.FreeCPUResources<SkinnedMeshModel>();
This allows:
- freeing heavy imported data
- keeping procedural meshes
- editor-safe workflows
This can only be done if you disable the FreeCPUResources property and call them within the Initialize() function or later.
9. Asset Lifetime vs Scene Lifetime
Assets:
- live independently of scenes
- can be reused across scenes
- are not destroyed when a scene is destroyed
Scenes only reference assets; they never own them.
This makes scene switching cheap and safe.
10. Asset Disposal (GPU Cleanup)
When the application shuts down:
assets.DisposeAssets(RenderDevice);
assets.ClearAssets();
This:
- releases GPU buffers
- deletes textures
- destroys shaders
- clears asset references
No implicit disposal occurs during runtime.
Summary
The asset system in GFX-Next is built around:
- explicit ownership
- manual lifetime control
- clear CPU/GPU separation
- editor and runtime parity
- zero hidden memory management
This design favors control and predictability over convenience.