-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathFNA3D_Driver_D3D11.c
More file actions
6003 lines (5439 loc) · 152 KB
/
FNA3D_Driver_D3D11.c
File metadata and controls
6003 lines (5439 loc) · 152 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
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
/* FNA3D - 3D Graphics Library for FNA
*
* Copyright (c) 2020-2023 Ethan Lee
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
* Ethan "flibitijibibo" Lee <flibitijibibo@flibitijibibo.com>
*
*/
#if FNA3D_DRIVER_D3D11
#include "FNA3D_Driver.h"
#include "FNA3D_PipelineCache.h"
#include "FNA3D_Driver_D3D11.h"
#include "FNA3D_Driver_D3D11_shaders.h"
#include <SDL.h>
#ifndef FNA3D_DXVK_NATIVE
#include <SDL_syswm.h>
#endif /* !FNA3D_DXVK_NATIVE */
/* D3D11 Libraries */
#if defined(_WIN32)
#define D3D11_DLL "d3d11.dll"
#define DXGI_DLL "dxgi.dll"
#elif defined(__APPLE__)
#define D3D11_DLL "libdxvk_d3d11.dylib"
#define DXGI_DLL "libdxvk_dxgi.dylib"
#else
#define D3D11_DLL "libdxvk_d3d11.so"
#define DXGI_DLL "libdxvk_dxgi.so"
#endif
#ifdef __WINRT__
#include <dxgi1_2.h>
#include <d3dcompiler.h>
#else
#include <dxgi.h>
#endif
#define ERROR_CHECK(msg) \
if (FAILED(res)) \
{ \
D3D11_INTERNAL_LogError(renderer->device, msg, res); \
}
#define ERROR_CHECK_RETURN(msg, ret) \
if (FAILED(res)) \
{ \
D3D11_INTERNAL_LogError(renderer->device, msg, res); \
return ret; \
}
#define ERROR_CHECK_UNLOCK_RETURN(msg, ret) \
if (FAILED(res)) \
{ \
D3D11_INTERNAL_LogError(renderer->device, msg, res); \
SDL_UnlockMutex(renderer->ctxLock); \
return ret; \
}
/* Internal Structures */
typedef struct D3D11Texture /* Cast FNA3D_Texture* to this! */
{
/* D3D Handles */
ID3D11Resource *handle; /* ID3D11Texture2D or ID3D11Texture3D */
ID3D11ShaderResourceView *shaderView;
/* Basic Info */
int32_t levelCount;
uint8_t isRenderTarget;
FNA3D_SurfaceFormat format;
/* Dimensions */
uint8_t rtType;
FNA3DNAMELESS union
{
struct
{
int32_t width;
int32_t height;
ID3D11RenderTargetView *rtView;
} twod;
struct
{
int32_t width;
int32_t height;
int32_t depth;
} threed;
struct
{
int32_t size;
ID3D11RenderTargetView **rtViews;
} cube;
};
ID3D11Resource *staging; /* ID3D11Texture2D or ID3D11Texture3D */
} D3D11Texture;
static D3D11Texture NullTexture =
{
NULL,
NULL,
1,
0,
FNA3D_SURFACEFORMAT_COLOR,
0,
{
{ 0, 0 }
},
NULL
};
typedef struct D3D11Renderbuffer /* Cast FNA3D_Renderbuffer* to this! */
{
ID3D11Texture2D *handle;
int32_t multiSampleCount;
#define RENDERBUFFER_COLOR 0
#define RENDERBUFFER_DEPTH 1
uint8_t type;
FNA3DNAMELESS union
{
struct
{
FNA3D_SurfaceFormat format;
ID3D11RenderTargetView *rtView;
} color;
struct
{
FNA3D_DepthFormat format;
ID3D11DepthStencilView *dsView;
} depth;
};
} D3D11Renderbuffer;
typedef struct D3D11Buffer /* Cast FNA3D_Buffer* to this! */
{
ID3D11Buffer *handle;
uint8_t dynamic;
int32_t size;
} D3D11Buffer;
typedef struct D3D11Effect /* Cast FNA3D_Effect* to this! */
{
MOJOSHADER_effect *effect;
} D3D11Effect;
typedef struct D3D11Query /* Cast FNA3D_Query* to this! */
{
ID3D11Query *handle;
} D3D11Query;
typedef struct D3D11Backbuffer
{
#define BACKBUFFER_TYPE_NULL 0
#define BACKBUFFER_TYPE_D3D11 1
uint8_t type;
int32_t width;
int32_t height;
FNA3D_DepthFormat depthFormat;
int32_t multiSampleCount;
ID3D11Texture2D* depthStencilBuffer;
ID3D11DepthStencilView* depthStencilView;
ID3D11Texture2D* stagingBuffer;
struct
{
/* Color */
FNA3D_SurfaceFormat surfaceFormat;
ID3D11Texture2D *colorBuffer;
ID3D11RenderTargetView *colorView;
ID3D11ShaderResourceView *shaderView;
/* Multisample */
ID3D11Texture2D *resolveBuffer;
} d3d11;
} D3D11Backbuffer;
typedef struct D3D11SwapchainData
{
IDXGISwapChain *swapchain;
ID3D11RenderTargetView *swapchainRTView;
void *windowHandle;
uint8_t isFullScreen;
uint8_t inExclusiveFullScreen;
uint8_t enterExclusiveFullScreenOnFocus;
} D3D11SwapchainData;
#define WINDOW_SWAPCHAIN_DATA "FNA3D_D3D11Swapchain"
typedef struct D3D11Renderer /* Cast FNA3D_Renderer* to this! */
{
/* Persistent D3D11 Objects */
ID3D11Device *device;
ID3D11DeviceContext *context;
void* d3d11_dll;
void* dxgi_dll;
void* factory; /* IDXGIFactory1 or IDXGIFactory2 */
IDXGIAdapter1 *adapter;
ID3DUserDefinedAnnotation *annotation;
BOOL supportsTearing;
SDL_mutex *ctxLock;
SDL_iconv_t iconv;
/* Window surfaces */
D3D11SwapchainData** swapchainDatas;
int32_t swapchainDataCount;
int32_t swapchainDataCapacity;
/* The Faux-Backbuffer */
D3D11Backbuffer *backbuffer;
uint8_t backbufferSizeChanged;
FNA3D_Rect prevSrcRect;
FNA3D_Rect prevDstRect;
struct
{
ID3D11VertexShader* vertexShader;
ID3D11PixelShader* pixelShader;
ID3D11SamplerState* samplerState;
ID3D11Buffer* vertexBuffer;
ID3D11Buffer* indexBuffer;
ID3D11InputLayout* inputLayout;
ID3D11RasterizerState* rasterizerState;
ID3D11BlendState* blendState;
} fauxBackbufferResources;
/* Capabilities */
uint8_t debugMode;
uint32_t supportsDxt1;
uint32_t supportsS3tc;
uint32_t supportsBc7;
uint8_t supportsSRGBRenderTarget;
int32_t maxMultiSampleCount;
D3D_FEATURE_LEVEL featureLevel;
/* Presentation */
uint8_t blockNestedPresentation;
uint8_t syncInterval;
/* Blend State */
ID3D11BlendState *blendState;
FNA3D_Color blendFactor;
int32_t multiSampleMask;
/* Depth Stencil State */
ID3D11DepthStencilState *depthStencilState;
int32_t stencilRef;
/* Rasterizer State */
FNA3D_Viewport viewport;
FNA3D_Rect scissorRect;
ID3D11RasterizerState *rasterizerState;
/* Textures */
D3D11Texture *textures[MAX_TOTAL_SAMPLERS];
ID3D11SamplerState *samplers[MAX_TOTAL_SAMPLERS];
/* Input Assembly */
ID3D11InputLayout *inputLayout;
FNA3D_PrimitiveType topology;
ID3D11Buffer *vertexBuffers[MAX_BOUND_VERTEX_BUFFERS];
uint32_t vertexBufferOffsets[MAX_BOUND_VERTEX_BUFFERS];
uint32_t vertexBufferStrides[MAX_BOUND_VERTEX_BUFFERS];
ID3D11Buffer *indexBuffer;
FNA3D_IndexElementSize indexElementSize;
/* Resource Caches */
PackedStateArray blendStateCache;
PackedStateArray depthStencilStateCache;
PackedStateArray rasterizerStateCache;
PackedStateArray samplerStateCache;
PackedVertexBufferBindingsArray inputLayoutCache;
/* Render Targets */
int32_t numRenderTargets;
ID3D11RenderTargetView *renderTargetViews[MAX_RENDERTARGET_BINDINGS];
ID3D11DepthStencilView *depthStencilView;
FNA3D_DepthFormat currentDepthFormat;
/* MojoShader Interop */
MOJOSHADER_d3d11Context *shaderContext;
MOJOSHADER_effect *currentEffect;
const MOJOSHADER_effectTechnique *currentTechnique;
uint32_t currentPass;
uint8_t effectApplied;
} D3D11Renderer;
/* XNA->D3D11 Translation Arrays */
static DXGI_FORMAT XNAToD3D_TextureFormat[] =
{
DXGI_FORMAT_R8G8B8A8_UNORM, /* SurfaceFormat.Color */
DXGI_FORMAT_B5G6R5_UNORM, /* SurfaceFormat.Bgr565 */
DXGI_FORMAT_B5G5R5A1_UNORM, /* SurfaceFormat.Bgra5551 */
DXGI_FORMAT_B4G4R4A4_UNORM, /* SurfaceFormat.Bgra4444 */
DXGI_FORMAT_BC1_UNORM, /* SurfaceFormat.Dxt1 */
DXGI_FORMAT_BC2_UNORM, /* SurfaceFormat.Dxt3 */
DXGI_FORMAT_BC3_UNORM, /* SurfaceFormat.Dxt5 */
DXGI_FORMAT_R8G8_SNORM, /* SurfaceFormat.NormalizedByte2 */
DXGI_FORMAT_R8G8B8A8_SNORM, /* SurfaceFormat.NormalizedByte4 */
DXGI_FORMAT_R10G10B10A2_UNORM, /* SurfaceFormat.Rgba1010102 */
DXGI_FORMAT_R16G16_UNORM, /* SurfaceFormat.Rg32 */
DXGI_FORMAT_R16G16B16A16_UNORM, /* SurfaceFormat.Rgba64 */
DXGI_FORMAT_A8_UNORM, /* SurfaceFormat.Alpha8 */
DXGI_FORMAT_R32_FLOAT, /* SurfaceFormat.Single */
DXGI_FORMAT_R32G32_FLOAT, /* SurfaceFormat.Vector2 */
DXGI_FORMAT_R32G32B32A32_FLOAT, /* SurfaceFormat.Vector4 */
DXGI_FORMAT_R16_FLOAT, /* SurfaceFormat.HalfSingle */
DXGI_FORMAT_R16G16_FLOAT, /* SurfaceFormat.HalfVector2 */
DXGI_FORMAT_R16G16B16A16_FLOAT, /* SurfaceFormat.HalfVector4 */
DXGI_FORMAT_R16G16B16A16_FLOAT, /* SurfaceFormat.HdrBlendable */
DXGI_FORMAT_B8G8R8A8_UNORM, /* SurfaceFormat.ColorBgraEXT */
DXGI_FORMAT_R8G8B8A8_UNORM_SRGB,/* SurfaceFormat.ColorSrgbEXT */
DXGI_FORMAT_BC3_UNORM_SRGB, /* SurfaceFormat.Dxt5SrgbEXT */
DXGI_FORMAT_BC7_UNORM, /* SurfaceFormat.BC7EXT */
DXGI_FORMAT_BC7_UNORM_SRGB, /* SurfaceFormat.BC7SrgbEXT */
};
static DXGI_FORMAT XNAToD3D_DepthFormat[] =
{
DXGI_FORMAT_UNKNOWN, /* DepthFormat.None */
DXGI_FORMAT_D16_UNORM, /* DepthFormat.Depth16 */
DXGI_FORMAT_D24_UNORM_S8_UINT, /* DepthFormat.Depth24 */
DXGI_FORMAT_D24_UNORM_S8_UINT /* DepthFormat.Depth24Stencil8 */
};
static LPCSTR XNAToD3D_VertexAttribSemanticName[] =
{
"POSITION", /* VertexElementUsage.Position */
"COLOR", /* VertexElementUsage.Color */
"TEXCOORD", /* VertexElementUsage.TextureCoordinate */
"NORMAL", /* VertexElementUsage.Normal */
"BINORMAL", /* VertexElementUsage.Binormal */
"TANGENT", /* VertexElementUsage.Tangent */
"BLENDINDICES", /* VertexElementUsage.BlendIndices */
"BLENDWEIGHT", /* VertexElementUsage.BlendWeight */
"SV_DEPTH", /* VertexElementUsage.Depth */
"FOG", /* VertexElementUsage.Fog */
"PSIZE", /* VertexElementUsage.PointSize */
"SV_SampleIndex", /* VertexElementUsage.Sample */
"TESSFACTOR" /* VertexElementUsage.TessellateFactor */
};
static DXGI_FORMAT XNAToD3D_VertexAttribFormat[] =
{
DXGI_FORMAT_R32_FLOAT, /* VertexElementFormat.Single */
DXGI_FORMAT_R32G32_FLOAT, /* VertexElementFormat.Vector2 */
DXGI_FORMAT_R32G32B32_FLOAT, /* VertexElementFormat.Vector3 */
DXGI_FORMAT_R32G32B32A32_FLOAT, /* VertexElementFormat.Vector4 */
DXGI_FORMAT_R8G8B8A8_UNORM, /* VertexElementFormat.Color */
DXGI_FORMAT_R8G8B8A8_UINT, /* VertexElementFormat.Byte4 */
DXGI_FORMAT_R16G16_SINT, /* VertexElementFormat.Short2 */
DXGI_FORMAT_R16G16B16A16_SINT, /* VertexElementFormat.Short4 */
DXGI_FORMAT_R16G16_SNORM, /* VertexElementFormat.NormalizedShort2 */
DXGI_FORMAT_R16G16B16A16_SNORM, /* VertexElementFormat.NormalizedShort4 */
DXGI_FORMAT_R16G16_FLOAT, /* VertexElementFormat.HalfVector2 */
DXGI_FORMAT_R16G16B16A16_FLOAT /* VertexElementFormat.HalfVector4 */
};
static DXGI_FORMAT XNAToD3D_IndexType[] =
{
DXGI_FORMAT_R16_UINT, /* IndexElementSize.SixteenBits */
DXGI_FORMAT_R32_UINT /* IndexElementSize.ThirtyTwoBits */
};
static D3D11_BLEND XNAToD3D_BlendMode[] =
{
D3D11_BLEND_ONE, /* Blend.One */
D3D11_BLEND_ZERO, /* Blend.Zero */
D3D11_BLEND_SRC_COLOR, /* Blend.SourceColor */
D3D11_BLEND_INV_SRC_COLOR, /* Blend.InverseSourceColor */
D3D11_BLEND_SRC_ALPHA, /* Blend.SourceAlpha */
D3D11_BLEND_INV_SRC_ALPHA, /* Blend.InverseSourceAlpha */
D3D11_BLEND_DEST_COLOR, /* Blend.DestinationColor */
D3D11_BLEND_INV_DEST_COLOR, /* Blend.InverseDestinationColor */
D3D11_BLEND_DEST_ALPHA, /* Blend.DestinationAlpha */
D3D11_BLEND_INV_DEST_ALPHA, /* Blend.InverseDestinationAlpha */
D3D11_BLEND_BLEND_FACTOR, /* Blend.BlendFactor */
D3D11_BLEND_INV_BLEND_FACTOR, /* Blend.InverseBlendFactor */
D3D11_BLEND_SRC_ALPHA_SAT /* Blend.SourceAlphaSaturation */
};
static D3D11_BLEND XNAToD3D_BlendModeAlpha[] =
{
D3D11_BLEND_ONE, /* Blend.One */
D3D11_BLEND_ZERO, /* Blend.Zero */
D3D11_BLEND_SRC_ALPHA, /* Blend.SourceColor */
D3D11_BLEND_INV_SRC_ALPHA, /* Blend.InverseSourceColor */
D3D11_BLEND_SRC_ALPHA, /* Blend.SourceAlpha */
D3D11_BLEND_INV_SRC_ALPHA, /* Blend.InverseSourceAlpha */
D3D11_BLEND_DEST_ALPHA, /* Blend.DestinationColor */
D3D11_BLEND_INV_DEST_ALPHA, /* Blend.InverseDestinationColor */
D3D11_BLEND_DEST_ALPHA, /* Blend.DestinationAlpha */
D3D11_BLEND_INV_DEST_ALPHA, /* Blend.InverseDestinationAlpha */
D3D11_BLEND_BLEND_FACTOR, /* Blend.BlendFactor */
D3D11_BLEND_INV_BLEND_FACTOR, /* Blend.InverseBlendFactor */
D3D11_BLEND_SRC_ALPHA_SAT /* Blend.SourceAlphaSaturation */
};
static D3D11_BLEND_OP XNAToD3D_BlendOperation[] =
{
D3D11_BLEND_OP_ADD, /* BlendFunction.Add */
D3D11_BLEND_OP_SUBTRACT, /* BlendFunction.Subtract */
D3D11_BLEND_OP_REV_SUBTRACT, /* BlendFunction.ReverseSubtract */
D3D11_BLEND_OP_MAX, /* BlendFunction.Max */
D3D11_BLEND_OP_MIN /* BlendFunction.Min */
};
static D3D11_COMPARISON_FUNC XNAToD3D_CompareFunc[] =
{
D3D11_COMPARISON_ALWAYS, /* CompareFunction.Always */
D3D11_COMPARISON_NEVER, /* CompareFunction.Never */
D3D11_COMPARISON_LESS, /* CompareFunction.Less */
D3D11_COMPARISON_LESS_EQUAL, /* CompareFunction.LessEqual */
D3D11_COMPARISON_EQUAL, /* CompareFunction.Equal */
D3D11_COMPARISON_GREATER_EQUAL, /* CompareFunction.GreaterEqual */
D3D11_COMPARISON_GREATER, /* CompareFunction.Greater */
D3D11_COMPARISON_NOT_EQUAL /* CompareFunction.NotEqual */
};
static D3D11_STENCIL_OP XNAToD3D_StencilOp[] =
{
D3D11_STENCIL_OP_KEEP, /* StencilOperation.Keep */
D3D11_STENCIL_OP_ZERO, /* StencilOperation.Zero */
D3D11_STENCIL_OP_REPLACE, /* StencilOperation.Replace */
D3D11_STENCIL_OP_INCR, /* StencilOperation.Increment */
D3D11_STENCIL_OP_DECR, /* StencilOperation.Decrement */
D3D11_STENCIL_OP_INCR_SAT, /* StencilOperation.IncrementSaturation */
D3D11_STENCIL_OP_DECR_SAT, /* StencilOperation.DecrementSaturation */
D3D11_STENCIL_OP_INVERT /* StencilOperation.Invert */
};
static D3D11_FILL_MODE XNAToD3D_FillMode[] =
{
D3D11_FILL_SOLID, /* FillMode.Solid */
D3D11_FILL_WIREFRAME /* FillMode.WireFrame */
};
static float XNAToD3D_DepthBiasScale[] =
{
0.0f, /* DepthFormat.None */
(float) ((1 << 16) - 1), /* DepthFormat.Depth16 */
(float) ((1 << 24) - 1), /* DepthFormat.Depth24 */
(float) ((1 << 24) - 1) /* DepthFormat.Depth24Stencil8 */
};
static D3D11_CULL_MODE XNAToD3D_CullMode[] =
{
D3D11_CULL_NONE, /* CullMode.None */
D3D11_CULL_BACK, /* CullMode.CullClockwiseFace */
D3D11_CULL_FRONT /* CullMode.CullCounterClockwiseFace */
};
static D3D11_TEXTURE_ADDRESS_MODE XNAToD3D_Wrap[] =
{
D3D11_TEXTURE_ADDRESS_WRAP, /* TextureAddressMode.Wrap */
D3D11_TEXTURE_ADDRESS_CLAMP, /* TextureAddressMode.Clamp */
D3D11_TEXTURE_ADDRESS_MIRROR /* TextureAddressMode.Mirror */
};
static D3D11_FILTER XNAToD3D_Filter[] =
{
D3D11_FILTER_MIN_MAG_MIP_LINEAR, /* TextureFilter.Linear */
D3D11_FILTER_MIN_MAG_MIP_POINT, /* TextureFilter.Point */
D3D11_FILTER_ANISOTROPIC, /* TextureFilter.Anisotropic */
D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT, /* TextureFilter.LinearMipPoint */
D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR, /* TextureFilter.PointMipLinear */
D3D11_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR, /* TextureFilter.MinLinearMagPointMipLinear */
D3D11_FILTER_MIN_LINEAR_MAG_MIP_POINT, /* TextureFilter.MinLinearMagPointMipPoint */
D3D11_FILTER_MIN_POINT_MAG_MIP_LINEAR, /* TextureFilter.MinPointMagLinearMipLinear */
D3D11_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT /* TextureFilter.MinPointMagLinearMipPoint */
};
static D3D_PRIMITIVE_TOPOLOGY XNAToD3D_Primitive[] =
{
D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST, /* PrimitiveType.TriangleList */
D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, /* PrimitiveType.TriangleStrip */
D3D_PRIMITIVE_TOPOLOGY_LINELIST, /* PrimitiveType.LineList */
D3D_PRIMITIVE_TOPOLOGY_LINESTRIP, /* PrimitiveType.LineStrip */
D3D_PRIMITIVE_TOPOLOGY_POINTLIST /* PrimitiveType.PointListEXT */
};
/* Helper Functions */
static void D3D11_INTERNAL_LogError(
ID3D11Device *device,
const char *msg,
HRESULT res
) {
#define MAX_ERROR_LEN 2048 /* FIXME: Arbitrary! */
/* Buffer for text, ensure space for \0 terminator after buffer */
char wszMsgBuff[MAX_ERROR_LEN + 1];
DWORD dwChars; /* Number of chars returned. */
if (res == DXGI_ERROR_DEVICE_REMOVED)
{
res = ID3D11Device_GetDeviceRemovedReason(device);
}
/* Try to get the message from the system errors. */
#ifdef _WIN32
dwChars = FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
res,
0,
wszMsgBuff,
MAX_ERROR_LEN,
NULL
);
#else
/* FIXME: Do we have error strings in dxvk-native? -flibit */
dwChars = 0;
#endif
/* No message? Screw it, just post the code. */
if (dwChars == 0)
{
FNA3D_LogError("%s! Error Code: 0x%08X", msg, res);
return;
}
/* Ensure valid range */
dwChars = SDL_min(dwChars, MAX_ERROR_LEN);
/* Trim whitespace from tail of message */
while (dwChars > 0)
{
if (wszMsgBuff[dwChars - 1] <= ' ')
{
dwChars--;
}
else
{
break;
}
}
/* Ensure null-terminated string */
wszMsgBuff[dwChars] = '\0';
FNA3D_LogError("%s! Error Code: %s (0x%08X)", msg, wszMsgBuff, res);
}
static inline uint32_t D3D11_INTERNAL_CalcSubresource(
uint32_t mipLevel,
uint32_t arraySlice,
uint32_t numLevels
) {
return mipLevel + (arraySlice * numLevels);
}
static uint8_t D3D11_INTERNAL_BlendEquals(
FNA3D_Color *a,
FNA3D_Color *b
) {
return SDL_memcmp(a, b, sizeof(FNA3D_Color)) == 0;
}
/* Pipeline State Object Caching */
static ID3D11BlendState* D3D11_INTERNAL_FetchBlendState(
D3D11Renderer *renderer,
FNA3D_BlendState *state
) {
PackedState packedState;
D3D11_BLEND_DESC desc = {0};
ID3D11BlendState *result;
HRESULT res;
/* Can we just reuse an existing state? */
packedState = GetPackedBlendState(*state);
result = (ID3D11BlendState*) PackedStateArray_Fetch(
renderer->blendStateCache,
packedState
);
if (result != NULL)
{
/* The state is already cached! */
return result;
}
/* We need to make a new blend state... */
desc.AlphaToCoverageEnable = 0;
desc.IndependentBlendEnable = 0;
desc.RenderTarget[0].BlendEnable = !(
state->colorSourceBlend == FNA3D_BLEND_ONE &&
state->colorDestinationBlend == FNA3D_BLEND_ZERO &&
state->alphaSourceBlend == FNA3D_BLEND_ONE &&
state->alphaDestinationBlend == FNA3D_BLEND_ZERO
);
if (desc.RenderTarget[0].BlendEnable)
{
desc.RenderTarget[0].BlendOp = XNAToD3D_BlendOperation[
state->colorBlendFunction
];
desc.RenderTarget[0].BlendOpAlpha = XNAToD3D_BlendOperation[
state->alphaBlendFunction
];
desc.RenderTarget[0].DestBlend = XNAToD3D_BlendMode[
state->colorDestinationBlend
];
desc.RenderTarget[0].DestBlendAlpha = XNAToD3D_BlendModeAlpha[
state->alphaDestinationBlend
];
desc.RenderTarget[0].SrcBlend = XNAToD3D_BlendMode[
state->colorSourceBlend
];
desc.RenderTarget[0].SrcBlendAlpha = XNAToD3D_BlendModeAlpha[
state->alphaSourceBlend
];
}
/* All other states should match for all targets... */
desc.RenderTarget[1] = desc.RenderTarget[0];
desc.RenderTarget[2] = desc.RenderTarget[0];
desc.RenderTarget[3] = desc.RenderTarget[0];
/* ... except RenderTargetWriteMask. */
desc.RenderTarget[0].RenderTargetWriteMask = (
(uint32_t) state->colorWriteEnable
);
desc.RenderTarget[1].RenderTargetWriteMask = (
(uint32_t) state->colorWriteEnable1
);
desc.RenderTarget[2].RenderTargetWriteMask = (
(uint32_t) state->colorWriteEnable2
);
desc.RenderTarget[3].RenderTargetWriteMask = (
(uint32_t) state->colorWriteEnable3
);
/* Bake the state! */
res = ID3D11Device_CreateBlendState(
renderer->device,
&desc,
&result
);
ERROR_CHECK_RETURN("Blend state creation failed", NULL)
PackedStateArray_Insert(
&renderer->blendStateCache,
packedState,
result
);
/* Return the state! */
return result;
}
static ID3D11DepthStencilState* D3D11_INTERNAL_FetchDepthStencilState(
D3D11Renderer *renderer,
FNA3D_DepthStencilState *state
) {
PackedState packedState;
D3D11_DEPTH_STENCIL_DESC desc;
D3D11_DEPTH_STENCILOP_DESC front, back;
ID3D11DepthStencilState *result;
HRESULT res;
/* Can we just reuse an existing state? */
packedState = GetPackedDepthStencilState(*state);
result = (ID3D11DepthStencilState*) PackedStateArray_Fetch(
renderer->depthStencilStateCache,
packedState
);
if (result != NULL)
{
/* The state is already cached! */
return result;
}
/* We have to make a new depth stencil state... */
desc.DepthEnable = state->depthBufferEnable;
desc.DepthWriteMask = (
state->depthBufferEnable && state->depthBufferWriteEnable ?
D3D11_DEPTH_WRITE_MASK_ALL :
D3D11_DEPTH_WRITE_MASK_ZERO
);
desc.DepthFunc = XNAToD3D_CompareFunc[
state->depthBufferFunction
];
desc.StencilEnable = state->stencilEnable;
desc.StencilReadMask = (uint8_t) state->stencilMask;
desc.StencilWriteMask = (uint8_t) state->stencilWriteMask;
front.StencilDepthFailOp = XNAToD3D_StencilOp[
state->stencilDepthBufferFail
];
front.StencilFailOp = XNAToD3D_StencilOp[
state->stencilFail
];
front.StencilFunc = XNAToD3D_CompareFunc[
state->stencilFunction
];
front.StencilPassOp = XNAToD3D_StencilOp[
state->stencilPass
];
if (state->twoSidedStencilMode)
{
back.StencilDepthFailOp = XNAToD3D_StencilOp[
state->ccwStencilDepthBufferFail
];
back.StencilFailOp = XNAToD3D_StencilOp[
state->ccwStencilFail
];
back.StencilFunc = XNAToD3D_CompareFunc[
state->ccwStencilFunction
];
back.StencilPassOp = XNAToD3D_StencilOp[
state->ccwStencilPass
];
}
else
{
back = front;
}
desc.FrontFace = front;
desc.BackFace = back;
/* Bake the state! */
res = ID3D11Device_CreateDepthStencilState(
renderer->device,
&desc,
&result
);
ERROR_CHECK_RETURN("Depth-stencil state creation failed", NULL)
PackedStateArray_Insert(
&renderer->depthStencilStateCache,
packedState,
result
);
/* Return the state! */
return result;
}
static ID3D11RasterizerState* D3D11_INTERNAL_FetchRasterizerState(
D3D11Renderer *renderer,
FNA3D_RasterizerState *state
) {
PackedState packedState;
float depthBias;
D3D11_RASTERIZER_DESC desc;
ID3D11RasterizerState *result;
HRESULT res;
depthBias = state->depthBias * XNAToD3D_DepthBiasScale[
renderer->currentDepthFormat
];
/* Can we just reuse an existing state? */
packedState = GetPackedRasterizerState(*state, depthBias);
result = (ID3D11RasterizerState*) PackedStateArray_Fetch(
renderer->rasterizerStateCache,
packedState
);
if (result != NULL)
{
/* The state is already cached! */
return result;
}
/* We have to make a new rasterizer state... */
desc.AntialiasedLineEnable = 0;
desc.CullMode = XNAToD3D_CullMode[state->cullMode];
desc.DepthBias = (int32_t) depthBias;
desc.DepthBiasClamp = D3D11_FLOAT32_MAX;
desc.DepthClipEnable = 1;
desc.FillMode = XNAToD3D_FillMode[state->fillMode];
desc.FrontCounterClockwise = 1;
desc.MultisampleEnable = state->multiSampleAntiAlias;
desc.ScissorEnable = state->scissorTestEnable;
desc.SlopeScaledDepthBias = state->slopeScaleDepthBias;
/* Bake the state! */
res = ID3D11Device_CreateRasterizerState(
renderer->device,
&desc,
&result
);
ERROR_CHECK_RETURN("Rasterizer state creation failed", NULL)
PackedStateArray_Insert(
&renderer->rasterizerStateCache,
packedState,
result
);
/* Return the state! */
return result;
}
static ID3D11SamplerState* D3D11_INTERNAL_FetchSamplerState(
D3D11Renderer *renderer,
FNA3D_SamplerState *state
) {
PackedState packedState;
D3D11_SAMPLER_DESC desc;
ID3D11SamplerState *result;
HRESULT res;
/* Can we just reuse an existing state? */
packedState = GetPackedSamplerState(*state);
result = (ID3D11SamplerState*) PackedStateArray_Fetch(
renderer->samplerStateCache,
packedState
);
if (result != NULL)
{
/* The state is already cached! */
return result;
}
/* We have to make a new sampler state... */
desc.AddressU = XNAToD3D_Wrap[state->addressU];
desc.AddressV = XNAToD3D_Wrap[state->addressV];
desc.AddressW = XNAToD3D_Wrap[state->addressW];
desc.BorderColor[0] = 1.0f;
desc.BorderColor[1] = 1.0f;
desc.BorderColor[2] = 1.0f;
desc.BorderColor[3] = 1.0f;
desc.ComparisonFunc = D3D11_COMPARISON_NEVER;
desc.Filter = XNAToD3D_Filter[state->filter];
desc.MaxAnisotropy = (uint32_t) state->maxAnisotropy;
desc.MaxLOD = D3D11_FLOAT32_MAX;
desc.MinLOD = (float) state->maxMipLevel;
desc.MipLODBias = state->mipMapLevelOfDetailBias;
/* Bake the state! */
res = ID3D11Device_CreateSamplerState(
renderer->device,
&desc,
&result
);
ERROR_CHECK_RETURN("Sampler state creation failed", NULL)
PackedStateArray_Insert(
&renderer->samplerStateCache,
packedState,
result
);
/* Return the state! */
return result;
}
static ID3D11InputLayout* D3D11_INTERNAL_FetchBindingsInputLayout(
D3D11Renderer *renderer,
FNA3D_VertexBufferBinding *bindings,
int32_t numBindings,
uint32_t *hash
) {
int32_t numElements, i, j, k, index, attribLoc, bindingsIndex;
FNA3D_VertexElementUsage usage;
uint8_t attrUse[MOJOSHADER_USAGE_TOTAL][16];
D3D11_INPUT_ELEMENT_DESC elements[16]; /* D3DCAPS9 MaxStreams <= 16 */
D3D11_INPUT_ELEMENT_DESC *d3dElement;
MOJOSHADER_d3d11Shader *vertexShader, *blah;
void *bytecode;
int32_t bytecodeLength;
HRESULT res;
ID3D11InputLayout *result;
/* We need the vertex shader... */
MOJOSHADER_d3d11GetBoundShaders(renderer->shaderContext, &vertexShader, &blah);
/* Can we just reuse an existing input layout? */
result = (ID3D11InputLayout*) PackedVertexBufferBindingsArray_Fetch(
renderer->inputLayoutCache,
bindings,
numBindings,
vertexShader,
&bindingsIndex,
hash
);
if (result != NULL)
{
/* This input layout has already been cached! */
return result;
}
/* We have to make a new input layout... */
/* There's this weird case where you can have overlapping
* vertex usage/index combinations. It seems like the first
* attrib gets priority, so whenever a duplicate attribute
* exists, give it the next available index. If that fails, we
* have to crash :/
* -flibit
*/
SDL_zero(attrUse);
/* Determine how many elements are actually in use */
numElements = 0;
for (i = 0; i < numBindings; i += 1)
{
/* Describe vertex attributes */
const FNA3D_VertexBufferBinding *binding = &bindings[i];
for (j = 0; j < binding->vertexDeclaration.elementCount; j += 1)
{
const FNA3D_VertexElement *element = &binding->vertexDeclaration.elements[j];
usage = element->vertexElementUsage;
index = element->usageIndex;
if (attrUse[usage][index])
{
index = -1;
for (k = 0; k < 16; k += 1)
{
if (!attrUse[usage][k])
{
index = k;
break;
}
}
if (index < 0)
{
FNA3D_LogError(
"Vertex usage collision!"
);
}
}
attrUse[usage][index] = 1;
attribLoc = MOJOSHADER_d3d11GetVertexAttribLocation(
vertexShader,
VertexAttribUsage(usage),
index
);
if (attribLoc == -1)
{
/* Stream not in use! */
continue;
}
numElements += 1;
d3dElement = &elements[attribLoc];
d3dElement->SemanticName = XNAToD3D_VertexAttribSemanticName[usage];
d3dElement->SemanticIndex = index;
d3dElement->Format = XNAToD3D_VertexAttribFormat[
element->vertexElementFormat
];
d3dElement->InputSlot = i;
d3dElement->AlignedByteOffset = element->offset;
d3dElement->InputSlotClass = (
binding->instanceFrequency > 0 ?
D3D11_INPUT_PER_INSTANCE_DATA :
D3D11_INPUT_PER_VERTEX_DATA
);
d3dElement->InstanceDataStepRate = (
binding->instanceFrequency > 0 ?
binding->instanceFrequency :
0
);
}
}
if (MOJOSHADER_d3d11CompileVertexShader(
renderer->shaderContext,
(unsigned long long) *hash,
elements,
numElements,
&bytecode,
&bytecodeLength
) < 0) {
FNA3D_LogError(
"%s", MOJOSHADER_d3d11GetError(renderer->shaderContext)
);
return NULL;
}
res = ID3D11Device_CreateInputLayout(
renderer->device,
elements,
numElements,
bytecode,
bytecodeLength,
&result
);
/* Check for errors now that elements is freed */
ERROR_CHECK_RETURN("Could not compile input layout", NULL)
/* Return the new input layout! */
PackedVertexBufferBindingsArray_Insert(
&renderer->inputLayoutCache,