I have a shader that needs to additively blend to its output render target. While it had been fully implemented and working, I recently refactored and have done something that is causing the alpha blending to not work anymore. I'm pretty sure that the problem is somewhere in my calls to either OpenGL or cgfx - but I'm currently at a loss for where exactly the problem is, as everything looks like it is set up properly for alpha blending to occur. No OpenGL or cg framework errors are showing up, either.
For some context, what I'm doing here is taking a buffer which contains screen position and luminance values for each pixel, copying it to a PBO, and using it as the vertex buffer for drawing GL_POINTS. Everything except for the alpha blending appears to be working as expected.
I've confirmed both that the input vertex buffer has the correct values, and that my vertex and fragment shaders are outputting the points to the correct locations and with the correct luminance values.
The way that I've arrived at the conclusion that the Alpha blending was broken is by making my vertex shader output every point to the same screen location and then setting the pixel shader to always output a value of float4(0.5) for that pixel. Invariably, the end color (dumped afterwards) ends up being float4(0.5).
The confusing part is that as far as I can tell, everything is properly set for alpha blending to occur.
The cgfx pass has the two following state assignments (among others - I'll put a full listing at the end):
BlendEnable = true;
BlendFunc = int2(One, One);
This ought to be enough, since I am calling cgSetPassState() - and indeed, when I use glGets to check the values of GL_BLEND_SRC, GL_BLEND_DEST, GL_BLEND, and GL_BLEND_EQUATION they all look appropriate (GL_ONE, GL_ONE, GL_TRUE, and GL_FUNC_ADD). This check was done immediately after the draw call.
I've been looking around to see if there's anything other than blending being enabled and the blending function being correctly set that would cause alpha blending not to occur, but without any luck. I considered that I could be doing something wrong with GL, but GL is telling me that blending is enabled. I doubt it's cgFX related (as otherwise the GL state wouldn't even be thinking it was enabled) but it still fails if I explicitly use GL calls to set the blend mode and enable it.
Here's the trimmed down code for starting the cgfx pass and the draw call:
CGtechnique renderTechnique = Filter->curTechnique;
TEXUNITCHECK;
CGpass pass = cgGetFirstPass(renderTechnique);
TEXUNITCHECK;
while (pass)
{
cgSetPassState(pass);
cgUpdatePassParameters(pass);
//drawFSPointQuadBuff((void*)PointQuad);
drawFSPointQuadBuff((void*)LumPointBuffer);
TEXUNITCHECK;
cgResetPassState(pass);
pass = cgGetNextPass(pass);
};
and the function with the draw call:
void drawFSPointQuadBuff(void* args)
{
PointBuffer* pointBuffer = (PointBuffer*)args;
FBOERRCHECK;
glClear(GL_COLOR_BUFFER_BIT);
GLERRCHECK;
glPointSize(1.0);
GLERRCHECK;
glEnableClientState(GL_VERTEX_ARRAY);
GLERRCHECK;
glEnable(GL_POINT_SMOOTH);
if (pointBuffer->BufferObject)
{
glBindBufferARB(GL_ARRAY_BUFFER_ARB, (unsigned int)pointBuffer->BufData);
glVertexPointer(pointBuffer->numComp, GL_FLOAT, 0, 0);
}
else
{
glVertexPointer(pointBuffer->numComp, GL_FLOAT, 0, pointBuffer->BufData);
};
GLERRCHECK;
glDrawArrays(GL_POINTS, 0, pointBuffer->numElem);
GLboolean testBool;
glGetBooleanv(GL_BLEND, &testBool);
int iblendColor, iblendDest, iblendEquation, iblendSrc;
glGetIntegerv(GL_BLEND_SRC, &iblendSrc);
glGetIntegerv(GL_BLEND_DST, &iblendDest);
glGetIntegerv(GL_BLEND_EQUATION, &iblendEquation);
if (iblendEquation == GL_FUNC_ADD)
{
cerr << "Correct func" << endl;
};
GLERRCHECK;
if (pointBuffer->BufferObject)
{
glBindBufferARB(GL_ARRAY_BUFFER_ARB,0);
}
GLERRCHECK;
glDisableClientState(GL_VERTEX_ARRAY);
GLERRCHECK;
};
Finally, here is the full state setting of the shader:
AlphaTestEnable = false;
DepthTestEnable = false;
DepthMask = false;
ColorMask = true;
CullFaceEnable = false;
BlendEnable = true;
BlendFunc = int2(One, One);
FragmentProgram =
compile glslf std_PS();
VertexProgram =
compile glslv bilatGridVS2();