Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Tools.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 
5 using SiliconStudio.Paradox.Graphics;
6 
7 namespace SiliconStudio.TextureConverter
8 {
9  /// <summary>
10  /// Provides general methods used by the libraries.
11  /// </summary>
12  internal class Tools
13  {
14  /// <summary>
15  /// Computes the pitch.
16  /// </summary>
17  /// <param name="fmt">The format.</param>
18  /// <param name="width">The width.</param>
19  /// <param name="height">The height.</param>
20  /// <param name="rowPitch">output row pitch.</param>
21  /// <param name="slicePitch">output slice pitch.</param>
22  public static void ComputePitch(PixelFormat fmt, int width, int height, out int rowPitch, out int slicePitch)
23  {
24  int widthCount = width;
25  int heightCount = height;
26 
27  int bpp = (int)GetBPP(fmt);
28 
29  if (IsCompressed(fmt))
30  {
31  widthCount = Math.Max(1, (width + 3) / 4);
32  heightCount = Math.Max(1, (height + 3) / 4);
33  rowPitch = widthCount * bpp;
34 
35  slicePitch = rowPitch * heightCount;
36  }
37  else if (fmt.IsPacked())
38  {
39  rowPitch = ((width + 1) >> 1) * 4;
40 
41  slicePitch = rowPitch * height;
42  }
43  else
44  {
45  if (bpp == 0)
46  bpp = fmt.SizeInBits();
47 
48  rowPitch = (width * bpp + 7) / 8;
49  slicePitch = rowPitch * height;
50  }
51  }
52 
53 
54  /// <summary>
55  /// Gets the BPP of the specified format.
56  /// </summary>
57  /// <param name="format">The format.</param>
58  /// <returns>the bytes per pixel of the specified format</returns>
59  public static uint GetBPP(PixelFormat format)
60  {
61  switch (format)
62  {
63  case PixelFormat.R32G32B32A32_Typeless:
64  case PixelFormat.R32G32B32A32_Float:
65  case PixelFormat.R32G32B32A32_UInt:
66  case PixelFormat.R32G32B32A32_SInt:
67  return 128;
68 
69  case PixelFormat.R32G32B32_Typeless:
70  case PixelFormat.R32G32B32_Float:
71  case PixelFormat.R32G32B32_UInt:
72  case PixelFormat.R32G32B32_SInt:
73  return 96;
74 
75  case PixelFormat.R16G16B16A16_Typeless:
76  case PixelFormat.R16G16B16A16_Float:
77  case PixelFormat.R16G16B16A16_UNorm:
78  case PixelFormat.R16G16B16A16_UInt:
79  case PixelFormat.R16G16B16A16_SNorm:
80  case PixelFormat.R16G16B16A16_SInt:
81  case PixelFormat.R32G32_Typeless:
82  case PixelFormat.R32G32_Float:
83  case PixelFormat.R32G32_UInt:
84  case PixelFormat.R32G32_SInt:
85  case PixelFormat.R32G8X24_Typeless:
86  case PixelFormat.D32_Float_S8X24_UInt:
87  case PixelFormat.R32_Float_X8X24_Typeless:
88  case PixelFormat.X32_Typeless_G8X24_UInt:
89  return 64;
90 
91  case PixelFormat.R10G10B10A2_Typeless:
92  case PixelFormat.R10G10B10A2_UNorm:
93  case PixelFormat.R10G10B10A2_UInt:
94  case PixelFormat.R11G11B10_Float:
95  case PixelFormat.R8G8B8A8_Typeless:
96  case PixelFormat.R8G8B8A8_UNorm:
97  case PixelFormat.R8G8B8A8_UNorm_SRgb:
98  case PixelFormat.R8G8B8A8_UInt:
99  case PixelFormat.R8G8B8A8_SNorm:
100  case PixelFormat.R8G8B8A8_SInt:
101  case PixelFormat.R16G16_Typeless:
102  case PixelFormat.R16G16_Float:
103  case PixelFormat.R16G16_UNorm:
104  case PixelFormat.R16G16_UInt:
105  case PixelFormat.R16G16_SNorm:
106  case PixelFormat.R16G16_SInt:
107  case PixelFormat.R32_Typeless:
108  case PixelFormat.D32_Float:
109  case PixelFormat.R32_Float:
110  case PixelFormat.R32_UInt:
111  case PixelFormat.R32_SInt:
112  case PixelFormat.R24G8_Typeless:
113  case PixelFormat.D24_UNorm_S8_UInt:
114  case PixelFormat.R24_UNorm_X8_Typeless:
115  case PixelFormat.X24_Typeless_G8_UInt:
116  case PixelFormat.R9G9B9E5_Sharedexp:
117  case PixelFormat.R8G8_B8G8_UNorm:
118  case PixelFormat.G8R8_G8B8_UNorm:
119  case PixelFormat.B8G8R8A8_UNorm:
120  case PixelFormat.B8G8R8X8_UNorm:
121  case PixelFormat.R10G10B10_Xr_Bias_A2_UNorm:
122  case PixelFormat.B8G8R8A8_Typeless:
123  case PixelFormat.B8G8R8A8_UNorm_SRgb:
124  case PixelFormat.B8G8R8X8_Typeless:
125  case PixelFormat.B8G8R8X8_UNorm_SRgb:
126  return 32;
127 
128  case PixelFormat.R8G8_Typeless:
129  case PixelFormat.R8G8_UNorm:
130  case PixelFormat.R8G8_UInt:
131  case PixelFormat.R8G8_SNorm:
132  case PixelFormat.R8G8_SInt:
133  case PixelFormat.R16_Typeless:
134  case PixelFormat.R16_Float:
135  case PixelFormat.D16_UNorm:
136  case PixelFormat.R16_UNorm:
137  case PixelFormat.R16_UInt:
138  case PixelFormat.R16_SNorm:
139  case PixelFormat.R16_SInt:
140  case PixelFormat.B5G6R5_UNorm:
141  case PixelFormat.B5G5R5A1_UNorm:
142  return 16;
143 
144  case PixelFormat.R8_Typeless:
145  case PixelFormat.R8_UNorm:
146  case PixelFormat.R8_UInt:
147  case PixelFormat.R8_SNorm:
148  case PixelFormat.R8_SInt:
149  case PixelFormat.A8_UNorm:
150  return 8;
151 
152 
153  case PixelFormat.BC1_Typeless:
154  case PixelFormat.BC1_UNorm:
155  case PixelFormat.BC1_UNorm_SRgb:
156  case PixelFormat.BC4_Typeless:
157  case PixelFormat.BC4_UNorm:
158  case PixelFormat.BC4_SNorm:
159  return 8;
160 
161 
162  case PixelFormat.BC2_Typeless:
163  case PixelFormat.BC2_UNorm:
164  case PixelFormat.BC2_UNorm_SRgb:
165  case PixelFormat.BC3_Typeless:
166  case PixelFormat.BC3_UNorm:
167  case PixelFormat.BC3_UNorm_SRgb:
168  case PixelFormat.BC5_Typeless:
169  case PixelFormat.BC5_UNorm:
170  case PixelFormat.BC5_SNorm:
171  case PixelFormat.BC6H_Typeless:
172  case PixelFormat.BC6H_Uf16:
173  case PixelFormat.BC6H_Sf16:
174  case PixelFormat.BC7_Typeless:
175  case PixelFormat.BC7_UNorm:
176  case PixelFormat.BC7_UNorm_SRgb:
177  return 16;
178 
179 
180  case PixelFormat.PVRTC_2bpp_RGB:
181  return 3;
182  case PixelFormat.PVRTC_2bpp_RGBA:
183  case PixelFormat.PVRTC_II_2bpp:
184  return 4;
185  case PixelFormat.PVRTC_4bpp_RGB:
186  return 6;
187  case PixelFormat.PVRTC_4bpp_RGBA:
188  case PixelFormat.PVRTC_II_4bpp:
189  return 8;
190 
191 
192  case PixelFormat.ETC1:
193  case PixelFormat.ETC2_RGBA:
194  case PixelFormat.ETC2_RGB_A1:
195  case PixelFormat.EAC_R11_Unsigned:
196  case PixelFormat.EAC_R11_Signed:
197  case PixelFormat.EAC_RG11_Unsigned:
198  case PixelFormat.EAC_RG11_Signed:
199  return 8;
200  case PixelFormat.ETC2_RGB:
201  return 6;
202 
203 
204  case PixelFormat.ATC_RGB:
205  return 12;
206  case PixelFormat.ATC_RGBA_Explicit:
207  case PixelFormat.ATC_RGBA_Interpolated:
208  return 16;
209 
210  case PixelFormat.R1_UNorm:
211  return 1;
212 
213  case PixelFormat.None:
214  default: return 0;
215  }
216  }
217 
218 
219  /// <summary>
220  /// Determines whether two different formats are in same channel order.
221  /// </summary>
222  /// <param name="format1">The format1.</param>
223  /// <param name="format2">The format2.</param>
224  /// <returns>
225  /// <c>true</c> if the formats are in the same channel order; otherwise, <c>false</c>.
226  /// </returns>
227  public static bool IsInSameChannelOrder(PixelFormat format1, PixelFormat format2)
228  {
229  return IsInBGRAOrder(format1) && IsInBGRAOrder(format2) || IsInRGBAOrder(format1) && IsInRGBAOrder(format2);
230  }
231 
232 
233  /// <summary>
234  /// Determines whether the specified format is in RGBA order.
235  /// </summary>
236  /// <param name="format">The format.</param>
237  /// <returns>
238  /// <c>true</c> if the specified format is in RGBA order; otherwise, <c>false</c>.
239  /// </returns>
240  public static bool IsInRGBAOrder(PixelFormat format)
241  {
242  switch (format)
243  {
244  case PixelFormat.R32G32B32A32_Typeless:
245  case PixelFormat.R32G32B32A32_Float:
246  case PixelFormat.R32G32B32A32_UInt:
247  case PixelFormat.R32G32B32A32_SInt:
248  case PixelFormat.R32G32B32_Typeless:
249  case PixelFormat.R32G32B32_Float:
250  case PixelFormat.R32G32B32_UInt:
251  case PixelFormat.R32G32B32_SInt:
252  case PixelFormat.R16G16B16A16_Typeless:
253  case PixelFormat.R16G16B16A16_Float:
254  case PixelFormat.R16G16B16A16_UNorm:
255  case PixelFormat.R16G16B16A16_UInt:
256  case PixelFormat.R16G16B16A16_SNorm:
257  case PixelFormat.R16G16B16A16_SInt:
258  case PixelFormat.R32G32_Typeless:
259  case PixelFormat.R32G32_Float:
260  case PixelFormat.R32G32_UInt:
261  case PixelFormat.R32G32_SInt:
262  case PixelFormat.R32G8X24_Typeless:
263  case PixelFormat.R10G10B10A2_Typeless:
264  case PixelFormat.R10G10B10A2_UNorm:
265  case PixelFormat.R10G10B10A2_UInt:
266  case PixelFormat.R11G11B10_Float:
267  case PixelFormat.R8G8B8A8_Typeless:
268  case PixelFormat.R8G8B8A8_UNorm:
269  case PixelFormat.R8G8B8A8_UNorm_SRgb:
270  case PixelFormat.R8G8B8A8_UInt:
271  case PixelFormat.R8G8B8A8_SNorm:
272  case PixelFormat.R8G8B8A8_SInt:
273  return true;
274  default:
275  return false;
276  }
277  }
278 
279 
280  /// <summary>
281  /// Determines whether the specified format is in BGRA order.
282  /// </summary>
283  /// <param name="format">The format.</param>
284  /// <returns>
285  /// <c>true</c> if the specified format is in BGRA order; otherwise, <c>false</c>.
286  /// </returns>
287  public static bool IsInBGRAOrder(PixelFormat format)
288  {
289  switch (format)
290  {
291  case PixelFormat.B8G8R8A8_UNorm:
292  case PixelFormat.B8G8R8X8_UNorm:
293  case PixelFormat.B8G8R8A8_Typeless:
294  case PixelFormat.B8G8R8A8_UNorm_SRgb:
295  case PixelFormat.B8G8R8X8_Typeless:
296  case PixelFormat.B8G8R8X8_UNorm_SRgb:
297  return true;
298  default:
299  return false;
300  }
301  }
302 
303  /// <summary>
304  /// Determines whether the specified format is compressed.
305  /// </summary>
306  /// <param name="fmt">The format.</param>
307  /// <returns>
308  /// <c>true</c> if the specified format is compressed; otherwise, <c>false</c>.
309  /// </returns>
310  public static bool IsCompressed(PixelFormat fmt)
311  {
312  switch (fmt)
313  {
314  case PixelFormat.BC1_Typeless:
315  case PixelFormat.BC1_UNorm:
316  case PixelFormat.BC1_UNorm_SRgb:
317  case PixelFormat.BC2_Typeless:
318  case PixelFormat.BC2_UNorm:
319  case PixelFormat.BC2_UNorm_SRgb:
320  case PixelFormat.BC3_Typeless:
321  case PixelFormat.BC3_UNorm:
322  case PixelFormat.BC3_UNorm_SRgb:
323  case PixelFormat.BC4_Typeless:
324  case PixelFormat.BC4_UNorm:
325  case PixelFormat.BC4_SNorm:
326  case PixelFormat.BC5_Typeless:
327  case PixelFormat.BC5_UNorm:
328  case PixelFormat.BC5_SNorm:
329  case PixelFormat.BC6H_Typeless:
330  case PixelFormat.BC6H_Uf16:
331  case PixelFormat.BC6H_Sf16:
332  case PixelFormat.BC7_Typeless:
333  case PixelFormat.BC7_UNorm:
334  case PixelFormat.BC7_UNorm_SRgb:
335  case PixelFormat.PVRTC_2bpp_RGB:
336  case PixelFormat.PVRTC_2bpp_RGBA:
337  case PixelFormat.PVRTC_4bpp_RGB:
338  case PixelFormat.PVRTC_4bpp_RGBA:
339  case PixelFormat.PVRTC_II_2bpp:
340  case PixelFormat.PVRTC_II_4bpp:
341  case PixelFormat.ETC1:
342  case PixelFormat.ETC2_RGB:
343  case PixelFormat.ETC2_RGBA:
344  case PixelFormat.ETC2_RGB_A1:
345  case PixelFormat.EAC_R11_Unsigned:
346  case PixelFormat.EAC_R11_Signed:
347  case PixelFormat.EAC_RG11_Unsigned:
348  case PixelFormat.EAC_RG11_Signed:
349  case PixelFormat.ATC_RGB:
350  case PixelFormat.ATC_RGBA_Explicit:
351  case PixelFormat.ATC_RGBA_Interpolated:
352  return true;
353  default:
354  return false;
355  }
356  }
357  }
358 }
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)
_Use_decl_annotations_ bool IsCompressed(DXGI_FORMAT fmt)
Definition: DirectXTex.inl:31
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
Definition: DirectXTexP.h:175
PixelFormat
Defines various types of pixel formats.
Definition: PixelFormat.cs:32