Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

What am I doing wrong (RWStructuredBuffer)

Discussion in 'Shaders' started by justin_kasowski, Apr 28, 2020.

  1. justin_kasowski

    justin_kasowski

    Joined:
    Jan 14, 2020
    Posts:
    45
    I've taken out a lot of the code but the problem lies somewhere in passing the RWStructuredBuffer and I've included how I'm declaring it... Debug prints out "0" for simulationVariableArray[0] in the C# code... It would appear it isn't being passed to the shader correctly. Getting a pink screen and occassionally popping the error:
    "Shader error in 'TriplePassShaderWithStructuredBuffer': maximum UAV register index exceeded, target has 0 slots, manual bind to slot u1 failed at line 256 (on d3d11)"

    I declared "register(u1)" in the shader and "Graphics.SetRandomWriteTarget(1, simulationVariablesBuffer, true);" in the C# code. Anyone know what I'm missing?


    Inside shader:
    Code (CSharp):
    1. GrabPass {
    2.            ...
    3.            
    4.             sampler2D _GrabTex2;
    5.      
    6.        // Variables passed in from RunShaderWithBuffer
    7.             uniform RWStructuredBuffer<float4> simulationVariables : register(u1); // x = r1, y = r2, z = r3, w = ca
    8.            ...
    9.  
    10.              fixed4 frag(v2f i) : SV_Target{
    11.  
    12.                    fixed4 col = tex2D(_GrabTex2, i.grabPos);        
    13.                    col.r = simulationVariables[0].x; // xPixel * yResolution + yPixel].x;
    14.                     col.b = simulationVariables[0].x; //xPixel * yResolution + yPixel].x;
    15.                     col.g = simulationVariables[0].x; //xPixel * yResolution + yPixel].x;
    16.                     col.a = 1.0;
    17.                    return col;
    18.                    
    19.                }

    Inside C#
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using System;
    6.  
    7. public class RunShaderWithBuffer : MonoBehaviour
    8. {
    9.     public Material mater;
    10.     private RenderTexture buff;
    11.  
    12.     // Update is called once per frame
    13.     private float lastUpdateTime = 0;
    14.     public float updateInterval = 0f;
    15.    
    16.     //Structured buffer variables
    17.     private ComputeBuffer electrodePositionBuffer;
    18.     ComputeBuffer simulationVariablesBuffer; // x = r1, y = r2, z = r3, w = ca
    19.     private bool firstRender = true;
    20.     Vector4[] simulationVariableArray;
    21.    
    22.    ...
    23.  
    24.     private void OnRenderImage(RenderTexture source, RenderTexture destination)
    25.     {
    26.         if (firstRender)
    27.         {
    28.             width = source.width;
    29.             height = source.height;
    30.             lastFrameTime = Time.time;
    31.             setVariables();
    32.  
    33.             float[] electrodePositionArray1D = CSMain1D();
    34.             stride = System.Runtime.InteropServices.Marshal.SizeOf(typeof(float));
    35.             electrodePositionBuffer = new ComputeBuffer( electrodePositionArray1D.Length, stride, ComputeBufferType.Default);
    36.             electrodePositionBuffer.SetData(electrodePositionArray1D);
    37.  
    38.             simulationVariableArray = InitializeSimulationVariables();
    39.             stride = System.Runtime.InteropServices.Marshal.SizeOf(typeof(Vector4));
    40.             simulationVariablesBuffer = new ComputeBuffer(simulationVariableArray.Length, stride, ComputeBufferType.Default);
    41.             simulationVariablesBuffer.SetData(simulationVariableArray);
    42.             Graphics.SetRandomWriteTarget(1, simulationVariablesBuffer, true);
    43.            
    44.             mater.SetBuffer("electrodePositionBuffer", electrodePositionBuffer);
    45.             mater.SetBuffer("simulationVariables", simulationVariablesBuffer);
    46.             mater.SetFloat("xPixelSize", (float)(1.0/width));
    47.             mater.SetFloat("yPixelSize", (float)(1.0/height));
    48.             mater.SetInt("xElectrodes", xElectrodes);
    49.             mater.SetInt("yElectrodes", yElectrodes);
    50.             mater.SetInt("xResolution", width);
    51.             mater.SetInt("yResolution", height);
    52.             mater.SetInt("numberElectrodes", numberElectrodes);
    53.             mater.SetInt("fov", fov);
    54.             mater.SetFloat("rho", rho);
    55.             mater.SetFloat("threshold", threshold);
    56.             firstRender = false;
    57.         }
    58.  
    59.         /*frameRenderTime = Time.time - lastFrameTime;
    60.         currentTime = frameRenderTime.ToString("f6");
    61.         currentTime = "Frame refresh rate is: " + currentTime + " sec.";
    62.         Debug.Log(currentTime);
    63.         lastFrameTime = Time.time; */
    64.  
    65.         //Debug.Log(simulationVariableArray[0].x);
    66.        
    67.        
    68.        
    69.         if (Time.time > lastUpdateTime + updateInterval)
    70.         {
    71.             Graphics.Blit(source, buff, mater);
    72.             lastUpdateTime = Time.time;
    73.         }
    74.         Graphics.Blit(buff, destination);
    75.     }
    76.  
    77.     private void OnApplicationQuit()
    78.     {
    79.         electrodePositionBuffer.Release();
    80.         simulationVariablesBuffer.Release();
    81.     }
    82. }
    83.  
     
  2. justin_kasowski

    justin_kasowski

    Joined:
    Jan 14, 2020
    Posts:
    45
    After hours upon hours of struggling with this, I realized I didn't have the line "#pragma target 5.0"
     
  3. smartplay

    smartplay

    Joined:
    Feb 13, 2019
    Posts:
    19
    Thank you it helped me
     
  4. JSmithIR

    JSmithIR

    Joined:
    Apr 13, 2023
    Posts:
    84
    Your hours of struggling has helped another helpless soul! :D