Create D3D textures within Storage classes. Fixed render target reference counting.

TRAC #18730 Signed-off-by: Daniel Koch Author: Nicolas Capens git-svn-id: https://angleproject.googlecode.com/svn/trunk@853 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent afa8ef36
...@@ -305,6 +305,11 @@ Colorbuffer::~Colorbuffer() ...@@ -305,6 +305,11 @@ Colorbuffer::~Colorbuffer()
IDirect3DSurface9 *Colorbuffer::getRenderTarget() IDirect3DSurface9 *Colorbuffer::getRenderTarget()
{ {
if (mRenderTarget)
{
mRenderTarget->AddRef();
}
return mRenderTarget; return mRenderTarget;
} }
......
...@@ -1519,9 +1519,23 @@ unsigned int Texture::issueSerial() ...@@ -1519,9 +1519,23 @@ unsigned int Texture::issueSerial()
return mCurrentSerial++; return mCurrentSerial++;
} }
TextureStorage2D::TextureStorage2D(IDirect3DTexture9 *texture, bool renderable) : TextureStorage(renderable) TextureStorage2D::TextureStorage2D(IDirect3DTexture9 *surfaceTexture) : TextureStorage(true)
{ {
mTexture = texture; mTexture = surfaceTexture;
}
TextureStorage2D::TextureStorage2D(int levels, D3DFORMAT format, int width, int height, bool renderable) : TextureStorage(renderable)
{
IDirect3DDevice9 *device = getDevice();
mTexture = NULL;
HRESULT result = device->CreateTexture(width, height, levels, renderable ? D3DUSAGE_RENDERTARGET : 0, format, D3DPOOL_DEFAULT, &mTexture, NULL);
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
error(GL_OUT_OF_MEMORY);
}
} }
TextureStorage2D::~TextureStorage2D() TextureStorage2D::~TextureStorage2D()
...@@ -1647,7 +1661,7 @@ void Texture2D::bindTexImage(egl::Surface *surface) ...@@ -1647,7 +1661,7 @@ void Texture2D::bindTexImage(egl::Surface *surface)
mImageArray[0].redefine(format, surface->getWidth(), surface->getHeight(), GL_UNSIGNED_BYTE); mImageArray[0].redefine(format, surface->getWidth(), surface->getHeight(), GL_UNSIGNED_BYTE);
delete mTexture; delete mTexture;
mTexture = new TextureStorage2D(surface->getOffscreenTexture(), true); mTexture = new TextureStorage2D(surface->getOffscreenTexture());
mSerial = issueSerial(); mSerial = issueSerial();
mColorbufferProxy.set(NULL); mColorbufferProxy.set(NULL);
mDirtyImages = true; mDirtyImages = true;
...@@ -1763,10 +1777,15 @@ void Texture2D::copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei ...@@ -1763,10 +1777,15 @@ void Texture2D::copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei
IDirect3DSurface9 *dest = mTexture->getSurfaceLevel(level); IDirect3DSurface9 *dest = mTexture->getSurfaceLevel(level);
getBlitter()->copy(source->getRenderTarget(), sourceRect, format, 0, destYOffset, dest); if (dest)
{
getBlitter()->copy(renderTarget, sourceRect, format, 0, destYOffset, dest);
dest->Release(); dest->Release();
} }
} }
}
renderTarget->Release();
} }
void Texture2D::copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) void Texture2D::copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source)
...@@ -1810,10 +1829,15 @@ void Texture2D::copySubImage(GLenum target, GLint level, GLint xoffset, GLint yo ...@@ -1810,10 +1829,15 @@ void Texture2D::copySubImage(GLenum target, GLint level, GLint xoffset, GLint yo
IDirect3DSurface9 *dest = mTexture->getSurfaceLevel(level); IDirect3DSurface9 *dest = mTexture->getSurfaceLevel(level);
getBlitter()->copy(source->getRenderTarget(), sourceRect, mImageArray[0].getFormat(), xoffset, destYOffset, dest); if (dest)
{
getBlitter()->copy(renderTarget, sourceRect, mImageArray[0].getFormat(), xoffset, destYOffset, dest);
dest->Release(); dest->Release();
} }
} }
}
renderTarget->Release();
} }
// Tests for GL texture object completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81. // Tests for GL texture object completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81.
...@@ -1916,21 +1940,14 @@ IDirect3DBaseTexture9 *Texture2D::getBaseTexture() const ...@@ -1916,21 +1940,14 @@ IDirect3DBaseTexture9 *Texture2D::getBaseTexture() const
// Constructs a Direct3D 9 texture resource from the texture images // Constructs a Direct3D 9 texture resource from the texture images
void Texture2D::createTexture() void Texture2D::createTexture()
{ {
IDirect3DDevice9 *device = getDevice(); GLsizei width = mImageArray[0].getWidth();
GLsizei height = mImageArray[0].getHeight();
GLint levels = creationLevels(width, height);
D3DFORMAT format = mImageArray[0].getD3DFormat(); D3DFORMAT format = mImageArray[0].getD3DFormat();
GLint levels = creationLevels(mImageArray[0].getWidth(), mImageArray[0].getHeight());
IDirect3DTexture9 *texture = NULL;
HRESULT result = device->CreateTexture(mImageArray[0].getWidth(), mImageArray[0].getHeight(), levels, 0, format, D3DPOOL_DEFAULT, &texture, NULL);
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
return error(GL_OUT_OF_MEMORY);
}
delete mTexture; delete mTexture;
mTexture = new TextureStorage2D(texture, false); mTexture = new TextureStorage2D(levels, format, width, height, false);
mSerial = issueSerial(); mSerial = issueSerial();
mColorbufferProxy.set(NULL); mColorbufferProxy.set(NULL);
mDirtyImages = true; mDirtyImages = true;
...@@ -1957,32 +1974,28 @@ void Texture2D::convertToRenderTarget() ...@@ -1957,32 +1974,28 @@ void Texture2D::convertToRenderTarget()
if (mImageArray[0].getWidth() != 0 && mImageArray[0].getHeight() != 0) if (mImageArray[0].getWidth() != 0 && mImageArray[0].getHeight() != 0)
{ {
egl::Display *display = getDisplay(); GLsizei width = mImageArray[0].getWidth();
IDirect3DDevice9 *device = getDevice(); GLsizei height = mImageArray[0].getHeight();
GLint levels = creationLevels(width, height);
D3DFORMAT format = mImageArray[0].getD3DFormat(); D3DFORMAT format = mImageArray[0].getD3DFormat();
GLint levels = creationLevels(mImageArray[0].getWidth(), mImageArray[0].getHeight());
IDirect3DTexture9 *texture2D = NULL; newTexture = new TextureStorage2D(levels, format, width, height, true);
HRESULT result = device->CreateTexture(mImageArray[0].getWidth(), mImageArray[0].getHeight(), levels, D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT, &texture2D, NULL);
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
return error(GL_OUT_OF_MEMORY);
}
newTexture = new TextureStorage2D(texture2D, true);
if (mTexture != NULL) if (mTexture != NULL)
{ {
egl::Display *display = getDisplay();
IDirect3DDevice9 *device = display->getDevice();
int levels = levelCount(); int levels = levelCount();
for (int i = 0; i < levels; i++) for (int i = 0; i < levels; i++)
{ {
IDirect3DSurface9 *source = mTexture->getSurfaceLevel(i); IDirect3DSurface9 *source = mTexture->getSurfaceLevel(i);
IDirect3DSurface9 *dest = newTexture->getSurfaceLevel(i); IDirect3DSurface9 *dest = newTexture->getSurfaceLevel(i);
if (source && dest)
{
display->endScene(); display->endScene();
result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE); HRESULT result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
if (FAILED(result)) if (FAILED(result))
{ {
...@@ -1994,9 +2007,10 @@ void Texture2D::convertToRenderTarget() ...@@ -1994,9 +2007,10 @@ void Texture2D::convertToRenderTarget()
return error(GL_OUT_OF_MEMORY); return error(GL_OUT_OF_MEMORY);
} }
}
source->Release(); if (source) source->Release();
dest->Release(); if (dest) dest->Release();
} }
} }
} }
...@@ -2096,14 +2110,21 @@ IDirect3DSurface9 *Texture2D::getRenderTarget(GLenum target) ...@@ -2096,14 +2110,21 @@ IDirect3DSurface9 *Texture2D::getRenderTarget(GLenum target)
updateTexture(); updateTexture();
IDirect3DSurface9 *renderTarget = mTexture->getSurfaceLevel(0); return mTexture->getSurfaceLevel(0);
return renderTarget;
} }
TextureStorageCubeMap::TextureStorageCubeMap(IDirect3DCubeTexture9 *texture, bool renderable) : TextureStorage(renderable) TextureStorageCubeMap::TextureStorageCubeMap(int levels, D3DFORMAT format, int size, bool renderable) : TextureStorage(renderable)
{ {
mTexture = texture; IDirect3DDevice9 *device = getDevice();
mTexture = NULL;
HRESULT result = device->CreateCubeTexture(size, levels, renderable ? D3DUSAGE_RENDERTARGET : 0, format, D3DPOOL_DEFAULT, &mTexture, NULL);
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
error(GL_OUT_OF_MEMORY);
}
} }
TextureStorageCubeMap::~TextureStorageCubeMap() TextureStorageCubeMap::~TextureStorageCubeMap()
...@@ -2359,21 +2380,13 @@ IDirect3DBaseTexture9 *TextureCubeMap::getBaseTexture() const ...@@ -2359,21 +2380,13 @@ IDirect3DBaseTexture9 *TextureCubeMap::getBaseTexture() const
// Constructs a Direct3D 9 texture resource from the texture images, or returns an existing one // Constructs a Direct3D 9 texture resource from the texture images, or returns an existing one
void TextureCubeMap::createTexture() void TextureCubeMap::createTexture()
{ {
IDirect3DDevice9 *device = getDevice(); GLsizei size = mImageArray[0][0].getWidth();
GLint levels = creationLevels(size, 0);
D3DFORMAT format = mImageArray[0][0].getD3DFormat(); D3DFORMAT format = mImageArray[0][0].getD3DFormat();
GLint levels = creationLevels(mImageArray[0][0].getWidth(), 0);
IDirect3DCubeTexture9 *texture = NULL;
HRESULT result = device->CreateCubeTexture(mImageArray[0][0].getWidth(), levels, 0, format, D3DPOOL_DEFAULT, &texture, NULL);
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
return error(GL_OUT_OF_MEMORY);
}
delete mTexture; delete mTexture;
mTexture = new TextureStorageCubeMap(texture, false); mTexture = new TextureStorageCubeMap(levels, format, size, false);
mSerial = issueSerial(); mSerial = issueSerial();
for(int face = 0; face < 6; face++) mFaceProxies[face].set(NULL); for(int face = 0; face < 6; face++) mFaceProxies[face].set(NULL);
mDirtyImages = true; mDirtyImages = true;
...@@ -2402,24 +2415,17 @@ void TextureCubeMap::convertToRenderTarget() ...@@ -2402,24 +2415,17 @@ void TextureCubeMap::convertToRenderTarget()
if (mImageArray[0][0].getWidth() != 0) if (mImageArray[0][0].getWidth() != 0)
{ {
egl::Display *display = getDisplay(); GLsizei size = mImageArray[0][0].getWidth();
IDirect3DDevice9 *device = getDevice(); GLint levels = creationLevels(size, 0);
D3DFORMAT format = mImageArray[0][0].getD3DFormat(); D3DFORMAT format = mImageArray[0][0].getD3DFormat();
GLint levels = creationLevels(mImageArray[0][0].getWidth(), 0);
IDirect3DCubeTexture9 *cubeTexture = NULL; newTexture = new TextureStorageCubeMap(levels, format, size, true);
HRESULT result = device->CreateCubeTexture(mImageArray[0][0].getWidth(), levels, D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT, &cubeTexture, NULL);
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
return error(GL_OUT_OF_MEMORY);
}
newTexture = new TextureStorageCubeMap(cubeTexture, true);
if (mTexture != NULL) if (mTexture != NULL)
{ {
egl::Display *display = getDisplay();
IDirect3DDevice9 *device = display->getDevice();
int levels = levelCount(); int levels = levelCount();
for (int f = 0; f < 6; f++) for (int f = 0; f < 6; f++)
{ {
...@@ -2428,8 +2434,10 @@ void TextureCubeMap::convertToRenderTarget() ...@@ -2428,8 +2434,10 @@ void TextureCubeMap::convertToRenderTarget()
IDirect3DSurface9 *source = mTexture->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i); IDirect3DSurface9 *source = mTexture->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i);
IDirect3DSurface9 *dest = newTexture->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i); IDirect3DSurface9 *dest = newTexture->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i);
if (source && dest)
{
display->endScene(); display->endScene();
result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE); HRESULT result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
if (FAILED(result)) if (FAILED(result))
{ {
...@@ -2441,9 +2449,10 @@ void TextureCubeMap::convertToRenderTarget() ...@@ -2441,9 +2449,10 @@ void TextureCubeMap::convertToRenderTarget()
return error(GL_OUT_OF_MEMORY); return error(GL_OUT_OF_MEMORY);
} }
}
source->Release(); if (source) source->Release();
dest->Release(); if (dest) dest->Release();
} }
} }
} }
...@@ -2537,10 +2546,15 @@ void TextureCubeMap::copyImage(GLenum target, GLint level, GLenum format, GLint ...@@ -2537,10 +2546,15 @@ void TextureCubeMap::copyImage(GLenum target, GLint level, GLenum format, GLint
IDirect3DSurface9 *dest = mTexture->getCubeMapSurface(target, level); IDirect3DSurface9 *dest = mTexture->getCubeMapSurface(target, level);
getBlitter()->copy(source->getRenderTarget(), sourceRect, format, 0, destYOffset, dest); if (dest)
{
getBlitter()->copy(renderTarget, sourceRect, format, 0, destYOffset, dest);
dest->Release(); dest->Release();
} }
} }
}
renderTarget->Release();
} }
void TextureCubeMap::copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) void TextureCubeMap::copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source)
...@@ -2588,10 +2602,15 @@ void TextureCubeMap::copySubImage(GLenum target, GLint level, GLint xoffset, GLi ...@@ -2588,10 +2602,15 @@ void TextureCubeMap::copySubImage(GLenum target, GLint level, GLint xoffset, GLi
IDirect3DSurface9 *dest = mTexture->getCubeMapSurface(target, level); IDirect3DSurface9 *dest = mTexture->getCubeMapSurface(target, level);
getBlitter()->copy(source->getRenderTarget(), sourceRect, mImageArray[0][0].getFormat(), xoffset, destYOffset, dest); if (dest)
{
getBlitter()->copy(renderTarget, sourceRect, mImageArray[0][0].getFormat(), xoffset, destYOffset, dest);
dest->Release(); dest->Release();
} }
} }
}
renderTarget->Release();
} }
bool TextureCubeMap::isCubeComplete() const bool TextureCubeMap::isCubeComplete() const
......
...@@ -232,7 +232,8 @@ class Texture : public RefCountObject ...@@ -232,7 +232,8 @@ class Texture : public RefCountObject
class TextureStorage2D : public TextureStorage class TextureStorage2D : public TextureStorage
{ {
public: public:
TextureStorage2D(IDirect3DTexture9 *texture, bool renderable); explicit TextureStorage2D(IDirect3DTexture9 *surfaceTexture);
TextureStorage2D(int levels, D3DFORMAT format, int width, int height, bool renderable);
virtual ~TextureStorage2D(); virtual ~TextureStorage2D();
...@@ -299,7 +300,7 @@ class Texture2D : public Texture ...@@ -299,7 +300,7 @@ class Texture2D : public Texture
class TextureStorageCubeMap : public TextureStorage class TextureStorageCubeMap : public TextureStorage
{ {
public: public:
TextureStorageCubeMap(IDirect3DCubeTexture9 *texture, bool renderable); TextureStorageCubeMap(int levels, D3DFORMAT format, int size, bool renderable);
virtual ~TextureStorageCubeMap(); virtual ~TextureStorageCubeMap();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment