I am using a commandbuffer to draw a quad in front of the camera. The code works using CommandBuffer.DrawProcedural but when I use CommandBuffer.DrawProceduralIndirect the Unity Editor interface becomes unresponsive and I have to kill the application.
Here is the CommandBuffer.DrawProcedural version
using UnityEngine;
using UnityEngine.Rendering;
using System.Collections;
using System.Runtime.InteropServices;
// Apply Script to Camera GameObject
public class MyCommandBuffer : MonoBehaviour {
public Shader shader;
ComputeBuffer cbPoints;
void Start () {
Material mat = new Material(this.shader);
Camera cam = this.GetComponent<Camera>();
CommandBuffer cb = new CommandBuffer();
var verts = new Vector4[6] { new Vector4(-1, -1, 0, 1),
new Vector4(1, 1, 0, 1),
new Vector4(1, -1, 0, 1),
new Vector4(1, 1, 0, 1),
new Vector4(-1, -1, 0, 1),
new Vector4(-1, 1, 0, 1) };
this.cbPoints = new ComputeBuffer(6, Marshal.SizeOf(typeof(Vector4)), ComputeBufferType.Default);
this.cbPoints.SetData(verts);
mat.SetBuffer("quadVerts", this.cbPoints);
cb.name = "stencil mask";
cb.DrawProcedural(cam.cameraToWorldMatrix, mat, 0, MeshTopology.Triangles, 6, 1);
cam.AddCommandBuffer(CameraEvent.BeforeForwardOpaque, cb);
}
void OnDestroy () {
if (this.cbPoints != null) this.cbPoints.Release();
this.cbPoints = null;
}
}
And here is the same code using CommandBuffer.DrawProceduralIndirect that is causing the Unity Editor to hang.
using UnityEngine;
using UnityEngine.Rendering;
using System.Collections;
using System.Runtime.InteropServices;
// Apply Script to Camera GameObject
public class MyCommandBuffer : MonoBehaviour {
public Shader shader;
ComputeBuffer cbPoints;
void Start () {
Material mat = new Material(this.shader);
Camera cam = this.GetComponent<Camera>();
CommandBuffer cb = new CommandBuffer();
var verts = new Vector4[6] { new Vector4(-1, -1, 0, 1),
new Vector4(1, 1, 0, 1),
new Vector4(1, -1, 0, 1),
new Vector4(1, 1, 0, 1),
new Vector4(-1, -1, 0, 1),
new Vector4(-1, 1, 0, 1) };
this.cbPoints = new ComputeBuffer(6, Marshal.SizeOf(typeof(Vector4)), ComputeBufferType.IndirectArguments);
this.cbPoints.SetData(verts);
mat.SetBuffer("quadVerts", this.cbPoints);
cb.name = "stencil mask";
cb.DrawProceduralIndirect(cam.cameraToWorldMatrix, mat, 0, MeshTopology.Triangles, this.cbPoints);
cam.AddCommandBuffer(CameraEvent.BeforeForwardOpaque, cb);
}
void OnDestroy () {
if (this.cbPoints != null) this.cbPoints.Release();
this.cbPoints = null;
}
}
Here is a simply shader to supply it:
Shader "testShader" {
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
struct input {
uint id : SV_VertexID;
uint inst : SV_InstanceID;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
StructuredBuffer<float4> quadVerts;
v2f vert(input i) {
v2f o;
o.pos = quadVerts[i.id];
o.uv = quadVerts[i.id].xy * 0.5 + 0.5;
return o;
}
half4 frag(v2f i) : COLOR {
return half4(i.uv.x, i.uv.y, 0, 1);
}
ENDCG
}
}
}
I’d appreciate any help in trying to get this working. Maybe I am misunderstanding how to use DrawProceduralIndirect with respect to the setup of my ComputeBuffer.