Skip to content

Commit 8194222

Browse files
committed
reverted approach and added TextureImage instead
1 parent 381a224 commit 8194222

File tree

8 files changed

+372
-149
lines changed

8 files changed

+372
-149
lines changed

jme3-core/src/main/java/com/jme3/material/Material.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.jme3.shader.bufferobject.BufferObject;
5151
import com.jme3.texture.Image;
5252
import com.jme3.texture.Texture;
53+
import com.jme3.texture.TextureImage;
5354
import com.jme3.texture.image.ColorSpace;
5455
import com.jme3.util.ListMap;
5556
import com.jme3.util.SafeArrayList;
@@ -451,7 +452,7 @@ public MatParamTexture getTextureParam(String name) {
451452
}
452453
return null;
453454
}
454-
455+
455456
/**
456457
* Returns a collection of all parameters set on this material.
457458
*
@@ -514,6 +515,10 @@ public void setParam(String name, VarType type, Object value) {
514515
if (technique != null) {
515516
technique.notifyParamChanged(name, type, value);
516517
}
518+
if (type.isImageType()) {
519+
// recompute sort id
520+
sortingId = -1;
521+
}
517522
}
518523
}
519524

@@ -859,9 +864,13 @@ private void updateShaderMaterialParameter(Renderer renderer, VarType type, Shad
859864
Uniform uniform = shader.getUniform(param.getPrefixedName());
860865
if (!override && uniform.isSetByCurrentMaterial()) return;
861866

862-
if (type.isTextureType()) {
867+
if (type.isTextureType() || type.isImageType()) {
863868
try {
864-
renderer.setTexture(unit.textureUnit, (Texture) param.getValue());
869+
if (type.isTextureType()) {
870+
renderer.setTexture(unit.textureUnit, (Texture) param.getValue());
871+
} else {
872+
renderer.setTextureImage(unit.textureUnit, (TextureImage) param.getValue());
873+
}
865874
} catch (TextureUnitException exception) {
866875
int numTexParams = unit.textureUnit + 1;
867876
String message = "Too many texture parameters (" + numTexParams + ") assigned\n to " + toString();

jme3-core/src/main/java/com/jme3/renderer/Renderer.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import com.jme3.texture.FrameBuffer;
4343
import com.jme3.texture.Image;
4444
import com.jme3.texture.Texture;
45+
import com.jme3.texture.TextureImage;
4546
import com.jme3.util.NativeObject;
4647
import java.nio.ByteBuffer;
4748
import java.util.EnumMap;
@@ -273,6 +274,15 @@ public interface Renderer {
273274
*/
274275
public void setTexture(int unit, Texture tex)
275276
throws TextureUnitException;
277+
278+
/**
279+
* Assigns a TextureImage to the specified texture unit.
280+
*
281+
* @param unit the index of the texture unit (≥0)
282+
* @param tex the texture image to assign
283+
* @throws TextureUnitException if the texture unit does not exist
284+
*/
285+
public void setTextureImage(int unit, TextureImage tex) throws TextureUnitException;
276286

277287
/**
278288
* Modifies the given Texture with the given Image.

jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import com.jme3.texture.Texture2D;
6262
import com.jme3.texture.Texture.ShadowCompareMode;
6363
import com.jme3.texture.Texture.WrapAxis;
64+
import com.jme3.texture.TextureImage;
6465
import com.jme3.texture.image.LastTextureState;
6566
import com.jme3.util.BufferUtils;
6667
import com.jme3.util.ListMap;
@@ -2549,13 +2550,6 @@ private void bindTextureAndUnit(int target, Image img, int unit) {
25492550
}
25502551
if (context.boundTextures[unit]==null||context.boundTextures[unit].get() != img.getWeakRef().get()) {
25512552
gl.glBindTexture(target, img.getId());
2552-
if (gl4 != null && img.getAccess() != null) {
2553-
// binds the image so that imageStore and imageLoad operations
2554-
// can be used in shaders on the image
2555-
gl4.glBindImageTexture(unit, img.getId(), 0, img.isLayered(),
2556-
Math.max(img.getBindLayer(), 0), img.getAccess().getGlEnum(),
2557-
texUtil.getImageFormat(img.getFormat(), false).internalFormat);
2558-
}
25592553
context.boundTextures[unit] = img.getWeakRef();
25602554
statistics.onTextureUse(img, true);
25612555
} else {
@@ -2578,13 +2572,6 @@ private void bindTextureOnly(int target, Image img, int unit) {
25782572
context.boundTextureUnit = unit;
25792573
}
25802574
gl.glBindTexture(target, img.getId());
2581-
if (gl4 != null && img.getAccess() != null) {
2582-
// binds the image so that imageStore and imageLoad operations
2583-
// can be used in shaders on the image
2584-
gl4.glBindImageTexture(unit, img.getId(), 0, img.isLayered(),
2585-
Math.max(img.getBindLayer(), 0), img.getAccess().getGlEnum(),
2586-
texUtil.getImageFormat(img.getFormat(), false).internalFormat);
2587-
}
25882575
context.boundTextures[unit] = img.getWeakRef();
25892576
statistics.onTextureUse(img, true);
25902577
} else {
@@ -2766,7 +2753,20 @@ public void setTexture(int unit, Texture tex) throws TextureUnitException {
27662753
if (tex.getName() != null) glext.glObjectLabel(GL.GL_TEXTURE, tex.getImage().getId(), tex.getName());
27672754
}
27682755
}
2769-
2756+
2757+
@Override
2758+
public void setTextureImage(int unit, TextureImage tex) throws TextureUnitException {
2759+
if (unit < 0 || unit >= RenderContext.maxTextureUnits) {
2760+
throw new TextureUnitException();
2761+
}
2762+
WeakReference<Image> ref = context.boundTextures[unit];
2763+
boolean bindRequired = tex.clearUpdateNeeded() || ref == null || ref.get() != tex.getImage().getWeakRef().get();
2764+
setTexture(unit, tex.getTexture());
2765+
if (gl4 != null && bindRequired) {
2766+
tex.bindImage(gl4, texUtil, unit);
2767+
}
2768+
}
2769+
27702770
@Override
27712771
public void setUniformBufferObject(int bindingPoint, BufferObject bufferObject) {
27722772
if (bufferObject.isUpdateNeeded()) {

jme3-core/src/main/java/com/jme3/renderer/opengl/TextureUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2021 jMonkeyEngine
2+
* Copyright (c) 2009-2024 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -46,7 +46,7 @@
4646
*
4747
* @author Kirill Vainer
4848
*/
49-
final class TextureUtil {
49+
public final class TextureUtil {
5050

5151
private static final Logger logger = Logger.getLogger(TextureUtil.class.getName());
5252

jme3-core/src/main/java/com/jme3/shader/VarType.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@
4343
import com.jme3.texture.Texture3D;
4444
import com.jme3.texture.TextureArray;
4545
import com.jme3.texture.TextureCubeMap;
46+
import com.jme3.texture.TextureImage;
4647

4748
public enum VarType {
48-
49+
4950
Float("float", float.class, Float.class),
5051
Vector2("vec2", Vector2f.class),
5152
Vector3("vec3", Vector3f.class),
@@ -56,7 +57,8 @@ public enum VarType {
5657
Vector2Array(true, false, "vec2", Vector2f[].class),
5758
Vector3Array(true, false, "vec3", Vector3f[].class),
5859
Vector4Array(true, false, "vec4", Vector4f[].class),
59-
60+
61+
Int("int", int.class, Integer.class),
6062
Boolean("bool", Boolean.class, boolean.class),
6163

6264
Matrix3(true, false, "mat3", Matrix3f.class),
@@ -70,12 +72,16 @@ public enum VarType {
7072
Texture3D(false, true, "sampler3D", Texture3D.class, Texture.class),
7173
TextureArray(false, true, "sampler2DArray|sampler2DArrayShadow", TextureArray.class, Texture.class),
7274
TextureCubeMap(false, true, "samplerCube", TextureCubeMap.class, Texture.class),
73-
Int("int", int.class, Integer.class),
75+
76+
Image2D(false, false, true, "image2D", TextureImage.class),
77+
Image3D(false, false, true, "image3D", TextureImage.class),
78+
7479
UniformBufferObject(false, false, "custom", BufferObject.class),
7580
ShaderStorageBufferObject(false, false, "custom", BufferObject.class);
7681

7782
private boolean usesMultiData = false;
7883
private boolean textureType = false;
84+
private boolean imageType = false;
7985
private final String glslType;
8086
private Class<?>[] javaTypes;
8187

@@ -98,6 +104,11 @@ public enum VarType {
98104
this.javaTypes = new Class<?>[0];
99105
}
100106
}
107+
108+
VarType(boolean multiData, boolean textureType, boolean imageType, String glslType, Class<?>... javaTypes) {
109+
this(multiData, textureType, glslType, javaTypes);
110+
this.imageType = imageType;
111+
}
101112

102113
/**
103114
* Check if the passed object is of a type mapped to this VarType
@@ -126,6 +137,10 @@ public Class<?>[] getJavaType() {
126137
public boolean isTextureType() {
127138
return textureType;
128139
}
140+
141+
public boolean isImageType() {
142+
return imageType;
143+
}
129144

130145
public boolean usesMultiData() {
131146
return usesMultiData;

jme3-core/src/main/java/com/jme3/system/NullRenderer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import com.jme3.texture.FrameBuffer;
4949
import com.jme3.texture.Image;
5050
import com.jme3.texture.Texture;
51+
import com.jme3.texture.TextureImage;
5152

5253
import java.nio.ByteBuffer;
5354
import java.util.EnumMap;
@@ -170,6 +171,11 @@ public void deleteFrameBuffer(FrameBuffer fb) {
170171
public void setTexture(int unit, Texture tex) throws TextureUnitException {
171172
// do nothing
172173
}
174+
175+
@Override
176+
public void setTextureImage(int unit, TextureImage tex) throws TextureUnitException {
177+
// do nothing
178+
}
173179

174180
@Override
175181
public void modifyTexture(Texture tex, Image pixels, int x, int y) {
@@ -314,4 +320,5 @@ public void setShaderStorageBufferObject(int bindingPoint, BufferObject bufferOb
314320
public void setUniformBufferObject(int bindingPoint, BufferObject bufferObject) {
315321

316322
}
323+
317324
}

jme3-core/src/main/java/com/jme3/texture/Image.java

Lines changed: 0 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import com.jme3.math.FastMath;
4040
import com.jme3.renderer.Caps;
4141
import com.jme3.renderer.Renderer;
42-
import com.jme3.renderer.opengl.GL2;
4342
import com.jme3.texture.image.ColorSpace;
4443
import com.jme3.texture.image.LastTextureState;
4544
import com.jme3.util.BufferUtils;
@@ -598,61 +597,6 @@ public boolean isFloatingPont(){
598597

599598

600599

601-
}
602-
603-
public enum Access {
604-
605-
/**
606-
* The image can only read from in a shader.
607-
*/
608-
ReadOnly(true, false, GL2.GL_READ_ONLY),
609-
610-
/**
611-
* The image can written to in a shader.
612-
*/
613-
WriteOnly(false, true, GL2.GL_WRITE_ONLY),
614-
615-
/**
616-
* The image can both be written to and read from in a shader.
617-
*/
618-
ReadWrite(true, true, GL2.GL_READ_WRITE);
619-
620-
private final boolean read, write;
621-
private final int glEnum;
622-
623-
private Access(boolean read, boolean write, int glEnum) {
624-
this.read = read;
625-
this.write = write;
626-
this.glEnum = glEnum;
627-
}
628-
629-
/**
630-
* If true, the image can be read from in a shader.
631-
*
632-
* @return
633-
*/
634-
public boolean isRead() {
635-
return read;
636-
}
637-
638-
/**
639-
* If true, the image can be written to in a shader.
640-
*
641-
* @return
642-
*/
643-
public boolean isWrite() {
644-
return write;
645-
}
646-
647-
/**
648-
* Corresponding OpenGL enum.
649-
*
650-
* @return
651-
*/
652-
public int getGlEnum() {
653-
return glEnum;
654-
}
655-
656600
}
657601

658602
// image attributes
@@ -662,8 +606,6 @@ public int getGlEnum() {
662606
protected ArrayList<ByteBuffer> data;
663607
protected int multiSamples = 1;
664608
protected ColorSpace colorSpace = null;
665-
protected Access access = null;
666-
protected int bindLayer = -1;
667609
// protected int mipOffset = 0;
668610

669611
// attributes relating to GL object
@@ -1268,70 +1210,6 @@ public ColorSpace getColorSpace() {
12681210
return colorSpace;
12691211
}
12701212

1271-
/**
1272-
* Sets the access modifier for this image.
1273-
* <p>
1274-
* If not null, the image will be bound in such a way as to allow
1275-
* {@code imageStore} and {@code imageLoad} functions to work. Otherwise
1276-
* the image will be bound normally.
1277-
* <p>
1278-
* default=null
1279-
*
1280-
* @param access
1281-
*/
1282-
public void setAccess(Access access) {
1283-
if (this.access != access) {
1284-
this.access = access;
1285-
setUpdateNeeded();
1286-
}
1287-
}
1288-
1289-
/**
1290-
*
1291-
* @return
1292-
* @see #setAccess(com.jme3.texture.Image.Access)
1293-
*/
1294-
public Access getAccess() {
1295-
return access;
1296-
}
1297-
1298-
/**
1299-
* Sets the bind layer used if {@link #getAccess()} does not
1300-
* return null.
1301-
* <p>
1302-
* If greater than or equal to zero, only the specified layer will be
1303-
* bound. If less than zero, the entire image will be bound.
1304-
* <p>
1305-
* default=-1
1306-
*
1307-
* @param bindLayer
1308-
*/
1309-
public void setBindLayer(int bindLayer) {
1310-
if (this.bindLayer != bindLayer) {
1311-
this.bindLayer = bindLayer;
1312-
setUpdateNeeded();
1313-
}
1314-
}
1315-
1316-
/**
1317-
*
1318-
* @return
1319-
* @see #setBindLayer(int)
1320-
*/
1321-
public int getBindLayer() {
1322-
return bindLayer;
1323-
}
1324-
1325-
/**
1326-
* Returns true if the entire image will be bound in cases where
1327-
* {@link #getAccess()} does not return null.
1328-
*
1329-
* @return
1330-
*/
1331-
public boolean isLayered() {
1332-
return bindLayer < 0;
1333-
}
1334-
13351213
@Override
13361214
public String toString(){
13371215
StringBuilder sb = new StringBuilder();
@@ -1408,8 +1286,6 @@ public void write(JmeExporter e) throws IOException {
14081286
capsule.write(multiSamples, "multiSamples", 1);
14091287
capsule.writeByteBufferArrayList(data, "data", null);
14101288
capsule.write(colorSpace, "colorSpace", null);
1411-
capsule.write(access, "access", null);
1412-
capsule.write(bindLayer, "bindLayer", -1);
14131289
}
14141290

14151291
@Override
@@ -1423,8 +1299,6 @@ public void read(JmeImporter importer) throws IOException {
14231299
multiSamples = capsule.readInt("multiSamples", 1);
14241300
data = capsule.readByteBufferArrayList("data", null);
14251301
colorSpace = capsule.readEnum("colorSpace", ColorSpace.class, null);
1426-
access = capsule.readEnum("access", Access.class, null);
1427-
bindLayer = capsule.readInt("bindLayer", -1);
14281302
if (mipMapSizes != null) {
14291303
needGeneratedMips = false;
14301304
mipsWereGenerated = true;

0 commit comments

Comments
 (0)