Mostly eliminates usage of getD3DFormat in Texture.

TRAC #21906

Signed-off-by: Daniel Koch

Author:    Shannon Woods

git-svn-id: https://angleproject.googlecode.com/svn/branches/dx11proto@1367 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/libGLESv2/Context.cpp b/src/libGLESv2/Context.cpp
index 0c50a40..42fee08 100644
--- a/src/libGLESv2/Context.cpp
+++ b/src/libGLESv2/Context.cpp
@@ -3547,11 +3547,8 @@
         return error(GL_INVALID_OPERATION, false);
     }
 
-    if(!dx2es::ConvertReadBufferFormat(renderbuffer->getD3DFormat(), format, type))
-    {
-        ASSERT(false);
-        return false;
-    }
+    *format = gl::ExtractFormat(renderbuffer->getActualFormat()); 
+    *type = gl::ExtractType(renderbuffer->getActualFormat());
 
     return true;
 }
@@ -4071,7 +4068,7 @@
         const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
             drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
         if (!validReadType || !validDrawType ||
-            readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
+            readFramebuffer->getColorbuffer()->getActualFormat() != drawFramebuffer->getColorbuffer()->getActualFormat())
         {
             ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
             return error(GL_INVALID_OPERATION);
@@ -4099,7 +4096,7 @@
             if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
             {
                 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
-                    readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
+                    readFramebuffer->getDepthbuffer()->getActualFormat() != drawFramebuffer->getDepthbuffer()->getActualFormat())
                 {
                     return error(GL_INVALID_OPERATION);
                 }
@@ -4115,7 +4112,7 @@
             if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
             {
                 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
-                    readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
+                    readFramebuffer->getStencilbuffer()->getActualFormat() != drawFramebuffer->getStencilbuffer()->getActualFormat())
                 {
                     return error(GL_INVALID_OPERATION);
                 }
diff --git a/src/libGLESv2/Renderbuffer.cpp b/src/libGLESv2/Renderbuffer.cpp
index b844d8a..d30dfa9 100644
--- a/src/libGLESv2/Renderbuffer.cpp
+++ b/src/libGLESv2/Renderbuffer.cpp
@@ -35,32 +35,32 @@
 
 GLuint RenderbufferInterface::getRedSize() const
 {
-    return dx2es::GetRedSize(getD3DFormat());
+    return gl::GetRedSize(getActualFormat());
 }
 
 GLuint RenderbufferInterface::getGreenSize() const
 {
-    return dx2es::GetGreenSize(getD3DFormat());
+    return gl::GetGreenSize(getActualFormat());
 }
 
 GLuint RenderbufferInterface::getBlueSize() const
 {
-    return dx2es::GetBlueSize(getD3DFormat());
+    return gl::GetBlueSize(getActualFormat());
 }
 
 GLuint RenderbufferInterface::getAlphaSize() const
 {
-    return dx2es::GetAlphaSize(getD3DFormat());
+    return gl::GetAlphaSize(getActualFormat());
 }
 
 GLuint RenderbufferInterface::getDepthSize() const
 {
-    return dx2es::GetDepthSize(getD3DFormat());
+    return gl::GetDepthSize(getActualFormat());
 }
 
 GLuint RenderbufferInterface::getStencilSize() const
 {
-    return dx2es::GetStencilSize(getD3DFormat());
+    return gl::GetStencilSize(getActualFormat());
 }
 
 ///// RenderbufferTexture2D Implementation ////////
@@ -116,9 +116,9 @@
     return mTexture2D->getInternalFormat(0);
 }
 
-D3DFORMAT RenderbufferTexture2D::getD3DFormat() const
+GLenum RenderbufferTexture2D::getActualFormat() const
 {
-    return mTexture2D->getD3DFormat(0);
+    return mTexture2D->getActualFormat(0);
 }
 
 GLsizei RenderbufferTexture2D::getSamples() const
@@ -184,9 +184,9 @@
     return mTextureCubeMap->getInternalFormat(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0);
 }
 
-D3DFORMAT RenderbufferTextureCubeMap::getD3DFormat() const
+GLenum RenderbufferTextureCubeMap::getActualFormat() const
 {
-    return mTextureCubeMap->getD3DFormat(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0);
+    return mTextureCubeMap->getActualFormat(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0);
 }
 
 GLsizei RenderbufferTextureCubeMap::getSamples() const
@@ -257,9 +257,9 @@
     return mInstance->getInternalFormat();
 }
 
-D3DFORMAT Renderbuffer::getD3DFormat() const
+GLenum Renderbuffer::getActualFormat() const
 {
-    return mInstance->getD3DFormat();
+    return mInstance->getActualFormat();
 }
 
 GLuint Renderbuffer::getRedSize() const
@@ -316,6 +316,7 @@
     mHeight = 0;
     mInternalFormat = GL_RGBA4;
     mD3DFormat = D3DFMT_A8R8G8B8;
+    mActualFormat = GL_RGBA8_OES;
     mSamples = 0;
 }
 
@@ -352,9 +353,9 @@
     return mInternalFormat;
 }
 
-D3DFORMAT RenderbufferStorage::getD3DFormat() const
+GLenum RenderbufferStorage::getActualFormat() const
 {
-    return mD3DFormat;
+    return mActualFormat;
 }
 
 GLsizei RenderbufferStorage::getSamples() const
@@ -391,6 +392,7 @@
         mHeight = description.Height;
         mInternalFormat = dx2es::ConvertBackBufferFormat(description.Format);
         mD3DFormat = description.Format;
+        mActualFormat = dx2es::GetEquivalentFormat(mD3DFormat);
         mSamples = dx2es::GetSamplesFromMultisampleType(description.MultiSampleType);
     }
 }
@@ -429,6 +431,7 @@
     mHeight = height;
     mInternalFormat = format;
     mD3DFormat = requestedFormat;
+    mActualFormat = dx2es::GetEquivalentFormat(mD3DFormat);
     mSamples = supportedSamples;
 }
 
@@ -465,6 +468,7 @@
         mInternalFormat = dx2es::ConvertDepthStencilFormat(description.Format);
         mSamples = dx2es::GetSamplesFromMultisampleType(description.MultiSampleType); 
         mD3DFormat = description.Format;
+        mActualFormat = dx2es::GetEquivalentFormat(mD3DFormat);
     }
 }
 
@@ -503,6 +507,7 @@
     mHeight = height;
     mInternalFormat = GL_DEPTH24_STENCIL8_OES;
     mD3DFormat = D3DFMT_D24S8;
+    mActualFormat = GL_DEPTH24_STENCIL8_OES;
     mSamples = supportedSamples;
 }
 
diff --git a/src/libGLESv2/Renderbuffer.h b/src/libGLESv2/Renderbuffer.h
index 14ba3db..1043347 100644
--- a/src/libGLESv2/Renderbuffer.h
+++ b/src/libGLESv2/Renderbuffer.h
@@ -45,7 +45,7 @@
     virtual GLsizei getWidth() const = 0;
     virtual GLsizei getHeight() const = 0;
     virtual GLenum getInternalFormat() const = 0;
-    virtual D3DFORMAT getD3DFormat() const = 0;
+    virtual GLenum getActualFormat() const = 0;
     virtual GLsizei getSamples() const = 0;
 
     GLuint getRedSize() const;
@@ -77,7 +77,7 @@
     virtual GLsizei getWidth() const;
     virtual GLsizei getHeight() const;
     virtual GLenum getInternalFormat() const;
-    virtual D3DFORMAT getD3DFormat() const;
+    virtual GLenum getActualFormat() const;
     virtual GLsizei getSamples() const;
 
     virtual unsigned int getSerial() const;
@@ -105,7 +105,7 @@
     virtual GLsizei getWidth() const;
     virtual GLsizei getHeight() const;
     virtual GLenum getInternalFormat() const;
-    virtual D3DFORMAT getD3DFormat() const;
+    virtual GLenum getActualFormat() const;
     virtual GLsizei getSamples() const;
 
     virtual unsigned int getSerial() const;
@@ -133,7 +133,7 @@
     virtual GLsizei getWidth() const;
     virtual GLsizei getHeight() const;
     virtual GLenum getInternalFormat() const;
-    virtual D3DFORMAT getD3DFormat() const;
+    virtual GLenum getActualFormat() const;
     virtual GLsizei getSamples() const;
 
     virtual unsigned int getSerial() const;
@@ -145,6 +145,7 @@
     GLsizei mWidth;
     GLsizei mHeight;
     GLenum mInternalFormat;
+    GLenum mActualFormat;
     D3DFORMAT mD3DFormat;
     GLsizei mSamples;
 
@@ -179,7 +180,7 @@
     GLsizei getWidth() const;
     GLsizei getHeight() const;
     GLenum getInternalFormat() const;
-    D3DFORMAT getD3DFormat() const;
+    GLenum getActualFormat() const;
     GLuint getRedSize() const;
     GLuint getGreenSize() const;
     GLuint getBlueSize() const;
diff --git a/src/libGLESv2/Texture.cpp b/src/libGLESv2/Texture.cpp
index 66718cb..3aea280 100644
--- a/src/libGLESv2/Texture.cpp
+++ b/src/libGLESv2/Texture.cpp
@@ -708,10 +708,10 @@
         return GL_NONE;
 }
 
-D3DFORMAT Texture2D::getD3DFormat(GLint level) const
+GLenum Texture2D::getActualFormat(GLint level) const
 {
     if (level < IMPLEMENTATION_MAX_TEXTURE_LEVELS)
-        return mImageArray[level].getD3DFormat();
+        return mImageArray[level].getActualFormat();
     else
         return D3DFMT_UNKNOWN;
 }
@@ -1360,10 +1360,10 @@
         return GL_NONE;
 }
 
-D3DFORMAT TextureCubeMap::getD3DFormat(GLenum target, GLint level) const
+GLenum TextureCubeMap::getActualFormat(GLenum target, GLint level) const
 {
     if (level < IMPLEMENTATION_MAX_TEXTURE_LEVELS)
-        return mImageArray[faceIndex(target)][level].getD3DFormat();
+        return mImageArray[faceIndex(target)][level].getActualFormat();
     else
         return D3DFMT_UNKNOWN;
 }
diff --git a/src/libGLESv2/Texture.h b/src/libGLESv2/Texture.h
index 293d5e2..5db0033 100644
--- a/src/libGLESv2/Texture.h
+++ b/src/libGLESv2/Texture.h
@@ -154,7 +154,7 @@
     GLsizei getWidth(GLint level) const;
     GLsizei getHeight(GLint level) const;
     GLenum getInternalFormat(GLint level) const;
-    D3DFORMAT getD3DFormat(GLint level) const;
+    GLenum getActualFormat(GLint level) const;
     bool isCompressed(GLint level) const;
     bool isDepth(GLint level) const;
 
@@ -222,7 +222,7 @@
     GLsizei getWidth(GLenum target, GLint level) const;
     GLsizei getHeight(GLenum target, GLint level) const;
     GLenum getInternalFormat(GLenum target, GLint level) const;
-    D3DFORMAT getD3DFormat(GLenum target, GLint level) const;
+    GLenum getActualFormat(GLenum target, GLint level) const;
     bool isCompressed(GLenum target, GLint level) const;
 
     void setImagePosX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
diff --git a/src/libGLESv2/renderer/Image.cpp b/src/libGLESv2/renderer/Image.cpp
index a0ebd32..0e78248 100644
--- a/src/libGLESv2/renderer/Image.cpp
+++ b/src/libGLESv2/renderer/Image.cpp
@@ -30,6 +30,7 @@
 
     mD3DPool = D3DPOOL_SYSTEMMEM;
     mD3DFormat = D3DFMT_UNKNOWN;
+    mActualFormat = GL_NONE;
 }
 
 Image::~Image()
@@ -80,6 +81,7 @@
         mInternalFormat = internalformat;
         // compute the d3d format that will be used
         mD3DFormat = Texture::ConvertTextureInternalFormat(internalformat);
+        mActualFormat = dx2es::GetEquivalentFormat(mD3DFormat);
 
         if (mSurface)
         {
@@ -165,6 +167,11 @@
     return Texture::IsTextureFormatRenderable(getD3DFormat());
 }
 
+GLenum Image::getActualFormat() const
+{
+    return mActualFormat;
+}
+
 D3DFORMAT Image::getD3DFormat() const
 {
     // this should only happen if the image hasn't been redefined first
diff --git a/src/libGLESv2/renderer/Image.h b/src/libGLESv2/renderer/Image.h
index 8f25913..a39f602 100644
--- a/src/libGLESv2/renderer/Image.h
+++ b/src/libGLESv2/renderer/Image.h
@@ -32,6 +32,7 @@
 
     bool isRenderableFormat() const;
     D3DFORMAT getD3DFormat() const;
+    GLenum getActualFormat() const;
 
     GLsizei getWidth() const {return mWidth;}
     GLsizei getHeight() const {return mHeight;}
@@ -108,6 +109,7 @@
 
     D3DPOOL mD3DPool;   // can only be D3DPOOL_SYSTEMMEM or D3DPOOL_MANAGED since it needs to be lockable.
     D3DFORMAT mD3DFormat;
+    GLenum mActualFormat;
 
     IDirect3DSurface9 *mSurface;
 };
diff --git a/src/libGLESv2/utilities.cpp b/src/libGLESv2/utilities.cpp
index 4651088..f0a8e83 100644
--- a/src/libGLESv2/utilities.cpp
+++ b/src/libGLESv2/utilities.cpp
@@ -1273,6 +1273,35 @@
     return GL_DEPTH24_STENCIL8_OES;
 }
 
+GLenum GetEquivalentFormat(D3DFORMAT format)
+{
+    if (format == D3DFMT_INTZ)
+        return GL_DEPTH24_STENCIL8_OES;
+    if (format == D3DFMT_NULL)
+        return GL_NONE;
+
+    switch (format)
+    {
+      case D3DFMT_A4R4G4B4:             return GL_RGBA4;
+      case D3DFMT_A8R8G8B8:             return GL_RGBA8_OES;
+      case D3DFMT_A1R5G5B5:             return GL_RGB5_A1;
+      case D3DFMT_R5G6B5:               return GL_RGB565;
+      case D3DFMT_X8R8G8B8:             return GL_RGB8_OES;
+      case D3DFMT_D16:                  return GL_DEPTH_COMPONENT16;
+      case D3DFMT_D24S8:                return GL_DEPTH24_STENCIL8_OES;
+      case D3DFMT_UNKNOWN:              return GL_NONE;
+      case D3DFMT_DXT1:                 return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
+      case D3DFMT_DXT3:                 return GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
+      case D3DFMT_DXT5:                 return GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
+      case D3DFMT_A32B32G32R32F:        return GL_RGBA32F_EXT;
+      case D3DFMT_A16B16G16R16F:        return GL_RGBA16F_EXT;
+      case D3DFMT_L8:                   return GL_LUMINANCE8_EXT;
+      case D3DFMT_A8L8:                 return GL_LUMINANCE8_ALPHA8_EXT;
+      default:              UNREACHABLE();
+        return GL_NONE;
+    }
+}
+
 }
 
 namespace dx
diff --git a/src/libGLESv2/utilities.h b/src/libGLESv2/utilities.h
index 167cd70..e50bb5d 100644
--- a/src/libGLESv2/utilities.h
+++ b/src/libGLESv2/utilities.h
@@ -100,6 +100,7 @@
 bool ConvertReadBufferFormat(D3DFORMAT d3dformat, GLenum *format, GLenum *type);
 GLenum ConvertBackBufferFormat(D3DFORMAT format);
 GLenum ConvertDepthStencilFormat(D3DFORMAT format);
+GLenum GetEquivalentFormat(D3DFORMAT format);
 
 }