游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1525|回复: 0

render to target shader问题

[复制链接]

16

主题

59

帖子

59

积分

注册会员

Rank: 2

积分
59
发表于 2010-9-11 21:51:00 | 显示全部楼层 |阅读模式
在做粒子系统 想把粒子的位置,速度存到贴图里 这样就能在GPU运算了
但是现在好像写不到贴图里 虽然我已经设置rendertarget了
麻烦大家帮我看看 谢谢了

void MyParticleSystem::Render(){
        m_pDevice->SetRenderState(D3DRS_LIGHTING, false);
        m_pDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, true);
        m_pDevice->SetRenderState(D3DRS_POINTSCALEENABLE, true);
        m_pDevice->SetRenderState(D3DRS_POINTSIZE, d3d::FtoDw(50.0f));
        m_pDevice->SetRenderState(D3DRS_POINTSIZE_MIN, d3d::FtoDw(0.0f));

        // control the size of the particle relative to distance
        m_pDevice->SetRenderState(D3DRS_POINTSCALE_A, d3d::FtoDw(0.0f));
        m_pDevice->SetRenderState(D3DRS_POINTSCALE_B, d3d::FtoDw(0.0f));
        m_pDevice->SetRenderState(D3DRS_POINTSCALE_C, d3d::FtoDw(1.0f));

        // use alpha from texture
        m_pDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
        m_pDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);

        m_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
        m_pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
        m_pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

        mFX->SetTexture( m_hCurPosTexture, m_pCurPosTexture );
        mFX->SetTexture( m_hCurVelTexture, m_pCurVelTexture );

        mFX->SetTexture( m_hNewPosTexture, m_pNewPosTexture );
        mFX->SetTexture( m_hNewVelTexture, m_pNewVelTexture );

        if(m_bFirst){
                HRESULT hr = 0;

                mFX->SetTechnique(m_hTechRT);

                LPDIRECT3DSURFACE9 pOriginalRenderTarget;
                m_pDevice->GetRenderTarget( 0, &pOriginalRenderTarget );

                //Begin passes
                UINT numPasses=0;
                mFX->Begin(&numPasses,0);
                for(int i = 0; i < numPasses; i++){
                       
                        m_pDevice->SetVertexDeclaration(m_pDecl);
                        mFX->BeginPass(i);

                        if(i == 0){
                                hr = m_pDevice->SetRenderTarget(0,m_pNewPosSurf);
                                if(FAILED(hr))
                                {
                                        MessageBox(0, L"SetRenderTarget() - FAILED", L"MyParticleSystem", 0);
                                }
                        }else if(i == 1){
                                m_pDevice->SetRenderTarget(0,m_pNewVelSurf);
                        }
                        m_pDevice->SetStreamSource(0, m_pVB, 0, sizeof(PARTICLE));
                        m_pDevice->DrawPrimitive(D3DPT_POINTLIST, 0, MAX_PARTICLES);

                        mFX->EndPass();       
                }

                mFX->End();

                m_pDevice->SetRenderTarget( 0, pOriginalRenderTarget );
                SAFE_RELEASE( pOriginalRenderTarget );

                m_bFirst = false;
        }else{
                //Activate an Effect
                mFX->SetTechnique(m_hTechFlame);

                LPDIRECT3DSURFACE9 pOriginalRenderTarget;
                m_pDevice->GetRenderTarget( 0, &pOriginalRenderTarget );

                //Begin passes
                UINT numPasses=0;
                mFX->Begin(&numPasses,0);

                for(int i = 0; i < numPasses; i++){
                        m_pDevice->SetVertexDeclaration(m_pDecl);
                        mFX->BeginPass(i);
                        if(i == 0){
                                m_pDevice->SetTexture(0, m_pCurVelTexture);
                                m_pDevice->SetRenderTarget(0,m_pNewVelSurf);
                        }else if(i == 1){
                                m_pDevice->SetTexture(0, m_pCurPosTexture);
                                m_pDevice->SetTexture(1, m_pCurVelTexture);
                                m_pDevice->SetRenderTarget(0,m_pNewPosSurf);
                        }else if(i == 2){
                                m_pDevice->SetTexture(0, m_pTex);// m_pCurPosTexture m_pTex
                                m_pDevice->SetRenderTarget( 0, pOriginalRenderTarget );
                        }
                        /*mFX->BeginPass(2);
                        m_pDevice->SetVertexDeclaration(m_pDecl);
                        m_pDevice->SetTexture(0, m_pTex);*/
                        m_pDevice->SetStreamSource(0, m_pVB, 0, sizeof(PARTICLE));
                        m_pDevice->DrawPrimitive(D3DPT_POINTLIST, 0, MAX_PARTICLES);
                        mFX->EndPass();       
                }

                mFX->End();
                SAFE_RELEASE( pOriginalRenderTarget );
        }

        m_pDevice->SetRenderState(D3DRS_LIGHTING,          true);
        m_pDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, false);
        m_pDevice->SetRenderState(D3DRS_POINTSCALEENABLE,  false);
        m_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,  false);

        if(m_bDebug){
                m_pDevice->SetFVF(DEBUG::FVF);
                m_pDevice->SetTexture(0,m_pCurPosTexture);
                m_pDevice->SetStreamSource(0, m_pVBDebug, 0, sizeof(DEBUG));
                m_pDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

                m_pDevice->SetTexture(0,m_pNewPosTexture);
                m_pDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2);
        }
       
        SwapSurfacesAndTexture();
}
void MyParticleSystem::SwapSurfacesAndTexture(){
        LPDIRECT3DTEXTURE9 tempTex;
        LPDIRECT3DSURFACE9 tempSur;

        tempTex = m_pCurPosTexture;
        m_pCurPosTexture = m_pNewPosTexture;
        m_pNewPosTexture = tempTex;

        tempTex = m_pCurVelTexture;
        m_pCurVelTexture = m_pNewVelTexture;
        m_pNewVelTexture = tempTex;

        m_pCurPosTexture->GetSurfaceLevel( 0, &m_pCurPosSurf );
        m_pCurVelTexture->GetSurfaceLevel( 0, &m_pCurVelSurf );

        m_pNewPosTexture->GetSurfaceLevel( 0, &m_pNewPosSurf );
        m_pNewVelTexture->GetSurfaceLevel( 0, &m_pNewVelSurf );

        m_hCurPosTexture        = mFX ->GetParameterByName( NULL, "CurPositionTexture" );
        m_hCurVelTexture        = mFX ->GetParameterByName( NULL, "CurVelocityTexture" );
        m_hNewPosTexture        = mFX ->GetParameterByName( NULL, "NewPositionTexture" );
        m_hNewVelTexture        = mFX ->GetParameterByName( NULL, "NewVelocityTexture" );
}


//--------------------------------------------------------------//
// Flame Effect
//--------------------------------------------------------------//

uniform extern float4x4 mViewProjection;
uniform extern float4 fViewPosition;
uniform extern float timeDelta;
float fMass = 0.1;
float3 Gravity = float3(0.0,-9.8,0.0);
uniform extern float4 Wind;
texture CurPositionTexture;
texture CurVelocityTexture;
texture NewPositionTexture;
texture NewVelocityTexture;

//-----------------------------------------------------------------------------
// Texture samplers
//-----------------------------------------------------------------------------
sampler CurVelSampler =
sampler_state
{
    Texture = <CurVelocityTexture>;
    MinFilter = POINT;
    MagFilter = POINT;

    AddressU = Clamp;
    AddressV = Clamp;
};

sampler CurPosSampler =
sampler_state
{
    Texture = <CurPositionTexture>;
    MinFilter = POINT;
    MagFilter = POINT;

    AddressU = Clamp;
    AddressV = Clamp;
};

sampler NewVelSampler =
sampler_state
{
    Texture = <NewVelocityTexture>;
    MinFilter = POINT;
    MagFilter = POINT;

    AddressU = Clamp;
    AddressV = Clamp;
};

sampler NewPosSampler =
sampler_state
{
    Texture = <NewPositionTexture>;
    MinFilter = POINT;
    MagFilter = POINT;

    AddressU = Clamp;
    AddressV = Clamp;
};

struct VS_FS_RT_OUTPUT{
   float4 Pos:    POSITION;
   float3 Vel:    TEXCOORD2;
   float4 pos:          TEXCOORD1;
   float2 tex:    TEXCOORD0;
};

VS_FS_RT_OUTPUT VSFirstRenderToTarget(float3 Pos: POSITION, float3 Vel: NORMAL, float2 Tex: TEXCOORD0, float4 Col: COLOR){
   VS_FS_RT_OUTPUT Out = (VS_FS_RT_OUTPUT)0;
   
//   Out.Pos = mul(float4(Pos,1.0f),mViewProjection);
//   Out.Pos.xyz /= Out.Pos.w;
   Out.Pos.xyz = Pos;
   Out.Pos.w = 1.0;
   Out.Vel = Vel;
   Out.pos = Out.Pos;
   Out.tex = Tex;

   return Out;

}

float4 PSFirstRenderToTargetPos(float4 pos:        TEXCOORD1, float2 Tex: TEXCOORD0): COLOR{
   //float4 RGBColor = tex2D(CurPosSampler, Tex) ;
   return pos;
}

float4 PSFirstRenderToTargetVel(float3 Vel: TEXCOORD2, float2 Tex: TEXCOORD0): COLOR{
   //float4 RGBColor = tex2D(CurPosSampler, Tex) ;
   return float4(Vel,1.0f);
}

struct VS_UPDATE_OUTPUT{
   float4 Pos:    POSITION;
   float3 Vel:    TEXCOORD3;
   float4 pos:          TEXCOORD2;
   float2 texPos:    TEXCOORD0;
   float2 texVel:    TEXCOORD1;
};

VS_UPDATE_OUTPUT VSUpdate(float3 Pos: POSITION, float3 Vel: NORMAL, float2 Tex: TEXCOORD0, float4 Col: COLOR){
   VS_UPDATE_OUTPUT Out = (VS_UPDATE_OUTPUT)0;
   
  // Out.Pos = mul(float4(Pos,1.0f),mViewProjection);
  // Out.Pos.xyz /= Out.Pos.w;
  // Out.Pos.w = 1.0;
  // Out.Vel = Vel;
  // Out.pos = Out.Pos;
   Out.texPos = Tex;
   Out.texVel = Tex;

   return Out;

}

float4 PSUpdatePos(VS_UPDATE_OUTPUT In): COLOR{
   float3 Position = tex2Dlod(CurPosSampler, float4(In.texPos,0,0)).xyz;
   float3 Velocity = tex2Dlod(CurVelSampler, float4(In.texVel,0,0)).xyz;
   Position = Position + Velocity * timeDelta;
   //float4 temp = mul(float4(Position,1.0f),mViewProjection);
   //temp.xyz /= temp.w;
   //temp.w = 1.0;
   return float4(Position,1.0);
}

float4 PSUpdateVel(VS_UPDATE_OUTPUT In): COLOR{
   float3 Velocity = tex2Dlod(CurVelSampler, float4(In.texVel,0,0)).xyz;
   Velocity = Velocity +(Gravity + Wind.xyz)* timeDelta;
   return float4(Velocity,1);
}

struct VS_OUTPUT {
   float4 Pos:    POSITION;
   float4 Col:    COLOR;
   float2 tex: TEXCOORD0;
};

VS_OUTPUT VSmain(float3 Pos: POSITION, float3 Vel: NORMAL, float2 texCoord: TEXCOORD0, float4 Col: COLOR){
   VS_OUTPUT Out = (VS_OUTPUT)0;
   float3 Position = tex2Dlod(CurPosSampler, float4(texCoord,0,0)).xyz;
   //Out.Pos.xyz = Position;
   //Out.Pos.w = 1.0f;
   Out.Pos = mul(float4(Position,1.0f),mViewProjection);
   Out.Col = Col;
   Out.tex = texCoord;

   return Out;
}

float4 PSmain(float4 Pos: POSITION, float4 Col: COLOR, float2 tex: TEXCOORD0): COLOR{

   return Col;
}

//--------------------------------------------------------------//
// Technique Render Position And Velocity To Target
//--------------------------------------------------------------//
technique FirstRenderToTarget
{
    pass P0
    {
        VertexShader = compile vs_2_0 VSFirstRenderToTarget();
        PixelShader  = compile ps_2_0 PSFirstRenderToTargetPos();
    }
    pass P1
    {
        VertexShader = compile vs_2_0 VSFirstRenderToTarget();
        PixelShader  = compile ps_2_0 PSFirstRenderToTargetVel();
    }
}

//--------------------------------------------------------------//
// Technique Flame
//--------------------------------------------------------------//
technique FlameTech
{
        pass P0
    {
        VertexShader = compile vs_2_0 VSUpdate();
        PixelShader  = compile ps_3_0 PSUpdateVel();
    }
    pass P1
    {
        VertexShader = compile vs_2_0 VSUpdate();
        PixelShader  = compile ps_3_0 PSUpdatePos();
    }
    pass P2
    {
        VertexShader = compile vs_3_0 VSmain();
        PixelShader = compile ps_2_0 PSmain();
    }

}

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-6 14:22

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表