-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathmock_gl_engine.cpp
More file actions
2135 lines (1708 loc) · 69.8 KB
/
mock_gl_engine.cpp
File metadata and controls
2135 lines (1708 loc) · 69.8 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 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run
#ifdef POLYSCOPE_BACKEND_OPENGL_MOCK_ENABLED
#include "polyscope/render/mock_opengl/mock_gl_engine.h"
#include "polyscope/messages.h"
#include "polyscope/options.h"
#include "polyscope/polyscope.h"
#include "polyscope/utilities.h"
#include "polyscope/render/shader_builder.h"
// all the shaders
#include "polyscope/render/opengl/shaders/common.h"
#include "polyscope/render/opengl/shaders/cylinder_shaders.h"
#include "polyscope/render/opengl/shaders/gizmo_shaders.h"
#include "polyscope/render/opengl/shaders/grid_shaders.h"
#include "polyscope/render/opengl/shaders/ground_plane_shaders.h"
#include "polyscope/render/opengl/shaders/histogram_shaders.h"
#include "polyscope/render/opengl/shaders/lighting_shaders.h"
#include "polyscope/render/opengl/shaders/ribbon_shaders.h"
#include "polyscope/render/opengl/shaders/rules.h"
#include "polyscope/render/opengl/shaders/sphere_shaders.h"
#include "polyscope/render/opengl/shaders/surface_mesh_shaders.h"
#include "polyscope/render/opengl/shaders/texture_draw_shaders.h"
#include "polyscope/render/opengl/shaders/vector_shaders.h"
#include "polyscope/render/opengl/shaders/volume_mesh_shaders.h"
#include "stb_image.h"
namespace polyscope {
namespace render {
namespace backend_openGL_mock {
void initializeRenderEngine() {
MockGLEngine* glEngine = new MockGLEngine();
engine = glEngine;
glEngine->initialize();
engine->allocateGlobalBuffersAndPrograms();
}
// == Map enums to native values
void checkGLError(bool fatal = true) {}
// =============================================================
// =================== Attribute buffer ========================
// =============================================================
GLAttributeBuffer::GLAttributeBuffer(RenderDataType dataType_, int arrayCount_)
: AttributeBuffer(dataType_, arrayCount_) {}
GLAttributeBuffer::~GLAttributeBuffer() { bind(); }
void GLAttributeBuffer::bind() {}
void GLAttributeBuffer::checkType(RenderDataType targetType) {
if (dataType != targetType) {
throw std::invalid_argument("Tried to set GLAttributeBuffer with wrong type. Actual type: " +
renderDataTypeName(dataType) + " Attempted type: " + renderDataTypeName(targetType));
}
}
void GLAttributeBuffer::checkArray(int testArrayCount) {
if (testArrayCount != arrayCount) {
throw std::invalid_argument("Tried to set GLAttributeBuffer with wrong array count. Actual count: " +
std::to_string(arrayCount) + " Attempted count: " + std::to_string(testArrayCount));
}
}
template <typename T>
void GLAttributeBuffer::setData_helper(const std::vector<T>& data) {
bind();
// allocate if needed
if (!isSet() || data.size() > bufferSize) {
setFlag = true;
uint64_t newSize = data.size();
newSize = std::max(newSize, 2 * bufferSize); // if we're expanding, at-least double
bufferSize = newSize;
}
// do the actual copy
dataSize = data.size();
checkGLError();
}
void GLAttributeBuffer::setData(const std::vector<glm::vec2>& data) {
checkType(RenderDataType::Vector2Float);
setData_helper(data);
}
void GLAttributeBuffer::setData(const std::vector<glm::vec3>& data) {
checkType(RenderDataType::Vector3Float);
setData_helper(data);
}
void GLAttributeBuffer::setData(const std::vector<std::array<glm::vec3, 2>>& data) {
checkType(RenderDataType::Vector3Float);
checkArray(2);
setData_helper(data);
}
void GLAttributeBuffer::setData(const std::vector<std::array<glm::vec3, 3>>& data) {
checkType(RenderDataType::Vector3Float);
checkArray(3);
setData_helper(data);
}
void GLAttributeBuffer::setData(const std::vector<std::array<glm::vec3, 4>>& data) {
checkType(RenderDataType::Vector3Float);
checkArray(4);
setData_helper(data);
}
void GLAttributeBuffer::setData(const std::vector<glm::vec4>& data) {
checkType(RenderDataType::Vector4Float);
setData_helper(data);
}
void GLAttributeBuffer::setData(const std::vector<float>& data) {
checkType(RenderDataType::Float);
setData_helper(data);
}
void GLAttributeBuffer::setData(const std::vector<double>& data) {
checkType(RenderDataType::Float);
// Convert input data to floats
std::vector<float> floatData(data.size());
for (unsigned int i = 0; i < data.size(); i++) {
floatData[i] = static_cast<float>(data[i]);
}
setData_helper(floatData);
}
void GLAttributeBuffer::setData(const std::vector<int32_t>& data) {
checkType(RenderDataType::Int);
setData_helper(data);
}
void GLAttributeBuffer::setData(const std::vector<uint32_t>& data) {
checkType(RenderDataType::UInt);
setData_helper(data);
}
void GLAttributeBuffer::setData(const std::vector<glm::uvec2>& data) {
checkType(RenderDataType::Vector2UInt);
setData_helper(data);
}
void GLAttributeBuffer::setData(const std::vector<glm::uvec3>& data) {
checkType(RenderDataType::Vector3UInt);
setData_helper(data);
}
void GLAttributeBuffer::setData(const std::vector<glm::uvec4>& data) {
checkType(RenderDataType::Vector4UInt);
setData_helper(data);
}
// === get single data values
template <typename T>
T GLAttributeBuffer::getData_helper(size_t ind) {
if (!isSet() || ind >= static_cast<size_t>(getDataSize() * getArrayCount())) exception("bad getData");
bind();
T readValue{};
return readValue;
}
float GLAttributeBuffer::getData_float(size_t ind) {
if (getType() != RenderDataType::Float) exception("bad getData type");
return getData_helper<float>(ind);
}
double GLAttributeBuffer::getData_double(size_t ind) { return getData_float(ind); }
glm::vec2 GLAttributeBuffer::getData_vec2(size_t ind) {
if (getType() != RenderDataType::Vector2Float) exception("bad getData type");
return getData_helper<glm::vec2>(ind);
}
glm::vec3 GLAttributeBuffer::getData_vec3(size_t ind) {
if (getType() != RenderDataType::Vector3Float) exception("bad getData type");
return getData_helper<glm::vec3>(ind);
}
glm::vec4 GLAttributeBuffer::getData_vec4(size_t ind) {
if (getType() != RenderDataType::Vector4Float) exception("bad getData type");
return getData_helper<glm::vec4>(ind);
}
int GLAttributeBuffer::getData_int(size_t ind) {
if (getType() != RenderDataType::Int) exception("bad getData type");
return getData_helper<int>(ind);
}
uint32_t GLAttributeBuffer::getData_uint32(size_t ind) {
if (getType() != RenderDataType::UInt) exception("bad getData type");
return getData_helper<uint32_t>(ind);
}
glm::uvec2 GLAttributeBuffer::getData_uvec2(size_t ind) {
if (getType() != RenderDataType::Vector2Float) exception("bad getData type");
return getData_helper<glm::uvec2>(ind);
}
glm::uvec3 GLAttributeBuffer::getData_uvec3(size_t ind) {
if (getType() != RenderDataType::Vector3Float) exception("bad getData type");
return getData_helper<glm::uvec3>(ind);
}
glm::uvec4 GLAttributeBuffer::getData_uvec4(size_t ind) {
if (getType() != RenderDataType::Vector4Float) exception("bad getData type");
return getData_helper<glm::uvec4>(ind);
}
// === get ranges of values
template <typename T>
std::vector<T> GLAttributeBuffer::getDataRange_helper(size_t start, size_t count) {
if (!isSet() || start + count > static_cast<size_t>(getDataSize() * getArrayCount())) exception("bad getData");
bind();
std::vector<T> readValues(count);
return readValues;
}
std::vector<float> GLAttributeBuffer::getDataRange_float(size_t start, size_t count) {
if (getType() != RenderDataType::Float) exception("bad getData type");
return getDataRange_helper<float>(start, count);
}
std::vector<double> GLAttributeBuffer::getDataRange_double(size_t start, size_t count) {
std::vector<float> floatValues = getDataRange_float(start, count);
std::vector<double> values(count);
for (size_t i = 0; i < count; i++) {
values[i] = static_cast<double>(floatValues[i]);
}
return values;
}
std::vector<glm::vec2> GLAttributeBuffer::getDataRange_vec2(size_t start, size_t count) {
if (getType() != RenderDataType::Vector2Float) exception("bad getData type");
return getDataRange_helper<glm::vec2>(start, count);
}
std::vector<glm::vec3> GLAttributeBuffer::getDataRange_vec3(size_t start, size_t count) {
if (getType() != RenderDataType::Vector3Float) exception("bad getData type");
return getDataRange_helper<glm::vec3>(start, count);
}
std::vector<glm::vec4> GLAttributeBuffer::getDataRange_vec4(size_t start, size_t count) {
if (getType() != RenderDataType::Vector4Float) exception("bad getData type");
return getDataRange_helper<glm::vec4>(start, count);
}
std::vector<int> GLAttributeBuffer::getDataRange_int(size_t start, size_t count) {
if (getType() != RenderDataType::Int) exception("bad getData type");
return getDataRange_helper<int>(start, count);
}
std::vector<uint32_t> GLAttributeBuffer::getDataRange_uint32(size_t start, size_t count) {
if (getType() != RenderDataType::UInt) exception("bad getData type");
return getDataRange_helper<uint32_t>(start, count);
}
std::vector<glm::uvec2> GLAttributeBuffer::getDataRange_uvec2(size_t start, size_t count) {
if (getType() != RenderDataType::Vector2UInt) exception("bad getData type");
return getDataRange_helper<glm::uvec2>(start, count);
}
std::vector<glm::uvec3> GLAttributeBuffer::getDataRange_uvec3(size_t start, size_t count) {
if (getType() != RenderDataType::Vector3UInt) exception("bad getData type");
return getDataRange_helper<glm::uvec3>(start, count);
}
std::vector<glm::uvec4> GLAttributeBuffer::getDataRange_uvec4(size_t start, size_t count) {
if (getType() != RenderDataType::Vector4UInt) exception("bad getData type");
bind();
return getDataRange_helper<glm::uvec4>(start, count);
}
uint32_t GLAttributeBuffer::getNativeBufferID() { return 777; }
// =============================================================
// ==================== Texture buffer =========================
// =============================================================
// create a 1D texture from data
GLTextureBuffer::GLTextureBuffer(TextureFormat format_, unsigned int size1D, const unsigned char* data)
: TextureBuffer(1, format_, size1D) {
checkGLError();
setFilterMode(FilterMode::Nearest);
}
GLTextureBuffer::GLTextureBuffer(TextureFormat format_, unsigned int size1D, const float* data)
: TextureBuffer(1, format_, size1D) {
checkGLError();
setFilterMode(FilterMode::Nearest);
}
// create a 2D texture from data
GLTextureBuffer::GLTextureBuffer(TextureFormat format_, unsigned int sizeX_, unsigned int sizeY_,
const unsigned char* data)
: TextureBuffer(2, format_, sizeX_, sizeY_) {
checkGLError();
setFilterMode(FilterMode::Nearest);
}
GLTextureBuffer::GLTextureBuffer(TextureFormat format_, unsigned int sizeX_, unsigned int sizeY_, const float* data)
: TextureBuffer(2, format_, sizeX_, sizeY_) {
checkGLError();
setFilterMode(FilterMode::Nearest);
}
// create a 3D texture from data
GLTextureBuffer::GLTextureBuffer(TextureFormat format_, unsigned int sizeX_, unsigned int sizeY_, unsigned int sizeZ_,
const unsigned char* data)
: TextureBuffer(3, format_, sizeX_, sizeY_, sizeZ_) {
checkGLError();
setFilterMode(FilterMode::Nearest);
}
GLTextureBuffer::GLTextureBuffer(TextureFormat format_, unsigned int sizeX_, unsigned int sizeY_, unsigned int sizeZ_,
const float* data)
: TextureBuffer(3, format_, sizeX_, sizeY_, sizeZ_) {
checkGLError();
setFilterMode(FilterMode::Nearest);
}
GLTextureBuffer::~GLTextureBuffer() {}
void GLTextureBuffer::resize(unsigned int newLen) {
TextureBuffer::resize(newLen);
bind();
if (dim == 1) {
} else {
exception("OpenGL error: called 1D resize on not-1D texture");
}
checkGLError();
}
void GLTextureBuffer::resize(unsigned int newX, unsigned int newY) {
TextureBuffer::resize(newX, newY);
bind();
if (dim == 2) {
} else {
exception("OpenGL error: called 2D resize on not-2D texture");
}
checkGLError();
}
void GLTextureBuffer::resize(unsigned int newX, unsigned int newY, unsigned int newZ) {
TextureBuffer::resize(newX, newY, newZ);
bind();
if (dim == 3) {
} else {
exception("OpenGL error: called 3D resize on not-3D texture");
}
checkGLError();
}
void GLTextureBuffer::setData(const std::vector<glm::vec2>& data) { exception("not implemented"); }
void GLTextureBuffer::setData(const std::vector<glm::vec3>& data) {
bind();
if (data.size() != getTotalSize()) {
exception("OpenGL error: texture buffer data is not the right size.");
}
switch (dim) {
case 1:
break;
case 2:
break;
case 3:
break;
}
checkGLError();
}
void GLTextureBuffer::setData(const std::vector<glm::vec4>& data) {
bind();
if (data.size() != getTotalSize()) {
exception("OpenGL error: texture buffer data is not the right size.");
}
switch (dim) {
case 1:
break;
case 2:
break;
case 3:
break;
}
checkGLError();
}
void GLTextureBuffer::setData(const std::vector<float>& data) {
bind();
if (data.size() != getTotalSize()) {
exception("OpenGL error: texture buffer data is not the right size.");
}
switch (dim) {
case 1:
break;
case 2:
break;
case 3:
break;
}
checkGLError();
}
void GLTextureBuffer::setData(const std::vector<double>& data) {
bind();
if (data.size() != getTotalSize()) {
exception("OpenGL error: texture buffer data is not the right size.");
}
switch (dim) {
case 1:
break;
case 2:
break;
case 3:
break;
}
checkGLError();
};
void GLTextureBuffer::setData(const std::vector<int32_t>& data) { exception("not implemented"); };
void GLTextureBuffer::setData(const std::vector<uint32_t>& data) { exception("not implemented"); };
void GLTextureBuffer::setData(const std::vector<glm::uvec2>& data) { exception("not implemented"); };
void GLTextureBuffer::setData(const std::vector<glm::uvec3>& data) { exception("not implemented"); };
void GLTextureBuffer::setData(const std::vector<glm::uvec4>& data) { exception("not implemented"); };
void GLTextureBuffer::setData(const std::vector<std::array<glm::vec3, 2>>& data) { exception("not implemented"); };
void GLTextureBuffer::setData(const std::vector<std::array<glm::vec3, 3>>& data) { exception("not implemented"); };
void GLTextureBuffer::setData(const std::vector<std::array<glm::vec3, 4>>& data) { exception("not implemented"); };
void GLTextureBuffer::setFilterMode(FilterMode newMode) {
bind();
if (dim == 1) {
switch (newMode) {
case FilterMode::Nearest:
break;
case FilterMode::Linear:
break;
}
}
if (dim == 2) {
switch (newMode) {
case FilterMode::Nearest:
break;
case FilterMode::Linear:
break;
}
}
checkGLError();
}
void* GLTextureBuffer::getNativeHandle() { return nullptr; }
uint32_t GLTextureBuffer::getNativeBufferID() { return 77; };
std::vector<float> GLTextureBuffer::getDataScalar() {
if (dimension(format) != 1) exception("called getDataScalar on texture which does not have a 1 dimensional format");
std::vector<float> outData;
outData.resize(getSizeX() * getSizeY());
return outData;
}
std::vector<glm::vec2> GLTextureBuffer::getDataVector2() {
if (dimension(format) != 2) exception("called getDataVector2 on texture which does not have a 2 dimensional format");
std::vector<glm::vec2> outData;
outData.resize(getSizeX() * getSizeY());
return outData;
}
std::vector<glm::vec3> GLTextureBuffer::getDataVector3() {
if (dimension(format) != 3) exception("called getDataVector3 on texture which does not have a 3 dimensional format");
exception("not implemented");
std::vector<glm::vec3> outData;
outData.resize(getSizeX() * getSizeY());
return outData;
}
void GLTextureBuffer::bind() {
if (dim == 1) {
}
if (dim == 2) {
}
checkGLError();
}
// =============================================================
// ===================== Render buffer =========================
// =============================================================
GLRenderBuffer::GLRenderBuffer(RenderBufferType type_, unsigned int sizeX_, unsigned int sizeY_)
: RenderBuffer(type_, sizeX_, sizeY_) {
checkGLError();
resize(sizeX, sizeY);
}
GLRenderBuffer::~GLRenderBuffer() {}
void GLRenderBuffer::resize(unsigned int newX, unsigned int newY) {
RenderBuffer::resize(newX, newY);
bind();
checkGLError();
}
void GLRenderBuffer::bind() { checkGLError(); }
// =============================================================
// ===================== Framebuffer ===========================
// =============================================================
GLFrameBuffer::GLFrameBuffer(unsigned int sizeX_, unsigned int sizeY_, bool isDefault) {
sizeX = sizeX_;
sizeY = sizeY_;
if (isDefault) {
} else {
}
checkGLError();
};
GLFrameBuffer::~GLFrameBuffer() {}
void GLFrameBuffer::bind() { checkGLError(); }
void GLFrameBuffer::addColorBuffer(std::shared_ptr<RenderBuffer> renderBufferIn) {
// it _better_ be a GL buffer
std::shared_ptr<GLRenderBuffer> renderBuffer = std::dynamic_pointer_cast<GLRenderBuffer>(renderBufferIn);
if (!renderBuffer) exception("tried to bind to non-GL render buffer");
renderBuffer->bind();
bind();
checkGLError();
renderBuffersColor.push_back(renderBuffer);
nColorBuffers++;
}
void GLFrameBuffer::addDepthBuffer(std::shared_ptr<RenderBuffer> renderBufferIn) {
// it _better_ be a GL buffer
std::shared_ptr<GLRenderBuffer> renderBuffer = std::dynamic_pointer_cast<GLRenderBuffer>(renderBufferIn);
if (!renderBuffer) exception("tried to bind to non-GL render buffer");
renderBuffer->bind();
bind();
// Sanity checks
// if (depthRenderBuffer != nullptr) exception("OpenGL error: already bound to render buffer");
// if (depthTextureBuffer != nullptr) exception("OpenGL error: already bound to texture buffer");
checkGLError();
renderBuffersDepth.push_back(renderBuffer);
}
void GLFrameBuffer::addColorBuffer(std::shared_ptr<TextureBuffer> textureBufferIn) {
// it _better_ be a GL buffer
std::shared_ptr<GLTextureBuffer> textureBuffer = std::dynamic_pointer_cast<GLTextureBuffer>(textureBufferIn);
if (!textureBuffer) exception("tried to bind to non-GL texture buffer");
textureBuffer->bind();
bind();
checkGLError();
// Sanity checks
// if (colorRenderBuffer != nullptr) exception("OpenGL error: already bound to render buffer");
// if (colorTextureBuffer != nullptr) exception("OpenGL error: already bound to texture buffer");
checkGLError();
textureBuffersColor.push_back(textureBuffer);
nColorBuffers++;
}
void GLFrameBuffer::addDepthBuffer(std::shared_ptr<TextureBuffer> textureBufferIn) {
// it _better_ be a GL buffer
std::shared_ptr<GLTextureBuffer> textureBuffer = std::dynamic_pointer_cast<GLTextureBuffer>(textureBufferIn);
if (!textureBuffer) exception("tried to bind to non-GL texture buffer");
textureBuffer->bind();
bind();
checkGLError();
// Sanity checks
// if (depthRenderBuffer != nullptr) exception("OpenGL error: already bound to render buffer");
// if (depthTextureBuffer != nullptr) exception("OpenGL error: already bound to texture buffer");
checkGLError();
textureBuffersDepth.push_back(textureBuffer);
}
void GLFrameBuffer::setDrawBuffers() {
bind();
checkGLError();
}
bool GLFrameBuffer::bindForRendering() {
bind();
render::engine->currRenderFramebuffer = this;
render::engine->setCurrentViewport({0, 0, view::bufferWidth, view::bufferHeight});
checkGLError();
return true;
}
void GLFrameBuffer::clear() {
if (!bindForRendering()) return;
}
std::array<float, 4> GLFrameBuffer::readFloat4(int xPos, int yPos) {
// Read from the buffer
std::array<float, 4> result = {1., 2., 3., 4.};
return result;
}
float GLFrameBuffer::readDepth(int xPos, int yPos) {
// Read from the buffer
float result = 0.5;
return result;
}
std::vector<unsigned char> GLFrameBuffer::readBuffer() {
bind();
int w = getSizeX();
int h = getSizeY();
// Read from openGL
size_t buffSize = w * h * 4;
std::vector<unsigned char> buff(buffSize);
return buff;
}
void GLFrameBuffer::blitTo(FrameBuffer* targetIn) {
// it _better_ be a GL buffer
GLFrameBuffer* target = dynamic_cast<GLFrameBuffer*>(targetIn);
if (!target) exception("tried to blitTo() non-GL framebuffer");
// target->bindForRendering();
bindForRendering();
checkGLError();
}
uint32_t GLFrameBuffer::getNativeBufferID() { return 0; }
// =============================================================
// ================== Shader Program =========================
// =============================================================
GLCompiledProgram::GLCompiledProgram(const std::vector<ShaderStageSpecification>& stages, DrawMode dm) : drawMode(dm) {
// Collect attributes and uniforms from all of the shaders
for (const ShaderStageSpecification& s : stages) {
for (ShaderSpecUniform u : s.uniforms) {
addUniqueUniform(u);
}
for (ShaderSpecAttribute a : s.attributes) {
addUniqueAttribute(a);
}
for (ShaderSpecTexture t : s.textures) {
addUniqueTexture(t);
}
}
if (attributes.size() == 0) {
throw std::invalid_argument("Uh oh... GLProgram has no attributes");
}
// Perform setup tasks
compileGLProgram(stages);
setDataLocations();
}
GLCompiledProgram::~GLCompiledProgram() {}
void GLCompiledProgram::compileGLProgram(const std::vector<ShaderStageSpecification>& stages) {}
void GLCompiledProgram::setDataLocations() {
// Uniforms
for (GLShaderUniform& u : uniforms) {
}
// Attributes
for (GLShaderAttribute& a : attributes) {
}
// Textures
for (GLShaderTexture& t : textures) {
}
}
void GLCompiledProgram::addUniqueAttribute(ShaderSpecAttribute newAttribute) {
for (GLShaderAttribute& a : attributes) {
if (a.name == newAttribute.name) {
// if it occurs twice, confirm that the occurences match
if (a.type != newAttribute.type)
exception("attribute " + a.name + " appears twice in program with different types");
return;
}
}
attributes.push_back(GLShaderAttribute{newAttribute.name, newAttribute.type, newAttribute.arrayCount, nullptr});
}
void GLCompiledProgram::addUniqueUniform(ShaderSpecUniform newUniform) {
for (GLShaderUniform& u : uniforms) {
if (u.name == newUniform.name) {
// if it occurs twice, confirm that the occurences match
if (u.type != newUniform.type) exception("uniform " + u.name + " appears twice in program with different types");
return;
}
}
uniforms.push_back(GLShaderUniform{newUniform.name, newUniform.type, false});
}
void GLCompiledProgram::addUniqueTexture(ShaderSpecTexture newTexture) {
for (GLShaderTexture& t : textures) {
if (t.name == newTexture.name) {
// if it occurs twice, confirm that the occurences match
if (t.dim != newTexture.dim)
exception("texture " + t.name + " appears twice in program with different dimensions");
return;
}
}
textures.push_back(GLShaderTexture{newTexture.name, newTexture.dim, 777, false, nullptr, nullptr});
}
GLShaderProgram::GLShaderProgram(const std::shared_ptr<GLCompiledProgram>& compiledProgram_)
: ShaderProgram(compiledProgram_->getDrawMode()), uniforms(compiledProgram_->getUniforms()),
attributes(compiledProgram_->getAttributes()), textures(compiledProgram_->getTextures()),
compiledProgram(compiledProgram_) {
createBuffers(); // only handles texture & index things, attributes are lazily created
}
GLShaderProgram::~GLShaderProgram() {}
void GLShaderProgram::bindVAO() {}
void GLShaderProgram::createBuffers() {
bindVAO();
// Create an index buffer, if we're using one
if (useIndex) {
}
// === Generate textures
// Set indices sequentially
uint32_t iTexture = 0;
for (GLShaderTexture& t : textures) {
t.index = iTexture++;
}
}
void GLShaderProgram::setAttribute(std::string name, std::shared_ptr<AttributeBuffer> externalBuffer) {
bindVAO();
checkGLError();
for (GLShaderAttribute& a : attributes) {
if (a.name == name) {
// check that types match
int compatCount = renderDataTypeCountCompatbility(a.type, externalBuffer->getType());
if (compatCount == 0)
throw std::invalid_argument("Tried to set attribute " + name + " to incompatibile type. Attribute " +
renderDataTypeName(a.type) + " set with buffer of type " +
renderDataTypeName(externalBuffer->getType()));
// check multiple-set errors (duplicates in externalBuffers list?)
if (a.buff) throw std::invalid_argument("attribute " + name + " is already set");
// cast to the engine type (booooooo)
std::shared_ptr<GLAttributeBuffer> engineExtBuff = std::dynamic_pointer_cast<GLAttributeBuffer>(externalBuffer);
if (!engineExtBuff) throw std::invalid_argument("attribute " + name + " external buffer engine type cast failed");
a.buff = engineExtBuff;
a.buff->bind();
assignBufferToVAO(a);
return;
}
}
throw std::invalid_argument("Tried to set nonexistent attribute with name " + name);
}
void GLShaderProgram::assignBufferToVAO(GLShaderAttribute& a) {
bindVAO();
a.buff->bind();
checkGLError();
// Choose the correct type for the buffer
for (int iArrInd = 0; iArrInd < a.arrayCount; iArrInd++) {
switch (a.type) {
case RenderDataType::Float:
break;
case RenderDataType::Int:
break;
case RenderDataType::UInt:
break;
case RenderDataType::Vector2Float:
break;
case RenderDataType::Vector3Float:
break;
case RenderDataType::Vector4Float:
break;
case RenderDataType::Vector2UInt:
break;
case RenderDataType::Vector3UInt:
break;
case RenderDataType::Vector4UInt:
break;
default:
throw std::invalid_argument("Unrecognized GLShaderAttribute type");
break;
}
}
checkGLError();
}
void GLShaderProgram::createBuffer(GLShaderAttribute& a) {
// generate the buffer if needed
std::shared_ptr<AttributeBuffer> newBuff = engine->generateAttributeBuffer(a.type, a.arrayCount);
std::shared_ptr<GLAttributeBuffer> engineNewBuff = std::dynamic_pointer_cast<GLAttributeBuffer>(newBuff);
if (!engineNewBuff) throw std::invalid_argument("buffer type cast failed");
a.buff = engineNewBuff;
assignBufferToVAO(a);
}
void GLShaderProgram::ensureBufferExists(GLShaderAttribute& a) {
if (!a.buff) {
createBuffer(a);
}
}
bool GLShaderProgram::hasUniform(std::string name) {
for (GLShaderUniform& u : uniforms) {
if (u.name == name) {
return true;
}
}
return false;
}
// Set an integer
void GLShaderProgram::setUniform(std::string name, int val) {
for (GLShaderUniform& u : uniforms) {
if (u.name == name) {
if (u.type == RenderDataType::Int) {
u.isSet = true;
} else {
throw std::invalid_argument("Tried to set GLShaderUniform with wrong type");
}
return;
}
}
throw std::invalid_argument("Tried to set nonexistent uniform with name " + name);
}
// Set an unsigned integer
void GLShaderProgram::setUniform(std::string name, unsigned int val) {
for (GLShaderUniform& u : uniforms) {
if (u.name == name) {
if (u.type == RenderDataType::UInt) {
u.isSet = true;
} else {
throw std::invalid_argument("Tried to set GLShaderUniform with wrong type");
}
return;
}
}
throw std::invalid_argument("Tried to set nonexistent uniform with name " + name);
}
// Set a float
void GLShaderProgram::setUniform(std::string name, float val) {
for (GLShaderUniform& u : uniforms) {
if (u.name == name) {
if (u.type == RenderDataType::Float) {
u.isSet = true;
} else {
throw std::invalid_argument("Tried to set GLShaderUniform with wrong type");
}
return;
}
}
throw std::invalid_argument("Tried to set nonexistent uniform with name " + name);
}
// Set a double --- WARNING casts down to float
void GLShaderProgram::setUniform(std::string name, double val) {
for (GLShaderUniform& u : uniforms) {
if (u.name == name) {
if (u.type == RenderDataType::Float) {
u.isSet = true;
} else {
throw std::invalid_argument("Tried to set GLShaderUniform with wrong type");
}
return;
}
}
throw std::invalid_argument("Tried to set nonexistent uniform with name " + name);
}
// Set a 4x4 uniform matrix
void GLShaderProgram::setUniform(std::string name, float* val) {
for (GLShaderUniform& u : uniforms) {
if (u.name == name) {
if (u.type == RenderDataType::Matrix44Float) {
u.isSet = true;
} else {
throw std::invalid_argument("Tried to set GLShaderUniform with wrong type");
}
return;
}
}
throw std::invalid_argument("Tried to set nonexistent uniform with name " + name);
}
// Set a vector2 uniform
void GLShaderProgram::setUniform(std::string name, glm::vec2 val) {
for (GLShaderUniform& u : uniforms) {
if (u.name == name) {
if (u.type == RenderDataType::Vector2Float) {
u.isSet = true;
} else {
throw std::invalid_argument("Tried to set GLShaderUniform with wrong type");
}
return;
}
}
throw std::invalid_argument("Tried to set nonexistent uniform with name " + name);
}
// Set a vector3 uniform
void GLShaderProgram::setUniform(std::string name, glm::vec3 val) {
for (GLShaderUniform& u : uniforms) {
if (u.name == name) {
if (u.type == RenderDataType::Vector3Float) {
u.isSet = true;
} else {
throw std::invalid_argument("Tried to set GLShaderUniform with wrong type");