700 likes | 884 Views
Blending and Stencilling. Paul Taylor 2010. Alpha Blending. Back-to-Front Disable Depth-Write We still use depth-read to clip polygons. Out of Order Polygons. Good Transparency Bad Transparency. with OIT. without OIT.
E N D
Blending and Stencilling Paul Taylor 2010
Alpha Blending • Back-to-Front • Disable Depth-Write • We still use depth-read to clip polygons
Out of Order Polygons Good TransparencyBad Transparency with OIT without OIT http://developer.nvidia.com/object/order_independent_transparency.html
Out of order solutions • Depth Peeling • The pixels are stripped away like an onion • Layer by layer • This requires a second depth buffer, used to store each previous ‘layer’
Layer 0 Layer 1 Layer 2 Layer 3
Issues • The depth buffer is stored in Projected Pixels, losing a lot of data accuracy • And now a pretty example:
1 layer 2 layers 3 layers 4 layers
Why a Teapot? When was it created? What is it called? Why was it created?
Why a Teapot? When was it created? 1975 What is it called? Utah Teapot or Newell Teapot Why was it created? Newell needed a mathematically describable object to render. As a universally accessible object, it has proven to be a useful benchmark to date...
The Actual Teapot The Rendered Teapot
Another Approach to OIT (K-Buffering) • This approach focuses on capturing the pixel layers as they are rendered • Next, utilising a full-screen shader to sort the pixels. • Very similar to other depth-peeling approaches
Nicolas Thibieroz – Reverse Depth Peeling • Layers are found back-to-front allowing immediate blending (great for low RAM hardware e.g. Consoles). 1. Determine furthest layer2. Fill-up depth buffer texture3. Fill-up normal and colour buffer4. Do lighting & shadowing5. Blend in back-buffer6. Go to 1 for the next layer For large amounts of transparency, four or more passes may be required! :-S
http://diaryofagraphicsprogrammer.blogspot.com/2009/11/order-independent-transparency.htmlhttp://diaryofagraphicsprogrammer.blogspot.com/2009/11/order-independent-transparency.html http://portal.acm.org/citation.cfm?id=1572769.1572779 Depth Peeling via a Bucket Sort
Weighted Averages • http://www.slideshare.net/acbess/order-independent-transparency-presentation • A cheap-out, ignoring the need to sort layers, it simply accumulates, weighted with distance
Actual Blending Paul Taylor 2010
The Blending Flow in Dx 10 http://takinginitiative.wordpress.com/
Why Blending... Blending has become more and more important in Video Games It allows for more realistic and faster lighting, shadows, and translucent effects As the uses for blending have increased, so has the complexity of the blending process
Dumbing down the complexities • In an idealised world we are dealing with 2 simple components: • A Colour Layer (RGB) • A Blending Layer (A) • This expands to 4 once we decide to have a source and a destination layer: • Source Colour Layer (sRGB) • Source Blending Layer (sA) • Destination Colour Layer (dRGB) • Destination Blending Layer (dA)
Why is the Alpha Layer Seperate? • The Alpha layer can be used in many different ways, the most obvious is as a stencilling / blending control value • This comes into play soon...
Now we must add the actual blending function: Known in Dx Land as D3D10_BLEND_OP: D3D10_BLEND_OP_ADD = 1, D3D10_BLEND_OP_SUBTRACT = 2, *D3D10_BLEND_OP_REV_SUBTRACT = 3, D3D10_BLEND_OP_MIN = 4, D3D10_BLEND_OP_MAX = 5 *Reverse Subtract will subtract source 2 from source 1
This gives us some basic control over how colours are blended together This leaves us with the following basic equation: PixelOut = sRGB <BlendOperation> dRGB 0.6, 0.7, 0.8 = 1.0, 1.0, 1.0 REV_SUBTRACT 0.4, 0.3, 0.2
Extending this to include an Alpha Layer • The alpha layer has its own blending operations, independent of the colour blending. • But how much of the total DX blending technique have we covered?
The Blending Flow in Dx 10 Pixel = sRGB <BlendOperation> dRGB http://takinginitiative.wordpress.com/
Pre Blending • This allows us to modify how each source pixel is sampled
Source and Destination Blending Now things are getting complicated • We can use SRC and DEST blends in BOTH the source and destination blending settings • It is these blends that complete the blending and give Dx 10 its power!
D3D has a LOT of blends! D3D10_BLEND_ZERO = 1, D3D10_BLEND_ONE = 2, D3D10_BLEND_SRC_COLOR = 3, D3D10_BLEND_INV_SRC_COLOR = 4, D3D10_BLEND_SRC_ALPHA = 5, D3D10_BLEND_INV_SRC_ALPHA = 6, D3D10_BLEND_DEST_ALPHA = 7, D3D10_BLEND_INV_DEST_ALPHA = 8, D3D10_BLEND_DEST_COLOR = 9, D3D10_BLEND_INV_DEST_COLOR = 10, D3D10_BLEND_SRC_ALPHA_SAT = 11, D3D10_BLEND_BLEND_FACTOR = 14, D3D10_BLEND_INV_BLEND_FACTOR = 15, D3D10_BLEND_SRC1_COLOR = 16, D3D10_BLEND_INV_SRC1_COLOR = 17, D3D10_BLEND_SRC1_ALPHA = 18, D3D10_BLEND_INV_SRC1_ALPHA = 19 Black White Blend Factor set through OMSetBlendState ‘’ Second source is from Pixel Shader* Second source is from Pixel Shader* Second source is from Pixel Shader* Second source is from Pixel Shader* * Dual-source colour blending
Matrix Direct Products What we previously had: Pixel = sRGB <BlendOperation> dRGB Adding the new blend to both our source and destination surfaces: Source = sRGB x sBlendOperation Destination = dRGB x dBlendOperation
Welcome to Ugly Town: Pixel = (sRGB x sBlendOperation) <BlendOperation> (dRGB x dBlendOperation) It looks better as a part of the flow chart:
Utilising these Blends sRGB = sRGB x sBlendOperation dRGB = dRGB x dBlendOperation Pixel = sRGB <BlendOperation> dRGB
Examples of Common Blending Methods • No Change Blending • sBlend = ZERO • dBlend = ONE • BlendOp = ADD Pixel = (sRGB x 0,0,0) + (dRGB x 1,1,1) Pixel = dRGB Why? Generating a depth / stencil buffer without drawing or changing the scene
Examples of Common Blending Methods • Additive Blending • sBlend = ONE • dBlend = ONE • BlendOp = ADD Pixel = (sRGB x 1,1,1) + (dRGB x 1,1,1) Pixel = sRGB + dRGB Why? Applying a lightmap to a texture
Examples of Common Blending Methods • Multiplicative Blending • sBlend = ZERO • dBlend = SRC_COLOR • BlendOp = ADD Pixel = (sRGB x 0,0,0) + (dRGB x sRGB) Pixel = dRGB x sRGB Why? A different approach to applying a lightmap to a texture
Examples of Common Blending Methods • Alpha Transpacency Blending • sBlend = SRC_ALPHA • dBlend = INV_SRC_ALPHA • BlendOp = ADD Pixel = (sRGB x sA,sA,sA) + (dRGB x 1-sA,1-sA,1-sA) If sA = 0.75 Pixel = dRGB x .75 + sRGB x .25 No intensity change, just colour variance (Range still 0 – 1) Why? Transparency
In Dx Code D3D10_BLEND_DESC BlendState; ZeroMemory(&BlendState, sizeof(D3D10_BLEND_DESC)); BlendState.BlendEnable[0] = TRUE; BlendState.SrcBlend = D3D10_BLEND_SRC_ALPHA; BlendState.DestBlend = D3D10_BLEND_INV_SRC_ALPHA; BlendState.BlendOp = D3D10_BLEND_OP_ADD; BlendState.SrcBlendAlpha = D3D10_BLEND_ZERO; BlendState.DestBlendAlpha = D3D10_BLEND_ZERO; BlendState.BlendOpAlpha = D3D10_BLEND_OP_ADD; BlendState.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL; pd3dDevice->CreateBlendState(&BlendState, &g_pBlendState);
Why are the following two lines arrays? BlendState.BlendEnable[0] = TRUE; BlendState.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL; Dx 10 can have up to 8 Render Targets Depending on exactly what you want to capture to each target, shaders can be defined per target *It will still take 1x pass for each shader Think about the layered images from last weeks lighting, these could be generated easily with different blends
Integrating the Blend Struct with the Flowchart BlendState.BlendEnable[0] = TRUE; BlendState.SrcBlend = D3D10_BLEND_SRC_ALPHA; BlendState.DestBlend = D3D10_BLEND_INV_SRC_ALPHA; BlendState.BlendOp = D3D10_BLEND_OP_ADD; BlendState.SrcBlendAlpha = D3D10_BLEND_ZERO; BlendState.DestBlendAlpha = D3D10_BLEND_ZERO; BlendState.BlendOpAlpha = D3D10_BLEND_OP_ADD; BlendState.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL;
Depth Tests and Blending • Do we need to disable depth-testing? • In almost all cases yes • When transparency blending we need to maintain the draw order as blends are non-commutative • Basic Additive / Subtractive / Multiplicative blends are commutative • Basic being those that do not use alpha blends
Can we just disable depth testing? • No! • Basically it’s still good to depth sample the existing rendered surfaces, in case out transparent objects are occluded • We can use a OMSetDepthStencilState to disable writing to the depth buffer, whist retaining the ability to read from the depth buffer, and reject obscured pixels
Alpha Clipping Enhanced Typically if the Alpha of a pixel is 0.0f we can safely discard it. Right? After a pixel goes through some different mip-mapping and scaling all your 0.0f pixels may be a little different Adding a clip call will fix this issue: Clip(alpha – 0.24f); This will drop a pixel form the PS if the value is below 0.0f
StencillingPart of the Output-Merger Stage Paul Taylor 2010
Utilising the Stencil Buffer To Begin : Back to basics How big is the Stencil Buffer? 8 bits 00 – FF. The most obvious use of a stencil buffer would require only 1 bit. So what of all the extra storage we have?
Multiple Stencils We could generate up to 8 different stencils in a single pass We could use an incremental stencil buffer to generate a depth-complexity map (as each polygon is written, the stencil increases value form 0 towards 255)
More Complex Blending! • At least we don’t have to deal with Pre-Blending or dual-source blending • But Stencilling introduces Masking
Flowcharts!!! • You already know most of the LHS http://msdn.microsoft.com/en-us/library/bb205120%28VS.85%29.aspx
Comparisons The Stencil Buffer does two jobs • The Stencil Test • Accumulating Values such as z-complexity The stencil test is the easy part! Once the stencil has been created.....
Using the Stencil Buffer • Stencil Reference Value (StencilRef) This is a value 0-255 passed in as the StencilState is set The stencil comparison simplified Stencil Ref <Comparison> PixelValue If a pixel fails the Stencil Test it is rejected and will never make it to the depth buffer of the back buffer So what are the possible comparisons...
Comparisons D3D10_COMPARISON_NEVER = 1, D3D10_COMPARISON_LESS = 2, D3D10_COMPARISON_EQUAL = 3, D3D10_COMPARISON_LESS_EQUAL = 4, D3D10_COMPARISON_GREATER = 5, D3D10_COMPARISON_NOT_EQUAL = 6, D3D10_COMPARISON_GREATER_EQUAL = 7, D3D10_COMPARISON_ALWAYS = 8
Masking • Stencil Read Mask • This allows the comparison to be done on a subset of the stencil bits. Actual comparison Stencil Ref & Mask <Comparison> PixelValue & Mask
Using the Test http://msdn.microsoft.com/en-us/library/bb205120%28VS.85%29.aspx