I just found the root of my problem(not the solution) ...Weirdest of all i have ever had:
for(int rrr=0;rrr!=1;rrr++)
{
g_pd3dDevice->ClearDepthStencilView( g_pDepthStencilViewSpace3D, D3D10_CLEAR_STENCIL, 0.0f, 0 );
g_pd3dDevice->OMSetDepthStencilState(g_pDepthStencilStateSpace3D,1);
if(rrr==0){g_pd3dDevice->OMSetBlendState(g_pBlSRed , 0, 0xffffffff);}
else if(rrr==1){g_pd3dDevice->OMSetBlendState(g_pBlSGreen , 0, 0xffffffff);}
else if(rrr==2){g_pd3dDevice->OMSetBlendState(g_pBlSBlue , 0, 0xffffffff);}
else if(rrr==3){g_pd3dDevice->OMSetBlendState(g_pBlSAlpha , 0, 0xffffffff);}
g_pT->SetFloat((float)rrr);
g_pTechnique3DSpace->GetPassByIndex( 0 )->Apply( 0 );
g_pd3dDevice->DrawInstanced( 1, 1, 0, 0);
}
The principal problem is that its passing the stencil test in the 2nd, 3rd, 4th and on an on loop, and appart from that in those same loops there are other weird things happening (i don't know why) that may be related to why it passes the stencil test.
I am rendering to 1 texel of a texture using a point vertex
The default value of all texels in texture is float4(-1,-1,-1,-1)
The stencil test passes if EQUAL, OMSetDepthStencilState() sets to 1 and ClearDepthStencilView() sets the other to 0, it will have to fail always
the depth test is disabled, if depth test fails nothing happens
if the stencil test passes or fails the stencil value is incremented
The blending just mask a color(the color in the name)
The value rendered to the texel is float4(rrr,rrr,rrr,rrr)
If the loop does 1 loop: for(int rrr=0;rrr!=1;rrr++)
-nothing is write to the texel, it failed the stencil test.
if the loop does 2 loops: for(int rrr=0;rrr!=2;rrr++)
-In the first loop nothing is rendered, it failed the stencil test.
-In the second loop it passes the stencil test(I dont know why) so it renders to the texel float4(0,0,0,0)-->it should be float4(1,1,1,1) but its not (i don't know why)
and it is masked by the unupdated red mask(i dont know why)-->it should be the green mask, the final texel value is float4(0,-1,-1,-1)-->g_pT is 0 not 1(i don't know why)
if the loop does 3 loops: for(int rrr=0;rrr!=3;rrr++)
-In the first loop nothing is rendered, it failed the stencil test.
-In the second loop it passes the stencil test(I don't know why), it renders to the texel float4(0,0,0,0)-->it should be float4(1,1,1,1) but its not (i don't know why)
and it is masked by the red mask(i don't know why)-->it should be the green mask, the final texel value is float4(0,-1,-1,-1)-->g_pT is 0 not 1(i don't know why)
-In the third loop it passes the stencil test(I don't know why) so it renders to the texel float4(1,1,1,1) -->it should be float4(2,2,2,2) but its not (i don't know why)
and it is masked by the green mask(i don't know why)-->it should be the blue mask, the final texel value is float4(0,1,-1,-1)
-->g_pT is 1 not 2(i don't know why)
In the end if i replace drawInstance() by a draw() inside a loop it all runs correctly but i cant because in the real code i run aroun 5000 instances