copenglgraphicstexturesopengl-4

Stbi_load causes error from glGetTextureHandleArb()


Trying to setup bindless textures, whenever I call glGetTextureHandleARB() it results in the OpenGL error GL_INVALID_OPERATION. This page says this is because my texture object specified is not complete. After spending (too) much time trying to figure out texture completeness here (and trying things with glTexParameters() to tell OpenGL that I don't have mipmaps), I don't see what I am doing wrong before the call and would appreciate some help.

texture.c:

#include "texture.h"
#include <glad/glad.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h>

struct Texture texture_create_bindless_texture(const char *path) {
    struct Texture texture;

    int components;
    void *data = stbi_load(path, &texture.width, &texture.height, &components, 
        4);

    glGenTextures(1, &texture.id);
    glBindTexture(GL_TEXTURE_2D, texture.id);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture.width, texture.height,
        0, GL_RGBA, GL_UNSIGNED_BYTE, data);

    texture.bindless_handle = glGetTextureHandleARB(texture.id); 
    glMakeTextureHandleResidentARB(texture.bindless_handle); 

    glBindTexture(GL_TEXTURE_2D, 0);
    stbi_image_free(data);

    return texture;
}

texture.h:

#ifndef TEXTURE_INCLUDED
#define TEXTURE_INCLUDED

#include <glad/glad.h>

struct Texture {
    int width;
    int height;
    GLuint id;
    GLuint64 bindless_handle;
};
struct Texture texture_create_bindless_texture(const char *path);

#endif

Solution

  • By mistake I gave stbi_load() a path that didn't exist, and neglected to add a check if the pointer returned was NULL (or if the path existed). I guess somewhere OpenGL didn't like that, but only told me about it when I tried to call glGetTextureHandleARB(). At least now I learned my lesson to check stuff passed to me by someone else :)