偶然一次查看RenderMonkey例子中的Particle System.rfx 的 FireParticleSystem 中發(fā)現(xiàn)了一種提高DX9渲染效率的設(shè)計(jì)方法
這里僅列出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,而不能進(jìn)行任何CPU方的算法設(shè)計(jì),因此要實(shí)現(xiàn)一個(gè)例子系統(tǒng),只能使用另外的方法,這個(gè)例子就是使用純Shader來(lái)實(shí)現(xiàn)了一個(gè)粒子系統(tǒng)的效果。
注意第31,32行中出現(xiàn)的Pos.z,這是本例子最有參考價(jià)值的地方。如果把Particles這個(gè)模型引用的QuadArray.3ds用MAX打開你就能發(fā)現(xiàn),這其實(shí)是一個(gè)多層疊出來(lái)的片, 每個(gè)片的間隔就是Pos.z。讓我們來(lái)整理下渲染出例子的整個(gè)流程:
由QuadArray.3ds提供Vertex數(shù)據(jù),也就是VertexBuffer.片狀的VB數(shù)據(jù)被送入管線,然后由上面的VertexShader程序,通過(guò)Pos.z將他們切開,控制這些片的頂點(diǎn)重塑例子的外觀。最后的PS只是簡(jiǎn)單的將光柵化后的像素點(diǎn)根據(jù)紋理采樣顯示出來(lái)。
2008年時(shí),我曾經(jīng)根據(jù)這個(gè)原理,設(shè)計(jì)了一套粒子系統(tǒng),原理與這個(gè)差不多,只不過(guò)VB是由Constant設(shè)置進(jìn)來(lái),在DX10/11以上就叫ConstantBuffer。測(cè)試了下,傳統(tǒng)的粒子系統(tǒng),在我的本子上大約只能跑60多幀,但是這個(gè)不鎖定VB的粒子系統(tǒng)卻可以跑300多幀。
最近決定使用這個(gè)技術(shù)優(yōu)化下我的引擎中繪制線段及片的系統(tǒng),以下是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 ];
}
這里有個(gè)細(xì)節(jié)需要注意。從最初分析看來(lái),多邊形的構(gòu)造都是由Constant輸入,并由VS代碼構(gòu)造,在VB中的數(shù)據(jù)好像只需要一個(gè)Index就夠了。但是實(shí)際測(cè)試下來(lái)發(fā)現(xiàn),這樣是錯(cuò)誤的,還是必須在頂點(diǎn)定義中添加繪制的基本元素,例如位置和紋理坐標(biāo)。
DX9因?yàn)椴婚_源,我們并不了解下面3種繪制方式的性能差異:
1. Constant發(fā)送
2. 鎖定VB發(fā)送
3. DrawPrimitiveUP系列使用系統(tǒng)內(nèi)建緩沖渲染
經(jīng)過(guò)測(cè)試發(fā)現(xiàn),DrawPrimitive在數(shù)據(jù)量小時(shí),比鎖定VB快些,而Constant發(fā)送方式?jīng)]有DrawPrimitiveUP快。
因此,使用Constant發(fā)送多邊形數(shù)據(jù)進(jìn)行構(gòu)造的方法在量小且固定的情況下對(duì)于性能提升是很有幫助的,但大量的頂點(diǎn)及變化的數(shù)據(jù)還是必須使用傳統(tǒng)的方法。