-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathwander.cpp
1504 lines (1224 loc) · 42.1 KB
/
wander.cpp
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
#ifdef _WIN32
#define _SILENCE_ALL_CXX20_DEPRECATION_WARNINGS
#define NOMINMAX
#endif
#include "wander_lib.h"
#include <array>
#include <cassert>
#include <sstream>
#include <string>
#include <locale>
#include <algorithm>
#include <string_view>
//#include "wasmtime.h"
#ifndef __EMSCRIPTEN__
#include <gl3w.c>
#else
#include <emscripten.h>
#include <GLES3/gl3.h>
#include <GLES3/gl2ext.h>
#include <GLES3/gl3platform.h>
#endif
#ifdef _WIN32
#include <d3d11.h>
#include <d3d11_1.h>
#pragma comment(lib, "OpenGL32.lib")
#endif
using namespace wander;
#ifndef _WIN32
#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING
#include <codecvt>
// convert wstring to UTF-8 string
std::string wstring_to_utf8 (const std::wstring& str)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
return myconv.to_bytes(str);
}
int _wfopen_s(FILE **f, const wchar_t *name, const wchar_t *mode) {
int ret = 0;
//assert(f);
*f = fopen(wstring_to_utf8(name).c_str(), wstring_to_utf8(mode).c_str());
/* Can't be sure about 1-to-1 mapping of errno and MS' errno_t */
if (!*f)
ret = errno;
return ret;
}
#endif
#if defined(RLT_RIVE) && defined(_WIN32)
#pragma comment(lib, "rive_pls_renderer.lib")
#pragma comment(lib, "rive.lib")
#pragma comment(lib, "rive_sheenbidi.lib")
#pragma comment(lib, "rive_harfbuzz.lib")
#pragma comment(lib, "rive_decoders.lib")
#pragma comment(lib, "libpng.lib")
#pragma comment(lib, "zlib.lib")
#include "rive/pls/pls_render_context.hpp"
#include "rive/pls/pls_renderer.hpp"
#include "rive/pls/d3d/pls_render_context_d3d_impl.hpp"
#include "rive/pls/d3d/d3d11.hpp"
using namespace rive;
using namespace rive::pls;
#pragma once
#define SAFE_RELEASE(x) \
{ \
if (x) \
{ \
x->Release(); \
x = 0; \
} \
}
// used to ensure that TXAA has no aftereffects
class D3D11SavedState
{
public:
D3D11SavedState(ID3D11DeviceContext *context) : pd3dContext(context)
{
pd3dContext->AddRef();
SaveState();
}
D3D11SavedState(ID3D11Device *device)
{
device->GetImmediateContext(&pd3dContext);
SaveState();
}
~D3D11SavedState()
{
// first set NULL RTs in case we have bounds targets that need to go back to SRVs
ID3D11RenderTargetView *pNULLRTVs[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT];
for (int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
pNULLRTVs[i] = NULL;
pd3dContext->OMSetRenderTargets(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, pNULLRTVs, NULL);
// Then restore state
pd3dContext->VSSetShaderResources(0, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, pVSSRVs);
pd3dContext->PSSetShaderResources(0, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, pPSSRVs);
pd3dContext->GSSetShaderResources(0, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, pGSSRVs);
pd3dContext->HSSetShaderResources(0, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, pHSSRVs);
pd3dContext->DSSetShaderResources(0, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, pDSSRVs);
pd3dContext->CSSetShaderResources(0, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, pCSSRVs);
UINT NumViewPorts = D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE;
pd3dContext->RSSetViewports(NumViewPorts, Viewports);
pd3dContext->VSSetShader(pVertexShader, NULL, NULL);
pd3dContext->PSSetShader(pPixelShader, NULL, NULL);
pd3dContext->GSSetShader(pGeometryShader, NULL, NULL);
pd3dContext->HSSetShader(pHullShader, NULL, NULL);
pd3dContext->DSSetShader(pDomainShader, NULL, NULL);
pd3dContext->CSSetShader(pComputeShader, NULL, NULL);
pd3dContext->VSSetSamplers(0, D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, pVSSamplers);
pd3dContext->PSSetSamplers(0, D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, pPSSamplers);
pd3dContext->GSSetSamplers(0, D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, pGSSamplers);
pd3dContext->HSSetSamplers(0, D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, pHSSamplers);
pd3dContext->DSSetSamplers(0, D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, pDSSamplers);
pd3dContext->CSSetSamplers(0, D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, pCSSamplers);
pd3dContext->VSSetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, pVSConstantBuffers);
pd3dContext->PSSetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, pPSConstantBuffers);
pd3dContext->GSSetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, pGSConstantBuffers);
pd3dContext->HSSetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, pHSConstantBuffers);
pd3dContext->DSSetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, pDSConstantBuffers);
pd3dContext->CSSetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, pCSConstantBuffers);
pd3dContext->RSSetState(pRasterizer);
pd3dContext->OMSetDepthStencilState(pDepthStencil, NULL);
pd3dContext->OMSetBlendState(pBlendState, BlendFactors, BlendSampleMask);
pd3dContext->IASetInputLayout(pInputLayout);
pd3dContext->IASetPrimitiveTopology(PrimitiveToplogy);
// pd3dContext->OMSetRenderTargetsAndUnorderedAccessViews(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, pRTVs, pDSV,
// D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, D3D11_PS_CS_UAV_REGISTER_COUNT, pUAVs, 0);
pd3dContext->OMSetRenderTargetsAndUnorderedAccessViews(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, pRTVs, pDSV, 0,
0, pUAVs, 0);
SAFE_RELEASE(pd3dContext); // we added a manual ref
for (int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
SAFE_RELEASE(pRTVs[i]);
SAFE_RELEASE(pDSV);
// for (int i = 0; i < D3D11_PS_CS_UAV_REGISTER_COUNT; i++)
// SAFE_RELEASE(pUAVs[i]);
for (int i = 0; i < D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT; i++)
{
SAFE_RELEASE(pVSSRVs[i]);
SAFE_RELEASE(pPSSRVs[i]);
SAFE_RELEASE(pGSSRVs[i]);
SAFE_RELEASE(pHSSRVs[i]);
SAFE_RELEASE(pDSSRVs[i]);
SAFE_RELEASE(pCSSRVs[i]);
}
SAFE_RELEASE(pVertexShader);
SAFE_RELEASE(pPixelShader);
SAFE_RELEASE(pGeometryShader);
SAFE_RELEASE(pHullShader);
SAFE_RELEASE(pDomainShader);
SAFE_RELEASE(pComputeShader);
for (int i = 0; i < D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT; i++)
{
SAFE_RELEASE(pPSSamplers[i]);
SAFE_RELEASE(pVSSamplers[i]);
SAFE_RELEASE(pGSSamplers[i]);
SAFE_RELEASE(pHSSamplers[i]);
SAFE_RELEASE(pDSSamplers[i]);
SAFE_RELEASE(pCSSamplers[i]);
}
for (int i = 0; i < D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT; i++)
{
SAFE_RELEASE(pVSConstantBuffers[i]);
SAFE_RELEASE(pPSConstantBuffers[i]);
SAFE_RELEASE(pGSConstantBuffers[i]);
SAFE_RELEASE(pHSConstantBuffers[i]);
SAFE_RELEASE(pDSConstantBuffers[i]);
SAFE_RELEASE(pCSConstantBuffers[i]);
}
for (int i = 0; i < D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT; i++)
SAFE_RELEASE(pPSConstantBuffers[i]);
SAFE_RELEASE(pRasterizer);
SAFE_RELEASE(pDepthStencil);
SAFE_RELEASE(pBlendState);
SAFE_RELEASE(pInputLayout);
}
private:
void SaveState()
{
pd3dContext->OMGetRenderTargetsAndUnorderedAccessViews(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, pRTVs, &pDSV, 0,
0, pUAVs);
// pd3dContext->OMGetRenderTargetsAndUnorderedAccessViews(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, pRTVs, &pDSV,
// D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, D3D11_PS_CS_UAV_REGISTER_COUNT, pUAVs);
pd3dContext->VSGetShaderResources(0, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, pVSSRVs);
pd3dContext->PSGetShaderResources(0, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, pPSSRVs);
pd3dContext->GSGetShaderResources(0, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, pGSSRVs);
pd3dContext->HSGetShaderResources(0, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, pHSSRVs);
pd3dContext->DSGetShaderResources(0, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, pDSSRVs);
pd3dContext->CSGetShaderResources(0, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, pCSSRVs);
UINT NumViewPorts = D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE;
pd3dContext->RSGetViewports(&NumViewPorts, Viewports);
pd3dContext->VSGetShader(&pVertexShader, NULL, NULL);
pd3dContext->PSGetShader(&pPixelShader, NULL, NULL);
pd3dContext->GSGetShader(&pGeometryShader, NULL, NULL);
pd3dContext->HSGetShader(&pHullShader, NULL, NULL);
pd3dContext->DSGetShader(&pDomainShader, NULL, NULL);
pd3dContext->CSGetShader(&pComputeShader, NULL, NULL);
pd3dContext->VSGetSamplers(0, D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, pVSSamplers);
pd3dContext->PSGetSamplers(0, D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, pPSSamplers);
pd3dContext->GSGetSamplers(0, D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, pGSSamplers);
pd3dContext->HSGetSamplers(0, D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, pHSSamplers);
pd3dContext->DSGetSamplers(0, D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, pDSSamplers);
pd3dContext->CSGetSamplers(0, D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, pCSSamplers);
pd3dContext->VSGetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, pVSConstantBuffers);
pd3dContext->PSGetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, pPSConstantBuffers);
pd3dContext->GSGetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, pGSConstantBuffers);
pd3dContext->HSGetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, pHSConstantBuffers);
pd3dContext->DSGetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, pDSConstantBuffers);
pd3dContext->CSGetConstantBuffers(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, pCSConstantBuffers);
pd3dContext->RSGetState(&pRasterizer);
pd3dContext->OMGetDepthStencilState(&pDepthStencil, NULL);
pd3dContext->OMGetBlendState(&pBlendState, BlendFactors, &BlendSampleMask);
pd3dContext->IAGetInputLayout(&pInputLayout);
pd3dContext->IAGetPrimitiveTopology(&PrimitiveToplogy);
}
ID3D11RenderTargetView *pRTVs[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT];
ID3D11UnorderedAccessView *pUAVs[D3D11_PS_CS_UAV_REGISTER_COUNT];
UINT pUAVInitialCounts[D3D11_PS_CS_UAV_REGISTER_COUNT];
ID3D11DepthStencilView *pDSV;
ID3D11ShaderResourceView *pVSSRVs[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT];
ID3D11ShaderResourceView *pPSSRVs[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT];
ID3D11ShaderResourceView *pGSSRVs[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT];
ID3D11ShaderResourceView *pHSSRVs[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT];
ID3D11ShaderResourceView *pDSSRVs[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT];
ID3D11ShaderResourceView *pCSSRVs[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT];
D3D11_VIEWPORT Viewports[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
ID3D11VertexShader *pVertexShader;
ID3D11PixelShader *pPixelShader;
ID3D11GeometryShader *pGeometryShader;
ID3D11HullShader *pHullShader;
ID3D11DomainShader *pDomainShader;
ID3D11ComputeShader *pComputeShader;
ID3D11SamplerState *pVSSamplers[D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT];
ID3D11SamplerState *pPSSamplers[D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT];
ID3D11SamplerState *pGSSamplers[D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT];
ID3D11SamplerState *pHSSamplers[D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT];
ID3D11SamplerState *pDSSamplers[D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT];
ID3D11SamplerState *pCSSamplers[D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT];
ID3D11Buffer *pVSConstantBuffers[D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT];
ID3D11Buffer *pPSConstantBuffers[D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT];
ID3D11Buffer *pGSConstantBuffers[D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT];
ID3D11Buffer *pHSConstantBuffers[D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT];
ID3D11Buffer *pDSConstantBuffers[D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT];
ID3D11Buffer *pCSConstantBuffers[D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT];
ID3D11RasterizerState *pRasterizer;
ID3D11DepthStencilState *pDepthStencil;
ID3D11BlendState *pBlendState;
FLOAT BlendFactors[4];
UINT BlendSampleMask;
ID3D11InputLayout *pInputLayout;
D3D11_PRIMITIVE_TOPOLOGY PrimitiveToplogy;
ID3D11DeviceContext *pd3dContext;
};
#endif
#ifndef __EMSCRIPTEN__
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap)
{
fprintf(stderr, "error: %s\n", message);
wasm_byte_vec_t error_message;
if (error != NULL)
{
wasmtime_error_message(error, &error_message);
wasmtime_error_delete(error);
}
else
{
wasm_trap_message(trap, &error_message);
wasm_trap_delete(trap);
}
fprintf(stderr, "%.*s\n", (int)error_message.size, error_message.data);
wasm_byte_vec_delete(&error_message);
exit(1);
}
#endif
template <size_t N>
auto split_fixed(char separator, std::string_view input)
{
std::array<std::string_view, N> results;
auto current = input.begin();
const auto End = input.end();
for (auto &part : results)
{
if (current == End)
{
const bool is_last_part = &part == &(results.back());
if (!is_last_part)
throw std::invalid_argument("not enough parts to split");
}
auto delim = std::find(current, End, separator);
part = {&*current, size_t(delim - current)};
current = delim;
if (delim != End)
++current;
}
if (current != End)
throw std::invalid_argument("too many parts to split");
return results;
}
#ifdef _WIN32
PalD3D11::PalD3D11(ID3D11Device *device, ID3D11DeviceContext *context) :
m_device(device), m_device_context(context), m_swapchain(nullptr)
{
}
PalD3D11::PalD3D11(ID3D11Device* device, ID3D11DeviceContext* context, IDXGISwapChain* swapchain) :
m_device(device), m_device_context(context), m_swapchain(swapchain)
{
#ifdef RLT_RIVE
PLSRenderContextD3DImpl::ContextOptions contextOptions{};
m_plsContext = PLSRenderContextD3DImpl::MakeContext(m_device, m_device_context, contextOptions);
#endif
}
PalD3D11::~PalD3D11() {};
ObjectID PalD3D11::CreateBuffer(BufferDescriptor desc, int length, const uint8_t data[])
{
m_buffers.emplace_back(nullptr);
D3D11_BUFFER_DESC vbd = {};
vbd.Usage = D3D11_USAGE_IMMUTABLE;
vbd.ByteWidth = length;
vbd.CPUAccessFlags = 0;
vbd.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA vinitData;
vinitData.pSysMem = data;
switch (desc.Type())
{
case BufferType::Vertex:
vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
break;
case BufferType::Index:
vbd.BindFlags = D3D11_BIND_INDEX_BUFFER;
break;
case BufferType::Texture2D:
exit_with_error("Texture2D not supported in D3D11 Buffers", nullptr, nullptr);
break;
case BufferType::DynamicMaterial:
vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
vbd.Usage = D3D11_USAGE_DYNAMIC;
break;
}
if (m_device->CreateBuffer(&vbd, &vinitData, &m_buffers.back()) != 0)
return -1;
return m_buffers.size() - 1;
}
ObjectID PalD3D11::CreateTexture(BufferDescriptor desc, int length, const uint8_t data[])
{
m_textures.emplace_back(nullptr);
DXGI_FORMAT format{};
auto pitch = 0;
switch (desc.Format())
{
case BufferFormat::Unknown:
case BufferFormat::Custom:
case BufferFormat::CustomVertex:
case BufferFormat::Index16:
case BufferFormat::Index32:
default:
return -1;
case BufferFormat::R8:
format = DXGI_FORMAT_R8_UNORM;
pitch = 1;
break;
case BufferFormat::RG8:
format = DXGI_FORMAT_R8G8_UNORM;
pitch = 2;
break;
case BufferFormat::RGB8:
format = DXGI_FORMAT_R8G8B8A8_UNORM;
pitch = 3; // legacy
break;
case BufferFormat::RGBA8:
format = DXGI_FORMAT_R8G8B8A8_UNORM;
pitch = 4;
break;
case BufferFormat::R16F:
format = DXGI_FORMAT_R16_FLOAT;
pitch = 2;
break;
case BufferFormat::RG16F:
format = DXGI_FORMAT_R16G16_FLOAT;
pitch = 4;
break;
case BufferFormat::RGB16F:
format = DXGI_FORMAT_R16G16B16A16_FLOAT;
pitch = 6; // legacy
break;
case BufferFormat::RGBA16F:
format = DXGI_FORMAT_R16G16B16A16_FLOAT;
pitch = 8;
break;
case BufferFormat::R32F:
format = DXGI_FORMAT_R32_FLOAT;
pitch = 4;
break;
case BufferFormat::RG32F:
format = DXGI_FORMAT_R32G32_FLOAT;
pitch = 8;
break;
case BufferFormat::RGB32F:
format = DXGI_FORMAT_R32G32B32_FLOAT;
pitch = 12;
break;
case BufferFormat::RGBA32F:
format = DXGI_FORMAT_R32G32B32A32_FLOAT;
pitch = 16;
break;
}
// Copy into a texture.
D3D11_TEXTURE2D_DESC texDesc;
texDesc.Width = desc.Width();
texDesc.Height = desc.Height();
texDesc.MipLevels = 1;
texDesc.ArraySize = 1;
texDesc.Format = format;
texDesc.SampleDesc.Count = 1;
texDesc.SampleDesc.Quality = 0;
texDesc.Usage = D3D11_USAGE_IMMUTABLE;
texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
texDesc.CPUAccessFlags = 0;
texDesc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA sub;
sub.pSysMem = data;
sub.SysMemPitch = pitch;
sub.SysMemSlicePitch = 0; // no array support yet
if (m_device->CreateTexture2D(&texDesc, &sub, &m_textures.back()))
return -1;
return m_textures.size() - 1;
}
void PalD3D11::UpdateBuffer(ObjectID buffer_id, int length, const uint8_t data[])
{
D3D11_MAPPED_SUBRESOURCE mappedResource;
ZeroMemory(&mappedResource, sizeof(D3D11_MAPPED_SUBRESOURCE));
m_device_context->Map(m_buffers[buffer_id], 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
memcpy(mappedResource.pData, data, length);
m_device_context->Unmap(m_buffers[buffer_id], 0);
}
void PalD3D11::DeleteBuffer(ObjectID buffer_id)
{
if (buffer_id != -1)
m_buffers[buffer_id]->Release();
}
ObjectID PalD3D11::CreateVector(int length, const uint8_t data[])
{
#ifdef RLT_RIVE
m_vector_commands.emplace_back(VectorLoader::Read(data, length));
m_vector_srvs.emplace_back(nullptr);
m_vector_textures.emplace_back(nullptr);
return m_vector_commands.size() - 1;
#else
return -1;
#endif
}
ObjectID PalD3D11::UpdateVector(int length, const uint8_t data[], ObjectID buffer_id)
{
#ifdef RLT_RIVE
m_vector_commands[buffer_id] = VectorLoader::Read(data, length);
return buffer_id;
#else
return -1;
#endif
}
void PalD3D11::DrawTriangleList(ObjectID buffer_id, int offset, int length, unsigned int stride)
{
if (buffer_id < 0)
return;
const UINT strides = stride;
constexpr UINT offsets = 0;
m_device_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
m_device_context->IASetVertexBuffers(0, 1, &m_buffers[buffer_id], &strides, &offsets);
m_device_context->Draw(length, offset);
}
void PalD3D11::DrawTriangleListMultiBuffer(ObjectID buffer_id, int offset, int length,
unsigned int stride, ObjectID material_buffer_id, unsigned int material_stride)
{
if (buffer_id < 0 || material_buffer_id < 0)
return;
ID3D11Buffer* const buffers[] = {m_buffers[buffer_id], m_buffers[material_buffer_id]};
const UINT strides[] = {stride, material_stride};
constexpr UINT offsets[] = {0, 0};
m_device_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
m_device_context->IASetVertexBuffers(0, 2, buffers, strides, offsets);
m_device_context->Draw(length, offset);
}
void PalD3D11::DrawVector(ObjectID buffer_id, int slot, int width, int height)
{
#if defined(RLT_RIVE)
{
D3D11SavedState savedState(m_device_context);
const auto &vector = m_vector_commands[buffer_id];
rive::Factory *factory = m_plsContext.get();
auto renderer = std::make_unique<PLSRenderer>(m_plsContext.get());
auto plsContextImpl = m_plsContext->static_impl_cast<PLSRenderContextD3DImpl>();
auto renderTarget = plsContextImpl->makeRenderTarget(width, height);
if (slot != -1 && m_vector_textures[buffer_id] == nullptr)
{
D3D11_TEXTURE2D_DESC CoordinateTexDesc = {
static_cast<UINT>(width), // UINT Width;
static_cast<UINT>(height), // UINT Height;
1, // UINT MipLevels;
1, // UINT ArraySize;
DXGI_FORMAT_R8G8B8A8_UNORM, // DXGI_FORMAT Format;
{1, 0}, // DXGI_SAMPLE_DESC SampleDesc;
D3D11_USAGE_DEFAULT, // D3D11_USAGE Usage;
D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET, // UINT BindFlags;
0, // UINT CPUAccessFlags;
0, // UINT MiscFlags;
};
m_device->CreateTexture2D(&CoordinateTexDesc, NULL, &m_vector_textures[buffer_id]);
m_device->CreateShaderResourceView(m_vector_textures[buffer_id], NULL, &m_vector_srvs[buffer_id]);
}
m_plsContext->beginFrame({
.renderTargetWidth = static_cast<uint32_t>(width),
.renderTargetHeight = static_cast<uint32_t>(height),
.clearColor = 0xFF000000,
.msaaSampleCount = 0,
.disableRasterOrdering = false,
.wireframe = false,
.fillsDisabled = false,
.strokesDisabled = false,
});
if (slot == -1)
{
ID3D11Texture2D *frameBuffer;
m_swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void **>(&frameBuffer));
renderTarget->setTargetTexture(frameBuffer);
}
else
{
renderTarget->setTargetTexture(m_vector_textures[buffer_id]);
}
for (const auto &paths : vector)
{
const auto &raw_paint = std::get<0>(paths);
auto paint = factory->makeRenderPaint();
//paint->blendMode(static_cast<BlendMode>(raw_paint.blendMode));
paint->join(static_cast<StrokeJoin>(raw_paint.join));
paint->cap(static_cast<StrokeCap>(raw_paint.cap));
paint->style(static_cast<RenderPaintStyle>(raw_paint.style));
paint->thickness(raw_paint.thickness);
paint->color(raw_paint.color);
RawPath rp{};
const auto &raw_path = std::get<1>(paths);
for (const auto &path : raw_path)
{
const auto type = std::get<0>(path);
const auto &points = std::get<1>(path);
switch (type)
{
case VectorLoader::EPathType::Move:
rp.moveTo(points[0].x, points[0].y);
break;
case VectorLoader::EPathType::Line:
rp.lineTo(points[0].x, points[0].y);
break;
case VectorLoader::EPathType::Quad:
rp.quadTo(points[0].x, points[0].y, points[1].x, points[1].y);
break;
case VectorLoader::EPathType::Cubic:
rp.cubicTo(points[0].x, points[0].y, points[1].x, points[1].y, points[2].x, points[2].y);
break;
case VectorLoader::EPathType::Close:
rp.close();
break;
}
}
auto path = factory->makeRenderPath(rp, FillRule::nonZero);
renderer->drawPath(path.get(), paint.get());
}
m_plsContext->flush({.renderTarget = renderTarget.get()});
}
if (slot != -1)
m_device_context->PSSetShaderResources(slot, 1, &m_vector_srvs[buffer_id]);
#endif
}
#endif
ObjectID PalOpenGL::CreateBuffer(BufferDescriptor desc, int length, const uint8_t data[])
{
switch (desc.Type())
{
case BufferType::Vertex:
break;
case BufferType::Index:
case BufferType::Texture2D:
CreateTexture(desc, length, data);
break;
}
GLuint vbo{};
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, length, data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
m_vbos.emplace_back(vbo);
GLuint vao{};
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
m_vaos.emplace_back(vao);
return m_vaos.size() - 1;
}
ObjectID PalOpenGL::CreateTexture(BufferDescriptor desc, int length, const uint8_t data[])
{
GLenum format{};
GLenum type{};
switch (desc.Format())
{
case BufferFormat::Unknown:
case BufferFormat::Custom:
case BufferFormat::CustomVertex:
case BufferFormat::Index16:
case BufferFormat::Index32:
return -1;
case BufferFormat::R8:
format = GL_UNSIGNED_BYTE;
type = GL_RED;
break;
case BufferFormat::RG8:
format = GL_UNSIGNED_BYTE;
type = GL_RG;
break;
case BufferFormat::RGB8:
format = GL_UNSIGNED_BYTE;
type = GL_RGB;
break;
case BufferFormat::RGBA8:
format = GL_UNSIGNED_BYTE;
type = GL_RGBA;
break;
case BufferFormat::R16F:
format = GL_HALF_FLOAT;
type = GL_RED;
break;
case BufferFormat::RG16F:
format = GL_HALF_FLOAT;
type = GL_RG;
break;
case BufferFormat::RGB16F:
format = GL_HALF_FLOAT;
type = GL_RGB;
break;
case BufferFormat::RGBA16F:
format = GL_HALF_FLOAT;
type = GL_RGBA;
break;
case BufferFormat::R32F:
format = GL_FLOAT;
type = GL_RED;
break;
case BufferFormat::RG32F:
format = GL_FLOAT;
type = GL_RG;
break;
case BufferFormat::RGB32F:
format = GL_FLOAT;
type = GL_RGB;
break;
case BufferFormat::RGBA32F:
format = GL_FLOAT;
type = GL_RGBA;
break;
default:
return -1;
}
// don't (yet) support tex arrays
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, type,
desc.Width(), desc.Height(), 0, type, format, data);
glGenerateMipmap(GL_TEXTURE_2D);
m_texs.emplace_back(texture);
return m_texs.size() - 1;
}
void PalOpenGL::UpdateBuffer(ObjectID buffer_id, int length, const uint8_t data[])
{
return;
}
void PalOpenGL::DeleteBuffer(ObjectID buffer_id)
{
glDeleteVertexArrays(1, &m_vaos[buffer_id]);
glDeleteBuffers(1, &m_vbos[buffer_id]);
}
ObjectID PalOpenGL::CreateVector(int length, const uint8_t data[])
{
return -1;
}
ObjectID PalOpenGL::UpdateVector(int length, const uint8_t data[], ObjectID buffer_id)
{
return -1;
}
void wander::PalOpenGL::DrawVector(ObjectID buffer_id, int slot, int width, int height)
{
}
void PalOpenGL::DrawTriangleList(ObjectID buffer_id, int offset, int length, unsigned stride)
{
glBindVertexArray(m_vaos[buffer_id]);
glDrawArrays(GL_TRIANGLES, offset, length);
glBindVertexArray(0);
}
void PalOpenGL::DrawTriangleListMultiBuffer(ObjectID buffer_id, int offset, int length,
unsigned int stride, ObjectID material_buffer_id,unsigned int material_stride)
{
DrawTriangleList(buffer_id, offset, length, stride);
}
void RenderTreeNode::RenderFixedStride(IRuntime* runtime, unsigned int stride) const
{
static_cast<Runtime*>(runtime)->PalImpl()->DrawTriangleList(m_buffer_id, m_offset, m_length, stride);
}
void RenderTreeNode::RenderFixedStrideWithMaterial(IRuntime *runtime, unsigned stride, unsigned material_stride) const
{
static_cast<Runtime *>(runtime)->PalImpl()->DrawTriangleListMultiBuffer(
m_buffer_id, m_offset, m_length, stride, m_material_buffer_id, material_stride
);
}
void RenderTreeNode::RenderVector(IRuntime *runtime, int slot, int width, int height) const
{
static_cast<Runtime*>(runtime)->PalImpl()->DrawVector(m_buffer_id, slot, width, height);
}
void RenderTreeNode::SetPooledBuffer(ObjectID buffer_id, int offset)
{
m_buffer_id = buffer_id;
m_offset += offset;
}
std::string RenderTreeNode::Metadata() const
{
return m_metadata;
}
#ifdef __EMSCRIPTEN__
EM_ASYNC_JS(uintptr_t, init_renderlet, (const char *str), {
if (Window.renderlet_id === undefined){
Window.renderlet_id = 0;
}
else {
++Window.renderlet_id;
}
await WebAssembly.instantiateStreaming(fetch(UTF8ToString(str)), {}).then(function(obj) {
Window["Start_" + Window.renderlet_id] = obj.instance.exports.Start;
Window["Start_mem_" + Window.renderlet_id] = obj.instance.exports.memory;
});
return Window.renderlet_id;
});
EM_ASYNC_JS(uintptr_t, run_renderlet, (int id), {
var offset = Window["Start_" + id]();
var length_output = new Uint8Array(Window["Start_mem_" + id].buffer, offset, 4);
var view = new DataView(length_output.buffer, offset, 4);
var length = view.getUint32(0, true);
var output = new Uint8Array(Window["Start_mem_" + id].buffer, offset, length);
var heapSpace = _malloc(output.length);
HEAP8.set(output, heapSpace);
return heapSpace;
});
EM_ASYNC_JS(void, delete_renderlet, (int id), {
Window["Start_" + id] = {};
Window["Start_mem_" + id] = {};
});
#endif
ObjectID wander::Runtime::LoadFromFile(const std::wstring& path)
{
return LoadFromFile(path, "Start");
}
ObjectID wander::Runtime::LoadFromFile(const std::wstring& path, const std::string& function)
{
#ifndef __EMSCRIPTEN__
auto context = WasmtimeContext{};
auto conf = wasm_config_new();
wasmtime_config_wasm_simd_set(conf, true);
wasmtime_config_wasm_bulk_memory_set(conf, true);
wasmtime_config_wasm_multi_value_set(conf, true);
wasmtime_config_wasm_multi_memory_set(conf, true);
wasmtime_config_wasm_reference_types_set(conf, true);
wasmtime_config_wasm_threads_set(conf, true);
wasmtime_config_cranelift_opt_level_set(conf, WASMTIME_OPT_LEVEL_NONE);
wasmtime_config_parallel_compilation_set(conf, true);
wasmtime_config_cranelift_debug_verifier_set(conf, false);
m_engine = wasm_engine_new_with_config(conf);
assert(m_engine != NULL);
context.Store = wasmtime_store_new(m_engine, NULL, NULL);
assert(context.Store != NULL);
context.Context = wasmtime_store_context(context.Store);
// Create a linker with WASI functions defined
context.Linker = wasmtime_linker_new(m_engine);
wasmtime_error_t *error = wasmtime_linker_define_wasi(context.Linker);
if (error != NULL)
exit_with_error("failed to link wasi", error, NULL);
wasm_byte_vec_t wasm;
// Load our input file to parse it next
FILE *file;
if (_wfopen_s(&file, path.c_str(), L"rb") || !file)
{
printf("> Error loading file!\n");
exit(1);
}
fseek(file, 0L, SEEK_END);
size_t file_size = ftell(file);
wasm_byte_vec_new_uninitialized(&wasm, file_size);
fseek(file, 0L, SEEK_SET);
if (fread(wasm.data, file_size, 1, file) != 1)
{
printf("> Error loading module!\n");
exit(1);
}
fclose(file);
error = wasmtime_module_new(m_engine, (uint8_t *)wasm.data, wasm.size, &context.Module);
if (!context.Module)
exit_with_error("failed to compile module", error, NULL);
wasm_byte_vec_delete(&wasm);
// Instantiate wasi
wasi_config_t *wasi_config = wasi_config_new();
assert(wasi_config);
wasi_config_inherit_argv(wasi_config);
wasi_config_inherit_env(wasi_config);
wasi_config_inherit_stdin(wasi_config);
wasi_config_inherit_stdout(wasi_config);
wasi_config_inherit_stderr(wasi_config);
error = wasmtime_context_set_wasi(context.Context, wasi_config);
if (error != NULL)
exit_with_error("failed to instantiate WASI", error, NULL);
error = wasmtime_linker_module(context.Linker, context.Context, "", 0, context.Module);
if (error != NULL)
exit_with_error("failed to instantiate module", error, NULL);
if (!wasmtime_linker_get(context.Linker, context.Context, "", 0,
function.c_str(), function.length(), &context.Run))
return -1;
if (!wasmtime_linker_get(context.Linker, context.Context, "", 0,
"memory", 6, &context.Memory))
return -1;
m_contexts.push_back(context);
m_params.push_back({});
return m_contexts.size() - 1;
#else
//init_renderlet(path.c_str());
m_context_count = init_renderlet("demo.wasm") + 1;
return m_context_count - 1;
#endif
}
void wander::Runtime::PushParam(ObjectID renderlet_id, float value)
{
Param p;
p.Type = Param::Float32;
p.Value.F32 = value;
m_params[renderlet_id].push(p);
}
void wander::Runtime::PushParam(ObjectID renderlet_id, double value)
{
Param p;
p.Type = Param::Float64;