-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathGfxElements.cpp
1596 lines (1184 loc) · 44.8 KB
/
GfxElements.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 2014-2016 Kjetil S. Matheussen
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#include <stdint.h>
#include <unistd.h>
#include <map>
#include <vector>
#include <QMap>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
#if __GNUC__ >= 11
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wdeprecated-copy"
#endif
#include <vlCore/VisualizationLibrary.hpp>
#include <vlGraphics/OpenGLContext.hpp>
#include <vlVG/VectorGraphics.hpp>
#include <vlGraphics/Rendering.hpp>
#include <vlVG/SceneManagerVectorGraphics.hpp>
#include <vlGraphics/Effect.hpp>
#include <vlGraphics/GLSL.hpp>
#if __GNUC__ >= 11
# pragma GCC diagnostic pop
#endif
#pragma GCC diagnostic pop
#define USE_FREETYPE 0
#define RADIUM_DRAW_FONTS_DIRECTLY 0
#define INCLUDE_SHADERS 1
double g_opengl_scale_ratio = 1.0;
struct PaintingData;
static void setActorEnableMask(vl::Actor *actor, const PaintingData *painting_data);
#include "TextBitmaps.hpp"
#include "../common/nsmtracker.h"
#include "../common/OS_settings_proc.h"
#include "../common/Mutex.hpp"
#include "../common/QueueStack.hpp"
#include "../Qt/Qt_colors_proc.h"
#define OPENGL_GFXELEMENTS_CPP
#define GE_DRAW_VL
#include "GfxElements.h"
#include "T2.hpp"
#define DEBUG_PRINT 0
#if defined(RELEASE) && DEBUG_PRINT==1
#error "oops"
#endif
#define USE_TRIANGLE_STRIPS 0
#define NUM_PREDEFINED_COLORS 16
static float g_height = 512; // Only access from main thread
static float g_height_t2_thread = 512; // Only access from t2 thread
// Called from vl::Widget::resizeEvent
void GE_set_height(int height){
if (height<=0)
height = 1; // avoid various situations.
g_height = height;
}
int GE_get_height(void){
//return root->song->tracker_windows->wblock->t.y2 - root->song->tracker_windows->wblock->t.y1;
return g_height;
}
/*
int GE_get_tracker_height(){
return root->song->tracker_windows->wblock->t.y2 - root->song->tracker_windows->wblock->t.y1;
}
extern struct Root *root;
*/
GE_Rgb GE_get_rgb(enum ColorNums colornum, bool is_instrument){
QColor c = get_qcolor(colornum);
//GE_Rgb ret = {50,60,20,255};
if(is_instrument)
apply_instrument_in_editor_colorization(c);
GE_Rgb ret = {(unsigned char)c.red(), (unsigned char)c.green(), (unsigned char)c.blue(), (unsigned char)c.alpha()};
return ret;
}
GE_Rgb GE_get_custom_rgb(int custom_colornum){
const QColor c = get_custom_qcolor(custom_colornum);
GE_Rgb ret = {(unsigned char)c.red(), (unsigned char)c.green(), (unsigned char)c.blue(), (unsigned char)c.alpha()};
return ret;
}
#if INCLUDE_SHADERS
static vl::GLSLFragmentShader *get_gradient_fragment_shader(GradientType::Type type){
static vl::ref<vl::GLSLFragmentShader> gradient_velocity_shader = NULL;
static vl::ref<vl::GLSLFragmentShader> gradient_horizontal_shader = NULL;
if(gradient_velocity_shader.get()==NULL) {
vl::String path1 =
vl::String(OS_get_program_path2().id)
.append(vl::String(OS_get_directory_separator()))
.append(vl::String("glsl"))
.append(vl::String(OS_get_directory_separator()))
;
vl::String path2 = path1;
gradient_velocity_shader = new vl::GLSLFragmentShader(path1.append("gradient.fs"));
gradient_horizontal_shader = new vl::GLSLFragmentShader(path2.append("horizontal_gradient.fs"));
}
if (type==GradientType::VELOCITY)
return gradient_velocity_shader.get();
else if (type==GradientType::HORIZONTAL)
return gradient_horizontal_shader.get();
else
RWarning("Unknown gradient type %d", type);
return NULL;
}
#endif
#if 0
static vl::GLSLVertexShader *get_gradient_vertex_shader(void){
static vl::ref<vl::GLSLVertexShader> gradient_shader = NULL;
if(gradient_shader.get()==NULL) {
vl::String path =
vl::String(OS_get_program_path2())
.append(vl::String(OS_get_directory_separator()))
.append(vl::String("glsl"))
.append(vl::String(OS_get_directory_separator()))
.append(vl::String("gradient.vs"))
;
gradient_shader = new vl::GLSLVertexShader(path);
}
return gradient_shader.get();
}
#endif
struct GradientTriangles : public vl::Effect {
GradientTriangles *next = NULL;
std::vector<vl::dvec2> triangles;
GradientType::Type type;
float y, height;
float x, width;
vl::fvec4 color1;
vl::fvec4 color2;
private:
vl::ref<vl::GLSLProgram> glsl; // seems like reference must be stored here to avoid memory problems.
bool glsl_is_valid = true;
vl::Uniform *uniform_y;
vl::Uniform *uniform_height;
vl::Uniform *uniform_color1;
vl::Uniform *uniform_color2;
vl::Uniform *uniform_x;
vl::Uniform *uniform_width;
public:
MyIMutex ref_mutex;
GradientTriangles(GradientType::Type type)
: type(type)
{
setRefCountMutex(&ref_mutex);
setAutomaticDelete(false);
init_shader();
}
~GradientTriangles(){
RError("~GradientTriangles was called. That should not happen. Expect crash.");
//abort();
}
private:
void init_shader(void){
#if !INCLUDE_SHADERS
glsl_is_valid = false;
#else
vl::Shader* shader = this->shader();
shader->enable(vl::EN_BLEND);
// These two lines probably takes a lot of time.
glsl = shader->gocGLSLProgram();
if (glsl->attachShader(get_gradient_fragment_shader(type)) == false){
GFX_Message2(NULL, true, "Unable to create OpenGL Shader. It might help to disable \"Draw in separate process\".");
glsl_is_valid = false;
return;
}
// And this one.
glsl->linkProgram();
if (glsl->linked()==false){
GFX_Message2(NULL, true, "Linking OpenGL Shader failed. It might help to disable \"Draw in separate process\".");
glsl_is_valid = false;
return;
}
if (type==GradientType::VELOCITY) {
uniform_y = glsl->gocUniform("y");
uniform_height = glsl->gocUniform("height");
}
uniform_color1 = glsl->gocUniform("color1");
uniform_color2 = glsl->gocUniform("color2");
uniform_x = glsl->gocUniform("x");
uniform_width = glsl->gocUniform("width");
#endif
}
public:
vl::Actor *render(vl::VectorGraphics *vg) {
vl::Actor *actor = vg->fillTriangles(triangles);
if (glsl_is_valid==true) {
actor->setEffect(this);
const double scale_ratio = safe_double_read(&g_opengl_scale_ratio);
uniform_color1->setUniform(color1);
uniform_color2->setUniform(color2);
if (type==GradientType::VELOCITY)
uniform_height->setUniformF(height*scale_ratio);
uniform_x->setUniformF(x*scale_ratio);
uniform_width->setUniformF(width);
if (type==GradientType::VELOCITY)
set_y_offset(0.0f);
}
return actor;
}
// called from main thread
void clean(){
triangles.clear();
}
// OpenGL thread, except when initializing. During initialization, it's T3 thread.
void set_y_offset(float y_offset){
if (glsl_is_valid && type==GradientType::VELOCITY)
uniform_y->setUniformF((y + y_offset)*safe_double_read(&g_opengl_scale_ratio));
}
};
#if DEBUG_PRINT
static int gradlist_length(GradientTriangles *gradients){
int ret = 0;
while(gradients!=NULL){
ret++;
gradients = gradients->next;
}
return ret;
}
#endif
static void TS_push_free_gradient_triangle(GradientType::Type type, GradientTriangles* gradient);
#define GQUEUE_MIN_SIZE 128 // If we have less gradients than this, we panic and allocate immediately.
#define GQUEUE_MAX_SIZE 256 // If we have less gradients than this, we allocate new ones when it's a good time to do so.
#define GQUEUE_TO_TYPE(I) ((I)==0 ? GradientType::HORIZONTAL : GradientType::VELOCITY)
#define TYPE_TO_GQUEUE(T) (T==GradientType::HORIZONTAL ? 0 : 1)
// Having two collections is not necessary, but I figured the code would be simpler this way (not so sure anymore).
// Buffer1 (GradientTrianglesCollection1) holds gradients which are currently used and
// gradients which have been used earlier, while Buffer2 (GradientTrianglesCollection2) holds fresh, i.e. never used, gradients.
// Of course, the "earlier used" and the "never used" buffers could be merged, and the code would probably be cleaner if we did.
//
// Why so complicated? Because there's some indication (virtualbox opengl driver) that drivers sometimes doesn't work
// if we allocate shaders on a different thread than the main OpenGL thread. Earlier we allocated in the T2 thread,
// which has it's own context, so it should be fine, but it didn't work with the virtualbox opengl driver. It's not very
// important what the virtualbox opengl driver does though, but it might be an indication that it's better to do it this way.
// Maybe it will fix the crashes that happens when enabling "draw in separate process" on OSX.
struct GradientTrianglesCollection2 {
GradientType::Type _type;
radium::Queue<GradientTriangles*, GQUEUE_MAX_SIZE> _queue; // t3 -> t2
DEFINE_ATOMIC(int, _num_gradients_to_push_to_1) = 0;
#if !defined(RELEASE)
int _num_created_total = 0;
int _num_requested_total = 0;
#endif
GradientTrianglesCollection2(GradientType::Type type)
: _type(type)
{}
DEFINE_ATOMIC(int, _visitnum) = 0;
bool T3_create_gradienttriangles_if_needed(bool force_creating_all, bool has_alloced_this_time, bool spent_some_time_last_call, bool got_new_t2_data){
bool has_alloced = false;
int size = _queue.size();
if (size > GQUEUE_MIN_SIZE && !force_creating_all)
if (has_alloced_this_time || spent_some_time_last_call || got_new_t2_data || is_playing()) // Preferably, we only create at most one shader each second frame while not playing. We don't want to allocate when we get new t2 data either, because those frames takes much longer time to render.
return has_alloced;
int num_to_push_to_1 = ATOMIC_GET(_num_gradients_to_push_to_1);
int num_pushed_to_1 = 0;
while (size < GQUEUE_MAX_SIZE || num_to_push_to_1>num_pushed_to_1){
#if DEBUG_PRINT
printf("T3: Creating new GradientTriangles instance. Queue #%d. queue size: %d. NULL: %d, Total: %d\n", TYPE_TO_GQUEUE(_type), size, num_to_push_to_1, ++_num_created_total);
#endif
auto *gradient = new GradientTriangles(_type);
has_alloced = true;
if (size==GQUEUE_MAX_SIZE){
TS_push_free_gradient_triangle(_type, gradient);
num_pushed_to_1++;
}else{
_queue.put(gradient);
size++;
}
if (size > GQUEUE_MIN_SIZE && !force_creating_all)
break;
}
if (num_pushed_to_1 > 0){
int newnum = ATOMIC_ADD_RETURN_NEW(_num_gradients_to_push_to_1, -num_pushed_to_1);
if (newnum < 0)
ATOMIC_SET(_num_gradients_to_push_to_1, 0);
}
ATOMIC_ADD(_visitnum, 1);
return has_alloced;
}
void T1_wait_until_all_gradients_are_created(void){
int visitnum = ATOMIC_GET(_visitnum);
if (visitnum==0) // Program startup. Deadlock without this check.
return;
#if THREADED_OPENGL
do{
msleep(20);
}while(ATOMIC_GET(_visitnum) < visitnum+2 || _queue.size() < GQUEUE_MAX_SIZE || ATOMIC_GET(_num_gradients_to_push_to_1) > 0);
#endif
}
void T1_prepare_for_new_rendering(void){
ATOMIC_SET(_num_gradients_to_push_to_1, 0); // If not, this number can grow very large.
}
GradientTriangles *T1_get_gradienttriangles(void){
int size = _queue.size();
#if DEBUG_PRINT
printf("T1: Requesting new gradient triangles. Queue #%d. queue size: %d. Total: %d. \n", TYPE_TO_GQUEUE(_type), size, ++_num_requested_total);
#endif
if (size > 0)
return _queue.get();
else{
ATOMIC_ADD(_num_gradients_to_push_to_1, 1);
return NULL;
}
}
};
static GradientTrianglesCollection2 horizontalGradientTriangles2(GradientType::HORIZONTAL);
static GradientTrianglesCollection2 velocityGradientTriangles2(GradientType::VELOCITY);
static GradientTrianglesCollection2 &get_collection2(GradientType::Type type){
if (type==GradientType::HORIZONTAL)
return horizontalGradientTriangles2;
else
return velocityGradientTriangles2;
}
void T3_create_gradienttriangles_if_needed(bool got_new_t2_data){
static bool spent_some_time_last_call = false;
static bool has_inited = false;
GradientTrianglesCollection2 *queue1, *queue2;
if (horizontalGradientTriangles2._queue.size() < velocityGradientTriangles2._queue.size()){
queue1 = &horizontalGradientTriangles2;
queue2 = &velocityGradientTriangles2;
} else {
queue1 = &velocityGradientTriangles2;
queue2 = &horizontalGradientTriangles2;
}
bool force_creating_all = has_inited==false || ATOMIC_GET(g_is_creating_all_GL_blocks);
bool has_alloced_this_time1 = queue1->T3_create_gradienttriangles_if_needed(force_creating_all, false, spent_some_time_last_call, got_new_t2_data);
bool has_alloced_this_time2 = queue2->T3_create_gradienttriangles_if_needed(force_creating_all, has_alloced_this_time1, spent_some_time_last_call, got_new_t2_data);
spent_some_time_last_call = has_alloced_this_time1 || has_alloced_this_time2 || got_new_t2_data;
has_inited = true;
}
static void T1_wait_until_all_gradients_are_created(void){
horizontalGradientTriangles2.T1_wait_until_all_gradients_are_created();
velocityGradientTriangles2.T1_wait_until_all_gradients_are_created();
}
static void T1_prepare_gradients2_for_new_rendering(void){
horizontalGradientTriangles2.T1_prepare_for_new_rendering();
velocityGradientTriangles2.T1_prepare_for_new_rendering();
}
static GradientTriangles *T1_get_gradienttriangles2(GradientType::Type type){
return get_collection2(type).T1_get_gradienttriangles();
}
struct GradientTrianglesCollection {
GradientType::Type type;
radium::Mutex lock;
// These two are only accessed by the main thread
GradientTriangles *used_gradient_triangles; // Only used by T3.
GradientTriangles *free_gradient_triangles; // Used by both T1 and T3. Must be protected by lock
GradientTrianglesCollection(GradientType::Type type)
: type(type)
, used_gradient_triangles(NULL)
, free_gradient_triangles(NULL)
{}
#if DEBUG_PRINT
int freesize(void){
radium::ScopedMutex daslock(lock);
return gradlist_length(free_gradient_triangles);
}
#endif
GradientTriangles *TS_get_free_gradient_triangle(void){
radium::ScopedMutex daslock(lock);
GradientTriangles *gradient = free_gradient_triangles;
if (gradient == NULL) {
return NULL;
} else {
free_gradient_triangles = gradient->next;
return gradient;
}
}
void TS_push_free_gradient_triangle(GradientTriangles *gradient){
radium::ScopedMutex daslock(lock);
R_ASSERT_NON_RELEASE(gradient->next==NULL);
gradient->next = free_gradient_triangles;
free_gradient_triangles = gradient;
}
void TS_push_several_free_gradient_triangle(GradientTriangles *gradients, GradientTriangles *last_element){
if (gradients==NULL){
R_ASSERT(last_element==NULL);
} else {
radium::ScopedMutex daslock(lock);
last_element->next = free_gradient_triangles;
free_gradient_triangles = gradients;
}
}
// main thread
void T1_collect_gradient_triangles_garbage(void){
GradientTriangles *new_used = NULL;
GradientTriangles *new_free = NULL;
GradientTriangles *last_new_free = NULL;
//R_ASSERT(free_gradient_triangles == NULL); // T3 could have pushed a free gradient since last check.
#if DEBUG_PRINT
int bef = gradlist_length(used_gradient_triangles);
#endif
GradientTriangles *gradient = used_gradient_triangles;
#if DEBUG_PRINT
int numa=0,numb=0;
#endif
while(gradient!=NULL){
GradientTriangles *next = gradient->next;
gradient->ref_mutex.lock();
bool is_free = gradient->referenceCount()==0;
gradient->ref_mutex.unlock();
if(is_free) {
gradient->next = new_free;
if (new_free==NULL)
last_new_free = gradient;
new_free = gradient;
#if DEBUG_PRINT
numa++;
#endif
} else {
gradient->next = new_used;
new_used = gradient;
}
#if DEBUG_PRINT
numb++;
#endif
gradient = next;
}
/*
// We clean it when allocating instead. Might cause less cpu spikes.
gradient = new_free;
while(gradient != NULL){
gradient->clean();
gradient = gradient->next;
}
*/
#if DEBUG_PRINT
int aft = gradlist_length(new_used);
int fbef = freesize();
#endif
used_gradient_triangles = new_used;
TS_push_several_free_gradient_triangle(new_free, last_new_free);
#if DEBUG_PRINT
printf(" Collecting gradient garbage finished #%d. %d / %d. Len: %d / %d. Free: %d/%d\n",TYPE_TO_GQUEUE(type), numa,numb,bef,aft,fbef,freesize());
#endif
}
// main thread
GradientTriangles *T1_get_gradient_triangles(void){
GradientTriangles *gradient = TS_get_free_gradient_triangle(); // Reuse old.
if (gradient!=NULL)
gradient->clean();
else
gradient = ::T1_get_gradienttriangles2(type); // Get a new.
if (gradient==NULL)
return NULL;
// push used
gradient->next = used_gradient_triangles;
used_gradient_triangles = gradient;
//printf("Size of #%d: %d. Freesize: %d\n", TYPE_TO_GQUEUE(type), gradlist_length(used_gradient_triangles), freesize());
return gradient;
}
};
static GradientTrianglesCollection horizontalGradientTriangles(GradientType::HORIZONTAL);
static GradientTrianglesCollection velocityGradientTriangles(GradientType::VELOCITY);
static GradientTrianglesCollection &get_collection(GradientType::Type type){
if (type==GradientType::HORIZONTAL)
return horizontalGradientTriangles;
else
return velocityGradientTriangles;
}
static GradientTriangles *T1_get_gradient_triangles(GradientType::Type type){
return get_collection(type).T1_get_gradient_triangles();
}
static void TS_push_free_gradient_triangle(GradientType::Type type, GradientTriangles* gradient){
get_collection(type).TS_push_free_gradient_triangle(gradient);
}
// Called from GE_start_writing();
static void T1_collect_gradients1_garbage(void){
horizontalGradientTriangles.T1_collect_gradient_triangles_garbage();
velocityGradientTriangles.T1_collect_gradient_triangles_garbage();
}
struct _GE_Context : public vl::Object{
//std::map< int, std::vector<vl::dvec2> > lines; // lines, boxes and polylines
std::vector< vl::dvec2 > boxes; // filled boxes
#if USE_TRIANGLE_STRIPS
std::vector< std::vector<vl::dvec2> > trianglestrips; // Used instead of polygons for displaying waveform data. Trianglestrips are directly supported by opengl, so it's faster plus that conversions from polygons to triangles are unnecessary.
#else
std::vector<vl::dvec2> triangles; // filled triangles. Used instead of trianglestrips. Just paint all triangles one by one. GPU usage seems approxiately the same as tringle strips, but CPU usage is significantly lower.
#endif
TextBitmaps textbitmaps;
TextBitmaps textbitmaps_halfsize;
union Color{
struct{
GE_Rgb c;
GE_Rgb c_gradient;
};
uint64_t key;
} color;
GE_Conf _conf;
int _slice;
mutable vl::ref<vl::Scissor> _scissor;
std::vector< vl::ref<GradientTriangles> > gradient_triangles;
//private:
// vl::ref<vl::Image> gradient;
public:
//bool is_gradient;
/*
vl::ref<vl::Image> get_gradient(){
if(gradient.get()==NULL)
gradient = vl::makeColorSpectrum(128,
get_vec4(color.c),
get_vec4(color.c_gradient)
);
return gradient;
}
*/
_GE_Context(const Color &_color, const GE_Conf &conf, int slice)
: textbitmaps(false)
, textbitmaps_halfsize(true)
//, has_scissor(false)
, color(_color)
, _conf(conf)
, _slice(slice)
//, gradient(NULL)
//, is_gradient(false)
{
//static int num = 0; printf("num2: %d\n", num++);
R_ASSERT(sizeof(Color)==sizeof(uint64_t));
}
vl::Scissor *get_scissor(const PaintingData *painting_data) const {
if (_conf.use_scissors==NO_SCISSORS)
return NULL;
if (_scissor.get()==NULL){
//static int num = 0; printf("num: %d\n", num++);
const SharedVariables *shared_variables = GE_get_shared_variables(painting_data);
const double scale_ratio = safe_double_read(&g_opengl_scale_ratio);
_scissor = new vl::Scissor(shared_variables->wtracks_scissor_x1 * scale_ratio,
0,
(shared_variables->wtracks_scissor_x2 - shared_variables->wtracks_scissor_x1) * scale_ratio,
g_height_t2_thread*scale_ratio
);
}
return _scissor.get();
}
#if 0
~_GE_Context(){
printf("Deleting context with z %d\n",_conf.z);
}
#endif
static float y(float y)
{
float height = g_height;
float ret = scale(y,
0,height,
height,0
);
//if (g_opengl_scale_ratio > 1.0)
// ret -= height/g_opengl_scale_ratio;
//else if (g_opengl_scale_ratio < 1.0)
// ret += height/2.0;
return ret;
}
vl::Transform *get_transform(T2_data *t2_data, bool &is_scroll_transform) const {
is_scroll_transform = false;
if (Z_IS_STATIC_X(_conf.z)){
is_scroll_transform = true;
return t2_data->scroll_transform.get();
}else if (_conf.z == Z_PLAYCURSOR)
return t2_data->playcursor_transform.get();
else if (_conf.z <= Z_MAX_SCROLLTRANSFORM){
is_scroll_transform = true;
return t2_data->scroll_transform.get();
}else if (_conf.z < Z_MIN_STATIC)
return t2_data->scrollbar_transform.get();
else
return NULL;
}
};
void GE_set_z(GE_Context *c, int new_z) {
c->_conf.z = new_z;
}
int GE_get_z(const GE_Context *c){
return c->_conf.z;
}
GE_Rgb GE_get_rgb(const GE_Context *c){
return c->color.c;
}
/* Drawing */
static void setActorEnableMask(vl::Actor *actor, const PaintingData *painting_data){
int height = g_height_t2_thread;
int y1 = scale(actor->boundingBox().maxCorner().y(),
height, 0,
0, height);
int y2 = scale(actor->boundingBox().minCorner().y(),
height, 0,
0, height
);
//printf(" y1: %d, y2: %d. mask: %x\n",y1,y2,getMask(y1,y2));
actor->setEnableMask(getMask(y1, y2, GE_get_slice_size(painting_data)));
}
static void setScrollTransform(const GE_Context *c, vl::Actor *actor, T2_data *t2_data){
vl::Scissor *scissor = c->get_scissor(t2_data->painting_data);
if (scissor != NULL)
actor->setScissor(scissor);
actor->computeBounds();
bool set_mask;
vl::Transform *transform = c->get_transform(t2_data, set_mask);
if (set_mask)
setActorEnableMask(actor, t2_data->painting_data);
actor->setTransform(transform);
}
/*
static int get_key_from_pen_width(float pen_width){
int ret = pen_width * 10;
if (ret==0)
ret = 1;
return ret;
}
static float get_pen_width_from_key(int key){
return (float)key/10.0;
}
*/
typedef QMap<int, // <- z
QHash<int, // <- block slice (from calculated from y)
QHash<enum UseScissors, // <- whether to use scissor. 0: no scissor, 1: scissor (tried to use array instead of hash, but the code became too complicated)
QHash<uint64_t, // <- color
vl::ref<GE_Context>
>
>
>
> Contexts;
// Contains all data necessary to paint the editor.
// Created in the main thread, and then transfered to the OpenGL thread.
struct PaintingData{
Contexts contexts;
//std::vector< vl::ref<GradientTriangles> > gradient_triangles;
SharedVariables shared_variables;
int slice_size;
PaintingData(int full_height, bool block_is_visible)
: shared_variables(block_is_visible)
{
slice_size = full_height / 32;
if (slice_size < MIN_SLICE_SIZE)
slice_size = MIN_SLICE_SIZE;
}
};
void GE_delete_painting_data(PaintingData *painting_data){
delete painting_data;
}
// This variable is only accessed by the main thread while building up a new PaintingData.
// It is not necessary for this variable to be global, and the code is more confusing because of that.
// However, by letting it be global, we don't have to send it around everywhere.
//
// In short: It can only be used by the main thread while while GL_create is called.
static PaintingData *g_painting_data = NULL;
// Called from the OpenGL thread
const SharedVariables *GE_get_shared_variables(const PaintingData *painting_data){
return &painting_data->shared_variables;
}
int GE_get_slice_size(const PaintingData *painting_data){
return painting_data->slice_size;
}
// Called from the main thread
void GE_start_writing(int full_height, bool block_is_visible){
R_ASSERT(g_painting_data==NULL);
T1_ensure_t2_is_initialized();
g_painting_data = new PaintingData(full_height, block_is_visible);
GE_fill_in_shared_variables(&g_painting_data->shared_variables, g_height);
T1_prepare_gradients2_for_new_rendering();
T1_collect_gradients1_garbage();
}
// Called from the main thread
void GE_end_writing(GE_Rgb new_background_color){
T1_send_data_to_t2(g_painting_data, new_background_color);
g_painting_data = NULL;
}
// Called from the main thread. Only used when loading song to ensure all gradients are created before starting to play.
void GE_wait_until_block_is_rendered(void){
T1_wait_until_t2_got_t1_data();
// T1_wait_until_t3_got_t2_data(); // A bit inconvenient to make that function since the t2_to_t3 queue is not threadsafe. Instead we let T1_wait_until_all_gradients_are_created wait at least two periods.
T1_wait_until_all_gradients_are_created();
}
/*****************************************/
/* Drawing. Called from OpenGL thread. */
/****************************************/
static void setColorBegin(vl::VectorGraphics *vg, const GE_Context *c){
#if 0
if(false && c->is_gradient){
vg->setImage(c->get_gradient().get());
vg->setColor(vl::white);
} else
#endif
vg->setColor(get_vec4(c->color.c));
}
static void setColorEnd(vl::VectorGraphics *vg, const GE_Context *c){
#if 0
if(c->is_gradient)
vg->setImage(NULL);
#endif
}
// This function can probably be avoided somehow. The absolute y position of the vertexes should be available for the shader GLSL code, but I haven't
// figured out how to get it yet.
//
// OpenGL Thread
void GE_update_triangle_gradient_shaders(PaintingData *painting_data, float y_offset){
for (const auto &hepp : painting_data->contexts) {
for (const auto &contexts : hepp) {
for (const auto &c2 : contexts) {
for (const auto &c : c2) {
for (vl::ref<GradientTriangles> gradient_triangles : c->gradient_triangles)
gradient_triangles->set_y_offset(y_offset);
}
}
}
}
}
// T2 thread.
void GE_draw_vl(T2_data *t2_data){
//GL_draw_lock();
PaintingData *painting_data = t2_data->painting_data;
g_height_t2_thread = painting_data->shared_variables.opengl_widget_height;
vl::VectorGraphics *vg = t2_data->vg.get();
vg->startDrawing(); {
#if RADIUM_DRAW_FONTS_DIRECTLY
vg->setFont("font/Cousine-Bold.ttf", 10, false);
#endif
vg->setLineSmoothing(true);
vg->setPolygonSmoothing(true);
//vg->setPointSmoothing(true); /* default value */
//vg->setPointSmoothing(root->editonoff); /* default value */
vg->setPointSmoothing(false); // images are drawn using drawPoint.
//vg->setTextureMode(vl::TextureMode_Repeat ); // Note: MAY FIX gradient triangle non-overlaps.
/*
vl::EBlendFactor src_rgb;
vl::EBlendFactor dst_rgb;
vl::EBlendFactor src_alpha;
vl::EBlendFactor dst_alpha;
vg->getBlendFunc(src_rgb,dst_rgb,src_alpha,dst_alpha);
vg->setBlendFunc(src_rgb, dst_rgb, vl::BF_ONE_MINUS_SRC_ALPHA, vl::BF_ONE_MINUS_DST_ALPHA);
*/
for (const auto &hepp : painting_data->contexts) {
for (const auto &contexts : hepp) {
for (const auto &c2 : contexts) {
// 1. Filled boxes
for(const auto &c : c2){
if(c->boxes.size() > 0) {
setColorBegin(vg, c.get());
setScrollTransform(c.get(), vg->fillQuads(c->boxes), t2_data);
setColorEnd(vg, c.get());
}