Question

I have a 2D scene, 2 textures set as render targets, and a vertex buffer.

first texture is filtered out by contrast/brightness pixel shader, the second acts as a menu, which I would not like to be processed by the same shader, but want it to be alphablended with the first one.

Everything runs fine, until I go to extreme values for brightness/contrast which looks like the fade out effect when changing the the second one and it stops to redraw at all on very extreme ones, though the data is always fed to the first one.

Disabling AlphaBlendnable corrects it, but I loose alpha blending... Any way to clean the buffers prior to applying pixel shaders? Any help appreciated.

pixel shader code:

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 ); \
}\

Rendering code

// 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__ );    
Was it helpful?

Solution

This has worked for me, seems the alpha channel must be accounted in this case

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;\
}\
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top