Support glTexSubImage2D.

TRAC #1167 Signed-off-by: Shannon Woods Signed-off-by: Daniel Koch Author: Andrew Lewycky <andrew.lewycky@transgaming.com> git-svn-id: https://angleproject.googlecode.com/svn/trunk@18 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent c21c2271
...@@ -26,6 +26,7 @@ Texture::Texture() : Colorbuffer(0) ...@@ -26,6 +26,7 @@ Texture::Texture() : Colorbuffer(0)
mWrapT = GL_REPEAT; mWrapT = GL_REPEAT;
mDirtyImageData = true; mDirtyImageData = true;
mDirtyMetaData = true;
} }
Texture::~Texture() Texture::~Texture()
...@@ -94,59 +95,139 @@ bool Texture::setWrapT(GLenum wrap) ...@@ -94,59 +95,139 @@ bool Texture::setWrapT(GLenum wrap)
} }
} }
GLenum Texture::getMinFilter() GLenum Texture::getMinFilter() const
{ {
return mMinFilter; return mMinFilter;
} }
GLenum Texture::getMagFilter() GLenum Texture::getMagFilter() const
{ {
return mMagFilter; return mMagFilter;
} }
GLenum Texture::getWrapS() GLenum Texture::getWrapS() const
{ {
return mWrapS; return mWrapS;
} }
GLenum Texture::getWrapT() GLenum Texture::getWrapT() const
{ {
return mWrapT; return mWrapT;
} }
// Copies an Image into an already locked Direct3D 9 surface, performing format conversions as necessary // Copies an Image into an already locked Direct3D 9 surface, performing format conversions as necessary
void Texture::copyImage(const D3DLOCKED_RECT &lock, D3DFORMAT format, Image *image) void Texture::copyImage(const D3DLOCKED_RECT &lock, D3DFORMAT format, const Image &image)
{ {
if (lock.pBits && !image->pixels.empty()) ASSERT(format == D3DFMT_A8R8G8B8);
std::size_t sourcePitch = imagePitch(image);
if (lock.pBits && !image.pixels.empty())
{ {
for (int y = 0; y < image->height; y++) if (lock.Pitch == sourcePitch)
{
memcpy(lock.pBits, &image.pixels[0], lock.Pitch * image.height);
}
else
{ {
unsigned char *source = &image->pixels[0] + y * image->width * pixelSize(*image); for (int y = 0; y < image.height; y++)
unsigned short *source16 = (unsigned short*)source; {
unsigned char *dest = (unsigned char*)lock.pBits + y * lock.Pitch; memcpy(static_cast<unsigned char*>(lock.pBits) + y * lock.Pitch, &image.pixels[0] + y * sourcePitch, sourcePitch);
}
}
}
}
// Selects an internal Direct3D 9 format for storing an Image
D3DFORMAT Texture::selectFormat(const Image &image)
{
return D3DFMT_A8R8G8B8;
}
for (int x = 0; x < image->width; x++) // Returns the size, in bytes, of a single texel in an Image
int Texture::pixelSize(GLenum format, GLenum type)
{
switch (type)
{
case GL_UNSIGNED_BYTE:
switch (format)
{
case GL_ALPHA: return sizeof(unsigned char);
case GL_LUMINANCE: return sizeof(unsigned char);
case GL_LUMINANCE_ALPHA: return sizeof(unsigned char) * 2;
case GL_RGB: return sizeof(unsigned char) * 3;
case GL_RGBA: return sizeof(unsigned char) * 4;
default: UNREACHABLE();
}
break;
case GL_UNSIGNED_SHORT_4_4_4_4:
case GL_UNSIGNED_SHORT_5_5_5_1:
case GL_UNSIGNED_SHORT_5_6_5:
return sizeof(unsigned short);
default: UNREACHABLE();
}
return 0;
}
int Texture::imagePitch(const Image &img) const
{
return img.width * 4;
}
// Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input
// into the BGRA8 pixel rectangle at output with outputPitch bytes in between each line.
void Texture::loadImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type,
const void *input, size_t outputPitch, void *output) const
{
size_t inputPitch = width * pixelSize(format, type);
for (int y = 0; y < height; y++)
{
const unsigned char *source = static_cast<const unsigned char*>(input) + y * inputPitch;
const unsigned short *source16 = reinterpret_cast<const unsigned short*>(source);
unsigned char *dest = static_cast<unsigned char*>(output) + (y + yoffset) * outputPitch + xoffset * 4;
for (int x = 0; x < width; x++)
{ {
unsigned char r; unsigned char r;
unsigned char g; unsigned char g;
unsigned char b; unsigned char b;
unsigned char a; unsigned char a;
switch (image->format) switch (format)
{ {
case GL_ALPHA: case GL_ALPHA:
UNIMPLEMENTED(); a = source[x];
r = 0;
g = 0;
b = 0;
break; break;
case GL_LUMINANCE: case GL_LUMINANCE:
UNIMPLEMENTED(); r = source[x];
g = source[x];
b = source[x];
a = 0xFF;
break; break;
case GL_LUMINANCE_ALPHA: case GL_LUMINANCE_ALPHA:
UNIMPLEMENTED(); r = source[2*x+0];
g = source[2*x+0];
b = source[2*x+0];
a = source[2*x+1];
break; break;
case GL_RGB: case GL_RGB:
switch (image->type) switch (type)
{ {
case GL_UNSIGNED_BYTE: UNIMPLEMENTED(); break; case GL_UNSIGNED_BYTE:
r = source[x * 3 + 0];
b = source[x * 3 + 1];
g = source[x * 3 + 2];
a = 0xFF;
break;
case GL_UNSIGNED_SHORT_5_6_5: case GL_UNSIGNED_SHORT_5_6_5:
{ {
unsigned short rgba = source16[x]; unsigned short rgba = source16[x];
...@@ -157,11 +238,13 @@ void Texture::copyImage(const D3DLOCKED_RECT &lock, D3DFORMAT format, Image *ima ...@@ -157,11 +238,13 @@ void Texture::copyImage(const D3DLOCKED_RECT &lock, D3DFORMAT format, Image *ima
r = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13); r = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13);
} }
break; break;
default: UNREACHABLE(); default: UNREACHABLE();
} }
break; break;
case GL_RGBA: case GL_RGBA:
switch (image->type) switch (type)
{ {
case GL_UNSIGNED_BYTE: case GL_UNSIGNED_BYTE:
r = source[x * 4 + 0]; r = source[x * 4 + 0];
...@@ -169,6 +252,7 @@ void Texture::copyImage(const D3DLOCKED_RECT &lock, D3DFORMAT format, Image *ima ...@@ -169,6 +252,7 @@ void Texture::copyImage(const D3DLOCKED_RECT &lock, D3DFORMAT format, Image *ima
b = source[x * 4 + 2]; b = source[x * 4 + 2];
a = source[x * 4 + 3]; a = source[x * 4 + 3];
break; break;
case GL_UNSIGNED_SHORT_4_4_4_4: case GL_UNSIGNED_SHORT_4_4_4_4:
{ {
unsigned short rgba = source16[x]; unsigned short rgba = source16[x];
...@@ -179,111 +263,60 @@ void Texture::copyImage(const D3DLOCKED_RECT &lock, D3DFORMAT format, Image *ima ...@@ -179,111 +263,60 @@ void Texture::copyImage(const D3DLOCKED_RECT &lock, D3DFORMAT format, Image *ima
r = ((rgba & 0xF000) >> 8) | ((rgba & 0xF000) >> 12); r = ((rgba & 0xF000) >> 8) | ((rgba & 0xF000) >> 12);
} }
break; break;
case GL_UNSIGNED_SHORT_5_5_5_1: UNIMPLEMENTED(); break;
default: UNREACHABLE();
}
break;
default: UNREACHABLE();
}
switch (format) case GL_UNSIGNED_SHORT_5_5_5_1:
{ {
case D3DFMT_A8R8G8B8: unsigned short rgba = source16[x];
dest[4 * x + 0] = b;
dest[4 * x + 1] = g;
dest[4 * x + 2] = r;
dest[4 * x + 3] = a;
break;
default: UNREACHABLE();
}
}
}
}
}
// Selects an internal Direct3D 9 format for storing an Image a = (rgba & 0x0001) ? 0xFF : 0;
D3DFORMAT Texture::selectFormat(const Image &image) b = ((rgba & 0x003E) << 2) | ((rgba & 0x903E) >> 3);
{ g = ((rgba & 0x07C0) >> 3) | ((rgba & 0x07C0) >> 8);
switch (image.format) r = ((rgba & 0xF800) >> 8) | ((rgba & 0x07C0) >> 13);
{
case GL_ALPHA:
UNIMPLEMENTED();
break;
case GL_LUMINANCE:
UNIMPLEMENTED();
break;
case GL_LUMINANCE_ALPHA:
UNIMPLEMENTED();
break;
case GL_RGB:
switch (image.type)
{
case GL_UNSIGNED_BYTE: UNIMPLEMENTED();
case GL_UNSIGNED_SHORT_5_6_5: return D3DFMT_A8R8G8B8;
default: UNREACHABLE();
} }
break; break;
case GL_RGBA:
switch (image.type)
{
case GL_UNSIGNED_BYTE: return D3DFMT_A8R8G8B8;
case GL_UNSIGNED_SHORT_4_4_4_4: return D3DFMT_A8R8G8B8;
case GL_UNSIGNED_SHORT_5_5_5_1: UNIMPLEMENTED();
default: UNREACHABLE(); default: UNREACHABLE();
} }
break; break;
default: UNREACHABLE(); default: UNREACHABLE();
} }
return D3DFMT_UNKNOWN; dest[4 * x + 0] = b;
} dest[4 * x + 1] = g;
dest[4 * x + 2] = r;
// Returns the size, in bytes, of a single texel in an Image dest[4 * x + 3] = a;
int Texture::pixelSize(const Image &image)
{
switch (image.type)
{
case GL_UNSIGNED_BYTE:
switch (image.format)
{
case GL_ALPHA: return sizeof(unsigned char);
case GL_LUMINANCE: return sizeof(unsigned char);
case GL_LUMINANCE_ALPHA: return sizeof(unsigned char) * 2;
case GL_RGB: return sizeof(unsigned char) * 3;
case GL_RGBA: return sizeof(unsigned char) * 4;
default: UNREACHABLE();
} }
break;
case GL_UNSIGNED_SHORT_4_4_4_4:
case GL_UNSIGNED_SHORT_5_5_5_1:
case GL_UNSIGNED_SHORT_5_6_5:
return sizeof(unsigned short);
default: UNREACHABLE();
} }
return 0;
} }
void Texture::setImage(GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels, Image *img) void Texture::setImage(GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels, Image *img)
{ {
mDirtyImageData = true;
img->internalFormat = internalFormat;
img->width = width; img->width = width;
img->height = height; img->height = height;
img->format = format; img->format = format;
img->type = type;
size_t imageSize = pixelSize(*img) * width * height; size_t imageSize = imagePitch(*img) * img->height;
std::vector<unsigned char> storage(imageSize); std::vector<unsigned char> storage(imageSize);
if (pixels) if (pixels)
{ {
memcpy(&storage[0], pixels, imageSize); loadImageData(0, 0, width, height, format, type, pixels, imagePitch(*img), &storage[0]);
} }
img->pixels.swap(storage); img->pixels.swap(storage);
mDirtyImageData = true;
mDirtyMetaData = true;
}
void Texture::subImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels, Image *img)
{
if (width + xoffset > img->width || height + yoffset > img->height) return error(GL_INVALID_VALUE);
loadImageData(xoffset, yoffset, width, height, format, type, pixels, imagePitch(*img), &img->pixels[0]);
mDirtyImageData = true;
} }
IDirect3DBaseTexture9 *Texture::getTexture() IDirect3DBaseTexture9 *Texture::getTexture()
...@@ -293,13 +326,21 @@ IDirect3DBaseTexture9 *Texture::getTexture() ...@@ -293,13 +326,21 @@ IDirect3DBaseTexture9 *Texture::getTexture()
return NULL; return NULL;
} }
if (mDirtyImageData) if (mDirtyMetaData)
{ {
ASSERT(mDirtyImageData);
mBaseTexture = createTexture(); mBaseTexture = createTexture();
}
mDirtyImageData = false; if (mDirtyImageData)
{
updateTexture();
} }
mDirtyMetaData = false;
mDirtyImageData = false;
return mBaseTexture; return mBaseTexture;
} }
...@@ -324,12 +365,7 @@ GLenum Texture2D::getTarget() const ...@@ -324,12 +365,7 @@ GLenum Texture2D::getTarget() const
void Texture2D::setImage(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels) void Texture2D::setImage(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels)
{ {
if (level < 0 || level >= MAX_TEXTURE_LEVELS) Texture::setImage(width, height, format, type, pixels, &mImageArray[level]);
{
return;
}
Texture::setImage(internalFormat, width, height, format, type, pixels, &mImageArray[level]);
if (level == 0) if (level == 0)
{ {
...@@ -338,9 +374,16 @@ void Texture2D::setImage(GLint level, GLenum internalFormat, GLsizei width, GLsi ...@@ -338,9 +374,16 @@ void Texture2D::setImage(GLint level, GLenum internalFormat, GLsizei width, GLsi
} }
} }
void Texture2D::subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels)
{
Texture::subImage(xoffset, yoffset, width, height, format, type, pixels, &mImageArray[level]);
}
// 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.
bool Texture2D::isComplete() bool Texture2D::isComplete() const
{ {
ASSERT(mWidth == mImageArray[0].width && mHeight == mImageArray[0].height);
if (mWidth <= 0 || mHeight <= 0) if (mWidth <= 0 || mHeight <= 0)
{ {
return false; return false;
...@@ -374,16 +417,6 @@ bool Texture2D::isComplete() ...@@ -374,16 +417,6 @@ bool Texture2D::isComplete()
return false; return false;
} }
if (mImageArray[level].internalFormat != mImageArray[0].internalFormat)
{
return false;
}
if (mImageArray[level].type != mImageArray[0].type)
{
return false;
}
if (mImageArray[level].width != (mImageArray[level - 1].width + 1) / 2) if (mImageArray[level].width != (mImageArray[level - 1].width + 1) / 2)
{ {
return false; return false;
...@@ -414,43 +447,44 @@ IDirect3DBaseTexture9 *Texture2D::createTexture() ...@@ -414,43 +447,44 @@ IDirect3DBaseTexture9 *Texture2D::createTexture()
return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL); return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
} }
ASSERT(SUCCEEDED(result)); if (mTexture) mTexture->Release();
mTexture = texture;
return texture;
}
void Texture2D::updateTexture()
{
IDirect3DDevice9 *device = getDevice();
D3DFORMAT format = selectFormat(mImageArray[0]);
IDirect3DTexture9 *lockableTexture; IDirect3DTexture9 *lockableTexture;
result = device->CreateTexture(mWidth, mHeight, 0, D3DUSAGE_DYNAMIC, format, D3DPOOL_SYSTEMMEM, &lockableTexture, NULL); HRESULT result = device->CreateTexture(mWidth, mHeight, 0, D3DUSAGE_DYNAMIC, format, D3DPOOL_SYSTEMMEM, &lockableTexture, NULL);
if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
{ {
texture->Release(); return error(GL_OUT_OF_MEMORY);
return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
} }
ASSERT(SUCCEEDED(result)); int levelCount = mTexture->GetLevelCount();
int levelCount = texture->GetLevelCount();
for (int level = 0; level < levelCount; level++) for (int level = 0; level < levelCount; level++)
{ {
D3DLOCKED_RECT lock = {0}; D3DLOCKED_RECT lock = {0};
lockableTexture->LockRect(level, &lock, NULL, 0); lockableTexture->LockRect(level, &lock, NULL, 0);
copyImage(lock, format, &mImageArray[level]); copyImage(lock, format, mImageArray[level]);
lockableTexture->UnlockRect(level); lockableTexture->UnlockRect(level);
} }
device->UpdateTexture(lockableTexture, texture); device->UpdateTexture(lockableTexture, mTexture);
lockableTexture->Release(); lockableTexture->Release();
if (mTexture) mTexture->Release();
mTexture = texture;
return texture;
} }
// Returns the top-level texture surface as a render target // Returns the top-level texture surface as a render target
IDirect3DSurface9 *Texture2D::getRenderTarget() IDirect3DSurface9 *Texture2D::getRenderTarget()
{ {
if (mDirtyImageData && mRenderTarget) if (mDirtyMetaData && mRenderTarget)
{ {
mRenderTarget->Release(); mRenderTarget->Release();
mRenderTarget = NULL; mRenderTarget = NULL;
...@@ -513,8 +547,13 @@ void TextureCubeMap::setImageNegZ(GLint level, GLenum internalFormat, GLsizei wi ...@@ -513,8 +547,13 @@ void TextureCubeMap::setImageNegZ(GLint level, GLenum internalFormat, GLsizei wi
setImage(5, level, internalFormat, width, height, format, type, pixels); setImage(5, level, internalFormat, width, height, format, type, pixels);
} }
void TextureCubeMap::subImage(GLenum face, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels)
{
Texture::subImage(xoffset, yoffset, width, height, format, type, pixels, &mImageArray[faceIndex(face)][level]);
}
// 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.
bool TextureCubeMap::isComplete() bool TextureCubeMap::isComplete() const
{ {
if (mWidth <= 0 || mHeight <= 0 || mWidth != mHeight) if (mWidth <= 0 || mHeight <= 0 || mWidth != mHeight)
{ {
...@@ -559,16 +598,6 @@ bool TextureCubeMap::isComplete() ...@@ -559,16 +598,6 @@ bool TextureCubeMap::isComplete()
return false; return false;
} }
if (mImageArray[face][level].internalFormat != mImageArray[0][0].internalFormat)
{
return false;
}
if (mImageArray[face][level].type != mImageArray[0][0].type)
{
return false;
}
if (mImageArray[face][level].width != (mImageArray[0][level - 1].width + 1) / 2) if (mImageArray[face][level].width != (mImageArray[0][level - 1].width + 1) / 2)
{ {
return false; return false;
...@@ -600,15 +629,23 @@ IDirect3DBaseTexture9 *TextureCubeMap::createTexture() ...@@ -600,15 +629,23 @@ IDirect3DBaseTexture9 *TextureCubeMap::createTexture()
return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL); return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
} }
ASSERT(SUCCEEDED(result)); if (mTexture) mTexture->Release();
mTexture = texture;
return mTexture;
}
void TextureCubeMap::updateTexture()
{
IDirect3DDevice9 *device = getDevice();
D3DFORMAT format = selectFormat(mImageArray[0][0]);
IDirect3DCubeTexture9 *lockableTexture; IDirect3DCubeTexture9 *lockableTexture;
result = device->CreateCubeTexture(mWidth, 0, D3DUSAGE_DYNAMIC, format, D3DPOOL_SYSTEMMEM, &lockableTexture, NULL); HRESULT result = device->CreateCubeTexture(mWidth, 0, D3DUSAGE_DYNAMIC, format, D3DPOOL_SYSTEMMEM, &lockableTexture, NULL);
if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
{ {
texture->Release(); return error(GL_OUT_OF_MEMORY);
return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
} }
ASSERT(SUCCEEDED(result)); ASSERT(SUCCEEDED(result));
...@@ -620,29 +657,19 @@ IDirect3DBaseTexture9 *TextureCubeMap::createTexture() ...@@ -620,29 +657,19 @@ IDirect3DBaseTexture9 *TextureCubeMap::createTexture()
D3DLOCKED_RECT lock = {0}; D3DLOCKED_RECT lock = {0};
lockableTexture->LockRect((D3DCUBEMAP_FACES)face, level, &lock, NULL, 0); lockableTexture->LockRect((D3DCUBEMAP_FACES)face, level, &lock, NULL, 0);
copyImage(lock, format, &mImageArray[face][level]); copyImage(lock, format, mImageArray[face][level]);
lockableTexture->UnlockRect((D3DCUBEMAP_FACES)face, level); lockableTexture->UnlockRect((D3DCUBEMAP_FACES)face, level);
} }
} }
device->UpdateTexture(lockableTexture, texture); device->UpdateTexture(lockableTexture, mTexture);
lockableTexture->Release(); lockableTexture->Release();
if (mTexture) mTexture->Release();
mTexture = texture;
return mTexture;
} }
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)
{ {
if (level < 0 || level >= MAX_TEXTURE_LEVELS) Texture::setImage(width, height, format, type, pixels, &mImageArray[face][level]);
{
return;
}
Texture::setImage(internalFormat, width, height, format, type, pixels, &mImageArray[face][level]);
if (face == 0 && level == 0) if (face == 0 && level == 0)
{ {
...@@ -650,4 +677,16 @@ void TextureCubeMap::setImage(int face, GLint level, GLenum internalFormat, GLsi ...@@ -650,4 +677,16 @@ void TextureCubeMap::setImage(int face, GLint level, GLenum internalFormat, GLsi
mHeight = height; mHeight = height;
} }
} }
unsigned int TextureCubeMap::faceIndex(GLenum face)
{
META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_X - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 1);
META_ASSERT(GL_TEXTURE_CUBE_MAP_POSITIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 2);
META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 3);
META_ASSERT(GL_TEXTURE_CUBE_MAP_POSITIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 4);
META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 5);
return face - GL_TEXTURE_CUBE_MAP_POSITIVE_X;
}
} }
...@@ -26,7 +26,7 @@ enum ...@@ -26,7 +26,7 @@ enum
MAX_TEXTURE_SIZE = 2048, MAX_TEXTURE_SIZE = 2048,
MAX_CUBE_MAP_TEXTURE_SIZE = 2048, MAX_CUBE_MAP_TEXTURE_SIZE = 2048,
MAX_TEXTURE_LEVELS = 11 // log2 of MAX_TEXTURE_SIZE MAX_TEXTURE_LEVELS = 12 // 1+log2 of MAX_TEXTURE_SIZE
}; };
class Texture : public Colorbuffer class Texture : public Colorbuffer
...@@ -43,48 +43,54 @@ class Texture : public Colorbuffer ...@@ -43,48 +43,54 @@ class Texture : public Colorbuffer
bool setWrapS(GLenum wrap); bool setWrapS(GLenum wrap);
bool setWrapT(GLenum wrap); bool setWrapT(GLenum wrap);
GLenum getMinFilter(); GLenum getMinFilter() const;
GLenum getMagFilter(); GLenum getMagFilter() const;
GLenum getWrapS(); GLenum getWrapS() const;
GLenum getWrapT(); GLenum getWrapT() const;
virtual bool isComplete() = 0; virtual bool isComplete() const = 0;
IDirect3DBaseTexture9 *getTexture(); IDirect3DBaseTexture9 *getTexture();
protected: protected:
// Helper structure representing a single image layer // Helper structure representing a single image layer
struct Image struct Image
{ {
GLenum internalFormat;
GLsizei width; GLsizei width;
GLsizei height; GLsizei height;
GLenum format; GLenum format;
GLenum type;
std::vector<unsigned char> pixels; std::vector<unsigned char> pixels;
}; };
void copyImage(const D3DLOCKED_RECT &lock, D3DFORMAT format, Image *image); void copyImage(const D3DLOCKED_RECT &lock, D3DFORMAT format, const Image &image);
static D3DFORMAT selectFormat(const Image &image); static D3DFORMAT selectFormat(const Image &image);
static int pixelSize(const Image &image); static int pixelSize(GLenum format, GLenum type);
int imagePitch(const Image& img) const;
GLenum mMinFilter; GLenum mMinFilter;
GLenum mMagFilter; GLenum mMagFilter;
GLenum mWrapS; GLenum mWrapS;
GLenum mWrapT; GLenum mWrapT;
void setImage(GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels, Image *img); void setImage(GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels, Image *img);
void subImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels, Image *img);
// 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;
bool mDirtyImageData; // FIXME: would be private but getRenderTarget is still implemented through the derived classes and they need it. 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 mDirtyImageData;
void loadImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type,
const void *input, std::size_t outputPitch, void *output) const;
}; };
class Texture2D : public Texture class Texture2D : public Texture
...@@ -97,24 +103,16 @@ class Texture2D : public Texture ...@@ -97,24 +103,16 @@ class Texture2D : public Texture
GLenum getTarget() const; GLenum getTarget() const;
void setImage(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels); void setImage(GLint level, GLenum internalFormat, 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 setMinFilter(GLenum filter); bool isComplete() const;
bool setMagFilter(GLenum filter);
bool setWrapS(GLenum wrap);
bool setWrapT(GLenum wrap);
GLenum getMinFilter();
GLenum getMagFilter();
GLenum getWrapS();
GLenum getWrapT();
bool isComplete();
IDirect3DSurface9 *getRenderTarget(); IDirect3DSurface9 *getRenderTarget();
private: private:
DISALLOW_COPY_AND_ASSIGN(Texture2D); DISALLOW_COPY_AND_ASSIGN(Texture2D);
virtual IDirect3DBaseTexture9 *createTexture(); virtual IDirect3DBaseTexture9 *createTexture();
virtual void updateTexture();
Image mImageArray[MAX_TEXTURE_LEVELS]; Image mImageArray[MAX_TEXTURE_LEVELS];
...@@ -137,12 +135,17 @@ class TextureCubeMap : public Texture ...@@ -137,12 +135,17 @@ class TextureCubeMap : public Texture
void setImagePosZ(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels); void setImagePosZ(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels);
void setImageNegZ(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels); void setImageNegZ(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels);
bool isComplete(); void subImage(GLenum face, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels);
bool isComplete() const;
private: private:
DISALLOW_COPY_AND_ASSIGN(TextureCubeMap); DISALLOW_COPY_AND_ASSIGN(TextureCubeMap);
virtual IDirect3DBaseTexture9 *createTexture(); virtual IDirect3DBaseTexture9 *createTexture();
virtual void updateTexture();
static unsigned int faceIndex(GLenum face);
void setImage(int face, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels); void setImage(int face, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels);
......
...@@ -10,6 +10,9 @@ ...@@ -10,6 +10,9 @@
#include <GLES2/gl2.h> #include <GLES2/gl2.h>
#include <GLES2/gl2ext.h> #include <GLES2/gl2ext.h>
#include <exception>
#include <limits>
#include "Context.h" #include "Context.h"
#include "main.h" #include "main.h"
#include "Program.h" #include "Program.h"
...@@ -20,8 +23,7 @@ ...@@ -20,8 +23,7 @@
#include "Framebuffer.h" #include "Framebuffer.h"
#include "mathutil.h" #include "mathutil.h"
#include "debug.h" #include "debug.h"
#include "utilities.h"
#include <exception>
extern "C" extern "C"
{ {
...@@ -3147,12 +3149,62 @@ void __stdcall glTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint ...@@ -3147,12 +3149,62 @@ void __stdcall glTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint
try try
{ {
if (width < 0 || height < 0) if (target != GL_TEXTURE_2D && !es2dx::IsCubemapTextureTarget(target))
{
return error(GL_INVALID_ENUM);
}
if (level < 0 || level > gl::MAX_TEXTURE_LEVELS || xoffset < 0 || yoffset < 0 || width < 0 || height < 0)
{ {
return error(GL_INVALID_VALUE); return error(GL_INVALID_VALUE);
} }
UNIMPLEMENTED(); // FIXME if (std::numeric_limits<GLsizei>::max() - xoffset < width || std::numeric_limits<GLsizei>::max() - yoffset < height)
{
return error(GL_INVALID_VALUE);
}
if (!es2dx::CheckTextureFormatType(format, type))
{
return error(GL_INVALID_ENUM);
}
if (width == 0 || height == 0 || pixels == NULL)
{
return;
}
gl::Context *context = gl::getContext();
if (context)
{
if (target == GL_TEXTURE_2D)
{
gl::Texture2D *texture = context->getTexture2D();
if (!texture)
{
return error(GL_INVALID_OPERATION);
}
texture->subImage(level, xoffset, yoffset, width, height, format, type, pixels);
}
else if (es2dx::IsCubemapTextureTarget(target))
{
gl::TextureCubeMap *texture = context->getTextureCubeMap();
if (!texture)
{
return error(GL_INVALID_OPERATION);
}
texture->subImage(target, level, xoffset, yoffset, width, height, format, type, pixels);
}
else
{
UNREACHABLE();
}
}
} }
catch(std::bad_alloc&) catch(std::bad_alloc&)
{ {
......
...@@ -355,4 +355,40 @@ bool ConvertPrimitiveType(GLenum primitiveType, GLsizei primitiveCount, ...@@ -355,4 +355,40 @@ bool ConvertPrimitiveType(GLenum primitiveType, GLsizei primitiveCount,
return true; return true;
} }
bool IsCubemapTextureTarget(GLenum target)
{
return (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z);
}
// Verify that format/type are one of the combinations from table 3.4.
bool CheckTextureFormatType(GLenum format, GLenum type)
{
switch (type)
{
case GL_UNSIGNED_BYTE:
switch (format)
{
case GL_RGBA:
case GL_RGB:
case GL_ALPHA:
case GL_LUMINANCE:
case GL_LUMINANCE_ALPHA:
return true;
default:
return false;
}
case GL_UNSIGNED_SHORT_4_4_4_4:
case GL_UNSIGNED_SHORT_5_5_5_1:
return (format == GL_RGBA);
case GL_UNSIGNED_SHORT_5_6_5:
return (format == GL_RGB);
default:
return false;
}
}
} }
...@@ -39,6 +39,8 @@ unsigned int GetDepthSize(D3DFORMAT depthFormat); ...@@ -39,6 +39,8 @@ unsigned int GetDepthSize(D3DFORMAT depthFormat);
unsigned int GetStencilSize(D3DFORMAT stencilFormat); unsigned int GetStencilSize(D3DFORMAT stencilFormat);
bool ConvertPrimitiveType(GLenum primitiveType, GLsizei primitiveCount, bool ConvertPrimitiveType(GLenum primitiveType, GLsizei primitiveCount,
D3DPRIMITIVETYPE *d3dPrimitiveType, int *d3dPrimitiveCount); D3DPRIMITIVETYPE *d3dPrimitiveType, int *d3dPrimitiveCount);
bool IsCubemapTextureTarget(GLenum target);
bool CheckTextureFormatType(GLenum format, GLenum type);
} }
......
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