-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTextureSmithExample.cs
More file actions
305 lines (266 loc) · 10.5 KB
/
Copy pathTextureSmithExample.cs
File metadata and controls
305 lines (266 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// TextureSmith C# example
// Requires: texture_smith_core.dll (built with `cargo build -p texture_smith_core --features library --release`)
// Target framework: .NET 6+
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
// ── Wrapper ──────────────────────────────────────────────────────────────────
public enum TsImageFormat
{
Png = 0,
Tga = 1,
Tiff = 2,
Dds = 3,
}
public enum TsDdsFormat
{
Bc1Unorm = 0,
Bc1UnormSrgb = 1,
Bc3Unorm = 2,
Bc3UnormSrgb = 3,
Bc4Unorm = 4,
Bc5Unorm = 5,
Bc7Unorm = 6,
Bc7UnormSrgb = 7,
Rgba8Unorm = 8,
Rgba8UnormSrgb = 9,
Bgra8Unorm = 10,
Bgra8UnormSrgb = 11,
R8Unorm = 12,
R8G8Unorm = 13,
Rgba16Float = 14,
R32Float = 15,
}
/// <summary>
/// One output image from a shader pass. Pixel data is RGBA8, row-major.
/// </summary>
public sealed class TsImage
{
public uint Width { get; }
public uint Height { get; }
public byte[] Pixels { get; } // RGBA8, Width * Height * 4 bytes
internal TsImage(uint w, uint h, byte[] pixels)
{
Width = w;
Height = h;
Pixels = pixels;
}
}
/// <summary>
/// Wraps the native TsResult handle. Dispose when done.
/// </summary>
public sealed class TsResult : IDisposable
{
private IntPtr _handle;
internal TsResult(IntPtr handle) => _handle = handle;
public uint Count => Native.ts_result_count(_handle);
/// <summary>Copies output <paramref name="index"/> pixel data into a managed TsImage.</summary>
public TsImage GetImage(uint index)
{
uint w = Native.ts_result_width(_handle, index);
uint h = Native.ts_result_height(_handle, index);
nuint len = Native.ts_result_len(_handle, index);
IntPtr srcPtr = Native.ts_result_data(_handle, index);
byte[] pixels = new byte[(int)len];
Marshal.Copy(srcPtr, pixels, 0, pixels.Length);
return new TsImage(w, h, pixels);
}
/// <summary>
/// Save all outputs to <paramref name="outputDir"/>.
/// Throws on failure.
/// </summary>
public void Save(
string outputDir,
TsImageFormat format = TsImageFormat.Png,
TsDdsFormat ddsFormat = TsDdsFormat.Rgba8Unorm)
{
IntPtr err = Native.ts_result_save(_handle, outputDir, (uint)format, (uint)ddsFormat);
if (err != IntPtr.Zero)
{
string msg = Marshal.PtrToStringAnsi(err) ?? "unknown error";
Native.ts_free_string(err);
throw new InvalidOperationException($"ts_result_save failed: {msg}");
}
}
/// <summary>
/// Save a single output at <paramref name="index"/> to <paramref name="outputDir"/>.
/// Throws on failure.
/// </summary>
public void Save(
uint index,
string outputDir,
TsImageFormat format = TsImageFormat.Png,
TsDdsFormat ddsFormat = TsDdsFormat.Rgba8Unorm)
{
IntPtr err = Native.ts_result_save_one(_handle, index, outputDir, (uint)format, (uint)ddsFormat);
if (err != IntPtr.Zero)
{
string msg = Marshal.PtrToStringAnsi(err) ?? "unknown error";
Native.ts_free_string(err);
throw new InvalidOperationException($"ts_result_save_one failed: {msg}");
}
}
public void Dispose()
{
if (_handle != IntPtr.Zero)
{
Native.ts_result_free(_handle);
_handle = IntPtr.Zero;
}
}
}
/// <summary>
/// Main entry point into the Texture Smith core library.
/// </summary>
public static class TextureSmith
{
/// <summary>
/// Process a set of images through the named shader.
/// </summary>
/// <param name="shadersDir">
/// Path to the directory that contains shader subdirectories
/// (each with a shader.wgsl and config.toml).
/// </param>
/// <param name="shaderName">
/// The <c>[shader].name</c> value from the target shader's config.toml.
/// </param>
/// <param name="inputs">
/// List of (raw file bytes, extension) pairs — e.g. (File.ReadAllBytes("a.png"), "png").
/// The extension tells the library how to decode the bytes.
/// </param>
/// <param name="parameters">Optional shader parameter overrides.</param>
/// <returns>A <see cref="TsResult"/> that must be disposed when done.</returns>
public static TsResult Process(
string shadersDir,
string shaderName,
IReadOnlyList<(byte[] Data, string Ext)> inputs,
IReadOnlyDictionary<string, float>? parameters = null)
{
int imageCount = inputs.Count;
// Pin each input byte array and collect the pointers.
var handles = new GCHandle[imageCount];
var dataPtrs = new IntPtr[imageCount];
var dataLens = new nuint[imageCount];
IntPtr[] extPtrs = new IntPtr[imageCount];
try
{
for (int i = 0; i < imageCount; i++)
{
handles[i] = GCHandle.Alloc(inputs[i].Data, GCHandleType.Pinned);
dataPtrs[i] = handles[i].AddrOfPinnedObject();
dataLens[i] = (nuint)inputs[i].Data.Length;
extPtrs[i] = Marshal.StringToHGlobalAnsi(inputs[i].Ext);
}
// Marshal parameter names and values.
parameters ??= new Dictionary<string, float>();
int paramCount = parameters.Count;
IntPtr[] paramNames = new IntPtr[paramCount];
float[] paramValues = new float[paramCount];
int pi = 0;
foreach (var (name, value) in parameters)
{
paramNames[pi] = Marshal.StringToHGlobalAnsi(name);
paramValues[pi] = value;
pi++;
}
try
{
IntPtr result = Native.ts_process(
shadersDir,
shaderName,
dataPtrs,
dataLens,
extPtrs,
(uint)imageCount,
paramNames,
paramValues,
(uint)paramCount);
if (result == IntPtr.Zero)
{
IntPtr errPtr = Native.ts_last_error();
string err = Marshal.PtrToStringAnsi(errPtr) ?? "unknown error";
throw new InvalidOperationException($"ts_process failed: {err}");
}
return new TsResult(result);
}
finally
{
foreach (IntPtr p in paramNames)
if (p != IntPtr.Zero) Marshal.FreeHGlobal(p);
}
}
finally
{
for (int i = 0; i < imageCount; i++)
{
if (handles[i].IsAllocated) handles[i].Free();
if (extPtrs[i] != IntPtr.Zero) Marshal.FreeHGlobal(extPtrs[i]);
}
}
}
}
// ── Native bindings (internal) ───────────────────────────────────────────────
internal static class Native
{
const string Lib = "texture_smith_core";
[DllImport(Lib, CharSet = CharSet.Ansi)]
public static extern IntPtr ts_process(
string shadersDir,
string shaderName,
IntPtr[] inputData,
nuint[] inputLens,
IntPtr[] extensions,
uint imageCount,
IntPtr[] paramNames,
float[] paramValues,
uint paramCount);
[DllImport(Lib)] public static extern uint ts_result_count(IntPtr result);
[DllImport(Lib)] public static extern IntPtr ts_result_data(IntPtr result, uint index);
[DllImport(Lib)] public static extern nuint ts_result_len(IntPtr result, uint index);
[DllImport(Lib)] public static extern uint ts_result_width(IntPtr result, uint index);
[DllImport(Lib)] public static extern uint ts_result_height(IntPtr result, uint index);
[DllImport(Lib, CharSet = CharSet.Ansi)]
public static extern IntPtr ts_result_save(IntPtr result, string outputDir, uint format, uint ddsFormat);
[DllImport(Lib, CharSet = CharSet.Ansi)]
public static extern IntPtr ts_result_save_one(IntPtr result, uint index, string outputDir, uint format, uint ddsFormat);
[DllImport(Lib)] public static extern void ts_result_free(IntPtr result);
[DllImport(Lib)] public static extern void ts_free_string(IntPtr ptr);
[DllImport(Lib)] public static extern IntPtr ts_last_error();
}
// ── Example usage ────────────────────────────────────────────────────────────
class Program
{
static void Main(string[] args)
{
// Paths — adjust these for your setup.
string shadersDir = @"C:\path\to\shaders"; // folder with shader subdirs
string outputDir = @"C:\path\to\output";
string shaderName = "My Shader"; // must match [shader].name in config.toml
// --- Example 1: process and save to disk ---
Console.WriteLine("Processing...");
var inputs = new List<(byte[], string)>
{
(File.ReadAllBytes(@"C:\path\to\diffuse.png"), "png"),
(File.ReadAllBytes(@"C:\path\to\normal.dds"), "dds"),
};
var parameters = new Dictionary<string, float>
{
["roughness"] = 0.8f,
["metallic"] = 0.0f,
};
using TsResult result = TextureSmith.Process(shadersDir, shaderName, inputs, parameters);
Console.WriteLine($"Got {result.Count} output(s).");
// Save all outputs as PNG files in outputDir.
result.Save(outputDir, TsImageFormat.Png);
Console.WriteLine($"Saved to {outputDir}");
// --- Example 2: grab raw RGBA8 pixel data instead ---
for (uint i = 0; i < result.Count; i++)
{
TsImage img = result.GetImage(i);
Console.WriteLine($"Output {i}: {img.Width}x{img.Height}, {img.Pixels.Length} bytes RGBA8");
// img.Pixels is a plain byte[] you can hand to any image library,
// e.g. ImageSharp, SkiaSharp, System.Drawing, etc.
}
}
}