Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
DxtNetWrapper.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Runtime.InteropServices;
5 using System.Security;
6 
7 namespace SiliconStudio.TextureConverter.DxtWrapper
8 {
9  #region enum
10  /// <summary>
11  /// Copy of the windows enum of DXGI_FORMAT in the file dxgiformat.h of the includes of Windows kit
12  /// </summary>
13  internal enum DXGI_FORMAT
14  {
115  DXGI_FORMAT_AYUV = 100,
116  DXGI_FORMAT_Y410 = 101,
117  DXGI_FORMAT_Y416 = 102,
118  DXGI_FORMAT_NV12 = 103,
119  DXGI_FORMAT_P010 = 104,
120  DXGI_FORMAT_P016 = 105,
122  DXGI_FORMAT_YUY2 = 107,
123  DXGI_FORMAT_Y210 = 108,
124  DXGI_FORMAT_Y216 = 109,
125  DXGI_FORMAT_NV11 = 110,
126  DXGI_FORMAT_AI44 = 111,
127  DXGI_FORMAT_IA44 = 112,
128  DXGI_FORMAT_P8 = 113,
129  DXGI_FORMAT_A8P8 = 114,
131  };
132 
133  [Flags]
134  internal enum DDS_FLAGS
135  {
136  DDS_FLAGS_NONE = 0x0,
137 
138  /// <summary>
139  /// Assume pitch is DWORD aligned instead of BYTE aligned (used by some legacy DDS files)
140  /// </summary>
142 
143  /// <summary>
144  /// Do not implicitly convert legacy formats that result in larger pixel sizes (24 bpp, 3:3:2, A8L8, A4L4, P8, A8P8)
145  /// </summary>
147 
148  /// <summary>
149  /// Do not use work-around for long-standing D3DX DDS file format issue which reversed the 10:10:10:2 color order masks
150  /// </summary>
152 
153  /// <summary>
154  /// Convert DXGI 1.1 BGR formats to DXGI_FORMAT_R8G8B8A8_UNorm to avoid use of optional WDDM 1.1 formats
155  /// </summary>
156  DDS_FLAGS_FORCE_RGB = 0x8,
157 
158  /// <summary>
159  /// Conversions avoid use of 565, 5551, and 4444 formats and instead expand to 8888 to avoid use of optional WDDM 1.2 formats
160  /// </summary>
161  DDS_FLAGS_NO_16BPP = 0x10,
162 
163  /// <summary>
164  /// Always use the 'DX10' header extension for DDS writer (i.e. don't try to write DX9 compatible DDS files)
165  /// </summary>
166  DDS_FLAGS_FORCE_DX10_EXT = 0x10000,
167  };
168 
169  [Flags]
170  internal enum TEX_COMPRESS_FLAGS
171  {
173 
174  /// <summary>
175  /// Enables dithering RGB colors for BC1-3 compression
176  /// </summary>
177  TEX_COMPRESS_RGB_DITHER = 0x10000,
178 
179  /// <summary>
180  /// Enables dithering alpha for BC1-3 compression
181  /// </summary>
182  TEX_COMPRESS_A_DITHER = 0x20000,
183 
184  /// <summary>
185  /// Enables both RGB and alpha dithering for BC1-3 compression
186  /// </summary>
187  TEX_COMPRESS_DITHER = 0x30000,
188 
189  /// <summary>
190  /// Uniform color weighting for BC1-3 compression; by default uses perceptual weighting
191  /// </summary>
192  TEX_COMPRESS_UNIFORM = 0x40000,
193 
194  /// <summary>
195  /// Compress is free to use multithreading to improve performance (by default it does not use multithreading)
196  /// </summary>
197  TEX_COMPRESS_PARALLEL = 0x10000000,
198  };
199 
200  [Flags]
201  internal enum TEX_PREMULTIPLY_ALPHA_FLAGS
202  {
204 
205  /// <summary>
206  /// Ignores sRGB colorspace conversions
207  /// </summary>
209 
210  TEX_PMALPHA_SRGB_IN = 0x1000000,
211  TEX_PMALPHA_SRGB_OUT = 0x2000000,
213  // if the input format type is IsSRGB(), then SRGB_IN is on by default
214  // if the output format type is IsSRGB(), then SRGB_OUT is on by default
215  };
216 
217  internal enum TEX_DIMENSION
218  {
222  };
223 
224  [Flags]
225  internal enum TEX_MISC_FLAG
226  {
227  TEX_MISC_TEXTURECUBE = 0x4,
228  };
229 
230  [Flags]
231  internal enum TEX_FILTER_FLAGS
232  {
233  TEX_FILTER_DEFAULT = 0,
234 
235  TEX_FILTER_WRAP_U = 0x1,
236  TEX_FILTER_WRAP_V = 0x2,
237  TEX_FILTER_WRAP_W = 0x4,
239  TEX_FILTER_MIRROR_U = 0x10,
240  TEX_FILTER_MIRROR_V = 0x20,
241  TEX_FILTER_MIRROR_W = 0x40,
243  // Wrap vs. Mirror vs. Clamp filtering options
244 
246  // Resize color and alpha channel independently
247 
248  TEX_FILTER_RGB_COPY_RED = 0x1000,
249  TEX_FILTER_RGB_COPY_GREEN = 0x2000,
250  TEX_FILTER_RGB_COPY_BLUE = 0x4000,
251  // When converting RGB to R, defaults to using grayscale. These flags indicate copying a specific channel instead
252  // When converting RGB to RG, defaults to copying RED | GREEN. These flags control which channels are selected instead.
253 
254  TEX_FILTER_DITHER = 0x10000,
255  // Use ordered 4x4 dithering for any required conversions
256  TEX_FILTER_DITHER_DIFFUSION = 0x20000,
257  // Use error-diffusion dithering for any required conversions
258 
259  TEX_FILTER_POINT = 0x100000,
260  TEX_FILTER_LINEAR = 0x200000,
261  TEX_FILTER_CUBIC = 0x300000,
262  TEX_FILTER_BOX = 0x400000,
263  TEX_FILTER_FANT = 0x400000, // Equiv to Box filtering for mipmap generation
264  TEX_FILTER_TRIANGLE = 0x500000,
265  // Filtering mode to use for any required image resizing
266 
267  TEX_FILTER_SRGB_IN = 0x1000000,
268  TEX_FILTER_SRGB_OUT = 0x2000000,
270  // sRGB <-> RGB for use in conversion operations
271  // if the input format type is IsSRGB(), then SRGB_IN is on by default
272  // if the output format type is IsSRGB(), then SRGB_OUT is on by default
273 
274  TEX_FILTER_FORCE_NON_WIC = 0x10000000,
275  // Forces use of the non-WIC path when both are an option
276 
277  TEX_FILTER_FORCE_WIC = 0x20000000,
278  // Forces use of the WIC path even when logic would have picked a non-WIC path when both are an option
279  };
280 
281  internal enum HRESULT
282  {
283  S_OK,
284  E_ABORT,
286  E_FAIL,
287  E_HANDLE,
288  E_INVALIDARG,
290  E_NOTIMPL,
292  E_POINTER,
293  E_UNEXPECTED,
294  }
295 
296  [Flags]
297  internal enum CP_FLAGS
298  {
299  /// <summary>
300  /// Normal operation
301  /// </summary>
302  CP_FLAGS_NONE = 0x0,
303 
304  /// <summary>
305  /// Assume pitch is DWORD aligned instead of BYTE aligned
306  /// </summary>
307  CP_FLAGS_LEGACY_DWORD = 0x1,
308 
309  /// <summary>
310  /// Override with a legacy 24 bits-per-pixel format size
311  /// </summary>
312  CP_FLAGS_24BPP = 0x10000,
313 
314  /// <summary>
315  /// Override with a legacy 16 bits-per-pixel format size
316  /// </summary>
317  CP_FLAGS_16BPP = 0x20000,
318 
319  /// <summary>
320  /// Override with a legacy 8 bits-per-pixel format size
321  /// </summary>
322  CP_FLAGS_8BPP = 0x40000,
323  };
324 
325  [Flags]
326  internal enum CNMAP_FLAGS
327  {
328  CNMAP_DEFAULT = 0,
329 
330  CNMAP_CHANNEL_RED = 0x1,
331  CNMAP_CHANNEL_GREEN = 0x2,
332  CNMAP_CHANNEL_BLUE = 0x3,
333  CNMAP_CHANNEL_ALPHA = 0x4,
335  // Channel selection when evaluting color value for height
336  // Luminance is a combination of red, green, and blue
337 
338  CNMAP_MIRROR_U = 0x1000,
339  CNMAP_MIRROR_V = 0x2000,
340  CNMAP_MIRROR = 0x3000,
341  // Use mirror semantics for scanline references (defaults to wrap)
342 
343  CNMAP_INVERT_SIGN = 0x4000,
344  // Inverts normal sign
345 
346  CNMAP_COMPUTE_OCCLUSION = 0x8000,
347  // Computes a crude occlusion term stored in the alpha channel
348  };
349  #endregion
350 
351 
352  /// <summary>
353  /// C# Equivalent of the DirectXTex structure Metadata
354  /// </summary>
355  [StructLayout(LayoutKind.Sequential)]
356  internal struct TexMetadata
357  {
358  public int width;
359  public int height; // Should be 1 for 1D textures
360  public int depth; // Should be 1 for 1D or 2D textures
361  public int arraySize; // For cubemap, this is a multiple of 6
362  public int mipLevels;
363  public TEX_MISC_FLAG miscFlags;
364  public int miscFlags2;
365  public DXGI_FORMAT format;
366  public TEX_DIMENSION dimension;
367 
368  public TexMetadata(int width, int height, int depth, int arraySize, int mipLevels, TEX_MISC_FLAG miscFlags, int miscFlags2, DXGI_FORMAT format, TEX_DIMENSION dimension)
369  {
370  this.width = width;
371  this.height = height;
372  this.depth = depth;
373  this.arraySize = arraySize;
374  this.mipLevels = mipLevels;
375  this.miscFlags = miscFlags;
376  this.miscFlags2 = miscFlags2;
377  this.format = format;
378  this.dimension = dimension;
379  }
380 
381  public override String ToString()
382  {
383  return "width:" + width + "\nheight:" + height + "\ndepth:" + depth + "\narraySize:" + arraySize + "\nmipLevels:" + mipLevels + "\nmiscFlags:" + miscFlags + "\nformat:" + format + "\ndimension:" + dimension;
384  }
385  }
386 
387  /// <summary>
388  /// C# Equivalent of the DirectXTex structure Image
389  /// </summary>
390  [StructLayout(LayoutKind.Sequential, Pack = 1)]
391  internal struct Image
392  {
393  public int width;
394  public int height;
395  public DXGI_FORMAT format;
396  public int rowPitch;
397  public int slicePitch;
398  public IntPtr pixels;
399 
400  public Image(int width, int height, DXGI_FORMAT format, int rowPitch, int slicePitch, IntPtr pixels)
401  {
402  this.width = width;
403  this.height = height;
404  this.format = format;
405  this.rowPitch = rowPitch;
406  this.slicePitch = slicePitch;
407  this.pixels = pixels;
408  }
409 
410  public override String ToString()
411  {
412  return "width:" + width + "\nheight:" + height + "\nformat:" + format + "\nrowPitch:" + rowPitch + "\nslicePitch:" + slicePitch + "\npixels:" + pixels;
413  }
414  }
415 
416  /// <summary>
417  /// Utility class binding DirectXTex utility methods
418  /// </summary>
419  internal class Utilities
420  {
421  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
422  private extern static void dxtComputePitch(DXGI_FORMAT fmt, int width, int height, out int rowPitch, out int slicePitch, CP_FLAGS flags);
423 
424  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
425  private extern static uint dxtLoadDDSFile(String filePath, DDS_FLAGS flags, out TexMetadata metadata, IntPtr image);
426 
427  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
428  private extern static bool dxtIsCompressed(DXGI_FORMAT fmt);
429 
430  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
431  private extern static uint dxtConvert(ref Image srcImage, DXGI_FORMAT format, TEX_FILTER_FLAGS filter, float threshold, IntPtr cImage);
432 
433  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
434  private extern static uint dxtConvertArray(Image[] srcImages, int nimages, ref TexMetadata metadata, DXGI_FORMAT format, TEX_FILTER_FLAGS filter, float threshold, IntPtr cImages);
435 
436  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
437  private extern static uint dxtCompress(ref Image srcImage, DXGI_FORMAT format, TEX_COMPRESS_FLAGS compress, float alphaRef, IntPtr cImage);
438 
439  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
440  private extern static uint dxtCompressArray(Image[] srcImages, int nimages, ref TexMetadata metadata, DXGI_FORMAT format, TEX_COMPRESS_FLAGS compress, float alphaRef, IntPtr cImages);
441 
442  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
443  private extern static uint dxtDecompress(ref Image cImage, DXGI_FORMAT format, IntPtr image);
444 
445  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
446  private extern static uint dxtDecompressArray(Image[] cImages, int nimages, ref TexMetadata metadata, DXGI_FORMAT format, IntPtr images);
447 
448  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
449  private extern static uint dxtSaveToDDSFile(ref Image image, DDS_FLAGS flags, string szFile);
450 
451  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
452  private extern static uint dxtSaveToDDSFileArray(Image[] images, int nimages, ref TexMetadata metadata, DDS_FLAGS flags, string szFile);
453 
454  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
455  private extern static uint dxtGenerateMipMaps(ref Image baseImage, TEX_FILTER_FLAGS filter, int levels, IntPtr mipChain, bool allow1D);
456 
457  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
458  private extern static uint dxtGenerateMipMapsArray(Image[] srcImages, int nimages, ref TexMetadata metadata, TEX_FILTER_FLAGS filter, int levels, IntPtr mipChain);
459 
460  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
461  private extern static uint dxtGenerateMipMaps3D(ref Image baseImage, int depth, TEX_FILTER_FLAGS filter, int levels, IntPtr mipChain);
462 
463  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
464  private extern static uint dxtGenerateMipMaps3DArray(Image[] srcImages, int nimages, ref TexMetadata metadata, TEX_FILTER_FLAGS filter, int levels, IntPtr mipChain );
465 
466  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
467  private extern static uint dxtResize(Image[] srcImages, int nimages, ref TexMetadata metadata, int width, int height, TEX_FILTER_FLAGS filter, IntPtr result);
468 
469  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
470  private extern static uint dxtComputeNormalMap(Image[] srcImages, int nimages, ref TexMetadata metadata, CNMAP_FLAGS flags, float amplitude, DXGI_FORMAT format, IntPtr normalMaps );
471 
472  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
473  private extern static uint dxtPremultiplyAlpha(Image[] srcImages, int nimages, ref TexMetadata metadata, TEX_PREMULTIPLY_ALPHA_FLAGS flags, IntPtr result);
474 
475  public static void ComputePitch(DXGI_FORMAT fmt, int width, int height, out int rowPitch, out int slicePitch, CP_FLAGS flags)
476  {
477  dxtComputePitch(fmt, width, height, out rowPitch, out slicePitch, flags);
478  }
479 
480  public static HRESULT LoadDDSFile(String filePath, DDS_FLAGS flags, out TexMetadata metadata, ScratchImage image)
481  {
482  return HandleHRESULT(dxtLoadDDSFile(filePath, flags, out metadata, image.ptr));
483  }
484 
485  public static HRESULT SaveToDDSFile(ref Image image, DDS_FLAGS flags, string szFile)
486  {
487  return HandleHRESULT(dxtSaveToDDSFile(ref image, flags, szFile));
488  }
489 
490  public static HRESULT SaveToDDSFile(Image[] images, int nimages, ref TexMetadata metadata, DDS_FLAGS flags, string szFile)
491  {
492  return HandleHRESULT(dxtSaveToDDSFileArray(images, nimages, ref metadata, flags, szFile));
493  }
494 
495  public static bool IsCompressed(DXGI_FORMAT fmt)
496  {
497  return dxtIsCompressed(fmt);
498  }
499 
500  public static HRESULT Convert(ref Image srcImage, DXGI_FORMAT format, TEX_FILTER_FLAGS filter, float threshold, ScratchImage cImage)
501  {
502  return HandleHRESULT(dxtConvert(ref srcImage, format, filter, threshold, cImage.ptr));
503  }
504 
505  public static HRESULT Convert(Image[] srcImages, int nimages, ref TexMetadata metadata, DXGI_FORMAT format, TEX_FILTER_FLAGS filter, float threshold, ScratchImage cImages)
506  {
507  return HandleHRESULT(dxtConvertArray(srcImages, nimages, ref metadata, format, filter, threshold, cImages.ptr));
508  }
509 
510  public static HRESULT Compress(ref Image srcImage, DXGI_FORMAT format, TEX_COMPRESS_FLAGS compress, float alphaRef, ScratchImage cImage)
511  {
512  return HandleHRESULT(dxtCompress(ref srcImage, format, compress, alphaRef, cImage.ptr));
513  }
514 
515  public static HRESULT Compress(Image[] srcImages, int nimages, ref TexMetadata metadata, DXGI_FORMAT format, TEX_COMPRESS_FLAGS compress, float alphaRef, ScratchImage cImages)
516  {
517  return HandleHRESULT(dxtCompressArray(srcImages, nimages, ref metadata, format, compress, alphaRef, cImages.ptr));
518  }
519 
520  public static HRESULT Decompress(ref Image cImage, DXGI_FORMAT format, ScratchImage image)
521  {
522  return HandleHRESULT(dxtDecompress(ref cImage, format, image.ptr));
523  }
524 
525  public static HRESULT Decompress(Image[] cImages, int nimages, ref TexMetadata metadata, DXGI_FORMAT format, ScratchImage images)
526  {
527  return HandleHRESULT(dxtDecompressArray(cImages, nimages, ref metadata, format, images.ptr));
528  }
529 
530  public static HRESULT GenerateMipMaps(ref Image baseImage, TEX_FILTER_FLAGS filter, int levels, ScratchImage mipChain, bool allow1D = false)
531  {
532  return HandleHRESULT(dxtGenerateMipMaps(ref baseImage, filter, levels, mipChain.ptr, allow1D));
533  }
534 
535  public static HRESULT GenerateMipMaps(Image[] srcImages, int nimages, ref TexMetadata metadata, TEX_FILTER_FLAGS filter, int levels, ScratchImage mipChain)
536  {
537  return HandleHRESULT(dxtGenerateMipMapsArray(srcImages, nimages, ref metadata, filter, levels, mipChain.ptr));
538  }
539 
540  public static HRESULT GenerateMipMaps3D(ref Image baseImage, int depth, TEX_FILTER_FLAGS filter, int levels, ScratchImage mipChain)
541  {
542  return HandleHRESULT(dxtGenerateMipMaps3D(ref baseImage, depth, filter, levels, mipChain.ptr));
543  }
544 
545  public static HRESULT GenerateMipMaps3D(Image[] srcImages, int nimages, ref TexMetadata metadata, TEX_FILTER_FLAGS filter, int levels, ScratchImage mipChain)
546  {
547  return HandleHRESULT(dxtGenerateMipMaps3DArray(srcImages, nimages, ref metadata, filter, levels, mipChain.ptr));
548  }
549 
550  public static HRESULT Resize(Image[] srcImages, int nimages, ref TexMetadata metadata, int width, int height, TEX_FILTER_FLAGS filter, ScratchImage result)
551  {
552  return HandleHRESULT(dxtResize(srcImages, nimages, ref metadata, width, height, filter, result.ptr));
553  }
554 
555  public static HRESULT ComputeNormalMap(Image[] srcImages, int nimages, ref TexMetadata metadata, CNMAP_FLAGS flags, float amplitude, DXGI_FORMAT format, ScratchImage normalMaps)
556  {
557  return HandleHRESULT(dxtComputeNormalMap(srcImages, nimages, ref metadata, flags, amplitude, format, normalMaps.ptr));
558  }
559 
560  public static HRESULT PremultiplyAlpha(Image[] srcImages, int nimages, ref TexMetadata metadata, TEX_PREMULTIPLY_ALPHA_FLAGS flags, ScratchImage result)
561  {
562  return HandleHRESULT(dxtPremultiplyAlpha(srcImages, nimages, ref metadata, flags, result.ptr));
563  }
564 
565 
566  public static HRESULT HandleHRESULT(uint hresult)
567  {
568  switch (hresult)
569  {
570  case 0x00000000: return HRESULT.S_OK;
571  case 0x80004004: return HRESULT.E_ABORT;
572  case 0x80070005: return HRESULT.E_ACCESSDENIED;
573  case 0x80004005: return HRESULT.E_FAIL;
574  case 0x80070006: return HRESULT.E_HANDLE;
575  case 0x80070057: return HRESULT.E_INVALIDARG;
576  case 0x80004002: return HRESULT.E_NOINTERFACE;
577  case 0x80004001: return HRESULT.E_NOTIMPL;
578  case 0x8007000E: return HRESULT.E_OUTOFMEMORY;
579  case 0x80004003: return HRESULT.E_POINTER;
580  case 0x8000FFFF: return HRESULT.E_UNEXPECTED;
581  default: return HRESULT.E_FAIL;
582  }
583  }
584 
585  }
586 
587 
588  /// <summary>
589  /// Binding of the DirectXTex class ScratchImage
590  /// </summary>
591  internal class ScratchImage : IDisposable
592  {
593  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
594  private extern static IntPtr dxtCreateScratchImage();
595 
596  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
597  private extern static void dxtDeleteScratchImage(IntPtr img);
598 
599  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
600  private extern static uint dxtInitialize(IntPtr img, out TexMetadata mdata);
601 
602  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
603  private extern static uint dxtInitialize1D(IntPtr img, DXGI_FORMAT fmt, int length, int arraySize, int mipLevels );
604 
605  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
606  private extern static uint dxtInitialize2D(IntPtr img, DXGI_FORMAT fmt, int width, int height, int arraySize, int mipLevels);
607 
608  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
609  private extern static uint dxtInitialize3D(IntPtr img, DXGI_FORMAT fmt, int width, int height, int depth, int mipLevels );
610 
611  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
612  private extern static uint dxtInitializeCube(IntPtr img, DXGI_FORMAT fmt, int width, int height, int nCubes, int mipLevels );
613 
614  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
615  private extern static uint dxtInitializeFromImage(IntPtr img, out Image srcImage, bool allow1D);
616 
617  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
618  private extern static uint dxtInitializeArrayFromImages(IntPtr img, Image[] images, int nImages, bool allow1D );
619 
620  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
621  private extern static uint dxtInitializeCubeFromImages(IntPtr img, Image[] images, int nImages);
622 
623  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
624  private extern static uint dxtInitialize3DFromImages(IntPtr img, Image[] images, int depth);
625 
626  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
627  private extern static void dxtRelease(IntPtr img);
628 
629  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
630  private extern static bool dxtOverrideFormat(IntPtr img, DXGI_FORMAT f);
631 
632  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
633  private extern static IntPtr dxtGetMetadata(IntPtr img);
634 
635  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
636  private extern static IntPtr dxtGetImage(IntPtr img, int mip, int item, int slice);
637 
638  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
639  private extern static IntPtr dxtGetImages(IntPtr img);
640 
641  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
642  private extern static int dxtGetImageCount(IntPtr img);
643 
644  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
645  private extern static IntPtr dxtGetPixels(IntPtr img);
646 
647  [DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
648  private extern static int dxtGetPixelsSize(IntPtr img);
649 
650 
651  public IntPtr ptr { private set; get; }
652 
653  public ScratchImage()
654  {
655  ptr = dxtCreateScratchImage();
656  }
657 
658  public void Dispose()
659  {
661  }
662 
663  public HRESULT Initialize(out TexMetadata mdata)
664  {
665  return Utilities.HandleHRESULT(dxtInitialize(ptr, out mdata));
666  }
667 
668  public HRESULT Initialize1D(DXGI_FORMAT fmt, int length, int arraySize, int mipLevels)
669  {
670  return Utilities.HandleHRESULT(dxtInitialize1D(ptr, fmt, length, arraySize, mipLevels));
671  }
672 
673  public HRESULT Initialize2D(DXGI_FORMAT fmt, int width, int height, int arraySize, int mipLevels)
674  {
675  return Utilities.HandleHRESULT(dxtInitialize2D(ptr, fmt, width, height, arraySize, mipLevels));
676  }
677 
678  public HRESULT Initialize3D(DXGI_FORMAT fmt, int width, int height, int depth, int mipLevels)
679  {
680  return Utilities.HandleHRESULT(dxtInitialize3D(ptr, fmt, width, height, depth, mipLevels));
681  }
682 
683  public HRESULT InitializeCube(DXGI_FORMAT fmt, int width, int height, int nCubes, int mipLevels)
684  {
685  return Utilities.HandleHRESULT(dxtInitializeCube(ptr, fmt, width, height, nCubes, mipLevels));
686  }
687 
688  public HRESULT InitializeFromImage(out Image srcImage, bool allow1D = false)
689  {
690  return Utilities.HandleHRESULT(dxtInitializeFromImage(ptr, out srcImage, allow1D));
691  }
692 
693  public HRESULT InitializeFromImages(Image[] images, int nImages, bool allow1D = false)
694  {
695  return Utilities.HandleHRESULT(dxtInitializeArrayFromImages(ptr, images, nImages, allow1D));
696  }
697 
698  public HRESULT InitializeCubeFromImages(Image[] images, int nImages)
699  {
700  return Utilities.HandleHRESULT(dxtInitializeCubeFromImages(ptr, images, nImages));
701  }
702 
703  public HRESULT Initialize3DFromImages(Image[] images, int depth)
704  {
705  return Utilities.HandleHRESULT(dxtInitialize3DFromImages(ptr, images, depth));
706  }
707 
708  public void Release()
709  {
710  dxtRelease(ptr);
711  }
712 
713  public bool OverrideFormat(TexMetadata mdata, DXGI_FORMAT f)
714  {
715  return dxtOverrideFormat(ptr, f);
716  }
717 
718  public TexMetadata metadata
719  {
720  get {return (TexMetadata)Marshal.PtrToStructure(dxtGetMetadata(ptr), typeof(TexMetadata));}
721  }
722 
723  public IntPtr data
724  {
725  get { return dxtGetPixels(ptr); }
726  }
727 
728  public int pixelSize
729  {
730  get { return dxtGetPixelsSize(ptr); }
731  }
732 
733  public int imageCount
734  {
735  get { return dxtGetImageCount(ptr); }
736  }
737 
738  public Image GetImage(int mip, int item, int slice)
739  {
740  return (Image)Marshal.PtrToStructure(dxtGetImage(ptr, mip, item, slice), typeof(Image));
741  }
742 
743  public Image[] GetImages()
744  {
745  IntPtr imagesPtr = dxtGetImages(ptr);
746  int imagenb = imageCount;
747 
748  Image[] images = new Image[imagenb];
749 
750 
751  for(int i=0;i<imagenb;++i)
752  {
753  images[i] = (Image)Marshal.PtrToStructure(imagesPtr + i * Marshal.SizeOf(images[0]), typeof(Image));
754  }
755 
756  return images;
757  }
758 
759  }
760 }
HRESULT dxtInitialize3D(DirectX::ScratchImage *img, DXGI_FORMAT fmt, size_t width, size_t height, size_t depth, size_t mipLevels)
HRESULT dxtInitialize1D(DirectX::ScratchImage *img, DXGI_FORMAT fmt, size_t length, size_t arraySize, size_t mipLevels)
HRESULT dxtConvertArray(const DirectX::Image *srcImages, size_t nimages, const DirectX::TexMetadata &metadata, DXGI_FORMAT format, DWORD filter, float threshold, DirectX::ScratchImage &cImage)
Definition: dxt_wrapper.cpp:16
_In_ size_t _In_ const TexMetadata _In_ DWORD _In_ size_t nImages
Definition: DirectXTexP.h:118
HRESULT Resize(_In_ const Image &srcImage, _In_ size_t width, _In_ size_t height, _In_ DWORD filter, _Out_ ScratchImage &image)
HRESULT dxtLoadDDSFile(LPCWSTR szFile, DWORD flags, DirectX::TexMetadata *metadata, DirectX::ScratchImage &image)
Definition: dxt_wrapper.cpp:78
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ DXGI_FORMAT _In_ DWORD flags
Definition: DirectXTexP.h:170
HRESULT dxtInitializeCubeFromImages(DirectX::ScratchImage *img, const DirectX::Image *images, size_t nImages)
HRESULT dxtGenerateMipMaps(const DirectX::Image &baseImage, DWORD filter, size_t levels, DirectX::ScratchImage &mipChain, bool allow1D=false)
Definition: dxt_wrapper.cpp:41
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float threshold
Definition: DirectXTexP.h:183
_In_ size_t pixelSize
Definition: DirectXTexP.h:116
const DirectX::TexMetadata & dxtGetMetadata(const DirectX::ScratchImage *img)
void dxtDeleteScratchImage(DirectX::ScratchImage *img)
Definition: dxt_wrapper.cpp:99
HRESULT dxtDecompressArray(const DirectX::Image *cImages, size_t nimages, const DirectX::TexMetadata &metadata, DXGI_FORMAT format, DirectX::ScratchImage &images)
Definition: dxt_wrapper.cpp:36
const DirectX::Image * dxtGetImage(const DirectX::ScratchImage *img, size_t mip, size_t item, size_t slice)
HRESULT dxtInitialize2D(DirectX::ScratchImage *img, DXGI_FORMAT fmt, size_t width, size_t height, size_t arraySize, size_t mipLevels)
HRESULT dxtResize(const DirectX::Image *srcImages, size_t nimages, const DirectX::TexMetadata &metadata, size_t width, size_t height, DWORD filter, DirectX::ScratchImage &result)
Definition: dxt_wrapper.cpp:61
HRESULT dxtGenerateMipMaps3DArray(const DirectX::Image *srcImages, size_t nimages, const DirectX::TexMetadata &metadata, DWORD filter, size_t levels, DirectX::ScratchImage &mipChain)
Definition: dxt_wrapper.cpp:56
HRESULT GenerateMipMaps(_In_ const Image &baseImage, _In_ DWORD filter, _In_ size_t levels, _Inout_ ScratchImage &mipChain, _In_ bool allow1D=false)
Flags
Enumeration of the new Assimp's flags.
_In_ size_t _In_ const TexMetadata & metadata
Definition: DirectXTexP.h:116
void ComputePitch(_In_ DXGI_FORMAT fmt, _In_ size_t width, _In_ size_t height, _Out_ size_t &rowPitch, _Out_ size_t &slicePitch, _In_ DWORD flags=CP_FLAGS_NONE)
const DirectX::Image * dxtGetImages(const DirectX::ScratchImage *img)
DirectX::ScratchImage * dxtCreateScratchImage()
Definition: dxt_wrapper.cpp:94
HRESULT dxtInitializeArrayFromImages(DirectX::ScratchImage *img, const DirectX::Image *images, size_t nImages, bool allow1D)
HRESULT dxtConvert(const DirectX::Image &srcImage, DXGI_FORMAT format, DWORD filter, float threshold, DirectX::ScratchImage &cImage)
Definition: dxt_wrapper.cpp:11
size_t dxtGetImageCount(const DirectX::ScratchImage *img)
SiliconStudio.Core.Utilities Utilities
Definition: Texture.cs:29
HRESULT dxtInitializeCube(DirectX::ScratchImage *img, DXGI_FORMAT fmt, size_t width, size_t height, size_t nCubes, size_t mipLevels)
HRESULT dxtCompress(const DirectX::Image &srcImage, DXGI_FORMAT format, DWORD compress, float alphaRef, DirectX::ScratchImage &cImage)
Definition: dxt_wrapper.cpp:21
HRESULT GenerateMipMaps3D(_In_reads_(depth) const Image *baseImages, _In_ size_t depth, _In_ DWORD filter, _In_ size_t levels, _Out_ ScratchImage &mipChain)
HRESULT dxtSaveToDDSFile(const DirectX::Image &image, DWORD flags, LPCWSTR szFile)
Definition: dxt_wrapper.cpp:83
bool dxtOverrideFormat(DirectX::ScratchImage *img, DXGI_FORMAT f)
HRESULT Convert(_In_ const Image &srcImage, _In_ DXGI_FORMAT format, _In_ DWORD filter, _In_ float threshold, _Out_ ScratchImage &image)
HRESULT dxtGenerateMipMapsArray(const DirectX::Image *srcImages, size_t nimages, const DirectX::TexMetadata &metadata, DWORD filter, size_t levels, DirectX::ScratchImage &mipChain)
Definition: dxt_wrapper.cpp:46
HRESULT Decompress(_In_ const Image &cImage, _In_ DXGI_FORMAT format, _Out_ ScratchImage &image)
_Use_decl_annotations_ bool IsCompressed(DXGI_FORMAT fmt)
Definition: DirectXTex.inl:31
HRESULT dxtPremultiplyAlpha(const DirectX::Image *srcImages, size_t nimages, const DirectX::TexMetadata &metadata, DWORD flags, DirectX::ScratchImage &result)
Definition: dxt_wrapper.cpp:71
HRESULT ComputeNormalMap(_In_ const Image &srcImage, _In_ DWORD flags, _In_ float amplitude, _In_ DXGI_FORMAT format, _Out_ ScratchImage &normalMap)
size_t dxtGetPixelsSize(const DirectX::ScratchImage *img)
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
Definition: DirectXTexP.h:175
bool dxtIsCompressed(DXGI_FORMAT fmt)
Definition: dxt_wrapper.cpp:9
HRESULT dxtGenerateMipMaps3D(const DirectX::Image *baseImages, size_t depth, DWORD filter, size_t levels, DirectX::ScratchImage &mipChain)
Definition: dxt_wrapper.cpp:51
HRESULT dxtInitializeFromImage(DirectX::ScratchImage *img, const DirectX::Image &srcImage, bool allow1D)
uint8_t * dxtGetPixels(const DirectX::ScratchImage *img)
HRESULT dxtInitialize(DirectX::ScratchImage *img, const DirectX::TexMetadata &mdata)
HRESULT dxtInitialize3DFromImages(DirectX::ScratchImage *img, const DirectX::Image *images, size_t depth)
HRESULT dxtCompressArray(const DirectX::Image *srcImages, size_t nimages, const DirectX::TexMetadata &metadata, DXGI_FORMAT format, DWORD compress, float alphaRef, DirectX::ScratchImage &cImages)
Definition: dxt_wrapper.cpp:26
HRESULT dxtComputeNormalMap(const DirectX::Image *srcImages, size_t nimages, const DirectX::TexMetadata &metadata, DWORD flags, float amplitude, DXGI_FORMAT format, DirectX::ScratchImage &normalMaps)
Definition: dxt_wrapper.cpp:66
HRESULT dxtDecompress(const DirectX::Image &cImage, DXGI_FORMAT format, DirectX::ScratchImage &image)
Definition: dxt_wrapper.cpp:31
HRESULT Compress(_In_ const Image &srcImage, _In_ DXGI_FORMAT format, _In_ DWORD compress, _In_ float alphaRef, _Out_ ScratchImage &cImage)
_Use_decl_annotations_ HRESULT SaveToDDSFile(const Image &image, DWORD flags, LPCWSTR szFile)
Definition: DirectXTex.inl:339
void dxtComputePitch(DXGI_FORMAT fmt, size_t width, size_t height, size_t &rowPitch, size_t &slicePitch, DWORD flags=DirectX::CP_FLAGS_NONE)
Definition: dxt_wrapper.cpp:4
HRESULT PremultiplyAlpha(_In_ const Image &srcImage, _In_ DWORD flags, _Out_ ScratchImage &image)
void dxtRelease(DirectX::ScratchImage *img)
HRESULT dxtSaveToDDSFileArray(const DirectX::Image *images, size_t nimages, const DirectX::TexMetadata &metadata, DWORD flags, LPCWSTR szFile)
Definition: dxt_wrapper.cpp:88