forked from sonyomega/gl_commandlist_bk3d_models
/
gl_commandlist_bk3d_models.cpp
1794 lines (1650 loc) · 60.6 KB
/
gl_commandlist_bk3d_models.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
/*-----------------------------------------------------------------------
Copyright (c) 2013, NVIDIA. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Neither the name of its contributors may be used to endorse
or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
feedback to tlorach@nvidia.com (Tristan Lorach)
*/ //--------------------------------------------------------------------
#include "gl_commandlist_bk3d_models.h"
#include "NVFBOBox.h"
#include "AntTweakBar.h"
#include <list>
#define GRIDDEF 20
#define GRIDSZ 1.0f
#define CROSSSZ 0.01f
#ifdef NOGZLIB
# define MODELNAME "SubMarine_134.bk3d"
# define MODELNAMEBACKUP "Smobby_134.bk3d"
#else
# define MODELNAME "SubMarine_134.bk3d.gz"
# define MODELNAMEBACKUP "Smobby_134.bk3d.gz"
#endif
//-----------------------------------------------------------------------------
// Derive the Window for this sample
//-----------------------------------------------------------------------------
class MyWindow: public WindowInertiaCamera
{
public:
NVFBOBox m_fboBox;
NVFBOBox::DownSamplingTechnique downsamplingMode;
MyWindow();
virtual bool init();
virtual void shutdown();
virtual void reshape(int w, int h);
virtual void motion(int x, int y);
virtual void mousewheel(short delta);
virtual void mouse(NVPWindow::MouseButton button, ButtonAction action, int mods, int x, int y);
//virtual void menu(int m);
virtual void keyboard(MyWindow::KeyCode key, ButtonAction action, int mods, int x, int y);
virtual void keyboardchar(unsigned char key, int mods, int x, int y);
//virtual void idle();
virtual void display();
void readConfigFile(const char* fname);
};
MyWindow::MyWindow() :
WindowInertiaCamera(vec3f(0.0f,1.0f,-3.0f),
vec3f(0,-0.4,0.33)) // target adjusted to SubMarine_134.bk3d.gz
, downsamplingMode(NVFBOBox::DS2)
{
}
static MyWindow g_myWindow;
//-----------------------------------------------------------------------------
// Grid
//-----------------------------------------------------------------------------
static const char *g_glslv_grid =
"#version 430\n"
"#extension GL_ARB_separate_shader_objects : enable\n"
"#extension GL_NV_command_list : enable\n"
"layout(std140,commandBindableNV,binding=" TOSTR(UBO_MATRIX) ") uniform matrixBuffer {\n"
" uniform mat4 mW;\n"
" uniform mat4 mVP;\n"
"} matrix;\n"
"layout(location=0) in vec3 P;\n"
"out gl_PerVertex {\n"
" vec4 gl_Position;\n"
"};\n"
"void main() {\n"
" gl_Position = matrix.mVP * (matrix.mW * ( vec4(P, 1.0)));\n"
"}\n"
;
static const char *g_glslf_grid =
"#version 430\n"
"#extension GL_ARB_separate_shader_objects : enable\n"
"#extension GL_NV_command_list : enable\n"
"layout(location=0) out vec4 outColor;\n"
"void main() {\n"
" outColor = vec4(0.5,0.7,0.5,1);\n"
"}\n"
;
//-----------------------------------------------------------------------------
// Help
//-----------------------------------------------------------------------------
static const char* s_sampleHelp =
"space: toggles continuous rendering\n"
"'c': use Commandlist\n"
"'e': use Commandlist EMULATION\n"
"'l': use glCallCommandListNV\n"
"'o': toggles object display\n"
"'g': toggles grid display\n"
"'s': toggle stats\n"
"'a': animate camera\n"
"'u': toggle UI overlay\n"
;
static const char* s_sampleHelpCmdLine =
"---------- Cmd-line arguments ----------\n"
"-v <VBO max Size>\n-m <bk3d model>\n"
"-c 0 or 1 : use command-lists\n"
"-b 0 or 1 : use bindless when no cmd list\n"
"-o 0 or 1 : display meshes\n"
"-g 0 or 1 : display grid\n"
"-s 0 or 1 : stats\n"
"-a 0 or 1 : animate camera\n"
"-u 0 or 1 : UI overlay\n"
"-i <file> : use a config file to load models and setup camera animation\n"
"-d 0 or 1 : debug stuff (ui)\n"
"-m <bk3d file> : load a specific model\n"
"<bk3d> : load a specific model\n"
"-q <msaa> : MSAA\n"
"-r <ss_val> : supersampling (1.0,1.5,2.0)\n"
"----------------------------------------\n"
;
//-----------------------------------------------------------------------------
// Prototype(s)
//-----------------------------------------------------------------------------
void updateViewportTokenBufferAndLineWidth(GLint x, GLint y, GLsizei width, GLsizei height, float lineW);
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
#ifdef USESVCUI
IWindowFolding* g_pTweakContainer = NULL;
#endif
TwBar* tweakBar = NULL;
nv_helpers::Profiler g_profiler;
nv_helpers_gl::ProfilerTimersGL g_gltimers;
GLSLShader g_shaderGrid;
bool g_bUseCommandLists = false;
bool g_bUseEmulation = false;
bool g_bUseCallCommandListNV = false;
bool g_bUseGridBindless = false;
bool g_bDisplayObject = true;
bool g_bRotateOx90 = true;
bool g_bWireframe = false;
float g_Supersampling = 1.0f;
int g_firstMesh = 0;
MatrixBufferGlobal g_globalMatrices;
BO g_uboMatrix = {0,0,0};
BO g_uboLight = {0,0,0};
TokenBuffer g_tokenBufferViewport;
//-----------------------------------------------------------------------------
// Static variables for setting up the scene...
//-----------------------------------------------------------------------------
std::vector<Bk3dModel*> s_bk3dModels;
#define FOREACHMODEL(cmd) {\
for(int m=0;m<s_bk3dModels.size(); m++) {\
s_bk3dModels[m]->cmd ;\
}\
}
static int s_curObject = 0;
static bool s_bCreateDebugUI = false;
static bool s_bShowAntTweakBar = true;
static int s_MSAA = 8;
//
// Camera animation: captured using '1' in the sample. Then copy and paste...
//
struct CameraAnim { vec3f eye, focus; float sleep; };
static std::vector<CameraAnim> s_cameraAnim;
static int s_cameraAnimItem = 0;
static int s_cameraAnimItems = 15;
static float s_cameraAnimIntervals= 0.1;
static bool s_bCameraAnim = false;
#define HELPDURATION 5.0
static float s_helpText = 0.0;
static int s_maxItems = -1;
static bool s_bDisplayGrid = true;
static bool s_bRecordGrid = true;
static bool s_bStats = false;
static GLuint s_header[GL_MAX_COMMANDS_NV] = {0};
static GLuint s_headerSizes[GL_MAX_COMMANDS_NV] = {0};
static GLuint s_vboGrid;
static GLuint s_vboGridSz;
static GLuint64 s_vboGridAddr;
static GLuint s_vboCross;
static GLuint s_vboCrossSz;
static GLuint64 s_vboCrossAddr;
static MaterialBuffer* s_material = NULL;
static int s_materialNItems = 0;
static LightBuffer s_light = { vec3f(0.4f,0.8f,0.3f) };
static GLuint s_vao = 0;
static CommandStatesBatch s_commandGrid;
static TokenBuffer s_tokenBufferGrid;
//------------------------------------------------------------------------------
// 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;
static Messages s_messages;
//------------------------------------------------------------------------------
void sample_print(int level, const char * txt)
{
// normally we should enter a critical section...
s_messages.push_back(LogMessage(level, txt) );
}
//-----------------------------------------------------------------------------
// Useful stuff for Command-list
//-----------------------------------------------------------------------------
static GLushort s_stages[STAGES];
struct Token_Nop {
static const GLenum ID = GL_NOP_COMMAND_NV;
NOPCommandNV cmd;
Token_Nop() {
cmd.header = s_header[ID];
}
};
struct Token_TerminateSequence {
static const GLenum ID = GL_TERMINATE_SEQUENCE_COMMAND_NV;
TerminateSequenceCommandNV cmd;
Token_TerminateSequence() {
cmd.header = s_header[ID];
}
};
struct Token_DrawElemsInstanced {
static const GLenum ID = GL_DRAW_ELEMENTS_INSTANCED_COMMAND_NV;
DrawElementsInstancedCommandNV cmd;
Token_DrawElemsInstanced() {
cmd.baseInstance = 0;
cmd.baseVertex = 0;
cmd.firstIndex = 0;
cmd.count = 0;
cmd.instanceCount = 1;
cmd.header = s_header[ID];
}
};
struct Token_DrawArraysInstanced {
static const GLenum ID = GL_DRAW_ARRAYS_INSTANCED_COMMAND_NV;
DrawArraysInstancedCommandNV cmd;
Token_DrawArraysInstanced() {
cmd.baseInstance = 0;
cmd.first = 0;
cmd.count = 0;
cmd.instanceCount = 1;
cmd.header = s_header[ID];
}
};
struct Token_DrawElements {
static const GLenum ID = GL_DRAW_ELEMENTS_COMMAND_NV;
DrawElementsCommandNV cmd;
Token_DrawElements() {
cmd.baseVertex = 0;
cmd.firstIndex = 0;
cmd.count = 0;
cmd.header = s_header[ID];
}
};
struct Token_DrawArrays {
static const GLenum ID = GL_DRAW_ARRAYS_COMMAND_NV;
DrawArraysCommandNV cmd;
Token_DrawArrays() {
cmd.first = 0;
cmd.count = 0;
cmd.header = s_header[ID];
}
};
struct Token_DrawElementsStrip {
static const GLenum ID = GL_DRAW_ELEMENTS_STRIP_COMMAND_NV;
DrawElementsCommandNV cmd;
Token_DrawElementsStrip() {
cmd.baseVertex = 0;
cmd.firstIndex = 0;
cmd.count = 0;
cmd.header = s_header[ID];
}
};
struct Token_DrawArraysStrip {
static const GLenum ID = GL_DRAW_ARRAYS_STRIP_COMMAND_NV;
DrawArraysCommandNV cmd;
Token_DrawArraysStrip() {
cmd.first = 0;
cmd.count = 0;
cmd.header = s_header[ID];
}
};
struct Token_AttributeAddress {
static const GLenum ID = GL_ATTRIBUTE_ADDRESS_COMMAND_NV;
AttributeAddressCommandNV cmd;
Token_AttributeAddress() {
cmd.header = s_header[ID];
}
};
struct Token_ElementAddress {
static const GLenum ID = GL_ELEMENT_ADDRESS_COMMAND_NV;
ElementAddressCommandNV cmd;
Token_ElementAddress() {
cmd.header = s_header[ID];
}
};
struct Token_UniformAddress {
static const GLenum ID = GL_UNIFORM_ADDRESS_COMMAND_NV;
UniformAddressCommandNV cmd;
Token_UniformAddress() {
cmd.header = s_header[ID];
}
};
struct Token_BlendColor{
static const GLenum ID = GL_BLEND_COLOR_COMMAND_NV;
BlendColorCommandNV cmd;
Token_BlendColor() {
cmd.header = s_header[ID];
}
};
struct Token_StencilRef{
static const GLenum ID = GL_STENCIL_REF_COMMAND_NV;
StencilRefCommandNV cmd;
Token_StencilRef() {
cmd.header = s_header[ID];
}
} ;
struct Token_LineWidth{
static const GLenum ID = GL_LINE_WIDTH_COMMAND_NV;
LineWidthCommandNV cmd;
Token_LineWidth() {
cmd.header = s_header[ID];
}
};
struct Token_PolygonOffset{
static const GLenum ID = GL_POLYGON_OFFSET_COMMAND_NV;
PolygonOffsetCommandNV cmd;
Token_PolygonOffset() {
cmd.header = s_header[ID];
}
};
struct Token_AlphaRef{
static const GLenum ID = GL_ALPHA_REF_COMMAND_NV;
AlphaRefCommandNV cmd;
Token_AlphaRef() {
cmd.header = s_header[ID];
}
};
struct Token_Viewport{
static const GLenum ID = GL_VIEWPORT_COMMAND_NV;
ViewportCommandNV cmd;
Token_Viewport() {
cmd.header = s_header[ID];
}
};
struct Token_Scissor {
static const GLenum ID = GL_SCISSOR_COMMAND_NV;
ScissorCommandNV cmd;
Token_Scissor() {
cmd.header = s_header[ID];
}
};
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
template <class T>
void registerSize()
{
s_headerSizes[T::ID] = sizeof(T);
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void initTokenInternals()
{
registerSize<Token_TerminateSequence>();
registerSize<Token_Nop>();
registerSize<Token_DrawElements>();
registerSize<Token_DrawArrays>();
registerSize<Token_DrawElementsStrip>();
registerSize<Token_DrawArraysStrip>();
registerSize<Token_DrawElemsInstanced>();
registerSize<Token_DrawArraysInstanced>();
registerSize<Token_AttributeAddress>();
registerSize<Token_ElementAddress>();
registerSize<Token_UniformAddress>();
registerSize<Token_LineWidth>();
registerSize<Token_PolygonOffset>();
registerSize<Token_Scissor>();
registerSize<Token_BlendColor>();
registerSize<Token_Viewport>();
registerSize<Token_AlphaRef>();
registerSize<Token_StencilRef>();
for (int i = 0; i < GL_MAX_COMMANDS_NV; i++){
// using i instead of a table of token IDs because the are arranged in the same order as i incrementing.
// shortcut for the source code. See gl_nv_command_list.h
s_header[i] = glGetCommandHeaderNV(i/*==Token enum*/,s_headerSizes[i]);
}
emucmdlist::InitHeaders(s_header, s_headerSizes);
s_stages[STAGE_VERTEX] = glGetStageIndexNV(GL_VERTEX_SHADER);
s_stages[STAGE_TESS_CONTROL] = glGetStageIndexNV(GL_TESS_CONTROL_SHADER);
s_stages[STAGE_TESS_EVALUATION] = glGetStageIndexNV(GL_TESS_EVALUATION_SHADER);
s_stages[STAGE_GEOMETRY] = glGetStageIndexNV(GL_GEOMETRY_SHADER);
s_stages[STAGE_FRAGMENT] = glGetStageIndexNV(GL_FRAGMENT_SHADER);
}
//------------------------------------------------------------------------------
// build
//------------------------------------------------------------------------------
std::string buildLineWidthCommand(float w)
{
std::string cmd;
Token_LineWidth lw;
lw.cmd.lineWidth = w;
cmd = std::string((const char*)&lw,sizeof(Token_LineWidth));
return cmd;
}
//------------------------------------------------------------------------------
// build
//------------------------------------------------------------------------------
std::string buildUniformAddressCommand(int idx, GLuint64 p, GLsizeiptr sizeBytes, ShaderStages stage)
{
std::string cmd;
Token_UniformAddress attr;
attr.cmd.stage = s_stages[stage];
attr.cmd.index = idx;
((GLuint64EXT*)&attr.cmd.addressLo)[0] = p;
cmd = std::string((const char*)&attr,sizeof(Token_UniformAddress));
return cmd;
}
//------------------------------------------------------------------------------
// build
//------------------------------------------------------------------------------
std::string buildAttributeAddressCommand(int idx, GLuint64 p, GLsizeiptr sizeBytes)
{
std::string cmd;
Token_AttributeAddress attr;
attr.cmd.index = idx;
((GLuint64EXT*)&attr.cmd.addressLo)[0] = p;
cmd = std::string((const char*)&attr,sizeof(Token_AttributeAddress));
return cmd;
}
//------------------------------------------------------------------------------
// build
//------------------------------------------------------------------------------
std::string buildElementAddressCommand(GLuint64 ptr, GLenum indexFormatGL)
{
std::string cmd;
Token_ElementAddress attr;
((GLuint64EXT*)&attr.cmd.addressLo)[0] = ptr;
switch(indexFormatGL)
{
case GL_UNSIGNED_INT:
attr.cmd.typeSizeInByte = 4;
break;
case GL_UNSIGNED_SHORT:
attr.cmd.typeSizeInByte = 2;
break;
}
cmd = std::string((const char*)&attr,sizeof(Token_AttributeAddress));
return cmd;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
std::string buildDrawElementsCommand(GLenum topologyGL, GLuint indexCount)
{
std::string cmd;
Token_DrawElements dc;
Token_DrawElementsStrip dcstrip;
switch(topologyGL)
{
case GL_TRIANGLE_STRIP:
case GL_QUAD_STRIP:
case GL_LINE_STRIP:
dcstrip.cmd.baseVertex = 0;
dcstrip.cmd.firstIndex = 0;
dcstrip.cmd.count = indexCount;
cmd = std::string((const char*)&dcstrip,sizeof(Token_DrawElementsStrip));
break;
default:
dc.cmd.baseVertex = 0;
dc.cmd.firstIndex = 0;
dc.cmd.count = indexCount;
cmd = std::string((const char*)&dc,sizeof(Token_DrawElements));
break;
}
return cmd;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
std::string buildDrawArraysCommand(GLenum topologyGL, GLuint indexCount)
{
std::string cmd;
Token_DrawArrays dc;
Token_DrawArraysStrip dcstrip;
switch(topologyGL)
{
case GL_TRIANGLE_STRIP:
case GL_QUAD_STRIP:
case GL_LINE_STRIP:
dcstrip.cmd.first = 0;
dcstrip.cmd.count = indexCount;
cmd = std::string((const char*)&dcstrip,sizeof(Token_DrawArraysStrip));
break;
default:
dc.cmd.first = 0;
dc.cmd.count = indexCount;
cmd = std::string((const char*)&dc,sizeof(Token_DrawArrays));
break;
}
return cmd;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
std::string buildViewportCommand(GLint x, GLint y, GLsizei width, GLsizei height)
{
std::string cmd;
Token_Viewport dc;
dc.cmd.x = x;
dc.cmd.y = y;
dc.cmd.width = width;
dc.cmd.height = height;
cmd = std::string((const char*)&dc,sizeof(Token_Viewport));
return cmd;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
std::string buildBlendColorCommand(GLclampf red,GLclampf green,GLclampf blue,GLclampf alpha)
{
std::string cmd;
Token_BlendColor dc;
dc.cmd.red = red;
dc.cmd.green = green;
dc.cmd.blue = blue;
dc.cmd.alpha = alpha;
cmd = std::string((const char*)&dc,sizeof(Token_BlendColor));
return cmd;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
std::string buildStencilRefCommand(GLuint frontStencilRef, GLuint backStencilRef)
{
std::string cmd;
Token_StencilRef dc;
dc.cmd.frontStencilRef = frontStencilRef;
dc.cmd.backStencilRef = backStencilRef;
cmd = std::string((const char*)&dc,sizeof(Token_StencilRef));
return cmd;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
std::string buildPolygonOffsetCommand(GLfloat scale, GLfloat bias)
{
std::string cmd;
Token_PolygonOffset dc;
dc.cmd.bias = bias;
dc.cmd.scale = scale;
cmd = std::string((const char*)&dc,sizeof(Token_PolygonOffset));
return cmd;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
std::string buildScissorCommand(GLint x, GLint y, GLsizei width, GLsizei height)
{
std::string cmd;
Token_Scissor dc;
dc.cmd.x = x;
dc.cmd.y = y;
dc.cmd.width = width;
dc.cmd.height = height;
cmd = std::string((const char*)&dc,sizeof(Token_Scissor));
return cmd;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool initBuffersGrid()
{
//
// Grid floor
//
glGenBuffers(1, &s_vboGrid);
vec3f *data = new vec3f[GRIDDEF*4];
vec3f *p = data;
int j=0;
for(int i=0; i<GRIDDEF; i++)
{
*(p++) = vec3f(-GRIDSZ, 0.0, GRIDSZ*(-1.0f+2.0f*(float)i/(float)GRIDDEF));
*(p++) = vec3f( GRIDSZ*(1.0f-2.0f/(float)GRIDDEF), 0.0, GRIDSZ*(-1.0f+2.0f*(float)i/(float)GRIDDEF));
*(p++) = vec3f(GRIDSZ*(-1.0f+2.0f*(float)i/(float)GRIDDEF), 0.0, -GRIDSZ);
*(p++) = vec3f(GRIDSZ*(-1.0f+2.0f*(float)i/(float)GRIDDEF), 0.0, GRIDSZ*(1.0f-2.0f/(float)GRIDDEF));
}
s_vboGridSz = sizeof(vec3f)*GRIDDEF*4;
glNamedBufferDataEXT(s_vboGrid,s_vboGridSz , data[0].vec_array, GL_STATIC_DRAW);
// make the buffer resident and get its pointer
glGetNamedBufferParameterui64vNV(s_vboGrid, GL_BUFFER_GPU_ADDRESS_NV, &s_vboGridAddr);
glMakeNamedBufferResidentNV(s_vboGrid, GL_READ_ONLY);
delete [] data;
//
// Target Cross
//
glGenBuffers(1, &s_vboCross);
vec3f crossVtx[6] = {
vec3f(-CROSSSZ, 0.0f, 0.0f), vec3f(CROSSSZ, 0.0f, 0.0f),
vec3f(0.0f, -CROSSSZ, 0.0f), vec3f(0.0f, CROSSSZ, 0.0f),
vec3f(0.0f, 0.0f, -CROSSSZ), vec3f(0.0f, 0.0f, CROSSSZ),
};
s_vboCrossSz = sizeof(vec3f)*6;
glNamedBufferDataEXT(s_vboCross,s_vboCrossSz , crossVtx[0].vec_array, GL_STATIC_DRAW);
// make the buffer resident and get its pointer
glGetNamedBufferParameterui64vNV(s_vboCross, GL_BUFFER_GPU_ADDRESS_NV, &s_vboCrossAddr);
glMakeNamedBufferResidentNV(s_vboCross, GL_READ_WRITE);
return true;
}
//------------------------------------------------------------------------------
// cleanup commandList for the Grid
//------------------------------------------------------------------------------
void cleanTokenBufferGrid()
{
glDeleteBuffers(1, &s_tokenBufferGrid.bufferID);
for(int i=0; i<s_commandGrid.stateGroups.size(); i++)
glDeleteStatesNV(1, &s_commandGrid.stateGroups[i]);
s_tokenBufferGrid.bufferID = 0;
s_tokenBufferGrid.data.clear();
s_commandGrid.clear();
s_bRecordGrid = true;
}
//------------------------------------------------------------------------------
// build commandList for the Grid
//------------------------------------------------------------------------------
bool recordTokenBufferGrid(GLuint fbo)
{
cleanTokenBufferGrid();
GLuint stateId;
g_shaderGrid.bindShader();
//
// enable/disable vertex attributes for our needs
//
glEnableVertexAttribArray(0);
for(int i=1; i<16; i++)
glDisableVertexAttribArray(i);
glEnableClientState(GL_VERTEX_ATTRIB_ARRAY_UNIFIED_NV);
glEnableClientState(GL_ELEMENT_ARRAY_UNIFIED_NV);
glEnableClientState(GL_UNIFORM_BUFFER_UNIFIED_NV);
glBindBufferBase(GL_UNIFORM_BUFFER,UBO_MATRIX, 0); // put them to zero for debug purpose
glBindBufferBase(GL_UNIFORM_BUFFER,UBO_LIGHT, 0);
//
// Bind the VBO essentially for the *stride information*
//
glBindVertexBuffer(0, s_vboGrid, 0, sizeof(vec3f));
//
// Vertex attribute format
//
glVertexAttribFormat(0, 3, GL_FLOAT, GL_FALSE, 0);
//
// build address command for attribute #0
// - assign UBO addresses to uniform index for a given shader stage
// - assign a VBO address to attribute 0 (only one needed here)
// - issue draw commands
//
std::string data = buildUniformAddressCommand(UBO_MATRIX, g_uboMatrix.Addr, g_uboMatrix.Sz, STAGE_VERTEX);
data += buildUniformAddressCommand(UBO_LIGHT, g_uboLight.Addr, g_uboLight.Sz, STAGE_FRAGMENT);
data += buildAttributeAddressCommand(0, s_vboGridAddr, s_vboGridSz);
data += buildDrawArraysCommand(GL_LINES, GRIDDEF*4);
//
// build another drawcall for the target cross
//
data += buildLineWidthCommand(4.0);
data += buildAttributeAddressCommand(0, s_vboCrossAddr, s_vboCrossSz);
data += buildDrawArraysCommand(GL_LINES, 6);
s_tokenBufferGrid.data = data; // token buffer containing commands
//
// Create a state and capture the state-machine of OpenGL
// *ALL* previously declared states will be taken, plus the topology passed as argument
//
glCreateStatesNV(1, &stateId);
glStateCaptureNV(stateId, GL_LINES);
emucmdlist::StateCaptureNV(stateId, GL_LINES); // for emulation purpose
emucmdlist::StateCaptureNV_Extra(stateId, sizeof(vec3f), 3,0, 0,0,0); // for emulation purpose
//
// Generate the token buffer in which we copy s_tokenBufferGrid.data
//
glGenBuffers(1, &s_tokenBufferGrid.bufferID);
glNamedBufferDataEXT(s_tokenBufferGrid.bufferID, data.size(), &data[0], GL_STATIC_DRAW);
glGetNamedBufferParameterui64vNV(s_tokenBufferGrid.bufferID, GL_BUFFER_GPU_ADDRESS_NV, &s_tokenBufferGrid.bufferAddr);
glMakeNamedBufferResidentNV(s_tokenBufferGrid.bufferID, GL_READ_WRITE);
//
// Build the tables for the command-state batch
//
// token buffer for the viewport setting
s_commandGrid.pushBatch(stateId, fbo,
g_tokenBufferViewport.bufferAddr,
&g_tokenBufferViewport.data[0],
g_tokenBufferViewport.data.size() );
// token buffer for drawing the grid
s_commandGrid.pushBatch(stateId, fbo,
s_tokenBufferGrid.bufferAddr,
&s_tokenBufferGrid.data[0],
s_tokenBufferGrid.data.size() );
LOGOK("Token buffer created for Grid\n");
LOGFLUSH();
s_bRecordGrid = false; // done recording
glDisableClientState(GL_VERTEX_ATTRIB_ARRAY_UNIFIED_NV);
glDisableClientState(GL_ELEMENT_ARRAY_UNIFIED_NV);
glDisableClientState(GL_UNIFORM_BUFFER_UNIFIED_NV);
return true;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void displayGrid(const InertiaCamera& camera, const mat4f projection, GLuint fbo)
{
//
// Update what is inside buffers
//
g_globalMatrices.mVP = projection * camera.m4_view;
g_globalMatrices.mW = mat4f(array16_id);
glNamedBufferSubDataEXT(g_uboMatrix.Id, 0, sizeof(g_globalMatrices), &g_globalMatrices);
//
// The cross vertex change is an example on how command-list are compatible with changing
// what is inside the vertex buffers. VBOs are outside of the token buffers...
//
const vec3f& p = camera.curFocusPos;
vec3f crossVtx[6] = {
vec3f(p.x-CROSSSZ, p.y, p.z), vec3f(p.x+CROSSSZ, p.y, p.z),
vec3f(p.x, p.y-CROSSSZ, p.z), vec3f(p.x, p.y+CROSSSZ, p.z),
vec3f(p.x, p.y, p.z-CROSSSZ), vec3f(p.x, p.y, p.z+CROSSSZ),
};
glNamedBufferSubDataEXT(s_vboCross, 0, sizeof(vec3f)*6, crossVtx);
// ------------------------------------------------------------------------------------------
// Case of recorded command-list
//
if(g_bUseCommandLists)
{
//
// Record draw commands if not already done
//
if(s_bRecordGrid)
recordTokenBufferGrid(fbo);
//
// execute the commands from the token buffer
//
if(g_bUseEmulation)
{
//
// an emulation of what got captured
//
emucmdlist::nvtokenRenderStatesSW(&s_commandGrid.dataPtrs[0], &s_commandGrid.sizes[0],
&s_commandGrid.stateGroups[0], &s_commandGrid.fbos[0], int(s_commandGrid.numItems ) );
} else {
//
// real Command-list's Token buffer with states execution
//
glDrawCommandsStatesAddressNV(
&s_commandGrid.dataGPUPtrs[0],
&s_commandGrid.sizes[0],
&s_commandGrid.stateGroups[0],
&s_commandGrid.fbos[0],
int(s_commandGrid.numItems ));
}
return;
}
// ------------------------------------------------------------------------------------------
// Case of regular rendering
//
g_shaderGrid.bindShader();
glEnableVertexAttribArray(0);
glDisableVertexAttribArray(1);
if(g_bUseGridBindless)
{
// --------------------------------------------------------------------------------------
// Using NVIDIA VBUM
//
glBindBufferBase(GL_UNIFORM_BUFFER,UBO_MATRIX, 0); // put them to zero for debug purpose
glBindBufferBase(GL_UNIFORM_BUFFER,UBO_LIGHT, 0);
//
// Enable Bindless
//
glEnableClientState(GL_VERTEX_ATTRIB_ARRAY_UNIFIED_NV);
glEnableClientState(GL_ELEMENT_ARRAY_UNIFIED_NV);
glEnableClientState(GL_UNIFORM_BUFFER_UNIFIED_NV);
//glDisableClientState(GL_UNIFORM_BUFFER_UNIFIED_NV);
//glBindBufferBase(GL_UNIFORM_BUFFER,UBO_MATRIX, g_uboMatrix.Id);
//glBindBufferBase(GL_UNIFORM_BUFFER,UBO_LIGHT, g_uboLight.Id);
glBufferAddressRangeNV(GL_VERTEX_ATTRIB_ARRAY_ADDRESS_NV, 0, s_vboGridAddr, s_vboGridSz);
glBufferAddressRangeNV(GL_UNIFORM_BUFFER_ADDRESS_NV, UBO_MATRIX, g_uboMatrix.Addr, g_uboMatrix.Sz);
//glBufferAddressRangeNV(GL_UNIFORM_BUFFER_ADDRESS_NV, UBO_LIGHT, g_uboLight.Addr, g_uboLight.Sz); // No need
// debug test: is alignment good ?
//{
// int offsetAlignment;
// glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &offsetAlignment);
// assert(sizeof(MaterialBuffer) == offsetAlignment);
//}
// BUG? Need to add this for proper stride
glBindVertexBuffer(0, s_vboGrid, 0, sizeof(vec3f));
glVertexAttribFormat(0,3, GL_FLOAT, GL_FALSE, 0);
// or this one does have the stride:
//glVertexAttribFormatNV(0, 3, GL_FLOAT, GL_FALSE, sizeof(vec3f));
glDrawArrays(GL_LINES, 0, GRIDDEF*4);
glBufferAddressRangeNV(GL_VERTEX_ATTRIB_ARRAY_ADDRESS_NV, 0, s_vboCrossAddr, s_vboCrossSz);
glDrawArrays(GL_LINES, 0, 6);
glDisableClientState(GL_VERTEX_ATTRIB_ARRAY_UNIFIED_NV);
glDisableClientState(GL_ELEMENT_ARRAY_UNIFIED_NV);
glDisableClientState(GL_UNIFORM_BUFFER_UNIFIED_NV);
} else {
// --------------------------------------------------------------------------------------
// Using regular VBO
//
glBindBufferBase(GL_UNIFORM_BUFFER,UBO_MATRIX, g_uboMatrix.Id);
glBindBufferBase(GL_UNIFORM_BUFFER,UBO_LIGHT, g_uboLight.Id);
glBindVertexBuffer(0, s_vboGrid, 0, sizeof(vec3f));
glVertexAttribFormat(0, 3, GL_FLOAT, GL_FALSE, 0);
//
// Draw!
//
glDrawArrays(GL_LINES, 0, GRIDDEF*4);
glBindVertexBuffer(0, s_vboCross, 0, sizeof(vec3f));
glDrawArrays(GL_LINES, 0, 6);
}
glDisableVertexAttribArray(0);
//g_shaderGrid.unbindShader();
}
//------------------------------------------------------------------------------
// AntTweakBar Callbacks
//------------------------------------------------------------------------------
void TW_CALL errorHandler(const char *errorMessage)
{
static bool inMessage = false;
if(inMessage)
return;
inMessage = true;
LOGW(errorMessage);
inMessage = false;
}
void TW_CALL setCLModeCB(const void *value, void * /*clientData*/)
{
g_TokenBufferGrouping = ((int*)value)[0];
}
void TW_CALL getCLModeCB(void *value, void * /*clientData*/)
{
((int *)value)[0] = g_TokenBufferGrouping;
}
void TW_CALL setMSAAModeCB(const void *value, void * clientData)
{
s_MSAA = ((int*)value)[0];
MyWindow* p = reinterpret_cast<MyWindow*>(clientData);
p->m_fboBox.resize(p->m_winSz[0], p->m_winSz[1], g_Supersampling, s_MSAA);
p->m_fboBox.MakeResourcesResident();
FOREACHMODEL(update_fbo_target(p->m_fboBox.GetFBO()));
// the comman-list needs to be rebuilt when FBO's resources changed
#if 1
FOREACHMODEL(invalidateCmdList());
s_bRecordGrid = true;
#else
init_command_list();
#endif
}
void TW_CALL getMSAAModeCB(void *value, void * /*clientData*/)
{
((int *)value)[0] = s_MSAA;
}
void TW_CALL setSSModeCB(const void *value, void * clientData)
{
g_Supersampling = 0.1f * (float)((int*)value)[0];
MyWindow* p = reinterpret_cast<MyWindow*>(clientData);
p->m_fboBox.resize(p->m_winSz[0], p->m_winSz[1], g_Supersampling, s_MSAA);
p->m_fboBox.MakeResourcesResident();
//
// update the token buffer in which the viewport setup happens for token rendering
//
updateViewportTokenBufferAndLineWidth(0,0,p->m_fboBox.getBufferWidth(),p->m_fboBox.getBufferHeight(), g_Supersampling);
//
// remember that commands must know which FBO is targeted
//
FOREACHMODEL(update_fbo_target(p->m_fboBox.GetFBO()));
// the comman-list needs to be rebuilt when FBO's resources changed
#if 1
FOREACHMODEL(invalidateCmdList());
s_bRecordGrid = true;
#else