Table Of ContentMetaprogramming GPUs
with Sh
Metaprogramming GPUs
with Sh
Michael McCool
Stefanus Du Toit
University of Waterloo, Ontario, Canada
Boca Raton London New York
CRC Press is an imprint of the
Taylor & Francis Group, an informa business
AN A K PETERS BOOK
CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
First issued in hardback 2019
© 2004 by Taylor & Francis Group, LLC
CRC Press is an imprint ofTaylor & Francis Group, an Informa business
No claim to original U.S. Government works
ISBN-13: 978-1-56881-229-8 (pbk)
ISBN-13: 978-1-138-42809-6 (hbk)
This book contains information obtained from authentic and highly regarded sources.
Reasonable efforts have been made to publish reliable data and information, but the author
and publisher cannot assume responsibility for the validity of all materials or the
consequences of their use. The authors and publishers have attempted to trace the copyright
holders of all material repraduced in this publication and apologize to copyright holders if
permission to publish in this form has not been obtained. If any copyright material has not
been acknowledged please write and let us know so we may rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted,
reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other
means, now known or hereafter invented, including photocopying, microfilming, and
recording, or in any information storage or retrieval system, without written permission fram
the publishers.
For permission to photocopy or use material electranically from this work, please access
www.copyright.com (http://www.copyright.com/l or contact the Copyright Clearance Center,
Inc. (CCe), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit
organization that provides licenses and registration for a variety of users. For organizations
that have been granted a photocopy license by the CCC, a separate system of payment has
been arranged.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks,
and are used only for identification and explanation without intent to infringe.
Visit the Taylor & Francis Web site at
http://www.taylorandfrancis.com
and the CRC Press Web site at
http://www.crcpress.com
Library of Congress Cataloging-in-Publication Data
Congress Contral Number is 2004053399.
Contents
List of Tables xi
List of Listings XV
Preface xvii
I Introduction 1
1 Overview 3
1.1 Programmable GPUs............................................................................ 8
1.2 Shading Languages............................................................................... 10
1.3 Stream Processing.................................................................................. 13
1.4 Metaprogramming............................................................................... 16
1.5 The Sh Metaprogramming Toolkit ................................................... 19
1.5.1 Immediate M ode...................................................................... 20
1.5.2 Stream Programming ............................................................. 20
1.5.3 Shader Programming ............................................................. 24
1.6 Conventions and Notation.................................................................. 27
1.6.1 Typesetting and Stylistic Conventions................................. 27
1.6.2 Coordinate Systems ............................................................... 28
1.6.3 Standard Vectors and Variable Names................................. 29
1.6.4 Global Parameters................................................................... 32
1.7 License, History, and Access................................................................ 35
1.8 Status and Future Development.......................................................... 36
1.8.1 Beta Release Features............................................................... 37
1.8.2 Year-End Features ................................................................... 38
1.8.3 Future Features......................................................................... 40
v
Tuples, Matrices, Operators, and Functions 43
2.1 Tuples..................................................................... 43
2.2 Operators and Library Functions..................... 44
2.3 M atrices............................................................... 46
2.4 Functions............................................................... 47
2.4.1 Example: Glass Support Functions . . . 47
2.4.2 Example: Reflectance Models............... 50
Programs, Parameters, and Attributes 63
3.1 Programs............................................................... 63
3.2 Example: Blinn-Phong Point-Source Lighting . 64
3.2.1 Vertex Shader.......................................... 64
3.2.2 Fragment Shader.................................... 67
3.2.3 Modularity ............................................. 68
3.2.4 Shader Framework................................. 71
3.3 Shader Algebra................................................... 72
3.4 Example: Surfaces, Lights, and Postprocessing 74
3.5 Example: Metaprogrammed CSG Text............ 77
3.6 Control Constructs............................................. 83
3.7 Example: Julia Set................................................ 84
Arrays and Textures 89
4.1 Texture Types...................................................... 89
4.2 Example: Texture Mapped Blinn-Phong . . . . 91
4.3 Example: Encapsulated BRDF Representation . 93
4.4 Example: Shiny Bump M ap.............................. 96
4.4.1 Vertex Shader.......................................... 97
4.4.2 Fragment Shader.................................... 98
4.5 Example: G lass................................................... 98
4.6 Example: Lucite................................................... 100
4.7 Example: Wood and Marble.............................. 102
4.7.1 Parameters................................................ 103
4.7.2 Vertex Shader.......................................... 104
4.7.3 Fragment Shader.................................... 104
4.8 Example: Worley Noise Shaders ..................... 105
4.9 Example: Wood from Phong.............................. 106
Streams and Channels 119
5.1 Channels............................................................... 119
5.2 Combining Channels into Streams.................. 119
Contents vii
5.3 Applying Programs to Streams............................................................120
5.4 Example: Particle Systems.....................................................................121
II Reference 127
6 Reference Overview 129
6.1 Setup.........................................................................................................129
6.2 Sample OpenGL GLUT Application...................................................130
6.3 The shrike Testbed............................................................... 134
6.4 API Documentation.................................................................................135
7 Types 137
7.1 Programs...................................................................................................137
7.2 Tuples.........................................................................................................138
7.2.1 Semantic Types...........................................................................138
7.2.2 Storage Types..............................................................................139
7.2.3 Binding Types..............................................................................141
7.2.4 Template Declaration..................................................................142
7.3 M atrices...................................................................................................144
7.4 Arrays, Tables, and Textures..................................................................145
7.5 Contexts...................................................................................................146
7.6 Exceptions and Error Handling............................................................146
7.7 Metadata...................................................................................................146
7.7.1 Object Names..............................................................................148
8 Standard Operators and Functions 151
8.1 Swizzling and Write Masking...............................................................153
8.2 Arithmetic................................................................................................156
8.3 Derivatives................................................................................................159
8.4 Noise.........................................................................................................161
8.5 Trigonometric and Exponential Functions..........................................163
8.6 Interpolation and Approximation.........................................................165
8.7 Geometry...................................................................................................166
8.8 Linear Algebra..........................................................................................167
8.9 Logical and Comparison Functions......................................................171
8.10 Discontinuities..........................................................................................173
8.11 Miscellaneous Functions........................................................................173
viii Contents
9 Arrays, Tables and Textures 177
9.1 Texture Formats......................................................................................178
9.2 Trait Modifiers.................................................. 180
9.3 Texture Memory and Storage..............................................................180
9.4 Texture and Array Access....................................................................181
10 Programs and Streams 183
10.1 Defining Programs................................................................................183
10.1.1 Compiling Programs.................................................................184
10.2 Binding Programs and D ata.................................................................185
10.2.1 Binding Programs ....................................................................185
10.2.2 Binding and Updating Data.....................................................186
10.2.3 Unbinding...................................................................................186
10.2.4 Querying Binding State.................... 186
10.3 Introspection............................................................................................187
10.4 Algebra.....................................................................................................189
10.4.1 Connection ................................................................................190
10.4.2 Combination .............................................................................190
10.5 Nibbles................................ 191
10.5.1 Interface Adaptation and Specialization................................191
10.5.2 Passthrough................................................................................192
10.5.3 Texture Access ..........................................................................193
10.5.4 Type and Size Conversion........................................................193
10.5.5 Transformations .......................................................................194
10.5.6 Basis Conversion.......................................................................194
10.5.7 Primitive Computations...........................................................195
10.6 Manipulators .........................................................................................196
10.6.1 Fixed Manipulators....................................................................197
10.6.2 Expandable Manipulators................................... 197
10.7 Kernels.....................................................................................................198
10.7.1 Universal Vertex Shader Kernel...............................................199
10.7.2 Surface Shader Kernels..............................................................201
10.7.3 Light Shader Kernels.................................................................203
10.7.4 Surface Map Kernels.................................................................205
10.7.5 Postprocessing Kernels ...........................................................206
10.8 Streams.....................................................................................................206
Contents ix
11 Control Constructs 209
11.1 Selection...................................................................................................210
11.1.1 IF .................................................................................................211
11.1.2 WHEN........................................................................................212
11.2 Iteration ...................................................................................................212
11.2.1 FOR .............................................................................................213
11.2.2 WHILE..........................................................................................214
11.2.3 DO/UNTIL.................................................................................214
11.2.4 BREAK/CONTINUE..................................................................214
12 Backends 217
12.1 OpenGL Backends.................................................................................218
12.1.1 Input and Output Binding.........................................................218
12.1.2 Texture Bindings........................................................................219
12.1.3 Parameter Bindings ..................................................................220
12.2 CPU Backends..........................................................................................220
12.3 The Sm Backend.......................................................................................221
III Engineering 223
13 Engineering Overview 225
13.1 Code Organization and Build System ................................................226
13.2 Template Instantiation...........................................................................227
14 Intermediate Representation 229
14.1 Reference Counting.................................................................................229
14.2 Basic Blocks.............................................................................................230
14.2.1 Special Statements.....................................................................234
14.3 The Control-Row Graph........................................................................235
14.4 Parsing......................................................................................................236
14.4.1 Basic Blocks.................................................................................236
14.4.2 Control Constructs.....................................................................237
15 Streams and Textures 241
15.1 Stream Representation...........................................................................241
15.2 Texture Representation...........................................................................242
15.3 Stream and Texture Memory..................................................................242
15.3.1 Memory and Storage Objects...................................................242
15.3.2 Finding Storages........................................................................244