brutal/samples/Hello/HelloGli.cs
Rian Drake 7a3b72d843
samples: add Hello/Demo samples for most BRUTAL packages (#19)
Add samples/, a collection of single-file .NET 10 apps run with
`dotnet run <file>.cs`. Hello/ has one minimal sample per package
that has a public API, many of them headless; Demo/ has fuller
programs that combine packages into a more interesting application.

samples/README.md indexes every sample and covers the layout, error
handling and per-OS requirements.
2026-10-27 09:10:25 +13:00

49 lines
1.4 KiB
C#

/*
* HelloGli
*
* Creates a texture with a full mip chain using GLI, saves it as a .dds and loads it back.
*/
#:property AllowUnsafeBlocks=true
#:package Ahwoo.Brutal.Gli@2026.10.1
#:package Ahwoo.Brutal.Numerics@2026.10.1
// RIAN: This package may be deprecated in the future, as the extensions it supports may be covered by better packages.
using Brutal.GliApi;
using Brutal.Numerics;
Run();
static void Run()
{
var extent = new int3(64, 64, 1);
// A 2D RGBA texture with a full mip chain (64x64 down to 1x1).
using var texture = new Gli.Texture(
Gli.Target._2D,
Gli.Format.RGBA8_UNorm_Pack8,
extent,
1,
1,
7,
Gli.Swizzles.Identity
);
unsafe
{
// Only the top mip level is filled; the smaller levels are left empty.
new Span<byte4>(texture.ImageData(0), extent.X * extent.Y).Fill(new byte4(255, 128, 0, 255));
}
var path = Path.Combine(Path.GetTempPath(), "HelloGli.dds");
texture.Save(path);
Console.WriteLine($"Wrote {path}");
using var loaded = new Gli.Texture(path);
Console.WriteLine($"Loaded {loaded.Target} {loaded.Format}, {loaded.Levels} levels, {loaded.Size} bytes");
for (var level = 0; level < loaded.Levels; level++)
Console.WriteLine(
$" Level {level}: {loaded.ImageExtent(level).X}x{loaded.ImageExtent(level).Y}, {loaded.ImageSize(level)} bytes"
);
}