# Sending World, View and Projection Matrices To D3D9 Vertex Shader

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

## Recommended Posts

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 on other sites

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 on other sites

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

##### Share on other sites

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 on other sites

@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 on other sites

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 on other sites

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 on other sites

@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

Edited by Medo3337

##### Share on other sites

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.

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 on other sites

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

• 10
• 16
• 14
• 18
• 15