blob: a5a7f9fafe604802377631780f8e48ebbcce9f8a [file] [log] [blame]
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +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
daniel@transgaming.com31b13e12012-11-28 19:34:30 +00007// Image.cpp: Implements the rx::Image class, which acts as the interface to
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +00008// 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.com3cef5392012-10-31 19:52:15 +000018#include "libGLESv2/Framebuffer.h"
daniel@transgaming.comd186dc72012-11-28 19:40:16 +000019#include "libGLESv2/renderer/RenderTarget9.h"
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +000020
daniel@transgaming.comd8e36562012-10-31 19:52:19 +000021#include "libGLESv2/renderer/renderer9_utils.h"
22
daniel@transgaming.com31b13e12012-11-28 19:34:30 +000023namespace rx
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +000024{
daniel@transgaming.com0ad830b2012-10-31 19:52:12 +000025
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 {
daniel@transgaming.com31b13e12012-11-28 19:34:30 +000071 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.com0ad830b2012-10-31 19:52:12 +000075 }
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
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000204Image::Image()
205{
206 mWidth = 0;
207 mHeight = 0;
208 mInternalFormat = GL_NONE;
209
210 mSurface = NULL;
211
212 mDirty = false;
213
daniel@transgaming.comea32d482012-11-28 19:33:18 +0000214 mRenderer = NULL;
215
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000216 mD3DPool = D3DPOOL_SYSTEMMEM;
217 mD3DFormat = D3DFMT_UNKNOWN;
daniel@transgaming.com20d36662012-10-31 19:51:43 +0000218 mActualFormat = GL_NONE;
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000219}
220
221Image::~Image()
222{
223 if (mSurface)
224 {
225 mSurface->Release();
226 }
227}
228
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +0000229void Image::generateMipmap(Image *dest, Image *source)
daniel@transgaming.com0ad830b2012-10-31 19:52:12 +0000230{
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.comf721fdb2012-12-20 20:53:11 +0000241void Image::copyLockableSurfaces(IDirect3DSurface9 *dest, IDirect3DSurface9 *source)
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000242{
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.comc5c806d2012-12-20 20:52:53 +0000269bool Image::redefine(rx::Renderer *renderer, GLint internalformat, GLsizei width, GLsizei height, bool forceRelease)
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000270{
271 if (mWidth != width ||
272 mHeight != height ||
273 mInternalFormat != internalformat ||
274 forceRelease)
275 {
daniel@transgaming.comc5c806d2012-12-20 20:52:53 +0000276 mRenderer = Renderer9::makeRenderer9(renderer);
277
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000278 mWidth = width;
279 mHeight = height;
280 mInternalFormat = internalformat;
281 // compute the d3d format that will be used
daniel@transgaming.comc5c806d2012-12-20 20:52:53 +0000282 mD3DFormat = mRenderer->ConvertTextureInternalFormat(internalformat);
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000283 mActualFormat = d3d9_gl::GetEquivalentFormat(mD3DFormat);
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000284
285 if (mSurface)
286 {
287 mSurface->Release();
288 mSurface = NULL;
289 }
290
291 return true;
292 }
293
294 return false;
295}
296
297void Image::createSurface()
298{
299 if(mSurface)
300 {
301 return;
302 }
303
304 IDirect3DTexture9 *newTexture = NULL;
305 IDirect3DSurface9 *newSurface = NULL;
306 const D3DPOOL poolToUse = D3DPOOL_SYSTEMMEM;
307 const D3DFORMAT d3dFormat = getD3DFormat();
308 ASSERT(d3dFormat != D3DFMT_INTZ); // We should never get here for depth textures
309
310 if (mWidth != 0 && mHeight != 0)
311 {
312 int levelToFetch = 0;
313 GLsizei requestWidth = mWidth;
314 GLsizei requestHeight = mHeight;
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000315 gl::MakeValidSize(true, gl::IsCompressed(mInternalFormat), &requestWidth, &requestHeight, &levelToFetch);
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000316
daniel@transgaming.comea32d482012-11-28 19:33:18 +0000317 IDirect3DDevice9 *device = mRenderer->getDevice(); // D3D9_REPLACE
318
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000319 HRESULT result = device->CreateTexture(requestWidth, requestHeight, levelToFetch + 1, 0, d3dFormat,
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000320 poolToUse, &newTexture, NULL);
321
322 if (FAILED(result))
323 {
324 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
325 ERR("Creating image surface failed.");
326 return error(GL_OUT_OF_MEMORY);
327 }
328
329 newTexture->GetSurfaceLevel(levelToFetch, &newSurface);
330 newTexture->Release();
331 }
332
333 mSurface = newSurface;
334 mDirty = false;
335 mD3DPool = poolToUse;
336}
337
338HRESULT Image::lock(D3DLOCKED_RECT *lockedRect, const RECT *rect)
339{
340 createSurface();
341
342 HRESULT result = D3DERR_INVALIDCALL;
343
344 if (mSurface)
345 {
346 result = mSurface->LockRect(lockedRect, rect, 0);
347 ASSERT(SUCCEEDED(result));
348
349 mDirty = true;
350 }
351
352 return result;
353}
354
355void Image::unlock()
356{
357 if (mSurface)
358 {
359 HRESULT result = mSurface->UnlockRect();
360 ASSERT(SUCCEEDED(result));
361 }
362}
363
364bool Image::isRenderableFormat() const
365{
daniel@transgaming.comdf14c762012-10-31 19:51:48 +0000366 return TextureStorage::IsTextureFormatRenderable(getD3DFormat());
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000367}
368
daniel@transgaming.com20d36662012-10-31 19:51:43 +0000369GLenum Image::getActualFormat() const
370{
371 return mActualFormat;
372}
373
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000374D3DFORMAT Image::getD3DFormat() const
375{
376 // this should only happen if the image hasn't been redefined first
377 // which would be a bug by the caller
378 ASSERT(mD3DFormat != D3DFMT_UNKNOWN);
379
380 return mD3DFormat;
381}
382
383IDirect3DSurface9 *Image::getSurface()
384{
385 createSurface();
386
387 return mSurface;
388}
389
daniel@transgaming.com0f195ad2012-10-31 19:51:59 +0000390void Image::setManagedSurface(TextureStorage2D *storage, int level)
391{
392 setManagedSurface(storage->getSurfaceLevel(level, false));
393}
394
395void Image::setManagedSurface(TextureStorageCubeMap *storage, int face, int level)
396{
397 setManagedSurface(storage->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, false));
398}
399
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000400void Image::setManagedSurface(IDirect3DSurface9 *surface)
401{
402 D3DSURFACE_DESC desc;
403 surface->GetDesc(&desc);
404 ASSERT(desc.Pool == D3DPOOL_MANAGED);
405
406 if ((GLsizei)desc.Width == mWidth && (GLsizei)desc.Height == mHeight)
407 {
408 if (mSurface)
409 {
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +0000410 copyLockableSurfaces(surface, mSurface);
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000411 mSurface->Release();
412 }
413
414 mSurface = surface;
415 mD3DPool = desc.Pool;
416 }
417}
418
daniel@transgaming.com0f195ad2012-10-31 19:51:59 +0000419bool Image::updateSurface(TextureStorage2D *storage, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000420{
daniel@transgaming.com0f195ad2012-10-31 19:51:59 +0000421 return updateSurface(storage->getSurfaceLevel(level, true), xoffset, yoffset, width, height);
422}
423
424bool Image::updateSurface(TextureStorageCubeMap *storage, int face, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
425{
426 return updateSurface(storage->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, true), xoffset, yoffset, width, height);
427}
428
429bool Image::updateSurface(IDirect3DSurface9 *destSurface, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
430{
431 if (!destSurface)
432 return false;
433
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000434 IDirect3DSurface9 *sourceSurface = getSurface();
435
436 if (sourceSurface && sourceSurface != destSurface)
437 {
438 RECT rect;
439 rect.left = xoffset;
440 rect.top = yoffset;
441 rect.right = xoffset + width;
442 rect.bottom = yoffset + height;
443
444 POINT point = {rect.left, rect.top};
daniel@transgaming.comea32d482012-11-28 19:33:18 +0000445
446 IDirect3DDevice9 *device = mRenderer->getDevice(); // D3D9_REPLACE
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000447
448 if (mD3DPool == D3DPOOL_MANAGED)
449 {
450 D3DSURFACE_DESC desc;
451 sourceSurface->GetDesc(&desc);
452
453 IDirect3DSurface9 *surf = 0;
454 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
455
456 if (SUCCEEDED(result))
457 {
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +0000458 copyLockableSurfaces(surf, sourceSurface);
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000459 result = device->UpdateSurface(surf, &rect, destSurface, &point);
460 ASSERT(SUCCEEDED(result));
461 surf->Release();
462 }
463 }
464 else
465 {
466 // UpdateSurface: source must be SYSTEMMEM, dest must be DEFAULT pools
467 HRESULT result = device->UpdateSurface(sourceSurface, &rect, destSurface, &point);
468 ASSERT(SUCCEEDED(result));
469 }
470 }
daniel@transgaming.com0f195ad2012-10-31 19:51:59 +0000471
472 destSurface->Release();
473 return true;
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000474}
475
476// Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input
477// into the target pixel rectangle.
478void Image::loadData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
479 GLint unpackAlignment, const void *input)
480{
481 RECT lockRect =
482 {
483 xoffset, yoffset,
484 xoffset + width, yoffset + height
485 };
486
487 D3DLOCKED_RECT locked;
488 HRESULT result = lock(&locked, &lockRect);
489 if (FAILED(result))
490 {
491 return;
492 }
493
494
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000495 GLsizei inputPitch = gl::ComputePitch(width, mInternalFormat, unpackAlignment);
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000496
497 switch (mInternalFormat)
498 {
499 case GL_ALPHA8_EXT:
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000500 if (gl::supportsSSE2())
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000501 {
502 loadAlphaDataSSE2(width, height, inputPitch, input, locked.Pitch, locked.pBits);
503 }
504 else
505 {
506 loadAlphaData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
507 }
508 break;
509 case GL_LUMINANCE8_EXT:
510 loadLuminanceData(width, height, inputPitch, input, locked.Pitch, locked.pBits, getD3DFormat() == D3DFMT_L8);
511 break;
512 case GL_ALPHA32F_EXT:
513 loadAlphaFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
514 break;
515 case GL_LUMINANCE32F_EXT:
516 loadLuminanceFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
517 break;
518 case GL_ALPHA16F_EXT:
519 loadAlphaHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
520 break;
521 case GL_LUMINANCE16F_EXT:
522 loadLuminanceHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
523 break;
524 case GL_LUMINANCE8_ALPHA8_EXT:
525 loadLuminanceAlphaData(width, height, inputPitch, input, locked.Pitch, locked.pBits, getD3DFormat() == D3DFMT_A8L8);
526 break;
527 case GL_LUMINANCE_ALPHA32F_EXT:
528 loadLuminanceAlphaFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
529 break;
530 case GL_LUMINANCE_ALPHA16F_EXT:
531 loadLuminanceAlphaHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
532 break;
533 case GL_RGB8_OES:
534 loadRGBUByteData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
535 break;
536 case GL_RGB565:
537 loadRGB565Data(width, height, inputPitch, input, locked.Pitch, locked.pBits);
538 break;
539 case GL_RGBA8_OES:
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000540 if (gl::supportsSSE2())
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000541 {
542 loadRGBAUByteDataSSE2(width, height, inputPitch, input, locked.Pitch, locked.pBits);
543 }
544 else
545 {
546 loadRGBAUByteData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
547 }
548 break;
549 case GL_RGBA4:
550 loadRGBA4444Data(width, height, inputPitch, input, locked.Pitch, locked.pBits);
551 break;
552 case GL_RGB5_A1:
553 loadRGBA5551Data(width, height, inputPitch, input, locked.Pitch, locked.pBits);
554 break;
555 case GL_BGRA8_EXT:
556 loadBGRAData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
557 break;
558 // float textures are converted to RGBA, not BGRA, as they're stored that way in D3D
559 case GL_RGB32F_EXT:
560 loadRGBFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
561 break;
562 case GL_RGB16F_EXT:
563 loadRGBHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
564 break;
565 case GL_RGBA32F_EXT:
566 loadRGBAFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
567 break;
568 case GL_RGBA16F_EXT:
569 loadRGBAHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
570 break;
571 default: UNREACHABLE();
572 }
573
574 unlock();
575}
576
577void Image::loadAlphaData(GLsizei width, GLsizei height,
578 int inputPitch, const void *input, size_t outputPitch, void *output) const
579{
580 const unsigned char *source = NULL;
581 unsigned char *dest = NULL;
582
583 for (int y = 0; y < height; y++)
584 {
585 source = static_cast<const unsigned char*>(input) + y * inputPitch;
586 dest = static_cast<unsigned char*>(output) + y * outputPitch;
587 for (int x = 0; x < width; x++)
588 {
589 dest[4 * x + 0] = 0;
590 dest[4 * x + 1] = 0;
591 dest[4 * x + 2] = 0;
592 dest[4 * x + 3] = source[x];
593 }
594 }
595}
596
597void Image::loadAlphaFloatData(GLsizei width, GLsizei height,
598 int inputPitch, const void *input, size_t outputPitch, void *output) const
599{
600 const float *source = NULL;
601 float *dest = NULL;
602
603 for (int y = 0; y < height; y++)
604 {
605 source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
606 dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputPitch);
607 for (int x = 0; x < width; x++)
608 {
609 dest[4 * x + 0] = 0;
610 dest[4 * x + 1] = 0;
611 dest[4 * x + 2] = 0;
612 dest[4 * x + 3] = source[x];
613 }
614 }
615}
616
617void Image::loadAlphaHalfFloatData(GLsizei width, GLsizei height,
618 int inputPitch, const void *input, size_t outputPitch, void *output) const
619{
620 const unsigned short *source = NULL;
621 unsigned short *dest = NULL;
622
623 for (int y = 0; y < height; y++)
624 {
625 source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
626 dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + y * outputPitch);
627 for (int x = 0; x < width; x++)
628 {
629 dest[4 * x + 0] = 0;
630 dest[4 * x + 1] = 0;
631 dest[4 * x + 2] = 0;
632 dest[4 * x + 3] = source[x];
633 }
634 }
635}
636
637void Image::loadLuminanceData(GLsizei width, GLsizei height,
638 int inputPitch, const void *input, size_t outputPitch, void *output, bool native) const
639{
640 const unsigned char *source = NULL;
641 unsigned char *dest = NULL;
642
643 for (int y = 0; y < height; y++)
644 {
645 source = static_cast<const unsigned char*>(input) + y * inputPitch;
646 dest = static_cast<unsigned char*>(output) + y * outputPitch;
647
648 if (!native) // BGRA8 destination format
649 {
650 for (int x = 0; x < width; x++)
651 {
652 dest[4 * x + 0] = source[x];
653 dest[4 * x + 1] = source[x];
654 dest[4 * x + 2] = source[x];
655 dest[4 * x + 3] = 0xFF;
656 }
657 }
658 else // L8 destination format
659 {
660 memcpy(dest, source, width);
661 }
662 }
663}
664
665void Image::loadLuminanceFloatData(GLsizei width, GLsizei height,
666 int inputPitch, const void *input, size_t outputPitch, void *output) const
667{
668 const float *source = NULL;
669 float *dest = NULL;
670
671 for (int y = 0; y < height; y++)
672 {
673 source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
674 dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputPitch);
675 for (int x = 0; x < width; x++)
676 {
677 dest[4 * x + 0] = source[x];
678 dest[4 * x + 1] = source[x];
679 dest[4 * x + 2] = source[x];
680 dest[4 * x + 3] = 1.0f;
681 }
682 }
683}
684
685void Image::loadLuminanceHalfFloatData(GLsizei width, GLsizei height,
686 int inputPitch, const void *input, size_t outputPitch, void *output) const
687{
688 const unsigned short *source = NULL;
689 unsigned short *dest = NULL;
690
691 for (int y = 0; y < height; y++)
692 {
693 source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
694 dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + y * outputPitch);
695 for (int x = 0; x < width; x++)
696 {
697 dest[4 * x + 0] = source[x];
698 dest[4 * x + 1] = source[x];
699 dest[4 * x + 2] = source[x];
700 dest[4 * x + 3] = 0x3C00; // SEEEEEMMMMMMMMMM, S = 0, E = 15, M = 0: 16bit flpt representation of 1
701 }
702 }
703}
704
705void Image::loadLuminanceAlphaData(GLsizei width, GLsizei height,
706 int inputPitch, const void *input, size_t outputPitch, void *output, bool native) const
707{
708 const unsigned char *source = NULL;
709 unsigned char *dest = NULL;
710
711 for (int y = 0; y < height; y++)
712 {
713 source = static_cast<const unsigned char*>(input) + y * inputPitch;
714 dest = static_cast<unsigned char*>(output) + y * outputPitch;
715
716 if (!native) // BGRA8 destination format
717 {
718 for (int x = 0; x < width; x++)
719 {
720 dest[4 * x + 0] = source[2*x+0];
721 dest[4 * x + 1] = source[2*x+0];
722 dest[4 * x + 2] = source[2*x+0];
723 dest[4 * x + 3] = source[2*x+1];
724 }
725 }
726 else
727 {
728 memcpy(dest, source, width * 2);
729 }
730 }
731}
732
733void Image::loadLuminanceAlphaFloatData(GLsizei width, GLsizei height,
734 int inputPitch, const void *input, size_t outputPitch, void *output) const
735{
736 const float *source = NULL;
737 float *dest = NULL;
738
739 for (int y = 0; y < height; y++)
740 {
741 source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
742 dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputPitch);
743 for (int x = 0; x < width; x++)
744 {
745 dest[4 * x + 0] = source[2*x+0];
746 dest[4 * x + 1] = source[2*x+0];
747 dest[4 * x + 2] = source[2*x+0];
748 dest[4 * x + 3] = source[2*x+1];
749 }
750 }
751}
752
753void Image::loadLuminanceAlphaHalfFloatData(GLsizei width, GLsizei height,
754 int inputPitch, const void *input, size_t outputPitch, void *output) const
755{
756 const unsigned short *source = NULL;
757 unsigned short *dest = NULL;
758
759 for (int y = 0; y < height; y++)
760 {
761 source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
762 dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + y * outputPitch);
763 for (int x = 0; x < width; x++)
764 {
765 dest[4 * x + 0] = source[2*x+0];
766 dest[4 * x + 1] = source[2*x+0];
767 dest[4 * x + 2] = source[2*x+0];
768 dest[4 * x + 3] = source[2*x+1];
769 }
770 }
771}
772
773void Image::loadRGBUByteData(GLsizei width, GLsizei height,
774 int inputPitch, const void *input, size_t outputPitch, void *output) const
775{
776 const unsigned char *source = NULL;
777 unsigned char *dest = NULL;
778
779 for (int y = 0; y < height; y++)
780 {
781 source = static_cast<const unsigned char*>(input) + y * inputPitch;
782 dest = static_cast<unsigned char*>(output) + y * outputPitch;
783 for (int x = 0; x < width; x++)
784 {
785 dest[4 * x + 0] = source[x * 3 + 2];
786 dest[4 * x + 1] = source[x * 3 + 1];
787 dest[4 * x + 2] = source[x * 3 + 0];
788 dest[4 * x + 3] = 0xFF;
789 }
790 }
791}
792
793void Image::loadRGB565Data(GLsizei width, GLsizei height,
794 int inputPitch, const void *input, size_t outputPitch, void *output) const
795{
796 const unsigned short *source = NULL;
797 unsigned char *dest = NULL;
798
799 for (int y = 0; y < height; y++)
800 {
801 source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
802 dest = static_cast<unsigned char*>(output) + y * outputPitch;
803 for (int x = 0; x < width; x++)
804 {
805 unsigned short rgba = source[x];
806 dest[4 * x + 0] = ((rgba & 0x001F) << 3) | ((rgba & 0x001F) >> 2);
807 dest[4 * x + 1] = ((rgba & 0x07E0) >> 3) | ((rgba & 0x07E0) >> 9);
808 dest[4 * x + 2] = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13);
809 dest[4 * x + 3] = 0xFF;
810 }
811 }
812}
813
814void Image::loadRGBFloatData(GLsizei width, GLsizei height,
815 int inputPitch, const void *input, size_t outputPitch, void *output) const
816{
817 const float *source = NULL;
818 float *dest = NULL;
819
820 for (int y = 0; y < height; y++)
821 {
822 source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
823 dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputPitch);
824 for (int x = 0; x < width; x++)
825 {
826 dest[4 * x + 0] = source[x * 3 + 0];
827 dest[4 * x + 1] = source[x * 3 + 1];
828 dest[4 * x + 2] = source[x * 3 + 2];
829 dest[4 * x + 3] = 1.0f;
830 }
831 }
832}
833
834void Image::loadRGBHalfFloatData(GLsizei width, GLsizei height,
835 int inputPitch, const void *input, size_t outputPitch, void *output) const
836{
837 const unsigned short *source = NULL;
838 unsigned short *dest = NULL;
839
840 for (int y = 0; y < height; y++)
841 {
842 source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
843 dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(output) + y * outputPitch);
844 for (int x = 0; x < width; x++)
845 {
846 dest[4 * x + 0] = source[x * 3 + 0];
847 dest[4 * x + 1] = source[x * 3 + 1];
848 dest[4 * x + 2] = source[x * 3 + 2];
849 dest[4 * x + 3] = 0x3C00; // SEEEEEMMMMMMMMMM, S = 0, E = 15, M = 0: 16bit flpt representation of 1
850 }
851 }
852}
853
854void Image::loadRGBAUByteData(GLsizei width, GLsizei height,
855 int inputPitch, const void *input, size_t outputPitch, void *output) const
856{
857 const unsigned int *source = NULL;
858 unsigned int *dest = NULL;
859 for (int y = 0; y < height; y++)
860 {
861 source = reinterpret_cast<const unsigned int*>(static_cast<const unsigned char*>(input) + y * inputPitch);
862 dest = reinterpret_cast<unsigned int*>(static_cast<unsigned char*>(output) + y * outputPitch);
863
864 for (int x = 0; x < width; x++)
865 {
866 unsigned int rgba = source[x];
867 dest[x] = (_rotl(rgba, 16) & 0x00ff00ff) | (rgba & 0xff00ff00);
868 }
869 }
870}
871
872void Image::loadRGBA4444Data(GLsizei width, GLsizei height,
873 int inputPitch, const void *input, size_t outputPitch, void *output) const
874{
875 const unsigned short *source = NULL;
876 unsigned char *dest = NULL;
877
878 for (int y = 0; y < height; y++)
879 {
880 source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
881 dest = static_cast<unsigned char*>(output) + y * outputPitch;
882 for (int x = 0; x < width; x++)
883 {
884 unsigned short rgba = source[x];
885 dest[4 * x + 0] = ((rgba & 0x00F0) << 0) | ((rgba & 0x00F0) >> 4);
886 dest[4 * x + 1] = ((rgba & 0x0F00) >> 4) | ((rgba & 0x0F00) >> 8);
887 dest[4 * x + 2] = ((rgba & 0xF000) >> 8) | ((rgba & 0xF000) >> 12);
888 dest[4 * x + 3] = ((rgba & 0x000F) << 4) | ((rgba & 0x000F) >> 0);
889 }
890 }
891}
892
893void Image::loadRGBA5551Data(GLsizei width, GLsizei height,
894 int inputPitch, const void *input, size_t outputPitch, void *output) const
895{
896 const unsigned short *source = NULL;
897 unsigned char *dest = NULL;
898
899 for (int y = 0; y < height; y++)
900 {
901 source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
902 dest = static_cast<unsigned char*>(output) + y * outputPitch;
903 for (int x = 0; x < width; x++)
904 {
905 unsigned short rgba = source[x];
906 dest[4 * x + 0] = ((rgba & 0x003E) << 2) | ((rgba & 0x003E) >> 3);
907 dest[4 * x + 1] = ((rgba & 0x07C0) >> 3) | ((rgba & 0x07C0) >> 8);
908 dest[4 * x + 2] = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13);
909 dest[4 * x + 3] = (rgba & 0x0001) ? 0xFF : 0;
910 }
911 }
912}
913
914void Image::loadRGBAFloatData(GLsizei width, GLsizei height,
915 int inputPitch, const void *input, size_t outputPitch, void *output) const
916{
917 const float *source = NULL;
918 float *dest = NULL;
919
920 for (int y = 0; y < height; y++)
921 {
922 source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
923 dest = reinterpret_cast<float*>(static_cast<unsigned char*>(output) + y * outputPitch);
924 memcpy(dest, source, width * 16);
925 }
926}
927
928void Image::loadRGBAHalfFloatData(GLsizei width, GLsizei height,
929 int inputPitch, const void *input, size_t outputPitch, void *output) const
930{
931 const unsigned char *source = NULL;
932 unsigned char *dest = NULL;
933
934 for (int y = 0; y < height; y++)
935 {
936 source = static_cast<const unsigned char*>(input) + y * inputPitch;
937 dest = static_cast<unsigned char*>(output) + y * outputPitch;
938 memcpy(dest, source, width * 8);
939 }
940}
941
942void Image::loadBGRAData(GLsizei width, GLsizei height,
943 int inputPitch, const void *input, size_t outputPitch, void *output) const
944{
945 const unsigned char *source = NULL;
946 unsigned char *dest = NULL;
947
948 for (int y = 0; y < height; y++)
949 {
950 source = static_cast<const unsigned char*>(input) + y * inputPitch;
951 dest = static_cast<unsigned char*>(output) + y * outputPitch;
952 memcpy(dest, source, width*4);
953 }
954}
955
956void Image::loadCompressedData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
957 const void *input) {
958 ASSERT(xoffset % 4 == 0);
959 ASSERT(yoffset % 4 == 0);
960
961 RECT lockRect = {
962 xoffset, yoffset,
963 xoffset + width, yoffset + height
964 };
965
966 D3DLOCKED_RECT locked;
967 HRESULT result = lock(&locked, &lockRect);
968 if (FAILED(result))
969 {
970 return;
971 }
972
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000973 GLsizei inputSize = gl::ComputeCompressedSize(width, height, mInternalFormat);
974 GLsizei inputPitch = gl::ComputeCompressedPitch(width, mInternalFormat);
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000975 int rows = inputSize / inputPitch;
976 for (int i = 0; i < rows; ++i)
977 {
978 memcpy((void*)((BYTE*)locked.pBits + i * locked.Pitch), (void*)((BYTE*)input + i * inputPitch), inputPitch);
979 }
980
981 unlock();
982}
983
984// This implements glCopyTex[Sub]Image2D for non-renderable internal texture formats and incomplete textures
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000985void Image::copy(GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, gl::Framebuffer *source)
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +0000986{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +0000987 RenderTarget9 *renderTarget = NULL;
988 IDirect3DSurface9 *surface = NULL;
989 gl::Renderbuffer *colorbuffer = source->getColorbuffer();
daniel@transgaming.com3cef5392012-10-31 19:52:15 +0000990
daniel@transgaming.comd186dc72012-11-28 19:40:16 +0000991 if (colorbuffer)
992 {
993 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
994 }
995
996 if (renderTarget)
997 {
998 surface = renderTarget->getSurface();
999 }
1000
1001 if (!surface)
daniel@transgaming.com3cef5392012-10-31 19:52:15 +00001002 {
1003 ERR("Failed to retrieve the render target.");
1004 return error(GL_OUT_OF_MEMORY);
1005 }
1006
daniel@transgaming.comea32d482012-11-28 19:33:18 +00001007 IDirect3DDevice9 *device = mRenderer->getDevice(); // D3D9_REPLACE
1008
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +00001009 IDirect3DSurface9 *renderTargetData = NULL;
1010 D3DSURFACE_DESC description;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001011 surface->GetDesc(&description);
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +00001012
1013 HRESULT result = device->CreateOffscreenPlainSurface(description.Width, description.Height, description.Format, D3DPOOL_SYSTEMMEM, &renderTargetData, NULL);
1014
1015 if (FAILED(result))
1016 {
1017 ERR("Could not create matching destination surface.");
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001018 surface->Release();
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +00001019 return error(GL_OUT_OF_MEMORY);
1020 }
1021
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001022 result = device->GetRenderTargetData(surface, renderTargetData);
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +00001023
1024 if (FAILED(result))
1025 {
1026 ERR("GetRenderTargetData unexpectedly failed.");
1027 renderTargetData->Release();
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001028 surface->Release();
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +00001029 return error(GL_OUT_OF_MEMORY);
1030 }
1031
1032 RECT sourceRect = {x, y, x + width, y + height};
1033 RECT destRect = {xoffset, yoffset, xoffset + width, yoffset + height};
1034
1035 D3DLOCKED_RECT sourceLock = {0};
1036 result = renderTargetData->LockRect(&sourceLock, &sourceRect, 0);
1037
1038 if (FAILED(result))
1039 {
1040 ERR("Failed to lock the source surface (rectangle might be invalid).");
1041 renderTargetData->Release();
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001042 surface->Release();
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +00001043 return error(GL_OUT_OF_MEMORY);
1044 }
1045
1046 D3DLOCKED_RECT destLock = {0};
1047 result = lock(&destLock, &destRect);
1048
1049 if (FAILED(result))
1050 {
1051 ERR("Failed to lock the destination surface (rectangle might be invalid).");
1052 renderTargetData->UnlockRect();
1053 renderTargetData->Release();
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001054 surface->Release();
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +00001055 return error(GL_OUT_OF_MEMORY);
1056 }
1057
1058 if (destLock.pBits && sourceLock.pBits)
1059 {
1060 unsigned char *source = (unsigned char*)sourceLock.pBits;
1061 unsigned char *dest = (unsigned char*)destLock.pBits;
1062
1063 switch (description.Format)
1064 {
1065 case D3DFMT_X8R8G8B8:
1066 case D3DFMT_A8R8G8B8:
1067 switch(getD3DFormat())
1068 {
1069 case D3DFMT_X8R8G8B8:
1070 case D3DFMT_A8R8G8B8:
1071 for(int y = 0; y < height; y++)
1072 {
1073 memcpy(dest, source, 4 * width);
1074
1075 source += sourceLock.Pitch;
1076 dest += destLock.Pitch;
1077 }
1078 break;
1079 case D3DFMT_L8:
1080 for(int y = 0; y < height; y++)
1081 {
1082 for(int x = 0; x < width; x++)
1083 {
1084 dest[x] = source[x * 4 + 2];
1085 }
1086
1087 source += sourceLock.Pitch;
1088 dest += destLock.Pitch;
1089 }
1090 break;
1091 case D3DFMT_A8L8:
1092 for(int y = 0; y < height; y++)
1093 {
1094 for(int x = 0; x < width; x++)
1095 {
1096 dest[x * 2 + 0] = source[x * 4 + 2];
1097 dest[x * 2 + 1] = source[x * 4 + 3];
1098 }
1099
1100 source += sourceLock.Pitch;
1101 dest += destLock.Pitch;
1102 }
1103 break;
1104 default:
1105 UNREACHABLE();
1106 }
1107 break;
1108 case D3DFMT_R5G6B5:
1109 switch(getD3DFormat())
1110 {
1111 case D3DFMT_X8R8G8B8:
1112 for(int y = 0; y < height; y++)
1113 {
1114 for(int x = 0; x < width; x++)
1115 {
1116 unsigned short rgb = ((unsigned short*)source)[x];
1117 unsigned char red = (rgb & 0xF800) >> 8;
1118 unsigned char green = (rgb & 0x07E0) >> 3;
1119 unsigned char blue = (rgb & 0x001F) << 3;
1120 dest[x + 0] = blue | (blue >> 5);
1121 dest[x + 1] = green | (green >> 6);
1122 dest[x + 2] = red | (red >> 5);
1123 dest[x + 3] = 0xFF;
1124 }
1125
1126 source += sourceLock.Pitch;
1127 dest += destLock.Pitch;
1128 }
1129 break;
1130 case D3DFMT_L8:
1131 for(int y = 0; y < height; y++)
1132 {
1133 for(int x = 0; x < width; x++)
1134 {
1135 unsigned char red = source[x * 2 + 1] & 0xF8;
1136 dest[x] = red | (red >> 5);
1137 }
1138
1139 source += sourceLock.Pitch;
1140 dest += destLock.Pitch;
1141 }
1142 break;
1143 default:
1144 UNREACHABLE();
1145 }
1146 break;
1147 case D3DFMT_A1R5G5B5:
1148 switch(getD3DFormat())
1149 {
1150 case D3DFMT_X8R8G8B8:
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 dest[x + 0] = blue | (blue >> 5);
1160 dest[x + 1] = green | (green >> 5);
1161 dest[x + 2] = red | (red >> 5);
1162 dest[x + 3] = 0xFF;
1163 }
1164
1165 source += sourceLock.Pitch;
1166 dest += destLock.Pitch;
1167 }
1168 break;
1169 case D3DFMT_A8R8G8B8:
1170 for(int y = 0; y < height; y++)
1171 {
1172 for(int x = 0; x < width; x++)
1173 {
1174 unsigned short argb = ((unsigned short*)source)[x];
1175 unsigned char red = (argb & 0x7C00) >> 7;
1176 unsigned char green = (argb & 0x03E0) >> 2;
1177 unsigned char blue = (argb & 0x001F) << 3;
1178 unsigned char alpha = (signed short)argb >> 15;
1179 dest[x + 0] = blue | (blue >> 5);
1180 dest[x + 1] = green | (green >> 5);
1181 dest[x + 2] = red | (red >> 5);
1182 dest[x + 3] = alpha;
1183 }
1184
1185 source += sourceLock.Pitch;
1186 dest += destLock.Pitch;
1187 }
1188 break;
1189 case D3DFMT_L8:
1190 for(int y = 0; y < height; y++)
1191 {
1192 for(int x = 0; x < width; x++)
1193 {
1194 unsigned char red = source[x * 2 + 1] & 0x7C;
1195 dest[x] = (red << 1) | (red >> 4);
1196 }
1197
1198 source += sourceLock.Pitch;
1199 dest += destLock.Pitch;
1200 }
1201 break;
1202 case D3DFMT_A8L8:
1203 for(int y = 0; y < height; y++)
1204 {
1205 for(int x = 0; x < width; x++)
1206 {
1207 unsigned char red = source[x * 2 + 1] & 0x7C;
1208 dest[x * 2 + 0] = (red << 1) | (red >> 4);
1209 dest[x * 2 + 1] = (signed char)source[x * 2 + 1] >> 7;
1210 }
1211
1212 source += sourceLock.Pitch;
1213 dest += destLock.Pitch;
1214 }
1215 break;
1216 default:
1217 UNREACHABLE();
1218 }
1219 break;
1220 default:
1221 UNREACHABLE();
1222 }
1223 }
1224
1225 unlock();
1226 renderTargetData->UnlockRect();
1227
1228 renderTargetData->Release();
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00001229 surface->Release();
daniel@transgaming.comb9d7e6f2012-10-31 19:08:32 +00001230
1231 mDirty = true;
1232}
1233
1234}