-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgl_path_rendering_CMYK.cpp
More file actions
1653 lines (1563 loc) · 68.4 KB
/
gl_path_rendering_CMYK.cpp
File metadata and controls
1653 lines (1563 loc) · 68.4 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
/*
* Copyright (c) 2018-2021, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-FileCopyrightText: Copyright (c) 2018-2021 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
//--------------------------------------------------------------------
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "GLSLProgram.h"
#include "nvgl/contextwindow_gl.hpp"
#include "nvgl/extensions_gl.hpp"
#include "nvgl/profiler_gl.hpp"
#include "nvh/appwindowcamerainertia.hpp"
#include <imgui/backends/imgui_impl_gl.h>
#include <list>
#include <nvpwindow.hpp>
//
// Camera animation: captured using '1' in the sample. Then copy and paste...
//
struct CameraAnim
{
glm::vec3 eye, focus;
};
static CameraAnim s_cameraAnim[] = {
{glm::vec3(-1.26, 0.47, 1.01), glm::vec3(-0.96, 0.47, 0.00)},
{glm::vec3(0.38, 0.34, 0.43), glm::vec3(0.37, 0.35, 0.14)},
{glm::vec3(1.58, 0.54, 0.65), glm::vec3(0.65, 0.56, -0.06)},
{glm::vec3(1.07, -1.91, 4.75), glm::vec3(0.07, -0.50, 0.22)},
{glm::vec3(-3.41, 0.39, 2.76), glm::vec3(-0.96, -0.33, -0.28)},
{glm::vec3(-0.94, -0.15, 0.69), glm::vec3(-0.96, -0.33, -0.28)},
{glm::vec3(-0.25, -0.18, 0.67), glm::vec3(-0.27, -0.36, -0.29)},
{glm::vec3(0.79, -0.26, 0.66), glm::vec3(0.77, -0.43, -0.30)},
{glm::vec3(0.38, -0.98, 0.79), glm::vec3(0.36, -1.15, -0.16)},
{glm::vec3(-1.46, -1.48, 0.05), glm::vec3(-0.58, -1.12, -0.41)},
{glm::vec3(-1.64, -0.70, 0.30), glm::vec3(-0.76, -0.35, -0.16)},
{glm::vec3(-1.13, -0.66, 1.39), glm::vec3(-0.76, -0.35, -0.16)},
{glm::vec3(-0.20, -0.65, 3.05), glm::vec3(-0.20, -0.45, -0.11)},
{glm::vec3(0.00, 0.00, 3.00), glm::vec3(0.00, 0.00, 0.00)}, //14 items
};
static int s_cameraAnimItem = 0;
static int s_cameraAnimItems = 14;
#define ANIMINTERVALL 1.5f
static float s_cameraAnimIntervals = ANIMINTERVALL;
static bool s_bCameraAnim = true;
static bool s_helpText = false;
static nvgl::ProfilerGL s_profiler;
static double s_statsCpuTime = 0;
static double s_statsGpuTime = 0;
#define PROFILE_SECTION(name) nvgl::ProfilerGL::Section _tempTimer(s_profiler, name);
//-----------------------------------------------------------------------------
// Derive the Window for this sample
//-----------------------------------------------------------------------------
class MyWindow : public AppWindowCameraInertia
{
bool m_validated;
public:
ImGuiH::Registry m_guiRegistry;
nvgl::ContextWindow m_contextWindowGL;
MyWindow()
: m_validated(false)
{
}
bool open(int posX, int posY, int width, int height, const char* title, const nvgl::ContextWindowCreateInfo& context);
void renderScene();
void processUI(int width, int height, double dt);
virtual void onWindowClose() override;
virtual void onWindowResize(int w, int h) override;
//virtual void motion(int x, int y) override;
//virtual void mousewheel(short delta) override;
//virtual void mouse(NVPWindow::MouseButton button, ButtonAction action, int mods, int x, int y) override;
//virtual void menu(int m) override;
virtual void onKeyboard(MyWindow::KeyCode key, ButtonAction action, int mods, int x, int y) override;
virtual void onKeyboardChar(unsigned char key, int mods, int x, int y) override;
//virtual void idle() override;
virtual void onWindowRefresh() override;
};
/////////////////////////////////////////////////////////////////////////
// Cst color
static const char* g_glslv_WVP_Position =
"#version 330\n"
"#extension GL_ARB_separate_shader_objects : enable\n"
"uniform mat4 mWVP;\n"
"layout(location=0) in vec3 P;\n"
"out gl_PerVertex {\n"
" vec4 gl_Position;\n"
"};\n"
"void main() {\n"
" gl_Position = mWVP * vec4(P, 1.0);\n"
"}\n";
static const char* g_glslf_OneMinusCMYK_A =
"#version 330\n"
"#extension GL_ARB_separate_shader_objects : enable\n"
"uniform vec4 CMYK;\n"
"uniform float alpha;\n"
"layout(location=0) out vec4 outCMYA;\n"
"layout(location=1) out vec4 outKA;\n"
"void main() {\n"
" outCMYA = vec4(1.0-CMYK.xyz, alpha);\n"
" outKA = vec4(1.0-CMYK.w,1,1, alpha);\n"
"}\n";
static const char* g_glslf_RGBA =
"#version 330\n"
"#extension GL_ARB_separate_shader_objects : enable\n"
"uniform vec4 RGBA;\n"
"layout(location=0) out vec4 outRGBA;\n"
"void main() {\n"
" outRGBA = RGBA;\n"
"}\n";
/////////////////////////////////////////////////////////////////////////
// FBO resolve
static const char* g_glslv_Tc =
"#version 330\n"
"#extension GL_ARB_separate_shader_objects : enable\n"
"uniform ivec2 viewportSz;\n"
"layout(location=0) in ivec2 P;\n"
"layout(location=0) out vec2 TcOut;\n"
"out gl_PerVertex {\n"
" vec4 gl_Position;\n"
"};\n"
"void main() {\n"
" TcOut = vec2(P);\n"
" gl_Position = vec4(vec2(P)/vec2(viewportSz)*2.0 - 1.0, 0.0, 1.0);\n"
"}\n";
#define CMYK2RGB \
"vec3 OneMinusCMYK2RGB(in vec4 CMYK) {\n" \
" vec3 cmy = vec3(1-min(1.0, (CMYK.x)+(CMYK.w)),\n" \
" 1-min(1.0, (CMYK.y)+(CMYK.w)),\n" \
" 1-min(1.0, (CMYK.z)+(CMYK.w)) );\n" \
" return cmy;\n" \
"}\n"
inline glm::vec3 convertCMYK2RGB(glm::vec4 CMYK)
{
return glm::vec3(1 - glm::min(1.0f, (CMYK.x) + (CMYK.w)), 1 - glm::min(1.0f, (CMYK.y) + (CMYK.w)),
1 - glm::min(1.0f, (CMYK.z) + (CMYK.w)));
}
// sampling 2 textures and output RGBA
static const char* g_glslf_Tex_CMYA_KA_2_RGBA =
"#version 330\n"
"#extension GL_ARB_separate_shader_objects : enable\n"
"uniform vec4 CMYK_Mask;\n"
"uniform sampler2D sampler_CMYA;\n"
"uniform sampler2D sampler_KA;\n"
"layout(location=0) in vec2 Tc;\n"
"layout(location=0) out vec4 outColor;\n" CMYK2RGB
"void main() {\n"
" vec4 c = vec4(0);\n"
" float alpha = 0;\n"
" c = texelFetch(sampler_CMYA, ivec2(Tc), 0);\n"
" alpha = c.w;\n"
" c.w = texelFetch(sampler_KA, ivec2(Tc), 0).x;\n"
" outColor = vec4(OneMinusCMYK2RGB(CMYK_Mask * (1-c)), alpha);\n"
"}\n";
// for sampling MSAA Texture
#define DEFINE_GLSLF_TEXMS_CMYA_KA_2_RGBA(VAR, msaa) \
static const char* VAR = \
"#version 330\n" \
"#extension GL_ARB_separate_shader_objects : enable\n" \
"uniform vec4 CMYK_Mask;\n" \
"uniform sampler2DMS samplerMS_CMYA;\n" \
"uniform sampler2DMS samplerMS_KA;\n" \
"layout(location=0) in vec2 Tc;\n" \
"layout(location=0) out vec4 outColor;\n" CMYK2RGB \
"void main() {\n" \
" vec4 c = vec4(0);\n" \
" float alpha = 0;\n" \
" for(int i=0; i<" #msaa \
"; i++) {\n" \
" vec4 tex;\n" \
" tex = texelFetch(samplerMS_CMYA, ivec2(Tc), i);\n" \
" alpha += tex.w;" \
" tex.w = texelFetch(samplerMS_KA, ivec2(Tc), i).x;\n" \
" c += tex;" \
" }\n" \
" outColor = vec4(OneMinusCMYK2RGB(CMYK_Mask * (1-(c / " #msaa "))), alpha/" #msaa \
");\n" \
"}\n";
DEFINE_GLSLF_TEXMS_CMYA_KA_2_RGBA(g_glslf_TexMS_CMYA_KA_2_RGBA_2x, 2)
DEFINE_GLSLF_TEXMS_CMYA_KA_2_RGBA(g_glslf_TexMS_CMYA_KA_2_RGBA_8x, 8)
DEFINE_GLSLF_TEXMS_CMYA_KA_2_RGBA(g_glslf_TexMS_CMYA_KA_2_RGBA_16x, 16)
static const char* g_glslf_TexMS_CMYA_KA_2_RGBA[3] = {g_glslf_TexMS_CMYA_KA_2_RGBA_2x, g_glslf_TexMS_CMYA_KA_2_RGBA_8x,
g_glslf_TexMS_CMYA_KA_2_RGBA_16x};
// for sampling MSAA Texture
#define DEFINE_GLSLF_IMAGEMS_CMYA_KA_2_RGBA(VAR, msaa) \
static const char* VAR = \
"#version 420\n" \
"uniform vec4 CMYK_Mask;\n" \
"uniform layout(rgba8) image2DMS imageMS_CMYA;\n" \
"uniform layout(rgba8) image2DMS imageMS_KA;\n" \
"layout(location=0) in vec2 Tc;\n" \
"layout(location=0) out vec4 outColor;\n" CMYK2RGB \
"void main() {\n" \
" vec4 c = vec4(0);\n" \
" float alpha = 0;\n" \
" for(int i=0; i<" #msaa \
"; i++) {\n" \
" vec4 tex;\n" \
" tex = imageLoad(imageMS_CMYA, ivec2(Tc), i);\n" \
" alpha += tex.w;" \
" tex.w = imageLoad(imageMS_KA, ivec2(Tc), i).x;\n" \
" c += tex;" \
" }\n" \
" outColor = vec4(OneMinusCMYK2RGB(CMYK_Mask * (1-(c / " #msaa "))), alpha/" #msaa \
");\n" \
"}\n";
DEFINE_GLSLF_IMAGEMS_CMYA_KA_2_RGBA(g_glslf_ImageMS_CMYA_KA_2_RGBA_2x, 2)
DEFINE_GLSLF_IMAGEMS_CMYA_KA_2_RGBA(g_glslf_ImageMS_CMYA_KA_2_RGBA_8x, 8)
DEFINE_GLSLF_IMAGEMS_CMYA_KA_2_RGBA(g_glslf_ImageMS_CMYA_KA_2_RGBA_16x, 16)
static const char* g_glslf_ImageMS_CMYA_KA_2_RGBA[3] = {g_glslf_ImageMS_CMYA_KA_2_RGBA_2x, g_glslf_ImageMS_CMYA_KA_2_RGBA_8x,
g_glslf_ImageMS_CMYA_KA_2_RGBA_16x};
static GLenum drawBuffers[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3,
GL_COLOR_ATTACHMENT4, GL_COLOR_ATTACHMENT5, GL_COLOR_ATTACHMENT6};
//------------------------------------
// Blending Equations
#define BLENDINGLIST() \
ENUMDECL(GL_FUNC_ADD) \
ENUMDECL(GL_SRC_NV) \
ENUMDECL(GL_DST_NV) \
ENUMDECL(GL_SRC_OVER_NV) \
ENUMDECL(GL_DST_OVER_NV) \
ENUMDECL(GL_SRC_IN_NV) \
ENUMDECL(GL_DST_IN_NV) \
ENUMDECL(GL_SRC_OUT_NV) \
ENUMDECL(GL_DST_OUT_NV) \
ENUMDECL(GL_SRC_ATOP_NV) \
ENUMDECL(GL_DST_ATOP_NV) \
/*ENUMDECL(GL_XOR_NV)*/ \
ENUMDECL(GL_MULTIPLY_NV) \
ENUMDECL(GL_SCREEN_NV) \
ENUMDECL(GL_OVERLAY_NV) \
ENUMDECL(GL_DARKEN_NV) \
ENUMDECL(GL_LIGHTEN_NV) \
ENUMDECL(GL_COLORDODGE_NV) \
ENUMDECL(GL_COLORBURN_NV) \
ENUMDECL(GL_HARDLIGHT_NV) \
ENUMDECL(GL_SOFTLIGHT_NV) \
ENUMDECL(GL_DIFFERENCE_NV) \
ENUMDECL(GL_EXCLUSION_NV) \
/*ENUMDECL(INVERT)*/ \
ENUMDECL(GL_INVERT_RGB_NV) \
ENUMDECL(GL_LINEARDODGE_NV) \
ENUMDECL(GL_LINEARBURN_NV) \
ENUMDECL(GL_VIVIDLIGHT_NV) \
ENUMDECL(GL_LINEARLIGHT_NV) \
ENUMDECL(GL_PINLIGHT_NV) \
ENUMDECL(GL_HARDMIX_NV) \
/*ENUMDECL(GL_HSL_HUE_NV) can only work with RGB*/ \
/*ENUMDECL(GL_HSL_SATURATION_NV) can only work with RGB*/ \
/*ENUMDECL(GL_HSL_COLOR_NV) can only work with RGB*/ \
/*ENUMDECL(GL_HSL_LUMINOSITY_NV) can only work with RGB*/ \
ENUMDECL(GL_PLUS_NV) \
ENUMDECL(GL_PLUS_CLAMPED_NV) \
ENUMDECL(GL_PLUS_CLAMPED_ALPHA_NV) \
ENUMDECL(GL_PLUS_DARKER_NV) \
ENUMDECL(GL_MINUS_NV) \
ENUMDECL(GL_MINUS_CLAMPED_NV) \
ENUMDECL(GL_CONTRAST_NV) \
ENUMDECL(GL_INVERT_OVG_NV) \
/*ENUMDECL(GL_RED_NV)*/ \
/*ENUMDECL(GL_GREEN_NV)*/ \
/*ENUMDECL(GL_BLUE_NV)*/ \
ENUMDECL(GL_ZERO)
#define ENUMDECL(a) #a,
const char* blendequationNames[] = {BLENDINGLIST()};
#undef ENUMDECL
#define ENUMDECL(a) a,
GLenum blendequations[] = {BLENDINGLIST()};
int g_curBlendEquation = 0;
//------------------------------------
// Blend funcs
#define BLENDINGFUNCS() \
ENUMDECL(GL_ONE) \
ENUMDECL(GL_SRC_COLOR) \
ENUMDECL(GL_ONE_MINUS_SRC_COLOR) \
ENUMDECL(GL_DST_COLOR) \
ENUMDECL(GL_ONE_MINUS_DST_COLOR) \
ENUMDECL(GL_SRC_ALPHA) \
ENUMDECL(GL_ONE_MINUS_SRC_ALPHA) \
ENUMDECL(GL_DST_ALPHA) \
ENUMDECL(GL_ONE_MINUS_DST_ALPHA) \
ENUMDECL(GL_CONSTANT_COLOR) \
ENUMDECL(GL_ONE_MINUS_CONSTANT_COLOR) \
ENUMDECL(GL_CONSTANT_ALPHA) \
ENUMDECL(GL_ONE_MINUS_CONSTANT_ALPHA) \
ENUMDECL(GL_ZERO)
#undef ENUMDECL
#define ENUMDECL(a) #a,
const char* blendfuncNames[] = {BLENDINGFUNCS()};
#undef ENUMDECL
#define ENUMDECL(a) a,
GLenum blendfuncs[] = {BLENDINGFUNCS()};
int g_blendSRC = 5;
int g_blendDST = 6;
//------------------------------------
// used to give a string name to the class through constructor: helps for error checking
#define P(p) p(#p)
GLSLProgram P(g_prog_Cst_OneMinusCMYK_A);
GLSLProgram P(g_progPR_Cst_OneMinusCMYK_A);
GLSLProgram P(g_prog_Cst_RGBA);
GLSLProgram P(g_progPR_Cst_RGBA);
GLSLProgram P(g_progTexCMYA_KA_2_RGBA);
GLSLProgram g_progTexMS_CMYA_KA_2_RGBA[3];
GLSLProgram P(g_progTex_CMYA_KA_2_RGBA);
GLSLProgram g_progImageMS_CMYA_KA_2_RGBA[3];
GLuint g_vboCircle = 0;
GLuint g_vboQuad = 0;
float g_alpha = 0.5;
int g_NObjs = 6;
bool g_usePathObj = true;
bool g_activeC = true;
bool g_activeM = true;
bool g_activeY = true;
bool g_activeK = true;
bool g_blendEnable = true;
int g_MSAARaster = 8;
int g_MSAAVal[] = {1, 2, 8, 16};
int g_CurMSAAColor = 0;
bool g_has_GL_NV_framebuffer_mixed_samples = false;
bool g_buseUI = true;
unsigned int g_pathObj = 0;
// From the driver, renderbuffers are really useless: they are just textures that cannot be used as textures
// it is now advised to just use textures and avoid renderbuffers...
//#define USE_RENDERBUFFERS
// FBO Stuff
GLuint fboSz[2] = {0, 0};
namespace Texture {
GLuint CMYA;
GLuint KA;
GLuint MS_CMYA;
GLuint MS_KA;
#ifndef USE_RENDERBUFFERS
GLuint DST;
GLuint MS_DST;
#endif
}; // namespace Texture
#ifdef USE_RENDERBUFFERS
namespace Renderbuffer {
GLuint RGBA;
GLuint RGBAMS;
GLuint DST;
GLuint DSTMS;
}; // namespace Renderbuffer
#endif
namespace FBO {
GLuint TexMS_CMYA_KA_DST;
GLuint Tex_CMYA_KA;
GLuint TexMS_CMYA;
GLuint Tex_CMYA;
GLuint TexMS_KA;
GLuint Tex_KA;
#ifdef USE_RENDERBUFFERS
GLuint RbMS;
GLuint Rb;
#endif
}; // namespace FBO
#define TexMS_RGBA TexMS_CMYA // we can recycle the CMYA for RGBA
enum BlitMode
{
RESOLVEWITHBLIT = 0,
RESOLVEWITHSHADERTEX,
RESOLVEWITHSHADERIMAGE,
RESOLVERGBATOBACKBUFFER, // for the RGBA case
};
BlitMode blitMode;
enum MRTMode
{
RENDER1STEP = 0,
RENDER2STEPS,
RENDER1STEPRGBA,
};
MRTMode mrtMode = RENDER1STEP;
GLuint g_vao = 0;
//------------------------------------------------------------------------------
// It is possible that this callback is invoked from another thread
// so let's just append messages for later diplay in the main loop
//------------------------------------------------------------------------------
struct LogMessage
{
LogMessage(int l, const char* t)
{
level = l;
txt = t;
}
int level;
std::string txt;
};
typedef std::list<LogMessage> Messages;
//------------------------------------------------------------------------------
void sample_print(int level, const char* txt) {}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// UI stuff
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
#define COMBO_BLITMODE 0
#define COMBO_MRTMODE 1
#define COMBO_MSAACOL 2
#define COMBO_MSAARAST 3
#define COMBO_BLENDEQ 4
#define COMBO_BLENDFNSRC 5
#define COMBO_BLENDFNDST 6
void MyWindow::processUI(int width, int height, double dt)
{
// Update imgui configuration
auto& imgui_io = ImGui::GetIO();
imgui_io.DeltaTime = static_cast<float>(dt);
imgui_io.DisplaySize = ImVec2(width, height);
ImGui::NewFrame();
ImGui::SetNextWindowBgAlpha(0.5);
ImGui::SetNextWindowSize(ImGuiH::dpiScaled(450, 0), ImGuiCond_FirstUseEver);
if(ImGui::Begin("NVIDIA " PROJECT_NAME, nullptr))
{
//ImGui::PushItemWidth(200);
ImGui::Text("gl and vk version");
ImGuiH::InputIntClamped("N Objs x&y", &g_NObjs, 1, 100);
ImGui::Separator();
//
// Blit modes
//
m_guiRegistry.enumCombobox(COMBO_BLITMODE, "Blit Mode", &blitMode);
//
// render mode on Muyltiple render-targets
//
m_guiRegistry.enumCombobox(COMBO_MRTMODE, "Render Pass", &mrtMode);
//
// Color samples combo
//
m_guiRegistry.enumCombobox(COMBO_MSAACOL, "MSAA Color samples", &g_CurMSAAColor);
//
// Mixed samples combo
//
if(g_has_GL_NV_framebuffer_mixed_samples)
{
m_guiRegistry.enumCombobox(COMBO_MSAARAST, "MSAA Raster samples", &g_MSAARaster);
}
//
// Blending Equations
//
m_guiRegistry.enumCombobox(COMBO_BLENDEQ, "Blend Equation", &g_curBlendEquation);
//
// Blending Funcs
//
m_guiRegistry.enumCombobox(COMBO_BLENDFNSRC, "Blend Func SRC", &g_blendSRC);
m_guiRegistry.enumCombobox(COMBO_BLENDFNDST, "Blend Func DST", &g_blendDST);
//
// Global transparency
//
ImGui::Separator();
ImGuiH::InputFloatClamped("Global Alpha", &g_alpha, 0.0, 1.0);
//ImGui::Checkbox("continuous render", &m_realtime.bNonStopRendering);
ImGui::Checkbox("animation", &s_bCameraAnim);
ImGui::Separator();
ImGui::Text("('h' to toggle help)");
//if(s_bStats)
// h += m_oglTextBig.drawString(5, m_winSz[1]-h, hudStats.c_str(), 0, glm::vec4(0.8,0.8,1.0,0.5).vec_array);
if(s_helpText)
{
ImGui::BeginChild("Help", ImVec2(400, 110), true);
// camera help
//ImGui::SetNextWindowCollapsed(0);
const char* txt = getHelpText();
ImGui::Text("'`' or 'u' to toggle UI\n");
ImGui::Text("%s", txt);
ImGui::EndChild();
}
int avg = 100;
if(s_profiler.getTotalFrames() % avg == avg - 1)
{
s_profiler.getAveragedValues("frame", s_statsCpuTime, s_statsGpuTime);
}
float gpuTimeF = float(s_statsGpuTime);
float cpuTimeF = float(s_statsCpuTime);
float maxTimeF = std::max(std::max(cpuTimeF, gpuTimeF), 0.0001f);
ImGui::Text("Frame [ms]: %2.1f", dt * 1000.0f);
ImGui::Text("Scene GPU [ms]: %2.3f", gpuTimeF / 1000.0f);
ImGui::Text("Scene CPU [ms]: %2.3f", cpuTimeF / 1000.0f);
ImGui::ProgressBar(gpuTimeF / maxTimeF, ImVec2(0.0f, 0.0f));
ImGui::ProgressBar(cpuTimeF / maxTimeF, ImVec2(0.0f, 0.0f));
}
ImGui::End();
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// FBO stuff
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
bool CheckFramebufferStatus()
{
GLenum status;
status = (GLenum)glCheckFramebufferStatus(GL_FRAMEBUFFER);
switch(status)
{
case GL_FRAMEBUFFER_COMPLETE:
return true;
case GL_FRAMEBUFFER_UNSUPPORTED:
LOGE("Unsupported framebuffer format\n");
assert(!"Unsupported framebuffer format");
break;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
LOGE("Framebuffer incomplete, missing attachment\n");
assert(!"Framebuffer incomplete, missing attachment");
break;
//case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
// PRINTF(("Framebuffer incomplete, attached images must have same dimensions\n"));
// assert(!"Framebuffer incomplete, attached images must have same dimensions");
// break;
//case GL_FRAMEBUFFER_INCOMPLETE_FORMATS:
// PRINTF(("Framebuffer incomplete, attached images must have same format\n"));
// assert(!"Framebuffer incomplete, attached images must have same format");
// break;
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
LOGE("Framebuffer incomplete, missing draw buffer\n");
assert(!"Framebuffer incomplete, missing draw buffer");
break;
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
LOGE("Framebuffer incomplete, missing read buffer\n");
assert(!"Framebuffer incomplete, missing read buffer");
break;
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
LOGE("Framebuffer incomplete attachment\n");
assert(!"Framebuffer incomplete attachment");
break;
case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
LOGE("Framebuffer incomplete multisample\n");
assert(!"Framebuffer incomplete multisample");
break;
default:
LOGE("Error %x\n", status);
assert(!"unknown FBO Error");
break;
}
return false;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
GLuint createTexture(int w, int h, int samples, int coverageSamples, GLenum intfmt, GLenum fmt)
{
GLuint textureID;
glGenTextures(1, &textureID);
if(samples <= 1)
{
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, intfmt, w, h, 0, GL_RGBA, GL_FLOAT, NULL);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
else
{
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textureID);
// Note: fixed-samples set to GL_TRUE, otherwise it could fail when attaching to FBO having render-buffer !!
if(coverageSamples > 1)
{
glTexImage2DMultisampleCoverageNV(GL_TEXTURE_2D_MULTISAMPLE, coverageSamples, samples, intfmt, w, h, GL_TRUE);
}
else
{
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, intfmt, w, h, GL_TRUE);
}
// Multi-sample textures don't suupport sampler state settings
//glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
//glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
//glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
//glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
return textureID;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
GLuint createTextureRGBA8(int w, int h, int samples, int coverageSamples)
{
return createTexture(w, h, samples, coverageSamples, GL_RGBA8, GL_RGBA);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
GLuint createTextureDST(int w, int h, int samples, int coverageSamples)
{
//return createTexture(w, h, samples, coverageSamples, GL_DEPTH24_STENCIL8, GL_DEPTH24_STENCIL8);
return createTexture(w, h, samples, coverageSamples, GL_STENCIL_INDEX8, GL_STENCIL_INDEX8);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
#ifdef USE_RENDERBUFFERS
GLuint createRenderBuffer(int w, int h, int samples, int coverageSamples, GLenum fmt)
{
int query;
GLuint rb;
glGenRenderbuffers(1, &rb);
glBindRenderbuffer(GL_RENDERBUFFER, rb);
if(coverageSamples)
{
glRenderbufferStorageMultisampleCoverageNV(GL_RENDERBUFFER, coverageSamples, samples, fmt, w, h);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_COVERAGE_SAMPLES_NV, &query);
if(query < coverageSamples)
rb = 0;
else if(query > coverageSamples)
{
// report back the actual number
coverageSamples = query;
LOGW("Warning: coverage samples is now %d\n", coverageSamples);
}
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_COLOR_SAMPLES_NV, &query);
if(query < samples)
rb = 0;
else if(query > samples)
{
// report back the actual number
samples = query;
LOGW("Warning: depth-samples is now %d\n", samples);
}
}
else
{
// create a regular MSAA color buffer
glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, fmt, w, h);
// check the number of samples
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &query);
if(query < samples)
rb = 0;
else if(query > samples)
{
samples = query;
LOGW("Warning: depth-samples is now %d\n", samples);
}
}
glBindRenderbuffer(GL_RENDERBUFFER, 0);
return rb;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
GLuint createRenderBufferRGBA8(int w, int h, int samples, int coverageSamples)
{
return createRenderBuffer(w, h, samples, coverageSamples, GL_RGBA8);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
GLuint createRenderBufferD24S8(int w, int h, int samples, int coverageSamples)
{
return createRenderBuffer(w, h, samples, coverageSamples, GL_DEPTH24_STENCIL8);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
GLuint createRenderBufferS8(int w, int h, int samples, int coverageSamples)
{
return createRenderBuffer(w, h, samples, coverageSamples, GL_STENCIL_INDEX8);
}
#endif
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
GLuint createFBO()
{
GLuint fb;
glGenFramebuffers(1, &fb);
return fb;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool attachTexture2D(GLuint framebuffer, GLuint textureID, int colorAttachment)
{
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + colorAttachment, GL_TEXTURE_2D, textureID, 0);
return CheckFramebufferStatus();
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool attachTexture2DMS(GLuint framebuffer, GLuint textureID, int colorAttachment)
{
if(g_MSAAVal[g_CurMSAAColor] <= 1)
return attachTexture2D(framebuffer, textureID, colorAttachment);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + colorAttachment, GL_TEXTURE_2D_MULTISAMPLE, textureID, 0);
return CheckFramebufferStatus();
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
#ifdef USE_RENDERBUFFERS
bool attachRenderbuffer(GLuint framebuffer, GLuint rb, int colorAttachment)
{
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + colorAttachment, GL_RENDERBUFFER, rb);
return CheckFramebufferStatus();
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool attachDSTRenderbuffer(GLuint framebuffer, GLuint dstrb)
{
bool bRes;
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
//glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, dstrb);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, dstrb);
return CheckFramebufferStatus();
}
#endif
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool attachDSTTexture2D(GLuint framebuffer, GLuint textureDepthID, GLenum target = GL_TEXTURE_2D)
{
bool bRes;
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
//glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, target, textureDepthID, 0);
//bRes = CheckFramebufferStatus();
//if(!bRes) return false;
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, target, textureDepthID, 0);
bRes = CheckFramebufferStatus();
return bRes;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool attachDSTTexture2DMS(GLuint framebuffer, GLuint textureDepthID)
{
return attachDSTTexture2D(framebuffer, textureDepthID, (g_MSAARaster > 1) ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void deleteTexture(GLuint texture)
{
glDeleteTextures(1, &texture);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
#ifdef USE_RENDERBUFFERS
void deleteRenderBuffer(GLuint rb)
{
glDeleteRenderbuffers(1, &rb);
}
#endif
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void deleteFBO(GLuint fbo)
{
glDeleteFramebuffers(1, &fbo);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void blitFBO(GLuint srcFBO, GLuint dstFBO, GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLenum filtering)
{
glBindFramebuffer(GL_READ_FRAMEBUFFER, srcFBO);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dstFBO);
// GL_NEAREST is needed when Stencil/depth are involved
glBlitFramebuffer(srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1,
GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, filtering);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void blitFBONearest(GLuint srcFBO, GLuint dstFBO, GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1)
{
blitFBO(srcFBO, dstFBO, srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, GL_NEAREST);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void blitFBOLinear(GLuint srcFBO, GLuint dstFBO, GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1)
{
blitFBO(srcFBO, dstFBO, srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, GL_LINEAR);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void deleteRenderTargets()
{
if(FBO::TexMS_CMYA_KA_DST)
deleteFBO(FBO::TexMS_CMYA_KA_DST);
if(FBO::Tex_CMYA_KA)
deleteFBO(FBO::Tex_CMYA_KA);
if(FBO::TexMS_CMYA)
deleteFBO(FBO::TexMS_CMYA);
if(FBO::Tex_CMYA)
deleteFBO(FBO::Tex_CMYA);
if(FBO::TexMS_KA)
deleteFBO(FBO::TexMS_KA);
if(FBO::Tex_KA)
deleteFBO(FBO::Tex_KA);
#ifdef USE_RENDERBUFFERS
if(FBO::RbMS)
deleteFBO(FBO::RbMS);
if(FBO::Rb)
deleteFBO(FBO::Rb);
#endif
if(Texture::CMYA)
deleteTexture(Texture::CMYA);
if(Texture::KA)
deleteTexture(Texture::KA);
if(Texture::MS_CMYA)
deleteTexture(Texture::MS_CMYA);
if(Texture::MS_KA)
deleteTexture(Texture::MS_KA);
#ifndef USE_RENDERBUFFERS
if(Texture::MS_DST)
deleteTexture(Texture::MS_DST);
if(Texture::DST)
deleteTexture(Texture::DST);
#else
if(Renderbuffer::RGBA)
deleteRenderBuffer(Renderbuffer::RGBA);
if(Renderbuffer::RGBAMS)
deleteRenderBuffer(Renderbuffer::RGBAMS);
if(Renderbuffer::DST)
deleteRenderBuffer(Renderbuffer::DST);
if(Renderbuffer::DSTMS)
deleteRenderBuffer(Renderbuffer::DSTMS);
#endif
fboSz[0] = 0;
fboSz[1] = 0;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void buildRenderTargets(int w, int h)
{
deleteRenderTargets();
if(g_has_GL_NV_framebuffer_mixed_samples)
{
if(g_MSAARaster < g_MSAAVal[g_CurMSAAColor])
g_MSAARaster = g_MSAAVal[g_CurMSAAColor];
}
else
g_MSAARaster = g_MSAAVal[g_CurMSAAColor];
LOGI("Building Render targets with MSAA Color = %d and MSAA Raster = %d\n", g_MSAAVal[g_CurMSAAColor], g_MSAARaster);
fboSz[0] = w;
fboSz[1] = h;
// a texture
Texture::CMYA = createTextureRGBA8(w, h, 0, 0);
Texture::KA = createTextureRGBA8(w, h, 0, 0);
// a texture in MSAA
Texture::MS_CMYA = createTextureRGBA8(w, h, g_MSAAVal[g_CurMSAAColor], 0);
Texture::MS_KA = createTextureRGBA8(w, h, g_MSAAVal[g_CurMSAAColor], 0); // TODO: RA8
#ifndef USE_RENDERBUFFERS
Texture::DST = createTextureDST(w, h, 0, 0);
Texture::MS_DST = createTextureDST(w, h, g_MSAARaster, 0);
#else
// a renderbuffer
Renderbuffer::RGBA = createRenderBufferRGBA8(w, h, 0, 0);
// a renderbuffer in MSAA
Renderbuffer::RGBAMS = createRenderBufferRGBA8(w, h, g_MSAAVal[g_CurMSAAColor], 0);
// a depth stencil
Renderbuffer::DST = createRenderBufferS8 /*D24S8*/ (w, h, 0, 0);
// a depth stencil in MSAA
Renderbuffer::DSTMS = createRenderBufferS8 /*D24S8*/ (w, h, g_MSAARaster, 0);
#endif
// fbo for texture MSAA as the color buffer
FBO::TexMS_CMYA_KA_DST = createFBO();
{
#ifdef USE_RENDERBUFFERS
attachDSTRenderbuffer(FBO::TexMS_CMYA_KA_DST, Renderbuffer::DSTMS);
#else
attachDSTTexture2DMS(FBO::TexMS_CMYA_KA_DST, Texture::MS_DST);
#endif
attachTexture2DMS(FBO::TexMS_CMYA_KA_DST, Texture::MS_CMYA, 0);
attachTexture2DMS(FBO::TexMS_CMYA_KA_DST, Texture::MS_KA, 1);
}
// fbo for a texture as the color buffer
FBO::Tex_CMYA_KA = createFBO();
{
attachTexture2D(FBO::Tex_CMYA_KA, Texture::CMYA, 0);
attachTexture2D(FBO::Tex_CMYA_KA, Texture::KA, 1);
}
// fbo for Blit operation
FBO::Tex_CMYA = createFBO();
{
attachTexture2D(FBO::Tex_CMYA, Texture::CMYA, 0);
}
FBO::Tex_KA = createFBO();
{
attachTexture2D(FBO::Tex_KA, Texture::KA, 0);
}
FBO::TexMS_CMYA = createFBO();
{
attachTexture2DMS(FBO::TexMS_CMYA, Texture::MS_CMYA, 0);
}
FBO::TexMS_KA = createFBO();
{
attachTexture2DMS(FBO::TexMS_KA, Texture::MS_KA, 0);
}
// fbo for renderbuffer MSAA as the color buffer
#ifdef USE_RENDERBUFFERS
FBO::RbMS = createFBO();
{
attachRenderbuffer(FBO::RbMS, Renderbuffer::RGBAMS, 0);
//attachDSTRenderbuffer(FBO::RbMS, Renderbuffer::DSTMS);
}
// fbo for renderbuffer as the color buffer
FBO::Rb = createFBO();
{
attachRenderbuffer(FBO::Rb, Renderbuffer::RGBA, 0);
attachDSTRenderbuffer(FBO::Rb, Renderbuffer::DST);
}
#endif
// build a VBO for the size of the FBO
//
// make a VBO for Quad
//
if(g_vboQuad == 0)
glGenBuffers(1, &g_vboQuad);
glBindBuffer(GL_ARRAY_BUFFER, g_vboQuad);
int vertices[2 * 4] = {0, 0, w, 0, 0, h, w, h};
glBufferData(GL_ARRAY_BUFFER, sizeof(int) * 2 * 4, vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------