daniel@transgaming.com | b5e1a27 | 2012-10-31 19:10:00 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // TextureStorage.cpp: Implements the abstract gl::TextureStorage class and its concrete derived |
| 8 | // classes TextureStorage2D and TextureStorageCubeMap, which act as the interface to the |
| 9 | // D3D-side texture. |
| 10 | |
| 11 | #include "libGLESv2/main.h" |
| 12 | #include "libGLESv2/renderer/TextureStorage.h" |
| 13 | |
| 14 | #include "common/debug.h" |
| 15 | |
| 16 | namespace gl |
| 17 | { |
| 18 | unsigned int TextureStorage::mCurrentTextureSerial = 1; |
| 19 | |
| 20 | TextureStorage::TextureStorage(DWORD usage) |
| 21 | : mD3DUsage(usage), |
| 22 | mD3DPool(getDisplay()->getRenderer()->getTexturePool(usage)), // D3D9_REPLACE |
| 23 | mTextureSerial(issueTextureSerial()), |
| 24 | mLodOffset(0) |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | TextureStorage::~TextureStorage() |
| 29 | { |
| 30 | } |
| 31 | |
daniel@transgaming.com | df14c76 | 2012-10-31 19:51:48 +0000 | [diff] [blame^] | 32 | DWORD TextureStorage::GetTextureUsage(D3DFORMAT d3dfmt, GLenum glusage, bool forceRenderable) |
| 33 | { |
| 34 | DWORD d3dusage = 0; |
| 35 | |
| 36 | if (d3dfmt == D3DFMT_INTZ) |
| 37 | { |
| 38 | d3dusage |= D3DUSAGE_DEPTHSTENCIL; |
| 39 | } |
| 40 | else if(forceRenderable || (TextureStorage::IsTextureFormatRenderable(d3dfmt) && (glusage == GL_FRAMEBUFFER_ATTACHMENT_ANGLE))) |
| 41 | { |
| 42 | d3dusage |= D3DUSAGE_RENDERTARGET; |
| 43 | } |
| 44 | return d3dusage; |
| 45 | } |
| 46 | |
| 47 | bool TextureStorage::IsTextureFormatRenderable(D3DFORMAT format) |
| 48 | { |
| 49 | if (format == D3DFMT_INTZ) |
| 50 | { |
| 51 | return true; |
| 52 | } |
| 53 | switch(format) |
| 54 | { |
| 55 | case D3DFMT_L8: |
| 56 | case D3DFMT_A8L8: |
| 57 | case D3DFMT_DXT1: |
| 58 | case D3DFMT_DXT3: |
| 59 | case D3DFMT_DXT5: |
| 60 | return false; |
| 61 | case D3DFMT_A8R8G8B8: |
| 62 | case D3DFMT_X8R8G8B8: |
| 63 | case D3DFMT_A16B16G16R16F: |
| 64 | case D3DFMT_A32B32G32R32F: |
| 65 | return true; |
| 66 | default: |
| 67 | UNREACHABLE(); |
| 68 | } |
| 69 | |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | D3DFORMAT TextureStorage::ConvertTextureInternalFormat(GLint internalformat) |
| 74 | { |
| 75 | switch (internalformat) |
| 76 | { |
| 77 | case GL_DEPTH_COMPONENT16: |
| 78 | case GL_DEPTH_COMPONENT32_OES: |
| 79 | case GL_DEPTH24_STENCIL8_OES: |
| 80 | return D3DFMT_INTZ; |
| 81 | case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: |
| 82 | case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: |
| 83 | return D3DFMT_DXT1; |
| 84 | case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE: |
| 85 | return D3DFMT_DXT3; |
| 86 | case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE: |
| 87 | return D3DFMT_DXT5; |
| 88 | case GL_RGBA32F_EXT: |
| 89 | case GL_RGB32F_EXT: |
| 90 | case GL_ALPHA32F_EXT: |
| 91 | case GL_LUMINANCE32F_EXT: |
| 92 | case GL_LUMINANCE_ALPHA32F_EXT: |
| 93 | return D3DFMT_A32B32G32R32F; |
| 94 | case GL_RGBA16F_EXT: |
| 95 | case GL_RGB16F_EXT: |
| 96 | case GL_ALPHA16F_EXT: |
| 97 | case GL_LUMINANCE16F_EXT: |
| 98 | case GL_LUMINANCE_ALPHA16F_EXT: |
| 99 | return D3DFMT_A16B16G16R16F; |
| 100 | case GL_LUMINANCE8_EXT: |
| 101 | if (getContext()->supportsLuminanceTextures()) |
| 102 | { |
| 103 | return D3DFMT_L8; |
| 104 | } |
| 105 | break; |
| 106 | case GL_LUMINANCE8_ALPHA8_EXT: |
| 107 | if (getContext()->supportsLuminanceAlphaTextures()) |
| 108 | { |
| 109 | return D3DFMT_A8L8; |
| 110 | } |
| 111 | break; |
| 112 | case GL_RGB8_OES: |
| 113 | case GL_RGB565: |
| 114 | return D3DFMT_X8R8G8B8; |
| 115 | } |
| 116 | |
| 117 | return D3DFMT_A8R8G8B8; |
| 118 | } |
| 119 | |
daniel@transgaming.com | b5e1a27 | 2012-10-31 19:10:00 +0000 | [diff] [blame] | 120 | bool TextureStorage::isRenderTarget() const |
| 121 | { |
| 122 | return (mD3DUsage & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL)) != 0; |
| 123 | } |
| 124 | |
| 125 | bool TextureStorage::isManaged() const |
| 126 | { |
| 127 | return (mD3DPool == D3DPOOL_MANAGED); |
| 128 | } |
| 129 | |
| 130 | D3DPOOL TextureStorage::getPool() const |
| 131 | { |
| 132 | return mD3DPool; |
| 133 | } |
| 134 | |
| 135 | DWORD TextureStorage::getUsage() const |
| 136 | { |
| 137 | return mD3DUsage; |
| 138 | } |
| 139 | |
| 140 | unsigned int TextureStorage::getTextureSerial() const |
| 141 | { |
| 142 | return mTextureSerial; |
| 143 | } |
| 144 | |
| 145 | unsigned int TextureStorage::issueTextureSerial() |
| 146 | { |
| 147 | return mCurrentTextureSerial++; |
| 148 | } |
| 149 | |
| 150 | int TextureStorage::getLodOffset() const |
| 151 | { |
| 152 | return mLodOffset; |
| 153 | } |
| 154 | |
| 155 | TextureStorage2D::TextureStorage2D(IDirect3DTexture9 *surfaceTexture) : TextureStorage(D3DUSAGE_RENDERTARGET), mRenderTargetSerial(RenderbufferStorage::issueSerial()) |
| 156 | { |
| 157 | mTexture = surfaceTexture; |
| 158 | } |
| 159 | |
| 160 | TextureStorage2D::TextureStorage2D(int levels, D3DFORMAT format, DWORD usage, int width, int height) |
| 161 | : TextureStorage(usage), mRenderTargetSerial(RenderbufferStorage::issueSerial()) |
| 162 | { |
| 163 | mTexture = NULL; |
| 164 | // if the width or height is not positive this should be treated as an incomplete texture |
| 165 | // we handle that here by skipping the d3d texture creation |
| 166 | if (width > 0 && height > 0) |
| 167 | { |
| 168 | IDirect3DDevice9 *device = getDisplay()->getRenderer()->getDevice(); // D3D9_REPLACE |
| 169 | MakeValidSize(false, dx::IsCompressedFormat(format), &width, &height, &mLodOffset); |
| 170 | HRESULT result = device->CreateTexture(width, height, levels ? levels + mLodOffset : 0, getUsage(), format, getPool(), &mTexture, NULL); |
| 171 | |
| 172 | if (FAILED(result)) |
| 173 | { |
| 174 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 175 | error(GL_OUT_OF_MEMORY); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | TextureStorage2D::~TextureStorage2D() |
| 181 | { |
| 182 | if (mTexture) |
| 183 | { |
| 184 | mTexture->Release(); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Increments refcount on surface. |
| 189 | // caller must Release() the returned surface |
| 190 | IDirect3DSurface9 *TextureStorage2D::getSurfaceLevel(int level, bool dirty) |
| 191 | { |
| 192 | IDirect3DSurface9 *surface = NULL; |
| 193 | |
| 194 | if (mTexture) |
| 195 | { |
| 196 | HRESULT result = mTexture->GetSurfaceLevel(level + mLodOffset, &surface); |
| 197 | ASSERT(SUCCEEDED(result)); |
| 198 | |
| 199 | // With managed textures the driver needs to be informed of updates to the lower mipmap levels |
| 200 | if (level != 0 && isManaged() && dirty) |
| 201 | { |
| 202 | mTexture->AddDirtyRect(NULL); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | return surface; |
| 207 | } |
| 208 | |
| 209 | IDirect3DBaseTexture9 *TextureStorage2D::getBaseTexture() const |
| 210 | { |
| 211 | return mTexture; |
| 212 | } |
| 213 | |
| 214 | unsigned int TextureStorage2D::getRenderTargetSerial(GLenum target) const |
| 215 | { |
| 216 | return mRenderTargetSerial; |
| 217 | } |
| 218 | |
| 219 | TextureStorageCubeMap::TextureStorageCubeMap(int levels, D3DFORMAT format, DWORD usage, int size) |
| 220 | : TextureStorage(usage), mFirstRenderTargetSerial(RenderbufferStorage::issueCubeSerials()) |
| 221 | { |
| 222 | mTexture = NULL; |
| 223 | // if the size is not positive this should be treated as an incomplete texture |
| 224 | // we handle that here by skipping the d3d texture creation |
| 225 | if (size > 0) |
| 226 | { |
| 227 | IDirect3DDevice9 *device = getDisplay()->getRenderer()->getDevice(); // D3D9_REPLACE |
| 228 | int height = size; |
| 229 | MakeValidSize(false, dx::IsCompressedFormat(format), &size, &height, &mLodOffset); |
| 230 | HRESULT result = device->CreateCubeTexture(size, levels ? levels + mLodOffset : 0, getUsage(), format, getPool(), &mTexture, NULL); |
| 231 | |
| 232 | if (FAILED(result)) |
| 233 | { |
| 234 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 235 | error(GL_OUT_OF_MEMORY); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | TextureStorageCubeMap::~TextureStorageCubeMap() |
| 241 | { |
| 242 | if (mTexture) |
| 243 | { |
| 244 | mTexture->Release(); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // Increments refcount on surface. |
| 249 | // caller must Release() the returned surface |
| 250 | IDirect3DSurface9 *TextureStorageCubeMap::getCubeMapSurface(GLenum faceTarget, int level, bool dirty) |
| 251 | { |
| 252 | IDirect3DSurface9 *surface = NULL; |
| 253 | |
| 254 | if (mTexture) |
| 255 | { |
| 256 | D3DCUBEMAP_FACES face = es2dx::ConvertCubeFace(faceTarget); |
| 257 | HRESULT result = mTexture->GetCubeMapSurface(face, level + mLodOffset, &surface); |
| 258 | ASSERT(SUCCEEDED(result)); |
| 259 | |
| 260 | // With managed textures the driver needs to be informed of updates to the lower mipmap levels |
| 261 | if (level != 0 && isManaged() && dirty) |
| 262 | { |
| 263 | mTexture->AddDirtyRect(face, NULL); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | return surface; |
| 268 | } |
| 269 | |
| 270 | IDirect3DBaseTexture9 *TextureStorageCubeMap::getBaseTexture() const |
| 271 | { |
| 272 | return mTexture; |
| 273 | } |
| 274 | |
| 275 | unsigned int TextureStorageCubeMap::getRenderTargetSerial(GLenum target) const |
| 276 | { |
| 277 | return mFirstRenderTargetSerial + TextureCubeMap::faceIndex(target); |
| 278 | } |
| 279 | |
| 280 | } |