/
ShaderProgram.java
934 lines (796 loc) · 30.9 KB
/
ShaderProgram.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
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.badlogic.gdx.graphics.glutils;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.math.Matrix3;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.Vector4;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.BufferUtils;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.ObjectIntMap;
import com.badlogic.gdx.utils.ObjectMap;
/**
* <p>
* A shader program encapsulates a vertex and fragment shader pair linked to form a shader program.
* </p>
*
* <p>
* After construction a ShaderProgram can be used to draw {@link Mesh}. To make the GPU use a specific ShaderProgram the programs
* {@link ShaderProgram#bind()} method must be used which effectively binds the program.
* </p>
*
* <p>
* When a ShaderProgram is bound one can set uniforms, vertex attributes and attributes as needed via the respective methods.
* </p>
*
* <p>
* A ShaderProgram must be disposed via a call to {@link ShaderProgram#dispose()} when it is no longer needed
* </p>
*
* <p>
* ShaderPrograms are managed. In case the OpenGL context is lost all shaders get invalidated and have to be reloaded. This
* happens on Android when a user switches to another application or receives an incoming call. Managed ShaderPrograms are
* automatically reloaded when the OpenGL context is recreated so you don't have to do this manually.
* </p>
*
* @author mzechner */
public class ShaderProgram implements Disposable {
/** default name for position attributes **/
public static final String POSITION_ATTRIBUTE = "a_position";
/** default name for normal attributes **/
public static final String NORMAL_ATTRIBUTE = "a_normal";
/** default name for color attributes **/
public static final String COLOR_ATTRIBUTE = "a_color";
/** default name for texcoords attributes, append texture unit number **/
public static final String TEXCOORD_ATTRIBUTE = "a_texCoord";
/** default name for tangent attribute **/
public static final String TANGENT_ATTRIBUTE = "a_tangent";
/** default name for binormal attribute **/
public static final String BINORMAL_ATTRIBUTE = "a_binormal";
/** default name for boneweight attribute **/
public static final String BONEWEIGHT_ATTRIBUTE = "a_boneWeight";
/** flag indicating whether attributes & uniforms must be present at all times **/
public static boolean pedantic = true;
/** code that is always added to the vertex shader code, typically used to inject a #version line. Note that this is added
* as-is, you should include a newline (`\n`) if needed. */
public static String prependVertexCode = "";
/** code that is always added to every fragment shader code, typically used to inject a #version line. Note that this is added
* as-is, you should include a newline (`\n`) if needed. */
public static String prependFragmentCode = "";
/** the list of currently available shaders **/
private final static ObjectMap<Application, Array<ShaderProgram>> shaders = new ObjectMap<Application, Array<ShaderProgram>>();
/** the log **/
private String log = "";
/** whether this program compiled successfully **/
private boolean isCompiled;
/** uniform lookup **/
private final ObjectIntMap<String> uniforms = new ObjectIntMap<String>();
/** uniform types **/
private final ObjectIntMap<String> uniformTypes = new ObjectIntMap<String>();
/** uniform sizes **/
private final ObjectIntMap<String> uniformSizes = new ObjectIntMap<String>();
/** uniform names **/
private String[] uniformNames;
/** attribute lookup **/
private final ObjectIntMap<String> attributes = new ObjectIntMap<String>();
/** attribute types **/
private final ObjectIntMap<String> attributeTypes = new ObjectIntMap<String>();
/** attribute sizes **/
private final ObjectIntMap<String> attributeSizes = new ObjectIntMap<String>();
/** attribute names **/
private String[] attributeNames;
/** program handle **/
private int program;
/** vertex shader handle **/
private int vertexShaderHandle;
/** fragment shader handle **/
private int fragmentShaderHandle;
/** matrix float buffer **/
private final FloatBuffer matrix;
/** vertex shader source **/
private final String vertexShaderSource;
/** fragment shader source **/
private final String fragmentShaderSource;
/** whether this shader was invalidated **/
private boolean invalidated;
/** reference count **/
private int refCount = 0;
/** Constructs a new ShaderProgram and immediately compiles it.
*
* @param vertexShader the vertex shader
* @param fragmentShader the fragment shader */
public ShaderProgram (String vertexShader, String fragmentShader) {
if (vertexShader == null) throw new IllegalArgumentException("vertex shader must not be null");
if (fragmentShader == null) throw new IllegalArgumentException("fragment shader must not be null");
if (prependVertexCode != null && prependVertexCode.length() > 0) vertexShader = prependVertexCode + vertexShader;
if (prependFragmentCode != null && prependFragmentCode.length() > 0) fragmentShader = prependFragmentCode + fragmentShader;
this.vertexShaderSource = vertexShader;
this.fragmentShaderSource = fragmentShader;
this.matrix = BufferUtils.newFloatBuffer(16);
compileShaders(vertexShader, fragmentShader);
if (isCompiled()) {
fetchAttributes();
fetchUniforms();
addManagedShader(Gdx.app, this);
}
}
public ShaderProgram (FileHandle vertexShader, FileHandle fragmentShader) {
this(vertexShader.readString(), fragmentShader.readString());
}
/** Loads and compiles the shaders, creates a new program and links the shaders.
*
* @param vertexShader
* @param fragmentShader */
private void compileShaders (String vertexShader, String fragmentShader) {
vertexShaderHandle = loadShader(GL20.GL_VERTEX_SHADER, vertexShader);
fragmentShaderHandle = loadShader(GL20.GL_FRAGMENT_SHADER, fragmentShader);
if (vertexShaderHandle == -1 || fragmentShaderHandle == -1) {
isCompiled = false;
return;
}
program = linkProgram(createProgram());
if (program == -1) {
isCompiled = false;
return;
}
isCompiled = true;
}
private int loadShader (int type, String source) {
GL20 gl = Gdx.gl20;
IntBuffer intbuf = BufferUtils.newIntBuffer(1);
int shader = gl.glCreateShader(type);
if (shader == 0) return -1;
gl.glShaderSource(shader, source);
gl.glCompileShader(shader);
gl.glGetShaderiv(shader, GL20.GL_COMPILE_STATUS, intbuf);
int compiled = intbuf.get(0);
if (compiled == 0) {
// gl.glGetShaderiv(shader, GL20.GL_INFO_LOG_LENGTH, intbuf);
// int infoLogLength = intbuf.get(0);
// if (infoLogLength > 1) {
String infoLog = gl.glGetShaderInfoLog(shader);
log += type == GL20.GL_VERTEX_SHADER ? "Vertex shader\n" : "Fragment shader:\n";
log += infoLog;
// }
return -1;
}
return shader;
}
protected int createProgram () {
GL20 gl = Gdx.gl20;
int program = gl.glCreateProgram();
return program != 0 ? program : -1;
}
private int linkProgram (int program) {
GL20 gl = Gdx.gl20;
if (program == -1) return -1;
gl.glAttachShader(program, vertexShaderHandle);
gl.glAttachShader(program, fragmentShaderHandle);
gl.glLinkProgram(program);
ByteBuffer tmp = ByteBuffer.allocateDirect(4);
tmp.order(ByteOrder.nativeOrder());
IntBuffer intbuf = tmp.asIntBuffer();
gl.glGetProgramiv(program, GL20.GL_LINK_STATUS, intbuf);
int linked = intbuf.get(0);
if (linked == 0) {
// Gdx.gl20.glGetProgramiv(program, GL20.GL_INFO_LOG_LENGTH, intbuf);
// int infoLogLength = intbuf.get(0);
// if (infoLogLength > 1) {
log = Gdx.gl20.glGetProgramInfoLog(program);
// }
return -1;
}
return program;
}
final static IntBuffer intbuf = BufferUtils.newIntBuffer(1);
/** @return the log info for the shader compilation and program linking stage. The shader needs to be bound for this method to
* have an effect. */
public String getLog () {
if (isCompiled) {
// Gdx.gl20.glGetProgramiv(program, GL20.GL_INFO_LOG_LENGTH, intbuf);
// int infoLogLength = intbuf.get(0);
// if (infoLogLength > 1) {
log = Gdx.gl20.glGetProgramInfoLog(program);
// }
return log;
} else {
return log;
}
}
/** @return whether this ShaderProgram compiled successfully. */
public boolean isCompiled () {
return isCompiled;
}
private int fetchAttributeLocation (String name) {
GL20 gl = Gdx.gl20;
// -2 == not yet cached
// -1 == cached but not found
int location;
if ((location = attributes.get(name, -2)) == -2) {
location = gl.glGetAttribLocation(program, name);
attributes.put(name, location);
}
return location;
}
private int fetchUniformLocation (String name) {
return fetchUniformLocation(name, pedantic);
}
public int fetchUniformLocation (String name, boolean pedantic) {
// -2 == not yet cached
// -1 == cached but not found
int location;
if ((location = uniforms.get(name, -2)) == -2) {
location = Gdx.gl20.glGetUniformLocation(program, name);
if (location == -1 && pedantic) {
if (isCompiled) throw new IllegalArgumentException("No uniform with name '" + name + "' in shader");
throw new IllegalStateException("An attempted fetch uniform from uncompiled shader \n" + getLog());
}
uniforms.put(name, location);
}
return location;
}
/** Sets the uniform with the given name. The {@link ShaderProgram} must be bound for this to work.
*
* @param name the name of the uniform
* @param value the value */
public void setUniformi (String name, int value) {
GL20 gl = Gdx.gl20;
checkManaged();
int location = fetchUniformLocation(name);
gl.glUniform1i(location, value);
}
public void setUniformi (int location, int value) {
GL20 gl = Gdx.gl20;
checkManaged();
gl.glUniform1i(location, value);
}
/** Sets the uniform with the given name. The {@link ShaderProgram} must be bound for this to work.
*
* @param name the name of the uniform
* @param value1 the first value
* @param value2 the second value */
public void setUniformi (String name, int value1, int value2) {
GL20 gl = Gdx.gl20;
checkManaged();
int location = fetchUniformLocation(name);
gl.glUniform2i(location, value1, value2);
}
public void setUniformi (int location, int value1, int value2) {
GL20 gl = Gdx.gl20;
checkManaged();
gl.glUniform2i(location, value1, value2);
}
/** Sets the uniform with the given name. The {@link ShaderProgram} must be bound for this to work.
*
* @param name the name of the uniform
* @param value1 the first value
* @param value2 the second value
* @param value3 the third value */
public void setUniformi (String name, int value1, int value2, int value3) {
GL20 gl = Gdx.gl20;
checkManaged();
int location = fetchUniformLocation(name);
gl.glUniform3i(location, value1, value2, value3);
}
public void setUniformi (int location, int value1, int value2, int value3) {
GL20 gl = Gdx.gl20;
checkManaged();
gl.glUniform3i(location, value1, value2, value3);
}
/** Sets the uniform with the given name. The {@link ShaderProgram} must be bound for this to work.
*
* @param name the name of the uniform
* @param value1 the first value
* @param value2 the second value
* @param value3 the third value
* @param value4 the fourth value */
public void setUniformi (String name, int value1, int value2, int value3, int value4) {
GL20 gl = Gdx.gl20;
checkManaged();
int location = fetchUniformLocation(name);
gl.glUniform4i(location, value1, value2, value3, value4);
}
public void setUniformi (int location, int value1, int value2, int value3, int value4) {
GL20 gl = Gdx.gl20;
checkManaged();
gl.glUniform4i(location, value1, value2, value3, value4);
}
/** Sets the uniform with the given name. The {@link ShaderProgram} must be bound for this to work.
*
* @param name the name of the uniform
* @param value the value */
public void setUniformf (String name, float value) {
GL20 gl = Gdx.gl20;
checkManaged();
int location = fetchUniformLocation(name);
gl.glUniform1f(location, value);
}
public void setUniformf (int location, float value) {
GL20 gl = Gdx.gl20;
checkManaged();
gl.glUniform1f(location, value);
}
/** Sets the uniform with the given name. The {@link ShaderProgram} must be bound for this to work.
*
* @param name the name of the uniform
* @param value1 the first value
* @param value2 the second value */
public void setUniformf (String name, float value1, float value2) {
GL20 gl = Gdx.gl20;
checkManaged();
int location = fetchUniformLocation(name);
gl.glUniform2f(location, value1, value2);
}
public void setUniformf (int location, float value1, float value2) {
GL20 gl = Gdx.gl20;
checkManaged();
gl.glUniform2f(location, value1, value2);
}
/** Sets the uniform with the given name. The {@link ShaderProgram} must be bound for this to work.
*
* @param name the name of the uniform
* @param value1 the first value
* @param value2 the second value
* @param value3 the third value */
public void setUniformf (String name, float value1, float value2, float value3) {
GL20 gl = Gdx.gl20;
checkManaged();
int location = fetchUniformLocation(name);
gl.glUniform3f(location, value1, value2, value3);
}
public void setUniformf (int location, float value1, float value2, float value3) {
GL20 gl = Gdx.gl20;
checkManaged();
gl.glUniform3f(location, value1, value2, value3);
}
/** Sets the uniform with the given name. The {@link ShaderProgram} must be bound for this to work.
*
* @param name the name of the uniform
* @param value1 the first value
* @param value2 the second value
* @param value3 the third value
* @param value4 the fourth value */
public void setUniformf (String name, float value1, float value2, float value3, float value4) {
GL20 gl = Gdx.gl20;
checkManaged();
int location = fetchUniformLocation(name);
gl.glUniform4f(location, value1, value2, value3, value4);
}
public void setUniformf (int location, float value1, float value2, float value3, float value4) {
GL20 gl = Gdx.gl20;
checkManaged();
gl.glUniform4f(location, value1, value2, value3, value4);
}
public void setUniform1fv (String name, float[] values, int offset, int length) {
GL20 gl = Gdx.gl20;
checkManaged();
int location = fetchUniformLocation(name);
gl.glUniform1fv(location, length, values, offset);
}
public void setUniform1fv (int location, float[] values, int offset, int length) {
GL20 gl = Gdx.gl20;
checkManaged();
gl.glUniform1fv(location, length, values, offset);
}
public void setUniform2fv (String name, float[] values, int offset, int length) {
GL20 gl = Gdx.gl20;
checkManaged();
int location = fetchUniformLocation(name);
gl.glUniform2fv(location, length / 2, values, offset);
}
public void setUniform2fv (int location, float[] values, int offset, int length) {
GL20 gl = Gdx.gl20;
checkManaged();
gl.glUniform2fv(location, length / 2, values, offset);
}
public void setUniform3fv (String name, float[] values, int offset, int length) {
GL20 gl = Gdx.gl20;
checkManaged();
int location = fetchUniformLocation(name);
gl.glUniform3fv(location, length / 3, values, offset);
}
public void setUniform3fv (int location, float[] values, int offset, int length) {
GL20 gl = Gdx.gl20;
checkManaged();
gl.glUniform3fv(location, length / 3, values, offset);
}
public void setUniform4fv (String name, float[] values, int offset, int length) {
GL20 gl = Gdx.gl20;
checkManaged();
int location = fetchUniformLocation(name);
gl.glUniform4fv(location, length / 4, values, offset);
}
public void setUniform4fv (int location, float[] values, int offset, int length) {
GL20 gl = Gdx.gl20;
checkManaged();
gl.glUniform4fv(location, length / 4, values, offset);
}
/** Sets the uniform matrix with the given name. The {@link ShaderProgram} must be bound for this to work.
*
* @param name the name of the uniform
* @param matrix the matrix */
public void setUniformMatrix (String name, Matrix4 matrix) {
setUniformMatrix(name, matrix, false);
}
/** Sets the uniform matrix with the given name. The {@link ShaderProgram} must be bound for this to work.
*
* @param name the name of the uniform
* @param matrix the matrix
* @param transpose whether the matrix should be transposed */
public void setUniformMatrix (String name, Matrix4 matrix, boolean transpose) {
setUniformMatrix(fetchUniformLocation(name), matrix, transpose);
}
public void setUniformMatrix (int location, Matrix4 matrix) {
setUniformMatrix(location, matrix, false);
}
public void setUniformMatrix (int location, Matrix4 matrix, boolean transpose) {
GL20 gl = Gdx.gl20;
checkManaged();
gl.glUniformMatrix4fv(location, 1, transpose, matrix.val, 0);
}
/** Sets the uniform matrix with the given name. The {@link ShaderProgram} must be bound for this to work.
*
* @param name the name of the uniform
* @param matrix the matrix */
public void setUniformMatrix (String name, Matrix3 matrix) {
setUniformMatrix(name, matrix, false);
}
/** Sets the uniform matrix with the given name. The {@link ShaderProgram} must be bound for this to work.
*
* @param name the name of the uniform
* @param matrix the matrix
* @param transpose whether the uniform matrix should be transposed */
public void setUniformMatrix (String name, Matrix3 matrix, boolean transpose) {
setUniformMatrix(fetchUniformLocation(name), matrix, transpose);
}
public void setUniformMatrix (int location, Matrix3 matrix) {
setUniformMatrix(location, matrix, false);
}
public void setUniformMatrix (int location, Matrix3 matrix, boolean transpose) {
GL20 gl = Gdx.gl20;
checkManaged();
gl.glUniformMatrix3fv(location, 1, transpose, matrix.val, 0);
}
/** Sets an array of uniform matrices with the given name. The {@link ShaderProgram} must be bound for this to work.
*
* @param name the name of the uniform
* @param buffer buffer containing the matrix data
* @param transpose whether the uniform matrix should be transposed */
public void setUniformMatrix3fv (String name, FloatBuffer buffer, int count, boolean transpose) {
GL20 gl = Gdx.gl20;
checkManaged();
((Buffer)buffer).position(0);
int location = fetchUniformLocation(name);
gl.glUniformMatrix3fv(location, count, transpose, buffer);
}
/** Sets an array of uniform matrices with the given name. The {@link ShaderProgram} must be bound for this to work.
*
* @param name the name of the uniform
* @param buffer buffer containing the matrix data
* @param transpose whether the uniform matrix should be transposed */
public void setUniformMatrix4fv (String name, FloatBuffer buffer, int count, boolean transpose) {
GL20 gl = Gdx.gl20;
checkManaged();
((Buffer)buffer).position(0);
int location = fetchUniformLocation(name);
gl.glUniformMatrix4fv(location, count, transpose, buffer);
}
public void setUniformMatrix4fv (int location, float[] values, int offset, int length) {
GL20 gl = Gdx.gl20;
checkManaged();
gl.glUniformMatrix4fv(location, length / 16, false, values, offset);
}
public void setUniformMatrix4fv (String name, float[] values, int offset, int length) {
setUniformMatrix4fv(fetchUniformLocation(name), values, offset, length);
}
/** Sets the uniform with the given name. The {@link ShaderProgram} must be bound for this to work.
*
* @param name the name of the uniform
* @param values x and y as the first and second values respectively */
public void setUniformf (String name, Vector2 values) {
setUniformf(name, values.x, values.y);
}
public void setUniformf (int location, Vector2 values) {
setUniformf(location, values.x, values.y);
}
/** Sets the uniform with the given name. The {@link ShaderProgram} must be bound for this to work.
*
* @param name the name of the uniform
* @param values x, y and z as the first, second and third values respectively */
public void setUniformf (String name, Vector3 values) {
setUniformf(name, values.x, values.y, values.z);
}
public void setUniformf (int location, Vector3 values) {
setUniformf(location, values.x, values.y, values.z);
}
/** Sets the uniform with the given name. The {@link ShaderProgram} must be bound for this to work.
*
* @param name the name of the uniform
* @param values x, y, z, and w as the first, second, third, and fourth values respectively */
public void setUniformf (String name, Vector4 values) {
setUniformf(name, values.x, values.y, values.z, values.w);
}
public void setUniformf (int location, Vector4 values) {
setUniformf(location, values.x, values.y, values.z, values.w);
}
/** Sets the uniform with the given name. The {@link ShaderProgram} must be bound for this to work.
*
* @param name the name of the uniform
* @param values r, g, b and a as the first through fourth values respectively */
public void setUniformf (String name, Color values) {
setUniformf(name, values.r, values.g, values.b, values.a);
}
public void setUniformf (int location, Color values) {
setUniformf(location, values.r, values.g, values.b, values.a);
}
/** Sets the vertex attribute with the given name. The {@link ShaderProgram} must be bound for this to work.
*
* @param name the attribute name
* @param size the number of components, must be >= 1 and <= 4
* @param type the type, must be one of GL20.GL_BYTE, GL20.GL_UNSIGNED_BYTE, GL20.GL_SHORT,
* GL20.GL_UNSIGNED_SHORT,GL20.GL_FIXED, or GL20.GL_FLOAT. GL_FIXED will not work on the desktop
* @param normalize whether fixed point data should be normalized. Will not work on the desktop
* @param stride the stride in bytes between successive attributes
* @param buffer the buffer containing the vertex attributes. */
public void setVertexAttribute (String name, int size, int type, boolean normalize, int stride, Buffer buffer) {
GL20 gl = Gdx.gl20;
checkManaged();
int location = fetchAttributeLocation(name);
if (location == -1) return;
gl.glVertexAttribPointer(location, size, type, normalize, stride, buffer);
}
public void setVertexAttribute (int location, int size, int type, boolean normalize, int stride, Buffer buffer) {
GL20 gl = Gdx.gl20;
checkManaged();
gl.glVertexAttribPointer(location, size, type, normalize, stride, buffer);
}
/** Sets the vertex attribute with the given name. The {@link ShaderProgram} must be bound for this to work.
*
* @param name the attribute name
* @param size the number of components, must be >= 1 and <= 4
* @param type the type, must be one of GL20.GL_BYTE, GL20.GL_UNSIGNED_BYTE, GL20.GL_SHORT,
* GL20.GL_UNSIGNED_SHORT,GL20.GL_FIXED, or GL20.GL_FLOAT. GL_FIXED will not work on the desktop
* @param normalize whether fixed point data should be normalized. Will not work on the desktop
* @param stride the stride in bytes between successive attributes
* @param offset byte offset into the vertex buffer object bound to GL20.GL_ARRAY_BUFFER. */
public void setVertexAttribute (String name, int size, int type, boolean normalize, int stride, int offset) {
GL20 gl = Gdx.gl20;
checkManaged();
int location = fetchAttributeLocation(name);
if (location == -1) return;
gl.glVertexAttribPointer(location, size, type, normalize, stride, offset);
}
public void setVertexAttribute (int location, int size, int type, boolean normalize, int stride, int offset) {
GL20 gl = Gdx.gl20;
checkManaged();
gl.glVertexAttribPointer(location, size, type, normalize, stride, offset);
}
/** @deprecated use {@link #bind()} instead, this method will be remove in future version */
@Deprecated
public void begin () {
bind();
}
public void bind () {
GL20 gl = Gdx.gl20;
checkManaged();
gl.glUseProgram(program);
}
/** @deprecated no longer necessary, this method will be remove in future version */
@Deprecated
public void end () {
}
/** Disposes all resources associated with this shader. Must be called when the shader is no longer used. */
public void dispose () {
GL20 gl = Gdx.gl20;
gl.glUseProgram(0);
gl.glDeleteShader(vertexShaderHandle);
gl.glDeleteShader(fragmentShaderHandle);
gl.glDeleteProgram(program);
if (shaders.get(Gdx.app) != null) shaders.get(Gdx.app).removeValue(this, true);
}
/** Disables the vertex attribute with the given name
*
* @param name the vertex attribute name */
public void disableVertexAttribute (String name) {
GL20 gl = Gdx.gl20;
checkManaged();
int location = fetchAttributeLocation(name);
if (location == -1) return;
gl.glDisableVertexAttribArray(location);
}
public void disableVertexAttribute (int location) {
GL20 gl = Gdx.gl20;
checkManaged();
gl.glDisableVertexAttribArray(location);
}
/** Enables the vertex attribute with the given name
*
* @param name the vertex attribute name */
public void enableVertexAttribute (String name) {
GL20 gl = Gdx.gl20;
checkManaged();
int location = fetchAttributeLocation(name);
if (location == -1) return;
gl.glEnableVertexAttribArray(location);
}
public void enableVertexAttribute (int location) {
GL20 gl = Gdx.gl20;
checkManaged();
gl.glEnableVertexAttribArray(location);
}
private void checkManaged () {
if (invalidated) {
compileShaders(vertexShaderSource, fragmentShaderSource);
invalidated = false;
}
}
private void addManagedShader (Application app, ShaderProgram shaderProgram) {
Array<ShaderProgram> managedResources = shaders.get(app);
if (managedResources == null) managedResources = new Array<ShaderProgram>();
managedResources.add(shaderProgram);
shaders.put(app, managedResources);
}
/** Invalidates all shaders so the next time they are used new handles are generated
* @param app */
public static void invalidateAllShaderPrograms (Application app) {
if (Gdx.gl20 == null) return;
Array<ShaderProgram> shaderArray = shaders.get(app);
if (shaderArray == null) return;
for (int i = 0; i < shaderArray.size; i++) {
shaderArray.get(i).invalidated = true;
shaderArray.get(i).checkManaged();
}
}
public static void clearAllShaderPrograms (Application app) {
shaders.remove(app);
}
public static String getManagedStatus () {
StringBuilder builder = new StringBuilder();
int i = 0;
builder.append("Managed shaders/app: { ");
for (Application app : shaders.keys()) {
builder.append(shaders.get(app).size);
builder.append(" ");
}
builder.append("}");
return builder.toString();
}
/** @return the number of managed shader programs currently loaded */
public static int getNumManagedShaderPrograms () {
return shaders.get(Gdx.app).size;
}
/** Sets the given attribute
*
* @param name the name of the attribute
* @param value1 the first value
* @param value2 the second value
* @param value3 the third value
* @param value4 the fourth value */
public void setAttributef (String name, float value1, float value2, float value3, float value4) {
GL20 gl = Gdx.gl20;
int location = fetchAttributeLocation(name);
gl.glVertexAttrib4f(location, value1, value2, value3, value4);
}
IntBuffer params = BufferUtils.newIntBuffer(1);
IntBuffer type = BufferUtils.newIntBuffer(1);
private void fetchUniforms () {
((Buffer)params).clear();
Gdx.gl20.glGetProgramiv(program, GL20.GL_ACTIVE_UNIFORMS, params);
int numUniforms = params.get(0);
uniformNames = new String[numUniforms];
for (int i = 0; i < numUniforms; i++) {
((Buffer)params).clear();
params.put(0, 1);
((Buffer)type).clear();
String name = Gdx.gl20.glGetActiveUniform(program, i, params, type);
int location = Gdx.gl20.glGetUniformLocation(program, name);
uniforms.put(name, location);
uniformTypes.put(name, type.get(0));
uniformSizes.put(name, params.get(0));
uniformNames[i] = name;
}
}
private void fetchAttributes () {
((Buffer)params).clear();
Gdx.gl20.glGetProgramiv(program, GL20.GL_ACTIVE_ATTRIBUTES, params);
int numAttributes = params.get(0);
attributeNames = new String[numAttributes];
for (int i = 0; i < numAttributes; i++) {
((Buffer)params).clear();
params.put(0, 1);
((Buffer)type).clear();
String name = Gdx.gl20.glGetActiveAttrib(program, i, params, type);
int location = Gdx.gl20.glGetAttribLocation(program, name);
attributes.put(name, location);
attributeTypes.put(name, type.get(0));
attributeSizes.put(name, params.get(0));
attributeNames[i] = name;
}
}
/** @param name the name of the attribute
* @return whether the attribute is available in the shader */
public boolean hasAttribute (String name) {
return attributes.containsKey(name);
}
/** @param name the name of the attribute
* @return the type of the attribute, one of {@link GL20#GL_FLOAT}, {@link GL20#GL_FLOAT_VEC2} etc. */
public int getAttributeType (String name) {
return attributeTypes.get(name, 0);
}
/** @param name the name of the attribute
* @return the location of the attribute or -1. */
public int getAttributeLocation (String name) {
return attributes.get(name, -1);
}
/** @param name the name of the attribute
* @return the size of the attribute or 0. */
public int getAttributeSize (String name) {
return attributeSizes.get(name, 0);
}
/** @param name the name of the uniform
* @return whether the uniform is available in the shader */
public boolean hasUniform (String name) {
return uniforms.containsKey(name);
}
/** @param name the name of the uniform
* @return the type of the uniform, one of {@link GL20#GL_FLOAT}, {@link GL20#GL_FLOAT_VEC2} etc. */
public int getUniformType (String name) {
return uniformTypes.get(name, 0);
}
/** @param name the name of the uniform
* @return the location of the uniform or -1. */
public int getUniformLocation (String name) {
return uniforms.get(name, -1);
}
/** @param name the name of the uniform
* @return the size of the uniform or 0. */
public int getUniformSize (String name) {
return uniformSizes.get(name, 0);
}
/** @return the attributes */
public String[] getAttributes () {
return attributeNames;
}
/** @return the uniforms */
public String[] getUniforms () {
return uniformNames;
}
/** @return the source of the vertex shader */
public String getVertexShaderSource () {
return vertexShaderSource;
}
/** @return the source of the fragment shader */
public String getFragmentShaderSource () {
return fragmentShaderSource;
}
/** @return the handle of the shader program */
public int getHandle () {
return program;
}
}