<p>Core OpenGL has no mechanism for this. The <a href="https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_compute_variable_group_size.txt" rel="nofollow noreferrer">ARB_compute_variable_group_size</a> extension provides such a feature. It doesn't allow for indirect compute operations though.</p>

<p>It <a href="http://opengl.gpuinfo.org/listreports.php?extension=GL_ARB_compute_variable_group_size" rel="nofollow noreferrer">doesn't seem to have much AMD support</a> though (outside of open source Linux drivers). And Intel support is nil.</p>

CC BY-SA 4.0
2018-09-03T17:01:34.620Z
2018-09-03T17:01:34.620Z
Nicol Bolas
<p>In addition to the more direct existing answer, there <em>is</em> a way to work around this in unextended OpenGL to some degree, feeding from the fact that in contrast to CUDA OpenGL compute shaders are <em>not</em> pre-compiled with application build (well, they <em>can</em> nowadays, but aren't by default). Instead you <em>usually</em> compile your compute shaders at some point during application runtime.</p>

<p>So a way to achieve a somewhat customizable workgroup size is to use a <strong>macro for it and then redefine this dynamically during application runtime but <em>before</em> shader compile time</strong>.</p>

<pre><code>layout(local_size_x = BLOCKSIZE) in;
</code></pre>

<p>And in the application all you have to do is prepend a <code>"#define BLOCKSIZE &lt;whatever&gt;\n"</code> to the shader source string you feed into <code>glCompileShader</code>. Of course in practice you have to care about stuff like the <code>#version</code> directive being the first line in a shader. You could as well have the define in the shader code already (maybe with a meaningful default size) and do a text replacement instead. But you get the idea.</p>

<p>Of course this means <strong>recompiling your compute shader for each and every workgroup size</strong> you're using. It's practically not much different from just having different shaders for each workgroup size. But at least it's a way to dynamically select the workgroup size during runtime while maintaining a single shader source.</p>

CC BY-SA 4.0
2018-09-04T13:03:35.783Z
2018-09-04T13:03:35.783Z
Christian Rau
<p>I am migrating code from CUDA to OpenGL compute shaders.
I could not find a way to set the local workgroup size outside the shader; is there a way to set gl_WorkGroupSize in the calling program like one can do for gl_NumWorkGroups?
Thanks in advance</p>

<opengl><compute-shader><cuda>
Can gl_WorkGroupSize be set outside the compute shader?
Vectorizer
# Can gl_WorkGroupSize be set outside the compute shader?

(Asked by: username_0 on 2018-09-03)

I am migrating code from CUDA to OpenGL compute shaders.
I could not find a way to set the local workgroup size outside the shader; is there a way to set gl\_WorkGroupSize in the calling program like one can do for gl\_NumWorkGroups?
Thanks in advance

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


---

## 2 answers

### Answer by username_1 on 2018-09-03. Score: 4 | Accepted answer

Core OpenGL has no mechanism for this. The [ARB\_compute\_variable\_group\_size](https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_compute_variable_group_size.txt) extension provides such a feature. It doesn't allow for indirect compute operations though.

It [doesn't seem to have much AMD support](http://opengl.gpuinfo.org/listreports.php?extension=GL_ARB_compute_variable_group_size) though (outside of open source Linux drivers). And Intel support is nil.


---

### Answer by username_2 on 2018-09-04. Score: 1

In addition to the more direct existing answer, there *is* a way to work around this in unextended OpenGL to some degree, feeding from the fact that in contrast to CUDA OpenGL compute shaders are *not* pre-compiled with application build (well, they *can* nowadays, but aren't by default). Instead you *usually* compile your compute shaders at some point during application runtime.

So a way to achieve a somewhat customizable workgroup size is to use a **macro for it and then redefine this dynamically during application runtime but *before* shader compile time**.

```
layout(local_size_x = BLOCKSIZE) in;

```

And in the application all you have to do is prepend a `"#define BLOCKSIZE <whatever>\n"` to the shader source string you feed into `glCompileShader`. Of course in practice you have to care about stuff like the `#version` directive being the first line in a shader. You could as well have the define in the shader code already (maybe with a meaningful default size) and do a text replacement instead. But you get the idea.

Of course this means **recompiling your compute shader for each and every workgroup size** you're using. It's practically not much different from just having different shaders for each workgroup size. But at least it's a way to dynamically select the workgroup size during runtime while maintaining a single shader source.


---

