Processing vertices-store vertices and indexes in the memory using Vertex buffering and index Buffering

Source: Internet
Author: User
Problem

Each time you call the device. drawuserprimitives method, the vertex is transmitted from the system memory to the video card. Normally, most of the data is not changed, which means that the same data is repeatedly transmitted each frame. Today, the video card has a large capacity and is quickly stored, so you can store the vertex data in the video memory for acceleration.Program. Transferring vertex data from the video memory to the video card is much faster, because the data is transmitted only between different chips on the same card. The same index data can also be accelerated.

Solution

By creating the vertexbuffer of the vertex array, you can copy the vertex data to the video memory. After the vertex is stored in the video memory, you can call the drawprimitives method (instead of the drawuserprimitives method) to obtain the vertex from the faster video memory.

Note:If the vertex almost does not need to be updated, this method can greatly improve the performance. However, when processing data called dynamic vertex data, this means that vertex data is updated frequently and you should use dynamicvertexbuffer, which will be introduced in the next tutorial.

Working Principle

You can transmit data to the video card and store them in the video memory at a time, instead of passing the vertex array to the video card every time you call the drawuserprimitives method. To implement this method, you can load the vertex array into a vertexbuffer. Add the vertexbuffer variable to the project:

 
Vertexbuffer vertbuffer;

Then, use the following method to generate six vertices and display two triangles with textures (see tutorial 5-1 ). At the end of this method, these six vertices are loaded into vertexbuffer and passed to the video memory:

 private void initvertices () {myvertexdeclaration = new vertexdeclaration (device, vertexpositiontexture. vertexelements); vertexpositiontexture [] vertices = new vertexpositiontexture [6]; int I = 0; vertices [I ++] = new vertexpositiontexture (New vector3 (-5.0f, new vector2 (-0.5f, 1.5f); vertices [I ++] = new vertexpositiontexture (New vector3 (-2.5f, new vector2 (0.5f,-1.5f )); vertices [I ++] = new vertexpositiontexture (New vector3 (0,-3, new vector2 (1.5f, 1.5f )); vertices [I ++] = new vertexpositiontexture (New vector3 (0,-3,-1), new vector2 (-0.5f, 1.5f )); vertices [I ++] = new vertexpositiontexture (New vector3 (2.5f, 5,-1), new vector2 (0.5f, 1.5f )); vertices [I ++] = new vertexpositiontexture (New vector3 (5.0f,-3,-1), new vector2 (1.5f, 1.5f); vertbuffer = new vertexbuffer (device, vertexpositiontexture. sizeinbytes * vertices. length, bufferusage. writeonly); vertbuffer. setdata (vertices, 0, vertices. length) ;}

After defining all vertices, you need to create a vertexbuffer. This vertexbuffer will be passed to the reserved memory of the video card. For this reason, you need to specify the number of bytes occupied by Device Links and vertices. This number is equal to (the size of a vertex in bytes) multiply by (number of vertices ).

Note: You certainly do not want to create a vertexbuffer for each frame. Make sure to only call this line.CodeOnce. If you want to overwrite vertexbuffer data, do not delete it and recreate it. Instead, you should use the setdata method to load new data to the existing vertexbuffer. It is necessary to delete vertexbuffer and recreate it as long as you need to increase the number of vertices.

After creating vertexbuffer, you can use the setdata method to transmit vertex data to the memory. Apparently, the setdata method needs to copy data from the vertex array. One of its overload methods can only write part of vertexbuffer data. In this case, you need to specify the vertex from which and the number of vertices to copy.

Note:If you want to update vertexbuffer content frequently, you should use the setdata method of dynamicvertexbufferdynamicvertexbuffer to be more powerful.

After storing vertex data in the video card, you can draw a triangle. You can use the code in tutorial 5-1, but there are two changes.

You will use the drawprimitive method to indicate that you will retrieve data from vertexbuffer for plotting. Before that, activate vertexbuffer.

Device. renderstate. cullmode = cullmode. none; basiceffect. world = matrix. identity; basiceffect. view = fpscam. viewmatrix; basiceffect. projection = fpscam. projectionmatrix; basiceffect. texture = mytexture; basiceffect. textureenabled = true; basiceffect. begin (); foreach (effectpass pass in basiceffect. currenttechnique. passes) {pass. begin (); device. vertexdeclaration = myvertexdeclaration; device. vertices [0]. setsource (vertbuffer, 0, vertexpositiontexture. sizeinbytes); device. drawprimitives (primitivetype. trianglelist, 0, 2); pass. end ();} basiceffect. end ();

In pass, as before, vertexdeclaration needs to be passed to the device to indicate the Data Type of vertex storage and set vertexbuffer in the next line of code. For each vertex, you can store different information in different vertexbuffers. In this example, you only use one vertexbuffer, so the specified index is 0. The setsource method indicates that vertbuffer is activated as the vertex source. You also need to specify the starting position of the first vertex and the number of bytes occupied by the vertex (so that the video card can crop the byte stream into an independent vertex ). After vertexbuffer is activated, draw from the first vertex using trianglelist. This vertexbuffer contains data of two triangles.

Performance Considerations: vertexbuffer Constructor

The vertexbuffer constructor can specify some useful selectable flags in the last parameter. The driver of the video card uses this flags to determine which memory is the fastest. The following are available bufferusages:

    • Bufferusage. None: read/write from vertexbuffer.
    • Bufferusage. Points: vertex data in vertexbuffer is used to draw vertices and genie. This is not related to performance.
    • Bufferusage. writeonly: Do not read data from vertexbuffer. When vertexbuffer is used, vertex data can be placed in a much faster Display memory. In this way, when you want to process vertex data, it is better to call the getdata method of vertexbuffers than to store a copy of vertex data in the system memory.

You can also combine these flags and use | to separate them to represent or operations (if there is no conflict, the result is the and logic of the specified option ).

Indexbuffer)

If you want to store indexes in the video card, you should create an indexbuffer after creating the vertexbuffer, so add an indexbuffer variable to the project:

 
Indexbuffer;

As a simple example, you will define two triangles. Because they share one vertex, you only need to define five independent vertices:

Private void initvertices () {myvertexdeclaration = new vertexdeclaration (device, vertexpositiontexture. vertexelements); vertexpositiontexture [] vertices = new vertexpositiontexture [5]; int I = 0; vertices [I ++] = new vertexpositiontexture (New vector3 (-5.0f,-3, -1), new vector2 (-0.5f, 1.5f); vertices [I ++] = new vertexpositiontexture (New vector3 (-2.5f, 5,-1 ), new vector2 (0.5f,-1.5f); vertices [I ++] = new vertexpositiontexture (New vector3 (0,-3,-1), new vector2 (1.5f, 1.5f); vertices [I ++] = new vertexpositiontexture (New vector3 (2.5f, 5,-1), new vector2 (0.5f,-1.5f )); vertices [I ++] = new vertexpositiontexture (New vector3 (5.0f,-3,-1), new vector2 (-0.5f, 1.5f); vertbuffer = new vertexbuffer (device, vertexpositiontexture. sizeinbytes * vertices. length, bufferusage. writeonly); vertbuffer. setdata (vertices, 0, vertices. length );}

Next, use the initindices method to create index arrays and copy them to the index Buffer:

Private void initindices () {int [] indices = new int [6]; int I = 0; indices [I ++] = 0; indices [I ++] = 1; indices [I ++] = 2; indices [I ++] = 2; indices [I ++] = 3; indices [I ++] = 4; indexbuffer = new indexbuffer (device, typeof (INT), indices. length, bufferusage. writeonly); indexbuffer. setdata <int> (indices );}

When creating indexbuffer, you must specify the index type, which can be ints or shorts, and the index quantity.

Beware:Some low-end video cards only support 16-bit indexes. If you use a 32-bit integer index, an error occurs. To solve this problem, you should store the index in a short array and specify to create a short-type index buffer.

TIPS:If your array contains no more than 32,768 indexes, you should use shorts instead of ints. In this way, the index buffer can save half of the memory.

Don't forget to call this method at the beginning of the project.

When drawing, you need to activate vertexbuffer and indexbuffer, and then call the drawindexedprimitives method to draw the triangle:

Device. renderstate. cullmode = cullmode. none; basiceffect. world = matrix. identity; basiceffect. view = fpscam. viewmatrix; basiceffect. projection = fpscam. projectionmatrix; basiceffect. texture = mytexture; basiceffect. textureenabled = true; basiceffect. begin (); foreach (effectpass pass in basiceffect. currenttechnique. passes) {pass. begin (); device. vertexdeclaration = myvertexdeclaration; device. vertices [0]. setsource (vertbuffer, 0, vertexpositiontexture. sizeinbytes); device. indices = indexbuffer; device. drawindexedprimitives (primitivetype. trianglelist, 0, 0, 5, 0, 2); pass. end ();} basiceffect. end ();
Code

Initvertices, initindices, and draw methods have been written before.

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.