1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
|
//
// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Image9.cpp: Implements the rx::Image9 class, which acts as the interface to
// the actual underlying surfaces of a Texture.
#include "libANGLE/renderer/d3d/d3d9/Image9.h"
#include "libANGLE/renderer/d3d/d3d9/renderer9_utils.h"
#include "libANGLE/renderer/d3d/d3d9/formatutils9.h"
#include "libANGLE/renderer/d3d/d3d9/Renderer9.h"
#include "libANGLE/renderer/d3d/d3d9/RenderTarget9.h"
#include "libANGLE/renderer/d3d/d3d9/TextureStorage9.h"
#include "libANGLE/formatutils.h"
#include "libANGLE/Framebuffer.h"
#include "libANGLE/FramebufferAttachment.h"
#include "libANGLE/Renderbuffer.h"
#include "common/utilities.h"
namespace rx
{
Image9::Image9(Renderer9 *renderer)
{
mSurface = NULL;
mRenderer = NULL;
mD3DPool = D3DPOOL_SYSTEMMEM;
mD3DFormat = D3DFMT_UNKNOWN;
mRenderer = renderer;
}
Image9::~Image9()
{
SafeRelease(mSurface);
}
gl::Error Image9::generateMip(IDirect3DSurface9 *destSurface, IDirect3DSurface9 *sourceSurface)
{
D3DSURFACE_DESC destDesc;
HRESULT result = destSurface->GetDesc(&destDesc);
ASSERT(SUCCEEDED(result));
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY, "Failed to query the source surface description for mipmap generation, result: 0x%X.", result);
}
D3DSURFACE_DESC sourceDesc;
result = sourceSurface->GetDesc(&sourceDesc);
ASSERT(SUCCEEDED(result));
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY, "Failed to query the destination surface description for mipmap generation, result: 0x%X.", result);
}
ASSERT(sourceDesc.Format == destDesc.Format);
ASSERT(sourceDesc.Width == 1 || sourceDesc.Width / 2 == destDesc.Width);
ASSERT(sourceDesc.Height == 1 || sourceDesc.Height / 2 == destDesc.Height);
const d3d9::D3DFormat &d3dFormatInfo = d3d9::GetD3DFormatInfo(sourceDesc.Format);
ASSERT(d3dFormatInfo.info().mipGenerationFunction != NULL);
D3DLOCKED_RECT sourceLocked = {0};
result = sourceSurface->LockRect(&sourceLocked, NULL, D3DLOCK_READONLY);
ASSERT(SUCCEEDED(result));
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY, "Failed to lock the source surface for mipmap generation, result: 0x%X.", result);
}
D3DLOCKED_RECT destLocked = {0};
result = destSurface->LockRect(&destLocked, NULL, 0);
ASSERT(SUCCEEDED(result));
if (FAILED(result))
{
sourceSurface->UnlockRect();
return gl::Error(GL_OUT_OF_MEMORY, "Failed to lock the destination surface for mipmap generation, result: 0x%X.", result);
}
const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(sourceLocked.pBits);
uint8_t *destData = reinterpret_cast<uint8_t*>(destLocked.pBits);
ASSERT(sourceData && destData);
d3dFormatInfo.info().mipGenerationFunction(sourceDesc.Width, sourceDesc.Height, 1, sourceData,
sourceLocked.Pitch, 0, destData, destLocked.Pitch,
0);
destSurface->UnlockRect();
sourceSurface->UnlockRect();
return gl::Error(GL_NO_ERROR);
}
gl::Error Image9::generateMipmap(Image9 *dest, Image9 *source)
{
IDirect3DSurface9 *sourceSurface = NULL;
gl::Error error = source->getSurface(&sourceSurface);
if (error.isError())
{
return error;
}
IDirect3DSurface9 *destSurface = NULL;
error = dest->getSurface(&destSurface);
if (error.isError())
{
return error;
}
error = generateMip(destSurface, sourceSurface);
if (error.isError())
{
return error;
}
dest->markDirty();
return gl::Error(GL_NO_ERROR);
}
gl::Error Image9::copyLockableSurfaces(IDirect3DSurface9 *dest, IDirect3DSurface9 *source)
{
D3DLOCKED_RECT sourceLock = {0};
D3DLOCKED_RECT destLock = {0};
HRESULT result;
result = source->LockRect(&sourceLock, NULL, 0);
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY, "Failed to lock source surface for copy, result: 0x%X.", result);
}
result = dest->LockRect(&destLock, NULL, 0);
if (FAILED(result))
{
source->UnlockRect();
return gl::Error(GL_OUT_OF_MEMORY, "Failed to lock source surface for copy, result: 0x%X.", result);
}
ASSERT(sourceLock.pBits && destLock.pBits);
D3DSURFACE_DESC desc;
source->GetDesc(&desc);
const d3d9::D3DFormat &d3dFormatInfo = d3d9::GetD3DFormatInfo(desc.Format);
unsigned int rows = desc.Height / d3dFormatInfo.blockHeight;
unsigned int bytes = d3d9::ComputeBlockSize(desc.Format, desc.Width, d3dFormatInfo.blockHeight);
ASSERT(bytes <= static_cast<unsigned int>(sourceLock.Pitch) &&
bytes <= static_cast<unsigned int>(destLock.Pitch));
for(unsigned int i = 0; i < rows; i++)
{
memcpy((char*)destLock.pBits + destLock.Pitch * i, (char*)sourceLock.pBits + sourceLock.Pitch * i, bytes);
}
source->UnlockRect();
dest->UnlockRect();
return gl::Error(GL_NO_ERROR);
}
bool Image9::redefine(GLenum target, GLenum internalformat, const gl::Extents &size, bool forceRelease)
{
// 3D textures are not supported by the D3D9 backend.
ASSERT(size.depth <= 1);
// Only 2D and cube texture are supported by the D3D9 backend.
ASSERT(target == GL_TEXTURE_2D || target == GL_TEXTURE_CUBE_MAP);
if (mWidth != size.width ||
mHeight != size.height ||
mDepth != size.depth ||
mInternalFormat != internalformat ||
forceRelease)
{
mWidth = size.width;
mHeight = size.height;
mDepth = size.depth;
mInternalFormat = internalformat;
// compute the d3d format that will be used
const d3d9::TextureFormat &d3d9FormatInfo = d3d9::GetTextureFormatInfo(internalformat);
mD3DFormat = d3d9FormatInfo.texFormat;
mRenderable = (d3d9FormatInfo.renderFormat != D3DFMT_UNKNOWN);
SafeRelease(mSurface);
mDirty = (d3d9FormatInfo.dataInitializerFunction != NULL);
return true;
}
return false;
}
gl::Error Image9::createSurface()
{
if (mSurface)
{
return gl::Error(GL_NO_ERROR);
}
IDirect3DTexture9 *newTexture = NULL;
IDirect3DSurface9 *newSurface = NULL;
const D3DPOOL poolToUse = D3DPOOL_SYSTEMMEM;
const D3DFORMAT d3dFormat = getD3DFormat();
if (mWidth != 0 && mHeight != 0)
{
int levelToFetch = 0;
GLsizei requestWidth = mWidth;
GLsizei requestHeight = mHeight;
d3d9::MakeValidSize(true, d3dFormat, &requestWidth, &requestHeight, &levelToFetch);
IDirect3DDevice9 *device = mRenderer->getDevice();
HRESULT result = device->CreateTexture(requestWidth, requestHeight, levelToFetch + 1, 0, d3dFormat,
poolToUse, &newTexture, NULL);
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
return gl::Error(GL_OUT_OF_MEMORY, "Failed to create image surface, result: 0x%X.", result);
}
newTexture->GetSurfaceLevel(levelToFetch, &newSurface);
SafeRelease(newTexture);
const d3d9::TextureFormat &d3dFormatInfo = d3d9::GetTextureFormatInfo(mInternalFormat);
if (d3dFormatInfo.dataInitializerFunction != NULL)
{
RECT entireRect;
entireRect.left = 0;
entireRect.right = mWidth;
entireRect.top = 0;
entireRect.bottom = mHeight;
D3DLOCKED_RECT lockedRect;
result = newSurface->LockRect(&lockedRect, &entireRect, 0);
ASSERT(SUCCEEDED(result));
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY, "Failed to lock image surface, result: 0x%X.", result);
}
d3dFormatInfo.dataInitializerFunction(mWidth, mHeight, 1, reinterpret_cast<uint8_t*>(lockedRect.pBits),
lockedRect.Pitch, 0);
result = newSurface->UnlockRect();
ASSERT(SUCCEEDED(result));
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY, "Failed to unlock image surface, result: 0x%X.", result);
}
}
}
mSurface = newSurface;
mDirty = false;
mD3DPool = poolToUse;
return gl::Error(GL_NO_ERROR);
}
gl::Error Image9::lock(D3DLOCKED_RECT *lockedRect, const RECT &rect)
{
gl::Error error = createSurface();
if (error.isError())
{
return error;
}
if (mSurface)
{
HRESULT result = mSurface->LockRect(lockedRect, &rect, 0);
ASSERT(SUCCEEDED(result));
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY, "Failed to lock image surface, result: 0x%X.", result);
}
mDirty = true;
}
return gl::Error(GL_NO_ERROR);
}
void Image9::unlock()
{
if (mSurface)
{
HRESULT result = mSurface->UnlockRect();
ASSERT(SUCCEEDED(result));
}
}
D3DFORMAT Image9::getD3DFormat() const
{
// this should only happen if the image hasn't been redefined first
// which would be a bug by the caller
ASSERT(mD3DFormat != D3DFMT_UNKNOWN);
return mD3DFormat;
}
bool Image9::isDirty() const
{
// Make sure to that this image is marked as dirty even if the staging texture hasn't been created yet
// if initialization is required before use.
return (mSurface || d3d9::GetTextureFormatInfo(mInternalFormat).dataInitializerFunction != NULL) && mDirty;
}
gl::Error Image9::getSurface(IDirect3DSurface9 **outSurface)
{
gl::Error error = createSurface();
if (error.isError())
{
return error;
}
*outSurface = mSurface;
return gl::Error(GL_NO_ERROR);
}
gl::Error Image9::setManagedSurface2D(TextureStorage *storage, int level)
{
IDirect3DSurface9 *surface = NULL;
TextureStorage9 *storage9 = GetAs<TextureStorage9>(storage);
gl::Error error = storage9->getSurfaceLevel(GL_TEXTURE_2D, level, false, &surface);
if (error.isError())
{
return error;
}
return setManagedSurface(surface);
}
gl::Error Image9::setManagedSurfaceCube(TextureStorage *storage, int face, int level)
{
IDirect3DSurface9 *surface = NULL;
TextureStorage9 *storage9 = GetAs<TextureStorage9>(storage);
gl::Error error =
storage9->getSurfaceLevel(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, false, &surface);
if (error.isError())
{
return error;
}
return setManagedSurface(surface);
}
gl::Error Image9::setManagedSurface(IDirect3DSurface9 *surface)
{
D3DSURFACE_DESC desc;
surface->GetDesc(&desc);
ASSERT(desc.Pool == D3DPOOL_MANAGED);
if ((GLsizei)desc.Width == mWidth && (GLsizei)desc.Height == mHeight)
{
if (mSurface)
{
gl::Error error = copyLockableSurfaces(surface, mSurface);
SafeRelease(mSurface);
if (error.isError())
{
return error;
}
}
mSurface = surface;
mD3DPool = desc.Pool;
}
return gl::Error(GL_NO_ERROR);
}
gl::Error Image9::copyToStorage(TextureStorage *storage, const gl::ImageIndex &index, const gl::Box ®ion)
{
gl::Error error = createSurface();
if (error.isError())
{
return error;
}
TextureStorage9 *storage9 = GetAs<TextureStorage9>(storage);
IDirect3DSurface9 *destSurface = NULL;
if (index.type == GL_TEXTURE_2D)
{
error = storage9->getSurfaceLevel(GL_TEXTURE_2D, index.mipIndex, true, &destSurface);
if (error.isError())
{
return error;
}
}
else
{
ASSERT(gl::IsCubeMapTextureTarget(index.type));
error = storage9->getSurfaceLevel(index.type, index.mipIndex, true, &destSurface);
if (error.isError())
{
return error;
}
}
error = copyToSurface(destSurface, region);
SafeRelease(destSurface);
return error;
}
gl::Error Image9::copyToSurface(IDirect3DSurface9 *destSurface, const gl::Box &area)
{
ASSERT(area.width > 0 && area.height > 0 && area.depth == 1);
ASSERT(destSurface);
IDirect3DSurface9 *sourceSurface = NULL;
gl::Error error = getSurface(&sourceSurface);
if (error.isError())
{
return error;
}
ASSERT(sourceSurface && sourceSurface != destSurface);
RECT rect;
rect.left = area.x;
rect.top = area.y;
rect.right = area.x + area.width;
rect.bottom = area.y + area.height;
POINT point = {rect.left, rect.top};
IDirect3DDevice9 *device = mRenderer->getDevice();
if (mD3DPool == D3DPOOL_MANAGED)
{
D3DSURFACE_DESC desc;
sourceSurface->GetDesc(&desc);
IDirect3DSurface9 *surf = 0;
HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY, "Internal CreateOffscreenPlainSurface call failed, result: 0x%X.", result);
}
copyLockableSurfaces(surf, sourceSurface);
result = device->UpdateSurface(surf, &rect, destSurface, &point);
SafeRelease(surf);
ASSERT(SUCCEEDED(result));
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY, "Internal UpdateSurface call failed, result: 0x%X.", result);
}
}
else
{
// UpdateSurface: source must be SYSTEMMEM, dest must be DEFAULT pools
HRESULT result = device->UpdateSurface(sourceSurface, &rect, destSurface, &point);
ASSERT(SUCCEEDED(result));
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY, "Internal UpdateSurface call failed, result: 0x%X.", result);
}
}
return gl::Error(GL_NO_ERROR);
}
// Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input
// into the target pixel rectangle.
gl::Error Image9::loadData(const gl::Box &area,
const gl::PixelUnpackState &unpack,
GLenum inputType,
const void *input,
bool applySkipImages)
{
// 3D textures are not supported by the D3D9 backend.
ASSERT(area.z == 0 && area.depth == 1);
ASSERT(getSizedInputFormat(inputType) == mInternalFormat);
const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(mInternalFormat);
GLuint inputRowPitch = 0;
ANGLE_TRY_RESULT(
formatInfo.computeRowPitch(area.width, unpack.alignment, unpack.rowLength),
inputRowPitch);
ASSERT(!applySkipImages);
ASSERT(unpack.skipPixels == 0);
ASSERT(unpack.skipRows == 0);
const d3d9::TextureFormat &d3dFormatInfo = d3d9::GetTextureFormatInfo(mInternalFormat);
ASSERT(d3dFormatInfo.loadFunction != NULL);
RECT lockRect =
{
area.x, area.y,
area.x + area.width, area.y + area.height
};
D3DLOCKED_RECT locked;
gl::Error error = lock(&locked, lockRect);
if (error.isError())
{
return error;
}
d3dFormatInfo.loadFunction(area.width, area.height, area.depth,
reinterpret_cast<const uint8_t *>(input), inputRowPitch, 0,
reinterpret_cast<uint8_t *>(locked.pBits), locked.Pitch, 0);
unlock();
return gl::Error(GL_NO_ERROR);
}
gl::Error Image9::loadCompressedData(const gl::Box &area, const void *input)
{
// 3D textures are not supported by the D3D9 backend.
ASSERT(area.z == 0 && area.depth == 1);
const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(mInternalFormat);
GLsizei inputRowPitch = 0;
ANGLE_TRY_RESULT(formatInfo.computeRowPitch(area.width, 1, 0), inputRowPitch);
GLsizei inputDepthPitch = 0;
ANGLE_TRY_RESULT(formatInfo.computeDepthPitch(area.height, 0, inputDepthPitch),
inputDepthPitch);
const d3d9::TextureFormat &d3d9FormatInfo = d3d9::GetTextureFormatInfo(mInternalFormat);
ASSERT(area.x % d3d9::GetD3DFormatInfo(d3d9FormatInfo.texFormat).blockWidth == 0);
ASSERT(area.y % d3d9::GetD3DFormatInfo(d3d9FormatInfo.texFormat).blockHeight == 0);
ASSERT(d3d9FormatInfo.loadFunction != NULL);
RECT lockRect =
{
area.x, area.y,
area.x + area.width, area.y + area.height
};
D3DLOCKED_RECT locked;
gl::Error error = lock(&locked, lockRect);
if (error.isError())
{
return error;
}
d3d9FormatInfo.loadFunction(area.width, area.height, area.depth,
reinterpret_cast<const uint8_t*>(input), inputRowPitch, inputDepthPitch,
reinterpret_cast<uint8_t*>(locked.pBits), locked.Pitch, 0);
unlock();
return gl::Error(GL_NO_ERROR);
}
// This implements glCopyTex[Sub]Image2D for non-renderable internal texture formats and incomplete textures
gl::Error Image9::copyFromRTInternal(const gl::Offset &destOffset,
const gl::Rectangle &sourceArea,
RenderTargetD3D *source)
{
ASSERT(source);
// ES3.0 only behaviour to copy into a 3d texture
ASSERT(destOffset.z == 0);
RenderTarget9 *renderTarget = GetAs<RenderTarget9>(source);
IDirect3DSurface9 *surface = renderTarget->getSurface();
ASSERT(surface);
IDirect3DDevice9 *device = mRenderer->getDevice();
IDirect3DSurface9 *renderTargetData = NULL;
D3DSURFACE_DESC description;
surface->GetDesc(&description);
HRESULT result = device->CreateOffscreenPlainSurface(description.Width, description.Height, description.Format, D3DPOOL_SYSTEMMEM, &renderTargetData, NULL);
if (FAILED(result))
{
SafeRelease(surface);
return gl::Error(GL_OUT_OF_MEMORY, "Could not create matching destination surface, result: 0x%X.", result);
}
result = device->GetRenderTargetData(surface, renderTargetData);
if (FAILED(result))
{
SafeRelease(renderTargetData);
SafeRelease(surface);
return gl::Error(GL_OUT_OF_MEMORY, "GetRenderTargetData unexpectedly failed, result: 0x%X.", result);
}
int width = sourceArea.width;
int height = sourceArea.height;
RECT sourceRect = { sourceArea.x, sourceArea.y, sourceArea.x + width, sourceArea.y + height };
RECT destRect = { destOffset.x, destOffset.y, destOffset.x + width, destOffset.y + height };
D3DLOCKED_RECT sourceLock = {0};
result = renderTargetData->LockRect(&sourceLock, &sourceRect, 0);
if (FAILED(result))
{
SafeRelease(renderTargetData);
SafeRelease(surface);
return gl::Error(GL_OUT_OF_MEMORY, "Failed to lock the source surface (rectangle might be invalid), result: 0x%X.", result);
}
D3DLOCKED_RECT destLock = {0};
gl::Error error = lock(&destLock, destRect);
if (error.isError())
{
renderTargetData->UnlockRect();
SafeRelease(renderTargetData);
SafeRelease(surface);
return error;
}
ASSERT(destLock.pBits && sourceLock.pBits);
unsigned char *sourcePixels = (unsigned char*)sourceLock.pBits;
unsigned char *destPixels = (unsigned char*)destLock.pBits;
switch (description.Format)
{
case D3DFMT_X8R8G8B8:
case D3DFMT_A8R8G8B8:
switch (getD3DFormat())
{
case D3DFMT_X8R8G8B8:
case D3DFMT_A8R8G8B8:
for (int y = 0; y < height; y++)
{
memcpy(destPixels, sourcePixels, 4 * width);
sourcePixels += sourceLock.Pitch;
destPixels += destLock.Pitch;
}
break;
case D3DFMT_L8:
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
destPixels[x] = sourcePixels[x * 4 + 2];
}
sourcePixels += sourceLock.Pitch;
destPixels += destLock.Pitch;
}
break;
case D3DFMT_A8L8:
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
destPixels[x * 2 + 0] = sourcePixels[x * 4 + 2];
destPixels[x * 2 + 1] = sourcePixels[x * 4 + 3];
}
sourcePixels += sourceLock.Pitch;
destPixels += destLock.Pitch;
}
break;
default:
UNREACHABLE();
}
break;
case D3DFMT_R5G6B5:
switch (getD3DFormat())
{
case D3DFMT_X8R8G8B8:
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
unsigned short rgb = ((unsigned short*)sourcePixels)[x];
unsigned char red = static_cast<unsigned char>((rgb & 0xF800) >> 8);
unsigned char green = static_cast<unsigned char>((rgb & 0x07E0) >> 3);
unsigned char blue = static_cast<unsigned char>((rgb & 0x001F) << 3);
destPixels[x + 0] = blue | (blue >> 5);
destPixels[x + 1] = green | (green >> 6);
destPixels[x + 2] = red | (red >> 5);
destPixels[x + 3] = 0xFF;
}
sourcePixels += sourceLock.Pitch;
destPixels += destLock.Pitch;
}
break;
case D3DFMT_L8:
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
unsigned char red = sourcePixels[x * 2 + 1] & 0xF8;
destPixels[x] = red | (red >> 5);
}
sourcePixels += sourceLock.Pitch;
destPixels += destLock.Pitch;
}
break;
default:
UNREACHABLE();
}
break;
case D3DFMT_A1R5G5B5:
switch (getD3DFormat())
{
case D3DFMT_X8R8G8B8:
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
unsigned short argb = ((unsigned short*)sourcePixels)[x];
unsigned char red = static_cast<unsigned char>((argb & 0x7C00) >> 7);
unsigned char green = static_cast<unsigned char>((argb & 0x03E0) >> 2);
unsigned char blue = static_cast<unsigned char>((argb & 0x001F) << 3);
destPixels[x + 0] = blue | (blue >> 5);
destPixels[x + 1] = green | (green >> 5);
destPixels[x + 2] = red | (red >> 5);
destPixels[x + 3] = 0xFF;
}
sourcePixels += sourceLock.Pitch;
destPixels += destLock.Pitch;
}
break;
case D3DFMT_A8R8G8B8:
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
unsigned short argb = ((unsigned short*)sourcePixels)[x];
unsigned char red = static_cast<unsigned char>((argb & 0x7C00) >> 7);
unsigned char green = static_cast<unsigned char>((argb & 0x03E0) >> 2);
unsigned char blue = static_cast<unsigned char>((argb & 0x001F) << 3);
unsigned char alpha = (signed short)argb >> 15;
destPixels[x + 0] = blue | (blue >> 5);
destPixels[x + 1] = green | (green >> 5);
destPixels[x + 2] = red | (red >> 5);
destPixels[x + 3] = alpha;
}
sourcePixels += sourceLock.Pitch;
destPixels += destLock.Pitch;
}
break;
case D3DFMT_L8:
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
unsigned char red = sourcePixels[x * 2 + 1] & 0x7C;
destPixels[x] = (red << 1) | (red >> 4);
}
sourcePixels += sourceLock.Pitch;
destPixels += destLock.Pitch;
}
break;
case D3DFMT_A8L8:
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
unsigned char red = sourcePixels[x * 2 + 1] & 0x7C;
destPixels[x * 2 + 0] = (red << 1) | (red >> 4);
destPixels[x * 2 + 1] = (signed char)sourcePixels[x * 2 + 1] >> 7;
}
sourcePixels += sourceLock.Pitch;
destPixels += destLock.Pitch;
}
break;
default:
UNREACHABLE();
}
break;
default:
UNREACHABLE();
}
unlock();
renderTargetData->UnlockRect();
SafeRelease(renderTargetData);
SafeRelease(surface);
mDirty = true;
return gl::Error(GL_NO_ERROR);
}
gl::Error Image9::copyFromTexStorage(const gl::ImageIndex &imageIndex, TextureStorage *source)
{
RenderTargetD3D *renderTarget = nullptr;
gl::Error error = source->getRenderTarget(imageIndex, &renderTarget);
if (error.isError())
{
return error;
}
gl::Rectangle sourceArea(0, 0, mWidth, mHeight);
return copyFromRTInternal(gl::Offset(), sourceArea, renderTarget);
}
gl::Error Image9::copyFromFramebuffer(const gl::Offset &destOffset,
const gl::Rectangle &sourceArea,
const gl::Framebuffer *source)
{
const gl::FramebufferAttachment *srcAttachment = source->getReadColorbuffer();
ASSERT(srcAttachment);
RenderTargetD3D *renderTarget = NULL;
gl::Error error = srcAttachment->getRenderTarget(&renderTarget);
if (error.isError())
{
return error;
}
ASSERT(renderTarget);
return copyFromRTInternal(destOffset, sourceArea, renderTarget);
}
} // namespace rx
|