Page 1 of 1

Added "compile_arguments" support for GLSLESCgProgram

Posted: Wed Mar 21, 2012 5:44 am
by oiram
Hi guys,
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
...
}
@generic.cg

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
...
}
However, GLSLESCgProgram does not support this, and I just add some code to do it.

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);
...
Hope it's useful. :)
Full code attached.

Re: Added "compile_arguments" support for GLSLESCgProgram

Posted: Wed Mar 21, 2012 7:21 am
by masterfalcon
Thanks! That's great. Would be able to add this to the patch tracker and fill out a contributor agreement? Patches can get lost on the forum sometimes.

Re: Added "compile_arguments" support for GLSLESCgProgram

Posted: Wed Mar 21, 2012 10:16 am
by oiram
I just submit a patch I think...
however I am first use hg to submit patch, so I have no idea how to do that, and I have read "http://www.ogre3d.org/developers/submit-patch" but I cannot find "Repository Explorer" and "Export Patches from here to selected…" in TortoiseHg, so I just click "commit" with diffs. :oops:

Re: Added "compile_arguments" support for GLSLESCgProgram

Posted: Wed Mar 21, 2012 3:03 pm
by masterfalcon
From the command line you just need to run(before committing) hg diff > patch.diff. Not sure what you can do now perhaps hg rollback, check status to make sure things are correct then make a diff.

Re: Added "compile_arguments" support for GLSLESCgProgram

Posted: Tue Mar 27, 2012 4:16 am
by oiram

Re: Added "compile_arguments" support for GLSLESCgProgram

Posted: Tue May 29, 2012 9:39 am
by oiram
Hi masterfalcon,
Did you see that patch what I attached? And I download the 1.8 release but does not see this change.
I think this change is most important thing for port Cg shader to OGl ES2.

Re: Added "compile_arguments" support for GLSLESCgProgram

Posted: Tue May 29, 2012 2:43 pm
by masterfalcon
Yes I did. It may make it in for 1.8.1.

Re: Added "compile_arguments" support for GLSLESCgProgram

Posted: Fri Dec 14, 2012 3:49 am
by oiram
Hi masterfalcon,
I did not see this in 1.8.1 and 1.9.
Is that in progress or ... ?

Thanks.

Re: Added "compile_arguments" support for GLSLESCgProgram

Posted: Fri Dec 14, 2012 6:42 am
by masterfalcon
Sorry about that, been busy. I'll probably put it in for 1.8 soon. Maybe this weekend.