<p>Pixel transfer operations are governed by more than just your parameters to a pixel transfer function. They are also governed by <a href="https://www.khronos.org/opengl/wiki/Pixel_Transfer#Pixel_transfer_parameters" rel="noreferrer">parameters passed to the <code>glPixelStore</code> functions</a>.</p>

<p>Of particular interest to you is <code>GL_PACK_ALIGNMENT</code>. This tells OpenGL what the byte alignment for each row of data is in your array. By default, this value is <em>four</em>, not one.</p>

<p>This means that OpenGL will believe that the second row of your image starts at this address: <code>&amp;myData[1028]</code>, since 1025 is not aligned to 4 bytes. And that means it will run off the end of your allocated buffer eventually.</p>

<p>If you are using data with 1-byte alignment for rows, you must inform OpenGL of this by calling <code>glPixelStorei(GL_PACK_ALIGNMENT, 1);</code>. Note that <a href="https://stackoverflow.com/a/11045278/734069">you should not do this by default; do it only when dealing with byte aligned data</a>.</p>

CC BY-SA 3.0
2018-01-13T16:57:47.890Z
2018-01-13T16:57:47.890Z
Nicol Bolas
<p>I'm trying to create single-channel (no mipmaps) texture initialized with my custom pixel data, but it crashes on glTextureSubImage2D. Any idea why?</p>

<pre><code>const std::vector&lt;GLubyte&gt; myData(1025 * 1025, 0);
GLuint texture;
glCreateTextures(GL_TEXTURE_2D, 1, &amp;texture);
glTextureStorage2D(texture, 1, GL_R8, 1025, 1025);
glTextureSubImage2D(texture, 0, 0, 0, 1025, 1025, GL_RED, GL_UNSIGNED_BYTE, &amp;myData[0]);
</code></pre>

<opengl><texture>
OpenGL: Why glTextureSubImage2D crashes?
regnar
# OpenGL: Why glTextureSubImage2D crashes?

(Asked by: username_0 on 2018-01-13)

I'm trying to create single-channel (no mipmaps) texture initialized with my custom pixel data, but it crashes on glTextureSubImage2D. Any idea why?

```
const std::vector<GLubyte> myData(1025 * 1025, 0);
GLuint texture;
glCreateTextures(GL_TEXTURE_2D, 1, &texture);
glTextureStorage2D(texture, 1, GL_R8, 1025, 1025);
glTextureSubImage2D(texture, 0, 0, 0, 1025, 1025, GL_RED, GL_UNSIGNED_BYTE, &myData[0]);

```

**Tags:** opengl, texture


---

## 1 answers

### Answer by username_1 on 2018-01-13. Score: 5

Pixel transfer operations are governed by more than just your parameters to a pixel transfer function. They are also governed by [parameters passed to the `glPixelStore` functions](https://www.khronos.org/opengl/wiki/Pixel_Transfer#Pixel_transfer_parameters).

Of particular interest to you is `GL_PACK_ALIGNMENT`. This tells OpenGL what the byte alignment for each row of data is in your array. By default, this value is *four*, not one.

This means that OpenGL will believe that the second row of your image starts at this address: `&myData[1028]`, since 1025 is not aligned to 4 bytes. And that means it will run off the end of your allocated buffer eventually.

If you are using data with 1-byte alignment for rows, you must inform OpenGL of this by calling `glPixelStorei(GL_PACK_ALIGNMENT, 1);`. Note that [you should not do this by default; do it only when dealing with byte aligned data](https://stackoverflow.com/a/11045278/734069).


---

