-
Notifications
You must be signed in to change notification settings - Fork 17
/
bk3d_glstandard.cpp
1083 lines (1014 loc) · 36.9 KB
/
bk3d_glstandard.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) 2016-2023, 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) 2016-2021 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
#include <glm/gtc/type_ptr.hpp>
#define EXTERNSVCUI
#define WINDOWINERTIACAMERA_EXTERN
#define EMUCMDLIST_EXTERN
#include "gl_vk_bk3dthreaded.h"
#include "helper_fbo.h"
#include <nvgl/profiler_gl.hpp>
#define GRIDDEF 20
#define GRIDSZ 1.0f
#define CROSSSZ 0.01f
namespace glstandard {
//------------------------------------------------------------------------------
// Globals
//------------------------------------------------------------------------------
GLuint g_MaxBOSz = 200000;
int g_TokenBufferGrouping = 0;
//-----------------------------------------------------------------------------
// Shaders
//-----------------------------------------------------------------------------
static const char *s_glslv_mesh =
"#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"
" vec3 eyePos;\n"
"} matrix;\n"
"layout(std140,commandBindableNV,binding=" TOSTR(UBO_MATRIXOBJ) ") uniform matrixObjBuffer {\n"
" uniform mat4 mO;\n"
"} object;\n"
"layout(location=0) in vec3 pos;\n"
"layout(location=1) in vec3 N;\n"
"\n"
"layout(location=1) out vec3 outN;\n"
"layout(location=2) out vec3 outWPos;\n"
"layout(location=3) out vec3 outEyePos;\n"
"out gl_PerVertex {\n"
" vec4 gl_Position;\n"
"};\n"
"void main()\n"
"{\n"
" outN = N.xzy;\n"
" vec4 wpos = matrix.mW * (object.mO * vec4(pos,1)); \n"
" gl_Position = matrix.mVP * wpos;\n"
" outWPos = wpos.xyz;\n"
" outEyePos = matrix.eyePos;\n"
"}\n"
;
static const char *s_glslf_mesh =
"#version 430\n"
"#extension GL_ARB_separate_shader_objects : enable\n"
"#extension GL_NV_command_list : enable\n"
"layout(std140,commandBindableNV,binding=" TOSTR(UBO_MATERIAL) ") uniform materialBuffer {\n"
" uniform vec3 diffuse;"
"} material;\n"
"layout(std140,commandBindableNV,binding=" TOSTR(UBO_LIGHT) ") uniform lightBuffer {\n"
" uniform vec3 dir;"
"} light;\n"
"layout(location=1) in vec3 N;\n"
"layout(location=2) in vec3 inWPos;\n"
"layout(location=3) in vec3 inEyePos;\n"
"layout(location=0,index=0) out vec4 outColor;\n"
"\n"
"vec3 Sky( vec3 ray )\n"
"{\n"
" return mix( vec3(.8), vec3(0), exp2(-(1.0/max(ray.y,.01))*vec3(.4,.6,1.0)) );\n"
"}\n"
"\n"
"mat2 mm2(in float a){float c = cos(a), s = sin(a);return mat2(c,-s,s,c);}\n"
"\n"
"vec3 Voronoi( vec3 pos )\n"
"{\n"
" vec3 d[8];\n"
" d[0] = vec3(0,0,0);\n"
" d[1] = vec3(1,0,0);\n"
" d[2] = vec3(0,1,0);\n"
" d[3] = vec3(1,1,0);\n"
" d[4] = vec3(0,0,1);\n"
" d[5] = vec3(1,0,1);\n"
" d[6] = vec3(0,1,1);\n"
" d[7] = vec3(1,1,1);\n"
" \n"
" const float maxDisplacement = .7; //tweak this to hide grid artefacts\n"
" \n"
" vec3 pf = floor(pos);\n"
"\n"
" const float phi = 1.61803398875;\n"
"\n"
" float closest = 12.0;\n"
" vec3 result;\n"
" for ( int i=0; i < 8; i++ )\n"
" {\n"
" vec3 v = (pf+d[i]);\n"
" vec3 r = fract(phi*v.yzx+17.*fract(v.zxy*phi)+v*v*.03);//Noise(ivec3(floor(pos+d[i])));\n"
" vec3 p = d[i] + maxDisplacement*(r.xyz-.5);\n"
" p -= fract(pos);\n"
" float lsq = dot(p,p);\n"
" if ( lsq < closest )\n"
" {\n"
" closest = lsq;\n"
" result = r;\n"
" }\n"
" }\n"
" return fract(result.xyz);//+result.www); // random colour\n"
"}\n"
"\n"
"vec3 shade( vec3 pos, vec3 norm, vec3 rayDir, vec3 lightDir )\n"
"{\n"
" vec3 paint = material.diffuse;\n"
"\n"
" vec3 norm2 = normalize(norm+.02*(Voronoi(pos*800.0)*2.0-1.0));\n"
" \n"
" if ( dot(norm2,rayDir) > 0.0 ) // we shouldn't see flecks that point away from us\n"
" norm2 -= 2.0*dot(norm2,rayDir)*rayDir;\n"
"\n"
"\n"
" // diffuse layer, reduce overall contrast\n"
" vec3 result = paint*.6*(pow(max(0.0,dot(norm,lightDir)),2.0)+.2);\n"
"\n"
" vec3 h = normalize( lightDir-rayDir );\n"
" vec3 s = pow(max(0.0,dot(h,norm2)),50.0)*10.0*vec3(1);\n"
"\n"
" float rdotn = dot(rayDir,norm2);\n"
" vec3 reflection = rayDir-2.0*rdotn*norm;\n"
" s += Sky( reflection );\n"
"\n"
" float f = pow(1.0+rdotn,5.0);\n"
" f = mix( .2, 1.0, f );\n"
" \n"
" result = mix(result,paint*s,f);\n"
" \n"
" // gloss layer\n"
" s = pow(max(0.0,dot(h,norm)),1000.0)*32.0*vec3(1);\n"
" \n"
" rdotn = dot(rayDir,norm);\n"
" reflection = rayDir-2.0*rdotn*norm;\n"
" \n"
" return result;\n"
"}\n"
"\n"
"void main()\n"
"{\n"
" vec3 lightDir = vec3(0, 0.707, 0.707);\n"
" vec3 ray = inWPos;\n"
" ray -= inEyePos;\n"
" ray = normalize(ray);\n"
" vec3 shaded = shade( inWPos, N, ray, lightDir );\n"
" outColor = vec4(shaded, 1);\n"
"}\n"
;
static const char *s_glslf_mesh_line =
"#version 430\n"
"#extension GL_ARB_separate_shader_objects : enable\n"
"#extension GL_NV_command_list : enable\n"
"layout(std140,commandBindableNV,binding=" TOSTR(UBO_MATERIAL) ") uniform materialBuffer {\n"
" uniform vec3 diffuse;"
"} material;\n"
"layout(std140,commandBindableNV,binding=" TOSTR(UBO_LIGHT) ") uniform lightBuffer {\n"
" uniform vec3 dir;"
"} light;\n"
"layout(location=1) in vec3 N;\n"
"layout(location=0) out vec4 outColor;\n"
"void main() {\n"
"\n"
" outColor = vec4(0.5,0.5,0.2,1);\n"
"}\n"
;
GLSLShader s_shaderMesh;
GLSLShader s_shaderMeshLine;
//-----------------------------------------------------------------------------
// 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";
GLSLShader s_shaderGrid;
struct BO
{
GLuint Id;
GLuint Sz;
};
BO g_uboMatrix = {0, 0};
BO g_uboLight = {0, 0};
static GLuint s_vboGrid;
static GLuint s_vboGridSz;
static GLuint s_vboCross;
static GLuint s_vboCrossSz;
static LightBuffer s_light = {glm::vec3(0.4f, 0.8f, 0.3f)};
static GLuint s_vao = 0;
class Bk3dModelStandard;
//------------------------------------------------------------------------------
// Renderer: can be OpenGL or other
//------------------------------------------------------------------------------
class RendererStandard : public Renderer
{
private:
bool m_bValid;
std::vector<Bk3dModelStandard*> m_pModels;
bool initResourcesGrid();
bool deleteResourcesGrid();
GLuint m_depthTexture;
GLuint m_colorTexture;
GLuint m_FBO;
GLuint m_MSAA;
void fboResize(int w, int h);
void fboInitialize(int w, int h, int MSAA);
void fboFinish();
int m_winSize[2];
nvgl::ProfilerGL m_profilerGL;
nvh::Profiler::SectionID m_slot;
public:
RendererStandard()
{
m_bValid = false;
g_renderers[g_numRenderers++] = this;
}
virtual ~RendererStandard() {}
virtual const char* getName() { return "Naive Standard VBO"; }
virtual bool valid() { return m_bValid; };
virtual bool initGraphics(int w, int h, int MSAA);
virtual bool terminateGraphics();
virtual bool initThreadLocalVars(int threadId);
virtual void releaseThreadLocalVars();
virtual void destroyCommandBuffers(bool bAll);
virtual void waitForGPUIdle();
virtual bool attachModel(Bk3dModel* pModel);
virtual bool detachModels();
virtual bool initResourcesModel(Bk3dModel* pModel);
virtual bool buildPrimaryCmdBuffer();
virtual bool deleteCmdBuffers();
virtual bool buildCmdBufferGrid();
virtual bool buildCmdBufferModel(Bk3dModel* pModel, int bufIdx, int mstart, int mend);
virtual void consolidateCmdBuffersModel(Bk3dModel* pModel, int numCmdBuffers);
virtual bool deleteCmdBufferModel(Bk3dModel* pModel);
virtual bool updateForChangedRenderTarget(Bk3dModel* pModel);
virtual bool invalidateCmdBufferGrid();
virtual void displayStart(const glm::mat4& world, const InertiaCamera& camera, const glm::mat4& projection, bool bTimingGlitch);
virtual void displayEnd();
virtual void displayGrid(const InertiaCamera& camera, const glm::mat4 projection);
virtual void displayBk3dModel(Bk3dModel* pModel, const glm::mat4& cameraView, const glm::mat4 projection, unsigned char topologies);
virtual void blitToBackbuffer();
virtual void updateViewport(GLint x, GLint y, GLsizei width, GLsizei height);
};
RendererStandard s_renderer;
//------------------------------------------------------------------------------
// Class for Object (made of 1 to N meshes)
//------------------------------------------------------------------------------
class Bk3dModelStandard
{
public:
Bk3dModelStandard(Bk3dModel* pGenericModel);
~Bk3dModelStandard();
private:
Bk3dModel* m_pGenericModel;
BO m_uboObjectMatrices;
BO m_uboMaterial;
public:
bool initResourcesObject();
bool deleteResourcesObject();
void displayObject(Renderer* pRenderer, const glm::mat4& cameraView, const glm::mat4 projection, GLuint fboMSAA8x, unsigned char topologies);
}; //Class Bk3dModelStandard
//------------------------------------------------------------------------------
// FBO FBO FBO FBO FBO FBO FBO FBO FBO FBO FBO
//------------------------------------------------------------------------------
void RendererStandard::fboResize(int w, int h)
{
m_winSize[0] = w;
m_winSize[1] = h;
if(m_depthTexture)
{
texture::deleteTexture(m_depthTexture);
m_depthTexture = 0;
}
if(m_colorTexture)
texture::deleteTexture(m_colorTexture);
if(m_FBO)
{
fbo::detachColorTexture(m_FBO, 0, m_MSAA);
fbo::detachDSTTexture(m_FBO, m_MSAA);
}
// initialize color texture
m_colorTexture = texture::createRGBA8(w, h, m_MSAA, 0);
fbo::attachTexture2D(m_FBO, m_colorTexture, 0, m_MSAA);
m_depthTexture = texture::createDST(w, h, m_MSAA, 0);
fbo::attachDSTTexture2D(m_FBO, m_depthTexture, m_MSAA);
fbo::CheckStatus();
}
void RendererStandard::fboInitialize(int w, int h, int MSAA)
{
m_winSize[0] = w;
m_winSize[1] = h;
m_MSAA = MSAA;
m_FBO = fbo::create();
m_colorTexture = texture::createRGBA8(w, h, MSAA, 0);
fbo::attachTexture2D(m_FBO, m_colorTexture, 0, MSAA);
m_depthTexture = texture::createDST(w, h, MSAA, 0);
fbo::attachDSTTexture2D(m_FBO, m_depthTexture, MSAA);
fbo::CheckStatus();
}
void RendererStandard::fboFinish()
{
if(m_depthTexture)
{
texture::deleteTexture(m_depthTexture);
m_depthTexture = 0;
}
if(m_colorTexture)
{
texture::deleteTexture(m_colorTexture);
m_colorTexture = 0;
}
if(m_FBO)
{
fbo::detachColorTexture(m_FBO, 0, m_MSAA);
fbo::detachDSTTexture(m_FBO, m_MSAA);
m_FBO = 0;
}
fbo::bind(0);
fbo::deleteFBO(m_FBO);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool RendererStandard::initResourcesGrid()
{
//
// Grid floor
//
glCreateBuffers(1, &s_vboGrid);
glm::vec3* data = new glm::vec3[GRIDDEF * 4];
glm::vec3* p = data;
int j = 0;
for(int i = 0; i < GRIDDEF; i++)
{
*(p++) = glm::vec3(-GRIDSZ, 0.0, GRIDSZ * (-1.0f + 2.0f * (float)i / (float)GRIDDEF));
*(p++) = glm::vec3(GRIDSZ * (1.0f - 2.0f / (float)GRIDDEF), 0.0, GRIDSZ * (-1.0f + 2.0f * (float)i / (float)GRIDDEF));
*(p++) = glm::vec3(GRIDSZ * (-1.0f + 2.0f * (float)i / (float)GRIDDEF), 0.0, -GRIDSZ);
*(p++) = glm::vec3(GRIDSZ * (-1.0f + 2.0f * (float)i / (float)GRIDDEF), 0.0, GRIDSZ * (1.0f - 2.0f / (float)GRIDDEF));
}
s_vboGridSz = sizeof(glm::vec3) * GRIDDEF * 4;
glNamedBufferData(s_vboGrid, s_vboGridSz, glm::value_ptr(data[0]), GL_STATIC_DRAW);
delete[] data;
//
// Target Cross
//
glCreateBuffers(1, &s_vboCross);
glm::vec3 crossVtx[6] = {
glm::vec3(-CROSSSZ, 0.0f, 0.0f), glm::vec3(CROSSSZ, 0.0f, 0.0f), glm::vec3(0.0f, -CROSSSZ, 0.0f),
glm::vec3(0.0f, CROSSSZ, 0.0f), glm::vec3(0.0f, 0.0f, -CROSSSZ), glm::vec3(0.0f, 0.0f, CROSSSZ),
};
s_vboCrossSz = sizeof(glm::vec3) * 6;
glNamedBufferData(s_vboCross, s_vboCrossSz, glm::value_ptr(crossVtx[0]), GL_STATIC_DRAW);
return true;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool RendererStandard::deleteResourcesGrid()
{
glDeleteBuffers(1, &s_vboGrid);
glDeleteBuffers(1, &s_vboCross);
return true;
}
//------------------------------------------------------------------------------
// build commandList for the Grid
//------------------------------------------------------------------------------
bool RendererStandard::invalidateCmdBufferGrid()
{
return true;
}
bool RendererStandard::buildPrimaryCmdBuffer()
{
return true;
}
bool RendererStandard::deleteCmdBuffers()
{
return true;
}
//------------------------------------------------------------------------------
// build commandList for the Grid
//------------------------------------------------------------------------------
bool RendererStandard::buildCmdBufferGrid()
{
return true;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void RendererStandard::displayStart(const glm::mat4& world, const InertiaCamera& camera, const glm::mat4& projection, bool bTimingGlitch)
{
m_slot = m_profilerGL.beginSection("scene");
glBindFramebuffer(GL_FRAMEBUFFER, m_FBO);
glEnable(GL_MULTISAMPLE);
glViewport(0, 0, m_winSize[0], m_winSize[1]);
float r = 0.0f; //bTimingGlitch ? 1.0f : 0.0f;
glClearColor(r, 0.1f, 0.15f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
//glDepthFunc(GL_LEQUAL);
glDisable(GL_CULL_FACE);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void RendererStandard::displayEnd()
{
m_profilerGL.endSection(m_slot);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void RendererStandard::displayGrid(const InertiaCamera& camera, const glm::mat4 projection)
{
const nvgl::ProfilerGL::Section profile(m_profilerGL, "display.Grid");
//
// Update what is inside buffers
//
g_globalMatrices.mVP = projection * camera.m4_view;
g_globalMatrices.mW = glm::mat4(1);
glNamedBufferSubData(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 glm::vec3& p = camera.curFocusPos;
glm::vec3 crossVtx[6] = {
glm::vec3(p.x - CROSSSZ, p.y, p.z), glm::vec3(p.x + CROSSSZ, p.y, p.z), glm::vec3(p.x, p.y - CROSSSZ, p.z),
glm::vec3(p.x, p.y + CROSSSZ, p.z), glm::vec3(p.x, p.y, p.z - CROSSSZ), glm::vec3(p.x, p.y, p.z + CROSSSZ),
};
glNamedBufferSubData(s_vboCross, 0, sizeof(glm::vec3) * 6, crossVtx);
// ------------------------------------------------------------------------------------------
// Case of regular rendering
//
s_shaderGrid.bindShader();
glEnableVertexAttribArray(0);
glDisableVertexAttribArray(1);
// --------------------------------------------------------------------------------------
// Using regular VBO
//
glBindBufferBase(GL_UNIFORM_BUFFER, UBO_MATRIX, g_uboMatrix.Id);
glBindVertexBuffer(0, s_vboGrid, 0, sizeof(glm::vec3));
glVertexAttribFormat(0, 3, GL_FLOAT, GL_FALSE, 0);
//
// Draw!
//
glDrawArrays(GL_LINES, 0, GRIDDEF * 4);
glBindVertexBuffer(0, s_vboCross, 0, sizeof(glm::vec3));
glDrawArrays(GL_LINES, 0, 6);
glDisableVertexAttribArray(0);
//s_shaderGrid.unbindShader();
}
//------------------------------------------------------------------------------
// this is an example of creating a piece of token buffer that would be put
// as a header before every single glDrawCommandsStatesAddressNV so that
// proper view setup (viewport) get properly done without relying on any
// typical OpenGL command.
// this approach is good avoid messing with OpenGL state machine and later could
// prevent extra driver validation
//------------------------------------------------------------------------------
void RendererStandard::updateViewport(GLint x, GLint y, GLsizei width, GLsizei height)
{
m_winSize[0] = width;
m_winSize[1] = height;
fboResize(width, height);
glLineWidth(1.0);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool RendererStandard::initGraphics(int w, int h, int MSAA)
{
//
// some offscreen buffer
//
fboInitialize(w, h, MSAA);
//
// Shader compilation
//
if(!s_shaderGrid.addVertexShaderFromString(g_glslv_grid))
return false;
if(!s_shaderGrid.addFragmentShaderFromString(g_glslf_grid))
return false;
if(!s_shaderGrid.link())
return false;
if(!s_shaderMesh.addVertexShaderFromString(s_glslv_mesh))
return false;
if(!s_shaderMesh.addFragmentShaderFromString(s_glslf_mesh))
return false;
if(!s_shaderMesh.link())
return false;
if(!s_shaderMeshLine.addVertexShaderFromString(s_glslv_mesh))
return false;
if(!s_shaderMeshLine.addFragmentShaderFromString(s_glslf_mesh_line))
return false;
if(!s_shaderMeshLine.link())
return false;
//
// Create some UBO for later share their 64 bits
//
glCreateBuffers(1, &g_uboMatrix.Id);
g_uboMatrix.Sz = sizeof(MatrixBufferGlobal);
glNamedBufferData(g_uboMatrix.Id, g_uboMatrix.Sz, &g_globalMatrices, GL_STREAM_DRAW);
//glBindBufferBase(GL_UNIFORM_BUFFER,UBO_MATRIX, g_uboMatrix.Id);
//
// Trivial Light info...
//
glCreateBuffers(1, &g_uboLight.Id);
g_uboLight.Sz = sizeof(LightBuffer);
glNamedBufferData(g_uboLight.Id, g_uboLight.Sz, &s_light, GL_STATIC_DRAW);
//glBindBufferBase(GL_UNIFORM_BUFFER,UBO_LIGHT, g_uboLight.Id);
//
// Misc OGL setup
//
glGenVertexArrays(1, &s_vao);
glBindVertexArray(s_vao);
//
// Grid
//
if(!initResourcesGrid())
return false;
//
// OpenGL timers
//
m_profilerGL = nvgl::ProfilerGL(&g_profiler);
m_profilerGL.init();
LOGOK("Initialized renderer %s\n", getName());
m_bValid = true;
return true;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool RendererStandard::terminateGraphics()
{
fboFinish();
detachModels();
deleteResourcesGrid();
glDeleteVertexArrays(1, &s_vao);
s_vao = 0;
glDeleteBuffers(1, &g_uboLight.Id);
g_uboLight.Id = 0;
glDeleteBuffers(1, &g_uboMatrix.Id);
g_uboMatrix.Id = 0;
s_shaderGrid.cleanup();
s_shaderMesh.cleanup();
s_shaderMeshLine.cleanup();
m_profilerGL.deinit();
m_bValid = false;
return true;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool RendererStandard::attachModel(Bk3dModel* pGenericModel)
{
Bk3dModelStandard* pModel = new Bk3dModelStandard(pGenericModel);
m_pModels.push_back(pModel); // keep track of allocated stuff
return true;
}
bool RendererStandard::detachModels()
{
bool b = true;
for(int i = 0; i < m_pModels.size(); i++)
{
if(!m_pModels[i]->deleteResourcesObject())
b = false;
delete m_pModels[i];
}
m_pModels.clear();
return b;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool RendererStandard::initResourcesModel(Bk3dModel* pGenericModel)
{
return ((Bk3dModelStandard*)pGenericModel->m_pRendererData)->initResourcesObject();
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
int g_AAAA;
bool RendererStandard::buildCmdBufferModel(Bk3dModel* pModel, int bufIdx, int mstart, int mend)
{
g_AAAA = mend;
return false;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void RendererStandard::consolidateCmdBuffersModel(Bk3dModel* pModel, int numCmdBuffers)
{
//((Bk3dModelCMDList*)pModel->m_pRendererData)->setNumCmdBuffers(numCmdBuffers);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool RendererStandard::deleteCmdBufferModel(Bk3dModel* pGenericModel)
{
return true;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool RendererStandard::updateForChangedRenderTarget(Bk3dModel* pModel)
{
return false;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void RendererStandard::displayBk3dModel(Bk3dModel* pGenericModel, const glm::mat4& cameraView, const glm::mat4 projection, unsigned char topologies)
{
const nvgl::ProfilerGL::Section profile(m_profilerGL, "display.Bk3dModel");
((Bk3dModelStandard*)pGenericModel->m_pRendererData)->displayObject(this, cameraView, projection, m_FBO, topologies);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void RendererStandard::blitToBackbuffer()
{
const nvgl::ProfilerGL::Section profile(m_profilerGL, "blitToBackbuffer");
glBindFramebuffer(GL_READ_FRAMEBUFFER, m_FBO);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBlitFramebuffer(0, 0, m_winSize[0], m_winSize[1], 0, 0, m_winSize[0], m_winSize[1], GL_COLOR_BUFFER_BIT, GL_NEAREST);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
Bk3dModelStandard::Bk3dModelStandard(Bk3dModel* pGenericModel)
{
memset(&m_uboObjectMatrices, 0, sizeof(BO));
memset(&m_uboMaterial, 0, sizeof(BO));
// keep track of the stuff related to this renderer
pGenericModel->m_pRendererData = this;
// keep track of the generic model data in the part for this renderer
m_pGenericModel = pGenericModel;
}
Bk3dModelStandard::~Bk3dModelStandard()
{
glDeleteBuffers(1, &m_uboObjectMatrices.Id);
glDeleteBuffers(1, &m_uboMaterial.Id);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool Bk3dModelStandard::deleteResourcesObject()
{
glDeleteBuffers(1, &m_uboMaterial.Id);
m_uboMaterial.Id = 0;
m_uboMaterial.Sz = 0;
glDeleteBuffers(1, &m_uboObjectMatrices.Id);
m_uboObjectMatrices.Id = 0;
m_uboObjectMatrices.Sz = 0;
bk3d::Mesh* pMesh = NULL;
for(int i = 0; i < m_pGenericModel->m_meshFile->pMeshes->n; i++)
{
pMesh = m_pGenericModel->m_meshFile->pMeshes->p[i];
int n = pMesh->pSlots->n;
for(int s = 0; s < n; s++)
{
bk3d::Slot* pS = pMesh->pSlots->p[s];
GLuint id = pS->userData;
pS->userData = 0;
glDeleteBuffers(1, &id);
}
for(int pg = 0; pg < pMesh->pPrimGroups->n; pg++)
{
bk3d::PrimGroup* pPG = pMesh->pPrimGroups->p[pg];
if(pPG->indexArrayByteSize > 0)
{
if((pPG->pOwnerOfIB == pPG) || (pPG->pOwnerOfIB == NULL)) // this primitive group doesn't use other's buffer
{
GLuint id = (GLuint)uintptr_t(pPG->userPtr);
pPG->userPtr = NULL;
glDeleteBuffers(1, &id);
}
}
}
}
return false;
}
//------------------------------------------------------------------------------
// It is possible to create one VBO for one Mesh; and one EBO for each primitive group
// however, for this sample, we will create only one VBO for all and one EBO
// meshes and primitive groups will have an offset in these buffers
//------------------------------------------------------------------------------
bool Bk3dModelStandard::initResourcesObject()
{
//m_pGenericModel->m_meshFile->pMeshes->n = 60000;
//
// create Buffer Object for materials
//
if(m_pGenericModel->m_meshFile->pMaterials && m_pGenericModel->m_meshFile->pMaterials->nMaterials)
{
//
// Material UBO: *TABLE* of multiple materials
// Then offset in it for various drawcalls
//
if(m_uboMaterial.Id == 0)
glCreateBuffers(1, &m_uboMaterial.Id);
m_uboMaterial.Sz = sizeof(MaterialBuffer) * m_pGenericModel->m_materialNItems;
glNamedBufferData(m_uboMaterial.Id, m_uboMaterial.Sz, m_pGenericModel->m_material, GL_STATIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, UBO_MATERIAL, m_uboMaterial.Id);
LOGI("%d materials stored in %d Kb\n", m_pGenericModel->m_meshFile->pMaterials->nMaterials, (m_uboMaterial.Sz + 512) / 1024);
}
//
// create Buffer Object for Object-matrices
//
if(m_pGenericModel->m_meshFile->pTransforms && m_pGenericModel->m_meshFile->pTransforms->nBones)
{
//
// Transformation UBO: *TABLE* of multiple transformations
// Then offset in it for various drawcalls
//
if(m_uboObjectMatrices.Id == 0)
glCreateBuffers(1, &m_uboObjectMatrices.Id);
m_uboObjectMatrices.Sz = sizeof(MatrixBufferObject) * m_pGenericModel->m_objectMatricesNItems;
glNamedBufferData(m_uboObjectMatrices.Id, m_uboObjectMatrices.Sz, m_pGenericModel->m_objectMatrices, GL_STATIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, UBO_MATRIXOBJ, m_uboObjectMatrices.Id);
LOGI("%d matrices stored in %d Kb\n", m_pGenericModel->m_meshFile->pTransforms->nBones, (m_uboObjectMatrices.Sz + 512) / 1024);
}
//
// First pass: evaluate the size of the single VBO
// and store offset to where we'll find data back
//
bk3d::Mesh* pMesh = NULL;
for(int i = 0; i < m_pGenericModel->m_meshFile->pMeshes->n; i++)
{
pMesh = m_pGenericModel->m_meshFile->pMeshes->p[i];
//
// Slots: buffers for vertices
//
int n = pMesh->pSlots->n;
for(int s = 0; s < n; s++)
{
bk3d::Slot* pS = pMesh->pSlots->p[s];
GLuint id;
glCreateBuffers(1, &id); // Buffer Object directly kept in the Slot
pS->userData = id;
#if 1
glNamedBufferData(id, pS->vtxBufferSizeBytes, NULL, GL_STATIC_DRAW);
#else
glNamedBufferStorage(id, pS->vtxBufferSizeBytes, NULL, 0); // Not working with NSight !!! https://www.opengl.org/registry/specs/ARB/buffer_storage.txt
#endif
glNamedBufferSubData(id, 0, pS->vtxBufferSizeBytes, pS->pVtxBufferData);
}
//
// Primitive groups
//
for(int pg = 0; pg < pMesh->pPrimGroups->n; pg++)
{
bk3d::PrimGroup* pPG = pMesh->pPrimGroups->p[pg];
if(pPG->indexArrayByteSize > 0)
{
if((pPG->pOwnerOfIB == pPG) || (pPG->pOwnerOfIB == NULL)) // this primitive group doesn't use other's buffer
{
GLuint id;
glCreateBuffers(1, &id);
pPG->userPtr = (int*)id;
#if 1
glNamedBufferData(id, pPG->indexArrayByteSize, NULL, GL_STATIC_DRAW);
#else
glNamedBufferStorage(id, pPG->indexArrayByteSize, NULL, 0); // Not working with NSight !!! https://www.opengl.org/registry/specs/ARB/buffer_storage.txt
#endif
glNamedBufferSubData(id, pPG->indexArrayByteOffset, pPG->indexArrayByteSize, pPG->pIndexBufferData);
}
else
{
pPG->userPtr = pPG->pOwnerOfIB->userPtr;
}
}
else
{
pPG->userPtr = NULL;
}
}
}
//LOGI("meshes: %d in :%d VBOs (%f Mb) and %d EBOs (%f Mb) \n", m_pGenericModel->m_meshFile->pMeshes->n, .size(), (float)totalVBOSz/(float)(1024*1024), m_ObjEBOs.size(), (float)totalEBOSz/(float)(1024*1024));
return true;
}
//------------------------------------------------------------------------------
// Really dumb display loop: cycling in meshes and primitive groups as they come
//------------------------------------------------------------------------------
void Bk3dModelStandard::displayObject(Renderer* pRenderer, const glm::mat4& cameraView, const glm::mat4 projection, GLuint fboMSAA8x, unsigned char topologies)
{
NXPROFILEFUNC(__FUNCTION__);
g_globalMatrices.mVP = projection * cameraView;
g_globalMatrices.mW = glm::mat4(1);
g_globalMatrices.eyePos = cameraView[3];
//g_globalMatrices.mW.rotate(nv_to_rad*180.0f, glm::vec3(0,1,0));
g_globalMatrices.mW = glm::rotate(g_globalMatrices.mW, -glm::radians(90.0f), glm::vec3(1, 0, 0));
g_globalMatrices.mW = glm::translate(g_globalMatrices.mW, -m_pGenericModel->m_posOffset);
g_globalMatrices.mW = glm::scale(g_globalMatrices.mW, glm::vec3(m_pGenericModel->m_scale));
glNamedBufferSubData(g_uboMatrix.Id, 0, sizeof(g_globalMatrices), &g_globalMatrices);
if(m_pGenericModel->m_meshFile)
{
GLuint curMaterial = 0;
GLuint curTransf = 0;
glBindBufferBase(GL_UNIFORM_BUFFER, UBO_MATRIX, g_uboMatrix.Id);
glBindBufferBase(GL_UNIFORM_BUFFER, UBO_LIGHT, g_uboLight.Id);
glBindBufferBase(GL_UNIFORM_BUFFER, UBO_MATRIXOBJ, m_uboObjectMatrices.Id);
//
// Loop 2 times: for filled topologies, then for lines
// ideally, the models should be pre-sorted by shaders...
// Alternating from one shader to the other is very expensive
// so it is better to do all geometries for one dedicated shader/material
//
#define LINES 1
#define POLYS 2
for(int s = LINES; s < POLYS + 1; s++)
{
if(s == POLYS)
{
if((topologies & 0x1C) == 0)
continue;
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.0, 1.0); // no issue with redundant call here: the state capture will just deal with simplifying things
s_shaderMesh.bindShader();
}
else
{
if((topologies & 3) == 0)
continue;
glDisable(GL_POLYGON_OFFSET_FILL);
s_shaderMeshLine.bindShader();
}
for(int m = 1; m < m_pGenericModel->m_meshFile->pMeshes->n; m++) //m_pGenericModel->m_meshFile->pMeshes->n; m++)
{
bk3d::Mesh* pMesh = m_pGenericModel->m_meshFile->pMeshes->p[m];
//
// First filter to eliminate meshes that aren't relevant for the pass
//
char bPrimType = 0;
for(int pg = 0; pg < pMesh->pPrimGroups->n; pg++)
{
bk3d::PrimGroup* pPG = pMesh->pPrimGroups->p[pg];
switch(pPG->topologyGL)
{
case GL_LINES:
case GL_LINE_STRIP:
bPrimType |= LINES;
break;
default:
bPrimType |= POLYS;
break;
}
}
// skip is exclusively for primitives out of the scope of this loop
if((s == POLYS) && (bPrimType == LINES))
continue;
if((s == LINES) && (bPrimType == POLYS))
continue;
if(pMesh->pTransforms && (pMesh->pTransforms->n > 0))
{
bk3d::Bone* pTransf = pMesh->pTransforms->p[0];
if(pTransf && (curTransf != pTransf->ID))
{
curTransf = pTransf->ID;
glBindBufferRange(GL_UNIFORM_BUFFER, UBO_MATRIXOBJ, m_uboObjectMatrices.Id,
curTransf * sizeof(MatrixBufferObject), sizeof(MatrixBufferObject));
}
}
int n = pMesh->pSlots->n;
// let's make it simple: for now we assume pos and normal come first:
// 0: vertex
// 1: normal
// the file could give any arbitrary kind of attributes. Normally, we should check the attribute type and make them match with the shader expectation
int bindingIndex = 0;
for(int s = 0; s < n; s++)
{
bk3d::Slot* pS = pMesh->pSlots->p[s];
glBindBuffer(GL_ARRAY_BUFFER, pS->userData);
for(int a = 0; a < pS->pAttributes->n; a++)
{
glEnableVertexAttribArray(bindingIndex);
bk3d::Attribute* pAttr = pS->pAttributes->p[a];
//pAttr->name would give the attribute name... assuming we are right, here.
//glBindVertexBuffer(bindingIndex, pS->userData, pAttr->dataOffsetBytes, pAttr->strideBytes);
glVertexAttribPointer(bindingIndex, pAttr->numComp, pAttr->formatGL, GL_FALSE, pAttr->strideBytes,
(const void*)pAttr->dataOffsetBytes);
bindingIndex++;
}
}
// disable other attributes... we never know
for(int j = bindingIndex; j <= 3 /*15*/; j++)
glDisableVertexAttribArray(bindingIndex);
//====> render primitive groups
for(int pg = 0; pg < pMesh->pPrimGroups->n; pg++)
{
bk3d::PrimGroup* pPG = pMesh->pPrimGroups->p[pg];
switch(pPG->topologyGL)
{