-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathGlTexture.java
More file actions
402 lines (314 loc) · 12.6 KB
/
GlTexture.java
File metadata and controls
402 lines (314 loc) · 12.6 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
package net.vulkanmod.gl;
import it.unimi.dsi.fastutil.ints.Int2ReferenceOpenHashMap;
import net.vulkanmod.vulkan.memory.MemoryManager;
import net.vulkanmod.vulkan.texture.ImageUtil;
import net.vulkanmod.vulkan.texture.SamplerManager;
import net.vulkanmod.vulkan.texture.VTextureSelector;
import net.vulkanmod.vulkan.texture.VulkanImage;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL30;
import org.lwjgl.system.MemoryUtil;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import static org.lwjgl.vulkan.VK10.*;
public class GlTexture {
private static int ID_COUNTER = 1;
private static final Int2ReferenceOpenHashMap<GlTexture> map = new Int2ReferenceOpenHashMap<>();
private static int boundTextureId = 0;
private static GlTexture defaultTexture;
private static GlTexture boundTexture;
private static int activeTexture = 0;
public static void bindIdToImage(int id, VulkanImage vulkanImage) {
GlTexture texture = map.get(id);
texture.vulkanImage = vulkanImage;
}
public static int genTextureId() {
int id = ID_COUNTER;
map.put(id, new GlTexture(id));
ID_COUNTER++;
return id;
}
// Default texture can vary depending on graphic drivers,
// but it's usually a square that is either white or black
private static GlTexture getDefaultTexture() {
if (GlTexture.defaultTexture != null)
return GlTexture.defaultTexture;
GlTexture defaultTexture = new GlTexture(0);
defaultTexture.vulkanImage = VulkanImage.createWhiteTexture();
GlTexture.defaultTexture = defaultTexture;
return defaultTexture;
}
public static void bindTexture(int id) {
boundTextureId = id;
if (id == 0) {
boundTexture = getDefaultTexture();
} else {
boundTexture = map.get(id);
}
if (id < 0)
return;
if (boundTexture == null)
throw new NullPointerException("bound texture is null");
VulkanImage vulkanImage = boundTexture.vulkanImage;
if (vulkanImage != null)
VTextureSelector.bindTexture(activeTexture, vulkanImage);
}
public static void glDeleteTextures(IntBuffer intBuffer) {
for (int i = intBuffer.position(); i < intBuffer.limit(); i++) {
glDeleteTextures(intBuffer.get(i));
}
}
public static void glDeleteTextures(int i) {
GlTexture glTexture = map.remove(i);
VulkanImage image = glTexture != null ? glTexture.vulkanImage : null;
if (image != null)
MemoryManager.getInstance().addToFreeable(image);
}
public static GlTexture getTexture(int id) {
if (id == 0)
return null;
return map.get(id);
}
public static void activeTexture(int i) {
activeTexture = i - GL30.GL_TEXTURE0;
VTextureSelector.setActiveTexture(activeTexture);
}
public static void texImage2D(int target, int level, int internalFormat, int width, int height, int border, int format, int type, long pixels) {
if (checkParams(level, width, height))
return;
boundTexture.allocateIfNeeded(width, height, internalFormat, type);
VTextureSelector.bindTexture(activeTexture, boundTexture.vulkanImage);
texSubImage2D(target, level, 0, 0, width, height, format, type, pixels);
}
public static void texImage2D(int target, int level, int internalFormat, int width, int height, int border, int format, int type, @Nullable ByteBuffer pixels) {
if (checkParams(level, width, height))
return;
boundTexture.allocateIfNeeded(width, height, internalFormat, type);
VTextureSelector.bindTexture(activeTexture, boundTexture.vulkanImage);
texSubImage2D(target, level, 0, 0, width, height, format, type, pixels);
}
private static boolean checkParams(int level, int width, int height) {
if (width == 0 || height == 0)
return true;
// TODO: levels
if (level != 0) {
// throw new UnsupportedOperationException();
return true;
}
return false;
}
public static void texSubImage2D(int target, int level, int xOffset, int yOffset, int width, int height, int format, int type, long pixels) {
if (width == 0 || height == 0)
return;
ByteBuffer src;
GlBuffer glBuffer = GlBuffer.getPixelUnpackBufferBound();
if (glBuffer != null) {
glBuffer.data.position((int) pixels);
src = glBuffer.data;
} else {
if (pixels != 0L) {
src = getByteBuffer(width, height, pixels);
} else {
src = null;
}
}
if (src != null)
boundTexture.uploadSubImage(xOffset, yOffset, width, height, format, src);
}
private static ByteBuffer getByteBuffer(int width, int height, long pixels) {
ByteBuffer src;
// TODO: hardcoded format size
int formatSize = 4;
src = MemoryUtil.memByteBuffer(pixels, width * height * formatSize);
return src;
}
public static void texSubImage2D(int target, int level, int xOffset, int yOffset, int width , int height, int format, int type, @Nullable ByteBuffer pixels) {
if (width == 0 || height == 0)
return;
ByteBuffer src;
GlBuffer glBuffer = GlBuffer.getPixelUnpackBufferBound();
if (glBuffer != null) {
if (pixels != null) {
throw new IllegalStateException("Trying to use pixel buffer when there is a Pixel Unpack Buffer bound.");
}
glBuffer.data.position(0);
src = glBuffer.data;
} else {
src = pixels;
}
if (src != null)
boundTexture.uploadSubImage(xOffset, yOffset, width, height, format, src);
}
public static void texParameteri(int target, int pName, int param) {
if (target != GL11.GL_TEXTURE_2D)
throw new UnsupportedOperationException("target != GL_TEXTURE_2D not supported");
switch (pName) {
case GL30.GL_TEXTURE_MAX_LEVEL -> boundTexture.setMaxLevel(param);
case GL30.GL_TEXTURE_MAX_LOD -> boundTexture.setMaxLod(param);
case GL30.GL_TEXTURE_MIN_LOD -> {}
case GL30.GL_TEXTURE_LOD_BIAS -> {}
case GL11.GL_TEXTURE_MAG_FILTER -> boundTexture.setMagFilter(param);
case GL11.GL_TEXTURE_MIN_FILTER -> boundTexture.setMinFilter(param);
case GL11.GL_TEXTURE_WRAP_S, GL11.GL_TEXTURE_WRAP_T -> boundTexture.setClamp(param);
default -> {}
}
//TODO
}
public static int getTexLevelParameter(int target, int level, int pName) {
if (target != GL11.GL_TEXTURE_2D)
throw new UnsupportedOperationException("target != GL_TEXTURE_2D not supported");
if (boundTexture == null)
return -1;
return switch (pName) {
case GL11.GL_TEXTURE_INTERNAL_FORMAT -> GlUtil.getGlFormat(boundTexture.vulkanImage.format);
case GL11.GL_TEXTURE_WIDTH -> boundTexture.vulkanImage.width;
case GL11.GL_TEXTURE_HEIGHT -> boundTexture.vulkanImage.height;
default -> -1;
};
}
public static void generateMipmap(int target) {
if (target != GL11.GL_TEXTURE_2D)
throw new UnsupportedOperationException("target != GL_TEXTURE_2D not supported");
// TODO: crashing
// boundTexture.generateMipmaps();
}
public static void getTexImage(int tex, int level, int format, int type, long pixels) {
VulkanImage image = boundTexture.vulkanImage;
GlBuffer buffer = GlBuffer.getPixelPackBufferBound();
long ptr;
if (buffer != null) {
buffer.data.position((int) pixels);
ptr = MemoryUtil.memAddress(buffer.data);
} else {
ptr = pixels;
}
ImageUtil.downloadTexture(image, ptr);
}
public static void setVulkanImage(int id, VulkanImage vulkanImage) {
GlTexture texture = map.get(id);
texture.vulkanImage = vulkanImage;
}
public static GlTexture getBoundTexture() {
return boundTexture;
}
final int id;
VulkanImage vulkanImage;
int internalFormat;
boolean needsUpdate = false;
int maxLevel = 0;
int maxLod = 0;
int minFilter, magFilter = GL11.GL_LINEAR;
boolean clamp = true;
public GlTexture(int id) {
this.id = id;
}
void allocateIfNeeded(int width, int height, int internalFormat, int type) {
this.internalFormat = internalFormat;
int vkFormat = GlUtil.vulkanFormat(internalFormat, type);
needsUpdate |= vulkanImage == null ||
vulkanImage.width != width || vulkanImage.height != height ||
vkFormat != vulkanImage.format;
if (needsUpdate) {
allocateImage(width, height, vkFormat);
updateSampler();
needsUpdate = false;
}
}
void allocateImage(int width, int height, int vkFormat) {
if (this.vulkanImage != null)
this.vulkanImage.free();
if (VulkanImage.isDepthFormat(vkFormat))
this.vulkanImage = VulkanImage.createDepthImage(vkFormat,
width, height,
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
false, true);
else
this.vulkanImage = new VulkanImage.Builder(width, height)
.setMipLevels(maxLevel + 1)
.setFormat(vkFormat)
.addUsage(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
.createVulkanImage();
}
void updateSampler() {
if (vulkanImage == null)
return;
byte samplerFlags;
samplerFlags = clamp ? SamplerManager.CLAMP_BIT : 0;
samplerFlags |= magFilter == GL11.GL_LINEAR ? SamplerManager.LINEAR_FILTERING_BIT : 0;
samplerFlags |= switch (minFilter) {
case GL11.GL_LINEAR_MIPMAP_LINEAR -> SamplerManager.USE_MIPMAPS_BIT | SamplerManager.MIPMAP_LINEAR_FILTERING_BIT;
case GL11.GL_NEAREST_MIPMAP_NEAREST -> SamplerManager.USE_MIPMAPS_BIT;
default -> 0;
};
vulkanImage.updateTextureSampler(maxLod, samplerFlags);
}
private void uploadSubImage(int xOffset, int yOffset, int width, int height, int format, ByteBuffer pixels) {
ByteBuffer src;
if (format == GL11.GL_RGB && vulkanImage.format == VK_FORMAT_R8G8B8A8_UNORM) {
src = GlUtil.RGBtoRGBA_buffer(pixels);
} else if (format == GL30.GL_BGRA && vulkanImage.format == VK_FORMAT_R8G8B8A8_UNORM) {
src = GlUtil.BGRAtoRGBA_buffer(pixels);
} else {
src = pixels;
}
this.vulkanImage.uploadSubTextureAsync(0, width, height, xOffset, yOffset, 0, 0, 0, src);
if (src != pixels) {
MemoryUtil.memFree(src);
}
}
void generateMipmaps() {
//TODO test
ImageUtil.generateMipmaps(vulkanImage);
}
void setMaxLevel(int l) {
if (l < 0)
throw new IllegalStateException("max level cannot be < 0.");
if (maxLevel != l) {
maxLevel = l;
needsUpdate = true;
}
}
void setMaxLod(int l) {
if (l < 0)
throw new IllegalStateException("max level cannot be < 0.");
if (maxLod != l) {
maxLod = l;
updateSampler();
}
}
void setMagFilter(int v) {
switch (v) {
case GL11.GL_LINEAR, GL11.GL_NEAREST -> {
}
default -> throw new IllegalArgumentException("illegal mag filter value: " + v);
}
this.magFilter = v;
updateSampler();
}
void setMinFilter(int v) {
switch (v) {
case GL11.GL_LINEAR, GL11.GL_NEAREST,
GL11.GL_LINEAR_MIPMAP_LINEAR, GL11.GL_NEAREST_MIPMAP_LINEAR,
GL11.GL_LINEAR_MIPMAP_NEAREST, GL11.GL_NEAREST_MIPMAP_NEAREST -> {
}
default -> throw new IllegalArgumentException("illegal min filter value: " + v);
}
this.minFilter = v;
updateSampler();
}
void setClamp(int v) {
if (v == GL30.GL_CLAMP_TO_EDGE) {
this.clamp = true;
} else {
this.clamp = false;
}
updateSampler();
}
public VulkanImage getVulkanImage() {
return vulkanImage;
}
public void setVulkanImage(VulkanImage vulkanImage) {
this.vulkanImage = vulkanImage;
}
}