Skip to main content

History: Your First Direct3D11 Shader

Source of version: 2 (current)

Copy to clipboard
            {maketoc}

!!Description
Because the Direct3D11 rendering subsystem for OGRE doesn't support Fixed Function Pipeline, it's necessary that you write your own vertex and fragment shaders, even the simplest of materials require shaders, and so, let this serve to you as a starting point for using the Direct3D11 rendering subsystem.

Note: This shader will __not__ work with Direct3D__9__ or OpenGL.

{IMG(src="http://img1.uploadscreenshot.com/images/orig/10/29418051843-orig.jpg",button="y",width="512", height="425", desc="ogrehead.mesh With the shader and material applied.",alt="Shader in Action")}{IMG}

!!Usage
Just give the material __Shader1_HLSL_4_0__ to your object/entity.

!!Shader1_fp_4_0.hlsl
{CODE(wrap="1", colors="c++")}float4 mainFP() : SV_TARGET 
{
		   
	return float4(1.0, 0.7, 0.0, 1.0);
}
{CODE}

!!Shader1_vp_4_0.hlsl
{CODE(wrap="1", colors="c++")}float4 mainVP(float4 pos : SV_POSITION, uniform float4x4 worldViewProj_m) : SV_Position
{
	// This keeps the object in place by multiplying the world view projection matrix by the position.
	return mul(worldViewProj_m, pos);
}
{CODE}

!!Shader1_4.0.material
Note: See [http://www.ogre3d.org/docs/manual/manual_18.html] for a list of profiles, but since the Direct3D11 rendering subsystem is not yet finished, profiles 4.0 and above are not listed.
Note #2: Shader profiles less than 4.0 (vs_4_0, ps_4_0) will not work with Direct3D11, and vice-versa (shader profiles greater than 3.0 (ps_3_0, vs_3_0) will not work with Direct3D9).

{CODE(wrap="1", colors="c++")}vertex_program Shader1_HLSL_vp_4 hlsl
{
	source  Shader1_vp_4_0.hlsl
	entry_point mainVP
	target vs_4_0
	default_params
	{
		param_named_auto worldViewProj_m worldviewproj_matrix
	}
}
 
fragment_program Shader1_HLSL_fp_4 hlsl
{
	source Shader1_fp_4_0.hlsl
	entry_point mainFP
	target ps_4_0
}

material Shader1_HLSL_4_0
{
	technique
	{
		pass
		{
			vertex_program_ref Shader1_HLSL_vp_4
			{
			}
			fragment_program_ref Shader1_HLSL_fp_4
			{
			}
		}
	}
}
{CODE}

!!Recommendations
To learn more about HLSL (or any shaders), you can read ((JaJDoo Shader Guide|JaJDoo's Shader Guide)), which does a very good job at explaining how HLSL (and shaders in general) work.
Since JaJDoo's shader guide is focused on Direct3D9 shader development, you should also read [http://msdn.microsoft.com/en-us/library/windows/desktop/bb509647(v=vs.85).aspx|the MSDN article on the semantic updates] that came with the new shader model/profile.

---
Alias: (alias(Your_First_Direct3D11_Shader))