michael@0: #include "precompiled.h" michael@0: // michael@0: // Copyright (c) 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: // TextureStorage9.cpp: Implements the abstract rx::TextureStorage9 class and its concrete derived michael@0: // classes TextureStorage9_2D and TextureStorage9_Cube, which act as the interface to the michael@0: // D3D9 texture. michael@0: michael@0: #include "libGLESv2/main.h" michael@0: #include "libGLESv2/renderer/Renderer9.h" michael@0: #include "libGLESv2/renderer/TextureStorage9.h" michael@0: #include "libGLESv2/renderer/SwapChain9.h" michael@0: #include "libGLESv2/renderer/RenderTarget9.h" michael@0: #include "libGLESv2/renderer/renderer9_utils.h" michael@0: #include "libGLESv2/Texture.h" michael@0: michael@0: namespace rx michael@0: { michael@0: TextureStorage9::TextureStorage9(Renderer *renderer, DWORD usage) michael@0: : mLodOffset(0), michael@0: mRenderer(Renderer9::makeRenderer9(renderer)), michael@0: mD3DUsage(usage), michael@0: mD3DPool(mRenderer->getTexturePool(usage)) michael@0: { michael@0: } michael@0: michael@0: TextureStorage9::~TextureStorage9() michael@0: { michael@0: } michael@0: michael@0: TextureStorage9 *TextureStorage9::makeTextureStorage9(TextureStorage *storage) michael@0: { michael@0: ASSERT(HAS_DYNAMIC_TYPE(TextureStorage9*, storage)); michael@0: return static_cast(storage); michael@0: } michael@0: michael@0: DWORD TextureStorage9::GetTextureUsage(D3DFORMAT d3dfmt, GLenum glusage, bool forceRenderable) michael@0: { michael@0: DWORD d3dusage = 0; michael@0: michael@0: if (d3dfmt == D3DFMT_INTZ) michael@0: { michael@0: d3dusage |= D3DUSAGE_DEPTHSTENCIL; michael@0: } michael@0: else if(forceRenderable || (TextureStorage9::IsTextureFormatRenderable(d3dfmt) && (glusage == GL_FRAMEBUFFER_ATTACHMENT_ANGLE))) michael@0: { michael@0: d3dusage |= D3DUSAGE_RENDERTARGET; michael@0: } michael@0: return d3dusage; michael@0: } michael@0: michael@0: bool TextureStorage9::IsTextureFormatRenderable(D3DFORMAT format) michael@0: { michael@0: if (format == D3DFMT_INTZ) michael@0: { michael@0: return true; michael@0: } michael@0: switch(format) michael@0: { michael@0: case D3DFMT_L8: michael@0: case D3DFMT_A8L8: michael@0: case D3DFMT_DXT1: michael@0: case D3DFMT_DXT3: michael@0: case D3DFMT_DXT5: michael@0: return false; michael@0: case D3DFMT_A8R8G8B8: michael@0: case D3DFMT_X8R8G8B8: michael@0: case D3DFMT_A16B16G16R16F: michael@0: case D3DFMT_A32B32G32R32F: michael@0: return true; michael@0: default: michael@0: UNREACHABLE(); michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: bool TextureStorage9::isRenderTarget() const michael@0: { michael@0: return (mD3DUsage & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL)) != 0; michael@0: } michael@0: michael@0: bool TextureStorage9::isManaged() const michael@0: { michael@0: return (mD3DPool == D3DPOOL_MANAGED); michael@0: } michael@0: michael@0: D3DPOOL TextureStorage9::getPool() const michael@0: { michael@0: return mD3DPool; michael@0: } michael@0: michael@0: DWORD TextureStorage9::getUsage() const michael@0: { michael@0: return mD3DUsage; michael@0: } michael@0: michael@0: int TextureStorage9::getLodOffset() const michael@0: { michael@0: return mLodOffset; michael@0: } michael@0: michael@0: int TextureStorage9::levelCount() michael@0: { michael@0: return getBaseTexture() ? getBaseTexture()->GetLevelCount() - getLodOffset() : 0; michael@0: } michael@0: michael@0: TextureStorage9_2D::TextureStorage9_2D(Renderer *renderer, SwapChain9 *swapchain) : TextureStorage9(renderer, D3DUSAGE_RENDERTARGET) michael@0: { michael@0: IDirect3DTexture9 *surfaceTexture = swapchain->getOffscreenTexture(); michael@0: mTexture = surfaceTexture; michael@0: mRenderTarget = NULL; michael@0: michael@0: initializeRenderTarget(); michael@0: } michael@0: michael@0: TextureStorage9_2D::TextureStorage9_2D(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height) michael@0: : TextureStorage9(renderer, GetTextureUsage(Renderer9::makeRenderer9(renderer)->ConvertTextureInternalFormat(internalformat), usage, forceRenderable)) michael@0: { michael@0: mTexture = NULL; michael@0: mRenderTarget = NULL; michael@0: // if the width or height is not positive this should be treated as an incomplete texture michael@0: // we handle that here by skipping the d3d texture creation michael@0: if (width > 0 && height > 0) michael@0: { michael@0: IDirect3DDevice9 *device = mRenderer->getDevice(); michael@0: gl::MakeValidSize(false, gl::IsCompressed(internalformat), &width, &height, &mLodOffset); michael@0: HRESULT result = device->CreateTexture(width, height, levels ? levels + mLodOffset : 0, getUsage(), michael@0: mRenderer->ConvertTextureInternalFormat(internalformat), getPool(), &mTexture, NULL); michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); michael@0: gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: } michael@0: michael@0: initializeRenderTarget(); michael@0: } michael@0: michael@0: TextureStorage9_2D::~TextureStorage9_2D() michael@0: { michael@0: if (mTexture) michael@0: { michael@0: mTexture->Release(); michael@0: } michael@0: michael@0: delete mRenderTarget; michael@0: } michael@0: michael@0: TextureStorage9_2D *TextureStorage9_2D::makeTextureStorage9_2D(TextureStorage *storage) michael@0: { michael@0: ASSERT(HAS_DYNAMIC_TYPE(TextureStorage9_2D*, storage)); michael@0: return static_cast(storage); michael@0: } michael@0: michael@0: // Increments refcount on surface. michael@0: // caller must Release() the returned surface michael@0: IDirect3DSurface9 *TextureStorage9_2D::getSurfaceLevel(int level, bool dirty) michael@0: { michael@0: IDirect3DSurface9 *surface = NULL; michael@0: michael@0: if (mTexture) michael@0: { michael@0: HRESULT result = mTexture->GetSurfaceLevel(level + mLodOffset, &surface); michael@0: ASSERT(SUCCEEDED(result)); michael@0: michael@0: // With managed textures the driver needs to be informed of updates to the lower mipmap levels michael@0: if (level + mLodOffset != 0 && isManaged() && dirty) michael@0: { michael@0: mTexture->AddDirtyRect(NULL); michael@0: } michael@0: } michael@0: michael@0: return surface; michael@0: } michael@0: michael@0: RenderTarget *TextureStorage9_2D::getRenderTarget() michael@0: { michael@0: return mRenderTarget; michael@0: } michael@0: michael@0: void TextureStorage9_2D::generateMipmap(int level) michael@0: { michael@0: IDirect3DSurface9 *upper = getSurfaceLevel(level - 1, false); michael@0: IDirect3DSurface9 *lower = getSurfaceLevel(level, true); michael@0: michael@0: if (upper != NULL && lower != NULL) michael@0: { michael@0: mRenderer->boxFilter(upper, lower); michael@0: } michael@0: michael@0: if (upper != NULL) upper->Release(); michael@0: if (lower != NULL) lower->Release(); michael@0: } michael@0: michael@0: IDirect3DBaseTexture9 *TextureStorage9_2D::getBaseTexture() const michael@0: { michael@0: return mTexture; michael@0: } michael@0: michael@0: void TextureStorage9_2D::initializeRenderTarget() michael@0: { michael@0: ASSERT(mRenderTarget == NULL); michael@0: michael@0: if (mTexture != NULL && isRenderTarget()) michael@0: { michael@0: IDirect3DSurface9 *surface = getSurfaceLevel(0, false); michael@0: michael@0: mRenderTarget = new RenderTarget9(mRenderer, surface); michael@0: } michael@0: } michael@0: michael@0: TextureStorage9_Cube::TextureStorage9_Cube(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size) michael@0: : TextureStorage9(renderer, GetTextureUsage(Renderer9::makeRenderer9(renderer)->ConvertTextureInternalFormat(internalformat), usage, forceRenderable)) michael@0: { michael@0: mTexture = NULL; michael@0: for (int i = 0; i < 6; ++i) michael@0: { michael@0: mRenderTarget[i] = NULL; michael@0: } michael@0: michael@0: // if the size is not positive this should be treated as an incomplete texture michael@0: // we handle that here by skipping the d3d texture creation michael@0: if (size > 0) michael@0: { michael@0: IDirect3DDevice9 *device = mRenderer->getDevice(); michael@0: int height = size; michael@0: gl::MakeValidSize(false, gl::IsCompressed(internalformat), &size, &height, &mLodOffset); michael@0: HRESULT result = device->CreateCubeTexture(size, levels ? levels + mLodOffset : 0, getUsage(), michael@0: mRenderer->ConvertTextureInternalFormat(internalformat), getPool(), &mTexture, NULL); michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); michael@0: gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: } michael@0: michael@0: initializeRenderTarget(); michael@0: } michael@0: michael@0: TextureStorage9_Cube::~TextureStorage9_Cube() michael@0: { michael@0: if (mTexture) michael@0: { michael@0: mTexture->Release(); michael@0: } michael@0: michael@0: for (int i = 0; i < 6; ++i) michael@0: { michael@0: delete mRenderTarget[i]; michael@0: } michael@0: } michael@0: michael@0: TextureStorage9_Cube *TextureStorage9_Cube::makeTextureStorage9_Cube(TextureStorage *storage) michael@0: { michael@0: ASSERT(HAS_DYNAMIC_TYPE(TextureStorage9_Cube*, storage)); michael@0: return static_cast(storage); michael@0: } michael@0: michael@0: // Increments refcount on surface. michael@0: // caller must Release() the returned surface michael@0: IDirect3DSurface9 *TextureStorage9_Cube::getCubeMapSurface(GLenum faceTarget, int level, bool dirty) michael@0: { michael@0: IDirect3DSurface9 *surface = NULL; michael@0: michael@0: if (mTexture) michael@0: { michael@0: D3DCUBEMAP_FACES face = gl_d3d9::ConvertCubeFace(faceTarget); michael@0: HRESULT result = mTexture->GetCubeMapSurface(face, level + mLodOffset, &surface); michael@0: ASSERT(SUCCEEDED(result)); michael@0: michael@0: // With managed textures the driver needs to be informed of updates to the lower mipmap levels michael@0: if (level != 0 && isManaged() && dirty) michael@0: { michael@0: mTexture->AddDirtyRect(face, NULL); michael@0: } michael@0: } michael@0: michael@0: return surface; michael@0: } michael@0: michael@0: RenderTarget *TextureStorage9_Cube::getRenderTarget(GLenum faceTarget) michael@0: { michael@0: return mRenderTarget[gl::TextureCubeMap::faceIndex(faceTarget)]; michael@0: } michael@0: michael@0: void TextureStorage9_Cube::generateMipmap(int face, int level) michael@0: { michael@0: IDirect3DSurface9 *upper = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level - 1, false); michael@0: IDirect3DSurface9 *lower = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, true); michael@0: michael@0: if (upper != NULL && lower != NULL) michael@0: { michael@0: mRenderer->boxFilter(upper, lower); michael@0: } michael@0: michael@0: if (upper != NULL) upper->Release(); michael@0: if (lower != NULL) lower->Release(); michael@0: } michael@0: michael@0: IDirect3DBaseTexture9 *TextureStorage9_Cube::getBaseTexture() const michael@0: { michael@0: return mTexture; michael@0: } michael@0: michael@0: void TextureStorage9_Cube::initializeRenderTarget() michael@0: { michael@0: if (mTexture != NULL && isRenderTarget()) michael@0: { michael@0: IDirect3DSurface9 *surface = NULL; michael@0: michael@0: for (int i = 0; i < 6; ++i) michael@0: { michael@0: ASSERT(mRenderTarget[i] == NULL); michael@0: michael@0: surface = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, false); michael@0: michael@0: mRenderTarget[i] = new RenderTarget9(mRenderer, surface); michael@0: } michael@0: } michael@0: } michael@0: michael@0: }