summaryrefslogtreecommitdiff
path: root/Runtime/GfxDevice/d3d11/InternalShaders/internalshaders.hlsl
blob: 2dce211d79a09a9aac8f65564ba1ea7fa4f792d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//--------------------------------------------------------------------------------------
// Vertex shaders for stream-out GPU skinning.
//--------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------
// Globals
//--------------------------------------------------------------------------------------
cbuffer cbBones : register ( b0 )
{
	float4x3 bones[BONECOUNT];
}

//--------------------------------------------------------------------------------------
// Input / Output structures
//--------------------------------------------------------------------------------------
struct VS_IN_MEX
{
	// Stream 0
    float3  Position        : POSITION;
    float3	Normal			: NORMAL;
    float4	Tangent			: TANGENT;

	// Stream 1
#if BONESPERVERTEX == 4
    float4  BoneWeights     : BLENDWEIGHT;
	int4  BoneIndices     : BLENDINDICES;

#elif BONESPERVERTEX == 2
    float2  BoneWeights     : BLENDWEIGHT;
	int2  BoneIndices     : BLENDINDICES;

#else // 1 bone per vertex
	int  BoneIndices     : BONEINDEX;

#endif
};

struct VS_OUTPUT
{
	float3 vPosition	: POSITION;
	float3 vNormal		: TEXCOORD0;
	float4 vTangent		: TEXCOORD1;
};

// --- Bones ---
inline float4x3 FetchBoneMatrix( int boneIndex )
{   
	return (float4x3)(bones[boneIndex]);
}
//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
VS_OUTPUT VSMain( VS_IN_MEX Input, uniform bool useNormal, uniform bool useTangent)
{
	VS_OUTPUT Output = (VS_OUTPUT)0;
	
#if BONESPERVERTEX == 4
	float boneWeights[4] = (float[4])Input.BoneWeights;  
    int boneIndices[4] = (int[4])Input.BoneIndices;
	
	float4x3 localToWorldMatrix = boneWeights[0] * FetchBoneMatrix(boneIndices[0]) +
				boneWeights[1] * FetchBoneMatrix(boneIndices[1]) +
				boneWeights[2] * FetchBoneMatrix(boneIndices[2]) +
				boneWeights[3] * FetchBoneMatrix(boneIndices[3]);

#elif BONESPERVERTEX == 2
	float boneWeights[2] = (float[2])Input.BoneWeights;  
    int boneIndices[2] = (int[2])Input.BoneIndices;
	
	float4x3 localToWorldMatrix = boneWeights[0] * FetchBoneMatrix(boneIndices[0]) +
				boneWeights[1] * FetchBoneMatrix(boneIndices[1]);


#else // 1
    int boneIndex = Input.BoneIndices;
	
	float4x3 localToWorldMatrix = FetchBoneMatrix(boneIndex);
#endif

	// Position
	Output.vPosition = mul( float4(Input.Position.xyz, 1.0), localToWorldMatrix ).xyz;
	if (useNormal)
    {
		Output.vNormal = normalize( mul( float4(Input.Normal.xyz,0.0f), localToWorldMatrix ) ).xyz;
	}
	
	// Tangent
	if (useTangent)
	{
		float3 outTangent3 = normalize( mul( float4(Input.Tangent.xyz,0.0f), localToWorldMatrix ) ).xyz;
		Output.vTangent = float4(outTangent3, Input.Tangent.w);
	}
	
	return Output;
}

// Functions are named StreamOutSkinVS_<components>_<bonespervertex>_<maxbonecount>

#define MERGE(a, b, c, delim) a##delim##b##delim##c

VS_OUTPUT MERGE(StreamOutSkinVS_Position,BONESPERVERTEX,BONECOUNT,_)(VS_IN_MEX Input)
{
	return VSMain(Input, false, false);
}

VS_OUTPUT MERGE(StreamOutSkinVS_Position_Normal,BONESPERVERTEX,BONECOUNT,_)(VS_IN_MEX Input)
{
	return VSMain(Input, true, false);
}

VS_OUTPUT MERGE(StreamOutSkinVS_Position_Normal_Tangent,BONESPERVERTEX,BONECOUNT,_)(VS_IN_MEX Input)
{
	return VSMain(Input, true, true);
}

VS_OUTPUT MERGE(StreamOutSkinVS_Position_Tangent,BONESPERVERTEX,BONECOUNT,_)(VS_IN_MEX Input)
{
	return VSMain(Input, false, true);
}