Processing vertices -- storing frequently updated vertex data in dynamicvertexbuffer

Source: Internet
Author: User
Problem

You need to update vertex data frequently. If you use the setdata method of vertexbuffer, it will slow down.Program.

Solution

If you plan to update vertex data frequently, use dynamicvertexbuffer instead of vertexbuffer.

This makes the data not stored in the fastest video memory, but some memory that is easier to process. Therefore, this will reduce the performance, but frequent changes to data in vertexbuffer will greatly improve the performance.

For vertexbuffer, as long as the video card is required to switch tasks (for example, ALT + Tab switching Program), the content in dynamicvertexbuffer must be overloaded, you can subscribe to the dynamicvertexbuffer contentlost event to implement this function.

Working Principle

The working principle of dynamicvertexbuffer is similar to that of vertexbuffer. First, you need a vertex array, as shown below:

Private void initvertices () {myvertexdeclaration = new vertexdeclaration (device, vertexpositiontexture. vertexelements); vertices = new vertexpositiontexture [6]; 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 (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); dynvertbuffer = new dynamicvertexbuffer (device, vertexpositiontexture. sizeinbytes * vertices. length, bufferusage. writeonly); dynvertbuffer. setdata (vertices, 0, vertices. length, setdataoptions. nooverwrite); dynvertbuffer. contentlost + = new eventhandler (dynvertbuffer_contentlost );}

Creating dynamicvertexbuffer accepts the same parameters as creating vertexbuffer, but the setdata method accepts a new enhanced parameter, which will be discussed below. Last lineCodeIs new. If the video card is required to process another program, the device will be lost, and the dynamicvertexbuffer content will also be lost. Therefore, you need to subscribe to a method on the dynamicvertexbuffer contentlost event. As long as the content of dynamicvertexbuffer is lost, the contentlost event will be triggered and the corresponding method will be called. This method will reload the content in dynamicvertexbuffer. In this example, you subscribe the dynvertbuffer_contentlost Method to the event.

Of course, you need to define this method to reload the buffered content in this method:

 
Private void dynvertbuffer_contentlost (Object sender, eventargs e) {dynvertbuffer. setdata (vertices, 0, vertices. length, setdataoptions. nooverwrite );}

Note:This requires local processing of vertex data. Because dynamixvertexbuffer is used only when vertex data changes frequently, you must first ensure that the data is correct. Remember that you will never read data from (dynamic) vertexbuffer.

When drawing is completed, dynamicvertexbuffer works in the same way as vertexbuffer.

Performance Considerations: dynamicvertexbuffer. setdata Method

Because dynamicvertexbuffer is used when you want to update data frequently, you will often use the setdata method. Therefore, the setdata method of dynamicvertexbuffer also accepts an additional parameter setdataoptions.

This parameter allows you to specify some options to speed up the program. By default, when you want to overwrite the content in the video memory, the video card cannot read data from the video memory because it does not support simultaneous read/write. When you write a large amount of data into the memory, the rendering process of the video card will be suspended, because the video card will wait until your copy process ends. However, there are two ways to ensure that the video card does not wait until the copy operation ends. You can use the setdataoptions parameter, which is optional below:

    • Setdataoptions. None: This option can fully control which part of vertexbuffer is overwritten. However, as explained above, this will lead to lower performance. If the data drawn by the video card is extracted from the content before vertexbuffer, the video card must be stopped until the slow copy process ends.
    • Setdataoptions. Discard: This option indicates that you no longer need the previous content of vertexbuffer. In this case, the data is stored in a new location of the video. When the write process occurs, the video card can continue to use old data. Once the write process is complete, the video card can use new data to draw and discard old data. In short, the video card does not have to wait, but you have to rewrite all the data. (You cannot use this option on the Xbox platform. You can only call drawuserindexedprimitive to draw frequently updated vertices .)
    • Setdataoptions. nooverwrite: This option is powerful but dangerous. You must ensure that the vertexbuffer section being overwritten is not used by the rendering process. In this way, you can overwrite the specific part of vertexbuffer. At this time, the video card does not need to wait until the copying process ends, because the part you are overwriting is not involved in the drawing process. This is faster than using the discard option, because you do not need to keep a new Part in the memory.

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.