Compute Shader 是为了在Unity中使用GPGPU,即通用目的图形计算单元,它独立于渲染管线,通用目的也说明了它可以用于多种目的,与CPU相比,GPU的优势在于多线程并行计算,当你想对大量数据进行相同的计算的时候,会快很多。
以下是使用说明:
#pragma kernel CSMain
#define GroupLength_x 8
RWStructuredBuffer<int> buffer;
[numthreads(4,4,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
buffer[id.x + id.y * GroupLength_x] = id.x + id.y * GroupLength_x;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
---------------------------------------------------------------------------------
public class _t1 : MonoBehaviour {
public ComputeShader cs;
// Use this for initialization
void Start () {
ComputeBuffer buffer = new ComputeBuffer(4 * 4 * 2 * 2, sizeof(int));
cs.SetBuffer(0, "buffer", buffer);
cs.Dispatch(0, 2, 2, 1);
int[] output = new int[buffer.count];
buffer.G