Skip to content

Commit 330add2

Browse files
committed
jacadoc for shaderregistry
1 parent 7f6016e commit 330add2

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

jplotter/src/main/java/hageldave/jplotter/util/ShaderRegistry.java

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,45 @@
55

66
import hageldave.jplotter.canvas.FBOCanvas;
77
import hageldave.jplotter.gl.Shader;
8-
import hageldave.jplotter.renderers.LinesRenderer;
8+
import hageldave.jplotter.util.Annotations.GLContextRequired;
99

10-
public class ShaderRegistry {
10+
/**
11+
* The ShaderRegistry class is a statically accessed class for keeping track of {@link Shader}s.
12+
* To avoid the creation of duplicate shaders in the same GL context, this class can be used to
13+
* easily allocate or get shaders shared by different objects.
14+
* <p>
15+
* Shaders are identified by context (canvasID) and a label, and they are obtained through the
16+
* {@link #getOrCreateShader(String, Supplier)} method.
17+
* When the shader is no longer in use by the object it has to be handed back to this class
18+
* through {@link #handbackShader(Shader)} which will close it if no longer in use by any other object.
19+
* <p>
20+
* Each shader in the registry is reference counted to determine if a registered shader is in use or not.
21+
* {@link #getOrCreateShader(String, Supplier)} increments the reference count, {@link #handbackShader(Shader)}
22+
* decrements the reference count.
23+
*
24+
* @author hageldave
25+
*/
26+
public final class ShaderRegistry {
1127

12-
static final HashMap<Integer,HashMap<String, Pair<Shader,int[]>>> context2label2shader = new HashMap<>();
13-
static final HashMap<Shader, Pair<Integer, String>> shader2contextAndLabel = new HashMap<>();
28+
private static final HashMap<Integer,HashMap<String, Pair<Shader,int[]>>> context2label2shader = new HashMap<>();
29+
private static final HashMap<Shader, Pair<Integer, String>> shader2contextAndLabel = new HashMap<>();
1430

31+
32+
private ShaderRegistry(){/* statically accessed singleton */}
33+
34+
/**
35+
* Returns the desired shader.
36+
* If a Shader with the provided label in the current GL context is already registered,
37+
* it will be returned and its reference count incremented.
38+
* Otherwise a new shader will be allocated using the specified supplier and registered.
39+
*
40+
* @param label of the shader
41+
* @param shadermaker supplier (constructor/factory) of the shader in case it was not yet registered.
42+
* @return shader corresponding to specified label and current context.
43+
*
44+
* @throws IllegalStateException when no context is active (FBOCanvas.CURRENTLY_ACTIVE_CANVAS == 0)
45+
*/
46+
@GLContextRequired
1547
public static Shader getOrCreateShader(String label, Supplier<Shader> shadermaker){
1648
int canvasid = FBOCanvas.CURRENTLY_ACTIVE_CANVAS;
1749
if(canvasid == 0){
@@ -37,6 +69,13 @@ public static Shader getOrCreateShader(String label, Supplier<Shader> shadermake
3769
return shaderref.first;
3870
}
3971

72+
/**
73+
* Hands back the specified shader, signaling it is no longer in use by the caller.
74+
* This decrements the reference count of the specified shader in the registry.
75+
* When the reference count drops to 0, the shader is closed (destroyed).
76+
* @param shader to be handed back.
77+
*/
78+
@GLContextRequired
4079
public static void handbackShader(Shader shader){
4180
int canvasid = FBOCanvas.CURRENTLY_ACTIVE_CANVAS;
4281
if(canvasid == 0){
@@ -63,7 +102,6 @@ public static void handbackShader(Shader shader){
63102
label2shader.remove(pair.second);
64103
shader2contextAndLabel.remove(shaderref.first);
65104
}
66-
67105
}
68106

69107

0 commit comments

Comments
 (0)