Cg runtime support "compile_arguments" with D3D/OpenGL. It's useful for implement shader generation by macro define like:
@generic.program
Code: Select all
vertex_program generic_vp cg
{
source generic.cg
profiles vs_2_0 arbvp1
entry_point generic_vp
compile_arguments -DSECOND_LIGHT=1
...
}
Code: Select all
void generic_vp
(
float4 iPosition : POSITION,
...
oLightDirection0.xyz = vLightPosition0.xyz - ( worldPosition.xyz * vLightPosition0.w );
float D0 = length( oLightDirection0.xyz );
oLightDirection0.w = 1.0 / dot( float3( 1.0, D0, D0 * D0 ), vLightAttenuation0.yzw );
#if SECOND_LIGHT
oLightDirection1.xyz = vLightPosition1.xyz - ( worldPosition.xyz * vLightPosition1.w );
float D1 = length( oLightDirection1.xyz );
oLightDirection1.w = 1.0 / dot( float3( 1.0, D1, D1 * D1 ), vLightAttenuation1.yzw );
#endif
...
}
Code: Select all
GLSLESCgProgram::GLSLESCgProgram(ResourceManager* creator,
const String& name, ResourceHandle handle,
const String& group, bool isManual, ManualResourceLoader* loader)
: GLSLESProgram(creator, name, handle, group, isManual, loader)
{
// Add parameter "entry_point" and "profiles" to the material serializer dictionary
if (createParamDictionary("GLSLESCgProgram"))
{
setupBaseParamDictionary();
ParamDictionary* dict = getParamDictionary();
dict->addParameter(ParameterDef("entry_point",
"The entry point for the Cg program.",
PT_STRING),&msCmdEntryPoint);
dict->addParameter(ParameterDef("profiles",
"Space-separated list of Cg profiles supported by this profile.",
PT_STRING),&msCmdProfiles);
[b]
dict->addParameter(ParameterDef("compile_arguments",
"A string of compilation arguments to pass to the Cg compiler.",
PT_STRING),&msCmdArgs);
[/b]
}
// Manually assign language now since we use it immediately
mSyntaxCode = "cg";
}
Code: Select all
[b]
String GLSLESCgProgram::resolveCgCompileArguments(const String& inSource)
{
String outSource;
// output will be at least this big
outSource.reserve(inSource.length());
StringVector::size_type n = 0, size = mCgArguments.size();
for (; n < size; ++n)
{
const String& arg = mCgArguments[n];
if (arg.find_first_of("-D") == 0)
{
StringVector macro = StringUtil::split(arg, "=");
if (macro.size() == 2)
{
macro[0] = macro[0].substr(2);
String define = String("#define ") + macro[0] + " " + macro[1] + "\n";
outSource.append(define);
}
}
}
outSource.append(inSource);
return outSource;
}
[/b]
Code: Select all
void GLSLESCgProgram::loadFromSource( void )
{
// check if syntax is supported
if (!isSyntaxSupported())
{
mSource = "";
LogManager::getSingleton().logMessage(
"File:" + mFilename +
" has unsupported syntax for hlsl2glsl.");
return;
}
// add a #define so we can control some cg code in shaders
mSource = "#define OPENGL_ES_2\n" + mSource;
[b]
// resolve compile arguments
buildArgs();
String sourceToUse = resolveCgCompileArguments(mSource);
[/b]
// resolve includes
sourceToUse = resolveCgIncludes(sourceToUse, this, mFilename);
...
Full code attached.