with the enhancement of gameplay, computational quantification, ourCpunot enough to deal with these problems quickly, andUnityopen an interface for us, we need to use it to deal with a large number of operations;we can put a lot of the logic of processing operations into thisShaderin, withGpuparallel ability to calculate, here is a simple way to say how he uses and how to transfer parameters and outgoing. Let's call it just like a method. First: InUnity, we can create aComputeshader:<ignore_js_op>When you open it, you'll find a script like this:<ignore_js_op>A brief introduction,1, the#pragmakernel MultiplyThis is a kernel,It can also be called a portal. There's at least one! thisMultiplyplease mark the Red line, it is important that we need to use it in the code;2, the next is a struct, which is a struct we define. We use it to correspond to the structure outside. 3,Rwstructuredbuffer<vecmatpair>databufferequivalent to a read-write variable that we declare I useC#indicated that:Vecmatpair[] DataBuffer;4, which is the method body, echoes the first one, and the outside invokes this similar toMainThe method of the function; You can write some simple logic to assign values to the above variables: (Here it is worth emphasizing that this variable is input and output, note!) );here, ourComputeshaderthe construction is completed;the followingC#the Writing, let it echo, pass in and output;<ignore_js_op>1, we need to define a structure that echoes with shader. 2, get this shader, we directly drag into the OK;and then the variables are basically done.:Let's take a look at the code body:<ignore_js_op> to remove a single line of symbols, we explain in one line, don't mess:1, the declaration of a length of 15 structure, which is our input parameters, (but with less than 15) 2, set up a loop, I gave him an assignment, is the same value; the contents of the loop are skipped; 3, this is important, remember the place where you marked the red line. This is the identification of the entrance, 4, the following array structure, used to collect the last 5 data required; 5, here is the test print skip; 6, Computebuffer This is also very important, you also need to declare a buffer parameter need to make this charge length, And all of the bytes used in this array (this is float=4 because it is 2 float=8 and because it is an array of length 5 =5*8=40); 7, the following is a set of processes, right, is to walk the flow; Associating this data structure with shader variables is associated SetData () Send incoming data into the shader; Focus, be sure to run, the first I finished writing found how all did not change, be sure to run Shader;dispatch (); Finally collect the return data (we need to receive the 40 bytes of data); Print the results of the drawings:<ignore_js_op> |