/
TextureUtils.cs
377 lines (327 loc) · 14.5 KB
/
TextureUtils.cs
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using SlimDX.Direct3D9;
namespace VVVV.Utils.SlimDX
{
/// <summary>
/// Method delegate for pixelshader like texture fill functions.
/// </summary>
public unsafe delegate void TextureFillFunction(uint[] oldData, uint* data, int row, int col, int width, int height);
/// <summary>
/// Method delegate for pixelshader like texture fill functions carrying user defined metadata in its arguments.
/// </summary>
public unsafe delegate void TextureFillFunction<TMetadata>(uint[] oldData, uint* data, int row, int col, int width, int height, TMetadata metadata);
/// <summary>
/// Method delegate for pixelshader like in place texture fill functions.
/// </summary>
public unsafe delegate void TextureFillFunctionInPlace(uint* data, int row, int col, int width, int height);
/// <summary>
/// Method delegate for pixelshader like in place texture fill functions carrying user defined metadata in its arguments.
/// </summary>
public unsafe delegate void TextureFillFunctionInPlace<TMetadata>(uint* data, int row, int col, int width, int height, TMetadata metadata);
/// <summary>
/// Provides some utils to work with DX textures
/// </summary>
public static class TextureUtils
{
/// <summary>
/// Create a <see cref="Texture">texture</see> of <paramref name="width" />
/// and <paramref name="height" /> on <paramref name="device" />.
/// </summary>
/// <param name="device">The device to create the texture on.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <returns>The newly created <see cref="Texture">texture</see>.</returns>
public static Texture CreateTexture(Device device, int width, int height)
{
var pool = Pool.Managed;
var usage = Usage.None;
if (device is DeviceEx)
{
pool = Pool.Default;
usage = Usage.Dynamic;
}
return new Texture(device, width, height, 1, usage, Format.A8R8G8B8, pool);
}
/// <summary>
/// Create a <see cref="Texture">texture</see> without an alpha channel of <paramref name="width" />
/// and <paramref name="height" /> on <paramref name="device" />.
/// </summary>
/// <param name="device">The device to create the texture on.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <returns>The newly created <see cref="Texture">texture</see>.</returns>
public static Texture CreateTextureNoAlpha(Device device, int width, int height)
{
var pool = Pool.Managed;
var usage = Usage.None;
if (device is DeviceEx)
{
pool = Pool.Default;
usage = Usage.Dynamic;
}
return new Texture(device, width, height, 1, usage, Format.X8R8G8B8, pool);
}
/// <summary>
/// Create a <see cref="Texture">texture</see> of <paramref name="width" />
/// and <paramref name="height" /> on <paramref name="device" />
/// and fill it with <paramref name="argbColor" />.
/// </summary>
/// <param name="device">The device to create the texture on.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="argbColor">The color to fill the texture with.</param>
/// <returns>The newly created <see cref="Texture">texture</see>.</returns>
public static Texture CreateColoredTexture(Device device, int width, int height, uint argbColor)
{
var pool = Pool.Managed;
var usage = Usage.None;
if (device is DeviceEx)
{
pool = Pool.Default;
usage = Usage.Dynamic;
}
var t = new Texture(device, width, height, 1, usage, Format.A8R8G8B8, pool);
var rect = t.LockRectangle(0, LockFlags.None).Data;
for (int i=0; i<(width*height); i++)
{
rect.Write(argbColor);
}
t.UnlockRectangle(0);
return t;
}
/// <summary>
/// Copies all pixels of the bitmap into the texture.
/// no checks are done, make sure the pixel count of the bitmap and texture matches
/// </summary>
/// <param name="bm"></param>
/// <param name="texture"></param>
public static void CopyBitmapToTexture(Bitmap bm, Texture texture)
{
var data = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadOnly, bm.PixelFormat);
var rect = texture.LockRectangle(0, LockFlags.None);
if(rect.Pitch == data.Stride)
{
Memory.Copy(rect.Data.DataPointer, data.Scan0, (uint)(data.Stride * data.Height));
}
else
{
//copy full lines
for (int i = 0; i < data.Height; i++)
{
Memory.Copy(rect.Data.DataPointer.Move(rect.Pitch * i), data.Scan0.Move(data.Stride * i), (uint)data.Stride);
}
}
texture.UnlockRectangle(0);
bm.UnlockBits(data);
}
/// <summary>
/// Retrievies the value at position <paramref name="row" />, <paramref name="col" />
/// from a data buffer of width <paramref name="width" />.
/// </summary>
/// <param name="data">The pointer to the data buffer to retrieve the value from.</param>
/// <param name="row">The row.</param>
/// <param name="col">The column.</param>
/// <param name="width">The width of the data buffer.</param>
/// <returns>The value at position row, col.</returns>
public unsafe static uint GetPtrVal2D(uint* data, int row, int col, int width)
{
return data[row * width + col];
}
/// <summary>
/// Sets the value at position <paramref name="row" />, <paramref name="col" />
/// in a data buffer of width <paramref name="width" />.
/// </summary>
/// <param name="data">The pointer to the data buffer.</param>
/// <param name="value">The value to set at position row, col.</param>
/// <param name="row">The row.</param>
/// <param name="col">The column.</param>
/// <param name="width">The width of the data buffer.</param>
public unsafe static void SetPtrVal2D(uint* data, uint value, int row, int col, int width)
{
data[row * width + col] = value;
}
/// <summary>
/// Retrievies the value at position <paramref name="row" />, <paramref name="col" />
/// from a data array of width <paramref name="width" />.
/// </summary>
/// <param name="data">The data array to retrieve the value from.</param>
/// <param name="row">The row.</param>
/// <param name="col">The column.</param>
/// <param name="width">The width of the data array.</param>
/// <returns>The value at position row, col.</returns>
public unsafe static uint GetArrayVal2D(this uint[] data, int row, int col, int width)
{
return data[row * width + col];
}
/// <summary>
/// Sets the value at position <paramref name="row" />, <paramref name="col" /> in a data array of
/// width <paramref name="width" />.
/// </summary>
/// <param name="data">The data array.</param>
/// <param name="value">The value to set at position row, col.</param>
/// <param name="row">The row.</param>
/// <param name="col">The column.</param>
/// <param name="width">The width of the data array.</param>
public unsafe static void SetArrayVal2D(this uint[] data, uint value, int row, int col, int width)
{
data[row * width + col] = value;
}
/// <summary>
/// Copy texture pixels to an array.
/// </summary>
/// <param name="src">Pointer to the texture.</param>
/// <param name="dest">Array to fill with the pixel data</param>
/// <param name="size">The size of the resulting array.</param>
public static void Copy32BitTexToArray(IntPtr src, uint[] dest, int size)
{
Marshal.Copy(src, (int[])((object)dest), 0, size);
}
/// <summary>
/// Fill a 32 bit texture with values retrieved from the function fillFunc.
/// </summary>
/// <param name="tex">The texture to fill.</param>
/// <param name="oldData">Array to fill with the old data.</param>
/// <param name="metadata">User defined metadata to hand over to the fillFunc.</param>
/// <param name="fillFunc">The function used to fill the texture.</param>
public unsafe static void Fill32BitTex<TMetadata>(Texture tex, uint[] oldData, TMetadata metadata, TextureFillFunction<TMetadata> fillFunc)
{
//lock the texture pixel data
var rect = tex.LockRectangle(0, LockFlags.None);
//calculate sizes
var byteLenght = (int)rect.Data.Length;
var pixelCount = byteLenght/4;
var width = rect.Pitch/4;
var height = byteLenght/rect.Pitch;
//get the pointer to the data
var data = (uint*)rect.Data.DataPointer.ToPointer();
//copy data to array, that we can replace the data
Copy32BitTexToArray(rect.Data.DataPointer, oldData, pixelCount);
//call the given function for each pixel
for(int i=0; i<height; i++)
for(int j=0; j<width; j++)
fillFunc(oldData, data, i, j, width, height, metadata);
//unlock texture
tex.UnlockRectangle(0);
}
/// <summary>
/// Fill a 32 bit texture with values retrieved from the function fillFunc.
/// </summary>
/// <param name="tex">The texture to fill.</param>
/// <param name="oldData">Array to fill with the old data</param>
/// <param name="fillFunc">The function used to fill the texture.</param>
public unsafe static void Fill32BitTex(Texture tex, uint[] oldData, TextureFillFunction fillFunc)
{
Fill32BitTex(tex, oldData, 0, (od, data, row, col, width, height, metadata) => fillFunc(od, data, row, col, width, height));
}
/// <summary>
/// Fill a 32 bit texture in parallel with values retrieved from the function fillFunc.
/// </summary>
/// <param name="tex">The texture to fill.</param>
/// <param name="oldData">Array to fill with the old data</param>
/// <param name="metadata">User defined metadata to hand over to the fillFunc.</param>
/// <param name="fillFunc">The function used to fill the texture.</param>
public unsafe static void Fill32BitTexParallel<TMetadata>(Texture tex, uint[] oldData, TMetadata metadata, TextureFillFunction<TMetadata> fillFunc)
{
//lock the texture pixel data
var rect = tex.LockRectangle(0, LockFlags.None);
//calculate sizes
var byteLenght = (int)rect.Data.Length;
var pixelCount = byteLenght/4;
var width = rect.Pitch/4;
var height = byteLenght/rect.Pitch;
//get the pointer to the data
var data = (uint*)rect.Data.DataPointer.ToPointer();
//copy data to array, that we can replace the data
Copy32BitTexToArray(rect.Data.DataPointer, oldData, pixelCount);
//call the given function for each pixel
Parallel.For(0, height, i =>
{
for(int j=0; j<width; j++)
fillFunc(oldData, data, i, j, width, height, metadata);
});
//unlock texture
tex.UnlockRectangle(0);
}
/// <summary>
/// Fill a 32 bit texture in parallel with values retrieved from the function fillFunc.
/// </summary>
/// <param name="tex">The texture to fill.</param>
/// <param name="oldData">Array to fill with the old data</param>
/// <param name="fillFunc">The function used to fill the texture.</param>
public unsafe static void Fill32BitTexParallel(Texture tex, uint[] oldData, TextureFillFunction fillFunc)
{
Fill32BitTexParallel(tex, oldData, 0, (od, data, row, col, width, height, metadata) => fillFunc(od, data, row, col, width, height));
}
/// <summary>
/// Fill a 32 bit texture in place with values retrieved from the function fillFunc.
/// </summary>
/// <param name="tex">The texture to fill.</param>
/// <param name="metadata">User defined metadata to hand over to the fillFunc.</param>
/// <param name="fillFunc">The function used to fill the texture.</param>
public unsafe static void Fill32BitTexInPlace<TMetadata>(Texture tex, TMetadata metadata, TextureFillFunctionInPlace<TMetadata> fillFunc)
{
//lock the texture pixel data
var rect = tex.LockRectangle(0, LockFlags.None);
//calculate sizes
var byteLenght = (int)rect.Data.Length;
var width = rect.Pitch/4;
var height = byteLenght/rect.Pitch;
//get the pointer to the data
var data = (uint*)rect.Data.DataPointer.ToPointer();
//call the given function for each pixel
for(int i=0; i<height; i++)
for(int j=0; j<width; j++)
fillFunc(data, i, j, width, height, metadata);
//unlock texture
tex.UnlockRectangle(0);
}
/// <summary>
/// Fill a 32 bit texture in place with values retrieved from the function fillFunc.
/// </summary>
/// <param name="tex">The texture to fill.</param>
/// <param name="fillFunc">The function used to fill the texture.</param>
public unsafe static void Fill32BitTexInPlace(Texture tex, TextureFillFunctionInPlace fillFunc)
{
Fill32BitTexInPlace(tex, 0, (data, row, col, width, height, metadata) => fillFunc(data, row, col, width, height));
}
/// <summary>
/// Fill a 32 bit texture parallel in place with values retrieved from the function fillFunc.
/// </summary>
/// <param name="tex">The texture to fill.</param>
/// <param name="metadata">User defined metadata to hand over to the fillFunc.</param>
/// <param name="fillFunc">The function used to fill the texture.</param>
public unsafe static void Fill32BitTexInPlaceParallel<TMetadata>(Texture tex, TMetadata metadata, TextureFillFunctionInPlace<TMetadata> fillFunc)
{
//lock the texture pixel data
var rect = tex.LockRectangle(0, LockFlags.None);
//calculate sizes
var byteLenght = (int)rect.Data.Length;
var width = rect.Pitch/4;
var height = byteLenght/rect.Pitch;
//get the pointer to the data
var data = (uint*)rect.Data.DataPointer.ToPointer();
//call the given function for each pixel
Parallel.For(0, height, i =>
{
for(int j=0; j<width; j++)
fillFunc(data, i, j, width, height, metadata);
});
//unlock texture
tex.UnlockRectangle(0);
}
/// <summary>
/// Fill a 32 bit texture parallel in place with values retrieved from the function fillFunc.
/// </summary>
/// <param name="tex">The texture to fill.</param>
/// <param name="fillFunc">The function used to fill the texture.</param>
public unsafe static void Fill32BitTexInPlaceParallel(Texture tex, TextureFillFunctionInPlace fillFunc)
{
Fill32BitTexInPlaceParallel(tex, 0, (data, row, col, width, height, metadata) => fillFunc(data, row, col, width, height));
}
}
}