Fix mipmapped texture arrays and 3D textures

Sorry if this is wrong place, your forum is really hard to navigate and understand.

There is a bug in jME causes to throw OpenGL Error (invalid value) when enabling mipmapping for texture array or 3d texture. Caused by wrong initialization (always initializes mip-level 0). Fix is easy as pie: Mipmapped texture arrays by Eirenliel · Pull Request #128 · jMonkeyEngine/jmonkeyengine · GitHub

Explanation: we have no levels more than 0 initialized by glTexImage3d, but trying to upload texture data for all mip-levels by getTexSubImage3d.

2 Likes

Thanks for the patch. This was the exact right place. Hopefully others with proper knowledge of this specific code will now get to take a look. (Note: you should also post about your other pull request in a similar way. I only commented on the first one.)

Well I can confirm that I had some bug when i last tried to use the 3dtextures with mipmaps.

I’m not sure I understand the solution fully (I guess it is probably correct, but my knowledge of that part is really limited). If we try to upload not exisitng mipmaps, what does the change from 0 to i change? As far as I understand it, instead of saying it always have 0 mipmaps we use the value provieded?

My knowledge of texture arrays are limited too, it looks like almost no one uses them, but they are very handy!

This is fixed code:
[java]if (index == -1){
GL12.glTexImage3D(target,
i,
glFmt.internalFormat,
mipWidth,
mipHeight,
image.getData().size(), //# of slices
border,
glFmt.format,
glFmt.dataType,
data);
}else{
GL12.glTexSubImage3D(target,
i, // level
0, // xoffset
0, // yoffset
index, // zoffset
width, // width
height, // height
1, // depth
glFmt.format,
glFmt.dataType,
data);
}
[/java]
In original there were 0 instead of i in glTexImage3D. I should add that this error happens only when we are using cpu-generated mipmaps (not asking opengl to generate them). Problem is: calls with index == -1 happens before actually uploading texture layers for every mip-map layer (see: com.jme3.renderer.lwjgl.LwjglRenderer.updateTexImageData:1917).
In this place we should initialize every mip-map layer, but we every time initializes layer 0. Then we are trying to upload texture layers with glTexSubImage3D for every mipmap level (i), but we created on OpenGL only level 0. It causes invalid value open gl error. (If we don’t have only 0 mip-level, of course).

Well I tried to use them for my terrain system, to avoid the uniform limit when using a many layers, but had problemw with getting mipamps to work ^^ (Guess what error i always got^^)

@Empire Phoenix said: (Guess what error i always got^^)
I decided to use texture arrays at start of my project, so i had no way back :D

Any thoughts?

Seems fine but it’s hard to test, do you have a test case?

I have my project. I can prepare test case, if needed, but i should transfer mipmap genertation for texture arrays in jME for this (standard jME method suitable only for 2d textures :().

@Eirenliel said: I have my project. I can prepare test case, if needed, but i should transfer mipmap genertation for texture arrays in jME for this (standard jME method suitable only for 2d textures :().
Isn't that what this patch is about?

No, this patch is not about generating mipmaps, its about uploading mipmaps on video card - it’s bugged for 3d-textures and 2d-texture arrays.

About generating:
jME’s MipMapGenerator using only 0 layer of image to generate MipMap. And doing it wrong, i guess. I will make patch today, with test case.

I did a bit of investigation and indeed that “0” should be “i”. This part will allocate storage in VRAM for the texture array’s mipmaps. It has to specify the resolution at each level of the array to work, hence the need to call glTexImage3D at each mipmap level with the data parameter set to NULL.

One way to test this functionality is loading a texture array from a DDS image or generating mipmaps in software.

The change has been merged.