Unity uses GPU instead of CPU to process and calculate simple tests

Source: Internet
Author: User

Http://www.manew.com/thread-110502-1-1.html

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>

Unity uses GPU instead of CPU to process and calculate simple tests

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.