blob: 10075222542b8e5e2b57f40d805eb65c0a1fc24e [file] [log] [blame]
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00001//
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// Image9.cpp: Implements the rx::Image9 class, which acts as the interface to
8// the actual underlying surfaces of a Texture.
9
10#include "libGLESv2/renderer/Image9.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"
18#include "libGLESv2/Framebuffer.h"
19#include "libGLESv2/renderer/RenderTarget9.h"
20
21#include "libGLESv2/renderer/renderer9_utils.h"
22
23namespace rx
24{
25
26namespace
27{
28struct 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
38struct 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
49struct 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
62struct 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 {
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);
75 }
76};
77
78struct 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
94template <typename T>
95void 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
148void 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
204Image9::Image9()
205{
206 mSurface = NULL;
207 mRenderer = NULL;
208
209 mD3DPool = D3DPOOL_SYSTEMMEM;
210 mD3DFormat = D3DFMT_UNKNOWN;
211}
212
213Image9::~Image9()
214{
215 if (mSurface)
216 {
217 mSurface->Release();
218 }
219}
220
221Image9 *Image9::makeImage9(Image *img)
222{
223 ASSERT(dynamic_cast<rx::Image9*>(img) != NULL);
224 return static_cast<rx::Image9*>(img);
225}
226
227void Image9::generateMipmap(Image9 *dest, Image9 *source)
228{
229 IDirect3DSurface9 *sourceSurface = source->getSurface();
230 if (sourceSurface == NULL)
231 return error(GL_OUT_OF_MEMORY);
232
233 IDirect3DSurface9 *destSurface = dest->getSurface();
234 GenerateMip(destSurface, sourceSurface);
235
236 source->markDirty();
237}
238
239void Image9::copyLockableSurfaces(IDirect3DSurface9 *dest, IDirect3DSurface9 *source)
240{
241 D3DLOCKED_RECT sourceLock = {0};
242 D3DLOCKED_RECT destLock = {0};
243
244 source->LockRect(&sourceLock, NULL, 0);
245 dest->LockRect(&destLock, NULL, 0);
246
247 if (sourceLock.pBits && destLock.pBits)
248 {
249 D3DSURFACE_DESC desc;
250 source->GetDesc(&desc);
251
252 int rows = dx::IsCompressedFormat(desc.Format) ? desc.Height / 4 : desc.Height;
253 int bytes = dx::ComputeRowSize(desc.Format, desc.Width);
254 ASSERT(bytes <= sourceLock.Pitch && bytes <= destLock.Pitch);
255
256 for(int i = 0; i < rows; i++)
257 {
258 memcpy((char*)destLock.pBits + destLock.Pitch * i, (char*)sourceLock.pBits + sourceLock.Pitch * i, bytes);
259 }
260
261 source->UnlockRect();
262 dest->UnlockRect();
263 }
264 else UNREACHABLE();
265}
266
267bool Image9::redefine(rx::Renderer *renderer, GLint internalformat, GLsizei width, GLsizei height, bool forceRelease)
268{
269 if (mWidth != width ||
270 mHeight != height ||
271 mInternalFormat != internalformat ||
272 forceRelease)
273 {
274 mRenderer = Renderer9::makeRenderer9(renderer);
275
276 mWidth = width;
277 mHeight = height;
278 mInternalFormat = internalformat;
279 // compute the d3d format that will be used
280 mD3DFormat = mRenderer->ConvertTextureInternalFormat(internalformat);
281 mActualFormat = d3d9_gl::GetEquivalentFormat(mD3DFormat);
282
283 if (mSurface)
284 {
285 mSurface->Release();
286 mSurface = NULL;
287 }
288
289 return true;
290 }
291
292 return false;
293}
294
295void Image9::createSurface()
296{
297 if(mSurface)
298 {
299 return;
300 }
301
302 IDirect3DTexture9 *newTexture = NULL;
303 IDirect3DSurface9 *newSurface = NULL;
304 const D3DPOOL poolToUse = D3DPOOL_SYSTEMMEM;
305 const D3DFORMAT d3dFormat = getD3DFormat();
306 ASSERT(d3dFormat != D3DFMT_INTZ); // We should never get here for depth textures
307
308 if (mWidth != 0 && mHeight != 0)
309 {
310 int levelToFetch = 0;
311 GLsizei requestWidth = mWidth;
312 GLsizei requestHeight = mHeight;
313 gl::MakeValidSize(true, gl::IsCompressed(mInternalFormat), &requestWidth, &requestHeight, &levelToFetch);
314
315 IDirect3DDevice9 *device = mRenderer->getDevice(); // D3D9_REPLACE
316
317 HRESULT result = device->CreateTexture(requestWidth, requestHeight, levelToFetch + 1, 0, d3dFormat,
318 poolToUse, &newTexture, NULL);
319
320 if (FAILED(result))
321 {
322 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
323 ERR("Creating image surface failed.");
324 return error(GL_OUT_OF_MEMORY);
325 }
326
327 newTexture->GetSurfaceLevel(levelToFetch, &newSurface);
328 newTexture->Release();
329 }
330
331 mSurface = newSurface;
332 mDirty = false;
333 mD3DPool = poolToUse;
334}
335
336HRESULT Image9::lock(D3DLOCKED_RECT *lockedRect, const RECT *rect)
337{
338 createSurface();
339
340 HRESULT result = D3DERR_INVALIDCALL;
341
342 if (mSurface)
343 {
344 result = mSurface->LockRect(lockedRect, rect, 0);
345 ASSERT(SUCCEEDED(result));
346
347 mDirty = true;
348 }
349
350 return result;
351}
352
353void Image9::unlock()
354{
355 if (mSurface)
356 {
357 HRESULT result = mSurface->UnlockRect();
358 ASSERT(SUCCEEDED(result));
359 }
360}
361
362bool Image9::isRenderableFormat() const
363{
364 return TextureStorage::IsTextureFormatRenderable(getD3DFormat());
365}
366
367D3DFORMAT Image9::getD3DFormat() const
368{
369 // this should only happen if the image hasn't been redefined first
370 // which would be a bug by the caller
371 ASSERT(mD3DFormat != D3DFMT_UNKNOWN);
372
373 return mD3DFormat;
374}
375
376IDirect3DSurface9 *Image9::getSurface()
377{
378 createSurface();
379
380 return mSurface;
381}
382
383void Image9::setManagedSurface(TextureStorage2D *storage, int level)
384{
385 setManagedSurface(storage->getSurfaceLevel(level, false));
386}
387
388void Image9::setManagedSurface(TextureStorageCubeMap *storage, int face, int level)
389{
390 setManagedSurface(storage->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, false));
391}
392
393void Image9::setManagedSurface(IDirect3DSurface9 *surface)
394{
395 D3DSURFACE_DESC desc;
396 surface->GetDesc(&desc);
397 ASSERT(desc.Pool == D3DPOOL_MANAGED);
398
399 if ((GLsizei)desc.Width == mWidth && (GLsizei)desc.Height == mHeight)
400 {
401 if (mSurface)
402 {
403 copyLockableSurfaces(surface, mSurface);
404 mSurface->Release();
405 }
406
407 mSurface = surface;
408 mD3DPool = desc.Pool;
409 }
410}
411
412bool Image9::updateSurface(TextureStorage2D *storage, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
413{
414 ASSERT(getSurface() != NULL);
415 return updateSurface(storage->getSurfaceLevel(level, true), xoffset, yoffset, width, height);
416}
417
418bool Image9::updateSurface(TextureStorageCubeMap *storage, int face, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
419{
420 ASSERT(getSurface() != NULL);
421 return updateSurface(storage->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, true), xoffset, yoffset, width, height);
422}
423
424bool Image9::updateSurface(IDirect3DSurface9 *destSurface, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
425{
426 if (!destSurface)
427 return false;
428
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
441 IDirect3DDevice9 *device = mRenderer->getDevice(); // D3D9_REPLACE
442
443 if (mD3DPool == D3DPOOL_MANAGED)
444 {
445 D3DSURFACE_DESC desc;
446 sourceSurface->GetDesc(&desc);
447
448 IDirect3DSurface9 *surf = 0;
449 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
450
451 if (SUCCEEDED(result))
452 {
453 copyLockableSurfaces(surf, sourceSurface);
454 result = device->UpdateSurface(surf, &rect, destSurface, &point);
455 ASSERT(SUCCEEDED(result));
456 surf->Release();
457 }
458 }
459 else
460 {
461 // UpdateSurface: source must be SYSTEMMEM, dest must be DEFAULT pools
462 HRESULT result = device->UpdateSurface(sourceSurface, &rect, destSurface, &point);
463 ASSERT(SUCCEEDED(result));
464 }
465 }
466
467 destSurface->Release();
468 return true;
469}
470
471// Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input
472// into the target pixel rectangle.
473void Image9::loadData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
474 GLint unpackAlignment, const void *input)
475{
476 RECT lockRect =
477 {
478 xoffset, yoffset,
479 xoffset + width, yoffset + height
480 };
481
482 D3DLOCKED_RECT locked;
483 HRESULT result = lock(&locked, &lockRect);
484 if (FAILED(result))
485 {
486 return;
487 }
488
489
490 GLsizei inputPitch = gl::ComputePitch(width, mInternalFormat, unpackAlignment);
491
492 switch (mInternalFormat)
493 {
494 case GL_ALPHA8_EXT:
495 if (gl::supportsSSE2())
496 {
497 loadAlphaDataSSE2(width, height, inputPitch, input, locked.Pitch, locked.pBits);
498 }
499 else
500 {
501 loadAlphaData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
502 }
503 break;
504 case GL_LUMINANCE8_EXT:
505 loadLuminanceData(width, height, inputPitch, input, locked.Pitch, locked.pBits, getD3DFormat() == D3DFMT_L8);
506 break;
507 case GL_ALPHA32F_EXT:
508 loadAlphaFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
509 break;
510 case GL_LUMINANCE32F_EXT:
511 loadLuminanceFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
512 break;
513 case GL_ALPHA16F_EXT:
514 loadAlphaHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
515 break;
516 case GL_LUMINANCE16F_EXT:
517 loadLuminanceHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
518 break;
519 case GL_LUMINANCE8_ALPHA8_EXT:
520 loadLuminanceAlphaData(width, height, inputPitch, input, locked.Pitch, locked.pBits, getD3DFormat() == D3DFMT_A8L8);
521 break;
522 case GL_LUMINANCE_ALPHA32F_EXT:
523 loadLuminanceAlphaFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
524 break;
525 case GL_LUMINANCE_ALPHA16F_EXT:
526 loadLuminanceAlphaHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
527 break;
528 case GL_RGB8_OES:
529 loadRGBUByteData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
530 break;
531 case GL_RGB565:
532 loadRGB565Data(width, height, inputPitch, input, locked.Pitch, locked.pBits);
533 break;
534 case GL_RGBA8_OES:
535 if (gl::supportsSSE2())
536 {
537 loadRGBAUByteDataSSE2(width, height, inputPitch, input, locked.Pitch, locked.pBits);
538 }
539 else
540 {
541 loadRGBAUByteData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
542 }
543 break;
544 case GL_RGBA4:
545 loadRGBA4444Data(width, height, inputPitch, input, locked.Pitch, locked.pBits);
546 break;
547 case GL_RGB5_A1:
548 loadRGBA5551Data(width, height, inputPitch, input, locked.Pitch, locked.pBits);
549 break;
550 case GL_BGRA8_EXT:
551 loadBGRAData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
552 break;
553 // float textures are converted to RGBA, not BGRA, as they're stored that way in D3D
554 case GL_RGB32F_EXT:
555 loadRGBFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
556 break;
557 case GL_RGB16F_EXT:
558 loadRGBHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
559 break;
560 case GL_RGBA32F_EXT:
561 loadRGBAFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
562 break;
563 case GL_RGBA16F_EXT:
564 loadRGBAHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
565 break;
566 default: UNREACHABLE();
567 }
568
569 unlock();
570}
571
572void Image9::loadCompressedData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
573 const void *input)
574{
575 ASSERT(xoffset % 4 == 0);
576 ASSERT(yoffset % 4 == 0);
577
578 RECT lockRect = {
579 xoffset, yoffset,
580 xoffset + width, yoffset + height
581 };
582
583 D3DLOCKED_RECT locked;
584 HRESULT result = lock(&locked, &lockRect);
585 if (FAILED(result))
586 {
587 return;
588 }
589
590 GLsizei inputSize = gl::ComputeCompressedSize(width, height, mInternalFormat);
591 GLsizei inputPitch = gl::ComputeCompressedPitch(width, mInternalFormat);
592 int rows = inputSize / inputPitch;
593 for (int i = 0; i < rows; ++i)
594 {
595 memcpy((void*)((BYTE*)locked.pBits + i * locked.Pitch), (void*)((BYTE*)input + i * inputPitch), inputPitch);
596 }
597
598 unlock();
599}
600
601// This implements glCopyTex[Sub]Image2D for non-renderable internal texture formats and incomplete textures
602void Image9::copy(GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, gl::Framebuffer *source)
603{
604 RenderTarget9 *renderTarget = NULL;
605 IDirect3DSurface9 *surface = NULL;
606 gl::Renderbuffer *colorbuffer = source->getColorbuffer();
607
608 if (colorbuffer)
609 {
610 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
611 }
612
613 if (renderTarget)
614 {
615 surface = renderTarget->getSurface();
616 }
617
618 if (!surface)
619 {
620 ERR("Failed to retrieve the render target.");
621 return error(GL_OUT_OF_MEMORY);
622 }
623
624 IDirect3DDevice9 *device = mRenderer->getDevice(); // D3D9_REPLACE
625
626 IDirect3DSurface9 *renderTargetData = NULL;
627 D3DSURFACE_DESC description;
628 surface->GetDesc(&description);
629
630 HRESULT result = device->CreateOffscreenPlainSurface(description.Width, description.Height, description.Format, D3DPOOL_SYSTEMMEM, &renderTargetData, NULL);
631
632 if (FAILED(result))
633 {
634 ERR("Could not create matching destination surface.");
635 surface->Release();
636 return error(GL_OUT_OF_MEMORY);
637 }
638
639 result = device->GetRenderTargetData(surface, renderTargetData);
640
641 if (FAILED(result))
642 {
643 ERR("GetRenderTargetData unexpectedly failed.");
644 renderTargetData->Release();
645 surface->Release();
646 return error(GL_OUT_OF_MEMORY);
647 }
648
649 RECT sourceRect = {x, y, x + width, y + height};
650 RECT destRect = {xoffset, yoffset, xoffset + width, yoffset + height};
651
652 D3DLOCKED_RECT sourceLock = {0};
653 result = renderTargetData->LockRect(&sourceLock, &sourceRect, 0);
654
655 if (FAILED(result))
656 {
657 ERR("Failed to lock the source surface (rectangle might be invalid).");
658 renderTargetData->Release();
659 surface->Release();
660 return error(GL_OUT_OF_MEMORY);
661 }
662
663 D3DLOCKED_RECT destLock = {0};
664 result = lock(&destLock, &destRect);
665
666 if (FAILED(result))
667 {
668 ERR("Failed to lock the destination surface (rectangle might be invalid).");
669 renderTargetData->UnlockRect();
670 renderTargetData->Release();
671 surface->Release();
672 return error(GL_OUT_OF_MEMORY);
673 }
674
675 if (destLock.pBits && sourceLock.pBits)
676 {
677 unsigned char *source = (unsigned char*)sourceLock.pBits;
678 unsigned char *dest = (unsigned char*)destLock.pBits;
679
680 switch (description.Format)
681 {
682 case D3DFMT_X8R8G8B8:
683 case D3DFMT_A8R8G8B8:
684 switch(getD3DFormat())
685 {
686 case D3DFMT_X8R8G8B8:
687 case D3DFMT_A8R8G8B8:
688 for(int y = 0; y < height; y++)
689 {
690 memcpy(dest, source, 4 * width);
691
692 source += sourceLock.Pitch;
693 dest += destLock.Pitch;
694 }
695 break;
696 case D3DFMT_L8:
697 for(int y = 0; y < height; y++)
698 {
699 for(int x = 0; x < width; x++)
700 {
701 dest[x] = source[x * 4 + 2];
702 }
703
704 source += sourceLock.Pitch;
705 dest += destLock.Pitch;
706 }
707 break;
708 case D3DFMT_A8L8:
709 for(int y = 0; y < height; y++)
710 {
711 for(int x = 0; x < width; x++)
712 {
713 dest[x * 2 + 0] = source[x * 4 + 2];
714 dest[x * 2 + 1] = source[x * 4 + 3];
715 }
716
717 source += sourceLock.Pitch;
718 dest += destLock.Pitch;
719 }
720 break;
721 default:
722 UNREACHABLE();
723 }
724 break;
725 case D3DFMT_R5G6B5:
726 switch(getD3DFormat())
727 {
728 case D3DFMT_X8R8G8B8:
729 for(int y = 0; y < height; y++)
730 {
731 for(int x = 0; x < width; x++)
732 {
733 unsigned short rgb = ((unsigned short*)source)[x];
734 unsigned char red = (rgb & 0xF800) >> 8;
735 unsigned char green = (rgb & 0x07E0) >> 3;
736 unsigned char blue = (rgb & 0x001F) << 3;
737 dest[x + 0] = blue | (blue >> 5);
738 dest[x + 1] = green | (green >> 6);
739 dest[x + 2] = red | (red >> 5);
740 dest[x + 3] = 0xFF;
741 }
742
743 source += sourceLock.Pitch;
744 dest += destLock.Pitch;
745 }
746 break;
747 case D3DFMT_L8:
748 for(int y = 0; y < height; y++)
749 {
750 for(int x = 0; x < width; x++)
751 {
752 unsigned char red = source[x * 2 + 1] & 0xF8;
753 dest[x] = red | (red >> 5);
754 }
755
756 source += sourceLock.Pitch;
757 dest += destLock.Pitch;
758 }
759 break;
760 default:
761 UNREACHABLE();
762 }
763 break;
764 case D3DFMT_A1R5G5B5:
765 switch(getD3DFormat())
766 {
767 case D3DFMT_X8R8G8B8:
768 for(int y = 0; y < height; y++)
769 {
770 for(int x = 0; x < width; x++)
771 {
772 unsigned short argb = ((unsigned short*)source)[x];
773 unsigned char red = (argb & 0x7C00) >> 7;
774 unsigned char green = (argb & 0x03E0) >> 2;
775 unsigned char blue = (argb & 0x001F) << 3;
776 dest[x + 0] = blue | (blue >> 5);
777 dest[x + 1] = green | (green >> 5);
778 dest[x + 2] = red | (red >> 5);
779 dest[x + 3] = 0xFF;
780 }
781
782 source += sourceLock.Pitch;
783 dest += destLock.Pitch;
784 }
785 break;
786 case D3DFMT_A8R8G8B8:
787 for(int y = 0; y < height; y++)
788 {
789 for(int x = 0; x < width; x++)
790 {
791 unsigned short argb = ((unsigned short*)source)[x];
792 unsigned char red = (argb & 0x7C00) >> 7;
793 unsigned char green = (argb & 0x03E0) >> 2;
794 unsigned char blue = (argb & 0x001F) << 3;
795 unsigned char alpha = (signed short)argb >> 15;
796 dest[x + 0] = blue | (blue >> 5);
797 dest[x + 1] = green | (green >> 5);
798 dest[x + 2] = red | (red >> 5);
799 dest[x + 3] = alpha;
800 }
801
802 source += sourceLock.Pitch;
803 dest += destLock.Pitch;
804 }
805 break;
806 case D3DFMT_L8:
807 for(int y = 0; y < height; y++)
808 {
809 for(int x = 0; x < width; x++)
810 {
811 unsigned char red = source[x * 2 + 1] & 0x7C;
812 dest[x] = (red << 1) | (red >> 4);
813 }
814
815 source += sourceLock.Pitch;
816 dest += destLock.Pitch;
817 }
818 break;
819 case D3DFMT_A8L8:
820 for(int y = 0; y < height; y++)
821 {
822 for(int x = 0; x < width; x++)
823 {
824 unsigned char red = source[x * 2 + 1] & 0x7C;
825 dest[x * 2 + 0] = (red << 1) | (red >> 4);
826 dest[x * 2 + 1] = (signed char)source[x * 2 + 1] >> 7;
827 }
828
829 source += sourceLock.Pitch;
830 dest += destLock.Pitch;
831 }
832 break;
833 default:
834 UNREACHABLE();
835 }
836 break;
837 default:
838 UNREACHABLE();
839 }
840 }
841
842 unlock();
843 renderTargetData->UnlockRect();
844
845 renderTargetData->Release();
846 surface->Release();
847
848 mDirty = true;
849}
850
851}