~thumper/nux/next-changes

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
/*
 * Copyright 2010 Inalogic® Inc.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License, as
 * published by the  Free Software Foundation; either version 2.1 or 3.0
 * of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of both the GNU Lesser General Public
 * License along with this program. If not, see <http://www.gnu.org/licenses/>
 *
 * Authored by: Jay Taoko <jaytaoko@inalogic.com>
 *
 */


#ifndef OPENGLENGINE_H
#define OPENGLENGINE_H

#include "GLResource.h"
#include "GpuDevice.h"
#include "GLDeviceObjects.h"
#include "GLResourceManager.h"
#include "GLTextureResourceManager.h"
#include "GLVertexResourceManager.h"
#include "GLRenderStates.h"
#include "FontTexture.h"
#include "RenderingPipe.h"
#include "GLShader.h"

#if defined(NUX_OS_WINDOWS)
  #include "GraphicsDisplay.h"
  #include "GLWindowManager.h"
#elif defined(NUX_OS_LINUX)
  #include "GraphicsDisplay.h"
  #include "GLWindowManager.h"
#endif

#define NUX_MAX_GAUSSIAN_SIGMA 11
#define NUX_MIN_GAUSSIAN_SIGMA 1

namespace nux
{

  class FontTexture;
  class FontRenderer;
  class FilePath;
  class BaseTexture;
  class TextureRectangle;

  class ROPConfig
  {
  public:
    ROPConfig();
    ~ROPConfig();

    bool Blend;
    t_u32 SrcBlend;
    t_u32 DstBlend;

    static ROPConfig Default;
  };

  class BlendOperator
  {
  public:
    BlendOperator ();
    ~BlendOperator ();

    void EnableBlending (bool enable);
    void SetPorterDuffOperator (PorterDuffOperator op);
    void SetCustomBlendOperator (unsigned int src_blend, unsigned int dst_blend);

    bool  _enable;
    unsigned int _src_blend;
    unsigned int _dst_blend;
  };

  typedef struct
  {
    int x;
    int y;
    int width;
    int height;

  } OpenGLContext;

  //! Rendering engine class
  class GraphicsEngine
  {
  public:
    /*!
      @param GlWindow The graphics window for this rendering engine.
      @param create_rendering_data If true, then in GraphicsEngine, the system creates the OpenGL shaders and the font textures for the rendering.
    */
    GraphicsEngine (GraphicsDisplay &GlWindow, bool create_rendering_data = true);
    ~GraphicsEngine();

    void SetContext (int x, int y, int width, int height);
    void GetContextSize (int &w, int &h) const;
    int GetContextWidth() const;
    int GetContextHeight() const;
    int GetContextX() const;
    int GetContextY() const;

    void GetWindowSize (int &w, int &h) const;
    int GetWindowWidth() const;
    int GetWindowHeight() const;

    OpenGLContext m_CurrrentContext;

    // Load Textures
    BaseTexture *Load2DTextureFile (const char *filename);
    BaseTexture *Load2DRectangleTextureFile (const char *filename);
    BaseTexture *Load2DTextureFileGenerateAlpha (const char *filename, int red, int green, int blue);

    void SetTexture (int TextureUnit, BaseTexture *Texture);
    void SetTexture (int TextureUnit, ObjectPtr< IOpenGLBaseTexture > Texture);
    void EnableTextureMode (int TextureUnit, int TextureMode);
    void DisableTextureMode (int TextureUnit, int TextureMode);
    void DisableAllTextureMode (int TextureUnit);


    ///////////////////
    // DRAW TEXTURE  //
    ///////////////////

    // Neutral
    void QRP_1Tex (int x, int y, int width, int height, ObjectPtr<IOpenGLBaseTexture> Tex0, TexCoordXForm &texxform, const Color &color0);
    void QRP_Pixelate (int x, int y, int width, int height, ObjectPtr<IOpenGLBaseTexture> DeviceTexture, TexCoordXForm &texxform, const Color &c0, int pixel_size);
    void QRP_Color (int x, int y, int width, int height, const Color &c0);
    void QRP_Color (int x, int y, int width, int height, const Color &c0, const Color &c1, const Color &c2, const Color &c3);
    void QRP_ColorModTexAlpha (int x, int y, int width, int height,
      ObjectPtr<IOpenGLBaseTexture> DeviceTexture, TexCoordXForm &texxform0, const Color &color);

    void QRP_2Tex (int x, int y, int width, int height,
      ObjectPtr<IOpenGLBaseTexture> DeviceTexture0, TexCoordXForm &texxform0, const Color &color0,
      ObjectPtr<IOpenGLBaseTexture> DeviceTexture1, TexCoordXForm &texxform1, const Color &color1);

    void QRP_2TexMod (int x, int y, int width, int height,
      ObjectPtr<IOpenGLBaseTexture> DeviceTexture0, TexCoordXForm &texxform0, const Color &color0,
      ObjectPtr<IOpenGLBaseTexture> DeviceTexture1, TexCoordXForm &texxform1, const Color &color1);

    void QRP_4Tex (int x, int y, int width, int height,
      ObjectPtr<IOpenGLBaseTexture> DeviceTexture0, TexCoordXForm &texxform0, const Color &color0,
      ObjectPtr<IOpenGLBaseTexture> DeviceTexture1, TexCoordXForm &texxform1, const Color &color1,
      ObjectPtr<IOpenGLBaseTexture> DeviceTexture2, TexCoordXForm &texxform2, const Color &color2,
      ObjectPtr<IOpenGLBaseTexture> DeviceTexture3, TexCoordXForm &texxform3, const Color &color3);

    void QRP_Triangle (int x0, int y0, int x1, int y1, int x2, int y2, Color c0);
    void QRP_Triangle (int x0, int y0, int x1, int y1, int x2, int y2, Color c0, Color c1, Color c2);
    void QRP_Line (int x0, int y0, int x1, int y1, Color c0);
    void QRP_Line (int x0, int y0, int x1, int y1, Color c0, Color c1);
    void QRP_QuadWireframe (int x0, int y0, int width, int height, Color c0, Color c1, Color c2, Color c3);


    ObjectPtr<IOpenGLBaseTexture> QRP_GetBlurTexture (
      int x, int y,
      int buffer_width, int buffer_height,
      ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform,
      const Color& c0,
      float sigma = 1.0f, int num_pass = 1);

    ObjectPtr<IOpenGLBaseTexture> QRP_GetAlphaTexture (
      ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform,
      const Color& c0);

    ObjectPtr<IOpenGLBaseTexture> QRP_GetColorMatrixTexture (
      ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform,
      const Color& c0,
      Matrix4 color_matrix, Vector4 offset);

    ObjectPtr<IOpenGLBaseTexture> QRP_GetPower (
      ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform, const Color& c0, const Vector4 &exponent);

    ObjectPtr<IOpenGLBaseTexture> QRP_GetLQBlur (
      int x, int y, int buffer_width, int buffer_height,
      ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform,
      const Color& c0);

    ObjectPtr<IOpenGLBaseTexture> QRP_GetHQBlur (
      int x, int y, int buffer_width, int buffer_height,
      ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform,
      const Color& c0, float sigma = 1.0f, int num_pass = 1);

    void QRP_DisturbedTexture (
      int x, int y, int width, int height,
      ObjectPtr<IOpenGLBaseTexture> distorsion_texture, TexCoordXForm &texxform0, const Color& c0,
      ObjectPtr<IOpenGLBaseTexture> src_device_texture, TexCoordXForm &texxform1, const Color& c1);

	  ObjectPtr<IOpenGLBaseTexture> QRP_GetPixelBlocks (
		  ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform0, const Color& c0, int pixel_size);

    void QRP_GetCopyTexture (
      int width, int height,
      ObjectPtr<IOpenGLBaseTexture>& dst_device_texture,
      ObjectPtr<IOpenGLBaseTexture>& src_device_texture,
      TexCoordXForm &texxform0, const Color& c0);

    // ASM
    void QRP_ASM_1Tex (int x, int y, int width, int height, ObjectPtr<IOpenGLBaseTexture> Tex0, TexCoordXForm &texxform, const Color &color0);
    void QRP_ASM_Pixelate (int x, int y, int width, int height, ObjectPtr<IOpenGLBaseTexture> DeviceTexture, TexCoordXForm &texxform, const Color &c0, int pixel_size);

    void QRP_ASM_Color (int x, int y, int width, int height, const Color &c0);
    void QRP_ASM_Color (int x, int y, int width, int height, const Color &c0, const Color &c1, const Color &c2, const Color &c3);
    void QRP_ASM_ColorModTexAlpha (int x, int y, int width, int height,
                               ObjectPtr<IOpenGLBaseTexture> DeviceTexture, TexCoordXForm &texxform0, const Color &color);

    void QRP_ASM_2Tex (int x, int y, int width, int height,
                   ObjectPtr<IOpenGLBaseTexture> DeviceTexture0, TexCoordXForm &texxform0, const Color &color0,
                   ObjectPtr<IOpenGLBaseTexture> DeviceTexture1, TexCoordXForm &texxform1, const Color &color1);

    void QRP_ASM_2TexMod (int x, int y, int width, int height,
      ObjectPtr<IOpenGLBaseTexture> DeviceTexture0, TexCoordXForm &texxform0, const Color &color0,
      ObjectPtr<IOpenGLBaseTexture> DeviceTexture1, TexCoordXForm &texxform1, const Color &color1);

    void QRP_ASM_4Tex (int x, int y, int width, int height,
                   ObjectPtr<IOpenGLBaseTexture> DeviceTexture0, TexCoordXForm &texxform0, const Color &color0,
                   ObjectPtr<IOpenGLBaseTexture> DeviceTexture1, TexCoordXForm &texxform1, const Color &color1,
                   ObjectPtr<IOpenGLBaseTexture> DeviceTexture2, TexCoordXForm &texxform2, const Color &color2,
                   ObjectPtr<IOpenGLBaseTexture> DeviceTexture3, TexCoordXForm &texxform3, const Color &color3);

    void QRP_ASM_Triangle (int x0, int y0, int x1, int y1, int x2, int y2, Color c0);
    void QRP_ASM_Triangle (int x0, int y0, int x1, int y1, int x2, int y2, Color c0, Color c1, Color c2);
    void QRP_ASM_Line (int x0, int y0, int x1, int y1, Color c0);
    void QRP_ASM_Line (int x0, int y0, int x1, int y1, Color c0, Color c1);
    void QRP_ASM_QuadWireframe (int x0, int y0, int width, int height, Color c0, Color c1, Color c2, Color c3);

    void QRP_ASM_Power  (int x, int y, int width, int height, ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform, const Color &c0, Vector4 exponent);
    void QRP_ASM_AlphaReplicate  (int x, int y, int width, int height, ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform, const Color &c0);
    void QRP_ASM_HorizontalGauss (int x, int y, int width, int height, ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform, const Color &c0, float sigma = 1.0f);
    void QRP_ASM_VerticalGauss   (int x, int y, int width, int height, ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform, const Color &c0, float sigma = 1.0f);
    void QRP_ASM_ColorMatrix     (int x, int y, int width, int height, ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform, const Color &c0, Matrix4 color_matrix, Vector4 offset);

    //! Blur texture
    /*!
        @param device_texture The texture that is to be blurred.
        @param texxform       Texture transformation of device_texture.
        @param x              Position of the source texture in result buffer.
        @param y              Position of the source texture in result buffer.
        @param buffer_width   Width of result texture.
        @param buffer_height  Height of result texture.
        @param sigma          
    */
    ObjectPtr<IOpenGLBaseTexture> QRP_ASM_GetBlurTexture (
      int x, int y, int buffer_width, int buffer_height,
      ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform, const Color& color, float sigma = 1.0f, int num_pass = 1);

    //! Replicate the alpha channel in all components of the texture.
    /*!
        @param device_texture Source texture.
        @param texxform Texture transformation parameter.
        @param color Modulation color.
        @return Texture with all components set to the alpha value of the source.
    */
    ObjectPtr<IOpenGLBaseTexture> QRP_ASM_GetAlphaTexture (
      ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform, const Color& color);

    //! Color matrix filter.
    /*!
        @param device_texture Source texture.
        @param texxform Texture transformation parameter.
        @param c Modulation color.
        @param color_matrix 4x4 section of the color matrix filter.
        @param offset Last column of the color matrix filter.
        @return Texture resulting from the processing of the source through a color matrix.
    */
    ObjectPtr<IOpenGLBaseTexture> QRP_ASM_GetColorMatrixTexture (
      ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform,
      const Color& c, Matrix4 color_matrix, Vector4 offset);

    //! Texture components raised to a power.
    /*!
        Each component of the texture is raised to a power provided in the exponent parameter.
        @param device_texture Source texture.
        @param texxform Texture transformation parameter.
        @param c Modulation color.
        @param exponent Power values for each component.
        @return A texture where the component of the source texture have been raised to a power.
    */
    ObjectPtr<IOpenGLBaseTexture> QRP_ASM_GetPower (
      ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform, const Color& c0, const Vector4 &exponent);

    ObjectPtr<IOpenGLBaseTexture> QRP_ASM_GetLQBlur (
      int x, int y, int buffer_width, int buffer_height, ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform,
      const Color& c0);

    ObjectPtr<IOpenGLBaseTexture> QRP_ASM_GetHQBlur (
      int x, int y, int buffer_width, int buffer_height, ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform,
      const Color& c0, float sigma = 1.0f, int num_pass = 1);

    void QRP_ASM_DisturbedTexture (
      int x, int y, int width, int height,
      ObjectPtr<IOpenGLBaseTexture> distorsion_texture, TexCoordXForm &texxform0, const Color& c0,
      ObjectPtr<IOpenGLBaseTexture> src_device_texture, TexCoordXForm &texxform1, const Color& c1);

    //! Pixel blocks.
    /*!
        @param device_texture Source texture.
        @param texxform Texture transformation parameter.
        @param color Modulation color.
        @param pixel_size Size of pixel blocks.
        @return A texture pixelated version of the source texture.
    */
    ObjectPtr<IOpenGLBaseTexture>  QRP_ASM_GetPixelBlocks (
		  ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform0, const Color& color, int pixel_size);

    void QRP_ASM_GetCopyTexture (
      int width, int height,
      ObjectPtr<IOpenGLBaseTexture>& dst_device_texture,
      ObjectPtr<IOpenGLBaseTexture>& src_device_texture,
      TexCoordXForm &texxform0, const Color& c0);

    // GLSL

    void QRP_GLSL_1Tex (int x, int y, int width, int height, ObjectPtr<IOpenGLBaseTexture> DeviceTexture, TexCoordXForm &texxform, const Color &c0);
    void QRP_GLSL_Pixelate (int x, int y, int width, int height, ObjectPtr<IOpenGLBaseTexture> DeviceTexture, TexCoordXForm &texxform, const Color &c0, int pixel_size);

    void QRP_GLSL_Color (int x, int y, int width, int height, const Color &c0);
    void QRP_GLSL_Color (int x, int y, int width, int height, const Color &c0, const Color &c1, const Color &c2, const Color &c3);
    void QRP_GLSL_ColorModTexAlpha (int x, int y, int width, int height,
                                    ObjectPtr<IOpenGLBaseTexture> DeviceTexture, TexCoordXForm &texxform0, const Color &color);

    void QRP_GLSL_2Tex (int x, int y, int width, int height,
                        ObjectPtr<IOpenGLBaseTexture> DeviceTexture0, TexCoordXForm &texxform0, const Color &color0,
                        ObjectPtr<IOpenGLBaseTexture> DeviceTexture1, TexCoordXForm &texxform1, const Color &color1);

    void QRP_GLSL_2TexMod (int x, int y, int width, int height,
      ObjectPtr<IOpenGLBaseTexture> DeviceTexture0, TexCoordXForm &texxform0, const Color &color0,
      ObjectPtr<IOpenGLBaseTexture> DeviceTexture1, TexCoordXForm &texxform1, const Color &color1);


    void QRP_GLSL_4Tex (int x, int y, int width, int height,
                        ObjectPtr<IOpenGLBaseTexture> DeviceTexture0, TexCoordXForm &texxform0, const Color &color0,
                        ObjectPtr<IOpenGLBaseTexture> DeviceTexture1, TexCoordXForm &texxform1, const Color &color1,
                        ObjectPtr<IOpenGLBaseTexture> DeviceTexture2, TexCoordXForm &texxform2, const Color &color2,
                        ObjectPtr<IOpenGLBaseTexture> DeviceTexture3, TexCoordXForm &texxform3, const Color &color3);

    void QRP_GLSL_Triangle      (int x0, int y0, int x1, int y1, int x2, int y2, Color c0);
    void QRP_GLSL_Triangle      (int x0, int y0, int x1, int y1, int x2, int y2, Color c0, Color c1, Color c2);
    void QRP_GLSL_Line          (int x0, int y0, int x1, int y1, Color c0);
    void QRP_GLSL_Line          (int x0, int y0, int x1, int y1, Color c0, Color c1);
    void QRP_GLSL_QuadWireframe (int x0, int y0, int width, int height, Color c0, Color c1, Color c2, Color c3);

    void QRP_GLSL_Power           (int x, int y, int width, int height, ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform, const Color &c0, Vector4 exponent);
    void QRP_GLSL_AlphaReplicate  (int x, int y, int width, int height, ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform, const Color &c0);
    void QRP_GLSL_HorizontalGauss (int x, int y, int width, int height, ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform, const Color &c0, float sigma = 1.0f);
    void QRP_GLSL_VerticalGauss   (int x, int y, int width, int height, ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform, const Color &c0, float sigma = 1.0f);
    void QRP_GLSL_HorizontalHQGauss (int x, int y, int width, int height, ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform, const Color &c0, float sigma = 1.0f);
    void QRP_GLSL_VerticalHQGauss   (int x, int y, int width, int height, ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform, const Color &c0, float sigma = 1.0f);
    void QRP_GLSL_ColorMatrix     (int x, int y, int width, int height, ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform, const Color &c0, Matrix4 color_matrix, Vector4 offset);

    /*!
        @param device_texture The texture that is to be blurred.
        @param texxform       Texture transformation of device_texture.
        @param x              Position of the source texture in result buffer.
        @param y              Position of the source texture in result buffer.
        @param buffer_width   Width of result texture.
        @param buffer_height  Height of result texture.
    */
    ObjectPtr<IOpenGLBaseTexture> QRP_GLSL_GetBlurTexture (
      int x, int y,
      int buffer_width, int buffer_height,
      ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform,
      const Color& c0,
      float sigma = 1.0f, int num_pass = 1);

    ObjectPtr<IOpenGLBaseTexture> QRP_GLSL_GetAlphaTexture (
      ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform,
      const Color& c0);

    ObjectPtr<IOpenGLBaseTexture> QRP_GLSL_GetColorMatrixTexture (
      ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform,
      const Color& c0,
      Matrix4 color_matrix, Vector4 offset);

    ObjectPtr<IOpenGLBaseTexture> QRP_GLSL_GetPower (
      ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform, const Color& c0, const Vector4 &exponent);

    ObjectPtr<IOpenGLBaseTexture> QRP_GLSL_GetLQBlur (
      int x, int y,
      int buffer_width, int buffer_height,
      ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform,
      const Color& c0);

    ObjectPtr<IOpenGLBaseTexture> QRP_GLSL_GetHQBlur (
      int x, int y,
      int buffer_width, int buffer_height,
      ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform,
      const Color& c0, float sigma = 1.0f, int num_pass = 1);

    void QRP_GLSL_DisturbedTexture (
      int x, int y, int width, int height,
      ObjectPtr<IOpenGLBaseTexture> distorsion_texture, TexCoordXForm &texxform0, const Color& c0,
      ObjectPtr<IOpenGLBaseTexture> src_device_texture, TexCoordXForm &texxform1, const Color& c1);

    ObjectPtr<IOpenGLBaseTexture> QRP_GLSL_GetDisturbedTexture(
      int x, int y, int width, int height,
      ObjectPtr<IOpenGLBaseTexture> distorsion_texture, TexCoordXForm &texxform0, const Color& c0,
      ObjectPtr<IOpenGLBaseTexture> src_device_texture, TexCoordXForm &texxform1, const Color& c1);

    //! Pixel blocks.
    /*!
        @param device_texture Source texture.
        @param texxform Texture transformation parameter.
        @param color Modulation color.
        @param pixel_size Size of pixel blocks.
        @return A texture pixelated version of the source texture.
    */
	  ObjectPtr<IOpenGLBaseTexture> QRP_GLSL_GetPixelBlocks (
		  ObjectPtr<IOpenGLBaseTexture> device_texture, TexCoordXForm &texxform0, const Color& c0, int pixel_size);

    void QRP_GLSL_GetCopyTexture(
      int width, int height,
      ObjectPtr<IOpenGLBaseTexture>& dst_device_texture,
      ObjectPtr<IOpenGLBaseTexture>& src_device_texture,
      TexCoordXForm &texxform0, const Color& c0);

    //////////////////////
    // DRAW CLIPPING    //
    //////////////////////

    mutable std::vector<Rect> ClippingRect;
    void PushClippingRectangle (Rect rect);
    void PopClippingRectangle ();
    void EmptyClippingRegion ();
    //! Set the clipping according to the clipping rectangle stack.
    void ApplyClippingRectangle ();
    
    void SetGlobalClippingRectangle (Rect rect);
    void DisableGlobalClippingRectangle ();

    //! Bypass the clipping rectangle stack and set a different clipping rectangle region.
    /*!
        You may restore the clipping rectangle stack with ApplyClippingRectangle.
    */
    void SetClippingRectangle (const Rect &rect);

    //! Bypass the clipping rectangle stack and set a different clipping rectangle region.
    void SetOpenGLClippingRectangle (int x, int y, unsigned int width, unsigned int height);

    Rect GetClippingRegion () const;
    int GetNumberOfClippingRegions () const;
    
    void AddClipOffset (int x, int y);  //!< Deprecated. Use PushClipOffset.
    void PushClipOffset (int x, int y);
    void PopClipOffset ();

    void ClearAreaColorDepthStencil (int x, int y, int width, int height, Color clearcolor, float cleardepth, int clearstencil);
    void ClearAreaColor (int x, int y, int width, int height, Color clearcolor);
    void ClearAreaDepthStencil (int x, int y, int width, int height, float cleardepth, int clearstencil);

    void Set3DView (int w, int h);

    //! Push a screen aligned 2D matrix.
    void Push2DWindow (int w, int h);
    void Pop2DWindow ();
    void Push2DModelViewMatrix (Matrix4 mat);
    Matrix4 Pop2DModelViewMatrix ();
    void Clear2DModelViewMatrix ();
    std::list<Matrix4> m_2DModelViewMatricesStack;

    void SetViewport (int x, int y, int w, int h);
    Rect GetViewportRect () const;
    int  GetViewportWidth () const;
    int  GetViewportHeight () const;
    int  GetViewportX () const;
    int  GetViewportY () const;

    void  GetViewportSize (int &viewport_width, int &viewport_height) const;

    void SetScissor (int x, int y, int w, int h);

    /*!
        When setting a matrix to translate the widgets, the scissor region is not translated accordingly.
        This function provides a mean to offset the scissor region. It is useful when translating a widget during and overlay drawing.
        See an example in the code of NUX-01.
        @param x Clipping area offset.
        @param y Clipping area offset.
    */
    void SetScissorOffset (int x, int y);

    Rect GetScissorRect();
    void EnableScissoring (bool b);


//     void LoadPainterImages();
//     const PainterImage* GetImage(UXStyleImageRef style);
//     Rect GetImageGeometry(UXStyleImageRef style);
//     std::list<PainterImage*> m_PainterImageList;

    int RenderColorText (ObjectPtr<FontTexture> Font, int x, int y, const NString &Str,
                         const Color &TextColor,
                         bool WriteAlphaChannel,
                         int NumCharacter);

    int RenderColorTextLineStatic (ObjectPtr<FontTexture> Font, const PageBBox &pageSize, const NString &Str,
                                   const Color &TextColor,
                                   bool WriteAlphaChannel,
                                   TextAlignment alignment);

    int RenderColorTextLineEdit (ObjectPtr<FontTexture> Font, const PageBBox &pageSize, const NString &Str,
                                 const Color &TextColor,
                                 bool WriteAlphaChannel,
                                 const Color &SelectedTextColor,
                                 const Color &SelectedTextBackgroundColor,
                                 const Color &TextBlinkColor,
                                 const Color &CursorColor,
                                 bool ShowCursor, unsigned int CursorPosition,
                                 int offset = 0, int selection_start = 0, int selection_end = 0);

    ObjectPtr <IOpenGLBaseTexture> CreateTextureFromBackBuffer (int x, int y, int width, int height);

    //Statistics
    void ResetStats();

    /*!
        Cache a resource if it has previously been cached. If the resource does not contain valid data
        then the returned value is not valid. Check that the returned hardware resource is valid by calling ObjectPtr<CachedResourceData>.IsValid().
        @param Resource The resource to cache.
        @return A hardware resource.
    */
    ObjectPtr<CachedResourceData> CacheResource (ResourceData *Resource);

    /*!
        Update a resource if it has previously been cached.
        @param Resource The resource to cache.
    */
    void UpdateResource (ResourceData *Resource);

    bool FlushCachedResourceData (ResourceData *Resource);
    bool IsResourceCached (ResourceData *Resource);
    NResourceCache ResourceCache;

    Matrix4 GetProjectionMatrix ();
    //! Return the transpose version of GetProjectionMatrix ();
    Matrix4 GetOpenGLProjectionMatrix ();
    
    void SetProjectionMatrix (const Matrix4 &matrix);
    
    //! Set orthographic projection matrix.
    /*!
        The default projection matrix used by nux.

        @param viewport_width Viewport width.
        @param viewport_height Viewport height.
    */
    void SetOrthographicProjectionMatrix (int viewport_width, int viewport_height);

    //! Reset the projection matrix to identity.
    void ResetProjectionMatrix ();

    Matrix4 GetModelViewMatrix ();
    //! Return the transpose version of GetModelViewMatrix ();
    Matrix4 GetOpenGLModelViewMatrix ();

    Matrix4 GetModelViewProjectionMatrix ();
    Matrix4 GetOpenGLModelViewProjectionMatrix ();

    GpuRenderStates &GetRenderStates()
    {
      return *_graphics_display.m_DeviceFactory->_gpu_render_states;
    }
    void ResetRenderStates()
    {
      _graphics_display.m_DeviceFactory->_gpu_render_states->ResetStateChangeToDefault();
    }
    void VerifyRenderStates()
    {
      _graphics_display.m_DeviceFactory->_gpu_render_states->CheckStateChange();
    }

    ObjectPtr<FontTexture> GetFont();
    ObjectPtr<FontTexture> GetBoldFont();

    //! Return True is GraphicsEngine is using the GLSL shader code path.
    /*!
        @return True is the system is using the GLSL shader code path.
    */
    bool UsingGLSLCodePath ();

    //! Return True is GraphicsEngine is using the ARB program code path.
    /*!
        @return True is the system is using the ARB program code path.
    */
    bool UsingARBProgramCodePath ();

    //! Push a model view matrix on the stack.
    void PushModelViewMatrix (const Matrix4 &matrix);

    //! Push an Identity model view matrix on the stack.
    void PushIdentityModelViewMatrix ();

    //! Push a 2D Translation model view matrix.
    /*!
        This is used by Nux to harmonize quads and lines pixel rendering in OpenGL.
    */
    void Push2DTranslationModelViewMatrix (float tx, float ty, float tz);

    //! Pop a model view matrix off the stack.
    /*!
        Return True is a matrix was successfully popped. False if there was no matrix to pop.
    */
    bool PopModelViewMatrix ();

    //! Reset the model view matrix to identity.
    void ResetModelViewMatrixStack ();

    //! Bypass the model view matrix stack and set a custom matrix.
    /*!
        You may restore the view matrix stack by calling ApplyModelViewMatrix.
    */
    void SetModelViewMatrix (const Matrix4 &matrix);

    //! Set the model view matrix according to the model view matrix stack.
    void ApplyModelViewMatrix ();

    //! Transform a rectangle with the model view matrix.
    /*!
        This transformation is only good as long as the model view matrix only contains 2D translations.
        The output rectangle width and height are the same as the input rectangle.

        @param rect The rectangle to transform.
    */
    Rect ModelViewXFormRect (const Rect& rect);

    //! Return the depth of the model view matrix stack.
    /*!
        @return The depth of the model view matrix stack.
    */
    int ModelViewStackDepth ();



    //! Push a projection matrix on the stack.
    void PushProjectionMatrix (const Matrix4 &matrix);

    //! Pop a projection matrix off the stack.
    /*!
        Return True is a matrix was successfully popped. False if there was no matrix to pop.
    */
    bool PopProjectionMatrix ();

    //! Return the depth of the projection matrix stack.
    /*!
        @return The depth of the projection matrix stack.
    */
    int ProjectionStackDepth ();





    //! Push a raster operation configuration setting on the stack.
    void PushPorterDuffBlend (const PorterDuffOperator &porter_duff_op);

    //! Push a state that disables the blending.
    void PushDisableBlend ();

    //! Push a custom blend state.
    /*!
        @param src_blend OpenGL source blending mode.
        @param dst_blend OpenGL destination blending mode.
    */
    void PushBlend (unsigned int src_blend, unsigned int dst_blend);

    //! Pop a raster operation configuration setting off the stack.
    /*!
        Return True is a matrix was successfully popped. False if there was no matrix to pop.
    */
    bool PopBlend ();

    //! Return the depth of the raster operation stack.
    /*!
        @return The depth of the raster operation stack.
    */
    int BlendStackDepth ();

  private:

    ObjectPtr<FontTexture> _normal_font;    //!< The normal font renderer
    ObjectPtr<FontTexture> _bold_font;      //!< The bold font renderer

    void InitShaders();

    //! Helper function to compute a Gaussian filter weights
    void GaussianWeights (float **weights, float sigma, unsigned int num_tap);

    //! Helper function to set an fbo
    void SetFrameBufferHelper(
      ObjectPtr<IOpenGLFrameBufferObject>& fbo,
      ObjectPtr<IOpenGLBaseTexture>& colorbuffer,
      ObjectPtr<IOpenGLBaseTexture>& depthbuffer,
      int width, int height);

#ifndef NUX_OPENGLES_20
    void InitAsmColorShader();
    //! Render polygons with a single color.
    ObjectPtr<IOpenGLAsmShaderProgram> m_AsmColor;

    void InitAsmTextureShader();
    //! Render polygons with a texture modulated by a color.
    ObjectPtr<IOpenGLAsmShaderProgram> m_AsmTextureModColor;
    //! Same as m_AsmTextureModColor for rectangle textures.
    ObjectPtr<IOpenGLAsmShaderProgram> m_AsmTextureRectModColor;

    void InitAsmPixelateShader();
    //! Render a pixelated texture over a polygon.
    ObjectPtr<IOpenGLAsmShaderProgram> m_AsmPixelate;
    //! Render a pixelated texture rectangle over a polygon.
    ObjectPtr<IOpenGLAsmShaderProgram> m_AsmPixelateRect;

    void InitAsmColorModTexMaskAlpha();
    //! Render polygons with a color masked by the alpha provided sampling a texture.
    //! Requires: Enable GPU Alpha Blending
    ObjectPtr<IOpenGLAsmShaderProgram> m_AsmColorModTexMaskAlpha;
    ObjectPtr<IOpenGLAsmShaderProgram> m_AsmColorModTexRectMaskAlpha;

    void InitAsm2TextureAdd();
    //! Render polygons with 2 textures, each modulated by a color, and added together.
    ObjectPtr<IOpenGLAsmShaderProgram> m_Asm2TextureAdd;
    ObjectPtr<IOpenGLAsmShaderProgram> m_Asm2TextureRectAdd;

    void InitAsm2TextureMod();
    //! Render polygons with 2 textures, each modulated by a color, and then multiplied together.
    ObjectPtr<IOpenGLAsmShaderProgram> m_Asm2TextureMod;
    ObjectPtr<IOpenGLAsmShaderProgram> m_Asm2TextureRectMod;

    void InitAsm4TextureAdd();
    //! Render polygons with 4 textures, each modulated by a color, and added together.
    ObjectPtr<IOpenGLAsmShaderProgram> m_Asm4TextureAdd;
    ObjectPtr<IOpenGLAsmShaderProgram> m_Asm4TextureRectAdd;

    void InitAsmPower ();
    //! Raise a texture component to a power.
    /*!
        result = color0 * (tex0)^(exponent) = (tex0.r^exponent.r, tex0.g^exponent.g, tex0.b^exponent.b, tex0.a^exponent.a);
    */
    ObjectPtr<IOpenGLAsmShaderProgram> _asm_tex_component_exponentiation_prog;
    ObjectPtr<IOpenGLAsmShaderProgram> _asm_texrect_component_exponentiation_prog;

    void InitAsmAlphaReplicate ();
    //! Replicate the alpha component into r, g and b.
    /*!
        result = color0 * (tex0.a, tex0.a, tex0.a, tex0.a);
    */
    ObjectPtr<IOpenGLAsmShaderProgram> _asm_tex_alpha_replicate_prog;
    ObjectPtr<IOpenGLAsmShaderProgram> _asm_texrect_alpha_replicate_prog;

    void InitAsmSeparableGaussFilter ();
    //! Gauss horizontal filter.
    ObjectPtr<IOpenGLAsmShaderProgram> _asm_tex_separable_gauss_filter_prog;
    ObjectPtr<IOpenGLAsmShaderProgram> _asm_texrect_separable_gauss_filter_prog;

//     void InitAsmSeparableGaussFilter2 ();
//     //! Gauss horizontal filter.
//     ObjectPtr<IOpenGLAsmShaderProgram> _asm_tex_separable_gauss_filter_prog2;
//     ObjectPtr<IOpenGLAsmShaderProgram> _asm_texrect_separable_gauss_filter_prog2;

    void InitAsmColorMatrixFilter ();
    //! Color matrix filter.
    ObjectPtr<IOpenGLAsmShaderProgram> _asm_tex_color_matrix_filter_prog;
    ObjectPtr<IOpenGLAsmShaderProgram> _asm_texrect_color_matrix_filter_prog;

    void InitAsm2TextureDepRead ();
    ObjectPtr<IOpenGLAsmShaderProgram> m_ASM2TextureDepRead;
    ObjectPtr<IOpenGLAsmShaderProgram> m_ASM2TextureRectDepRead;

    void InitAsmBlendModes();

    ObjectPtr<IOpenGLAsmShaderProgram> m_AsmPSBNormal;
    ObjectPtr<IOpenGLAsmShaderProgram> m_AsmPSBLighten;
    ObjectPtr<IOpenGLAsmShaderProgram> m_AsmPSBDarken;
    ObjectPtr<IOpenGLAsmShaderProgram> m_AsmPSBMultiply;
#endif

    void InitSlColorShader();
    //! Render polygons with a single color.
    ObjectPtr<IOpenGLShaderProgram> m_SlColor;

    void InitSlTextureShader();
    //! Render polygons with a texture modulated by a color.
    ObjectPtr<IOpenGLShaderProgram> m_SlTextureModColor;

    void InitSlPixelateShader();
    //! Render a pixelated texture over a polygon.
    ObjectPtr<IOpenGLShaderProgram> m_SLPixelate;

    void InitSlColorModTexMaskAlpha();
    //! Render polygons with a color masked by the alpha provided sampling a texture.
    //! Requires: Enable GPU Alpha Blending
    ObjectPtr<IOpenGLShaderProgram> m_SlColorModTexMaskAlpha;
    ObjectPtr<IOpenGLShaderProgram> m_SlColorModTexRectMaskAlpha;

    void InitSl2TextureAdd();
    //! Render polygons with 2 textures, each modulated by a color, and added together.
    //! result = (tex0*color0)+(tex1*color1)
    ObjectPtr<IOpenGLShaderProgram> m_Sl2TextureAdd;

    void InitSl2TextureDepRead();
    //! Render a quad with 2 textures. One texture sample is use to offset the coordinates of the second texture read.
    //! result = (tex1 (coord1.xy + tex0.xy)*color1)
    ObjectPtr<IOpenGLShaderProgram> m_Sl2TextureDepRead;


    void InitSl2TextureMod();
    //! Render polygons with 2 textures, each modulated by a color, and then multiplied together.
    //! result = (tex0*color0)*(tex1*color1)
    ObjectPtr<IOpenGLShaderProgram> m_Sl2TextureMod;

    void InitSl4TextureAdd();
    //! Render polygons with 4 textures, each modulated by a color, and added together.
    ObjectPtr<IOpenGLShaderProgram> m_Sl4TextureAdd;

    void InitSLPower ();
    //! Raise a texture component to a power.
    /*!
        result = color0 * (tex0)^(exponent) = (tex0.r^exponent.r, tex0.g^exponent.g, tex0.b^exponent.b, tex0.a^exponent.a);
    */
    ObjectPtr<IOpenGLShaderProgram> _component_exponentiation_prog;

    void InitSLAlphaReplicate ();
    //! Replicate the alpha component into r, g and b.
    /*!
        result = color0 * (tex0.a, tex0.a, tex0.a, tex0.a);
    */
    ObjectPtr<IOpenGLShaderProgram> _alpha_replicate_prog;

    void InitSLHorizontalGaussFilter ();
    //! Gauss horizontal filter.
    ObjectPtr<IOpenGLShaderProgram> _horizontal_gauss_filter_prog;
    
    void InitSLVerticalGaussFilter ();
    //! Gauss vertical filter.
    ObjectPtr<IOpenGLShaderProgram> _vertical_gauss_filter_prog;
    

    void InitSLHorizontalHQGaussFilter (int sigma);
    //! Gauss horizontal filter.
    ObjectPtr<IOpenGLShaderProgram> _horizontal_hq_gauss_filter_prog[NUX_MAX_GAUSSIAN_SIGMA];

    void InitSLVerticalHQGaussFilter (int sigma);
    //! Gauss vertical filter.
    ObjectPtr<IOpenGLShaderProgram> _vertical_hq_gauss_filter_prog[NUX_MAX_GAUSSIAN_SIGMA];



    void InitSLColorMatrixFilter ();
    //! Color matrix filter.
    ObjectPtr<IOpenGLShaderProgram> _color_matrix_filter_prog;

    void InitSlBlendModes();

    void InitBlendModeNormal();
    void InitBlendModeLighten();
    void InitBlendModeDarken();

    ObjectPtr<IOpenGLShaderProgram> _shader_layer_blend_normal;
    ObjectPtr<IOpenGLShaderProgram> _shader_layer_blend_lighten;
    ObjectPtr<IOpenGLShaderProgram> _shader_layer_blend_darken;
    ObjectPtr<IOpenGLShaderProgram> _shader_layer_blend_multiply;


    //! Test the gpu features and set variables such as \e _use_glsl_shaders.
    void EvaluateGpuCaps ();

    bool _use_glsl_shaders; //!< True if the system is using the glsl code path.

    ObjectPtr<IOpenGLFrameBufferObject> _offscreen_fbo;
    ObjectPtr<IOpenGLBaseTexture> _offscreen_color_rt0;
    ObjectPtr<IOpenGLBaseTexture> _offscreen_depth_rt0;
    ObjectPtr<IOpenGLBaseTexture> _offscreen_color_rt1;
    ObjectPtr<IOpenGLBaseTexture> _offscreen_depth_rt1;
    ObjectPtr<IOpenGLBaseTexture> _offscreen_color_rt2;
    ObjectPtr<IOpenGLBaseTexture> _offscreen_depth_rt2;
    ObjectPtr<IOpenGLBaseTexture> _offscreen_color_rt3;
    ObjectPtr<IOpenGLBaseTexture> _offscreen_depth_rt3;

    Matrix4 _projection_matrix;
    Matrix4 _model_view_matrix;
    
    std::list<Matrix4>   _model_view_stack;
    std::list<BlendOperator> _blend_stack;

    //! The system GraphicsDisplay object
    GraphicsDisplay &_graphics_display;

    Rect _viewport;
    Rect _scissor;
    int _clip_offset_x;
    int _clip_offset_y;
    Rect _clipping_rect;


    //! Set to \i true to enable the global clipping rectangle.
    /*!
        When rendering directly to the OpenGL back buffer, we may enable a global clipping
        rectangle. This is mostly useful in embedded mode.
        \sa _global_clipping_rect;
    */
    bool _global_clipping_enabled;

    //! Define a global clipping rectangle.
    Rect _global_clipping_rect;

    std::list<Point> _clip_offset_stack;

    FontRenderer *_font_renderer;

    //Statistics
    mutable long m_quad_stats;
    mutable long m_quad_tex_stats;
    mutable long m_triangle_stats;
    mutable long m_triangle_tex_stats;
    mutable long m_line_stats;

    GraphicsEngine (const GraphicsEngine &);
    // Does not make sense for a singleton. This is a self assignment.
    GraphicsEngine& operator = (const GraphicsEngine &);
    // Declare operator address-of as private
    GraphicsEngine* operator & ();
  };

}

#endif // OPENGLENGINE_H