- Home /
ComputeShader.FindKernel() returns always -1
I tried to experiment with compute shaders today but ran in to some problems: ComputeShader.FindKernel() returns always -1. I looked at the DX11Examples and everything worked fine there with almost same things. Copied a script and a compute shader from there to my project and everything works still fine. Is there anything wrong with my code that could cause this?
//CSScript.cs://
using UnityEngine; using System.Collections;
public class CSScript : MonoBehaviour {
public RenderTexture renderTex;
public ComputeShader compute;
public int FWRKernel;
void Start () {
renderTex = new RenderTexture(128, 128, 0);
renderTex.enableRandomWrite = true;
FWRKernel = compute.FindKernel("FillWithRed"); //Returns -1
compute.SetTexture(FWRKernel, "res", renderTex);
compute.Dispatch(FWRKernel, 4, 4, 1);
renderer.material.SetTexture("_MainTex", renderTex);
}
}
//SSGI.compute://
pragma kernel FillWithRed
Texture2D res;
[numthreads(4,4,1)] void FillWithRed(uint3 dtid : SV_DispatchThreadID) { res[dtid.xy] = float4(1,0,0,1); }
There can be some other errors in my code as I haven´t been able to test it yet.
Edit. The .compute code looks messed up for me when posted if codes are in separate code tags. Sorry for that.
Edit2. Got it working. Replacing Texture2D with RWTexture2D and creating RenderTexture with RenderTexture.Create() worked. Could it be made so that it gives error when something is wrong with the ComputeShader like it does with other type of scripts? Would really save time.