Added "compile_arguments" support for GLSLESCgProgram

Discussion of issues specific to mobile platforms such as iOS, Android, Symbian and Meego.
Post Reply
oiram
Halfling
Posts: 87
Joined: Sun Dec 13, 2009 5:06 am
x 6

Added "compile_arguments" support for GLSLESCgProgram

Post 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.
Attachments
OgreGLSLESCgProgram.zip
(6.31 KiB) Downloaded 144 times
User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126
Contact:

Re: Added "compile_arguments" support for GLSLESCgProgram

Post 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.
oiram
Halfling
Posts: 87
Joined: Sun Dec 13, 2009 5:06 am
x 6

Re: Added "compile_arguments" support for GLSLESCgProgram

Post 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:
User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126
Contact:

Re: Added "compile_arguments" support for GLSLESCgProgram

Post 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.
oiram
Halfling
Posts: 87
Joined: Sun Dec 13, 2009 5:06 am
x 6

Re: Added "compile_arguments" support for GLSLESCgProgram

Post by oiram »

oiram
Halfling
Posts: 87
Joined: Sun Dec 13, 2009 5:06 am
x 6

Re: Added "compile_arguments" support for GLSLESCgProgram

Post 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.
User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126
Contact:

Re: Added "compile_arguments" support for GLSLESCgProgram

Post by masterfalcon »

Yes I did. It may make it in for 1.8.1.
oiram
Halfling
Posts: 87
Joined: Sun Dec 13, 2009 5:06 am
x 6

Re: Added "compile_arguments" support for GLSLESCgProgram

Post by oiram »

Hi masterfalcon,
I did not see this in 1.8.1 and 1.9.
Is that in progress or ... ?

Thanks.
Last edited by oiram on Fri Dec 14, 2012 7:48 am, edited 1 time in total.
User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126
Contact:

Re: Added "compile_arguments" support for GLSLESCgProgram

Post by masterfalcon »

Sorry about that, been busy. I'll probably put it in for 1.8 soon. Maybe this weekend.
Post Reply