direct3d alphablend和像素着色器

时间:2012-01-23 13:13:46

标签: direct3d alphablending pixel-shader

我有一个2D场景,2个纹理设置为渲染目标,还有一个顶点缓冲区。

第一个纹理被对比度/亮度像素着色器滤除,第二个用作菜单,我不希望由同一个着色器处理,但希望它与第一个进行alpha混合。

一切都运行良好,直到我达到亮度/对比度的极端值,当改变第二个时看起来像淡出效果,并且在非常极端的情况下停止重绘,尽管数据总是被送到第一个。

禁用AlphaBlendnable可以纠正它,但是我松散了alpha混合...在应用像素着色器之前清除缓冲区的任何方法?任何帮助表示赞赏。

像素着色器代码:

float offsetBrightness = 0.0f; \
float offsetContrast   = 0.0f; \
float4 PSBrightnessContrast(float2 inCoord : TEXCOORD0) : COLOR0\
{\
    float4 color = tex2D( screen, inCoord.xy); \
    color = color + offsetBrightness; \
    color = color * (1.0 + offsetContrast); \
    return saturate( color ); \
}\

渲染代码

// Begin the scene
    hr = IDirect3DDevice9_BeginScene(d3ddev);
  if (FAILED(hr)) 
    throw FatalException( DXGetErrorDescription(hr), _T(__FILE__), __LINE__ );

// Render the vertex buffer contents
hr = IDirect3DDevice9_SetStreamSource(d3ddev, 0, d3dvtc, 0, sizeof(CUSTOMVERTEX));
if (FAILED(hr)) {
    IDirect3DDevice9_EndScene(d3ddev);
    throw FatalException( DXGetErrorDescription(hr), _T(__FILE__), __LINE__ );
}

// we use FVF instead of vertex shader
hr = IDirect3DDevice9_SetFVF(d3ddev, D3DFVF_CUSTOMVERTEX);
if (FAILED(hr)) {
    IDirect3DDevice9_EndScene(d3ddev);
    throw FatalException( DXGetErrorDescription(hr), _T(__FILE__), __LINE__ );
}

//apply shaders

UINT shaderPasses;
if(d3dxShader)
{
    hr = d3dxShader->Begin( &shaderPasses, 0 );
    if (FAILED(hr))
        throw FatalException( DXGetErrorDescription(hr), _T(__FILE__), __LINE__ );
}
else
    shaderPasses = 1;

for( UINT uPass = 0; uPass < shaderPasses; ++uPass )
{
    if(d3dxShader)
    {
        hr = d3dxShader->BeginPass(uPass);
        if (FAILED(hr))
            throw FatalException( DXGetErrorDescription(hr), _T(__FILE__), __LINE__ );
    }

    hr = IDirect3DDevice9_SetTexture(d3ddev, 0, (LPDIRECT3DBASETEXTURE9)d3dtex);
    if (FAILED(hr)) {
        IDirect3DDevice9_EndScene(d3ddev);
        throw FatalException( DXGetErrorDescription(hr), _T(__FILE__), __LINE__ );
    }

    // draw rectangle
    hr = IDirect3DDevice9_DrawPrimitive(d3ddev, D3DPT_TRIANGLEFAN, 0, 2);
    if (FAILED(hr)) {
        IDirect3DDevice9_EndScene(d3ddev);
        throw FatalException( DXGetErrorDescription(hr), _T(__FILE__), __LINE__ );
    }

    if(d3dxShader)
    {
        hr = d3dxShader->EndPass();
        if (FAILED(hr))
            throw FatalException( DXGetErrorDescription(hr), _T(__FILE__), __LINE__ );
    }
}

if(d3dxShader)
{
    hr = d3dxShader->End();
    if (FAILED(hr))
        throw FatalException( DXGetErrorDescription(hr), _T(__FILE__), __LINE__ );
}

// Render the vertex buffer contents
hr = IDirect3DDevice9_SetStreamSource(d3ddev, 0, d3dvtc, 0, sizeof(CUSTOMVERTEX));
if (FAILED(hr)) {
    IDirect3DDevice9_EndScene(d3ddev);
    throw FatalException( DXGetErrorDescription(hr), _T(__FILE__), __LINE__ );
}

// we use FVF instead of vertex shader
hr = IDirect3DDevice9_SetFVF(d3ddev, D3DFVF_CUSTOMVERTEX);
if (FAILED(hr)) {
    IDirect3DDevice9_EndScene(d3ddev);
    throw FatalException( DXGetErrorDescription(hr), _T(__FILE__), __LINE__ );
}

// Setup our texture. Using textures introduces the texture stage states,
// which govern how textures get blended together (in the case of multiple
// textures) and lighting information. In this case, we are modulating
// (blending) our texture with the diffuse color of the vertices.
hr = IDirect3DDevice9_SetTexture(d3ddev, 0, (LPDIRECT3DBASETEXTURE9)d3dtex2);
if (FAILED(hr)) {
    IDirect3DDevice9_EndScene(d3ddev);
    throw FatalException( DXGetErrorDescription(hr), _T(__FILE__), __LINE__ );
}

// draw rectangle
hr = IDirect3DDevice9_DrawPrimitive(d3ddev, D3DPT_TRIANGLEFAN, 0, 2);
if (FAILED(hr)) {
    IDirect3DDevice9_EndScene(d3ddev);
    throw FatalException( DXGetErrorDescription(hr), _T(__FILE__), __LINE__ );
}

// End the scene
hr = IDirect3DDevice9_EndScene(d3ddev);
if (FAILED(hr)) 
    throw FatalException( DXGetErrorDescription(hr), _T(__FILE__), __LINE__ );    

1 个答案:

答案 0 :(得分:0)

这对我有用,似乎在这种情况下必须考虑alpha通道

float offsetBrightness = 0.0f; \
float offsetContrast   = 0.0f; \
float4 PSBrightnessContrast(float2 inCoord : TEXCOORD0) : COLOR0\
{\
   float4 pixelColor = tex2D( screen, inCoord.xy); \
   pixelColor.rgb /= pixelColor.a; \
   \
  pixelColor.rgb = ((pixelColor.rgb + 1.0f) * max(offsetContrast, 0)) - 1.0f; \
  pixelColor.rgb += offsetBrightness; \
  pixelColor.rgb *= pixelColor.a; \
\
 return pixelColor;\
}\