PageRenderTime 201ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/3rdparty/angle/src/libGLESv2/renderer/renderer9_utils.cpp

https://bitbucket.org/cvp2ri/qt5-tlsauth
C++ | 500 lines | 439 code | 52 blank | 9 comment | 50 complexity | 638cb275726ca03e652d4ba03f0b0970 MD5 | raw file
  1. #include "precompiled.h"
  2. //
  3. // Copyright (c) 2002-2012 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.
  6. //
  7. // renderer9_utils.cpp: Conversion functions and other utility routines
  8. // specific to the D3D9 renderer.
  9. #include "libGLESv2/renderer/renderer9_utils.h"
  10. #include "libGLESv2/mathutil.h"
  11. #include "libGLESv2/Context.h"
  12. #include "common/debug.h"
  13. namespace gl_d3d9
  14. {
  15. D3DCMPFUNC ConvertComparison(GLenum comparison)
  16. {
  17. D3DCMPFUNC d3dComp = D3DCMP_ALWAYS;
  18. switch (comparison)
  19. {
  20. case GL_NEVER: d3dComp = D3DCMP_NEVER; break;
  21. case GL_ALWAYS: d3dComp = D3DCMP_ALWAYS; break;
  22. case GL_LESS: d3dComp = D3DCMP_LESS; break;
  23. case GL_LEQUAL: d3dComp = D3DCMP_LESSEQUAL; break;
  24. case GL_EQUAL: d3dComp = D3DCMP_EQUAL; break;
  25. case GL_GREATER: d3dComp = D3DCMP_GREATER; break;
  26. case GL_GEQUAL: d3dComp = D3DCMP_GREATEREQUAL; break;
  27. case GL_NOTEQUAL: d3dComp = D3DCMP_NOTEQUAL; break;
  28. default: UNREACHABLE();
  29. }
  30. return d3dComp;
  31. }
  32. D3DCOLOR ConvertColor(gl::Color color)
  33. {
  34. return D3DCOLOR_RGBA(gl::unorm<8>(color.red),
  35. gl::unorm<8>(color.green),
  36. gl::unorm<8>(color.blue),
  37. gl::unorm<8>(color.alpha));
  38. }
  39. D3DBLEND ConvertBlendFunc(GLenum blend)
  40. {
  41. D3DBLEND d3dBlend = D3DBLEND_ZERO;
  42. switch (blend)
  43. {
  44. case GL_ZERO: d3dBlend = D3DBLEND_ZERO; break;
  45. case GL_ONE: d3dBlend = D3DBLEND_ONE; break;
  46. case GL_SRC_COLOR: d3dBlend = D3DBLEND_SRCCOLOR; break;
  47. case GL_ONE_MINUS_SRC_COLOR: d3dBlend = D3DBLEND_INVSRCCOLOR; break;
  48. case GL_DST_COLOR: d3dBlend = D3DBLEND_DESTCOLOR; break;
  49. case GL_ONE_MINUS_DST_COLOR: d3dBlend = D3DBLEND_INVDESTCOLOR; break;
  50. case GL_SRC_ALPHA: d3dBlend = D3DBLEND_SRCALPHA; break;
  51. case GL_ONE_MINUS_SRC_ALPHA: d3dBlend = D3DBLEND_INVSRCALPHA; break;
  52. case GL_DST_ALPHA: d3dBlend = D3DBLEND_DESTALPHA; break;
  53. case GL_ONE_MINUS_DST_ALPHA: d3dBlend = D3DBLEND_INVDESTALPHA; break;
  54. case GL_CONSTANT_COLOR: d3dBlend = D3DBLEND_BLENDFACTOR; break;
  55. case GL_ONE_MINUS_CONSTANT_COLOR: d3dBlend = D3DBLEND_INVBLENDFACTOR; break;
  56. case GL_CONSTANT_ALPHA: d3dBlend = D3DBLEND_BLENDFACTOR; break;
  57. case GL_ONE_MINUS_CONSTANT_ALPHA: d3dBlend = D3DBLEND_INVBLENDFACTOR; break;
  58. case GL_SRC_ALPHA_SATURATE: d3dBlend = D3DBLEND_SRCALPHASAT; break;
  59. default: UNREACHABLE();
  60. }
  61. return d3dBlend;
  62. }
  63. D3DBLENDOP ConvertBlendOp(GLenum blendOp)
  64. {
  65. D3DBLENDOP d3dBlendOp = D3DBLENDOP_ADD;
  66. switch (blendOp)
  67. {
  68. case GL_FUNC_ADD: d3dBlendOp = D3DBLENDOP_ADD; break;
  69. case GL_FUNC_SUBTRACT: d3dBlendOp = D3DBLENDOP_SUBTRACT; break;
  70. case GL_FUNC_REVERSE_SUBTRACT: d3dBlendOp = D3DBLENDOP_REVSUBTRACT; break;
  71. default: UNREACHABLE();
  72. }
  73. return d3dBlendOp;
  74. }
  75. D3DSTENCILOP ConvertStencilOp(GLenum stencilOp)
  76. {
  77. D3DSTENCILOP d3dStencilOp = D3DSTENCILOP_KEEP;
  78. switch (stencilOp)
  79. {
  80. case GL_ZERO: d3dStencilOp = D3DSTENCILOP_ZERO; break;
  81. case GL_KEEP: d3dStencilOp = D3DSTENCILOP_KEEP; break;
  82. case GL_REPLACE: d3dStencilOp = D3DSTENCILOP_REPLACE; break;
  83. case GL_INCR: d3dStencilOp = D3DSTENCILOP_INCRSAT; break;
  84. case GL_DECR: d3dStencilOp = D3DSTENCILOP_DECRSAT; break;
  85. case GL_INVERT: d3dStencilOp = D3DSTENCILOP_INVERT; break;
  86. case GL_INCR_WRAP: d3dStencilOp = D3DSTENCILOP_INCR; break;
  87. case GL_DECR_WRAP: d3dStencilOp = D3DSTENCILOP_DECR; break;
  88. default: UNREACHABLE();
  89. }
  90. return d3dStencilOp;
  91. }
  92. D3DTEXTUREADDRESS ConvertTextureWrap(GLenum wrap)
  93. {
  94. D3DTEXTUREADDRESS d3dWrap = D3DTADDRESS_WRAP;
  95. switch (wrap)
  96. {
  97. case GL_REPEAT: d3dWrap = D3DTADDRESS_WRAP; break;
  98. case GL_CLAMP_TO_EDGE: d3dWrap = D3DTADDRESS_CLAMP; break;
  99. case GL_MIRRORED_REPEAT: d3dWrap = D3DTADDRESS_MIRROR; break;
  100. default: UNREACHABLE();
  101. }
  102. return d3dWrap;
  103. }
  104. D3DCULL ConvertCullMode(GLenum cullFace, GLenum frontFace)
  105. {
  106. D3DCULL cull = D3DCULL_CCW;
  107. switch (cullFace)
  108. {
  109. case GL_FRONT:
  110. cull = (frontFace == GL_CCW ? D3DCULL_CW : D3DCULL_CCW);
  111. break;
  112. case GL_BACK:
  113. cull = (frontFace == GL_CCW ? D3DCULL_CCW : D3DCULL_CW);
  114. break;
  115. case GL_FRONT_AND_BACK:
  116. cull = D3DCULL_NONE; // culling will be handled during draw
  117. break;
  118. default: UNREACHABLE();
  119. }
  120. return cull;
  121. }
  122. D3DCUBEMAP_FACES ConvertCubeFace(GLenum cubeFace)
  123. {
  124. D3DCUBEMAP_FACES face = D3DCUBEMAP_FACE_POSITIVE_X;
  125. switch (cubeFace)
  126. {
  127. case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
  128. face = D3DCUBEMAP_FACE_POSITIVE_X;
  129. break;
  130. case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
  131. face = D3DCUBEMAP_FACE_NEGATIVE_X;
  132. break;
  133. case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
  134. face = D3DCUBEMAP_FACE_POSITIVE_Y;
  135. break;
  136. case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
  137. face = D3DCUBEMAP_FACE_NEGATIVE_Y;
  138. break;
  139. case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
  140. face = D3DCUBEMAP_FACE_POSITIVE_Z;
  141. break;
  142. case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
  143. face = D3DCUBEMAP_FACE_NEGATIVE_Z;
  144. break;
  145. default: UNREACHABLE();
  146. }
  147. return face;
  148. }
  149. DWORD ConvertColorMask(bool red, bool green, bool blue, bool alpha)
  150. {
  151. return (red ? D3DCOLORWRITEENABLE_RED : 0) |
  152. (green ? D3DCOLORWRITEENABLE_GREEN : 0) |
  153. (blue ? D3DCOLORWRITEENABLE_BLUE : 0) |
  154. (alpha ? D3DCOLORWRITEENABLE_ALPHA : 0);
  155. }
  156. D3DTEXTUREFILTERTYPE ConvertMagFilter(GLenum magFilter, float maxAnisotropy)
  157. {
  158. if (maxAnisotropy > 1.0f)
  159. {
  160. return D3DTEXF_ANISOTROPIC;
  161. }
  162. D3DTEXTUREFILTERTYPE d3dMagFilter = D3DTEXF_POINT;
  163. switch (magFilter)
  164. {
  165. case GL_NEAREST: d3dMagFilter = D3DTEXF_POINT; break;
  166. case GL_LINEAR: d3dMagFilter = D3DTEXF_LINEAR; break;
  167. default: UNREACHABLE();
  168. }
  169. return d3dMagFilter;
  170. }
  171. void ConvertMinFilter(GLenum minFilter, D3DTEXTUREFILTERTYPE *d3dMinFilter, D3DTEXTUREFILTERTYPE *d3dMipFilter, float maxAnisotropy)
  172. {
  173. switch (minFilter)
  174. {
  175. case GL_NEAREST:
  176. *d3dMinFilter = D3DTEXF_POINT;
  177. *d3dMipFilter = D3DTEXF_NONE;
  178. break;
  179. case GL_LINEAR:
  180. *d3dMinFilter = D3DTEXF_LINEAR;
  181. *d3dMipFilter = D3DTEXF_NONE;
  182. break;
  183. case GL_NEAREST_MIPMAP_NEAREST:
  184. *d3dMinFilter = D3DTEXF_POINT;
  185. *d3dMipFilter = D3DTEXF_POINT;
  186. break;
  187. case GL_LINEAR_MIPMAP_NEAREST:
  188. *d3dMinFilter = D3DTEXF_LINEAR;
  189. *d3dMipFilter = D3DTEXF_POINT;
  190. break;
  191. case GL_NEAREST_MIPMAP_LINEAR:
  192. *d3dMinFilter = D3DTEXF_POINT;
  193. *d3dMipFilter = D3DTEXF_LINEAR;
  194. break;
  195. case GL_LINEAR_MIPMAP_LINEAR:
  196. *d3dMinFilter = D3DTEXF_LINEAR;
  197. *d3dMipFilter = D3DTEXF_LINEAR;
  198. break;
  199. default:
  200. *d3dMinFilter = D3DTEXF_POINT;
  201. *d3dMipFilter = D3DTEXF_NONE;
  202. UNREACHABLE();
  203. }
  204. if (maxAnisotropy > 1.0f)
  205. {
  206. *d3dMinFilter = D3DTEXF_ANISOTROPIC;
  207. }
  208. }
  209. D3DFORMAT ConvertRenderbufferFormat(GLenum format)
  210. {
  211. switch (format)
  212. {
  213. case GL_NONE: return D3DFMT_NULL;
  214. case GL_RGBA4:
  215. case GL_RGB5_A1:
  216. case GL_RGBA8_OES: return D3DFMT_A8R8G8B8;
  217. case GL_RGB565: return D3DFMT_R5G6B5;
  218. case GL_RGB8_OES: return D3DFMT_X8R8G8B8;
  219. case GL_DEPTH_COMPONENT16:
  220. case GL_STENCIL_INDEX8:
  221. case GL_DEPTH24_STENCIL8_OES: return D3DFMT_D24S8;
  222. default: UNREACHABLE(); return D3DFMT_A8R8G8B8;
  223. }
  224. }
  225. D3DMULTISAMPLE_TYPE GetMultisampleTypeFromSamples(GLsizei samples)
  226. {
  227. if (samples <= 1)
  228. return D3DMULTISAMPLE_NONE;
  229. else
  230. return (D3DMULTISAMPLE_TYPE)samples;
  231. }
  232. }
  233. namespace d3d9_gl
  234. {
  235. unsigned int GetStencilSize(D3DFORMAT stencilFormat)
  236. {
  237. if (stencilFormat == D3DFMT_INTZ)
  238. {
  239. return 8;
  240. }
  241. switch(stencilFormat)
  242. {
  243. case D3DFMT_D24FS8:
  244. case D3DFMT_D24S8:
  245. return 8;
  246. case D3DFMT_D24X4S4:
  247. return 4;
  248. case D3DFMT_D15S1:
  249. return 1;
  250. case D3DFMT_D16_LOCKABLE:
  251. case D3DFMT_D32:
  252. case D3DFMT_D24X8:
  253. case D3DFMT_D32F_LOCKABLE:
  254. case D3DFMT_D16:
  255. return 0;
  256. //case D3DFMT_D32_LOCKABLE: return 0; // DirectX 9Ex only
  257. //case D3DFMT_S8_LOCKABLE: return 8; // DirectX 9Ex only
  258. default:
  259. return 0;
  260. }
  261. }
  262. unsigned int GetAlphaSize(D3DFORMAT colorFormat)
  263. {
  264. switch (colorFormat)
  265. {
  266. case D3DFMT_A16B16G16R16F:
  267. return 16;
  268. case D3DFMT_A32B32G32R32F:
  269. return 32;
  270. case D3DFMT_A2R10G10B10:
  271. return 2;
  272. case D3DFMT_A8R8G8B8:
  273. return 8;
  274. case D3DFMT_A1R5G5B5:
  275. return 1;
  276. case D3DFMT_X8R8G8B8:
  277. case D3DFMT_R5G6B5:
  278. return 0;
  279. default:
  280. return 0;
  281. }
  282. }
  283. GLsizei GetSamplesFromMultisampleType(D3DMULTISAMPLE_TYPE type)
  284. {
  285. if (type == D3DMULTISAMPLE_NONMASKABLE)
  286. return 0;
  287. else
  288. return type;
  289. }
  290. bool IsFormatChannelEquivalent(D3DFORMAT d3dformat, GLenum format)
  291. {
  292. switch (d3dformat)
  293. {
  294. case D3DFMT_L8:
  295. return (format == GL_LUMINANCE);
  296. case D3DFMT_A8L8:
  297. return (format == GL_LUMINANCE_ALPHA);
  298. case D3DFMT_DXT1:
  299. return (format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT || format == GL_COMPRESSED_RGB_S3TC_DXT1_EXT);
  300. case D3DFMT_DXT3:
  301. return (format == GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE);
  302. case D3DFMT_DXT5:
  303. return (format == GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE);
  304. case D3DFMT_A8R8G8B8:
  305. case D3DFMT_A16B16G16R16F:
  306. case D3DFMT_A32B32G32R32F:
  307. return (format == GL_RGBA || format == GL_BGRA_EXT);
  308. case D3DFMT_X8R8G8B8:
  309. return (format == GL_RGB);
  310. default:
  311. if (d3dformat == D3DFMT_INTZ && gl::IsDepthTexture(format))
  312. return true;
  313. return false;
  314. }
  315. }
  316. GLenum ConvertBackBufferFormat(D3DFORMAT format)
  317. {
  318. switch (format)
  319. {
  320. case D3DFMT_A4R4G4B4: return GL_RGBA4;
  321. case D3DFMT_A8R8G8B8: return GL_RGBA8_OES;
  322. case D3DFMT_A1R5G5B5: return GL_RGB5_A1;
  323. case D3DFMT_R5G6B5: return GL_RGB565;
  324. case D3DFMT_X8R8G8B8: return GL_RGB8_OES;
  325. default:
  326. UNREACHABLE();
  327. }
  328. return GL_RGBA4;
  329. }
  330. GLenum ConvertDepthStencilFormat(D3DFORMAT format)
  331. {
  332. if (format == D3DFMT_INTZ)
  333. {
  334. return GL_DEPTH24_STENCIL8_OES;
  335. }
  336. switch (format)
  337. {
  338. case D3DFMT_D16:
  339. case D3DFMT_D24X8:
  340. return GL_DEPTH_COMPONENT16;
  341. case D3DFMT_D24S8:
  342. return GL_DEPTH24_STENCIL8_OES;
  343. case D3DFMT_UNKNOWN:
  344. return GL_NONE;
  345. default:
  346. UNREACHABLE();
  347. }
  348. return GL_DEPTH24_STENCIL8_OES;
  349. }
  350. GLenum ConvertRenderTargetFormat(D3DFORMAT format)
  351. {
  352. if (format == D3DFMT_INTZ)
  353. {
  354. return GL_DEPTH24_STENCIL8_OES;
  355. }
  356. switch (format)
  357. {
  358. case D3DFMT_A4R4G4B4: return GL_RGBA4;
  359. case D3DFMT_A8R8G8B8: return GL_RGBA8_OES;
  360. case D3DFMT_A1R5G5B5: return GL_RGB5_A1;
  361. case D3DFMT_R5G6B5: return GL_RGB565;
  362. case D3DFMT_X8R8G8B8: return GL_RGB8_OES;
  363. case D3DFMT_D16:
  364. case D3DFMT_D24X8:
  365. return GL_DEPTH_COMPONENT16;
  366. case D3DFMT_D24S8:
  367. return GL_DEPTH24_STENCIL8_OES;
  368. case D3DFMT_UNKNOWN:
  369. return GL_NONE;
  370. default:
  371. UNREACHABLE();
  372. }
  373. return GL_RGBA4;
  374. }
  375. GLenum GetEquivalentFormat(D3DFORMAT format)
  376. {
  377. if (format == D3DFMT_INTZ)
  378. return GL_DEPTH24_STENCIL8_OES;
  379. if (format == D3DFMT_NULL)
  380. return GL_NONE;
  381. switch (format)
  382. {
  383. case D3DFMT_A4R4G4B4: return GL_RGBA4;
  384. case D3DFMT_A8R8G8B8: return GL_RGBA8_OES;
  385. case D3DFMT_A1R5G5B5: return GL_RGB5_A1;
  386. case D3DFMT_R5G6B5: return GL_RGB565;
  387. case D3DFMT_X8R8G8B8: return GL_RGB8_OES;
  388. case D3DFMT_D16: return GL_DEPTH_COMPONENT16;
  389. case D3DFMT_D24S8: return GL_DEPTH24_STENCIL8_OES;
  390. case D3DFMT_UNKNOWN: return GL_NONE;
  391. case D3DFMT_DXT1: return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  392. case D3DFMT_DXT3: return GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
  393. case D3DFMT_DXT5: return GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
  394. case D3DFMT_A32B32G32R32F: return GL_RGBA32F_EXT;
  395. case D3DFMT_A16B16G16R16F: return GL_RGBA16F_EXT;
  396. case D3DFMT_L8: return GL_LUMINANCE8_EXT;
  397. case D3DFMT_A8L8: return GL_LUMINANCE8_ALPHA8_EXT;
  398. default: UNREACHABLE();
  399. return GL_NONE;
  400. }
  401. }
  402. }
  403. namespace d3d9
  404. {
  405. bool IsCompressedFormat(D3DFORMAT surfaceFormat)
  406. {
  407. switch(surfaceFormat)
  408. {
  409. case D3DFMT_DXT1:
  410. case D3DFMT_DXT2:
  411. case D3DFMT_DXT3:
  412. case D3DFMT_DXT4:
  413. case D3DFMT_DXT5:
  414. return true;
  415. default:
  416. return false;
  417. }
  418. }
  419. size_t ComputeRowSize(D3DFORMAT format, unsigned int width)
  420. {
  421. if (format == D3DFMT_INTZ)
  422. {
  423. return 4 * width;
  424. }
  425. switch (format)
  426. {
  427. case D3DFMT_L8:
  428. return 1 * width;
  429. case D3DFMT_A8L8:
  430. return 2 * width;
  431. case D3DFMT_X8R8G8B8:
  432. case D3DFMT_A8R8G8B8:
  433. return 4 * width;
  434. case D3DFMT_A16B16G16R16F:
  435. return 8 * width;
  436. case D3DFMT_A32B32G32R32F:
  437. return 16 * width;
  438. case D3DFMT_DXT1:
  439. return 8 * ((width + 3) / 4);
  440. case D3DFMT_DXT3:
  441. case D3DFMT_DXT5:
  442. return 16 * ((width + 3) / 4);
  443. default:
  444. UNREACHABLE();
  445. return 0;
  446. }
  447. }
  448. }