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.

Rwstructuredbuffer Access In A Vertex Shader

Discussion in 'Shaders' started by lifeFo, Apr 13, 2019.

  1. lifeFo

    lifeFo

    Joined:
    Apr 14, 2017
    Posts:
    15
    Shader here
    Code (CSharp):
    1. Shader "RW Structured Buffer"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "black" {}
    6.     }
    7.     Subshader
    8.     {
    9.         Pass
    10.         {
    11.             CGPROGRAM
    12.             #pragma vertex vertex_shader
    13.             #pragma fragment pixel_shader
    14.             #pragma target 5.0
    15.             sampler2D _MainTex;
    16.              RWStructuredBuffer<float3> data : register(u4);
    17.             struct APPDATA
    18.             {
    19.                 float4 vertex : POSITION;
    20.                 float2 uv : TEXCOORD0;
    21.                 uint id : SV_VertexID;
    22.             };
    23.             struct SHADERDATA
    24.             {
    25.                 float4 vertex : SV_POSITION;
    26.                 float2 uv : TEXCOORD0;
    27.             };
    28.             SHADERDATA vertex_shader (APPDATA IN)
    29.             {
    30.                 SHADERDATA vs;
    31.                // IN.vertex.y = 0.0-tex2Dlod(_MainTex,float4(IN.uv,0,0)).r*(sin(_Time.g)*0.5+0.5);
    32.                 data[IN.id] =float3(3.4,2.4,3.4);
    33.                 vs.vertex = UnityObjectToClipPos(IN.vertex);
    34.                 vs.uv = IN.uv;
    35.                 return vs;
    36.             }
    37.             float4 pixel_shader (SHADERDATA ps) : SV_TARGET
    38.             {
    39.                 return tex2D(_MainTex,ps.uv);
    40.             }
    41.             ENDCG
    42.         }
    43.     }
    44. }
    and script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class tmp : MonoBehaviour {
    6.  
    7.      public Material material;
    8.     public GameObject plane;
    9.     ComputeBuffer compute_buffer;
    10.     Mesh mesh;
    11.     Vector3[] data;
    12.     void Start()
    13.     {
    14.         mesh = plane.GetComponent<MeshFilter>().mesh;
    15.         data = mesh.vertices;
    16.         compute_buffer = new ComputeBuffer(data.Length, sizeof(float)*3, ComputeBufferType.Default);
    17.     }
    18.     void Update()
    19.     {
    20.       Graphics.ClearRandomWriteTargets();
    21.         material.SetPass(0);
    22.         material.SetBuffer("data", compute_buffer);
    23.         Graphics.SetRandomWriteTarget(4, compute_buffer,false);
    24.         compute_buffer.GetData(data);
    25.         if(Time.frameCount%10==0)
    26.         for (int i = 0; i < data.Length; i++)
    27.         {
    28.             Debug.Log(data[i]);
    29.         }
    30.     }
    31.     void OnDestroy()
    32.     {
    33.         compute_buffer.Dispose();
    34.     }
    35. }
    36.  
    log say
    log.PNG
    Afer I set buffer in vertex shader with data[IN.id] =float3(3.4,2.4,3.4);
    Anyone know what's happen here.
     
    Last edited: Apr 17, 2019
  2. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    292
    1. Make sure that you use standard 3D template Unity project. This code currently not work properly with High-Definition RP or Lightweight RP (cause to Graphics.SetRandomWriteTarget with RWStructuredBuffer).
    2. RWStructuredBuffer is supported for all types of shaders in hardware with minimum D3D_FEATURE_LEVEL_11_1.
    (minimum AMD Radeon HD 8000 or NVIDIA GeForce GTX 900 or Intel HD Graphics 5000/4x00 and Windows 8).
    For hardware with D3D_FEATURE_LEVEL_11_0 is only supported for pixel and compute shaders.
     
  3. lifeFo

    lifeFo

    Joined:
    Apr 14, 2017
    Posts:
    15
    I didn't used in High-Definition RP or Lightweight RP .iEditt's just unit shader and i try this on both dx11 and vulkan either not work.i also try this in dx12 it's seems dx12 has completely differ error behaviour ,i guess dx12 still bugging .By the way My griphic card is gtx1070, so i suppose it support vulkan and dx11 dx12.Any way thanks your message. I post this thread because I am not get useful info for my problem from search,I thought may be i can get help from unity guys.