Setting up DirectX issues...

Started by
17 comments, last by SteveHatcher 10 years, 4 months ago

Oh Sorry, Im guessing you mean the .fx file that D3Dx11CompileFromFile is grabbing?

It is:


float4 VS_Main( float4 pos : POSITION ) : SV_POSITION
{
    return pos;
}


float4 PS_Main( float4 pos : SV_POSITION ) : SV_TARGET
{
    return float4( 0.0f, 1.0f, 0.0f, 1.0f );
}

A solid green shader as far as I am aware.

Advertisement

Your vertex shader just passes the unchanged vertex positions. To apply any transformation, e.g. rotation, you need to multiply with the transformation matrix you assigned to the constant buffer.

Precisely. Yeah, that's a pass-through vertex shader, the position doesn't change at all wink.png

It should look something like this:


cbuffer VSParameters : register(b0)  // deliberately assigning slot 0 !
{
	matrix WVP;
};

float4 VS_Main( float4 pos : POSITION ) : SV_POSITION
{
	return mul(position, WVP);
}
Also, you probably want to rotate (and/or scale) first, then translate. Order of multiplication matters with matrices:


XMMATRIX TriangleWorld = rotationZ * translation;

Okay, thanks guys. I am still slowly digging my way through the literature.

Is there a way to create a simple shader like this but entirely in a struct, or class of my definition so it does not need a separate .fx file? If so, what function do I use instead of the D3Dx11CompileFromFile?

I hope my question makes sense. Thanks. you guys are extremely helpful and awesome!

Not quite sure what you mean. You can have several shaders in the same file (like you already do for both vertex and pixel shader). And there are compile functions which take source code directly, without the detour of a file, e.g. D3DX11CompileFromMemory or alternatively the newer compile function D3DCompile.

Put your HLSL source as a static string in your cpp file and feed it to one of those functions.

(Alternatively one could even let the command line compiler fxc spit out compiled binaries as a hex array source code, with the option /Fh. This way no runtime compilation is needed)


Is there a way to create a simple shader like this but entirely in a struct, or class of my definition so it does not need a separate .fx file? If so, what function do I use instead of the D3Dx11CompileFromFile?

No, that doesn't make much sense.

Of course you could include the source of your HLSL file as string constant in your code, but this is not very common.

If you don't want to compile your shaders at runtime, you can do it offline using the fxc.exe command line compiler. Take this:

http://msdn.microsoft.com/en-us/library/windows/desktop/bb509709%28v=vs.85%29.aspx

What I do is this: I let fxc.exe create an header file (command line option /Fh). This .h file contains a byte array with the compiled shader code that can be passed to e.g. ID3D11Device::CreateVertexShader.

Just out of interest, why do they have them in a separate file? I can't see the advantage of this as opposed to having them in a header file and just including it?

Thanks

I'm not sure if I get the idea behind your question. You have a project with mixed languages: C++ and HLSL. They have different compilers. Why would you mix the languages in a single file?

I'm not sure if I get the idea behind your question. You have a project with mixed languages: C++ and HLSL. They have different compilers. Why would you mix the languages in a single file?

That answers my question, didn't click about it being a different language lol. Thanks

This topic is closed to new replies.

Advertisement