For example, to draw a rect, the general method is
Vertexbuffer-> lock ();
Fillrectvertex ();
Vertexbuffer-> unlock ();
Settexture ();
Drawprimitive ();
The validity rate of drawing multiple 2D elements is very low. The reason is that lock/unlock is called multiple times.
Improvement Method:
2d element Rendering
Use a unified memory buffer and Command record.
During painting, the vertex data is first filled into the buffer, and the Command record is filled, including the shader, vertex idx/size, and primitive type of the current element.
During actual painting, copy the buffer to the vertex buffer, and then execute the Command record (set shader, drawprimitive (idx, size )), here, we can merge adjacent similar commands (continuous commands with the same shader and primitive type, such as drawtext)
Here, the shader contains texture, texture blend (without PS), PS/Vs, and PS/Vs-related change scripts (such as making a game icon, mixed by two layers of texture, one layer Keeps rotating)
If necessary, you can also use the zcoordinate to mark the command order. Sort the command before execution (the translucent element is finally drawn). Reduce render state switching.
Three ways to draw 2D elements.
Draw all 3D elements first, and then clear Z before drawing 2D, then z can be buffered.
1. Use the potion_t vertex format to skip world/view changes. The vertex coordinates are pixel coordinates.
2. Use the world/view matrix of identity to disable illumination. The vertex coordinates are X [-], Y [-], the top and right are positive, and the origin is in the center of the screen/window. The advantage of this method is that the drawing is calculated in percentage and has nothing to do with the resolution.
3. Use vs to skip world/view changes. Similar effects to 2 can also be added with some simple animations.
For example, a rotating windmill directly changes the texture U/V