Sort-Independent Alpha Blending. Houman Meshkin Senior Graphics Engineer Perpetual Entertainment

Similar documents
Image Synthesis. Transparency. computer graphics & visualization

Equations, Inequalities & Partial Fractions

Optimizing AAA Games for Mobile Platforms

Basic numerical skills: EQUATIONS AND HOW TO SOLVE THEM. x + 5 = = (2-2) = = 5. x = 7-5. x + 0 = 20.

Making natural looking Volumetric Clouds In Blender 2.48a

Sums & Series. a i. i=1

INTRODUCTION TO RENDERING TECHNIQUES

Vieta s Formulas and the Identity Theorem

Properties of Real Numbers

Click on the links below to jump directly to the relevant section

a 11 x 1 + a 12 x a 1n x n = b 1 a 21 x 1 + a 22 x a 2n x n = b 2.

Linear Programming. Solving LP Models Using MS Excel, 18

Calculate Highest Common Factors(HCFs) & Least Common Multiples(LCMs) NA1

CHAPTER 5 Round-off errors

Lezione 4: Grafica 3D*(II)

Math 115 Spring 2011 Written Homework 5 Solutions

Image Synthesis. Fur Rendering. computer graphics & visualization

A Little Set Theory (Never Hurt Anybody)

Lecture 2 Matrix Operations

BCC Multi Stripe Wipe

Data Centric Interactive Visualization of Very Large Data

2.6 Exponents and Order of Operations

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS

8 Primes and Modular Arithmetic

Kevin James. MTHSC 102 Section 1.5 Exponential Functions and Models

This unit will lay the groundwork for later units where the students will extend this knowledge to quadratic and exponential functions.

Image Processing and Computer Graphics. Rendering Pipeline. Matthias Teschner. Computer Science Department University of Freiburg

Pigeonhole Principle Solutions

The Purchase Price in M&A Deals

My Materials. In this tutorial, we ll examine the material settings for some simple common materials used in modeling.

Some Polynomial Theorems. John Kennedy Mathematics Department Santa Monica College 1900 Pico Blvd. Santa Monica, CA

Section 10.4 Vectors

Equations, Lenses and Fractions

Linear Equations and Inequalities

The Method of Partial Fractions Math 121 Calculus II Spring 2015

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS. + + x 2. x n. a 11 a 12 a 1n b 1 a 21 a 22 a 2n b 2 a 31 a 32 a 3n b 3. a m1 a m2 a mn b m

Trigonometric Functions and Triangles

Systems of Equations Involving Circles and Lines

3. Mathematical Induction

Numerical and Algebraic Fractions

MATHEMATICS FOR ENGINEERING BASIC ALGEBRA

Otis Photo Lab Inkjet Printing Demo

Advanced Visual Effects with Direct3D

A Short Guide to Significant Figures

Georgia Standards of Excellence Curriculum Map. Mathematics. GSE 8 th Grade

Compound Interest. Invest 500 that earns 10% interest each year for 3 years, where each interest payment is reinvested at the same rate:

Welcome to the topic on valuation methods.

Year 9 set 1 Mathematics notes, to accompany the 9H book.

Working with the BCC Clouds Generator

Accentuate the Negative: Homework Examples from ACE

A Prime Investigation with 7, 11, and 13

6.3 Conditional Probability and Independence

Answer: (a) Since we cannot repeat men on the committee, and the order we select them in does not matter, ( )

In mathematics, there are four attainment targets: using and applying mathematics; number and algebra; shape, space and measures, and handling data.

5 means to write it as a product something times something instead of a sum something plus something plus something.

Deferred Shading & Screen Space Effects

Section 6.1 Factoring Expressions

3.3. Solving Polynomial Equations. Introduction. Prerequisites. Learning Outcomes

FOIL FACTORING. Factoring is merely undoing the FOIL method. Let s look at an example: Take the polynomial x²+4x+4.

Partial Fractions. (x 1)(x 2 + 1)

Multiplication and Division with Rational Numbers

There is a simple equation for calculating dilutions. It is also easy to present the logic of the equation.

5.5. Solving linear systems by the elimination method

Basic Use of the TI-84 Plus

3. Solve the equation containing only one variable for that variable.

Integrals of Rational Functions

If A is divided by B the result is 2/3. If B is divided by C the result is 4/7. What is the result if A is divided by C?

EdExcel Decision Mathematics 1

5 Numerical Differentiation

0.8 Rational Expressions and Equations

Test 4 Sample Problem Solutions, = , 7 5, = = 1.4. Moving the decimal two spots to the left gives

with functions, expressions and equations which follow in units 3 and 4.

Prot Maximization and Cost Minimization

NVIDIA IndeX Enabling Interactive and Scalable Visualization for Large Data Marc Nienhaus, NVIDIA IndeX Engineering Manager and Chief Architect

Clock Arithmetic and Modular Systems Clock Arithmetic The introduction to Chapter 4 described a mathematical system

SOLVING QUADRATIC EQUATIONS - COMPARE THE FACTORING ac METHOD AND THE NEW DIAGONAL SUM METHOD By Nghi H. Nguyen

Grade 5 Math Content 1

3.2. Solving quadratic equations. Introduction. Prerequisites. Learning Outcomes. Learning Style

Definition 8.1 Two inequalities are equivalent if they have the same solution set. Add or Subtract the same value on both sides of the inequality.

Chemical Kinetics. 2. Using the kinetics of a given reaction a possible reaction mechanism

7.7 Solving Rational Equations

No Solution Equations Let s look at the following equation: 2 +3=2 +7

SYSTEMS OF PYTHAGOREAN TRIPLES. Acknowledgements. I would like to thank Professor Laura Schueller for advising and guiding me

Applications of Fermat s Little Theorem and Congruences

Common Core Standards for Fantasy Sports Worksheets. Page 1

Kenken For Teachers. Tom Davis June 27, Abstract

4.1. COMPLEX NUMBERS

Basics of Polynomial Theory

Kapitel 1 Multiplication of Long Integers (Faster than Long Multiplication)

Regions in a circle. 7 points 57 regions

PURSUITS IN MATHEMATICS often produce elementary functions as solutions that need to be

Answer Key for California State Standards: Algebra I

Chapter 14 Review Note Sample Excerpt

CSS 101. CSS CODE The code in a style sheet is made up of rules of the following types

Time Series Forecasting Techniques

Mean, Median, Standard Deviation Prof. McGahagan Stat 1040

Inventory Management IV: Inventory Management Systems

CSI 333 Lecture 1 Number Systems

Transcription:

Sort-Independent Alpha Blending Houman Meshkin Senior Graphics Engineer Perpetual Entertainment hmeshkin@perpetual.com

Alpha blending Alpha blending is used to show translucent objects Translucent objects render by blending with the background Opaque objects just cover the background

Varying alpha

Blending order The color of a translucent pixel depends on the color of the pixel beneath it it will blend with that pixel, partially showing its color, and partially showing the color of the pixel beneath it Translucent objects must be rendered from far to near

Challenge It s very complex and complicated to render pixels from far to near Object-center sorting is common still can be time consuming Object sorting doesn t guarantee pixel sorting objects can intersect each other objects can be concave pixel sorting is required for correctness

The Formula C0: foreground RGB color A0: alpha representing foreground s translucency D0: background RGB color FinalColor = A0 * C0 + (1 A0) * D0 as A0 varies between 0 and 1, FinalColor varies between D0 and C0

Multiple translucent layers

Formula for multiple translucent layers Cn: RGB from n th layer An: Alpha from n th layer D0: background D1 = A0*C0 + (1 - A0)*D0 D2 = A1*C1 + (1 - A1)*D1 D3 = A2*C2 + (1 - A2)*D2 D4 = A3*C3 + (1 - A3)*D3

Expanding the formula D4 = A3*C3 + A2*C2*(1 - A3) + A1*C1*(1 - A3)*(1 - A2) + A0*C0*(1 - A3)*(1 - A2)*(1 - A1) + D0*(1 - A3)*(1 - A2)*(1 - A1)*(1 - A0)

Further expanding D4 = A3*C3 + A2*C2 - A2*A3*C2 + A1*C1 - A1*A3*C1 - A1*A2*C1 + A1*A2*A3*C1 + A0*C0 - A0*A3*C0 - A0*A2*C0 + A0*A2*A3*C0 - A0*A1*C0 + A0*A1*A3*C0 + A0*A1*A2*C0 - A0*A1*A2*A3*C0 + D0 - A3*D0 - A2*D0 + A2*A3*D0 - A1*D0 + A1*A3*D0 + A1*A2*D0 - A1*A2*A3*D0 - A0*D0 + A0*A3*D0 + A0*A2*D0 - A0*A2*A3*D0 + A0*A1*D0 - A0*A1*A3*D0 - A0*A1*A2*D0 + A0*A1*A2*A3*D0

Rearranging D4 = D0 + A0*C0 + A1*C1 + A2*C2 + A3*C3 - A0*D0 - A1*D0 - A2*D0 - A3*D0 + A0*A3*D0 + A0*A2*D0 + A0*A1*D0 + A1*A3*D0 + A1*A2*D0 + A2*A3*D0 - A0*A3*C0 - A0*A2*C0 - A0*A1*C0 - A1*A3*C1 - A1*A2*C1 - A2*A3*C2 + A0*A1*A2*C0 + A0*A1*A3*C0 + A0*A2*A3*C0 + A1*A2*A3*C1 - A0*A1*A2*D0 - A0*A1*A3*D0 - A0*A2*A3*D0 - A1*A2*A3*D0 + A0*A1*A2*A3*D0 - A0*A1*A2*A3*C0

Sanity check Let s make sure the expanded formula is still correct case where all alpha = 0 D4 = D0 only background color shows (D0) case where all alpha = 1 D4 = C3 last layer s color shows (C3)

Pattern recognition D4 = D0 + A0*C0 + A1*C1 + A2*C2 + A3*C3 - A0*D0 - A1*D0 - A2*D0 - A3*D0 + A0*A3*D0 + A0*A2*D0 + A0*A1*D0 + A1*A3*D0 + A1*A2*D0 + A2*A3*D0 - A0*A3*C0 - A0*A2*C0 - A0*A1*C0 - A1*A3*C1 - A1*A2*C1 - A2*A3*C2 + A0*A1*A2*C0 + A0*A1*A3*C0 + A0*A2*A3*C0 + A1*A2*A3*C1 - A0*A1*A2*D0 - A0*A1*A3*D0 - A0*A2*A3*D0 - A1*A2*A3*D0 + A0*A1*A2*A3*D0 - A0*A1*A2*A3*C0 There s clearly a pattern here we can easily extrapolate this for any number of layers There is also a balance of additions and subtractions with layer colors and background color

Order dependence D4 = D0 + A0*C0 + A1*C1 + A2*C2 + A3*C3 - A0*D0 - A1*D0 - A2*D0 - A3*D0 - A0*A1*A2*D0 - A0*A1*A3*D0 - A0*A2*A3*D0 - A1*A2*A3*D0 + A0*A1*A2*A3*D0 - A0*A3*C0 - A0*A2*C0 - A0*A1*C0 - A1*A3*C1 - A1*A2*C1 - A2*A3*C2 + A0*A3*D0 + A0*A2*D0 + A0*A1*D0 + A1*A3*D0 + A1*A2*D0 + A2*A3*D0 + A0*A1*A2*C0 + A0*A1*A3*C0 + A0*A2*A3*C0 + A1*A2*A3*C1 - A0*A1*A2*A3*C0 order independent part order dependent part

Order independent Part D4 = D0 + A0*C0 + A1*C1 + A2*C2 + A3*C3 - A0*D0 - A1*D0 - A2*D0 - A3*D0 - A0*A1*A2*D0 - A0*A1*A3*D0 - A0*A2*A3*D0 - A1*A2*A3*D0 + A0*A1*A2*A3*D0 Summation and multiplication are both commutative operations i.e. order doesn t matter A0 + A1 = A1 + A0 A0 * A1 = A1 * A0 A0*C0 + A1*C1 = A1*C1 + A0*C0

Order independent Part D4 = D0 + A0*C0 + A1*C1 + A2*C2 + A3*C3 - A0*D0 - A1*D0 - A2*D0 - A3*D0 - A0*A1*A2*D0 - A0*A1*A3*D0 - A0*A2*A3*D0 - A1*A2*A3*D0 + A0*A1*A2*A3*D0 Highlighted part may not be obvious, but here s the simple proof: = - A0*A1*A2*D0 - A0*A1*A3*D0 - A0*A2*A3*D0 - A1*A2*A3*D0 - D0*A0*A1*A2*A3 * (1/A0 + 1/A1 + 1/A2 + 1/A3)

Order dependent Part D4 = - A0*A3*C0 - A0*A2*C0 - A0*A1*C0 - A1*A3*C1 - A1*A2*C1 - A2*A3*C2 + A0*A3*D0 + A0*A2*D0 + A0*A1*D0 + A1*A3*D0 + A1*A2*D0 + A2*A3*D0 + A0*A1*A2*C0 + A0*A1*A3*C0 + A0*A2*A3*C0 + A1*A2*A3*C1 - A0*A1*A2*A3*C0 These operations depend on order results will vary if transparent layers are reordered proof that proper alpha blending requires sorting

Can we ignore the order dependent part? Do these contribute a lot to the final result of the formula? not if the alpha values are relatively low they re all multiplying alpha values < 1 together even with just 2 layers each with alpha = 0.25 0.25 * 0.25 = 0.0625 which can be relatively insignificant more layers also makes them less important as do darker colors

Error analysis Let s analyze the ignored order dependent part (error) in some easy scenarios all alphas = 0 error = 0 all alphas = 0.25 error = 0.375*D0-0.14453125*C0-0.109375*C1-0.0625*C2 all alphas = 0.5 error = 1.5*D0-0.4375*C0-0.375*C1-0.25*C2 all alphas = 0.75 error = 3.375*D0-0.73828125*C0-0.703125*C1-0.5625*C2 all alphas = 1 error = 6*D0 - C0 - C1 - C2

Simpler is better A smaller part of the formula works much better in practice = D0 + A0*C0 + A1*C1 + A2*C2 + A3*C3 - A0*D0 - A1*D0 - A2*D0 - A3*D0 The balance in the formula is important it maintains the weight of the formula This is much simpler and requires only 2 passes and a single render target 1 less pass and 2 less render targets This formula is also exactly correct when blending a single translucent layer

Error analysis Let s analyze the simpler formula in some easy scenarios all alphas = 0 error simple = 0 error prev = 0 all alphas = 0.25 error simple = 0.31640625*D0-0.14453125*C0-0.109375*C1-0.0625*C2 error prev = 0.375*D0-0.14453125*C0-0.109375*C1-0.0625*C2 all alphas = 0.5 error simple = 1.0625*D0-0.4375*C0-0.375*C1-0.25*C2 error prev = 1.5*D0-0.4375*C0-0.375*C1-0.25*C2 all alphas = 0.75 error simple = 2.00390625*D0-0.73828125*C0-0.703125*C1-0.5625*C2 error prev = 3.375*D0-0.73828125*C0-0.703125*C1-0.5625*C2 all alphas = 1 error simple = 3*D0 - C0 - C1 - C2 error prev = 6*D0 - C0 - C1 - C2

Error comparison Simpler formula actually has less error explains why it looks better This is mainly because of the more balanced formula positives cancelling out negatives source colors cancelling out background color

Does it really work? Little error with relatively low alpha values good approximation Completely inaccurate with higher alpha values Demo can show it much better than text

Sorted, alpha = 0.25

Approx, alpha = 0.25

Sorted, alpha = 0.5

Approx, alpha = 0.5

Implementation We want to implement the order independent part and just ignore the order dependent part D4 = D0 + A0*C0 + A1*C1 + A2*C2 + A3*C3 - A0*D0 - A1*D0 - A2*D0 - A3*D0 - A0*A1*A2*D0 - A0*A1*A3*D0 - A0*A2*A3*D0 - A1*A2*A3*D0 + A0*A1*A2*A3*D0 8 bits per component is not sufficient not enough range or accuracy Use 16 bits per component (64 bits per pixel for RGBA) newer hardware support alpha blending with 64 bpp buffers We can use multiple render targets to compute multiple parts of the equation simultaneously

1 st pass Use additive blending SrcAlphaBlend = 1 DstAlphaBlend = 1 FinalRGBA = SrcRGBA + DstRGBA render target #1, n th layer RGB = An * Cn Alpha = An render target #2, n th layer RGB = 1 / An Alpha = An

1 st pass results After n translucent layers have been blended we get: render target #1: RGB1 = A0 * C0 + A1 * C1 + + An * Cn Alpha1 = A0 + A1 + + An render target #2: RGB2 = 1 / A0 + 1 / A1 + + 1 / An Alpha2 = A0 + A1 + + An

2 nd pass Use multiplicative blending SrcAlphaBlend = 0 DstAlphaBlend = SrcRGBA FinalRGBA = SrcRGBA * DstRGBA render target #3, n th layer RGB = Cn Alpha = An

2 nd pass results After n translucent layers have been blended we get: render target #3: RGB3 = C0 * C1 * * Cn Alpha3 = A0 * A1 * * An This pass isn t really necessary for the better and simpler formula just for completeness

Results We now have the following background D0 render target #1: RGB1 = A0 * C0 + A1 * C1 + + An * Cn Alpha1 = A0 + A1 + + An render target #2: RGB2 = 1 / A0 + 1 / A1 + + 1 / An Alpha2 = A0 + A1 + + An render target #3: RGB3 = C0 * C1 * * Cn Alpha3 = A0 * A1 * * An

Final pass Blend results in a pixel shader RGB1 - D0 * Alpha1 = A0*C0 + A1*C1 + A2*C2 + A3*C3 - D0 * (A0 + A1 + A2 + A3) D0 * Alpha3 = D0 * (A0*A1*A2*A3) D0 * RGB2 * Alpha3 = D0 * (1/A0 + 1/A1 + 1/A2 + 1/A3) * (A0*A1*A2*A3) = D0 * (A1*A2*A3 + A0*A2*A3 + A0*A1*A3 + A0*A1*A2) Sum results with background color (D0) and we get: = D0 + A0*C0 + A1*C1 + A2*C2 + A3*C3 - D0 * (A0 + A1 + A2 + A3) + D0 * (A0*A1*A2*A3) - D0 * (A1*A2*A3 + A0*A2*A3 + A0*A1*A3 + A0*A1*A2) That s the whole sort independent part of the blend formula

Application This technique is best suited for particles too many to sort slight inaccuracy in their color shouldn t matter too much Not so good for very general case, with all ranges of alpha values For general case, works best with highly translucent objects i.e. low alpha values

Can we do better? I hope so Keep looking at the order dependent part of the formula to see if we can find more order independent parts out of it D4 = D0 + A0*C0 + A1*C1 + A2*C2 + A3*C3 - A0*D0 - A1*D0 - A2*D0 - A3*D0 - A0*A1*A2*D0 - A0*A1*A3*D0 - A0*A2*A3*D0 - A1*A2*A3*D0 + A0*A1*A2*A3*D0 - A0*A3*C0 - A0*A2*C0 - A0*A1*C0 - A1*A3*C1 - A1*A2*C1 - A2*A3*C2 + A0*A3*D0 + A0*A2*D0 + A0*A1*D0 + A1*A3*D0 + A1*A2*D0 + A2*A3*D0 + A0*A1*A2*C0 + A0*A1*A3*C0 + A0*A2*A3*C0 + A1*A2*A3*C1 - A0*A1*A2*A3*C0 Or use a completely different algorithm

Q&A If you have any other ideas or suggestions I d love to hear them email: hmeshkin@perpetual.com