Gritz SIG2011course OSL Safe

56
Compiler Technology in Open Shading Language Larry Gritz Sony Pictures Imageworks Monday, August 15, 2011

description

Gritz-SIG2011course-OSL-safe.pdf

Transcript of Gritz SIG2011course OSL Safe

Page 1: Gritz SIG2011course OSL Safe

Compiler Technology inOpen Shading Language

Larry GritzSony Pictures Imageworks

Monday, August 15, 2011

Page 2: Gritz SIG2011course OSL Safe

sony pictures

Open Shading Language (OSL)•Designed for physically-based GI•Scales to production use•A language spec that could be used by any renderer•A library that can be embedded in CPU renderers•Open source•In production now!

Monday, August 15, 2011

Page 3: Gritz SIG2011course OSL Safe

sony pictures

Motivation•(Alice in Wonderland images omitted)

Monday, August 15, 2011

Page 4: Gritz SIG2011course OSL Safe

sony pictures

What’s wrong with shaders•Black boxes, can’t reason about them•Can’t sample, defer, or reorder•Suboptimal for a modern ray tracer•Units are sloppy, hard to be physically correct•If C/C++: difficult, versionitis, can crash, hard to globally optimize.

•Hardware dependence & limitations

Monday, August 15, 2011

Page 5: Gritz SIG2011course OSL Safe

sony pictures

Radiance closures•OSL shaders don’t return colors•Return a symbolic rep that can be “run” later

–Act “as if” they are a radiance value–But aren’t evaluated until later

•View independent•Consistent units (radiance)•Can be sampled•Unify reflection, transparency, emission

Monday, August 15, 2011

Page 6: Gritz SIG2011course OSL Safe

sony pictures

OSL goals•Similar to RSL/GSL, but evolved & easier•Separate description vs implementation

•End versionitis nightmare•Late-stage optimization•Hide renderer internals

•Renderer control of rays / physical shading• no light loops or trace calls

•Lazy running of layers•Closures describe materials/lights•Automatic differentiation

•No crashing, NaN, etc.•Allow multiple back-ends

Monday, August 15, 2011

Page 7: Gritz SIG2011course OSL Safe

sony pictures

System workflow•Compiler (oslc) precompiles individual source modules (.osl) to bytecode (.oso)

•At render time, material networks are assembled•JIT to x86 to execute•OSL runtime execution is a library•Renderer provides a callback interface

Monday, August 15, 2011

Page 8: Gritz SIG2011course OSL Safe

sony pictures

Compiling shaders

shader gamma (color Cin = 1, float gam = 1, output color Cout = 1){ Cout = pow (Cin, 1/gam);}

gamma.osl

OpenShadingLanguage 1.00# Compiled by oslc 0.6.0shader gammaparam color Cin 1 1 1param float gam 1oparam color Cout 1 1 1temp float $tmp1const float $const2 1code ___main___# gamma.osl:5 div $tmp1 $const2 gam pow Cout Cin $tmp1 end

gamma.oso

Monday, August 15, 2011

Page 9: Gritz SIG2011course OSL Safe

sony pictures

Shader networks

layer 5: "wood1"

texturemap

name

s

t

Cout

texturemap

name

s

t

Cout

layer 3: "gam1"

gamma

Cin Cout

gam2.2

layer 4: "gam2"

gamma

Cin Cout

gam1.0

rings

grain

wood

Cilayer 2: "tex2"

layer 1: "tex1"

"rings.tx"

"grain.tx"

Monday, August 15, 2011

Page 10: Gritz SIG2011course OSL Safe

sony pictures

Interpreter vs LLVM•First try: SIMD interpreter

–render batches of points at once–interpret one instruction at a time, all points in lockstep–analyze to find uniform values–amortize overhead over the grid

Monday, August 15, 2011

Page 11: Gritz SIG2011course OSL Safe

sony pictures

Interpreter vs LLVM•Works great if batches are big enough•Easy for primary rays, secondary rays incoherent•Batches small, too much overhead cohering

Monday, August 15, 2011

Page 12: Gritz SIG2011course OSL Safe

sony pictures

Interpreter vs LLVM•Next try: translate oso into LLVM IR, JIT

•no exploitation of ‘uniform’ values•but no interpreter overhead•no need to try to scrape together coherent rays•LLVM optimizer

•Generate full IR for some ops•Others “call” functions, inlined by LLVM•Generate enter/exit code•Lazy evaluation of shader nodes

Monday, August 15, 2011

Page 13: Gritz SIG2011course OSL Safe

sony pictures

Interpreter vs LLVM•LLVM vastly outperformed interpreter•Greatly simplified the entire system

–other than LLVM dependency

•Simplified renderer, no need for batches

Monday, August 15, 2011

Page 14: Gritz SIG2011course OSL Safe

sony pictures

DerivativesC = texture (“foo.exr”, s, t, ...)

•To properly filter this texture lookup, you want to know how s & t vary over a pixel area.

•dsdx, dsdy, dtdx, dtdy

Monday, August 15, 2011

Page 15: Gritz SIG2011course OSL Safe

sony pictures

Derivatives•Most renderers calculate derivatives by:

• Ignoring the problem•Having “special” texture coordinates

Monday, August 15, 2011

Page 16: Gritz SIG2011course OSL Safe

sony pictures

Derivatives•Most renderers calculate derivatives by:

• Ignoring the problem•Having “special” texture coordinates•Computing on grids (Reyes)

Monday, August 15, 2011

Page 17: Gritz SIG2011course OSL Safe

sony pictures

Derivatives•Most renderers calculate derivatives by:

• Ignoring the problem•Having “special” texture coordinates•Computing on grids (Reyes)•Shade rays as 3 point grids (Gritz, JGT ‘96)

Monday, August 15, 2011

Page 18: Gritz SIG2011course OSL Safe

sony pictures

Derivatives•Most renderers calculate derivatives by:

• Ignoring the problem•Having “special” texture coordinates•Computing on grids (Reyes)•Shade rays as 3 point grids (Gritz, JGT ‘96)

•We don’t have grids•We don’t want to compute extra points•We want derivs of arbitrary expressions

Monday, August 15, 2011

Page 19: Gritz SIG2011course OSL Safe

sony pictures

Automatic differentiation•Use dual arithmetic (Piponi, JGT 2004)•Each variable can carry d/dx and d/dy differentials: x = {val, dx, dy}

•Define math ops on these dual variables

Monday, August 15, 2011

Page 20: Gritz SIG2011course OSL Safe

sony pictures

Automatic differentiation

template<class T>Dual2<T> operator* (const Dual2<T> &a,

const Dual2<T> &b){ return Dual2<T> (a.val()*b.val(), a.val()*b.dx() + a.dx()*b.val(), a.val()*b.dy() + a.dy()*b.val());}

Monday, August 15, 2011

Page 21: Gritz SIG2011course OSL Safe

sony pictures

Only some symbols need derivs•Find all data dependencies

•add R, A, B ➞ R depends on A and B• “w” args to an op depend on all the “r” args to that op

•Only some ops take derivs of their argsaastep, area, displace, Dx, Dy, environment, texture

•Mark those symbols as “needing derivatives”•And so on for their dependencies...•Careful about connected shader parameters

Monday, August 15, 2011

Page 22: Gritz SIG2011course OSL Safe

sony pictures

Derivative ops•Now we know which symbols need derivs

–Renderer supplies derivs of (P, u, v, interpolated vars)

•Ops involving them generate deriv IR–shortcut: if the w args of an op don’t need derivs, just do the non-deriv computations

•In practice, ~5% of symbols need to carry derivs•Total execution cost of arbitrary derivs is <10%

Monday, August 15, 2011

Page 23: Gritz SIG2011course OSL Safe

sony pictures

Runtime optimization•At runtime, we know:

–layout and connectivity of the shader network–parameter values

•So we optimize the shader oso right before LLVM IR

Monday, August 15, 2011

Page 24: Gritz SIG2011course OSL Safe

sony pictures

Runtime optimization

•Unconnected, uninterpolated params ➞ constants–also connected if upstream layer knows output value

Monday, August 15, 2011

Page 25: Gritz SIG2011course OSL Safe

sony pictures

Track “aliasing” within blocks

•Until A is reassigned, or control flow

•This lets us treat a lot of variables as if they were constant within a basic block.

Monday, August 15, 2011

Page 26: Gritz SIG2011course OSL Safe

sony pictures

Track “aliasing” within blocks

assign A $constB (now we know A’s value)

•Until A is reassigned, or control flow

•This lets us treat a lot of variables as if they were constant within a basic block.

Monday, August 15, 2011

Page 27: Gritz SIG2011course OSL Safe

sony pictures

Track “aliasing” within blocks

assign A $constB (now we know A’s value)

assign A B (now we know A == B)

•Until A is reassigned, or control flow

•This lets us treat a lot of variables as if they were constant within a basic block.

Monday, August 15, 2011

Page 28: Gritz SIG2011course OSL Safe

sony pictures

Constant folding

Monday, August 15, 2011

Page 29: Gritz SIG2011course OSL Safe

sony pictures

Constant folding

add A $constB $constC assign A $constD

Monday, August 15, 2011

Page 30: Gritz SIG2011course OSL Safe

sony pictures

Constant folding

add A $constB $constC assign A $constD

add A B $const0 assign A B

Monday, August 15, 2011

Page 31: Gritz SIG2011course OSL Safe

sony pictures

Constant folding

add A $constB $constC assign A $constD

add A B $const0 assign A B

div A A $const1 nop

Monday, August 15, 2011

Page 32: Gritz SIG2011course OSL Safe

sony pictures

Constant folding

add A $constB $constC assign A $constD

add A B $const0 assign A B

div A A $const1 nop

mul A B $const0 assign A $const0

Monday, August 15, 2011

Page 33: Gritz SIG2011course OSL Safe

sony pictures

Useless op elimination

Monday, August 15, 2011

Page 34: Gritz SIG2011course OSL Safe

sony pictures

Useless op elimination

add A A 0 nop

Monday, August 15, 2011

Page 35: Gritz SIG2011course OSL Safe

sony pictures

Useless op elimination

add A A 0 nop

add A A C nopsub A A C

Monday, August 15, 2011

Page 36: Gritz SIG2011course OSL Safe

sony pictures

Useless op elimination

add A A 0 nop

add A A C nopsub A A C

assign A B nop (B is an alias of A)

Monday, August 15, 2011

Page 37: Gritz SIG2011course OSL Safe

sony pictures

Useless op elimination

add A A 0 nop

add A A C nopsub A A C

assign A B nop (B is an alias of A)

assign A B nop (A & B have the same value)

Monday, August 15, 2011

Page 38: Gritz SIG2011course OSL Safe

sony pictures

Runtime optimization

Monday, August 15, 2011

Page 39: Gritz SIG2011course OSL Safe

sony pictures

Runtime optimization•Dead code elimination

–entire conditionals, loops–assignments to variables that aren’t used again

Monday, August 15, 2011

Page 40: Gritz SIG2011course OSL Safe

sony pictures

Runtime optimization•Dead code elimination

–entire conditionals, loops–assignments to variables that aren’t used again

•Dead variable elimination

Monday, August 15, 2011

Page 41: Gritz SIG2011course OSL Safe

sony pictures

Runtime optimization•Dead code elimination

–entire conditionals, loops–assignments to variables that aren’t used again

•Dead variable elimination•Dead shader parameter/output elimination

Monday, August 15, 2011

Page 42: Gritz SIG2011course OSL Safe

sony pictures

Runtime optimization•Dead code elimination

–entire conditionals, loops–assignments to variables that aren’t used again

•Dead variable elimination•Dead shader parameter/output elimination•Dead shader layer elimination

Monday, August 15, 2011

Page 43: Gritz SIG2011course OSL Safe

sony pictures

Runtime optimization•Dead code elimination

–entire conditionals, loops–assignments to variables that aren’t used again

•Dead variable elimination•Dead shader parameter/output elimination•Dead shader layer elimination•Coalesce temporaries with nonoverlapping lifetimes

Monday, August 15, 2011

Page 44: Gritz SIG2011course OSL Safe

sony pictures

Runtime optimization results

Monday, August 15, 2011

Page 45: Gritz SIG2011course OSL Safe

sony pictures

Runtime optimization results•Reduce code & symbols 95-98% before LLVM

–IR gen, LLVM opt, JIT in seconds, not minutes–LLVM also optimizes its IR

Monday, August 15, 2011

Page 46: Gritz SIG2011course OSL Safe

sony pictures

Runtime optimization results•Reduce code & symbols 95-98% before LLVM

–IR gen, LLVM opt, JIT in seconds, not minutes–LLVM also optimizes its IR

Monday, August 15, 2011

Page 47: Gritz SIG2011course OSL Safe

sony pictures

Runtime optimization results•Reduce code & symbols 95-98% before LLVM

–IR gen, LLVM opt, JIT in seconds, not minutes–LLVM also optimizes its IR

•20-25% faster execution than old C shaders–and safe! (no buffer overflows, crashes, etc.)

Monday, August 15, 2011

Page 48: Gritz SIG2011course OSL Safe

sony pictures

Putting it all together•(“The Amazing Spider-Man” shot omitted, sorry.)

Monday, August 15, 2011

Page 49: Gritz SIG2011course OSL Safe

sony pictures

Some stats: frame 1350•43 different shader masters (distinct .osl/oso)•1885 shader groups (materials)•140,964 shader instances (master + params)•average 75 instances per group•Load, runtime opt, LLVM IR/opt/JIT:

–5m22s across all threads (~26s per thread)–out of a 3h22:00 render with 12 threads–aside: more time assembling/loading than rendering

Monday, August 15, 2011

Page 50: Gritz SIG2011course OSL Safe

sony pictures

Some stats: frame 1350•Typical shader group pre-optimized:

–50-100k ops–20-40k symbols (including temporaries)

•After runtime optimization:–1k-5k ops–100-2k symbols–many shader groups eliminated entirely

Monday, August 15, 2011

Page 51: Gritz SIG2011course OSL Safe

sony pictures

Some stats: frame 1350•Texture:

–497M texture queries (each of which is a bicubic mipmap lookup, more when anisotropic)

–~9500 textures (~6700 with unique texels)–700 GB of texture referenced (not counting dupes)–Runtime memory: 500 MB cache–www.openimageio.org

Monday, August 15, 2011

Page 52: Gritz SIG2011course OSL Safe

sony pictures

This is already old•I’ve seen shader groups with 1.5M ops•Not uncommon for >> 1 TB texture referenced

Monday, August 15, 2011

Page 53: Gritz SIG2011course OSL Safe

sony pictures

Where are we?•Our shader library is converted•Our shader writers are exclusively writing OSL•All new shows using OSL

–The Amazing Spider-Man–Men in Black 3–Oz, the Great and Powerful–other things I can’t say

•We’ve ripped out support for C shaders

Monday, August 15, 2011

Page 54: Gritz SIG2011course OSL Safe

sony pictures

Open source•opensource.imageworks.com•github.com/imageworks/OpenShadingLangage•“New BSD” license•This is really our live development tree!

Monday, August 15, 2011

Page 55: Gritz SIG2011course OSL Safe

sony pictures

Main takeaways•Small domain-specific language•Separate implementation from description•LLVM to JIT native code at runtime•Extensive runtime optimization when network and parameters are known

•Outperforms compiled C shaders•Open source

Monday, August 15, 2011

Page 56: Gritz SIG2011course OSL Safe

sony pictures

Acknowledgements / Q&A•OSL developers: Cliff Stein, Chris Kulla, Alejandro Conty, Solomon Boulos

•SPI renderer, shader teams, SW management•Participants on the mail list

•Contact: opensource.imageworks.com [email protected]

Monday, August 15, 2011