Unity Compute Shader 03

sequence

in front of:

(278 messages) Unity compute shader 01_averagePerson's blog - CSDN blog

(278 messages) Unity compute shader 02_averagePerson's blog - CSDN blog

I have learned some concepts in GPU, how to drag scripts in unity, and how to generate textures with compute shaders.

But how to parallelize common data with compute shaders? 

Baidu launch

CPU multithreading

From simple to complex... To understand how to use GPU to process data in parallel, is it reasonable to start with how to use CPU to process data in parallel? [Ignore python's multithreading is false parallelism]

import threading

class myThread(threading.Thread):
    def __init__(self, num):#这个cmd,就是要执行的命令
        threading.Thread.__init__(self)
        self.num=num
    def run(self):
        print("第",self.num,"个任务已完成")
    
num=10
threadList=[]

for i in range(num):
    thread=myThread(i)
    threadList.append(thread)
for i in range(num):#调用 start() 方法启动子线程,它会调用run方法
    threadList[i].start()
for i in range(num):#让主线程等着这若干个子线程执行完
    threadList[i].join()

print("主线程已结束")

 The join function feels a bit parallel.

Well, now that 1+1=2 is known, what is the difference between CPU multithreading and GPU multithreading? Take a guess:

Access data from memory - CPU multi-threaded parallel computing - write data back to memory

Data transfer from CPU to GPU - GPU multi-threaded parallel computing - data transfer from GPU to CPU

Not only the CPU is used, but the CPU+GPU is used, so it is called heterogeneous programming.

(272 messages) What is CUDA - Introduction to CUDA_Limo Mao's Blog-CSDN Blog_cuda

 

GPU multithreading

The main reference is this page: 

Original: 

Kyle Halladay - Getting Started With Compute Shaders In Unity

Chinese: 

 Unity | Talking about Compute Shader - Zhihu (zhihu.com)

 Divide it into two halves. The first half is about calculating the texture generated by the shader, which is ignored here. Mainly look at the second half. The second half introduces how to calculate common data in parallel, and gives an example of manually calculating vertex transformations.

For this example, don’t look at it first, guess first: it probably involves these things: CPU, GPU, and then get something to connect them, the relationship between the two sides of the river and a bridge.

Two structure arrays [equivalent to that bridge]

How to transfer data from CPU to GPU? By teleportation? Both ends must have a corresponding data structure to store, because the data is unchanged but the location has changed, so the data structure at both ends should also be the same.

//C#脚本里的结构体数组
struct VecMatPair
{
public Vector3 point;
public Matrix4x4 matrix;
}
VecMatPair[] data = new VecMatPair[5];

//计算着色器里的结构体数组
struct VecMatPair
{
	float3 pos;
	float4x4 mat;
};
RWStructuredBuffer<VecMatPair> dataBuffer;

GPU side [equivalent to the bank of the river]

This is a computer shader script

#pragma kernel Multiply

struct VecMatPair
{
	float3 pos;
	float4x4 mat;
};

RWStructuredBuffer<VecMatPair> dataBuffer;

[numthreads(16,1,1)]
void Multiply (uint3 id : SV_DispatchThreadID)
{
    dataBuffer[id.x].pos = mul(dataBuffer[id.x].mat,
    				float4(dataBuffer[id.x].pos, 1.0));
}

CPU side [equivalent to the other side]

the code 

This is a plain C# script 

public ComputeShader shader;

struct VecMatPair
{
public Vector3 point;
public Matrix4x4 matrix;
}

void RunShader()
{
VecMatPair[] data = new VecMatPair[5];
//INITIALIZE DATA HERE

//STEP1: 把INITIALIZE DATA HERE的数据,用computer buffer包装一下,然后发送到GPU端
ComputeBuffer buffer = new ComputeBuffer(data.Length, 76);//开辟显存。数组有5个元素,每个元素包含4*4+3=19个float值,一个float是4个字节,所以每个元素是19*4=76个字节
buffer.SetData(data);//发送数据到GPU上开辟的显存区域
int kernel = shader.FindKernel("Multiply");
shader.SetBuffer(kernel, "dataBuffer", buffer);//相当于绑定了一下:在这个kernel里,可以通过dataBuffer[index]来使用这个buffer

//STEP2: 多线程并行计算
shader.Dispatch(kernel, data.Length, 1,1);

//STEP3: 取回结果
VecMatPair[] output = new VecMatPair[5];
buffer.GetData(output);
}

ComputerBuffer

Official documentation: Unity - Scripting API: ComputeBuffer (unity3d.com) 

GPU data buffer, indicating that this thing is in the video memory

This thing can still be used in normal shaders, got it.

The constructor is as follows:

Unity - Scripting API: ComputeBuffer.ComputeBuffer (unity3d.com)

Looking at the parameters in its construction parameters, it is obviously opening up memory...

Moreover, Stride, in fact, when you see it, you should think of VAO and VBO as a conditioned reflex. But you don't, it's normal, otherwise why would you be called Xiaobai?

ComputerBuffer.SetData

Official documentation:  Unity - Scripting API: ComputeBuffer.SetData (unity3d.com)

 After opening up the video memory, you have to store things in it. Obviously, this function does this.

ComputerShader.SetBuffer

Official documentation: Unity - Scripting API: ComputeShader.SetBuffer (unity3d.com)

 id, name, buffer, looks like the bind in VBO?

Bound, two can be said to be bound. Why bind?  

How to use it after storing it in the buffer1 developed in the video memory? Use a pointer a of the same variable type to point to it? Then you can use buffer1 through a instead of buffer2 or any other buffer.

Each kernel is set separately, storage and use are separated, it looks very flexible...

About bind and VBO

Probably, between binding and unbinding, the bufferxxx pointed to by vbo is manipulated, which means. Per-kernel is also section by section, is this also the meaning?

Just feel it.

(271 messages) Understanding glGenBuffers and glBindBuffer_Mo Zhi's Blog-CSDN Blog_glgenbuffers

(271 messages) Understanding of glGenBuffers glBindBuffer glBufferData in OpenGL_Stealing the Blog-CSDN Blog_glgenbuffers

ending

Probably equivalent to delete

summary

Such an example is very good, because it is relatively pure, and does not involve other textures, materials, how to drag the script, how to display it with the shader after calculation... just like the safe mode. Easy to grasp the backbone, suitable for 0-based learning...

It's just Baidu, it won't work.

Maybe you have to be able to string them together in a way you are familiar with and understandable.

Guess you like

Origin blog.csdn.net/averagePerson/article/details/128256540