Repository navigation
Expand file tree
/
Copy pathd3d8_resources.c
More file actions
640 lines (552 loc) · 20 KB
/
Copy pathd3d8_resources.c
File metadata and controls
640 lines (552 loc) · 20 KB
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
/**
* D3D8 Resource Management - Vertex Buffers, Index Buffers, Textures
*
* Implements Xbox D3D8 resource creation and Lock/Unlock using D3D11.
* Resources use system memory staging with UpdateSubresource on Unlock
* for maximum compatibility with D3D8 Lock semantics.
*/
#include "d3d8_internal.h"
#include "d3d8_swizzle.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/* ================================================================
* Format conversion: Xbox D3DFORMAT → DXGI_FORMAT
* ================================================================ */
DXGI_FORMAT d3d8_to_dxgi_format(D3DFORMAT fmt)
{
switch (fmt) {
case D3DFMT_A8R8G8B8: return DXGI_FORMAT_B8G8R8A8_UNORM;
case D3DFMT_X8R8G8B8: return DXGI_FORMAT_B8G8R8X8_UNORM;
case D3DFMT_R5G6B5: return DXGI_FORMAT_B5G6R5_UNORM;
case D3DFMT_A1R5G5B5: return DXGI_FORMAT_B5G5R5A1_UNORM;
case D3DFMT_DXT1: return DXGI_FORMAT_BC1_UNORM;
case D3DFMT_DXT3: return DXGI_FORMAT_BC2_UNORM;
case D3DFMT_DXT5: return DXGI_FORMAT_BC3_UNORM;
case D3DFMT_A8: return DXGI_FORMAT_A8_UNORM;
case D3DFMT_L8: return DXGI_FORMAT_R8_UNORM;
case D3DFMT_D24S8: return DXGI_FORMAT_D24_UNORM_S8_UINT;
case D3DFMT_D16: return DXGI_FORMAT_D16_UNORM;
case D3DFMT_LIN_A8R8G8B8: return DXGI_FORMAT_B8G8R8A8_UNORM;
case D3DFMT_LIN_X8R8G8B8: return DXGI_FORMAT_B8G8R8X8_UNORM;
case D3DFMT_LIN_R5G6B5: return DXGI_FORMAT_B5G6R5_UNORM;
case D3DFMT_LIN_A1R5G5B5: return DXGI_FORMAT_B5G5R5A1_UNORM;
case D3DFMT_A4R4G4B4: return DXGI_FORMAT_B4G4R4A4_UNORM;
case D3DFMT_LIN_A4R4G4B4: return DXGI_FORMAT_B4G4R4A4_UNORM;
case D3DFMT_A8L8: return DXGI_FORMAT_R8G8_UNORM;
case D3DFMT_P8: return DXGI_FORMAT_R8_UNORM; /* palette handled separately */
case D3DFMT_INDEX16: return DXGI_FORMAT_R16_UINT;
case D3DFMT_INDEX32: return DXGI_FORMAT_R32_UINT;
default:
fprintf(stderr, "D3D8: Unknown format 0x%X, using R8G8B8A8\n", fmt);
return DXGI_FORMAT_R8G8B8A8_UNORM;
}
}
UINT d3d8_format_bpp(D3DFORMAT fmt)
{
switch (fmt) {
case D3DFMT_A8R8G8B8:
case D3DFMT_X8R8G8B8:
case D3DFMT_LIN_A8R8G8B8:
case D3DFMT_LIN_X8R8G8B8:
case D3DFMT_INDEX32:
return 32;
case D3DFMT_R5G6B5:
case D3DFMT_A1R5G5B5:
case D3DFMT_A4R4G4B4:
case D3DFMT_LIN_R5G6B5:
case D3DFMT_LIN_A1R5G5B5:
case D3DFMT_LIN_A4R4G4B4:
case D3DFMT_D16:
case D3DFMT_INDEX16:
return 16;
case D3DFMT_A8:
case D3DFMT_L8:
case D3DFMT_P8:
return 8;
case D3DFMT_DXT1: return 4; /* 4 bits per pixel (BC1) */
case D3DFMT_DXT3:
case D3DFMT_DXT5: return 8; /* 8 bits per pixel (BC2/BC3) */
case D3DFMT_D24S8: return 32;
default: return 32;
}
}
BOOL d3d8_format_is_compressed(D3DFORMAT fmt)
{
return fmt == D3DFMT_DXT1 || fmt == D3DFMT_DXT3 || fmt == D3DFMT_DXT5;
}
UINT d3d8_row_pitch(D3DFORMAT fmt, UINT width)
{
if (d3d8_format_is_compressed(fmt)) {
UINT block_width = (width + 3) / 4;
UINT block_bytes = (fmt == D3DFMT_DXT1) ? 8 : 16;
return block_width * block_bytes;
}
return (width * d3d8_format_bpp(fmt)) / 8;
}
/* ================================================================
* Vertex Buffer Implementation
* ================================================================ */
static D3D8VertexBuffer *vb_from_iface(IDirect3DVertexBuffer8 *iface)
{
return (D3D8VertexBuffer *)iface;
}
static HRESULT __stdcall vb_QueryInterface(IDirect3DVertexBuffer8 *self, const IID *riid, void **ppv)
{
(void)self; (void)riid; (void)ppv;
return E_NOINTERFACE;
}
static ULONG __stdcall vb_AddRef(IDirect3DVertexBuffer8 *self)
{
D3D8VertexBuffer *vb = vb_from_iface(self);
return (ULONG)InterlockedIncrement(&vb->ref_count);
}
static ULONG __stdcall vb_Release(IDirect3DVertexBuffer8 *self)
{
D3D8VertexBuffer *vb = vb_from_iface(self);
LONG ref = InterlockedDecrement(&vb->ref_count);
if (ref <= 0) {
if (vb->d3d11_buffer) ID3D11Buffer_Release(vb->d3d11_buffer);
free(vb->sys_mem);
free(vb);
}
return (ULONG)ref;
}
static HRESULT __stdcall vb_GetDevice(IDirect3DVertexBuffer8 *self, IDirect3DDevice8 **ppDevice)
{
(void)self;
*ppDevice = xbox_GetD3DDevice();
return S_OK;
}
static DWORD __stdcall vb_SetPriority(IDirect3DVertexBuffer8 *self, DWORD Priority)
{
(void)self; (void)Priority;
return 0;
}
static DWORD __stdcall vb_GetPriority(IDirect3DVertexBuffer8 *self)
{
(void)self;
return 0;
}
static void __stdcall vb_PreLoad(IDirect3DVertexBuffer8 *self)
{
(void)self;
}
static DWORD __stdcall vb_GetType(IDirect3DVertexBuffer8 *self)
{
(void)self;
return 3; /* D3DRTYPE_VERTEXBUFFER */
}
static HRESULT __stdcall vb_Lock(IDirect3DVertexBuffer8 *self, UINT OffsetToLock, UINT SizeToLock, BYTE **ppbData, DWORD Flags)
{
D3D8VertexBuffer *vb = vb_from_iface(self);
(void)SizeToLock; (void)Flags;
if (!ppbData) return E_INVALIDARG;
if (vb->locked) return E_FAIL;
*ppbData = vb->sys_mem + OffsetToLock;
vb->locked = TRUE;
return S_OK;
}
static HRESULT __stdcall vb_Unlock(IDirect3DVertexBuffer8 *self)
{
D3D8VertexBuffer *vb = vb_from_iface(self);
if (!vb->locked) return E_FAIL;
vb->locked = FALSE;
vb->dirty = TRUE;
/* Upload to GPU */
ID3D11DeviceContext *ctx = d3d8_GetD3D11Context();
if (ctx && vb->d3d11_buffer) {
ID3D11DeviceContext_UpdateSubresource(ctx,
(ID3D11Resource *)vb->d3d11_buffer,
0, NULL, vb->sys_mem, vb->size, 0);
vb->dirty = FALSE;
}
return S_OK;
}
static HRESULT __stdcall vb_GetDesc(IDirect3DVertexBuffer8 *self, void *pDesc)
{
(void)self; (void)pDesc;
return E_NOTIMPL;
}
static const IDirect3DVertexBuffer8Vtbl g_vb_vtbl = {
vb_QueryInterface,
vb_AddRef,
vb_Release,
vb_GetDevice,
vb_SetPriority,
vb_GetPriority,
vb_PreLoad,
vb_GetType,
vb_Lock,
vb_Unlock,
vb_GetDesc,
};
HRESULT d3d8_CreateVertexBufferImpl(UINT Length, DWORD Usage, DWORD FVF, IDirect3DVertexBuffer8 **ppVB)
{
D3D8VertexBuffer *vb;
D3D11_BUFFER_DESC bd;
HRESULT hr;
if (!ppVB) return E_INVALIDARG;
vb = (D3D8VertexBuffer *)calloc(1, sizeof(*vb));
if (!vb) return E_OUTOFMEMORY;
vb->sys_mem = (BYTE *)calloc(1, Length);
if (!vb->sys_mem) { free(vb); return E_OUTOFMEMORY; }
/* Create D3D11 buffer */
memset(&bd, 0, sizeof(bd));
bd.ByteWidth = Length;
bd.Usage = D3D11_USAGE_DEFAULT;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
hr = ID3D11Device_CreateBuffer(d3d8_GetD3D11Device(), &bd, NULL, &vb->d3d11_buffer);
if (FAILED(hr)) {
free(vb->sys_mem);
free(vb);
return hr;
}
vb->iface.lpVtbl = &g_vb_vtbl;
vb->ref_count = 1;
vb->size = Length;
vb->fvf = FVF;
vb->usage = Usage;
*ppVB = &vb->iface;
return S_OK;
}
/* ================================================================
* Index Buffer Implementation
* ================================================================ */
static D3D8IndexBuffer *ib_from_iface(IDirect3DIndexBuffer8 *iface)
{
return (D3D8IndexBuffer *)iface;
}
static HRESULT __stdcall ib_QueryInterface(IDirect3DIndexBuffer8 *self, const IID *riid, void **ppv)
{
(void)self; (void)riid; (void)ppv;
return E_NOINTERFACE;
}
static ULONG __stdcall ib_AddRef(IDirect3DIndexBuffer8 *self)
{
return (ULONG)InterlockedIncrement(&ib_from_iface(self)->ref_count);
}
static ULONG __stdcall ib_Release(IDirect3DIndexBuffer8 *self)
{
D3D8IndexBuffer *ib = ib_from_iface(self);
LONG ref = InterlockedDecrement(&ib->ref_count);
if (ref <= 0) {
if (ib->d3d11_buffer) ID3D11Buffer_Release(ib->d3d11_buffer);
free(ib->sys_mem);
free(ib);
}
return (ULONG)ref;
}
static HRESULT __stdcall ib_GetDevice(IDirect3DIndexBuffer8 *self, IDirect3DDevice8 **ppDevice)
{
(void)self;
*ppDevice = xbox_GetD3DDevice();
return S_OK;
}
static DWORD __stdcall ib_SetPriority(IDirect3DIndexBuffer8 *self, DWORD Priority) { (void)self; (void)Priority; return 0; }
static DWORD __stdcall ib_GetPriority(IDirect3DIndexBuffer8 *self) { (void)self; return 0; }
static void __stdcall ib_PreLoad(IDirect3DIndexBuffer8 *self) { (void)self; }
static DWORD __stdcall ib_GetType(IDirect3DIndexBuffer8 *self) { (void)self; return 4; /* D3DRTYPE_INDEXBUFFER */ }
static HRESULT __stdcall ib_Lock(IDirect3DIndexBuffer8 *self, UINT OffsetToLock, UINT SizeToLock, BYTE **ppbData, DWORD Flags)
{
D3D8IndexBuffer *ib = ib_from_iface(self);
(void)SizeToLock; (void)Flags;
if (!ppbData) return E_INVALIDARG;
if (ib->locked) return E_FAIL;
*ppbData = ib->sys_mem + OffsetToLock;
ib->locked = TRUE;
return S_OK;
}
static HRESULT __stdcall ib_Unlock(IDirect3DIndexBuffer8 *self)
{
D3D8IndexBuffer *ib = ib_from_iface(self);
if (!ib->locked) return E_FAIL;
ib->locked = FALSE;
ib->dirty = TRUE;
ID3D11DeviceContext *ctx = d3d8_GetD3D11Context();
if (ctx && ib->d3d11_buffer) {
ID3D11DeviceContext_UpdateSubresource(ctx,
(ID3D11Resource *)ib->d3d11_buffer,
0, NULL, ib->sys_mem, ib->size, 0);
ib->dirty = FALSE;
}
return S_OK;
}
static HRESULT __stdcall ib_GetDesc(IDirect3DIndexBuffer8 *self, void *pDesc)
{
(void)self; (void)pDesc;
return E_NOTIMPL;
}
static const IDirect3DIndexBuffer8Vtbl g_ib_vtbl = {
ib_QueryInterface, ib_AddRef, ib_Release,
ib_GetDevice, ib_SetPriority, ib_GetPriority, ib_PreLoad, ib_GetType,
ib_Lock, ib_Unlock, ib_GetDesc,
};
HRESULT d3d8_CreateIndexBufferImpl(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer8 **ppIB)
{
D3D8IndexBuffer *ib;
D3D11_BUFFER_DESC bd;
HRESULT hr;
if (!ppIB) return E_INVALIDARG;
ib = (D3D8IndexBuffer *)calloc(1, sizeof(*ib));
if (!ib) return E_OUTOFMEMORY;
ib->sys_mem = (BYTE *)calloc(1, Length);
if (!ib->sys_mem) { free(ib); return E_OUTOFMEMORY; }
memset(&bd, 0, sizeof(bd));
bd.ByteWidth = Length;
bd.Usage = D3D11_USAGE_DEFAULT;
bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
hr = ID3D11Device_CreateBuffer(d3d8_GetD3D11Device(), &bd, NULL, &ib->d3d11_buffer);
if (FAILED(hr)) { free(ib->sys_mem); free(ib); return hr; }
ib->iface.lpVtbl = &g_ib_vtbl;
ib->ref_count = 1;
ib->size = Length;
ib->format = Format;
ib->usage = Usage;
*ppIB = &ib->iface;
return S_OK;
}
/* ================================================================
* Texture Implementation
* ================================================================ */
static D3D8Texture *tex_from_iface(IDirect3DTexture8 *iface)
{
return (D3D8Texture *)iface;
}
static HRESULT __stdcall tex_QueryInterface(IDirect3DTexture8 *self, const IID *riid, void **ppv)
{
(void)self; (void)riid; (void)ppv;
return E_NOINTERFACE;
}
static ULONG __stdcall tex_AddRef(IDirect3DTexture8 *self)
{
return (ULONG)InterlockedIncrement(&tex_from_iface(self)->ref_count);
}
static ULONG __stdcall tex_Release(IDirect3DTexture8 *self)
{
D3D8Texture *tex = tex_from_iface(self);
LONG ref = InterlockedDecrement(&tex->ref_count);
if (ref <= 0) {
UINT level;
if (tex->srv) ID3D11ShaderResourceView_Release(tex->srv);
if (tex->d3d11_texture) ID3D11Texture2D_Release(tex->d3d11_texture);
for (level = 0; level < tex->levels && level < 16; ++level)
free(tex->mip_sys_mem[level]);
free(tex);
}
return (ULONG)ref;
}
static HRESULT __stdcall tex_GetDevice(IDirect3DTexture8 *self, IDirect3DDevice8 **ppDevice)
{
(void)self;
*ppDevice = xbox_GetD3DDevice();
return S_OK;
}
static DWORD __stdcall tex_SetPriority(IDirect3DTexture8 *self, DWORD Priority) { (void)self; (void)Priority; return 0; }
static DWORD __stdcall tex_GetPriority(IDirect3DTexture8 *self) { (void)self; return 0; }
static void __stdcall tex_PreLoad(IDirect3DTexture8 *self) { (void)self; }
static DWORD __stdcall tex_GetType(IDirect3DTexture8 *self) { (void)self; return 5; /* D3DRTYPE_TEXTURE */ }
static DWORD __stdcall tex_GetLevelCount(IDirect3DTexture8 *self)
{
return tex_from_iface(self)->levels;
}
static HRESULT __stdcall tex_GetLevelDesc(IDirect3DTexture8 *self, UINT Level, D3DSURFACE_DESC *pDesc)
{
D3D8Texture *tex = tex_from_iface(self);
if (!pDesc || Level >= tex->levels) return E_INVALIDARG;
pDesc->Format = tex->d3d8_format;
pDesc->Width = tex->width >> Level;
pDesc->Height = tex->height >> Level;
if (pDesc->Width < 1) pDesc->Width = 1;
if (pDesc->Height < 1) pDesc->Height = 1;
pDesc->Pool = D3DPOOL_DEFAULT;
return S_OK;
}
static HRESULT __stdcall tex_GetSurfaceLevel(IDirect3DTexture8 *self, UINT Level, IDirect3DSurface8 **ppSurface)
{
(void)self; (void)Level; (void)ppSurface;
return E_NOTIMPL;
}
static HRESULT __stdcall tex_LockRect(IDirect3DTexture8 *self, UINT Level, D3DLOCKED_RECT *pLockedRect, const RECT *pRect, DWORD Flags)
{
D3D8Texture *tex = tex_from_iface(self);
(void)pRect; (void)Flags;
if (!pLockedRect || Level >= tex->levels || Level >= 16 ||
!tex->mip_sys_mem[Level]) return E_INVALIDARG;
if (tex->locked) return E_FAIL;
pLockedRect->Pitch = (INT)tex->mip_pitch[Level];
pLockedRect->pBits = tex->mip_sys_mem[Level];
tex->locked_level = Level;
tex->locked = TRUE;
return S_OK;
}
static HRESULT __stdcall tex_UnlockRect(IDirect3DTexture8 *self, UINT Level)
{
D3D8Texture *tex = tex_from_iface(self);
UINT width, height, pitch;
BYTE *level_mem;
if (Level >= tex->levels || Level >= 16 || !tex->locked ||
tex->locked_level != Level) return E_FAIL;
tex->locked = FALSE;
tex->dirty = TRUE;
width = tex->width >> Level;
height = tex->height >> Level;
if (width < 1) width = 1;
if (height < 1) height = 1;
pitch = tex->mip_pitch[Level];
level_mem = tex->mip_sys_mem[Level];
/* Upload the unlocked mip level to its matching D3D11 subresource. */
ID3D11DeviceContext *ctx = d3d8_GetD3D11Context();
if (ctx && tex->d3d11_texture) {
UINT rows;
BYTE *upload_data = level_mem;
BYTE *unswizzled = NULL;
if (d3d8_format_is_compressed(tex->d3d8_format))
rows = (height + 3) / 4;
else
rows = height;
/* Unswizzle if the format is a swizzled Xbox format */
if (!d3d8_format_is_compressed(tex->d3d8_format) &&
d3d8_format_is_swizzled(tex->d3d8_format))
{
UINT bpp = d3d8_format_bpp(tex->d3d8_format) / 8;
UINT linear_size = width * height * bpp;
unswizzled = (BYTE *)malloc(linear_size);
if (unswizzled) {
xbox_unswizzle_rect(unswizzled, level_mem,
width, height, bpp);
upload_data = unswizzled;
}
}
ID3D11DeviceContext_UpdateSubresource(ctx,
(ID3D11Resource *)tex->d3d11_texture,
Level, NULL, upload_data, pitch, pitch * rows);
tex->dirty = FALSE;
if (unswizzled) free(unswizzled);
}
return S_OK;
}
static const IDirect3DTexture8Vtbl g_tex_vtbl = {
tex_QueryInterface, tex_AddRef, tex_Release,
tex_GetDevice, tex_SetPriority, tex_GetPriority, tex_PreLoad, tex_GetType,
tex_GetLevelCount,
tex_GetLevelDesc, tex_GetSurfaceLevel, tex_LockRect, tex_UnlockRect,
};
static HRESULT d3d8_CreateTextureImplInternal(
UINT Width, UINT Height, UINT Levels, DWORD Usage, D3DFORMAT Format,
BOOL cubemap, IDirect3DTexture8 **ppTex)
{
D3D8Texture *tex;
D3D11_TEXTURE2D_DESC td;
D3D11_SHADER_RESOURCE_VIEW_DESC srvd;
HRESULT hr;
UINT data_size;
UINT level;
(void)Usage;
if (!ppTex) return E_INVALIDARG;
tex = (D3D8Texture *)calloc(1, sizeof(*tex));
if (!tex) return E_OUTOFMEMORY;
tex->d3d8_format = Format;
tex->dxgi_format = d3d8_to_dxgi_format(Format);
tex->width = Width;
tex->height = Height;
tex->levels = Levels ? Levels : 1;
if (tex->levels > 16) tex->levels = 16;
tex->cubemap = cubemap;
/* Keep a staging allocation for every guest-provided mip level. */
for (level = 0; level < tex->levels; ++level) {
UINT level_width = Width >> level;
UINT level_height = Height >> level;
if (level_width < 1) level_width = 1;
if (level_height < 1) level_height = 1;
tex->mip_pitch[level] = d3d8_row_pitch(Format, level_width);
if (d3d8_format_is_compressed(Format))
data_size = tex->mip_pitch[level] * ((level_height + 3) / 4);
else
data_size = tex->mip_pitch[level] * level_height;
tex->mip_sys_mem[level] = (BYTE *)calloc(1, data_size);
if (!tex->mip_sys_mem[level]) {
UINT allocated;
for (allocated = 0; allocated < level; ++allocated)
free(tex->mip_sys_mem[allocated]);
free(tex);
return E_OUTOFMEMORY;
}
}
tex->sys_mem = tex->mip_sys_mem[0];
tex->pitch = tex->mip_pitch[0];
/* Create D3D11 texture */
memset(&td, 0, sizeof(td));
td.Width = Width;
td.Height = Height;
td.MipLevels = tex->levels;
td.ArraySize = cubemap ? 6 : 1;
td.Format = tex->dxgi_format;
td.SampleDesc.Count = 1;
td.Usage = D3D11_USAGE_DEFAULT;
td.BindFlags = D3D11_BIND_SHADER_RESOURCE;
td.MiscFlags = cubemap ? D3D11_RESOURCE_MISC_TEXTURECUBE : 0;
hr = ID3D11Device_CreateTexture2D(d3d8_GetD3D11Device(), &td, NULL, &tex->d3d11_texture);
if (FAILED(hr)) {
fprintf(stderr, "D3D8: CreateTexture2D failed: 0x%08lX (fmt=%d %ux%u)\n", hr, Format, Width, Height);
for (level = 0; level < tex->levels; ++level)
free(tex->mip_sys_mem[level]);
free(tex);
return hr;
}
/* Create shader resource view */
memset(&srvd, 0, sizeof(srvd));
srvd.Format = tex->dxgi_format;
if (cubemap) {
srvd.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
srvd.TextureCube.MostDetailedMip = 0;
srvd.TextureCube.MipLevels = tex->levels;
} else {
srvd.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvd.Texture2D.MipLevels = tex->levels;
}
hr = ID3D11Device_CreateShaderResourceView(d3d8_GetD3D11Device(),
(ID3D11Resource *)tex->d3d11_texture, &srvd, &tex->srv);
if (FAILED(hr)) {
ID3D11Texture2D_Release(tex->d3d11_texture);
for (level = 0; level < tex->levels; ++level)
free(tex->mip_sys_mem[level]);
free(tex);
return hr;
}
tex->iface.lpVtbl = &g_tex_vtbl;
tex->ref_count = 1;
*ppTex = &tex->iface;
return S_OK;
}
HRESULT d3d8_CreateTextureImpl(UINT Width, UINT Height, UINT Levels,
DWORD Usage, D3DFORMAT Format,
IDirect3DTexture8 **ppTex)
{
return d3d8_CreateTextureImplInternal(Width, Height, Levels, Usage,
Format, FALSE, ppTex);
}
HRESULT d3d8_CreateCubeTextureImpl(UINT EdgeLength, UINT Levels, DWORD Usage,
D3DFORMAT Format,
IDirect3DTexture8 **ppTex)
{
return d3d8_CreateTextureImplInternal(EdgeLength, EdgeLength, Levels,
Usage, Format, TRUE, ppTex);
}
HRESULT d3d8_UpdateCubeTextureFace(IDirect3DTexture8 *texture, UINT Face,
UINT Level, const void *data,
UINT row_pitch, UINT depth_pitch)
{
D3D8Texture *tex = tex_from_iface(texture);
ID3D11DeviceContext *context = d3d8_GetD3D11Context();
UINT subresource;
if (!tex || !tex->cubemap || !tex->d3d11_texture || !context || !data ||
Face >= 6u || Level >= tex->levels)
return E_INVALIDARG;
subresource = Level + Face * tex->levels;
ID3D11DeviceContext_UpdateSubresource(
context, (ID3D11Resource *)tex->d3d11_texture, subresource, NULL,
data, row_pitch, depth_pitch);
tex->dirty = FALSE;
return S_OK;
}