daniel@transgaming.com | b9d7e6f | 2012-10-31 19:08:32 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // Image.cpp: Implements the gl::Image class, which acts as the interface to |
| 8 | // the actual underlying surfaces of a Texture. |
| 9 | |
| 10 | #include "libGLESv2/renderer/Image.h" |
| 11 | |
| 12 | #include "libEGL/Display.h" |
| 13 | |
| 14 | #include "libGLESv2/main.h" |
| 15 | #include "libGLESv2/mathutil.h" |
| 16 | #include "libGLESv2/utilities.h" |
| 17 | #include "libGLESv2/Texture.h" |
daniel@transgaming.com | 3cef539 | 2012-10-31 19:52:15 +0000 | [diff] [blame] | 18 | #include "libGLESv2/Framebuffer.h" |
daniel@transgaming.com | b9d7e6f | 2012-10-31 19:08:32 +0000 | [diff] [blame] | 19 | |
daniel@transgaming.com | d8e3656 | 2012-10-31 19:52:19 +0000 | [diff] [blame^] | 20 | #include "libGLESv2/renderer/renderer9_utils.h" |
| 21 | |
daniel@transgaming.com | b9d7e6f | 2012-10-31 19:08:32 +0000 | [diff] [blame] | 22 | namespace gl |
| 23 | { |
daniel@transgaming.com | 0ad830b | 2012-10-31 19:52:12 +0000 | [diff] [blame] | 24 | |
| 25 | namespace |
| 26 | { |
| 27 | struct L8 |
| 28 | { |
| 29 | unsigned char L; |
| 30 | |
| 31 | static void average(L8 *dst, const L8 *src1, const L8 *src2) |
| 32 | { |
| 33 | dst->L = ((src1->L ^ src2->L) >> 1) + (src1->L & src2->L); |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | struct A8L8 |
| 38 | { |
| 39 | unsigned char L; |
| 40 | unsigned char A; |
| 41 | |
| 42 | static void average(A8L8 *dst, const A8L8 *src1, const A8L8 *src2) |
| 43 | { |
| 44 | *(unsigned short*)dst = (((*(unsigned short*)src1 ^ *(unsigned short*)src2) & 0xFEFE) >> 1) + (*(unsigned short*)src1 & *(unsigned short*)src2); |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | struct A8R8G8B8 |
| 49 | { |
| 50 | unsigned char B; |
| 51 | unsigned char G; |
| 52 | unsigned char R; |
| 53 | unsigned char A; |
| 54 | |
| 55 | static void average(A8R8G8B8 *dst, const A8R8G8B8 *src1, const A8R8G8B8 *src2) |
| 56 | { |
| 57 | *(unsigned int*)dst = (((*(unsigned int*)src1 ^ *(unsigned int*)src2) & 0xFEFEFEFE) >> 1) + (*(unsigned int*)src1 & *(unsigned int*)src2); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | struct A16B16G16R16F |
| 62 | { |
| 63 | unsigned short R; |
| 64 | unsigned short G; |
| 65 | unsigned short B; |
| 66 | unsigned short A; |
| 67 | |
| 68 | static void average(A16B16G16R16F *dst, const A16B16G16R16F *src1, const A16B16G16R16F *src2) |
| 69 | { |
| 70 | dst->R = float32ToFloat16((float16ToFloat32(src1->R) + float16ToFloat32(src2->R)) * 0.5f); |
| 71 | dst->G = float32ToFloat16((float16ToFloat32(src1->G) + float16ToFloat32(src2->G)) * 0.5f); |
| 72 | dst->B = float32ToFloat16((float16ToFloat32(src1->B) + float16ToFloat32(src2->B)) * 0.5f); |
| 73 | dst->A = float32ToFloat16((float16ToFloat32(src1->A) + float16ToFloat32(src2->A)) * 0.5f); |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | struct A32B32G32R32F |
| 78 | { |
| 79 | float R; |
| 80 | float G; |
| 81 | float B; |
| 82 | float A; |
| 83 | |
| 84 | static void average(A32B32G32R32F *dst, const A32B32G32R32F *src1, const A32B32G32R32F *src2) |
| 85 | { |
| 86 | dst->R = (src1->R + src2->R) * 0.5f; |
| 87 | dst->G = (src1->G + src2->G) * 0.5f; |
| 88 | dst->B = (src1->B + src2->B) * 0.5f; |
| 89 | dst->A = (src1->A + src2->A) * 0.5f; |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | template <typename T> |
| 94 | void GenerateMip(unsigned int sourceWidth, unsigned int sourceHeight, |
| 95 | const unsigned char *sourceData, int sourcePitch, |
| 96 | unsigned char *destData, int destPitch) |
| 97 | { |
| 98 | unsigned int mipWidth = std::max(1U, sourceWidth >> 1); |
| 99 | unsigned int mipHeight = std::max(1U, sourceHeight >> 1); |
| 100 | |
| 101 | if (sourceHeight == 1) |
| 102 | { |
| 103 | ASSERT(sourceWidth != 1); |
| 104 | |
| 105 | const T *src = (const T*)sourceData; |
| 106 | T *dst = (T*)destData; |
| 107 | |
| 108 | for (unsigned int x = 0; x < mipWidth; x++) |
| 109 | { |
| 110 | T::average(&dst[x], &src[x * 2], &src[x * 2 + 1]); |
| 111 | } |
| 112 | } |
| 113 | else if (sourceWidth == 1) |
| 114 | { |
| 115 | ASSERT(sourceHeight != 1); |
| 116 | |
| 117 | for (unsigned int y = 0; y < mipHeight; y++) |
| 118 | { |
| 119 | const T *src0 = (const T*)(sourceData + y * 2 * sourcePitch); |
| 120 | const T *src1 = (const T*)(sourceData + y * 2 * sourcePitch + sourcePitch); |
| 121 | T *dst = (T*)(destData + y * destPitch); |
| 122 | |
| 123 | T::average(dst, src0, src1); |
| 124 | } |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | for (unsigned int y = 0; y < mipHeight; y++) |
| 129 | { |
| 130 | const T *src0 = (const T*)(sourceData + y * 2 * sourcePitch); |
| 131 | const T *src1 = (const T*)(sourceData + y * 2 * sourcePitch + sourcePitch); |
| 132 | T *dst = (T*)(destData + y * destPitch); |
| 133 | |
| 134 | for (unsigned int x = 0; x < mipWidth; x++) |
| 135 | { |
| 136 | T tmp0; |
| 137 | T tmp1; |
| 138 | |
| 139 | T::average(&tmp0, &src0[x * 2], &src0[x * 2 + 1]); |
| 140 | T::average(&tmp1, &src1[x * 2], &src1[x * 2 + 1]); |
| 141 | T::average(&dst[x], &tmp0, &tmp1); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void GenerateMip(IDirect3DSurface9 *destSurface, IDirect3DSurface9 *sourceSurface) |
| 148 | { |
| 149 | D3DSURFACE_DESC destDesc; |
| 150 | HRESULT result = destSurface->GetDesc(&destDesc); |
| 151 | ASSERT(SUCCEEDED(result)); |
| 152 | |
| 153 | D3DSURFACE_DESC sourceDesc; |
| 154 | result = sourceSurface->GetDesc(&sourceDesc); |
| 155 | ASSERT(SUCCEEDED(result)); |
| 156 | |
| 157 | ASSERT(sourceDesc.Format == destDesc.Format); |
| 158 | ASSERT(sourceDesc.Width == 1 || sourceDesc.Width / 2 == destDesc.Width); |
| 159 | ASSERT(sourceDesc.Height == 1 || sourceDesc.Height / 2 == destDesc.Height); |
| 160 | |
| 161 | D3DLOCKED_RECT sourceLocked = {0}; |
| 162 | result = sourceSurface->LockRect(&sourceLocked, NULL, D3DLOCK_READONLY); |
| 163 | ASSERT(SUCCEEDED(result)); |
| 164 | |
| 165 | D3DLOCKED_RECT destLocked = {0}; |
| 166 | result = destSurface->LockRect(&destLocked, NULL, 0); |
| 167 | ASSERT(SUCCEEDED(result)); |
| 168 | |
| 169 | const unsigned char *sourceData = reinterpret_cast<const unsigned char*>(sourceLocked.pBits); |
| 170 | unsigned char *destData = reinterpret_cast<unsigned char*>(destLocked.pBits); |
| 171 | |
| 172 | if (sourceData && destData) |
| 173 | { |
| 174 | switch (sourceDesc.Format) |
| 175 | { |
| 176 | case D3DFMT_L8: |
| 177 | GenerateMip<L8>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch); |
| 178 | break; |
| 179 | case D3DFMT_A8L8: |
| 180 | GenerateMip<A8L8>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch); |
| 181 | break; |
| 182 | case D3DFMT_A8R8G8B8: |
| 183 | case D3DFMT_X8R8G8B8: |
| 184 | GenerateMip<A8R8G8B8>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch); |
| 185 | break; |
| 186 | case D3DFMT_A16B16G16R16F: |
| 187 | GenerateMip<A16B16G16R16F>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch); |
| 188 | break; |
| 189 | case D3DFMT_A32B32G32R32F: |
| 190 | GenerateMip<A32B32G32R32F>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch); |
| 191 | break; |
| 192 | default: |
| 193 | UNREACHABLE(); |
| 194 | break; |
| 195 | } |
| 196 | |
| 197 | destSurface->UnlockRect(); |
| 198 | sourceSurface->UnlockRect(); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
daniel@transgaming.com | b9d7e6f | 2012-10-31 19:08:32 +0000 | [diff] [blame] | 203 | Image::Image() |
| 204 | { |
| 205 | mWidth = 0; |
| 206 | mHeight = 0; |
| 207 | mInternalFormat = GL_NONE; |
| 208 | |
| 209 | mSurface = NULL; |
| 210 | |
| 211 | mDirty = false; |
| 212 | |
| 213 | mD3DPool = D3DPOOL_SYSTEMMEM; |
| 214 | mD3DFormat = D3DFMT_UNKNOWN; |
daniel@transgaming.com | 20d3666 | 2012-10-31 19:51:43 +0000 | [diff] [blame] | 215 | mActualFormat = GL_NONE; |
daniel@transgaming.com | b9d7e6f | 2012-10-31 19:08:32 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | Image::~Image() |
| 219 | { |
| 220 | if (mSurface) |
| 221 | { |
| 222 | mSurface->Release(); |
| 223 | } |
| 224 | } |
| 225 | |
daniel@transgaming.com | 0ad830b | 2012-10-31 19:52:12 +0000 | [diff] [blame] | 226 | void Image::GenerateMipmap(Image *dest, Image *source) |
| 227 | { |
| 228 | IDirect3DSurface9 *sourceSurface = source->getSurface(); |
| 229 | if (sourceSurface == NULL) |
| 230 | return error(GL_OUT_OF_MEMORY); |
| 231 | |
| 232 | IDirect3DSurface9 *destSurface = dest->getSurface(); |
| 233 | GenerateMip(destSurface, sourceSurface); |
| 234 | |
| 235 | source->markDirty(); |
| 236 | } |
| 237 | |
daniel@transgaming.com | b9d7e6f | 2012-10-31 19:08:32 +0000 | [diff] [blame] | 238 | void Image::CopyLockableSurfaces(IDirect3DSurface9 *dest, IDirect3DSurface9 *source) |
| 239 | { |
| 240 | D3DLOCKED_RECT sourceLock = {0}; |
| 241 | D3DLOCKED_RECT destLock = {0}; |
| 242 | |
| 243 | source->LockRect(&sourceLock, NULL, 0); |
| 244 | dest->LockRect(&destLock, NULL, 0); |
| 245 | |
| 246 | if (sourceLock.pBits && destLock.pBits) |
| 247 | { |
| 248 | D3DSURFACE_DESC desc; |
| 249 | source->GetDesc(&desc); |
| 250 | |
| 251 | int rows = dx::IsCompressedFormat(desc.Format) ? desc.Height / 4 : desc.Height; |
| 252 | int bytes = dx::ComputeRowSize(desc.Format, desc.Width); |
| 253 | ASSERT(bytes <= sourceLock.Pitch && bytes <= destLock.Pitch); |
| 254 | |
| 255 | for(int i = 0; i < rows; i++) |
| 256 | { |
| 257 | memcpy((char*)destLock.pBits + destLock.Pitch * i, (char*)sourceLock.pBits + sourceLock.Pitch * i, bytes); |
| 258 | } |
| 259 | |
| 260 | source->UnlockRect(); |
| 261 | dest->UnlockRect(); |
| 262 | } |
| 263 | else UNREACHABLE(); |
| 264 | } |
| 265 | |
| 266 | bool Image::redefine(GLint internalformat, GLsizei width, GLsizei height, bool forceRelease) |
| 267 | { |
| 268 | if (mWidth != width || |
| 269 | mHeight != height || |
| 270 | mInternalFormat != internalformat || |
| 271 | forceRelease) |
| 272 | { |
| 273 | mWidth = width; |
| 274 | mHeight = height; |
| 275 | mInternalFormat = internalformat; |
| 276 | // compute the d3d format that will be used |
daniel@transgaming.com | df14c76 | 2012-10-31 19:51:48 +0000 | [diff] [blame] | 277 | mD3DFormat = TextureStorage::ConvertTextureInternalFormat(internalformat); |
daniel@transgaming.com | 20d3666 | 2012-10-31 19:51:43 +0000 | [diff] [blame] | 278 | mActualFormat = dx2es::GetEquivalentFormat(mD3DFormat); |
daniel@transgaming.com | b9d7e6f | 2012-10-31 19:08:32 +0000 | [diff] [blame] | 279 | |
| 280 | if (mSurface) |
| 281 | { |
| 282 | mSurface->Release(); |
| 283 | mSurface = NULL; |
| 284 | } |
| 285 | |
| 286 | return true; |
| 287 | } |
| 288 | |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | void Image::createSurface() |
| 293 | { |
| 294 | if(mSurface) |
| 295 | { |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | IDirect3DTexture9 *newTexture = NULL; |
| 300 | IDirect3DSurface9 *newSurface = NULL; |
| 301 | const D3DPOOL poolToUse = D3DPOOL_SYSTEMMEM; |
| 302 | const D3DFORMAT d3dFormat = getD3DFormat(); |
| 303 | ASSERT(d3dFormat != D3DFMT_INTZ); // We should never get here for depth textures |
| 304 | |
| 305 | if (mWidth != 0 && mHeight != 0) |
| 306 | { |
| 307 | int levelToFetch = 0; |
| 308 | GLsizei requestWidth = mWidth; |
| 309 | GLsizei requestHeight = mHeight; |
| 310 | MakeValidSize(true, IsCompressed(mInternalFormat), &requestWidth, &requestHeight, &levelToFetch); |
| 311 | |
| 312 | // D3D9_REPLACE |
| 313 | IDirect3DDevice9 *device = getDisplay()->getRenderer()->getDevice(); |
| 314 | HRESULT result = device->CreateTexture(requestWidth, requestHeight, levelToFetch + 1, NULL, d3dFormat, |
| 315 | poolToUse, &newTexture, NULL); |
| 316 | |
| 317 | if (FAILED(result)) |
| 318 | { |
| 319 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 320 | ERR("Creating image surface failed."); |
| 321 | return error(GL_OUT_OF_MEMORY); |
| 322 | } |
| 323 | |
| 324 | newTexture->GetSurfaceLevel(levelToFetch, &newSurface); |
| 325 | newTexture->Release(); |
| 326 | } |
| 327 | |
| 328 | mSurface = newSurface; |
| 329 | mDirty = false; |
| 330 | mD3DPool = poolToUse; |
| 331 | } |
| 332 | |
| 333 | HRESULT Image::lock(D3DLOCKED_RECT *lockedRect, const RECT *rect) |
| 334 | { |
| 335 | createSurface(); |
| 336 | |
| 337 | HRESULT result = D3DERR_INVALIDCALL; |
| 338 | |
| 339 | if (mSurface) |
| 340 | { |
| 341 | result = mSurface->LockRect(lockedRect, rect, 0); |
| 342 | ASSERT(SUCCEEDED(result)); |
| 343 | |
| 344 | mDirty = true; |
| 345 | } |
| 346 | |
| 347 | return result; |
| 348 | } |
| 349 | |
| 350 | void Image::unlock() |
| 351 | { |
| 352 | if (mSurface) |
| 353 | { |
| 354 | HRESULT result = mSurface->UnlockRect(); |
| 355 | ASSERT(SUCCEEDED(result)); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | bool Image::isRenderableFormat() const |
| 360 | { |
daniel@transgaming.com | df14c76 | 2012-10-31 19:51:48 +0000 | [diff] [blame] | 361 | return TextureStorage::IsTextureFormatRenderable(getD3DFormat()); |
daniel@transgaming.com | b9d7e6f | 2012-10-31 19:08:32 +0000 | [diff] [blame] | 362 | } |
| 363 | |
daniel@transgaming.com | 20d3666 | 2012-10-31 19:51:43 +0000 | [diff] [blame] | 364 | GLenum Image::getActualFormat() const |
| 365 | { |
| 366 | return mActualFormat; |
| 367 | } |
| 368 | |
daniel@transgaming.com | b9d7e6f | 2012-10-31 19:08:32 +0000 | [diff] [blame] | 369 | D3DFORMAT Image::getD3DFormat() const |
| 370 | { |
| 371 | // this should only happen if the image hasn't been redefined first |
| 372 | // which would be a bug by the caller |
| 373 | ASSERT(mD3DFormat != D3DFMT_UNKNOWN); |
| 374 | |
| 375 | return mD3DFormat; |
| 376 | } |
| 377 | |
| 378 | IDirect3DSurface9 *Image::getSurface() |
| 379 | { |
| 380 | createSurface(); |
| 381 | |
| 382 | return mSurface; |
| 383 | } |
| 384 | |
daniel@transgaming.com | 0f195ad | 2012-10-31 19:51:59 +0000 | [diff] [blame] | 385 | void Image::setManagedSurface(TextureStorage2D *storage, int level) |
| 386 | { |
| 387 | setManagedSurface(storage->getSurfaceLevel(level, false)); |
| 388 | } |
| 389 | |
| 390 | void Image::setManagedSurface(TextureStorageCubeMap *storage, int face, int level) |
| 391 | { |
| 392 | setManagedSurface(storage->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, false)); |
| 393 | } |
| 394 | |
daniel@transgaming.com | b9d7e6f | 2012-10-31 19:08:32 +0000 | [diff] [blame] | 395 | void Image::setManagedSurface(IDirect3DSurface9 *surface) |
| 396 | { |
| 397 | D3DSURFACE_DESC desc; |
| 398 | surface->GetDesc(&desc); |
| 399 | ASSERT(desc.Pool == D3DPOOL_MANAGED); |
| 400 | |
| 401 | if ((GLsizei)desc.Width == mWidth && (GLsizei)desc.Height == mHeight) |
| 402 | { |
| 403 | if (mSurface) |
| 404 | { |
| 405 | CopyLockableSurfaces(surface, mSurface); |
| 406 | mSurface->Release(); |
| 407 | } |
| 408 | |
| 409 | mSurface = surface; |
| 410 | mD3DPool = desc.Pool; |
| 411 | } |
| 412 | } |
| 413 | |
daniel@transgaming.com | 0f195ad | 2012-10-31 19:51:59 +0000 | [diff] [blame] | 414 | bool Image::updateSurface(TextureStorage2D *storage, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) |
daniel@transgaming.com | b9d7e6f | 2012-10-31 19:08:32 +0000 | [diff] [blame] | 415 | { |
daniel@transgaming.com | 0f195ad | 2012-10-31 19:51:59 +0000 | [diff] [blame] | 416 | return updateSurface(storage->getSurfaceLevel(level, true), xoffset, yoffset, width, height); |
| 417 | } |
| 418 | |
| 419 | bool Image::updateSurface(TextureStorageCubeMap *storage, int face, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) |
| 420 | { |
| 421 | return updateSurface(storage->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, true), xoffset, yoffset, width, height); |
| 422 | } |
| 423 | |
| 424 | bool Image::updateSurface(IDirect3DSurface9 *destSurface, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) |
| 425 | { |
| 426 | if (!destSurface) |
| 427 | return false; |
| 428 | |
daniel@transgaming.com | b9d7e6f | 2012-10-31 19:08:32 +0000 | [diff] [blame] | 429 | IDirect3DSurface9 *sourceSurface = getSurface(); |
| 430 | |
| 431 | if (sourceSurface && sourceSurface != destSurface) |
| 432 | { |
| 433 | RECT rect; |
| 434 | rect.left = xoffset; |
| 435 | rect.top = yoffset; |
| 436 | rect.right = xoffset + width; |
| 437 | rect.bottom = yoffset + height; |
| 438 | |
| 439 | POINT point = {rect.left, rect.top}; |
| 440 | IDirect3DDevice9 *device = getDisplay()->getRenderer()->getDevice(); // D3D9_REPLACE |
| 441 | |
| 442 | if (mD3DPool == D3DPOOL_MANAGED) |
| 443 | { |
| 444 | D3DSURFACE_DESC desc; |
| 445 | sourceSurface->GetDesc(&desc); |
| 446 | |
| 447 | IDirect3DSurface9 *surf = 0; |
| 448 | HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL); |
| 449 | |
| 450 | if (SUCCEEDED(result)) |
| 451 | { |
| 452 | CopyLockableSurfaces(surf, sourceSurface); |
| 453 | result = device->UpdateSurface(surf, &rect, destSurface, &point); |
| 454 | ASSERT(SUCCEEDED(result)); |
| 455 | surf->Release(); |
| 456 | } |
| 457 | } |
| 458 | else |
| 459 | { |
| 460 | // UpdateSurface: source must be SYSTEMMEM, dest must be DEFAULT pools |
| 461 | HRESULT result = device->UpdateSurface(sourceSurface, &rect, destSurface, &point); |
| 462 | ASSERT(SUCCEEDED(result)); |
| 463 | } |
| 464 | } |
daniel@transgaming.com | 0f195ad | 2012-10-31 19:51:59 +0000 | [diff] [blame] | 465 | |
| 466 | destSurface->Release(); |
| 467 | return true; |
daniel@transgaming.com | b9d7e6f | 2012-10-31 19:08:32 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | // Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input |
| 471 | // into the target pixel rectangle. |
| 472 | void Image::loadData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, |
| 473 | GLint unpackAlignment, const void *input) |
| 474 | { |
| 475 | RECT lockRect = |
| 476 | { |
| 477 | xoffset, yoffset, |
| 478 | xoffset + width, yoffset + height |
| 479 | }; |
| 480 | |
| 481 | D3DLOCKED_RECT locked; |
| 482 | HRESULT result = lock(&locked, &lockRect); |
| 483 | if (FAILED(result)) |
| 484 | { |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | |
| 489 | GLsizei inputPitch = ComputePitch(width, mInternalFormat, unpackAlignment); |
| 490 | |
| 491 | switch (mInternalFormat) |
| 492 | { |
| 493 | case GL_ALPHA8_EXT: |
| 494 | if (supportsSSE2()) |
| 495 | { |
| 496 | loadAlphaDataSSE2(width, height, inputPitch, input, locked.Pitch, locked.pBits); |
| 497 | } |
| 498 | else |
| 499 | { |
| 500 | loadAlphaData(width, height, inputPitch, input, locked.Pitch, locked.pBits); |
| 501 | } |
| 502 | break; |
| 503 | case GL_LUMINANCE8_EXT: |
| 504 | loadLuminanceData(width, height, inputPitch, input, locked.Pitch, locked.pBits, getD3DFormat() == D3DFMT_L8); |
| 505 | break; |
| 506 | case GL_ALPHA32F_EXT: |
| 507 | loadAlphaFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits); |
| 508 | break; |
| 509 | case GL_LUMINANCE32F_EXT: |
| 510 | loadLuminanceFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits); |
| 511 | break; |
| 512 | case GL_ALPHA16F_EXT: |
| 513 | loadAlphaHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits); |
| 514 | break; |
| 515 | case GL_LUMINANCE16F_EXT: |
| 516 | loadLuminanceHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits); |
| 517 | break; |
| 518 | case GL_LUMINANCE8_ALPHA8_EXT: |
| 519 | loadLuminanceAlphaData(width, height, inputPitch, input, locked.Pitch, locked.pBits, getD3DFormat() == D3DFMT_A8L8); |
| 520 | break; |
| 521 | case GL_LUMINANCE_ALPHA32F_EXT: |
| 522 | loadLuminanceAlphaFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits); |
| 523 | break; |
| 524 | case GL_LUMINANCE_ALPHA16F_EXT: |
| 525 | loadLuminanceAlphaHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits); |
| 526 | break; |
| 527 | case GL_RGB8_OES: |
| 528 | loadRGBUByteData(width, height, inputPitch, input, locked.Pitch, locked.pBits); |
| 529 | break; |
| 530 | case GL_RGB565: |
| 531 | loadRGB565Data(width, height, inputPitch, input, locked.Pitch, locked.pBits); |
| 532 | break; |
| 533 | case GL_RGBA8_OES: |
| 534 | if (supportsSSE2()) |
| 535 | { |
| 536 | loadRGBAUByteDataSSE2(width, height, inputPitch, input, locked.Pitch, locked.pBits); |
| 537 | } |
| 538 | else |
| 539 | { |
| 540 | loadRGBAUByteData(width, height, inputPitch, input, locked.Pitch, locked.pBits); |
| 541 | } |
| 542 | break; |
| 543 | case GL_RGBA4: |
| 544 | loadRGBA4444Data(width, height, inputPitch, input, locked.Pitch, locked.pBits); |
| 545 | break; |
| 546 | case GL_RGB5_A1: |
| 547 | loadRGBA5551Data(width, height, inputPitch, input, locked.Pitch, locked.pBits); |
| 548 | break; |
| 549 | case GL_BGRA8_EXT: |
| 550 | loadBGRAData(width, height, inputPitch, input, locked.Pitch, locked.pBits); |
| 551 | break; |
| 552 | // float textures are converted to RGBA, not BGRA, as they're stored that way in D3D |
| 553 | case GL_RGB32F_EXT: |
| 554 | loadRGBFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits); |
| 555 | break; |
| 556 | case GL_RGB16F_EXT: |
| 557 | loadRGBHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits); |
| 558 | break; |
| 559 | case GL_RGBA32F_EXT: |
| 560 | loadRGBAFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits); |
| 561 | break; |
| 562 | case GL_RGBA16F_EXT: |
| 563 | loadRGBAHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits); |
| 564 | break; |
| 565 | default: UNREACHABLE(); |
| 566 | } |
| 567 | |
| 568 | unlock(); |
| 569 | } |
| 570 | |
| 571 | void Image::loadAlphaData(GLsizei width, GLsizei height, |
| 572 | int inputPitch, const void *input, size_t outputPitch, void *output) const |
| 573 | { |
| 574 | const unsigned char *source = NULL; |
| 575 | unsigned char *dest = NULL; |
| 576 | |
| 577 | for (int y = 0; y < height; y++) |
| 578 | { |
| 579 | source = static_cast<const unsigned char*>(input) + y * inputPitch; |
| 580 | dest = static_cast<unsigned char*>(output) + y * outputPitch; |
| 581 | for (int x = 0; x < width; x++) |
| 582 | { |
| 583 | dest[4 * x + 0] = 0; |
| 584 | dest[4 * x + 1] = 0; |
| 585 | dest[4 * x + 2] = 0; |
| 586 | dest[4 * x + 3] = source[x]; |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | void Image::loadAlphaFloatData(GLsizei width, GLsizei height, |
| 592 | int inputPitch, const void *input, size_t outputPitch, void *output) const |
| 593 | { |
| 594 | const float *source = NULL; |
| 595 | float *dest = NULL; |
| 596 | |
| 597 | for (int y = 0; y < height; y++) |
| 598 | { |
| 599 | source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch); |
| 600 | dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputPitch); |
| 601 | for (int x = 0; x < width; x++) |
| 602 | { |
| 603 | dest[4 * x + 0] = 0; |
| 604 | dest[4 * x + 1] = 0; |
| 605 | dest[4 * x + 2] = 0; |
| 606 | dest[4 * x + 3] = source[x]; |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | void Image::loadAlphaHalfFloatData(GLsizei width, GLsizei height, |
| 612 | int inputPitch, const void *input, size_t outputPitch, void *output) const |
| 613 | { |
| 614 | const unsigned short *source = NULL; |
| 615 | unsigned short *dest = NULL; |
| 616 | |
| 617 | for (int y = 0; y < height; y++) |
| 618 | { |
| 619 | source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch); |
| 620 | dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + y * outputPitch); |
| 621 | for (int x = 0; x < width; x++) |
| 622 | { |
| 623 | dest[4 * x + 0] = 0; |
| 624 | dest[4 * x + 1] = 0; |
| 625 | dest[4 * x + 2] = 0; |
| 626 | dest[4 * x + 3] = source[x]; |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | void Image::loadLuminanceData(GLsizei width, GLsizei height, |
| 632 | int inputPitch, const void *input, size_t outputPitch, void *output, bool native) const |
| 633 | { |
| 634 | const unsigned char *source = NULL; |
| 635 | unsigned char *dest = NULL; |
| 636 | |
| 637 | for (int y = 0; y < height; y++) |
| 638 | { |
| 639 | source = static_cast<const unsigned char*>(input) + y * inputPitch; |
| 640 | dest = static_cast<unsigned char*>(output) + y * outputPitch; |
| 641 | |
| 642 | if (!native) // BGRA8 destination format |
| 643 | { |
| 644 | for (int x = 0; x < width; x++) |
| 645 | { |
| 646 | dest[4 * x + 0] = source[x]; |
| 647 | dest[4 * x + 1] = source[x]; |
| 648 | dest[4 * x + 2] = source[x]; |
| 649 | dest[4 * x + 3] = 0xFF; |
| 650 | } |
| 651 | } |
| 652 | else // L8 destination format |
| 653 | { |
| 654 | memcpy(dest, source, width); |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | void Image::loadLuminanceFloatData(GLsizei width, GLsizei height, |
| 660 | int inputPitch, const void *input, size_t outputPitch, void *output) const |
| 661 | { |
| 662 | const float *source = NULL; |
| 663 | float *dest = NULL; |
| 664 | |
| 665 | for (int y = 0; y < height; y++) |
| 666 | { |
| 667 | source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch); |
| 668 | dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputPitch); |
| 669 | for (int x = 0; x < width; x++) |
| 670 | { |
| 671 | dest[4 * x + 0] = source[x]; |
| 672 | dest[4 * x + 1] = source[x]; |
| 673 | dest[4 * x + 2] = source[x]; |
| 674 | dest[4 * x + 3] = 1.0f; |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | void Image::loadLuminanceHalfFloatData(GLsizei width, GLsizei height, |
| 680 | int inputPitch, const void *input, size_t outputPitch, void *output) const |
| 681 | { |
| 682 | const unsigned short *source = NULL; |
| 683 | unsigned short *dest = NULL; |
| 684 | |
| 685 | for (int y = 0; y < height; y++) |
| 686 | { |
| 687 | source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch); |
| 688 | dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + y * outputPitch); |
| 689 | for (int x = 0; x < width; x++) |
| 690 | { |
| 691 | dest[4 * x + 0] = source[x]; |
| 692 | dest[4 * x + 1] = source[x]; |
| 693 | dest[4 * x + 2] = source[x]; |
| 694 | dest[4 * x + 3] = 0x3C00; // SEEEEEMMMMMMMMMM, S = 0, E = 15, M = 0: 16bit flpt representation of 1 |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | void Image::loadLuminanceAlphaData(GLsizei width, GLsizei height, |
| 700 | int inputPitch, const void *input, size_t outputPitch, void *output, bool native) const |
| 701 | { |
| 702 | const unsigned char *source = NULL; |
| 703 | unsigned char *dest = NULL; |
| 704 | |
| 705 | for (int y = 0; y < height; y++) |
| 706 | { |
| 707 | source = static_cast<const unsigned char*>(input) + y * inputPitch; |
| 708 | dest = static_cast<unsigned char*>(output) + y * outputPitch; |
| 709 | |
| 710 | if (!native) // BGRA8 destination format |
| 711 | { |
| 712 | for (int x = 0; x < width; x++) |
| 713 | { |
| 714 | dest[4 * x + 0] = source[2*x+0]; |
| 715 | dest[4 * x + 1] = source[2*x+0]; |
| 716 | dest[4 * x + 2] = source[2*x+0]; |
| 717 | dest[4 * x + 3] = source[2*x+1]; |
| 718 | } |
| 719 | } |
| 720 | else |
| 721 | { |
| 722 | memcpy(dest, source, width * 2); |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | void Image::loadLuminanceAlphaFloatData(GLsizei width, GLsizei height, |
| 728 | int inputPitch, const void *input, size_t outputPitch, void *output) const |
| 729 | { |
| 730 | const float *source = NULL; |
| 731 | float *dest = NULL; |
| 732 | |
| 733 | for (int y = 0; y < height; y++) |
| 734 | { |
| 735 | source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch); |
| 736 | dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputPitch); |
| 737 | for (int x = 0; x < width; x++) |
| 738 | { |
| 739 | dest[4 * x + 0] = source[2*x+0]; |
| 740 | dest[4 * x + 1] = source[2*x+0]; |
| 741 | dest[4 * x + 2] = source[2*x+0]; |
| 742 | dest[4 * x + 3] = source[2*x+1]; |
| 743 | } |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | void Image::loadLuminanceAlphaHalfFloatData(GLsizei width, GLsizei height, |
| 748 | int inputPitch, const void *input, size_t outputPitch, void *output) const |
| 749 | { |
| 750 | const unsigned short *source = NULL; |
| 751 | unsigned short *dest = NULL; |
| 752 | |
| 753 | for (int y = 0; y < height; y++) |
| 754 | { |
| 755 | source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch); |
| 756 | dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + y * outputPitch); |
| 757 | for (int x = 0; x < width; x++) |
| 758 | { |
| 759 | dest[4 * x + 0] = source[2*x+0]; |
| 760 | dest[4 * x + 1] = source[2*x+0]; |
| 761 | dest[4 * x + 2] = source[2*x+0]; |
| 762 | dest[4 * x + 3] = source[2*x+1]; |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | void Image::loadRGBUByteData(GLsizei width, GLsizei height, |
| 768 | int inputPitch, const void *input, size_t outputPitch, void *output) const |
| 769 | { |
| 770 | const unsigned char *source = NULL; |
| 771 | unsigned char *dest = NULL; |
| 772 | |
| 773 | for (int y = 0; y < height; y++) |
| 774 | { |
| 775 | source = static_cast<const unsigned char*>(input) + y * inputPitch; |
| 776 | dest = static_cast<unsigned char*>(output) + y * outputPitch; |
| 777 | for (int x = 0; x < width; x++) |
| 778 | { |
| 779 | dest[4 * x + 0] = source[x * 3 + 2]; |
| 780 | dest[4 * x + 1] = source[x * 3 + 1]; |
| 781 | dest[4 * x + 2] = source[x * 3 + 0]; |
| 782 | dest[4 * x + 3] = 0xFF; |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | void Image::loadRGB565Data(GLsizei width, GLsizei height, |
| 788 | int inputPitch, const void *input, size_t outputPitch, void *output) const |
| 789 | { |
| 790 | const unsigned short *source = NULL; |
| 791 | unsigned char *dest = NULL; |
| 792 | |
| 793 | for (int y = 0; y < height; y++) |
| 794 | { |
| 795 | source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch); |
| 796 | dest = static_cast<unsigned char*>(output) + y * outputPitch; |
| 797 | for (int x = 0; x < width; x++) |
| 798 | { |
| 799 | unsigned short rgba = source[x]; |
| 800 | dest[4 * x + 0] = ((rgba & 0x001F) << 3) | ((rgba & 0x001F) >> 2); |
| 801 | dest[4 * x + 1] = ((rgba & 0x07E0) >> 3) | ((rgba & 0x07E0) >> 9); |
| 802 | dest[4 * x + 2] = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13); |
| 803 | dest[4 * x + 3] = 0xFF; |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | void Image::loadRGBFloatData(GLsizei width, GLsizei height, |
| 809 | int inputPitch, const void *input, size_t outputPitch, void *output) const |
| 810 | { |
| 811 | const float *source = NULL; |
| 812 | float *dest = NULL; |
| 813 | |
| 814 | for (int y = 0; y < height; y++) |
| 815 | { |
| 816 | source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch); |
| 817 | dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputPitch); |
| 818 | for (int x = 0; x < width; x++) |
| 819 | { |
| 820 | dest[4 * x + 0] = source[x * 3 + 0]; |
| 821 | dest[4 * x + 1] = source[x * 3 + 1]; |
| 822 | dest[4 * x + 2] = source[x * 3 + 2]; |
| 823 | dest[4 * x + 3] = 1.0f; |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | void Image::loadRGBHalfFloatData(GLsizei width, GLsizei height, |
| 829 | int inputPitch, const void *input, size_t outputPitch, void *output) const |
| 830 | { |
| 831 | const unsigned short *source = NULL; |
| 832 | unsigned short *dest = NULL; |
| 833 | |
| 834 | for (int y = 0; y < height; y++) |
| 835 | { |
| 836 | source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch); |
| 837 | dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + y * outputPitch); |
| 838 | for (int x = 0; x < width; x++) |
| 839 | { |
| 840 | dest[4 * x + 0] = source[x * 3 + 0]; |
| 841 | dest[4 * x + 1] = source[x * 3 + 1]; |
| 842 | dest[4 * x + 2] = source[x * 3 + 2]; |
| 843 | dest[4 * x + 3] = 0x3C00; // SEEEEEMMMMMMMMMM, S = 0, E = 15, M = 0: 16bit flpt representation of 1 |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | void Image::loadRGBAUByteData(GLsizei width, GLsizei height, |
| 849 | int inputPitch, const void *input, size_t outputPitch, void *output) const |
| 850 | { |
| 851 | const unsigned int *source = NULL; |
| 852 | unsigned int *dest = NULL; |
| 853 | for (int y = 0; y < height; y++) |
| 854 | { |
| 855 | source = reinterpret_cast<const unsigned int*>(static_cast<const unsigned char*>(input) + y * inputPitch); |
| 856 | dest = reinterpret_cast<unsigned int*>(static_cast<unsigned char*>(output) + y * outputPitch); |
| 857 | |
| 858 | for (int x = 0; x < width; x++) |
| 859 | { |
| 860 | unsigned int rgba = source[x]; |
| 861 | dest[x] = (_rotl(rgba, 16) & 0x00ff00ff) | (rgba & 0xff00ff00); |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | void Image::loadRGBA4444Data(GLsizei width, GLsizei height, |
| 867 | int inputPitch, const void *input, size_t outputPitch, void *output) const |
| 868 | { |
| 869 | const unsigned short *source = NULL; |
| 870 | unsigned char *dest = NULL; |
| 871 | |
| 872 | for (int y = 0; y < height; y++) |
| 873 | { |
| 874 | source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch); |
| 875 | dest = static_cast<unsigned char*>(output) + y * outputPitch; |
| 876 | for (int x = 0; x < width; x++) |
| 877 | { |
| 878 | unsigned short rgba = source[x]; |
| 879 | dest[4 * x + 0] = ((rgba & 0x00F0) << 0) | ((rgba & 0x00F0) >> 4); |
| 880 | dest[4 * x + 1] = ((rgba & 0x0F00) >> 4) | ((rgba & 0x0F00) >> 8); |
| 881 | dest[4 * x + 2] = ((rgba & 0xF000) >> 8) | ((rgba & 0xF000) >> 12); |
| 882 | dest[4 * x + 3] = ((rgba & 0x000F) << 4) | ((rgba & 0x000F) >> 0); |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | void Image::loadRGBA5551Data(GLsizei width, GLsizei height, |
| 888 | int inputPitch, const void *input, size_t outputPitch, void *output) const |
| 889 | { |
| 890 | const unsigned short *source = NULL; |
| 891 | unsigned char *dest = NULL; |
| 892 | |
| 893 | for (int y = 0; y < height; y++) |
| 894 | { |
| 895 | source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch); |
| 896 | dest = static_cast<unsigned char*>(output) + y * outputPitch; |
| 897 | for (int x = 0; x < width; x++) |
| 898 | { |
| 899 | unsigned short rgba = source[x]; |
| 900 | dest[4 * x + 0] = ((rgba & 0x003E) << 2) | ((rgba & 0x003E) >> 3); |
| 901 | dest[4 * x + 1] = ((rgba & 0x07C0) >> 3) | ((rgba & 0x07C0) >> 8); |
| 902 | dest[4 * x + 2] = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13); |
| 903 | dest[4 * x + 3] = (rgba & 0x0001) ? 0xFF : 0; |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | void Image::loadRGBAFloatData(GLsizei width, GLsizei height, |
| 909 | int inputPitch, const void *input, size_t outputPitch, void *output) const |
| 910 | { |
| 911 | const float *source = NULL; |
| 912 | float *dest = NULL; |
| 913 | |
| 914 | for (int y = 0; y < height; y++) |
| 915 | { |
| 916 | source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch); |
| 917 | dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputPitch); |
| 918 | memcpy(dest, source, width * 16); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | void Image::loadRGBAHalfFloatData(GLsizei width, GLsizei height, |
| 923 | int inputPitch, const void *input, size_t outputPitch, void *output) const |
| 924 | { |
| 925 | const unsigned char *source = NULL; |
| 926 | unsigned char *dest = NULL; |
| 927 | |
| 928 | for (int y = 0; y < height; y++) |
| 929 | { |
| 930 | source = static_cast<const unsigned char*>(input) + y * inputPitch; |
| 931 | dest = static_cast<unsigned char*>(output) + y * outputPitch; |
| 932 | memcpy(dest, source, width * 8); |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | void Image::loadBGRAData(GLsizei width, GLsizei height, |
| 937 | int inputPitch, const void *input, size_t outputPitch, void *output) const |
| 938 | { |
| 939 | const unsigned char *source = NULL; |
| 940 | unsigned char *dest = NULL; |
| 941 | |
| 942 | for (int y = 0; y < height; y++) |
| 943 | { |
| 944 | source = static_cast<const unsigned char*>(input) + y * inputPitch; |
| 945 | dest = static_cast<unsigned char*>(output) + y * outputPitch; |
| 946 | memcpy(dest, source, width*4); |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | void Image::loadCompressedData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, |
| 951 | const void *input) { |
| 952 | ASSERT(xoffset % 4 == 0); |
| 953 | ASSERT(yoffset % 4 == 0); |
| 954 | |
| 955 | RECT lockRect = { |
| 956 | xoffset, yoffset, |
| 957 | xoffset + width, yoffset + height |
| 958 | }; |
| 959 | |
| 960 | D3DLOCKED_RECT locked; |
| 961 | HRESULT result = lock(&locked, &lockRect); |
| 962 | if (FAILED(result)) |
| 963 | { |
| 964 | return; |
| 965 | } |
| 966 | |
| 967 | GLsizei inputSize = ComputeCompressedSize(width, height, mInternalFormat); |
| 968 | GLsizei inputPitch = ComputeCompressedPitch(width, mInternalFormat); |
| 969 | int rows = inputSize / inputPitch; |
| 970 | for (int i = 0; i < rows; ++i) |
| 971 | { |
| 972 | memcpy((void*)((BYTE*)locked.pBits + i * locked.Pitch), (void*)((BYTE*)input + i * inputPitch), inputPitch); |
| 973 | } |
| 974 | |
| 975 | unlock(); |
| 976 | } |
| 977 | |
| 978 | // This implements glCopyTex[Sub]Image2D for non-renderable internal texture formats and incomplete textures |
daniel@transgaming.com | 3cef539 | 2012-10-31 19:52:15 +0000 | [diff] [blame] | 979 | void Image::copy(GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) |
daniel@transgaming.com | b9d7e6f | 2012-10-31 19:08:32 +0000 | [diff] [blame] | 980 | { |
daniel@transgaming.com | 3cef539 | 2012-10-31 19:52:15 +0000 | [diff] [blame] | 981 | IDirect3DSurface9 *renderTarget = source->getRenderTarget(); |
| 982 | |
| 983 | if (!renderTarget) |
| 984 | { |
| 985 | ERR("Failed to retrieve the render target."); |
| 986 | return error(GL_OUT_OF_MEMORY); |
| 987 | } |
| 988 | |
daniel@transgaming.com | b9d7e6f | 2012-10-31 19:08:32 +0000 | [diff] [blame] | 989 | IDirect3DDevice9 *device = getDisplay()->getRenderer()->getDevice(); // D3D9_REPLACE |
| 990 | IDirect3DSurface9 *renderTargetData = NULL; |
| 991 | D3DSURFACE_DESC description; |
| 992 | renderTarget->GetDesc(&description); |
| 993 | |
| 994 | HRESULT result = device->CreateOffscreenPlainSurface(description.Width, description.Height, description.Format, D3DPOOL_SYSTEMMEM, &renderTargetData, NULL); |
| 995 | |
| 996 | if (FAILED(result)) |
| 997 | { |
| 998 | ERR("Could not create matching destination surface."); |
daniel@transgaming.com | 3cef539 | 2012-10-31 19:52:15 +0000 | [diff] [blame] | 999 | renderTarget->Release(); |
daniel@transgaming.com | b9d7e6f | 2012-10-31 19:08:32 +0000 | [diff] [blame] | 1000 | return error(GL_OUT_OF_MEMORY); |
| 1001 | } |
| 1002 | |
| 1003 | result = device->GetRenderTargetData(renderTarget, renderTargetData); |
| 1004 | |
| 1005 | if (FAILED(result)) |
| 1006 | { |
| 1007 | ERR("GetRenderTargetData unexpectedly failed."); |
| 1008 | renderTargetData->Release(); |
daniel@transgaming.com | 3cef539 | 2012-10-31 19:52:15 +0000 | [diff] [blame] | 1009 | renderTarget->Release(); |
daniel@transgaming.com | b9d7e6f | 2012-10-31 19:08:32 +0000 | [diff] [blame] | 1010 | return error(GL_OUT_OF_MEMORY); |
| 1011 | } |
| 1012 | |
| 1013 | RECT sourceRect = {x, y, x + width, y + height}; |
| 1014 | RECT destRect = {xoffset, yoffset, xoffset + width, yoffset + height}; |
| 1015 | |
| 1016 | D3DLOCKED_RECT sourceLock = {0}; |
| 1017 | result = renderTargetData->LockRect(&sourceLock, &sourceRect, 0); |
| 1018 | |
| 1019 | if (FAILED(result)) |
| 1020 | { |
| 1021 | ERR("Failed to lock the source surface (rectangle might be invalid)."); |
| 1022 | renderTargetData->Release(); |
daniel@transgaming.com | 3cef539 | 2012-10-31 19:52:15 +0000 | [diff] [blame] | 1023 | renderTarget->Release(); |
daniel@transgaming.com | b9d7e6f | 2012-10-31 19:08:32 +0000 | [diff] [blame] | 1024 | return error(GL_OUT_OF_MEMORY); |
| 1025 | } |
| 1026 | |
| 1027 | D3DLOCKED_RECT destLock = {0}; |
| 1028 | result = lock(&destLock, &destRect); |
| 1029 | |
| 1030 | if (FAILED(result)) |
| 1031 | { |
| 1032 | ERR("Failed to lock the destination surface (rectangle might be invalid)."); |
| 1033 | renderTargetData->UnlockRect(); |
| 1034 | renderTargetData->Release(); |
daniel@transgaming.com | 3cef539 | 2012-10-31 19:52:15 +0000 | [diff] [blame] | 1035 | renderTarget->Release(); |
daniel@transgaming.com | b9d7e6f | 2012-10-31 19:08:32 +0000 | [diff] [blame] | 1036 | return error(GL_OUT_OF_MEMORY); |
| 1037 | } |
| 1038 | |
| 1039 | if (destLock.pBits && sourceLock.pBits) |
| 1040 | { |
| 1041 | unsigned char *source = (unsigned char*)sourceLock.pBits; |
| 1042 | unsigned char *dest = (unsigned char*)destLock.pBits; |
| 1043 | |
| 1044 | switch (description.Format) |
| 1045 | { |
| 1046 | case D3DFMT_X8R8G8B8: |
| 1047 | case D3DFMT_A8R8G8B8: |
| 1048 | switch(getD3DFormat()) |
| 1049 | { |
| 1050 | case D3DFMT_X8R8G8B8: |
| 1051 | case D3DFMT_A8R8G8B8: |
| 1052 | for(int y = 0; y < height; y++) |
| 1053 | { |
| 1054 | memcpy(dest, source, 4 * width); |
| 1055 | |
| 1056 | source += sourceLock.Pitch; |
| 1057 | dest += destLock.Pitch; |
| 1058 | } |
| 1059 | break; |
| 1060 | case D3DFMT_L8: |
| 1061 | for(int y = 0; y < height; y++) |
| 1062 | { |
| 1063 | for(int x = 0; x < width; x++) |
| 1064 | { |
| 1065 | dest[x] = source[x * 4 + 2]; |
| 1066 | } |
| 1067 | |
| 1068 | source += sourceLock.Pitch; |
| 1069 | dest += destLock.Pitch; |
| 1070 | } |
| 1071 | break; |
| 1072 | case D3DFMT_A8L8: |
| 1073 | for(int y = 0; y < height; y++) |
| 1074 | { |
| 1075 | for(int x = 0; x < width; x++) |
| 1076 | { |
| 1077 | dest[x * 2 + 0] = source[x * 4 + 2]; |
| 1078 | dest[x * 2 + 1] = source[x * 4 + 3]; |
| 1079 | } |
| 1080 | |
| 1081 | source += sourceLock.Pitch; |
| 1082 | dest += destLock.Pitch; |
| 1083 | } |
| 1084 | break; |
| 1085 | default: |
| 1086 | UNREACHABLE(); |
| 1087 | } |
| 1088 | break; |
| 1089 | case D3DFMT_R5G6B5: |
| 1090 | switch(getD3DFormat()) |
| 1091 | { |
| 1092 | case D3DFMT_X8R8G8B8: |
| 1093 | for(int y = 0; y < height; y++) |
| 1094 | { |
| 1095 | for(int x = 0; x < width; x++) |
| 1096 | { |
| 1097 | unsigned short rgb = ((unsigned short*)source)[x]; |
| 1098 | unsigned char red = (rgb & 0xF800) >> 8; |
| 1099 | unsigned char green = (rgb & 0x07E0) >> 3; |
| 1100 | unsigned char blue = (rgb & 0x001F) << 3; |
| 1101 | dest[x + 0] = blue | (blue >> 5); |
| 1102 | dest[x + 1] = green | (green >> 6); |
| 1103 | dest[x + 2] = red | (red >> 5); |
| 1104 | dest[x + 3] = 0xFF; |
| 1105 | } |
| 1106 | |
| 1107 | source += sourceLock.Pitch; |
| 1108 | dest += destLock.Pitch; |
| 1109 | } |
| 1110 | break; |
| 1111 | case D3DFMT_L8: |
| 1112 | for(int y = 0; y < height; y++) |
| 1113 | { |
| 1114 | for(int x = 0; x < width; x++) |
| 1115 | { |
| 1116 | unsigned char red = source[x * 2 + 1] & 0xF8; |
| 1117 | dest[x] = red | (red >> 5); |
| 1118 | } |
| 1119 | |
| 1120 | source += sourceLock.Pitch; |
| 1121 | dest += destLock.Pitch; |
| 1122 | } |
| 1123 | break; |
| 1124 | default: |
| 1125 | UNREACHABLE(); |
| 1126 | } |
| 1127 | break; |
| 1128 | case D3DFMT_A1R5G5B5: |
| 1129 | switch(getD3DFormat()) |
| 1130 | { |
| 1131 | case D3DFMT_X8R8G8B8: |
| 1132 | for(int y = 0; y < height; y++) |
| 1133 | { |
| 1134 | for(int x = 0; x < width; x++) |
| 1135 | { |
| 1136 | unsigned short argb = ((unsigned short*)source)[x]; |
| 1137 | unsigned char red = (argb & 0x7C00) >> 7; |
| 1138 | unsigned char green = (argb & 0x03E0) >> 2; |
| 1139 | unsigned char blue = (argb & 0x001F) << 3; |
| 1140 | dest[x + 0] = blue | (blue >> 5); |
| 1141 | dest[x + 1] = green | (green >> 5); |
| 1142 | dest[x + 2] = red | (red >> 5); |
| 1143 | dest[x + 3] = 0xFF; |
| 1144 | } |
| 1145 | |
| 1146 | source += sourceLock.Pitch; |
| 1147 | dest += destLock.Pitch; |
| 1148 | } |
| 1149 | break; |
| 1150 | case D3DFMT_A8R8G8B8: |
| 1151 | for(int y = 0; y < height; y++) |
| 1152 | { |
| 1153 | for(int x = 0; x < width; x++) |
| 1154 | { |
| 1155 | unsigned short argb = ((unsigned short*)source)[x]; |
| 1156 | unsigned char red = (argb & 0x7C00) >> 7; |
| 1157 | unsigned char green = (argb & 0x03E0) >> 2; |
| 1158 | unsigned char blue = (argb & 0x001F) << 3; |
| 1159 | unsigned char alpha = (signed short)argb >> 15; |
| 1160 | dest[x + 0] = blue | (blue >> 5); |
| 1161 | dest[x + 1] = green | (green >> 5); |
| 1162 | dest[x + 2] = red | (red >> 5); |
| 1163 | dest[x + 3] = alpha; |
| 1164 | } |
| 1165 | |
| 1166 | source += sourceLock.Pitch; |
| 1167 | dest += destLock.Pitch; |
| 1168 | } |
| 1169 | break; |
| 1170 | case D3DFMT_L8: |
| 1171 | for(int y = 0; y < height; y++) |
| 1172 | { |
| 1173 | for(int x = 0; x < width; x++) |
| 1174 | { |
| 1175 | unsigned char red = source[x * 2 + 1] & 0x7C; |
| 1176 | dest[x] = (red << 1) | (red >> 4); |
| 1177 | } |
| 1178 | |
| 1179 | source += sourceLock.Pitch; |
| 1180 | dest += destLock.Pitch; |
| 1181 | } |
| 1182 | break; |
| 1183 | case D3DFMT_A8L8: |
| 1184 | for(int y = 0; y < height; y++) |
| 1185 | { |
| 1186 | for(int x = 0; x < width; x++) |
| 1187 | { |
| 1188 | unsigned char red = source[x * 2 + 1] & 0x7C; |
| 1189 | dest[x * 2 + 0] = (red << 1) | (red >> 4); |
| 1190 | dest[x * 2 + 1] = (signed char)source[x * 2 + 1] >> 7; |
| 1191 | } |
| 1192 | |
| 1193 | source += sourceLock.Pitch; |
| 1194 | dest += destLock.Pitch; |
| 1195 | } |
| 1196 | break; |
| 1197 | default: |
| 1198 | UNREACHABLE(); |
| 1199 | } |
| 1200 | break; |
| 1201 | default: |
| 1202 | UNREACHABLE(); |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | unlock(); |
| 1207 | renderTargetData->UnlockRect(); |
| 1208 | |
| 1209 | renderTargetData->Release(); |
daniel@transgaming.com | 3cef539 | 2012-10-31 19:52:15 +0000 | [diff] [blame] | 1210 | renderTarget->Release(); |
daniel@transgaming.com | b9d7e6f | 2012-10-31 19:08:32 +0000 | [diff] [blame] | 1211 | |
| 1212 | mDirty = true; |
| 1213 | } |
| 1214 | |
| 1215 | } |