Jump to content
  • Advertisement
Sign in to follow this  

Problem Using Compute Shader To Write To Texture

This topic is 712 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


I've recently been attempting to render 3D textures in a compute shader for an upcoming project of mine. However, I have met an unexpected issue I cannot solve. No matter what I tried the texture was not written to. In order to ascertain what is causing the defect I set up a simple test case consisting of an all red 16x16 texture being created on a compute shader and then rendered to the screen using a full-screen quad. Yet the issue persists. I'm not too sure where to go next. I've tried substituting glTexImage2D(...) with glTexStorage2D(...) to no avail. I am at a loss here so any help would be very much appreciated.


Relevant Code:


compute shader:

#version 450 core
layout(local_size_x = 1, local_size_y = 1) in;
layout(rgba32f) uniform image2D destination;

void main(void)
	ivec2 index = ivec2(gl_GlobalInvocationID.xy);
	imageStore(destination, index, vec4(1.0f, 0.0f, 0.0f, 1.0f));

compute shader dispatch method:

void draw_texture(GLuint *texture_handle, GLuint draw_program)
	GLuint tex;
	glGenTextures(1, &tex);
	glActiveTexture(GL_TEXTURE0 + 0);
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 16, 16, 0, GL_RGBA, GL_FLOAT, NULL);
	GLuint uniform_location = glGetUniformLocation(draw_program, "destination");
	glBindImageTexture(uniform_location, tex, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
	glDispatchCompute(16, 16, 1);
	*texture_handle = tex;

 Thanks for your time!

Share this post

Link to post
Share on other sites

How do you attempt to read the data? With image load/store you need manual synchronization (using glMemoryBarrier, ) to make sure the data is available in successive drawing operations or compute calls. For example if you want to fetch from the texture in a shader directly after writing to it, you need a glMemoryBarrier(GL_TEXTURE_FETCH_MEMORY_BIT) in between the dispatch call and the draw call that uses the texture.


edit: Whoops, saw you already have a memory barrier in there. Still, you should make sure the correct flag is set. With the SHADER_IMAGE_ACCESS_BARRIER_BIT the correct synchronization is only guaranteed if you use this image in subsequent calls by reading from it with image load/store as well (so no normal texture fetching for example).


yet another edit: Also, the memoryBarrier call is before the dispatch. If you access the texture after, you still need another barrier after the dispatch call. The barrier before it only ensures synchronization with everything that happens before it.

Edited by agleed

Share this post

Link to post
Share on other sites

I assume you're checking for GL errors and have none? Have you tried running it via a debugger such as RenderDoc, and using it to check the contents of the texture before / after the dispatch?

Share this post

Link to post
Share on other sites

 Thanks for all the replies. I've switched to glTexStorage2D, removed the memory barrier call from before the dispatch and moved it after along with changing the flag to GL_TEXTURE_FETCH_BARRIER_BIT, and added calls to glGetError.  This has revealed the following errors:


Invalid value error after the following call:

glTexStorage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 16, 16);


I'll check out RenderDoc and attempt to debug this.


EDIT: I had accidentally set levels to 0 rather than 1. Fixing this has allowed the texture to be rendered from the compute shader.

Thanks for all the help!

Edited by multifractal

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!