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