Application crashes when loading more than one texture on cubemap

I’m trying to pass a cubemap sampler to my shader. However, whenever I load the second face to my cubemap, my program crashes. In gdb, I get this:

Program received signal SIGSEGV, Segmentation fault.
0x6e0ec0ad in ig7icd32!RegisterProcTableCallback () from C:\WINDOWS\SysWOW64\ig7icd32.dll

Here’s some constant variables I use as helpers for loading my textures in a loop:

const GLenum cubeMapFaces[6] = {GL_TEXTURE_CUBE_MAP_POSITIVE_X,
	GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
	GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
	GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
	GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
	GL_TEXTURE_CUBE_MAP_NEGATIVE_Z};
const int cubeMapOffsets[10] = {0,0x100000,0x140000,0x150000,0x154000,
	0x155000,0x155400,0x155500,0x155540,0x155550};
const int cubeMapSizes[10] = {512,256,128,64,32,16,8,4,2,1};

In the initialization portion of my program, I set the cubemap as texture 6, like so:

glUniform1i(glGetUniformLocation(prgId[1],"u_Radiance"),6);

In my draw loop, I do the following:

glUseProgram(prgId[1]);

glGenTextures(1,&texId[6]);
glActiveTexture(GL_TEXTURE6);
glBindTexture(GL_TEXTURE_CUBE_MAP,texId[6]);
glTexParameteri(GL_TEXTURE_CUBE_MAP,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP,GL_TEXTURE_WRAP_R,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP,GL_TEXTURE_BASE_LEVEL,0);
glTexParameteri(GL_TEXTURE_CUBE_MAP,GL_TEXTURE_MAX_LEVEL,7);

for(int i=0; i<6; i++) {
	for(int j=0; j<8; j++) {
		glTexImage2D(cubeMapFaces[i],j,GL_RGBA8,cubeMapSizes[j],cubeMapSizes[j],
			0,GL_RGBA,GL_UNSIGNED_BYTE,&EnvironmentMap->Faces[i].TexData[cubeMapOffsets[j]]);
	}
}

//drawing code omitted for clarity

glDeleteTextures(1,&texId[6]);

Some things I tried:

Removing the for loops and just having one glTexImage2D: Works, but the cubemap is black.
Running glTexImage2D twice or more, in any way, outside or inside a loop: Crashes.

I assume &EnvironmentMap->Faces[i].TexData[cubeMapOffsets[j]] is well-formed, but I’m not sure. In the case that it is, what else could be the cause?


GPU is Intel HD 3rd Generation/3000 (supports up to GL 4.0, I’m using GL 3.3 for my application), OS is Windows 10 x64.

EDIT

Here’s the drawing code, if it turns out to be useful:

glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(4);
//Render objects to screen
for(int i=0; i<256; i++) {
	if(objBuffer[i].IsActive) {
		glm::mat4 mdl = glm::translate(glm::mat4(),objBuffer[i].Position);
		mdl *= glm::toMat4(objBuffer[i].Rotation);
		mdl *= glm::scale(glm::mat4(),objBuffer[i].Scale);
		vsd[1].u_PositionMtx = u_Projection*u_View*mdl;
		vsd[1].u_NormalMtx = glm::transpose(glm::inverse(u_View*mdl));
		vsd[1].u_ShadowBias = shadowBiasMtx*proj*view*mdl;
		
		glGenBuffers(1,&vsdId[1]);
		glBindBuffer(GL_UNIFORM_BUFFER,vsdId[1]);
		glBufferData(GL_UNIFORM_BUFFER,sizeof(VertexShaderData),&vsd[1],GL_STATIC_DRAW);
		glBindBufferBase(GL_UNIFORM_BUFFER,8,vsdId[1]);
		
		if(objBuffer[i].IsAnimated) {
			for(int j=0; j<28; j++) {
				vsd[1].u_Bones[j] = objBuffer[i].Bones[j];
				vsd[1].u_NormalBones[j] = glm::transpose(glm::inverse(objBuffer[i].Bones[j]));
			}
		}
		
		for(int j=0; j<(objBuffer[i].ObjModel->MatCount); j++) {
			//Setup vertex arrays
			glBindBuffer(GL_ARRAY_BUFFER,vboId[0]);
			glBufferData(GL_ARRAY_BUFFER,
				objBuffer[i].ObjModel->Entries[j].VertexCount*12,
				objBuffer[i].ObjModel->Entries[j].Positions,GL_STATIC_DRAW);
			glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
			
			glBindBuffer(GL_ARRAY_BUFFER,vboId[1]);
			glBufferData(GL_ARRAY_BUFFER,
				objBuffer[i].ObjModel->Entries[j].VertexCount*12,
				objBuffer[i].ObjModel->Entries[j].Normals,GL_STATIC_DRAW);
			glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,0);
			
			glBindBuffer(GL_ARRAY_BUFFER,vboId[2]);
			glBufferData(GL_ARRAY_BUFFER,
				objBuffer[i].ObjModel->Entries[j].VertexCount<<4,
				objBuffer[i].ObjModel->Entries[j].Tangents,GL_STATIC_DRAW);
			glVertexAttribPointer(2,4,GL_FLOAT,GL_FALSE,0,0);
			
			glBindBuffer(GL_ARRAY_BUFFER,vboId[4]);
			glBufferData(GL_ARRAY_BUFFER,
				objBuffer[i].ObjModel->Entries[j].VertexCount<<3,
				objBuffer[i].ObjModel->Entries[j].UVs,GL_STATIC_DRAW);
			glVertexAttribPointer(4,2,GL_FLOAT,GL_FALSE,0,0);
			
			glBindBuffer(GL_ARRAY_BUFFER,vboId[6]);
			glBufferData(GL_ARRAY_BUFFER,
				objBuffer[i].ObjModel->Entries[j].VertexCount<<4,
				objBuffer[i].ObjModel->Entries[j].Joints,GL_STATIC_DRAW);
			glVertexAttribPointer(6,4,GL_SHORT,GL_FALSE,0,0);
			
			glBindBuffer(GL_ARRAY_BUFFER,vboId[7]);
			glBufferData(GL_ARRAY_BUFFER,
				objBuffer[i].ObjModel->Entries[j].VertexCount<<4,
				objBuffer[i].ObjModel->Entries[j].Weights,GL_STATIC_DRAW);
			glVertexAttribPointer(7,4,GL_FLOAT,GL_FALSE,0,0);
			
			//Setup textures
			glGenTextures(5,texId);
			for(int k=0; k<5; k++) {
				int texIndex = objBuffer[i].ObjModel->Entries[j].TexIndices[k];
				glActiveTexture(GL_TEXTURE0+k);
				glBindTexture(GL_TEXTURE_2D,texId[k]);
				glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
				glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
				glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
				glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
				glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_BASE_LEVEL,0);
				glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAX_LEVEL,
					objBuffer[i].ObjTexture->Entries[texIndex].MipCount-1);
				if(objBuffer[i].ObjTexture->Entries[texIndex].InternalFormat!=GL_RGBA8) {
				glCompressedTexImage2D(GL_TEXTURE_2D,0,
					objBuffer[i].ObjTexture->Entries[texIndex].InternalFormat,
					objBuffer[i].ObjTexture->Entries[texIndex].Width,
					objBuffer[i].ObjTexture->Entries[texIndex].Height,0,
					objBuffer[i].ObjTexture->Entries[texIndex].TexSize,
					objBuffer[i].ObjTexture->Entries[texIndex].TexData);
				} else {
				glTexImage2D(GL_TEXTURE_2D,0,
					objBuffer[i].ObjTexture->Entries[texIndex].InternalFormat,
					objBuffer[i].ObjTexture->Entries[texIndex].Width,
					objBuffer[i].ObjTexture->Entries[texIndex].Height,0,GL_RGBA,GL_UNSIGNED_BYTE,
					objBuffer[i].ObjTexture->Entries[texIndex].TexData);
				}
			}
			
			//Draw primitives and free textures
			glDrawElements(GL_TRIANGLES,
				objBuffer[i].ObjModel->Entries[j].TriCount,GL_UNSIGNED_SHORT,
				objBuffer[i].ObjModel->Entries[j].Indices);
			glDeleteTextures(5,texId);
		}
		glDeleteBuffers(1,&vsdId[1]);
	}
}
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glDisableVertexAttribArray(4);

The offsets are correct, provided that they’re in bytes. Can you show the declaration for the TexData field? It should be char* or char (optionally with const and/or unsigned qualifiers).

I note that the inner loop (j) only goes up to 8 when it should be 10, but that wouldn’t cause a crash.

When you say:

do you actually mean “in any way”? Like, the exact same call twice in succession will crash the second time?

It is a char *, yes.


I made it go to 8, since textures are to be aligned by 4 pixels, and my code doesn’t allow for that yet.


Yeah, basically. So that loop crashes, but these also crash:

for(int i=0; i<1; i++) {
	for(int j=0; j<2; j++) {
		glTexImage2D(cubeMapFaces[i],j,GL_RGBA8,cubeMapSizes[j],cubeMapSizes[j],
			0,GL_RGBA,GL_UNSIGNED_BYTE,&EnvironmentMap->Faces[i].TexData[cubeMapOffsets[j]]);
	}
}

 

glTexImage2D(cubeMapFaces[0],j,GL_RGBA8,512,512,
	0,GL_RGBA,GL_UNSIGNED_BYTE,&EnvironmentMap->Faces[0].TexData[0]);
glTexImage2D(cubeMapFaces[1],j,GL_RGBA8,512,512,
	0,GL_RGBA,GL_UNSIGNED_BYTE,&EnvironmentMap->Faces[1].TexData[0]);

But these don’t:

for(int i=0; i<1; i++) {
	for(int j=0; j<1; j++) {
		glTexImage2D(cubeMapFaces[i],j,GL_RGBA8,cubeMapSizes[j],cubeMapSizes[j],
			0,GL_RGBA,GL_UNSIGNED_BYTE,&EnvironmentMap->Faces[i].TexData[cubeMapOffsets[j]]);
	}
}

 

glTexImage2D(cubeMapFaces[0],j,GL_RGBA8,512,512,
	0,GL_RGBA,GL_UNSIGNED_BYTE,&EnvironmentMap->Faces[0].TexData[0]);

So yeah, not sure what’s going on.

EDIT

So apparently the issue is the FENV constructor I have for EnvironmentMap not allocating enough space for TexData. Here’s what I’ve got:

Faces[i].TexSize = 0;
int width = Faces[i].Width;
int height = Faces[i].Height;
for(int i=0; i<Faces[i].MipCount; i++) {
	Faces[i].TexSize += (((width>4)?width:4)*height)<<2;
	width >>= 1;
	height >>= 1;
}

Normally, Width and Height are 512, and MipCount is 10. I expect a size of 0x155570, but I don’t.

EDIT 2

I can’t believe I was this stupid! I already used the i variable for the face loop, and I was reusing it. Changing it to j did the trick!