战魂小筑

讨论群:309800774 知乎关注:http://zhihu.com/people/sunicdavy 开源项目:https://github.com/davyxu

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  257 随笔 :: 0 文章 :: 506 评论 :: 0 Trackbacks

偶然一次查看RenderMonkey例子中的Particle System.rfx 的 FireParticleSystem 中发现了一种提高DX9渲染效率的设计方法

image

这里仅列出Vertex Shader参考:

 

   1:  float4x4 view_proj_matrix: register(c0);
   2:  float4x4 view_matrix: register(c4);
   3:  float time_0_X: register(c8);
   4:  float4 particleSystemPosition: register(c9);
   5:  float particleSystemShape: register(c10);
   6:  float particleSpread: register(c11);
   7:  float particleSpeed: register(c12);
   8:  float particleSystemHeight: register(c13);
   9:  float particleSize: register(c14);
  10:  // The model for the particle system consists of a hundred quads.
  11:  // These quads are simple (-1,-1) to (1,1) quads where each quad
  12:  // has a z ranging from 0 to 1. The z will be used to differenciate
  13:  // between different particles
  14:   
  15:  struct VS_OUTPUT {
  16:     float4 Pos: POSITION;
  17:     float2 texCoord: TEXCOORD0;
  18:     float color: TEXCOORD1;
  19:  };
  20:   
  21:  VS_OUTPUT main(float4 Pos: POSITION){
  22:     VS_OUTPUT Out;
  23:   
  24:     // Loop particles
  25:     float t = frac(Pos.z + particleSpeed * time_0_X);
  26:     // Determine the shape of the system
  27:     float s = pow(t, particleSystemShape);
  28:   
  29:     float3 pos;
  30:     // Spread particles in a semi-random fashion
  31:     pos.x = particleSpread * s * cos(62 * Pos.z);
  32:     pos.z = particleSpread * s * sin(163 * Pos.z);
  33:     // Particles goes up
  34:     pos.y = particleSystemHeight * t;
  35:   
  36:     // Billboard the quads.
  37:     // The view matrix gives us our right and up vectors.
  38:     pos += particleSize * (Pos.x * view_matrix[0] + Pos.y * view_matrix[1]);
  39:     // And put the system into place
  40:     pos += particleSystemPosition;
  41:   
  42:     Out.Pos = mul(view_proj_matrix, float4(pos, 1));
  43:     Out.texCoord = Pos.xy;
  44:     Out.color = 1 - t;
  45:   
  46:     return Out;
  47:  }

由于RenderMonkey本身只能使用Shader,而不能进行任何CPU方的算法设计,因此要实现一个例子系统,只能使用另外的方法,这个例子就是使用纯Shader来实现了一个粒子系统的效果。

注意第31,32行中出现的Pos.z,这是本例子最有参考价值的地方。如果把Particles这个模型引用的QuadArray.3ds用MAX打开你就能发现,这其实是一个多层叠出来的片, 每个片的间隔就是Pos.z。让我们来整理下渲染出例子的整个流程:

由QuadArray.3ds提供Vertex数据,也就是VertexBuffer.片状的VB数据被送入管线,然后由上面的VertexShader程序,通过Pos.z将他们切开,控制这些片的顶点重塑例子的外观。最后的PS只是简单的将光栅化后的像素点根据纹理采样显示出来。

2008年时,我曾经根据这个原理,设计了一套粒子系统,原理与这个差不多,只不过VB是由Constant设置进来,在DX10/11以上就叫ConstantBuffer。测试了下,传统的粒子系统,在我的本子上大约只能跑60多帧,但是这个不锁定VB的粒子系统却可以跑300多帧。

最近决定使用这个技术优化下我的引擎中绘制线段及片的系统,以下是VertexShader的代码:

#define VERTEX_COUNT 80
 
float4 PositionBuffer[VERTEX_COUNT];
float2 UVBuffer[VERTEX_COUNT];
float4 ColorBuffer[VERTEX_COUNT];
 
float4x4 Transform;
 
void Main(
    in float InIndex : TEXCOORD0,
    out float4 OutPosition : POSITION,
    out float2 OutTexCoord : TEXCOORD1,
    out float4 OutColor : COLOR0
    )
{
    OutPosition = mul( PositionBuffer[ InIndex ] , Transform );
    OutColor = ColorBuffer[ InIndex ];
    OutTexCoord = UVBuffer[ InIndex ];
}

这里有个细节需要注意。从最初分析看来,多边形的构造都是由Constant输入,并由VS代码构造,在VB中的数据好像只需要一个Index就够了。但是实际测试下来发现,这样是错误的,还是必须在顶点定义中添加绘制的基本元素,例如位置和纹理坐标。

DX9因为不开源,我们并不了解下面3种绘制方式的性能差异:

1. Constant发送

2. 锁定VB发送

3. DrawPrimitiveUP系列使用系统内建缓冲渲染

 

经过测试发现,DrawPrimitive在数据量小时,比锁定VB快些,而Constant发送方式没有DrawPrimitiveUP快。

因此,使用Constant发送多边形数据进行构造的方法在量小且固定的情况下对于性能提升是很有帮助的,但大量的顶点及变化的数据还是必须使用传统的方法。

posted on 2010-07-27 14:46 战魂小筑 阅读(2196) 评论(1)  编辑 收藏 引用 所属分类: 渲染 Shader 引擎

评论

# re: DirectX 9高效渲染之利用Constant构建渲染数据 2012-10-24 22:46 王月
Constant怎么发送?  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理