Learn ComputeShader 01 First Computer Shader

使用Unity版本:2019.4.12f1

整体流程:

1添加一个quad object并添加一个无光照材质

2.相机投影模式设置为正交

3.调整quad使其完全显示在相机内

4.创建脚本并且使用计算着色器覆盖quad的纹理

5.创建一个compute shader

前三步完成以后结果应该是这样的:

 创建脚本并且使用计算着色器覆盖quad的纹理

创建脚本并且命名为AssignTexture

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AssignTexture : MonoBehaviour
{
    public ComputeShader shader;
    public int texResolution = 256;

    Renderer rend;
    RenderTexture outputTexture;
    int kernoHandle;
    


    // Start is called before the first frame update
    void Start()
    {
        outputTexture = new RenderTexture(texResolution, texResolution, 0);//创建一个颜色纹理,第三个参数0表示不需要深度信息
        outputTexture.enableRandomWrite = true;//允许compute shader 去修改纹理
        outputTexture.Create();

        rend = GetComponent<Renderer>();//确保渲染器组件已经启用
        rend.enabled = true;

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

这里说下Render.Create()函数,引用下unity官方文档的解释。

也就是说创建实例的时候并不会创建纹理,只有在active状态的时候或者调用create函数的时候才会创建纹理(active状态比如纹理设置为相机的目标纹理或者被设置为渲染目标)

接下来把脚本附加给quad,然后创建一个compute shader附加到脚本上。

一个compute shader包含一个或者多个kernel。每个Kernel都可以通过mono behaviour 脚本调用,通过使用computeshader的dispatch方法。

一个默认的compute shader包含了以下内容:

// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel CSMain

// Create a RenderTexture with enableRandomWrite flag and set it
// with cs.SetTexture
RWTexture2D<float4> Result;

[numthreads(8,8,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
    // TODO: insert actual code here!

    Result[id.xy] = float4(id.x & id.y, (id.x & 15)/15.0, (id.y & 15)/15.0, 0.0);
}

每个kernel都需要一个pragma声明,然后是要调用的函数的名称。

接下来是声明一个纹理变量,这个纹理允许读和写每一个像素值

接下来这句话

[numthreads(8,8,1)]

说明了一个线程组(Thread Group)中可以被执行的线程(Thread)总数量。一共是8*8*1=64个线程

线程组ID是(0,0,0)对应的id.xy应该是下面这样的,这个时候id.x和y指向纹理的左下角,多线程在左下角处理一个8*8的像素

 当线程组ID是(1,0,0)时

当线程组ID是(0,1,0)的时候

前面已经说过,每个线程组会有64个线程分别处理64(8*8)个像素, 如果我们要绘制一个256*256分辨率的纹理,就分别需要(256/8,256/8)个线程组

这里就要说下ComputeShader.Dispatch这个函数了,也就是帮我们分配并且启动线程组的函数。

引用下unity官方文档的解释:

另外这三个参数必须都不为0计算着色器才会正常工作,也就是说z至少为1才可以。

比如说x,y,z分别是(4,2,3),那么每个线程组都会被调用,一共调用4*2*3=24次

如果x,y,z分别是(4,2,1),那么每个线程组都会被调用,一共调用4*2*1=8次

如果在某个维度上指定线程组数量为0,就意味着在该维度上没有线程组进行计算,这将导致计算着色器无法执行任何计算任务。因此,至少需要一个线程组来确保计算着色器能够执行并发计算任务。

那么接下来完善我们的c#脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AssignTexture : MonoBehaviour
{
    public ComputeShader shader;
    public int texResolution = 256;

    Renderer rend;
    RenderTexture outputTexture;
    int kernoHandle;
    


    // Start is called before the first frame update
    void Start()
    {
        outputTexture = new RenderTexture(texResolution, texResolution, 0);//创建一个颜色纹理,第三个参数0表示不需要深度信息
        outputTexture.enableRandomWrite = true;//允许compute shader 去修改纹理
        outputTexture.Create();

        rend = GetComponent<Renderer>();//确保渲染器组件已经启用
        rend.enabled = true;

        InitShader();

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.U))
        {
            DispatchShader(texResolution / 8, texResolution / 8);
        }
    }

    private void InitShader()
    {
        kernoHandle = shader.FindKernel("CSMain");
        shader.SetTexture(kernoHandle, "Result", outputTexture);
        rend.material.SetTexture("_MainTex", outputTexture);

        DispatchShader(texResolution / 16, texResolution / 16);

    }

    private void DispatchShader(int x,int y)
    {
        shader.Dispatch(kernoHandle, x, y, 1);

    }
}

默认使用一半的线程组绘制的结果:

确实x和y方向都只绘制了一半的像素

正常绘制以后的结果:

相关推荐

  1. <span style='color:red;'>01</span>-<span style='color:red;'>02</span>

    01-02

    2024-04-27 10:08:03      55 阅读
  2. 【2024.01.02】刷算法07

    2024-04-27 10:08:03       57 阅读
  3. <span style='color:red;'>01</span>-<span style='color:red;'>04</span>css

    01-04css

    2024-04-27 10:08:03      40 阅读
  4. 2024-02-01(Hive)

    2024-04-27 10:08:03       56 阅读
  5. 01_02_mysql04_数据类型

    2024-04-27 10:08:03       55 阅读
  6. Day-<span style='color:red;'>02</span>-<span style='color:red;'>01</span>

    Day-02-01

    2024-04-27 10:08:03      60 阅读
  7. Day-<span style='color:red;'>01</span>-<span style='color:red;'>02</span>

    Day-01-02

    2024-04-27 10:08:03      44 阅读
  8. <span style='color:red;'>01</span>-<span style='color:red;'>02</span>-5

    01-02-5

    2024-04-27 10:08:03      23 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-27 10:08:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-27 10:08:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-27 10:08:03       82 阅读
  4. Python语言-面向对象

    2024-04-27 10:08:03       91 阅读

热门阅读

  1. 4.25 作业

    2024-04-27 10:08:03       31 阅读
  2. DevOps转型的意义:加速创新、提高效率

    2024-04-27 10:08:03       26 阅读
  3. Rust 字符串基本使用教程及代码演示

    2024-04-27 10:08:03       32 阅读
  4. DRF 权限介绍

    2024-04-27 10:08:03       25 阅读
  5. 如何在ubuntu 24.04上安装配置x11vnc以便远程访问

    2024-04-27 10:08:03       36 阅读
  6. Python pip安装如何切换国内源

    2024-04-27 10:08:03       35 阅读
  7. 【Linux】tr命令删除空格,sed替换空行

    2024-04-27 10:08:03       32 阅读
  8. MyBatis特殊SQL的执行

    2024-04-27 10:08:03       41 阅读
  9. windows Server 2012精讲系列课程

    2024-04-27 10:08:03       27 阅读
  10. Leetcode 347:前K个高频元素

    2024-04-27 10:08:03       23 阅读
  11. Markdown生成word和pdf

    2024-04-27 10:08:03       30 阅读
  12. k8s笔记 | StatefulSet 有状态

    2024-04-27 10:08:03       34 阅读