-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchanges.txt
1194 lines (1173 loc) · 100 KB
/
changes.txt
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
diff --git a/graphics-by-opengl-j2se/src/main/java/com/nucleus/opengl/shader/GLShaderProgram.java b/graphics-by-opengl-j2se/src/main/java/com/nucleus/opengl/shader/GLShaderProgram.java
index 37f02b78..29c224b7 100644
--- a/graphics-by-opengl-j2se/src/main/java/com/nucleus/opengl/shader/GLShaderProgram.java
+++ b/graphics-by-opengl-j2se/src/main/java/com/nucleus/opengl/shader/GLShaderProgram.java
@@ -6,15 +6,11 @@ import java.io.StringReader;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
-import com.nucleus.BackendException;
import com.nucleus.SimpleLogger;
import com.nucleus.common.BufferUtils;
-import com.nucleus.common.Constants;
-import com.nucleus.common.StringUtils;
import com.nucleus.geometry.AttributeBuffer;
import com.nucleus.geometry.AttributeUpdater;
import com.nucleus.geometry.AttributeUpdater.BufferIndex;
@@ -24,29 +20,22 @@ import com.nucleus.opengl.GLES30Wrapper;
import com.nucleus.opengl.GLESWrapper;
import com.nucleus.opengl.GLESWrapper.GLES20;
import com.nucleus.opengl.GLESWrapper.GLES30;
-import com.nucleus.opengl.GLESWrapper.GLES31;
-import com.nucleus.opengl.GLESWrapper.GLES32;
-import com.nucleus.opengl.GLESWrapper.ProgramInfo;
import com.nucleus.opengl.GLException;
import com.nucleus.opengl.GLUtils;
-import com.nucleus.opengl.shader.ShaderSource.ESSLVersion;
-import com.nucleus.renderer.NucleusRenderer;
-import com.nucleus.renderer.NucleusRenderer.Matrices;
import com.nucleus.renderer.NucleusRenderer.Renderers;
import com.nucleus.renderer.Pass;
import com.nucleus.renderer.Window;
import com.nucleus.shader.BlockBuffer;
+import com.nucleus.shader.DefaultGraphicsShader;
import com.nucleus.shader.FloatBlockBuffer;
+import com.nucleus.shader.GraphicsShader;
import com.nucleus.shader.Shader;
-import com.nucleus.shader.ShaderProgram;
+import com.nucleus.shader.ShaderSource;
import com.nucleus.shader.ShaderVariable;
import com.nucleus.shader.ShaderVariable.InterfaceBlock;
import com.nucleus.shader.ShaderVariable.VariableType;
import com.nucleus.shader.VariableIndexer;
-import com.nucleus.texturing.Texture2D;
-import com.nucleus.texturing.TextureType;
import com.nucleus.texturing.TiledTexture2D;
-import com.nucleus.vecmath.Matrix;
/**
* This class handles loading, compiling and linking of OpenGL ES shader programs.
@@ -61,129 +50,24 @@ import com.nucleus.vecmath.Matrix;
*
*
*/
-public abstract class GLShaderProgram extends ShaderProgram implements Shader {
+public abstract class GLShaderProgram extends DefaultGraphicsShader implements Shader {
- public static final String PROGRAM_DIRECTORY = "assets/";
- /**
- * Shader suffix as added after checking for which version to use
- */
- public static final String FRAGMENT_TYPE_SUFFIX = ".fs";
- public static final String VERTEX_TYPE_SUFFIX = ".vs";
- public static final String GEOMETRY_TYPE_SUFFIX = ".gs";
- public static final String COMPUTE_TYPE_SUFFIX = ".cs";
-
- protected final static String MUST_SET_FIELDS = "Must set attributesPerVertex,vertexShaderName and fragmentShaderName";
protected final static String NO_ACTIVE_UNIFORMS = "No active uniforms, forgot to call createProgram()?";
- /**
- * The different type of programs that can be linked from different type of shaders.
- *
- */
- public enum ProgramType {
- VERTEX_FRAGMENT(),
- COMPUTE(),
- VERTEX_GEOMETRY_FRAGMENT();
- }
-
- /**
- * The different type of shaders
- *
- */
- public enum ShaderType {
- VERTEX(GLES20.GL_VERTEX_SHADER, 0, VERTEX_TYPE_SUFFIX),
- FRAGMENT(GLES20.GL_FRAGMENT_SHADER, 1, FRAGMENT_TYPE_SUFFIX),
- GEOMETRY(GLES32.GL_GEOMETRY_SHADER, 2, GEOMETRY_TYPE_SUFFIX),
- COMPUTE(GLES31.GL_COMPUTE_SHADER, 3, COMPUTE_TYPE_SUFFIX);
-
- public final int value;
- public final int index;
- public final String suffix;
-
- private ShaderType(int value, int index, String suffix) {
- this.value = value;
- this.index = index;
- this.suffix = suffix;
- }
-
- /**
- * Returns the enum from the GL shader type, eg GL_VERTEX_SHADER
- *
- * @param shader
- * @return
- */
- public static ShaderType getFromType(int shaderType) {
- for (ShaderType t : values()) {
- if (t.value == shaderType) {
- return t;
- }
- }
- return null;
- }
-
- }
-
protected GLShaderProgram shadowPass1;
protected GLShaderProgram shadowPass2;
- public final static String SHADER_SOURCE_ERROR = "Error setting shader source: ";
- public final static String COMPILE_SHADER_ERROR = "Error compiling shader: ";
- public final static String CREATE_SHADER_ERROR = "Can not create shader object, context not active?";
- public final static String ATTACH_SOURCE_ERROR = "Error attaching shader source";
- public final static String LINK_PROGRAM_ERROR = "Error linking program: ";
- public final static String VARIABLE_LOCATION_ERROR = "Could not get shader variable location: ";
- public final static String GET_PROGRAM_INFO_ERROR = "Error fetching program info.";
-
- /**
- * The GL program object
- */
- private int program = Constants.NO_VALUE;
- /**
- * Available after {@link #fetchProgramInfo(GLES20Wrapper)} has been called
- */
- private ProgramInfo info;
-
- private ShaderSource[] shaderSources;
- private int[] shaderNames;
-
- /**
- * active attributes
- */
- protected NamedShaderVariable[] activeAttributes;
- /**
- * active uniforms
- */
- protected NamedShaderVariable[] activeUniforms;
-
- protected HashMap<Integer, NamedShaderVariable> blockVariables = new HashMap<>(); // Active block uniforms, index is
- // the
- // uniform index from GL
- protected ArrayList<String>[] commonSources = new ArrayList[ShaderType.values().length];
-
/**
* Unmapped variable types
*/
protected List<Integer> unMappedTypes = new ArrayList<>();
- /**
- * Returns the name of the shader source - including relative source path BUT excluding vertex shader + extension.
- * for instance to load /asssets/line/flatvertex.essl this method shall return 'line/flat' for the shaderType
- * GL_VERTEX_SHADER
- *
- * Default behavior is to return the function path + shader source name
- *
- * @param shaderType The shader type to return source for
- * @return
- */
- protected String getShaderSourceName(ShaderType type) {
- return function.getShaderSourceName(type);
- }
-
protected GLShaderProgram(Pass pass, Shading shading, String category,
- GLShaderProgram.ProgramType shaders) {
+ GraphicsShader.ProgramType shaders) {
super(pass, shading, category, shaders);
}
- protected GLShaderProgram(Categorizer function, GLShaderProgram.ProgramType shaders) {
+ protected GLShaderProgram(Categorizer function, GraphicsShader.ProgramType shaders) {
super(function, shaders);
}
@@ -196,33 +80,6 @@ public abstract class GLShaderProgram extends ShaderProgram implements Shader {
return variableIndexer;
}
- /**
- * Creates an indexer from the {@link #activeAttributes} - only use this method of the indexer is not set.
- *
- * @return
- * @throws IllegalArgumentException If an indexer has been set by calling {@link #setIndexer(VariableIndexer)}
- */
- public VariableIndexer createIndexer() {
- if (variableIndexer != null) {
- throw new IllegalArgumentException("Indexer has been set.");
- }
- int size = activeAttributes.length;
- String[] names = new String[size];
- int[] offsets = new int[size];
- VariableType[] types = new VariableType[size];
- BufferIndex[] bufferIndexes = new BufferIndex[size];
- BufferIndex bufferIndex = BufferIndex.ATTRIBUTES;
- for (int i = 0; i < size; i++) {
- names[i] = activeAttributes[i].getName();
- offsets[i] = activeAttributes[i].getOffset();
- types[i] = activeAttributes[i].getType();
- // TODO - currently only supports using one buffer
- bufferIndexes[i] = bufferIndex;
- }
- return new NamedVariableIndexer(names, offsets, types, bufferIndexes,
- new int[] { getAttributesPerVertex(bufferIndex) });
- }
-
/**
* Loads the version correct shader sources for the sourceNames and types.
* The shader sourcenames will be versioned, when this method returns the shaders sourcecode can be fetched
@@ -242,134 +99,6 @@ public abstract class GLShaderProgram extends ShaderProgram implements Shader {
}
}
- /**
- * Sets the name of the shaders in this program and returns an array of {@link ShaderSource}, normally 2 - one for
- * vertex and one for fragment-shader.
- * This method must be called before the program is created.
- *
- * @param version Highest level of GL that is supported
- */
- protected ShaderSource[] createShaderSource(Renderers version) {
- ShaderSource[] sources = null;
- ShaderType[] shaderTypes;
- switch (shaders) {
- case VERTEX_FRAGMENT:
- sources = new ShaderSource[2];
- shaderTypes = new ShaderType[] { ShaderType.VERTEX, ShaderType.FRAGMENT };
- break;
- case COMPUTE:
- sources = new ShaderSource[1];
- shaderTypes = new ShaderType[] { ShaderType.COMPUTE };
- break;
- case VERTEX_GEOMETRY_FRAGMENT:
- sources = new ShaderSource[3];
- shaderTypes = new ShaderType[] { ShaderType.VERTEX, ShaderType.GEOMETRY,
- ShaderType.FRAGMENT };
- break;
- default:
- throw new IllegalArgumentException("Not implemented for " + shaders);
- }
- for (int i = 0; i < shaderTypes.length; i++) {
- sources[i] = getShaderSource(version, shaderTypes[i]);
- }
- return sources;
- }
-
- /**
- * Returns the name of the shader source for the specified type, this is taken from the function using pass, shading
- * and category.
- * Override in subclasses to point to other shader source
- *
- * @param version Highest GL version that is supported, used to fetch versioned source name.
- * @param type The shader type to return source for
- * @return
- */
- protected ShaderSource getShaderSource(Renderers version, ShaderType type) {
-
- String sourceNameVersion = getSourceNameVersion(version, type.value);
- switch (type) {
- case VERTEX:
- return new ShaderSource(PROGRAM_DIRECTORY + sourceNameVersion + getShaderSourceName(type),
- type);
- case FRAGMENT:
- return new ShaderSource(PROGRAM_DIRECTORY + sourceNameVersion + getShaderSourceName(type),
- type);
- case COMPUTE:
- return new ShaderSource(PROGRAM_DIRECTORY + sourceNameVersion + getShaderSourceName(type),
- type);
- case GEOMETRY:
- return new ShaderSource(PROGRAM_DIRECTORY + sourceNameVersion + getShaderSourceName(type),
- type);
- default:
- throw new IllegalArgumentException("Not implemented for type: " + type);
-
- }
- }
-
- /**
- * Called by {@link #getShaderSource(Renderers, int)} to append shader (ESSL) version to sourcename.
- * This is used to be able to load different shader sources depending on if GLES major version is less than 3.
- * Override this if different source shall be used depending on available renderer/shader version.
- * Default is to append _v300 if GLES version is 3 or above.
- *
- * @param version Highest GL version that is supported
- * @param type Shader type, GL_VERTEX_SHADER, GL_FRAGMENT_SHADER, GL_COMPUTE_SHADER
- * @return Empty string "", or shader version to append to source name if different shader source shall be used for
- * a specific shader version.
- */
- protected String getSourceNameVersion(Renderers version, int type) {
- switch (version) {
- case GLES20:
- return ShaderSource.V200 + "/";
- case GLES30:
- case GLES31:
- case GLES32:
- return ShaderSource.V300 + "/";
- default:
- throw new IllegalArgumentException("Not implemented for " + version);
- }
- }
-
- public GLShaderProgram createProgram(NucleusRenderer renderer) throws BackendException {
- shaderSources = createShaderSource(GLES20Wrapper.getInfo().getRenderVersion());
- if (shaderSources == null) {
- throw new ShaderProgramException(MUST_SET_FIELDS);
- }
- createProgram(renderer, shaderSources);
- return this;
- }
-
- /**
- * Sorts the attributes used based on BufferIndex - attribute variables are sorted based on buffer in the specified
- * result array.
- * Finds the shader attribute variables per buffer using VariableMapping, iterate through defined (by subclasses)
- * attribute variable mapping.
- * Put the result in the result array and set the {@linkplain NamedShaderVariable} offset based on used attributes.
- *
- * TODO Add check for mismatch of size, ie if ShaderVariables has one variable as float3 and it is defined is
- * program as float4 then raise error.
- *
- * @param resultArray Array to store shader variables for each attribute buffer in, attributes for buffer 1 will go
- * at index 0.
- */
- private void sortAttributeVariablePerBuffer(ShaderVariable[][] resultArray) {
- if (variableIndexer == null) {
- // If indexer not specified then use one buffer.
- resultArray[defaultDynamicAttribBuffer.index] = new NamedShaderVariable[info
- .getActiveVariables(VariableType.ATTRIBUTE)];
- if (resultArray[defaultDynamicAttribBuffer.index].length != activeAttributes.length) {
- throw new IllegalArgumentException("Active variable array size mismatch - active count from info "
- + info.getActiveVariables(VariableType.ATTRIBUTE) + ", array size "
- + activeAttributes.length);
- }
- resultArray[BufferIndex.ATTRIBUTES.index] = activeAttributes;
- } else {
- for (int index = 0; index < resultArray.length; index++) {
- resultArray[index] = sortByBuffer(variableIndexer, activeAttributes, index);
- }
- }
- }
-
/**
* Sort the variables belonging to the specified buffer index. Returning an array with the variables.
*
@@ -412,95 +141,6 @@ public abstract class GLShaderProgram extends ShaderProgram implements Shader {
}
}
- /**
- * Returns the number of attributes per vertex that are used by the program.
- *
- * @param buffer The buffer to get attributes per vertex for
- * @return Number of attributes as used by this program, per vertex 0 or -1 if not defined.
- */
- private int getAttributesPerVertex(BufferIndex buffer) {
- if (attributesPerVertex.length > buffer.index) {
- return attributesPerVertex[buffer.index];
- } else {
- // No attribute buffer for this program.
- return -1;
- }
- }
-
- /**
- * Returns an array with number of attributes used per vertex, for each attribute buffer that is used by this
- * program.
- * This is the minimal storage that this program needs per vertex.
- *
- * @return
- */
- public int[] getAttributeSizes() {
- int[] attributeSize = new int[attributeBufferCount];
- for (BufferIndex index : BufferIndex.values()) {
- if (index.index >= attributeSize.length) {
- break;
- }
- attributeSize[index.index] = getAttributesPerVertex(index);
- }
- return attributeSize;
- }
-
- /**
- * Internal method, creates the array storage for for uniform samplers, sampler usage is specific to program
- * and does not need to be stored in mesh.
- *
- */
- protected void createSamplerStorage() {
- if (activeUniforms == null) {
- throw new IllegalArgumentException(NO_ACTIVE_UNIFORMS);
- }
- int samplerSize = 0;
- samplerSize = getSamplerSize(activeUniforms);
- if (samplerSize > 0) {
- createSamplers(samplerSize);
- } else {
- SimpleLogger.d(getClass(), "No samplers used");
- }
- }
-
- /**
- * Creates array store for uniform data that fits this shader program.
- * - try to use uniform blocks as much as possible instead of separate uniforms
- *
- * @return
- */
- protected FloatBuffer createUniformArray() {
- if (activeUniforms == null) {
- throw new IllegalArgumentException(NO_ACTIVE_UNIFORMS);
- }
- int uniformSize = getVariableSize(activeUniforms, VariableType.UNIFORM);
- return BufferUtils.createFloatBuffer(uniformSize);
- }
-
- /**
- * Creates the block (uniform) buffers needed for this program, if any are used.
- * Always creates a {@link FloatBlockBuffer} for uniforms.
- * If BlockBuffer is needed for other type than uniforms it needs to be implemented.
- * Binds uniform block to a binding point using the block index, this means that there is one binding point
- * per uniform block.
- *
- * @param renderer
- * @return Uniform variable block buffers, using buffer objects, for this program, or null if not used.
- */
- protected BlockBuffer[] createUniformBlockBuffers(NucleusRenderer renderer) throws BackendException {
- if (uniformInterfaceBlocks == null) {
- return null;
- }
- for (InterfaceBlock block : uniformInterfaceBlocks) {
- GLES30Wrapper gles = (GLES30Wrapper) renderer.getBackend();
- // Here the binding point and block index is the same.
- gles.glUniformBlockBinding(program, block.blockIndex, block.blockIndex);
- }
- BlockBuffer[] buffers = BlockBuffer.createBlockBuffers(uniformInterfaceBlocks);
- renderer.getBufferFactory().createUBOs(buffers);
- return buffers;
- }
-
/**
* Set the attribute pointer(s) using the data in the vertexbuffer, this shall make the necessary calls to
* set the pointers for used attributes, enable pointers as needed.
@@ -520,111 +160,6 @@ public abstract class GLShaderProgram extends ShaderProgram implements Shader {
}
}
- /**
- * Uploads the uniforms to render backend
- * When this method returns the uniform data has been uploaded to GL and is ready.
- *
- * @param gles
- */
- public void uploadUniforms(GLES20Wrapper gles)
- throws GLException {
- uploadUniforms(gles, uniforms, activeUniforms);
- }
-
- /**
- * Uploads uniforms to GL, float array data is uploaded and if blockbuffer is used it is bound.
- * If uniform block buffer is dirty and uses UBO the data is first uploaded to buffer object then uniform block is
- * bound.
- *
- *
- * @param gles
- * @param uniformData
- * @param activeUniforms
- * @throws GLException
- */
- protected void uploadUniforms(GLES20Wrapper gles, FloatBuffer uniformData, ShaderVariable[] activeUniforms)
- throws GLException {
-
- for (ShaderVariable v : activeUniforms) {
- // If null then declared in program but not used, silently ignore
- if (v != null) {
- if (v.getBlockIndex() != Constants.NO_VALUE) {
- setUniformBlock((GLES30Wrapper) gles, uniformBlockBuffers[v.getBlockIndex()], v);
- } else {
- uploadUniform(gles, uniformData, v);
- }
- }
- }
- }
-
- /**
- * Prepares a texture used before rendering starts.
- * This shall set texture parameters to used textures, ie activate texture, bind texture then set parameters.
- * TODO - This should be moved to a class that handles nucleus texture/mesh
- *
- * @param renderer
- * @param texture
- * @throws BackendException
- */
- @Deprecated
- public void prepareTexture(NucleusRenderer renderer, Texture2D texture) throws BackendException {
- if (texture == null || texture.getTextureType() == TextureType.Untextured) {
- return;
- }
- /**
- * TODO - make texture names into enums
- */
- int unit = samplers.get(getUniformByName("uTexture").getOffset());
- renderer.prepareTexture(texture, unit);
- }
-
- /**
- * Fetches the program info and stores in this class.
- * This will read active attribute and uniform names, call this after the program has been compiled and linked.
- *
- * @param gles
- * @throws GLException If attribute or uniform locations could not be found.
- */
- protected void fetchProgramInfo(GLES20Wrapper gles) throws GLException {
- info = gles.getProgramInfo(program);
- GLUtils.handleError(gles, GET_PROGRAM_INFO_ERROR);
- activeAttributes = new NamedShaderVariable[info.getActiveVariables(VariableType.ATTRIBUTE)];
- activeUniforms = new NamedShaderVariable[info.getActiveVariables(VariableType.UNIFORM)];
- int uniformBlockCount = info.getActiveVariables(VariableType.UNIFORM_BLOCK);
- if (uniformBlockCount > 0) {
- uniformInterfaceBlocks = gles.getUniformBlocks(info);
- for (InterfaceBlock block : uniformInterfaceBlocks) {
- fetchActiveVariables(gles, VariableType.UNIFORM_BLOCK, info, block);
- }
- }
- fetchActiveVariables(gles, VariableType.ATTRIBUTE, info, null);
- fetchActiveVariables(gles, VariableType.UNIFORM, info, null);
- attributeVariables = new ShaderVariable[attributeBufferCount][];
- attributesPerVertex = new int[attributeBufferCount];
- paddingPerVertex = new int[attributeBufferCount];
- sortAttributeVariablePerBuffer(attributeVariables);
- }
-
- /**
- * Maps shader variables to defined offsets or updates the shader variable offsets to runtime values
- *
- * @param gles
- * @indexer Offsets to use for shader variables, if specified active variables will be mapped to offsets
- * as found when comparing variable name.
- * If null then offsets will be updated according to used variables in an increasing manner and will be tightly
- * packed.
- * @throws GLException
- */
- protected void mapAttributeOffsets(GLES20Wrapper gles, NamedVariableIndexer indexer) throws GLException {
- if (indexer == null) {
- dynamicMapOffsets();
- } else {
- setVariableOffsets(gles, activeAttributes, indexer);
- // Map the used uniforms from all used shader variables.
- dynamicMapShaderOffset(activeUniforms, VariableType.UNIFORM);
- }
- }
-
/**
* Use the offset as specified in the indexer and update or set the offset in program variables.
* Use this when the offset mapping of variables shall be controlled, for instance by a shared program.
@@ -644,138 +179,6 @@ public abstract class GLShaderProgram extends ShaderProgram implements Shader {
}
}
- /**
- * Dynamically sets used shader variable offsets, for ATTRIBUTES and UNIFORMS
- * The offset will be tightly packed based on used variable size, the order of used variables will be the same.
- */
- private void dynamicMapOffsets() {
- for (ShaderVariable[] sv : attributeVariables) {
- // In case only attribute buffer is used the first index will be null.
- if (sv != null) {
- dynamicMapShaderOffset(sv, VariableType.ATTRIBUTE);
- }
- }
- // Map the used uniforms from all used shader variables.
- dynamicMapShaderOffset(activeUniforms, VariableType.UNIFORM);
- }
-
- /**
- * Fetches active variables info of the specified type (attribute or uniform) and calls implementing shader program
- * class to store the shader variable.
- * This will create the mapping between the specific shader program and the location of shader variables
- * so that each shader variable can be indexed using the implementing shader program.
- *
- * @param gles
- * @param type Type of variable to fetch info for.
- * @param info
- * @param block Variable block info or null - store block variables here
- * @throws GLException If attribute or uniform location(s) are -1, ie they could not be found using the name.
- */
- private void fetchActiveVariables(GLES20Wrapper gles, VariableType type, ProgramInfo info, InterfaceBlock block)
- throws GLException {
- // If interface block the count is number of indices in block
- int count = block == null ? info.getActiveVariables(type) : block.indices.length;
- if (count == 0) {
- return;
- }
- // If type is uniform block then query max length of uniform name
- VariableType infoType = type != ShaderVariable.VariableType.UNIFORM_BLOCK ? type
- : VariableType.UNIFORM;
- byte[] nameBuffer = new byte[info.getMaxNameLength(infoType)];
- NamedShaderVariable variable = null;
- for (int i = 0; i < count; i++) {
- variable = null;
- if (block != null) {
- variable = gles.getActiveVariable(program, type, block.indices[i], nameBuffer);
- // Add to current block uniforms
- this.blockVariables.put(variable.getActiveIndex(), variable);
- addShaderVariable(variable);
- } else {
- // Check if uniform variable already has been fetched from block - then check that it is used in type of
- // shader.
- variable = getBlockVariable(type, i);
- if (variable != null) {
- SimpleLogger.d(getClass(),
- type.name() + " using block variable for index " + i + ", " + variable.getName());
- } else {
- variable = gles.getActiveVariable(program, type, i, nameBuffer);
- setVariableLocation(gles, program, variable);
- addShaderVariable(variable);
- }
- }
- }
- }
-
- /**
- * Returns ShaderVariable from variable blocks, if the block variable is used in the type.
- *
- * @param type Shader type
- * @param index
- * @return
- */
- protected NamedShaderVariable getBlockVariable(VariableType type, int index) {
- NamedShaderVariable var = blockVariables.get(index);
- if (var != null) {
- InterfaceBlock block = uniformInterfaceBlocks[var.getBlockIndex()];
- switch (block.usage) {
- case VERTEX_SHADER:
- return type == VariableType.UNIFORM ? var : null;
- case FRAGMENT_SHADER:
- return type == VariableType.ATTRIBUTE ? var : null;
- case VERTEX_FRAGMENT_SHADER:
- return var;
- }
- }
- return null;
- }
-
- /**
- * Fetch the GLES shader variable location (name) and set in the ShaderVariable
- * This is the name (int) value to use to access the variable in GL
- *
- * @param gles
- * @param program
- * @param variable
- * @throws GLException
- */
- protected void setVariableLocation(GLES20Wrapper gles, int program, NamedShaderVariable variable)
- throws GLException {
- switch (variable.getType()) {
- case ATTRIBUTE:
- variable.setLocation(gles.glGetAttribLocation(program, variable.getName()));
- break;
- case UNIFORM:
- variable.setLocation(gles.glGetUniformLocation(program, variable.getName()));
- break;
- case UNIFORM_BLOCK:
- // Location is already set - do nothing.
- break;
- }
- if (variable.getLocation() < 0) {
- throw new GLException(VARIABLE_LOCATION_ERROR + variable.getName(), 0);
- }
- }
-
- /**
- * Returns the active shader uniform by name, or null if not found
- *
- * @param uniform Name of uniform to return
- * @return
- */
- public NamedShaderVariable getUniformByName(String uniform) {
- return getVariableByName(uniform, activeUniforms);
- }
-
- /**
- * Returns the active shader attribute by name, or null if not found
- *
- * @param attrib Name of attribute to return
- * @return
- */
- public NamedShaderVariable getAttributeByName(String attrib) {
- return getVariableByName(attrib, activeAttributes);
- }
-
protected NamedShaderVariable getVariableByName(String name, NamedShaderVariable[] variables) {
for (NamedShaderVariable v : variables) {
if (v != null && v.getName().contentEquals(name)) {
@@ -785,37 +188,6 @@ public abstract class GLShaderProgram extends ShaderProgram implements Shader {
return null;
}
- /**
- * Links the specified vertex and fragment shaders to the specified program.
- *
- * @param gles GLES20 platform specific wrapper.
- * @param program
- * @param shaderNames
- * @throws GLException If the program could not be linked with the shaders.
- */
- public void linkProgram(GLES20Wrapper gles, int program, int[] shaderNames)
- throws GLException {
- for (int name : shaderNames) {
- gles.glAttachShader(program, name);
- }
- GLUtils.handleError(gles, ATTACH_SOURCE_ERROR);
- gles.glLinkProgram(program);
- SimpleLogger.d(getClass(), gles.glGetProgramInfoLog(program));
- GLUtils.handleError(gles, LINK_PROGRAM_ERROR);
- }
-
- private void logShaderSources(GLES20Wrapper gles, int[] shaderNames) {
- SimpleLogger.d(getClass(), "Common vertex shaders:");
- // It could be an exception before shader names are allocated
- if (shaderNames != null) {
- int index = 1;
- for (int name : shaderNames) {
- SimpleLogger.d(getClass(), "Shader source for shader " + index++ + " : " + toString());
- logNumberedShaderSource(gles, name);
- }
- }
- }
-
/**
* Logs the shader source for the specified shader, using numbered lines.
*
@@ -835,55 +207,6 @@ public abstract class GLShaderProgram extends ShaderProgram implements Shader {
SimpleLogger.d(getClass(), System.lineSeparator() + sb.toString());
}
- /**
- * Creates the shader name, attaches the source and compiles the shader.
- *
- * @param gles
- * @param source
- * @param library true if this is not the main shader
- * @return The created shader
- * @throws GLException If there is an error setting or calling to compiling shader source.
- * @throws GLCompilerException If compilation failed
- */
- public int compileShader(GLES20Wrapper gles, ShaderSource source) throws GLException, GLCompilerException {
- int shader = gles.glCreateShader(source.type.value);
- if (shader == 0) {
- throw new GLException(CREATE_SHADER_ERROR, GLES20.GL_NO_ERROR);
- }
- source.appendSource(ShaderSource.PRECISION, getCommonSources(source.type));
- source.insertDefines(ShaderSource.PRECISION, getDefines(source.type));
- compileShader(gles, source, shader);
- return shader;
- }
-
- /**
- * Compiles the shader from the shader source.
- * The GL version will be appended to the source, calling {@link GLESWrapper#getShaderVersion()}
- *
- * @param gles GLES20 platform specific wrapper.
- * @param source The shader source
- * @param shader OpenGL object to compile the shader to.
- * @throws GLException If there is an error setting or calling to compile shader source
- * @throws GLCompilerException If compilation failed
- */
- public void compileShader(GLES20Wrapper gles, ShaderSource source, int shader)
- throws GLException, GLCompilerException {
- String sourceStr = null;
- if (source.getSource() == null) {
- throw new IllegalArgumentException("Shader source is null for " + source.getFullSourceName());
- }
- sourceStr = source.getVersionedShaderSource();
- // These calls only return an error if there is an error in the parameters, invalid shader etc.
- // ie it does not mean compilation is successful - check compile status to know.
- gles.glShaderSource(shader, sourceStr);
- GLUtils.handleError(gles, SHADER_SOURCE_ERROR + source.getFullSourceName());
- gles.glCompileShader(shader);
- GLUtils.handleError(gles, COMPILE_SHADER_ERROR + source.getFullSourceName());
-
- // Check compilation status to know if compilation was success or failure
- checkCompileStatus(gles, source, shader);
- }
-
/**
* Checks the compile status of the specified shader program - if shader is not successfully compiled an exception
* is thrown.
@@ -901,172 +224,6 @@ public abstract class GLShaderProgram extends ShaderProgram implements Shader {
}
}
- /**
- * Checks the link status of the specified program - if link status returns false then an exception is thrown.
- *
- * @param gles
- * @param program The program to check link status on
- * @throws GLException
- */
- public void checkLinkStatus(GLES20Wrapper gles, int program) throws GLException {
- int[] linkStatus = new int[1];
- gles.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
- if (linkStatus[0] != GLES20.GL_TRUE) {
- throw new GLException(LINK_PROGRAM_ERROR + Integer.toString(program));
- }
- }
-
- /**
- * Validates the program - only call this in debug mode.
- * Set uniform and texture data before calling this method.
- *
- * @param gles
- */
- public void validateProgram(GLES20Wrapper gles) {
- gles.glValidateProgram(program);
- String result = gles.glGetProgramInfoLog(program);
- int[] status = new int[1];
- gles.glGetProgramiv(program, GLES20.GL_VALIDATE_STATUS, status, 0);
- if (status[0] != GLES20.GL_TRUE) {
- SimpleLogger.d(getClass(), "Could not validate program\n");
- SimpleLogger.d(getClass(), result);
- throw new IllegalArgumentException("Could not validate program:\n" + result);
- } else {
- SimpleLogger.d(getClass(), "Program " + program + " validated OK.");
- }
- }
-
- /**
- * Returns the program object for this shader program.
- *
- * @return The program object.
- */
- public int getProgram() {
- return program;
- }
-
- /**
- * Stores the shader variable in this program, if variable is of unmapped type, for instance Sampler, then it is
- * skipped. Also skip variables that are defined in code but not used in shader.
- * Variables are stored in {@link #activeUniforms} or {@link #activeAttributes}
- *
- * @param variable
- * @throws IllegalArgumentException If shader variables are null, the program has probably not been created,
- * or if a variable has no mapping in the code.
- */
- protected void addShaderVariable(NamedShaderVariable variable) {
- // If variable type is is unMappedTypes then skip, for instance texture
- if (unMappedTypes.contains(variable.getDataType())) {
- return;
- }
- setShaderVariable(variable);
- }
-
- /**
- * Sets the active shader variable into {@link NamedShaderVariable} array - call this when variable has been
- * validated.
- *
- * @param variable
- */
- protected void setShaderVariable(NamedShaderVariable variable) {
- switch (variable.getType()) {
- case ATTRIBUTE:
- activeAttributes[variable.getActiveIndex()] = variable;
- break;
- case UNIFORM:
- if (activeUniforms[variable.getActiveIndex()] != null) {
- throw new IllegalArgumentException("Not null");
- }
- activeUniforms[variable.getActiveIndex()] = variable;
- break;
- case UNIFORM_BLOCK:
- if (activeUniforms[variable.getActiveIndex()] != null) {
- throw new IllegalArgumentException("Not null");
- }
- activeUniforms[variable.getActiveIndex()] = variable;
- break;
- default:
- // DO NOTHING
- }
- }
-
- /**
- * Utility method to create the vertex and shader program using the specified shader names.
- * The shaders will be loaded, compiled and linked.
- * Vertex shader, fragment shader and program objects will be created.
- * If program compiles successfully then the program info is fetched.
- *
- * @param renderer
- * @param sources Name of shaders to load, compile and link
- * @throws BackendException If program could not be compiled and linked
- */
- protected void createProgram(NucleusRenderer renderer, ShaderSource[] sources) throws BackendException {
- SimpleLogger.d(getClass(),
- "Creating program for: " + sources.length + " shaders in program " + getClass().getSimpleName()
- + ", sources are:");
- for (ShaderSource ss : sources) {
- SimpleLogger.d(getClass(), ss.getFullSourceName());
- }
- GLES20Wrapper gles = (GLES20Wrapper) renderer.getBackend();
- try {
- loadShaderSources(gles, sources);
- createCommonShaders(gles, sources);
- ESSLVersion minVersion = ShaderSource.getMinVersion(sources);
- shaderNames = new int[sources.length];
- program = gles.glCreateProgram();
- for (int shaderIndex = 0; shaderIndex < sources.length; shaderIndex++) {
- // Insert the correct version depending on platform implementation.
- sources[shaderIndex].setShaderVersion(gles.replaceShaderVersion(minVersion));
-
- SimpleLogger.d(getClass(),
- "Compiling " + sources[shaderIndex].getFullSourceName());
- shaderNames[shaderIndex] = compileShader(gles, sources[shaderIndex]);
-
- }
- linkProgram(gles, program, shaderNames);
- checkLinkStatus(gles, program);
- fetchProgramInfo(gles);
- mapAttributeOffsets(gles, (NamedVariableIndexer) variableIndexer);
- setAttributesPerVertex();
- uniforms = createUniformArray();
- if (GLES20Wrapper.getInfo().getRenderVersion().major >= 3) {
- uniformBlockBuffers = createUniformBlockBuffers(renderer);
- }
- createSamplerStorage();
- setSamplers();
- initUniformData(uniforms);
- } catch (GLCompilerException e) {
- logNumberedShaderSource(gles, e.shader);
- SimpleLogger.d(getClass(), e.getMessage() + " from source:" + System.lineSeparator());
- throw e;
- } catch (BackendException e) {
- logShaderSources(gles, shaderNames);
- throw e;
- } catch (IOException e) {
- throw new GLException(e.toString(), -1);
- }
- }
-
- /**
- * Fetch the storage requirements for each vertex, per buffer
- */
- protected void setAttributesPerVertex() {
- if (variableIndexer == null) {
- // Fetch packed size for uniform/attribute data
- for (int i = 0; i < attributeBufferCount; i++) {
- // If only attribute buffer is used the first array index will be null
- if (attributeVariables[i] != null) {
- attributesPerVertex[i] = getVariableSize(attributeVariables[i], VariableType.ATTRIBUTE);
- }
- }
- } else {
- for (int i = 0; i < variableIndexer.getSizesPerVertex().length; i++) {
- attributesPerVertex[i] = variableIndexer.getSizePerVertex(i);
- }
- }
-
- }
-
/**
* Sets the uniform data into the block - if BlockBuffer is dirty the UBO is updated.
*
@@ -1154,63 +311,11 @@ public abstract class GLShaderProgram extends ShaderProgram implements Shader {
if (GLUtils.handleError(gles,
"setUniform: " + variable.getLocation() + ", dataType: " + variable.getDataType() +
", size " + variable.getSize())) {
- // Log shader sourcenames
- StringBuffer strBuffer = new StringBuffer();
- for (ShaderSource s : shaderSources) {
- strBuffer.append(s.getFullSourceName() + " : ");
- }
- SimpleLogger.d(getClass(), strBuffer.toString());
- }
-
- }
-
- /**
- * Returns the size of all the shader variables of the specified type, either ATTRIBUTE or UNIFORM
- * EXCLUDING the size of Sampler2D variables.
- * For uniforms this corresponds to the total size buffer size needed - the size of Sampler2D variables.
- * For attributes this corresponds to the total buffer size needed, normally attribute data is put in
- * dynamic and static buffers.
- *
- * @param variables
- * @param type
- * @param index BufferIndex to the buffer that the variables belong to, or null
- * @return Total size, in floats, of all defined shader variables of the specified type
- */
- protected int getVariableSize(ShaderVariable[] variables, VariableType type) {
- int size = 0;
- for (ShaderVariable v : variables) {
- if (v != null && v.getType() == type && v.getDataType() != GLES20.GL_SAMPLER_2D) {
- size += v.getSizeInFloats();