ebook img

Practical GPU Graphics with wgpu and Rust PDF

471 Pages·2021·21.591 MB·English
by  Jack Xu
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Practical GPU Graphics with wgpu and Rust

Practical GPU Graphics with wgpu and Rust YouTube(cid:3)Channel:(cid:3)Practical(cid:3)Programming(cid:3)with(cid:3)Dr.(cid:3)Xu(cid:3) Practical GPU Graphics with wgpu and Rust Creating Advanced GPU Graphics on Native Devices and the Web in Rust Using wgpu – the Next-Generation Graphics API Jack Xu, PhD UniCAD Publishing Practical GPU Graphics with wgpu and Rust Copyright © 2021 by Jack Xu, PhD Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1UC Editor: Anna Xu All rights reserved. No part of the contents of this book and corresponding example source code may be reproduced or transmitted in any form or by any means without the written permission of the author. The author has made every effort in the preparation of this book to ensure the accuracy of the information; however, this book is sold without warranty, either express or implied. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained in the book. Published by UniCAD Publishing. New York, USA ISBN: 979-8404949377 Publisher’s Cataloging-in-Publication Data Xu, Jack Practical GPU Graphics with wgpu and Rust: Creating Advanced GPU Graphics on Native Devices and the Web in Rust Using wgpu – the Next-Generation Graphics API / Jack Xu – 1st ed. p.cm. ISBN 979-8404949377 1. wgpu. 2. Rust. 3. WebGPU. 4. Shader. 5. GPU Acceleration. 6. Graphics on Web. 7. SPIR-V. 8. WebGL. 9. WebAssembly. 10. WASM. 11. Graphics Rendering. 12. GPU Computing. I. Title. II. Title. III Title: Practical GPU Graphics with wgpu and Rust Contents Contents .................................................................................................... v Introduction ............................................................................................. 1 Overview ............................................................................................................... 1 What this Book Includes........................................................................................ 3 Is this Book for You?............................................................................................. 3 What Do You Need to Use this Book? .................................................................. 4 How this Book Is Organized ................................................................................. 4 Using Code Examples ........................................................................................... 6 Customer Support .................................................................................................. 6 1 Setting Up Development Tools ......................................................... 7 1.1 Hardware Requirements ................................................................................. 7 1.2 Install C++ Build Tools................................................................................ 10 1.3 Install Rust ................................................................................................... 10 1.4 Install Visual Studio Code ........................................................................... 11 1.5 Run a Rust Application ................................................................................ 11 1.6 Configuration ............................................................................................... 12 1.7 Creating a Window ...................................................................................... 15 1.8 Rust Basics ................................................................................................... 15 1.8.1 Data Types ..................................................................................... 17 1.8.2 Functions ........................................................................................ 17 vi| Practical GPU Graphics with wgpu and Rust 1.8.3 Control Flows ................................................................................. 18 1.8.4 Arrays, Vectors, and Slices ............................................................ 21 1.8.5 Structs ............................................................................................ 22 1.8.6 Enums ............................................................................................ 23 1.8.7 Generics ......................................................................................... 24 2 wgpu Basics ...................................................................................... 25 2.1 First wgpu Example ..................................................................................... 25 2.1.1 WGSL Shaders ............................................................................... 25 2.1.2 Common Rust Code ....................................................................... 26 2.1.3 Rust Main Function ........................................................................ 28 2.2 wgpu API ..................................................................................................... 30 2.2.1 wgpu Backend ................................................................................ 30 2.2.2 Surface ........................................................................................... 31 2.2.3 Load Shaders .................................................................................. 32 2.2.4 Rendering Pipeline ......................................................................... 32 2.2.5 Rendering Output ........................................................................... 34 2.3 Shader Program ............................................................................................ 35 2.3.1 Why Use WGSL Shaders? ............................................................. 36 2.3.2 Writing Shader Code ...................................................................... 36 2.4 Triangle with Different Vertex Colors ......................................................... 38 2.4.1 Shader Code ................................................................................... 39 2.4.2 Rust Code ....................................................................................... 39 3 wgpu Primitives ............................................................................... 41 3.1 Creating Points ............................................................................................. 42 3.1.1 Rust Code ....................................................................................... 42 3.1.2 Shader Code ................................................................................... 43 3.1.3 Run Application ............................................................................. 44 3.2 Creating Lines .............................................................................................. 45 3.3 Creating Triangles ........................................................................................ 47 3.3.1 Rust Code ....................................................................................... 47 3.3.2 Shader Code ................................................................................... 48 3.3.3 Run Application ............................................................................. 49 4 GPU Buffers ..................................................................................... 51 4.1 GPU Buffer .................................................................................................. 51 4.2 Creating a Colored Triangle ......................................................................... 52 4.2.1 Rust Code ....................................................................................... 53 Contents |vii 4.2.2 Shader Code ................................................................................... 60 4.2.3 Run Application ............................................................................. 60 4.3 Creating a Colored Square ........................................................................... 61 4.3.1 Rust Code ....................................................................................... 62 4.3.2 Run Application ............................................................................. 62 4.4 Creating a Square with an Index Buffer ....................................................... 63 4.4.1 Rust Code ....................................................................................... 63 4.4.2 Run Application ............................................................................. 65 5 3D Transformations ........................................................................ 67 5.1 Basics of 3D Matrices and Transformations ................................................ 67 5.1.1 Introducing cgmath ........................................................................ 67 5.1.2 3D Vector and Matrix Operations .................................................. 68 5.1.3 Scaling ............................................................................................ 69 5.1.4 Translation ..................................................................................... 71 5.1.5 Rotation .......................................................................................... 72 5.1.6 Combining Transformations .......................................................... 74 5.2 Projections and Viewing .............................................................................. 75 5.2.1 Transforming Coordinates ............................................................. 75 5.2.2 Viewing Transform ........................................................................ 76 5.2.3 Perspective Projection .................................................................... 79 5.2.4 Orthographic Projection ................................................................. 83 5.3 Transformations in wgpu.............................................................................. 86 6 3D Shapes and Camera ................................................................... 87 6.1 Uniform Buffers and Bind Groups ............................................................... 87 6.2 Creating a 3D Line ....................................................................................... 89 6.2.1 Common Code ............................................................................... 89 6.2.2 Rust Code ....................................................................................... 92 6.2.3 Shader Program .............................................................................. 98 6.2.4 Run Application ............................................................................. 98 6.3 Creating a Cube with Distinct Face Colors ................................................ 100 6.3.1 Create Vertex Data ....................................................................... 100 6.3.2 Rust Code ..................................................................................... 102 6.3.3 Shader Program ............................................................................ 109 6.3.4 Run Application ........................................................................... 110 6.4 Creating a Cube with Distinct Vertex Colors ............................................ 111 6.4.1 Create Vertex Data ....................................................................... 111 6.4.2 Rust Code ..................................................................................... 111 viii| Practical GPU Graphics with wgpu and Rust 6.4.3 Run Application ........................................................................... 118 6.5 Rotating Objects ......................................................................................... 119 6.5.1 Rust Code ..................................................................................... 119 6.5.2 Run Application ........................................................................... 120 6.6 Camera Controls......................................................................................... 121 6.6.1 Camera Code ................................................................................ 121 6.6.2 Rust Code ..................................................................................... 123 6.6.3 Run Application ........................................................................... 130 7 3D Wireframe Shapes ................................................................... 131 7.1 Common Code ........................................................................................... 131 7.2 Cube Wireframe ......................................................................................... 136 7.2.1 Rust File ....................................................................................... 136 7.2.2 Run Application ........................................................................... 137 7.3 Sphere Wireframe ...................................................................................... 138 7.3.1 Spherical Coordinate System ....................................................... 138 7.3.2 Rust Code ..................................................................................... 140 7.3.3 Run Application ........................................................................... 141 7.4 Cylinder Wireframe ................................................................................... 141 7.4.1 Cylindrical Coordinate System .................................................... 142 7.4.2 Rust Code ..................................................................................... 143 7.4.3 Run Application ........................................................................... 145 7.5 Cone Wireframe ......................................................................................... 146 7.5.1 Rust Code ..................................................................................... 147 7.5.2 Run Application ........................................................................... 148 7.6 Torus Wireframe ........................................................................................ 149 7.6.1 Rust Code ..................................................................................... 150 7.6.2 Run Application ........................................................................... 151 8 Lighting in WGPU ........................................................................ 153 8.1 Light Components ...................................................................................... 153 8.2 Normal Vectors .......................................................................................... 154 8.2.1 Surface Normal of a Cube ............................................................ 155 8.2.2 Surface Normal of a Sphere ......................................................... 155 8.2.3 Surface Normal of a Cylinder ...................................................... 155 8.2.4 Surface Normal of a Polyhedral Surface...................................... 155 8.3 Lighting Calculation................................................................................... 156 8.3.1 Diffuse Light ................................................................................ 156 8.3.2 Specular Light .............................................................................. 157 Contents |ix 8.4 Lighting in Shaders .................................................................................... 158 8.4.1 Transform Normals ...................................................................... 158 8.4.2 Shader with Lighting .................................................................... 159 8.5 Common Code ........................................................................................... 161 8.6 Cube with Lighting .................................................................................... 170 8.6.1 Rust Code ..................................................................................... 170 8.6.2 Run Application ........................................................................... 171 8.7 Sphere with Lighting .................................................................................. 171 8.7.1 Vertex and Normal Data .............................................................. 172 8.7.2 Rust Code ..................................................................................... 173 8.7.3 Run Application ........................................................................... 174 8.8 Cylinder with Lighting ............................................................................... 174 8.8.1 Vertex and Normal Data .............................................................. 175 8.8.2 Rust Code ..................................................................................... 177 8.8.3 Run Application ........................................................................... 177 8.9 Cone with Lighting .................................................................................... 178 8.9.1 Vertex and Normal Data .............................................................. 179 8.9.2 Rust Code ..................................................................................... 180 8.9.3 Run Application ........................................................................... 181 8.10 Torus with Lighting ................................................................................... 181 8.10.1 Vertex and Normal Data .............................................................. 182 8.10.2 Rust Code ..................................................................................... 183 8.10.3 Run Application ........................................................................... 183 9 Colormaps and 3D Surfaces ......................................................... 185 9.1 Color Models .............................................................................................. 185 9.2 Colormaps .................................................................................................. 186 9.2.1 Colormap Data ............................................................................. 186 9.2.2 Color Interpolation ....................................................................... 187 9.3 Shaders with Lighting and Vertex Color ................................................... 188 9.4 Simple 3D Surfaces .................................................................................... 190 9.4.1 Position, Normal, and Color Data ................................................ 191 9.4.2 Common Code ............................................................................. 193 9.4.3 Sinc Surface ................................................................................. 201 9.4.4 Peaks Surface ............................................................................... 203 9.5 Parametric 3D Surfaces .............................................................................. 205 9.5.1 Vertex and Normal Data .............................................................. 205 9.5.2 Klein Bottle .................................................................................. 206 9.5.3 Wellenkugel Surface .................................................................... 209

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.