protected bool AddPSFinalAssignmentInvocation( Function psMain, int groupOrder, ref int internalCounter )
		{
			var curFuncInvocation = new FunctionInvocation( FFPRenderState.FFPFuncAssign,
			                                                (int)FFPRenderState.FFPFragmentShaderStage.PSColorBegin + 1,
			                                                internalCounter++ );
			curFuncInvocation.PushOperand( this.psTempDiffuseColor, Operand.OpSemantic.In );
			curFuncInvocation.PushOperand( this.psDiffuse, Operand.OpSemantic.Out );
			psMain.AddAtomInstance( curFuncInvocation );

			curFuncInvocation = new FunctionInvocation( FFPRenderState.FFPFuncAssign,
			                                            (int)FFPRenderState.FFPFragmentShaderStage.PSColorBegin + 1,
			                                            internalCounter++ );
			curFuncInvocation.PushOperand( this.psDiffuse, Operand.OpSemantic.In );
			curFuncInvocation.PushOperand( this.psOutDiffuse, Operand.OpSemantic.Out );
			psMain.AddAtomInstance( curFuncInvocation );

			if ( this.specularEnabled )
			{
				curFuncInvocation = new FunctionInvocation( FFPRenderState.FFPFuncAssign,
				                                            (int)FFPRenderState.FFPFragmentShaderStage.PSColorBegin + 1,
				                                            internalCounter++ );
				curFuncInvocation.PushOperand( this.psTempSpecularColor, Operand.OpSemantic.In );
				curFuncInvocation.PushOperand( this.psSpecular, Operand.OpSemantic.Out );
				psMain.AddAtomInstance( curFuncInvocation );
			}


			return true;
		}
		/// <summary>
		///   Generates local parameters for the split parameters and perform packing/unpacking operation using them.
		/// </summary>
		/// <param name="fun"> </param>
		/// <param name="progType"> </param>
		/// <param name="mergedParams"> </param>
		/// <param name="splitParams"> </param>
		/// <param name="localParamsMap"> </param>
		internal static void GenerateLocalSplitParameters( Function func, GpuProgramType progType,
		                                                   List<MergeParameter> mergedParams,
		                                                   List<Parameter> splitParams,
		                                                   Dictionary<Parameter, Parameter> localParamsMap )
		{
			//No split params created.
			if ( splitParams.Count == 0 )
			{
				return;
			}

			//Create the local parameters + map from source to local
			for ( int i = 0; i < splitParams.Count; i++ )
			{
				Parameter srcParameter = splitParams[ i ];
				Parameter localParameter = func.ResolveLocalParameter( srcParameter.Semantic, srcParameter.Index,
				                                                       "lssplit_" + srcParameter.Name, srcParameter.Type );

				localParamsMap.Add( srcParameter, localParameter );
			}

			int invocationCounter = 0;

			//Establish link between the local parameter to the merged parameter.
			for ( int i = 0; i < mergedParams.Count; i++ )
			{
				var curMergeParameter = mergedParams[ i ];

				for ( int p = 0; p < curMergeParameter.SourceParameterCount; p++ )
				{
					Parameter srcMergedParameter = curMergeParameter.SourceParameter[ p ];

					if ( localParamsMap.ContainsKey( srcMergedParameter ) )
					{
						//Case it is the vertex shader -> assign the local parameter to the output merged parameter
						if ( progType == GpuProgramType.Vertex )
						{
							var curFuncInvocation = new FunctionInvocation( FFPRenderState.FFPFuncAssign,
							                                                (int)
							                                                FFPRenderState.FFPVertexShaderStage.
							                                                	VSPostProcess, invocationCounter++ );

							curFuncInvocation.PushOperand( localParamsMap[ srcMergedParameter ], Operand.OpSemantic.In,
							                               curMergeParameter.GetSourceParameterMask( p ) );
							curFuncInvocation.PushOperand(
								curMergeParameter.GetDestinationParameter( (int)Operand.OpSemantic.Out, i ),
								Operand.OpSemantic.Out, curMergeParameter.GetDestinationParameterMask( p ) );
							func.AddAtomInstance( curFuncInvocation );
						}
						else if ( progType == GpuProgramType.Fragment )
						{
							var curFuncInvocation = new FunctionInvocation( FFPRenderState.FFPFuncAssign,
							                                                (int)
							                                                FFPRenderState.FFPFragmentShaderStage.
							                                                	PSPreProcess, invocationCounter++ );

							curFuncInvocation.PushOperand(
								curMergeParameter.GetDestinationParameter( (int)Operand.OpSemantic.In, i ),
								Operand.OpSemantic.In, curMergeParameter.GetDestinationParameterMask( p ) );
							curFuncInvocation.PushOperand( localParamsMap[ srcMergedParameter ], Operand.OpSemantic.Out,
							                               curMergeParameter.GetSourceParameterMask( p ) );
							func.AddAtomInstance( curFuncInvocation );
						}
					}
				}
			}
		}
		protected bool AddPSGlobalIlluminationInvocation( Function psMain, int groupOrder, ref int internalCount )
		{
			FunctionInvocation curFuncInvocation = null;

			if ( ( this.trackVertexColorType & TrackVertexColor.Ambient ) == 0 &&
			     ( this.trackVertexColorType & TrackVertexColor.Emissive ) == 0 )
			{
				curFuncInvocation = new FunctionInvocation( FFPRenderState.FFPFuncAssign, groupOrder, internalCount++ );
				curFuncInvocation.PushOperand( this.derivedSceneColor, Operand.OpSemantic.In );
				curFuncInvocation.PushOperand( this.psTempDiffuseColor, Operand.OpSemantic.Out );
				psMain.AddAtomInstance( curFuncInvocation );
			}
			else
			{
				if ( ( this.trackVertexColorType & TrackVertexColor.Ambient ) != 0 )
				{
					curFuncInvocation = new FunctionInvocation( FFPRenderState.FFPFuncModulate, groupOrder,
					                                            internalCount++ );
					curFuncInvocation.PushOperand( this.lightAmbientColor, Operand.OpSemantic.In );
					curFuncInvocation.PushOperand( this.psDiffuse, Operand.OpSemantic.In );
					curFuncInvocation.PushOperand( this.psTempDiffuseColor, Operand.OpSemantic.Out );
					psMain.AddAtomInstance( curFuncInvocation );
				}
				else
				{
					curFuncInvocation = new FunctionInvocation( FFPRenderState.FFPFuncAssign, groupOrder,
					                                            internalCount++ );
					curFuncInvocation.PushOperand( this.derivedAmbientLightColor, Operand.OpSemantic.In,
					                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
					                                 (int)Operand.OpMask.Z ) );
					curFuncInvocation.PushOperand( this.psDiffuse, Operand.OpSemantic.In,
					                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
					                                 (int)Operand.OpMask.Z ) );
					curFuncInvocation.PushOperand( this.psTempDiffuseColor, Operand.OpSemantic.Out );
					psMain.AddAtomInstance( curFuncInvocation );
				}

				if ( ( this.trackVertexColorType & TrackVertexColor.Emissive ) != 0 )
				{
					curFuncInvocation = new FunctionInvocation( FFPRenderState.FFPFuncAdd, groupOrder, internalCount++ );
					curFuncInvocation.PushOperand( this.psDiffuse, Operand.OpSemantic.In );
					curFuncInvocation.PushOperand( this.psTempDiffuseColor, Operand.OpSemantic.In );
					curFuncInvocation.PushOperand( this.psTempDiffuseColor, Operand.OpSemantic.Out );
					psMain.AddAtomInstance( curFuncInvocation );
				}
				else
				{
					curFuncInvocation = new FunctionInvocation( FFPRenderState.FFPFuncAdd, groupOrder, internalCount++ );
					curFuncInvocation.PushOperand( this.surfaceEmissiveColor, Operand.OpSemantic.In );
					curFuncInvocation.PushOperand( this.psTempDiffuseColor, Operand.OpSemantic.In );
					curFuncInvocation.PushOperand( this.psTempDiffuseColor, Operand.OpSemantic.Out );
					psMain.AddAtomInstance( curFuncInvocation );
				}
			}
			if ( this.specularEnabled )
			{
				curFuncInvocation = new FunctionInvocation( FFPRenderState.FFPFuncAssign, groupOrder, internalCount++ );
				curFuncInvocation.PushOperand( this.psSpecular, Operand.OpSemantic.In );
				curFuncInvocation.PushOperand( this.psTempSpecularColor, Operand.OpSemantic.Out );
				psMain.AddAtomInstance( curFuncInvocation );
			}
			return true;
		}
		protected bool AddPSIlluminationInvocation( LightParams curLightParams, Function psMain, int groupOrder,
		                                            ref int internalCounter )
		{
			FunctionInvocation curFuncInvocation = null;
			//Merge diffuse color with vertex color if need to
			if ( ( this.trackVertexColorType & TrackVertexColor.Diffuse ) != 0 )
			{
				curFuncInvocation = new FunctionInvocation( FFPRenderState.FFPFuncModulate, groupOrder,
				                                            internalCounter++ );
				curFuncInvocation.PushOperand( this.psDiffuse, Operand.OpSemantic.In,
				                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y | (int)Operand.OpMask.Z ) );
				curFuncInvocation.PushOperand( curLightParams.DiffuseColor, Operand.OpSemantic.In,
				                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y | (int)Operand.OpMask.Z ) );
				curFuncInvocation.PushOperand( curLightParams.DiffuseColor, Operand.OpSemantic.Out,
				                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y | (int)Operand.OpMask.Z ) );
				psMain.AddAtomInstance( curFuncInvocation );
			}

			//Merge specular color with vertex color if need to
			if ( this.specularEnabled && ( this.trackVertexColorType & TrackVertexColor.Specular ) != 0 )
			{
				curFuncInvocation = new FunctionInvocation( FFPRenderState.FFPFuncModulate, groupOrder,
				                                            internalCounter++ );
				curFuncInvocation.PushOperand( this.psDiffuse, Operand.OpSemantic.In,
				                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y | (int)Operand.OpMask.Z ) );
				curFuncInvocation.PushOperand( curLightParams.SpecularColor, Operand.OpSemantic.In,
				                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y | (int)Operand.OpMask.Z ) );
				curFuncInvocation.PushOperand( curLightParams.SpecularColor, Operand.OpSemantic.Out,
				                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y | (int)Operand.OpMask.Z ) );
				psMain.AddAtomInstance( curFuncInvocation );
			}

			switch ( curLightParams.Type )
			{
				case LightType.Directional:
					if ( this.specularEnabled )
					{
						curFuncInvocation = new FunctionInvocation( SGXFuncLightDirectionDiffuseSpecular, groupOrder,
						                                            internalCounter++ );
						curFuncInvocation.PushOperand( this.psNormal, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( this.psInView, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( curLightParams.PSInDirection, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( curLightParams.DiffuseColor, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( curLightParams.SpecularColor, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( this.surfaceShininess, Operand.OpSemantic.In );
						curFuncInvocation.PushOperand( this.psTempDiffuseColor, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( this.psTempSpecularColor, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( this.psTempDiffuseColor, Operand.OpSemantic.Out,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( this.psTempSpecularColor, Operand.OpSemantic.Out,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						psMain.AddAtomInstance( curFuncInvocation );
					}
					else
					{
						curFuncInvocation = new FunctionInvocation( SGXFuncLightDirectionalDiffuse, groupOrder,
						                                            internalCounter++ );
						curFuncInvocation.PushOperand( this.psNormal, Operand.OpSemantic.In );
						curFuncInvocation.PushOperand( curLightParams.PSInDirection, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( curLightParams.DiffuseColor, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( this.psTempDiffuseColor, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( this.psTempDiffuseColor, Operand.OpSemantic.Out,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						psMain.AddAtomInstance( curFuncInvocation );
					}
					break;
				case LightType.Point:
					if ( this.specularEnabled )
					{
						curFuncInvocation = new FunctionInvocation( SGXFuncLightPointDiffuseSpecular, groupOrder,
						                                            internalCounter++ );
						curFuncInvocation.PushOperand( this.psNormal, Operand.OpSemantic.In );
						curFuncInvocation.PushOperand( this.psInView, Operand.OpSemantic.In );
						curFuncInvocation.PushOperand( curLightParams.PSInToLightDir, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( curLightParams.AttenuatParams, Operand.OpSemantic.In );
						curFuncInvocation.PushOperand( curLightParams.DiffuseColor, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( curLightParams.SpecularColor, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( this.surfaceShininess, Operand.OpSemantic.In );
						curFuncInvocation.PushOperand( this.psTempDiffuseColor, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( this.psTempSpecularColor, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( this.psTempDiffuseColor, Operand.OpSemantic.Out,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( this.psTempSpecularColor, Operand.OpSemantic.Out,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						psMain.AddAtomInstance( curFuncInvocation );
					}
					else
					{
						curFuncInvocation = new FunctionInvocation( SGXFuncLightPointDiffuse, groupOrder,
						                                            internalCounter++ );
						curFuncInvocation.PushOperand( this.psNormal, Operand.OpSemantic.In );
						curFuncInvocation.PushOperand( curLightParams.PSInToLightDir, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( curLightParams.AttenuatParams, Operand.OpSemantic.In );
						curFuncInvocation.PushOperand( curLightParams.DiffuseColor, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( this.psTempDiffuseColor, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( this.psTempDiffuseColor, Operand.OpSemantic.Out,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						psMain.AddAtomInstance( curFuncInvocation );
					}
					break;
				case LightType.Spotlight:
					if ( this.specularEnabled )
					{
						curFuncInvocation = new FunctionInvocation( SGXFuncLightSpotDiffuseSpecular, groupOrder,
						                                            internalCounter++ );
						curFuncInvocation.PushOperand( this.psNormal, Operand.OpSemantic.In );
						curFuncInvocation.PushOperand( this.psInView, Operand.OpSemantic.In );
						curFuncInvocation.PushOperand( curLightParams.PSInToLightDir, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( curLightParams.PSInDirection, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( curLightParams.AttenuatParams, Operand.OpSemantic.In );
						curFuncInvocation.PushOperand( curLightParams.SpotParams, Operand.OpSemantic.In );
						curFuncInvocation.PushOperand( curLightParams.DiffuseColor, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( curLightParams.SpecularColor, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( this.surfaceShininess, Operand.OpSemantic.In );
						curFuncInvocation.PushOperand( this.psTempDiffuseColor, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( this.psTempSpecularColor, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( this.psTempDiffuseColor, Operand.OpSemantic.Out,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( this.psTempSpecularColor, Operand.OpSemantic.Out,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						psMain.AddAtomInstance( curFuncInvocation );
					}
					else
					{
						curFuncInvocation = new FunctionInvocation( SGXFuncLightSpotDiffuse, groupOrder,
						                                            internalCounter++ );
						curFuncInvocation.PushOperand( this.psNormal, Operand.OpSemantic.In );
						curFuncInvocation.PushOperand( curLightParams.PSInToLightDir, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( curLightParams.PSInDirection, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( curLightParams.AttenuatParams, Operand.OpSemantic.In );
						curFuncInvocation.PushOperand( curLightParams.SpotParams, Operand.OpSemantic.In );
						curFuncInvocation.PushOperand( curLightParams.DiffuseColor, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( this.psTempDiffuseColor, Operand.OpSemantic.In,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						curFuncInvocation.PushOperand( this.psTempDiffuseColor, Operand.OpSemantic.Out,
						                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
						                                 (int)Operand.OpMask.Z ) );
						psMain.AddAtomInstance( curFuncInvocation );
					}
					break;
			}
			return true;
		}
		internal bool AddVSIlluminationInvocation( LightParams curLightParams, Function vsMain, int groupOrder,
		                                           ref int internalCounter )
		{
			FunctionInvocation curFuncInvocation = null;

			//Computer light direction in texture space.
			if ( curLightParams.Direction != null && curLightParams.VSOutDirection != null )
			{
				//Transform to texture space.
				if ( this.normalMapSpace == RTShaderSystem.NormalMapSpace.Tangent )
				{
					curFuncInvocation = new FunctionInvocation( SGXFuncTranformNormal, groupOrder, internalCounter++ );
					curFuncInvocation.PushOperand( this.vsTBNMatrix, Operand.OpSemantic.In );
					curFuncInvocation.PushOperand( curLightParams.Direction, Operand.OpSemantic.In,
					                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
					                                 (int)Operand.OpMask.Z ) );
					curFuncInvocation.PushOperand( curLightParams.VSOutDirection, Operand.OpSemantic.Out );
					vsMain.AddAtomInstance( curFuncInvocation );
				}
					//Output object space
				else if ( this.normalMapSpace == RTShaderSystem.NormalMapSpace.Object )
				{
					curFuncInvocation = new FunctionInvocation( FFPRenderState.FFPFuncAssign, groupOrder,
					                                            internalCounter++ );
					curFuncInvocation.PushOperand( curLightParams.Direction, Operand.OpSemantic.In,
					                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y |
					                                 (int)Operand.OpMask.Z ) );
					curFuncInvocation.PushOperand( curLightParams.VSOutDirection, Operand.OpSemantic.Out );
					vsMain.AddAtomInstance( curFuncInvocation );
				}
			}

			//Transform light vector to target space
			if ( curLightParams.Position != null && curLightParams.VSOutToLightDir != null )
			{
				//Compute light vector.
				curFuncInvocation = new FunctionInvocation( FFPRenderState.FFPFuncSubtract, groupOrder,
				                                            internalCounter++ );
				curFuncInvocation.PushOperand( curLightParams.Position, Operand.OpSemantic.In,
				                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y | (int)Operand.OpMask.Z ) );
				curFuncInvocation.PushOperand( this.vsWorldPosition, Operand.OpSemantic.In );
				curFuncInvocation.PushOperand( this.vsLocalDir, Operand.OpSemantic.Out );
				vsMain.AddAtomInstance( curFuncInvocation );

				//Transform to object space
				curFuncInvocation = new FunctionInvocation( SGXFuncTranformNormal, groupOrder, internalCounter++ );
				curFuncInvocation.PushOperand( this.worldInvRotMatrix, Operand.OpSemantic.In );
				curFuncInvocation.PushOperand( this.vsLocalDir, Operand.OpSemantic.In );
				curFuncInvocation.PushOperand( this.vsLocalDir, Operand.OpSemantic.Out );
				vsMain.AddAtomInstance( curFuncInvocation );

				if ( this.normalMapSpace == RTShaderSystem.NormalMapSpace.Tangent )
				{
					curFuncInvocation = new FunctionInvocation( SGXFuncTranformNormal, groupOrder, internalCounter++ );
					curFuncInvocation.PushOperand( this.vsTBNMatrix, Operand.OpSemantic.In );
					curFuncInvocation.PushOperand( this.vsLocalDir, Operand.OpSemantic.In );
					curFuncInvocation.PushOperand( curLightParams.VSOutToLightDir, Operand.OpSemantic.Out );
					vsMain.AddAtomInstance( curFuncInvocation );
				}
					//Output object space
				else if ( this.normalMapSpace == RTShaderSystem.NormalMapSpace.Object )
				{
					curFuncInvocation = new FunctionInvocation( FFPRenderState.FFPFuncAssign, groupOrder,
					                                            internalCounter++ );
					curFuncInvocation.PushOperand( this.vsLocalDir, Operand.OpSemantic.In );
					curFuncInvocation.PushOperand( curLightParams.VSOutToLightDir, Operand.OpSemantic.Out );
					vsMain.AddAtomInstance( curFuncInvocation );
				}
			}

			return true;
		}
		protected bool AddPSNormalFetchInvocation( Function psMain, int groupOrder, ref int internalCounter )
		{
			FunctionInvocation curFuncInvocation = null;
			curFuncInvocation = new FunctionInvocation( SGXFuncFetchNormal, groupOrder, internalCounter++ );
			curFuncInvocation.PushOperand( this.normalMapSampler, Operand.OpSemantic.In );
			curFuncInvocation.PushOperand( this.psInTexcoord, Operand.OpSemantic.In );
			curFuncInvocation.PushOperand( this.psNormal, Operand.OpSemantic.Out );
			psMain.AddAtomInstance( curFuncInvocation );

			return true;
		}
		protected bool AddVSInvocation( Function vsMain, int groupOrder, int internalCounter )
		{
			FunctionInvocation curFuncInvocation = null;


			//Construct TNB matrix.
			if ( this.normalMapSpace == RTShaderSystem.NormalMapSpace.Tangent )
			{
				curFuncInvocation = new FunctionInvocation( SGXFuncConstructTbnMatrix, groupOrder, internalCounter++ );
				curFuncInvocation.PushOperand( this.vsInNormal, Operand.OpSemantic.In );
				curFuncInvocation.PushOperand( this.vsInTangent, Operand.OpSemantic.In );
				curFuncInvocation.PushOperand( this.vsTBNMatrix, Operand.OpSemantic.Out );
				vsMain.AddAtomInstance( curFuncInvocation );
			}

			//Output texture coordinates
			curFuncInvocation = new FunctionInvocation( FFPRenderState.FFPFuncAssign, groupOrder, internalCounter++ );
			curFuncInvocation.PushOperand( this.vsInTexcoord, Operand.OpSemantic.In );
			curFuncInvocation.PushOperand( this.vsOutTexcoord, Operand.OpSemantic.Out );
			vsMain.AddAtomInstance( curFuncInvocation );

			//computer world space position
			if ( this.vsWorldPosition != null )
			{
				curFuncInvocation = new FunctionInvocation( SGXFuncTransformPosition, groupOrder, internalCounter++ );
				curFuncInvocation.PushOperand( this.worldMatrix, Operand.OpSemantic.In );
				curFuncInvocation.PushOperand( this.vsInPosition, Operand.OpSemantic.In );
				curFuncInvocation.PushOperand( this.vsWorldPosition, Operand.OpSemantic.Out );
				vsMain.AddAtomInstance( curFuncInvocation );
			}

			//compute the view vector
			if ( this.vsInPosition != null && this.vsOutView != null )
			{
				//View vector in world space

				curFuncInvocation = new FunctionInvocation( FFPRenderState.FFPFuncSubtract, groupOrder,
				                                            internalCounter++ );
				curFuncInvocation.PushOperand( this.camPosWorldSpace, Operand.OpSemantic.In,
				                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y | (int)Operand.OpMask.Z ) );
				curFuncInvocation.PushOperand( this.vsWorldPosition, Operand.OpSemantic.In,
				                               ( (int)Operand.OpMask.X | (int)Operand.OpMask.Y | (int)Operand.OpMask.Z ) );
				curFuncInvocation.PushOperand( this.vsLocalDir, Operand.OpSemantic.Out );
				vsMain.AddAtomInstance( curFuncInvocation );

				//Transform to object space.
				curFuncInvocation = new FunctionInvocation( SGXFuncTranformNormal, groupOrder, internalCounter++ );
				curFuncInvocation.PushOperand( this.worldInvRotMatrix, Operand.OpSemantic.In );
				curFuncInvocation.PushOperand( this.vsLocalDir, Operand.OpSemantic.In );
				curFuncInvocation.PushOperand( this.vsLocalDir, Operand.OpSemantic.Out );
				vsMain.AddAtomInstance( curFuncInvocation );

				//Transform to tangent space
				if ( this.normalMapSpace == RTShaderSystem.NormalMapSpace.Tangent )
				{
					curFuncInvocation = new FunctionInvocation( SGXFuncTranformNormal, groupOrder, internalCounter++ );
					curFuncInvocation.PushOperand( this.vsTBNMatrix, Operand.OpSemantic.In );
					curFuncInvocation.PushOperand( this.vsLocalDir, Operand.OpSemantic.In );
					curFuncInvocation.PushOperand( this.vsOutView, Operand.OpSemantic.Out );
					vsMain.AddAtomInstance( curFuncInvocation );
				}
					//output object space
				else if ( this.normalMapSpace == RTShaderSystem.NormalMapSpace.Object )
				{
					curFuncInvocation = new FunctionInvocation( FFPRenderState.FFPFuncAssign, groupOrder,
					                                            internalCounter++ );
					curFuncInvocation.PushOperand( this.vsLocalDir, Operand.OpSemantic.In );
					curFuncInvocation.PushOperand( this.vsOutView, Operand.OpSemantic.Out );
					vsMain.AddAtomInstance( curFuncInvocation );
				}
			}

			//Add per light functions
			for ( int i = 0; i < this.lightParamsList.Count; i++ )
			{
				if ( AddVSIlluminationInvocation( this.lightParamsList[ i ], vsMain, groupOrder, ref internalCounter ) ==
				     false )
				{
					return false;
				}
			}
			return true;
		}
		private bool AddPsInvocations( Function psMain, int groupOrder )
		{
			FunctionInvocation funcInvocation = null;
			int internalCounter = 0;
			funcInvocation = new FunctionInvocation( SGXFuncInstancedViewportsDiscardOutOfBounds, groupOrder, internalCounter++ );
			funcInvocation.PushOperand( this.psInMonitorsCount, Operand.OpSemantic.In );
			funcInvocation.PushOperand( this.psInMonitorIndex, Operand.OpSemantic.In );
			funcInvocation.PushOperand( this.psInPositionProjectiveSpace, Operand.OpSemantic.In );

			psMain.AddAtomInstance( funcInvocation );

			return true;
		}
		private bool AddVsInvocations( Function vsMain, int groupOrder )
		{
			FunctionInvocation funcInvocation = null;
			int internalCounter = 0;

			funcInvocation = new FunctionInvocation( SGXFuncInstancedViewportsTransform, groupOrder, internalCounter++ );
			funcInvocation.PushOperand( this.vsInPosition, Operand.OpSemantic.In );
			funcInvocation.PushOperand( this.worldViewMatrix, Operand.OpSemantic.In );
			funcInvocation.PushOperand( this.projectionMatrix, Operand.OpSemantic.In );
			funcInvocation.PushOperand( this.vsInViewportOffsetMatrixR0, Operand.OpSemantic.In );
			funcInvocation.PushOperand( this.vsInViewportOffsetMatrixR1, Operand.OpSemantic.In );
			funcInvocation.PushOperand( this.vsInViewportOffsetMatrixR2, Operand.OpSemantic.In );
			funcInvocation.PushOperand( this.vsInViewportOffsetMatrixR3, Operand.OpSemantic.In );
			funcInvocation.PushOperand( this.vsInMonitorsCount, Operand.OpSemantic.In );
			funcInvocation.PushOperand( this.vsInMonitorIndex, Operand.OpSemantic.In );
			funcInvocation.PushOperand( this.vsOriginalOutPositionProjectiveSpace, Operand.OpSemantic.Out );
			vsMain.AddAtomInstance( funcInvocation );

			//Output position in projective space
			funcInvocation = new FunctionInvocation( FFPRenderState.FFPFuncAssign, groupOrder, internalCounter++ );
			funcInvocation.PushOperand( this.vsOriginalOutPositionProjectiveSpace, Operand.OpSemantic.In );
			funcInvocation.PushOperand( this.vsOutPositionProjectiveSpace, Operand.OpSemantic.Out );
			vsMain.AddAtomInstance( funcInvocation );

			//Output monitor index
			funcInvocation = new FunctionInvocation( FFPRenderState.FFPFuncAssign, groupOrder, internalCounter++ );
			funcInvocation.PushOperand( this.vsInMonitorIndex, Operand.OpSemantic.In );
			funcInvocation.PushOperand( this.vsOutMonitorIndex, Operand.OpSemantic.Out );
			vsMain.AddAtomInstance( funcInvocation );

			return true;
		}
Beispiel #10
0
		private bool AddPsInvocations( Function psMain, int groupOrder )
		{
			FunctionInvocation funcInvocation = null;
			int internalCounter = 0;
			funcInvocation = new FunctionInvocation( SGXFuncApplyReflectionMap, groupOrder, internalCounter++ );
			funcInvocation.PushOperand( this.maskMapSampler, Operand.OpSemantic.In );
			funcInvocation.PushOperand( this.psInMaskTexcoord, Operand.OpSemantic.In );
			funcInvocation.PushOperand( this.reflectionMapSampler, Operand.OpSemantic.In );
			funcInvocation.PushOperand( this.psInReflectionTexcoord, Operand.OpSemantic.In );
			funcInvocation.PushOperand( this.psOutDiffuse, Operand.OpSemantic.In,
			                            (int)( Operand.OpMask.X | Operand.OpMask.Y | Operand.OpMask.Z ) );
			funcInvocation.PushOperand( this.reflectionPower, Operand.OpSemantic.In );
			funcInvocation.PushOperand( this.psOutDiffuse, Operand.OpSemantic.Out,
			                            (int)( Operand.OpMask.X | Operand.OpMask.Y | Operand.OpMask.Z ) );

			psMain.AddAtomInstance( funcInvocation );

			return true;
		}
Beispiel #11
0
		private bool AddVsInvocations( Function vsMain, int groupOrder )
		{
			FunctionInvocation funcInvocation = null;
			int internalCounter = 0;

			//Output mask texgture coordinates
			funcInvocation = new FunctionInvocation( FFPRenderState.FFPFuncAssign, groupOrder, internalCounter++ );
			funcInvocation.PushOperand( this.vsInMaskTexcoord, Operand.OpSemantic.In );
			funcInvocation.PushOperand( this.vsOutMaskTexcoord, Operand.OpSemantic.Out );
			vsMain.AddAtomInstance( funcInvocation );

			//Output reflection texture coordinates.
			if ( this.reflectionMapType == TextureType.TwoD )
			{
				funcInvocation = new FunctionInvocation( FFPRenderState.FFPFunGenerateTexcoordEnvSphere, groupOrder,
				                                         internalCounter++ );
				funcInvocation.PushOperand( this.worldITMatrix, Operand.OpSemantic.In );
				funcInvocation.PushOperand( this.viewMatrix, Operand.OpSemantic.In );
				funcInvocation.PushOperand( this.vsInputNormal, Operand.OpSemantic.In );
				funcInvocation.PushOperand( this.vsOutReflectionTexcoord, Operand.OpSemantic.Out );
				vsMain.AddAtomInstance( funcInvocation );
			}
			else
			{
				funcInvocation = new FunctionInvocation( FFPRenderState.FFPFuncGenerateTexCoordEnvReflect, groupOrder,
				                                         internalCounter++ );
				funcInvocation.PushOperand( this.worldMatrix, Operand.OpSemantic.In );
				funcInvocation.PushOperand( this.worldITMatrix, Operand.OpSemantic.In );
				funcInvocation.PushOperand( this.viewMatrix, Operand.OpSemantic.In );
				funcInvocation.PushOperand( this.vsInputNormal, Operand.OpSemantic.In );
				funcInvocation.PushOperand( this.vsInputPos, Operand.OpSemantic.In );
				funcInvocation.PushOperand( this.vsOutReflectionTexcoord, Operand.OpSemantic.Out );
				vsMain.AddAtomInstance( funcInvocation );
			}

			return true;
		}