   [D3D10] rendering depth to texture
  Hi All!I'm trying to render two blocks of geometry and save their depth values to a texture. Here is how they look when drawn separately (the backfaces are culled):The near one:The far one:I'm using the following render states:DepthStencilState EnableDepth{   DepthEnable = TRUE;   DepthWriteMask = ALL;   DepthFunc = LESS;};BlendState MyBlending{   AlphaToCoverageEnable = FALSE;   BlendEnable[0] = TRUE;   SrcBlend = SRC_COLOR;   DestBlend = DEST_COLOR;   BlendOp = MIN;   RenderTargetWriteMask[0] = 0xFF;};Now the result differs depending on the order in which I draw the blocks. Here comes the correct result:However, in my program I cannot always guarantee the correct order in which the blocks are drawn. So I often get the following result:The problem is, as can be seen above, that the farther block is overdrawing the nearer one. Are the render states that I've mentioned above only valid for a single draw call? Is it possible to get the correct result without proper ordering?I'd rather avoid putting the data to a single buffer, since the blocks are meant to be reusable and of varying size.I couldn't really find any other solution so any help is appreciated.   
  What's with the funky blending states? You're doing this:result = min(src * src, dest * dest);For rendering depth you shouldn't need any blending, just enable depth tests and it will come out fine.   ;  Ok, I think I'm completely lost now. Maybe I'll try to explain things more thoroughly.The format of my texture (which I'm trying to render depth into) is DXGI_FORMAT_R32_FLOAT. Its bind flags are D3D10_BIND_SHADER_RESOURCE and D3D10_BIND_RENDER_TARGET. Then I set this texture as the render target and draw the first block of geometry (the near one from post #1). I write the depth values in the pixel shader. Everything works fine so far.Then I draw the second block (the far one from post #1; when drawn alone it looks good too). In the meanwhile I did nothing with the texture. Everything from this block is drawn on top of the previously rendered values. What I was trying to do with the blending thing was to compare the previously written value to the value I have from the current block, and take the smaller one. This should indicate which pixel is closer.And now I found that the blending states I've mentioned just above and in the first post as well - actually do nothing.Surely there must be something wrong with the idea ;) I'd appreciate any help to get to know what's the correct way of doing things here.Btw., MJP, could you explain why the blending states in the first post cause the result you've mentioned?Quote:result = min(src * src, dest * dest);;  Yeah I understand what you're doing. I was just saying that the blending states are unnecessary: as long as you have depth test and depth writes enabled, you should be able to output your depth value to the render target without any blending enabled and you should get the result that you want. If that's not happening, then either your states or the contents of your depth buffer aren't what you want them to be. For either case PIX is great debugging tool. What you'll want to do is capture a single frame, and then find the draw calls where you're rendering your depth. IF you highlight either draw call, you can then look at the state of your ID3D10Device top see what depth states are enabled. You can also look at your depth-stencil buffer, and see what's in there. Oh and before you mess with PIX, make sure you're clearing your depth buffer before rending. That's a pretty easy thing to forget!As for blend states, they work like this:-SrcBlend defines what value should be multiplied with the incoming color, AKA the color you output from your pixel shader. Since you specified "SRC_COLOR" as the value, that means the incoming (source) color rgb values will be multiplied with itself. Some other possible values are ONE (multiply by 1), ZERO (multiply by 0), and SRC_ALPHA (the alpha component of the source color).-DestBlend is the same, but for the color that's already in the render target-BlendOp specified how the source color and dest color should be combined after they are multiplied with the SrcBlend/DestBlend values. So for instance if you specify SrcBlend = SRC_ALPHA, DestBlend = INV_SRC_ALPHA, and BlendOp = ADD you get this:result = (source.rgb * source.a) + (dest.rgb * (1 - source.a))In your case you were using "MIN", which means the result is the minimum of the left and right sides.   ;  Thank you very much for your help! I was passing NULL as a depth-stencil view in OMSetRenderTargets() [oh]   