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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//===========================================================================//
#ifdef D3D_ASYNC_SUPPORTED
#ifndef D3DASYNC_H
#define D3DASYNC_H
#ifdef _WIN32
#pragma once
#endif
// Set this to 1 to allow d3d calls to be buffered and played back on another thread
// Slamming this off - it's causing very hot D3D9 function calls to not be inlined and contain a bunch of unused code. (Does this code even work/add real value any more?)
#define SHADERAPI_USE_SMP 0
// Set this to 1 to allow buffering of the whole frame to memory and then playback (singlethreaded).
// This is for debugging only and is used to test the performance of just calling D3D and rendering without other CPU overhead.
#define SHADERAPI_BUFFER_D3DCALLS 0
#if SHADERAPI_BUFFER_D3DCALLS && !SHADERAPI_USE_SMP
# error "SHADERAPI_USE_SMP must be 1 for SHADERAPI_BUFFER_D3DCALLS to work!"
#endif
#include "recording.h"
#include "strtools.h"
#include "glmgr/dxabstract.h"
#ifdef NDEBUG
#define DO_D3D(x) Dx9Device()->x
#else
#define DO_D3D(x) Dx9Device()->x
//#define DO_D3D(x) { HRESULT hr=Dx9Device()->x; Assert( !FAILED(hr) ); }
#endif
#define PUSHBUFFER_NELEMS 4096
enum PushBufferState
{
PUSHBUFFER_AVAILABLE,
PUSHBUFFER_BEING_FILLED,
PUSHBUFFER_SUBMITTED,
PUSHBUFFER_BEING_USED_FOR_LOCKEDDATA,
};
class PushBuffer
{
friend class D3DDeviceWrapper;
volatile PushBufferState m_State;
uint32 m_BufferData[PUSHBUFFER_NELEMS];
public:
PushBuffer(void)
{
m_State = PUSHBUFFER_AVAILABLE;
}
};
// When running multithreaded, lock for write calls actually return a pointer to temporary memory
// buffer. When the buffer is later unlocked by the caller, data must be queued with the Unlock()
// that lets the d3d thread know how much data to copy from where. One possible optimization for
// things which write a lot of data into lock buffers woudl be to proviude a way for the caller to
// occasionally check if the Lock() has been dequeued. If so, the the data pushed so far could be
// copied asynchronously into the buffer, while the caller would be told to switch to writing
// directly to the vertex buffer.
//
// another possibility would be lock()ing in advance for large ones, such as the world renderer,
// or keeping multiple locked vb's open for meshbuilder.
struct LockedBufferContext
{
PushBuffer *m_pPushBuffer; // if a push buffer was used to hold
// the temporary data, this will be non-null
void *m_pMallocedMemory; // if memory had to be malloc'd, this will be set.
size_t m_MallocSize; // # of bytes malloced if mallocedmem ptr non-null
LockedBufferContext( void )
{
m_pPushBuffer = NULL;
m_pMallocedMemory = NULL;
}
};
// push buffer commands follow
enum PushBufferCommand
{
PBCMD_END, // at end of push buffer
PBCMD_SET_RENDERSTATE, // state, val
PBCMD_SET_TEXTURE, // stage, txtr
PBCMD_DRAWPRIM, // prim type, start v, nprims
PBCMD_DRAWINDEXEDPRIM, // prim type, baseidx, minidx, numv, starti, pcount
PBCMD_SET_PIXEL_SHADER, // shaderptr
PBCMD_SET_VERTEX_SHADER, // shaderptr
PBCMD_SET_PIXEL_SHADER_CONSTANT, // startreg, nregs, data...
PBCMD_SET_BOOLEAN_PIXEL_SHADER_CONSTANT, // startreg, nregs, data...
PBCMD_SET_INTEGER_PIXEL_SHADER_CONSTANT, // startreg, nregs, data...
PBCMD_SET_VERTEX_SHADER_CONSTANT, // startreg, nregs, data...
PBCMD_SET_BOOLEAN_VERTEX_SHADER_CONSTANT, // startreg, nregs, data...
PBCMD_SET_INTEGER_VERTEX_SHADER_CONSTANT, // startreg, nregs, data...
PBCMD_SET_RENDER_TARGET, // idx, targetptr
PBCMD_SET_DEPTH_STENCIL_SURFACE, // surfptr
PBCMD_SET_STREAM_SOURCE, // idx, sptr, ofs, stride
PBCMD_SET_INDICES, // idxbuffer
PBCMD_SET_SAMPLER_STATE, // stage, state, val
PBCMD_UNLOCK_VB, // vptr
PBCMD_UNLOCK_IB, // idxbufptr
PBCMD_SETVIEWPORT, // vp_struct
PBCMD_CLEAR, // count, n rect structs, flags, color, z, stencil
PBCMD_SET_VERTEXDECLARATION, // vdeclptr
PBCMD_BEGIN_SCENE, //
PBCMD_END_SCENE, //
PBCMD_PRESENT, // complicated..see code
PBCMD_SETCLIPPLANE, // idx, 4 floats
PBCMD_STRETCHRECT, // see code
PBCMD_ASYNC_LOCK_VB, // see code
PBCMD_ASYNC_UNLOCK_VB,
PBCMD_ASYNC_LOCK_IB, // see code
PBCMD_ASYNC_UNLOCK_IB,
PBCMD_SET_SCISSOR_RECT, // RECT
};
#define N_DWORDS( x ) (( sizeof(x)+3)/sizeof( DWORD ))
#define N_DWORDS_IN_PTR (N_DWORDS( void * ))
class D3DDeviceWrapper
{
private:
IDirect3DDevice9 *m_pD3DDevice;
bool m_bSupportsTessellation;
int m_nCurrentTessLevel;
TessellationMode_t m_nTessellationMode;
#if SHADERAPI_USE_SMP
uintptr_t m_pASyncThreadHandle;
PushBuffer *m_pCurPushBuffer;
uint32 *m_pOutputPtr;
size_t m_PushBufferFreeSlots;
#endif
#if SHADERAPI_BUFFER_D3DCALLS
bool m_bBufferingD3DCalls;
# define SHADERAPI_BUFFER_MAXRENDERTARGETS 4
IDirect3DSurface9 *m_StoredRenderTargets[SHADERAPI_BUFFER_MAXRENDERTARGETS];
#endif
PushBuffer *FindFreePushBuffer( PushBufferState newstate ); // find a free push buffer and change its state
void GetPushBuffer(void); // set us up to point at a new push buffer
void SubmitPushBufferAndGetANewOne(void); // submit the current push buffer
void ExecutePushBuffer( PushBuffer const *pb);
#if SHADERAPI_USE_SMP
void Synchronize(void); // wait for all commands to be done
#else
FORCEINLINE void Synchronize(void)
{
}
#endif
void SubmitIfNotBusy(void);
#if SHADERAPI_USE_SMP
template<class T> FORCEINLINE void PushStruct( PushBufferCommand cmd, T const *str )
{
int nwords=N_DWORDS( T );
AllocatePushBufferSpace( 1+ nwords );
m_pOutputPtr[0]=cmd;
memcpy( m_pOutputPtr+1, str, sizeof( T ) );
m_pOutputPtr += 1+nwords;
}
FORCEINLINE void AllocatePushBufferSpace(size_t nSlots)
{
// check for N slots of space, and decrement amount of space left
if ( nSlots>m_PushBufferFreeSlots ) // out of room?
{
SubmitPushBufferAndGetANewOne();
}
m_PushBufferFreeSlots -= nSlots;
}
// simple methods for pushing a few words into output buffer
FORCEINLINE void Push( PushBufferCommand cmd )
{
AllocatePushBufferSpace(1);
m_pOutputPtr[0]=cmd;
m_pOutputPtr++;
}
FORCEINLINE void Push( PushBufferCommand cmd, int arg1)
{
AllocatePushBufferSpace(2);
m_pOutputPtr[0]=cmd;
m_pOutputPtr[1]=arg1;
m_pOutputPtr += 2;
}
FORCEINLINE void Push( PushBufferCommand cmd, void *ptr )
{
AllocatePushBufferSpace(1+N_DWORDS_IN_PTR);
*(m_pOutputPtr++)=cmd;
*((void **) m_pOutputPtr)=ptr;
m_pOutputPtr+=N_DWORDS_IN_PTR;
}
FORCEINLINE void Push( PushBufferCommand cmd, void *ptr, void *ptr1 )
{
AllocatePushBufferSpace(1+2*N_DWORDS_IN_PTR);
*(m_pOutputPtr++)=cmd;
*((void **) m_pOutputPtr)=ptr;
m_pOutputPtr+=N_DWORDS_IN_PTR;
*((void **) m_pOutputPtr)=ptr1;
m_pOutputPtr+=N_DWORDS_IN_PTR;
}
FORCEINLINE void Push( PushBufferCommand cmd, void *arg1, uint32 arg2, uint32 arg3, uint32 arg4,
void *arg5)
{
AllocatePushBufferSpace(1+N_DWORDS_IN_PTR+1+1+1+N_DWORDS_IN_PTR);
*(m_pOutputPtr++)=cmd;
*((void **) m_pOutputPtr)=arg1;
m_pOutputPtr+=N_DWORDS_IN_PTR;
*(m_pOutputPtr++)=arg2;
*(m_pOutputPtr++)=arg3;
*(m_pOutputPtr++)=arg4;
*((void **) m_pOutputPtr)=arg5;
m_pOutputPtr+=N_DWORDS_IN_PTR;
}
FORCEINLINE void Push( PushBufferCommand cmd, uint32 arg1, void *ptr )
{
AllocatePushBufferSpace(2+N_DWORDS_IN_PTR);
*(m_pOutputPtr++)=cmd;
*(m_pOutputPtr++)=arg1;
*((void **) m_pOutputPtr)=ptr;
m_pOutputPtr+=N_DWORDS_IN_PTR;
}
FORCEINLINE void Push( PushBufferCommand cmd, uint32 arg1, void *ptr, int arg2, int arg3 )
{
AllocatePushBufferSpace( 4+N_DWORDS_IN_PTR );
*(m_pOutputPtr++)=cmd;
*(m_pOutputPtr++)=arg1;
*((void **) m_pOutputPtr)=ptr;
m_pOutputPtr+=N_DWORDS_IN_PTR;
m_pOutputPtr[0]=arg2;
m_pOutputPtr[1]=arg3;
m_pOutputPtr += 2;
}
FORCEINLINE void Push( PushBufferCommand cmd, int arg1, int arg2)
{
AllocatePushBufferSpace(3);
m_pOutputPtr[0]=cmd;
m_pOutputPtr[1]=arg1;
m_pOutputPtr[2]=arg2;
m_pOutputPtr += 3;
}
FORCEINLINE void Push( PushBufferCommand cmd, int arg1, int arg2, int arg3)
{
AllocatePushBufferSpace(4);
m_pOutputPtr[0]=cmd;
m_pOutputPtr[1]=arg1;
m_pOutputPtr[2]=arg2;
m_pOutputPtr[3]=arg3;
m_pOutputPtr += 4;
}
FORCEINLINE void Push( PushBufferCommand cmd, int arg1, int arg2, int arg3, int arg4, int arg5, int arg6 )
{
AllocatePushBufferSpace(7);
m_pOutputPtr[0]=cmd;
m_pOutputPtr[1]=arg1;
m_pOutputPtr[2]=arg2;
m_pOutputPtr[3]=arg3;
m_pOutputPtr[4]=arg4;
m_pOutputPtr[5]=arg5;
m_pOutputPtr[6]=arg6;
m_pOutputPtr += 7;
}
#else
template<class T> FORCEINLINE void PushStruct( PushBufferCommand cmd, T const *str )
{
}
FORCEINLINE void AllocatePushBufferSpace(size_t nSlots)
{
}
// simple methods for pushing a few words into output buffer
FORCEINLINE void Push( PushBufferCommand cmd )
{
}
FORCEINLINE void Push( PushBufferCommand cmd, int arg1)
{
}
FORCEINLINE void Push( PushBufferCommand cmd, void *ptr )
{
}
FORCEINLINE void Push( PushBufferCommand cmd, void *ptr, void *ptr1 )
{
}
FORCEINLINE void Push( PushBufferCommand cmd, void *arg1, uint32 arg2, uint32 arg3, uint32 arg4,
void *arg5)
{
}
FORCEINLINE void Push( PushBufferCommand cmd, uint32 arg1, void *ptr )
{
}
FORCEINLINE void Push( PushBufferCommand cmd, uint32 arg1, void *ptr, int arg2, int arg3 )
{
}
FORCEINLINE void Push( PushBufferCommand cmd, int arg1, int arg2)
{
}
FORCEINLINE void Push( PushBufferCommand cmd, int arg1, int arg2, int arg3)
{
}
FORCEINLINE void Push( PushBufferCommand cmd, int arg1, int arg2, int arg3, int arg4, int arg5, int arg6 )
{
}
#endif
FORCEINLINE bool ASyncMode(void) const
{
#if SHADERAPI_USE_SMP
# if SHADERAPI_BUFFER_D3DCALLS
return m_bBufferingD3DCalls;
# else
return (m_pASyncThreadHandle != 0 );
# endif
#else
return false;
#endif
}
FORCEINLINE IDirect3DDevice9* Dx9Device(void) const
{
return m_pD3DDevice;
}
void AsynchronousLock( IDirect3DVertexBuffer9* vb, size_t offset, size_t size, void **ptr,
DWORD flags,
LockedBufferContext *lb);
void AsynchronousLock( IDirect3DIndexBuffer9* ib, size_t offset, size_t size, void **ptr,
DWORD flags,
LockedBufferContext *lb);
// handlers for push buffer contexts
void HandleAsynchronousLockVBCommand( uint32 const *dptr );
void HandleAsynchronousUnLockVBCommand( uint32 const *dptr );
void HandleAsynchronousLockIBCommand( uint32 const *dptr );
void HandleAsynchronousUnLockIBCommand( uint32 const *dptr );
public:
#if SHADERAPI_BUFFER_D3DCALLS
void ExecuteAllWork( void );
#endif
void RunThread( void ); // this is what the worker thread runs
void SetASyncMode( bool onoff );
bool IsActive( void )const
{
return m_pD3DDevice != NULL;
}
void D3DeviceWrapper(void)
{
m_pD3DDevice = 0;
#if SHADERAPI_USE_SMP
m_pASyncThreadHandle = 0;
#endif
#if SHADERAPI_BUFFER_D3DCALLS
m_bBufferingD3DCalls = false;
#endif
}
void SetDevicePtr(IDirect3DDevice9 *pD3DDev )
{
m_pD3DDevice = pD3DDev;
}
void SetSupportsTessellation( bool bSupportsTessellation )
{
m_bSupportsTessellation = bSupportsTessellation;
}
void ShutDownDevice(void)
{
if ( ASyncMode() )
{
// sync w/ thread
}
m_pD3DDevice = 0;
}
void FORCEINLINE SetDepthStencilSurface( IDirect3DSurface9 *new_stencil )
{
if ( ASyncMode() )
Push( PBCMD_SET_DEPTH_STENCIL_SURFACE, new_stencil );
else
DO_D3D( SetDepthStencilSurface( new_stencil ) );
}
HRESULT CreateCubeTexture(
UINT EdgeLength,
UINT Levels,
DWORD Usage,
D3DFORMAT Format,
D3DPOOL Pool,
IDirect3DCubeTexture9 ** ppCubeTexture,
HANDLE* pSharedHandle,
char *debugLabel = NULL // <-- OK to not pass this arg, only passed through on DX_TO_GL_ABSTRACTION
)
{
Synchronize();
return m_pD3DDevice->CreateCubeTexture( EdgeLength, Levels, Usage, Format, Pool,
ppCubeTexture, pSharedHandle
#if defined( DX_TO_GL_ABSTRACTION )
,debugLabel
#endif
);
}
HRESULT CreateVolumeTexture(
UINT Width,
UINT Height,
UINT Depth,
UINT Levels,
DWORD Usage,
D3DFORMAT Format,
D3DPOOL Pool,
IDirect3DVolumeTexture9** ppVolumeTexture,
HANDLE* pSharedHandle,
char *debugLabel = NULL // <-- OK to not pass this arg, only passed through on DX_TO_GL_ABSTRACTION
)
{
Synchronize();
return m_pD3DDevice->CreateVolumeTexture( Width, Height, Depth, Levels,
Usage, Format, Pool, ppVolumeTexture,
pSharedHandle
#if defined( DX_TO_GL_ABSTRACTION )
,debugLabel
#endif
);
}
HRESULT CreateOffscreenPlainSurface( UINT Width,
UINT Height,
D3DFORMAT Format,
D3DPOOL Pool,
IDirect3DSurface9** ppSurface,
HANDLE* pSharedHandle)
{
Synchronize();
return m_pD3DDevice->CreateOffscreenPlainSurface( Width, Height, Format, Pool,
ppSurface, pSharedHandle);
}
HRESULT CreateTexture(
UINT Width,
UINT Height,
UINT Levels,
DWORD Usage,
D3DFORMAT Format,
D3DPOOL Pool,
IDirect3DTexture9** ppTexture,
HANDLE* pSharedHandle,
char *debugLabel = NULL // <-- OK to not pass this arg, only passed through on DX_TO_GL_ABSTRACTION
)
{
Synchronize();
return m_pD3DDevice->CreateTexture( Width, Height, Levels, Usage,
Format, Pool, ppTexture, pSharedHandle
#if defined( DX_TO_GL_ABSTRACTION )
,debugLabel
#endif
);
}
HRESULT GetRenderTargetData(
IDirect3DSurface9* pRenderTarget,
IDirect3DSurface9* pDestSurface
)
{
Synchronize();
return m_pD3DDevice->GetRenderTargetData( pRenderTarget, pDestSurface );
}
void GetDeviceCaps( D3DCAPS9 * pCaps )
{
Synchronize();
m_pD3DDevice->GetDeviceCaps( pCaps );
}
LPCSTR GetPixelShaderProfile( void )
{
Synchronize();
return D3DXGetPixelShaderProfile( m_pD3DDevice );
}
HRESULT TestCooperativeLevel( void )
{
// hack! We are going to assume that calling this immediately when in buffered mode isn't going to cause problems.
#if !SHADERAPI_BUFFER_D3DCALLS
Synchronize();
#endif
return m_pD3DDevice->TestCooperativeLevel();
}
HRESULT GetFrontBufferData( UINT iSwapChain, IDirect3DSurface9 * pDestSurface )
{
Synchronize();
return m_pD3DDevice->GetFrontBufferData( iSwapChain, pDestSurface );
}
void SetGammaRamp( int swapchain, int flags, D3DGAMMARAMP const *pRamp)
{
Synchronize();
m_pD3DDevice->SetGammaRamp( swapchain, flags, pRamp);
}
HRESULT GetTexture( DWORD Stage, IDirect3DBaseTexture9 ** ppTexture )
{
Synchronize();
return m_pD3DDevice->GetTexture( Stage, ppTexture );
}
HRESULT GetFVF( DWORD * pFVF )
{
Synchronize();
return m_pD3DDevice->GetFVF( pFVF );
}
HRESULT GetDepthStencilSurface(
IDirect3DSurface9 ** ppZStencilSurface
)
{
Synchronize();
return m_pD3DDevice->GetDepthStencilSurface( ppZStencilSurface );
}
FORCEINLINE void SetClipPlane( int idx, float const * pplane)
{
RECORD_COMMAND( DX8_SET_CLIP_PLANE, 5 );
RECORD_INT( idx );
RECORD_FLOAT( pplane[0] );
RECORD_FLOAT( pplane[1] );
RECORD_FLOAT( pplane[2] );
RECORD_FLOAT( pplane[3] );
#if SHADERAPI_USE_SMP
if ( ASyncMode() )
{
AllocatePushBufferSpace( 6 );
m_pOutputPtr[0]=PBCMD_SETCLIPPLANE;
m_pOutputPtr[1]=idx;
memcpy(m_pOutputPtr+2,pplane, 4*sizeof(float) );
m_pOutputPtr += 6;
}
else
#endif
DO_D3D( SetClipPlane( idx, pplane ) );
}
FORCEINLINE void SetVertexDeclaration( IDirect3DVertexDeclaration9 *decl )
{
RECORD_COMMAND( DX8_SET_VERTEX_DECLARATION, 1 );
RECORD_INT( ( int ) decl );
#if SHADERAPI_USE_SMP
if ( ASyncMode() )
{
Push( PBCMD_SET_VERTEXDECLARATION, decl );
}
else
#endif
DO_D3D( SetVertexDeclaration( decl ) );
}
FORCEINLINE void SetViewport( D3DVIEWPORT9 const *vp )
{
RECORD_COMMAND( DX8_SET_VIEWPORT, 1 );
RECORD_STRUCT( vp, sizeof( *vp ));
#if SHADERAPI_USE_SMP
if ( ASyncMode() )
PushStruct( PBCMD_SETVIEWPORT, vp );
else
#endif
DO_D3D( SetViewport( vp ) );
}
HRESULT GetRenderTarget(
DWORD RenderTargetIndex,
IDirect3DSurface9 ** ppRenderTarget)
{
#if SHADERAPI_BUFFER_D3DCALLS
if ( ASyncMode() )
{
Assert( RenderTargetIndex >= 0 && RenderTargetIndex < SHADERAPI_BUFFER_MAXRENDERTARGETS );
*ppRenderTarget = m_StoredRenderTargets[RenderTargetIndex];
return D3D_OK;
}
#endif
Synchronize();
return m_pD3DDevice->GetRenderTarget( RenderTargetIndex, ppRenderTarget );
}
HRESULT CreateQuery( D3DQUERYTYPE Type, IDirect3DQuery9** ppQuery )
{
Synchronize();
return m_pD3DDevice->CreateQuery( Type, ppQuery );
}
HRESULT CreateRenderTarget(
UINT Width,
UINT Height,
D3DFORMAT Format,
D3DMULTISAMPLE_TYPE MultiSample,
DWORD MultisampleQuality,
BOOL Lockable,
IDirect3DSurface9** ppSurface,
HANDLE* pSharedHandle
)
{
Synchronize();
return m_pD3DDevice->CreateRenderTarget( Width, Height, Format, MultiSample,
MultisampleQuality, Lockable, ppSurface,
pSharedHandle);
}
HRESULT CreateDepthStencilSurface(
UINT Width,
UINT Height,
D3DFORMAT Format,
D3DMULTISAMPLE_TYPE MultiSample,
DWORD MultisampleQuality,
BOOL Discard,
IDirect3DSurface9** ppSurface,
HANDLE* pSharedHandle
)
{
Synchronize();
return m_pD3DDevice->CreateDepthStencilSurface( Width, Height, Format, MultiSample,
MultisampleQuality, Discard, ppSurface,
pSharedHandle );
}
FORCEINLINE void SetRenderTarget( int idx, IDirect3DSurface9 *new_rt )
{
if (ASyncMode())
{
Push( PBCMD_SET_RENDER_TARGET, idx, new_rt );
#if SHADERAPI_BUFFER_D3DCALLS
m_StoredRenderTargets[idx] = new_rt;
#endif
}
else
{
// NOTE: If the debug runtime breaks here on the shadow depth render target that is normal. dx9 doesn't directly support shadow
// depth texturing so we are forced to initialize this texture without the render target flagr
DO_D3D( SetRenderTarget( idx, new_rt) );
}
}
FORCEINLINE void LightEnable( int lidx, bool onoff )
{
RECORD_COMMAND( DX8_LIGHT_ENABLE, 2 );
RECORD_INT( lidx );
RECORD_INT( onoff );
Synchronize();
DO_D3D( LightEnable( lidx, onoff ) );
}
FORCEINLINE void SetRenderState( D3DRENDERSTATETYPE state, DWORD val )
{
// Assert( state >= 0 && state < MAX_NUM_RENDERSTATES );
RECORD_RENDER_STATE( state, val );
if (ASyncMode())
{
Push( PBCMD_SET_RENDERSTATE, state, val );
}
else
DO_D3D( SetRenderState( state, val ) );
}
FORCEINLINE void SetRenderStateInline( D3DRENDERSTATETYPE state, DWORD val )
{
// Assert( state >= 0 && state < MAX_NUM_RENDERSTATES );
RECORD_RENDER_STATE( state, val );
if (ASyncMode())
{
SetRenderState( state, val );
}
else
{
#ifdef DX_TO_GL_ABSTRACTION
DO_D3D( SetRenderStateInline( state, val ) );
#else
DO_D3D( SetRenderState( state, val ) );
#endif
}
}
FORCEINLINE void SetScissorRect( const RECT *pScissorRect )
{
RECORD_COMMAND( DX8_SET_SCISSOR_RECT, 1 );
RECORD_STRUCT( pScissorRect, 4 * sizeof(LONG) );
#if SHADERAPI_USE_SMP
if ( ASyncMode() )
{
AllocatePushBufferSpace( 5 );
m_pOutputPtr[0] = PBCMD_SET_SCISSOR_RECT;
memcpy( m_pOutputPtr + 1, pScissorRect, sizeof( *pScissorRect ) );
}
else
#endif
DO_D3D( SetScissorRect( pScissorRect ) );
}
FORCEINLINE void SetVertexShaderConstantF( UINT StartRegister, CONST float * pConstantData,
UINT Vector4fCount)
{
RECORD_COMMAND( DX8_SET_VERTEX_SHADER_CONSTANT, 3 );
RECORD_INT( StartRegister );
RECORD_INT( Vector4fCount );
RECORD_STRUCT( pConstantData, Vector4fCount * 4 * sizeof(float) );
#if SHADERAPI_USE_SMP
if ( ASyncMode() )
{
AllocatePushBufferSpace(3+4*Vector4fCount);
m_pOutputPtr[0]=PBCMD_SET_VERTEX_SHADER_CONSTANT;
m_pOutputPtr[1]=StartRegister;
m_pOutputPtr[2]=Vector4fCount;
memcpy(m_pOutputPtr+3,pConstantData,sizeof(float)*4*Vector4fCount);
m_pOutputPtr+=3+4*Vector4fCount;
}
else
#endif
DO_D3D( SetVertexShaderConstantF( StartRegister, pConstantData, Vector4fCount ) );
}
FORCEINLINE void SetVertexShaderConstantB( UINT StartRegister, CONST int * pConstantData,
UINT BoolCount)
{
RECORD_COMMAND( DX8_SET_VERTEX_SHADER_CONSTANT, 3 );
RECORD_INT( StartRegister );
RECORD_INT( BoolCount );
RECORD_STRUCT( pConstantData, BoolCount * sizeof(int) );
#if SHADERAPI_USE_SMP
if ( ASyncMode() )
{
AllocatePushBufferSpace(3+BoolCount);
m_pOutputPtr[0]=PBCMD_SET_BOOLEAN_VERTEX_SHADER_CONSTANT;
m_pOutputPtr[1]=StartRegister;
m_pOutputPtr[2]=BoolCount;
memcpy(m_pOutputPtr+3,pConstantData,sizeof(int)*BoolCount);
m_pOutputPtr+=3+BoolCount;
}
else
#endif
DO_D3D( SetVertexShaderConstantB( StartRegister, pConstantData, BoolCount ) );
}
FORCEINLINE void SetVertexShaderConstantI( UINT StartRegister, CONST int * pConstantData,
UINT Vector4IntCount)
{
RECORD_COMMAND( DX8_SET_VERTEX_SHADER_CONSTANT, 3 );
RECORD_INT( StartRegister );
RECORD_INT( Vector4IntCount );
RECORD_STRUCT( pConstantData, Vector4IntCount * 4 * sizeof(int) );
#if SHADERAPI_USE_SMP
if ( ASyncMode() )
{
AllocatePushBufferSpace(3+4*Vector4IntCount);
m_pOutputPtr[0]=PBCMD_SET_INTEGER_VERTEX_SHADER_CONSTANT;
m_pOutputPtr[1]=StartRegister;
m_pOutputPtr[2]=Vector4IntCount;
memcpy(m_pOutputPtr+3,pConstantData,sizeof(int)*4*Vector4IntCount);
m_pOutputPtr+=3+4*Vector4IntCount;
}
else
#endif
DO_D3D( SetVertexShaderConstantI( StartRegister, pConstantData, Vector4IntCount ) );
}
FORCEINLINE void SetPixelShaderConstantF( UINT StartRegister, CONST float * pConstantData,
UINT Vector4fCount)
{
RECORD_COMMAND( DX8_SET_PIXEL_SHADER_CONSTANT, 3 );
RECORD_INT( StartRegister );
RECORD_INT( Vector4fCount );
RECORD_STRUCT( pConstantData, Vector4fCount * 4 * sizeof(float) );
#if SHADERAPI_USE_SMP
if ( ASyncMode() )
{
AllocatePushBufferSpace(3+4*Vector4fCount);
m_pOutputPtr[0]=PBCMD_SET_PIXEL_SHADER_CONSTANT;
m_pOutputPtr[1]=StartRegister;
m_pOutputPtr[2]=Vector4fCount;
memcpy(m_pOutputPtr+3,pConstantData,sizeof(float)*4*Vector4fCount);
m_pOutputPtr+=3+4*Vector4fCount;
}
else
#endif
DO_D3D( SetPixelShaderConstantF( StartRegister, pConstantData, Vector4fCount ) );
}
FORCEINLINE void SetPixelShaderConstantB( UINT StartRegister, CONST int * pConstantData,
UINT BoolCount)
{
RECORD_COMMAND( DX8_SET_PIXEL_SHADER_CONSTANT, 3 );
RECORD_INT( StartRegister );
RECORD_INT( BoolCount );
RECORD_STRUCT( pConstantData, BoolCount * sizeof(int) );
#if SHADERAPI_USE_SMP
if ( ASyncMode() )
{
AllocatePushBufferSpace(3+BoolCount);
m_pOutputPtr[0]=PBCMD_SET_BOOLEAN_PIXEL_SHADER_CONSTANT;
m_pOutputPtr[1]=StartRegister;
m_pOutputPtr[2]=BoolCount;
memcpy(m_pOutputPtr+3,pConstantData,sizeof(int)*BoolCount);
m_pOutputPtr+=3+BoolCount;
}
else
#endif
DO_D3D( SetPixelShaderConstantB( StartRegister, pConstantData, BoolCount ) );
}
FORCEINLINE void SetPixelShaderConstantI( UINT StartRegister, CONST int * pConstantData,
UINT Vector4IntCount)
{
RECORD_COMMAND( DX8_SET_PIXEL_SHADER_CONSTANT, 3 );
RECORD_INT( StartRegister );
RECORD_INT( Vector4IntCount );
RECORD_STRUCT( pConstantData, Vector4IntCount * 4 * sizeof(int) );
#if SHADERAPI_USE_SMP
if ( ASyncMode() )
{
AllocatePushBufferSpace(3+4*Vector4IntCount);
m_pOutputPtr[0]=PBCMD_SET_INTEGER_PIXEL_SHADER_CONSTANT;
m_pOutputPtr[1]=StartRegister;
m_pOutputPtr[2]=Vector4IntCount;
memcpy(m_pOutputPtr+3,pConstantData,sizeof(int)*4*Vector4IntCount);
m_pOutputPtr+=3+4*Vector4IntCount;
}
else
#endif
DO_D3D( SetPixelShaderConstantI( StartRegister, pConstantData, Vector4IntCount ) );
}
HRESULT StretchRect( IDirect3DSurface9 * pSourceSurface,
CONST RECT * pSourceRect,
IDirect3DSurface9 * pDestSurface,
CONST RECT * pDestRect,
D3DTEXTUREFILTERTYPE Filter )
{
#if SHADERAPI_USE_SMP
if ( ASyncMode() )
{
AllocatePushBufferSpace(1+1+1+N_DWORDS( RECT )+1+1+N_DWORDS( RECT ) + 1);
*(m_pOutputPtr++)=PBCMD_STRETCHRECT;
*(m_pOutputPtr++)=(int) pSourceSurface;
*(m_pOutputPtr++)=(pSourceRect != NULL);
if (pSourceRect)
{
memcpy(m_pOutputPtr,pSourceRect,sizeof(RECT));
}
m_pOutputPtr+=N_DWORDS(RECT);
*(m_pOutputPtr++)=(int) pDestSurface;
*(m_pOutputPtr++)=(pDestRect != NULL);
if (pDestRect)
memcpy(m_pOutputPtr,pDestRect,sizeof(RECT));
m_pOutputPtr+=N_DWORDS(RECT);
*(m_pOutputPtr++)=Filter;
return S_OK; // !bug!
}
else
#endif
return m_pD3DDevice->
StretchRect( pSourceSurface, pSourceRect, pDestSurface, pDestRect, Filter );
}
FORCEINLINE void BeginScene(void)
{
RECORD_COMMAND( DX8_BEGIN_SCENE, 0 );
if ( ASyncMode() )
Push( PBCMD_BEGIN_SCENE );
else
DO_D3D( BeginScene() );
}
FORCEINLINE void EndScene(void)
{
RECORD_COMMAND( DX8_END_SCENE, 0 );
if ( ASyncMode() )
Push( PBCMD_END_SCENE );
else
DO_D3D( EndScene() );
}
FORCEINLINE HRESULT Lock( IDirect3DVertexBuffer9* vb, size_t offset, size_t size, void **ptr, DWORD flags )
{
Assert( size ); // lock size of 0 = unknown entire size of buffer = bad
Synchronize();
HRESULT hr = vb->Lock(offset, size, ptr, flags);
switch (hr)
{
case D3DERR_INVALIDCALL:
Warning( "D3DERR_INVALIDCALL - Vertex Buffer Lock Failed in %s on line %d(offset %d, size %d, flags 0x%x)\n", V_UnqualifiedFileName(__FILE__), __LINE__, offset, size, flags );
break;
case D3DERR_DRIVERINTERNALERROR:
Warning( "D3DERR_DRIVERINTERNALERROR - Vertex Buffer Lock Failed in %s on line %d (offset %d, size %d, flags 0x%x)\n", V_UnqualifiedFileName(__FILE__), __LINE__, offset, size, flags );
break;
case D3DERR_OUTOFVIDEOMEMORY:
Warning( "D3DERR_OUTOFVIDEOMEMORY - Vertex Buffer Lock Failed in %s on line %d (offset %d, size %d, flags 0x%x)\n", V_UnqualifiedFileName(__FILE__), __LINE__, offset, size, flags );
break;
}
return hr;
}
FORCEINLINE HRESULT Lock( IDirect3DVertexBuffer9* vb, size_t offset, size_t size, void **ptr,
DWORD flags,
LockedBufferContext *lb)
{
HRESULT hr = D3D_OK;
// asynchronous write-only dynamic vb lock
if ( ASyncMode() )
{
AsynchronousLock( vb, offset, size, ptr, flags, lb );
}
else
{
hr = vb->Lock(offset, size, ptr, flags);
switch (hr)
{
case D3DERR_INVALIDCALL:
Warning( "D3DERR_INVALIDCALL - Vertex Buffer Lock Failed in %s on line %d(offset %d, size %d, flags 0x%x)\n", V_UnqualifiedFileName(__FILE__), __LINE__, offset, size, flags );
break;
case D3DERR_DRIVERINTERNALERROR:
Warning( "D3DERR_DRIVERINTERNALERROR - Vertex Buffer Lock Failed in %s on line %d (offset %d, size %d, flags 0x%x)\n", V_UnqualifiedFileName(__FILE__), __LINE__, offset, size, flags );
break;
case D3DERR_OUTOFVIDEOMEMORY:
Warning( "D3DERR_OUTOFVIDEOMEMORY - Vertex Buffer Lock Failed in %s on line %d (offset %d, size %d, flags 0x%x)\n", V_UnqualifiedFileName(__FILE__), __LINE__, offset, size, flags );
break;
}
}
return hr;
}
FORCEINLINE HRESULT Lock( IDirect3DIndexBuffer9* ib, size_t offset, size_t size, void **ptr, DWORD flags)
{
HRESULT hr = D3D_OK;
Synchronize();
hr = ib->Lock(offset, size, ptr, flags);
switch (hr)
{
case D3DERR_INVALIDCALL:
Warning( "D3DERR_INVALIDCALL - Index Buffer Lock Failed in %s on line %d(offset %d, size %d, flags 0x%x)\n", V_UnqualifiedFileName(__FILE__), __LINE__, offset, size, flags );
break;
case D3DERR_DRIVERINTERNALERROR:
Warning( "D3DERR_DRIVERINTERNALERROR - Index Buffer Lock Failed in %s on line %d (offset %d, size %d, flags 0x%x)\n", V_UnqualifiedFileName(__FILE__), __LINE__, offset, size, flags );
break;
case D3DERR_OUTOFVIDEOMEMORY:
Warning( "D3DERR_OUTOFVIDEOMEMORY - Index Buffer Lock Failed in %s on line %d (offset %d, size %d, flags 0x%x)\n", V_UnqualifiedFileName(__FILE__), __LINE__, offset, size, flags );
break;
}
return hr;
}
// asycnhronous lock of index buffer
FORCEINLINE HRESULT Lock( IDirect3DIndexBuffer9* ib, size_t offset, size_t size, void **ptr, DWORD flags,
LockedBufferContext * lb)
{
HRESULT hr = D3D_OK;
if ( ASyncMode() )
AsynchronousLock( ib, offset, size, ptr, flags, lb );
else
{
hr = ib->Lock(offset, size, ptr, flags);
switch (hr)
{
case D3DERR_INVALIDCALL:
Warning( "D3DERR_INVALIDCALL - Index Buffer Lock Failed in %s on line %d(offset %d, size %d, flags 0x%x)\n", V_UnqualifiedFileName(__FILE__), __LINE__, offset, size, flags );
break;
case D3DERR_DRIVERINTERNALERROR:
Warning( "D3DERR_DRIVERINTERNALERROR - Index Buffer Lock Failed in %s on line %d (offset %d, size %d, flags 0x%x)\n", V_UnqualifiedFileName(__FILE__), __LINE__, offset, size, flags );
break;
case D3DERR_OUTOFVIDEOMEMORY:
Warning( "D3DERR_OUTOFVIDEOMEMORY - Index Buffer Lock Failed in %s on line %d (offset %d, size %d, flags 0x%x)\n", V_UnqualifiedFileName(__FILE__), __LINE__, offset, size, flags );
break;
}
}
return hr;
}
#ifndef DX_TO_GL_ABSTRACTION
FORCEINLINE HRESULT UpdateSurface( IDirect3DSurface9* pSourceSurface, CONST RECT* pSourceRect, IDirect3DSurface9* pDestSurface, CONST POINT* pDestPoint )
{
return m_pD3DDevice->UpdateSurface( pSourceSurface, pSourceRect, pDestSurface, pDestPoint );
}
#endif
void Release( IDirect3DIndexBuffer9* ib )
{
Synchronize();
ib->Release();
}
void Release( IDirect3DVertexBuffer9* vb )
{
Synchronize();
vb->Release();
}
FORCEINLINE void Unlock( IDirect3DVertexBuffer9* vb )
{
// needed for d3d on pc only
if ( ASyncMode() )
Push(PBCMD_UNLOCK_VB, vb);
else
{
HRESULT hr = vb->Unlock( );
if ( FAILED(hr) )
{
Warning( "Vertex Buffer Unlock Failed in %s on line %d\n", V_UnqualifiedFileName(__FILE__), __LINE__ );
}
}
}
FORCEINLINE void Unlock( IDirect3DVertexBuffer9* vb, LockedBufferContext *lb, size_t unlock_size)
{
// needed for d3d on pc only
#if SHADERAPI_USE_SMP
if ( ASyncMode() )
{
AllocatePushBufferSpace( 1+N_DWORDS_IN_PTR+N_DWORDS( LockedBufferContext )+1 );
*(m_pOutputPtr++)=PBCMD_ASYNC_UNLOCK_VB;
*((IDirect3DVertexBuffer9* *) m_pOutputPtr)=vb;
m_pOutputPtr+=N_DWORDS_IN_PTR;
*((LockedBufferContext *) m_pOutputPtr)=*lb;
m_pOutputPtr+=N_DWORDS( LockedBufferContext );
*(m_pOutputPtr++)=unlock_size;
}
else
#endif
{
HRESULT hr = vb->Unlock();
if ( FAILED(hr) )
{
Warning( "Vertex Buffer Unlock Failed in %s on line %d\n", V_UnqualifiedFileName(__FILE__), __LINE__ );
}
}
}
FORCEINLINE void Unlock( IDirect3DIndexBuffer9* ib )
{
// needed for d3d on pc only
if ( ASyncMode() )
Push(PBCMD_UNLOCK_IB, ib);
else
{
HRESULT hr = ib->Unlock();
if ( FAILED(hr) )
{
Warning( "Index Buffer Unlock Failed in %s on line %d\n", V_UnqualifiedFileName(__FILE__), __LINE__ );
}
}
}
FORCEINLINE void Unlock( IDirect3DIndexBuffer9* ib, LockedBufferContext *lb, size_t unlock_size)
{
// needed for d3d on pc only
#if SHADERAPI_USE_SMP
if ( ASyncMode() )
{
AllocatePushBufferSpace( 1+N_DWORDS_IN_PTR+N_DWORDS( LockedBufferContext )+1 );
*(m_pOutputPtr++)=PBCMD_ASYNC_UNLOCK_IB;
*((IDirect3DIndexBuffer9* *) m_pOutputPtr)=ib;
m_pOutputPtr+=N_DWORDS_IN_PTR;
*((LockedBufferContext *) m_pOutputPtr)=*lb;
m_pOutputPtr+=N_DWORDS( LockedBufferContext );
*(m_pOutputPtr++)=unlock_size;
}
else
#endif
{
HRESULT hr = ib->Unlock( );
if ( FAILED(hr) )
{
Warning( "Index Buffer Unlock Failed in %s on line %d\n", V_UnqualifiedFileName(__FILE__), __LINE__ );
}
}
}
void ShowCursor( bool onoff)
{
Synchronize();
DO_D3D( ShowCursor(onoff) );
}
FORCEINLINE void Clear( int count, D3DRECT const *pRects, int Flags, D3DCOLOR color, float Z, int stencil)
{
#if SHADERAPI_USE_SMP
if ( ASyncMode() )
{
int n_rects_words = count * N_DWORDS( D3DRECT );
AllocatePushBufferSpace( 2 + n_rects_words + 4 );
*(m_pOutputPtr++) = PBCMD_CLEAR;
*(m_pOutputPtr++) = count;
if ( count )
{
memcpy( m_pOutputPtr, pRects, count * sizeof( D3DRECT ) );
m_pOutputPtr += n_rects_words;
}
*(m_pOutputPtr++) = Flags;
*( (D3DCOLOR *) m_pOutputPtr ) = color;
m_pOutputPtr++;
*( (float *) m_pOutputPtr ) = Z;
m_pOutputPtr++;
*(m_pOutputPtr++) = stencil;
}
else
#endif
DO_D3D( Clear(count, pRects, Flags, color, Z, stencil) );
}
HRESULT Reset( D3DPRESENT_PARAMETERS *parms)
{
RECORD_COMMAND( DX8_RESET, 1 );
RECORD_STRUCT( parms, sizeof(*parms) );
Synchronize();
return m_pD3DDevice->Reset( parms );
}
void Release( void )
{
Synchronize();
DO_D3D( Release() );
}
FORCEINLINE void SetTexture(int stage, IDirect3DBaseTexture9 *txtr)
{
RECORD_COMMAND( DX8_SET_TEXTURE, 3 );
RECORD_INT( stage );
RECORD_INT( -1 );
RECORD_INT( -1 );
if (ASyncMode())
{
Push( PBCMD_SET_TEXTURE, stage, txtr );
}
else
DO_D3D( SetTexture( stage, txtr) );
}
void SetTransform( D3DTRANSFORMSTATETYPE mtrx_id, D3DXMATRIX const *mt)
{
RECORD_COMMAND( DX8_SET_TRANSFORM, 2 );
RECORD_INT( mtrx_id );
RECORD_STRUCT( mt, sizeof(D3DXMATRIX) );
Synchronize();
DO_D3D( SetTransform( mtrx_id, mt) );
}
FORCEINLINE void SetSamplerState( int stage, D3DSAMPLERSTATETYPE state, DWORD val)
{
RECORD_SAMPLER_STATE( stage, state, val );
if ( ASyncMode() )
Push( PBCMD_SET_SAMPLER_STATE, stage, state, val );
else
DO_D3D( SetSamplerState( stage, state, val) );
}
void SetFVF( int fvf)
{
Synchronize();
DO_D3D( SetFVF( fvf) );
}
FORCEINLINE void SetTextureStageState( int stage, D3DTEXTURESTAGESTATETYPE state, DWORD val )
{
RECORD_TEXTURE_STAGE_STATE( stage, state, val );
Synchronize();
DO_D3D( SetTextureStageState( stage, state, val) );
}
FORCEINLINE void DrawPrimitive(
D3DPRIMITIVETYPE PrimitiveType,
UINT StartVertex,
UINT PrimitiveCount
)
{
RECORD_COMMAND( DX8_DRAW_PRIMITIVE, 3 );
RECORD_INT( PrimitiveType );
RECORD_INT( StartVertex );
RECORD_INT( PrimitiveCount );
if ( ASyncMode() )
{
Push( PBCMD_DRAWPRIM, PrimitiveType, StartVertex, PrimitiveCount );
SubmitIfNotBusy();
}
else
DO_D3D( DrawPrimitive( PrimitiveType, StartVertex, PrimitiveCount ) );
}
HRESULT CreateVertexDeclaration(
CONST D3DVERTEXELEMENT9* pVertexElements,
IDirect3DVertexDeclaration9** ppDecl
)
{
Synchronize();
return m_pD3DDevice->CreateVertexDeclaration( pVertexElements, ppDecl );
}
HRESULT ValidateDevice( DWORD * pNumPasses )
{
Synchronize();
return m_pD3DDevice->ValidateDevice( pNumPasses );
}
HRESULT CreateVertexShader(
CONST DWORD * pFunction,
IDirect3DVertexShader9** ppShader,
const char *pShaderName,
char *debugLabel = NULL
)
{
Synchronize();
#ifdef DX_TO_GL_ABSTRACTION
return m_pD3DDevice->CreateVertexShader( pFunction, ppShader, pShaderName, debugLabel );
#else
return m_pD3DDevice->CreateVertexShader( pFunction, ppShader );
#endif
}
HRESULT CreatePixelShader(
CONST DWORD * pFunction,
IDirect3DPixelShader9** ppShader,
const char *pShaderName,
char *debugLabel = NULL
)
{
Synchronize();
#ifdef DX_TO_GL_ABSTRACTION
return m_pD3DDevice->CreatePixelShader( pFunction, ppShader, pShaderName, debugLabel );
#else
return m_pD3DDevice->CreatePixelShader( pFunction, ppShader );
#endif
}
FORCEINLINE void SetIndices(
IDirect3DIndexBuffer9 * pIndexData
)
{
if ( ASyncMode() )
Push( PBCMD_SET_INDICES, pIndexData );
else
DO_D3D( SetIndices( pIndexData ) );
}
FORCEINLINE void SetStreamSource(
UINT StreamNumber,
IDirect3DVertexBuffer9 * pStreamData,
UINT OffsetInBytes,
UINT Stride
)
{
if ( ASyncMode() )
Push( PBCMD_SET_STREAM_SOURCE, StreamNumber, pStreamData, OffsetInBytes, Stride );
else
DO_D3D( SetStreamSource( StreamNumber, pStreamData, OffsetInBytes, Stride ) );
}
HRESULT CreateVertexBuffer(
UINT Length,
DWORD Usage,
DWORD FVF,
D3DPOOL Pool,
IDirect3DVertexBuffer9** ppVertexBuffer,
HANDLE* pSharedHandle
)
{
Synchronize();
return m_pD3DDevice->CreateVertexBuffer( Length, Usage, FVF,
Pool, ppVertexBuffer, pSharedHandle );
}
HRESULT CreateIndexBuffer(
UINT Length,
DWORD Usage,
D3DFORMAT Format,
D3DPOOL Pool,
IDirect3DIndexBuffer9** ppIndexBuffer,
HANDLE* pSharedHandle
)
{
Synchronize();
return m_pD3DDevice->CreateIndexBuffer( Length, Usage, Format, Pool, ppIndexBuffer,
pSharedHandle );
}
FORCEINLINE void DrawIndexedPrimitive(
D3DPRIMITIVETYPE Type,
INT BaseVertexIndex,
UINT MinIndex,
UINT NumVertices,
UINT StartIndex,
UINT PrimitiveCount )
{
RECORD_COMMAND( DX8_DRAW_INDEXED_PRIMITIVE, 6 );
RECORD_INT( Type );
RECORD_INT( BaseVertexIndex );
RECORD_INT( MinIndex );
RECORD_INT( NumVertices );
RECORD_INT( StartIndex );
RECORD_INT( PrimitiveCount );
if ( ASyncMode() )
{
Push(PBCMD_DRAWINDEXEDPRIM,
Type, BaseVertexIndex, MinIndex, NumVertices, StartIndex, PrimitiveCount );
// SubmitIfNotBusy();
}
else
{
DO_D3D( DrawIndexedPrimitive( Type, BaseVertexIndex, MinIndex, NumVertices, StartIndex, PrimitiveCount ) );
}
}
#ifndef DX_TO_GL_ABSTRACTION
FORCEINLINE void DrawTessellatedIndexedPrimitive( INT BaseVertexIndex, UINT MinIndex, UINT NumVertices,
UINT StartIndex, UINT PrimitiveCount )
{
// Setup our stream-source frequencies
DO_D3D( SetStreamSourceFreq( 0, D3DSTREAMSOURCE_INDEXEDDATA | PrimitiveCount ) );
DO_D3D( SetStreamSourceFreq( VertexStreamSpec_t::STREAM_MORPH, D3DSTREAMSOURCE_INSTANCEDATA | 1ul ) );
DO_D3D( SetStreamSourceFreq( VertexStreamSpec_t::STREAM_SUBDQUADS, D3DSTREAMSOURCE_INSTANCEDATA | 1ul ) );
int nIndicesPerPatch = ( ( ( m_nCurrentTessLevel + 1 ) * 2 + 2 ) * m_nCurrentTessLevel ) - 2;
int nVerticesPerPatch = m_nCurrentTessLevel + 1;
nVerticesPerPatch *= nVerticesPerPatch;
int nPrimitiveCount = nIndicesPerPatch - 2;
DO_D3D( DrawIndexedPrimitive( D3DPT_TRIANGLESTRIP, 0, 0, nVerticesPerPatch, 0, nPrimitiveCount ) );
// Disable instancing
DO_D3D( SetStreamSourceFreq( 0, 1ul ) );
DO_D3D( SetStreamSourceFreq( VertexStreamSpec_t::STREAM_MORPH, 1ul ) );
DO_D3D( SetStreamSourceFreq( VertexStreamSpec_t::STREAM_SUBDQUADS, 1ul ) );
}
FORCEINLINE void DrawTessellatedPrimitive( UINT StartVertex, UINT PrimitiveCount )
{
// Setup our stream-source frequencies
DO_D3D( SetStreamSourceFreq( 0, D3DSTREAMSOURCE_INDEXEDDATA | PrimitiveCount ) );
DO_D3D( SetStreamSourceFreq( VertexStreamSpec_t::STREAM_MORPH, D3DSTREAMSOURCE_INSTANCEDATA | 1ul ) );
DO_D3D( SetStreamSourceFreq( VertexStreamSpec_t::STREAM_SUBDQUADS, D3DSTREAMSOURCE_INSTANCEDATA | 1ul ) );
int nIndicesPerPatch = ( ( ( m_nCurrentTessLevel + 1 ) * 2 + 2 ) * m_nCurrentTessLevel ) - 2;
int nVerticesPerPatch = m_nCurrentTessLevel + 1;
nVerticesPerPatch *= nVerticesPerPatch;
int nPrimitiveCount = nIndicesPerPatch - 2;
DO_D3D( DrawIndexedPrimitive( D3DPT_TRIANGLESTRIP, 0, 0, nVerticesPerPatch, 0, nPrimitiveCount ) );
// Disable instancing
DO_D3D( SetStreamSourceFreq( 0, 1ul ) );
DO_D3D( SetStreamSourceFreq( VertexStreamSpec_t::STREAM_MORPH, 1ul ) );
DO_D3D( SetStreamSourceFreq( VertexStreamSpec_t::STREAM_SUBDQUADS, 1ul ) );
}
FORCEINLINE void SetTessellationLevel( float level )
{
// Track our current tessellation level
m_nCurrentTessLevel = (int)ceil( level );
}
#endif
void SetMaterial( D3DMATERIAL9 const *mat)
{
RECORD_COMMAND( DX8_SET_MATERIAL, 1 );
RECORD_STRUCT( &mat, sizeof(mat) );
Synchronize();
DO_D3D( SetMaterial( mat ) );
}
FORCEINLINE void SetPixelShader( IDirect3DPixelShader9 *pShader )
{
RECORD_COMMAND( DX8_SET_PIXEL_SHADER, 1 );
RECORD_INT( ( int ) pShader );
if ( ASyncMode() )
Push( PBCMD_SET_PIXEL_SHADER, pShader );
else
DO_D3D( SetPixelShader( pShader ) );
}
FORCEINLINE void SetVertexShader( IDirect3DVertexShader9 *pShader )
{
if ( ASyncMode() )
Push( PBCMD_SET_VERTEX_SHADER, pShader );
else
DO_D3D( SetVertexShader( pShader ) );
}
#ifdef DX_TO_GL_ABSTRACTION
FORCEINLINE HRESULT LinkShaderPair( IDirect3DVertexShader9* vs, IDirect3DPixelShader9* ps )
{
Assert ( !ASyncMode() );
return DO_D3D( LinkShaderPair( vs, ps ) );
}
HRESULT QueryShaderPair( int index, GLMShaderPairInfo *infoOut )
{
Assert ( !ASyncMode() );
return DO_D3D( QueryShaderPair( index, infoOut ) );
}
void SetMaxUsedVertexShaderConstantsHint( uint nMaxReg )
{
Assert( !ASyncMode() );
DO_D3D( SetMaxUsedVertexShaderConstantsHint( nMaxReg ) );
}
#endif
void EvictManagedResources( void )
{
if (m_pD3DDevice) // people call this before creating the device
{
Synchronize();
DO_D3D( EvictManagedResources() );
}
}
void SetLight( int i, D3DLIGHT9 const *l)
{
RECORD_COMMAND( DX8_SET_LIGHT, 2 );
RECORD_INT( i );
RECORD_STRUCT( l, sizeof(*l) );
Synchronize();
DO_D3D( SetLight(i, l) );
}
void DrawIndexedPrimitiveUP( D3DPRIMITIVETYPE PrimitiveType,
UINT MinVertexIndex,
UINT NumVertices,
UINT PrimitiveCount,
CONST void * pIndexData,
D3DFORMAT IndexDataFormat,
CONST void* pVertexStreamZeroData,
UINT VertexStreamZeroStride )
{
Synchronize();
DO_D3D( DrawIndexedPrimitiveUP( PrimitiveType, MinVertexIndex, NumVertices, PrimitiveCount,
pIndexData, IndexDataFormat, pVertexStreamZeroData,
VertexStreamZeroStride ) );
}
HRESULT Present(
CONST RECT * pSourceRect,
CONST RECT * pDestRect,
VD3DHWND hDestWindowOverride,
CONST RGNDATA * pDirtyRegion)
{
RECORD_COMMAND( DX8_PRESENT, 0 );
#if SHADERAPI_USE_SMP
if ( ASyncMode() )
{
// need to deal with ret code here
AllocatePushBufferSpace(1+1+
N_DWORDS( RECT )+1+N_DWORDS( RECT )+1+1+N_DWORDS( RGNDATA ));
*(m_pOutputPtr++)=PBCMD_PRESENT;
*(m_pOutputPtr++)=( pSourceRect != NULL );
if (pSourceRect)
memcpy(m_pOutputPtr, pSourceRect, sizeof( RECT ) );
m_pOutputPtr+=N_DWORDS( RECT );
*(m_pOutputPtr++)=( pDestRect != NULL );
if (pDestRect)
memcpy(m_pOutputPtr, pDestRect, sizeof( RECT ) );
m_pOutputPtr+=N_DWORDS( RECT );
*(m_pOutputPtr++)=(uint32) hDestWindowOverride;
*(m_pOutputPtr++)=( pDirtyRegion != NULL );
if (pDirtyRegion)
memcpy(m_pOutputPtr, pDirtyRegion, sizeof( RGNDATA ));
m_pOutputPtr+=N_DWORDS( RGNDATA );
return S_OK; // not good - caller wants to here about lost devices
}
else
#endif
return m_pD3DDevice->Present( pSourceRect, pDestRect,
hDestWindowOverride, pDirtyRegion );
}
#if defined( DX_TO_GL_ABSTRACTION )
void AcquireThreadOwnership( )
{
m_pD3DDevice->AcquireThreadOwnership();
}
void ReleaseThreadOwnership( )
{
m_pD3DDevice->ReleaseThreadOwnership();
}
#endif
};
#endif // D3DASYNC_H
#endif // #if D3D_ASYNC_SUPPORTED
|