• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Tispe

Porting from Fixed to Programmable pipeline

13 posts in this topic

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?
0

Share this post


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

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?
0

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

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?
0

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

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

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

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
0

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
0

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

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
0

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!
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0