I have read:
http://forum.unity3d.com/threads/148874-Compute-Shaders
http://docs.unity3d.com/Documentation/Manual/ComputeShaders.html
and I have some questions:
- Will it work for non-Pro version? I can create CS but for example OnRenderImage func is not available.
- How to assign texture (or mesh data etc.) to compute shader? I see that we have creating new texture - so also new data in the memory. Is it needed? Do I have to use string related to texture name in my assets browser or sth different?
- If I have multiple compute shaders and meshes, how often per frame will be switched context GPGPU/rendering? Will be executed all compute shaders then rendered all data or not?
Looks like it doesn’t work a little bit better - I forgot to drag and drop the script. Now it has been executed but not evertything is good.
Well known kernel:
#pragma kernel FillWithRed
RWTexture2D<float4> Result;
[numthreads(4,4,1)]
void FillWithRed(uint3 dtid : SV_DispatchThreadID)
{
Result[dtid.xy] = float4(1.0,0.0,0.0,1.0);
}
The script:
using UnityEngine;
using System.Collections;
public class FillWithRed : MonoBehaviour
{
public int width = 128;
public int height = 128;
public ComputeShader compute;
private bool created = false;
private RenderTexture output;
private void Init()
{
if(created)
{
return;
}
else
{
if (!SystemInfo.supportsComputeShaders)
{
Debug.LogWarning ("supportsComputeShaders fail");
return;
}
Debug.Log("init FillWithRed");
output = new RenderTexture (width, height, 0, RenderTextureFormat.ARGB32);
output.enableRandomWrite = true;
output.Create();
created = true;
}
}
void Start ()
{
Debug.Log("start FillWithRed");
Init ();
}
void Update ()
{
if(created)
{
compute.SetTexture (0, "Result", output);
compute.Dispatch(0,4,4,1);
renderer.material.SetTexture("_MainTex",output);
}
else
{
Debug.Log("texture not created");
}
}
}
If I remove the line renderer.material.SetTexture("_MainTex",output);I will see no changes (textured and shader mesh) - sounds “ok” because I will change separate data only which will be not in use by renderer. But if I leave it - my mesh is grey (shaded only). :x