-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathPShader.java
More file actions
1478 lines (1197 loc) · 42.8 KB
/
PShader.java
File metadata and controls
1478 lines (1197 loc) · 42.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-15 The Processing Foundation
Copyright (c) 2004-12 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology
This library 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, version 2.1.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
package processing.opengl;
import processing.core.*;
import processing.opengl.PGraphicsOpenGL.GLResourceShader;
import java.net.URL;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.HashMap;
/**
* This class encapsulates a GLSL shader program, including a vertex
* and a fragment shader. Based on the GLSLShader class from GLGraphics, which
* in turn was originally based in the code by JohnG:
* http://processing.org/discourse/beta/num_1159494801.html
*
* @webref rendering:shaders
*/
public class PShader implements PConstants {
static protected final int POINT = 0;
static protected final int LINE = 1;
static protected final int POLY = 2;
static protected final int COLOR = 3;
static protected final int LIGHT = 4;
static protected final int TEXTURE = 5;
static protected final int TEXLIGHT = 6;
static protected String pointShaderAttrRegexp =
"attribute *vec2 *offset";
static protected String pointShaderInRegexp =
"in *vec2 *offset;";
static protected String lineShaderAttrRegexp =
"attribute *vec4 *direction";
static protected String lineShaderInRegexp =
"in *vec4 *direction";
static protected String pointShaderDefRegexp =
"#define *PROCESSING_POINT_SHADER";
static protected String lineShaderDefRegexp =
"#define *PROCESSING_LINE_SHADER";
static protected String colorShaderDefRegexp =
"#define *PROCESSING_COLOR_SHADER";
static protected String lightShaderDefRegexp =
"#define *PROCESSING_LIGHT_SHADER";
static protected String texShaderDefRegexp =
"#define *PROCESSING_TEXTURE_SHADER";
static protected String texlightShaderDefRegexp =
"#define *PROCESSING_TEXLIGHT_SHADER";
static protected String polyShaderDefRegexp =
"#define *PROCESSING_POLYGON_SHADER";
static protected String triShaderAttrRegexp =
"#define *PROCESSING_TRIANGLES_SHADER";
static protected String quadShaderAttrRegexp =
"#define *PROCESSING_QUADS_SHADER";
protected PApplet parent;
// The main renderer associated to the parent PApplet.
//protected PGraphicsOpenGL pgMain;
// We need a reference to the renderer since a shader might
// be called by different renderers within a single application
// (the one corresponding to the main surface, or other offscreen
// renderers).
protected PGraphicsOpenGL primaryPG;
protected PGraphicsOpenGL currentPG;
protected PGL pgl;
protected int context; // The context that created this shader.
// The shader type: POINT, LINE, POLY, etc.
protected int type;
public int glProgram;
public int glVertex;
public int glFragment;
private GLResourceShader glres;
protected URL vertexURL;
protected URL fragmentURL;
protected String vertexFilename;
protected String fragmentFilename;
protected String[] vertexShaderSource;
protected String[] fragmentShaderSource;
protected boolean bound;
protected HashMap<String, UniformValue> uniformValues = null;
protected HashMap<Integer, Texture> textures;
protected HashMap<Integer, Integer> texUnits;
// Direct buffers to pass shader data to GL
protected IntBuffer intBuffer;
protected FloatBuffer floatBuffer;
protected boolean loadedAttributes = false;
protected boolean loadedUniforms = false;
// Uniforms common to all shader types
protected int transformMatLoc;
protected int modelviewMatLoc;
protected int projectionMatLoc;
protected int ppixelsLoc;
protected int ppixelsUnit;
protected int viewportLoc;
protected int resolutionLoc;
// Uniforms only for lines and points
protected int perspectiveLoc;
protected int scaleLoc;
// Lighting uniforms
protected int lightCountLoc;
protected int lightPositionLoc;
protected int lightNormalLoc;
protected int lightAmbientLoc;
protected int lightDiffuseLoc;
protected int lightSpecularLoc;
protected int lightFalloffLoc;
protected int lightSpotLoc;
// Texturing uniforms
protected Texture texture;
protected int texUnit;
protected int textureLoc;
protected int texMatrixLoc;
protected int texOffsetLoc;
protected float[] tcmat;
// Vertex attributes
protected int vertexLoc;
protected int colorLoc;
protected int normalLoc;
protected int texCoordLoc;
protected int normalMatLoc;
protected int directionLoc;
protected int offsetLoc;
protected int ambientLoc;
protected int specularLoc;
protected int emissiveLoc;
protected int shininessLoc;
public PShader() {
parent = null;
pgl = null;
context = -1;
this.vertexURL = null;
this.fragmentURL = null;
this.vertexFilename = null;
this.fragmentFilename = null;
glProgram = 0;
glVertex = 0;
glFragment = 0;
intBuffer = PGL.allocateIntBuffer(1);
floatBuffer = PGL.allocateFloatBuffer(1);
bound = false;
type = -1;
}
public PShader(PApplet parent) {
this();
this.parent = parent;
primaryPG = (PGraphicsOpenGL)parent.g;
pgl = primaryPG.pgl;
context = pgl.createEmptyContext();
}
/**
* Creates a shader program using the specified vertex and fragment
* shaders.
*
* @param parent the parent program
* @param vertFilename name of the vertex shader
* @param fragFilename name of the fragment shader
*/
public PShader(PApplet parent, String vertFilename, String fragFilename) {
this.parent = parent;
primaryPG = (PGraphicsOpenGL)parent.g;
pgl = primaryPG.pgl;
this.vertexURL = null;
this.fragmentURL = null;
this.vertexFilename = vertFilename;
this.fragmentFilename = fragFilename;
fragmentShaderSource = pgl.loadFragmentShader(fragFilename);
vertexShaderSource = pgl.loadVertexShader(vertFilename);
glProgram = 0;
glVertex = 0;
glFragment = 0;
intBuffer = PGL.allocateIntBuffer(1);
floatBuffer = PGL.allocateFloatBuffer(1);
int vertType = getShaderType(vertexShaderSource, -1);
int fragType = getShaderType(fragmentShaderSource, -1);
if (vertType == -1 && fragType == -1) {
type = PShader.POLY;
} else if (vertType == -1) {
type = fragType;
} else if (fragType == -1) {
type = vertType;
} else if (fragType == vertType) {
type = vertType;
} else {
PGraphics.showWarning(PGraphicsOpenGL.INCONSISTENT_SHADER_TYPES);
}
}
/**
* @param vertURL network location of the vertex shader
* @param fragURL network location of the fragment shader
*/
public PShader(PApplet parent, URL vertURL, URL fragURL) {
this.parent = parent;
primaryPG = (PGraphicsOpenGL)parent.g;
pgl = primaryPG.pgl;
this.vertexURL = vertURL;
this.fragmentURL = fragURL;
this.vertexFilename = null;
this.fragmentFilename = null;
fragmentShaderSource = pgl.loadFragmentShader(fragURL);
vertexShaderSource = pgl.loadVertexShader(vertURL);
glProgram = 0;
glVertex = 0;
glFragment = 0;
intBuffer = PGL.allocateIntBuffer(1);
floatBuffer = PGL.allocateFloatBuffer(1);
int vertType = getShaderType(vertexShaderSource, -1);
int fragType = getShaderType(fragmentShaderSource, -1);
if (vertType == -1 && fragType == -1) {
type = PShader.POLY;
} else if (vertType == -1) {
type = fragType;
} else if (fragType == -1) {
type = vertType;
} else if (fragType == vertType) {
type = vertType;
} else {
PGraphics.showWarning(PGraphicsOpenGL.INCONSISTENT_SHADER_TYPES);
}
}
public PShader(PApplet parent, String[] vertSource, String[] fragSource) {
this.parent = parent;
primaryPG = (PGraphicsOpenGL)parent.g;
pgl = primaryPG.pgl;
this.vertexURL = null;
this.fragmentURL = null;
this.vertexFilename = null;
this.fragmentFilename = null;
vertexShaderSource = vertSource;
fragmentShaderSource = fragSource;
glProgram = 0;
glVertex = 0;
glFragment = 0;
intBuffer = PGL.allocateIntBuffer(1);
floatBuffer = PGL.allocateFloatBuffer(1);
int vertType = getShaderType(vertexShaderSource, -1);
int fragType = getShaderType(fragmentShaderSource, -1);
if (vertType == -1 && fragType == -1) {
type = PShader.POLY;
} else if (vertType == -1) {
type = fragType;
} else if (fragType == -1) {
type = vertType;
} else if (fragType == vertType) {
type = vertType;
} else {
PGraphics.showWarning(PGraphicsOpenGL.INCONSISTENT_SHADER_TYPES);
}
}
public void setVertexShader(String vertFilename) {
this.vertexFilename = vertFilename;
vertexShaderSource = pgl.loadVertexShader(vertFilename);
}
public void setVertexShader(URL vertURL) {
this.vertexURL = vertURL;
vertexShaderSource = pgl.loadVertexShader(vertURL);
}
public void setVertexShader(String[] vertSource) {
vertexShaderSource = vertSource;
}
public void setFragmentShader(String fragFilename) {
this.fragmentFilename = fragFilename;
fragmentShaderSource = pgl.loadFragmentShader(fragFilename);
}
public void setFragmentShader(URL fragURL) {
this.fragmentURL = fragURL;
fragmentShaderSource = pgl.loadFragmentShader(fragURL);
}
public void setFragmentShader(String[] fragSource) {
fragmentShaderSource = fragSource;
}
/**
* Initializes (if needed) and binds the shader program.
*/
public void bind() {
init();
if (!bound) {
pgl.useProgram(glProgram);
bound = true;
consumeUniforms();
bindTextures();
}
if (hasType()) bindTyped();
}
/**
* Unbinds the shader program.
*/
public void unbind() {
if (hasType()) unbindTyped();
if (bound) {
unbindTextures();
pgl.useProgram(0);
bound = false;
}
}
/**
* Returns true if the shader is bound, false otherwise.
*/
public boolean bound() {
return bound;
}
/**
* @webref rendering:shaders
* @brief Sets a variable within the shader
* @param name the name of the uniform variable to modify
* @param x first component of the variable to modify
*/
public void set(String name, int x) {
setUniformImpl(name, UniformValue.INT1, new int[] { x });
}
/**
* @param y second component of the variable to modify. The variable has to be declared with an array/vector type in the shader (i.e.: int[2], vec2)
*/
public void set(String name, int x, int y) {
setUniformImpl(name, UniformValue.INT2, new int[] { x, y });
}
/**
* @param z third component of the variable to modify. The variable has to be declared with an array/vector type in the shader (i.e.: int[3], vec3)
*/
public void set(String name, int x, int y, int z) {
setUniformImpl(name, UniformValue.INT3, new int[] { x, y, z });
}
/**
* @param w fourth component of the variable to modify. The variable has to be declared with an array/vector type in the shader (i.e.: int[4], vec4)
*/
public void set(String name, int x, int y, int z, int w) {
setUniformImpl(name, UniformValue.INT4, new int[] { x, y, z, w });
}
public void set(String name, float x) {
setUniformImpl(name, UniformValue.FLOAT1, new float[] { x });
}
public void set(String name, float x, float y) {
setUniformImpl(name, UniformValue.FLOAT2, new float[] { x, y });
}
public void set(String name, float x, float y, float z) {
setUniformImpl(name, UniformValue.FLOAT3, new float[] { x, y, z });
}
public void set(String name, float x, float y, float z, float w) {
setUniformImpl(name, UniformValue.FLOAT4, new float[] { x, y, z, w });
}
/**
* @param vec modifies all the components of an array/vector uniform variable. PVector can only be used if the type of the variable is vec3.
*/
public void set(String name, PVector vec) {
setUniformImpl(name, UniformValue.FLOAT3,
new float[] { vec.x, vec.y, vec.z });
}
public void set(String name, boolean x) {
setUniformImpl(name, UniformValue.INT1, new int[] { (x)?1:0 });
}
public void set(String name, boolean x, boolean y) {
setUniformImpl(name, UniformValue.INT2,
new int[] { (x)?1:0, (y)?1:0 });
}
public void set(String name, boolean x, boolean y, boolean z) {
setUniformImpl(name, UniformValue.INT3,
new int[] { (x)?1:0, (y)?1:0, (z)?1:0 });
}
public void set(String name, boolean x, boolean y, boolean z, boolean w) {
setUniformImpl(name, UniformValue.INT4,
new int[] { (x)?1:0, (y)?1:0, (z)?1:0, (w)?1:0 });
}
public void set(String name, int[] vec) {
set(name, vec, 1);
}
/**
* @param ncoords number of coordinates per element, max 4
*/
public void set(String name, int[] vec, int ncoords) {
if (ncoords == 1) {
setUniformImpl(name, UniformValue.INT1VEC, vec);
} else if (ncoords == 2) {
setUniformImpl(name, UniformValue.INT2VEC, vec);
} else if (ncoords == 3) {
setUniformImpl(name, UniformValue.INT3VEC, vec);
} else if (ncoords == 4) {
setUniformImpl(name, UniformValue.INT4VEC, vec);
} else if (4 < ncoords) {
PGraphics.showWarning("Only up to 4 coordinates per element are " +
"supported.");
} else {
PGraphics.showWarning("Wrong number of coordinates: it is negative!");
}
}
public void set(String name, float[] vec) {
set(name, vec, 1);
}
public void set(String name, float[] vec, int ncoords) {
if (ncoords == 1) {
setUniformImpl(name, UniformValue.FLOAT1VEC, vec);
} else if (ncoords == 2) {
setUniformImpl(name, UniformValue.FLOAT2VEC, vec);
} else if (ncoords == 3) {
setUniformImpl(name, UniformValue.FLOAT3VEC, vec);
} else if (ncoords == 4) {
setUniformImpl(name, UniformValue.FLOAT4VEC, vec);
} else if (4 < ncoords) {
PGraphics.showWarning("Only up to 4 coordinates per element are " +
"supported.");
} else {
PGraphics.showWarning("Wrong number of coordinates: it is negative!");
}
}
public void set(String name, boolean[] vec) {
set(name, vec, 1);
}
public void set(String name, boolean[] boolvec, int ncoords) {
int[] vec = new int[boolvec.length];
for (int i = 0; i < boolvec.length; i++) {
vec[i] = (boolvec[i])?1:0;
}
set(name, vec, ncoords);
}
/**
* @param mat matrix of values
*/
public void set(String name, PMatrix2D mat) {
float[] matv = { mat.m00, mat.m01,
mat.m10, mat.m11 };
setUniformImpl(name, UniformValue.MAT2, matv);
}
public void set(String name, PMatrix3D mat) {
set(name, mat, false);
}
/**
* @param use3x3 enforces the matrix is 3 x 3
*/
public void set(String name, PMatrix3D mat, boolean use3x3) {
if (use3x3) {
float[] matv = { mat.m00, mat.m01, mat.m02,
mat.m10, mat.m11, mat.m12,
mat.m20, mat.m21, mat.m22 };
setUniformImpl(name, UniformValue.MAT3, matv);
} else {
float[] matv = { mat.m00, mat.m01, mat.m02, mat.m03,
mat.m10, mat.m11, mat.m12, mat.m13,
mat.m20, mat.m21, mat.m22, mat.m23,
mat.m30, mat.m31, mat.m32, mat.m33 };
setUniformImpl(name, UniformValue.MAT4, matv);
}
}
/**
* @param tex sets the sampler uniform variable to read from this image texture
*/
public void set(String name, PImage tex) {
setUniformImpl(name, UniformValue.SAMPLER2D, tex);
}
/**
* Extra initialization method that can be used by subclasses, called after
* compiling and attaching the vertex and fragment shaders, and before
* linking the shader program.
*
*/
protected void setup() {
}
protected void draw(int idxId, int count, int offset) {
pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, idxId);
pgl.drawElements(PGL.TRIANGLES, count, PGL.INDEX_TYPE,
offset * PGL.SIZEOF_INDEX);
pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, 0);
}
/**
* Returns the ID location of the attribute parameter given its name.
*
* @param name String
* @return int
*/
protected int getAttributeLoc(String name) {
init();
return pgl.getAttribLocation(glProgram, name);
}
/**
* Returns the ID location of the uniform parameter given its name.
*
* @param name String
* @return int
*/
protected int getUniformLoc(String name) {
init();
return pgl.getUniformLocation(glProgram, name);
}
protected void setAttributeVBO(int loc, int vboId, int size, int type,
boolean normalized, int stride, int offset) {
if (-1 < loc) {
pgl.bindBuffer(PGL.ARRAY_BUFFER, vboId);
pgl.vertexAttribPointer(loc, size, type, normalized, stride, offset);
}
}
protected void setUniformValue(int loc, int x) {
if (-1 < loc) {
pgl.uniform1i(loc, x);
}
}
protected void setUniformValue(int loc, int x, int y) {
if (-1 < loc) {
pgl.uniform2i(loc, x, y);
}
}
protected void setUniformValue(int loc, int x, int y, int z) {
if (-1 < loc) {
pgl.uniform3i(loc, x, y, z);
}
}
protected void setUniformValue(int loc, int x, int y, int z, int w) {
if (-1 < loc) {
pgl.uniform4i(loc, x, y, z, w);
}
}
protected void setUniformValue(int loc, float x) {
if (-1 < loc) {
pgl.uniform1f(loc, x);
}
}
protected void setUniformValue(int loc, float x, float y) {
if (-1 < loc) {
pgl.uniform2f(loc, x, y);
}
}
protected void setUniformValue(int loc, float x, float y, float z) {
if (-1 < loc) {
pgl.uniform3f(loc, x, y, z);
}
}
protected void setUniformValue(int loc, float x, float y, float z, float w) {
if (-1 < loc) {
pgl.uniform4f(loc, x, y, z, w);
}
}
protected void setUniformVector(int loc, int[] vec, int ncoords,
int length) {
if (-1 < loc) {
updateIntBuffer(vec);
if (ncoords == 1) {
pgl.uniform1iv(loc, length, intBuffer);
} else if (ncoords == 2) {
pgl.uniform2iv(loc, length, intBuffer);
} else if (ncoords == 3) {
pgl.uniform3iv(loc, length, intBuffer);
} else if (ncoords == 4) {
pgl.uniform3iv(loc, length, intBuffer);
}
}
}
protected void setUniformVector(int loc, float[] vec, int ncoords,
int length) {
if (-1 < loc) {
updateFloatBuffer(vec);
if (ncoords == 1) {
pgl.uniform1fv(loc, length, floatBuffer);
} else if (ncoords == 2) {
pgl.uniform2fv(loc, length, floatBuffer);
} else if (ncoords == 3) {
pgl.uniform3fv(loc, length, floatBuffer);
} else if (ncoords == 4) {
pgl.uniform4fv(loc, length, floatBuffer);
}
}
}
protected void setUniformMatrix(int loc, float[] mat) {
if (-1 < loc) {
updateFloatBuffer(mat);
if (mat.length == 4) {
pgl.uniformMatrix2fv(loc, 1, false, floatBuffer);
} else if (mat.length == 9) {
pgl.uniformMatrix3fv(loc, 1, false, floatBuffer);
} else if (mat.length == 16) {
pgl.uniformMatrix4fv(loc, 1, false, floatBuffer);
}
}
}
protected void setUniformTex(int loc, Texture tex) {
if (texUnits != null) {
Integer unit = texUnits.get(loc);
if (unit != null) {
pgl.activeTexture(PGL.TEXTURE0 + unit);
tex.bind();
} else {
throw new RuntimeException("Cannot find unit for texture " + tex);
}
}
}
protected void setUniformImpl(String name, int type, Object value) {
if (uniformValues == null) {
uniformValues = new HashMap<String, UniformValue>();
}
uniformValues.put(name, new UniformValue(type, value));
}
protected void consumeUniforms() {
if (uniformValues != null && 0 < uniformValues.size()) {
int unit = 0;
for (String name: uniformValues.keySet()) {
int loc = getUniformLoc(name);
if (loc == -1) {
PGraphics.showWarning("The shader doesn't have a uniform called \"" +
name + "\" OR the uniform was removed during " +
"compilation because it was unused.");
continue;
}
UniformValue val = uniformValues.get(name);
if (val.type == UniformValue.INT1) {
int[] v = ((int[])val.value);
pgl.uniform1i(loc, v[0]);
} else if (val.type == UniformValue.INT2) {
int[] v = ((int[])val.value);
pgl.uniform2i(loc, v[0], v[1]);
} else if (val.type == UniformValue.INT3) {
int[] v = ((int[])val.value);
pgl.uniform3i(loc, v[0], v[1], v[2]);
} else if (val.type == UniformValue.INT4) {
int[] v = ((int[])val.value);
pgl.uniform4i(loc, v[0], v[1], v[2], v[3]);
} else if (val.type == UniformValue.FLOAT1) {
float[] v = ((float[])val.value);
pgl.uniform1f(loc, v[0]);
} else if (val.type == UniformValue.FLOAT2) {
float[] v = ((float[])val.value);
pgl.uniform2f(loc, v[0], v[1]);
} else if (val.type == UniformValue.FLOAT3) {
float[] v = ((float[])val.value);
pgl.uniform3f(loc, v[0], v[1], v[2]);
} else if (val.type == UniformValue.FLOAT4) {
float[] v = ((float[])val.value);
pgl.uniform4f(loc, v[0], v[1], v[2], v[3]);
} else if (val.type == UniformValue.INT1VEC) {
int[] v = ((int[])val.value);
updateIntBuffer(v);
pgl.uniform1iv(loc, v.length, intBuffer);
} else if (val.type == UniformValue.INT2VEC) {
int[] v = ((int[])val.value);
updateIntBuffer(v);
pgl.uniform2iv(loc, v.length / 2, intBuffer);
} else if (val.type == UniformValue.INT3VEC) {
int[] v = ((int[])val.value);
updateIntBuffer(v);
pgl.uniform3iv(loc, v.length / 3, intBuffer);
} else if (val.type == UniformValue.INT4VEC) {
int[] v = ((int[])val.value);
updateIntBuffer(v);
pgl.uniform4iv(loc, v.length / 4, intBuffer);
} else if (val.type == UniformValue.FLOAT1VEC) {
float[] v = ((float[])val.value);
updateFloatBuffer(v);
pgl.uniform1fv(loc, v.length, floatBuffer);
} else if (val.type == UniformValue.FLOAT2VEC) {
float[] v = ((float[])val.value);
updateFloatBuffer(v);
pgl.uniform2fv(loc, v.length / 2, floatBuffer);
} else if (val.type == UniformValue.FLOAT3VEC) {
float[] v = ((float[])val.value);
updateFloatBuffer(v);
pgl.uniform3fv(loc, v.length / 3, floatBuffer);
} else if (val.type == UniformValue.FLOAT4VEC) {
float[] v = ((float[])val.value);
updateFloatBuffer(v);
pgl.uniform4fv(loc, v.length / 4, floatBuffer);
} else if (val.type == UniformValue.MAT2) {
float[] v = ((float[])val.value);
updateFloatBuffer(v);
pgl.uniformMatrix2fv(loc, 1, false, floatBuffer);
} else if (val.type == UniformValue.MAT3) {
float[] v = ((float[])val.value);
updateFloatBuffer(v);
pgl.uniformMatrix3fv(loc, 1, false, floatBuffer);
} else if (val.type == UniformValue.MAT4) {
float[] v = ((float[])val.value);
updateFloatBuffer(v);
pgl.uniformMatrix4fv(loc, 1, false, floatBuffer);
} else if (val.type == UniformValue.SAMPLER2D) {
PImage img = (PImage)val.value;
Texture tex = currentPG.getTexture(img);
if (textures == null) textures = new HashMap<Integer, Texture>();
textures.put(loc, tex);
if (texUnits == null) texUnits = new HashMap<Integer, Integer>();
if (texUnits.containsKey(loc)) {
unit = texUnits.get(loc);
pgl.uniform1i(loc, unit);
} else {
texUnits.put(loc, unit);
pgl.uniform1i(loc, unit);
}
unit++;
}
}
uniformValues.clear();
}
}
protected void updateIntBuffer(int[] vec) {
intBuffer = PGL.updateIntBuffer(intBuffer, vec, false);
}
protected void updateFloatBuffer(float[] vec) {
floatBuffer = PGL.updateFloatBuffer(floatBuffer, vec, false);
}
protected void bindTextures() {
if (textures != null && texUnits != null) {
for (int loc: textures.keySet()) {
Texture tex = textures.get(loc);
Integer unit = texUnits.get(loc);
if (unit != null) {
pgl.activeTexture(PGL.TEXTURE0 + unit);
tex.bind();
} else {
throw new RuntimeException("Cannot find unit for texture " + tex);
}
}
}
}
protected void unbindTextures() {
if (textures != null && texUnits != null) {
for (int loc: textures.keySet()) {
Texture tex = textures.get(loc);
Integer unit = texUnits.get(loc);
if (unit != null) {
pgl.activeTexture(PGL.TEXTURE0 + unit);
tex.unbind();
} else {
throw new RuntimeException("Cannot find unit for texture " + tex);
}
}
pgl.activeTexture(PGL.TEXTURE0);
}
}
public void init() {
if (glProgram == 0 || contextIsOutdated()) {
create();
if (compile()) {
pgl.attachShader(glProgram, glVertex);
pgl.attachShader(glProgram, glFragment);
setup();
pgl.linkProgram(glProgram);
validate();
}
}
}
protected void create() {
context = pgl.getCurrentContext();
glres = new GLResourceShader(this);
}
protected boolean compile() {
boolean vertRes = true;
if (hasVertexShader()) {
vertRes = compileVertexShader();
} else {
PGraphics.showException("Doesn't have a vertex shader");
}
boolean fragRes = true;
if (hasFragmentShader()) {
fragRes = compileFragmentShader();
} else {
PGraphics.showException("Doesn't have a fragment shader");
}
return vertRes && fragRes;
}
protected void validate() {
pgl.getProgramiv(glProgram, PGL.LINK_STATUS, intBuffer);
boolean linked = intBuffer.get(0) == 0 ? false : true;
if (!linked) {
PGraphics.showException("Cannot link shader program:\n" +
pgl.getProgramInfoLog(glProgram));
}
pgl.validateProgram(glProgram);
pgl.getProgramiv(glProgram, PGL.VALIDATE_STATUS, intBuffer);
boolean validated = intBuffer.get(0) == 0 ? false : true;
if (!validated) {
PGraphics.showException("Cannot validate shader program:\n" +
pgl.getProgramInfoLog(glProgram));
}
}
protected boolean contextIsOutdated() {
boolean outdated = !pgl.contextIsCurrent(context);
if (outdated) {
dispose();
}
return outdated;
}
protected boolean hasVertexShader() {
return vertexShaderSource != null && 0 < vertexShaderSource.length;
}
protected boolean hasFragmentShader() {
return fragmentShaderSource != null && 0 < fragmentShaderSource.length;
}
/**
* @param shaderSource a string containing the shader's code
*/
protected boolean compileVertexShader() {
pgl.shaderSource(glVertex, PApplet.join(vertexShaderSource, "\n"));
pgl.compileShader(glVertex);
pgl.getShaderiv(glVertex, PGL.COMPILE_STATUS, intBuffer);
boolean compiled = intBuffer.get(0) == 0 ? false : true;
if (!compiled) {
PGraphics.showException("Cannot compile vertex shader:\n" +
pgl.getShaderInfoLog(glVertex));
return false;
} else {
return true;
}
}
/**
* @param shaderSource a string containing the shader's code
*/
protected boolean compileFragmentShader() {
pgl.shaderSource(glFragment, PApplet.join(fragmentShaderSource, "\n"));
pgl.compileShader(glFragment);
pgl.getShaderiv(glFragment, PGL.COMPILE_STATUS, intBuffer);
boolean compiled = intBuffer.get(0) == 0 ? false : true;
if (!compiled) {
PGraphics.showException("Cannot compile fragment shader:\n" +