Shader的真正编译过程

文件路径:

\Engine_Updating\Engine\Source\Runtime\Engine\Private\Materials\MaterialShared.cpp


/**
* Compiles this material for Platform, storing the result in OutShaderMap
*
* @param ShaderMapId - the set of static parameters to compile
* @param Platform - the platform to compile for
* @param OutShaderMap - the shader map to compile
* @return - true if compile succeeded or was not necessary (shader map for ShaderMapId was found and was complete)
*/
bool FMaterial::BeginCompileShaderMap(
	const FMaterialShaderMapId& ShaderMapId, 
	EShaderPlatform Platform, 
	TRefCountPtr& OutShaderMap, 
	bool bApplyCompletedShaderMapForRendering)
{
#if WITH_EDITORONLY_DATA
	bool bSuccess = false;

	STAT(double MaterialCompileTime = 0);

	TRefCountPtr NewShaderMap = new FMaterialShaderMap();

	SCOPE_SECONDS_COUNTER(MaterialCompileTime);

	// Generate the material shader code.
	FMaterialCompilationOutput NewCompilationOutput;
	FHLSLMaterialTranslator MaterialTranslator(this,NewCompilationOutput,ShaderMapId.ParameterSet,Platform,GetQualityLevel(),ShaderMapId.FeatureLevel);
	bSuccess = MaterialTranslator.Translate();

	if(bSuccess)
	{
		// Create a shader compiler environment for the material that will be shared by all jobs from this material
		TRefCountPtr MaterialEnvironment = new FShaderCompilerEnvironment();

		MaterialTranslator.GetMaterialEnvironment(Platform, *MaterialEnvironment);
		const FString MaterialShaderCode = MaterialTranslator.GetMaterialShaderCode();



最后两行,倒数第二行,设置环境变量,就是材质编辑器里面的一堆bool值,

倒数第一行,获得材质的HLSL代码。


所以要想在材质编辑器里面添加一个变量,就先在umaterial里面添加一个变量,然后fmaterial获取,添加到倒数第二个函数里


Engine_Updating\Engine\Shaders\MaterialTemplate.usf

然后在这个文件中添加特定的%s,,

最后一个函数添加push param ()才能返回正确的code,其实push就是把传入的字符串替换掉%s。





你可能感兴趣的