michael@0: #include "precompiled.h" michael@0: // michael@0: // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: // renderer9_utils.cpp: Conversion functions and other utility routines michael@0: // specific to the D3D9 renderer. michael@0: michael@0: #include "libGLESv2/renderer/renderer9_utils.h" michael@0: #include "libGLESv2/mathutil.h" michael@0: #include "libGLESv2/Context.h" michael@0: michael@0: #include "common/debug.h" michael@0: michael@0: namespace gl_d3d9 michael@0: { michael@0: michael@0: D3DCMPFUNC ConvertComparison(GLenum comparison) michael@0: { michael@0: D3DCMPFUNC d3dComp = D3DCMP_ALWAYS; michael@0: switch (comparison) michael@0: { michael@0: case GL_NEVER: d3dComp = D3DCMP_NEVER; break; michael@0: case GL_ALWAYS: d3dComp = D3DCMP_ALWAYS; break; michael@0: case GL_LESS: d3dComp = D3DCMP_LESS; break; michael@0: case GL_LEQUAL: d3dComp = D3DCMP_LESSEQUAL; break; michael@0: case GL_EQUAL: d3dComp = D3DCMP_EQUAL; break; michael@0: case GL_GREATER: d3dComp = D3DCMP_GREATER; break; michael@0: case GL_GEQUAL: d3dComp = D3DCMP_GREATEREQUAL; break; michael@0: case GL_NOTEQUAL: d3dComp = D3DCMP_NOTEQUAL; break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: michael@0: return d3dComp; michael@0: } michael@0: michael@0: D3DCOLOR ConvertColor(gl::Color color) michael@0: { michael@0: return D3DCOLOR_RGBA(gl::unorm<8>(color.red), michael@0: gl::unorm<8>(color.green), michael@0: gl::unorm<8>(color.blue), michael@0: gl::unorm<8>(color.alpha)); michael@0: } michael@0: michael@0: D3DBLEND ConvertBlendFunc(GLenum blend) michael@0: { michael@0: D3DBLEND d3dBlend = D3DBLEND_ZERO; michael@0: michael@0: switch (blend) michael@0: { michael@0: case GL_ZERO: d3dBlend = D3DBLEND_ZERO; break; michael@0: case GL_ONE: d3dBlend = D3DBLEND_ONE; break; michael@0: case GL_SRC_COLOR: d3dBlend = D3DBLEND_SRCCOLOR; break; michael@0: case GL_ONE_MINUS_SRC_COLOR: d3dBlend = D3DBLEND_INVSRCCOLOR; break; michael@0: case GL_DST_COLOR: d3dBlend = D3DBLEND_DESTCOLOR; break; michael@0: case GL_ONE_MINUS_DST_COLOR: d3dBlend = D3DBLEND_INVDESTCOLOR; break; michael@0: case GL_SRC_ALPHA: d3dBlend = D3DBLEND_SRCALPHA; break; michael@0: case GL_ONE_MINUS_SRC_ALPHA: d3dBlend = D3DBLEND_INVSRCALPHA; break; michael@0: case GL_DST_ALPHA: d3dBlend = D3DBLEND_DESTALPHA; break; michael@0: case GL_ONE_MINUS_DST_ALPHA: d3dBlend = D3DBLEND_INVDESTALPHA; break; michael@0: case GL_CONSTANT_COLOR: d3dBlend = D3DBLEND_BLENDFACTOR; break; michael@0: case GL_ONE_MINUS_CONSTANT_COLOR: d3dBlend = D3DBLEND_INVBLENDFACTOR; break; michael@0: case GL_CONSTANT_ALPHA: d3dBlend = D3DBLEND_BLENDFACTOR; break; michael@0: case GL_ONE_MINUS_CONSTANT_ALPHA: d3dBlend = D3DBLEND_INVBLENDFACTOR; break; michael@0: case GL_SRC_ALPHA_SATURATE: d3dBlend = D3DBLEND_SRCALPHASAT; break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: michael@0: return d3dBlend; michael@0: } michael@0: michael@0: D3DBLENDOP ConvertBlendOp(GLenum blendOp) michael@0: { michael@0: D3DBLENDOP d3dBlendOp = D3DBLENDOP_ADD; michael@0: michael@0: switch (blendOp) michael@0: { michael@0: case GL_FUNC_ADD: d3dBlendOp = D3DBLENDOP_ADD; break; michael@0: case GL_FUNC_SUBTRACT: d3dBlendOp = D3DBLENDOP_SUBTRACT; break; michael@0: case GL_FUNC_REVERSE_SUBTRACT: d3dBlendOp = D3DBLENDOP_REVSUBTRACT; break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: michael@0: return d3dBlendOp; michael@0: } michael@0: michael@0: D3DSTENCILOP ConvertStencilOp(GLenum stencilOp) michael@0: { michael@0: D3DSTENCILOP d3dStencilOp = D3DSTENCILOP_KEEP; michael@0: michael@0: switch (stencilOp) michael@0: { michael@0: case GL_ZERO: d3dStencilOp = D3DSTENCILOP_ZERO; break; michael@0: case GL_KEEP: d3dStencilOp = D3DSTENCILOP_KEEP; break; michael@0: case GL_REPLACE: d3dStencilOp = D3DSTENCILOP_REPLACE; break; michael@0: case GL_INCR: d3dStencilOp = D3DSTENCILOP_INCRSAT; break; michael@0: case GL_DECR: d3dStencilOp = D3DSTENCILOP_DECRSAT; break; michael@0: case GL_INVERT: d3dStencilOp = D3DSTENCILOP_INVERT; break; michael@0: case GL_INCR_WRAP: d3dStencilOp = D3DSTENCILOP_INCR; break; michael@0: case GL_DECR_WRAP: d3dStencilOp = D3DSTENCILOP_DECR; break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: michael@0: return d3dStencilOp; michael@0: } michael@0: michael@0: D3DTEXTUREADDRESS ConvertTextureWrap(GLenum wrap) michael@0: { michael@0: D3DTEXTUREADDRESS d3dWrap = D3DTADDRESS_WRAP; michael@0: michael@0: switch (wrap) michael@0: { michael@0: case GL_REPEAT: d3dWrap = D3DTADDRESS_WRAP; break; michael@0: case GL_CLAMP_TO_EDGE: d3dWrap = D3DTADDRESS_CLAMP; break; michael@0: case GL_MIRRORED_REPEAT: d3dWrap = D3DTADDRESS_MIRROR; break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: michael@0: return d3dWrap; michael@0: } michael@0: michael@0: D3DCULL ConvertCullMode(GLenum cullFace, GLenum frontFace) michael@0: { michael@0: D3DCULL cull = D3DCULL_CCW; michael@0: switch (cullFace) michael@0: { michael@0: case GL_FRONT: michael@0: cull = (frontFace == GL_CCW ? D3DCULL_CW : D3DCULL_CCW); michael@0: break; michael@0: case GL_BACK: michael@0: cull = (frontFace == GL_CCW ? D3DCULL_CCW : D3DCULL_CW); michael@0: break; michael@0: case GL_FRONT_AND_BACK: michael@0: cull = D3DCULL_NONE; // culling will be handled during draw michael@0: break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: michael@0: return cull; michael@0: } michael@0: michael@0: D3DCUBEMAP_FACES ConvertCubeFace(GLenum cubeFace) michael@0: { michael@0: D3DCUBEMAP_FACES face = D3DCUBEMAP_FACE_POSITIVE_X; michael@0: michael@0: switch (cubeFace) michael@0: { michael@0: case GL_TEXTURE_CUBE_MAP_POSITIVE_X: michael@0: face = D3DCUBEMAP_FACE_POSITIVE_X; michael@0: break; michael@0: case GL_TEXTURE_CUBE_MAP_NEGATIVE_X: michael@0: face = D3DCUBEMAP_FACE_NEGATIVE_X; michael@0: break; michael@0: case GL_TEXTURE_CUBE_MAP_POSITIVE_Y: michael@0: face = D3DCUBEMAP_FACE_POSITIVE_Y; michael@0: break; michael@0: case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y: michael@0: face = D3DCUBEMAP_FACE_NEGATIVE_Y; michael@0: break; michael@0: case GL_TEXTURE_CUBE_MAP_POSITIVE_Z: michael@0: face = D3DCUBEMAP_FACE_POSITIVE_Z; michael@0: break; michael@0: case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: michael@0: face = D3DCUBEMAP_FACE_NEGATIVE_Z; michael@0: break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: michael@0: return face; michael@0: } michael@0: michael@0: DWORD ConvertColorMask(bool red, bool green, bool blue, bool alpha) michael@0: { michael@0: return (red ? D3DCOLORWRITEENABLE_RED : 0) | michael@0: (green ? D3DCOLORWRITEENABLE_GREEN : 0) | michael@0: (blue ? D3DCOLORWRITEENABLE_BLUE : 0) | michael@0: (alpha ? D3DCOLORWRITEENABLE_ALPHA : 0); michael@0: } michael@0: michael@0: D3DTEXTUREFILTERTYPE ConvertMagFilter(GLenum magFilter, float maxAnisotropy) michael@0: { michael@0: if (maxAnisotropy > 1.0f) michael@0: { michael@0: return D3DTEXF_ANISOTROPIC; michael@0: } michael@0: michael@0: D3DTEXTUREFILTERTYPE d3dMagFilter = D3DTEXF_POINT; michael@0: switch (magFilter) michael@0: { michael@0: case GL_NEAREST: d3dMagFilter = D3DTEXF_POINT; break; michael@0: case GL_LINEAR: d3dMagFilter = D3DTEXF_LINEAR; break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: michael@0: return d3dMagFilter; michael@0: } michael@0: michael@0: void ConvertMinFilter(GLenum minFilter, D3DTEXTUREFILTERTYPE *d3dMinFilter, D3DTEXTUREFILTERTYPE *d3dMipFilter, float maxAnisotropy) michael@0: { michael@0: switch (minFilter) michael@0: { michael@0: case GL_NEAREST: michael@0: *d3dMinFilter = D3DTEXF_POINT; michael@0: *d3dMipFilter = D3DTEXF_NONE; michael@0: break; michael@0: case GL_LINEAR: michael@0: *d3dMinFilter = D3DTEXF_LINEAR; michael@0: *d3dMipFilter = D3DTEXF_NONE; michael@0: break; michael@0: case GL_NEAREST_MIPMAP_NEAREST: michael@0: *d3dMinFilter = D3DTEXF_POINT; michael@0: *d3dMipFilter = D3DTEXF_POINT; michael@0: break; michael@0: case GL_LINEAR_MIPMAP_NEAREST: michael@0: *d3dMinFilter = D3DTEXF_LINEAR; michael@0: *d3dMipFilter = D3DTEXF_POINT; michael@0: break; michael@0: case GL_NEAREST_MIPMAP_LINEAR: michael@0: *d3dMinFilter = D3DTEXF_POINT; michael@0: *d3dMipFilter = D3DTEXF_LINEAR; michael@0: break; michael@0: case GL_LINEAR_MIPMAP_LINEAR: michael@0: *d3dMinFilter = D3DTEXF_LINEAR; michael@0: *d3dMipFilter = D3DTEXF_LINEAR; michael@0: break; michael@0: default: michael@0: *d3dMinFilter = D3DTEXF_POINT; michael@0: *d3dMipFilter = D3DTEXF_NONE; michael@0: UNREACHABLE(); michael@0: } michael@0: michael@0: if (maxAnisotropy > 1.0f) michael@0: { michael@0: *d3dMinFilter = D3DTEXF_ANISOTROPIC; michael@0: } michael@0: } michael@0: michael@0: D3DFORMAT ConvertRenderbufferFormat(GLenum format) michael@0: { michael@0: switch (format) michael@0: { michael@0: case GL_NONE: return D3DFMT_NULL; michael@0: case GL_RGBA4: michael@0: case GL_RGB5_A1: michael@0: case GL_RGBA8_OES: return D3DFMT_A8R8G8B8; michael@0: case GL_RGB565: return D3DFMT_R5G6B5; michael@0: case GL_RGB8_OES: return D3DFMT_X8R8G8B8; michael@0: case GL_DEPTH_COMPONENT16: michael@0: case GL_STENCIL_INDEX8: michael@0: case GL_DEPTH24_STENCIL8_OES: return D3DFMT_D24S8; michael@0: default: UNREACHABLE(); return D3DFMT_A8R8G8B8; michael@0: } michael@0: } michael@0: michael@0: D3DMULTISAMPLE_TYPE GetMultisampleTypeFromSamples(GLsizei samples) michael@0: { michael@0: if (samples <= 1) michael@0: return D3DMULTISAMPLE_NONE; michael@0: else michael@0: return (D3DMULTISAMPLE_TYPE)samples; michael@0: } michael@0: michael@0: } michael@0: michael@0: namespace d3d9_gl michael@0: { michael@0: michael@0: unsigned int GetStencilSize(D3DFORMAT stencilFormat) michael@0: { michael@0: if (stencilFormat == D3DFMT_INTZ) michael@0: { michael@0: return 8; michael@0: } michael@0: switch(stencilFormat) michael@0: { michael@0: case D3DFMT_D24FS8: michael@0: case D3DFMT_D24S8: michael@0: return 8; michael@0: case D3DFMT_D24X4S4: michael@0: return 4; michael@0: case D3DFMT_D15S1: michael@0: return 1; michael@0: case D3DFMT_D16_LOCKABLE: michael@0: case D3DFMT_D32: michael@0: case D3DFMT_D24X8: michael@0: case D3DFMT_D32F_LOCKABLE: michael@0: case D3DFMT_D16: michael@0: return 0; michael@0: //case D3DFMT_D32_LOCKABLE: return 0; // DirectX 9Ex only michael@0: //case D3DFMT_S8_LOCKABLE: return 8; // DirectX 9Ex only michael@0: default: michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: unsigned int GetAlphaSize(D3DFORMAT colorFormat) michael@0: { michael@0: switch (colorFormat) michael@0: { michael@0: case D3DFMT_A16B16G16R16F: michael@0: return 16; michael@0: case D3DFMT_A32B32G32R32F: michael@0: return 32; michael@0: case D3DFMT_A2R10G10B10: michael@0: return 2; michael@0: case D3DFMT_A8R8G8B8: michael@0: return 8; michael@0: case D3DFMT_A1R5G5B5: michael@0: return 1; michael@0: case D3DFMT_X8R8G8B8: michael@0: case D3DFMT_R5G6B5: michael@0: return 0; michael@0: default: michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: GLsizei GetSamplesFromMultisampleType(D3DMULTISAMPLE_TYPE type) michael@0: { michael@0: if (type == D3DMULTISAMPLE_NONMASKABLE) michael@0: return 0; michael@0: else michael@0: return type; michael@0: } michael@0: michael@0: bool IsFormatChannelEquivalent(D3DFORMAT d3dformat, GLenum format) michael@0: { michael@0: switch (d3dformat) michael@0: { michael@0: case D3DFMT_L8: michael@0: return (format == GL_LUMINANCE); michael@0: case D3DFMT_A8L8: michael@0: return (format == GL_LUMINANCE_ALPHA); michael@0: case D3DFMT_DXT1: michael@0: return (format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT || format == GL_COMPRESSED_RGB_S3TC_DXT1_EXT); michael@0: case D3DFMT_DXT3: michael@0: return (format == GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE); michael@0: case D3DFMT_DXT5: michael@0: return (format == GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE); michael@0: case D3DFMT_A8R8G8B8: michael@0: case D3DFMT_A16B16G16R16F: michael@0: case D3DFMT_A32B32G32R32F: michael@0: return (format == GL_RGBA || format == GL_BGRA_EXT); michael@0: case D3DFMT_X8R8G8B8: michael@0: return (format == GL_RGB); michael@0: default: michael@0: if (d3dformat == D3DFMT_INTZ && gl::IsDepthTexture(format)) michael@0: return true; michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: GLenum ConvertBackBufferFormat(D3DFORMAT format) michael@0: { michael@0: switch (format) michael@0: { michael@0: case D3DFMT_A4R4G4B4: return GL_RGBA4; michael@0: case D3DFMT_A8R8G8B8: return GL_RGBA8_OES; michael@0: case D3DFMT_A1R5G5B5: return GL_RGB5_A1; michael@0: case D3DFMT_R5G6B5: return GL_RGB565; michael@0: case D3DFMT_X8R8G8B8: return GL_RGB8_OES; michael@0: default: michael@0: UNREACHABLE(); michael@0: } michael@0: michael@0: return GL_RGBA4; michael@0: } michael@0: michael@0: GLenum ConvertDepthStencilFormat(D3DFORMAT format) michael@0: { michael@0: if (format == D3DFMT_INTZ) michael@0: { michael@0: return GL_DEPTH24_STENCIL8_OES; michael@0: } michael@0: switch (format) michael@0: { michael@0: case D3DFMT_D16: michael@0: case D3DFMT_D24X8: michael@0: return GL_DEPTH_COMPONENT16; michael@0: case D3DFMT_D24S8: michael@0: return GL_DEPTH24_STENCIL8_OES; michael@0: case D3DFMT_UNKNOWN: michael@0: return GL_NONE; michael@0: default: michael@0: UNREACHABLE(); michael@0: } michael@0: michael@0: return GL_DEPTH24_STENCIL8_OES; michael@0: } michael@0: michael@0: GLenum ConvertRenderTargetFormat(D3DFORMAT format) michael@0: { michael@0: if (format == D3DFMT_INTZ) michael@0: { michael@0: return GL_DEPTH24_STENCIL8_OES; michael@0: } michael@0: michael@0: switch (format) michael@0: { michael@0: case D3DFMT_A4R4G4B4: return GL_RGBA4; michael@0: case D3DFMT_A8R8G8B8: return GL_RGBA8_OES; michael@0: case D3DFMT_A1R5G5B5: return GL_RGB5_A1; michael@0: case D3DFMT_R5G6B5: return GL_RGB565; michael@0: case D3DFMT_X8R8G8B8: return GL_RGB8_OES; michael@0: case D3DFMT_D16: michael@0: case D3DFMT_D24X8: michael@0: return GL_DEPTH_COMPONENT16; michael@0: case D3DFMT_D24S8: michael@0: return GL_DEPTH24_STENCIL8_OES; michael@0: case D3DFMT_UNKNOWN: michael@0: return GL_NONE; michael@0: default: michael@0: UNREACHABLE(); michael@0: } michael@0: michael@0: return GL_RGBA4; michael@0: } michael@0: michael@0: GLenum GetEquivalentFormat(D3DFORMAT format) michael@0: { michael@0: if (format == D3DFMT_INTZ) michael@0: return GL_DEPTH24_STENCIL8_OES; michael@0: if (format == D3DFMT_NULL) michael@0: return GL_NONE; michael@0: michael@0: switch (format) michael@0: { michael@0: case D3DFMT_A4R4G4B4: return GL_RGBA4; michael@0: case D3DFMT_A8R8G8B8: return GL_RGBA8_OES; michael@0: case D3DFMT_A1R5G5B5: return GL_RGB5_A1; michael@0: case D3DFMT_R5G6B5: return GL_RGB565; michael@0: case D3DFMT_X8R8G8B8: return GL_RGB8_OES; michael@0: case D3DFMT_D16: return GL_DEPTH_COMPONENT16; michael@0: case D3DFMT_D24S8: return GL_DEPTH24_STENCIL8_OES; michael@0: case D3DFMT_UNKNOWN: return GL_NONE; michael@0: case D3DFMT_DXT1: return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; michael@0: case D3DFMT_DXT3: return GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE; michael@0: case D3DFMT_DXT5: return GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE; michael@0: case D3DFMT_A32B32G32R32F: return GL_RGBA32F_EXT; michael@0: case D3DFMT_A16B16G16R16F: return GL_RGBA16F_EXT; michael@0: case D3DFMT_L8: return GL_LUMINANCE8_EXT; michael@0: case D3DFMT_A8L8: return GL_LUMINANCE8_ALPHA8_EXT; michael@0: default: UNREACHABLE(); michael@0: return GL_NONE; michael@0: } michael@0: } michael@0: michael@0: } michael@0: michael@0: namespace d3d9 michael@0: { michael@0: michael@0: bool IsCompressedFormat(D3DFORMAT surfaceFormat) michael@0: { michael@0: switch(surfaceFormat) michael@0: { michael@0: case D3DFMT_DXT1: michael@0: case D3DFMT_DXT2: michael@0: case D3DFMT_DXT3: michael@0: case D3DFMT_DXT4: michael@0: case D3DFMT_DXT5: michael@0: return true; michael@0: default: michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: size_t ComputeRowSize(D3DFORMAT format, unsigned int width) michael@0: { michael@0: if (format == D3DFMT_INTZ) michael@0: { michael@0: return 4 * width; michael@0: } michael@0: switch (format) michael@0: { michael@0: case D3DFMT_L8: michael@0: return 1 * width; michael@0: case D3DFMT_A8L8: michael@0: return 2 * width; michael@0: case D3DFMT_X8R8G8B8: michael@0: case D3DFMT_A8R8G8B8: michael@0: return 4 * width; michael@0: case D3DFMT_A16B16G16R16F: michael@0: return 8 * width; michael@0: case D3DFMT_A32B32G32R32F: michael@0: return 16 * width; michael@0: case D3DFMT_DXT1: michael@0: return 8 * ((width + 3) / 4); michael@0: case D3DFMT_DXT3: michael@0: case D3DFMT_DXT5: michael@0: return 16 * ((width + 3) / 4); michael@0: default: michael@0: UNREACHABLE(); michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: }