Reputation: 331
I want to use image2D as 2D storage for vertices which will be modified by compute shader but things doesnt work.
Create textures:
glGenTextures(1, &HeightMap);
glBindTexture(GL_TEXTURE_2D, HeightMap);
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA32F, 513, 513, 0,GL_RGBA32F, GL_UNSIGNED_BYTE, 0);
Use and dispatch compute shader:
glUseProgram(ComputeProgram);
glActiveTexture(GL_TEXTURE0);
glBindImageTexture(0, HeightMap, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
glDispatchCompute(1, 1, 1 );
glMemoryBarrier( GL_ALL_BARRIER_BITS );
And compute shader:
#version 430 core
layout( std430, binding=1 ) buffer VertBuffer
{
vec4 Positions[ ];
};
layout( local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout (binding=0, rgba32f) uniform image2D HeightMap;
void main (void)
{
imageStore(HeightMap, ivec2(0,0),vec4(0,0,0,1));
Positions[0]=imageLoad(HeightMap, ivec2(0,0)).rgba;
}
Upvotes: 3
Views: 9280
Reputation: 331
Ok i found solution. This is how you use image2D for read and write data with compute shader:
Create texture:
glGenTextures(1, &HeightMap);
glBindTexture(GL_TEXTURE_2D, HeightMap);
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA32F, 513, 513, 0,GL_RGBA, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glGenerateMipmap(GL_TEXTURE_2D);
Dispatch compute shader:
glBindBufferBase( GL_SHADER_STORAGE_BUFFER, 1, VerticesBuffer );
glBindImageTexture(0, HeightMap, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
glUseProgram(ComputeProgram);
glUniform1i(glGetUniformLocation(ComputeProgram, "HeightMap"), 0);
glDispatchCompute(1, 1, 1 );
glMemoryBarrier( GL_ALL_BARRIER_BITS );
Example compute shader:
#version 430 core
layout( std430, binding=1 ) buffer VertBuffer
{
vec4 Positions[ ];
};
layout( local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout (rgba32f) uniform image2D HeightMap;
void main (void)
{
ivec2 pos=ivec2(0,0);
imageStore(HeightMap, pos,vec4(10,0,0,1));
Positions[0].xyzw=imageLoad(HeightMap, pos).rgba;
}
Upvotes: 3
Reputation: 474266
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA32F, 513, 513, 0,GL_RGBA32F, GL_UNSIGNED_BYTE, 0);
Always check your OpenGL errors. This line fails because GL_RGBA32F
is not a legal pixel transfer format. The pixel transfer format only specifies the number of components you're using. It should be GL_RGBA
. This causes an OpenGL error.
Yes, I know you're not actually transfering pixel data. But OpenGL requires that the pixel transfer parameters be legitimate even if you're not actually doing a pixel transfer.
Also, the type (the next parameter) should be GL_FLOAT
, not GL_UNSIGNED_BYTE
.
Upvotes: 2