HelixToolkit.Nex.Repository 1.2.0

dotnet add package HelixToolkit.Nex.Repository --version 1.2.0
                    
NuGet\Install-Package HelixToolkit.Nex.Repository -Version 1.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HelixToolkit.Nex.Repository" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HelixToolkit.Nex.Repository" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="HelixToolkit.Nex.Repository" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add HelixToolkit.Nex.Repository --version 1.2.0
                    
#r "nuget: HelixToolkit.Nex.Repository, 1.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package HelixToolkit.Nex.Repository@1.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=HelixToolkit.Nex.Repository&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=HelixToolkit.Nex.Repository&version=1.2.0
                    
Install as a Cake Tool
# HelixToolkit.Nex.Repository

The `HelixToolkit.Nex.Repository` package provides a robust, thread-safe caching mechanism for various GPU resources such as textures, shaders, and samplers. It is designed to optimize resource management in the HelixToolkit.Nex engine by implementing an LRU (Least Recently Used) eviction policy, ensuring efficient memory usage and performance.

## Overview

The repository package is a critical component of the HelixToolkit.Nex engine, responsible for managing the lifecycle of GPU resources. It provides:
- Automatic deduplication of resources to prevent redundant GPU resource creation.
- Thread-safe access to cached resources, ensuring consistency and performance in multi-threaded environments.
- LRU eviction policy to manage memory usage effectively.
- Optional time-based expiration for cached entries.
- Comprehensive cache statistics for monitoring and debugging.

## Key Types

| Type | Description |
|------|-------------|
| `IRepository<TKey, TEntry, TResource>` | Interface for a generic repository that caches resources with LRU eviction. |
| `ISamplerRepository` | Interface for caching sampler state resources. |
| `IShaderRepository` | Interface for caching compiled SPIR-V shader modules. |
| `ITextureRepository` | Interface for caching GPU texture resources. |
| `Repository<TKey, TEntry, TResource>` | Abstract class implementing the generic repository pattern. |
| `SamplerRepository` | Concrete implementation for caching sampler resources. |
| `ShaderRepository` | Concrete implementation for caching shader modules. |
| `TextureRepository` | Concrete implementation for caching texture resources. |
| `SamplerRef` | Wrapper for a live sampler resource with disposal notification. |
| `TextureRef` | Wrapper for a live texture resource with disposal notification. |
| `CacheEntry<TResource>` | Represents a cache entry for a resource. |
| `RepositoryStatistics` | Provides statistics about the repository cache. |

## Usage Examples

### Caching a Texture from a Stream

```csharp
using HelixToolkit.Nex.Repository;
using System.IO;

var context = /* Obtain IContext instance */;
var textureRepo = new TextureRepository(context);

using var stream = File.OpenRead("path/to/texture.png");
var textureRef = textureRepo.GetOrCreateFromStream("uniqueTextureName", stream, generateMipmaps: true);

Caching a Shader Module from GLSL Source

using HelixToolkit.Nex.Repository;

var context = /* Obtain IContext instance */;
var shaderRepo = new ShaderRepository(context);

string glslSource = "/* GLSL shader code */";
var shaderModule = shaderRepo.GetOrCreateFromGlsl(ShaderStage.Vertex, glslSource);

Caching a Sampler Resource

using HelixToolkit.Nex.Repository;

var context = /* Obtain IContext instance */;
var samplerRepo = new SamplerRepository(context);

var samplerDesc = new SamplerStateDesc
{
    MinFilter = Filter.Linear,
    MagFilter = Filter.Linear,
    WrapU = WrapMode.Repeat,
    WrapV = WrapMode.Repeat
};

var samplerRef = samplerRepo.GetOrCreate("uniqueSamplerKey", samplerDesc);

Asynchronously Caching a Texture from a File

using HelixToolkit.Nex.Repository;
using System.Threading.Tasks;

var context = /* Obtain IContext instance */;
var textureRepo = new TextureRepository(context);

var textureRef = await textureRepo.GetOrCreateFromFileAsync("path/to/texture.png", generateMipmaps: true);

Architecture Notes

  • Design Patterns: The repository pattern is used extensively to manage resource caching. Each repository type (e.g., SamplerRepository, ShaderRepository, TextureRepository) implements a specific interface, providing a consistent API for resource management.
  • Dependencies: The repositories depend on the HelixToolkit.Nex.Graphics package for context and resource creation. They also utilize Microsoft.Extensions.Logging for logging purposes.
  • LRU Eviction: The repositories implement an LRU eviction policy to ensure that the most recently used resources are retained while older, less frequently accessed resources are evicted when the cache reaches its capacity.
  • Thread Safety: All public methods are thread-safe, allowing concurrent access and modification of the cache without data races or inconsistencies.
  • Resource Wrappers: SamplerRef and TextureRef provide a safe way to handle GPU resources, ensuring that resources are properly disposed and notifying consumers when resources are no longer valid.

Configuration

The project now supports additional build configurations for Linux:

  • LinuxDebug
  • LinuxRelease

These configurations allow for building and testing the repository package on Linux platforms, expanding the versatility and deployment options for the HelixToolkit.Nex engine.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on HelixToolkit.Nex.Repository:

Package Downloads
HelixToolkit.Nex.Material

Package Description

HelixToolkit.Nex.Rendering

Package Description

HelixToolkit.Nex.Engine

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0 254 7/17/2026