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
|
/*
* Copyright 2009, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// This file implements the D3D9 versions of the texture resources, as well as
// the related GAPID3D9 function implementations.
#include "command_buffer/service/win/d3d9/gapi_d3d9.h"
#include "command_buffer/service/win/d3d9/texture_d3d9.h"
namespace o3d {
namespace command_buffer {
// Converts a texture format to a D3D texture format.
D3DFORMAT TextureD3D9::D3DFormat(texture::Format format) {
switch (format) {
case texture::XRGB8: return D3DFMT_X8R8G8B8;
case texture::ARGB8: return D3DFMT_A8R8G8B8;
case texture::ABGR16F: return D3DFMT_A16B16G16R16F;
case texture::R32F: return D3DFMT_R32F;
case texture::ABGR32F: return D3DFMT_A32B32G32R32F;
case texture::DXT1: return D3DFMT_DXT1;
// TODO(petersont): Add DXT3/5 support.
default: return D3DFMT_UNKNOWN;
};
}
// Converts a cube map face to a D3D face.
D3DCUBEMAP_FACES TextureD3D9::D3DFace(texture::Face face) {
switch (face) {
case texture::kFacePositiveX:
return D3DCUBEMAP_FACE_POSITIVE_X;
case texture::kFaceNegativeX:
return D3DCUBEMAP_FACE_NEGATIVE_X;
case texture::kFacePositiveY:
return D3DCUBEMAP_FACE_POSITIVE_Y;
case texture::kFaceNegativeY:
return D3DCUBEMAP_FACE_NEGATIVE_Y;
case texture::kFacePositiveZ:
return D3DCUBEMAP_FACE_POSITIVE_Z;
case texture::kFaceNegativeZ:
return D3DCUBEMAP_FACE_NEGATIVE_Z;
}
LOG(FATAL) << "Not reached.";
return D3DCUBEMAP_FACE_POSITIVE_X;
}
// Texture 2D functions
// Destroys the 2D texture, releasing the D3D texture, and its shadow if any.
Texture2DD3D9::~Texture2DD3D9() {
DCHECK(d3d_texture_);
d3d_texture_ = NULL;
if (d3d_shadow_) {
d3d_shadow_->Release();
d3d_shadow_ = NULL;
}
}
// Creates a 2D texture. For dynamic textures, create it in the default pool,
// and a shadow version in the system memory pool (that we can lock). For
// regular texture, simply create one in the managed pool.
Texture2DD3D9 *Texture2DD3D9::Create(GAPID3D9 *gapi,
unsigned int width,
unsigned int height,
unsigned int levels,
texture::Format format,
unsigned int flags,
bool enable_render_surfaces) {
DCHECK_GT(width, 0U);
DCHECK_GT(height, 0U);
DCHECK_GT(levels, 0U);
D3DFORMAT d3d_format = D3DFormat(format);
IDirect3DDevice9 *device = gapi->d3d_device();
if (enable_render_surfaces) {
CComPtr<IDirect3DTexture9> d3d_texture = NULL;
HRESULT result = device->CreateTexture(width, height, levels,
D3DUSAGE_RENDERTARGET, d3d_format,
D3DPOOL_DEFAULT, &d3d_texture,
NULL);
if (result != D3D_OK) {
LOG(ERROR) << "DirectX error when calling CreateTexture: "
<< DXGetErrorStringA(result);
return NULL;
}
return new Texture2DD3D9(levels, format, flags, width, height, d3d_texture,
NULL, enable_render_surfaces);
} else if (flags & texture::kDynamic) {
CComPtr<IDirect3DTexture9> d3d_texture = NULL;
HRESULT result = device->CreateTexture(width, height, levels,
D3DUSAGE_DYNAMIC, d3d_format,
D3DPOOL_DEFAULT, &d3d_texture,
NULL);
if (result != D3D_OK) {
LOG(ERROR) << "DirectX error when calling CreateTexture: "
<< DXGetErrorStringA(result);
return NULL;
}
CComPtr<IDirect3DTexture9> d3d_shadow = NULL;
result = device->CreateTexture(width, height, levels, D3DUSAGE_DYNAMIC,
d3d_format, D3DPOOL_SYSTEMMEM, &d3d_shadow,
NULL);
if (result != D3D_OK) {
LOG(ERROR) << "DirectX error when calling CreateTexture: "
<< DXGetErrorStringA(result);
return NULL;
}
return new Texture2DD3D9(levels, format, flags, width, height, d3d_texture,
d3d_shadow, enable_render_surfaces);
} else {
CComPtr<IDirect3DTexture9> d3d_texture = NULL;
HRESULT result = device->CreateTexture(width, height, levels, 0, d3d_format,
D3DPOOL_MANAGED, &d3d_texture, NULL);
if (result != D3D_OK) {
LOG(ERROR) << "DirectX error when calling CreateTexture: "
<< DXGetErrorStringA(result);
return NULL;
}
return new Texture2DD3D9(levels, format, flags, width, height, d3d_texture,
NULL, enable_render_surfaces);
}
}
// Sets the data in the texture, using LockRect()/UnlockRect(). For dynamic
// textures, it copies the data to the shadow texture, then updates the actual
// texture. For regular texture, it directly modifies the actual texture.
bool Texture2DD3D9::SetData(GAPID3D9 *gapi,
const Volume &volume,
unsigned int level,
texture::Face face,
unsigned int row_pitch,
unsigned int slice_pitch,
unsigned int size,
const void *data) {
DCHECK(d3d_texture_);
IDirect3DTexture9 *lock_texture = d3d_shadow_ ? d3d_shadow_ : d3d_texture_;
MipLevelInfo mip_info;
MakeMipLevelInfo(&mip_info, format(), width_, height_, 1, level);
TransferInfo src_transfer_info;
MakeTransferInfo(&src_transfer_info, mip_info, volume, row_pitch,
slice_pitch);
if (!CheckVolume(mip_info, volume) || level >= levels() ||
size < src_transfer_info.total_size)
return false;
bool full_rect = IsFullVolume(mip_info, volume);
D3DLOCKED_RECT locked_rect;
RECT rect = {volume.x, volume.y, volume.x+volume.width,
volume.y+volume.height};
DWORD lock_flags =
full_rect && (flags() & texture::kDynamic) ? D3DLOCK_DISCARD : 0;
HR(lock_texture->LockRect(level, &locked_rect, full_rect ? NULL : &rect,
lock_flags));
TransferInfo dst_transfer_info;
MakeTransferInfo(&dst_transfer_info, mip_info, volume, locked_rect.Pitch,
slice_pitch);
TransferVolume(volume, mip_info, dst_transfer_info, locked_rect.pBits,
src_transfer_info, data);
HR(lock_texture->UnlockRect(level));
if (d3d_shadow_) {
HR(gapi->d3d_device()->UpdateTexture(d3d_shadow_, d3d_texture_));
}
return true;
}
// Gets the data from the texture, using LockRect()/UnlockRect(). For dynamic
// textures, it gets the data from the shadow texture, For regular texture, it
// gets it directly from the actual texture.
bool Texture2DD3D9::GetData(GAPID3D9 *gapi,
const Volume &volume,
unsigned int level,
texture::Face face,
unsigned int row_pitch,
unsigned int slice_pitch,
unsigned int size,
void *data) {
DCHECK(d3d_texture_);
IDirect3DTexture9 *lock_texture = d3d_shadow_ ? d3d_shadow_ : d3d_texture_;
MipLevelInfo mip_info;
MakeMipLevelInfo(&mip_info, format(), width_, height_, 1, level);
TransferInfo dst_transfer_info;
MakeTransferInfo(&dst_transfer_info, mip_info, volume, row_pitch,
slice_pitch);
if (!CheckVolume(mip_info, volume) || level >= levels() ||
size < dst_transfer_info.total_size)
return false;
bool full_rect = IsFullVolume(mip_info, volume);
D3DLOCKED_RECT locked_rect;
RECT rect = {volume.x, volume.y, volume.x+volume.width,
volume.y+volume.height};
DWORD lock_flags = D3DLOCK_READONLY;
HR(lock_texture->LockRect(level, &locked_rect, full_rect ? NULL : &rect,
lock_flags));
TransferInfo src_transfer_info;
MakeTransferInfo(&src_transfer_info, mip_info, volume, locked_rect.Pitch,
slice_pitch);
TransferVolume(volume, mip_info, dst_transfer_info, data,
src_transfer_info, locked_rect.pBits);
HR(lock_texture->UnlockRect(level));
return true;
}
bool Texture2DD3D9::CreateRenderSurface(int width,
int height,
int mip_level,
int side,
IDirect3DSurface9** direct3d_surface) {
IDirect3DTexture9* d3d_texture =
static_cast<IDirect3DTexture9*>(d3d_base_texture());
D3DSURFACE_DESC surface_desc;
d3d_texture->GetLevelDesc(mip_level, &surface_desc);
if (width != surface_desc.Width || height != surface_desc.Height) {
return false;
}
HR(d3d_texture->GetSurfaceLevel(mip_level, direct3d_surface));
return true;
}
// Texture 3D functions
// Destroys the 3D texture.
Texture3DD3D9::~Texture3DD3D9() {
DCHECK(d3d_texture_);
d3d_texture_ = NULL;
if (d3d_shadow_) {
d3d_shadow_->Release();
d3d_shadow_ = NULL;
}
}
// Creates a 3D texture. For dynamic textures, create it in the default pool,
// and a shadow version in the system memory pool (that we can lock). For
// regular texture, simply create one in the managed pool.
Texture3DD3D9 *Texture3DD3D9::Create(GAPID3D9 *gapi,
unsigned int width,
unsigned int height,
unsigned int depth,
unsigned int levels,
texture::Format format,
unsigned int flags,
bool enable_render_surfaces) {
DCHECK_GT(width, 0U);
DCHECK_GT(height, 0U);
DCHECK_GT(depth, 0U);
DCHECK_GT(levels, 0U);
D3DFORMAT d3d_format = D3DFormat(format);
IDirect3DDevice9 *device = gapi->d3d_device();
if (enable_render_surfaces) {
CComPtr<IDirect3DVolumeTexture9> d3d_texture = NULL;
HRESULT result = device->CreateVolumeTexture(width, height, depth, levels,
D3DUSAGE_RENDERTARGET,
d3d_format, D3DPOOL_DEFAULT,
&d3d_texture, NULL);
if (result != D3D_OK) {
LOG(ERROR) << "DirectX error when calling CreateTexture: "
<< DXGetErrorStringA(result);
return NULL;
}
return new Texture3DD3D9(levels, format, flags, width, height, depth,
d3d_texture, NULL, enable_render_surfaces);
} else if (flags & texture::kDynamic) {
CComPtr<IDirect3DVolumeTexture9> d3d_texture = NULL;
HRESULT result = device->CreateVolumeTexture(width, height, depth, levels,
D3DUSAGE_DYNAMIC, d3d_format,
D3DPOOL_DEFAULT, &d3d_texture,
NULL);
if (result != D3D_OK) {
LOG(ERROR) << "DirectX error when calling CreateTexture: "
<< DXGetErrorStringA(result);
return NULL;
}
CComPtr<IDirect3DVolumeTexture9> d3d_shadow = NULL;
result = device->CreateVolumeTexture(width, height, depth, levels,
D3DUSAGE_DYNAMIC, d3d_format,
D3DPOOL_SYSTEMMEM, &d3d_shadow, NULL);
if (result != D3D_OK) {
LOG(ERROR) << "DirectX error when calling CreateTexture: "
<< DXGetErrorStringA(result);
return NULL;
}
return new Texture3DD3D9(levels, format, flags, width, height, depth,
d3d_texture, d3d_shadow, enable_render_surfaces);
} else {
CComPtr<IDirect3DVolumeTexture9> d3d_texture = NULL;
HRESULT result = device->CreateVolumeTexture(width, height, depth, levels,
D3DUSAGE_DYNAMIC, d3d_format,
D3DPOOL_MANAGED, &d3d_texture,
NULL);
if (result != D3D_OK) {
LOG(ERROR) << "DirectX error when calling CreateTexture: "
<< DXGetErrorStringA(result);
return NULL;
}
return new Texture3DD3D9(levels, format, flags, width, height, depth,
d3d_texture, NULL, enable_render_surfaces);
}
}
// Sets the data in the texture, using LockBox()/UnlockBox(). For dynamic
// textures, it copies the data to the shadow texture, then updates the actual
// texture. For regular texture, it directly modifies the actual texture.
bool Texture3DD3D9::SetData(GAPID3D9 *gapi,
const Volume &volume,
unsigned int level,
texture::Face face,
unsigned int row_pitch,
unsigned int slice_pitch,
unsigned int size,
const void *data) {
DCHECK(d3d_texture_);
IDirect3DVolumeTexture9 *lock_texture =
d3d_shadow_ ? d3d_shadow_ : d3d_texture_;
MipLevelInfo mip_info;
MakeMipLevelInfo(&mip_info, format(), width_, height_, depth_, level);
TransferInfo src_transfer_info;
MakeTransferInfo(&src_transfer_info, mip_info, volume, row_pitch,
slice_pitch);
if (!CheckVolume(mip_info, volume) || level >= levels() ||
size < src_transfer_info.total_size)
return false;
bool full_box = IsFullVolume(mip_info, volume);
D3DLOCKED_BOX locked_box;
D3DBOX box = {volume.x, volume.y, volume.z, volume.x+volume.width,
volume.y+volume.height, volume.z+volume.depth};
DWORD lock_flags =
full_box && (flags() & texture::kDynamic) ? D3DLOCK_DISCARD : 0;
HR(lock_texture->LockBox(level, &locked_box, full_box ? NULL : &box,
lock_flags));
TransferInfo dst_transfer_info;
MakeTransferInfo(&dst_transfer_info, mip_info, volume, locked_box.RowPitch,
locked_box.SlicePitch);
TransferVolume(volume, mip_info, dst_transfer_info, locked_box.pBits,
src_transfer_info, data);
HR(lock_texture->UnlockBox(level));
if (d3d_shadow_) {
HR(gapi->d3d_device()->UpdateTexture(d3d_shadow_, d3d_texture_));
}
return true;
}
// Gets the data from the texture, using LockBox()/UnlockBox(). For dynamic
// textures, it gets the data from the shadow texture, For regular texture, it
// gets it directly from the actual texture.
bool Texture3DD3D9::GetData(GAPID3D9 *gapi,
const Volume &volume,
unsigned int level,
texture::Face face,
unsigned int row_pitch,
unsigned int slice_pitch,
unsigned int size,
void *data) {
DCHECK(d3d_texture_);
IDirect3DVolumeTexture9 *lock_texture =
d3d_shadow_ ? d3d_shadow_ : d3d_texture_;
MipLevelInfo mip_info;
MakeMipLevelInfo(&mip_info, format(), width_, height_, depth_, level);
TransferInfo dst_transfer_info;
MakeTransferInfo(&dst_transfer_info, mip_info, volume, row_pitch,
slice_pitch);
if (!CheckVolume(mip_info, volume) || level >= levels() ||
size < dst_transfer_info.total_size)
return false;
bool full_box = IsFullVolume(mip_info, volume);
D3DLOCKED_BOX locked_box;
D3DBOX box = {volume.x, volume.y, volume.z, volume.x+volume.width,
volume.y+volume.height, volume.z+volume.depth};
DWORD lock_flags = D3DLOCK_READONLY;
HR(lock_texture->LockBox(level, &locked_box, full_box ? NULL : &box,
lock_flags));
TransferInfo src_transfer_info;
MakeTransferInfo(&src_transfer_info, mip_info, volume, locked_box.RowPitch,
locked_box.SlicePitch);
TransferVolume(volume, mip_info, dst_transfer_info, data,
src_transfer_info, locked_box.pBits);
HR(lock_texture->UnlockBox(level));
return true;
}
bool Texture3DD3D9::CreateRenderSurface(int width,
int height,
int mip_level,
int side,
IDirect3DSurface9** direct3d_surface) {
// TODO(rlp): Currently unsupported.
DCHECK(false);
return false;
}
// Texture Cube functions.
// Destroys the cube map texture, releasing the D3D texture, and its shadow if
// any.
TextureCubeD3D9::~TextureCubeD3D9() {
DCHECK(d3d_texture_);
d3d_texture_ = NULL;
if (d3d_shadow_) {
d3d_shadow_->Release();
d3d_shadow_ = NULL;
}
}
// Creates a cube map texture. For dynamic textures, create it in the default
// pool, and a shadow version in the system memory pool (that we can lock). For
// regular texture, simply create one in the managed pool.
TextureCubeD3D9 *TextureCubeD3D9::Create(GAPID3D9 *gapi,
unsigned int side,
unsigned int levels,
texture::Format format,
unsigned int flags,
bool enable_render_surfaces) {
DCHECK_GT(side, 0U);
DCHECK_GT(levels, 0U);
D3DFORMAT d3d_format = D3DFormat(format);
IDirect3DDevice9 *device = gapi->d3d_device();
if (enable_render_surfaces) {
CComPtr<IDirect3DCubeTexture9> d3d_texture = NULL;
HRESULT result = device->CreateCubeTexture(side, levels,
D3DUSAGE_RENDERTARGET,
d3d_format, D3DPOOL_DEFAULT,
&d3d_texture, NULL);
if (result != D3D_OK) {
LOG(ERROR) << "DirectX error when calling CreateTexture: "
<< DXGetErrorStringA(result);
return NULL;
}
return new TextureCubeD3D9(levels, format, flags, side, d3d_texture, NULL,
enable_render_surfaces);
} else if (flags & texture::kDynamic) {
CComPtr<IDirect3DCubeTexture9> d3d_texture = NULL;
HRESULT result = device->CreateCubeTexture(side, levels, D3DUSAGE_DYNAMIC,
d3d_format, D3DPOOL_DEFAULT,
&d3d_texture, NULL);
if (result != D3D_OK) {
LOG(ERROR) << "DirectX error when calling CreateTexture: "
<< DXGetErrorStringA(result);
return NULL;
}
CComPtr<IDirect3DCubeTexture9> d3d_shadow = NULL;
result = device->CreateCubeTexture(side, levels, D3DUSAGE_DYNAMIC,
d3d_format, D3DPOOL_SYSTEMMEM,
&d3d_shadow, NULL);
if (result != D3D_OK) {
LOG(ERROR) << "DirectX error when calling CreateTexture: "
<< DXGetErrorStringA(result);
return NULL;
}
return new TextureCubeD3D9(levels, format, flags, side, d3d_texture,
d3d_shadow, enable_render_surfaces);
} else {
CComPtr<IDirect3DCubeTexture9> d3d_texture = NULL;
HRESULT result = device->CreateCubeTexture(side, levels, 0, d3d_format,
D3DPOOL_MANAGED, &d3d_texture,
NULL);
if (result != D3D_OK) {
LOG(ERROR) << "DirectX error when calling CreateTexture: "
<< DXGetErrorStringA(result);
return NULL;
}
return new TextureCubeD3D9(levels, format, flags, side, d3d_texture, NULL,
enable_render_surfaces);
}
}
// Sets the data in the texture, using LockRect()/UnlockRect(). For dynamic
// textures, it copies the data to the shadow texture, then updates the actual
// texture. For regular texture, it directly modifies the actual texture.
bool TextureCubeD3D9::SetData(GAPID3D9 *gapi,
const Volume &volume,
unsigned int level,
texture::Face face,
unsigned int row_pitch,
unsigned int slice_pitch,
unsigned int size,
const void *data) {
DCHECK(d3d_texture_);
IDirect3DCubeTexture9 *lock_texture =
d3d_shadow_ ? d3d_shadow_ : d3d_texture_;
MipLevelInfo mip_info;
MakeMipLevelInfo(&mip_info, format(), side_, side_, 1, level);
TransferInfo src_transfer_info;
MakeTransferInfo(&src_transfer_info, mip_info, volume, row_pitch,
slice_pitch);
if (!CheckVolume(mip_info, volume) || level >= levels() ||
size < src_transfer_info.total_size)
return false;
D3DCUBEMAP_FACES d3d_face = D3DFace(face);
bool full_rect = IsFullVolume(mip_info, volume);
D3DLOCKED_RECT locked_rect;
RECT rect = {volume.x, volume.y, volume.x+volume.width,
volume.y+volume.height};
DWORD lock_flags =
full_rect && (flags() & texture::kDynamic) ? D3DLOCK_DISCARD : 0;
HR(lock_texture->LockRect(d3d_face, level, &locked_rect,
full_rect ? NULL : &rect, lock_flags));
TransferInfo dst_transfer_info;
MakeTransferInfo(&dst_transfer_info, mip_info, volume, locked_rect.Pitch,
slice_pitch);
TransferVolume(volume, mip_info, dst_transfer_info, locked_rect.pBits,
src_transfer_info, data);
HR(lock_texture->UnlockRect(d3d_face, level));
if (d3d_shadow_) {
HR(gapi->d3d_device()->UpdateTexture(d3d_shadow_, d3d_texture_));
}
return true;
}
// Gets the data from the texture, using LockRect()/UnlockRect(). For dynamic
// textures, it gets the data from the shadow texture, For regular texture, it
// gets it directly from the actual texture.
bool TextureCubeD3D9::GetData(GAPID3D9 *gapi,
const Volume &volume,
unsigned int level,
texture::Face face,
unsigned int row_pitch,
unsigned int slice_pitch,
unsigned int size,
void *data) {
DCHECK(d3d_texture_);
IDirect3DCubeTexture9 *lock_texture =
d3d_shadow_ ? d3d_shadow_ : d3d_texture_;
MipLevelInfo mip_info;
MakeMipLevelInfo(&mip_info, format(), side_, side_, 1, level);
TransferInfo dst_transfer_info;
MakeTransferInfo(&dst_transfer_info, mip_info, volume, row_pitch,
slice_pitch);
if (!CheckVolume(mip_info, volume) || level >= levels() ||
size < dst_transfer_info.total_size)
return false;
D3DCUBEMAP_FACES d3d_face = D3DFace(face);
bool full_rect = IsFullVolume(mip_info, volume);
D3DLOCKED_RECT locked_rect;
RECT rect = {volume.x, volume.y, volume.x+volume.width,
volume.y+volume.height};
DWORD lock_flags = D3DLOCK_READONLY;
HR(lock_texture->LockRect(d3d_face, level, &locked_rect,
full_rect ? NULL : &rect, lock_flags));
TransferInfo src_transfer_info;
MakeTransferInfo(&src_transfer_info, mip_info, volume, locked_rect.Pitch,
slice_pitch);
TransferVolume(volume, mip_info, dst_transfer_info, data,
src_transfer_info, locked_rect.pBits);
HR(lock_texture->UnlockRect(d3d_face, level));
return true;
}
bool TextureCubeD3D9::CreateRenderSurface(
int width,
int height,
int mip_level,
int side,
IDirect3DSurface9** direct3d_surface) {
IDirect3DCubeTexture9* d3d_cube_texture =
static_cast<IDirect3DCubeTexture9*>(d3d_base_texture());
D3DSURFACE_DESC surface_desc;
d3d_cube_texture->GetLevelDesc(mip_level, &surface_desc);
if (width != surface_desc.Width || height != surface_desc.Height ||
side < 0 || side > 5) {
return false;
}
HR(d3d_cube_texture->GetCubeMapSurface(
D3DFace(static_cast<texture::Face>(side)),
mip_level,
direct3d_surface));
return true;
}
// GAPID3D9 functions.
// Destroys a texture resource.
BufferSyncInterface::ParseError GAPID3D9::DestroyTexture(ResourceId id) {
// Dirty effect, because this texture id may be used
DirtyEffect();
return textures_.Destroy(id) ?
BufferSyncInterface::kParseNoError :
BufferSyncInterface::kParseInvalidArguments;
}
// Creates a 2D texture resource.
BufferSyncInterface::ParseError GAPID3D9::CreateTexture2D(
ResourceId id,
unsigned int width,
unsigned int height,
unsigned int levels,
texture::Format format,
unsigned int flags,
bool enable_render_surfaces) {
Texture2DD3D9 *texture = Texture2DD3D9::Create(this, width, height, levels,
format, flags,
enable_render_surfaces);
if (!texture) return BufferSyncInterface::kParseInvalidArguments;
// Dirty effect, because this texture id may be used
DirtyEffect();
textures_.Assign(id, texture);
return BufferSyncInterface::kParseNoError;
}
// Creates a 3D texture resource.
BufferSyncInterface::ParseError GAPID3D9::CreateTexture3D(
ResourceId id,
unsigned int width,
unsigned int height,
unsigned int depth,
unsigned int levels,
texture::Format format,
unsigned int flags,
bool enable_render_surfaces) {
Texture3DD3D9 *texture = Texture3DD3D9::Create(this, width, height, depth,
levels, format, flags,
enable_render_surfaces);
if (!texture) return BufferSyncInterface::kParseInvalidArguments;
// Dirty effect, because this texture id may be used
DirtyEffect();
textures_.Assign(id, texture);
return BufferSyncInterface::kParseNoError;
}
// Creates a cube map texture resource.
BufferSyncInterface::ParseError GAPID3D9::CreateTextureCube(
ResourceId id,
unsigned int side,
unsigned int levels,
texture::Format format,
unsigned int flags,
bool enable_render_surfaces) {
TextureCubeD3D9 *texture = TextureCubeD3D9::Create(this, side, levels,
format, flags,
enable_render_surfaces);
if (!texture) return BufferSyncInterface::kParseInvalidArguments;
// Dirty effect, because this texture id may be used
DirtyEffect();
textures_.Assign(id, texture);
return BufferSyncInterface::kParseNoError;
}
// Copies the data into a texture resource.
BufferSyncInterface::ParseError GAPID3D9::SetTextureData(
ResourceId id,
unsigned int x,
unsigned int y,
unsigned int z,
unsigned int width,
unsigned int height,
unsigned int depth,
unsigned int level,
texture::Face face,
unsigned int row_pitch,
unsigned int slice_pitch,
unsigned int size,
const void *data) {
TextureD3D9 *texture = textures_.Get(id);
if (!texture)
return BufferSyncInterface::kParseInvalidArguments;
Volume volume = {x, y, z, width, height, depth};
return texture->SetData(this, volume, level, face, row_pitch, slice_pitch,
size, data) ?
BufferSyncInterface::kParseNoError :
BufferSyncInterface::kParseInvalidArguments;
}
// Copies the data from a texture resource.
BufferSyncInterface::ParseError GAPID3D9::GetTextureData(
ResourceId id,
unsigned int x,
unsigned int y,
unsigned int z,
unsigned int width,
unsigned int height,
unsigned int depth,
unsigned int level,
texture::Face face,
unsigned int row_pitch,
unsigned int slice_pitch,
unsigned int size,
void *data) {
TextureD3D9 *texture = textures_.Get(id);
if (!texture)
return BufferSyncInterface::kParseInvalidArguments;
Volume volume = {x, y, z, width, height, depth};
return texture->GetData(this, volume, level, face, row_pitch, slice_pitch,
size, data) ?
BufferSyncInterface::kParseNoError :
BufferSyncInterface::kParseInvalidArguments;
}
} // namespace command_buffer
} // namespace o3d
|