1 #include "precompiled.h"
3 // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
8 // formatutils9.cpp: Queries for GL image formats and their translations to D3D9
11 #include "libGLESv2/renderer/d3d9/formatutils9.h"
12 #include "libGLESv2/renderer/d3d9/Renderer9.h"
13 #include "libGLESv2/renderer/generatemip.h"
14 #include "libGLESv2/renderer/loadimage.h"
15 #include "libGLESv2/renderer/copyimage.h"
16 #include "libGLESv2/renderer/vertexconversion.h"
21 // Each GL internal format corresponds to one D3D format and data loading function.
22 // Due to not all formats being available all the time, some of the function/format types are wrapped
23 // in templates that perform format support queries on a Renderer9 object which is supplied
24 // when requesting the function or format.
26 typedef bool ((Renderer9::*Renderer9FormatCheckFunction)(void) const);
27 typedef LoadImageFunction (*RendererCheckLoadFunction)(const Renderer9 *renderer);
29 template <Renderer9FormatCheckFunction pred, LoadImageFunction prefered, LoadImageFunction fallback>
30 LoadImageFunction RendererCheckLoad(const Renderer9 *renderer)
32 return ((renderer->*pred)()) ? prefered : fallback;
35 template <LoadImageFunction loadFunc>
36 LoadImageFunction SimpleLoad(const Renderer9 *renderer)
41 LoadImageFunction UnreachableLoad(const Renderer9 *renderer)
47 typedef bool (*FallbackPredicateFunction)(void);
49 template <FallbackPredicateFunction pred, LoadImageFunction prefered, LoadImageFunction fallback>
50 LoadImageFunction FallbackLoadFunction(const Renderer9 *renderer)
52 return pred() ? prefered : fallback;
55 typedef D3DFORMAT (*FormatQueryFunction)(const rx::Renderer9 *renderer);
57 template <Renderer9FormatCheckFunction pred, D3DFORMAT prefered, D3DFORMAT fallback>
58 D3DFORMAT CheckFormatSupport(const rx::Renderer9 *renderer)
60 return (renderer->*pred)() ? prefered : fallback;
63 template <D3DFORMAT format>
64 D3DFORMAT D3D9Format(const rx::Renderer9 *renderer)
71 FormatQueryFunction mTexFormat;
72 FormatQueryFunction mRenderFormat;
73 RendererCheckLoadFunction mLoadFunction;
76 : mTexFormat(NULL), mRenderFormat(NULL), mLoadFunction(NULL)
79 D3D9FormatInfo(FormatQueryFunction textureFormat, FormatQueryFunction renderFormat, RendererCheckLoadFunction loadFunc)
80 : mTexFormat(textureFormat), mRenderFormat(renderFormat), mLoadFunction(loadFunc)
84 const D3DFORMAT D3DFMT_INTZ = ((D3DFORMAT)(MAKEFOURCC('I','N','T','Z')));
85 const D3DFORMAT D3DFMT_NULL = ((D3DFORMAT)(MAKEFOURCC('N','U','L','L')));
87 typedef std::pair<GLenum, D3D9FormatInfo> D3D9FormatPair;
88 typedef std::map<GLenum, D3D9FormatInfo> D3D9FormatMap;
90 static D3D9FormatMap BuildD3D9FormatMap()
94 // | Internal format | Texture format | Render format | Load function |
95 map.insert(D3D9FormatPair(GL_NONE, D3D9FormatInfo(D3D9Format<D3DFMT_NULL>, D3D9Format<D3DFMT_NULL>, UnreachableLoad )));
97 map.insert(D3D9FormatPair(GL_DEPTH_COMPONENT16, D3D9FormatInfo(D3D9Format<D3DFMT_INTZ>, D3D9Format<D3DFMT_D24S8>, UnreachableLoad )));
98 map.insert(D3D9FormatPair(GL_DEPTH_COMPONENT32_OES, D3D9FormatInfo(D3D9Format<D3DFMT_INTZ>, D3D9Format<D3DFMT_D32>, UnreachableLoad )));
99 map.insert(D3D9FormatPair(GL_DEPTH24_STENCIL8_OES, D3D9FormatInfo(D3D9Format<D3DFMT_INTZ>, D3D9Format<D3DFMT_D24S8>, UnreachableLoad )));
100 map.insert(D3D9FormatPair(GL_STENCIL_INDEX8, D3D9FormatInfo(D3D9Format<D3DFMT_UNKNOWN>, D3D9Format<D3DFMT_D24S8>, UnreachableLoad ))); // TODO: What's the texture format?
102 map.insert(D3D9FormatPair(GL_RGBA32F_EXT, D3D9FormatInfo(D3D9Format<D3DFMT_A32B32G32R32F>, D3D9Format<D3DFMT_A32B32G32R32F>, SimpleLoad<loadToNative<GLfloat, 4> > )));
103 map.insert(D3D9FormatPair(GL_RGB32F_EXT, D3D9FormatInfo(D3D9Format<D3DFMT_A32B32G32R32F>, D3D9Format<D3DFMT_A32B32G32R32F>, SimpleLoad<loadToNative3To4<GLfloat, gl::Float32One> >)));
104 map.insert(D3D9FormatPair(GL_RG32F_EXT, D3D9FormatInfo(D3D9Format<D3DFMT_G32R32F>, D3D9Format<D3DFMT_G32R32F>, SimpleLoad<loadToNative<GLfloat, 2> > )));
105 map.insert(D3D9FormatPair(GL_R32F_EXT, D3D9FormatInfo(D3D9Format<D3DFMT_R32F>, D3D9Format<D3DFMT_R32F>, SimpleLoad<loadToNative<GLfloat, 1> > )));
106 map.insert(D3D9FormatPair(GL_ALPHA32F_EXT, D3D9FormatInfo(D3D9Format<D3DFMT_A32B32G32R32F>, D3D9Format<D3DFMT_UNKNOWN>, SimpleLoad<loadAlphaFloatDataToRGBA> )));
107 map.insert(D3D9FormatPair(GL_LUMINANCE32F_EXT, D3D9FormatInfo(D3D9Format<D3DFMT_A32B32G32R32F>, D3D9Format<D3DFMT_UNKNOWN>, SimpleLoad<loadLuminanceFloatDataToRGBA> )));
108 map.insert(D3D9FormatPair(GL_LUMINANCE_ALPHA32F_EXT, D3D9FormatInfo(D3D9Format<D3DFMT_A32B32G32R32F>, D3D9Format<D3DFMT_UNKNOWN>, SimpleLoad<loadLuminanceAlphaFloatDataToRGBA> )));
110 map.insert(D3D9FormatPair(GL_RGBA16F_EXT, D3D9FormatInfo(D3D9Format<D3DFMT_A16B16G16R16F>, D3D9Format<D3DFMT_A16B16G16R16F>, SimpleLoad<loadToNative<GLhalf, 4> > )));
111 map.insert(D3D9FormatPair(GL_RGB16F_EXT, D3D9FormatInfo(D3D9Format<D3DFMT_A16B16G16R16F>, D3D9Format<D3DFMT_A16B16G16R16F>, SimpleLoad<loadToNative3To4<GLhalf, gl::Float16One> >)));
112 map.insert(D3D9FormatPair(GL_RG16F_EXT, D3D9FormatInfo(D3D9Format<D3DFMT_G16R16F>, D3D9Format<D3DFMT_G16R16F>, SimpleLoad<loadToNative<GLhalf, 2> > )));
113 map.insert(D3D9FormatPair(GL_R16F_EXT, D3D9FormatInfo(D3D9Format<D3DFMT_R16F>, D3D9Format<D3DFMT_R16F>, SimpleLoad<loadToNative<GLhalf, 1> > )));
114 map.insert(D3D9FormatPair(GL_ALPHA16F_EXT, D3D9FormatInfo(D3D9Format<D3DFMT_A16B16G16R16F>, D3D9Format<D3DFMT_UNKNOWN>, SimpleLoad<loadAlphaHalfFloatDataToRGBA> )));
115 map.insert(D3D9FormatPair(GL_LUMINANCE16F_EXT, D3D9FormatInfo(D3D9Format<D3DFMT_A16B16G16R16F>, D3D9Format<D3DFMT_UNKNOWN>, SimpleLoad<loadLuminanceHalfFloatDataToRGBA> )));
116 map.insert(D3D9FormatPair(GL_LUMINANCE_ALPHA16F_EXT, D3D9FormatInfo(D3D9Format<D3DFMT_A16B16G16R16F>, D3D9Format<D3DFMT_UNKNOWN>, SimpleLoad<loadLuminanceAlphaHalfFloatDataToRGBA>)));
118 map.insert(D3D9FormatPair(GL_ALPHA8_EXT, D3D9FormatInfo(D3D9Format<D3DFMT_A8R8G8B8>, D3D9Format<D3DFMT_A8R8G8B8>, FallbackLoadFunction<gl::supportsSSE2, loadAlphaDataToBGRASSE2, loadAlphaDataToBGRA>)));
120 map.insert(D3D9FormatPair(GL_RGB8_OES, D3D9FormatInfo(D3D9Format<D3DFMT_X8R8G8B8>, D3D9Format<D3DFMT_X8R8G8B8>, SimpleLoad<loadRGBUByteDataToBGRX> )));
121 map.insert(D3D9FormatPair(GL_RGB565, D3D9FormatInfo(CheckFormatSupport<&Renderer9::getRGB565TextureSupport, D3DFMT_R5G6B5, D3DFMT_X8R8G8B8>, CheckFormatSupport<&Renderer9::getRGB565TextureSupport, D3DFMT_R5G6B5, D3DFMT_X8R8G8B8>, RendererCheckLoad<&Renderer9::getRGB565TextureSupport, loadToNative<GLushort, 1>, loadRGB565DataToBGRA>)));
122 map.insert(D3D9FormatPair(GL_RGBA8_OES, D3D9FormatInfo(D3D9Format<D3DFMT_A8R8G8B8>, D3D9Format<D3DFMT_A8R8G8B8>, FallbackLoadFunction<gl::supportsSSE2, loadRGBAUByteDataToBGRASSE2, loadRGBAUByteDataToBGRA>)));
123 map.insert(D3D9FormatPair(GL_RGBA4, D3D9FormatInfo(D3D9Format<D3DFMT_A8R8G8B8>, D3D9Format<D3DFMT_A8R8G8B8>, SimpleLoad<loadRGBA4444DataToBGRA> )));
124 map.insert(D3D9FormatPair(GL_RGB5_A1, D3D9FormatInfo(D3D9Format<D3DFMT_A8R8G8B8>, D3D9Format<D3DFMT_A8R8G8B8>, SimpleLoad<loadRGBA5551DataToBGRA> )));
125 map.insert(D3D9FormatPair(GL_R8_EXT, D3D9FormatInfo(D3D9Format<D3DFMT_X8R8G8B8>, D3D9Format<D3DFMT_X8R8G8B8>, SimpleLoad<loadRUByteDataToBGRX> )));
126 map.insert(D3D9FormatPair(GL_RG8_EXT, D3D9FormatInfo(D3D9Format<D3DFMT_X8R8G8B8>, D3D9Format<D3DFMT_X8R8G8B8>, SimpleLoad<loadRGUByteDataToBGRX> )));
128 map.insert(D3D9FormatPair(GL_BGRA8_EXT, D3D9FormatInfo(D3D9Format<D3DFMT_A8R8G8B8>, D3D9Format<D3DFMT_A8R8G8B8>, SimpleLoad<loadToNative<GLubyte, 4> > )));
129 map.insert(D3D9FormatPair(GL_BGRA4_ANGLEX, D3D9FormatInfo(D3D9Format<D3DFMT_A8R8G8B8>, D3D9Format<D3DFMT_A8R8G8B8>, SimpleLoad<loadRGBA4444DataToRGBA> )));
130 map.insert(D3D9FormatPair(GL_BGR5_A1_ANGLEX, D3D9FormatInfo(D3D9Format<D3DFMT_A8R8G8B8>, D3D9Format<D3DFMT_A8R8G8B8>, SimpleLoad<loadRGBA5551DataToRGBA> )));
132 map.insert(D3D9FormatPair(GL_COMPRESSED_RGB_S3TC_DXT1_EXT, D3D9FormatInfo(D3D9Format<D3DFMT_DXT1>, D3D9Format<D3DFMT_UNKNOWN>, SimpleLoad<loadCompressedBlockDataToNative<4, 4, 8> >)));
133 map.insert(D3D9FormatPair(GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, D3D9FormatInfo(D3D9Format<D3DFMT_DXT1>, D3D9Format<D3DFMT_UNKNOWN>, SimpleLoad<loadCompressedBlockDataToNative<4, 4, 8> >)));
134 map.insert(D3D9FormatPair(GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE, D3D9FormatInfo(D3D9Format<D3DFMT_DXT3>, D3D9Format<D3DFMT_UNKNOWN>, SimpleLoad<loadCompressedBlockDataToNative<4, 4, 16> >)));
135 map.insert(D3D9FormatPair(GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE, D3D9FormatInfo(D3D9Format<D3DFMT_DXT5>, D3D9Format<D3DFMT_UNKNOWN>, SimpleLoad<loadCompressedBlockDataToNative<4, 4, 16> >)));
137 // These formats require checking if the renderer supports D3DFMT_L8 or D3DFMT_A8L8 and
138 // then changing the format and loading function appropriately.
139 map.insert(D3D9FormatPair(GL_LUMINANCE8_EXT, D3D9FormatInfo(CheckFormatSupport<&Renderer9::getLuminanceTextureSupport, D3DFMT_L8, D3DFMT_A8R8G8B8>, D3D9Format<D3DFMT_UNKNOWN>, RendererCheckLoad<&Renderer9::getLuminanceTextureSupport, loadToNative<GLubyte, 1>, loadLuminanceDataToBGRA>)));
140 map.insert(D3D9FormatPair(GL_LUMINANCE8_ALPHA8_EXT, D3D9FormatInfo(CheckFormatSupport<&Renderer9::getLuminanceAlphaTextureSupport, D3DFMT_A8L8, D3DFMT_A8R8G8B8>, D3D9Format<D3DFMT_UNKNOWN>, RendererCheckLoad<&Renderer9::getLuminanceTextureSupport, loadToNative<GLubyte, 2>, loadLuminanceAlphaDataToBGRA>)));
145 static bool GetD3D9FormatInfo(GLenum internalFormat, D3D9FormatInfo *outFormatInfo)
147 static const D3D9FormatMap formatMap = BuildD3D9FormatMap();
148 D3D9FormatMap::const_iterator iter = formatMap.find(internalFormat);
149 if (iter != formatMap.end())
153 *outFormatInfo = iter->second;
163 // A map to determine the pixel size and mip generation function of a given D3D format
169 GLenum mInternalFormat;
171 MipGenerationFunction mMipGenerationFunction;
172 ColorReadFunction mColorReadFunction;
175 : mPixelBits(0), mBlockWidth(0), mBlockHeight(0), mInternalFormat(GL_NONE), mMipGenerationFunction(NULL),
176 mColorReadFunction(NULL)
179 D3DFormatInfo(GLuint pixelBits, GLuint blockWidth, GLuint blockHeight, GLenum internalFormat,
180 MipGenerationFunction mipFunc, ColorReadFunction readFunc)
181 : mPixelBits(pixelBits), mBlockWidth(blockWidth), mBlockHeight(blockHeight), mInternalFormat(internalFormat),
182 mMipGenerationFunction(mipFunc), mColorReadFunction(readFunc)
186 typedef std::pair<D3DFORMAT, D3DFormatInfo> D3D9FormatInfoPair;
187 typedef std::map<D3DFORMAT, D3DFormatInfo> D3D9FormatInfoMap;
189 static D3D9FormatInfoMap BuildD3D9FormatInfoMap()
191 D3D9FormatInfoMap map;
193 // | D3DFORMAT | | S |W |H | Internal format | Mip generation function | Color read function |
194 map.insert(D3D9FormatInfoPair(D3DFMT_NULL, D3DFormatInfo( 0, 0, 0, GL_NONE, NULL, NULL )));
195 map.insert(D3D9FormatInfoPair(D3DFMT_UNKNOWN, D3DFormatInfo( 0, 0, 0, GL_NONE, NULL, NULL )));
197 map.insert(D3D9FormatInfoPair(D3DFMT_L8, D3DFormatInfo( 8, 1, 1, GL_LUMINANCE8_EXT, GenerateMip<L8>, ReadColor<L8, GLfloat> )));
198 map.insert(D3D9FormatInfoPair(D3DFMT_A8, D3DFormatInfo( 8, 1, 1, GL_ALPHA8_EXT, GenerateMip<A8>, ReadColor<A8, GLfloat> )));
199 map.insert(D3D9FormatInfoPair(D3DFMT_A8L8, D3DFormatInfo( 16, 1, 1, GL_LUMINANCE8_ALPHA8_EXT, GenerateMip<A8L8>, ReadColor<A8L8, GLfloat> )));
200 map.insert(D3D9FormatInfoPair(D3DFMT_A4R4G4B4, D3DFormatInfo( 16, 1, 1, GL_BGRA4_ANGLEX, GenerateMip<B4G4R4A4>, ReadColor<B4G4R4A4, GLfloat> )));
201 map.insert(D3D9FormatInfoPair(D3DFMT_A1R5G5B5, D3DFormatInfo( 16, 1, 1, GL_BGR5_A1_ANGLEX, GenerateMip<B5G5R5A1>, ReadColor<B5G5R5A1, GLfloat> )));
202 map.insert(D3D9FormatInfoPair(D3DFMT_R5G6B5, D3DFormatInfo( 16, 1, 1, GL_RGB565, GenerateMip<R5G6B5>, ReadColor<R5G6B5, GLfloat> )));
203 map.insert(D3D9FormatInfoPair(D3DFMT_X8R8G8B8, D3DFormatInfo( 32, 1, 1, GL_BGRA8_EXT, GenerateMip<B8G8R8X8>, ReadColor<B8G8R8X8, GLfloat> )));
204 map.insert(D3D9FormatInfoPair(D3DFMT_A8R8G8B8, D3DFormatInfo( 32, 1, 1, GL_BGRA8_EXT, GenerateMip<B8G8R8A8>, ReadColor<B8G8R8A8, GLfloat> )));
205 map.insert(D3D9FormatInfoPair(D3DFMT_R16F, D3DFormatInfo( 16, 1, 1, GL_R16F_EXT, GenerateMip<R16F>, ReadColor<R16F, GLfloat> )));
206 map.insert(D3D9FormatInfoPair(D3DFMT_G16R16F, D3DFormatInfo( 32, 1, 1, GL_RG16F_EXT, GenerateMip<R16G16F>, ReadColor<R16G16F, GLfloat> )));
207 map.insert(D3D9FormatInfoPair(D3DFMT_A16B16G16R16F, D3DFormatInfo( 64, 1, 1, GL_RGBA16F_EXT, GenerateMip<R16G16B16A16F>, ReadColor<R16G16B16A16F, GLfloat>)));
208 map.insert(D3D9FormatInfoPair(D3DFMT_R32F, D3DFormatInfo( 32, 1, 1, GL_R32F_EXT, GenerateMip<R32F>, ReadColor<R32F, GLfloat> )));
209 map.insert(D3D9FormatInfoPair(D3DFMT_G32R32F, D3DFormatInfo( 64, 1, 1, GL_RG32F_EXT, GenerateMip<R32G32F>, ReadColor<R32G32F, GLfloat> )));
210 map.insert(D3D9FormatInfoPair(D3DFMT_A32B32G32R32F, D3DFormatInfo(128, 1, 1, GL_RGBA32F_EXT, GenerateMip<R32G32B32A32F>, ReadColor<R32G32B32A32F, GLfloat>)));
212 map.insert(D3D9FormatInfoPair(D3DFMT_D16, D3DFormatInfo( 16, 1, 1, GL_DEPTH_COMPONENT16, NULL, NULL )));
213 map.insert(D3D9FormatInfoPair(D3DFMT_D24S8, D3DFormatInfo( 32, 1, 1, GL_DEPTH24_STENCIL8_OES, NULL, NULL )));
214 map.insert(D3D9FormatInfoPair(D3DFMT_D24X8, D3DFormatInfo( 32, 1, 1, GL_DEPTH_COMPONENT16, NULL, NULL )));
215 map.insert(D3D9FormatInfoPair(D3DFMT_D32, D3DFormatInfo( 32, 1, 1, GL_DEPTH_COMPONENT32_OES, NULL, NULL )));
217 map.insert(D3D9FormatInfoPair(D3DFMT_INTZ, D3DFormatInfo( 32, 1, 1, GL_DEPTH24_STENCIL8_OES, NULL, NULL )));
219 map.insert(D3D9FormatInfoPair(D3DFMT_DXT1, D3DFormatInfo( 64, 4, 4, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, NULL, NULL )));
220 map.insert(D3D9FormatInfoPair(D3DFMT_DXT3, D3DFormatInfo(128, 4, 4, GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE, NULL, NULL )));
221 map.insert(D3D9FormatInfoPair(D3DFMT_DXT5, D3DFormatInfo(128, 4, 4, GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE, NULL, NULL )));
226 static const D3D9FormatInfoMap &GetD3D9FormatInfoMap()
228 static const D3D9FormatInfoMap infoMap = BuildD3D9FormatInfoMap();
232 static bool GetD3D9FormatInfo(D3DFORMAT format, D3DFormatInfo *outFormatInfo)
234 const D3D9FormatInfoMap &infoMap = GetD3D9FormatInfoMap();
235 D3D9FormatInfoMap::const_iterator iter = infoMap.find(format);
236 if (iter != infoMap.end())
240 *outFormatInfo = iter->second;
249 static d3d9::D3DFormatSet BuildAllD3DFormatSet()
251 d3d9::D3DFormatSet set;
253 const D3D9FormatInfoMap &infoMap = GetD3D9FormatInfoMap();
254 for (D3D9FormatInfoMap::const_iterator i = infoMap.begin(); i != infoMap.end(); ++i)
256 set.insert(i->first);
262 struct D3D9FastCopyFormat
264 D3DFORMAT mSourceFormat;
268 D3D9FastCopyFormat(D3DFORMAT sourceFormat, GLenum destFormat, GLenum destType)
269 : mSourceFormat(sourceFormat), mDestFormat(destFormat), mDestType(destType)
272 bool operator<(const D3D9FastCopyFormat& other) const
274 return memcmp(this, &other, sizeof(D3D9FastCopyFormat)) < 0;
278 typedef std::map<D3D9FastCopyFormat, ColorCopyFunction> D3D9FastCopyMap;
279 typedef std::pair<D3D9FastCopyFormat, ColorCopyFunction> D3D9FastCopyPair;
281 static D3D9FastCopyMap BuildFastCopyMap()
285 map.insert(D3D9FastCopyPair(D3D9FastCopyFormat(D3DFMT_A8R8G8B8, GL_RGBA, GL_UNSIGNED_BYTE), CopyBGRAUByteToRGBAUByte));
290 typedef std::pair<GLint, InitializeTextureDataFunction> InternalFormatInitialzerPair;
291 typedef std::map<GLint, InitializeTextureDataFunction> InternalFormatInitialzerMap;
293 static InternalFormatInitialzerMap BuildInternalFormatInitialzerMap()
295 InternalFormatInitialzerMap map;
297 map.insert(InternalFormatInitialzerPair(GL_RGB16F, initialize4ComponentData<GLhalf, 0x0000, 0x0000, 0x0000, gl::Float16One>));
298 map.insert(InternalFormatInitialzerPair(GL_RGB32F, initialize4ComponentData<GLfloat, 0x00000000, 0x00000000, 0x00000000, gl::Float32One>));
303 static const InternalFormatInitialzerMap &GetInternalFormatInitialzerMap()
305 static const InternalFormatInitialzerMap map = BuildInternalFormatInitialzerMap();
312 MipGenerationFunction GetMipGenerationFunction(D3DFORMAT format)
314 D3DFormatInfo d3dFormatInfo;
315 if (GetD3D9FormatInfo(format, &d3dFormatInfo))
317 return d3dFormatInfo.mMipGenerationFunction;
326 LoadImageFunction GetImageLoadFunction(GLenum internalFormat, const Renderer9 *renderer)
333 ASSERT(renderer->getCurrentClientVersion() == 2);
335 D3D9FormatInfo d3d9FormatInfo;
336 if (GetD3D9FormatInfo(internalFormat, &d3d9FormatInfo))
338 return d3d9FormatInfo.mLoadFunction(renderer);
347 GLuint GetFormatPixelBytes(D3DFORMAT format)
349 D3DFormatInfo d3dFormatInfo;
350 if (GetD3D9FormatInfo(format, &d3dFormatInfo))
352 return d3dFormatInfo.mPixelBits / 8;
361 GLuint GetBlockWidth(D3DFORMAT format)
363 D3DFormatInfo d3dFormatInfo;
364 if (GetD3D9FormatInfo(format, &d3dFormatInfo))
366 return d3dFormatInfo.mBlockWidth;
375 GLuint GetBlockHeight(D3DFORMAT format)
377 D3DFormatInfo d3dFormatInfo;
378 if (GetD3D9FormatInfo(format, &d3dFormatInfo))
380 return d3dFormatInfo.mBlockHeight;
389 GLuint GetBlockSize(D3DFORMAT format, GLuint width, GLuint height)
391 D3DFormatInfo d3dFormatInfo;
392 if (GetD3D9FormatInfo(format, &d3dFormatInfo))
394 GLuint numBlocksWide = (width + d3dFormatInfo.mBlockWidth - 1) / d3dFormatInfo.mBlockWidth;
395 GLuint numBlocksHight = (height + d3dFormatInfo.mBlockHeight - 1) / d3dFormatInfo.mBlockHeight;
397 return (d3dFormatInfo.mPixelBits * numBlocksWide * numBlocksHight) / 8;
406 void MakeValidSize(bool isImage, D3DFORMAT format, GLsizei *requestWidth, GLsizei *requestHeight, int *levelOffset)
408 D3DFormatInfo d3dFormatInfo;
409 if (GetD3D9FormatInfo(format, &d3dFormatInfo))
411 int upsampleCount = 0;
413 GLsizei blockWidth = d3dFormatInfo.mBlockWidth;
414 GLsizei blockHeight = d3dFormatInfo.mBlockHeight;
416 // Don't expand the size of full textures that are at least (blockWidth x blockHeight) already.
417 if (isImage || *requestWidth < blockWidth || *requestHeight < blockHeight)
419 while (*requestWidth % blockWidth != 0 || *requestHeight % blockHeight != 0)
422 *requestHeight <<= 1;
426 *levelOffset = upsampleCount;
430 const D3DFormatSet &GetAllUsedD3DFormats()
432 static const D3DFormatSet formatSet = BuildAllD3DFormatSet();
436 ColorReadFunction GetColorReadFunction(D3DFORMAT format)
438 D3DFormatInfo d3dFormatInfo;
439 if (GetD3D9FormatInfo(format, &d3dFormatInfo))
441 return d3dFormatInfo.mColorReadFunction;
450 ColorCopyFunction GetFastCopyFunction(D3DFORMAT sourceFormat, GLenum destFormat, GLenum destType, GLuint clientVersion)
452 static const D3D9FastCopyMap fastCopyMap = BuildFastCopyMap();
453 D3D9FastCopyMap::const_iterator iter = fastCopyMap.find(D3D9FastCopyFormat(sourceFormat, destFormat, destType));
454 return (iter != fastCopyMap.end()) ? iter->second : NULL;
457 GLenum GetDeclTypeComponentType(D3DDECLTYPE declType)
461 case D3DDECLTYPE_FLOAT1: return GL_FLOAT;
462 case D3DDECLTYPE_FLOAT2: return GL_FLOAT;
463 case D3DDECLTYPE_FLOAT3: return GL_FLOAT;
464 case D3DDECLTYPE_FLOAT4: return GL_FLOAT;
465 case D3DDECLTYPE_UBYTE4: return GL_UNSIGNED_INT;
466 case D3DDECLTYPE_SHORT2: return GL_INT;
467 case D3DDECLTYPE_SHORT4: return GL_INT;
468 case D3DDECLTYPE_UBYTE4N: return GL_UNSIGNED_NORMALIZED;
469 case D3DDECLTYPE_SHORT4N: return GL_SIGNED_NORMALIZED;
470 case D3DDECLTYPE_USHORT4N: return GL_UNSIGNED_NORMALIZED;
471 case D3DDECLTYPE_SHORT2N: return GL_SIGNED_NORMALIZED;
472 case D3DDECLTYPE_USHORT2N: return GL_UNSIGNED_NORMALIZED;
473 default: UNREACHABLE(); return GL_NONE;
477 // Attribute format conversion
478 enum { NUM_GL_VERTEX_ATTRIB_TYPES = 6 };
480 struct FormatConverter
483 std::size_t outputElementSize;
484 void (*convertArray)(const void *in, std::size_t stride, std::size_t n, void *out);
485 D3DDECLTYPE d3dDeclType;
488 struct TranslationDescription
491 FormatConverter preferredConversion;
492 FormatConverter fallbackConversion;
495 static unsigned int typeIndex(GLenum type);
496 static const FormatConverter &formatConverter(const gl::VertexAttribute &attribute);
498 bool mTranslationsInitialized = false;
499 FormatConverter mFormatConverters[NUM_GL_VERTEX_ATTRIB_TYPES][2][4];
501 // Mapping from OpenGL-ES vertex attrib type to D3D decl type:
504 // BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm)
505 // UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast)
506 // UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize)
507 // SHORT SHORT (Identity)
508 // SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize)
509 // UNSIGNED_SHORT FLOAT (Cast)
510 // UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize)
511 // FIXED (not in WebGL) FLOAT (FixedToFloat)
512 // FLOAT FLOAT (Identity)
514 // GLToCType maps from GL type (as GLenum) to the C typedef.
515 template <GLenum GLType> struct GLToCType { };
517 template <> struct GLToCType<GL_BYTE> { typedef GLbyte type; };
518 template <> struct GLToCType<GL_UNSIGNED_BYTE> { typedef GLubyte type; };
519 template <> struct GLToCType<GL_SHORT> { typedef GLshort type; };
520 template <> struct GLToCType<GL_UNSIGNED_SHORT> { typedef GLushort type; };
521 template <> struct GLToCType<GL_FIXED> { typedef GLuint type; };
522 template <> struct GLToCType<GL_FLOAT> { typedef GLfloat type; };
524 // This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.)
535 // D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type.
536 template <unsigned int D3DType> struct D3DToCType { };
538 template <> struct D3DToCType<D3DVT_FLOAT> { typedef float type; };
539 template <> struct D3DToCType<D3DVT_SHORT> { typedef short type; };
540 template <> struct D3DToCType<D3DVT_SHORT_NORM> { typedef short type; };
541 template <> struct D3DToCType<D3DVT_UBYTE> { typedef unsigned char type; };
542 template <> struct D3DToCType<D3DVT_UBYTE_NORM> { typedef unsigned char type; };
543 template <> struct D3DToCType<D3DVT_USHORT_NORM> { typedef unsigned short type; };
545 // Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size.
546 template <unsigned int type, int size> struct WidenRule { };
548 template <int size> struct WidenRule<D3DVT_FLOAT, size> : NoWiden<size> { };
549 template <int size> struct WidenRule<D3DVT_SHORT, size> : WidenToEven<size> { };
550 template <int size> struct WidenRule<D3DVT_SHORT_NORM, size> : WidenToEven<size> { };
551 template <int size> struct WidenRule<D3DVT_UBYTE, size> : WidenToFour<size> { };
552 template <int size> struct WidenRule<D3DVT_UBYTE_NORM, size> : WidenToFour<size> { };
553 template <int size> struct WidenRule<D3DVT_USHORT_NORM, size> : WidenToEven<size> { };
555 // VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination.
556 template <unsigned int d3dtype, int size> struct VertexTypeFlags { };
558 template <unsigned int _capflag, unsigned int _declflag>
559 struct VertexTypeFlagsHelper
561 enum { capflag = _capflag };
562 enum { declflag = _declflag };
565 template <> struct VertexTypeFlags<D3DVT_FLOAT, 1> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { };
566 template <> struct VertexTypeFlags<D3DVT_FLOAT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { };
567 template <> struct VertexTypeFlags<D3DVT_FLOAT, 3> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { };
568 template <> struct VertexTypeFlags<D3DVT_FLOAT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { };
569 template <> struct VertexTypeFlags<D3DVT_SHORT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { };
570 template <> struct VertexTypeFlags<D3DVT_SHORT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { };
571 template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT2N, D3DDECLTYPE_SHORT2N> { };
572 template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT4N, D3DDECLTYPE_SHORT4N> { };
573 template <> struct VertexTypeFlags<D3DVT_UBYTE, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4, D3DDECLTYPE_UBYTE4> { };
574 template <> struct VertexTypeFlags<D3DVT_UBYTE_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4N, D3DDECLTYPE_UBYTE4N> { };
575 template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT2N, D3DDECLTYPE_USHORT2N> { };
576 template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT4N, D3DDECLTYPE_USHORT4N> { };
579 // VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums).
580 template <GLenum GLtype, bool normalized> struct VertexTypeMapping { };
582 template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred>
583 struct VertexTypeMappingBase
585 enum { preferred = Preferred };
586 enum { fallback = Fallback };
589 template <> struct VertexTypeMapping<GL_BYTE, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Cast
590 template <> struct VertexTypeMapping<GL_BYTE, true> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Normalize
591 template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, false> : VertexTypeMappingBase<D3DVT_UBYTE, D3DVT_FLOAT> { }; // Identity, Cast
592 template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, true> : VertexTypeMappingBase<D3DVT_UBYTE_NORM, D3DVT_FLOAT> { }; // Identity, Normalize
593 template <> struct VertexTypeMapping<GL_SHORT, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Identity
594 template <> struct VertexTypeMapping<GL_SHORT, true> : VertexTypeMappingBase<D3DVT_SHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
595 template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, false> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Cast
596 template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, true> : VertexTypeMappingBase<D3DVT_USHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize
597 template <bool normalized> struct VertexTypeMapping<GL_FIXED, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // FixedToFloat
598 template <bool normalized> struct VertexTypeMapping<GL_FLOAT, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Identity
601 // Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat).
602 // The conversion rules themselves are defined in vertexconversion.h.
604 // Almost all cases are covered by Cast (including those that are actually Identity since Cast<T,T> knows it's an identity mapping).
605 template <GLenum fromType, bool normalized, unsigned int toType>
606 struct ConversionRule : Cast<typename GLToCType<fromType>::type, typename D3DToCType<toType>::type> { };
608 // All conversions from normalized types to float use the Normalize operator.
609 template <GLenum fromType> struct ConversionRule<fromType, true, D3DVT_FLOAT> : Normalize<typename GLToCType<fromType>::type> { };
611 // Use a full specialization for this so that it preferentially matches ahead of the generic normalize-to-float rules.
612 template <> struct ConversionRule<GL_FIXED, true, D3DVT_FLOAT> : FixedToFloat<GLint, 16> { };
613 template <> struct ConversionRule<GL_FIXED, false, D3DVT_FLOAT> : FixedToFloat<GLint, 16> { };
615 // A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1)
616 // whether it is normalized or not.
617 template <class T, bool normalized> struct DefaultVertexValuesStage2 { };
619 template <class T> struct DefaultVertexValuesStage2<T, true> : NormalizedDefaultValues<T> { };
620 template <class T> struct DefaultVertexValuesStage2<T, false> : SimpleDefaultValues<T> { };
622 // Work out the default value rule for a D3D type (expressed as the C type) and
623 template <class T, bool normalized> struct DefaultVertexValues : DefaultVertexValuesStage2<T, normalized> { };
624 template <bool normalized> struct DefaultVertexValues<float, normalized> : SimpleDefaultValues<float> { };
626 // Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion.
627 // The fallback conversion produces an output that all D3D9 devices must support.
628 template <class T> struct UsePreferred { enum { type = T::preferred }; };
629 template <class T> struct UseFallback { enum { type = T::fallback }; };
631 // Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion,
632 // it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag
633 // and the D3DDECLTYPE member needed for the vertex declaration in declflag.
634 template <GLenum fromType, bool normalized, int size, template <class T> class PreferenceRule>
636 : VertexDataConverter<typename GLToCType<fromType>::type,
637 WidenRule<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type, size>,
638 ConversionRule<fromType,
640 PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>,
641 DefaultVertexValues<typename D3DToCType<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>::type, normalized > >
644 enum { d3dtype = PreferenceRule< VertexTypeMapping<fromType, normalized> >::type };
645 enum { d3dsize = WidenRule<d3dtype, size>::finalWidth };
648 enum { capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag };
649 enum { declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag };
652 // Initialize a TranslationInfo
653 #define TRANSLATION(type, norm, size, preferred) \
655 Converter<type, norm, size, preferred>::identity, \
656 Converter<type, norm, size, preferred>::finalSize, \
657 Converter<type, norm, size, preferred>::convertArray, \
658 static_cast<D3DDECLTYPE>(Converter<type, norm, size, preferred>::declflag) \
661 #define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \
663 Converter<type, norm, size, UsePreferred>::capflag, \
664 TRANSLATION(type, norm, size, UsePreferred), \
665 TRANSLATION(type, norm, size, UseFallback) \
668 #define TRANSLATIONS_FOR_TYPE(type) \
670 { 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) }, \
671 { 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) }, \
674 #define TRANSLATIONS_FOR_TYPE_NO_NORM(type) \
676 { 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) }, \
677 { 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) }, \
680 const TranslationDescription mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1]
682 TRANSLATIONS_FOR_TYPE(GL_BYTE),
683 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE),
684 TRANSLATIONS_FOR_TYPE(GL_SHORT),
685 TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT),
686 TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FIXED),
687 TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FLOAT)
690 void InitializeVertexTranslations(const rx::Renderer9 *renderer)
692 DWORD declTypes = renderer->getCapsDeclTypes();
694 for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++)
696 for (unsigned int j = 0; j < 2; j++)
698 for (unsigned int k = 0; k < 4; k++)
700 if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0)
702 mFormatConverters[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion;
706 mFormatConverters[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion;
713 unsigned int typeIndex(GLenum type)
717 case GL_BYTE: return 0;
718 case GL_UNSIGNED_BYTE: return 1;
719 case GL_SHORT: return 2;
720 case GL_UNSIGNED_SHORT: return 3;
721 case GL_FIXED: return 4;
722 case GL_FLOAT: return 5;
724 default: UNREACHABLE(); return 5;
728 const FormatConverter &formatConverter(const gl::VertexFormat &vertexFormat)
730 // Pure integer attributes only supported in ES3.0
731 ASSERT(!vertexFormat.mPureInteger);
732 return mFormatConverters[typeIndex(vertexFormat.mType)][vertexFormat.mNormalized][vertexFormat.mComponents - 1];
735 VertexCopyFunction GetVertexCopyFunction(const gl::VertexFormat &vertexFormat)
737 return formatConverter(vertexFormat).convertArray;
740 size_t GetVertexElementSize(const gl::VertexFormat &vertexFormat)
742 return formatConverter(vertexFormat).outputElementSize;
745 VertexConversionType GetVertexConversionType(const gl::VertexFormat &vertexFormat)
747 return (formatConverter(vertexFormat).identity ? VERTEX_CONVERT_NONE : VERTEX_CONVERT_CPU);
750 D3DDECLTYPE GetNativeVertexFormat(const gl::VertexFormat &vertexFormat)
752 return formatConverter(vertexFormat).d3dDeclType;
760 D3DFORMAT GetTextureFormat(GLenum internalFormat, const Renderer9 *renderer)
765 return D3DFMT_UNKNOWN;
768 ASSERT(renderer->getCurrentClientVersion() == 2);
770 D3D9FormatInfo d3d9FormatInfo;
771 if (GetD3D9FormatInfo(internalFormat, &d3d9FormatInfo))
773 return d3d9FormatInfo.mTexFormat(renderer);
778 return D3DFMT_UNKNOWN;
782 D3DFORMAT GetRenderFormat(GLenum internalFormat, const Renderer9 *renderer)
787 return D3DFMT_UNKNOWN;
790 ASSERT(renderer->getCurrentClientVersion() == 2);
792 D3D9FormatInfo d3d9FormatInfo;
793 if (GetD3D9FormatInfo(internalFormat, &d3d9FormatInfo))
795 return d3d9FormatInfo.mRenderFormat(renderer);
800 return D3DFMT_UNKNOWN;
804 D3DMULTISAMPLE_TYPE GetMultisampleType(GLsizei samples)
806 return (samples > 1) ? static_cast<D3DMULTISAMPLE_TYPE>(samples) : D3DMULTISAMPLE_NONE;
809 bool RequiresTextureDataInitialization(GLint internalFormat)
811 const InternalFormatInitialzerMap &map = GetInternalFormatInitialzerMap();
812 return map.find(internalFormat) != map.end();
815 InitializeTextureDataFunction GetTextureDataInitializationFunction(GLint internalFormat)
817 const InternalFormatInitialzerMap &map = GetInternalFormatInitialzerMap();
818 InternalFormatInitialzerMap::const_iterator iter = map.find(internalFormat);
819 if (iter != map.end())
835 GLenum GetInternalFormat(D3DFORMAT format)
837 static const D3D9FormatInfoMap infoMap = BuildD3D9FormatInfoMap();
838 D3D9FormatInfoMap::const_iterator iter = infoMap.find(format);
839 if (iter != infoMap.end())
841 return iter->second.mInternalFormat;
850 GLsizei GetSamplesCount(D3DMULTISAMPLE_TYPE type)
852 return (type != D3DMULTISAMPLE_NONMASKABLE) ? type : 0;
855 bool IsFormatChannelEquivalent(D3DFORMAT d3dformat, GLenum format, GLuint clientVersion)
857 GLenum internalFormat = d3d9_gl::GetInternalFormat(d3dformat);
858 GLenum convertedFormat = gl::GetFormat(internalFormat, clientVersion);
859 return convertedFormat == format;