1 //
2 // Copyright 2013 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 // formatutils9.cpp: Queries for GL image formats and their translations to D3D9
8 // formats.
9
10 #include "libANGLE/renderer/d3d/d3d9/formatutils9.h"
11
12 #include "image_util/copyimage.h"
13 #include "image_util/generatemip.h"
14 #include "image_util/loadimage.h"
15
16 #include "anglebase/no_destructor.h"
17 #include "libANGLE/renderer/d3d/d3d9/Renderer9.h"
18 #include "libANGLE/renderer/d3d/d3d9/vertexconversion.h"
19
20 using namespace angle;
21
22 namespace rx
23 {
24
25 namespace d3d9
26 {
27
28 constexpr D3DFORMAT D3DFMT_INTZ = ((D3DFORMAT)(MAKEFOURCC('I', 'N', 'T', 'Z')));
29 constexpr D3DFORMAT D3DFMT_NULL = ((D3DFORMAT)(MAKEFOURCC('N', 'U', 'L', 'L')));
30
31 // A map to determine the pixel size and mip generation function of a given D3D format
32 typedef std::map<D3DFORMAT, D3DFormat> D3D9FormatInfoMap;
33
34 typedef std::pair<GLint, InitializeTextureDataFunction> InternalFormatInitialzerPair;
35 typedef std::map<GLint, InitializeTextureDataFunction> InternalFormatInitialzerMap;
36
BuildInternalFormatInitialzerMap()37 static InternalFormatInitialzerMap BuildInternalFormatInitialzerMap()
38 {
39 using namespace angle; // For image initialization functions
40
41 InternalFormatInitialzerMap map;
42
43 map.insert(InternalFormatInitialzerPair(
44 GL_RGB16F, Initialize4ComponentData<GLhalf, 0x0000, 0x0000, 0x0000, gl::Float16One>));
45 map.insert(InternalFormatInitialzerPair(
46 GL_RGB32F,
47 Initialize4ComponentData<GLfloat, 0x00000000, 0x00000000, 0x00000000, gl::Float32One>));
48
49 return map;
50 }
51
UnreachableLoad(const ImageLoadContext & context,size_t width,size_t height,size_t depth,const uint8_t * input,size_t inputRowPitch,size_t inputDepthPitch,uint8_t * output,size_t outputRowPitch,size_t outputDepthPitch)52 static void UnreachableLoad(const ImageLoadContext &context,
53 size_t width,
54 size_t height,
55 size_t depth,
56 const uint8_t *input,
57 size_t inputRowPitch,
58 size_t inputDepthPitch,
59 uint8_t *output,
60 size_t outputRowPitch,
61 size_t outputDepthPitch)
62 {
63 UNREACHABLE();
64 }
65
66 typedef std::pair<GLenum, TextureFormat> D3D9FormatPair;
67 typedef std::map<GLenum, TextureFormat> D3D9FormatMap;
68
TextureFormat()69 TextureFormat::TextureFormat()
70 : texFormat(D3DFMT_UNKNOWN),
71 renderFormat(D3DFMT_UNKNOWN),
72 dataInitializerFunction(nullptr),
73 loadFunction(UnreachableLoad)
74 {}
75
InsertD3D9FormatInfo(D3D9FormatMap * map,GLenum internalFormat,D3DFORMAT texFormat,D3DFORMAT renderFormat,LoadImageFunction loadFunction)76 static inline void InsertD3D9FormatInfo(D3D9FormatMap *map,
77 GLenum internalFormat,
78 D3DFORMAT texFormat,
79 D3DFORMAT renderFormat,
80 LoadImageFunction loadFunction)
81 {
82 TextureFormat info;
83 info.texFormat = texFormat;
84 info.renderFormat = renderFormat;
85
86 static const angle::base::NoDestructor<InternalFormatInitialzerMap> dataInitializationMap(
87 BuildInternalFormatInitialzerMap());
88 InternalFormatInitialzerMap::const_iterator dataInitIter =
89 dataInitializationMap->find(internalFormat);
90 info.dataInitializerFunction =
91 (dataInitIter != dataInitializationMap->end()) ? dataInitIter->second : nullptr;
92
93 info.loadFunction = loadFunction;
94
95 map->insert(std::make_pair(internalFormat, info));
96 }
97
BuildD3D9FormatMap()98 static D3D9FormatMap BuildD3D9FormatMap()
99 {
100 using namespace angle; // For image loading functions
101
102 D3D9FormatMap map;
103
104 // clang-format off
105 // | Internal format | Texture format | Render format | Load function |
106 InsertD3D9FormatInfo(&map, GL_NONE, D3DFMT_NULL, D3DFMT_NULL, UnreachableLoad );
107
108 // We choose to downsample the GL_DEPTH_COMPONENT32_OES format to a 24-bit format because D3DFMT_D32 is not widely
109 // supported. We're allowed to do this because:
110 // - The ES spec 2.0.25 sec 3.7.1 states that we're allowed to store texture formats with internal format
111 // resolutions of our own choosing.
112 // - OES_depth_texture states that downsampling of the depth formats is allowed.
113 // - ANGLE_depth_texture does not state minimum required resolutions of the depth texture formats it
114 // introduces.
115 // In ES3 however, there are minimum resolutions for the texture formats and this would not be allowed.
116
117 InsertD3D9FormatInfo(&map, GL_DEPTH_COMPONENT16, D3DFMT_INTZ, D3DFMT_D24S8, UnreachableLoad );
118 InsertD3D9FormatInfo(&map, GL_DEPTH_COMPONENT32_OES, D3DFMT_INTZ, D3DFMT_D24X8, UnreachableLoad );
119 InsertD3D9FormatInfo(&map, GL_DEPTH24_STENCIL8_OES, D3DFMT_INTZ, D3DFMT_D24S8, UnreachableLoad );
120 InsertD3D9FormatInfo(&map, GL_STENCIL_INDEX8, D3DFMT_UNKNOWN, D3DFMT_D24S8, UnreachableLoad ); // TODO: What's the texture format?
121
122 InsertD3D9FormatInfo(&map, GL_RGBA32F_EXT, D3DFMT_A32B32G32R32F, D3DFMT_A32B32G32R32F, LoadToNative<GLfloat, 4> );
123 InsertD3D9FormatInfo(&map, GL_RGB32F_EXT, D3DFMT_A32B32G32R32F, D3DFMT_A32B32G32R32F, LoadToNative3To4<GLfloat, gl::Float32One>);
124 InsertD3D9FormatInfo(&map, GL_RG32F_EXT, D3DFMT_G32R32F, D3DFMT_G32R32F, LoadToNative<GLfloat, 2> );
125 InsertD3D9FormatInfo(&map, GL_R32F_EXT, D3DFMT_R32F, D3DFMT_R32F, LoadToNative<GLfloat, 1> );
126 InsertD3D9FormatInfo(&map, GL_ALPHA32F_EXT, D3DFMT_A32B32G32R32F, D3DFMT_UNKNOWN, LoadA32FToRGBA32F );
127 InsertD3D9FormatInfo(&map, GL_LUMINANCE32F_EXT, D3DFMT_A32B32G32R32F, D3DFMT_UNKNOWN, LoadL32FToRGBA32F );
128 InsertD3D9FormatInfo(&map, GL_LUMINANCE_ALPHA32F_EXT, D3DFMT_A32B32G32R32F, D3DFMT_UNKNOWN, LoadLA32FToRGBA32F );
129
130 InsertD3D9FormatInfo(&map, GL_RGBA16F_EXT, D3DFMT_A16B16G16R16F, D3DFMT_A16B16G16R16F, LoadToNative<GLhalf, 4> );
131 InsertD3D9FormatInfo(&map, GL_RGB16F_EXT, D3DFMT_A16B16G16R16F, D3DFMT_A16B16G16R16F, LoadToNative3To4<GLhalf, gl::Float16One> );
132 InsertD3D9FormatInfo(&map, GL_RG16F_EXT, D3DFMT_G16R16F, D3DFMT_G16R16F, LoadToNative<GLhalf, 2> );
133 InsertD3D9FormatInfo(&map, GL_R16F_EXT, D3DFMT_R16F, D3DFMT_R16F, LoadToNative<GLhalf, 1> );
134 InsertD3D9FormatInfo(&map, GL_ALPHA16F_EXT, D3DFMT_A16B16G16R16F, D3DFMT_UNKNOWN, LoadA16FToRGBA16F );
135 InsertD3D9FormatInfo(&map, GL_LUMINANCE16F_EXT, D3DFMT_A16B16G16R16F, D3DFMT_UNKNOWN, LoadL16FToRGBA16F );
136 InsertD3D9FormatInfo(&map, GL_LUMINANCE_ALPHA16F_EXT, D3DFMT_A16B16G16R16F, D3DFMT_UNKNOWN, LoadLA16FToRGBA16F );
137
138 InsertD3D9FormatInfo(&map, GL_ALPHA8_EXT, D3DFMT_A8R8G8B8, D3DFMT_A8R8G8B8, LoadA8ToBGRA8 );
139
140 InsertD3D9FormatInfo(&map, GL_RGB8_OES, D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, LoadRGB8ToBGRX8 );
141 InsertD3D9FormatInfo(&map, GL_RGB565, D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, LoadR5G6B5ToBGRA8 );
142 InsertD3D9FormatInfo(&map, GL_RGBA8_OES, D3DFMT_A8R8G8B8, D3DFMT_A8R8G8B8, LoadRGBA8ToBGRA8 );
143 InsertD3D9FormatInfo(&map, GL_RGBA4, D3DFMT_A8R8G8B8, D3DFMT_A8R8G8B8, LoadRGBA4ToBGRA8 );
144 InsertD3D9FormatInfo(&map, GL_RGB5_A1, D3DFMT_A8R8G8B8, D3DFMT_A8R8G8B8, LoadRGB5A1ToBGRA8 );
145 InsertD3D9FormatInfo(&map, GL_R8_EXT, D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, LoadR8ToBGRX8 );
146 InsertD3D9FormatInfo(&map, GL_RG8_EXT, D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, LoadRG8ToBGRX8 );
147
148 InsertD3D9FormatInfo(&map, GL_SRGB8, D3DFMT_X8R8G8B8, D3DFMT_UNKNOWN, LoadRGB8ToBGRX8 );
149 InsertD3D9FormatInfo(&map, GL_SRGB8_ALPHA8_EXT, D3DFMT_A8R8G8B8, D3DFMT_A8R8G8B8, LoadRGBA8ToBGRA8 );
150
151 InsertD3D9FormatInfo(&map, GL_BGRA_EXT, D3DFMT_A8R8G8B8, D3DFMT_A8R8G8B8, LoadToNative<GLubyte, 4> );
152 InsertD3D9FormatInfo(&map, GL_BGRA8_EXT, D3DFMT_A8R8G8B8, D3DFMT_A8R8G8B8, LoadToNative<GLubyte, 4> );
153 InsertD3D9FormatInfo(&map, GL_BGRA4_ANGLEX, D3DFMT_A8R8G8B8, D3DFMT_A8R8G8B8, LoadBGRA4ToBGRA8 );
154 InsertD3D9FormatInfo(&map, GL_BGR5_A1_ANGLEX, D3DFMT_A8R8G8B8, D3DFMT_A8R8G8B8, LoadBGR5A1ToBGRA8 );
155
156 InsertD3D9FormatInfo(&map, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, D3DFMT_DXT1, D3DFMT_UNKNOWN, LoadCompressedToNative<4, 4, 1, 8> );
157 InsertD3D9FormatInfo(&map, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, D3DFMT_DXT1, D3DFMT_UNKNOWN, LoadCompressedToNative<4, 4, 1, 8> );
158 InsertD3D9FormatInfo(&map, GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE, D3DFMT_DXT3, D3DFMT_UNKNOWN, LoadCompressedToNative<4, 4, 1, 16> );
159 InsertD3D9FormatInfo(&map, GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE, D3DFMT_DXT5, D3DFMT_UNKNOWN, LoadCompressedToNative<4, 4, 1, 16> );
160
161 InsertD3D9FormatInfo(&map, GL_COMPRESSED_SRGB_S3TC_DXT1_EXT, D3DFMT_DXT1, D3DFMT_UNKNOWN, LoadCompressedToNative<4, 4, 1, 8> );
162 InsertD3D9FormatInfo(&map, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, D3DFMT_DXT1, D3DFMT_UNKNOWN, LoadCompressedToNative<4, 4, 1, 8> );
163 InsertD3D9FormatInfo(&map, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, D3DFMT_DXT3, D3DFMT_UNKNOWN, LoadCompressedToNative<4, 4, 1, 16> );
164 InsertD3D9FormatInfo(&map, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, D3DFMT_DXT5, D3DFMT_UNKNOWN, LoadCompressedToNative<4, 4, 1, 16> );
165
166 // These formats require checking if the renderer supports D3DFMT_L8 or D3DFMT_A8L8 and
167 // then changing the format and loading function appropriately.
168 InsertD3D9FormatInfo(&map, GL_LUMINANCE8_EXT, D3DFMT_L8, D3DFMT_UNKNOWN, LoadToNative<GLubyte, 1> );
169 InsertD3D9FormatInfo(&map, GL_LUMINANCE8_ALPHA8_EXT, D3DFMT_A8L8, D3DFMT_UNKNOWN, LoadToNative<GLubyte, 2> );
170 InsertD3D9FormatInfo(&map, GL_LUMINANCE4_ALPHA4_OES, D3DFMT_A4L4, D3DFMT_UNKNOWN, LoadToNative<GLubyte, 1> );
171 // clang-format on
172
173 return map;
174 }
175
GetTextureFormatInfo(GLenum internalFormat)176 const TextureFormat &GetTextureFormatInfo(GLenum internalFormat)
177 {
178 static const angle::base::NoDestructor<D3D9FormatMap> formatMap(BuildD3D9FormatMap());
179 D3D9FormatMap::const_iterator iter = formatMap->find(internalFormat);
180 if (iter != formatMap->end())
181 {
182 return iter->second;
183 }
184 else
185 {
186 static const TextureFormat defaultInfo;
187 return defaultInfo;
188 }
189 }
190
GetDeclTypeComponentType(D3DDECLTYPE declType)191 static GLenum GetDeclTypeComponentType(D3DDECLTYPE declType)
192 {
193 switch (declType)
194 {
195 case D3DDECLTYPE_FLOAT1:
196 return GL_FLOAT;
197 case D3DDECLTYPE_FLOAT2:
198 return GL_FLOAT;
199 case D3DDECLTYPE_FLOAT3:
200 return GL_FLOAT;
201 case D3DDECLTYPE_FLOAT4:
202 return GL_FLOAT;
203 case D3DDECLTYPE_UBYTE4:
204 return GL_UNSIGNED_INT;
205 case D3DDECLTYPE_SHORT2:
206 return GL_INT;
207 case D3DDECLTYPE_SHORT4:
208 return GL_INT;
209 case D3DDECLTYPE_UBYTE4N:
210 return GL_UNSIGNED_NORMALIZED;
211 case D3DDECLTYPE_SHORT4N:
212 return GL_SIGNED_NORMALIZED;
213 case D3DDECLTYPE_USHORT4N:
214 return GL_UNSIGNED_NORMALIZED;
215 case D3DDECLTYPE_SHORT2N:
216 return GL_SIGNED_NORMALIZED;
217 case D3DDECLTYPE_USHORT2N:
218 return GL_UNSIGNED_NORMALIZED;
219 default:
220 UNREACHABLE();
221 return GL_NONE;
222 }
223 }
224
225 // Attribute format conversion
226 enum
227 {
228 NUM_GL_VERTEX_ATTRIB_TYPES = 6
229 };
230
231 struct TranslationDescription
232 {
233 DWORD capsFlag;
234 VertexFormat preferredConversion;
235 VertexFormat fallbackConversion;
236 };
237
238 // Mapping from OpenGL-ES vertex attrib type to D3D decl type:
239 //
240 // BYTE SHORT (Cast)
241 // BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm)
242 // UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast)
243 // UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize)
244 // SHORT SHORT (Identity)
245 // SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize)
246 // UNSIGNED_SHORT FLOAT (Cast)
247 // UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize)
248 // FIXED (not in WebGL) FLOAT (FixedToFloat)
249 // FLOAT FLOAT (Identity)
250
251 // GLToCType maps from GL type (as GLenum) to the C typedef.
252 template <GLenum GLType>
253 struct GLToCType
254 {};
255
256 template <>
257 struct GLToCType<GL_BYTE>
258 {
259 typedef GLbyte type;
260 };
261 template <>
262 struct GLToCType<GL_UNSIGNED_BYTE>
263 {
264 typedef GLubyte type;
265 };
266 template <>
267 struct GLToCType<GL_SHORT>
268 {
269 typedef GLshort type;
270 };
271 template <>
272 struct GLToCType<GL_UNSIGNED_SHORT>
273 {
274 typedef GLushort type;
275 };
276 template <>
277 struct GLToCType<GL_FIXED>
278 {
279 typedef GLuint type;
280 };
281 template <>
282 struct GLToCType<GL_FLOAT>
283 {
284 typedef GLfloat type;
285 };
286
287 // This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.)
288 enum D3DVertexType
289 {
290 D3DVT_FLOAT,
291 D3DVT_SHORT,
292 D3DVT_SHORT_NORM,
293 D3DVT_UBYTE,
294 D3DVT_UBYTE_NORM,
295 D3DVT_USHORT_NORM
296 };
297
298 // D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type.
299 template <unsigned int D3DType>
300 struct D3DToCType
301 {};
302
303 template <>
304 struct D3DToCType<D3DVT_FLOAT>
305 {
306 typedef float type;
307 };
308 template <>
309 struct D3DToCType<D3DVT_SHORT>
310 {
311 typedef short type;
312 };
313 template <>
314 struct D3DToCType<D3DVT_SHORT_NORM>
315 {
316 typedef short type;
317 };
318 template <>
319 struct D3DToCType<D3DVT_UBYTE>
320 {
321 typedef unsigned char type;
322 };
323 template <>
324 struct D3DToCType<D3DVT_UBYTE_NORM>
325 {
326 typedef unsigned char type;
327 };
328 template <>
329 struct D3DToCType<D3DVT_USHORT_NORM>
330 {
331 typedef unsigned short type;
332 };
333
334 // Encode the type/size combinations that D3D permits. For each type/size it expands to a widener
335 // that will provide the appropriate final size.
336 template <unsigned int type, int size>
337 struct WidenRule
338 {};
339
340 template <int size>
341 struct WidenRule<D3DVT_FLOAT, size> : NoWiden<size>
342 {};
343 template <int size>
344 struct WidenRule<D3DVT_SHORT, size> : WidenToEven<size>
345 {};
346 template <int size>
347 struct WidenRule<D3DVT_SHORT_NORM, size> : WidenToEven<size>
348 {};
349 template <int size>
350 struct WidenRule<D3DVT_UBYTE, size> : WidenToFour<size>
351 {};
352 template <int size>
353 struct WidenRule<D3DVT_UBYTE_NORM, size> : WidenToFour<size>
354 {};
355 template <int size>
356 struct WidenRule<D3DVT_USHORT_NORM, size> : WidenToEven<size>
357 {};
358
359 // VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D
360 // vertex type & size combination.
361 template <unsigned int d3dtype, int size>
362 struct VertexTypeFlags
363 {};
364
365 template <unsigned int _capflag, unsigned int _declflag>
366 struct VertexTypeFlagsHelper
367 {
368 enum
369 {
370 capflag = _capflag
371 };
372 enum
373 {
374 declflag = _declflag
375 };
376 };
377
378 template <>
379 struct VertexTypeFlags<D3DVT_FLOAT, 1> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1>
380 {};
381 template <>
382 struct VertexTypeFlags<D3DVT_FLOAT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2>
383 {};
384 template <>
385 struct VertexTypeFlags<D3DVT_FLOAT, 3> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3>
386 {};
387 template <>
388 struct VertexTypeFlags<D3DVT_FLOAT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4>
389 {};
390 template <>
391 struct VertexTypeFlags<D3DVT_SHORT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2>
392 {};
393 template <>
394 struct VertexTypeFlags<D3DVT_SHORT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4>
395 {};
396 template <>
397 struct VertexTypeFlags<D3DVT_SHORT_NORM, 2>
398 : VertexTypeFlagsHelper<D3DDTCAPS_SHORT2N, D3DDECLTYPE_SHORT2N>
399 {};
400 template <>
401 struct VertexTypeFlags<D3DVT_SHORT_NORM, 4>
402 : VertexTypeFlagsHelper<D3DDTCAPS_SHORT4N, D3DDECLTYPE_SHORT4N>
403 {};
404 template <>
405 struct VertexTypeFlags<D3DVT_UBYTE, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4, D3DDECLTYPE_UBYTE4>
406 {};
407 template <>
408 struct VertexTypeFlags<D3DVT_UBYTE_NORM, 4>
409 : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4N, D3DDECLTYPE_UBYTE4N>
410 {};
411 template <>
412 struct VertexTypeFlags<D3DVT_USHORT_NORM, 2>
413 : VertexTypeFlagsHelper<D3DDTCAPS_USHORT2N, D3DDECLTYPE_USHORT2N>
414 {};
415 template <>
416 struct VertexTypeFlags<D3DVT_USHORT_NORM, 4>
417 : VertexTypeFlagsHelper<D3DDTCAPS_USHORT4N, D3DDECLTYPE_USHORT4N>
418 {};
419
420 // VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as
421 // D3DVertexType enums).
422 template <GLenum GLtype, bool normalized>
423 struct VertexTypeMapping
424 {};
425
426 template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred>
427 struct VertexTypeMappingBase
428 {
429 enum
430 {
431 preferred = Preferred
432 };
433 enum
434 {
435 fallback = Fallback
436 };
437 };
438
439 template <>
440 struct VertexTypeMapping<GL_BYTE, false> : VertexTypeMappingBase<D3DVT_SHORT>
441 {}; // Cast
442 template <>
443 struct VertexTypeMapping<GL_BYTE, true> : VertexTypeMappingBase<D3DVT_FLOAT>
444 {}; // Normalize
445 template <>
446 struct VertexTypeMapping<GL_UNSIGNED_BYTE, false> : VertexTypeMappingBase<D3DVT_UBYTE, D3DVT_FLOAT>
447 {}; // Identity, Cast
448 template <>
449 struct VertexTypeMapping<GL_UNSIGNED_BYTE, true>
450 : VertexTypeMappingBase<D3DVT_UBYTE_NORM, D3DVT_FLOAT>
451 {}; // Identity, Normalize
452 template <>
453 struct VertexTypeMapping<GL_SHORT, false> : VertexTypeMappingBase<D3DVT_SHORT>
454 {}; // Identity
455 template <>
456 struct VertexTypeMapping<GL_SHORT, true> : VertexTypeMappingBase<D3DVT_SHORT_NORM, D3DVT_FLOAT>
457 {}; // Cast, Normalize
458 template <>
459 struct VertexTypeMapping<GL_UNSIGNED_SHORT, false> : VertexTypeMappingBase<D3DVT_FLOAT>
460 {}; // Cast
461 template <>
462 struct VertexTypeMapping<GL_UNSIGNED_SHORT, true>
463 : VertexTypeMappingBase<D3DVT_USHORT_NORM, D3DVT_FLOAT>
464 {}; // Cast, Normalize
465 template <bool normalized>
466 struct VertexTypeMapping<GL_FIXED, normalized> : VertexTypeMappingBase<D3DVT_FLOAT>
467 {}; // FixedToFloat
468 template <bool normalized>
469 struct VertexTypeMapping<GL_FLOAT, normalized> : VertexTypeMappingBase<D3DVT_FLOAT>
470 {}; // Identity
471
472 // Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule
473 // (Cast, Normalize, Identity, FixedToFloat). The conversion rules themselves are defined in
474 // vertexconversion.h.
475
476 // Almost all cases are covered by Cast (including those that are actually Identity since Cast<T,T>
477 // knows it's an identity mapping).
478 template <GLenum fromType, bool normalized, unsigned int toType>
479 struct ConversionRule : Cast<typename GLToCType<fromType>::type, typename D3DToCType<toType>::type>
480 {};
481
482 // All conversions from normalized types to float use the Normalize operator.
483 template <GLenum fromType>
484 struct ConversionRule<fromType, true, D3DVT_FLOAT> : Normalize<typename GLToCType<fromType>::type>
485 {};
486
487 // Use a full specialization for this so that it preferentially matches ahead of the generic
488 // normalize-to-float rules.
489 template <>
490 struct ConversionRule<GL_FIXED, true, D3DVT_FLOAT> : FixedToFloat<GLint, 16>
491 {};
492 template <>
493 struct ConversionRule<GL_FIXED, false, D3DVT_FLOAT> : FixedToFloat<GLint, 16>
494 {};
495
496 // A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues
497 // (i.e. 0/1) whether it is normalized or not.
498 template <class T, bool normalized>
499 struct DefaultVertexValuesStage2
500 {};
501
502 template <class T>
503 struct DefaultVertexValuesStage2<T, true> : NormalizedDefaultValues<T>
504 {};
505 template <class T>
506 struct DefaultVertexValuesStage2<T, false> : SimpleDefaultValues<T>
507 {};
508
509 // Work out the default value rule for a D3D type (expressed as the C type) and
510 template <class T, bool normalized>
511 struct DefaultVertexValues : DefaultVertexValuesStage2<T, normalized>
512 {};
513 template <bool normalized>
514 struct DefaultVertexValues<float, normalized> : SimpleDefaultValues<float>
515 {};
516
517 // Policy rules for use with Converter, to choose whether to use the preferred or fallback
518 // conversion. The fallback conversion produces an output that all D3D9 devices must support.
519 template <class T>
520 struct UsePreferred
521 {
522 enum
523 {
524 type = T::preferred
525 };
526 };
527 template <class T>
528 struct UseFallback
529 {
530 enum
531 {
532 type = T::fallback
533 };
534 };
535
536 // Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback
537 // conversion, it provides all the members of the appropriate VertexDataConverter, the
538 // D3DCAPS9::DeclTypes flag in cap flag and the D3DDECLTYPE member needed for the vertex declaration
539 // in declflag.
540 template <GLenum fromType, bool normalized, int size, template <class T> class PreferenceRule>
541 struct Converter
542 : VertexDataConverter<
543 typename GLToCType<fromType>::type,
544 WidenRule<PreferenceRule<VertexTypeMapping<fromType, normalized>>::type, size>,
545 ConversionRule<fromType,
546 normalized,
547 PreferenceRule<VertexTypeMapping<fromType, normalized>>::type>,
548 DefaultVertexValues<typename D3DToCType<PreferenceRule<
549 VertexTypeMapping<fromType, normalized>>::type>::type,
550 normalized>>
551 {
552 private:
553 enum
554 {
555 d3dtype = PreferenceRule<VertexTypeMapping<fromType, normalized>>::type
556 };
557 enum
558 {
559 d3dsize = WidenRule<d3dtype, size>::finalWidth
560 };
561
562 public:
563 enum
564 {
565 capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag
566 };
567 enum
568 {
569 declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag
570 };
571 };
572
VertexFormat()573 VertexFormat::VertexFormat()
574 : conversionType(VERTEX_CONVERT_NONE),
575 outputElementSize(0),
576 copyFunction(nullptr),
577 nativeFormat(D3DDECLTYPE_UNUSED),
578 componentType(GL_NONE)
579 {}
580
581 // Initialize a TranslationInfo
CreateVertexFormatInfo(bool identity,size_t elementSize,VertexCopyFunction copyFunc,D3DDECLTYPE nativeFormat)582 VertexFormat CreateVertexFormatInfo(bool identity,
583 size_t elementSize,
584 VertexCopyFunction copyFunc,
585 D3DDECLTYPE nativeFormat)
586 {
587 VertexFormat formatInfo;
588 formatInfo.conversionType = identity ? VERTEX_CONVERT_NONE : VERTEX_CONVERT_CPU;
589 formatInfo.outputElementSize = elementSize;
590 formatInfo.copyFunction = copyFunc;
591 formatInfo.nativeFormat = nativeFormat;
592 formatInfo.componentType = GetDeclTypeComponentType(nativeFormat);
593 return formatInfo;
594 }
595
596 #define TRANSLATION(type, norm, size, preferred) \
597 CreateVertexFormatInfo( \
598 Converter<type, norm, size, preferred>::identity, \
599 Converter<type, norm, size, preferred>::finalSize, \
600 Converter<type, norm, size, preferred>::convertArray, \
601 static_cast<D3DDECLTYPE>(Converter<type, norm, size, preferred>::declflag))
602
603 #define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \
604 { \
605 Converter<type, norm, size, UsePreferred>::capflag, \
606 TRANSLATION(type, norm, size, UsePreferred), \
607 TRANSLATION(type, norm, size, UseFallback) \
608 }
609
610 #define TRANSLATIONS_FOR_TYPE(type) \
611 { \
612 {TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 1), \
613 TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 2), \
614 TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 3), \
615 TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 4)}, \
616 {TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 1), \
617 TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 2), \
618 TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 3), \
619 TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 4)}, \
620 }
621
622 #define TRANSLATIONS_FOR_TYPE_NO_NORM(type) \
623 { \
624 {TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 1), \
625 TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 2), \
626 TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 3), \
627 TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 4)}, \
628 {TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 1), \
629 TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 2), \
630 TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 3), \
631 TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 4)}, \
632 }
633
ComputeTypeIndex(GLenum type)634 static inline unsigned int ComputeTypeIndex(GLenum type)
635 {
636 switch (type)
637 {
638 case GL_BYTE:
639 return 0;
640 case GL_UNSIGNED_BYTE:
641 return 1;
642 case GL_SHORT:
643 return 2;
644 case GL_UNSIGNED_SHORT:
645 return 3;
646 case GL_FIXED:
647 return 4;
648 case GL_FLOAT:
649 return 5;
650
651 default:
652 UNREACHABLE();
653 return 5;
654 }
655 }
656
GetVertexFormatInfo(DWORD supportedDeclTypes,angle::FormatID vertexFormatID)657 const VertexFormat &GetVertexFormatInfo(DWORD supportedDeclTypes, angle::FormatID vertexFormatID)
658 {
659 static DWORD initializedDeclTypes = 0;
660 static VertexFormat formatConverters[NUM_GL_VERTEX_ATTRIB_TYPES][2][4];
661 if (initializedDeclTypes != supportedDeclTypes)
662 {
663 const TranslationDescription
664 translations[NUM_GL_VERTEX_ATTRIB_TYPES][2]
665 [4] = // [GL types as enumerated by typeIndex()][normalized][size-1]
666 {TRANSLATIONS_FOR_TYPE(GL_BYTE), TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE),
667 TRANSLATIONS_FOR_TYPE(GL_SHORT), TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT),
668 TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FIXED), TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FLOAT)};
669 for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++)
670 {
671 for (unsigned int j = 0; j < 2; j++)
672 {
673 for (unsigned int k = 0; k < 4; k++)
674 {
675 if (translations[i][j][k].capsFlag == 0 ||
676 (supportedDeclTypes & translations[i][j][k].capsFlag) != 0)
677 {
678 formatConverters[i][j][k] = translations[i][j][k].preferredConversion;
679 }
680 else
681 {
682 formatConverters[i][j][k] = translations[i][j][k].fallbackConversion;
683 }
684 }
685 }
686 }
687 initializedDeclTypes = supportedDeclTypes;
688 }
689
690 const gl::VertexFormat &vertexFormat = gl::GetVertexFormatFromID(vertexFormatID);
691
692 // Pure integer attributes only supported in ES3.0
693 ASSERT(!vertexFormat.pureInteger);
694 return formatConverters[ComputeTypeIndex(vertexFormat.type)][vertexFormat.normalized]
695 [vertexFormat.components - 1];
696 }
697 } // namespace d3d9
698 } // namespace rx
699