Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


Porting from Fixed to Programmable pipeline


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 Tispe   Members   -  Reputation: 1039

Like
0Likes
Like

Posted 03 March 2012 - 08:45 AM

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?

Sponsor:

#2 Slig Commando   Members   -  Reputation: 334

Like
0Likes
Like

Posted 03 March 2012 - 11:52 AM

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.

#3 Tispe   Members   -  Reputation: 1039

Like
0Likes
Like

Posted 03 March 2012 - 12:11 PM

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?

#4 Slig Commando   Members   -  Reputation: 334

Like
0Likes
Like

Posted 03 March 2012 - 12:21 PM

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

#5 Tispe   Members   -  Reputation: 1039

Like
0Likes
Like

Posted 03 March 2012 - 01:59 PM

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?

#6 Slig Commando   Members   -  Reputation: 334

Like
0Likes
Like

Posted 03 March 2012 - 02:32 PM

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)

#7 Tispe   Members   -  Reputation: 1039

Like
0Likes
Like

Posted 03 March 2012 - 05:46 PM

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++)?

#8 Slig Commando   Members   -  Reputation: 334

Like
0Likes
Like

Posted 03 March 2012 - 05:49 PM

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

#9 MJP   Moderators   -  Reputation: 11585

Like
0Likes
Like

Posted 03 March 2012 - 05:54 PM

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

#10 Slig Commando   Members   -  Reputation: 334

Like
0Likes
Like

Posted 03 March 2012 - 06:04 PM

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

#11 Tispe   Members   -  Reputation: 1039

Like
0Likes
Like

Posted 04 March 2012 - 10:44 AM

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.

#12 Slig Commando   Members   -  Reputation: 334

Like
0Likes
Like

Posted 04 March 2012 - 12:19 PM

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!

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


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. http://en.wikipedia....iki/Gimbal_lock

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. http://toymaker.info/Games/index.html
Including effect files

#13 Tispe   Members   -  Reputation: 1039

Like
0Likes
Like

Posted 04 March 2012 - 02:08 PM

sorry I refered to "HLSLwithoutEffects" sample.

#14 kauna   Crossbones+   -  Reputation: 2747

Like
0Likes
Like

Posted 04 March 2012 - 05:14 PM

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

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?


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!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS