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