-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShaderService.java
More file actions
182 lines (162 loc) · 6.93 KB
/
ShaderService.java
File metadata and controls
182 lines (162 loc) · 6.93 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
package fr.fabcc.ffmgl.services;
import static java.lang.foreign.ValueLayout.ADDRESS;
import static java.lang.foreign.ValueLayout.JAVA_FLOAT;
import static java.lang.foreign.ValueLayout.JAVA_INT;
import java.io.File;
import java.io.IOException;
import java.lang.foreign.Arena;
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.MemorySegment;
import java.lang.invoke.MethodHandle;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import fr.fabcc.ffmgl.data.CompiledShader;
import fr.fabcc.ffmgl.data.UncompiledShader;
import fr.fabcc.ffmgl.enumeration.ShaderType;
import fr.fabcc.ffmgl.lib.GLUtils;
import fr.fabcc.ffmgl.lib.gl.GL;
public class ShaderService {
private MethodHandle glCreateShader;
private MethodHandle glShaderSource;
private MethodHandle glCompileShader;
private MethodHandle glGetShaderiv;
private MethodHandle glGetShaderInfoLog;
private MethodHandle glGetProgramInfoLog;
private MethodHandle glCreateProgram;
private MethodHandle glAttachShader;
private MethodHandle glLinkProgram;
private MethodHandle glUseProgram;
private MethodHandle glClearColor;
private MethodHandle glClear;
private MethodHandle glDrawArrays;
private MethodHandle glGetProgramiv;
public ShaderService(Arena arena) {
this.glCreateShader = GLUtils.loadGLFunction(arena, "glCreateShader",
FunctionDescriptor.of(JAVA_INT, JAVA_INT));
this.glShaderSource = GLUtils.loadGLFunction(arena, "glShaderSource",
FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS));
this.glCompileShader = GLUtils.loadGLFunction(arena, "glCompileShader", FunctionDescriptor.ofVoid(JAVA_INT));
this.glGetShaderiv = GLUtils.loadGLFunction(arena, "glGetShaderiv",
FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT, ADDRESS));
this.glGetShaderInfoLog = GLUtils.loadGLFunction(arena, "glGetShaderInfoLog",
FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS));
this.glCreateProgram = GLUtils.loadGLFunction(arena, "glCreateProgram", FunctionDescriptor.of(JAVA_INT));
this.glAttachShader = GLUtils.loadGLFunction(arena, "glAttachShader",
FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT));
this.glLinkProgram = GLUtils.loadGLFunction(arena, "glLinkProgram", FunctionDescriptor.ofVoid(JAVA_INT));
this.glUseProgram = GLUtils.loadGLFunction(arena, "glUseProgram", FunctionDescriptor.ofVoid(JAVA_INT));
this.glClearColor = GLUtils.loadGLFunction(arena, "glClearColor",
FunctionDescriptor.ofVoid(JAVA_FLOAT, JAVA_FLOAT, JAVA_FLOAT, JAVA_FLOAT));
this.glClear = GLUtils.loadGLFunction(arena, "glClear", FunctionDescriptor.ofVoid(JAVA_INT));
this.glDrawArrays = GLUtils.loadGLFunction(arena, "glDrawArrays",
FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT, JAVA_INT));
this.glGetProgramInfoLog = GLUtils.loadGLFunction(arena, "glGetProgramInfoLog",
FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS));
this.glGetProgramiv = GLUtils.loadGLFunction(arena, "glGetProgramiv",
FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT, ADDRESS));
}
public UncompiledShader createShader(
File vertexShaderFile,
File fragmentShaderFile) throws IOException {
return createShader(vertexShaderFile.toPath(), fragmentShaderFile.toPath());
}
public UncompiledShader createShader(
Path vertexShaderPath,
Path fragmentShaderPath) throws IOException {
Map<ShaderType, String> shaderSources = new HashMap<>();
if (!Files.exists(vertexShaderPath)) {
throw new IOException(
"Vertex shader file does not exist: " + vertexShaderPath.toAbsolutePath());
}
if (!Files.exists(fragmentShaderPath)) {
throw new IOException(
"Fragment shader file does not exist: " + fragmentShaderPath.toAbsolutePath());
}
String vertexShaderSource = Files.readString(vertexShaderPath);
String fragmentShaderSource = Files.readString(fragmentShaderPath);
shaderSources.put(ShaderType.VERTEX, vertexShaderSource);
shaderSources.put(ShaderType.FRAGMENT, fragmentShaderSource);
return new UncompiledShader(shaderSources);
}
/**
* Compile a shader from its source code.
*
* @param arena
* @param uncompiledShader
* @return
*/
public CompiledShader compileShader(
Arena arena,
UncompiledShader uncompiledShader) {
try {
Map<ShaderType, Integer> shaderIds = new HashMap<>();
for (var entry : uncompiledShader.getShaderSources().entrySet()) {
ShaderType shaderType = entry.getKey();
String shaderSource = entry.getValue();
long shaderId = (int) glCreateShader.invokeExact((int) shaderType.getValue());
MemorySegment fsSrc = arena.allocateFrom(shaderSource);
MemorySegment fsPtr = arena.allocate(ADDRESS);
fsPtr.set(ADDRESS, 0, fsSrc);
glShaderSource.invokeExact((int) shaderId, 1, fsPtr, MemorySegment.NULL);
glCompileShader.invokeExact((int) shaderId);
shaderIds.put(shaderType, (int) shaderId);
}
long programId = (int) glCreateProgram.invokeExact();
for (int shaderId : shaderIds.values()) {
glAttachShader.invokeExact((int) programId, shaderId);
}
glLinkProgram.invokeExact((int) programId);
return new CompiledShader(programId, shaderIds);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
public List<String> getShaderCompileErrors(Arena arena, CompiledShader shader) {
try {
List<String> errors = new ArrayList<>();
// Vérifier chaque shader individuellement
for (var entry : shader.getShaders().entrySet()) {
ShaderType shaderType = entry.getKey();
int shaderId = entry.getValue();
// Vérifier le statut de compilation
MemorySegment compileStatus = arena.allocate(JAVA_INT);
glGetShaderiv.invokeExact(shaderId, GL.GL_COMPILE_STATUS(), compileStatus); // GL_COMPILE_STATUS =
// 0x8B81
if (compileStatus.get(JAVA_INT, 0) == 0) { // GL_FALSE
// Récupérer le log d'erreur
MemorySegment infoLog = arena.allocate(1024);
glGetShaderInfoLog.invokeExact(shaderId, 1024, MemorySegment.NULL, infoLog);
String infoLogStr = infoLog.getString(0);
if (!infoLogStr.trim().isEmpty()) {
errors.add(shaderType + " shader error: " + infoLogStr.trim());
}
}
}
// Vérifier aussi le linking du programme
MemorySegment linkStatus = arena.allocate(JAVA_INT);
glGetProgramiv.invokeExact((int) shader.getProgramId(), 0x8B82, linkStatus); // GL_LINK_STATUS = 0x8B82
if (linkStatus.get(JAVA_INT, 0) == 0) { // GL_FALSE
MemorySegment infoLog = arena.allocate(1024);
glGetProgramInfoLog.invokeExact((int) shader.getProgramId(), 1024, MemorySegment.NULL, infoLog);
String infoLogStr = infoLog.getString(0);
if (!infoLogStr.trim().isEmpty()) {
errors.add("Program link error: " + infoLogStr.trim());
}
}
return errors;
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
public void useProgram(CompiledShader shader) {
try {
glUseProgram.invokeExact((int) shader.getProgramId());
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
}