// // Copyright (c) 2013-2014 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // formatutils9.cpp: Queries for GL image formats and their translations to D3D9 // formats. #include "libANGLE/renderer/d3d/copyimage.h" #include "libANGLE/renderer/d3d/d3d9/formatutils9.h" #include "libANGLE/renderer/d3d/d3d9/Renderer9.h" #include "libANGLE/renderer/d3d/d3d9/vertexconversion.h" #include "libANGLE/renderer/d3d/generatemip.h" #include "libANGLE/renderer/d3d/loadimage.h" namespace rx { namespace d3d9 { const D3DFORMAT D3DFMT_INTZ = ((D3DFORMAT)(MAKEFOURCC('I', 'N', 'T', 'Z'))); const D3DFORMAT D3DFMT_NULL = ((D3DFORMAT)(MAKEFOURCC('N', 'U', 'L', 'L'))); struct D3D9FastCopyFormat { GLenum destFormat; GLenum destType; ColorCopyFunction copyFunction; D3D9FastCopyFormat(GLenum destFormat, GLenum destType, ColorCopyFunction copyFunction) : destFormat(destFormat), destType(destType), copyFunction(copyFunction) { } bool operator<(const D3D9FastCopyFormat& other) const { return memcmp(this, &other, sizeof(D3D9FastCopyFormat)) < 0; } }; typedef std::multimap D3D9FastCopyMap; static D3D9FastCopyMap BuildFastCopyMap() { D3D9FastCopyMap map; map.insert(std::make_pair(D3DFMT_A8R8G8B8, D3D9FastCopyFormat(GL_RGBA, GL_UNSIGNED_BYTE, CopyBGRA8ToRGBA8))); return map; } // A map to determine the pixel size and mip generation function of a given D3D format typedef std::map D3D9FormatInfoMap; D3DFormat::D3DFormat() : pixelBytes(0), blockWidth(0), blockHeight(0), redBits(0), greenBits(0), blueBits(0), alphaBits(0), luminanceBits(0), depthBits(0), stencilBits(0), internalFormat(GL_NONE), mipGenerationFunction(NULL), colorReadFunction(NULL), fastCopyFunctions() { } ColorCopyFunction D3DFormat::getFastCopyFunction(GLenum format, GLenum type) const { FastCopyFunctionMap::const_iterator iter = fastCopyFunctions.find(std::make_pair(format, type)); return (iter != fastCopyFunctions.end()) ? iter->second : NULL; } static inline void InsertD3DFormatInfo(D3D9FormatInfoMap *map, D3DFORMAT format, GLuint bits, GLuint blockWidth, GLuint blockHeight, GLuint redBits, GLuint greenBits, GLuint blueBits, GLuint alphaBits, GLuint lumBits, GLuint depthBits, GLuint stencilBits, GLenum internalFormat, MipGenerationFunction mipFunc, ColorReadFunction colorReadFunc) { D3DFormat info; info.pixelBytes = bits / 8; info.blockWidth = blockWidth; info.blockHeight = blockHeight; info.redBits = redBits; info.greenBits = greenBits; info.blueBits = blueBits; info.alphaBits = alphaBits; info.luminanceBits = lumBits; info.depthBits = depthBits; info.stencilBits = stencilBits; info.internalFormat = internalFormat; info.mipGenerationFunction = mipFunc; info.colorReadFunction = colorReadFunc; static const D3D9FastCopyMap fastCopyMap = BuildFastCopyMap(); std::pair fastCopyIter = fastCopyMap.equal_range(format); for (D3D9FastCopyMap::const_iterator i = fastCopyIter.first; i != fastCopyIter.second; i++) { info.fastCopyFunctions.insert(std::make_pair(std::make_pair(i->second.destFormat, i->second.destType), i->second.copyFunction)); } map->insert(std::make_pair(format, info)); } static D3D9FormatInfoMap BuildD3D9FormatInfoMap() { D3D9FormatInfoMap map; // | D3DFORMAT | S |W |H | R | G | B | A | L | D | S | Internal format | Mip generation function | Color read function | InsertD3DFormatInfo(&map, D3DFMT_NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, GL_NONE, NULL, NULL ); InsertD3DFormatInfo(&map, D3DFMT_UNKNOWN, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, GL_NONE, NULL, NULL ); InsertD3DFormatInfo(&map, D3DFMT_L8, 8, 1, 1, 0, 0, 0, 0, 8, 0, 0, GL_LUMINANCE8_EXT, GenerateMip, ReadColor ); InsertD3DFormatInfo(&map, D3DFMT_A8, 8, 1, 1, 0, 0, 0, 8, 0, 0, 0, GL_ALPHA8_EXT, GenerateMip, ReadColor ); InsertD3DFormatInfo(&map, D3DFMT_A8L8, 16, 1, 1, 0, 0, 0, 8, 8, 0, 0, GL_LUMINANCE8_ALPHA8_EXT, GenerateMip, ReadColor ); InsertD3DFormatInfo(&map, D3DFMT_A4R4G4B4, 16, 1, 1, 4, 4, 4, 4, 0, 0, 0, GL_BGRA4_ANGLEX, GenerateMip, ReadColor ); InsertD3DFormatInfo(&map, D3DFMT_A1R5G5B5, 16, 1, 1, 5, 5, 5, 1, 0, 0, 0, GL_BGR5_A1_ANGLEX, GenerateMip, ReadColor ); InsertD3DFormatInfo(&map, D3DFMT_R5G6B5, 16, 1, 1, 5, 6, 5, 0, 0, 0, 0, GL_RGB565, GenerateMip, ReadColor ); InsertD3DFormatInfo(&map, D3DFMT_X8R8G8B8, 32, 1, 1, 8, 8, 8, 0, 0, 0, 0, GL_BGRA8_EXT, GenerateMip, ReadColor ); InsertD3DFormatInfo(&map, D3DFMT_A8R8G8B8, 32, 1, 1, 8, 8, 8, 8, 0, 0, 0, GL_BGRA8_EXT, GenerateMip, ReadColor ); InsertD3DFormatInfo(&map, D3DFMT_R16F, 16, 1, 1, 16, 0, 0, 0, 0, 0, 0, GL_R16F_EXT, GenerateMip, ReadColor ); InsertD3DFormatInfo(&map, D3DFMT_G16R16F, 32, 1, 1, 16, 16, 0, 0, 0, 0, 0, GL_RG16F_EXT, GenerateMip, ReadColor ); InsertD3DFormatInfo(&map, D3DFMT_A16B16G16R16F, 64, 1, 1, 16, 16, 16, 16, 0, 0, 0, GL_RGBA16F_EXT, GenerateMip, ReadColor); InsertD3DFormatInfo(&map, D3DFMT_R32F, 32, 1, 1, 32, 0, 0, 0, 0, 0, 0, GL_R32F_EXT, GenerateMip, ReadColor ); InsertD3DFormatInfo(&map, D3DFMT_G32R32F, 64, 1, 1, 32, 32, 0, 0, 0, 0, 0, GL_RG32F_EXT, GenerateMip, ReadColor ); InsertD3DFormatInfo(&map, D3DFMT_A32B32G32R32F, 128, 1, 1, 32, 32, 32, 32, 0, 0, 0, GL_RGBA32F_EXT, GenerateMip, ReadColor); InsertD3DFormatInfo(&map, D3DFMT_D16, 16, 1, 1, 0, 0, 0, 0, 0, 16, 0, GL_DEPTH_COMPONENT16, NULL, NULL ); InsertD3DFormatInfo(&map, D3DFMT_D24S8, 32, 1, 1, 0, 0, 0, 0, 0, 24, 8, GL_DEPTH24_STENCIL8_OES, NULL, NULL ); InsertD3DFormatInfo(&map, D3DFMT_D24X8, 32, 1, 1, 0, 0, 0, 0, 0, 24, 0, GL_DEPTH_COMPONENT16, NULL, NULL ); InsertD3DFormatInfo(&map, D3DFMT_D32, 32, 1, 1, 0, 0, 0, 0, 0, 32, 0, GL_DEPTH_COMPONENT32_OES, NULL, NULL ); InsertD3DFormatInfo(&map, D3DFMT_INTZ, 32, 1, 1, 0, 0, 0, 0, 0, 24, 8, GL_DEPTH24_STENCIL8_OES, NULL, NULL ); InsertD3DFormatInfo(&map, D3DFMT_DXT1, 64, 4, 4, 0, 0, 0, 0, 0, 0, 0, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, NULL, NULL ); InsertD3DFormatInfo(&map, D3DFMT_DXT3, 128, 4, 4, 0, 0, 0, 0, 0, 0, 0, GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE, NULL, NULL ); InsertD3DFormatInfo(&map, D3DFMT_DXT5, 128, 4, 4, 0, 0, 0, 0, 0, 0, 0, GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE, NULL, NULL ); return map; } const D3DFormat &GetD3DFormatInfo(D3DFORMAT format) { static const D3D9FormatInfoMap infoMap = BuildD3D9FormatInfoMap(); D3D9FormatInfoMap::const_iterator iter = infoMap.find(format); if (iter != infoMap.end()) { return iter->second; } else { static const D3DFormat defaultInfo; return defaultInfo; } } typedef std::pair InternalFormatInitialzerPair; typedef std::map InternalFormatInitialzerMap; static InternalFormatInitialzerMap BuildInternalFormatInitialzerMap() { InternalFormatInitialzerMap map; map.insert(InternalFormatInitialzerPair(GL_RGB16F, Initialize4ComponentData)); map.insert(InternalFormatInitialzerPair(GL_RGB32F, Initialize4ComponentData)); return map; } // Each GL internal format corresponds to one D3D format and data loading function. // Due to not all formats being available all the time, some of the function/format types are wrapped // in templates that perform format support queries on a Renderer9 object which is supplied // when requesting the function or format. typedef bool(*FallbackPredicateFunction)(); template static void FallbackLoad(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) { if (pred()) { prefered(width, height, depth, input, inputRowPitch, inputDepthPitch, output, outputRowPitch, outputDepthPitch); } else { fallback(width, height, depth, input, inputRowPitch, inputDepthPitch, output, outputRowPitch, outputDepthPitch); } } static void UnreachableLoad(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) { UNREACHABLE(); } typedef std::pair D3D9FormatPair; typedef std::map D3D9FormatMap; TextureFormat::TextureFormat() : texFormat(D3DFMT_UNKNOWN), renderFormat(D3DFMT_UNKNOWN), dataInitializerFunction(NULL), loadFunction(UnreachableLoad) { } static inline void InsertD3D9FormatInfo(D3D9FormatMap *map, GLenum internalFormat, D3DFORMAT texFormat, D3DFORMAT renderFormat, LoadImageFunction loadFunction) { TextureFormat info; info.texFormat = texFormat; info.renderFormat = renderFormat; static const InternalFormatInitialzerMap dataInitializationMap = BuildInternalFormatInitialzerMap(); InternalFormatInitialzerMap::const_iterator dataInitIter = dataInitializationMap.find(internalFormat); info.dataInitializerFunction = (dataInitIter != dataInitializationMap.end()) ? dataInitIter->second : NULL; info.loadFunction = loadFunction; map->insert(std::make_pair(internalFormat, info)); } static D3D9FormatMap BuildD3D9FormatMap() { D3D9FormatMap map; // | Internal format | Texture format | Render format | Load function | InsertD3D9FormatInfo(&map, GL_NONE, D3DFMT_NULL, D3DFMT_NULL, UnreachableLoad ); // We choose to downsample the GL_DEPTH_COMPONENT32_OES format to a 24-bit format because D3DFMT_D32 is not widely // supported. We're allowed to do this because: // - The ES spec 2.0.25 sec 3.7.1 states that we're allowed to store texture formats with internal format // resolutions of our own choosing. // - OES_depth_texture states that downsampling of the depth formats is allowed. // - ANGLE_depth_texture does not state minimum required resolutions of the depth texture formats it // introduces. // In ES3 however, there are minimum resolutions for the texture formats and this would not be allowed. InsertD3D9FormatInfo(&map, GL_DEPTH_COMPONENT16, D3DFMT_INTZ, D3DFMT_D24S8, UnreachableLoad ); InsertD3D9FormatInfo(&map, GL_DEPTH_COMPONENT32_OES, D3DFMT_INTZ, D3DFMT_D24X8, UnreachableLoad ); InsertD3D9FormatInfo(&map, GL_DEPTH24_STENCIL8_OES, D3DFMT_INTZ, D3DFMT_D24S8, UnreachableLoad ); InsertD3D9FormatInfo(&map, GL_STENCIL_INDEX8, D3DFMT_UNKNOWN, D3DFMT_D24S8, UnreachableLoad ); // TODO: What's the texture format? InsertD3D9FormatInfo(&map, GL_RGBA32F_EXT, D3DFMT_A32B32G32R32F, D3DFMT_A32B32G32R32F, LoadToNative ); InsertD3D9FormatInfo(&map, GL_RGB32F_EXT, D3DFMT_A32B32G32R32F, D3DFMT_A32B32G32R32F, LoadToNative3To4); InsertD3D9FormatInfo(&map, GL_RG32F_EXT, D3DFMT_G32R32F, D3DFMT_G32R32F, LoadToNative ); InsertD3D9FormatInfo(&map, GL_R32F_EXT, D3DFMT_R32F, D3DFMT_R32F, LoadToNative ); InsertD3D9FormatInfo(&map, GL_ALPHA32F_EXT, D3DFMT_A32B32G32R32F, D3DFMT_UNKNOWN, LoadA32FToRGBA32F ); InsertD3D9FormatInfo(&map, GL_LUMINANCE32F_EXT, D3DFMT_A32B32G32R32F, D3DFMT_UNKNOWN, LoadL32FToRGBA32F ); InsertD3D9FormatInfo(&map, GL_LUMINANCE_ALPHA32F_EXT, D3DFMT_A32B32G32R32F, D3DFMT_UNKNOWN, LoadLA32FToRGBA32F ); InsertD3D9FormatInfo(&map, GL_RGBA16F_EXT, D3DFMT_A16B16G16R16F, D3DFMT_A16B16G16R16F, LoadToNative ); InsertD3D9FormatInfo(&map, GL_RGB16F_EXT, D3DFMT_A16B16G16R16F, D3DFMT_A16B16G16R16F, LoadToNative3To4 ); InsertD3D9FormatInfo(&map, GL_RG16F_EXT, D3DFMT_G16R16F, D3DFMT_G16R16F, LoadToNative ); InsertD3D9FormatInfo(&map, GL_R16F_EXT, D3DFMT_R16F, D3DFMT_R16F, LoadToNative ); InsertD3D9FormatInfo(&map, GL_ALPHA16F_EXT, D3DFMT_A16B16G16R16F, D3DFMT_UNKNOWN, LoadA16FToRGBA16F ); InsertD3D9FormatInfo(&map, GL_LUMINANCE16F_EXT, D3DFMT_A16B16G16R16F, D3DFMT_UNKNOWN, LoadL16FToRGBA16F ); InsertD3D9FormatInfo(&map, GL_LUMINANCE_ALPHA16F_EXT, D3DFMT_A16B16G16R16F, D3DFMT_UNKNOWN, LoadLA16FToRGBA16F ); InsertD3D9FormatInfo(&map, GL_ALPHA8_EXT, D3DFMT_A8R8G8B8, D3DFMT_A8R8G8B8, FallbackLoad); InsertD3D9FormatInfo(&map, GL_RGB8_OES, D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, LoadRGB8ToBGRX8 ); InsertD3D9FormatInfo(&map, GL_RGB565, D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, LoadR5G6B5ToBGRA8 ); InsertD3D9FormatInfo(&map, GL_RGBA8_OES, D3DFMT_A8R8G8B8, D3DFMT_A8R8G8B8, FallbackLoad); InsertD3D9FormatInfo(&map, GL_RGBA4, D3DFMT_A8R8G8B8, D3DFMT_A8R8G8B8, LoadRGBA4ToBGRA8 ); InsertD3D9FormatInfo(&map, GL_RGB5_A1, D3DFMT_A8R8G8B8, D3DFMT_A8R8G8B8, LoadRGB5A1ToBGRA8 ); InsertD3D9FormatInfo(&map, GL_R8_EXT, D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, LoadR8ToBGRX8 ); InsertD3D9FormatInfo(&map, GL_RG8_EXT, D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, LoadRG8ToBGRX8 ); InsertD3D9FormatInfo(&map, GL_BGRA8_EXT, D3DFMT_A8R8G8B8, D3DFMT_A8R8G8B8, LoadToNative ); InsertD3D9FormatInfo(&map, GL_BGRA4_ANGLEX, D3DFMT_A8R8G8B8, D3DFMT_A8R8G8B8, LoadBGRA4ToBGRA8 ); InsertD3D9FormatInfo(&map, GL_BGR5_A1_ANGLEX, D3DFMT_A8R8G8B8, D3DFMT_A8R8G8B8, LoadBGR5A1ToBGRA8 ); InsertD3D9FormatInfo(&map, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, D3DFMT_DXT1, D3DFMT_UNKNOWN, LoadCompressedToNative<4, 4, 8> ); InsertD3D9FormatInfo(&map, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, D3DFMT_DXT1, D3DFMT_UNKNOWN, LoadCompressedToNative<4, 4, 8> ); InsertD3D9FormatInfo(&map, GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE, D3DFMT_DXT3, D3DFMT_UNKNOWN, LoadCompressedToNative<4, 4, 16> ); InsertD3D9FormatInfo(&map, GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE, D3DFMT_DXT5, D3DFMT_UNKNOWN, LoadCompressedToNative<4, 4, 16> ); // These formats require checking if the renderer supports D3DFMT_L8 or D3DFMT_A8L8 and // then changing the format and loading function appropriately. InsertD3D9FormatInfo(&map, GL_LUMINANCE8_EXT, D3DFMT_L8, D3DFMT_UNKNOWN, LoadToNative ); InsertD3D9FormatInfo(&map, GL_LUMINANCE8_ALPHA8_EXT, D3DFMT_A8L8, D3DFMT_UNKNOWN, LoadToNative ); return map; } const TextureFormat &GetTextureFormatInfo(GLenum internalFormat) { static const D3D9FormatMap formatMap = BuildD3D9FormatMap(); D3D9FormatMap::const_iterator iter = formatMap.find(internalFormat); if (iter != formatMap.end()) { return iter->second; } else { static const TextureFormat defaultInfo; return defaultInfo; } } static GLenum GetDeclTypeComponentType(D3DDECLTYPE declType) { switch (declType) { case D3DDECLTYPE_FLOAT1: return GL_FLOAT; case D3DDECLTYPE_FLOAT2: return GL_FLOAT; case D3DDECLTYPE_FLOAT3: return GL_FLOAT; case D3DDECLTYPE_FLOAT4: return GL_FLOAT; case D3DDECLTYPE_UBYTE4: return GL_UNSIGNED_INT; case D3DDECLTYPE_SHORT2: return GL_INT; case D3DDECLTYPE_SHORT4: return GL_INT; case D3DDECLTYPE_UBYTE4N: return GL_UNSIGNED_NORMALIZED; case D3DDECLTYPE_SHORT4N: return GL_SIGNED_NORMALIZED; case D3DDECLTYPE_USHORT4N: return GL_UNSIGNED_NORMALIZED; case D3DDECLTYPE_SHORT2N: return GL_SIGNED_NORMALIZED; case D3DDECLTYPE_USHORT2N: return GL_UNSIGNED_NORMALIZED; default: UNREACHABLE(); return GL_NONE; } } // Attribute format conversion enum { NUM_GL_VERTEX_ATTRIB_TYPES = 6 }; struct TranslationDescription { DWORD capsFlag; VertexFormat preferredConversion; VertexFormat fallbackConversion; }; // Mapping from OpenGL-ES vertex attrib type to D3D decl type: // // BYTE SHORT (Cast) // BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm) // UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast) // UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize) // SHORT SHORT (Identity) // SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize) // UNSIGNED_SHORT FLOAT (Cast) // UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize) // FIXED (not in WebGL) FLOAT (FixedToFloat) // FLOAT FLOAT (Identity) // GLToCType maps from GL type (as GLenum) to the C typedef. template struct GLToCType { }; template <> struct GLToCType { typedef GLbyte type; }; template <> struct GLToCType { typedef GLubyte type; }; template <> struct GLToCType { typedef GLshort type; }; template <> struct GLToCType { typedef GLushort type; }; template <> struct GLToCType { typedef GLuint type; }; template <> struct GLToCType { typedef GLfloat type; }; // This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.) enum D3DVertexType { D3DVT_FLOAT, D3DVT_SHORT, D3DVT_SHORT_NORM, D3DVT_UBYTE, D3DVT_UBYTE_NORM, D3DVT_USHORT_NORM }; // D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type. template struct D3DToCType { }; template <> struct D3DToCType { typedef float type; }; template <> struct D3DToCType { typedef short type; }; template <> struct D3DToCType { typedef short type; }; template <> struct D3DToCType { typedef unsigned char type; }; template <> struct D3DToCType { typedef unsigned char type; }; template <> struct D3DToCType { typedef unsigned short type; }; // Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size. template struct WidenRule { }; template struct WidenRule : NoWiden { }; template struct WidenRule : WidenToEven { }; template struct WidenRule : WidenToEven { }; template struct WidenRule : WidenToFour { }; template struct WidenRule : WidenToFour { }; template struct WidenRule : WidenToEven { }; // VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination. template struct VertexTypeFlags { }; template struct VertexTypeFlagsHelper { enum { capflag = _capflag }; enum { declflag = _declflag }; }; template <> struct VertexTypeFlags : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { }; template <> struct VertexTypeFlags : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { }; template <> struct VertexTypeFlags : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { }; template <> struct VertexTypeFlags : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { }; template <> struct VertexTypeFlags : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { }; template <> struct VertexTypeFlags : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { }; template <> struct VertexTypeFlags : VertexTypeFlagsHelper { }; template <> struct VertexTypeFlags : VertexTypeFlagsHelper { }; template <> struct VertexTypeFlags : VertexTypeFlagsHelper { }; template <> struct VertexTypeFlags : VertexTypeFlagsHelper { }; template <> struct VertexTypeFlags : VertexTypeFlagsHelper { }; template <> struct VertexTypeFlags : VertexTypeFlagsHelper { }; // VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums). template struct VertexTypeMapping { }; template struct VertexTypeMappingBase { enum { preferred = Preferred }; enum { fallback = Fallback }; }; template <> struct VertexTypeMapping : VertexTypeMappingBase { }; // Cast template <> struct VertexTypeMapping : VertexTypeMappingBase { }; // Normalize template <> struct VertexTypeMapping : VertexTypeMappingBase { }; // Identity, Cast template <> struct VertexTypeMapping : VertexTypeMappingBase { }; // Identity, Normalize template <> struct VertexTypeMapping : VertexTypeMappingBase { }; // Identity template <> struct VertexTypeMapping : VertexTypeMappingBase { }; // Cast, Normalize template <> struct VertexTypeMapping : VertexTypeMappingBase { }; // Cast template <> struct VertexTypeMapping : VertexTypeMappingBase { }; // Cast, Normalize template struct VertexTypeMapping : VertexTypeMappingBase { }; // FixedToFloat template struct VertexTypeMapping : VertexTypeMappingBase { }; // Identity // Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat). // The conversion rules themselves are defined in vertexconversion.h. // Almost all cases are covered by Cast (including those that are actually Identity since Cast knows it's an identity mapping). template struct ConversionRule : Cast::type, typename D3DToCType::type> { }; // All conversions from normalized types to float use the Normalize operator. template struct ConversionRule : Normalize::type> { }; // Use a full specialization for this so that it preferentially matches ahead of the generic normalize-to-float rules. template <> struct ConversionRule : FixedToFloat { }; template <> struct ConversionRule : FixedToFloat { }; // A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1) // whether it is normalized or not. template struct DefaultVertexValuesStage2 { }; template struct DefaultVertexValuesStage2 : NormalizedDefaultValues { }; template struct DefaultVertexValuesStage2 : SimpleDefaultValues { }; // Work out the default value rule for a D3D type (expressed as the C type) and template struct DefaultVertexValues : DefaultVertexValuesStage2 { }; template struct DefaultVertexValues : SimpleDefaultValues { }; // Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion. // The fallback conversion produces an output that all D3D9 devices must support. template struct UsePreferred { enum { type = T::preferred }; }; template struct UseFallback { enum { type = T::fallback }; }; // Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion, // it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag // and the D3DDECLTYPE member needed for the vertex declaration in declflag. template class PreferenceRule> struct Converter : VertexDataConverter< typename GLToCType::type, WidenRule>::type, size>, ConversionRule>::type>, DefaultVertexValues>::type>::type, normalized>> { private: enum { d3dtype = PreferenceRule>::type }; enum { d3dsize = WidenRule::finalWidth }; public: enum { capflag = VertexTypeFlags::capflag }; enum { declflag = VertexTypeFlags::declflag }; }; VertexFormat::VertexFormat() : conversionType(VERTEX_CONVERT_NONE), outputElementSize(0), copyFunction(NULL), nativeFormat(D3DDECLTYPE_UNUSED), componentType(GL_NONE) { } // Initialize a TranslationInfo VertexFormat CreateVertexFormatInfo(bool identity, size_t elementSize, VertexCopyFunction copyFunc, D3DDECLTYPE nativeFormat) { VertexFormat formatInfo; formatInfo.conversionType = identity ? VERTEX_CONVERT_NONE : VERTEX_CONVERT_CPU; formatInfo.outputElementSize = elementSize; formatInfo.copyFunction = copyFunc; formatInfo.nativeFormat = nativeFormat; formatInfo.componentType = GetDeclTypeComponentType(nativeFormat); return formatInfo; } #define TRANSLATION(type, norm, size, preferred) \ CreateVertexFormatInfo \ ( \ Converter::identity, \ Converter::finalSize, \ Converter::convertArray, \ static_cast(Converter::declflag) \ ) #define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \ { \ Converter::capflag, \ TRANSLATION(type, norm, size, UsePreferred), \ TRANSLATION(type, norm, size, UseFallback) \ } #define TRANSLATIONS_FOR_TYPE(type) \ { \ { TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 4) }, \ { TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 4) }, \ } #define TRANSLATIONS_FOR_TYPE_NO_NORM(type) \ { \ { TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 4) }, \ { TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 4) }, \ } static inline unsigned int ComputeTypeIndex(GLenum type) { switch (type) { case GL_BYTE: return 0; case GL_UNSIGNED_BYTE: return 1; case GL_SHORT: return 2; case GL_UNSIGNED_SHORT: return 3; case GL_FIXED: return 4; case GL_FLOAT: return 5; default: UNREACHABLE(); return 5; } } const VertexFormat &GetVertexFormatInfo(DWORD supportedDeclTypes, gl::VertexFormatType vertexFormatType) { static bool initialized = false; static DWORD initializedDeclTypes = 0; static VertexFormat formatConverters[NUM_GL_VERTEX_ATTRIB_TYPES][2][4]; if (initializedDeclTypes != supportedDeclTypes) { const TranslationDescription translations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1] { TRANSLATIONS_FOR_TYPE(GL_BYTE), TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE), TRANSLATIONS_FOR_TYPE(GL_SHORT), TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT), TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FIXED), TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FLOAT) }; for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++) { for (unsigned int j = 0; j < 2; j++) { for (unsigned int k = 0; k < 4; k++) { if (translations[i][j][k].capsFlag == 0 || (supportedDeclTypes & translations[i][j][k].capsFlag) != 0) { formatConverters[i][j][k] = translations[i][j][k].preferredConversion; } else { formatConverters[i][j][k] = translations[i][j][k].fallbackConversion; } } } } initialized = true; initializedDeclTypes = supportedDeclTypes; } const gl::VertexFormat &vertexFormat = gl::GetVertexFormatFromType(vertexFormatType); // Pure integer attributes only supported in ES3.0 ASSERT(!vertexFormat.pureInteger); return formatConverters[ComputeTypeIndex(vertexFormat.type)][vertexFormat.normalized][vertexFormat.components - 1]; } } }