Logical Operation Style Alpha Blending in D3D

Started by
9 comments, last by ET3D 16 years, 5 months ago
In OpenGL, one can do logical operations such as OR/XOR/Invert to current fragment and the value in the framebuffer correspondingly. I'm asking if there is any similar operations in D3D. Thanks a lot! [Edited by - NicholasYang on October 29, 2007 9:36:52 PM]
Advertisement
There is a group of render states in D3D9 (or Output Merger stage in D3D10) that control how the new fragments are combined with the existing ones.

You use color blending by manipulating the following render states:
D3DRS_BLENDFACTOR
D3DRS_SRCBLEND
D3DRS_DESTBLEND
...and the corresponding states for alpha channel combiners.

It is easy to use the inverse of either source or destination, but bitwise operations are not directly supported using the standard blending scheme. Instead, you could use a render-target texture as a temporary surface and use pixel shaders to implement custom blending (on modern cards, you can run just about any logic there).

Niko Suni

Quote:Original post by Nik02
There is a group of render states in D3D9 (or Output Merger stage in D3D10) that control how the new fragments are combined with the existing ones.

You use color blending by manipulating the following render states:
D3DRS_BLENDFACTOR
D3DRS_SRCBLEND
D3DRS_DESTBLEND
...and the corresponding states for alpha channel combiners.

It is easy to use the inverse of either source or destination, but bitwise operations are not directly supported using the standard blending scheme. Instead, you could use a render-target texture as a temporary surface and use pixel shaders to implement custom blending (on modern cards, you can run just about any logic there).


Thanks for your suggestion! But there is something more complicated that custom blending could not do. For example, I want to do an operation that is very similar to Alpha Blending. The difference between them is that, I want the operator to be a logical operator, EX. XOR/OR. I could not afford to render one layer to a surface and then render another and after several layers are ready, use a pixel shader to combine them. This is very inefficient.
What can I do in D3D to implement this ?

Prior to Direct3D 10 (which introduces a 32bit integer instruction set) you can't really do proper logical or bitwise operations in a pixel shader. You can fudge the results by being clever and hoping that floating point precision doesn't bite you, but ultimately it's still a hack.

Quote:I could not afford to render one layer to a surface and then render another and after several layers are ready, use a pixel shader to combine them. This is very inefficient.
Are you sure? I'm not saying you're wrong as such, but the style of architecture Niko proposed is fairly common place in D3D rendering (breaking down complex operations into many smaller ones) and isn't always bad for performance.

GPU's have pretty complex performance characteristics, so something that sounds bad on paper isn't necessarily bad in practice (and vice versa)...

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Quote:Original post by jollyjeffers
Prior to Direct3D 10 (which introduces a 32bit integer instruction set) you can't really do proper logical or bitwise operations in a pixel shader. You can fudge the results by being clever and hoping that floating point precision doesn't bite you, but ultimately it's still a hack.


It should be easy to get a logical operation done in D3D9 by use of a 256 x 256 x 8-bit lookup texture. You'd just need 3 lookups per pixel (one for each channel). I suspect it would be quicker than the floating point maths, and it's certainly less dependant on precision.
Yup, the look-up table approach is a good one provided you can fit everything into a texture (e.g. 8-12bit resolution) and don't mind trading arithmetic performance for bandwidth and memory consumption.

I've found it to work quite well for factorising other rendering functions, but equally I've seen reports of it being quicker to brute force the arithmetic and save bandwidth and memory for other things... YMMV [smile]

Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

There is one thing I want to HIGHLIGHT. I mean I want to implement a ALPHA- BLENDING-LIKE logical operation. How can I do it Just Like In OPENGL.
Rendering layer after layer has the shortcoming of complexity. First of all, you do not know how many layers there actually are. You need to guarantee that all layers have been rendered. You need to do depth peeling again and again. You need to sample many many samples from the layers in the final combination pass whose efficiency can not be guaranteed.
As Niko suggested, you'll need to look into the D3DBLEND enumeration. That lists each and every operation you can do in an "alpha blend style".

The fixed function texture cascade can do some clever operations, but ultimately that is only a different way of expressing SM1 pixel shaders.

If you can't find anything in D3DBLEND then you're going to have to go down the complex pixel shader route. Might just be that D3D doesn't expose the same GPU functionality as OpenGL...

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

NicholasYang, can you tell me how it's done in OpenGL? I don't remember such a blending operation in OpenGL (granted, I used it long ago), and it doesn't make sense to me, considering that bitwise operations depend on the number format. So seeing some reference to the operation in OpenGL would be nice. Thanks.
Quote:Original post by ET3D
NicholasYang, can you tell me how it's done in OpenGL? I don't remember such a blending operation in OpenGL (granted, I used it long ago), and it doesn't make sense to me, considering that bitwise operations depend on the number format. So seeing some reference to the operation in OpenGL would be nice. Thanks.


It is called logical operation in OpenGL, you can get some discussion from the book <<OpenGL programming Guide>> chapter 10, section 10.2.5. I suppose that for GPU, there is only one architecture of pipeline, only drivers differ for OpenGL and D3D. If I'm right, there must be some method to achieve the same functionality in D3D as in OpenGL

This topic is closed to new replies.

Advertisement