• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            戰(zhàn)魂小筑

            討論群:309800774 知乎關(guān)注:http://zhihu.com/people/sunicdavy 開源項(xiàng)目:https://github.com/davyxu

               :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              257 隨筆 :: 0 文章 :: 506 評(píng)論 :: 0 Trackbacks

            偶然一次查看RenderMonkey例子中的Particle System.rfx 的 FireParticleSystem 中發(fā)現(xiàn)了一種提高DX9渲染效率的設(shè)計(jì)方法

            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,而不能進(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)的方法。

            posted on 2010-07-27 14:46 戰(zhàn)魂小筑 閱讀(2240) 評(píng)論(1)  編輯 收藏 引用 所屬分類: 渲染 Shader 引擎

            評(píng)論

            # re: DirectX 9高效渲染之利用Constant構(gòu)建渲染數(shù)據(jù) 2012-10-24 22:46 王月
            Constant怎么發(fā)送?  回復(fù)  更多評(píng)論
              

            亚洲精品国产自在久久| 国产成人精品久久一区二区三区av | 青青草原精品99久久精品66 | 人人狠狠综合久久88成人| 中文字幕久久精品| 九九精品久久久久久噜噜| 久久久久噜噜噜亚洲熟女综合| 国产成人99久久亚洲综合精品| 精品国产乱码久久久久久浪潮| 久久精品二区| 狠狠色丁香久久婷婷综合| 久久午夜无码鲁丝片| 精品久久久久久综合日本| 久久91精品久久91综合| 国产精品gz久久久| 久久91精品国产91久| 色偷偷久久一区二区三区| 国产精品久久久久影视不卡| 99久久www免费人成精品| 日日狠狠久久偷偷色综合0| 精品国产青草久久久久福利| 精品久久777| 亚洲国产成人久久综合野外| 日日噜噜夜夜狠狠久久丁香五月| 狠狠干狠狠久久| 亚洲精品高清一二区久久| 高清免费久久午夜精品| 久久无码人妻精品一区二区三区| 无码专区久久综合久中文字幕| 伊人久久大香线蕉精品| 性高朝久久久久久久久久| 99久久国语露脸精品国产| 亚洲欧美一级久久精品| 97久久精品无码一区二区| 亚洲欧美一级久久精品| 四虎国产精品免费久久5151| 精品久久久久久国产| 久久成人永久免费播放| 久久99热国产这有精品| 亚洲精品无码久久一线| 亚洲国产精品综合久久网络|