forked from GPUOpen-Effects/FidelityFX-FSR2
/
ffx_fsr2_gl.cpp
1272 lines (1089 loc) · 50.1 KB
/
ffx_fsr2_gl.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
// This file is part of the FidelityFX SDK.
//
// Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All rights reserved.
//
// 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.
#include "../ffx_fsr2.h"
#include "ffx_fsr2_gl.h"
#include "glad/gl.h"
#include "shaders/ffx_fsr2_shaders_gl.h" // include all the precompiled SPIR-V (GL) shaders for the FSR2 passes
#include "../ffx_fsr2_private.h"
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <codecvt>
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <Windows.h> // GetModuleHandleA
// prototypes for functions in the interface
FfxErrorCode GetDeviceCapabilitiesGL(FfxFsr2Interface* backendInterface, FfxDeviceCapabilities* deviceCapabilities, FfxDevice);
FfxErrorCode CreateBackendContextGL(FfxFsr2Interface* backendInterface, FfxDevice);
FfxErrorCode DestroyBackendContextGL(FfxFsr2Interface* backendInterface);
FfxErrorCode CreateResourceGL(FfxFsr2Interface* backendInterface, const FfxCreateResourceDescription* desc, FfxResourceInternal* outResource);
FfxErrorCode RegisterResourceGL(FfxFsr2Interface* backendInterface, const FfxResource* inResource, FfxResourceInternal* outResourceInternal);
FfxErrorCode UnregisterResourcesGL(FfxFsr2Interface* backendInterface);
FfxResourceDescription GetResourceDescriptorGL(FfxFsr2Interface* backendInterface, FfxResourceInternal resource);
FfxErrorCode DestroyResourceGL(FfxFsr2Interface* backendInterface, FfxResourceInternal resource);
FfxErrorCode CreatePipelineGL(FfxFsr2Interface* backendInterface, FfxFsr2Pass passId, const FfxPipelineDescription* desc, FfxPipelineState* outPass);
FfxErrorCode DestroyPipelineGL(FfxFsr2Interface* backendInterface, FfxPipelineState* pipeline);
FfxErrorCode ScheduleGpuJobGL(FfxFsr2Interface* backendInterface, const FfxGpuJobDescription* job);
FfxErrorCode ExecuteGpuJobsGL(FfxFsr2Interface* backendInterface, FfxCommandList);
namespace
{
constexpr uint32_t FSR2_MAX_QUEUED_FRAMES = 4;
constexpr uint32_t FSR2_MAX_RESOURCE_COUNT = 64;
constexpr uint32_t FSR2_MAX_STAGING_RESOURCE_COUNT = 8;
constexpr uint32_t FSR2_MAX_GPU_JOBS = 32;
constexpr uint32_t FSR2_MAX_UNIFORM_BUFFERS = 4;
constexpr uint32_t FSR2_MAX_IMAGE_VIEWS = 32;
constexpr uint32_t FSR2_MAX_BUFFERED_DESCRIPTORS = FFX_FSR2_PASS_COUNT * FSR2_MAX_QUEUED_FRAMES;
constexpr uint32_t FSR2_UBO_RING_BUFFER_SIZE = FSR2_MAX_BUFFERED_DESCRIPTORS * FSR2_MAX_UNIFORM_BUFFERS;
constexpr uint32_t FSR2_UBO_SIZE = 256;
constexpr uint32_t FSR2_DEFAULT_SUBGROUP_SIZE = 32;
namespace GL
{
struct Texture { GLuint id = {}; };
struct Buffer { GLuint id = {}; };
struct Sampler { GLuint id = {}; };
}
}
struct BackendContext_GL {
enum class Aspect { UNDEFINED, COLOR, DEPTH };
// store for resources and resourceViews
struct Resource
{
#ifdef _DEBUG
char resourceName[64] = {};
#endif
FfxResourceDescription resourceDescription;
GL::Buffer buffer = {};
GL::Texture textureAllMipsView = {};
GL::Texture textureSingleMipViews[FSR2_MAX_IMAGE_VIEWS] = {};
Aspect textureAspect = {};
};
struct UniformBuffer
{
GL::Buffer bufferResource = {};
uint8_t* pData = {};
};
struct GLFunctionTable
{
ffx_glGetProcAddress glGetProcAddress = nullptr;
PFNGLGETINTEGERVPROC glGetIntegerv = nullptr;
PFNGLGETSTRINGIPROC glGetStringi = nullptr;
PFNGLGETSTRINGPROC glGetString = nullptr;
PFNGLGETSHADERIVPROC glGetShaderiv = nullptr;
PFNGLGETPROGRAMIVPROC glGetProgramiv = nullptr;
PFNGLOBJECTLABELPROC glObjectLabel = nullptr;
PFNGLCREATESAMPLERSPROC glCreateSamplers = nullptr;
PFNGLSAMPLERPARAMETERIPROC glSamplerParameteri = nullptr;
PFNGLSAMPLERPARAMETERFPROC glSamplerParameterf = nullptr;
PFNGLCREATEBUFFERSPROC glCreateBuffers = nullptr;
PFNGLNAMEDBUFFERSTORAGEPROC glNamedBufferStorage = nullptr;
PFNGLCREATETEXTURESPROC glCreateTextures = nullptr;
PFNGLGENTEXTURESPROC glGenTextures = nullptr;
PFNGLTEXTUREVIEWPROC glTextureView = nullptr;
PFNGLTEXTURESTORAGE1DPROC glTextureStorage1D = nullptr;
PFNGLTEXTURESTORAGE2DPROC glTextureStorage2D = nullptr;
PFNGLTEXTURESTORAGE3DPROC glTextureStorage3D = nullptr;
PFNGLCREATESHADERPROC glCreateShader = nullptr;
PFNGLSHADERBINARYPROC glShaderBinary = nullptr;
PFNGLSPECIALIZESHADERPROC glSpecializeShader = nullptr;
PFNGLCOMPILESHADERPROC glCompileShader = nullptr;
PFNGLCREATEPROGRAMPROC glCreateProgram = nullptr;
PFNGLATTACHSHADERPROC glAttachShader = nullptr;
PFNGLLINKPROGRAMPROC glLinkProgram = nullptr;
PFNGLDELETEPROGRAMPROC glDeleteProgram = nullptr;
PFNGLDELETETEXTURESPROC glDeleteTextures = nullptr;
PFNGLDELETEBUFFERSPROC glDeleteBuffers = nullptr;
PFNGLDELETESAMPLERSPROC glDeleteSamplers = nullptr;
PFNGLDELETESHADERPROC glDeleteShader = nullptr;
PFNGLMAPNAMEDBUFFERRANGEPROC glMapNamedBufferRange = nullptr;
PFNGLUNMAPNAMEDBUFFERPROC glUnmapNamedBuffer = nullptr;
PFNGLMEMORYBARRIERPROC glMemoryBarrier = nullptr;
PFNGLUSEPROGRAMPROC glUseProgram = nullptr;
PFNGLPROGRAMUNIFORM1IPROC glProgramUniform1i = nullptr;
PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation = nullptr;
PFNGLBINDTEXTUREUNITPROC glBindTextureUnit = nullptr;
PFNGLBINDSAMPLERPROC glBindSampler = nullptr;
PFNGLBINDBUFFERRANGEPROC glBindBufferRange = nullptr;
PFNGLBINDIMAGETEXTUREPROC glBindImageTexture = nullptr;
PFNGLDISPATCHCOMPUTEPROC glDispatchCompute = nullptr;
PFNGLCOPYNAMEDBUFFERSUBDATAPROC glCopyNamedBufferSubData = nullptr;
PFNGLCOPYIMAGESUBDATAPROC glCopyImageSubData = nullptr;
PFNGLTEXTURESUBIMAGE1DPROC glTextureSubImage1D = nullptr;
PFNGLTEXTURESUBIMAGE2DPROC glTextureSubImage2D = nullptr;
PFNGLTEXTURESUBIMAGE3DPROC glTextureSubImage3D = nullptr;
PFNGLCLEARTEXIMAGEPROC glClearTexImage = nullptr;
};
GLFunctionTable glFunctionTable = {};
FfxDeviceCapabilities capabilities = {};
uint32_t gpuJobCount = 0;
FfxGpuJobDescription gpuJobs[FSR2_MAX_GPU_JOBS] = {};
uint32_t nextStaticResource = 0;
uint32_t nextDynamicResource = 0;
uint32_t stagingResourceCount = 0;
Resource resources[FSR2_MAX_RESOURCE_COUNT] = {};
FfxResourceInternal stagingResources[FSR2_MAX_STAGING_RESOURCE_COUNT] = {};
GL::Sampler pointSampler = {};
GL::Sampler linearSampler = {};
UniformBuffer uboRingBuffer[FSR2_UBO_RING_BUFFER_SIZE] = {};
uint32_t uboRingBufferIndex = 0;
};
FFX_API size_t ffxFsr2GetScratchMemorySizeGL()
{
return sizeof(BackendContext_GL);
}
FfxErrorCode ffxFsr2GetInterfaceGL(
FfxFsr2Interface* outInterface,
void* scratchBuffer,
size_t scratchBufferSize,
ffx_glGetProcAddress getProcAddress)
{
FFX_RETURN_ON_ERROR(
outInterface,
FFX_ERROR_INVALID_POINTER);
FFX_RETURN_ON_ERROR(
scratchBuffer,
FFX_ERROR_INVALID_POINTER);
FFX_RETURN_ON_ERROR(
scratchBufferSize >= ffxFsr2GetScratchMemorySizeGL(),
FFX_ERROR_INSUFFICIENT_MEMORY);
outInterface->fpGetDeviceCapabilities = GetDeviceCapabilitiesGL;
outInterface->fpCreateBackendContext = CreateBackendContextGL;
outInterface->fpDestroyBackendContext = DestroyBackendContextGL;
outInterface->fpCreateResource = CreateResourceGL;
outInterface->fpRegisterResource = RegisterResourceGL;
outInterface->fpUnregisterResources = UnregisterResourcesGL;
outInterface->fpGetResourceDescription = GetResourceDescriptorGL;
outInterface->fpDestroyResource = DestroyResourceGL;
outInterface->fpCreatePipeline = CreatePipelineGL;
outInterface->fpDestroyPipeline = DestroyPipelineGL;
outInterface->fpScheduleGpuJob = ScheduleGpuJobGL;
outInterface->fpExecuteGpuJobs = ExecuteGpuJobsGL;
outInterface->scratchBuffer = scratchBuffer;
outInterface->scratchBufferSize = scratchBufferSize;
BackendContext_GL* context = (BackendContext_GL*)scratchBuffer;
context->glFunctionTable.glGetProcAddress = getProcAddress;
return FFX_OK;
}
static void loadGLFunctions(BackendContext_GL* backendContext, ffx_glGetProcAddress getProcAddress)
{
FFX_ASSERT(NULL != backendContext);
backendContext->glFunctionTable.glObjectLabel = (PFNGLOBJECTLABELPROC)getProcAddress("glObjectLabel");
backendContext->glFunctionTable.glGetIntegerv = (PFNGLGETINTEGERVPROC)getProcAddress("glGetIntegerv");
backendContext->glFunctionTable.glGetString = (PFNGLGETSTRINGPROC)getProcAddress("glGetString");
backendContext->glFunctionTable.glGetStringi = (PFNGLGETSTRINGIPROC)getProcAddress("glGetStringi");
backendContext->glFunctionTable.glGetShaderiv = (PFNGLGETSHADERIVPROC)getProcAddress("glGetShaderiv");
backendContext->glFunctionTable.glGetProgramiv = (PFNGLGETPROGRAMIVPROC)getProcAddress("glGetProgramiv");
backendContext->glFunctionTable.glCreateSamplers = (PFNGLCREATESAMPLERSPROC)getProcAddress("glCreateSamplers");
backendContext->glFunctionTable.glSamplerParameteri = (PFNGLSAMPLERPARAMETERIPROC)getProcAddress("glSamplerParameteri");
backendContext->glFunctionTable.glSamplerParameterf = (PFNGLSAMPLERPARAMETERFPROC)getProcAddress("glSamplerParameterf");
backendContext->glFunctionTable.glCreateBuffers = (PFNGLCREATEBUFFERSPROC)getProcAddress("glCreateBuffers");
backendContext->glFunctionTable.glNamedBufferStorage = (PFNGLNAMEDBUFFERSTORAGEPROC)getProcAddress("glNamedBufferStorage");
backendContext->glFunctionTable.glCreateTextures = (PFNGLCREATETEXTURESPROC)getProcAddress("glCreateTextures");
backendContext->glFunctionTable.glGenTextures = (PFNGLGENTEXTURESPROC)getProcAddress("glGenTextures");
backendContext->glFunctionTable.glTextureView = (PFNGLTEXTUREVIEWPROC)getProcAddress("glTextureView");
backendContext->glFunctionTable.glTextureStorage1D = (PFNGLTEXTURESTORAGE1DPROC)getProcAddress("glTextureStorage1D");
backendContext->glFunctionTable.glTextureStorage2D = (PFNGLTEXTURESTORAGE2DPROC)getProcAddress("glTextureStorage2D");
backendContext->glFunctionTable.glTextureStorage3D = (PFNGLTEXTURESTORAGE3DPROC)getProcAddress("glTextureStorage3D");
backendContext->glFunctionTable.glCreateShader = (PFNGLCREATESHADERPROC)getProcAddress("glCreateShader");
backendContext->glFunctionTable.glShaderBinary = (PFNGLSHADERBINARYPROC)getProcAddress("glShaderBinary");
backendContext->glFunctionTable.glSpecializeShader = (PFNGLSPECIALIZESHADERPROC)getProcAddress("glSpecializeShader");
backendContext->glFunctionTable.glCompileShader = (PFNGLCOMPILESHADERPROC)getProcAddress("glCompileShader");
backendContext->glFunctionTable.glCreateProgram = (PFNGLCREATEPROGRAMPROC)getProcAddress("glCreateProgram");
backendContext->glFunctionTable.glAttachShader = (PFNGLATTACHSHADERPROC)getProcAddress("glAttachShader");
backendContext->glFunctionTable.glLinkProgram = (PFNGLLINKPROGRAMPROC)getProcAddress("glLinkProgram");
backendContext->glFunctionTable.glDeleteProgram = (PFNGLDELETEPROGRAMPROC)getProcAddress("glDeleteProgram");
backendContext->glFunctionTable.glDeleteTextures = (PFNGLDELETETEXTURESPROC)getProcAddress("glDeleteTextures");
backendContext->glFunctionTable.glDeleteBuffers = (PFNGLDELETEBUFFERSPROC)getProcAddress("glDeleteBuffers");
backendContext->glFunctionTable.glDeleteSamplers = (PFNGLDELETESAMPLERSPROC)getProcAddress("glDeleteSamplers");
backendContext->glFunctionTable.glDeleteShader = (PFNGLDELETESHADERPROC)getProcAddress("glDeleteShader");
backendContext->glFunctionTable.glMapNamedBufferRange = (PFNGLMAPNAMEDBUFFERRANGEPROC)getProcAddress("glMapNamedBufferRange");
backendContext->glFunctionTable.glUnmapNamedBuffer = (PFNGLUNMAPNAMEDBUFFERPROC)getProcAddress("glUnmapNamedBuffer");
backendContext->glFunctionTable.glMemoryBarrier = (PFNGLMEMORYBARRIERPROC)getProcAddress("glMemoryBarrier");
backendContext->glFunctionTable.glUseProgram = (PFNGLUSEPROGRAMPROC)getProcAddress("glUseProgram");
backendContext->glFunctionTable.glProgramUniform1i = (PFNGLPROGRAMUNIFORM1IPROC)getProcAddress("glProgramUniform1i");
backendContext->glFunctionTable.glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)getProcAddress("glGetUniformLocation");
backendContext->glFunctionTable.glBindTextureUnit = (PFNGLBINDTEXTUREUNITPROC)getProcAddress("glBindTextureUnit");
backendContext->glFunctionTable.glBindSampler = (PFNGLBINDSAMPLERPROC)getProcAddress("glBindSampler");
backendContext->glFunctionTable.glBindBufferRange = (PFNGLBINDBUFFERRANGEPROC)getProcAddress("glBindBufferRange");
backendContext->glFunctionTable.glBindImageTexture = (PFNGLBINDIMAGETEXTUREPROC)getProcAddress("glBindImageTexture");
backendContext->glFunctionTable.glDispatchCompute = (PFNGLDISPATCHCOMPUTEPROC)getProcAddress("glDispatchCompute");
backendContext->glFunctionTable.glCopyNamedBufferSubData = (PFNGLCOPYNAMEDBUFFERSUBDATAPROC)getProcAddress("glCopyNamedBufferSubData");
backendContext->glFunctionTable.glCopyImageSubData = (PFNGLCOPYIMAGESUBDATAPROC)getProcAddress("glCopyImageSubData");
backendContext->glFunctionTable.glTextureSubImage1D = (PFNGLTEXTURESUBIMAGE1DPROC)getProcAddress("glTextureSubImage1D");
backendContext->glFunctionTable.glTextureSubImage2D = (PFNGLTEXTURESUBIMAGE2DPROC)getProcAddress("glTextureSubImage2D");
backendContext->glFunctionTable.glTextureSubImage3D = (PFNGLTEXTURESUBIMAGE3DPROC)getProcAddress("glTextureSubImage3D");
backendContext->glFunctionTable.glClearTexImage = (PFNGLCLEARTEXIMAGEPROC)getProcAddress("glClearTexImage");
}
static GLenum getGLFormatFromSurfaceFormat(FfxSurfaceFormat fmt)
{
switch (fmt)
{
case FFX_SURFACE_FORMAT_R32G32B32A32_TYPELESS:
return GL_RGBA32F;
case FFX_SURFACE_FORMAT_R32G32B32A32_FLOAT:
return GL_RGBA32F;
case FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT:
return GL_RGBA16F;
case FFX_SURFACE_FORMAT_R16G16B16A16_UNORM:
return GL_RGBA16;
case FFX_SURFACE_FORMAT_R32G32_FLOAT:
return GL_RG32F;
case FFX_SURFACE_FORMAT_R32_UINT:
return GL_R32UI;
case FFX_SURFACE_FORMAT_R8G8B8A8_TYPELESS:
return GL_RGBA8;
case FFX_SURFACE_FORMAT_R8G8B8A8_UNORM:
return GL_RGBA8;
case FFX_SURFACE_FORMAT_R11G11B10_FLOAT:
return GL_R11F_G11F_B10F;
case FFX_SURFACE_FORMAT_R16G16_FLOAT:
return GL_RG16F;
case FFX_SURFACE_FORMAT_R16G16_UINT:
return GL_RG16UI;
case FFX_SURFACE_FORMAT_R16_FLOAT:
return GL_R16F;
case FFX_SURFACE_FORMAT_R16_UINT:
return GL_R16UI;
case FFX_SURFACE_FORMAT_R16_UNORM:
return GL_R16;
case FFX_SURFACE_FORMAT_R16_SNORM:
return GL_R16_SNORM;
case FFX_SURFACE_FORMAT_R8_UNORM:
return GL_R8;
case FFX_SURFACE_FORMAT_R8G8_UNORM:
return GL_RG8;
case FFX_SURFACE_FORMAT_R32_FLOAT:
return GL_R32F;
case FFX_SURFACE_FORMAT_R8_UINT:
return GL_R8UI;
default:
FFX_ASSERT_FAIL("");
return 0;
}
}
static GLenum getGLUploadFormatFromSurfaceFormat(FfxSurfaceFormat fmt)
{
switch (fmt)
{
case FFX_SURFACE_FORMAT_R32G32B32A32_TYPELESS:
case FFX_SURFACE_FORMAT_R32G32B32A32_FLOAT:
case FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT:
case FFX_SURFACE_FORMAT_R16G16B16A16_UNORM:
case FFX_SURFACE_FORMAT_R8G8B8A8_TYPELESS:
case FFX_SURFACE_FORMAT_R8G8B8A8_UNORM:
return GL_RGBA;
case FFX_SURFACE_FORMAT_R11G11B10_FLOAT:
return GL_RGB;
case FFX_SURFACE_FORMAT_R32G32_FLOAT:
case FFX_SURFACE_FORMAT_R16G16_FLOAT:
case FFX_SURFACE_FORMAT_R16G16_UINT:
case FFX_SURFACE_FORMAT_R8G8_UNORM:
return GL_RG;
case FFX_SURFACE_FORMAT_R16_FLOAT:
case FFX_SURFACE_FORMAT_R16_UNORM:
case FFX_SURFACE_FORMAT_R16_SNORM:
case FFX_SURFACE_FORMAT_R8_UNORM:
case FFX_SURFACE_FORMAT_R32_FLOAT:
return GL_RED;
case FFX_SURFACE_FORMAT_R8_UINT:
case FFX_SURFACE_FORMAT_R16_UINT:
case FFX_SURFACE_FORMAT_R32_UINT:
return GL_RED_INTEGER;
default: FFX_ASSERT_FAIL(""); return 0;
}
}
static GLenum getGLUploadTypeFromSurfaceFormat(FfxSurfaceFormat fmt)
{
switch (fmt)
{
case FFX_SURFACE_FORMAT_R32G32B32A32_TYPELESS:
case FFX_SURFACE_FORMAT_R32G32B32A32_FLOAT:
case FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT:
case FFX_SURFACE_FORMAT_R32G32_FLOAT:
case FFX_SURFACE_FORMAT_R11G11B10_FLOAT:
case FFX_SURFACE_FORMAT_R16G16_FLOAT:
case FFX_SURFACE_FORMAT_R16_FLOAT:
case FFX_SURFACE_FORMAT_R32_FLOAT:
return GL_FLOAT;
case FFX_SURFACE_FORMAT_R8G8B8A8_UNORM:
case FFX_SURFACE_FORMAT_R8G8B8A8_TYPELESS:
case FFX_SURFACE_FORMAT_R8G8_UNORM:
case FFX_SURFACE_FORMAT_R8_UNORM:
return GL_UNSIGNED_BYTE;
case FFX_SURFACE_FORMAT_R32_UINT:
return GL_UNSIGNED_INT;
case FFX_SURFACE_FORMAT_R16G16B16A16_UNORM:
case FFX_SURFACE_FORMAT_R16_UNORM:
case FFX_SURFACE_FORMAT_R16G16_UINT:
case FFX_SURFACE_FORMAT_R16_UINT:
case FFX_SURFACE_FORMAT_R8_UINT:
return GL_UNSIGNED_SHORT;
case FFX_SURFACE_FORMAT_R16_SNORM:
return GL_SHORT;
default: FFX_ASSERT_FAIL(""); return 0;
}
}
FfxSurfaceFormat ffxGetSurfaceFormatGL(GLenum fmt)
{
switch (fmt)
{
case GL_RGBA32F:
return FFX_SURFACE_FORMAT_R32G32B32A32_FLOAT;
case GL_RGBA16F:
return FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT;
case GL_RGBA16:
return FFX_SURFACE_FORMAT_R16G16B16A16_UNORM;
case GL_RG32F:
return FFX_SURFACE_FORMAT_R32G32_FLOAT;
case GL_R32UI:
return FFX_SURFACE_FORMAT_R32_UINT;
case GL_RGBA8:
return FFX_SURFACE_FORMAT_R8G8B8A8_UNORM;
case GL_R11F_G11F_B10F:
return FFX_SURFACE_FORMAT_R11G11B10_FLOAT;
case GL_RG16F:
return FFX_SURFACE_FORMAT_R16G16_FLOAT;
case GL_RG16UI:
return FFX_SURFACE_FORMAT_R16G16_UINT;
case GL_R16F:
return FFX_SURFACE_FORMAT_R16_FLOAT;
case GL_R16UI:
return FFX_SURFACE_FORMAT_R16_UINT;
case GL_R16:
return FFX_SURFACE_FORMAT_R16_UNORM;
case GL_R16_SNORM:
return FFX_SURFACE_FORMAT_R16_SNORM;
case GL_R8:
return FFX_SURFACE_FORMAT_R8_UNORM;
case GL_R32F:
return FFX_SURFACE_FORMAT_R32_FLOAT;
case GL_R8UI:
return FFX_SURFACE_FORMAT_R8_UINT;
default:
return FFX_SURFACE_FORMAT_UNKNOWN;
}
}
static BackendContext_GL::UniformBuffer accquireDynamicUBO(BackendContext_GL* backendContext, uint32_t size, const void* pData)
{
// the ubo ring buffer is pre-populated with VkBuffer objects of 256-bytes to prevent creating buffers at runtime
FFX_ASSERT(size <= 256);
BackendContext_GL::UniformBuffer& ubo = backendContext->uboRingBuffer[backendContext->uboRingBufferIndex];
if (pData)
{
memcpy(ubo.pData, pData, size);
}
backendContext->uboRingBufferIndex++;
if (backendContext->uboRingBufferIndex >= FSR2_UBO_RING_BUFFER_SIZE)
{
backendContext->uboRingBufferIndex = 0;
}
return ubo;
}
FfxResource ffxGetTextureResourceGL(GLuint textureGL, uint32_t width, uint32_t height, GLenum imgFormat, const wchar_t* name)
{
FfxResource resource = {};
resource.resource = reinterpret_cast<void*>(static_cast<uintptr_t>(textureGL));
resource.descriptorData = 0;
resource.description.flags = FFX_RESOURCE_FLAGS_NONE;
resource.description.type = FFX_RESOURCE_TYPE_TEXTURE2D;
resource.description.width = width;
resource.description.height = height;
resource.description.depth = 1;
resource.description.mipCount = 1;
resource.description.format = ffxGetSurfaceFormatGL(imgFormat);
switch (imgFormat)
{
case GL_DEPTH_COMPONENT16:
case GL_DEPTH_COMPONENT24:
case GL_DEPTH_COMPONENT32F:
case GL_DEPTH24_STENCIL8:
case GL_DEPTH32F_STENCIL8:
{
resource.isDepth = true;
break;
}
default:
{
resource.isDepth = false;
break;
}
}
#ifdef _DEBUG
if (name)
{
wcscpy_s(resource.name, name);
}
#endif
return resource;
}
FfxResource ffxGetBufferResourceGL(GLuint bufferGL, uint32_t size, const wchar_t* name)
{
FfxResource resource = {};
resource.resource = reinterpret_cast<void*>(static_cast<uintptr_t>(bufferGL));
resource.descriptorData = 0;
resource.description.flags = FFX_RESOURCE_FLAGS_NONE;
resource.description.type = FFX_RESOURCE_TYPE_BUFFER;
resource.description.width = size;
resource.description.height = 1;
resource.description.depth = 1;
resource.description.mipCount = 1;
resource.description.format = FFX_SURFACE_FORMAT_UNKNOWN;
resource.isDepth = false;
#ifdef _DEBUG
if (name)
{
wcscpy_s(resource.name, name);
}
#endif
return resource;
}
GLuint ffxGetGLImage(FfxFsr2Context* context, uint32_t resId)
{
FFX_ASSERT(context);
FfxFsr2Context_Private* contextPrivate = (FfxFsr2Context_Private*)(context);
BackendContext_GL* backendContext = (BackendContext_GL*)(contextPrivate->contextDescription.callbacks.scratchBuffer);
int32_t internalIndex = contextPrivate->uavResources[resId].internalIndex;
return (internalIndex == -1) ? 0 : backendContext->resources[internalIndex].textureAllMipsView.id;
}
FfxErrorCode RegisterResourceGL(
FfxFsr2Interface* backendInterface,
const FfxResource* inFfxResource,
FfxResourceInternal* outFfxResourceInternal
)
{
FFX_ASSERT(backendInterface);
BackendContext_GL* backendContext = (BackendContext_GL*)(backendInterface->scratchBuffer);
if (inFfxResource->resource == nullptr) {
outFfxResourceInternal->internalIndex = FFX_FSR2_RESOURCE_IDENTIFIER_NULL;
return FFX_OK;
}
FFX_ASSERT(backendContext->nextDynamicResource > backendContext->nextStaticResource);
outFfxResourceInternal->internalIndex = backendContext->nextDynamicResource--;
BackendContext_GL::Resource* backendResource = &backendContext->resources[outFfxResourceInternal->internalIndex];
backendResource->resourceDescription = inFfxResource->description;
#ifdef _DEBUG
size_t retval = 0;
wcstombs_s(&retval, backendResource->resourceName, sizeof(backendResource->resourceName), inFfxResource->name, sizeof(backendResource->resourceName));
if (retval >= 64) backendResource->resourceName[63] = '\0';
#endif
if (inFfxResource->description.type == FFX_RESOURCE_TYPE_BUFFER)
{
const auto buffer = static_cast<GLuint>(reinterpret_cast<uintptr_t>(inFfxResource->resource));
backendResource->buffer = { buffer };
}
else
{
const auto texture = static_cast<GLuint>(reinterpret_cast<uintptr_t>(inFfxResource->resource));
backendResource->textureAllMipsView = { texture };
backendResource->textureSingleMipViews[0] = { texture };
if (texture) {
if (inFfxResource->isDepth)
{
backendResource->textureAspect = BackendContext_GL::Aspect::DEPTH;
}
else
{
backendResource->textureAspect = BackendContext_GL::Aspect::COLOR;
}
}
}
return FFX_OK;
}
// dispose dynamic resources: This should be called at the end of the frame
FfxErrorCode UnregisterResourcesGL(FfxFsr2Interface* backendInterface)
{
FFX_ASSERT(backendInterface);
BackendContext_GL* backendContext = (BackendContext_GL*)(backendInterface->scratchBuffer);
backendContext->nextDynamicResource = FSR2_MAX_RESOURCE_COUNT - 1;
return FFX_OK;
}
FfxErrorCode GetDeviceCapabilitiesGL(FfxFsr2Interface* backendInterface, FfxDeviceCapabilities* deviceCapabilities, FfxDevice)
{
FFX_ASSERT(backendInterface);
FFX_ASSERT(deviceCapabilities);
BackendContext_GL* backendContext = (BackendContext_GL*)backendInterface->scratchBuffer;
// no shader model in vulkan so assume the minimum
deviceCapabilities->minimumSupportedShaderModel = FFX_SHADER_MODEL_5_1;
deviceCapabilities->waveLaneCountMin = 0;
deviceCapabilities->waveLaneCountMax = 0;
deviceCapabilities->fp16Supported = false;
deviceCapabilities->raytracingSupported = false;
// check if extensions are supported
// Workaround: latest AMD driver does not report GL_KHR_shader_subgroup, despite the extension being supported
bool vendorIsAmd = false;
const auto* vendor = reinterpret_cast<const char*>(backendContext->glFunctionTable.glGetString(GL_VENDOR));
if (strstr(vendor, "ATI"))
{
vendorIsAmd = true;
}
bool subgroupSupported = false;
GLint numExtensions{};
backendContext->glFunctionTable.glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions);
for (GLint i = 0; i < numExtensions; i++)
{
const auto* extensionString = reinterpret_cast<const char*>(backendContext->glFunctionTable.glGetStringi(GL_EXTENSIONS, i));
if (vendorIsAmd || (strcmp(extensionString, "GL_KHR_shader_subgroup") == 0))
{
GLint supportedStages{};
backendContext->glFunctionTable.glGetIntegerv(GL_SUBGROUP_SUPPORTED_STAGES_KHR, &supportedStages);
if (supportedStages & GL_COMPUTE_SHADER_BIT)
{
subgroupSupported = true;
}
}
if (strcmp(extensionString, "GL_NV_gpu_shader5") == 0 || strcmp(extensionString, "GL_AMD_gpu_shader_half_float") == 0)
{
deviceCapabilities->fp16Supported = true;
}
}
// Workaround: RenderDoc prevents many extensions from being reported
// In this case, shaders that use GL_KHR_shader_subgroup will still work, but API calls using constants from it will not
bool renderDocIsAttached = GetModuleHandleA("renderdoc.dll");
if (renderDocIsAttached)
{
subgroupSupported = true;
}
if (!subgroupSupported)
{
return FFX_ERROR_BACKEND_API_ERROR; // GL_KHR_shader_subgroup is required
}
GLint subgroupSize = FSR2_DEFAULT_SUBGROUP_SIZE;
if (!renderDocIsAttached)
{
backendContext->glFunctionTable.glGetIntegerv(GL_SUBGROUP_SIZE_KHR, &subgroupSize);
}
deviceCapabilities->waveLaneCountMin = static_cast<uint32_t>(subgroupSize);
deviceCapabilities->waveLaneCountMax = static_cast<uint32_t>(subgroupSize);
return FFX_OK;
}
FfxErrorCode CreateBackendContextGL(FfxFsr2Interface* backendInterface, FfxDevice)
{
FFX_ASSERT(backendInterface);
// set up some internal resources we need (space for resource views and constant buffers)
BackendContext_GL* backendContext = (BackendContext_GL*)backendInterface->scratchBuffer;
backendContext->nextStaticResource = 0;
backendContext->nextDynamicResource = FSR2_MAX_RESOURCE_COUNT - 1;
// load OpenGL functions
loadGLFunctions(backendContext, backendContext->glFunctionTable.glGetProcAddress);
FFX_VALIDATE(GetDeviceCapabilitiesGL(backendInterface, &backendContext->capabilities, nullptr));
// create samplers
backendContext->glFunctionTable.glCreateSamplers(1, &backendContext->pointSampler.id);
backendContext->glFunctionTable.glSamplerParameteri(backendContext->pointSampler.id, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
backendContext->glFunctionTable.glSamplerParameteri(backendContext->pointSampler.id, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
backendContext->glFunctionTable.glSamplerParameteri(backendContext->pointSampler.id, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
backendContext->glFunctionTable.glSamplerParameteri(backendContext->pointSampler.id, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
backendContext->glFunctionTable.glSamplerParameteri(backendContext->pointSampler.id, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
backendContext->glFunctionTable.glSamplerParameterf(backendContext->pointSampler.id, GL_TEXTURE_MIN_LOD, -1000);
backendContext->glFunctionTable.glSamplerParameterf(backendContext->pointSampler.id, GL_TEXTURE_MAX_LOD, 1000);
backendContext->glFunctionTable.glSamplerParameterf(backendContext->pointSampler.id, GL_TEXTURE_MAX_ANISOTROPY, 1);
backendContext->glFunctionTable.glCreateSamplers(1, &backendContext->linearSampler.id);
backendContext->glFunctionTable.glSamplerParameteri(backendContext->linearSampler.id, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
backendContext->glFunctionTable.glSamplerParameteri(backendContext->linearSampler.id, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
backendContext->glFunctionTable.glSamplerParameteri(backendContext->linearSampler.id, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
backendContext->glFunctionTable.glSamplerParameteri(backendContext->linearSampler.id, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
backendContext->glFunctionTable.glSamplerParameteri(backendContext->linearSampler.id, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
backendContext->glFunctionTable.glSamplerParameterf(backendContext->linearSampler.id, GL_TEXTURE_MIN_LOD, -1000);
backendContext->glFunctionTable.glSamplerParameterf(backendContext->linearSampler.id, GL_TEXTURE_MAX_LOD, 1000);
backendContext->glFunctionTable.glSamplerParameterf(backendContext->linearSampler.id, GL_TEXTURE_MAX_ANISOTROPY, 1);
// allocate ring buffer of uniform buffers
for (uint32_t i = 0; i < FSR2_UBO_RING_BUFFER_SIZE; i++)
{
BackendContext_GL::UniformBuffer& ubo = backendContext->uboRingBuffer[i];
backendContext->glFunctionTable.glCreateBuffers(1, &ubo.bufferResource.id);
constexpr GLbitfield mapFlags = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT;
backendContext->glFunctionTable.glNamedBufferStorage(ubo.bufferResource.id, FSR2_UBO_SIZE, nullptr, mapFlags);
// map the memory block
ubo.pData = (uint8_t*)backendContext->glFunctionTable.glMapNamedBufferRange(ubo.bufferResource.id, 0, FSR2_UBO_SIZE, mapFlags);
if (!ubo.pData)
{
return FFX_ERROR_BACKEND_API_ERROR;
}
}
backendContext->gpuJobCount = 0;
backendContext->stagingResourceCount = 0;
backendContext->uboRingBufferIndex = 0;
return FFX_OK;
}
FfxErrorCode DestroyBackendContextGL(FfxFsr2Interface* backendInterface)
{
FFX_ASSERT(backendInterface);
BackendContext_GL* backendContext = (BackendContext_GL*)backendInterface->scratchBuffer;
for (uint32_t i = 0; i < backendContext->stagingResourceCount; i++)
{
DestroyResourceGL(backendInterface, backendContext->stagingResources[i]);
}
for (uint32_t i = 0; i < FSR2_UBO_RING_BUFFER_SIZE; i++)
{
BackendContext_GL::UniformBuffer& ubo = backendContext->uboRingBuffer[i];
// buffer is implicitly unmapped by deleting it
backendContext->glFunctionTable.glDeleteBuffers(1, &ubo.bufferResource.id);
}
backendContext->glFunctionTable.glDeleteSamplers(1, &backendContext->pointSampler.id);
backendContext->glFunctionTable.glDeleteSamplers(1, &backendContext->linearSampler.id);
// clear all the fields of the context
*backendContext = {};
return FFX_OK;
}
// create a internal resource that will stay alive until effect gets shut down
FfxErrorCode CreateResourceGL(
FfxFsr2Interface* backendInterface,
const FfxCreateResourceDescription* createResourceDescription,
FfxResourceInternal* outResource)
{
FFX_ASSERT(backendInterface);
FFX_ASSERT(createResourceDescription);
FFX_ASSERT(outResource);
BackendContext_GL* backendContext = (BackendContext_GL*)backendInterface->scratchBuffer;
FFX_ASSERT(backendContext->nextStaticResource + 1 < backendContext->nextDynamicResource);
outResource->internalIndex = backendContext->nextStaticResource++;
BackendContext_GL::Resource* res = &backendContext->resources[outResource->internalIndex];
res->resourceDescription = createResourceDescription->resourceDescription;
res->resourceDescription.mipCount = createResourceDescription->resourceDescription.mipCount;
if (res->resourceDescription.mipCount == 0)
{
res->resourceDescription.mipCount = (uint32_t)(1 + floor(log2(FFX_MAXIMUM(FFX_MAXIMUM(createResourceDescription->resourceDescription.width, createResourceDescription->resourceDescription.height), createResourceDescription->resourceDescription.depth))));
}
#ifdef _DEBUG
size_t retval = 0;
wcstombs_s(&retval, res->resourceName, sizeof(res->resourceName), createResourceDescription->name, sizeof(res->resourceName));
if (retval >= 64) res->resourceName[63] = '\0';
#endif
switch (createResourceDescription->resourceDescription.type)
{
case FFX_RESOURCE_TYPE_BUFFER:
{
if (createResourceDescription->initData)
{
FFX_ASSERT(createResourceDescription->resourceDescription.width == createResourceDescription->initDataSize);
}
backendContext->glFunctionTable.glCreateBuffers(1, &res->buffer.id);
backendContext->glFunctionTable.glNamedBufferStorage(
res->buffer.id,
createResourceDescription->resourceDescription.width,
createResourceDescription->initData,
0);
#ifdef _DEBUG
backendContext->glFunctionTable.glObjectLabel(GL_BUFFER, res->buffer.id, -1, res->resourceName);
#endif
break;
}
case FFX_RESOURCE_TYPE_TEXTURE1D:
{
backendContext->glFunctionTable.glCreateTextures(GL_TEXTURE_1D, 1, &res->textureAllMipsView.id);
backendContext->glFunctionTable.glTextureStorage1D(
res->textureAllMipsView.id,
res->resourceDescription.mipCount,
getGLFormatFromSurfaceFormat(createResourceDescription->resourceDescription.format),
createResourceDescription->resourceDescription.width);
if (createResourceDescription->initData)
{
backendContext->glFunctionTable.glad_glTextureSubImage1D(
res->textureAllMipsView.id,
0,
0,
createResourceDescription->resourceDescription.width,
getGLUploadFormatFromSurfaceFormat(createResourceDescription->resourceDescription.format),
getGLUploadTypeFromSurfaceFormat(createResourceDescription->resourceDescription.format),
createResourceDescription->initData);
}
break;
}
case FFX_RESOURCE_TYPE_TEXTURE2D:
{
backendContext->glFunctionTable.glCreateTextures(GL_TEXTURE_2D, 1, &res->textureAllMipsView.id);
backendContext->glFunctionTable.glTextureStorage2D(
res->textureAllMipsView.id,
res->resourceDescription.mipCount,
getGLFormatFromSurfaceFormat(createResourceDescription->resourceDescription.format),
createResourceDescription->resourceDescription.width,
createResourceDescription->resourceDescription.height);
if (createResourceDescription->initData)
{
backendContext->glFunctionTable.glad_glTextureSubImage2D(
res->textureAllMipsView.id,
0,
0,
0,
createResourceDescription->resourceDescription.width,
createResourceDescription->resourceDescription.height,
getGLUploadFormatFromSurfaceFormat(createResourceDescription->resourceDescription.format),
getGLUploadTypeFromSurfaceFormat(createResourceDescription->resourceDescription.format),
createResourceDescription->initData);
}
break;
}
case FFX_RESOURCE_TYPE_TEXTURE3D:
{
backendContext->glFunctionTable.glCreateTextures(GL_TEXTURE_3D, 1, &res->textureAllMipsView.id);
backendContext->glFunctionTable.glTextureStorage3D(
res->textureAllMipsView.id,
res->resourceDescription.mipCount,
getGLFormatFromSurfaceFormat(createResourceDescription->resourceDescription.format),
createResourceDescription->resourceDescription.width,
createResourceDescription->resourceDescription.height,
createResourceDescription->resourceDescription.depth);
if (createResourceDescription->initData)
{
backendContext->glFunctionTable.glad_glTextureSubImage3D(
res->textureAllMipsView.id,
0,
0,
0,
0,
createResourceDescription->resourceDescription.width,
createResourceDescription->resourceDescription.height,
createResourceDescription->resourceDescription.depth,
getGLUploadFormatFromSurfaceFormat(createResourceDescription->resourceDescription.format),
getGLUploadTypeFromSurfaceFormat(createResourceDescription->resourceDescription.format),
createResourceDescription->initData);
}
break;
}
default:;
}
if (createResourceDescription->resourceDescription.type != FFX_RESOURCE_TYPE_BUFFER)
{
GLenum type = 0;
switch (createResourceDescription->resourceDescription.type)
{
case FFX_RESOURCE_TYPE_TEXTURE1D: type = GL_TEXTURE_1D; break;
case FFX_RESOURCE_TYPE_TEXTURE2D: type = GL_TEXTURE_2D; break;
case FFX_RESOURCE_TYPE_TEXTURE3D: type = GL_TEXTURE_3D; break;
}
res->textureAspect = BackendContext_GL::Aspect::COLOR;
for (uint32_t i = 0; i < res->resourceDescription.mipCount; i++)
{
backendContext->glFunctionTable.glGenTextures(1, &res->textureSingleMipViews[i].id);
backendContext->glFunctionTable.glTextureView(
res->textureSingleMipViews[i].id,
type,
res->textureAllMipsView.id,
getGLFormatFromSurfaceFormat(createResourceDescription->resourceDescription.format),
i,
1,
0,
1);
// texture view name
#ifdef _DEBUG
backendContext->glFunctionTable.glObjectLabel(GL_TEXTURE, res->textureSingleMipViews[i].id, -1, res->resourceName);
#endif
}
// texture name
#ifdef _DEBUG
backendContext->glFunctionTable.glObjectLabel(GL_TEXTURE, res->textureAllMipsView.id, -1, res->resourceName);
#endif
}
return FFX_OK;
}
FfxResourceDescription GetResourceDescriptorGL(FfxFsr2Interface* backendInterface, FfxResourceInternal resource)
{
FFX_ASSERT(backendInterface);
BackendContext_GL* backendContext = (BackendContext_GL*)backendInterface->scratchBuffer;
if (resource.internalIndex == -1)
{
return {};
}
return backendContext->resources[resource.internalIndex].resourceDescription;
}
FfxErrorCode CreatePipelineGL(FfxFsr2Interface* backendInterface, FfxFsr2Pass pass, const FfxPipelineDescription* pipelineDescription, FfxPipelineState* outPipeline)
{
FFX_ASSERT(backendInterface);
FFX_ASSERT(pipelineDescription);
BackendContext_GL* backendContext = (BackendContext_GL*)backendInterface->scratchBuffer;
// query device capabilities
FfxDeviceCapabilities deviceCapabilities;
GetDeviceCapabilitiesGL(backendInterface, &deviceCapabilities, nullptr);
bool useLut = false;
if (deviceCapabilities.waveLaneCountMax == 64)
{
useLut = true;
}
// check if we have 16bit floating point.
bool supportedFP16 = deviceCapabilities.fp16Supported;
if (pass == FFX_FSR2_PASS_ACCUMULATE || pass == FFX_FSR2_PASS_ACCUMULATE_SHARPEN)
{
// Workaround: Disable FP16 path for the accumulate pass on NVIDIA due to reduced occupancy and high VRAM throughput.
const auto* vendor = reinterpret_cast<const char*>(backendContext->glFunctionTable.glGetString(GL_VENDOR));
if (strstr(vendor, "NVIDIA"))
{
supportedFP16 = false;
}
}
// work out what permutation to load.
uint32_t flags = 0;
flags |= (pipelineDescription->contextFlags & FFX_FSR2_ENABLE_HIGH_DYNAMIC_RANGE) ? FSR2_SHADER_PERMUTATION_HDR_COLOR_INPUT : 0;
flags |= (pipelineDescription->contextFlags & FFX_FSR2_ENABLE_DISPLAY_RESOLUTION_MOTION_VECTORS) ? 0 : FSR2_SHADER_PERMUTATION_LOW_RES_MOTION_VECTORS;
flags |= (pipelineDescription->contextFlags & FFX_FSR2_ENABLE_MOTION_VECTORS_JITTER_CANCELLATION) ? FSR2_SHADER_PERMUTATION_JITTER_MOTION_VECTORS : 0;
flags |= (pipelineDescription->contextFlags & FFX_FSR2_ENABLE_DEPTH_INVERTED) ? FSR2_SHADER_PERMUTATION_DEPTH_INVERTED : 0;
flags |= (pass == FFX_FSR2_PASS_ACCUMULATE_SHARPEN) ? FSR2_SHADER_PERMUTATION_ENABLE_SHARPENING : 0;
flags |= (useLut) ? FSR2_SHADER_PERMUTATION_REPROJECT_USE_LANCZOS_TYPE : 0;
//flags |= (canForceWave64) ? FSR2_SHADER_PERMUTATION_FORCE_WAVE64 : 0; // cannot force wave64 in OpenGL
flags |= (supportedFP16 && (pass != FFX_FSR2_PASS_RCAS)) ? FSR2_SHADER_PERMUTATION_ALLOW_FP16 : 0;
const Fsr2ShaderBlobGL shaderBlob = fsr2GetPermutationBlobByIndexGL(pass, flags);
FFX_ASSERT(shaderBlob.data && shaderBlob.size);
// populate the pass.
outPipeline->srvCount = shaderBlob.combinedSamplerCount;
outPipeline->uavCount = shaderBlob.storageImageCount;
outPipeline->constCount = shaderBlob.uniformBufferCount;
FFX_ASSERT(shaderBlob.storageImageCount < FFX_MAX_NUM_UAVS);
FFX_ASSERT(shaderBlob.combinedSamplerCount < FFX_MAX_NUM_SRVS);
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
for (uint32_t srvIndex = 0; srvIndex < outPipeline->srvCount; ++srvIndex)
{
outPipeline->srvResourceBindings[srvIndex].slotIndex = shaderBlob.boundCombinedSamplerBindings[srvIndex];
wcscpy_s(outPipeline->srvResourceBindings[srvIndex].name, converter.from_bytes(shaderBlob.boundCombinedSamplerNames[srvIndex]).c_str());
}
for (uint32_t uavIndex = 0; uavIndex < outPipeline->uavCount; ++uavIndex)
{
outPipeline->uavResourceBindings[uavIndex].slotIndex = shaderBlob.boundStorageImageBindings[uavIndex];
wcscpy_s(outPipeline->uavResourceBindings[uavIndex].name, converter.from_bytes(shaderBlob.boundStorageImageNames[uavIndex]).c_str());
}
for (uint32_t cbIndex = 0; cbIndex < outPipeline->constCount; ++cbIndex)
{
outPipeline->cbResourceBindings[cbIndex].slotIndex = shaderBlob.boundUniformBufferBindings[cbIndex];
wcscpy_s(outPipeline->cbResourceBindings[cbIndex].name, converter.from_bytes(shaderBlob.boundUniformBufferNames[cbIndex]).c_str());
}
// create the shader module
GLuint shader = backendContext->glFunctionTable.glCreateShader(GL_COMPUTE_SHADER);
backendContext->glFunctionTable.glShaderBinary(1, &shader, GL_SHADER_BINARY_FORMAT_SPIR_V, shaderBlob.data, shaderBlob.size);
backendContext->glFunctionTable.glSpecializeShader(shader, "main", 0, nullptr, nullptr);
GLint compileStatus{};