Hash :
ea0e1af4
Author :
Date :
2010-03-22T19:33:14
Minor reshuffling of directory structure in preparation of ESSL to GLSL compiler work. 1. Added include/GLSLANG which includes compiler API 2. Deleted src/include and moved the header files to the same directory as the corresponding source files 3. Modied include path to be relative to src/. I have only fixed paths for files I moved. We should fix it for all new files at least. It is much easier to see where an included file is coming from. I noticed that a few libGLESv2 source files include headers from libEGL project, which seems wrong. I think we should address this issue. Next step: move compiler source files to compiler/frontend and create two new projects compiler/glsl_backend and compiler/hlsl_backend. Review URL: http://codereview.appspot.com/662042 git-svn-id: https://angleproject.googlecode.com/svn/trunk@62 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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
//
// Copyright (c) 2002-2010 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.
//
// Texture.cpp: Implements the gl::Texture class and its derived classes
// Texture2D and TextureCubeMap. Implements GL texture objects and related
// functionality. [OpenGL ES 2.0.24] section 3.7 page 63.
#include "Texture.h"
#include <algorithm>
#include "main.h"
#include "mathutil.h"
#include "common/debug.h"
#include "utilities.h"
namespace gl
{
Texture::Image::Image()
: dirty(false), surface(NULL)
{
}
Texture::Image::~Image()
{
if (surface) surface->Release();
}
Texture::Texture() : Colorbuffer(0)
{
mMinFilter = GL_NEAREST_MIPMAP_LINEAR;
mMagFilter = GL_LINEAR;
mWrapS = GL_REPEAT;
mWrapT = GL_REPEAT;
mDirtyMetaData = true;
}
Texture::~Texture()
{
}
// Returns true on successful filter state update (valid enum parameter)
bool Texture::setMinFilter(GLenum filter)
{
switch (filter)
{
case GL_NEAREST:
case GL_LINEAR:
case GL_NEAREST_MIPMAP_NEAREST:
case GL_LINEAR_MIPMAP_NEAREST:
case GL_NEAREST_MIPMAP_LINEAR:
case GL_LINEAR_MIPMAP_LINEAR:
mMinFilter = filter;
return true;
default:
return false;
}
}
// Returns true on successful filter state update (valid enum parameter)
bool Texture::setMagFilter(GLenum filter)
{
switch (filter)
{
case GL_NEAREST:
case GL_LINEAR:
mMagFilter = filter;
return true;
default:
return false;
}
}
// Returns true on successful wrap state update (valid enum parameter)
bool Texture::setWrapS(GLenum wrap)
{
switch (wrap)
{
case GL_REPEAT:
case GL_CLAMP_TO_EDGE:
case GL_MIRRORED_REPEAT:
mWrapS = wrap;
return true;
default:
return false;
}
}
// Returns true on successful wrap state update (valid enum parameter)
bool Texture::setWrapT(GLenum wrap)
{
switch (wrap)
{
case GL_REPEAT:
case GL_CLAMP_TO_EDGE:
case GL_MIRRORED_REPEAT:
mWrapT = wrap;
return true;
default:
return false;
}
}
GLenum Texture::getMinFilter() const
{
return mMinFilter;
}
GLenum Texture::getMagFilter() const
{
return mMagFilter;
}
GLenum Texture::getWrapS() const
{
return mWrapS;
}
GLenum Texture::getWrapT() const
{
return mWrapT;
}
// Selects an internal Direct3D 9 format for storing an Image
D3DFORMAT Texture::selectFormat(GLenum format)
{
return D3DFMT_A8R8G8B8;
}
// 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;
}
GLsizei Texture::computePitch(GLsizei width, GLenum format, GLenum type, GLint alignment) const
{
ASSERT(alignment > 0 && isPow2(alignment));
GLsizei rawPitch = pixelSize(format, type) * width;
return (rawPitch + alignment - 1) & ~(alignment - 1);
}
// 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,
GLint unpackAlignment, const void *input, size_t outputPitch, void *output) const
{
GLsizei inputPitch = computePitch(width, format, type, unpackAlignment);
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 g;
unsigned char b;
unsigned char a;
switch (format)
{
case GL_ALPHA:
a = source[x];
r = 0;
g = 0;
b = 0;
break;
case GL_LUMINANCE:
r = source[x];
g = source[x];
b = source[x];
a = 0xFF;
break;
case GL_LUMINANCE_ALPHA:
r = source[2*x+0];
g = source[2*x+0];
b = source[2*x+0];
a = source[2*x+1];
break;
case GL_RGB:
switch (type)
{
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:
{
unsigned short rgba = source16[x];
a = 0xFF;
b = ((rgba & 0x001F) << 3) | ((rgba & 0x001F) >> 2);
g = ((rgba & 0x07E0) >> 3) | ((rgba & 0x07E0) >> 9);
r = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13);
}
break;
default: UNREACHABLE();
}
break;
case GL_RGBA:
switch (type)
{
case GL_UNSIGNED_BYTE:
r = source[x * 4 + 0];
g = source[x * 4 + 1];
b = source[x * 4 + 2];
a = source[x * 4 + 3];
break;
case GL_UNSIGNED_SHORT_4_4_4_4:
{
unsigned short rgba = source16[x];
a = ((rgba & 0x000F) << 4) | ((rgba & 0x000F) >> 0);
b = ((rgba & 0x00F0) << 0) | ((rgba & 0x00F0) >> 4);
g = ((rgba & 0x0F00) >> 4) | ((rgba & 0x0F00) >> 8);
r = ((rgba & 0xF000) >> 8) | ((rgba & 0xF000) >> 12);
}
break;
case GL_UNSIGNED_SHORT_5_5_5_1:
{
unsigned short rgba = source16[x];
a = (rgba & 0x0001) ? 0xFF : 0;
b = ((rgba & 0x003E) << 2) | ((rgba & 0x003E) >> 3);
g = ((rgba & 0x07C0) >> 3) | ((rgba & 0x07C0) >> 8);
r = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13);
}
break;
default: UNREACHABLE();
}
break;
default: UNREACHABLE();
}
dest[4 * x + 0] = b;
dest[4 * x + 1] = g;
dest[4 * x + 2] = r;
dest[4 * x + 3] = a;
}
}
}
void Texture::setImage(GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, Image *img)
{
IDirect3DSurface9 *newSurface = NULL;
HRESULT result = getDevice()->CreateOffscreenPlainSurface(width, height, selectFormat(format), D3DPOOL_SYSTEMMEM, &newSurface, NULL);
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
return error(GL_OUT_OF_MEMORY);
}
if (img->surface) img->surface->Release();
img->surface = newSurface;
img->width = width;
img->height = height;
img->format = format;
if (pixels != NULL)
{
D3DLOCKED_RECT locked;
HRESULT result = newSurface->LockRect(&locked, NULL, 0);
ASSERT(SUCCEEDED(result));
if (SUCCEEDED(result))
{
loadImageData(0, 0, width, height, format, type, unpackAlignment, pixels, locked.Pitch, locked.pBits);
newSurface->UnlockRect();
}
img->dirty = true;
}
mDirtyMetaData = true;
}
void Texture::subImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, Image *img)
{
if (width + xoffset > img->width || height + yoffset > img->height) return error(GL_INVALID_VALUE);
D3DLOCKED_RECT locked;
HRESULT result = img->surface->LockRect(&locked, NULL, 0);
ASSERT(SUCCEEDED(result));
if (SUCCEEDED(result))
{
loadImageData(xoffset, yoffset, width, height, format, type, unpackAlignment, pixels, locked.Pitch, locked.pBits);
img->surface->UnlockRect();
}
img->dirty = true;
}
IDirect3DBaseTexture9 *Texture::getTexture()
{
if (!isComplete())
{
return NULL;
}
if (mDirtyMetaData)
{
mBaseTexture = createTexture();
}
if (mDirtyMetaData || dirtyImageData())
{
updateTexture();
}
mDirtyMetaData = false;
ASSERT(!dirtyImageData());
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()
{
mTexture = NULL;
}
Texture2D::~Texture2D()
{
if (mTexture)
{
mTexture->Release();
mTexture = NULL;
}
}
GLenum Texture2D::getTarget() const
{
return GL_TEXTURE_2D;
}
void Texture2D::setImage(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
{
Texture::setImage(width, height, format, type, unpackAlignment, pixels, &mImageArray[level]);
if (level == 0)
{
mWidth = width;
mHeight = height;
}
}
void Texture2D::commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
{
ASSERT(mImageArray[level].surface != NULL);
if (mTexture != NULL)
{
IDirect3DSurface9 *destLevel = NULL;
HRESULT result = mTexture->GetSurfaceLevel(level, &destLevel);
ASSERT(SUCCEEDED(result));
if (SUCCEEDED(result))
{
Image *img = &mImageArray[level];
RECT sourceRect;
sourceRect.left = xoffset;
sourceRect.top = yoffset;
sourceRect.right = xoffset + width;
sourceRect.bottom = yoffset + height;
POINT destPoint;
destPoint.x = xoffset;
destPoint.y = yoffset;
result = getDevice()->UpdateSurface(img->surface, &sourceRect, destLevel, &destPoint);
ASSERT(SUCCEEDED(result));
destLevel->Release();
img->dirty = false;
}
}
}
void Texture2D::subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
{
Texture::subImage(xoffset, yoffset, width, height, format, type, unpackAlignment, pixels, &mImageArray[level]);
commitRect(level, xoffset, yoffset, width, height);
}
// Tests for GL texture object completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81.
bool Texture2D::isComplete() const
{
ASSERT(mWidth == mImageArray[0].width && mHeight == mImageArray[0].height);
if (mWidth <= 0 || mHeight <= 0)
{
return false;
}
bool mipmapping;
switch (mMinFilter)
{
case GL_NEAREST:
case GL_LINEAR:
mipmapping = false;
break;
case GL_NEAREST_MIPMAP_NEAREST:
case GL_LINEAR_MIPMAP_NEAREST:
case GL_NEAREST_MIPMAP_LINEAR:
case GL_LINEAR_MIPMAP_LINEAR:
mipmapping = true;
break;
default: UNREACHABLE();
}
if (mipmapping)
{
int q = log2(std::max(mWidth, mHeight));
for (int level = 1; level <= q; level++)
{
if (mImageArray[level].format != mImageArray[0].format)
{
return false;
}
if (mImageArray[level].width != (mImageArray[level - 1].width + 1) / 2)
{
return false;
}
if (mImageArray[level].height != (mImageArray[level - 1].height + 1) / 2)
{
return false;
}
}
}
return true;
}
// Constructs a Direct3D 9 texture resource from the texture images, or returns an existing one
IDirect3DBaseTexture9 *Texture2D::createTexture()
{
IDirect3DTexture9 *texture;
IDirect3DDevice9 *device = getDevice();
D3DFORMAT format = selectFormat(mImageArray[0].format);
HRESULT result = device->CreateTexture(mWidth, mHeight, 0, 0, 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) mTexture->Release();
mTexture = texture;
return texture;
}
void Texture2D::updateTexture()
{
IDirect3DDevice9 *device = getDevice();
int levelCount = mTexture->GetLevelCount();
for (int level = 0; level < levelCount; level++)
{
if (mImageArray[level].dirty)
{
IDirect3DSurface9 *levelSurface = NULL;
HRESULT result = mTexture->GetSurfaceLevel(level, &levelSurface);
ASSERT(SUCCEEDED(result));
if (SUCCEEDED(result))
{
result = device->UpdateSurface(mImageArray[level].surface, NULL, levelSurface, NULL);
ASSERT(SUCCEEDED(result));
levelSurface->Release();
mImageArray[level].dirty = false;
}
}
}
}
IDirect3DBaseTexture9 *Texture2D::convertToRenderTarget()
{
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))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
}
if (mTexture != NULL)
{
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();
}
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
{
int q = log2(std::max(mWidth, mHeight));
for (int i = 0; i <= q; i++)
{
if (mImageArray[i].dirty) return true;
}
return false;
}
TextureCubeMap::TextureCubeMap()
{
mTexture = NULL;
}
TextureCubeMap::~TextureCubeMap()
{
if (mTexture)
{
mTexture->Release();
mTexture = NULL;
}
}
GLenum TextureCubeMap::getTarget() const
{
return GL_TEXTURE_CUBE_MAP;
}
void TextureCubeMap::setImagePosX(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
{
setImage(0, level, internalFormat, width, height, format, type, unpackAlignment, pixels);
}
void TextureCubeMap::setImageNegX(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
{
setImage(1, level, internalFormat, width, height, format, type, unpackAlignment, pixels);
}
void TextureCubeMap::setImagePosY(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
{
setImage(2, level, internalFormat, width, height, format, type, unpackAlignment, pixels);
}
void TextureCubeMap::setImageNegY(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
{
setImage(3, level, internalFormat, width, height, format, type, unpackAlignment, pixels);
}
void TextureCubeMap::setImagePosZ(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
{
setImage(4, level, internalFormat, width, height, format, type, unpackAlignment, pixels);
}
void TextureCubeMap::setImageNegZ(GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
{
setImage(5, level, internalFormat, width, height, format, type, unpackAlignment, pixels);
}
void TextureCubeMap::commitRect(GLenum faceTarget, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
{
int face = faceIndex(faceTarget);
ASSERT(mImageArray[face][level].surface != NULL);
if (mTexture != NULL)
{
IDirect3DSurface9 *destLevel = NULL;
HRESULT result = mTexture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(face), level, &destLevel);
ASSERT(SUCCEEDED(result));
if (SUCCEEDED(result))
{
Image *img = &mImageArray[face][level];
RECT sourceRect;
sourceRect.left = xoffset;
sourceRect.top = yoffset;
sourceRect.right = xoffset + width;
sourceRect.bottom = yoffset + height;
POINT destPoint;
destPoint.x = xoffset;
destPoint.y = yoffset;
result = getDevice()->UpdateSurface(img->surface, &sourceRect, destLevel, &destPoint);
ASSERT(SUCCEEDED(result));
destLevel->Release();
img->dirty = false;
}
}
}
void TextureCubeMap::subImage(GLenum face, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels)
{
Texture::subImage(xoffset, yoffset, width, height, format, type, unpackAlignment, pixels, &mImageArray[faceIndex(face)][level]);
commitRect(face, level, xoffset, yoffset, width, height);
}
// Tests for GL texture object completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81.
bool TextureCubeMap::isComplete() const
{
if (mWidth <= 0 || mHeight <= 0 || mWidth != mHeight)
{
return false;
}
bool mipmapping;
switch (mMinFilter)
{
case GL_NEAREST:
case GL_LINEAR:
mipmapping = false;
break;
case GL_NEAREST_MIPMAP_NEAREST:
case GL_LINEAR_MIPMAP_NEAREST:
case GL_NEAREST_MIPMAP_LINEAR:
case GL_LINEAR_MIPMAP_LINEAR:
mipmapping = true;
break;
default: UNREACHABLE();
}
for (int face = 0; face < 6; face++)
{
if (mImageArray[face][0].width != mWidth || mImageArray[face][0].height != mHeight)
{
return false;
}
}
if (mipmapping)
{
int q = log2(mWidth);
for (int face = 0; face < 6; face++)
{
for (int level = 1; level <= q; level++)
{
if (mImageArray[face][level].format != mImageArray[0][0].format)
{
return false;
}
if (mImageArray[face][level].width != (mImageArray[0][level - 1].width + 1) / 2)
{
return false;
}
if (mImageArray[face][level].height != (mImageArray[0][level - 1].height + 1) / 2)
{
return false;
}
}
}
}
return true;
}
// Constructs a Direct3D 9 texture resource from the texture images, or returns an existing one
IDirect3DBaseTexture9 *TextureCubeMap::createTexture()
{
IDirect3DDevice9 *device = getDevice();
D3DFORMAT format = selectFormat(mImageArray[0][0].format);
IDirect3DCubeTexture9 *texture;
HRESULT result = device->CreateCubeTexture(mWidth, 0, 0, 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) mTexture->Release();
mTexture = texture;
return mTexture;
}
void TextureCubeMap::updateTexture()
{
IDirect3DDevice9 *device = getDevice();
for (int face = 0; face < 6; face++)
{
for (int level = 0; level <= log2(mWidth); level++)
{
Image *img = &mImageArray[face][level];
if (img->dirty)
{
IDirect3DSurface9 *levelSurface;
HRESULT result = mTexture->GetCubeMapSurface(static_cast<D3DCUBEMAP_FACES>(face), level, &levelSurface);
ASSERT(SUCCEEDED(result));
if (SUCCEEDED(result))
{
result = device->UpdateSurface(img->surface, NULL, levelSurface, NULL);
ASSERT(SUCCEEDED(result));
levelSurface->Release();
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, GLint unpackAlignment, const void *pixels)
{
Texture::setImage(width, height, format, type, unpackAlignment, pixels, &mImageArray[face][level]);
if (face == 0 && level == 0)
{
mWidth = width;
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;
}
bool TextureCubeMap::dirtyImageData() const
{
int q = log2(mWidth);
for (int f = 0; f < 6; f++)
{
for (int i = 0; i <= q; i++)
{
if (mImageArray[f][i].dirty) return true;
}
}
return false;
}
}