<p>I can see a few things wrong here. </p>

<p>First, you're setting your GL buffer size to <code>sizeof(vertices) * vertices.size()</code>
This looks wrong. What is vertices? A std::array of vec4? Then you want <code>vertices.size() * sizeof(glm::vec4)</code>, or just <code>sizeof(vertices)</code></p>

<p>Second, as wandering-warrior said, you want to be binding to <code>GL_ARRAY_BUFFER</code> before calling <code>glVertexAttribPointer</code></p>

<p>Third, I don't know what you're doing with <code>bufferOffset</code> in the second part of the code. This is a valid approach if the data is laid out sequentially in one large buffer (vertices then normals then tangents etc. etc.). But in your case you're using a separate buffer for each data set - so each attribute should start reading from the beginning of its own buffer, i.e. <code>bufferOffset</code> should always be 0. </p>

<p>Fourth, you don't seem to be actually <em>using</em> your compute shader anywhere. It's not enough to just call <code>glUseProgram</code>, you need to call <code>glDispatchCompute</code> after you bind your buffers to actually run it.</p>

<p>Also, it's not common practice to store binormals in an array, since they can be reconstructed in the fragment shader from the tangent and normal using a cross product.</p>

<p>I would do it something like this (using OpenGL 4.5 with the new Direct State Access functions to save all those buffer bindings).</p>

<pre><code>int bufferSize = sizeof(vertices);

glCreateBuffers(1, &amp;vbo_Previous);
glNamedBufferData(vbo_Previous, bufferSize, &amp;vertices[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, vbo_Previous);

glCreateBuffers(1, &amp;vbo_Current);
glNamedBufferData(vbo_Current, bufferSize, &amp;vertices[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, vbo_Current);

glCreateBuffers(1, &amp;vbo_Next);
glNamedBufferData(vbo_Next, bufferSize, &amp;vertices[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, vbo_Next);

glCreateBuffers(1, &amp;vbo_Normal);
glNamedBufferData(vbo_Normal, bufferSize, &amp;normals[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, vbo_Normal);

glCreateBuffers(1, &amp;vbo_Tangent);
glNamedBufferData(vbo_Tangent, bufferSize, &amp;tangents[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, vbo_Tangent);

glCreateBuffers(1, &amp;vbo_Texcoords);
glNamedBufferData(vbo_Texcoords, sizeof(texCoords), &amp;texCoords[0], 
GL_STATIC_DRAW);

glCreateBuffers(1, &amp;ebo);
glNamedBufferData(ebo, sizeof(elements), &amp;elements[0], GL_STATIC_DRAW);
//you have element data in an array, right?

glCreateVertexArrays(1, &amp;vao);
glBindVertexArray(vao);

glBindVertexBuffer(0, vbo_Next, 0, 0);
glVertexAttribBinding(0,0);
glVertexAttribFormat(0, 4, GL_FLOAT, GL_FALSE, 0);
glBindVertexBuffer(1, vbo_Normal, 0, 0);
glVertexAttribBinding(1,1);
glVertexAttribFormat(1, 4, GL_FLOAT, GL_FALSE, 0);
glBindVertexBuffer(2, vbo_Tangent, 0, 0);
glVertexAttribBinding(2,2);
glVertexAttribFormat(2, 4, GL_FLOAT, GL_FALSE, 0);
glBindVertexBuffer(3, vbo_Texcoords, 0, 0);
glVertexAttribBinding(3,3);
glVertexAttribFormat(3, 2, GL_FLOAT, GL_FALSE, 0);
glVertexArrayElementBuffer(vao, ebo);

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);

computeShader.use();
glDispatchCompute (/*number of groups*/)
computeShader.unUse();

glMemoryBarrier(GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT);

renderShader.use();
glDrawElements(GL_TRIANGLES, elements.size(), GL_UNSIGNED_INT, 0);
renderShader.unUse();
</code></pre>

CC BY-SA 3.0
2017-12-27T06:43:25.317Z
2018-02-06T07:57:14.563Z
2018-02-06T07:57:14.563Z
russ
<p>I think you are confusing all the binding targets thingy. From what I see your vertex data is coming from compute shader after some processing and now you want to pass it to the vertex shader.</p>

<p>You can create a buffer object once, use it as an SSBO for use in a compute shader then use it as a VBO for use in rendering. i.e</p>

<pre><code>// Setup SSBO for use in Compute shader
glGenBuffers(1, &amp;vbo_Current);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Current);
glBufferData(GL_SHADER_STORAGE_BUFFER, bufferSize, &amp;vertices[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, vbo_Current);

// Setup the same Buffer object as VBO for use in rendering
glBindBuffer(GL_ARRAY_BUFFER, vbo_Current);   &lt;- Note the change in binding target
GlVertexAttribPointer().......                &lt;- set your vertex data
</code></pre>

<p>Hence there is no need to generate 1 more buffer object for the texture, just pass the same buffer but change the binding points to <code>GL_ARRAY_BUFFER</code></p>

<p>However do note that you need to use appropriate Memory barrier calls as SSBO reads and writes are incoherent memory accesses. Don't know much about these but you can find more information <a href="https://www.khronos.org/opengl/wiki/Memory_Model" rel="nofollow noreferrer">here</a></p>

CC BY-SA 3.0
2017-12-26T15:49:55.953Z
I think the source of confusion is `target` parameter in `glBufferData`. If buffer object is just data, when why we need to specify this to actually upload raw data?
mdkdy
CC BY-SA 3.0
2017-12-26T15:56:16.357Z
@narthex: Because that's how [OpenGL objects work](https://www.khronos.org/opengl/wiki/OpenGL_Object). Unless you're [using DSA](https://www.khronos.org/opengl/wiki/Direct_State_Access), you have to bind them somewhere to the context to be able to edit them. This is one of the most *consistent* parts of the OpenGL API, and it's basically the first thing anyone should learn about OpenGL.
Nicol Bolas
CC BY-SA 3.0
2017-12-26T14:48:51.100Z
2017-12-26T14:48:51.100Z
gallickgunner
<p>I'm working with OpenGL and facing some difficulties because I'm not familiar with OpenGL. I tried to search related example in Google, but I could not find some useful code.  </p>

<p>There are five arrays.
I bound and changed the data in compute shahder as follows:</p>

<pre><code>computeShader.use();
int bufferSize = sizeof(vertices) * vertices.size();
glGenBuffers(1, &amp;vbo_Previous);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Previous);
glBufferData(GL_SHADER_STORAGE_BUFFER, bufferSize, &amp;vertices[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, vbo_Previous);


glGenBuffers(1, &amp;vbo_Current);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Current);
glBufferData(GL_SHADER_STORAGE_BUFFER, bufferSize, &amp;vertices[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, vbo_Current);

glGenBuffers(1, &amp;vbo_Next);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Next);
glBufferData(GL_SHADER_STORAGE_BUFFER, bufferSize, &amp;vertices[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, vbo_Next);

glGenBuffers(1, &amp;vbo_Normal);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Normal);
glBufferData(GL_SHADER_STORAGE_BUFFER, bufferSize, &amp;normals[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, vbo_Normal);

glGenBuffers(1, &amp;vbo_Tangent);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Tangent);
glBufferData(GL_SHADER_STORAGE_BUFFER, bufferSize, &amp;tangents[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, vbo_Tangent);

glGenBuffers(1, &amp;vbo_Binormal);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Binormal);
glBufferData(GL_SHADER_STORAGE_BUFFER, bufferSize, &amp;binormals[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, vbo_Binormal);
computeShader.unUse();
</code></pre>

<p>And I want to use the data in vertex shader.
I tried like that, but I doesn't work. </p>

<pre><code>glGenVertexArrays(1, &amp;vao);
glGenBuffers(1, &amp;ebo);
glBindVertexArray(vao);


int bufferOffset = 0;
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Next);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, (void*)(bufferOffset));

bufferOffset += sizeof(glm::vec4) * vertices.size();
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Normal);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)(bufferOffset));

bufferOffset += sizeof(glm::vec4) * normals.size();
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Tangent);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, (void*)(bufferOffset));

bufferOffset += sizeof(glm::vec4) * tangents.size();
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Binormal);
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 0, (void*)(bufferOffset));

//Because the texture coordinate are not changed in compute shader I generated the vbo for texture coordinate data
bufferOffset += sizeof(glm::vec4) * binormals.size();
glGenBuffers(1, &amp;vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec2)*texCoords.size(), &amp;texCoords[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, 0, (void*)(bufferOffset));
</code></pre>

<p>I hope to use the data in vertex shader as follows:</p>

<pre><code>#version 430 core
layout(location = 0) in vec4 aPos;
layout(location = 1) in vec4 aNormal;
layout(location = 2) in vec4 aTangent;
layout(location = 3) in vec4 aBinormal;
layout(location = 4) in vec2 aTexCoord;
</code></pre>

<p>How to set vertex object array to use the data?</p>

CC BY-SA 3.0
2017-12-26T12:01:14.767Z
What do you mean by `ComputeShader.use()` ? And if you are passing SSBO to vertex shader you will have to define interface blocks, afaik you can't use them like uniform variables. Check here
https://www.khronos.org/opengl/wiki/Shader_Storage_Buffer_Object
gallickgunner
CC BY-SA 3.0
2017-12-26T13:21:21.607Z
@wandering-warrior Oh, sorry to confuse you. ComputeShader.use() is just glUseProgram(compute.glsl), so we can ingnore it. And I wonder your meaning 'you can't use them like uniform variables'. I don't want to use the data like uniform variables, instead, I just want to pass the whole arrays to vertex shader, probably it means that when I call glDrawElements or glDrawArrays(triangle), the vertex shader would get the array stream.
shashack
<opengl><shader><compute-shader>
How to use the data manipulated in OpenGL Compute Shader
shashack
# How to use the data manipulated in OpenGL Compute Shader

(Asked by: username_0 on 2017-12-26)

I'm working with OpenGL and facing some difficulties because I'm not familiar with OpenGL. I tried to search related example in Google, but I could not find some useful code.

There are five arrays.
I bound and changed the data in compute shahder as follows:

```
computeShader.use();
int bufferSize = sizeof(vertices) * vertices.size();
glGenBuffers(1, &vbo_Previous);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Previous);
glBufferData(GL_SHADER_STORAGE_BUFFER, bufferSize, &vertices[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, vbo_Previous);

glGenBuffers(1, &vbo_Current);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Current);
glBufferData(GL_SHADER_STORAGE_BUFFER, bufferSize, &vertices[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, vbo_Current);

glGenBuffers(1, &vbo_Next);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Next);
glBufferData(GL_SHADER_STORAGE_BUFFER, bufferSize, &vertices[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, vbo_Next);

glGenBuffers(1, &vbo_Normal);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Normal);
glBufferData(GL_SHADER_STORAGE_BUFFER, bufferSize, &normals[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, vbo_Normal);

glGenBuffers(1, &vbo_Tangent);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Tangent);
glBufferData(GL_SHADER_STORAGE_BUFFER, bufferSize, &tangents[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, vbo_Tangent);

glGenBuffers(1, &vbo_Binormal);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Binormal);
glBufferData(GL_SHADER_STORAGE_BUFFER, bufferSize, &binormals[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, vbo_Binormal);
computeShader.unUse();

```

And I want to use the data in vertex shader.
I tried like that, but I doesn't work.

```
glGenVertexArrays(1, &vao);
glGenBuffers(1, &ebo);
glBindVertexArray(vao);

int bufferOffset = 0;
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Next);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, (void*)(bufferOffset));

bufferOffset += sizeof(glm::vec4) * vertices.size();
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Normal);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)(bufferOffset));

bufferOffset += sizeof(glm::vec4) * normals.size();
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Tangent);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, (void*)(bufferOffset));

bufferOffset += sizeof(glm::vec4) * tangents.size();
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Binormal);
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 0, (void*)(bufferOffset));

//Because the texture coordinate are not changed in compute shader I generated the vbo for texture coordinate data
bufferOffset += sizeof(glm::vec4) * binormals.size();
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec2)*texCoords.size(), &texCoords[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, 0, (void*)(bufferOffset));

```

I hope to use the data in vertex shader as follows:

```
#version 430 core
layout(location = 0) in vec4 aPos;
layout(location = 1) in vec4 aNormal;
layout(location = 2) in vec4 aTangent;
layout(location = 3) in vec4 aBinormal;
layout(location = 4) in vec2 aTexCoord;

```

How to set vertex object array to use the data?

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

> **Comments:**
> **username_1**: What do you mean by `ComputeShader.use()` ? And if you are passing SSBO to vertex shader you will have to define interface blocks, afaik you can't use them like uniform variables. Check here https://www.khronos.org/opengl/wiki/Shader_Storage_Buffer_Object  
> **username_0**: @wandering-warrior Oh, sorry to confuse you. ComputeShader.use() is just glUseProgram(compute.glsl), so we can ingnore it. And I wonder your meaning 'you can't use them like uniform variables'. I don't want to use the data like uniform variables, instead, I just want to pass the whole arrays to vertex shader, probably it means that when I call glDrawElements or glDrawArrays(triangle), the vertex shader would get the array stream.  

---

## 2 answers

### Answer by username_2 on 2017-12-27. Score: 2 | Accepted answer

I can see a few things wrong here.

First, you're setting your GL buffer size to `sizeof(vertices) * vertices.size()`
This looks wrong. What is vertices? A std::array of vec4? Then you want `vertices.size() * sizeof(glm::vec4)`, or just `sizeof(vertices)`

Second, as wandering-warrior said, you want to be binding to `GL_ARRAY_BUFFER` before calling `glVertexAttribPointer`

Third, I don't know what you're doing with `bufferOffset` in the second part of the code. This is a valid approach if the data is laid out sequentially in one large buffer (vertices then normals then tangents etc. etc.). But in your case you're using a separate buffer for each data set - so each attribute should start reading from the beginning of its own buffer, i.e. `bufferOffset` should always be 0.

Fourth, you don't seem to be actually *using* your compute shader anywhere. It's not enough to just call `glUseProgram`, you need to call `glDispatchCompute` after you bind your buffers to actually run it.

Also, it's not common practice to store binormals in an array, since they can be reconstructed in the fragment shader from the tangent and normal using a cross product.

I would do it something like this (using OpenGL 4.5 with the new Direct State Access functions to save all those buffer bindings).

```
int bufferSize = sizeof(vertices);

glCreateBuffers(1, &vbo_Previous);
glNamedBufferData(vbo_Previous, bufferSize, &vertices[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, vbo_Previous);

glCreateBuffers(1, &vbo_Current);
glNamedBufferData(vbo_Current, bufferSize, &vertices[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, vbo_Current);

glCreateBuffers(1, &vbo_Next);
glNamedBufferData(vbo_Next, bufferSize, &vertices[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, vbo_Next);

glCreateBuffers(1, &vbo_Normal);
glNamedBufferData(vbo_Normal, bufferSize, &normals[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, vbo_Normal);

glCreateBuffers(1, &vbo_Tangent);
glNamedBufferData(vbo_Tangent, bufferSize, &tangents[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, vbo_Tangent);

glCreateBuffers(1, &vbo_Texcoords);
glNamedBufferData(vbo_Texcoords, sizeof(texCoords), &texCoords[0], 
GL_STATIC_DRAW);

glCreateBuffers(1, &ebo);
glNamedBufferData(ebo, sizeof(elements), &elements[0], GL_STATIC_DRAW);
//you have element data in an array, right?

glCreateVertexArrays(1, &vao);
glBindVertexArray(vao);

glBindVertexBuffer(0, vbo_Next, 0, 0);
glVertexAttribBinding(0,0);
glVertexAttribFormat(0, 4, GL_FLOAT, GL_FALSE, 0);
glBindVertexBuffer(1, vbo_Normal, 0, 0);
glVertexAttribBinding(1,1);
glVertexAttribFormat(1, 4, GL_FLOAT, GL_FALSE, 0);
glBindVertexBuffer(2, vbo_Tangent, 0, 0);
glVertexAttribBinding(2,2);
glVertexAttribFormat(2, 4, GL_FLOAT, GL_FALSE, 0);
glBindVertexBuffer(3, vbo_Texcoords, 0, 0);
glVertexAttribBinding(3,3);
glVertexAttribFormat(3, 2, GL_FLOAT, GL_FALSE, 0);
glVertexArrayElementBuffer(vao, ebo);

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);

computeShader.use();
glDispatchCompute (/*number of groups*/)
computeShader.unUse();

glMemoryBarrier(GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT);

renderShader.use();
glDrawElements(GL_TRIANGLES, elements.size(), GL_UNSIGNED_INT, 0);
renderShader.unUse();

```


---

### Answer by username_1 on 2017-12-26. Score: 1

I think you are confusing all the binding targets thingy. From what I see your vertex data is coming from compute shader after some processing and now you want to pass it to the vertex shader.

You can create a buffer object once, use it as an SSBO for use in a compute shader then use it as a VBO for use in rendering. i.e

```
// Setup SSBO for use in Compute shader
glGenBuffers(1, &vbo_Current);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo_Current);
glBufferData(GL_SHADER_STORAGE_BUFFER, bufferSize, &vertices[0], GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, vbo_Current);

// Setup the same Buffer object as VBO for use in rendering
glBindBuffer(GL_ARRAY_BUFFER, vbo_Current);   <- Note the change in binding target
GlVertexAttribPointer().......                <- set your vertex data

```

Hence there is no need to generate 1 more buffer object for the texture, just pass the same buffer but change the binding points to `GL_ARRAY_BUFFER`

However do note that you need to use appropriate Memory barrier calls as SSBO reads and writes are incoherent memory accesses. Don't know much about these but you can find more information [here](https://www.khronos.org/opengl/wiki/Memory_Model)

> **Comments:**
> **username_3**: I think the source of confusion is `target` parameter in `glBufferData`. If buffer object is just data, when why we need to specify this to actually upload raw data?  
> **username_4**: @narthex: Because that's how [OpenGL objects work](https://www.khronos.org/opengl/wiki/OpenGL_Object). Unless you're [using DSA](https://www.khronos.org/opengl/wiki/Direct_State_Access), you have to bind them somewhere to the context to be able to edit them. This is one of the most *consistent* parts of the OpenGL API, and it's basically the first thing anyone should learn about OpenGL.  

---

