Directx9.03d vertex cache and index Cache

Source: Internet
Author: User

Vertex cache and index Cache

Create vertex cache and index Cache

HRESULT IDirect3DIndexBuffer9:: CreateVertexBuffer(      
    UINT      Length,
    DWORD     Usage,
    DWORD      FVF,
    D3DPOOL   Pool,
    IDirect3DVertexBuffer9 **  ppVertexBuffer,
    HANDLE *   pHandle
);
HRESULT IDirect3DIndexBuffer9:: CreateVertexBuffer(      
    UINT     Length,
    DWORD    Usage,
    DWORD    FVF,
    D3DPOOL   Pool,
    IDirect3DVertexBuffer9 ** ppVertexBuffer,
    HANDLE * pHandle
);

Lingth indicates the number of bytes allocated by the cache. If you want to make the vertex cache enough to store 8 vertex set to 8 * sizeof (vertex ),

Usega specifies how to use cache attributes on the first day. Can be 0 or marked:

· D3dusage _ dynamic sets the cache as the driver cache.

· D3dusage _ points this tag specifies that the cache will be used to store vertex elements.

· D3dusage _ softwareprocesssing.

· D3dusage _ writeonly specifies that the application cache operation mode is "write only ". In this way, the driver can place the cache in the memory address most suitable for write operations. Note that an error will occur when you perform operations on the cache created with the marker.

Fvf is stored in the vertex cache and flexible in the vertex format.

Internal pool of the pool to accommodate the cache

Ppvertexbuuffer is used to receive the pointer created in the vertex cache.

Format specifies that the index size is d3dfmt_index16, which indicates a 16-bit index. Set d3dfmt_index32 to indicate a 32-bit index.

Ppindexbuffer is used to receive the index cache pointer of the target.

Access cache content

 

Use lock to obtain the pointer pointing to the cached content and unlock it with unlock.

HRESULT IDirect3DVertexBuffer9:: Lock(      
    UINT OffsetToLock,
    UINT SizeToLock,
    VOID **ppbData,
    DWORD Flags
);
HRESULT IDirect3DIndexBuffer9:: Lock(      
    UINT OffsetToLock,
    UINT SizeToLock,
    VOID **ppbData,
    DWORD Flags
);

Offsettolock: the offset from the start point of the cache to the start point of the lock. The unit is byte.

Number of bytes to be locked by sizetolock.

Ppbdata points to the starting position of the locked storage.

Flags indicates that the locking method can be 0.

· D3DLOCK _ DISCARD: This tag is used in dynamic cache. It indicates that the cached content is discarded and a pointer pointing to the re-allocated cache is returned. This tag is very useful. This allows the hardware to continue to use discarded cache data for plotting when we access the newly allocated memory, so that the hardware will not be suspended.

· D3DLOCK_NOOVERWRITE this flag is only used for Dynamic Caching. After this tag is used, data can only be written to the cache in append mode, that is, you cannot overwrite any content in the storage area currently used for painting.

· D3DLOCK_READONLY indicates that the cache you lock is readable but not writable.

Obtain vertex cache and index Cache Information

 

Information about vertex cache and vertex cache.

D3DVERTEXBUFFER_DESC vbDescription;

VertexBuffer-> GetDesc (& vbDescription); // get vb info

D3DINDEXBUFFER_DESC ibDescription;

IndexBuffer-> GatDesc (& ibDescription); // get vb info

The D3DVERTEXBUFFER_DESC and D3DINDEXBUFFER_DESC structures are defined as follows:

typedef struct _D3DVERTEXBUFFER_DESC {
    D3DFORMAT Format;
    D3DRESOURCETYPE Type;
    DWORD Usage;
    D3DPOOL Pool;
    UINT Size;
    DWORD FVF;
} D3DVERTEXBUFFER_DESC;
typedef struct _D3DINDEXBUFFER_DESC {
    D3DFORMAT Format;
    D3DRESOURCETYPE Type;
    DWORD Usage;
    D3DPOOL Pool;
    UINT Size;
} D3DINDEXBUFFER_DESC;

Drawing status (Some translate to "rendering status ")

When your application needs to use a draw State different from the default value, you need to modify it. This status is valid until it is modified.

HRESULT IDirect3DDevice9: SetRenderState (

D3DRENDERSTATETYPE State // the state to change

DWORD Value // value of the new state

);

Preparation

After creating a vertex cache and an index cache, you may need to complete three steps before drawing the stored content.

1)Data Stream Input Source. The link between the vertex cache and the data stream is to transmit the information of the ry to the drawing pipeline.

The method is as follows:

HRESULT IDirect3DDevice9: SetStreamSource (

UINT StreamNumber,

IDirect3DVertexBuffer9 * pStreamData,

UINT OffsetInBytes,

);

· StreamNumber indicates that the data stream established with the vertex cache cannot be set to 0.

· PStreamData refers to the vertex cache pointer that you want to establish a link to a given data stream

· OffsetInBytes is an offset from the start point of the data stream. The unit is byte and the start position is specified.

· The size of each element in the Stride vertex cache to be linked to the data stream, in bytes.

For example:

// Vb is a Vertex cache that stores Vertex-type Vertex data.

_ Device-> SetStreamSource (0, vb, 0, sizeof (Vertex ));

2)Set vertex format. Specify the vertex format used in subsequent draw calls.

_ Device-> setfvf (d3dfvf_xyz | d2dfvf_diffuse | d3dfvf_tex1 );

3)SET index Cache. Only one index cache is allowed at any time. When you use a different index cache to draw an object, you must switch. The settings are as follows:

_ Device-> setindices (_ Ib); // pass copy of index buffer pointer.

Use vertex cache and index cache for rendering

After creating the vertex/index cache and preparing for it, use drawprimitive or drawindexedprimitive to transfer the information of the shipping ry through the drawing pipeline. These methods obtain vertex information from the vertex stream and index information from the index cache.

This method can be used to draw elements with no index information used

Hresult idirect3ddevice9: drawprimitive (

D3dprimitivetype primitivetype,

Uint startvertex,

Uint primitivecount

);

· Primitive type drawn by primitivetype. For example, we can draw points, lines, and triangles. In the future, we will use the triangle and the d3dpt_trianglelist parameter.

· Startvertex: The index of the element from which only the vertex data is read. This parameter gives us a certain degree of freedom, so that we can only draw a part of the vertex cache.

· Primitivecount: number of elements to be drawn.

This method uses index information to draw elements.

HRESULT IDirect3DDevice9: DrawIndexedPrimitive (

D3DPRIMITIVETYPE Type,

INT BaseVertexIndex,

UINT MinIndex,

UINT NumVertices,

UINT StartIndex,

UINT PrimitiveCount

);

· Primitive Type drawn by Type. For example, you can draw points, lines, and triangles. Because we use triangles, we use the D3DPT_TRIANGLELIST parameter.

· BaseVertexIndex adds a base for the index,

· Minimum index value that can be referenced by MinIndex.

· NuVertices-Total number of vertices to be referenced in this call.

· Index of the element at the start point of the shard index in the StartIndex vertex Cache

· PrimitiveCount: Total number of elements to be drawn.

Benin/End Sciene

The painting method must be called between methods consisting of IDirect3DDevice9: BeginScene and IDirect3DDevice9: EndScene.

For example:

_ Device-> BeginScene ();

_ Device-> DrawPrim (....)

_ Device-> EndScene ();

DirectX 9.0 3DGame Development Programming Basics

Jiangxi University of Technology FangSH 2010-4-7

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.