2.3 Steps to use vertex shaders
Read this article shows that you have agreed to the statement
The following list summarizes the necessary steps for creating and using the vertex shader.
1. Write and compile the vertex shader
2. Create an IDirect3dvertexShader9 interface to represent the vertex shader based on the compiled shader code.
3. Use the idirect3ddevice9 :: setvertexshader method to allow this vertex shader.
Of course, after we finish these, we have to destroy this vertex shader. The following sections will be more detailed into these steps.
2.3.1 Write and compile the vertex shader
First, we must write a vertex shader program. In the HLSL chapter in this book, we have written our shaders (the translator's note: See all sections in the first chapter of the translation of my translation). Once the shader code is written, we use the D3DxCompileshaderFromFile function to compile this shader, as described in 1.2.2. Recall, this function returns an ID3dxbuffer pointer, which contains compiled shader code.
2.3.2 Creating a vertex shader
Once we have a compiled shader code, we can get a pointer to an IDirect3dvertexshader9 interface, which represents a vertex shader - by using the following method:
HRESULT IDIRECT3DDEVICE9 :: CreateVertexShader
Const DWORD * PFunction,
IDirect3dvertexshader9 ** PPShader
);
PFunction - pointer to compiler code
PPShader - Returns a pointer to an IDirect3dvertExshader9 interface
For example, assuming that the variable shader is an ID3DXBuffer (interface pointer) containing the compiled shader code. Then you have to get an iDirect3dvertexshader9 interface, we can write:
IDirect3dvertexshader9 * toonshader = 0;
HR = device-> CreateVertexShader
(DWORD *) Shader-> getBufferPointer (),
& Toonshader;
Note: Reaffirming again, D3DXCompileshaderFromFile is a function that returns the compiled shader code (shader).
2.3.3 Creating a vertex shader
After we get a pointer to the IDirect3dvertexshader9 interface representing our vertex shader, we can use the following method to allow (enable) it:
HRESULT IDIRECT3DDEVICE9 :: SETVERTEXSHADER (
IDirect3dvertexshader9 * pshader
);
This method only accepts a parameter, we pass a pointer to the vertex shader you want to allow. To allow this shader we created in Section 2.3.2, we can write:
Device-> setvertexshader (TOONSHADER);
2.3.4 Destroy the vertex shader
Like all Direc3D interfaces, we must clear them, we must call the Release method after using them. Still take the vertex shader created in Section 2.3.2 as an example, we write:
D3D:: Release