<p>The <strong>2nd argument</strong> of glTextureStorage2D() API is <strong>mip-map levels</strong>, if you specify it as <strong>0</strong>, it causes troubles you may have.</p>
<pre><code>glTextureStorage2D(m_texture, 0, m_internalFormat, (GLsizei)width, (GLsizei)height);
</code></pre>

CC BY-SA 4.0
2023-02-22T16:32:05.307Z
2023-02-22T16:32:05.307Z
Duncan Chen
<p>For the old approach, generating a texture for the ImageTexture for compute shaders would look like this:</p>
<pre><code>glGenTextures(1, &amp;m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, (GLsizei)m_width, (GLsizei)m_height, 0, GL_RGBA, GL_FLOAT, nullptr);
</code></pre>
<p>However, in DSA, glTexImage2D is no longer a thing and we need to use <code>glTextureStorage2D</code> and <code>glTextureSubImage2D</code> to replace it.
Yet, the new DSA functions don't work with ImageTextures, when I try to create texture for the ImageTextures, the texture returns black. Here is the code:</p>
<pre><code>glCreateTextures(GL_TEXTURE_2D, 1, &amp;m_texture);
glTextureStorage2D(m_texture, 0, m_internalFormat, (GLsizei)width, (GLsizei)height);

glTextureParameteri(m_texture, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextureParameteri(m_texture, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTextureParameteri(m_texture, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTextureParameteri(m_texture, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTextureSubImage2D(m_texture, 0, 0, 0, (GLsizei)width, (GLsizei)height, m_dataFormat, GL_UNSIGNED_BYTE, nullptr);
</code></pre>
<p>I've read about that the new DSA functions are immutable textures and maybe that causes this problem? If that is the case, what is the DSA counterpart for creating mutable textures?</p>

CC BY-SA 4.0
2023-02-17T18:35:03.783Z
What do you mean by "image texture"?
Nicol Bolas
CC BY-SA 4.0
2023-02-17T18:36:58.890Z
You cannot pass a `nullptr` to `glTex(ture)SubImage*D`. If you don't want to fill the texture with data, then just don't call it.
Nicol Bolas
<opengl><texture><compute-shader>
How to properly create textures using DSA for ImageTexture in OpenGL
Liyuu_
# How to properly create textures using DSA for ImageTexture in OpenGL

(Asked by: username_0 on 2023-02-17)

For the old approach, generating a texture for the ImageTexture for compute shaders would look like this:

```
glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, (GLsizei)m_width, (GLsizei)m_height, 0, GL_RGBA, GL_FLOAT, nullptr);

```

However, in DSA, glTexImage2D is no longer a thing and we need to use `glTextureStorage2D` and `glTextureSubImage2D` to replace it.
Yet, the new DSA functions don't work with ImageTextures, when I try to create texture for the ImageTextures, the texture returns black. Here is the code:

```
glCreateTextures(GL_TEXTURE_2D, 1, &m_texture);
glTextureStorage2D(m_texture, 0, m_internalFormat, (GLsizei)width, (GLsizei)height);

glTextureParameteri(m_texture, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextureParameteri(m_texture, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTextureParameteri(m_texture, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTextureParameteri(m_texture, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTextureSubImage2D(m_texture, 0, 0, 0, (GLsizei)width, (GLsizei)height, m_dataFormat, GL_UNSIGNED_BYTE, nullptr);

```

I've read about that the new DSA functions are immutable textures and maybe that causes this problem? If that is the case, what is the DSA counterpart for creating mutable textures?

**Tags:** opengl, texture, compute-shader

> **Comments:**
> **username_1**: What do you mean by "image texture"?  
> **username_1**: You cannot pass a `nullptr` to `glTex(ture)SubImage*D`. If you don't want to fill the texture with data, then just don't call it.  

---

## 1 answers

### Answer by username_2 on 2023-02-22. Score: 0

The **2nd argument** of glTextureStorage2D() API is **mip-map levels**, if you specify it as **0**, it causes troubles you may have.

```
glTextureStorage2D(m_texture, 0, m_internalFormat, (GLsizei)width, (GLsizei)height);

```


---

