Default textures to non-renderable.

TRAC #11309 * Lift getRenderTarget() into base class Texture. * Add pure virtual convertToRenderTarget() and getSurface(). Author: Andrew Lewycky Signed-off-by: Shannon Woods Signed-off-by: Daniel Koch git-svn-id: https://angleproject.googlecode.com/svn/trunk@54 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent 842f7a4e
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "main.h" #include "main.h"
#include "mathutil.h" #include "mathutil.h"
#include "debug.h" #include "debug.h"
#include "utilities.h"
namespace gl namespace gl
{ {
...@@ -354,6 +355,31 @@ IDirect3DBaseTexture9 *Texture::getTexture() ...@@ -354,6 +355,31 @@ IDirect3DBaseTexture9 *Texture::getTexture()
return mBaseTexture; return mBaseTexture;
} }
// Returns the top-level texture surface as a render target
IDirect3DSurface9 *Texture::getRenderTarget(GLenum target)
{
if (mDirtyMetaData && mRenderTarget)
{
mRenderTarget->Release();
mRenderTarget = NULL;
}
if (!mRenderTarget)
{
mBaseTexture = convertToRenderTarget();
mRenderTarget = getSurface(target);
}
if (dirtyImageData())
{
updateTexture();
}
mDirtyMetaData = false;
return mRenderTarget;
}
Texture2D::Texture2D() Texture2D::Texture2D()
{ {
mTexture = NULL; mTexture = NULL;
...@@ -391,25 +417,31 @@ void Texture2D::commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei wi ...@@ -391,25 +417,31 @@ void Texture2D::commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei wi
if (mTexture != NULL) if (mTexture != NULL)
{ {
IDirect3DSurface9 *destLevel = NULL; IDirect3DSurface9 *destLevel = NULL;
mTexture->GetSurfaceLevel(level, &destLevel); HRESULT result = mTexture->GetSurfaceLevel(level, &destLevel);
Image *img = &mImageArray[level]; ASSERT(SUCCEEDED(result));
RECT sourceRect; if (SUCCEEDED(result))
sourceRect.left = xoffset; {
sourceRect.top = yoffset; Image *img = &mImageArray[level];
sourceRect.right = xoffset + width;
sourceRect.bottom = yoffset + height;
POINT destPoint; RECT sourceRect;
destPoint.x = xoffset; sourceRect.left = xoffset;
destPoint.y = yoffset; sourceRect.top = yoffset;
sourceRect.right = xoffset + width;
sourceRect.bottom = yoffset + height;
getDevice()->UpdateSurface(img->surface, &sourceRect, destLevel, &destPoint); POINT destPoint;
destPoint.x = xoffset;
destPoint.y = yoffset;
destLevel->Release(); result = getDevice()->UpdateSurface(img->surface, &sourceRect, destLevel, &destPoint);
ASSERT(SUCCEEDED(result));
img->dirty = false; destLevel->Release();
img->dirty = false;
}
} }
} }
...@@ -480,10 +512,11 @@ IDirect3DBaseTexture9 *Texture2D::createTexture() ...@@ -480,10 +512,11 @@ IDirect3DBaseTexture9 *Texture2D::createTexture()
IDirect3DDevice9 *device = getDevice(); IDirect3DDevice9 *device = getDevice();
D3DFORMAT format = selectFormat(mImageArray[0].format); D3DFORMAT format = selectFormat(mImageArray[0].format);
HRESULT result = device->CreateTexture(mWidth, mHeight, 0, D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT, &texture, NULL); HRESULT result = device->CreateTexture(mWidth, mHeight, 0, 0, format, D3DPOOL_DEFAULT, &texture, NULL);
if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) if (FAILED(result))
{ {
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL); return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
} }
...@@ -503,32 +536,102 @@ void Texture2D::updateTexture() ...@@ -503,32 +536,102 @@ void Texture2D::updateTexture()
if (mImageArray[level].dirty) if (mImageArray[level].dirty)
{ {
IDirect3DSurface9 *levelSurface = NULL; IDirect3DSurface9 *levelSurface = NULL;
mTexture->GetSurfaceLevel(level, &levelSurface); HRESULT result = mTexture->GetSurfaceLevel(level, &levelSurface);
device->UpdateSurface(mImageArray[level].surface, NULL, levelSurface, NULL); ASSERT(SUCCEEDED(result));
levelSurface->Release(); if (SUCCEEDED(result))
{
result = device->UpdateSurface(mImageArray[level].surface, NULL, levelSurface, NULL);
ASSERT(SUCCEEDED(result));
mImageArray[level].dirty = false; levelSurface->Release();
mImageArray[level].dirty = false;
}
} }
} }
} }
// Returns the top-level texture surface as a render target IDirect3DBaseTexture9 *Texture2D::convertToRenderTarget()
IDirect3DSurface9 *Texture2D::getRenderTarget()
{ {
if (mDirtyMetaData && mRenderTarget) IDirect3DTexture9 *texture;
IDirect3DDevice9 *device = getDevice();
D3DFORMAT format = selectFormat(mImageArray[0].format);
HRESULT result = device->CreateTexture(mWidth, mHeight, 0, D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT, &texture, NULL);
if (FAILED(result))
{ {
mRenderTarget->Release(); ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
mRenderTarget = NULL; return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
} }
if (!mRenderTarget && getTexture()) // FIXME: getTexture fails for incomplete textures. Check spec. if (mTexture != NULL)
{ {
mTexture->GetSurfaceLevel(0, &mRenderTarget); int levels = texture->GetLevelCount();
for (int i = 0; i < levels; i++)
{
IDirect3DSurface9 *source;
result = mTexture->GetSurfaceLevel(i, &source);
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
texture->Release();
return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
}
IDirect3DSurface9 *dest;
result = texture->GetSurfaceLevel(i, &dest);
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
texture->Release();
source->Release();
return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
}
result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
texture->Release();
source->Release();
dest->Release();
return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
}
source->Release();
dest->Release();
}
mTexture->Release();
} }
return mRenderTarget; mTexture = texture;
return mTexture;
}
IDirect3DSurface9 *Texture2D::getSurface(GLenum target)
{
ASSERT(target == GL_TEXTURE_2D);
IDirect3DSurface9 *surface = NULL;
HRESULT result = mTexture->GetSurfaceLevel(0, &surface);
ASSERT(SUCCEEDED(result));
return surface;
} }
bool Texture2D::dirtyImageData() const bool Texture2D::dirtyImageData() const
...@@ -601,25 +704,31 @@ void TextureCubeMap::commitRect(GLenum faceTarget, GLint level, GLint xoffset, G ...@@ -601,25 +704,31 @@ void TextureCubeMap::commitRect(GLenum faceTarget, GLint level, GLint xoffset, G
if (mTexture != NULL) if (mTexture != NULL)
{ {
IDirect3DSurface9 *destLevel = NULL; IDirect3DSurface9 *destLevel = NULL;
mTexture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(face), level, &destLevel); HRESULT result = mTexture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(face), level, &destLevel);
ASSERT(SUCCEEDED(result));
Image *img = &mImageArray[face][level]; if (SUCCEEDED(result))
{
Image *img = &mImageArray[face][level];
RECT sourceRect; RECT sourceRect;
sourceRect.left = xoffset; sourceRect.left = xoffset;
sourceRect.top = yoffset; sourceRect.top = yoffset;
sourceRect.right = xoffset + width; sourceRect.right = xoffset + width;
sourceRect.bottom = yoffset + height; sourceRect.bottom = yoffset + height;
POINT destPoint; POINT destPoint;
destPoint.x = xoffset; destPoint.x = xoffset;
destPoint.y = yoffset; destPoint.y = yoffset;
getDevice()->UpdateSurface(img->surface, &sourceRect, destLevel, &destPoint); result = getDevice()->UpdateSurface(img->surface, &sourceRect, destLevel, &destPoint);
ASSERT(SUCCEEDED(result));
destLevel->Release(); destLevel->Release();
img->dirty = false; img->dirty = false;
}
} }
} }
...@@ -699,10 +808,11 @@ IDirect3DBaseTexture9 *TextureCubeMap::createTexture() ...@@ -699,10 +808,11 @@ IDirect3DBaseTexture9 *TextureCubeMap::createTexture()
IDirect3DCubeTexture9 *texture; IDirect3DCubeTexture9 *texture;
HRESULT result = device->CreateCubeTexture(mWidth, 0, D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT, &texture, NULL); HRESULT result = device->CreateCubeTexture(mWidth, 0, 0, format, D3DPOOL_DEFAULT, &texture, NULL);
if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) if (FAILED(result))
{ {
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL); return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
} }
...@@ -724,19 +834,104 @@ void TextureCubeMap::updateTexture() ...@@ -724,19 +834,104 @@ void TextureCubeMap::updateTexture()
if (img->dirty) if (img->dirty)
{ {
IDirect3DSurface9 *levelSurface = NULL; IDirect3DSurface9 *levelSurface;
mTexture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(face), level, &levelSurface); HRESULT result = mTexture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(face), level, &levelSurface);
device->UpdateSurface(img->surface, NULL, levelSurface, NULL); ASSERT(SUCCEEDED(result));
levelSurface->Release(); if (SUCCEEDED(result))
{
result = device->UpdateSurface(img->surface, NULL, levelSurface, NULL);
ASSERT(SUCCEEDED(result));
levelSurface->Release();
img->dirty = false; img->dirty = false;
}
} }
} }
} }
} }
IDirect3DBaseTexture9 *TextureCubeMap::convertToRenderTarget()
{
IDirect3DCubeTexture9 *texture;
IDirect3DDevice9 *device = getDevice();
D3DFORMAT format = selectFormat(mImageArray[0][0].format);
HRESULT result = device->CreateCubeTexture(mWidth, 0, D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT, &texture, NULL);
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
}
if (mTexture != NULL)
{
int levels = texture->GetLevelCount();
for (int f = 0; f < 6; f++)
{
for (int i = 0; i < levels; i++)
{
IDirect3DSurface9 *source;
result = mTexture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(f), i, &source);
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
texture->Release();
return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
}
IDirect3DSurface9 *dest;
result = texture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(f), i, &dest);
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
texture->Release();
source->Release();
return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
}
result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
texture->Release();
source->Release();
dest->Release();
return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
}
}
}
mTexture->Release();
}
mTexture = texture;
return mTexture;
}
IDirect3DSurface9 *TextureCubeMap::getSurface(GLenum target)
{
ASSERT(es2dx::IsCubemapTextureTarget(target));
IDirect3DSurface9 *surface = NULL;
HRESULT result = mTexture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(faceIndex(target)), 0, &surface);
ASSERT(SUCCEEDED(result));
return surface;
}
void TextureCubeMap::setImage(int face, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels) void TextureCubeMap::setImage(int face, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels)
{ {
Texture::setImage(width, height, format, type, pixels, &mImageArray[face][level]); Texture::setImage(width, height, format, type, pixels, &mImageArray[face][level]);
......
...@@ -49,7 +49,10 @@ class Texture : public Colorbuffer ...@@ -49,7 +49,10 @@ class Texture : public Colorbuffer
GLenum getWrapT() const; GLenum getWrapT() const;
virtual bool isComplete() const = 0; virtual bool isComplete() const = 0;
IDirect3DBaseTexture9 *getTexture(); IDirect3DBaseTexture9 *getTexture();
IDirect3DSurface9 *getRenderTarget(GLenum target);
IDirect3DSurface9 *getRenderTarget() { return getRenderTarget(GL_TEXTURE_2D); } // FIXME: to be removed once FBO rendering is completed.
protected: protected:
// Helper structure representing a single image layer // Helper structure representing a single image layer
...@@ -82,16 +85,18 @@ class Texture : public Colorbuffer ...@@ -82,16 +85,18 @@ class Texture : public Colorbuffer
// The pointer returned is weak and it is assumed the derived class will keep a strong pointer until the next createTexture() call. // The pointer returned is weak and it is assumed the derived class will keep a strong pointer until the next createTexture() call.
virtual IDirect3DBaseTexture9 *createTexture() = 0; virtual IDirect3DBaseTexture9 *createTexture() = 0;
virtual void updateTexture() = 0; virtual void updateTexture() = 0;
virtual IDirect3DBaseTexture9 *convertToRenderTarget() = 0;
virtual IDirect3DSurface9 *getSurface(GLenum target) = 0;
virtual bool dirtyImageData() const = 0; virtual bool dirtyImageData() const = 0;
bool mDirtyMetaData; // FIXME: would be private but getRenderTarget is still implemented through the derived classes and they need it.
private: private:
DISALLOW_COPY_AND_ASSIGN(Texture); DISALLOW_COPY_AND_ASSIGN(Texture);
IDirect3DBaseTexture9 *mBaseTexture; // This is a weak pointer. The derived class is assumed to own a strong pointer. IDirect3DBaseTexture9 *mBaseTexture; // This is a weak pointer. The derived class is assumed to own a strong pointer.
bool mDirtyMetaData;
void loadImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, void loadImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type,
const void *input, std::size_t outputPitch, void *output) const; const void *input, std::size_t outputPitch, void *output) const;
}; };
...@@ -109,13 +114,14 @@ class Texture2D : public Texture ...@@ -109,13 +114,14 @@ class Texture2D : public Texture
void subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels); void subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels);
bool isComplete() const; bool isComplete() const;
IDirect3DSurface9 *getRenderTarget();
private: private:
DISALLOW_COPY_AND_ASSIGN(Texture2D); DISALLOW_COPY_AND_ASSIGN(Texture2D);
virtual IDirect3DBaseTexture9 *createTexture(); virtual IDirect3DBaseTexture9 *createTexture();
virtual void updateTexture(); virtual void updateTexture();
virtual IDirect3DBaseTexture9 *convertToRenderTarget();
virtual IDirect3DSurface9 *getSurface(GLenum target);
virtual bool dirtyImageData() const; virtual bool dirtyImageData() const;
...@@ -151,6 +157,8 @@ class TextureCubeMap : public Texture ...@@ -151,6 +157,8 @@ class TextureCubeMap : public Texture
virtual IDirect3DBaseTexture9 *createTexture(); virtual IDirect3DBaseTexture9 *createTexture();
virtual void updateTexture(); virtual void updateTexture();
virtual IDirect3DBaseTexture9 *convertToRenderTarget();
virtual IDirect3DSurface9 *getSurface(GLenum target);
virtual bool dirtyImageData() const; virtual bool dirtyImageData() const;
......
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