In my program, I need to generate dynamic number and dynamic size of textures.So I write a function to do this
int TexGener::genTex(const ivec2& texSize,int layer){
if(curContext->makeCurrent(this)){
GLuint tmpTbo = 0;
func->glActiveTexture(GL_TEXTURE5);
func->glGenTextures(1,&tmpTbo);
func->glBindTexture(GL_TEXTURE_2D,tmpTbo);
int wid = texSize.x;
int hei = texSize.y;
func->glTexStorage2D(GL_TEXTURE_2D,layer,GL_RGBA16F,wid,hei);
if(layer == 1){
func->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
func->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
func->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
func->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
else{
func->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);//use mipmap
func->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
func->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
func->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
GLint result;
func->glGetTexParameteriv(GL_TEXTURE_2D,GL_TEXTURE_IMMUTABLE_FORMAT,&result);
qDebug()<<"texture malloc:"<<result;
curContext->doneCurrent();
if(tmpTbo > 0){
appliedTex.append(tmpTbo);
return appliedTex.size() - 1;
}
}
return -1;
}
In this function,texSize
specifies the width and height of a texture, layer
specifies the levels.I find that if texSize
is bigger than 2048 * 2048,glTexStorage2D could fail sometimes,but always success if smaller than 2048* 2048. Then I checked GPU memory, only 500MB which is 8% of whole 6GB GPU memory is occupied when calling this funcion,I don’t know why could glTexStorage2D
fail,and how can I fix this?