Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AtitcNetWrapper.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.Security;
5 using System.Runtime.InteropServices;
6 
7 namespace SiliconStudio.TextureConverter.AtitcWrapper
8 {
9  #region Enum
10  internal enum Format
11  {
12  ATI_TC_FORMAT_Unknown, ///< An undefined texture format.
13  ATI_TC_FORMAT_ARGB_8888, ///< An ARGB format with 8-bit fixed channels.
14  ATI_TC_FORMAT_RGB_888, ///< A RGB format with 8-bit fixed channels.
15  ATI_TC_FORMAT_RG_8, ///< A two component format with 8-bit fixed channels.
16  ATI_TC_FORMAT_R_8, ///< A single component format with 8-bit fixed channels.
17  ATI_TC_FORMAT_ARGB_2101010, ///< An ARGB format with 10-bit fixed channels for color & a 2-bit fixed channel for alpha.
18  ATI_TC_FORMAT_ARGB_16, ///< A ARGB format with 16-bit fixed channels.
19  ATI_TC_FORMAT_RG_16, ///< A two component format with 16-bit fixed channels.
20  ATI_TC_FORMAT_R_16, ///< A single component format with 16-bit fixed channels.
21  ATI_TC_FORMAT_ARGB_16F, ///< An ARGB format with 16-bit floating-point channels.
22  ATI_TC_FORMAT_RG_16F, ///< A two component format with 16-bit floating-point channels.
23  ATI_TC_FORMAT_R_16F, ///< A single component with 16-bit floating-point channels.
24  ATI_TC_FORMAT_ARGB_32F, ///< An ARGB format with 32-bit floating-point channels.
25  ATI_TC_FORMAT_RG_32F, ///< A two component format with 32-bit floating-point channels.
26  ATI_TC_FORMAT_R_32F, ///< A single component with 32-bit floating-point channels.
27  ATI_TC_FORMAT_DXT1, ///< An opaque (or 1-bit alpha) DXTC compressed texture format. Four bits per pixel.
28  ATI_TC_FORMAT_DXT3, ///< A DXTC compressed texture format with explicit alpha. Eight bits per pixel.
29  ATI_TC_FORMAT_DXT5, ///< A DXTC compressed texture format with interpolated alpha. Eight bits per pixel.
30  ATI_TC_FORMAT_DXT5_xGBR, ///< A DXT5 with the red component swizzled into the alpha channel. Eight bits per pixel.
31  ATI_TC_FORMAT_DXT5_RxBG, ///< A swizzled DXT5 format with the green component swizzled into the alpha channel. Eight bits per pixel.
32  ATI_TC_FORMAT_DXT5_RBxG, ///< A swizzled DXT5 format with the green component swizzled into the alpha channel & the blue component swizzled into the green channel. Eight bits per pixel.
33  ATI_TC_FORMAT_DXT5_xRBG, ///< A swizzled DXT5 format with the green component swizzled into the alpha channel & the red component swizzled into the green channel. Eight bits per pixel.
34  ATI_TC_FORMAT_DXT5_RGxB, ///< A swizzled DXT5 format with the blue component swizzled into the alpha channel. Eight bits per pixel.
35  ATI_TC_FORMAT_DXT5_xGxR, ///< A two-component swizzled DXT5 format with the red component swizzled into the alpha channel & the green component in the green channel. Eight bits per pixel.
36  ATI_TC_FORMAT_ATI1N, ///< A single component compression format using the same technique as DXT5 alpha. Four bits per pixel.
37  ATI_TC_FORMAT_ATI2N, ///< A two component compression format using the same technique as DXT5 alpha. Designed for compression object space normal maps. Eight bits per pixel.
38  ATI_TC_FORMAT_ATI2N_XY, ///< A two component compression format using the same technique as DXT5 alpha. The same as ATI2N but with the channels swizzled. Eight bits per pixel.
39  ATI_TC_FORMAT_ATI2N_DXT5, ///< An ATI2N like format using DXT5. Intended for use on GPUs that do not natively support ATI2N. Eight bits per pixel.
40  ATI_TC_FORMAT_BC1, ///< A four component opaque (or 1-bit alpha) compressed texture format for Microsoft DirectX10. Identical to DXT1. Four bits per pixel.
41  ATI_TC_FORMAT_BC2, ///< A four component compressed texture format with explicit alpha for Microsoft DirectX10. Identical to DXT3. Eight bits per pixel.
42  ATI_TC_FORMAT_BC3, ///< A four component compressed texture format with interpolated alpha for Microsoft DirectX10. Identical to DXT5. Eight bits per pixel.
43  ATI_TC_FORMAT_BC4, ///< A single component compressed texture format for Microsoft DirectX10. Identical to ATI1N. Four bits per pixel.
44  ATI_TC_FORMAT_BC5, ///< A two component compressed texture format for Microsoft DirectX10. Identical to ATI2N. Eight bits per pixel.
45  ATI_TC_FORMAT_ATC_RGB, ///< ATI_TC - a compressed RGB format for cellphones & hand-held devices.
46  ATI_TC_FORMAT_ATC_RGBA_Explicit, ///< ATI_TC - a compressed ARGB format with explicit alpha for cellphones & hand-held devices.
47  ATI_TC_FORMAT_ATC_RGBA_Interpolated, ///< ATI_TC - a compressed ARGB format with interpolated alpha for cellphones & hand-held devices.
48  ATI_TC_FORMAT_ETC_RGB, ///< ETC (aka Ericsson Texture Compression) - a compressed RGB format for cellphones.
49  ATI_TC_FORMAT_MAX = ATI_TC_FORMAT_ETC_RGB
50  } ;
51 
52  internal enum Result
53  {
54  ATI_TC_OK = 0, ///< Ok.
55  ATI_TC_ABORTED, ///< The conversion was aborted.
56  ATI_TC_ERR_INVALID_SOURCE_TEXTURE, ///< The source texture is invalid.
57  ATI_TC_ERR_INVALID_DEST_TEXTURE, ///< The destination texture is invalid.
58  ATI_TC_ERR_UNSUPPORTED_SOURCE_FORMAT, ///< The source format is not a supported format.
59  ATI_TC_ERR_UNSUPPORTED_DEST_FORMAT, ///< The destination format is not a supported format.
60  ATI_TC_ERR_SIZE_MISMATCH, ///< The source and destination texture sizes do not match.
61  ATI_TC_ERR_UNABLE_TO_INIT_CODEC, ///< ATI_Compress was unable to initialize the codec needed for conversion.
62  ATI_TC_ERR_GENERIC ///< An unknown error occurred.
63  } ;
64 
65  internal enum Speed
66  {
67  ATI_TC_Speed_Normal, ///< Highest quality mode
68  ATI_TC_Speed_Fast, ///< Slightly lower quality but much faster compression mode - DXTn & ATInN only
69  ATI_TC_Speed_SuperFast, ///< Slightly lower quality but much, much faster compression mode - DXTn & ATInN only
70  } ;
71  #endregion
72 
73 
74  /// <summary>
75  /// C# Equivalent of ATC structure CompressOptions
76  /// </summary>
77  internal struct CompressOptions
78  {
79  public int dwSize; ///< The size of this structure.
80  public bool bUseChannelWeighting; ///< Use channel weightings. With swizzled formats the weighting applies to the data within the specified channel not the channel itself.
81  public double fWeightingRed; ///< The weighting of the Red or X Channel.
82  public double fWeightingGreen; ///< The weighting of the Green or Y Channel.
83  public double fWeightingBlue; ///< The weighting of the Blue or Z Channel.
84  public bool bUseAdaptiveWeighting; ///< Adapt weighting on a per-block basis.
85  public bool bDXT1UseAlpha; ///< Encode single-bit alpha data. Only valid when compressing to DXT1 & BC1.
86  public byte nAlphaThreshold; ///< The alpha threshold to use when compressing to DXT1 & BC1 with bDXT1UseAlpha. Texels with an alpha value less than the threshold are treated as transparent.
87  public bool bDisableMultiThreading; ///< Disable multi-threading of the compression. This will slow the compression but can be useful if you're managing threads in your application.
88  public Speed nCompressionSpeed; ///< The trade-off between compression speed & quality.
89 
90  public unsafe CompressOptions(bool bUseChannelWeighting, double fWeightingRed, double fWeightingGreen, double fWeightingBlue, bool bUseAdaptiveWeighting, bool bDXT1UseAlpha, byte nAlphaThreshold, bool bDisableMultiThreading, Speed nCompressionSpeed)
91  {
92  this.dwSize = sizeof(CompressOptions);
93  this.bUseChannelWeighting = bUseChannelWeighting;
94  this.fWeightingRed = fWeightingRed;
95  this.fWeightingGreen = fWeightingGreen;
96  this.fWeightingBlue = fWeightingBlue;
97  this.bUseAdaptiveWeighting = bUseAdaptiveWeighting;
98  this.bDXT1UseAlpha = bDXT1UseAlpha;
99  this.nAlphaThreshold = nAlphaThreshold;
100  this.bDisableMultiThreading = bDisableMultiThreading;
101  this.nCompressionSpeed = nCompressionSpeed;
102  }
103  }
104 
105  /// <summary>
106  /// C# Equivalent of ATC structure Texture
107  /// </summary>
108  internal struct Texture
109  {
110  public int dwSize; ///< Size of this structure.
111  public int dwWidth; ///< Width of the texture.
112  public int dwHeight; ///< Height of the texture.
113  public int dwPitch; ///< Distance to start of next line - necessary only for uncompressed textures.
114  public Format format; ///< Format of the texture.
115  public int dwDataSize; ///< Size of the allocated texture data.
116  public IntPtr pData; ///< Pointer to the texture data
117 
118  public unsafe Texture(int dwWidth, int dwHeight, int dwPitch, Format format, int dwDataSize, IntPtr pData)
119  {
120  this.dwSize = sizeof(Texture);
121  this.dwWidth = dwWidth;
122  this.dwHeight = dwHeight;
123  this.dwPitch = dwPitch;
124  this.format = format;
125  this.dwDataSize = dwDataSize;
126  this.pData = pData;
127  }
128 
129  public override String ToString()
130  {
131  return "width:" + dwWidth + "\nheight:" + dwHeight + "\nformat:" + format + "\nrowPitch:" + dwPitch + "\nSize:" + dwDataSize + "\npixels:" + pData;
132  }
133  }
134 
135  /// <summary>
136  /// Binding of the ATC utility methods
137  /// </summary>
138  internal class Utilities
139  {
140 
141  #region Bindings
142  [DllImport("AtitcWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
143  private extern static int atitcCalculateBufferSize(out Texture pTexture);
144 
145  [DllImport("AtitcWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
146  private extern static void atitcDeleteData(out Texture pTexture);
147 
148  [DllImport("AtitcWrapper", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
149  private extern static Result atitcConvertTexture(out Texture pSourceTexture, out Texture pDestTexture, out CompressOptions pOptions);
150  #endregion
151 
152  public static int CalculateBufferSize(out Texture pTexture)
153  {
154  return atitcCalculateBufferSize(out pTexture);
155  }
156 
157  public static Result ConvertTexture(out Texture pSourceTexture, out Texture pDestTexture, out CompressOptions pOptions)
158  {
159  return atitcConvertTexture(out pSourceTexture, out pDestTexture, out pOptions);
160  }
161 
162  public static void DeleteData(out Texture pTexture)
163  {
164  atitcDeleteData(out pTexture);
165  }
166  }
167 
168 }
A swizzled DXT5 format with the blue component swizzled into the alpha channel. Eight bits per pixel...
The source and destination texture sizes do not match.
ATI_TC - a compressed ARGB format with interpolated alpha for cellphones & hand-held devices...
ATI_TC_ERROR atitcConvertTexture(const ATI_TC_Texture *pSourceTexture, ATI_TC_Texture *pDestTexture, const ATI_TC_CompressOptions *pOptions)
An ARGB format with 10-bit fixed channels for color & a 2-bit fixed channel for alpha.
ATI_TC - a compressed RGB format for cellphones & hand-held devices.
A DXTC compressed texture format with interpolated alpha. Eight bits per pixel.
A two component format with 32-bit floating-point channels.
ATI_TC - a compressed ARGB format with explicit alpha for cellphones & hand-held devices.
A single component compressed texture format for Microsoft DirectX10. Identical to ATI1N...
int atitcCalculateBufferSize(const ATI_TC_Texture *pTexture)
A swizzled DXT5 format with the green component swizzled into the alpha channel & the red component s...
A two component format with 8-bit fixed channels.
A single component format with 8-bit fixed channels.
A single component format with 16-bit fixed channels.
SiliconStudio.Core.Utilities Utilities
Definition: Texture.cs:29
A two component compression format using the same technique as DXT5 alpha. The same as ATI2N but with...
A DXT5 with the red component swizzled into the alpha channel. Eight bits per pixel.
void atitcDeleteData(ATI_TC_Texture *pTexture)
A four component compressed texture format with interpolated alpha for Microsoft DirectX10. Identical to DXT5. Eight bits per pixel.
A two component compressed texture format for Microsoft DirectX10. Identical to ATI2N. Eight bits per pixel.
An opaque (or 1-bit alpha) DXTC compressed texture format. Four bits per pixel.
Slightly lower quality but much faster compression mode - DXTn & ATInN only.
A four component compressed texture format with explicit alpha for Microsoft DirectX10. Identical to DXT3. Eight bits per pixel.
A single component with 16-bit floating-point channels.
ETC (aka Ericsson Texture Compression) - a compressed RGB format for cellphones.
A two component format with 16-bit fixed channels.
A two component compression format using the same technique as DXT5 alpha. Designed for compression o...
A two-component swizzled DXT5 format with the red component swizzled into the alpha channel & the gre...
An ARGB format with 16-bit floating-point channels.
A two component format with 16-bit floating-point channels.
Slightly lower quality but much, much faster compression mode - DXTn & ATInN only.
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
Definition: DirectXTexP.h:175
A swizzled DXT5 format with the green component swizzled into the alpha channel & the blue component ...
A single component compression format using the same technique as DXT5 alpha. Four bits per pixel...
A swizzled DXT5 format with the green component swizzled into the alpha channel. Eight bits per pixel...
A four component opaque (or 1-bit alpha) compressed texture format for Microsoft DirectX10. Identical to DXT1. Four bits per pixel.
An ATI2N like format using DXT5. Intended for use on GPUs that do not natively support ATI2N...
An ARGB format with 32-bit floating-point channels.
A DXTC compressed texture format with explicit alpha. Eight bits per pixel.
A single component with 32-bit floating-point channels.
ATI_Compress was unable to initialize the codec needed for conversion.