-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvrhi.h
More file actions
2572 lines (2266 loc) · 105 KB
/
vrhi.h
File metadata and controls
2572 lines (2266 loc) · 105 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
/*
-- Vrhi --
Copyright 2026 UAA Software
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.
*/
#pragma once
// Define this if you have these in PCH already.
#ifndef VRHI_SKIP_COMMON_DEPENDENCY_INCLUDES
#include <cstdint>
#include <cstring>
#include <string>
#include <vector>
#include <functional>
#include <filesystem>
#include <fstream>
#include <glm/glm.hpp>
#endif // VRHI_SKIP_COMMON_DEPENDENCY_INCLUDES
#include <nvrhi/nvrhi.h>
// --------------------------------------------------------------------------
// Init
// --------------------------------------------------------------------------
struct vhInitData
{
// Application identity for Vulkan instance creation.
std::string appName = "VRHI_APP";
std::string engineName = "VRHI_ENGINE";
// Device selection. -1 for automatic (Discrete > Integrated > CPU).
int deviceIndex = -1;
// Initial window resolution for swapchain creation.
glm::ivec2 resolution = glm::ivec2( 1280, 720 );
// Logging and thread initialisation callbacks.
std::function<void( bool error, const std::string& )> fnLogCallback = nullptr;
std::function<void() > fnThreadInitCallback = nullptr;
std::function<void( const char*, bool )> fnProfileCallback = nullptr;
// Debug and feature toggles.
bool debug = false;
bool raytracing = true;
bool forceVulkan12 = false;
bool nullMode = false;
bool logBackendCmds = false;
bool logPSOCache = false;
bool debugBlockWaitForBackend = false;
bool errorOnSkippedDraw = false;
// Platform Window Handles
// On Windows, set windowHandle to (void*)HWND.
// On Linux (X11), set windowHandle to (void*)Window and displayHandle to (void*)Display.
// On macOS, set windowHandle to (void*)CAMetalLayer* by default, or (void*)NSView* with macOSWindowIsNSView = true.
void* windowHandle = nullptr;
void* displayHandle = nullptr;
bool macOSWindowIsNSView = false;
bool headless = true;
bool vsync = true;
// Shader compilation configuration.
std::string shaderCompileTempDir = "./tmp/shader_cache/";
std::string shaderMakePath = "./tools/linux_release";
std::string shaderMakeSlangPath = "./tools/linux_release";
bool forceShaderRecompile = false; // Ignore cache, always recompile
bool skipShaderCacheWrite = false; // Don't write .spirv cache files
bool dumpShaderSource = false; // Write .slang source files for debugging
bool robust = false;
bool markers = true;
bool renderdoc = false;
// Register binding shifts define 4 distinct ranges for descriptor types.
//
// * Samplers (s registers): Default shift is 100.
// * Textures (t registers): Default shift is 200.
// * Constant Buffers (b registers): Default shift is 300.
// * UAVs (u registers): Default shift is 400.
//
// The range values can be customised, the separation pattern cannot.
// Do not shift manual slots yourself; vhCompileShader applies these ranges automatically.
// Must be s < t < b < u shifts, with uRegShift having highest value.
//
uint32_t shaderMake_sRegShift = 100;
uint32_t shaderMake_tRegShift = 200;
uint32_t shaderMake_bRegShift = 300;
uint32_t shaderMake_uRegShift = 400;
// Global uniform buffer sizes. These back VRHI's state uniform system (vhState::uniforms).
uint32_t maxViewGlobals = 4 * 1024; // Per-view constants (camera, projection, etc).
uint32_t maxWorldMatrices = 16 * 1024; // World-space transform matrices.
uint32_t maxUserGlobals = 16 * 1024 * 1024; // User shader parameters (material data, etc).
// Seeds the Vulkan driver pipeline cache. Obtained from a previous vhGetPSOCache().
std::vector< uint8_t > psoCacheInitialData;
};
typedef uint32_t vhTexture;
typedef uint32_t vhBuffer;
typedef uint32_t vhShader;
typedef uint32_t vhUniform;
typedef uint64_t vhTimerID;
typedef std::vector< uint8_t > vhMem;
typedef std::vector< vhShader > vhProgram;
typedef uint32_t vhAccelStruct;
typedef uint32_t vhRTPipeline;
typedef uint32_t vhShaderTable;
typedef uint64_t vhStateId;
typedef uint32_t vhHeap;
extern vhInitData g_vhInit;
extern nvrhi::DeviceHandle g_vhDevice;
extern std::atomic<int32_t> g_vhErrorCounter;
extern std::atomic<int32_t> g_vhPSOCompileCounter;
// --------------------------------------------------------------------------
// Defines
// --------------------------------------------------------------------------
#define VRHI_VERSION_MAJOR 0
#define VRHI_VERSION_MINOR 1
#define VRHI_INVALID_HANDLE 0xFFFFFFFF
#define VRHI_MIPMAP_COMPLETE -1
// --------------------------------------------------------------------------
// Shader Stages
// --------------------------------------------------------------------------
constexpr uint64_t VRHI_SHADER_STAGE_VERTEX = 1;
constexpr uint64_t VRHI_SHADER_STAGE_PIXEL = 2;
constexpr uint64_t VRHI_SHADER_STAGE_COMPUTE = 3;
constexpr uint64_t VRHI_SHADER_STAGE_HULL = 4;
constexpr uint64_t VRHI_SHADER_STAGE_DOMAIN = 5;
constexpr uint64_t VRHI_SHADER_STAGE_GEOMETRY = 6;
constexpr uint64_t VRHI_SHADER_STAGE_RAYGEN = 7;
constexpr uint64_t VRHI_SHADER_STAGE_MISS = 8;
constexpr uint64_t VRHI_SHADER_STAGE_CLOSEST_HIT = 9;
constexpr uint64_t VRHI_SHADER_STAGE_MESH = 10;
constexpr uint64_t VRHI_SHADER_STAGE_AMPLIFICATION = 11;
constexpr uint64_t VRHI_SHADER_STAGE_ANY_HIT = 12;
constexpr uint64_t VRHI_SHADER_STAGE_INTERSECTION = 13;
constexpr uint64_t VRHI_SHADER_STAGE_CALLABLE = 14;
constexpr uint64_t VRHI_SHADER_STAGE_MAX = 14;
constexpr uint64_t VRHI_SHADER_STAGE_MASK = 0xF;
// Shader Stage -> Descriptor Set Mapping (Fixed, Non-Merging):
// Each stage gets its own fixed set index to avoid descriptor set merging.
// Mutually exclusive stages share indices (e.g. VS/CS, PS/Miss).
// Use VRHI_STAGE_SPACE in register declarations to target these automatically.
//
constexpr uint32_t VRHI_DESCRIPTOR_SET_VERTEX = 1; // VS default set (shared with CS)
constexpr uint32_t VRHI_DESCRIPTOR_SET_PIXEL = 2; // PS default set
constexpr uint32_t VRHI_DESCRIPTOR_SET_HULL = 3;
constexpr uint32_t VRHI_DESCRIPTOR_SET_DOMAIN = 4;
constexpr uint32_t VRHI_DESCRIPTOR_SET_GEOMETRY = 5;
constexpr uint32_t VRHI_DESCRIPTOR_SET_MESH = 6;
constexpr uint32_t VRHI_DESCRIPTOR_SET_AMPLIFICATION = 7;
constexpr uint32_t VRHI_DESCRIPTOR_SET_COMPUTE = 1; // CS default set (shared with VS)
constexpr uint32_t VRHI_DESCRIPTOR_SET_RAYGEN = 1; // RT stages
constexpr uint32_t VRHI_DESCRIPTOR_SET_MISS = 2;
constexpr uint32_t VRHI_DESCRIPTOR_SET_CLOSEST_HIT = 3;
constexpr uint32_t VRHI_DESCRIPTOR_SET_ANY_HIT = 4;
constexpr uint32_t VRHI_DESCRIPTOR_SET_INTERSECTION = 5;
constexpr uint32_t VRHI_DESCRIPTOR_SET_CALLABLE = 6;
constexpr uint32_t VRHI_DESCRIPTOR_SET_MAX = 8;
// Returns the fixed descriptor set index for a given shader stage.
//
// Each shader stage uses a unique, independent Descriptor Set index for its VRHI_STAGE_SPACE resources.
// Set 1: Vertex / Compute
// Set 2: Pixel
// Set 3+: Other stages
//
inline uint32_t vhGetDescriptorSetForStage( uint64_t stageFlags )
{
uint64_t stage = stageFlags & VRHI_SHADER_STAGE_MASK;
switch ( stage )
{
case VRHI_SHADER_STAGE_VERTEX: return VRHI_DESCRIPTOR_SET_VERTEX;
case VRHI_SHADER_STAGE_PIXEL: return VRHI_DESCRIPTOR_SET_PIXEL;
case VRHI_SHADER_STAGE_COMPUTE: return VRHI_DESCRIPTOR_SET_COMPUTE;
case VRHI_SHADER_STAGE_HULL: return VRHI_DESCRIPTOR_SET_HULL;
case VRHI_SHADER_STAGE_DOMAIN: return VRHI_DESCRIPTOR_SET_DOMAIN;
case VRHI_SHADER_STAGE_GEOMETRY: return VRHI_DESCRIPTOR_SET_GEOMETRY;
case VRHI_SHADER_STAGE_MESH: return VRHI_DESCRIPTOR_SET_MESH;
case VRHI_SHADER_STAGE_AMPLIFICATION: return VRHI_DESCRIPTOR_SET_AMPLIFICATION;
case VRHI_SHADER_STAGE_RAYGEN: return VRHI_DESCRIPTOR_SET_RAYGEN;
case VRHI_SHADER_STAGE_MISS: return VRHI_DESCRIPTOR_SET_MISS;
case VRHI_SHADER_STAGE_CLOSEST_HIT: return VRHI_DESCRIPTOR_SET_CLOSEST_HIT;
case VRHI_SHADER_STAGE_ANY_HIT: return VRHI_DESCRIPTOR_SET_ANY_HIT;
case VRHI_SHADER_STAGE_INTERSECTION: return VRHI_DESCRIPTOR_SET_INTERSECTION;
case VRHI_SHADER_STAGE_CALLABLE: return VRHI_DESCRIPTOR_SET_CALLABLE;
default: assert( !"Invalid stage" ); return VRHI_DESCRIPTOR_SET_MAX;
}
}
constexpr uint64_t VRHI_SHADER_SM_5_0 = ( 1 << 4 );
constexpr uint64_t VRHI_SHADER_SM_6_0 = ( 2 << 4 );
constexpr uint64_t VRHI_SHADER_SM_6_5 = ( 3 << 4 ); // Default behaviour if 0
constexpr uint64_t VRHI_SHADER_SM_6_6 = ( 4 << 4 );
constexpr uint64_t VRHI_SHADER_SM_MASK = 0xF0;
constexpr uint64_t VRHI_SHADER_DEBUG = ( 1ULL << 8 ); // -O0 -g -embedPDB
constexpr uint64_t VRHI_SHADER_ROW_MAJOR = ( 1ULL << 9 ); // -matrix-layout-row-major
constexpr uint64_t VRHI_SHADER_WARNINGS_AS_ERRORS = ( 1ULL << 10 ); // -warnings-as-errors
constexpr uint64_t VRHI_SHADER_STRIP_REFLECTION = ( 1ULL << 11 ); // --stripReflection. Good for release builds to reduce binary size.
constexpr uint64_t VRHI_SHADER_ALL_RESOURCES_BOUND = ( 1ULL << 12 ); // --allResourcesBound. Optimisation hint for the compiler.
constexpr uint64_t VRHI_SHADER_PATCH_DSET0 = ( 1ULL << 13 ); // Post-compile SPIR-V patch: remaps DescriptorSet 0 to VRHI_STAGE_SPACE (stage-specific set). Useful when shaders omit explicit register spaces.
// --------------------------------------------------------------------------
// Buffers
// --------------------------------------------------------------------------
constexpr uint64_t VRHI_BUFFER_NONE = 0x0000;
constexpr uint64_t VRHI_BUFFER_COMPUTE_READ = 0x0100;
constexpr uint64_t VRHI_BUFFER_COMPUTE_WRITE = 0x0200;
constexpr uint64_t VRHI_BUFFER_DRAW_INDIRECT = 0x0400;
constexpr uint64_t VRHI_BUFFER_ALLOW_RESIZE = 0x0800;
constexpr uint64_t VRHI_BUFFER_INDEX32 = 0x1000;
constexpr uint64_t VRHI_BUFFER_VIRTUAL = 0x2000;
// Buffer is intended for use as a ray tracing acceleration structure build input (e.g. BLAS
// vertex/index buffers, or raw AABB buffers for procedural geometry).
constexpr uint64_t VRHI_BUFFER_ACCEL_INPUT = 0x4000;
constexpr uint64_t VRHI_BUFFER_COMPUTE_READ_WRITE = ( VRHI_BUFFER_COMPUTE_READ | VRHI_BUFFER_COMPUTE_WRITE );
// --------------------------------------------------------------------------
// Textures
// --------------------------------------------------------------------------
constexpr uint64_t VRHI_TEXTURE_NONE = 0x0000000000000000;
constexpr uint64_t VRHI_TEXTURE_RT = 0x0000001000000000;
constexpr uint64_t VRHI_TEXTURE_COMPUTE_WRITE = 0x0000100000000000;
constexpr uint64_t VRHI_TEXTURE_SRGB = 0x0000200000000000;
constexpr uint64_t VRHI_TEXTURE_BLIT_DST = 0x0000400000000000;
constexpr uint64_t VRHI_TEXTURE_VIRTUAL = 0x0000800000000000;
// --------------------------------------------------------------------------
// Samplers
// --------------------------------------------------------------------------
constexpr uint32_t VRHI_SAMPLER_U_WRAP = 0x00000000;
constexpr uint32_t VRHI_SAMPLER_U_MIRROR = 0x00000001;
constexpr uint32_t VRHI_SAMPLER_U_CLAMP = 0x00000002;
constexpr uint32_t VRHI_SAMPLER_U_BORDER = 0x00000003;
constexpr uint32_t VRHI_SAMPLER_U_SHIFT = 0;
constexpr uint32_t VRHI_SAMPLER_U_MASK = 0x00000003;
constexpr uint32_t VRHI_SAMPLER_V_WRAP = 0x00000000;
constexpr uint32_t VRHI_SAMPLER_V_MIRROR = 0x00000004;
constexpr uint32_t VRHI_SAMPLER_V_CLAMP = 0x00000008;
constexpr uint32_t VRHI_SAMPLER_V_BORDER = 0x0000000c;
constexpr uint32_t VRHI_SAMPLER_V_SHIFT = 2;
constexpr uint32_t VRHI_SAMPLER_V_MASK = 0x0000000c;
constexpr uint32_t VRHI_SAMPLER_W_WRAP = 0x00000000;
constexpr uint32_t VRHI_SAMPLER_W_MIRROR = 0x00000010;
constexpr uint32_t VRHI_SAMPLER_W_CLAMP = 0x00000020;
constexpr uint32_t VRHI_SAMPLER_W_BORDER = 0x00000030;
constexpr uint32_t VRHI_SAMPLER_W_SHIFT = 4;
constexpr uint32_t VRHI_SAMPLER_W_MASK = 0x00000030;
constexpr uint32_t VRHI_SAMPLER_MIN_LINEAR = 0x00000000;
constexpr uint32_t VRHI_SAMPLER_MIN_POINT = 0x00000040;
constexpr uint32_t VRHI_SAMPLER_MIN_ANISOTROPIC = 0x00000080;
constexpr uint32_t VRHI_SAMPLER_MIN_SHIFT = 6;
constexpr uint32_t VRHI_SAMPLER_MIN_MASK = 0x000000c0;
constexpr uint32_t VRHI_SAMPLER_MAG_LINEAR = 0x00000000;
constexpr uint32_t VRHI_SAMPLER_MAG_POINT = 0x00000100;
constexpr uint32_t VRHI_SAMPLER_MAG_ANISOTROPIC = 0x00000200;
constexpr uint32_t VRHI_SAMPLER_MAG_SHIFT = 8;
constexpr uint32_t VRHI_SAMPLER_MAG_MASK = 0x00000300;
constexpr uint32_t VRHI_SAMPLER_MIP_LINEAR = 0x00000000;
constexpr uint32_t VRHI_SAMPLER_MIP_POINT = 0x00000400;
constexpr uint32_t VRHI_SAMPLER_MIP_NONE = 0x00000800;
constexpr uint32_t VRHI_SAMPLER_MIP_SHIFT = 10;
constexpr uint32_t VRHI_SAMPLER_MIP_MASK = 0x00000c00;
constexpr uint32_t VRHI_SAMPLER_COMPARE_LESS = 0x00001000;
constexpr uint32_t VRHI_SAMPLER_COMPARE_LEQUAL = 0x00002000;
constexpr uint32_t VRHI_SAMPLER_COMPARE_EQUAL = 0x00003000;
constexpr uint32_t VRHI_SAMPLER_COMPARE_GEQUAL = 0x00004000;
constexpr uint32_t VRHI_SAMPLER_COMPARE_GREATER = 0x00005000;
constexpr uint32_t VRHI_SAMPLER_COMPARE_NOTEQUAL = 0x00006000;
constexpr uint32_t VRHI_SAMPLER_COMPARE_NEVER = 0x00007000;
constexpr uint32_t VRHI_SAMPLER_COMPARE_ALWAYS = 0x00008000;
constexpr uint32_t VRHI_SAMPLER_COMPARE_SHIFT = 12;
constexpr uint32_t VRHI_SAMPLER_COMPARE_MASK = 0x0000f000;
#define VRHI_SAMPLER_MIPBIAS_SHIFT 16
#define VRHI_SAMPLER_MIPBIAS_MASK 0x00ff0000
#define VRHI_SAMPLER_MIPBIAS( v ) ( ( ( uint32_t )( int32_t )( ( v ) * 16.0f ) << VRHI_SAMPLER_MIPBIAS_SHIFT ) & VRHI_SAMPLER_MIPBIAS_MASK )
#define VRHI_SAMPLER_BORDER_COLOUR_SHIFT 24
#define VRHI_SAMPLER_BORDER_COLOUR_MASK 0x0f000000
#define VRHI_SAMPLER_BORDER_COLOUR( v ) ( ( ( uint32_t )( v ) << VRHI_SAMPLER_BORDER_COLOUR_SHIFT ) & VRHI_SAMPLER_BORDER_COLOUR_MASK )
constexpr uint32_t VRHI_SAMPLER_SAMPLE_STENCIL = 0x10000000;
#define VRHI_SAMPLER_MAX_ANISOTROPY_SHIFT 29
#define VRHI_SAMPLER_MAX_ANISOTROPY_MASK 0xe0000000
#define VRHI_SAMPLER_MAX_ANISOTROPY( v ) ( ( ( uint32_t )( v ) << VRHI_SAMPLER_MAX_ANISOTROPY_SHIFT ) & VRHI_SAMPLER_MAX_ANISOTROPY_MASK )
constexpr uint32_t VRHI_SAMPLER_ANISOTROPY_1 = VRHI_SAMPLER_MAX_ANISOTROPY( 0 );
constexpr uint32_t VRHI_SAMPLER_ANISOTROPY_2 = VRHI_SAMPLER_MAX_ANISOTROPY( 1 );
constexpr uint32_t VRHI_SAMPLER_ANISOTROPY_4 = VRHI_SAMPLER_MAX_ANISOTROPY( 2 );
constexpr uint32_t VRHI_SAMPLER_ANISOTROPY_8 = VRHI_SAMPLER_MAX_ANISOTROPY( 3 );
constexpr uint32_t VRHI_SAMPLER_ANISOTROPY_16 = VRHI_SAMPLER_MAX_ANISOTROPY( 4 );
constexpr uint32_t VRHI_SAMPLER_NONE = 0x00000000;
constexpr uint32_t VRHI_SAMPLER_POINT = (
VRHI_SAMPLER_MIN_POINT |
VRHI_SAMPLER_MAG_POINT |
VRHI_SAMPLER_MIP_POINT );
constexpr uint32_t VRHI_SAMPLER_UVW_MIRROR = (
VRHI_SAMPLER_U_MIRROR |
VRHI_SAMPLER_V_MIRROR |
VRHI_SAMPLER_W_MIRROR );
constexpr uint32_t VRHI_SAMPLER_UVW_CLAMP = (
VRHI_SAMPLER_U_CLAMP |
VRHI_SAMPLER_V_CLAMP |
VRHI_SAMPLER_W_CLAMP );
constexpr uint32_t VRHI_SAMPLER_UVW_BORDER = (
VRHI_SAMPLER_U_BORDER |
VRHI_SAMPLER_V_BORDER |
VRHI_SAMPLER_W_BORDER );
constexpr uint32_t VRHI_SAMPLER_UVW_WRAP = (
VRHI_SAMPLER_U_WRAP |
VRHI_SAMPLER_V_WRAP |
VRHI_SAMPLER_W_WRAP );
constexpr uint32_t VRHI_SAMPLER_BITS_MASK = (
VRHI_SAMPLER_U_MASK |
VRHI_SAMPLER_V_MASK |
VRHI_SAMPLER_W_MASK |
VRHI_SAMPLER_MIN_MASK |
VRHI_SAMPLER_MAG_MASK |
VRHI_SAMPLER_MIP_MASK |
VRHI_SAMPLER_COMPARE_MASK |
VRHI_SAMPLER_MIPBIAS_MASK |
VRHI_SAMPLER_BORDER_COLOUR_MASK |
VRHI_SAMPLER_SAMPLE_STENCIL |
VRHI_SAMPLER_MAX_ANISOTROPY_MASK );
// --------------------------------------------------------------------------
// State
// --------------------------------------------------------------------------
constexpr uint64_t VRHI_STATE_WRITE_R = 0x0000000000000001;
constexpr uint64_t VRHI_STATE_WRITE_G = 0x0000000000000002;
constexpr uint64_t VRHI_STATE_WRITE_B = 0x0000000000000004;
constexpr uint64_t VRHI_STATE_WRITE_A = 0x0000000000000008;
constexpr uint64_t VRHI_STATE_WRITE_Z = 0x0000004000000000;
constexpr uint64_t VRHI_STATE_WRITE_RGB = (
VRHI_STATE_WRITE_R |
VRHI_STATE_WRITE_G |
VRHI_STATE_WRITE_B );
constexpr uint64_t VRHI_STATE_WRITE_MASK = (
VRHI_STATE_WRITE_RGB |
VRHI_STATE_WRITE_A |
VRHI_STATE_WRITE_Z );
constexpr uint64_t VRHI_STATE_DEPTH_TEST_LESS = 0x0000000000000010;
constexpr uint64_t VRHI_STATE_DEPTH_TEST_LEQUAL = 0x0000000000000020;
constexpr uint64_t VRHI_STATE_DEPTH_TEST_EQUAL = 0x0000000000000030;
constexpr uint64_t VRHI_STATE_DEPTH_TEST_GEQUAL = 0x0000000000000040;
constexpr uint64_t VRHI_STATE_DEPTH_TEST_GREATER = 0x0000000000000050;
constexpr uint64_t VRHI_STATE_DEPTH_TEST_NOTEQUAL = 0x0000000000000060;
constexpr uint64_t VRHI_STATE_DEPTH_TEST_NEVER = 0x0000000000000070;
constexpr uint64_t VRHI_STATE_DEPTH_TEST_ALWAYS = 0x0000000000000080;
constexpr uint64_t VRHI_STATE_DEPTH_TEST_SHIFT = 4;
constexpr uint64_t VRHI_STATE_DEPTH_TEST_MASK = 0x00000000000000f0;
constexpr uint64_t VRHI_STATE_BLEND_ZERO = 0x0000000000001000;
constexpr uint64_t VRHI_STATE_BLEND_ONE = 0x0000000000002000;
constexpr uint64_t VRHI_STATE_BLEND_SRC_COLOUR = 0x0000000000003000;
constexpr uint64_t VRHI_STATE_BLEND_INV_SRC_COLOUR = 0x0000000000004000;
constexpr uint64_t VRHI_STATE_BLEND_SRC_ALPHA = 0x0000000000005000;
constexpr uint64_t VRHI_STATE_BLEND_INV_SRC_ALPHA = 0x0000000000006000;
constexpr uint64_t VRHI_STATE_BLEND_DST_ALPHA = 0x0000000000007000;
constexpr uint64_t VRHI_STATE_BLEND_INV_DST_ALPHA = 0x0000000000008000;
constexpr uint64_t VRHI_STATE_BLEND_DST_COLOUR = 0x0000000000009000;
constexpr uint64_t VRHI_STATE_BLEND_INV_DST_COLOUR = 0x000000000000a000;
constexpr uint64_t VRHI_STATE_BLEND_SRC_ALPHA_SAT = 0x000000000000b000;
constexpr uint64_t VRHI_STATE_BLEND_FACTOR = 0x000000000000c000;
constexpr uint64_t VRHI_STATE_BLEND_INV_FACTOR = 0x000000000000d000;
constexpr uint64_t VRHI_STATE_BLEND_SHIFT = 12;
constexpr uint64_t VRHI_STATE_BLEND_MASK = 0x000000000ffff000;
constexpr uint64_t VRHI_STATE_BLEND_EQUATION_ADD = 0x0000000000000000;
constexpr uint64_t VRHI_STATE_BLEND_EQUATION_SUB = 0x0000000010000000;
constexpr uint64_t VRHI_STATE_BLEND_EQUATION_REVSUB = 0x0000000020000000;
constexpr uint64_t VRHI_STATE_BLEND_EQUATION_MIN = 0x0000000030000000;
constexpr uint64_t VRHI_STATE_BLEND_EQUATION_MAX = 0x0000000040000000;
constexpr uint64_t VRHI_STATE_BLEND_EQUATION_SHIFT = 28;
constexpr uint64_t VRHI_STATE_BLEND_EQUATION_MASK = 0x00000003f0000000;
constexpr uint64_t VRHI_STATE_CULL_NONE = 0x0000000000000000;
constexpr uint64_t VRHI_STATE_CULL_BACK = 0x0000000000000100;
constexpr uint64_t VRHI_STATE_CULL_FRONT = 0x0000000000000200;
constexpr uint64_t VRHI_STATE_CULL_SHIFT = 8;
constexpr uint64_t VRHI_STATE_CULL_MASK = 0x0000000000000300;
constexpr uint64_t VRHI_STATE_FRONT_CW = 0x0000000000000400;
constexpr uint64_t VRHI_STATE_PT_TRIANGLES = 0x0000000000000000;
constexpr uint64_t VRHI_STATE_PT_TRISTRIP = 0x0001000000000000;
constexpr uint64_t VRHI_STATE_PT_LINES = 0x0002000000000000;
constexpr uint64_t VRHI_STATE_PT_LINESTRIP = 0x0003000000000000;
constexpr uint64_t VRHI_STATE_PT_POINTS = 0x0004000000000000;
constexpr uint64_t VRHI_STATE_PT_SHIFT = 48;
constexpr uint64_t VRHI_STATE_PT_MASK = 0x0007000000000000;
constexpr uint64_t VRHI_STATE_MSAA = 0x0100000000000000;
constexpr uint64_t VRHI_STATE_LINEAA = 0x0200000000000000;
constexpr uint64_t VRHI_STATE_CONSERVATIVE_RASTER = 0x0400000000000000;
constexpr uint64_t VRHI_STATE_NONE = 0x0000000000000000;
constexpr uint64_t VRHI_STATE_BLEND_INDEPENDENT = 0x0000000400000000;
constexpr uint64_t VRHI_STATE_BLEND_ALPHA_TO_COVERAGE = 0x0000000800000000;
constexpr uint64_t VRHI_STATE_DEPTH_CLIP = 0x0000010000000000;
constexpr uint64_t VRHI_STATE_DEPTH_TEST_ENABLE = 0x0000020000000000;
constexpr uint64_t VRHI_STATE_DEFAULT = (
VRHI_STATE_WRITE_RGB |
VRHI_STATE_WRITE_A |
VRHI_STATE_WRITE_Z |
VRHI_STATE_DEPTH_TEST_LESS |
VRHI_STATE_CULL_BACK |
VRHI_STATE_MSAA );
constexpr uint64_t VRHI_STATE_MASK = 0xffffffffffffffff;
constexpr uint64_t VRHI_STATE_DEBUG_NONE = 0x0000000000000000;
constexpr uint64_t VRHI_STATE_DEBUG_LOG_MISSING_BINDINGS = 0x0000000000000001;
constexpr uint64_t VRHI_STATE_DEBUG_LOG_ALL_BINDINGS = 0x0000000000000002;
constexpr uint64_t VRHI_STATE_DEBUG_LOG_VATTRIB_MISMATCH = 0x0000000000000004;
constexpr uint64_t VRHI_STATE_DEBUG_LOG_BINDING_MISMATCH = 0x0000000000000008;
constexpr uint64_t VRHI_STATE_DEBUG_ALL = 0xffffffffffffffff;
// Blend helper macros
#define VRHI_STATE_BLEND_FUNC_SEPARATE(_srcRGB, _dstRGB, _srcA, _dstA) ( UINT64_C( 0 ) \
| ( ( ( uint64_t )( _srcRGB ) | ( ( uint64_t )( _dstRGB ) << 4 ) ) ) \
| ( ( ( uint64_t )( _srcA ) | ( ( uint64_t )( _dstA ) << 4 ) ) << 8 ) )
#define VRHI_STATE_BLEND_EQUATION_SEPARATE(_equationRGB, _equationA) ( ( uint64_t )( _equationRGB ) | ( ( uint64_t )( _equationA ) << 3 ) )
#define VRHI_STATE_BLEND_FUNC(_src, _dst) VRHI_STATE_BLEND_FUNC_SEPARATE( _src, _dst, _src, _dst )
#define VRHI_STATE_BLEND_EQUATION(_equation) VRHI_STATE_BLEND_EQUATION_SEPARATE( _equation, _equation )
// Predefined blend modes
constexpr uint64_t VRHI_STATE_BLEND_ADD = (
VRHI_STATE_BLEND_FUNC( VRHI_STATE_BLEND_ONE, VRHI_STATE_BLEND_ONE ) );
constexpr uint64_t VRHI_STATE_BLEND_ALPHA = (
VRHI_STATE_BLEND_FUNC( VRHI_STATE_BLEND_SRC_ALPHA, VRHI_STATE_BLEND_INV_SRC_ALPHA ) );
constexpr uint64_t VRHI_STATE_BLEND_DARKEN = (
VRHI_STATE_BLEND_FUNC( VRHI_STATE_BLEND_ONE, VRHI_STATE_BLEND_ONE ) |
VRHI_STATE_BLEND_EQUATION( VRHI_STATE_BLEND_EQUATION_MIN ) );
constexpr uint64_t VRHI_STATE_BLEND_LIGHTEN = (
VRHI_STATE_BLEND_FUNC( VRHI_STATE_BLEND_ONE, VRHI_STATE_BLEND_ONE ) |
VRHI_STATE_BLEND_EQUATION( VRHI_STATE_BLEND_EQUATION_MAX ) );
constexpr uint64_t VRHI_STATE_BLEND_MULTIPLY = (
VRHI_STATE_BLEND_FUNC( VRHI_STATE_BLEND_DST_COLOUR, VRHI_STATE_BLEND_ZERO ) );
constexpr uint64_t VRHI_STATE_BLEND_NORMAL = (
VRHI_STATE_BLEND_FUNC( VRHI_STATE_BLEND_ONE, VRHI_STATE_BLEND_INV_SRC_ALPHA ) );
constexpr uint64_t VRHI_STATE_BLEND_SCREEN = (
VRHI_STATE_BLEND_FUNC( VRHI_STATE_BLEND_ONE, VRHI_STATE_BLEND_INV_SRC_COLOUR ) );
constexpr uint64_t VRHI_STATE_BLEND_LINEAR_BURN = (
VRHI_STATE_BLEND_FUNC( VRHI_STATE_BLEND_DST_COLOUR, VRHI_STATE_BLEND_INV_DST_COLOUR ) |
VRHI_STATE_BLEND_EQUATION( VRHI_STATE_BLEND_EQUATION_SUB ) );
// --------------------------------------------------------------------------
// Stencil
// --------------------------------------------------------------------------
constexpr uint64_t VRHI_STENCIL_NONE = 0x0000000000000000;
constexpr uint64_t VRHI_STENCIL_MASK = 0xffffffffffffffff;
constexpr uint64_t VRHI_STENCIL_DEFAULT = 0x0000000000000000;
constexpr uint64_t VRHI_STENCIL_FUNC_REF_SHIFT = 0;
constexpr uint64_t VRHI_STENCIL_FUNC_REF_MASK = 0x00000000000000ff;
#define VRHI_STENCIL_FUNC_REF(v) ( ( ( uint64_t )( v ) << VRHI_STENCIL_FUNC_REF_SHIFT ) & VRHI_STENCIL_FUNC_REF_MASK )
constexpr uint64_t VRHI_STENCIL_FUNC_RMASK_SHIFT = 8;
constexpr uint64_t VRHI_STENCIL_FUNC_RMASK_MASK = 0x000000000000ff00;
#define VRHI_STENCIL_FUNC_RMASK(v) ( ( ( uint64_t )( v ) << VRHI_STENCIL_FUNC_RMASK_SHIFT ) & VRHI_STENCIL_FUNC_RMASK_MASK )
constexpr uint64_t VRHI_STENCIL_FUNC_WMASK_SHIFT = 16;
constexpr uint64_t VRHI_STENCIL_FUNC_WMASK_MASK = 0x0000000000ff0000;
#define VRHI_STENCIL_FUNC_WMASK(v) ( ( ( uint64_t )( v ) << VRHI_STENCIL_FUNC_WMASK_SHIFT ) & VRHI_STENCIL_FUNC_WMASK_MASK )
constexpr uint64_t VRHI_STENCIL_TEST_LESS = 0x0000000001000000;
constexpr uint64_t VRHI_STENCIL_TEST_LEQUAL = 0x0000000002000000;
constexpr uint64_t VRHI_STENCIL_TEST_EQUAL = 0x0000000003000000;
constexpr uint64_t VRHI_STENCIL_TEST_GEQUAL = 0x0000000004000000;
constexpr uint64_t VRHI_STENCIL_TEST_GREATER = 0x0000000005000000;
constexpr uint64_t VRHI_STENCIL_TEST_NOTEQUAL = 0x0000000006000000;
constexpr uint64_t VRHI_STENCIL_TEST_NEVER = 0x0000000007000000;
constexpr uint64_t VRHI_STENCIL_TEST_ALWAYS = 0x0000000008000000;
constexpr uint64_t VRHI_STENCIL_TEST_SHIFT = 24;
constexpr uint64_t VRHI_STENCIL_TEST_MASK = 0x000000000f000000;
constexpr uint64_t VRHI_STENCIL_OP_FAIL_S_ZERO = 0x0000000000000000;
constexpr uint64_t VRHI_STENCIL_OP_FAIL_S_KEEP = 0x0000000010000000;
constexpr uint64_t VRHI_STENCIL_OP_FAIL_S_REPLACE = 0x0000000020000000;
constexpr uint64_t VRHI_STENCIL_OP_FAIL_S_INCR = 0x0000000030000000;
constexpr uint64_t VRHI_STENCIL_OP_FAIL_S_INCRSAT = 0x0000000040000000;
constexpr uint64_t VRHI_STENCIL_OP_FAIL_S_DECR = 0x0000000050000000;
constexpr uint64_t VRHI_STENCIL_OP_FAIL_S_DECRSAT = 0x0000000060000000;
constexpr uint64_t VRHI_STENCIL_OP_FAIL_S_INVERT = 0x0000000070000000;
constexpr uint64_t VRHI_STENCIL_OP_FAIL_S_SHIFT = 28;
constexpr uint64_t VRHI_STENCIL_OP_FAIL_S_MASK = 0x00000000f0000000;
constexpr uint64_t VRHI_STENCIL_OP_FAIL_Z_ZERO = 0x0000000000000000;
constexpr uint64_t VRHI_STENCIL_OP_FAIL_Z_KEEP = 0x0000000100000000;
constexpr uint64_t VRHI_STENCIL_OP_FAIL_Z_REPLACE = 0x0000000200000000;
constexpr uint64_t VRHI_STENCIL_OP_FAIL_Z_INCR = 0x0000000300000000;
constexpr uint64_t VRHI_STENCIL_OP_FAIL_Z_INCRSAT = 0x0000000400000000;
constexpr uint64_t VRHI_STENCIL_OP_FAIL_Z_DECR = 0x0000000500000000;
constexpr uint64_t VRHI_STENCIL_OP_FAIL_Z_DECRSAT = 0x0000000600000000;
constexpr uint64_t VRHI_STENCIL_OP_FAIL_Z_INVERT = 0x0000000700000000;
constexpr uint64_t VRHI_STENCIL_OP_FAIL_Z_SHIFT = 32;
constexpr uint64_t VRHI_STENCIL_OP_FAIL_Z_MASK = 0x0000000f00000000;
constexpr uint64_t VRHI_STENCIL_OP_PASS_Z_ZERO = 0x0000000000000000;
constexpr uint64_t VRHI_STENCIL_OP_PASS_Z_KEEP = 0x0000001000000000;
constexpr uint64_t VRHI_STENCIL_OP_PASS_Z_REPLACE = 0x0000002000000000;
constexpr uint64_t VRHI_STENCIL_OP_PASS_Z_INCR = 0x0000003000000000;
constexpr uint64_t VRHI_STENCIL_OP_PASS_Z_INCRSAT = 0x0000004000000000;
constexpr uint64_t VRHI_STENCIL_OP_PASS_Z_DECR = 0x0000005000000000;
constexpr uint64_t VRHI_STENCIL_OP_PASS_Z_DECRSAT = 0x0000006000000000;
constexpr uint64_t VRHI_STENCIL_OP_PASS_Z_INVERT = 0x0000007000000000;
constexpr uint64_t VRHI_STENCIL_OP_PASS_Z_SHIFT = 36;
constexpr uint64_t VRHI_STENCIL_OP_PASS_Z_MASK = 0x000000f000000000;
constexpr uint64_t VRHI_STENCIL_BACK_TEST_SHIFT = 40;
constexpr uint64_t VRHI_STENCIL_BACK_TEST_MASK = 0x00000f0000000000;
constexpr uint64_t VRHI_STENCIL_BACK_OP_FAIL_S_SHIFT = 44;
constexpr uint64_t VRHI_STENCIL_BACK_OP_FAIL_S_MASK = 0x000f000000000000;
constexpr uint64_t VRHI_STENCIL_BACK_OP_FAIL_Z_SHIFT = 48;
constexpr uint64_t VRHI_STENCIL_BACK_OP_FAIL_Z_MASK = 0x00f0000000000000;
constexpr uint64_t VRHI_STENCIL_BACK_OP_PASS_Z_SHIFT = 52;
constexpr uint64_t VRHI_STENCIL_BACK_OP_PASS_Z_MASK = 0x00f0000000000000;
// --------------------------------------------------------------------------
// Clear
// --------------------------------------------------------------------------
constexpr uint16_t VRHI_CLEAR_NONE = 0x0000;
constexpr uint16_t VRHI_CLEAR_COLOR = 0x0001;
constexpr uint16_t VRHI_CLEAR_DEPTH = 0x0002;
constexpr uint16_t VRHI_CLEAR_STENCIL = 0x0004;
constexpr uint16_t VRHI_CLEAR_UINT = 0x2000;
constexpr uint16_t VRHI_CLEAR_DISCARD_COLOR_0 = 0x0008;
constexpr uint16_t VRHI_CLEAR_DISCARD_COLOR_1 = 0x0010;
constexpr uint16_t VRHI_CLEAR_DISCARD_COLOR_2 = 0x0020;
constexpr uint16_t VRHI_CLEAR_DISCARD_COLOR_3 = 0x0040;
constexpr uint16_t VRHI_CLEAR_DISCARD_COLOR_4 = 0x0080;
constexpr uint16_t VRHI_CLEAR_DISCARD_COLOR_5 = 0x0100;
constexpr uint16_t VRHI_CLEAR_DISCARD_COLOR_6 = 0x0200;
constexpr uint16_t VRHI_CLEAR_DISCARD_COLOR_7 = 0x0400;
constexpr uint16_t VRHI_CLEAR_DISCARD_DEPTH = 0x0800;
constexpr uint16_t VRHI_CLEAR_DISCARD_STENCIL = 0x1000;
constexpr uint16_t VRHI_CLEAR_DISCARD_COLOR_MASK = (
VRHI_CLEAR_DISCARD_COLOR_0 |
VRHI_CLEAR_DISCARD_COLOR_1 |
VRHI_CLEAR_DISCARD_COLOR_2 |
VRHI_CLEAR_DISCARD_COLOR_3 |
VRHI_CLEAR_DISCARD_COLOR_4 |
VRHI_CLEAR_DISCARD_COLOR_5 |
VRHI_CLEAR_DISCARD_COLOR_6 |
VRHI_CLEAR_DISCARD_COLOR_7 );
constexpr uint16_t VRHI_CLEAR_DISCARD_MASK = (
VRHI_CLEAR_DISCARD_COLOR_MASK |
VRHI_CLEAR_DISCARD_DEPTH |
VRHI_CLEAR_DISCARD_STENCIL );
// --------------------------------------------------------------------------
// Variable Rate Shading
// --------------------------------------------------------------------------
constexpr uint64_t VRHI_VRS_1X1 = 0x0; // Full resolution (default)
constexpr uint64_t VRHI_VRS_1X2 = 0x1;
constexpr uint64_t VRHI_VRS_2X1 = 0x2;
constexpr uint64_t VRHI_VRS_2X2 = 0x3;
constexpr uint64_t VRHI_VRS_2X4 = 0x4;
constexpr uint64_t VRHI_VRS_4X2 = 0x5;
constexpr uint64_t VRHI_VRS_4X4 = 0x6;
constexpr uint64_t VRHI_VRS_COMBINER_PASSTHROUGH = 0x00;
constexpr uint64_t VRHI_VRS_COMBINER_OVERRIDE = 0x10;
constexpr uint64_t VRHI_VRS_COMBINER_MIN = 0x20;
constexpr uint64_t VRHI_VRS_COMBINER_MAX = 0x30;
constexpr uint64_t VRHI_VRS_COMBINER_SUM = 0x40;
// --------------------------------------------------------------------------
// Dirty Flags & Draw Flags
// --------------------------------------------------------------------------
constexpr uint64_t VRHI_DIRTY_WORLD = ( 1ULL << 0 );
constexpr uint64_t VRHI_DIRTY_VERTEX_INDEX = ( 1ULL << 1 );
constexpr uint64_t VRHI_DIRTY_CAMERA = ( 1ULL << 2 );
constexpr uint64_t VRHI_DIRTY_PIPELINE = ( 1ULL << 3 );
constexpr uint64_t VRHI_DIRTY_VIEWPORT = ( 1ULL << 4 );
constexpr uint64_t VRHI_DIRTY_ATTACHMENTS = ( 1ULL << 5 );
constexpr uint64_t VRHI_DIRTY_TEXTURE_SAMPLERS = ( 1ULL << 6 );
constexpr uint64_t VRHI_DIRTY_BUFFERS = ( 1ULL << 7 );
constexpr uint64_t VRHI_DIRTY_CONSTANTS = ( 1ULL << 8 );
constexpr uint64_t VRHI_DIRTY_PUSH_CONSTANTS = ( 1ULL << 9 );
constexpr uint64_t VRHI_DIRTY_PROGRAM = ( 1ULL << 10 );
constexpr uint64_t VRHI_DIRTY_UNIFORMS = ( 1ULL << 11 );
constexpr uint64_t VRHI_DIRTY_VRS = ( 1ULL << 12 );
constexpr uint64_t VRHI_DIRTY_INDIRECT = ( 1ULL << 13 );
constexpr uint64_t VRHI_DIRTY_DEPTH_BIAS = ( 1ULL << 14 );
constexpr uint64_t VRHI_DIRTY_ACCEL_STRUCT = ( 1ULL << 15 );
constexpr uint64_t VRHI_DIRTY_ALL = 0xFFFFFFFFFFFFFFFF;
constexpr uint32_t VRHI_DRAW_INDEXED = ( 1u << 0 );
constexpr uint32_t VRHI_DRAW_INDIRECT = ( 1u << 1 );
// Render target blend helper macros (cannot be constexpr)
#define VRHI_STATE_BLEND_FUNC_RT_x(_src, _dst) (0 \
| ( (uint32_t)( (_src)>>VRHI_STATE_BLEND_SHIFT) \
| ( (uint32_t)( (_dst)>>VRHI_STATE_BLEND_SHIFT)<<4) ) \
)
#define VRHI_STATE_BLEND_FUNC_RT_xE(_src, _dst, _equation) (0 \
| VRHI_STATE_BLEND_FUNC_RT_x(_src, _dst) \
| ( (uint32_t)( (_equation)>>VRHI_STATE_BLEND_EQUATION_SHIFT)<<8) \
)
#define VRHI_STATE_BLEND_FUNC_RT_1(_src, _dst) (VRHI_STATE_BLEND_FUNC_RT_x(_src, _dst)<< 0)
#define VRHI_STATE_BLEND_FUNC_RT_2(_src, _dst) (VRHI_STATE_BLEND_FUNC_RT_x(_src, _dst)<<11)
#define VRHI_STATE_BLEND_FUNC_RT_3(_src, _dst) (VRHI_STATE_BLEND_FUNC_RT_x(_src, _dst)<<22)
#define VRHI_STATE_BLEND_FUNC_RT_1E(_src, _dst, _equation) (VRHI_STATE_BLEND_FUNC_RT_xE(_src, _dst, _equation)<< 0)
#define VRHI_STATE_BLEND_FUNC_RT_2E(_src, _dst, _equation) (VRHI_STATE_BLEND_FUNC_RT_xE(_src, _dst, _equation)<<11)
#define VRHI_STATE_BLEND_FUNC_RT_3E(_src, _dst, _equation) (VRHI_STATE_BLEND_FUNC_RT_xE(_src, _dst, _equation)<<22)
// --------------------------------------------------------------------------
// Interface
// --------------------------------------------------------------------------
// Manually Regenerate this with py vidl.py vrhi.h src/vrhi_generated.h
// Cmake should automatically do this already.
// ************************************************************
// *** WARNING: POINTER INPUT MEMORY MANAGEMENT REQUIRED! ***
// ************************************************************
//
// VRHI uses a threaded backend. Pointer inputs (especially const char*)
// MUST be either:
// 1. 3-frame managed delayed free, OR
// 2. Static/literal strings, OR
// 3. Memory held until next flush/frame of backend.
//
// PASSING POINTERS TO STACK MEMORY WILL CAUSE CORRUPTION!
// const char* is NOT vmem managed!
// ************************************************************
// ------------ Device ------------
// Initialises the Vulkan RHI and starts the backend command thread.
//
// Must be called before any other RHI functions. Uses `g_vhInit` for configuration.
// If windowHandle is provided in g_vhInit, a swapchain will be created.
void vhInit( bool quiet = false );
// Shuts down the Vulkan RHI and stops the backend command thread.
//
// Cleans up all resources and waits for the GPU to finish.
void vhShutdown( bool quiet = false );
// Extracts the Vulkan driver pipeline cache. Flushes pending RHI work first.
// Returns false in null mode or on driver error; outData is cleared on failure.
bool vhGetPSOCache( std::vector< uint8_t >& outData );
// Presents the current frame and advances the swapchain.
// Returns false if the swapchain is invalid or window is resized.
// If running in headless mode, this simply flushes commands and waits if vsync-like behaviour is desired, always returning true.
bool vhFrame();
// Returns the texture handle for the current backbuffer of the swapchain.
// Returns VRHI_INVALID_HANDLE if in headless mode.
vhTexture vhGetBackbuffer();
struct vhMemoryStats
{
uint64_t heapBudget[16] = {}; // Available budget per heap (bytes)
uint64_t heapUsage[16] = {}; // Current usage per heap (bytes)
uint64_t heapSize[16] = {}; // Total heap size per heap (bytes)
uint32_t heapCount = 0; // Number of valid heaps
bool supported = false; // True if VK_EXT_memory_budget is available
};
struct vhRenderStats
{
uint64_t drawCalls = 0; // Accumulated draw calls (direct draws count instances, indirect draws count as 1)
uint64_t dispatchCalls = 0; // Accumulated dispatch calls (both direct and indirect)
};
// Hot-path diagnostic counters. Bumped via relaxed atomics; vhPerfCheck() dumps + clears.
struct vhPerfCounters
{
std::atomic< uint64_t > arenaOverflows = 0;
std::atomic< uint64_t > arenaMallocBytes = 0;
std::atomic< uint64_t > enqueueYields = 0;
std::atomic< uint64_t > enqueueRetryFloor = 0;
std::atomic< uint64_t > resolveCacheRebuilds = 0;
std::atomic< uint64_t > resolveCacheHits = 0;
std::atomic< uint64_t > psoCacheHits = 0;
std::atomic< uint64_t > psoCacheMisses = 0;
};
extern vhPerfCounters g_vhPerf;
// Logs perf counters via vhLog. No-op if all zero. reset=true zeroes them after.
void vhPerfCheck( bool reset = true );
struct vhDeviceInfo
{
std::string name; // "NVIDIA GeForce RTX 4090 - Discrete GPU"
std::string driver; // "NVIDIA 546.33"
std::string apiVersion; // "1.3.295"
std::string queues; // "Graphics:0 Compute:1 Transfer:2"
std::string summary; // Full device information string
bool raytracing = false;
bool bindless = false;
bool vrs = false;
bool asyncCompute = false;
bool memoryBudget = false;
uint32_t maxTextureSize = 0;
uint32_t maxColorAttachments = 0;
uint64_t totalVRAM = 0;
};
extern vhDeviceInfo g_vhDeviceInfo;
// Queries GPU memory statistics using VK_EXT_memory_budget extension.
// Returns memory budget and usage per heap. If the extension is unavailable,
// only heapSize/heapCount are populated (from basic Vulkan properties).
vhMemoryStats vhStatsMemory();
// Returns current frame or previous frame completed statistics.
// Counters are reset at the start of vhFrame().
vhRenderStats vhGetStats();
// Returns the total number of frames presented (or flushed in headless mode) since initialisation.
// This counter starts at 0 and increments at the end of every vhFrame() call.
uint64_t vhGetFrameNumber();
// Queries whether a specific device feature is supported.
// Returns true if the feature is supported on the current device.
//
// For features that require additional information (e.g. VRS tile size), pass an optional
// output struct via `pInfo` with size `infoSize`. See nvrhi::Feature for available features.
bool vhQueryFeatureSupport( nvrhi::Feature feature, void* pInfo = nullptr, size_t infoSize = 0 );
// Queries the supported operations for a specific texture/buffer format.
// Returns a bitmask of nvrhi::FormatSupport flags indicating what operations
// the format can be used for (e.g. RenderTarget, ShaderSample, UAV, etc).
nvrhi::FormatSupport vhQueryFormatSupport( nvrhi::Format format );
// Flushes the command queue to the backend.
//
// If `wait` is true, blocks until the backend has processed all queued commands.
// If `wait` is false, returns immediately after submitting the flush.
//
// This does not wait for the GPU to finish executing the work; use `vhFinish()` for that.
void vhFlush( bool wait = true );
// Flushes all commands and blocks until the GPU has completed all queued work.
// Use this for synchronisation points, readbacks, or before accessing GPU results.
void vhFinish();
// Clears backend caches (e.g. framebuffers). Call this after a window resize.
// VIDL_GENERATE
void vhResizeCleanup();
// Resizes the window swapchain to the specified dimensions.
// This performs a full GPU flush for safety. Performance is not critical during resize operations.
void vhResize( int width, int height );
// Returns the current window size.
// This is distinct from the initial resolution and reflects the actual swapchain dimensions.
glm::uvec2 vhGetWindowSize();
// Begin GPU timing measurement for the given timer ID.
// If the timer ID has not been seen before, it will be automatically created.
// VIDL_GENERATE
void vhBeginTimerQuery( vhTimerID timerID );
// End GPU timing measurement for the given timer ID.
// VIDL_GENERATE
void vhEndTimerQuery( vhTimerID timerID );
// Get the GPU time in seconds for the given timer ID.
// Returns 0.0f if the query results are not yet available or if the timer ID is invalid.
// Note: Due to GPU/CPU latency, you should read results from N frames ago (ring buffer of VRHI_MAX_FRAMES_INFLIGHT).
float vhGetTimerQueryTime( vhTimerID timerID );
// Begins a debug marker region for GPU capture tools (RenderDoc, etc).
// `name` is the marker name to display in profiling tools.
// If g_vhInit.markers is false, this call is ignored.
// VIDL_GENERATE
void vhBeginMarker( const std::string& name );
// Ends a debug marker region.
// If g_vhInit.markers is false, this call is ignored.
// VIDL_GENERATE
void vhEndMarker();
// Begins a frame capture for GPU debugging tools.
// If g_vhInit.markers is false, this call is ignored.
// VIDL_GENERATE
void vhCaptureStart();
// Ends the frame capture.
// If g_vhInit.markers is false, this call is ignored.
// VIDL_GENERATE
void vhCaptureEnd();
// Helper to allocate memory for data upload or download.
// The caller is responsible for allocating data to feed into vh* API functions, but not responsible for freeing it.
// This is freed by the backend every flush when the commands are processed.
inline vhMem* vhAllocMem( uint64_t size )
{
return new vhMem( size );
}
// Helper to allocate memory for data upload or download, copying the data from the provided vector.
// The caller is responsible for allocating data to feed into vh* API functions, but not responsible for freeing it.
// This is freed by the backend every flush when the commands are processed.
inline vhMem* vhAllocMem( const std::vector< uint8_t >& data )
{
return new vhMem( data );
}
// ------------ Texture ------------
struct vhTextureMipInfo
{
glm::ivec3 dimensions;
int64_t size;
int64_t offset;
int64_t slice_size;
int32_t pitch;
};
struct vhTexInfo
{
nvrhi::TextureDimension target = nvrhi::TextureDimension::Texture2D;
nvrhi::Format format = nvrhi::Format::UNKNOWN;
glm::ivec3 dimensions = glm::ivec3( 0, 0, 0 );
int32_t arrayLayers = 0;
int32_t mipLevels = 0;
int32_t samples = 0;
};
// Allocates a unique texture handle.
//
// Returns a valid `vhTexture` handle, or `VRHI_INVALID_HANDLE` on failure.
vhTexture vhAllocTexture();
// Resets internal texture state without destroying the handle.
// VIDL_GENERATE
void vhResetTexture( vhTexture texture );
// Allocates a unique buffer handle.
//
// Returns a valid `vhBuffer` handle, or `VRHI_INVALID_HANDLE` on failure.
vhBuffer vhAllocBuffer();
// Resets internal buffer state without destroying the handle.
// VIDL_GENERATE
void vhResetBuffer( vhBuffer buffer );
// Enqueues a command to destroy the texture associated with `texture`.
//
// `texture` is the handle to the texture to be destroyed.
// VIDL_GENERATE
void vhDestroyTexture( vhTexture texture );
// Enqueues a command to create a texture with the specified parameters.
//
// `texture` must be a handle allocated via `vhAllocTexture`.
// `target` specifies the texture dimensionality.
// `dimensions` specifies width, height, and depth.
// `numMips` and `numLayers` specify mip count and array size.
// `numMips` can be VRHI_MIPMAP_COMPLETE to create a full mip chain down to 1x1x1.
// `format` is the pixel format.
// `flag` specifies usage and sampling options.
// `data` is optional initial pixel data. Takes ownership of the memory.
// VIDL_GENERATE
vhTexture vhCreateTexture(
vhTexture texture,
const char* name,
nvrhi::TextureDimension target,
glm::ivec3 dimensions,
int numMips, int numLayers,
nvrhi::Format format,
uint64_t flag = VRHI_TEXTURE_NONE | VRHI_SAMPLER_NONE,
const vhMem* data = nullptr
);
// Helper to create a 2D texture.
inline vhTexture vhCreateTexture2D(
vhTexture texture,
const char* name,
glm::ivec2 dimensions,
int numMips,
nvrhi::Format format,
uint64_t flag = VRHI_TEXTURE_NONE | VRHI_SAMPLER_NONE,
const vhMem* data = nullptr
)
{
return vhCreateTexture( texture, name, nvrhi::TextureDimension::Texture2D, glm::ivec3( dimensions, 1 ), numMips, 1, format, flag, data );
}
// Helper to create a 3D texture.
inline vhTexture vhCreateTexture3D(
vhTexture texture,
const char* name,
glm::ivec3 dimensions,
int numMips,
nvrhi::Format format,
uint64_t flag = VRHI_TEXTURE_NONE | VRHI_SAMPLER_NONE,
const vhMem* data = nullptr
)
{
return vhCreateTexture( texture, name, nvrhi::TextureDimension::Texture3D, dimensions, numMips, 1, format, flag, data );
}
// Helper to create a Cube texture.
inline vhTexture vhCreateTextureCube(
vhTexture texture,
const char* name,
int dimension,
int numMips,
nvrhi::Format format,
uint64_t flag = VRHI_TEXTURE_NONE | VRHI_SAMPLER_NONE,
const vhMem* data = nullptr
)
{
return vhCreateTexture( texture, name, nvrhi::TextureDimension::TextureCube, glm::ivec3( dimension, dimension, 1 ), numMips, 6, format, flag, data );
}
// Helper to create a 2D texture array.
inline vhTexture vhCreateTexture2DArray(
vhTexture texture,
const char* name,
glm::ivec2 dimensions,
int numLayers,
int numMips,
nvrhi::Format format,
uint64_t flag = VRHI_TEXTURE_NONE | VRHI_SAMPLER_NONE,
const vhMem* data = nullptr
)
{
return vhCreateTexture( texture, name, nvrhi::TextureDimension::Texture2DArray, glm::ivec3( dimensions, 1 ), numMips, numLayers, format, flag, data );
}
// Helper to create a Cube texture array.
inline vhTexture vhCreateTextureCubeArray(
vhTexture texture,
const char* name,
int dimension,
int numLayers,
int numMips,
nvrhi::Format format,
uint64_t flag = VRHI_TEXTURE_NONE | VRHI_SAMPLER_NONE,
const vhMem* data = nullptr
)