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