<p>well, if anyone is stumbling across this and needs to know what solved it for me: We can bind a compute buffer to global shader memory if we just know what exactly we need to do</p>

<p>within the Compute shader the RWstructuredBuffer is declared as (without the "\" infront of float):</p>

<pre><code>uniform RWStructuredBuffer&lt;\float3&gt; buffer : register(u1);
</code></pre>

<p>(not certain whether <code>register(u1)</code> is really needed</p>

<p>within the vert/frag shader the buffer is defined as:</p>

<pre><code>uniform StructuredBuffer&lt;\float3&gt; buffer;   
</code></pre>

<p>In a c# script before the first kernel dispatch (we only need to assign this buffer once), I assign the buffer via</p>

<pre><code>buffer = new ComputeBuffer(size, sizeof(float)*3,ComputeBufferType.Default);
Shader.SetGlobalBuffer(Shader.PropertyToID("buffer"), buffer);
</code></pre>

<p>It took me ages to find this and it works as far as I can tell. <a href="https://www.tzaeru.com/site/compute-shader-basics-with-unity/" rel="nofollow noreferrer">The article I finally stumbled over that did something similar</a>. I do not have to read the data back to cpu with buffer.GetData(container) and put it into the material manually. 
If the compute shader changes the buffer data this will be reflected in the vert/frag shader immediately. </p>

CC BY-SA 4.0
2020-05-27T07:22:34.653Z
Thanks for coming back to answer your own question and to help people with the same problem in the future. Really appreciate this.
wychmaster
CC BY-SA 4.0
2020-05-24T19:09:48.757Z
2020-05-28T10:27:10.550Z
2020-05-28T10:27:10.550Z
AverageGatsby
<p>I am relatively new to compute shaders and gpgpu in general so bear with me if i am missing the point somewhere. </p>

<p>The main thing i do not yet understand / see is how to get memory from a compute shader to global shader memory usable in a pixel shader without passing it back and forth between GPU and CPU. </p>

<p>I am implementing an irradiance caching lighting solution with Unity3D for learning purposes. I have written a compute shader that calculates the spherical harmonic coefficients for each volume. Now i can go ahead and read those values back to gpu using buffer.GetData() on the cpu side and shader.SetGlobalVectorArray(), but as far as I understand that will copy the memory to CPU resulting in allocation overhead. </p>

<p>I have seen similar posts where people were suggesting just doing the computation in a pixel shader to begin with, but i don't see how that is supposed to help? Which pixel shader and how? I only want these computations to be done once for each volume and they require parallel reduction. </p>

<p>Potentially my search terms are inadequate but i cannot find any answer to how to get results from compute shaders to global shader memory. 
Thanks in advance </p>

<compute-shader><unity><buffers>
Sharing memory between compute shader and pixel shader
AverageGatsby
# Sharing memory between compute shader and pixel shader

(Asked by: username_0 on 2020-03-29)

I am relatively new to compute shaders and gpgpu in general so bear with me if i am missing the point somewhere.

The main thing i do not yet understand / see is how to get memory from a compute shader to global shader memory usable in a pixel shader without passing it back and forth between GPU and CPU.

I am implementing an irradiance caching lighting solution with Unity3D for learning purposes. I have written a compute shader that calculates the spherical harmonic coefficients for each volume. Now i can go ahead and read those values back to gpu using buffer.GetData() on the cpu side and shader.SetGlobalVectorArray(), but as far as I understand that will copy the memory to CPU resulting in allocation overhead.

I have seen similar posts where people were suggesting just doing the computation in a pixel shader to begin with, but i don't see how that is supposed to help? Which pixel shader and how? I only want these computations to be done once for each volume and they require parallel reduction.

Potentially my search terms are inadequate but i cannot find any answer to how to get results from compute shaders to global shader memory.
Thanks in advance

**Tags:** compute-shader, unity, buffers


---

## 1 answers

### Answer by username_0 on 2020-05-24. Score: 3 | Accepted answer

well, if anyone is stumbling across this and needs to know what solved it for me: We can bind a compute buffer to global shader memory if we just know what exactly we need to do

within the Compute shader the RWstructuredBuffer is declared as (without the "\" infront of float):

```
uniform RWStructuredBuffer<\float3> buffer : register(u1);

```

(not certain whether `register(u1)` is really needed

within the vert/frag shader the buffer is defined as:

```
uniform StructuredBuffer<\float3> buffer;   

```

In a c# script before the first kernel dispatch (we only need to assign this buffer once), I assign the buffer via

```
buffer = new ComputeBuffer(size, sizeof(float)*3,ComputeBufferType.Default);
Shader.SetGlobalBuffer(Shader.PropertyToID("buffer"), buffer);

```

It took me ages to find this and it works as far as I can tell. [The article I finally stumbled over that did something similar](https://www.tzaeru.com/site/compute-shader-basics-with-unity/). I do not have to read the data back to cpu with buffer.GetData(container) and put it into the material manually.
If the compute shader changes the buffer data this will be reflected in the vert/frag shader immediately.

> **Comments:**
> **username_1**: Thanks for coming back to answer your own question and to help people with the same problem in the future. Really appreciate this.  

---

