unity ComputeBuffer & Compute Shader

https://docs.unity3d.com/ScriptReference/ComputeBuffer.html
https://blog.csdn.net/weixin_38884324/article/details/79284373
https://blog.csdn.net/csharpupdown/article/details/79385544
http://www.sohu.com/a/223507355_667928
http://www.xuanyusong.com/archives/4488

ComputeBuffer
以上抄送自:https://docs.unity3d.com/ScriptReference/ComputeBuffer.html

description
gpu shader buffer, mostly for use with compute shaders.
computershader programs often need arbitrary data to be read & written into memory buffers. computer buffer class is exactly for that. u can create & fill them from script code, and use themm in compute shaders or regular shaders.

compute buffers are alway supported in compute shaders. compute shader support can be queried runtime using SystemInfo.supportsComputeShaders. https://docs.unity3d.com/ScriptReference/SystemInfo-supportsComputeShaders.html
See the Compute Shaders Manual page for more information about platforms supporting compute shaders. In regular graphics shaders the compute buffer support requires minimum shader model 4.5.

on the shader side, ComputeBuffers with default ComputeBufferType map to StructuredBuffer and RWStructuredBuffer in HLSL.

https://docs.unity3d.com/Manual/class-ComputeShader.html
compute shaders are programs that run on the graphics card, outside of the normal rendering pipeline. they can be used for massively parallel GPU algorithms, or to accelerate pars of game rendering. in order to efficiently use them, an in-depth knowledge of GPU architectures and parallel algorithms is often needed; as well as knowledge of DirectCompute, OpenGL Compute, CUDA, or OpenCL.

compute shaders in unity closely match DirectX 11 DirectCompute technology. platforms where compute shaders work:
• Windows and Windows Store, with a DirectX 11 or DirectX 12 graphics API and Shader Model 5.0 GPU
• macOS and iOS using Metal graphics API
•Android, Linux and Windows platforms with Vulkan API
•Modern OpenGL platforms (OpenGL 4.3 on Linux or Windows; OpenGL ES 3.1 on Android). Note that Mac OS X does not support OpenGL 4.3
•Modern consoles (Sony PS4 and Microsoft Xbox One)

compute shader support can be queried runtime using : SystemInfo.supportsComputeShaders.

Compute shader Assets
similar to regular shaders, compute shaders are Asset files in your project, with a .compute file extension. they are written in DirectX 11 style HLSL language, with a minimal number of #pragma compilation directives to indicate which functions to compile as compute shader kernels.

here’s a basic example of a compute shader file, which fills the output texture with red:

// test.compute

#pragma kernel FillWithRed //这个FillWithRed是下面的函数名

RWTexture2D<float4> res;

[numthreads(1,1,1)]
void FillWithRed (uint3 dtid : SV_DispatchThreadID)
{
    res[dtid.xy] = float4(1,0,0,1); //涂成红色
}

the language is standard DX11 HLSL, with an additional #pragma kernel FillWithRed directive.
one compute shader Asset file must contain at least one compute kernel that can be invoked, and that function is indicated by the #pragma directive. there can be more kernels in the file; just add multiple #pragma kernel lines.

When using multiple #pragma kernel lines, note that comments of the style // text are not permitted on the same line as the #pragma kernel directives, and cause compilation errors if used.
不能在同一行注释,否则编译报错

The #pragma kernel line can optionally be followed by a number of preprocessor macros to define while compiling that kernel, for example:

#pragma kernel KernelOne SOME_DEFINE DEFINE_WITH_VALUE=1337
#pragma kernel KernelTwo OTHER_DEFINE
// ...

invoking compute shaders 计算着色器的调用
in your script, define a variable of ComputeShader type and assign a reference to the Asset. this allows u to invoke them with ComputeShader.Dispatch function. See Unity documentation on ComputeShader class for more details.

closely related to compute shaders is a ComputeBuffer class, which defines arbitrary data buffer (“structured buffer” in DX11 lingo). Render Textures can also be written into from compute shaders, if they have “random access” flag set (“unordered access view” in DX11). See RenderTexture.enableRandomWrite to learn more about this.

https://blog.csdn.net/weixin_38884324/article/details/79284373
下面介绍一个unity的例子:

using Unity.Collections;
using UnityEngine;
using Unity.Collections.LowLevel.Unsafe;

public class ComputeShaderDemo : MonoBehaviour
{
    public ComputeShader shader;
    struct Data
    {
        public int a;
        public int b;
    }

    struct Result
    {
        public int c;
    }

    private void Start()
    {
        Data[] input = new Data[2];
        Result[] output = new Result[2];

        for (int i = 0; i < input.Length; ++i)
        {
            input[i].a = 2 * i;
            input[i].b = 2 * i + 1 + i;
            Debug.LogError("input[" + i + "]" + input[i].a + "  " + input[i].b);
        }

        ComputeBuffer inputbuffer = new ComputeBuffer(input.Length, 2 * sizeof(int));
        ComputeBuffer outputbuffer = new ComputeBuffer(output.Length, 1 * sizeof(int));

        int k = shader.FindKernel("FunSum");

        inputbuffer.SetData(input);
        shader.SetBuffer(k, "input", inputbuffer);

        shader.SetBuffer(k, "output", outputbuffer);
        shader.Dispatch(k, output.Length, 2, 10);

        outputbuffer.GetData(output);

        for (int i = 0; i < output.Length; ++i)
        {
            Debug.LogError("output[" + i + "]" + output[i].c);
        }

        inputbuffer.Dispose();
        outputbuffer.Dispose();
    }
}
#pragma kernel FunSum

struct Data 
{
	int a;
	int b;
};

struct Result
{
	int c;
};

StructuredBuffer<Data> input;
RWStructuredBuffer<Result> output;

[numthreads(12, 1, 1)]
void FunSum(uint3 id : SV_DispatchThreadID)
{
	output[id.x].c = input[id.x].a + input[id.x].b;
}

在这里插入图片描述

到此结束。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值