You are previewing OpenGL® Programming Guide: The Official Guide to Learning OpenGL®, Version 4.5 with SPIR-V, Ninth Edition.
O'Reilly logo
OpenGL® Programming Guide: The Official Guide to Learning OpenGL®, Version 4.5 with SPIR-V, Ninth Edition

Book Description

Complete Coverage of OpenGL® 4.5—the Latest Version (Includes 4.5, 4.4, SPIR-V, and Extensions)

The latest version of today’s leading worldwide standard for computer graphics, OpenGL 4.5 delivers significant improvements in application efficiency, flexibility, and performance. OpenGL 4.5 is an exceptionally mature and robust platform for programming high-quality computer-generated images and interactive applications using 2D and 3D objects, color images, and shaders.

OpenGL® Programming Guide, Ninth Edition, presents definitive, comprehensive information on OpenGL 4.5, 4.4, SPIR-V, OpenGL extensions, and the OpenGL Shading Language. It will serve you for as long as you write or maintain OpenGL code.

This edition of the best-selling “Red Book” fully integrates shader techniques alongside classic, function-centric approaches, and contains extensive code examples that demonstrate modern techniques. Starting with the fundamentals, its wide-ranging coverage includes drawing, color, pixels, fragments, transformations, textures, framebuffers, light and shadow, and memory techniques for advanced rendering and nongraphical applications. It also offers discussions of all shader stages, including thorough explorations of tessellation, geometric, and compute shaders.

New coverage in this edition includes

  • Thorough coverage of OpenGL 4.5 Direct State Access (DSA), which overhauls the OpenGL programming model and how applications access objects

  • Deeper discussions and more examples of shader functionality and GPU processing, reflecting industry trends to move functionality onto graphics processors

  • Demonstrations and examples of key features based on community feedback and suggestions

  • Updated appendixes covering the latest OpenGL libraries, related APIs, functions, variables, formats, and debugging and profiling techniques

  • Table of Contents

    1. About This E-Book
    2. Title Page
    3. Copyright Page
    4. Praise for previous editions of OpenGL® Programming Guide
    5. Dedication Page
    6. Contents
    7. Figures
    8. Tables
    9. Examples
    10. About This Guide
      1. What This Guide Contains
      2. What’s New in This Edition
      3. What You Should Know Before Reading This Guide
      4. How to Obtain the Sample Code
      5. Errata
      6. Style Conventions
      7. About the OpenGL Series
    11. Acknowledgments
      1. John Kessenich
      2. Graham Sellers
      3. Dave Shreiner
    12. Chapter 1. Introduction to OpenGL
      1. What Is OpenGL?
      2. Your First Look at an OpenGL Program
      3. OpenGL Syntax
      4. OpenGL’s Rendering Pipeline
        1. Preparing to Send Data to OpenGL
        2. Sending Data to OpenGL
        3. Vertex Shading
        4. Tessellation Shading
        5. Geometry Shading
        6. Primitive Assembly
        7. Clipping
        8. Rasterization
        9. Fragment Shading
        10. Per-Fragment Operations
      5. Our First Program: A Detailed Discussion
        1. Entering main()
        2. OpenGL Initialization
        3. Our First OpenGL Drawing
    13. Chapter 2. Shader Fundamentals
      1. Shaders and OpenGL
      2. OpenGL’s Programmable Pipeline
      3. An Overview of the OpenGL Shading Language
        1. Creating Shaders with GLSL
        2. Storage Qualifiers
        3. Statements
        4. Computational Invariance
        5. Shader Preprocessor
        6. Compiler Control
        7. Global Shader-Compilation Option
      4. Interface Blocks
        1. Uniform Blocks
        2. Specifying Uniform Blocks in Shaders
        3. Accessing Uniform Blocks from Your Application
        4. Buffer Blocks
        5. In/Out Blocks, Locations, and Components
      5. Compiling Shaders
      6. Shader Subroutines
        1. Advanced
        2. GLSL Subroutine Setup
        3. Selecting Shader Subroutines
      7. Separate Shader Objects
        1. Advanced
      8. SPIR-V
        1. Reasons to Choose SPIR-V
        2. Using SPIR-V with OpenGL
        3. Using GLSL to Generate SPIR-V for OpenGL
        4. Glslang
        5. What’s Inside SPIR-V?
    14. Chapter 3. Drawing with OpenGL
      1. OpenGL Graphics Primitives
        1. Points
        2. Lines, Strips, and Loops
        3. Triangles, Strips, and Fans
      2. Data in OpenGL Buffers
        1. Creating and Allocating Buffers
        2. Getting Data into and out of Buffers
        3. Accessing the Content of Buffers
        4. Discarding Buffer Data
      3. Vertex Specification
        1. VertexAttribPointer in Depth
        2. Static Vertex-Attribute Specification
      4. OpenGL Drawing Commands
        1. Restarting Primitives
        2. Instanced Rendering
    15. Chapter 4. Color, Pixels, and Fragments
      1. Basic Color Theory
      2. Buffers and Their Uses
        1. Color Buffers
        2. Depth Buffer
        3. Stencil Buffer
        4. Clearing Buffers
        5. Masking Buffers
      3. Color and OpenGL
        1. Color Representation and OpenGL
        2. Smoothly Interpolating Data
      4. Testing and Operating on Fragments
        1. Scissor Test
        2. Multisample Fragment Operations
        3. Stencil Test
        4. Stencil Examples
        5. Depth Test
        6. Blending
        7. Logical Operations
        8. Occlusion Query
        9. Conditional Rendering
      5. Multisampling
        1. Sample Shading
      6. Per-Primitive Antialiasing
        1. Antialiasing Lines
        2. Antialiasing Polygons
      7. Reading and Copying Pixel Data
        1. Clamping Returned Values
      8. Copying Pixel Rectangles
    16. Chapter 5. Viewing Transformations, Culling, Clipping, and Feedback
      1. Viewing
        1. Viewing Model
        2. Camera Model
        3. Orthographic Viewing Model
      2. User Transformations
        1. Matrix Multiply Refresher
        2. Homogeneous Coordinates
        3. Linear Transformations and Matrices
        4. Transforming Normals
        5. OpenGL Matrices
      3. OpenGL Transformations
        1. Advanced: User Culling and Clipping
        2. Controlling OpenGL Transformations
      4. Transform Feedback
        1. Transform Feedback Objects
        2. Transform Feedback Buffers
        3. Configuring Transform Feedback Varyings
        4. Starting and Stopping Transform Feedback
        5. Transform Feedback Example—Particle System
    17. Chapter 6. Textures and Framebuffers
      1. Introduction to Texturing
      2. Basic Texture Types
      3. Creating and Initializing Textures
        1. Proxy Textures
      4. Specifying Texture Data
        1. Explicitly Setting Texture Data
        2. Loading Textures from Buffers
        3. Loading Images from Files
        4. Retrieving Texture Data
        5. Texture Data Layout
      5. Texture Formats
        1. Internal Formats
        2. External Formats
      6. Compressed Textures
      7. Sampler Objects
        1. Sampler Parameters
      8. Using Textures
        1. Texture Coordinates
        2. Arranging Texture Data
        3. Using Multiple Textures
      9. Complex Texture Types
        1. 3D Textures
        2. Array Textures
        3. Cube-Map Textures
        4. Shadow Samplers
        5. Depth-Stencil Textures
        6. Buffer Textures
      10. Texture Views
      11. Filtering
        1. Linear Filtering
        2. Using and Generating Mipmaps
        3. Calculating the Mipmap Level
        4. Mipmap Level-of-Detail Control
      12. Advanced Texture Lookup Functions
        1. Explicit Level of Detail
        2. Explicit Gradient Specification
        3. Texture Fetch with Offsets
        4. Projective Texturing
        5. Texture Queries in Shaders
        6. Gathering Texels
        7. Combining Special Functions
      13. Bindless Textures
        1. Texture Handles
        2. Texture Residency
        3. Sampling Bindless Textures
      14. Sparse Textures
        1. Sparse Texture Commitment
        2. Sparse Texture Pages
      15. Point Sprites
        1. Textured Point Sprites
        2. Controlling the Appearance of Points
      16. Framebuffer Objects
      17. Rendering to Texture Maps
        1. Discarding Rendered Data
        2. Renderbuffers
        3. Creating Renderbuffer Storage
        4. Framebuffer Attachments
        5. Framebuffer Completeness
        6. Invalidating Framebuffers
        7. Writing to Multiple Renderbuffers Simultaneously
        8. Selecting Color Buffers for Writing and Reading
        9. Dual-Source Blending
      18. Chapter Summary
        1. Texture Redux
        2. Texture Best Practices
    18. Chapter 7. Light and Shadow
      1. Lighting Introduction
      2. Classic Lighting Model
        1. Fragment Shaders for Different Light Styles
        2. Moving Calculations to the Vertex Shader
        3. Multiple Lights and Materials
        4. Lighting Coordinate Systems
        5. Limitations of the Classic Lighting Model
      3. Advanced Lighting Models
        1. Hemisphere Lighting
        2. Image-Based Lighting
        3. Lighting with Spherical Harmonics
      4. Shadow Mapping
        1. Creating a Shadow Map
        2. Using a Shadow Map
    19. Chapter 8. Procedural Texturing
      1. Procedural Texturing
        1. Regular Patterns
        2. Toy Ball
        3. Lattice
        4. Procedural Shading Summary
      2. Bump Mapping
        1. Application Setup
        2. Vertex Shader
        3. Fragment Shader
        4. Normal Maps
      3. Antialiasing Procedural Textures
        1. Sources of Aliasing
        2. Avoiding Aliasing
        3. Increasing Resolution
        4. Antialiasing High Frequencies
        5. Frequency Clamping
        6. Procedural Antialiasing Summary
      4. Noise
        1. Definition of Noise
        2. Noise Textures
        3. Trade-Offs
        4. A Simple Noise Shader
        5. Turbulence
        6. Marble
        7. Granite
        8. Wood
        9. Noise Summary
      5. Further Information
    20. Chapter 9. Tessellation Shaders
      1. Tessellation Shaders
      2. Tessellation Patches
      3. Tessellation Control Shaders
        1. Generating Output-Patch Vertices
        2. Tessellation Control Shader Variables
        3. Controlling Tessellation
      4. Tessellation Evaluation Shaders
        1. Specifying the Primitive Generation Domain
        2. Specifying the Face Winding for Generated Primitives
        3. Specifying the Spacing of Tessellation Coordinates
        4. Additional Tessellation Evaluation Shader layout Options
        5. Specifying a Vertex’s Position
        6. Tessellation Evaluation Shader Variables
      5. A Tessellation Example: The Teapot
        1. Processing Patch Input Vertices
        2. Evaluating Tessellation Coordinates for the Teapot
      6. Additional Tessellation Techniques
        1. View-Dependent Tessellation
        2. Shared Tessellated Edges and Cracking
        3. Displacement Mapping
    21. Chapter 10. Geometry Shaders
      1. Creating a Geometry Shader
      2. Geometry Shader Inputs and Outputs
        1. Geometry Shader Inputs
        2. Special Geometry Shader Primitives
        3. Geometry Shader Outputs
      3. Producing Primitives
        1. Culling Geometry
        2. Geometry Amplification
      4. Advanced Transform Feedback
        1. Multiple Output Streams
        2. Primitive Queries
        3. Using Transform Feedback Results
      5. Geometry Shader Instancing
      6. Multiple Viewports and Layered Rendering
        1. Viewport Index
        2. Layered Rendering
      7. Chapter Summary
        1. Geometry Shader Redux
        2. Geometry Shader Best Practices
    22. Chapter 11. Memory
      1. Using Textures for Generic Data Storage
        1. Binding Textures to Image Units
        2. Reading and Writing to Images
      2. Shader Storage Buffer Objects
        1. Writing Structured Data
      3. Atomic Operations and Synchronization
        1. Atomic Operations on Images
        2. Atomic Operations on Buffers
        3. Sync Objects
        4. Image Qualifiers and Barriers
        5. High-Performance Atomic Counters
      4. Example: Order-Independent Transparency
        1. Principles of Operation
        2. Initialization
        3. Rendering
        4. Sorting and Blending
        5. Results
    23. Chapter 12. Compute Shaders
      1. Overview
      2. Workgroups and Dispatch
        1. Knowing Where You Are
      3. Communication and Synchronization
        1. Communication
        2. Synchronization
      4. Examples
        1. Physical Simulation
        2. Image Processing
      5. Chapter Summary
        1. Compute Shader Redux
        2. Compute Shader Best Practices
    24. Appendix A. Support Libraries
      1. Basics of GLFW: The OpenGL Utility Framework
      2. Initializing and Creating a Window
      3. Handling User Input
      4. Controlling the Window
      5. Shutting Down Cleanly
      6. GL3W: OpenGL Glue
    25. Appendix B. OpenGL ES and WebGL
      1. OpenGL ES
      2. WebGL
        1. Setting Up WebGL Within an HTML5 Page
        2. Initializing Shaders in WebGL
        3. Initializing Vertex Data in WebGL
        4. Using Texture Maps in WebGL
    26. Appendix C. Built-in GLSL Variables and Functions
      1. Built-in Variables
        1. Built-in Variable Declarations
        2. Built-in Variable Descriptions
      2. Built-in Constants
      3. Built-in Functions
        1. Angle and Trigonometry Functions
        2. Exponential Functions
        3. Common Functions
        4. Floating-Point Pack and Unpack Functions
        5. Geometric Functions
        6. Matrix Functions
        7. Vector Relational Functions
        8. Integer Functions
        9. Texture Functions
        10. Atomic-Counter Functions
        11. Atomic Memory Functions
        12. Image Functions
        13. Fragment Processing Functions
        14. Geometry Shader Functions
        15. Shader Invocation Control Functions
        16. Shader Memory Control Functions
    27. Appendix D. State Variables
      1. The Query Commands
      2. OpenGL State Variables
        1. Current Values and Associated Data
        2. Vertex Array Object State
        3. Vertex Array Data
        4. Buffer Object State
        5. Transformation State
        6. Coloring State
        7. Rasterization State
        8. Multisampling
        9. Textures
        10. Pixel Operations
        11. Framebuffer Controls
        12. Framebuffer State
        13. Renderbuffer State
        14. Pixel State
        15. Shader Object State
        16. Shader Program Pipeline Object State
        17. Shader Program Object State
        18. Program Interface State
        19. Program Object Resource State
        20. Vertex and Geometry Shader State
        21. Query Object State
        22. Image State
        23. Transform Feedback State
        24. Atomic Counter State
        25. Shader Storage Buffer State
        26. Sync Object State
        27. Hints
        28. Compute Dispatch State
        29. Implementation-Dependent Values
        30. Tessellation Shader Implementation-Dependent Limits
        31. Geometry Shader Implementation-Dependent Limits
        32. Fragment Shader Implementation-Dependent Limits
        33. Implementation-Dependent Compute Shader Limits
        34. Implementation-Dependent Shader Limits
        35. Implementation-Dependent Debug Output State
        36. Implementation-Dependent Values
        37. Internal Format-Dependent Values
        38. Implementation-Dependent Transform Feedback Limits
        39. Framebuffer-Dependent Values
        40. Miscellaneous
    28. Appendix E. Homogeneous Coordinates and Transformation Matrices
      1. Homogeneous Coordinates
        1. Transforming Vertices
        2. Transforming Normals
      2. Transformation Matrices
        1. Translation
        2. Scaling
        3. Rotation
        4. Perspective Projection
        5. Orthographic Projection
    29. Appendix F. Floating-Point Formats for Textures, Framebuffers, and Renderbuffers
      1. Reduced-Precision Floating-Point Values
      2. 16-Bit Floating-Point Values
      3. 10- and 11-Bit Unsigned Floating-Point Values
    30. Appendix G. Debugging and Profiling OpenGL
      1. Creating a Debug Context
      2. Debug Output
        1. Debug Messages
        2. Filtering Messages
        3. Application-Generated Messages
      3. Debug Groups
        1. Naming Objects
      4. Profiling
        1. Profiling Tools
        2. In-Application Profiling
    31. Appendix H. Buffer Object Layouts
      1. Using Standard Layout Qualifiers
      2. The std140 Layout Rules
      3. The std430 Layout Rules
    32. Glossary
    33. Index
    34. Code Snippets