Sign in to follow this  
Medo Mex

Sending World, View and Projection Matrices To D3D9 Vertex Shader

Recommended Posts

Medo Mex    891

Passing world, view, project matrices to the vertex shader in D3D11 can be done by creating constant buffer, then using deviceContext->VSSetConstantBuffers()

 

Now I'm trying to do the exact same thing in D3D9, I have in the shader file:

cbuffer MatrixBuffer
{
     matrix worldMatrix;
     matrix viewMatrix;
     matrix projectionMatrix;
};

How do I pass world, view and projection matrices to this vertex shader in D3D9?

 

EDIT: I don't want to use ID3DXEffect.

Edited by Medo3337

Share this post


Link to post
Share on other sites
Jason Z    6436

There is no such thing as a constant buffer in D3D9 - instead you set individual registers.  Have you taken a look at some starter D3D9 tutorials yet?

Share this post


Link to post
Share on other sites
Buckeye    10747

Without ID3DXEffect, the simplest method is to get the ID3DXConstantTable from the D3DXCompileShaderFromFile call. Use the handles extracted from the constant table to set your variables. E.g., constantTable->SetMatrix(...).

 

Alternatively, use the data stream from dev9->CreateVertexShader(...) as the argument in D3DXGetShaderConstantTable(...).

Edited by Buckeye

Share this post


Link to post
Share on other sites
Medo Mex    891

@Buckeye: I want to have a single method, for example: IRenderer::SetTransform(D3DXMATRIX matWorld, D3DXMATRIX matView, D3DXMATRIX matProj)

 

The above method should be able to set the shader constants "world, view, projection" on either D3D9 or D3D11 (based on the D3D version I choose)

 

I mean I can't use cbuffer in my shader since it's not support by D3D9, so if I will use constant table in D3D9, what should I use in D3D11 to do the same thing with the same shader?

Share this post


Link to post
Share on other sites
Buckeye    10747

Now you're playing "Bring me a rock." With each suggestion, you respond with "That's not the right rock."

 


I want to have a single method ... The above method should be able to set the shader constants "world, view, projection" on either D3D9 or D3D11 (based on the D3D version I choose)

 

As it appears you're not familiar with how D3D9 "works," follow JasonZ's suggestion. There are also pertinent D3D9 examples in the DXSDK, notably "HLSLwithoutEffects."

 

If you're looking for the function call SetMatrixWithEitherD3D9OrD3D11(...)  there ain't no such thing. They are different APIs and they have different methods to accomplish things.

If you choose D3D9, use D3D9 methods. If you choose D3D11, use D3D11 methods.

Share this post


Link to post
Share on other sites
Jason Z    6436


If you're looking for the function call SetMatrixWithEitherD3D9OrD3D11(...) there ain't no such thing. They are different APIs and they have different methods to accomplish things.

If you choose D3D9, use D3D9 methods. If you choose D3D11, use D3D11 methods.

This, in particular, is the answer to your question.  The point is that you would need to build a small wrapper layer to abstract this particular feature away in order to communize the two APIs. 

Share this post


Link to post
Share on other sites
Medo Mex    891

@Buckeye: Do you mean I should use constant buffers for D3D11 and registers for D3D9?

 

If yes, does that means I should use vertex shader macros to check if it's D3D9 or D3D11 and define the matrices accordingly?

 

Example

VS:

#ifdef D3D11
cbuffer MatrixBuffer
{
     matrix worldMatrix;
     matrix viewMatrix;
     matrix projectionMatrix;
};
#else
     float4x4 worldMatrix;
     float4x4 viewMatrix;
     float4x4 projectionMatrix;
#endif

 

Thanks for your help.

Edited by Medo3337

Share this post


Link to post
Share on other sites
Buckeye    10747


Do you mean I should use constant buffers for D3D11 and registers for D3D9? If yes, does that means I should use vertex shader macros to check if it's D3D9 or D3D11 and define the matrices accordingly?

Example ...

 

"Yes" to constant buffers for D3D11. "Probably no" to "registers" for D3D9. What do you mean by using "registers" in D3D9? As suggested several times now, and as you clearly don't understand how shaders are used in D3D9, there are a lot of tutorials, books and examples for you to work through.

 

Defining macros in the shader code you just posted has nothing to do with your original question, or the responses you've gotten. With regard to the macros, it's likely they're unnecessary, as you're probably using shader model 4 or above. If you have specific questions about shader code that doesn't work in your app, you should start a new topic.

 

You asked-


How do I pass world, view and projection matrices to this vertex shader in D3D9?

 

That's what this topic has been about, and responses provided.

Share this post


Link to post
Share on other sites
Medo Mex    891

@Buckeye: Well, I have been writing VS and PS for long time, so you shouldn't really judge other people.

 

Perhaps there is misunderstanding.

 

FYI, Using macros resolved the problem, I used constant buffers in D3D11 and ID3DXConstantTable in D3D9 to pass world, view and projection matrices.

Share this post


Link to post
Share on other sites
BornToCode    1185

@Buckeye. At the end of the day everything is store into some sort of register. I guess what he is getting at is that, he can just store them directly without using a Constant buffer which is still stored into a register at the end of the day.DirectX9 has no cocept of Constant Buffer so you need to set up directly just like you have in that other example.

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