Hash :
b64ed282
Author :
Date :
2012-11-28T20:54:02
Adds makeRenderer9/11 functions TRAC #22134 Signed-off-by: Nicolas Capens Signed-off-by: Daniel Koch git-svn-id: https://angleproject.googlecode.com/svn/branches/dx11proto@1479 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 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
//
// 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.
//
// Image.cpp: Implements the rx::Image class, which acts as the interface to
// the actual underlying surfaces of a Texture.
#include "libGLESv2/renderer/Image.h"
#include "libEGL/Display.h"
#include "libGLESv2/main.h"
#include "libGLESv2/mathutil.h"
#include "libGLESv2/utilities.h"
#include "libGLESv2/Texture.h"
#include "libGLESv2/Framebuffer.h"
#include "libGLESv2/renderer/RenderTarget9.h"
#include "libGLESv2/renderer/renderer9_utils.h"
namespace rx
{
namespace
{
struct L8
{
unsigned char L;
static void average(L8 *dst, const L8 *src1, const L8 *src2)
{
dst->L = ((src1->L ^ src2->L) >> 1) + (src1->L & src2->L);
}
};
struct A8L8
{
unsigned char L;
unsigned char A;
static void average(A8L8 *dst, const A8L8 *src1, const A8L8 *src2)
{
*(unsigned short*)dst = (((*(unsigned short*)src1 ^ *(unsigned short*)src2) & 0xFEFE) >> 1) + (*(unsigned short*)src1 & *(unsigned short*)src2);
}
};
struct A8R8G8B8
{
unsigned char B;
unsigned char G;
unsigned char R;
unsigned char A;
static void average(A8R8G8B8 *dst, const A8R8G8B8 *src1, const A8R8G8B8 *src2)
{
*(unsigned int*)dst = (((*(unsigned int*)src1 ^ *(unsigned int*)src2) & 0xFEFEFEFE) >> 1) + (*(unsigned int*)src1 & *(unsigned int*)src2);
}
};
struct A16B16G16R16F
{
unsigned short R;
unsigned short G;
unsigned short B;
unsigned short A;
static void average(A16B16G16R16F *dst, const A16B16G16R16F *src1, const A16B16G16R16F *src2)
{
dst->R = gl::float32ToFloat16((gl::float16ToFloat32(src1->R) + gl::float16ToFloat32(src2->R)) * 0.5f);
dst->G = gl::float32ToFloat16((gl::float16ToFloat32(src1->G) + gl::float16ToFloat32(src2->G)) * 0.5f);
dst->B = gl::float32ToFloat16((gl::float16ToFloat32(src1->B) + gl::float16ToFloat32(src2->B)) * 0.5f);
dst->A = gl::float32ToFloat16((gl::float16ToFloat32(src1->A) + gl::float16ToFloat32(src2->A)) * 0.5f);
}
};
struct A32B32G32R32F
{
float R;
float G;
float B;
float A;
static void average(A32B32G32R32F *dst, const A32B32G32R32F *src1, const A32B32G32R32F *src2)
{
dst->R = (src1->R + src2->R) * 0.5f;
dst->G = (src1->G + src2->G) * 0.5f;
dst->B = (src1->B + src2->B) * 0.5f;
dst->A = (src1->A + src2->A) * 0.5f;
}
};
template <typename T>
void GenerateMip(unsigned int sourceWidth, unsigned int sourceHeight,
const unsigned char *sourceData, int sourcePitch,
unsigned char *destData, int destPitch)
{
unsigned int mipWidth = std::max(1U, sourceWidth >> 1);
unsigned int mipHeight = std::max(1U, sourceHeight >> 1);
if (sourceHeight == 1)
{
ASSERT(sourceWidth != 1);
const T *src = (const T*)sourceData;
T *dst = (T*)destData;
for (unsigned int x = 0; x < mipWidth; x++)
{
T::average(&dst[x], &src[x * 2], &src[x * 2 + 1]);
}
}
else if (sourceWidth == 1)
{
ASSERT(sourceHeight != 1);
for (unsigned int y = 0; y < mipHeight; y++)
{
const T *src0 = (const T*)(sourceData + y * 2 * sourcePitch);
const T *src1 = (const T*)(sourceData + y * 2 * sourcePitch + sourcePitch);
T *dst = (T*)(destData + y * destPitch);
T::average(dst, src0, src1);
}
}
else
{
for (unsigned int y = 0; y < mipHeight; y++)
{
const T *src0 = (const T*)(sourceData + y * 2 * sourcePitch);
const T *src1 = (const T*)(sourceData + y * 2 * sourcePitch + sourcePitch);
T *dst = (T*)(destData + y * destPitch);
for (unsigned int x = 0; x < mipWidth; x++)
{
T tmp0;
T tmp1;
T::average(&tmp0, &src0[x * 2], &src0[x * 2 + 1]);
T::average(&tmp1, &src1[x * 2], &src1[x * 2 + 1]);
T::average(&dst[x], &tmp0, &tmp1);
}
}
}
}
void GenerateMip(IDirect3DSurface9 *destSurface, IDirect3DSurface9 *sourceSurface)
{
D3DSURFACE_DESC destDesc;
HRESULT result = destSurface->GetDesc(&destDesc);
ASSERT(SUCCEEDED(result));
D3DSURFACE_DESC sourceDesc;
result = sourceSurface->GetDesc(&sourceDesc);
ASSERT(SUCCEEDED(result));
ASSERT(sourceDesc.Format == destDesc.Format);
ASSERT(sourceDesc.Width == 1 || sourceDesc.Width / 2 == destDesc.Width);
ASSERT(sourceDesc.Height == 1 || sourceDesc.Height / 2 == destDesc.Height);
D3DLOCKED_RECT sourceLocked = {0};
result = sourceSurface->LockRect(&sourceLocked, NULL, D3DLOCK_READONLY);
ASSERT(SUCCEEDED(result));
D3DLOCKED_RECT destLocked = {0};
result = destSurface->LockRect(&destLocked, NULL, 0);
ASSERT(SUCCEEDED(result));
const unsigned char *sourceData = reinterpret_cast<const unsigned char*>(sourceLocked.pBits);
unsigned char *destData = reinterpret_cast<unsigned char*>(destLocked.pBits);
if (sourceData && destData)
{
switch (sourceDesc.Format)
{
case D3DFMT_L8:
GenerateMip<L8>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch);
break;
case D3DFMT_A8L8:
GenerateMip<A8L8>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch);
break;
case D3DFMT_A8R8G8B8:
case D3DFMT_X8R8G8B8:
GenerateMip<A8R8G8B8>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch);
break;
case D3DFMT_A16B16G16R16F:
GenerateMip<A16B16G16R16F>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch);
break;
case D3DFMT_A32B32G32R32F:
GenerateMip<A32B32G32R32F>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch);
break;
default:
UNREACHABLE();
break;
}
destSurface->UnlockRect();
sourceSurface->UnlockRect();
}
}
}
Image::Image()
{
mWidth = 0;
mHeight = 0;
mInternalFormat = GL_NONE;
mSurface = NULL;
mDirty = false;
mRenderer = NULL;
mD3DPool = D3DPOOL_SYSTEMMEM;
mD3DFormat = D3DFMT_UNKNOWN;
mActualFormat = GL_NONE;
}
Image::~Image()
{
if (mSurface)
{
mSurface->Release();
}
}
void Image::GenerateMipmap(Image *dest, Image *source)
{
IDirect3DSurface9 *sourceSurface = source->getSurface();
if (sourceSurface == NULL)
return error(GL_OUT_OF_MEMORY);
IDirect3DSurface9 *destSurface = dest->getSurface();
GenerateMip(destSurface, sourceSurface);
source->markDirty();
}
void Image::CopyLockableSurfaces(IDirect3DSurface9 *dest, IDirect3DSurface9 *source)
{
D3DLOCKED_RECT sourceLock = {0};
D3DLOCKED_RECT destLock = {0};
source->LockRect(&sourceLock, NULL, 0);
dest->LockRect(&destLock, NULL, 0);
if (sourceLock.pBits && destLock.pBits)
{
D3DSURFACE_DESC desc;
source->GetDesc(&desc);
int rows = dx::IsCompressedFormat(desc.Format) ? desc.Height / 4 : desc.Height;
int bytes = dx::ComputeRowSize(desc.Format, desc.Width);
ASSERT(bytes <= sourceLock.Pitch && bytes <= destLock.Pitch);
for(int i = 0; i < rows; i++)
{
memcpy((char*)destLock.pBits + destLock.Pitch * i, (char*)sourceLock.pBits + sourceLock.Pitch * i, bytes);
}
source->UnlockRect();
dest->UnlockRect();
}
else UNREACHABLE();
}
bool Image::redefine(rx::Renderer9 *renderer, GLint internalformat, GLsizei width, GLsizei height, bool forceRelease)
{
if (mWidth != width ||
mHeight != height ||
mInternalFormat != internalformat ||
forceRelease)
{
mWidth = width;
mHeight = height;
mInternalFormat = internalformat;
// compute the d3d format that will be used
mD3DFormat = renderer->ConvertTextureInternalFormat(internalformat);
mActualFormat = d3d9_gl::GetEquivalentFormat(mD3DFormat);
mRenderer = Renderer9::makeRenderer9(renderer); // D3D9_REPLACE
if (mSurface)
{
mSurface->Release();
mSurface = NULL;
}
return true;
}
return false;
}
void Image::createSurface()
{
if(mSurface)
{
return;
}
IDirect3DTexture9 *newTexture = NULL;
IDirect3DSurface9 *newSurface = NULL;
const D3DPOOL poolToUse = D3DPOOL_SYSTEMMEM;
const D3DFORMAT d3dFormat = getD3DFormat();
ASSERT(d3dFormat != D3DFMT_INTZ); // We should never get here for depth textures
if (mWidth != 0 && mHeight != 0)
{
int levelToFetch = 0;
GLsizei requestWidth = mWidth;
GLsizei requestHeight = mHeight;
gl::MakeValidSize(true, gl::IsCompressed(mInternalFormat), &requestWidth, &requestHeight, &levelToFetch);
IDirect3DDevice9 *device = mRenderer->getDevice(); // D3D9_REPLACE
HRESULT result = device->CreateTexture(requestWidth, requestHeight, levelToFetch + 1, 0, d3dFormat,
poolToUse, &newTexture, NULL);
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
ERR("Creating image surface failed.");
return error(GL_OUT_OF_MEMORY);
}
newTexture->GetSurfaceLevel(levelToFetch, &newSurface);
newTexture->Release();
}
mSurface = newSurface;
mDirty = false;
mD3DPool = poolToUse;
}
HRESULT Image::lock(D3DLOCKED_RECT *lockedRect, const RECT *rect)
{
createSurface();
HRESULT result = D3DERR_INVALIDCALL;
if (mSurface)
{
result = mSurface->LockRect(lockedRect, rect, 0);
ASSERT(SUCCEEDED(result));
mDirty = true;
}
return result;
}
void Image::unlock()
{
if (mSurface)
{
HRESULT result = mSurface->UnlockRect();
ASSERT(SUCCEEDED(result));
}
}
bool Image::isRenderableFormat() const
{
return TextureStorage::IsTextureFormatRenderable(getD3DFormat());
}
GLenum Image::getActualFormat() const
{
return mActualFormat;
}
D3DFORMAT Image::getD3DFormat() const
{
// this should only happen if the image hasn't been redefined first
// which would be a bug by the caller
ASSERT(mD3DFormat != D3DFMT_UNKNOWN);
return mD3DFormat;
}
IDirect3DSurface9 *Image::getSurface()
{
createSurface();
return mSurface;
}
void Image::setManagedSurface(TextureStorage2D *storage, int level)
{
setManagedSurface(storage->getSurfaceLevel(level, false));
}
void Image::setManagedSurface(TextureStorageCubeMap *storage, int face, int level)
{
setManagedSurface(storage->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, false));
}
void Image::setManagedSurface(IDirect3DSurface9 *surface)
{
D3DSURFACE_DESC desc;
surface->GetDesc(&desc);
ASSERT(desc.Pool == D3DPOOL_MANAGED);
if ((GLsizei)desc.Width == mWidth && (GLsizei)desc.Height == mHeight)
{
if (mSurface)
{
CopyLockableSurfaces(surface, mSurface);
mSurface->Release();
}
mSurface = surface;
mD3DPool = desc.Pool;
}
}
bool Image::updateSurface(TextureStorage2D *storage, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
{
return updateSurface(storage->getSurfaceLevel(level, true), xoffset, yoffset, width, height);
}
bool Image::updateSurface(TextureStorageCubeMap *storage, int face, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
{
return updateSurface(storage->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, true), xoffset, yoffset, width, height);
}
bool Image::updateSurface(IDirect3DSurface9 *destSurface, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
{
if (!destSurface)
return false;
IDirect3DSurface9 *sourceSurface = getSurface();
if (sourceSurface && sourceSurface != destSurface)
{
RECT rect;
rect.left = xoffset;
rect.top = yoffset;
rect.right = xoffset + width;
rect.bottom = yoffset + height;
POINT point = {rect.left, rect.top};
IDirect3DDevice9 *device = mRenderer->getDevice(); // D3D9_REPLACE
if (mD3DPool == D3DPOOL_MANAGED)
{
D3DSURFACE_DESC desc;
sourceSurface->GetDesc(&desc);
IDirect3DSurface9 *surf = 0;
HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
if (SUCCEEDED(result))
{
CopyLockableSurfaces(surf, sourceSurface);
result = device->UpdateSurface(surf, &rect, destSurface, &point);
ASSERT(SUCCEEDED(result));
surf->Release();
}
}
else
{
// UpdateSurface: source must be SYSTEMMEM, dest must be DEFAULT pools
HRESULT result = device->UpdateSurface(sourceSurface, &rect, destSurface, &point);
ASSERT(SUCCEEDED(result));
}
}
destSurface->Release();
return true;
}
// Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input
// into the target pixel rectangle.
void Image::loadData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
GLint unpackAlignment, const void *input)
{
RECT lockRect =
{
xoffset, yoffset,
xoffset + width, yoffset + height
};
D3DLOCKED_RECT locked;
HRESULT result = lock(&locked, &lockRect);
if (FAILED(result))
{
return;
}
GLsizei inputPitch = gl::ComputePitch(width, mInternalFormat, unpackAlignment);
switch (mInternalFormat)
{
case GL_ALPHA8_EXT:
if (gl::supportsSSE2())
{
loadAlphaDataSSE2(width, height, inputPitch, input, locked.Pitch, locked.pBits);
}
else
{
loadAlphaData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
}
break;
case GL_LUMINANCE8_EXT:
loadLuminanceData(width, height, inputPitch, input, locked.Pitch, locked.pBits, getD3DFormat() == D3DFMT_L8);
break;
case GL_ALPHA32F_EXT:
loadAlphaFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
break;
case GL_LUMINANCE32F_EXT:
loadLuminanceFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
break;
case GL_ALPHA16F_EXT:
loadAlphaHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
break;
case GL_LUMINANCE16F_EXT:
loadLuminanceHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
break;
case GL_LUMINANCE8_ALPHA8_EXT:
loadLuminanceAlphaData(width, height, inputPitch, input, locked.Pitch, locked.pBits, getD3DFormat() == D3DFMT_A8L8);
break;
case GL_LUMINANCE_ALPHA32F_EXT:
loadLuminanceAlphaFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
break;
case GL_LUMINANCE_ALPHA16F_EXT:
loadLuminanceAlphaHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
break;
case GL_RGB8_OES:
loadRGBUByteData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
break;
case GL_RGB565:
loadRGB565Data(width, height, inputPitch, input, locked.Pitch, locked.pBits);
break;
case GL_RGBA8_OES:
if (gl::supportsSSE2())
{
loadRGBAUByteDataSSE2(width, height, inputPitch, input, locked.Pitch, locked.pBits);
}
else
{
loadRGBAUByteData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
}
break;
case GL_RGBA4:
loadRGBA4444Data(width, height, inputPitch, input, locked.Pitch, locked.pBits);
break;
case GL_RGB5_A1:
loadRGBA5551Data(width, height, inputPitch, input, locked.Pitch, locked.pBits);
break;
case GL_BGRA8_EXT:
loadBGRAData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
break;
// float textures are converted to RGBA, not BGRA, as they're stored that way in D3D
case GL_RGB32F_EXT:
loadRGBFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
break;
case GL_RGB16F_EXT:
loadRGBHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
break;
case GL_RGBA32F_EXT:
loadRGBAFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
break;
case GL_RGBA16F_EXT:
loadRGBAHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
break;
default: UNREACHABLE();
}
unlock();
}
void Image::loadAlphaData(GLsizei width, GLsizei height,
int inputPitch, const void *input, size_t outputPitch, void *output) const
{
const unsigned char *source = NULL;
unsigned char *dest = NULL;
for (int y = 0; y < height; y++)
{
source = static_cast<const unsigned char*>(input) + y * inputPitch;
dest = static_cast<unsigned char*>(output) + y * outputPitch;
for (int x = 0; x < width; x++)
{
dest[4 * x + 0] = 0;
dest[4 * x + 1] = 0;
dest[4 * x + 2] = 0;
dest[4 * x + 3] = source[x];
}
}
}
void Image::loadAlphaFloatData(GLsizei width, GLsizei height,
int inputPitch, const void *input, size_t outputPitch, void *output) const
{
const float *source = NULL;
float *dest = NULL;
for (int y = 0; y < height; y++)
{
source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputPitch);
for (int x = 0; x < width; x++)
{
dest[4 * x + 0] = 0;
dest[4 * x + 1] = 0;
dest[4 * x + 2] = 0;
dest[4 * x + 3] = source[x];
}
}
}
void Image::loadAlphaHalfFloatData(GLsizei width, GLsizei height,
int inputPitch, const void *input, size_t outputPitch, void *output) const
{
const unsigned short *source = NULL;
unsigned short *dest = NULL;
for (int y = 0; y < height; y++)
{
source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + y * outputPitch);
for (int x = 0; x < width; x++)
{
dest[4 * x + 0] = 0;
dest[4 * x + 1] = 0;
dest[4 * x + 2] = 0;
dest[4 * x + 3] = source[x];
}
}
}
void Image::loadLuminanceData(GLsizei width, GLsizei height,
int inputPitch, const void *input, size_t outputPitch, void *output, bool native) const
{
const unsigned char *source = NULL;
unsigned char *dest = NULL;
for (int y = 0; y < height; y++)
{
source = static_cast<const unsigned char*>(input) + y * inputPitch;
dest = static_cast<unsigned char*>(output) + y * outputPitch;
if (!native) // BGRA8 destination format
{
for (int x = 0; x < width; x++)
{
dest[4 * x + 0] = source[x];
dest[4 * x + 1] = source[x];
dest[4 * x + 2] = source[x];
dest[4 * x + 3] = 0xFF;
}
}
else // L8 destination format
{
memcpy(dest, source, width);
}
}
}
void Image::loadLuminanceFloatData(GLsizei width, GLsizei height,
int inputPitch, const void *input, size_t outputPitch, void *output) const
{
const float *source = NULL;
float *dest = NULL;
for (int y = 0; y < height; y++)
{
source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputPitch);
for (int x = 0; x < width; x++)
{
dest[4 * x + 0] = source[x];
dest[4 * x + 1] = source[x];
dest[4 * x + 2] = source[x];
dest[4 * x + 3] = 1.0f;
}
}
}
void Image::loadLuminanceHalfFloatData(GLsizei width, GLsizei height,
int inputPitch, const void *input, size_t outputPitch, void *output) const
{
const unsigned short *source = NULL;
unsigned short *dest = NULL;
for (int y = 0; y < height; y++)
{
source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + y * outputPitch);
for (int x = 0; x < width; x++)
{
dest[4 * x + 0] = source[x];
dest[4 * x + 1] = source[x];
dest[4 * x + 2] = source[x];
dest[4 * x + 3] = 0x3C00; // SEEEEEMMMMMMMMMM, S = 0, E = 15, M = 0: 16bit flpt representation of 1
}
}
}
void Image::loadLuminanceAlphaData(GLsizei width, GLsizei height,
int inputPitch, const void *input, size_t outputPitch, void *output, bool native) const
{
const unsigned char *source = NULL;
unsigned char *dest = NULL;
for (int y = 0; y < height; y++)
{
source = static_cast<const unsigned char*>(input) + y * inputPitch;
dest = static_cast<unsigned char*>(output) + y * outputPitch;
if (!native) // BGRA8 destination format
{
for (int x = 0; x < width; x++)
{
dest[4 * x + 0] = source[2*x+0];
dest[4 * x + 1] = source[2*x+0];
dest[4 * x + 2] = source[2*x+0];
dest[4 * x + 3] = source[2*x+1];
}
}
else
{
memcpy(dest, source, width * 2);
}
}
}
void Image::loadLuminanceAlphaFloatData(GLsizei width, GLsizei height,
int inputPitch, const void *input, size_t outputPitch, void *output) const
{
const float *source = NULL;
float *dest = NULL;
for (int y = 0; y < height; y++)
{
source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputPitch);
for (int x = 0; x < width; x++)
{
dest[4 * x + 0] = source[2*x+0];
dest[4 * x + 1] = source[2*x+0];
dest[4 * x + 2] = source[2*x+0];
dest[4 * x + 3] = source[2*x+1];
}
}
}
void Image::loadLuminanceAlphaHalfFloatData(GLsizei width, GLsizei height,
int inputPitch, const void *input, size_t outputPitch, void *output) const
{
const unsigned short *source = NULL;
unsigned short *dest = NULL;
for (int y = 0; y < height; y++)
{
source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + y * outputPitch);
for (int x = 0; x < width; x++)
{
dest[4 * x + 0] = source[2*x+0];
dest[4 * x + 1] = source[2*x+0];
dest[4 * x + 2] = source[2*x+0];
dest[4 * x + 3] = source[2*x+1];
}
}
}
void Image::loadRGBUByteData(GLsizei width, GLsizei height,
int inputPitch, const void *input, size_t outputPitch, void *output) const
{
const unsigned char *source = NULL;
unsigned char *dest = NULL;
for (int y = 0; y < height; y++)
{
source = static_cast<const unsigned char*>(input) + y * inputPitch;
dest = static_cast<unsigned char*>(output) + y * outputPitch;
for (int x = 0; x < width; x++)
{
dest[4 * x + 0] = source[x * 3 + 2];
dest[4 * x + 1] = source[x * 3 + 1];
dest[4 * x + 2] = source[x * 3 + 0];
dest[4 * x + 3] = 0xFF;
}
}
}
void Image::loadRGB565Data(GLsizei width, GLsizei height,
int inputPitch, const void *input, size_t outputPitch, void *output) const
{
const unsigned short *source = NULL;
unsigned char *dest = NULL;
for (int y = 0; y < height; y++)
{
source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
dest = static_cast<unsigned char*>(output) + y * outputPitch;
for (int x = 0; x < width; x++)
{
unsigned short rgba = source[x];
dest[4 * x + 0] = ((rgba & 0x001F) << 3) | ((rgba & 0x001F) >> 2);
dest[4 * x + 1] = ((rgba & 0x07E0) >> 3) | ((rgba & 0x07E0) >> 9);
dest[4 * x + 2] = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13);
dest[4 * x + 3] = 0xFF;
}
}
}
void Image::loadRGBFloatData(GLsizei width, GLsizei height,
int inputPitch, const void *input, size_t outputPitch, void *output) const
{
const float *source = NULL;
float *dest = NULL;
for (int y = 0; y < height; y++)
{
source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputPitch);
for (int x = 0; x < width; x++)
{
dest[4 * x + 0] = source[x * 3 + 0];
dest[4 * x + 1] = source[x * 3 + 1];
dest[4 * x + 2] = source[x * 3 + 2];
dest[4 * x + 3] = 1.0f;
}
}
}
void Image::loadRGBHalfFloatData(GLsizei width, GLsizei height,
int inputPitch, const void *input, size_t outputPitch, void *output) const
{
const unsigned short *source = NULL;
unsigned short *dest = NULL;
for (int y = 0; y < height; y++)
{
source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + y * outputPitch);
for (int x = 0; x < width; x++)
{
dest[4 * x + 0] = source[x * 3 + 0];
dest[4 * x + 1] = source[x * 3 + 1];
dest[4 * x + 2] = source[x * 3 + 2];
dest[4 * x + 3] = 0x3C00; // SEEEEEMMMMMMMMMM, S = 0, E = 15, M = 0: 16bit flpt representation of 1
}
}
}
void Image::loadRGBAUByteData(GLsizei width, GLsizei height,
int inputPitch, const void *input, size_t outputPitch, void *output) const
{
const unsigned int *source = NULL;
unsigned int *dest = NULL;
for (int y = 0; y < height; y++)
{
source = reinterpret_cast<const unsigned int*>(static_cast<const unsigned char*>(input) + y * inputPitch);
dest = reinterpret_cast<unsigned int*>(static_cast<unsigned char*>(output) + y * outputPitch);
for (int x = 0; x < width; x++)
{
unsigned int rgba = source[x];
dest[x] = (_rotl(rgba, 16) & 0x00ff00ff) | (rgba & 0xff00ff00);
}
}
}
void Image::loadRGBA4444Data(GLsizei width, GLsizei height,
int inputPitch, const void *input, size_t outputPitch, void *output) const
{
const unsigned short *source = NULL;
unsigned char *dest = NULL;
for (int y = 0; y < height; y++)
{
source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
dest = static_cast<unsigned char*>(output) + y * outputPitch;
for (int x = 0; x < width; x++)
{
unsigned short rgba = source[x];
dest[4 * x + 0] = ((rgba & 0x00F0) << 0) | ((rgba & 0x00F0) >> 4);
dest[4 * x + 1] = ((rgba & 0x0F00) >> 4) | ((rgba & 0x0F00) >> 8);
dest[4 * x + 2] = ((rgba & 0xF000) >> 8) | ((rgba & 0xF000) >> 12);
dest[4 * x + 3] = ((rgba & 0x000F) << 4) | ((rgba & 0x000F) >> 0);
}
}
}
void Image::loadRGBA5551Data(GLsizei width, GLsizei height,
int inputPitch, const void *input, size_t outputPitch, void *output) const
{
const unsigned short *source = NULL;
unsigned char *dest = NULL;
for (int y = 0; y < height; y++)
{
source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
dest = static_cast<unsigned char*>(output) + y * outputPitch;
for (int x = 0; x < width; x++)
{
unsigned short rgba = source[x];
dest[4 * x + 0] = ((rgba & 0x003E) << 2) | ((rgba & 0x003E) >> 3);
dest[4 * x + 1] = ((rgba & 0x07C0) >> 3) | ((rgba & 0x07C0) >> 8);
dest[4 * x + 2] = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13);
dest[4 * x + 3] = (rgba & 0x0001) ? 0xFF : 0;
}
}
}
void Image::loadRGBAFloatData(GLsizei width, GLsizei height,
int inputPitch, const void *input, size_t outputPitch, void *output) const
{
const float *source = NULL;
float *dest = NULL;
for (int y = 0; y < height; y++)
{
source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputPitch);
memcpy(dest, source, width * 16);
}
}
void Image::loadRGBAHalfFloatData(GLsizei width, GLsizei height,
int inputPitch, const void *input, size_t outputPitch, void *output) const
{
const unsigned char *source = NULL;
unsigned char *dest = NULL;
for (int y = 0; y < height; y++)
{
source = static_cast<const unsigned char*>(input) + y * inputPitch;
dest = static_cast<unsigned char*>(output) + y * outputPitch;
memcpy(dest, source, width * 8);
}
}
void Image::loadBGRAData(GLsizei width, GLsizei height,
int inputPitch, const void *input, size_t outputPitch, void *output) const
{
const unsigned char *source = NULL;
unsigned char *dest = NULL;
for (int y = 0; y < height; y++)
{
source = static_cast<const unsigned char*>(input) + y * inputPitch;
dest = static_cast<unsigned char*>(output) + y * outputPitch;
memcpy(dest, source, width*4);
}
}
void Image::loadCompressedData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
const void *input) {
ASSERT(xoffset % 4 == 0);
ASSERT(yoffset % 4 == 0);
RECT lockRect = {
xoffset, yoffset,
xoffset + width, yoffset + height
};
D3DLOCKED_RECT locked;
HRESULT result = lock(&locked, &lockRect);
if (FAILED(result))
{
return;
}
GLsizei inputSize = gl::ComputeCompressedSize(width, height, mInternalFormat);
GLsizei inputPitch = gl::ComputeCompressedPitch(width, mInternalFormat);
int rows = inputSize / inputPitch;
for (int i = 0; i < rows; ++i)
{
memcpy((void*)((BYTE*)locked.pBits + i * locked.Pitch), (void*)((BYTE*)input + i * inputPitch), inputPitch);
}
unlock();
}
// This implements glCopyTex[Sub]Image2D for non-renderable internal texture formats and incomplete textures
void Image::copy(GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, gl::Framebuffer *source)
{
RenderTarget9 *renderTarget = NULL;
IDirect3DSurface9 *surface = NULL;
gl::Renderbuffer *colorbuffer = source->getColorbuffer();
if (colorbuffer)
{
renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
}
if (renderTarget)
{
surface = renderTarget->getSurface();
}
if (!surface)
{
ERR("Failed to retrieve the render target.");
return error(GL_OUT_OF_MEMORY);
}
IDirect3DDevice9 *device = mRenderer->getDevice(); // D3D9_REPLACE
IDirect3DSurface9 *renderTargetData = NULL;
D3DSURFACE_DESC description;
surface->GetDesc(&description);
HRESULT result = device->CreateOffscreenPlainSurface(description.Width, description.Height, description.Format, D3DPOOL_SYSTEMMEM, &renderTargetData, NULL);
if (FAILED(result))
{
ERR("Could not create matching destination surface.");
surface->Release();
return error(GL_OUT_OF_MEMORY);
}
result = device->GetRenderTargetData(surface, renderTargetData);
if (FAILED(result))
{
ERR("GetRenderTargetData unexpectedly failed.");
renderTargetData->Release();
surface->Release();
return error(GL_OUT_OF_MEMORY);
}
RECT sourceRect = {x, y, x + width, y + height};
RECT destRect = {xoffset, yoffset, xoffset + width, yoffset + height};
D3DLOCKED_RECT sourceLock = {0};
result = renderTargetData->LockRect(&sourceLock, &sourceRect, 0);
if (FAILED(result))
{
ERR("Failed to lock the source surface (rectangle might be invalid).");
renderTargetData->Release();
surface->Release();
return error(GL_OUT_OF_MEMORY);
}
D3DLOCKED_RECT destLock = {0};
result = lock(&destLock, &destRect);
if (FAILED(result))
{
ERR("Failed to lock the destination surface (rectangle might be invalid).");
renderTargetData->UnlockRect();
renderTargetData->Release();
surface->Release();
return error(GL_OUT_OF_MEMORY);
}
if (destLock.pBits && sourceLock.pBits)
{
unsigned char *source = (unsigned char*)sourceLock.pBits;
unsigned char *dest = (unsigned char*)destLock.pBits;
switch (description.Format)
{
case D3DFMT_X8R8G8B8:
case D3DFMT_A8R8G8B8:
switch(getD3DFormat())
{
case D3DFMT_X8R8G8B8:
case D3DFMT_A8R8G8B8:
for(int y = 0; y < height; y++)
{
memcpy(dest, source, 4 * width);
source += sourceLock.Pitch;
dest += destLock.Pitch;
}
break;
case D3DFMT_L8:
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
dest[x] = source[x * 4 + 2];
}
source += sourceLock.Pitch;
dest += destLock.Pitch;
}
break;
case D3DFMT_A8L8:
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
dest[x * 2 + 0] = source[x * 4 + 2];
dest[x * 2 + 1] = source[x * 4 + 3];
}
source += sourceLock.Pitch;
dest += destLock.Pitch;
}
break;
default:
UNREACHABLE();
}
break;
case D3DFMT_R5G6B5:
switch(getD3DFormat())
{
case D3DFMT_X8R8G8B8:
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
unsigned short rgb = ((unsigned short*)source)[x];
unsigned char red = (rgb & 0xF800) >> 8;
unsigned char green = (rgb & 0x07E0) >> 3;
unsigned char blue = (rgb & 0x001F) << 3;
dest[x + 0] = blue | (blue >> 5);
dest[x + 1] = green | (green >> 6);
dest[x + 2] = red | (red >> 5);
dest[x + 3] = 0xFF;
}
source += sourceLock.Pitch;
dest += destLock.Pitch;
}
break;
case D3DFMT_L8:
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
unsigned char red = source[x * 2 + 1] & 0xF8;
dest[x] = red | (red >> 5);
}
source += sourceLock.Pitch;
dest += destLock.Pitch;
}
break;
default:
UNREACHABLE();
}
break;
case D3DFMT_A1R5G5B5:
switch(getD3DFormat())
{
case D3DFMT_X8R8G8B8:
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
unsigned short argb = ((unsigned short*)source)[x];
unsigned char red = (argb & 0x7C00) >> 7;
unsigned char green = (argb & 0x03E0) >> 2;
unsigned char blue = (argb & 0x001F) << 3;
dest[x + 0] = blue | (blue >> 5);
dest[x + 1] = green | (green >> 5);
dest[x + 2] = red | (red >> 5);
dest[x + 3] = 0xFF;
}
source += sourceLock.Pitch;
dest += destLock.Pitch;
}
break;
case D3DFMT_A8R8G8B8:
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
unsigned short argb = ((unsigned short*)source)[x];
unsigned char red = (argb & 0x7C00) >> 7;
unsigned char green = (argb & 0x03E0) >> 2;
unsigned char blue = (argb & 0x001F) << 3;
unsigned char alpha = (signed short)argb >> 15;
dest[x + 0] = blue | (blue >> 5);
dest[x + 1] = green | (green >> 5);
dest[x + 2] = red | (red >> 5);
dest[x + 3] = alpha;
}
source += sourceLock.Pitch;
dest += destLock.Pitch;
}
break;
case D3DFMT_L8:
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
unsigned char red = source[x * 2 + 1] & 0x7C;
dest[x] = (red << 1) | (red >> 4);
}
source += sourceLock.Pitch;
dest += destLock.Pitch;
}
break;
case D3DFMT_A8L8:
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
unsigned char red = source[x * 2 + 1] & 0x7C;
dest[x * 2 + 0] = (red << 1) | (red >> 4);
dest[x * 2 + 1] = (signed char)source[x * 2 + 1] >> 7;
}
source += sourceLock.Pitch;
dest += destLock.Pitch;
}
break;
default:
UNREACHABLE();
}
break;
default:
UNREACHABLE();
}
}
unlock();
renderTargetData->UnlockRect();
renderTargetData->Release();
surface->Release();
mDirty = true;
}
}