Porting from Fixed to Programmable pipeline

Started by
12 comments, last by kauna 12 years, 1 month ago
Hello

My application is now ready to use a shader instead of the Fixed Function Pipeline. At the moment I am skinning and transforming vertices on the CPU. I need to update my vertex format to include Bone indices and weights. My current vertex struct is as follows:

#define VERTEX_FVF (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_DIFFUSE|D3DFVF_TEX1)

struct VERTEX
{
float x,y,z; // Position
float hx,hy,hz; // Normal
DWORD color; // Color
float tu,tv; // Texture UV
};



But before I dive in I have some questions.


1. Do I still need to SetFVF() with shaders?


2. What do I include in my Vertex Struct and FVF definition to accomodate Bone indices and Weights (I only have 2 bones max influencing a vertex)?

struct VERTEX
{
float x,y,z; // Position
float hx,hy,hz; // Normal
DWORD color; // Color
float tu,tv; // Texture UV
float w1,w2; // Weights
BYTE Indx1, Indx2; //Indices
};

Would this work or do I need to padd for 4 bones? How would the input for the vertex shader look like for the above vertex struct (float2 weights : BLENDWEIGHT0)?


3. Is it possible to have two position vectors, "float4 pos2 : POSITION0" as input parameters?


4. My position "Vector" has 3 members, and the transform matrix is 4x4, do I need a dummy unit padding the vector to multiply, how does this affect position, does it have to be 0.0f?
Advertisement
When moving to a programmable pipeline, you no longer use FVF anywhere. You define a vertex declaration, describing each part of your vertex, then you use the D3D device to set the vertex declaration.

For example, this is my vertex declaration i use in my engine

const D3DVERTEXELEMENT9 vEle[] =
{
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
{0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
{0, 32, D3DDECLTYPE_FLOAT1, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1}, //Using bones(Represent boolean value)
{0, 36, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 2}, //Bones
{0, 52, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 3}, //Weights

D3DDECL_END()
};


And then to use it

if(d3dDevice->CreateVertexDeclaration(vEle, &myIDirect3DVertexDeclation9Pointer) != D3D_OK)
//Error

if(_d3dDevice->SetVertexDeclaration(myIDirect3DVertexDeclation9Pointer) != D3D_OK)
//Error


You have to define and set a vertex declaration so that the shader knows how to read the data you send to the GPU. You can find more info on MSDN or if you google about vertex declarations.
I have not seen that anywhere in any Shader tutorial....

From the tutorials I have read, I only need to read/compile an effect and set it, then Set constants and effect->Begin() before drawing subsets.

What am I missing here?
I guess I kind've skipped that step =p. So an effect file, is just a file that can hold multiple shader programs. You don't have to use an effect file, you can simply create a shader in your code, but effect files make it much nicer to work with(imho).

So at a minimum, you need atleast one Vertex shader which outputs a vertex position, and one pixel shader, which will color everything.
Here is a simple vertex and pixel shader

void simpleVertexShader(float4 iPos : POSITION0,
float2 iUV : TEXCOORD0,
out float4 oPos : POSITION0,
out float2 oUV : TEXCOORD0)
{

float4 vWorldPos = mul(iPos, world);
float4 vViewPos = mul(vWorldPos, cView);

oPos = mul(vViewPos, cProj);

oUV = iUV;
}


Here you are passing in a vert position, and a texture uv(which you described in your vertex declaration). You do some modifying(ie transforming to world space), and then your pixel shader is called.


float4 simplePixelShader(float2 iUV : TEXCOORD0) : COLOR0
{
//Sample your texture using your UV coordinate that you passed "out" from your vertex shader
return tex2D(ObjectSampler, iUV);//This will return a color from the texture
}


The effect->begin and effect->begin pass all apply here, im just describing the process a little

Edit: In this case, your constants are your 4x4 transform matrices that you need to calculate in your code, and set in the shader

Edit again: I apologize if I am not exactly answering your question. I think you are able to use your fvf and still work with shaders, I just have never done it that way. I moved to programmable pipeline ASAP when I started learning directx9. There are two main reasons, is that programmable is much more powerful, and FVF is deprecated. Especially if you ever plan in the future to move to dx10 or dx11, they do not support FVF, so if I were you, i would forget about FVF regardless and focus on pure programmable pipeline(not fvf).
So there are two ways of working with Shaders, with and without ID3DXEffect?

Does this mean I precompile the HLSL shader to binary so that it is ready during run-time as a resource?

So there are two ways of working with Shaders, with and without ID3DXEffect?


Correct, you do not have to use an effect file to use shaders


Does this mean I precompile the HLSL shader to binary so that it is ready during run-time as a resource?


I may be wrong, but I believe shader programs are compiled during run time when switching between techniques(what shader programs to use when drawing)
Do you know of a good tutorial describing each of these steps? I want to get the whole picture.

How would you static link the shader code to the executable binary such that no other file dependancies are needed (Visual Studio 2008 c++)?
You don't precompile shader programs and then link them, like you would a library or dll. They are implicitly compiled at runtime on the GPU, no explicit linking required =)
When you compile an effect, all possible shader permutations that it can use are pre-compiled. Setting a technique just switched which shader(s) get used.

Tispe: there's a sample in the SDK called "HLSLwithoutEffects", which demonstrates how to compile raw vertex + pixel shaders and use them at runtime. That sample compiles the shaders at runtime, but you can do it offline using fxc.exe (command line compiler included with the SDK) or using a custom tool.

If you want the compiled shader code to be part of the executable, then you'll want to precompile the shader to a file and then add that file as a resource. Then at runtime you can get a pointer to that resource data, and pass it to CreateVertexShader/CreatePixelShader. You can also add the text code as a resource, and then compile that at runtime with D3DXCompileShaderFromResource
Leave it to MJP to set things straight... BTW, your profile picture are one of the ones I always try to find whenever I have a question. =P

This topic is closed to new replies.

Advertisement