/
GLRenderer.java
3323 lines (2933 loc) · 125 KB
/
GLRenderer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2009-2021 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.renderer.opengl;
import com.jme3.material.RenderState;
import com.jme3.material.RenderState.BlendFunc;
import com.jme3.material.RenderState.BlendMode;
import com.jme3.material.RenderState.StencilOperation;
import com.jme3.material.RenderState.TestFunction;
import com.jme3.math.*;
import com.jme3.opencl.OpenCLObjectManager;
import com.jme3.renderer.*;
import com.jme3.scene.Mesh;
import com.jme3.scene.Mesh.Mode;
import com.jme3.scene.VertexBuffer;
import com.jme3.scene.VertexBuffer.Format;
import com.jme3.scene.VertexBuffer.Type;
import com.jme3.scene.VertexBuffer.Usage;
import com.jme3.shader.*;
import com.jme3.shader.Shader.ShaderSource;
import com.jme3.shader.Shader.ShaderType;
import com.jme3.texture.FrameBuffer;
import com.jme3.texture.FrameBuffer.RenderBuffer;
import com.jme3.texture.Image;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture2D;
import com.jme3.texture.Texture.ShadowCompareMode;
import com.jme3.texture.Texture.WrapAxis;
import com.jme3.texture.image.LastTextureState;
import com.jme3.util.BufferUtils;
import com.jme3.util.ListMap;
import com.jme3.util.MipMapGenerator;
import com.jme3.util.NativeObjectManager;
import jme3tools.shader.ShaderDebug;
import java.lang.ref.WeakReference;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class GLRenderer implements Renderer {
private static final Logger logger = Logger.getLogger(GLRenderer.class.getName());
private static final boolean VALIDATE_SHADER = false;
private static final Pattern GLVERSION_PATTERN = Pattern.compile(".*?(\\d+)\\.(\\d+).*");
private final ByteBuffer nameBuf = BufferUtils.createByteBuffer(250);
private final StringBuilder stringBuf = new StringBuilder(250);
private final IntBuffer intBuf1 = BufferUtils.createIntBuffer(1);
private final IntBuffer intBuf16 = BufferUtils.createIntBuffer(16);
private final RenderContext context = new RenderContext();
private final NativeObjectManager objManager = new NativeObjectManager();
private final EnumSet<Caps> caps = EnumSet.noneOf(Caps.class);
private final EnumMap<Limits, Integer> limits = new EnumMap<>(Limits.class);
private FrameBuffer mainFbOverride = null;
private int defaultFBO = 0;
private final Statistics statistics = new Statistics();
private int vpX, vpY, vpW, vpH;
private int clipX, clipY, clipW, clipH;
private int defaultAnisotropicFilter = 1;
private boolean linearizeSrgbImages;
private HashSet<String> extensions;
private final GL gl;
private final GL2 gl2;
private final GL3 gl3;
private final GL4 gl4;
private final GLExt glext;
private final GLFbo glfbo;
private final TextureUtil texUtil;
public GLRenderer(GL gl, GLExt glext, GLFbo glfbo) {
this.gl = gl;
this.gl2 = gl instanceof GL2 ? (GL2)gl : null;
this.gl3 = gl instanceof GL3 ? (GL3)gl : null;
this.gl4 = gl instanceof GL4 ? (GL4)gl : null;
this.glfbo = glfbo;
this.glext = glext;
this.texUtil = new TextureUtil(gl, gl2, glext);
}
@Override
public Statistics getStatistics() {
return statistics;
}
@Override
public EnumSet<Caps> getCaps() {
return caps;
}
// Not making public yet ...
@Override
public EnumMap<Limits, Integer> getLimits() {
return limits;
}
private HashSet<String> loadExtensions() {
HashSet<String> extensionSet = new HashSet<>(64);
if (caps.contains(Caps.OpenGL30)) {
// If OpenGL3+ is available, use the non-deprecated way
// of getting supported extensions.
gl3.glGetInteger(GL3.GL_NUM_EXTENSIONS, intBuf16);
int extensionCount = intBuf16.get(0);
for (int i = 0; i < extensionCount; i++) {
String extension = gl3.glGetString(GL.GL_EXTENSIONS, i);
extensionSet.add(extension);
}
} else {
extensionSet.addAll(Arrays.asList(gl.glGetString(GL.GL_EXTENSIONS).split(" ")));
}
return extensionSet;
}
public static int extractVersion(String version) {
Matcher m = GLVERSION_PATTERN.matcher(version);
if (m.matches()) {
int major = Integer.parseInt(m.group(1));
int minor = Integer.parseInt(m.group(2));
if (minor >= 10 && minor % 10 == 0) {
// some versions can look like "1.30" instead of "1.3".
// make sure to correct for this
minor /= 10;
}
return major * 100 + minor * 10;
} else {
return -1;
}
}
private boolean hasExtension(String extensionName) {
return extensions.contains(extensionName);
}
private void loadCapabilitiesES() {
int oglVer = extractVersion(gl.glGetString(GL.GL_VERSION));
caps.add(Caps.GLSL100);
caps.add(Caps.OpenGLES20);
caps.add(Caps.Multisample);
if (oglVer >= 300) {
caps.add(Caps.OpenGLES30);
caps.add(Caps.GLSL300);
// Instancing is core in GLES300
caps.add(Caps.MeshInstancing);
}
if (oglVer >= 310) {
caps.add(Caps.OpenGLES31);
caps.add(Caps.GLSL310);
}
if (oglVer >= 320) {
caps.add(Caps.OpenGLES32);
caps.add(Caps.GLSL320);
caps.add(Caps.GeometryShader);
caps.add(Caps.TesselationShader);
}
// Important: Do not add OpenGL20 - that's the desktop capability!
}
private void loadCapabilitiesGL2() {
int oglVer = extractVersion(gl.glGetString(GL.GL_VERSION));
if (oglVer >= 200) {
caps.add(Caps.OpenGL20);
if (oglVer >= 210) {
caps.add(Caps.OpenGL21);
}
if (oglVer >= 300) {
caps.add(Caps.OpenGL30);
}
if (oglVer >= 310) {
caps.add(Caps.OpenGL31);
}
if (oglVer >= 320) {
caps.add(Caps.OpenGL32);
}
if (oglVer >= 330) {
caps.add(Caps.OpenGL33);
caps.add(Caps.GeometryShader);
}
if (oglVer >= 400) {
caps.add(Caps.OpenGL40);
caps.add(Caps.TesselationShader);
}
if (oglVer >= 410) {
caps.add(Caps.OpenGL41);
}
if (oglVer >= 420) {
caps.add(Caps.OpenGL42);
}
if (oglVer >= 430) {
caps.add(Caps.OpenGL43);
}
if (oglVer >= 440) {
caps.add(Caps.OpenGL44);
}
if (oglVer >= 450) {
caps.add(Caps.OpenGL45);
}
}
int glslVer = extractVersion(gl.glGetString(GL.GL_SHADING_LANGUAGE_VERSION));
switch (glslVer) {
default:
if (glslVer < 400) {
break;
}
// so that future OpenGL revisions won't break jme3
// fall through intentional
case 450:
caps.add(Caps.GLSL450);
case 440:
caps.add(Caps.GLSL440);
case 430:
caps.add(Caps.GLSL430);
case 420:
caps.add(Caps.GLSL420);
case 410:
caps.add(Caps.GLSL410);
case 400:
caps.add(Caps.GLSL400);
case 330:
caps.add(Caps.GLSL330);
case 150:
caps.add(Caps.GLSL150);
case 140:
caps.add(Caps.GLSL140);
case 130:
caps.add(Caps.GLSL130);
case 120:
caps.add(Caps.GLSL120);
case 110:
caps.add(Caps.GLSL110);
case 100:
caps.add(Caps.GLSL100);
break;
}
// Workaround, always assume we support GLSL100 & GLSL110
// Supporting OpenGL 2.0 means supporting GLSL 1.10.
caps.add(Caps.GLSL110);
caps.add(Caps.GLSL100);
// Fix issue in TestRenderToMemory when GL.GL_FRONT is the main
// buffer being used.
context.initialDrawBuf = getInteger(GL2.GL_DRAW_BUFFER);
context.initialReadBuf = getInteger(GL2.GL_READ_BUFFER);
// XXX: This has to be GL.GL_BACK for canvas on Mac
// Since initialDrawBuf is GL.GL_FRONT for pbuffer, gotta
// change this value later on ...
// initialDrawBuf = GL.GL_BACK;
// initialReadBuf = GL.GL_BACK;
}
private void loadCapabilitiesCommon() {
extensions = loadExtensions();
limits.put(Limits.VertexTextureUnits, getInteger(GL.GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS));
if (limits.get(Limits.VertexTextureUnits) > 0) {
caps.add(Caps.VertexTextureFetch);
}
limits.put(Limits.FragmentTextureUnits, getInteger(GL.GL_MAX_TEXTURE_IMAGE_UNITS));
if (caps.contains(Caps.OpenGLES20)) {
limits.put(Limits.FragmentUniformVectors, getInteger(GL.GL_MAX_FRAGMENT_UNIFORM_VECTORS));
limits.put(Limits.VertexUniformVectors, getInteger(GL.GL_MAX_VERTEX_UNIFORM_VECTORS));
} else {
limits.put(Limits.FragmentUniformVectors, getInteger(GL.GL_MAX_FRAGMENT_UNIFORM_COMPONENTS) / 4);
limits.put(Limits.VertexUniformVectors, getInteger(GL.GL_MAX_VERTEX_UNIFORM_COMPONENTS) / 4);
}
limits.put(Limits.VertexAttributes, getInteger(GL.GL_MAX_VERTEX_ATTRIBS));
limits.put(Limits.TextureSize, getInteger(GL.GL_MAX_TEXTURE_SIZE));
limits.put(Limits.CubemapSize, getInteger(GL.GL_MAX_CUBE_MAP_TEXTURE_SIZE));
if (hasExtension("GL_ARB_draw_instanced") &&
hasExtension("GL_ARB_instanced_arrays")) {
// TODO: If there were a way to call the EXT extension for GLES2, should check also (hasExtension("GL_EXT_draw_instanced") && hasExtension("GL_EXT_instanced_arrays"))
caps.add(Caps.MeshInstancing);
}
if (hasExtension("GL_OES_element_index_uint") || gl2 != null) {
caps.add(Caps.IntegerIndexBuffer);
}
if (hasExtension("GL_ARB_texture_buffer_object")) {
caps.add(Caps.TextureBuffer);
}
// == texture format extensions ==
boolean hasFloatTexture;
hasFloatTexture = hasExtension("GL_OES_texture_half_float") &&
hasExtension("GL_OES_texture_float");
if (!hasFloatTexture) {
hasFloatTexture = hasExtension("GL_ARB_texture_float") &&
hasExtension("GL_ARB_half_float_pixel");
if (!hasFloatTexture) {
hasFloatTexture = caps.contains(Caps.OpenGL30);
}
}
if (hasFloatTexture) {
caps.add(Caps.FloatTexture);
}
// integer texture format extensions
if(hasExtension("GL_EXT_texture_integer") || caps.contains(Caps.OpenGL30))
caps.add(Caps.IntegerTexture);
if (hasExtension("GL_OES_depth_texture") || gl2 != null) {
caps.add(Caps.DepthTexture);
}
if (hasExtension("GL_OES_depth24")) {
caps.add(Caps.Depth24);
}
if (hasExtension("GL_OES_rgb8_rgba8") ||
hasExtension("GL_ARM_rgba8") ||
hasExtension("GL_EXT_texture_format_BGRA8888")) {
caps.add(Caps.Rgba8);
}
if (caps.contains(Caps.OpenGL30) || caps.contains(Caps.OpenGLES30) || hasExtension("GL_OES_packed_depth_stencil")) {
caps.add(Caps.PackedDepthStencilBuffer);
}
if (hasExtension("GL_ARB_color_buffer_float") &&
hasExtension("GL_ARB_half_float_pixel")) {
// XXX: Require both 16 and 32 bit float support for FloatColorBuffer.
caps.add(Caps.FloatColorBuffer);
}
if (caps.contains(Caps.OpenGLES30) || hasExtension("GL_ARB_depth_buffer_float")) {
caps.add(Caps.FloatDepthBuffer);
}
if ((hasExtension("GL_EXT_packed_float") && hasFloatTexture) ||
caps.contains(Caps.OpenGL30)) {
// Either OpenGL3 is available or both packed_float & half_float_pixel.
caps.add(Caps.PackedFloatColorBuffer);
caps.add(Caps.PackedFloatTexture);
}
if (hasExtension("GL_EXT_texture_shared_exponent") || caps.contains(Caps.OpenGL30)) {
caps.add(Caps.SharedExponentTexture);
}
if (hasExtension("GL_EXT_texture_compression_s3tc")) {
caps.add(Caps.TextureCompressionS3TC);
}
if (hasExtension("GL_ARB_ES3_compatibility")) {
caps.add(Caps.TextureCompressionETC2);
caps.add(Caps.TextureCompressionETC1);
} else if (hasExtension("GL_OES_compressed_ETC1_RGB8_texture")) {
caps.add(Caps.TextureCompressionETC1);
}
// == end texture format extensions ==
if (hasExtension("GL_ARB_vertex_array_object") || caps.contains(Caps.OpenGL30)) {
caps.add(Caps.VertexBufferArray);
}
if (hasExtension("GL_ARB_texture_non_power_of_two") ||
hasExtension("GL_OES_texture_npot") ||
caps.contains(Caps.OpenGL30)) {
caps.add(Caps.NonPowerOfTwoTextures);
} else {
logger.log(Level.WARNING, "Your graphics card does not "
+ "support non-power-of-2 textures. "
+ "Some features might not work.");
}
if (caps.contains(Caps.OpenGLES20)) {
// OpenGL ES 2 has some limited support for NPOT textures
caps.add(Caps.PartialNonPowerOfTwoTextures);
}
if (hasExtension("GL_EXT_texture_array") || caps.contains(Caps.OpenGL30) || caps.contains(Caps.OpenGLES30)) {
caps.add(Caps.TextureArray);
}
if (hasExtension("GL_EXT_texture_filter_anisotropic")) {
caps.add(Caps.TextureFilterAnisotropic);
limits.put(Limits.TextureAnisotropy, getInteger(GLExt.GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT));
}
if (hasExtension("GL_EXT_framebuffer_object")
|| caps.contains(Caps.OpenGL30)
|| caps.contains(Caps.OpenGLES20)) {
caps.add(Caps.FrameBuffer);
limits.put(Limits.RenderBufferSize, getInteger(GLFbo.GL_MAX_RENDERBUFFER_SIZE_EXT));
limits.put(Limits.FrameBufferAttachments, getInteger(GLFbo.GL_MAX_COLOR_ATTACHMENTS_EXT));
if (hasExtension("GL_EXT_framebuffer_blit") || caps.contains(Caps.OpenGL30) || caps.contains(Caps.OpenGLES30)) {
caps.add(Caps.FrameBufferBlit);
}
if (hasExtension("GL_EXT_framebuffer_multisample") || caps.contains(Caps.OpenGLES30)) {
caps.add(Caps.FrameBufferMultisample);
limits.put(Limits.FrameBufferSamples, getInteger(GLExt.GL_MAX_SAMPLES_EXT));
}
if (hasExtension("GL_ARB_texture_multisample") || caps.contains(Caps.OpenGLES31)) { // GLES31 does not fully support it
caps.add(Caps.TextureMultisample);
limits.put(Limits.ColorTextureSamples, getInteger(GLExt.GL_MAX_COLOR_TEXTURE_SAMPLES));
limits.put(Limits.DepthTextureSamples, getInteger(GLExt.GL_MAX_DEPTH_TEXTURE_SAMPLES));
if (!limits.containsKey(Limits.FrameBufferSamples)) {
// In case they want to query samples on main FB ...
limits.put(Limits.FrameBufferSamples, limits.get(Limits.ColorTextureSamples));
}
}
if (hasExtension("GL_ARB_draw_buffers") || caps.contains(Caps.OpenGL30) || caps.contains(Caps.OpenGLES30)) {
limits.put(Limits.FrameBufferMrtAttachments, getInteger(GLExt.GL_MAX_DRAW_BUFFERS_ARB));
if (limits.get(Limits.FrameBufferMrtAttachments) > 1) {
caps.add(Caps.FrameBufferMRT);
}
} else {
limits.put(Limits.FrameBufferMrtAttachments, 1);
}
}
if (hasExtension("GL_ARB_multisample") /*|| caps.contains(Caps.OpenGLES20)*/) {
boolean available = getInteger(GLExt.GL_SAMPLE_BUFFERS_ARB) != 0;
int samples = getInteger(GLExt.GL_SAMPLES_ARB);
logger.log(Level.FINER, "Samples: {0}", samples);
boolean enabled = gl.glIsEnabled(GLExt.GL_MULTISAMPLE_ARB);
if (samples > 0 && available && !enabled) {
// Doesn't seem to be necessary .. OGL spec says it's always
// set by default?
gl.glEnable(GLExt.GL_MULTISAMPLE_ARB);
}
caps.add(Caps.Multisample);
}
// Supports sRGB pipeline.
if ( (hasExtension("GL_ARB_framebuffer_sRGB") && hasExtension("GL_EXT_texture_sRGB"))
|| caps.contains(Caps.OpenGL30) ) {
caps.add(Caps.Srgb);
}
// Supports seamless cubemap
if (hasExtension("GL_ARB_seamless_cube_map") || caps.contains(Caps.OpenGL32)) {
caps.add(Caps.SeamlessCubemap);
}
if (caps.contains(Caps.OpenGL32) && !hasExtension("GL_ARB_compatibility")) {
caps.add(Caps.CoreProfile);
}
if (hasExtension("GL_ARB_get_program_binary")) {
int binaryFormats = getInteger(GLExt.GL_NUM_PROGRAM_BINARY_FORMATS);
if (binaryFormats > 0) {
caps.add(Caps.BinaryShader);
}
}
if (hasExtension("GL_OES_geometry_shader") || hasExtension("GL_EXT_geometry_shader")) {
caps.add(Caps.GeometryShader);
}
if (hasExtension("GL_OES_tessellation_shader") || hasExtension("GL_EXT_tessellation_shader")) {
caps.add(Caps.TesselationShader);
}
if (hasExtension("GL_ARB_shader_storage_buffer_object")) {
caps.add(Caps.ShaderStorageBufferObject);
limits.put(Limits.ShaderStorageBufferObjectMaxBlockSize, getInteger(GL4.GL_MAX_SHADER_STORAGE_BLOCK_SIZE));
// Commented out until we support ComputeShaders and the ComputeShader Cap
// limits.put(Limits.ShaderStorageBufferObjectMaxComputeBlocks, getInteger(GL4.GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS));
if (caps.contains(Caps.GeometryShader)) {
limits.put(Limits.ShaderStorageBufferObjectMaxGeometryBlocks, getInteger(GL4.GL_MAX_GEOMETRY_SHADER_STORAGE_BLOCKS));
}
limits.put(Limits.ShaderStorageBufferObjectMaxFragmentBlocks, getInteger(GL4.GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS));
limits.put(Limits.ShaderStorageBufferObjectMaxVertexBlocks, getInteger(GL4.GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS));
if (caps.contains(Caps.TesselationShader)) {
limits.put(Limits.ShaderStorageBufferObjectMaxTessControlBlocks, getInteger(GL4.GL_MAX_TESS_CONTROL_SHADER_STORAGE_BLOCKS));
limits.put(Limits.ShaderStorageBufferObjectMaxTessEvaluationBlocks, getInteger(GL4.GL_MAX_TESS_EVALUATION_SHADER_STORAGE_BLOCKS));
}
limits.put(Limits.ShaderStorageBufferObjectMaxCombineBlocks, getInteger(GL4.GL_MAX_COMBINED_SHADER_STORAGE_BLOCKS));
}
if (hasExtension("GL_ARB_uniform_buffer_object")) {
caps.add(Caps.UniformBufferObject);
limits.put(Limits.UniformBufferObjectMaxBlockSize, getInteger(GL3.GL_MAX_UNIFORM_BLOCK_SIZE));
if (caps.contains(Caps.GeometryShader)) {
limits.put(Limits.UniformBufferObjectMaxGeometryBlocks, getInteger(GL3.GL_MAX_GEOMETRY_UNIFORM_BLOCKS));
}
limits.put(Limits.UniformBufferObjectMaxFragmentBlocks, getInteger(GL3.GL_MAX_FRAGMENT_UNIFORM_BLOCKS));
limits.put(Limits.UniformBufferObjectMaxVertexBlocks, getInteger(GL3.GL_MAX_VERTEX_UNIFORM_BLOCKS));
}
if(caps.contains(Caps.OpenGL20)){
caps.add(Caps.UnpackRowLength);
}
// Print context information
logger.log(Level.INFO, "OpenGL Renderer Information\n" +
" * Vendor: {0}\n" +
" * Renderer: {1}\n" +
" * OpenGL Version: {2}\n" +
" * GLSL Version: {3}\n" +
" * Profile: {4}",
new Object[]{
gl.glGetString(GL.GL_VENDOR),
gl.glGetString(GL.GL_RENDERER),
gl.glGetString(GL.GL_VERSION),
gl.glGetString(GL.GL_SHADING_LANGUAGE_VERSION),
caps.contains(Caps.CoreProfile) ? "Core" : "Compatibility"
});
// Print capabilities (if fine logging is enabled)
if (logger.isLoggable(Level.FINE)) {
StringBuilder sb = new StringBuilder();
sb.append("Supported capabilities: \n");
for (Caps cap : caps)
{
sb.append("\t").append(cap.toString()).append("\n");
}
sb.append("\nHardware limits: \n");
for (Limits limit : Limits.values()) {
Integer value = limits.get(limit);
if (value == null) {
value = 0;
}
sb.append("\t").append(limit.name()).append(" = ")
.append(value).append("\n");
}
logger.log(Level.FINE, sb.toString());
}
texUtil.initialize(caps);
}
private void loadCapabilities() {
if (gl2 != null && !(gl instanceof GLES_30)) {
loadCapabilitiesGL2();
} else {
loadCapabilitiesES();
}
loadCapabilitiesCommon();
}
private int getInteger(int en) {
intBuf16.clear();
gl.glGetInteger(en, intBuf16);
return intBuf16.get(0);
}
private boolean getBoolean(int en) {
gl.glGetBoolean(en, nameBuf);
return nameBuf.get(0) != (byte)0;
}
@SuppressWarnings("fallthrough")
@Override
public void initialize() {
loadCapabilities();
// Initialize default state..
gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
if (caps.contains(Caps.SeamlessCubemap)) {
// Enable this globally. Should be OK.
gl.glEnable(GLExt.GL_TEXTURE_CUBE_MAP_SEAMLESS);
}
if (caps.contains(Caps.CoreProfile)) {
// Core Profile requires VAO to be bound.
gl3.glGenVertexArrays(intBuf16);
int vaoId = intBuf16.get(0);
gl3.glBindVertexArray(vaoId);
}
if (gl2 != null && !(gl instanceof GLES_30)) {
gl2.glEnable(GL2.GL_VERTEX_PROGRAM_POINT_SIZE);
if (!caps.contains(Caps.CoreProfile)) {
gl2.glEnable(GL2.GL_POINT_SPRITE);
}
}
IntBuffer tmp = BufferUtils.createIntBuffer(16);
gl.glGetInteger(GL.GL_FRAMEBUFFER_BINDING, tmp);
tmp.rewind();
int fbOnLoad = tmp.get();
if(fbOnLoad > 0)
{
// Overriding default FB to fbOnLoad. Mostly iOS fix for scene processors and filters
defaultFBO = fbOnLoad;
}
}
@Override
public void invalidateState() {
context.reset();
if (gl2 != null) {
context.initialDrawBuf = getInteger(GL2.GL_DRAW_BUFFER);
context.initialReadBuf = getInteger(GL2.GL_READ_BUFFER);
}
}
@Override
public void resetGLObjects() {
logger.log(Level.FINE, "Reseting objects and invalidating state");
objManager.resetObjects();
statistics.clearMemory();
invalidateState();
}
@Override
public void cleanup() {
logger.log(Level.FINE, "Deleting objects and invalidating state");
objManager.deleteAllObjects(this);
OpenCLObjectManager.getInstance().deleteAllObjects();
statistics.clearMemory();
invalidateState();
}
/*********************************************************************\
|* Render State *|
\*********************************************************************/
@Override
public void setDepthRange(float start, float end) {
gl.glDepthRange(start, end);
}
@Override
public void clearBuffers(boolean color, boolean depth, boolean stencil) {
int bits = 0;
if (color) {
//See explanations of the depth below, we must enable color write to be able to clear the color buffer
if (context.colorWriteEnabled == false) {
gl.glColorMask(true, true, true, true);
context.colorWriteEnabled = true;
}
bits = GL.GL_COLOR_BUFFER_BIT;
}
if (depth) {
// glClear(GL.GL_DEPTH_BUFFER_BIT) seems to not work when glDepthMask is false
// here s some link on openl board
// http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=257223
// if depth clear is requested, we enable the depthMask
if (context.depthWriteEnabled == false) {
gl.glDepthMask(true);
context.depthWriteEnabled = true;
}
bits |= GL.GL_DEPTH_BUFFER_BIT;
}
if (stencil) {
// May need to set glStencilMask(0xFF) here if we ever allow users
// to change the stencil mask.
bits |= GL.GL_STENCIL_BUFFER_BIT;
}
if (bits != 0) {
gl.glClear(bits);
}
}
@Override
public void setBackgroundColor(ColorRGBA color) {
if (!context.clearColor.equals(color)) {
gl.glClearColor(color.r, color.g, color.b, color.a);
context.clearColor.set(color);
}
}
@Override
public void setDefaultAnisotropicFilter(int level) {
if (level < 1) {
throw new IllegalArgumentException("level cannot be less than 1");
}
this.defaultAnisotropicFilter = level;
}
@Override
public void setAlphaToCoverage(boolean value) {
if (caps.contains(Caps.Multisample)) {
if (value) {
gl.glEnable(GLExt.GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
} else {
gl.glDisable(GLExt.GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
}
}
}
@Override
public void applyRenderState(RenderState state) {
if (gl2 != null) {
if (state.isWireframe() && !context.wireframe) {
gl2.glPolygonMode(GL.GL_FRONT_AND_BACK, GL2.GL_LINE);
context.wireframe = true;
} else if (!state.isWireframe() && context.wireframe) {
gl2.glPolygonMode(GL.GL_FRONT_AND_BACK, GL2.GL_FILL);
context.wireframe = false;
}
}
if (state.isDepthTest() && !context.depthTestEnabled) {
gl.glEnable(GL.GL_DEPTH_TEST);
context.depthTestEnabled = true;
} else if (!state.isDepthTest() && context.depthTestEnabled) {
gl.glDisable(GL.GL_DEPTH_TEST);
context.depthTestEnabled = false;
}
if (state.isDepthTest() && state.getDepthFunc() != context.depthFunc) {
gl.glDepthFunc(convertTestFunction(state.getDepthFunc()));
context.depthFunc = state.getDepthFunc();
}
if (state.isDepthWrite() && !context.depthWriteEnabled) {
gl.glDepthMask(true);
context.depthWriteEnabled = true;
} else if (!state.isDepthWrite() && context.depthWriteEnabled) {
gl.glDepthMask(false);
context.depthWriteEnabled = false;
}
if (state.isColorWrite() && !context.colorWriteEnabled) {
gl.glColorMask(true, true, true, true);
context.colorWriteEnabled = true;
} else if (!state.isColorWrite() && context.colorWriteEnabled) {
gl.glColorMask(false, false, false, false);
context.colorWriteEnabled = false;
}
if (state.isPolyOffset()) {
if (!context.polyOffsetEnabled) {
gl.glEnable(GL.GL_POLYGON_OFFSET_FILL);
gl.glPolygonOffset(state.getPolyOffsetFactor(),
state.getPolyOffsetUnits());
context.polyOffsetEnabled = true;
context.polyOffsetFactor = state.getPolyOffsetFactor();
context.polyOffsetUnits = state.getPolyOffsetUnits();
} else {
if (state.getPolyOffsetFactor() != context.polyOffsetFactor
|| state.getPolyOffsetUnits() != context.polyOffsetUnits) {
gl.glPolygonOffset(state.getPolyOffsetFactor(),
state.getPolyOffsetUnits());
context.polyOffsetFactor = state.getPolyOffsetFactor();
context.polyOffsetUnits = state.getPolyOffsetUnits();
}
}
} else {
if (context.polyOffsetEnabled) {
gl.glDisable(GL.GL_POLYGON_OFFSET_FILL);
context.polyOffsetEnabled = false;
context.polyOffsetFactor = 0;
context.polyOffsetUnits = 0;
}
}
if (state.getFaceCullMode() != context.cullMode) {
if (state.getFaceCullMode() == RenderState.FaceCullMode.Off) {
gl.glDisable(GL.GL_CULL_FACE);
} else {
gl.glEnable(GL.GL_CULL_FACE);
}
switch (state.getFaceCullMode()) {
case Off:
break;
case Back:
gl.glCullFace(GL.GL_BACK);
break;
case Front:
gl.glCullFace(GL.GL_FRONT);
break;
case FrontAndBack:
gl.glCullFace(GL.GL_FRONT_AND_BACK);
break;
default:
throw new UnsupportedOperationException("Unrecognized face cull mode: "
+ state.getFaceCullMode());
}
context.cullMode = state.getFaceCullMode();
}
// Always update the blend equations and factors when using custom blend mode.
if (state.getBlendMode() == BlendMode.Custom) {
changeBlendMode(BlendMode.Custom);
blendFuncSeparate(
state.getCustomSfactorRGB(),
state.getCustomDfactorRGB(),
state.getCustomSfactorAlpha(),
state.getCustomDfactorAlpha());
blendEquationSeparate(state.getBlendEquation(), state.getBlendEquationAlpha());
// Update the blend equations and factors only on a mode change for all the other (common) blend modes.
} else if (state.getBlendMode() != context.blendMode) {
changeBlendMode(state.getBlendMode());
switch (state.getBlendMode()) {
case Off:
break;
case Additive:
blendFunc(RenderState.BlendFunc.One, RenderState.BlendFunc.One);
break;
case AlphaAdditive:
blendFunc(RenderState.BlendFunc.Src_Alpha, RenderState.BlendFunc.One);
break;
case Alpha:
blendFunc(RenderState.BlendFunc.Src_Alpha, RenderState.BlendFunc.One_Minus_Src_Alpha);
break;
case AlphaSumA:
blendFuncSeparate(
RenderState.BlendFunc.Src_Alpha,
RenderState.BlendFunc.One_Minus_Src_Alpha,
RenderState.BlendFunc.One,
RenderState.BlendFunc.One
);
break;
case PremultAlpha:
blendFunc(RenderState.BlendFunc.One, RenderState.BlendFunc.One_Minus_Src_Alpha);
break;
case Modulate:
blendFunc(RenderState.BlendFunc.Dst_Color, RenderState.BlendFunc.Zero);
break;
case ModulateX2:
blendFunc(RenderState.BlendFunc.Dst_Color, RenderState.BlendFunc.Src_Color);
break;
case Color:
case Screen:
blendFunc(RenderState.BlendFunc.One, RenderState.BlendFunc.One_Minus_Src_Color);
break;
case Exclusion:
blendFunc(RenderState.BlendFunc.One_Minus_Dst_Color, RenderState.BlendFunc.One_Minus_Src_Color);
break;
default:
throw new UnsupportedOperationException("Unrecognized blend mode: "
+ state.getBlendMode());
}
// All of the common modes requires the ADD equation.
// (This might change in the future?)
blendEquationSeparate(RenderState.BlendEquation.Add, RenderState.BlendEquationAlpha.InheritColor);
}
if (context.stencilTest != state.isStencilTest()
|| context.frontStencilStencilFailOperation != state.getFrontStencilStencilFailOperation()
|| context.frontStencilDepthFailOperation != state.getFrontStencilDepthFailOperation()
|| context.frontStencilDepthPassOperation != state.getFrontStencilDepthPassOperation()
|| context.backStencilStencilFailOperation != state.getBackStencilStencilFailOperation()
|| context.backStencilDepthFailOperation != state.getBackStencilDepthFailOperation()
|| context.backStencilDepthPassOperation != state.getBackStencilDepthPassOperation()
|| context.frontStencilFunction != state.getFrontStencilFunction()
|| context.backStencilFunction != state.getBackStencilFunction()) {
context.frontStencilStencilFailOperation = state.getFrontStencilStencilFailOperation(); //terrible looking, I know
context.frontStencilDepthFailOperation = state.getFrontStencilDepthFailOperation();
context.frontStencilDepthPassOperation = state.getFrontStencilDepthPassOperation();
context.backStencilStencilFailOperation = state.getBackStencilStencilFailOperation();
context.backStencilDepthFailOperation = state.getBackStencilDepthFailOperation();
context.backStencilDepthPassOperation = state.getBackStencilDepthPassOperation();
context.frontStencilFunction = state.getFrontStencilFunction();
context.backStencilFunction = state.getBackStencilFunction();
if (state.isStencilTest()) {
gl.glEnable(GL.GL_STENCIL_TEST);
gl.glStencilOpSeparate(GL.GL_FRONT,
convertStencilOperation(state.getFrontStencilStencilFailOperation()),
convertStencilOperation(state.getFrontStencilDepthFailOperation()),
convertStencilOperation(state.getFrontStencilDepthPassOperation()));
gl.glStencilOpSeparate(GL.GL_BACK,
convertStencilOperation(state.getBackStencilStencilFailOperation()),
convertStencilOperation(state.getBackStencilDepthFailOperation()),
convertStencilOperation(state.getBackStencilDepthPassOperation()));
gl.glStencilFuncSeparate(GL.GL_FRONT,
convertTestFunction(state.getFrontStencilFunction()),
0, Integer.MAX_VALUE);
gl.glStencilFuncSeparate(GL.GL_BACK,
convertTestFunction(state.getBackStencilFunction()),
0, Integer.MAX_VALUE);
} else {
gl.glDisable(GL.GL_STENCIL_TEST);
}
}
if (context.lineWidth != state.getLineWidth()) {
gl.glLineWidth(state.getLineWidth());
context.lineWidth = state.getLineWidth();
}
}
private void changeBlendMode(RenderState.BlendMode blendMode) {
if (blendMode != context.blendMode) {
if (blendMode == RenderState.BlendMode.Off) {
gl.glDisable(GL.GL_BLEND);
} else if (context.blendMode == RenderState.BlendMode.Off) {
gl.glEnable(GL.GL_BLEND);
}
context.blendMode = blendMode;
}
}
private void blendEquationSeparate(RenderState.BlendEquation blendEquation, RenderState.BlendEquationAlpha blendEquationAlpha) {
if (blendEquation != context.blendEquation || blendEquationAlpha != context.blendEquationAlpha) {
int glBlendEquation = convertBlendEquation(blendEquation);
int glBlendEquationAlpha = blendEquationAlpha == RenderState.BlendEquationAlpha.InheritColor
? glBlendEquation
: convertBlendEquationAlpha(blendEquationAlpha);
gl.glBlendEquationSeparate(glBlendEquation, glBlendEquationAlpha);
context.blendEquation = blendEquation;
context.blendEquationAlpha = blendEquationAlpha;
}
}
private void blendFunc(RenderState.BlendFunc sfactor, RenderState.BlendFunc dfactor) {
if (sfactor != context.sfactorRGB
|| dfactor != context.dfactorRGB
|| sfactor != context.sfactorAlpha
|| dfactor != context.dfactorAlpha) {
gl.glBlendFunc(
convertBlendFunc(sfactor),
convertBlendFunc(dfactor));
context.sfactorRGB = sfactor;
context.dfactorRGB = dfactor;
context.sfactorAlpha = sfactor;
context.dfactorAlpha = dfactor;
}
}
private void blendFuncSeparate(RenderState.BlendFunc sfactorRGB, RenderState.BlendFunc dfactorRGB,
RenderState.BlendFunc sfactorAlpha, RenderState.BlendFunc dfactorAlpha) {
if (sfactorRGB != context.sfactorRGB
|| dfactorRGB != context.dfactorRGB
|| sfactorAlpha != context.sfactorAlpha
|| dfactorAlpha != context.dfactorAlpha) {
gl.glBlendFuncSeparate(
convertBlendFunc(sfactorRGB),
convertBlendFunc(dfactorRGB),
convertBlendFunc(sfactorAlpha),
convertBlendFunc(dfactorAlpha));
context.sfactorRGB = sfactorRGB;
context.dfactorRGB = dfactorRGB;
context.sfactorAlpha = sfactorAlpha;
context.dfactorAlpha = dfactorAlpha;
}
}
private int convertBlendEquation(RenderState.BlendEquation blendEquation) {
switch (blendEquation) {
case Add:
return GL2.GL_FUNC_ADD;
case Subtract: