现在的位置: 首页 > 综合 > 正文

Shader Compilation for Multiple Platforms

2012年03月23日 ⁄ 综合 ⁄ 共 2814字 ⁄ 字号 评论关闭
最近因为跨平台原因,考虑到shader的问题。

早就从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 "" "")

 

 

抱歉!评论已关闭.