[1.8]Cg in Ogre iOS

Discussion of issues specific to mobile platforms such as iOS, Android, Symbian and Meego.
d000hg
Goblin
Posts: 257
Joined: Tue Sep 02, 2008 9:41 pm
x 1

[1.8]Cg in Ogre iOS

Post by d000hg »

Building from CMake with the defaults, I don't see anything about Cg. I note the CMake flag OGRE_CONFIG_ENABLE_GLES2_CG_SUPPORT, but generating with that checked didn't create CgProgramManager which is what I thought would happen.

Can someone explain how Cg fits in with iOS/GLES2, and what changes that CMake flag actually does?

edit: I now finished rebuilding with OGRE_CONFIG_ENABLE_GLES2_CG_SUPPORT and now SampleBrowser doesn't display anything, I get lots of errors like
23:57:51: File: failed to convert from Cg to glsl with the following errors:
ERROR: 0:60: 'float2x4' : syntax error syntax error
ERROR: 1 compilation errors. No code generated.
23:57:50: OGRE EXCEPTION(2:InvalidParametersException): Parameter called worldDualQuaternion2x4Array does not exist. in GpuProgramParameters::_findNamedConstantDefinition at /usr/local/ogre/ogre1.8/src/OgreMain/src/OgreGpuProgramParams.cpp (line 1449)
23:57:50: Compiler error: invalid parameters in DualQuaternion.program(118): setting of constant failed
23:57:50: OGRE EXCEPTION(2:InvalidParametersException): Parameter called viewProjectionMatrix does not exist. in GpuProgramParameters::_findNamedConstantDefinition at /usr/local/ogre/ogre1.8/src/OgreMain/src/OgreGpuProgramParams.cpp (line 1449)
23:57:50: Compiler error: invalid parameters in DualQuaternion.program(119): setting of constant failed
23:57:50: File:DeferredShading/post/LightMaterial_vs.cg has unsupported syntax for hlsl2glsl.
What is wrong and is this expected? Especially the last one, which talks about hlsl2glsl?

I only ran on the simulator.
User avatar
masterfalcon
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126

Re: [1.8]Cg in Ogre iOS

Post by masterfalcon »

That support has not been extensively tested. And hasnt been looked at for quite some time so it may be broken. But yes, it should create the cg program manager, the output of which is then run through hlsl2glsl.
d000hg
Goblin
Posts: 257
Joined: Tue Sep 02, 2008 9:41 pm
x 1

Re: [1.8]Cg in Ogre iOS

Post by d000hg »

So Plugin_CgProgramManager should be a separate project (or whatever it's called in XCcode) alongside Plugin_OctreeSceneManager et al, and I should end up with libPlugin_CgProgramManager.a in my /lib/$CONFIG dir?
I don't have either which is probably explaining why it's not working! I will do a clean run tomorrow to confirm, but if anyone else could check that'd be great.

On this topic - what HL shaders are supported reliably in GLES2? It seems ES has it's own special language rather than using GLSL? So are HLSL/GLSL safe and only Cg is untested? And if 1.8 is at Release Candidate stage, shouldn't everything be tested? :wink:
User avatar
masterfalcon
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126

Re: [1.8]Cg in Ogre iOS

Post by masterfalcon »

It uses GLSL ES, which is a subset of GLSL. But there aren't that many differences.

The HLSL support was added by Assaf as kind of an experiment, it's mostly functional but the results can be slow and sometimes buggy. Which is why it's not enabled by default. Most people choose to just rewrite their shaders anyways.

If there was enough interest(or if you want to do it yourself) we could revisit this and fix it up but in its current state is is going to remain as an advanced feature disabled by default.
d000hg
Goblin
Posts: 257
Joined: Tue Sep 02, 2008 9:41 pm
x 1

Re: [1.8]Cg in Ogre iOS

Post by d000hg »

So Cg is not actually supported on iOS, only via re-interpreting to native shaders? My Googling for "Cg and iOS" left me a little unclear on that but it sounds there is no native support or standard conversion tool.

I wouldn't know where to start doing this myself, but I do note my strong interest/support in this feature :)
I can only barely write Cg as a non-shader guy and learning another shader language just to port my app to iOS is a big pain when I just want to get my existing code running for tests!
d000hg
Goblin
Posts: 257
Joined: Tue Sep 02, 2008 9:41 pm
x 1

Re: [1.8]Cg in Ogre iOS

Post by d000hg »

masterfalcon wrote: The HLSL support was added by Assaf as kind of an experiment, it's mostly functional but the results can be slow and sometimes buggy. Which is why it's not enabled by default. Most people choose to just rewrite their shaders anyways.
I've done a complete clean build dir with CMake and I confirm I do NOT get a CgProgramManager. At all - even the headers don't get copied into my build dir. So something seems screwy, I cannot even get as far as testing how slow and buggy it is :)

Any advice how to sort this out, it sounds like a CMake script issue?
User avatar
masterfalcon
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126

Re: [1.8]Cg in Ogre iOS

Post by masterfalcon »

Sounds like it probably is.
d000hg
Goblin
Posts: 257
Joined: Tue Sep 02, 2008 9:41 pm
x 1

Re: [1.8]Cg in Ogre iOS

Post by d000hg »

I don't know CMake but looking in ConfigureBuild.cmake and FeatureSummary.cmake (which seem to be key files), It appears the flag OGRE_BUILD_PLUGIN_CG needs to be set... I don't even see this listed in CMAKE GUI for iOS platform though it is for Mac/Win. Is this a simple omission or a deeper bug... perhaps OGRE_CONFIG_ENABLE_GLES2_CG_SUPPORT should auto-set OGRE_BUILD_PLUGIN_CG?

I will look more into scripts but I wonder if selecting OGRE_BUILD_PLATFORM_APPLE_IOS deliberately removes OGRE_BUILD_PLUGIN_CG, or if it's an accident.
CABAListic
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 2903
Joined: Thu Jan 18, 2007 2:48 pm
x 58

Re: [1.8]Cg in Ogre iOS

Post by CABAListic »

I don't know about iOS builds, but the option is only presented if the Cg libs and headers were found. So make sure that Cg is actually listed as a found dependency in the CMake output.
d000hg
Goblin
Posts: 257
Joined: Tue Sep 02, 2008 9:41 pm
x 1

Re: [1.8]Cg in Ogre iOS

Post by d000hg »

Yeah I have:

Code: Select all

Cg_INCLUDE_DIR:PATH=/Library/Frameworks/Cg.framework/Headers
If I uncheck the iOS platform checkbox in CMake, the Cg plugin option appears again alongside plugin-bsp, etc. So my first question is why choosing iOS platform makes the option disappear from CMake... I guess I'll just do a search-files for OGRE_BUILD_PLUGIN_CG and find the references!

However please see http://www.ogre3d.org/forums/viewtopic.php?f=2&t=69851 - I have Cg framework but get issues on Mac builds. Maybe it's related, I don't know. When I build Mac using CMake, I certainly get CgProgramManager project so it's not consistent between iOS/Mac.
User avatar
masterfalcon
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126

Re: [1.8]Cg in Ogre iOS

Post by masterfalcon »

Actually, if I remember correctly, the CGProgramManager isn't used for Cg on iOS. They are read in and translated using hlsl2glsl and treated as glsl es programs.
d000hg
Goblin
Posts: 257
Joined: Tue Sep 02, 2008 9:41 pm
x 1

Re: [1.8]Cg in Ogre iOS

Post by d000hg »

I suppose that would make sense. Now we're back to it apparently failing on every single .cg script in the SampleBrowser. I don't know if that suggests a systematic problem in the converter.

I probably need to get a skeleton app running with no shaders and then see what happens.

Does the run-time shader system automatically generate GLES shaders or is there a chance it might be trying to create Cg shaders when the Cg flag is set?
User avatar
masterfalcon
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126

Re: [1.8]Cg in Ogre iOS

Post by masterfalcon »

It will use Cg. I just tried building it here and I see mainly errors where 2x4 matrices are being generated by the RTSS, which then makes them fail conversion.
d000hg
Goblin
Posts: 257
Joined: Tue Sep 02, 2008 9:41 pm
x 1

Re: [1.8]Cg in Ogre iOS

Post by d000hg »

That sounds very familiar. Also in the error log I saw, lots of errors didn't show a shader file and this explains why - they were run-time generated.

All other bugs aside, it would be useful if I could easily tell RTSS to use the native shader language regardless if Cg is enabled. I don't know if that's a request we can put for future development... telling RTSS what language to use... but can anyone suggest how I can do this for my own projects, or at least what #define I should be looking for in RTSS??
d000hg
Goblin
Posts: 257
Joined: Tue Sep 02, 2008 9:41 pm
x 1

Re: [1.8]Cg in Ogre iOS

Post by d000hg »

Aha, in Ogre::RTShader::ShaderGenerator::ShaderGenerator():

Code: Select all

	HighLevelGpuProgramManager& hmgr = HighLevelGpuProgramManager::getSingleton();

	if (hmgr.isLanguageSupported("cg"))
	{
		mShaderLanguage	= "cg";
	}
	else if (hmgr.isLanguageSupported("glsl"))
	{
		mShaderLanguage	= "glsl";
	}
	else if (hmgr.isLanguageSupported("glsles"))
	{
		mShaderLanguage	= "glsles";
	}
	else if (hmgr.isLanguageSupported("hlsl"))
	{
		mShaderLanguage	= "hlsl";
	}
	else
	{
In iOS, is GLSL also interpreted to GLSLES or is it a native language? It would seem to me that RTSS should prefer native shader languages, and therefore attempt in the order "glsles", "glsl", "hlsl", "cg". But I know virtually nothing about this stuff, is that sensible? Should we at least push "cg" further down the list?

edit: seemed to work, raised as a bug here http://www.ogre3d.org/forums/viewtopic.php?f=22&t=70032
oiram
Halfling
Posts: 87
Joined: Sun Dec 13, 2009 5:06 am
x 6

Re: [1.8]Cg in Ogre iOS

Post by oiram »

Hi guys,
I use Cg shader language and tested on PowerVR simulator with windows xp, few steps to make it works:
1. download and compile hlsl2glslfork and glsl-optimizer.
2. copy glsl_optimizer.h and hlsl2glsl.h into "Dependencies\include", copy mesaglsl2.lib and hlsl2glsl.lib into "Dependencies\lib\Debug" and "Dependencies\lib\Release".
3. copy GL ES2 include/lib from PoverVR sdk to Dependencies directory if you want to test app on windows platform.
4. run cmake for OGRE and make sure it found HLSL2GLSL and GLSLOptimizer.
5. modify "#define OGRE_NO_GLES2_CG_SUPPORT 0" and "#define OGRE_NO_GLES2_GLSL_OPTIMISER 0" in OgreBuildSettings.h.
6. compile OGRE with vc.
7. CgProgramManager is unused for iOS.
8. remove float2x4 and float3x4 in RTShaderSystem because only square matrices are supported in GL ES 2, and remember do not use float3x4 for hardware skinning.
9. Added "compile_arguments" support for GLSLESCgProgram on this thread: http://www.ogre3d.org/forums/viewtopic.php?f=21&t=69437.

That's all about it and hope it's helpful. :D
d000hg
Goblin
Posts: 257
Joined: Tue Sep 02, 2008 9:41 pm
x 1

Re: [1.8]Cg in Ogre iOS

Post by d000hg »

oiram wrote:8. remove float2x4 and float3x4 in RTShaderSystem because only square matrices are supported in GL ES 2, and remember do not use float3x4 for hardware skinning.
How did you do this part?
oiram
Halfling
Posts: 87
Joined: Sun Dec 13, 2009 5:06 am
x 6

Re: [1.8]Cg in Ogre iOS

Post by oiram »

see this:
http://www.ogre3d.org/forums/viewtopic.php?f=21&t=69192

1. Just commit non-square matrices at this time, and it works most cases.
2. Use float4x4 for hardware skinning.
d000hg
Goblin
Posts: 257
Joined: Tue Sep 02, 2008 9:41 pm
x 1

Re: [1.8]Cg in Ogre iOS

Post by d000hg »

Did any of this stuff ever make it into a patch or Ogre itself? Is it feasible it could... I'm unclear what hlsl2glslfork is needed for but if there's a way to get Cg working on GLES2 it would be a great boon. Even if you end up writing custom shaders eventually, being able to run existing ones for prototyping and so on would be great.
User avatar
masterfalcon
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126

Re: [1.8]Cg in Ogre iOS

Post by masterfalcon »

Some of it may have but I don't remember. I think there's a patch that i haven't applied yet but it'll probably go into 1.9.

hlsl2glsl is needed to convert the Cg shaders to GLSL so they can be used. You see, there's no native support for Cg in GL ES 2 but we can translate them at runtime.
d000hg
Goblin
Posts: 257
Joined: Tue Sep 02, 2008 9:41 pm
x 1

Re: [1.8]Cg in Ogre iOS

Post by d000hg »

Oh yes I realise about hlsl2glsl but why specifically did the other poster get the forked version?

If there's a patch floating around I'd love to try it out; I'm actively developing on iOS right now and happy to give feedback on it.