Texture "unbind" not working / weird

I am using OpenGL 4.4 and have found a code fragment where I can reproduce my issue:


        if (visualDepthTexture) {
            glViewport(100, screenHeight - (screenHeight / 5) - 100, screenWidth / 5, screenHeight / 5);
            glClearDepthf(1f);
            glClear(GL_DEPTH_BUFFER_BIT);
            glActiveTexture(GL_TEXTURE0);
            testTexture.bind();
            depthTexture.bind().compareMode(GL_NONE).unbind();
            glDrawBuffer(GL_BACK);
            depthBox.draw(0);
            testTexture.unbind();
        }

More specifically, the following sequence:


            testTexture.bind();
            depthTexture.bind().compareMode(GL_NONE).unbind();
            glDrawBuffer(GL_BACK);
            depthBox.draw(0);
            testTexture.unbind();

Is resulting in faulty code, and the output of the draw is a black rectangle.

When using the following codesnippet:


            depthTexture.bind().compareMode(GL_NONE);
            glDrawBuffer(GL_BACK);
            depthBox.draw(0);
            depthTexture.unbind();

The result is a gray output, as is according to my current expectations.

When I use the following code:


            testTexture.bind();
            depthTexture.bind().compareMode(GL_NONE).unbind();
            testTexture.bind();
            glDrawBuffer(GL_BACK);
            depthBox.draw(0);
            testTexture.unbind();

The output is the texture stored in testTexture, as expected.

This leads me to think that my Texture class is broken:


public class Texture {
    private int textureId;
    private boolean created = false;

    private final int textureType;

    public Texture(final int textureType) {
        this.textureType = textureType;
    }

    public Texture create() {
        assertNotCreated();
        textureId = GL11.glGenTextures();

        created = true;
        return this;
    }

    public Texture bind() {
        assertCreated();
        GL11.glBindTexture(textureType, textureId);
        return this;
    }

    public Texture minFilter(final int value) {
        assertCreated();
        GL11.glTexParameteri(textureType, GL11.GL_TEXTURE_MIN_FILTER, value);
        return this;
    }

    public Texture magFilter(final int value) {
        assertCreated();
        GL11.glTexParameteri(textureType, GL11.GL_TEXTURE_MAG_FILTER, value);
        return this;
    }

    public Texture compareMode(final int value) {
        assertCreated();
        GL11.glTexParameteri(textureType, GL14.GL_TEXTURE_COMPARE_MODE, value);
        return this;
    }

    public Texture compareFunc(final int value) {
        assertCreated();
        GL11.glTexParameteri(textureType, GL14.GL_TEXTURE_COMPARE_FUNC, value);
        return this;
    }

    //TODO possibly push down into Texture2D for example and rename to normal storage()
    public Texture storage2D(final int levels, final int internalFormat, final int width, final int height) {
        assertCreated();
        GL42.glTexStorage2D(textureType, levels, internalFormat, width, height);
        return this;
    }

    public void delete() {
        assertCreated();
        GL11.glDeleteTextures(textureId);
    }

    public void unbind() {
        assertCreated();
        GL11.glBindTexture(textureType, 0);
    }

    public int getTextureId() {
        return textureId;
    }

    public int getTextureType() {
        return textureType;
    }

    public boolean hasBeenCreated() {
        return created;
    }

    private void assertCreated() {
        if (!hasBeenCreated()) {
            throw new RuntimeException("Texture has not been created.");
        }
    }

    private void assertNotCreated() {
        if (hasBeenCreated()) {
            throw new RuntimeException("Texture has been created already.");
        }
    }

    @Override
    public String toString() {
        return "Texture(" + textureId + ", " + created + ", " + textureType + ")";
    }
}

Values for textureType = GL11.GL_TEXTURE_2D in all cases.

If it is really the case that textures are not getting unbound properly, then this may explain all weird issues I have been getting until now.
Also just to doublecheck, the textures (depthTexture, testTexture) have resp. ID’s 1 and 2.

Can’t figure out how to delete the question, but figured out that it is of course not working as no texture is bound when I do the actual draw call.