Hey all,
First of all, I’m using Unity 2020.1. I’ve been bashing my head at this particular wall for a few days now, and finally found something working at GitHub - cecarlsen/HowToDrawATriangle: Examples of how to draw a procedural triangle in Unity3D, which was a huge help. The problem is, whenever I try to launch the DrawProceduralIndirectNow + GraphicBuffer indices example it doesn’t work. The one not using a GraphicsBuffer does. Is this a known bug, or is it borked in the current version of Unity?
Here’s my code that also doesn’t work with a GraphicsBuffer, but works without it. Sorry for the curly braces, I come from Java
using UnityEngine;
namespace DrawProceduralIndirect_Quad {
public class DrawQuad : MonoBehaviour {
private static readonly int Vertices = Shader.PropertyToID("vertices");
[SerializeField]
private ComputeShader computeShader;
[SerializeField]
private Material material;
private ComputeBuffer _vertexBuffer;
private GraphicsBuffer _graphicsBuffer;
private GraphicsBuffer _argsBuffer;
private void OnEnable() {
_vertexBuffer = new ComputeBuffer(6, sizeof(float) * 4);
_graphicsBuffer = new GraphicsBuffer(GraphicsBuffer.Target.Index, 6, sizeof(int));
var args = new[] {_vertexBuffer.count, 1, 0, 0};
_argsBuffer = new GraphicsBuffer(GraphicsBuffer.Target.IndirectArguments, args.Length, sizeof(int));
_argsBuffer.SetData(args);
computeShader.SetBuffer(0, "vertices", _vertexBuffer);
computeShader.SetBuffer(0, "indices", _graphicsBuffer);
material.SetBuffer(Vertices, _vertexBuffer);
}
private void OnDisable() {
_vertexBuffer.Release();
_graphicsBuffer.Release();
_argsBuffer.Release();
}
private void Update() {
Graphics.ClearRandomWriteTargets();
Graphics.SetRandomWriteTarget(1, _vertexBuffer, false);
Graphics.SetRandomWriteTarget(1, _graphicsBuffer, false);
computeShader.Dispatch(0, 1, 1, 1);
var vertices = new Vector4[4];
var indices = new int[6];
_vertexBuffer.GetData(vertices);
_graphicsBuffer.GetData(indices);
// this logs correct values for both vertices and indices
Debug.Log($"vertices: {string.Join(", ", vertices)}");
Debug.Log($"indices: {string.Join(", ", indices)}");
}
private void OnRenderObject() {
material.SetPass(0);
Graphics.DrawProceduralIndirectNow(MeshTopology.Triangles, _graphicsBuffer, _argsBuffer); // doesn't work
// Graphics.DrawProceduralIndirectNow(MeshTopology.Triangles, _argsBuffer); -- this works fine, but draws a triangle because it lacks indices
}
}
}
Compute shader:
#pragma kernel CSMain
RWStructuredBuffer<float4> vertices;
RWStructuredBuffer<int> indices;
[numthreads(1,1,1)]
void CSMain (uint3 id : SV_DispatchThreadID) {
vertices[0] = float4(0, 0, 0, 1);
vertices[1] = float4(0, 1, 0, 1);
vertices[2] = float4(1, 1, 0, 1);
vertices[3] = float4(1, 0, 0, 1);
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
indices[3] = 0;
indices[4] = 2;
indices[5] = 3;
}
Vertex shader:
Shader "Unlit/QuadShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 5.0
#include "UnityCG.cginc"
struct v2f
{
float4 vertex : SV_POSITION;
};
StructuredBuffer<float4> vertices;
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (uint id : SV_VertexID) {
v2f o;
o.vertex = UnityObjectToClipPos(vertices[id]);
return o;
}
fixed4 frag (v2f i) : SV_Target {
return float4(1, 1, 1, 1);
}
ENDCG
}
}
}
I would appreciate any help on this