Hash :
76d3e6e0
Author :
Date :
2012-10-31T19:55:33
Rename renderer namespace to rx Trac #21999 Author: Shannon Woods Signed-off-by: Daniel Koch git-svn-id: https://angleproject.googlecode.com/svn/branches/dx11proto@1379 736b8ea6-26fd-11df-bfd4-992fa37f6226
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
//
// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// TextureStorage.cpp: Implements the abstract gl::TextureStorage class and its concrete derived
// classes TextureStorage2D and TextureStorageCubeMap, which act as the interface to the
// D3D-side texture.
#include "libGLESv2/main.h"
#include "libGLESv2/renderer/TextureStorage.h"
#include "libGLESv2/renderer/SwapChain.h"
#include "libGLESv2/Blit.h"
#include "libGLESv2/renderer/renderer9_utils.h"
#include "common/debug.h"
namespace gl
{
unsigned int TextureStorage::mCurrentTextureSerial = 1;
TextureStorage::TextureStorage(DWORD usage)
: mD3DUsage(usage),
mD3DPool(getDisplay()->getRenderer9()->getTexturePool(usage)), // D3D9_REPLACE
mTextureSerial(issueTextureSerial()),
mLodOffset(0)
{
}
TextureStorage::~TextureStorage()
{
}
DWORD TextureStorage::GetTextureUsage(D3DFORMAT d3dfmt, GLenum glusage, bool forceRenderable)
{
DWORD d3dusage = 0;
if (d3dfmt == D3DFMT_INTZ)
{
d3dusage |= D3DUSAGE_DEPTHSTENCIL;
}
else if(forceRenderable || (TextureStorage::IsTextureFormatRenderable(d3dfmt) && (glusage == GL_FRAMEBUFFER_ATTACHMENT_ANGLE)))
{
d3dusage |= D3DUSAGE_RENDERTARGET;
}
return d3dusage;
}
bool TextureStorage::IsTextureFormatRenderable(D3DFORMAT format)
{
if (format == D3DFMT_INTZ)
{
return true;
}
switch(format)
{
case D3DFMT_L8:
case D3DFMT_A8L8:
case D3DFMT_DXT1:
case D3DFMT_DXT3:
case D3DFMT_DXT5:
return false;
case D3DFMT_A8R8G8B8:
case D3DFMT_X8R8G8B8:
case D3DFMT_A16B16G16R16F:
case D3DFMT_A32B32G32R32F:
return true;
default:
UNREACHABLE();
}
return false;
}
D3DFORMAT TextureStorage::ConvertTextureInternalFormat(GLint internalformat)
{
switch (internalformat)
{
case GL_DEPTH_COMPONENT16:
case GL_DEPTH_COMPONENT32_OES:
case GL_DEPTH24_STENCIL8_OES:
return D3DFMT_INTZ;
case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
return D3DFMT_DXT1;
case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
return D3DFMT_DXT3;
case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
return D3DFMT_DXT5;
case GL_RGBA32F_EXT:
case GL_RGB32F_EXT:
case GL_ALPHA32F_EXT:
case GL_LUMINANCE32F_EXT:
case GL_LUMINANCE_ALPHA32F_EXT:
return D3DFMT_A32B32G32R32F;
case GL_RGBA16F_EXT:
case GL_RGB16F_EXT:
case GL_ALPHA16F_EXT:
case GL_LUMINANCE16F_EXT:
case GL_LUMINANCE_ALPHA16F_EXT:
return D3DFMT_A16B16G16R16F;
case GL_LUMINANCE8_EXT:
if (getContext()->supportsLuminanceTextures())
{
return D3DFMT_L8;
}
break;
case GL_LUMINANCE8_ALPHA8_EXT:
if (getContext()->supportsLuminanceAlphaTextures())
{
return D3DFMT_A8L8;
}
break;
case GL_RGB8_OES:
case GL_RGB565:
return D3DFMT_X8R8G8B8;
}
return D3DFMT_A8R8G8B8;
}
Blit *TextureStorage::getBlitter()
{
Context *context = getContext();
return context->getBlitter();
}
bool TextureStorage::isRenderTarget() const
{
return (mD3DUsage & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL)) != 0;
}
bool TextureStorage::isManaged() const
{
return (mD3DPool == D3DPOOL_MANAGED);
}
D3DPOOL TextureStorage::getPool() const
{
return mD3DPool;
}
DWORD TextureStorage::getUsage() const
{
return mD3DUsage;
}
unsigned int TextureStorage::getTextureSerial() const
{
return mTextureSerial;
}
unsigned int TextureStorage::issueTextureSerial()
{
return mCurrentTextureSerial++;
}
int TextureStorage::getLodOffset() const
{
return mLodOffset;
}
int TextureStorage::levelCount()
{
return getBaseTexture() ? getBaseTexture()->GetLevelCount() - getLodOffset() : 0;
}
bool TextureStorage::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
{
if (source && dest)
{
HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
rx::Renderer9 *renderer = getDisplay()->getRenderer9();
IDirect3DDevice9 *device = renderer->getDevice(); // D3D9_REPLACE
if (fromManaged)
{
D3DSURFACE_DESC desc;
source->GetDesc(&desc);
IDirect3DSurface9 *surf = 0;
result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
if (SUCCEEDED(result))
{
Image::CopyLockableSurfaces(surf, source);
result = device->UpdateSurface(surf, NULL, dest, NULL);
surf->Release();
}
}
else
{
renderer->endScene();
result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
}
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
return false;
}
}
return true;
}
TextureStorage2D::TextureStorage2D(rx::SwapChain *swapchain) : TextureStorage(D3DUSAGE_RENDERTARGET), mRenderTargetSerial(RenderbufferStorage::issueSerial())
{
IDirect3DTexture9 *surfaceTexture = swapchain->getOffscreenTexture();
mTexture = surfaceTexture;
}
TextureStorage2D::TextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
: TextureStorage(GetTextureUsage(ConvertTextureInternalFormat(internalformat), usage, forceRenderable)),
mRenderTargetSerial(RenderbufferStorage::issueSerial())
{
mTexture = NULL;
// if the width or height is not positive this should be treated as an incomplete texture
// we handle that here by skipping the d3d texture creation
if (width > 0 && height > 0)
{
IDirect3DDevice9 *device = getDisplay()->getRenderer9()->getDevice(); // D3D9_REPLACE
MakeValidSize(false, gl::IsCompressed(internalformat), &width, &height, &mLodOffset);
HRESULT result = device->CreateTexture(width, height, levels ? levels + mLodOffset : 0, getUsage(),
ConvertTextureInternalFormat(internalformat), getPool(), &mTexture, NULL);
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
error(GL_OUT_OF_MEMORY);
}
}
}
TextureStorage2D::~TextureStorage2D()
{
if (mTexture)
{
mTexture->Release();
}
}
bool TextureStorage2D::copyToRenderTarget(TextureStorage2D *dest, TextureStorage2D *source)
{
bool result = false;
if (source && dest)
{
int levels = source->levelCount();
for (int i = 0; i < levels; ++i)
{
IDirect3DSurface9 *srcSurf = source->getSurfaceLevel(i, false);
IDirect3DSurface9 *dstSurf = dest->getSurfaceLevel(i, false);
result = TextureStorage::copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
if (srcSurf) srcSurf->Release();
if (dstSurf) dstSurf->Release();
if (!result)
return false;
}
}
return result;
}
// Increments refcount on surface.
// caller must Release() the returned surface
IDirect3DSurface9 *TextureStorage2D::getSurfaceLevel(int level, bool dirty)
{
IDirect3DSurface9 *surface = NULL;
if (mTexture)
{
HRESULT result = mTexture->GetSurfaceLevel(level + mLodOffset, &surface);
ASSERT(SUCCEEDED(result));
// With managed textures the driver needs to be informed of updates to the lower mipmap levels
if (level != 0 && isManaged() && dirty)
{
mTexture->AddDirtyRect(NULL);
}
}
return surface;
}
void TextureStorage2D::generateMipmap(int level)
{
IDirect3DSurface9 *upper = getSurfaceLevel(level - 1, false);
IDirect3DSurface9 *lower = getSurfaceLevel(level, true);
if (upper != NULL && lower != NULL)
{
getBlitter()->boxFilter(upper, lower);
}
if (upper != NULL) upper->Release();
if (lower != NULL) lower->Release();
}
IDirect3DBaseTexture9 *TextureStorage2D::getBaseTexture() const
{
return mTexture;
}
unsigned int TextureStorage2D::getRenderTargetSerial(GLenum target) const
{
return mRenderTargetSerial;
}
TextureStorageCubeMap::TextureStorageCubeMap(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
: TextureStorage(GetTextureUsage(ConvertTextureInternalFormat(internalformat), usage, forceRenderable)),
mFirstRenderTargetSerial(RenderbufferStorage::issueCubeSerials())
{
mTexture = NULL;
// if the size is not positive this should be treated as an incomplete texture
// we handle that here by skipping the d3d texture creation
if (size > 0)
{
IDirect3DDevice9 *device = getDisplay()->getRenderer9()->getDevice(); // D3D9_REPLACE
int height = size;
MakeValidSize(false, gl::IsCompressed(internalformat), &size, &height, &mLodOffset);
HRESULT result = device->CreateCubeTexture(size, levels ? levels + mLodOffset : 0, getUsage(),
ConvertTextureInternalFormat(internalformat), getPool(), &mTexture, NULL);
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
error(GL_OUT_OF_MEMORY);
}
}
}
TextureStorageCubeMap::~TextureStorageCubeMap()
{
if (mTexture)
{
mTexture->Release();
}
}
bool TextureStorageCubeMap::copyToRenderTarget(TextureStorageCubeMap *dest, TextureStorageCubeMap *source)
{
bool result = false;
if (source && dest)
{
int levels = source->levelCount();
for (int f = 0; f < 6; f++)
{
for (int i = 0; i < levels; i++)
{
IDirect3DSurface9 *srcSurf = source->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
IDirect3DSurface9 *dstSurf = dest->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
result = TextureStorage::copyToRenderTarget(dstSurf, srcSurf, source->isManaged());
if (srcSurf) srcSurf->Release();
if (dstSurf) dstSurf->Release();
if (!result)
return false;
}
}
}
return result;
}
// Increments refcount on surface.
// caller must Release() the returned surface
IDirect3DSurface9 *TextureStorageCubeMap::getCubeMapSurface(GLenum faceTarget, int level, bool dirty)
{
IDirect3DSurface9 *surface = NULL;
if (mTexture)
{
D3DCUBEMAP_FACES face = es2dx::ConvertCubeFace(faceTarget);
HRESULT result = mTexture->GetCubeMapSurface(face, level + mLodOffset, &surface);
ASSERT(SUCCEEDED(result));
// With managed textures the driver needs to be informed of updates to the lower mipmap levels
if (level != 0 && isManaged() && dirty)
{
mTexture->AddDirtyRect(face, NULL);
}
}
return surface;
}
void TextureStorageCubeMap::generateMipmap(int face, int level)
{
IDirect3DSurface9 *upper = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level - 1, false);
IDirect3DSurface9 *lower = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, true);
if (upper != NULL && lower != NULL)
{
getBlitter()->boxFilter(upper, lower);
}
if (upper != NULL) upper->Release();
if (lower != NULL) lower->Release();
}
IDirect3DBaseTexture9 *TextureStorageCubeMap::getBaseTexture() const
{
return mTexture;
}
unsigned int TextureStorageCubeMap::getRenderTargetSerial(GLenum target) const
{
return mFirstRenderTargetSerial + TextureCubeMap::faceIndex(target);
}
}