Sending World, View and Projection Matrices To D3D9 Vertex Shader

Started by
9 comments, last by BornToCode 9 years, 1 month ago

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.

Advertisement

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?

@Jason Z: Well, I'm trying to get the same shader to work with both D3D9 and D3D11.

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

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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

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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.


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.

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


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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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

This topic is closed to new replies.

Advertisement