早就從hlsl改為cg,而且cg也對pc和mac支持很好,只不過有說法是cg runtime在ati上運行效果不佳,以及rumtime的臃腫。
那么依此觀點的話,編寫原生的glsl讓display driver/runtime來解析應該是最好的方法,但同時又不願意同時維護2個。
回顧之前,cg被opengl拒之門外而使用了3dlab的glsl,加上ms和nv一起搞的hlsl,這種人為的天然障礙真是我輩的悲劇。
而且cg的核心人員都陸續跳到ms,感覺nv總是有點3dfx的影子,很多時候都領先於業內推出自己的技術,像是cg,像是cuda,但卻始終達不到ms這樣的領導地位,自己的那一套總是成不了業內標准,像是cg於hlsl/glsl,像是cuda於opencl。
回到hlsl -> glsl的問題上,ati之前推過一個項目叫hlsl2glsl,后來被unity3d的人接手擴展為hlsl2glslfork,以lib的方式存在,而且很方便就可以通過幾個接口進行translate。
接下來的問題是這個支持只到sm3.0,對我目前來說是剛剛好。
只不過就像下面unity3d的人所說,在opengles上,driver對glsl的優化做得不好(我的判斷是因為opengles的runtim沒有做,另外移動平台不像pc上nv和ati兩家競爭強烈,所以很多優化已在driver中完成),所以又引入一個glsl-optimizer來負責做優化的工作。
以我之前在ATI的經驗,以hlsl為例,d3d runtime會負責做一次優化,傳入driver的byte code已經是像glsl-optimizer那樣,去掉了在臃腫的代碼,該inline的,去掉無用的變量/函數等等,但runtime畢竟還只是一個上層的中間件,面對不同的顯卡,只有自家driver才知道如何做“更具體的優化",於是driver會根據不同的顯卡走不同的path進一步做優化處理。而這一塊,目前在mobile platform上應該還是很混亂,導致需要app負責進行優化再傳入。
令人欣慰的是,ogre的做法是,pc/mac上依然使用cg來解決,在ogles中使用hlsl2glslfork + glsl-optimizer來完成cg -> glsl,這一塊已在es 2.x render system中實現,不過我的看法是,既然如此,考慮到向下兼容的問題,不如直接在1.8中將所有的shader以hlsl實現,然后ogl和ogles都使用translator完成豈不更原生?更有效率?
下面是一些相關資料
------------------------------------------------------------------------------
http://blogs.unity3d.com/2010/10/20/shader-compilation-for-multiple-platforms/
http://aras-p.info/blog/2010/09/29/glsl-optimizer/
OGRE
http://www.ogre3d.org/forums/viewtopic.php?f=21&t=62256
4 new changesets in ogre: http://bitbucket.org/sinbad/ogre/changeset/f38dbc3f6dd6/ changeset: r2617:f38dbc3f6dd6 user: assaframan date: 2011-01-03 20:24:46 summary: ES 2.x render system: Added basic Cg support. OGRE_CG_SUPPORT_FOR_GLES2 flag was added to control if this option is on. http://code.google.com/p/hlsl2glslfork/ is used to convert the Cg code to glsl. hlsl2glslfork code is required to compile for the Cg support to compile and link. Not all Cg programs compile or link. hlsl2glslfork CMake find script is not provided with this commit. affected #: 10 files (27.4 KB)
http://bitbucket.org/sinbad/ogre/changeset/0b40d4b9ece4/ changeset: r2697:0b40d4b9ece4 user: masterfalcon date: 2011-03-12 03:23:32 summary: GLES2: Add support for the GLSL Optimiser. See here for more info about it: https://github.com/aras-p/glsl-optimizer It is enabled by default but you can opt out by adding "use_optimiser false" to a program definition affected #: 14 files (13.1 KB) --- a/CMake/Dependencies.cmake Fri Mar 11 20:16:36 2011 -0600 +++ b/CMake/Dependencies.cmake Fri Mar 11 20:23:32 2011 -0600 @@ -173,6 +173,13 @@ find_package(TBB) macro_log_feature(TBB_FOUND "tbb" "Threading Building Blocks" "http://www.threadingbuildingblocks.org/" FALSE "" "") +# GLSL-Optimizer +find_package(GLSLOptimizer) +macro_log_feature(GLSL_Optimizer_FOUND "GLSL Optimizer" "GLSL Optimizer" "http://github.com/aras-p/glsl-optimizer/" FALSE "" "") + +# HLSL2GLSL +find_package(HLSL2GLSL) +macro_log_feature(HLSL2GLSL_FOUND "HLSL2GLSL" "HLSL2GLSL" "http://hlsl2glslfork.googlecode.com/" FALSE "" "")