• Advertisement
Sign in to follow this  

Porting from Fixed to Programmable pipeline

This topic is 2147 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
[CODE]
#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
};
[/CODE]


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 [b]FVF definition[/b] to accomodate Bone indices and Weights (I only have 2 bones max influencing a vertex)?
[CODE]
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
};
[/CODE]
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?

Share this post


Link to post
Share on other sites
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
[code]
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()
};
[/code]

And then to use it
[code]
if(d3dDevice->CreateVertexDeclaration(vEle, &myIDirect3DVertexDeclation9Pointer) != D3D_OK)
//Error

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

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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
[code]
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;
}
[/code]

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.

[code]
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
}
[/code]

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).

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[quote name='Tispe' timestamp='1330804778' post='4918947']
So there are two ways of working with Shaders, with and without ID3DXEffect?
[/quote]

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

[quote name='Tispe' timestamp='1330804778' post='4918947']
Does this mean I precompile the HLSL shader to binary so that it is ready during run-time as a resource?
[/quote]

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)

Share this post


Link to post
Share on other sites
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++)?

Share this post


Link to post
Share on other sites
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 =)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Why is it that sample code does not help at all -.-
Wall-of-Text sample <<<<<< An elegant tutorial.

I am too discouraged to look away from ID3DXEffect now....


So. Back on topic. Question 2,3 and 4 still up for grabs.

Share this post


Link to post
Share on other sites
What was posted was hardly a wall of samples. It was 10-15 lines of code at most with explanations of what is going on. Also, you shouldn't be discouraged. Learning something like Directx and the theory ect... to simulating a 3d world is a lot of work!

[quote name='Tispe' timestamp='1330879469' post='4919202']
So. Back on topic. Question 2,3 and 4 still up for grabs.
[/quote]

2. The typical way to include bone indices in your vertex is to create your own structure that allows for multiple declarations(ie. MyStruct(or class) has 4 int values) that define what index to use. You could also use a D3DXVECTOR4(4 float values used as indices, allowing for a vertex to belong to four bones). Then in your FVF, you would use a texture coordinate semantic(D3DFVF_TEX0,1,2,3). The tex coord semantic is often used for miscellaneous info, not just UV coordinates. You would do the same thing for bone weights.

3. You should be able to, except instead of using POSITION0, you would use POSITION1.

4. You could use a 4x4 matrix to transform, the extra stuff will just get cut out. In reality, you want to use a fourth homogenous coordinate 'w'. This will come in handy later when performing quaternion rotations and avoid gimbal locks. [url="http://en.wikipedia.org/wiki/Gimbal_lock"]http://en.wikipedia....iki/Gimbal_lock[/url]

Edit: Just a disclaimer, I am not all that well versed on bone animating, but this is what I "Think" I know... =)

Edit again: Here is a great site with easy to understand tutorials. [url="http://toymaker.info/Games/index.html"]http://toymaker.info/Games/index.html [/url]
Including effect files

Share this post


Link to post
Share on other sites
[quote name='Slig Commando' timestamp='1330885183' post='4919225']
4. You could use a 4x4 matrix to transform, the extra stuff will just get cut out. In reality, you want to use a fourth homogenous coordinate 'w'. This will come in handy later when performing quaternion rotations and avoid gimbal locks. http://en.wikipedia....iki/Gimbal_lock
[/quote]
[quote name='Tispe' timestamp='1330785943' post='4918858']
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?
[/quote]

You vertex structure may define a 3 component position. This is totally correct. There are rules for shaders how they handle this situation. ie. program side position has 3 components and shader side expects 4 components. In this particular case, when reading the position value, the GPU pads the 3-component vector with 1.0 for the w-component. So in the shader the position value will be float4(position.xyz,1.0f), which makes the value suitable for to work correctly with 4x4 matrix. Of course, nothing prevents you to provide the 4th component in your vertex structure, but that's unnecessary.

As for gimbal locks or quaternions... I think that they aren't related to this question.

Best regards!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement