Image-to-GLObject change caused issues – isolated

I have isolated the issues with my terrain engine that cropped up after revision 6287. It turns up in a material that uses multiple textures, where the first texture is generated per frame. I can work around the issue by reordering the textures, but that isn’t exactly a fix. Here’s the simplest program I could build that exhibits the error.



https://docs.google.com/leaf?id=0B8pkrcYcY5bWODNlNDMxNDEtNTU0Mi00ZDBmLWFhOWEtMzcxYzZjZGY2YTYy&hl=en



Shadowislord (or anyone else familiar with the renderer internals): any idea what the root cause might be?



Move the m_Grass texture after the m_Normals texture to see the bug show up (3 of the four panels are no longer able to read the m_Normals texture - it’s all zeroes).

Ok, I found out the issue here. Since we separated the concept of textures and texture data (images) as GLObjects, there are times when an image was reused, and its texture state was applied to previously bound textures since the image was not changed. Also, when images were uploaded to the GPU, it always used texture unit 0, possibly overwriting what was supposed to be in texture unit 0. I’ve updated it to use the final texture unit to upload, also saving a few state transitions. Here is the patch. I’ll see if I can get commit access or get someone to import it.



[patch]

Index: src/lwjgl-ogl/com/jme3/renderer/lwjgl/LwjglRenderer.java

===================================================================

— src/lwjgl-ogl/com/jme3/renderer/lwjgl/LwjglRenderer.java (revision 6310)

+++ src/lwjgl-ogl/com/jme3/renderer/lwjgl/LwjglRenderer.java (working copy)

@@ -1127,7 +1127,7 @@

Texture tex = rb.getTexture();

Image image = tex.getImage();

if (image.isUpdateNeeded())

  •        updateTexImageData(image, tex.getType(), tex.getMinFilter().usesMipMapLevels());<br />
    
  •        updateTexImageData(image, tex.getType(), tex.getMinFilter().usesMipMapLevels(), 0);<br />
    

glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
convertAttachmentSlot(rb.getSlot()),
@@ -1444,7 +1444,7 @@
}
}

- public void updateTexImageData(Image img, Texture.Type type, boolean mips){
+ public void updateTexImageData(Image img, Texture.Type type, boolean mips, int unit){
int texId = img.getId();
if (texId == -1){
// create texture
@@ -1458,14 +1458,13 @@

// bind texture
int target = convertTextureType(type);
- if (context.boundTextures[0] != img){
- if (context.boundTextureUnit != 0){
- glActiveTexture(GL_TEXTURE0);
- context.boundTextureUnit = 0;
- }
-
+ if (context.boundTextureUnit != unit){
+ glActiveTexture(GL_TEXTURE0+unit);
+ context.boundTextureUnit = unit;
+ }
+ if (context.boundTextures[unit] != img){
glBindTexture(target, texId);
- context.boundTextures[0] = img;
+ context.boundTextures[unit] = img;
}

if (!img.hasMipmaps() && mips){
@@ -1515,7 +1514,7 @@
public void setTexture(int unit, Texture tex){
Image image = tex.getImage();
if (image.isUpdateNeeded()){
- updateTexImageData(image, tex.getType(), tex.getMinFilter().usesMipMapLevels());
+ updateTexImageData(image, tex.getType(), tex.getMinFilter().usesMipMapLevels(), unit);
}

int texId = image.getId();
@@ -1532,12 +1531,11 @@
// glEnable(type);
}

+ if (context.boundTextureUnit != unit){
+ glActiveTexture(GL_TEXTURE0 + unit);
+ context.boundTextureUnit = unit;
+ }
if (textures[unit] != image){
- if (context.boundTextureUnit != unit){
- glActiveTexture(GL_TEXTURE0 + unit);
- context.boundTextureUnit = unit;
- }
-
glBindTexture(type, texId);
textures[unit] = image;
[/patch]
1 Like

Very beautiful, things in the core should be stable and bugless and now we are one step closer to that!

Patch applied and thank you very much