• <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 知乎關注:http://zhihu.com/people/sunicdavy 開源項目:https://github.com/davyxu

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

            偶然一次查看RenderMonkey例子中的Particle System.rfx 的 FireParticleSystem 中發(fā)現(xiàn)了一種提高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方的算法設計,因此要實現(xiàn)一個例子系統(tǒng),只能使用另外的方法,這個例子就是使用純Shader來實現(xiàn)了一個粒子系統(tǒng)的效果。

            注意第31,32行中出現(xiàn)的Pos.z,這是本例子最有參考價值的地方。如果把Particles這個模型引用的QuadArray.3ds用MAX打開你就能發(fā)現(xiàn),這其實是一個多層疊出來的片, 每個片的間隔就是Pos.z。讓我們來整理下渲染出例子的整個流程:

            由QuadArray.3ds提供Vertex數(shù)據(jù),也就是VertexBuffer.片狀的VB數(shù)據(jù)被送入管線,然后由上面的VertexShader程序,通過Pos.z將他們切開,控制這些片的頂點重塑例子的外觀。最后的PS只是簡單的將光柵化后的像素點根據(jù)紋理采樣顯示出來。

            2008年時,我曾經(jīng)根據(jù)這個原理,設計了一套粒子系統(tǒng),原理與這個差不多,只不過VB是由Constant設置進來,在DX10/11以上就叫ConstantBuffer。測試了下,傳統(tǒng)的粒子系統(tǒng),在我的本子上大約只能跑60多幀,但是這個不鎖定VB的粒子系統(tǒng)卻可以跑300多幀。

            最近決定使用這個技術優(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 ];
            }

            這里有個細節(jié)需要注意。從最初分析看來,多邊形的構(gòu)造都是由Constant輸入,并由VS代碼構(gòu)造,在VB中的數(shù)據(jù)好像只需要一個Index就夠了。但是實際測試下來發(fā)現(xiàn),這樣是錯誤的,還是必須在頂點定義中添加繪制的基本元素,例如位置和紋理坐標。

            DX9因為不開源,我們并不了解下面3種繪制方式的性能差異:

            1. Constant發(fā)送

            2. 鎖定VB發(fā)送

            3. DrawPrimitiveUP系列使用系統(tǒng)內(nèi)建緩沖渲染

             

            經(jīng)過測試發(fā)現(xiàn),DrawPrimitive在數(shù)據(jù)量小時,比鎖定VB快些,而Constant發(fā)送方式?jīng)]有DrawPrimitiveUP快。

            因此,使用Constant發(fā)送多邊形數(shù)據(jù)進行構(gòu)造的方法在量小且固定的情況下對于性能提升是很有幫助的,但大量的頂點及變化的數(shù)據(jù)還是必須使用傳統(tǒng)的方法。

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

            評論

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

            久久精品成人欧美大片| 亚洲综合久久综合激情久久 | 一本大道久久东京热无码AV| 久久亚洲天堂| 色婷婷综合久久久久中文一区二区| 99久久er这里只有精品18| 久久久久国产精品嫩草影院| 精品久久久无码21p发布| 久久国产乱子伦精品免费强| 久久久国产亚洲精品| 久久久精品一区二区三区| 日本久久中文字幕| 久久成人影院精品777| 久久久国产视频| 欧美久久综合九色综合| 狠色狠色狠狠色综合久久| 伊人久久大香线蕉av不卡| 久久青草国产手机看片福利盒子| 久久人人爽人人爽人人爽 | 久久精品中文字幕第23页| 久久免费的精品国产V∧| 性做久久久久久免费观看| 亚洲国产成人久久综合一| 久久丫精品国产亚洲av不卡| 狠狠色丁香久久婷婷综合蜜芽五月| 老司机国内精品久久久久| 久久99国产综合精品女同| 伊人久久综合无码成人网| 中文成人无码精品久久久不卡 | 亚洲精品无码久久久久| 色偷偷88欧美精品久久久| 国产高潮久久免费观看| 久久r热这里有精品视频| 精品久久久久久亚洲| 国产精品久久自在自线观看| 久久国产精品成人片免费| 国内精品综合久久久40p| 一97日本道伊人久久综合影院| 色综合久久88色综合天天 | 久久午夜无码鲁丝片午夜精品| 久久国产午夜精品一区二区三区|