Access Violation when calling glBindTexture when creating GL_TEXTURE_2D_ARRAY

I’m having access violation on the call for glBindTexture only when trying to rebind the last bound texture of GL_TEXTURE_2D_ARRAY (which there wasn’t so I’m always reset the binding by binding 0).
This is my code:

Texture2D::Texture2D(unsigned int format, unsigned int width, unsigned int height, unsigned int unit, unsigned int mimapLevels, unsigned int layers)
    	: Texture(GL_TEXTURE_2D_ARRAY, unit)
    {
    	unsigned int internalFormat;
    	if (format == GL_DEPTH_COMPONENT)
    	{
    		internalFormat = GL_DEPTH_COMPONENT32;
    	}
    	else
    	{
    		internalFormat = format;
    	}
    	m_Format = format;
    	m_Width = width;
    	m_Height = height;
    	unsigned int bound = 0;
    	glGetIntegerv(GL_TEXTURE_BINDING_2D_ARRAY, (int*)&bound);
    	GLCALL(glGenTextures(1, &m_ID));
    	GLCALL(glActiveTexture(GL_TEXTURE0 + m_Unit));
    	GLCALL(glBindTexture(m_Target, m_ID));
    	GLCALL(glTexParameteri(m_Target, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
    	GLCALL(glTexParameteri(m_Target, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
    	GLCALL(glTexStorage3D(m_Target, mimapLevels, internalFormat, width, height, layers));
    	for (size_t i = 0; i < layers; i++)
    	{
    		glTexSubImage3D(m_Target, 0, 0, 0, i, m_Width, m_Height, 1, m_Format, s_FormatTypeMap[internalFormat], NULL);
    	}
    	GLCALL(glBindTexture(m_Target, bound));
    }

The last code where I try to reset the target with: GLCALL(glBindTexture(m_Target, bound)); is causing access violation error. OGL pointers are initialized with glad at the beginning of the program:

		if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
		{
			std::cout << "Failed to initialize GLAD" << std::endl;
			return -1;
		}

And this only happens with GL_TEXTURE_2D_ARRAY, even when this is the first line of my code (after initialization of-course), example code:

auto t = Texture2D(GL_DEPTH_COMPONENT, 1024, 1024, 10, 1, 4);

Any idea what may be causing it?
Thanks in advance!

I actually commented out the last call to glBindTexture in the constructor and let the code run and got another access violation after when calling glCreateProgram. It seems any gl call right after this Texture2D constructor code will result in access violation and I’m really baffled why?

A null data pointer isn’t meaningful for glTexSubImage*D. There’s no need to call glTexSubImage3D until you actually upload data.

I have no idea whether that’s related to your problems, though.

If you’re trying to make object-oriented wrappers, it would all be much simpler if you can use the DSA functions added in 4.5 (glTexture* rather than glTex*). Also: if you’re planning on having destructors call glDelete* functions, ensure that destruction occurs while the context is still bound (if you aim to support multiple contexts, this is likely to be a significant issue; otherwise, you just need to ensure you delete everything before destroying the window).

I see, so if I intend to use this Texture 2D array for cascade shadow maps is this call sufficient:

GLCALL(glTexStorage3D(m_Target, mimapLevels, internalFormat, width, height, layers));

Where would I set the pixel format for each image (not the internal format)?
Right now when trying to render shadow map of each cascade where using:

void FrameBuffer::BindTextureArrayAttachmentWrite(size_t index, size_t layer)
{
	Bind();
	auto& texture = m_TextureAttachments[index];
	texture.second->Bind();
	glFramebufferTextureLayer(m_Target, texture.first, texture.second->GetID(), 0, layer);
}

result in a complete black shadow maps so something is obviously wrong and I’m not sure what. Before switching to Texture 2D array I just had the frame buffer keep reference to N normal Texture2D object and bind for write respectively to the cascade index and it was working.

Yes.

Uh, what? The internal format is the format of the texture itself; the external format (the format/type parameters to glTexImage*, glTexSubImage*, glGetTexImage) is only relevant if you’re transferring pixel data from/to application memory. All layers in an array texture have the same format.

1 Like