Node Based Shader系统基本完成,进入调试和优化阶段
Posted 潘李亮
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Node Based Shader系统基本完成,进入调试和优化阶段相关的知识,希望对你有一定的参考价值。
Shader 系统的代码生成器基本完成。已经能组合Node来生成一个完整的shader代码
现在可以用如下的语句
xGpuProgNameParser _shaderNameParser;
_shaderNameParser.setShaderName(eShaderType::eShader_VertexShader , L"YV12Video<0:YV12ToRGBA,RGBAGray;>");
xShaderCodeNodeMgr _shaderManager(L"ShaderManager");
_shaderManager.addPath(_ABSPATH(L"./shader/"));
ds_string _code;
_shaderNameParser.generateCode(eShader_VertexShader , &_shaderManager,_code);
或者:
xGpuProgNameParser _shaderNameParser;
xGpuProgramName _shaderName;
_shaderNameParser.setShaderName(eShader_VertexShader , L"simple.vertex" );
_shaderNameParser.setShaderName(eShader_PixelShader , L"simple.pixel" );
_shaderNameParser.addShaderNode(eShader_PixelShader , L"simple.mask" );
_shaderNameParser.addShaderNode(eShader_PixelShader , L"simple.fakehdr" );
_shaderNameParser.addShaderNode(eShader_PixelShader , L"RGBAGray" );
_shaderNameParser.toName(_shaderName);
HGpuProgram hProgram = m_pRenderApi->gpuProgramManager()->load( _shaderName);
IGpuProgram* pGpuProgram = hProgram.getResource() ;
生成如下代码:
=======生成的代码==================
//视频处理的PS
sampler2D YTextue;
sampler2D UTextue;
sampler2D VTextue;
float cTime;
struct PS_INPUT
{
float2 Texcoord : TEXCOORD0;
};
struct PS_OUTPUT
{
float4 rgbColor : COLOR0;
};
struct CS_YUV2RGBRet
{
float4 color;
};
CS_YUV2RGBRet CS_YUV2RGB(float3 yuvColorIn);
struct CM_GrayRet
{
float4 color;
};
CM_GrayRet CM_Gray(float3 ColorIn);
CS_YUV2RGBRet CS_YUV2RGB(float3 yuvColorIn)
{
//==ColorSpace======================================
float3 delYuv = float3(-16.0/255.0 , -128.0/255.0 , -128.0/255.0);
float3 yuvColor = yuvColorIn.xyz + delYuv;
float3 matYUVRGB1 = float3(1.164, 2.018 , 0.0 );
float3 matYUVRGB2 = float3(1.164, -0.391 , -0.813 );
float3 matYUVRGB3 = float3(1.164, 0.0 , 1.596 );
float4 rgbColor;
rgbColor.x = dot(yuvColor,matYUVRGB1);
rgbColor.y = dot(yuvColor,matYUVRGB2);
rgbColor.z = dot(yuvColor,matYUVRGB3);
rgbColor.w = 1.0f;
//==================================================
CS_YUV2RGBRet ret;
ret.color = rgbColor;
return ret;
}
CM_GrayRet CM_Gray(float3 ColorIn)
{
CM_GrayRet ret;
ret.color = ColorIn.xxx;
return ret;
}
PS_OUTPUT main( PS_INPUT Input )
{
//===采样输入器=====================================
float4 yuvColor;
yuvColor.x = tex2D( YTextue, Input.Texcoord ).x;
yuvColor.y = tex2D( UTextue, Input.Texcoord ).x;
yuvColor.z = tex2D( VTextue, Input.Texcoord ).x;
yuvColor.w = 1.0f;
PS_OUTPUT RetVal ;
//==================================================
//应用修改器 name=YV12ToRGBA
CS_YUV2RGBRet Node_0_Ret = CS_YUV2RGB(float3(yuvColor.xyz));
//应用修改器 name=RGBAGray
CM_GrayRet Node_1_Ret = CM_Gray(float3(Node_0_Ret.color.xyz));
RetVal.rgbColor = Node_1_Ret.color;
//==================================================
return RetVal;
}
//以下为原始的节点文件,该方法能有效的防止Shader代码的爆炸。
YV12Video.hlsl
///{{Declaration
//视频处理的PS
sampler2D YTextue;
sampler2D UTextue;
sampler2D VTextue;
float cTime;
struct PS_INPUT
{
float2 Texcoord : TEXCOORD0;
};
struct PS_OUTPUT
{
float4 rgbColor : COLOR0;
};
///}}
/*
///{{ParamTable //Shader的参数描述表
texture YTexture : <file="" , manager="" , width="" , height="" /> ;
float cTime : TIME ;
///}}
*/
///{{SourceCode
PS_OUTPUT main( PS_INPUT Input )
{
//===采样输入器=====================================
float4 yuvColor;
yuvColor.x = tex2D( YTextue, Input.Texcoord ).x;
yuvColor.y = tex2D( UTextue, Input.Texcoord ).x;
yuvColor.z = tex2D( VTextue, Input.Texcoord ).x;
yuvColor.w = 1.0f;
PS_OUTPUT RetVal ;
//==================================================
///}}
/*
///{{Socket = ColorModify // Socket = 名字, 在这里定义一个插入点
Input
{
float4 yuvColor : YUVCOLOR;
}
Output var = RetVal
{
float4 rgbColor : COLOR0;
}
///}}
*/
///{{DefaultCode = ColorModify
//默认的代码 ,如果Name指定的插槽没有插入Node。则该代码才会被编译进去
RetVal.rgbColor = float4(yuvColor,1.0);
///}}
///{{SourceCode
//==================================================
return RetVal;
}
///}}
YV12ToRGBA.hlsl
/*
///{{Slot = CS_YUV2RGB
Input
{
float3 yuvColorIn : YUVCOLOR; // 类型 名字 : 语义
}
Output class = CS_YUV2RGBRet
{
float4 color: COLOR0;
}
///}}
*/
///{{Declaration
struct CS_YUV2RGBRet
{
float4 color;
};
CS_YUV2RGBRet CS_YUV2RGB(float3 yuvColorIn);
///}}
///{{SourceCode
CS_YUV2RGBRet CS_YUV2RGB(float3 yuvColorIn)
{
//==ColorSpace======================================
float3 delYuv = float3(-16.0/255.0 , -128.0/255.0 , -128.0/255.0);
float3 yuvColor = yuvColorIn.xyz + delYuv;
float3 matYUVRGB1 = float3(1.164, 2.018 , 0.0 );
float3 matYUVRGB2 = float3(1.164, -0.391 , -0.813 );
float3 matYUVRGB3 = float3(1.164, 0.0 , 1.596 );
float4 rgbColor;
rgbColor.x = dot(yuvColor,matYUVRGB1);
rgbColor.y = dot(yuvColor,matYUVRGB2);
rgbColor.z = dot(yuvColor,matYUVRGB3);
rgbColor.w = 1.0f;
//==================================================
CS_YUV2RGBRet ret;
ret.color = rgbColor;
return ret;
}
///}}
RGBAGray.hlsl
/*
///{{Slot = CM_Gray
Input
{
float3 ColorIn : COLOR0; // 类型 名字 : 语义
}
Output class = CM_GrayRet
{
float4 color: COLOR0;
}
///}}
*/
///{{Declaration
struct CM_GrayRet
{
float4 color;
};
CM_GrayRet CM_Gray(float3 ColorIn);
///}}
///{{SourceCode
CM_GrayRet CM_Gray(float3 ColorIn)
{
CM_GrayRet ret;
ret.color = ColorIn.xxx;
return ret;
}
///}}
以上是关于Node Based Shader系统基本完成,进入调试和优化阶段的主要内容,如果未能解决你的问题,请参考以下文章
Physically Based Shader Development for Unity 2017 Develop Custom Lighting Systems