-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsire.h
3936 lines (3159 loc) · 105 KB
/
sire.h
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
/*
SIRE - Simple Immediate Renderer
https://github.com/gennariarmando/sire
MIT License
Copyright (c) 2025 _AG
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// Usage: To use with a renderer define SIRE_XX before including this header file or uncomment below:
//#define SIRE_INCLUDE_MINIMAL_DEPENDECIES
//#define SIRE_DX9
//#define SIRE_INCLUDE_DX9
//
//#define SIRE_DX10
//#define SIRE_INCLUDE_DX10
//
//#define SIRE_DX11
//#define SIRE_INCLUDE_DX11
//
//#define SIRE_DX11ON12
//#define SIRE_INCLUDE_DX11ON12
//
//#define SIRE_DX12
//#define SIRE_INCLUDE_DX12
//
//#define SIRE_OPENGL
//#define SIRE_INCLUDE_OPENGL
//#define SIRE_VULKAN
//#define SIRE_INCLUDE_VULKAN
#pragma once
#ifdef SIRE_INCLUDE_DX9
#include <d3d9.h>
#include <d3dx9.h>
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
#include <DirectXMath.h>
#endif
#ifdef SIRE_INCLUDE_DX10
#include <d3d10_1.h>
#include <d3d10.h>
#include <dxgi.h>
#include <DirectXMath.h>
#pragma comment(lib, "d3d10_1.lib")
#pragma comment(lib, "d3d10.lib")
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "d3dcompiler.lib")
#endif
#ifdef SIRE_INCLUDE_DX11
#include <d3d11.h>
#include <d3dcompiler.h>
#include <DirectXMath.h>
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "d3dcompiler.lib")
#ifdef SIRE_DX11ON12
#include <wrl.h>
#include <d3d11on12.h>
#include <dxgi1_4.h>
using namespace Microsoft::WRL;
using namespace DirectX;
#endif
#endif
#ifdef SIRE_INCLUDE_DX12
#include <d3d12.h>
#include <dxgi.h>
#include <DirectXMath.h>
#pragma comment(lib, "d3d12.lib")
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "d3dcompiler.lib")
#endif
#ifdef SIRE_INCLUDE_OPENGL
#define SOGL_MAJOR_VERSION 4
#define SOGL_MINOR_VERSION 5
#define SOGL_OVR_multiview
#define SOGL_KHR_parallel_shader_compile
#define SOGL_IMPLEMENTATION_WIN32
#include "glad/include/glad/glad.h"
#include "glad/include/KHR/khrplatform.h"
#pragma comment(lib, "opengl32.lib")
#endif
#ifdef SIRE_INCLUDE_VULKAN
#define VK_USE_PLATFORM_WIN32_KHR
#include <vulkan/vulkan.h>
#pragma comment(lib, "vulkan-1.lib")
#endif
#ifdef SIRE_INCLUDE_MINIMAL_DEPENDECIES
#include <iostream>
#include <vector>
#include <array>
#include <windef.h>
#endif
#ifdef SIRE_DX11ON12
namespace d3d11on12
{
static inline bool isD3D11on12 = false;
static inline UINT bufferIndex = 0;
static inline UINT bufferCount = 0;
static inline DXGI_SWAP_CHAIN_DESC swapChainDesc;
static inline Microsoft::WRL::ComPtr<ID3D12Device> d3d12Device = nullptr;
static inline Microsoft::WRL::ComPtr<ID3D12CommandQueue> commandQueue = nullptr;
static inline std::vector<Microsoft::WRL::ComPtr<ID3D12Resource>> d3d12RenderTargets;
static inline std::vector<Microsoft::WRL::ComPtr<ID3D11RenderTargetView>> d3d11RenderTargetViews;
static inline std::vector<Microsoft::WRL::ComPtr<ID3D11Resource>> d3d11WrappedBackBuffers;
static inline Microsoft::WRL::ComPtr<IDXGISwapChain3> swapChain3 = nullptr;
static inline Microsoft::WRL::ComPtr<ID3D11Device> d3d11Device = nullptr;
static inline Microsoft::WRL::ComPtr<ID3D11On12Device> d3d11On12Device = nullptr;
static inline Microsoft::WRL::ComPtr<ID3D11DeviceContext> d3d11Context = nullptr;
static inline HRESULT RetrieveD3DDeviceFromSwapChain(IDXGISwapChain* pSwapChain)
{
HRESULT hr = pSwapChain->GetDevice(IID_PPV_ARGS(d3d11Device.GetAddressOf()));
if (SUCCEEDED(hr)) {
isD3D11on12 = false;
return hr;
}
hr = pSwapChain->GetDevice(IID_PPV_ARGS(d3d12Device.GetAddressOf()));
if (SUCCEEDED(hr)) {
isD3D11on12 = true;
}
return hr;
}
static inline BOOL WaitForCommandQueueIfRunningD3D12()
{
if (isD3D11on12 && commandQueue.Get() == nullptr) {
return TRUE;
}
return FALSE;
}
static inline HRESULT GetSwapChainDescription(IDXGISwapChain* pSwapChain)
{
ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
HRESULT hr = pSwapChain->GetDesc(&swapChainDesc);
if (SUCCEEDED(hr)) {
bufferCount = isD3D11on12 ? swapChainDesc.BufferCount : 1;
}
return hr;
}
static inline HRESULT CreateD3D11On12Device()
{
D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;
HRESULT hr = D3D11On12CreateDevice(
d3d12Device.Get(),
NULL,
&featureLevel,
1,
reinterpret_cast<IUnknown**>(commandQueue.GetAddressOf()),
1,
0,
d3d11Device.GetAddressOf(),
d3d11Context.GetAddressOf(),
nullptr);
if (FAILED(hr)) return hr;
return d3d11Device.As(&d3d11On12Device);
}
static inline HRESULT CreateD3D12RtvHeap(_Outptr_ ID3D12DescriptorHeap** ppRtvHeap)
{
D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = {};
rtvHeapDesc.NumDescriptors = bufferCount;
rtvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
rtvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
return d3d12Device->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(ppRtvHeap));
}
static inline HRESULT CreateD3D12RenderTargetView(IDXGISwapChain* pSwapChain, UINT bufferIndex, D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle)
{
HRESULT hr = pSwapChain->GetBuffer(bufferIndex, IID_PPV_ARGS(&d3d12RenderTargets[bufferIndex]));
if (FAILED(hr)) return hr;
d3d12Device->CreateRenderTargetView(d3d12RenderTargets[bufferIndex].Get(), nullptr, rtvHandle);
return S_OK;
}
static inline HRESULT CreateD3D11WrappedBackBuffer(UINT bufferIndex)
{
D3D11_RESOURCE_FLAGS d3d11Flags = { D3D11_BIND_RENDER_TARGET };
return d3d11On12Device->CreateWrappedResource(
d3d12RenderTargets[bufferIndex].Get(),
&d3d11Flags,
D3D12_RESOURCE_STATE_RENDER_TARGET,
D3D12_RESOURCE_STATE_PRESENT,
IID_PPV_ARGS(&d3d11WrappedBackBuffers[bufferIndex]));
}
static inline HRESULT CreateD3D11RenderTargetViewWithWrappedBackBuffer(UINT bufferIndex)
{
return d3d11Device->CreateRenderTargetView(
d3d11WrappedBackBuffers[bufferIndex].Get(),
nullptr,
d3d11RenderTargetViews[bufferIndex].GetAddressOf());
}
static inline HRESULT CreateD3D12Buffers(IDXGISwapChain* pSwapChain)
{
d3d12RenderTargets.resize(bufferCount);
d3d11WrappedBackBuffers.resize(bufferCount);
d3d11RenderTargetViews.resize(bufferCount);
ComPtr<ID3D12DescriptorHeap> rtvHeap;
HRESULT hr = CreateD3D12RtvHeap(rtvHeap.GetAddressOf());
if (FAILED(hr)) return hr;
D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle(rtvHeap->GetCPUDescriptorHandleForHeapStart());
UINT rtvDescriptorSize = d3d12Device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
for (UINT i = 0; i < bufferCount; i++) {
hr = CreateD3D12RenderTargetView(pSwapChain, i, rtvHandle);
if (FAILED(hr)) return hr;
hr = CreateD3D11WrappedBackBuffer(i);
if (FAILED(hr)) return hr;
hr = CreateD3D11RenderTargetViewWithWrappedBackBuffer(i);
if (FAILED(hr)) return hr;
rtvHandle.ptr += rtvDescriptorSize;
}
return S_OK;
}
static inline HRESULT InitD3D12(IDXGISwapChain* pSwapChain)
{
HRESULT hr = CreateD3D11On12Device();
if (FAILED(hr)) return hr;
hr = pSwapChain->QueryInterface(IID_PPV_ARGS(&swapChain3));
if (FAILED(hr)) return hr;
return CreateD3D12Buffers(pSwapChain);
}
static inline BOOL InitD3D11on12Resources(IDXGISwapChain* swapChain)
{
HRESULT hr = RetrieveD3DDeviceFromSwapChain(swapChain);
if (FAILED(hr)) return FALSE;
if (WaitForCommandQueueIfRunningD3D12()) return FALSE;
hr = GetSwapChainDescription(swapChain);
if (FAILED(hr)) return FALSE;
hr = InitD3D12(swapChain);
if (FAILED(hr)) return FALSE;
return TRUE;
}
static inline void ReleaseViewsBuffersAndContext()
{
for (int i = 0; i < bufferCount; i++)
{
if (d3d12Device.Get() == nullptr)
{
d3d11RenderTargetViews[i].ReleaseAndGetAddressOf();
}
else
{
d3d11RenderTargetViews[i].ReleaseAndGetAddressOf();
d3d12RenderTargets[i].ReleaseAndGetAddressOf();
d3d11WrappedBackBuffers[i].ReleaseAndGetAddressOf();
}
}
if (d3d11Context.Get() != nullptr)
{
d3d11Context->Flush();
d3d11Context.Reset();
}
if (d3d11On12Device)
d3d11On12Device.Reset();
if (swapChain3)
swapChain3.Reset();
if (d3d11Device)
d3d11Device.Reset();
// Reset state
bufferCount = 0;
bufferIndex = 0;
isD3D11on12 = false;
}
static inline void SetCommandQueue(ID3D12CommandQueue* cq)
{
commandQueue = cq;
}
}
#endif
template <typename T>
class SirePtr {
public:
SirePtr() {
ptr = std::make_unique<T>();
}
SirePtr(T* p) : ptr(p) {}
~SirePtr() {
Reset();
}
SirePtr(const SirePtr<T>& other) = delete;
SirePtr<T>& operator=(const SirePtr<T>& other) = delete;
SirePtr(SirePtr<T>&& other) noexcept : ptr(nullptr) {
*this = std::move(other);
}
SirePtr<T>& operator=(SirePtr<T>&& other) noexcept {
if (this != &other) {
Reset();
ptr = std::move(other.ptr);
other.ptr = nullptr;
}
return *this;
}
T* Get() const {
return ptr.get();
}
T** GetAddressOf() {
return ptr.get();
}
T* operator->() const {
return ptr.get();
}
operator bool() const {
return (ptr != nullptr);
}
void Reset() {
Release();
ptr.reset();
}
void Reset(T* p) {
Release();
ptr.reset(p);
}
void Release() {
if (ptr) {
ptr->Release();
ptr = nullptr;
}
}
private:
std::unique_ptr<T> ptr;
};
class Sire {
public:
enum eSireProjection {
SIRE_PROJ_NONE,
SIRE_PROJ_ORTHOGRAPHIC,
SIRE_PROJ_PERSPECTIVE,
};
enum eSireRenderState {
SIRE_BLEND_ALPHATESTENABLE,
SIRE_BLEND_SRCBLEND,
SIRE_BLEND_DESTBLEND,
SIRE_BLEND_BLENDOP,
SIRE_BLEND_SRCBLENDALPHA,
SIRE_BLEND_DESTBLENDALPHA,
SIRE_BLEND_BLENDOPALPHA,
SIRE_BLEND_WRITEMASK,
SIRE_BLEND_CULLMODE,
SIRE_BLEND_FILLMODE,
SIRE_BLEND_STENCILENABLE,
SIRE_BLEND_COLORWRITEENABLE,
};
enum eSireFillMode {
SIRE_FILL_WIREFRAME = 2,
SIRE_FILL_SOLID = 3
};
enum eSireCull {
SIRE_CULL_NONE = 1,
SIRE_CULL_FRONT = 2,
SIRE_CULL_BACK = 3
};
enum eSireBlend {
SIRE_BLEND_ZERO = 1,
SIRE_BLEND_ONE = 2,
SIRE_BLEND_SRC_COLOR = 3,
SIRE_BLEND_INV_SRC_COLOR = 4,
SIRE_BLEND_SRC_ALPHA = 5,
SIRE_BLEND_INV_SRC_ALPHA = 6,
SIRE_BLEND_DEST_ALPHA = 7,
SIRE_BLEND_INV_DEST_ALPHA = 8,
SIRE_BLEND_DEST_COLOR = 9,
SIRE_BLEND_INV_DEST_COLOR = 10,
SIRE_BLEND_SRC_ALPHA_SAT = 11,
SIRE_BLEND_BLEND_FACTOR = 14,
SIRE_BLEND_INV_BLEND_FACTOR = 15,
SIRE_BLEND_SRC1_COLOR = 16,
SIRE_BLEND_INV_SRC1_COLOR = 17,
SIRE_BLEND_SRC1_ALPHA = 18,
SIRE_BLEND_INV_SRC1_ALPHA = 19
};
enum eSireBlendOp {
SIRE_BLEND_OP_ADD = 1,
SIRE_BLEND_OP_SUBTRACT = 2,
SIRE_BLEND_OP_REV_SUBTRACT = 3,
SIRE_BLEND_OP_MIN = 4,
SIRE_BLEND_OP_MAX = 5
};
enum eSireRenderer {
SIRE_RENDERER_NULL = -1,
SIRE_RENDERER_DX9,
SIRE_RENDERER_DX10,
SIRE_RENDERER_DX11,
SIRE_RENDERER_DX12,
SIRE_RENDERER_OPENGL,
SIRE_RENDERER_VULKAN,
SIRE_NUM_RENDERERS,
};
enum eSireColorWriteEnable {
SIRE_COLOR_WRITE_ENABLE_RED = 1,
SIRE_COLOR_WRITE_ENABLE_GREEN = 2,
SIRE_COLOR_WRITE_ENABLE_BLUE = 4,
SIRE_COLOR_WRITE_ENABLE_ALPHA = 8,
SIRE_COLOR_WRITE_ENABLE_ALL = (((SIRE_COLOR_WRITE_ENABLE_RED | SIRE_COLOR_WRITE_ENABLE_GREEN) | SIRE_COLOR_WRITE_ENABLE_BLUE) | SIRE_COLOR_WRITE_ENABLE_ALPHA)
};
enum eSirePrimitiveType {
SIRE_POINT,
SIRE_LINE,
SIRE_TRIANGLE,
};
struct tSireTexture2D {
int32_t w;
int32_t h;
int32_t format;
int32_t swapColors;
struct tSirePtrsHolder {
uintptr_t* texture;
uintptr_t* surface;
eSireRenderer owner;
tSirePtrsHolder() {
owner = GetCurrentRenderer();
texture = nullptr;
surface = nullptr;
}
~tSirePtrsHolder() {
Release();
}
void Release() {
if (GetCurrentRenderer() == owner) {
switch (GetCurrentRenderer()) {
#ifdef SIRE_DX9
case SIRE_RENDERER_DX9: {
IDirect3DTexture9* tex = reinterpret_cast<IDirect3DTexture9*>(texture);
IDirect3DSurface9* surf = reinterpret_cast<IDirect3DSurface9*>(surface);
Sire::Release(tex);
Sire::Release(surf);
} break;
#endif
#ifdef SIRE_DX10
case SIRE_RENDERER_DX10: {
ID3D10ShaderResourceView* tex = reinterpret_cast<ID3D10ShaderResourceView*>(texture);
ID3D10Texture2D* surf = reinterpret_cast<ID3D10Texture2D*>(surface);
Sire::Release(tex);
Sire::Release(surf);
} break;
#endif
#ifdef SIRE_DX11
case SIRE_RENDERER_DX11: {
ID3D11ShaderResourceView* tex = reinterpret_cast<ID3D11ShaderResourceView*>(texture);
ID3D11Texture2D* surf = reinterpret_cast<ID3D11Texture2D*>(surface);
Sire::Release(tex);
Sire::Release(surf);
} break;
#endif
}
}
texture = nullptr;
surface = nullptr;
}
};
tSirePtrsHolder ptrs;
tSireTexture2D() {
w = 0;
h = 0;
format = 0;
swapColors = 0;
}
~tSireTexture2D() {
}
void Set(int32_t width, int32_t height, int32_t format, uintptr_t* tex, uintptr_t* surf) {
this->w = width;
this->h = height;
this->format = format;
this->ptrs.texture = tex;
this->ptrs.surface = surf;
}
void Release() {
}
};
struct tSireInt2 {
int32_t x, y;
};
struct tSireFloat2 {
float x, y;
};
struct tSireFloat3 {
float x, y, z;
};
struct tSireFloat4 {
float x, y, z, w;
};
private:
struct tRenderState {
uint8_t blendEnable;
uint8_t srcBlend;
uint8_t dstBlend;
uint8_t blendop;
uint8_t srcBlendAlpha;
uint8_t destBlendAlpha;
uint8_t blendOpAlpha;
uint8_t renderTargetWriteMask;
uint8_t cullMode;
uint8_t fillMode;
uint8_t stencilEnable;
uint32_t sampleMask;
};
struct alignas(16) tSireMatrix {
union {
struct {
float _11, _12, _13, _14;
float _21, _22, _23, _24;
float _31, _32, _33, _34;
float _41, _42, _43, _44;
};
float m[4][4];
};
void Identity() {
_11 = 1.0f;
_12 = 0.0f;
_13 = 0.0f;
_14 = 0.0f;
_21 = 0.0f;
_22 = 1.0f;
_23 = 0.0f;
_24 = 0.0f;
_31 = 0.0f;
_32 = 0.0f;
_33 = 1.0f;
_34 = 0.0f;
_41 = 0.0f;
_42 = 0.0f;
_43 = 0.0f;
_44 = 1.0f;
}
void Orthographic(float x, float y, float width, float height, float nearPlane, float farPlane) {
_11 = 2.0f / width;
_12 = 0.0f;
_13 = 0.0f;
_14 = 0.0f;
_21 = 0.0f;
_22 = -2.0f / height;
_23 = 0.0f;
_24 = 0.0f;
_31 = 0.0f;
_32 = 0.0f;
_33 = 1.0f / (farPlane - nearPlane);
_34 = 0.0f;
_41 = -(x + x + width) / width;
_42 = (y + y + height) / height;
_43 = -nearPlane / (farPlane - nearPlane);
_44 = 1.0f;
}
void Perspective(float fov, float aspectRatio, float nearPlane, float farPlane) {
float f = tanf(fov * 0.5f);
float invAspectRatio = 1.0f / aspectRatio;
_11 = 1.0f / (f * invAspectRatio);
_12 = 0.0f;
_13 = 0.0f;
_14 = 0.0f;
_21 = 0.0f;
_22 = 1.0f / f;
_23 = 0.0f;
_24 = 0.0f;
_31 = 0.0f;
_32 = 0.0f;
_33 = farPlane / (farPlane - nearPlane);
_34 = 1.0f;
_41 = 0.0f;
_42 = 0.0f;
_43 = (-farPlane * nearPlane) / (farPlane - nearPlane);
_44 = 0.0f;
}
void Transpose() {
std::swap(_12, _21);
std::swap(_13, _31);
std::swap(_14, _41);
std::swap(_23, _32);
std::swap(_24, _42);
std::swap(_34, _43);
}
#ifdef SIRE_DX9
D3DXMATRIX ToD3DXMATRIX() {
D3DXMATRIX out = {};
out._11 = _11;
out._12 = _12;
out._13 = _13;
out._14 = _14;
out._21 = _21;
out._22 = _22;
out._23 = _23;
out._24 = _24;
out._31 = _31;
out._32 = _32;
out._33 = _33;
out._34 = _34;
out._41 = _41;
out._42 = _42;
out._43 = _43;
out._44 = _44;
return out;
}
#endif
std::array<float, 16> ToFloatArray() {
std::array<float, 16> out = {};
out[0] = _11;
out[1] = _21;
out[2] = _31;
out[3] = _41;
out[4] = _12;
out[5] = _22;
out[6] = _32;
out[7] = _42;
out[8] = _13;
out[9] = _23;
out[10] = _33;
out[11] = _43;
out[12] = _14;
out[13] = _24;
out[14] = _34;
out[15] = _44;
return out;
}
};
struct tConstBuff {
tSireMatrix matrix;
int32_t hasTex;
int32_t hasMask;
int32_t swapColors;
};
struct tVertex {
tSireFloat3 pos;
tSireFloat4 col;
tSireFloat2 uv0;
tSireFloat2 uv1;
};
struct tVertexLegacy {
tSireFloat3 pos;
uint32_t diff;
tSireFloat2 uv0;
tSireFloat2 uv1;
};
struct tSireViewport {
float x;
float y;
float w;
float h;
float mind;
float maxd;
};
struct tSireShared {
tSireViewport viewport;
float fov;
float nearPlane;
float farPlane;
tRenderState renderStates;
tSireShared() {
viewport = { 0.0f, 0.0f, 640.0f, 480.0f, 0.0f, 1.0f };
fov = 60.0f;
nearPlane = 0.0f;
farPlane = 1.0f;
renderStates = { 0, SIRE_BLEND_SRC_ALPHA, SIRE_BLEND_INV_SRC_ALPHA, SIRE_BLEND_OP_ADD, SIRE_BLEND_ONE, SIRE_BLEND_ZERO, SIRE_BLEND_OP_ADD, SIRE_COLOR_WRITE_ENABLE_ALL, SIRE_CULL_NONE, SIRE_FILL_SOLID };
}
~tSireShared() {
}
};
struct SireRenderer {
bool initialised;
HWND hWnd;
uint32_t textureFormat; // TODO: make global enum formats.
virtual bool IsRendererActive() { return false; }
virtual void Init(uintptr_t* ptr) {}
virtual void Shutdown() {}
virtual void Begin() {}
virtual void End() {}
virtual void SetVertices(tVertex const& v) {}
virtual void SetRenderStates(tRenderState const& s) {}
virtual void SetViewport(tSireViewport const& v) {}
virtual void CopyResource(uintptr_t* dst, uintptr_t* src) {}
virtual void SetTexture(uintptr_t* tex, uintptr_t* mask) {}
virtual HWND GetWindow() { return hWnd; }
virtual uint8_t* Lock(void* ptr) { return nullptr; }
virtual void Unlock(void* ptr) {}
virtual void SetTextureFormat(uint32_t format) { textureFormat = format; }
virtual uint32_t GetTextureFormat() { return textureFormat; }
virtual void SetPixelShader(uintptr_t* ps) {}
virtual void SetVertexShader(uintptr_t* vs) {}
virtual uintptr_t* CreatePixelShader(std::string const& shaderCode, const char* targetVersion = "ps_3_0") { return nullptr; }
virtual uintptr_t* CreateVertexShader(std::string const& shaderCode, const char* targetVersion = "vs_3_0") { return nullptr; }
SireRenderer() {
initialised = false;
hWnd = nullptr;
textureFormat = 0;
}
bool operator==(const SireRenderer& other) const {
return false;
}
};
#ifdef SIRE_DX9
struct SireDirectX9 : SireRenderer {
IDirect3DDevice9* dev;
IDirect3DVertexBuffer9* vb;
IDirect3DIndexBuffer9* ib;
IDirect3DTexture9* tex;
IDirect3DTexture9* mask;
IDirect3DVertexDeclaration9* vertexDeclaration;
IDirect3DPixelShader9* pixelShader;
IDirect3DVertexShader9* vertexShader;
ID3DXConstantTable* pct;
ID3DXConstantTable* vct;
IDirect3DPixelShader9* internalPixelShader;
IDirect3DVertexShader9* internalVertexShader;
std::vector<tVertexLegacy> verticesLegacy;
IDirect3DStateBlock9* stateBlock;
SireDirectX9() : SireRenderer() {
dev = nullptr;
vb = nullptr;
ib = nullptr;
tex = nullptr;
mask = nullptr;
vertexDeclaration = nullptr;
pixelShader = nullptr;
vertexShader = nullptr;
pct = nullptr;
vct = nullptr;
internalPixelShader = nullptr;
internalVertexShader = nullptr;
verticesLegacy = {};
stateBlock = nullptr;
textureFormat = D3DFMT_A8R8G8B8;
}
// Start virtual override
bool IsRendererActive() override {
if (initialised && dev) {
HRESULT hr = dev->TestCooperativeLevel();
return SUCCEEDED(hr);
}
return false;
}
void Init(uintptr_t* d) override {
if (initialised)
return;
dev = reinterpret_cast<IDirect3DDevice9*>(d);
IDirect3DSwapChain9* swapchain = nullptr;
dev->GetSwapChain(0, &swapchain);
D3DPRESENT_PARAMETERS presentParams;
ZeroMemory(&presentParams, sizeof(presentParams));
swapchain->GetPresentParameters(&presentParams);
hWnd = presentParams.hDeviceWindow;
dev->CreateVertexBuffer(sizeof(tVertexLegacy) * 65536, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY,
0, D3DPOOL_DEFAULT, &vb, nullptr);
dev->CreateIndexBuffer(sizeof(uint16_t) * 65536, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16, D3DPOOL_DEFAULT, &ib, nullptr);
// Init shaders
ID3DXBuffer* VS = CompileShader(hlslShader2_0, "VShader", "vs_2_0");
ID3DXBuffer* PS = CompileShader(hlslShader2_0, "PShader", "ps_2_0");
internalVertexShader = CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize());
internalPixelShader = CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize());
D3DXGetShaderConstantTable(static_cast<const DWORD*>(VS->GetBufferPointer()), &pct);
D3DXGetShaderConstantTable(static_cast<const DWORD*>(PS->GetBufferPointer()), &vct);
Release(VS);
Release(PS);
D3DVERTEXELEMENT9 vertexElements[] = {
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, sizeof(float[3]), D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
{ 0, sizeof(float[3]) + sizeof(D3DCOLOR), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
{ 0, sizeof(float[5]) + sizeof(D3DCOLOR), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
D3DDECL_END()
};
dev->CreateVertexDeclaration(vertexElements, &vertexDeclaration);
initialised = true;
}
void Shutdown() override {
if (!initialised)
return;
Release(vb);
Release(ib);
Release(vertexDeclaration);
Release(pct);
Release(vct);
tex = nullptr;
mask = nullptr;
dev = nullptr;
initialised = false;
}
void Begin() override {
verticesLegacy.clear();
dev->CreateStateBlock(D3DSBT_ALL, &stateBlock);
stateBlock->Capture();
}
void End() override {
// Fallback to internal shaders if unset.
if (!pixelShader && internalPixelShader)
pixelShader = internalPixelShader;
if (!vertexShader && internalVertexShader)
vertexShader = internalVertexShader;
void* out = nullptr;
vb->Lock(0, 0, (void**)&out, 0);
memcpy(out, verticesLegacy.data(), verticesLegacy.size() * sizeof(tVertexLegacy));
vb->Unlock();
ib->Lock(0, 0, (void**)&out, 0);
memcpy(out, indices.data(), indices.size() * sizeof(uint16_t));
ib->Unlock();
tConstBuff tempcb = cb;
D3DXMATRIX m = tempcb.matrix.ToD3DXMATRIX();
pct->SetMatrix(dev, pct->GetConstantByName(NULL, "proj"), &m);
pct->SetMatrix(dev, vct->GetConstantByName(NULL, "proj"), &m);
pct->SetInt(dev, pct->GetConstantByName(NULL, "hasTex"), tempcb.hasTex);
pct->SetInt(dev, vct->GetConstantByName(NULL, "hasTex"), tempcb.hasTex);
pct->SetInt(dev, pct->GetConstantByName(NULL, "hasMask"), tempcb.hasMask);
pct->SetInt(dev, vct->GetConstantByName(NULL, "hasMask"), tempcb.hasMask);
pct->SetInt(dev, pct->GetConstantByName(NULL, "swapColors"), tempcb.swapColors);
pct->SetInt(dev, vct->GetConstantByName(NULL, "swapColors"), tempcb.swapColors);
auto renderTarget = GetRenderTarget();
dev->SetRenderTarget(0, renderTarget);
dev->SetVertexDeclaration(vertexDeclaration);
dev->SetTexture(0, tex);
dev->SetTexture(1, mask);
dev->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
dev->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
dev->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
dev->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
dev->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
dev->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
dev->SetSamplerState(1, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
dev->SetSamplerState(1, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
dev->SetRenderState(D3DRS_WRAP0, 0);
dev->SetRenderState(D3DRS_WRAP1, 0);
dev->SetPixelShader(pixelShader);
dev->SetVertexShader(vertexShader);
dev->SetStreamSource(0, vb, 0, sizeof(tVertexLegacy));
dev->SetIndices(ib);
D3DPRIMITIVETYPE type = D3DPT_POINTLIST;
switch (primitiveType) {
case SIRE_LINE:
type = D3DPT_LINELIST;
break;
case SIRE_POINT:
type = D3DPT_POINTLIST;
break;