Download presentation
Presentation is loading. Please wait.
1
Chapter 6 GPU, Shaders, and Shading Languages
CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
2
Programmable Graphics Hardware: History
1984 : Cook’s Shade trees RenderMan Shading Language in late 80’s 1980 ~ 1998 : (Hardware) Pixel-Planes/PixelFlow at UNC 1992 : OpenGL 1.1 standard 1998 : Multi-texturing 1999 : NVIDIA’s GeForce256 - 1999 : Register Combiners 1999 : Stanford’s Real-Time Programmable Shading Project Implement programmable shading operation in real time via multiple rendering passes Quake III CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
3
Programmable Graphics Hardware: History
2000 : Peercy et al. proposed a system that translated RenderMan shaders to run in multiple passes on graphics hardware They found that GPU lacked two features that would make this approach very general Dependent texture reads – use computation results as texture coordinates Data types with extended range and precision in textures and color buffers 2001 : NVIDIA GeForce 3 DirectX 8.0, 8.1 (Shader Model 1.1, 1.2~1.4) Vertex shader Programmed in assembly-like language Pixel shader Allows very limited “programs” (DirectorX 8.0); become better (8.1) Limited in length (12 instructions or less) Lacked dependent texture read and float data No flow control CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
4
Programmable Graphics Hardware: History
2002 DirectX 9.0 (Shader Model 2.0) Fully programmable vertex and pixel shaders Support for arbitrary dependent texture reads and 16-bit float Support for flow control Shader programming language: HLSL, GLSL, Cg 2004 Shader 3.0 An incremental improvement, turning optional features into requirements Adding dependent texture reads in vertex shader 2007 Shader 4.0 – DirectX 10 – support only high-level languages Unified shader architecture Geometry shader, Stream output CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
5
CG Lab., CS Dept., NCTU, Jung Hong Chuang
Fixed Pipeline Vertex Attributes of vertex are fixed Position, normal, RGBA, texture coordinate Projection transformation is fixed Lighting function is fixed Phone model (isotropic BRDF) Fragment How to generate final pixel color is fixed Texture access and blending with multitexture CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
6
CG Lab., CS Dept., NCTU, Jung Hong Chuang
Fixed Pipeline Vertex data Transformation & Lighting Primitive assembly Viewport culling & Clipping Vertex pipeline Rasterizer setup Fragment Processing (Texture Blending) Per Fragment operations Frame Buffer operations Fragment pipeline Frame Buffer CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
7
CG Lab., CS Dept., NCTU, Jung Hong Chuang
Fixed Pipeline Transformation & lighting Model, view, projection transformation Lighting computation Primitive assembly Assembly vertices to primitives such as line segment and triangles Viewport culling and clipping Must be done on a primitive by primitive basis Back-face culling View volume culling CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
8
CG Lab., CS Dept., NCTU, Jung Hong Chuang
Fixed Pipeline Rasterization or scan conversion Determines fragments inside the primitive Output a set of fragments for each primitives with information such as color, depth, texture coordinate Texture blending or fragment processing Texture accessing and blending Fog Per fragment operations Stencil and depth test Fragments passing all tests are written to frame-buffer (with alpha blending) CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
9
Modern Programmable Pipeline
Three programmable processors to replace components in the fixed rendering pipeline Vertex processor Geometry processor Fragment (pixel) processor Vertex data Vertex processor Geometry processor Viewport culling & Clipping Rasterizer setup Fragment processor Per Fragment operations Frame Buffer operations Frame Buffer CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
10
Why Programmable Pipeline?
Vertex processor Modify/create/ignore attributes of vertex, such as position, color, normal, texture coordinates Deformation, transformation Support different lighting models Geometry processor Allows GPU to create and destroy geometric primitives (points, lines, triangles) on the fly Fragment processor Processes individual pixels, allowing complex shading equation to be evaluated per pixel CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
11
CG Lab., CS Dept., NCTU, Jung Hong Chuang
Vertex Processor Programmability in vertex level. Replace the Transformation & Lighting unit. Vertex transformation Normal transformation & normalization Texture coordinate generation Texture coordinate transformation Per-vertex lighting Vertex Data Transformation & Lighting Vertex processor Primitive assembly Viewport culling & Clipping Rasterizer setup CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
12
CG Lab., CS Dept., NCTU, Jung Hong Chuang
Vertex Processor What does vertex processor do Each vertex is associated with a vertex processor Provides a way to modify attributes associated with each vertex Position, Normal, Color, Texture coordinates… In OpenGL, the vertex is assigned using glVertex*() Performs the lighting computation Shader to be run on the vertex processor are called vertex shader CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
13
CG Lab., CS Dept., NCTU, Jung Hong Chuang
Vertex Processor What vertex processor can not do It does not replace graphics operations for assembling the primitives You can not ask vertex processor to change the primitive type or change the order of vertices form the primitives No vertex can be created or deleted. Results of a vertex can not be passed to another vertex Parallel computing CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
14
Vertex Processor Fixed vs. programmable
Without programmable vertex shaders Change of vertex attributes must be done by CPU, and the result is sent to the pipeline for every frame. No way to change the vertex lighting With programmable vertex shader All changes of vertex attributes are done by vertex shaders Different vertex lightings can be performed CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
15
CG Lab., CS Dept., NCTU, Jung Hong Chuang
Fragment Processor Programmability in fragment level. Replace the following operations Operations on interpolated values Texture access Texture applications Fog Color sum Alpha test (new for Shader Model 4.0) Rasterizer setup Texture blending Fragment processor Per Fragment operations Frame buffer operations CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
16
CG Lab., CS Dept., NCTU, Jung Hong Chuang
Fragment Processor What does fragment processor do Each pixel is associated with a fragment processor. Provides a way to compute the final pixel color and depth value base on the input data. The input data are the result of the rasterization stage, and can not be controlled by user explicitly. Shaders to be run on the fragment processor are called fragment shaders or pixel shaders. CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
17
CG Lab., CS Dept., NCTU, Jung Hong Chuang
Fragment Processor What does fragment processor do (cont.) Dependent texture read Access the texture content base on the texture coordinate computed on the fragment Early version of vertex shader can not perform the dependent texture read - Nvidia’s Geforce 6 series have Multiple render targets (MRT) Multiple vectors can be generated for each fragment and saved to different buffers a single pass can generate several images, instead of one pass per output buffer Blending can be performed on multiple buffers DirectX 10.1 allows different blending operations on each MRT buffer; previous versions only allow same blending operation CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
18
CG Lab., CS Dept., NCTU, Jung Hong Chuang
Fragment Processor What fragment processor can not do Frame buffer operations (depth test, alpha test, stencil test…) Alpha blending, pixel ownership test, plane masking… The fragment processor can not update the pixel that does not associate to it. Results of a pixel can not be used by another pixel in the same frame. CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
19
CG Lab., CS Dept., NCTU, Jung Hong Chuang
GPU in DX10 (in late 2006) Direct X 10 features: Shader Model 4.0 Unified shader architecture (common-shader code) Geometry shader Stream out Input Assembler Video Memory (Buffer Texture, Constant Buffer) Vertex Shader Geometry Shader Stream Output Rasterizer Pixel Shader Output Merger CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
20
CG Lab., CS Dept., NCTU, Jung Hong Chuang
GPU in DX10 (in late 2006) Unified shader architecture The vertex, pixel, and geometry shaders share a programming model Earlier GPUs had less commonality between vertex and pixel shaders and did not have geometry shader Shaders are programmed using C-like shading languages such as HLSL, Cg, and GLSL Previous shader models allowed programming directly in the assembly language Shaders are compiled to a machine-independent assembly language. The assembly language is converted to the machine language in a separate step, usually in the drivers CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
21
CG Lab., CS Dept., NCTU, Jung Hong Chuang
GPU in DX10 (in late 2006) Draw call Invokes the graphics API to draw a group of primitives, so causing the graphics pipeline to execute Two type of inputs to shaders Uniform input Values that remain constant throughout a draw call (but can be changed between draw calls) Are stored once and reused across all the vertices and pixels in the draw call (Example: texture) Varying input Values that are different for each vertex or pixel processed by the shader CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
22
CG Lab., CS Dept., NCTU, Jung Hong Chuang
GPU in DX10 (in late 2006) Flow control If, case, loop… Shaders support two types of flow control Static flow control Based on the value of uniform inputs Flow of code is constant over the draw call, allowing the same shader to be used in a variety of different situations (e.g., varying number of lights) Dynamic flow control Based on the values of varying inputs Much more powerful than static flow control but is more costly CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
23
CG Lab., CS Dept., NCTU, Jung Hong Chuang
GPU in DX10 (in late 2006) Standard use of GPU Data always passed through the pipeline and intermediate results could not be accessed Stream output After vertices are processed by vertex shader and, optionally, geometry shader, these can be output in a stream, in addition to being sent to rasterization stage Rasterization could, in fact, be turned off entirely Then used purely as a non-graphical stream processor Stream data can be sent back through the pipeline CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
24
CG Lab., CS Dept., NCTU, Jung Hong Chuang
Vertex Processor Dataflow The inputs of a vertex shader are the attributes associated on the vertex (vertex posotion, color, normal, texture coordinates…) These values are assigned by glVertex(), glNormal(), glColor(), … in OpenGL The outputs of a vertex shader are the attributes of the vertex, also. Uniform variables Transform matrix, Material, Light sources, … Attribute variables Color, Normal, Position, Tex coord, … Vertex processor Texture Maps Output variables Color, Position, Tex coord, … CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
25
CG Lab., CS Dept., NCTU, Jung Hong Chuang
Fragment Processor Dataflow The inputs of a fragment shader are the attributes associated on the fragment (depth value, color, normal, texture coordinates…) These values are interpolated in the rasterization stage Can not be assigned by user directly The outputs of a fragment shader are the color and the depth value of the pixel Uniform variables Transform matrix, Material, Light sources, … Outputs of the rasterizer Fragment processor Texture Maps Special output variables gl_FragColor gl_FracDepth CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
26
CG Lab., CS Dept., NCTU, Jung Hong Chuang
Geometry Shader Programmability for primitive assembly Replace the Primitive Assembly unit. Right after vertex shader. Input Primitives A point, A line, A triangle Line with adjacency (2 adjacent vertices on the polyline) Triangle with adjacency (3 vertices outside of the triangle) Output Zero or more primitives Points, Polylines, Triangle strips May be in different type Input triangles, output their centroids Vertex Data Vertex processor Primitive assembly Geometry processor Viewport culling & Clipping Stream output Rasterizer setup CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
27
CG Lab., CS Dept., NCTU, Jung Hong Chuang
Geometry Shader Programmability for primitive assembly A mesh can be modified by editing vertices, adding new primitives, and remove others To provide additional capability to generate complex primitives Applications Silhouette detection and extrusion Access to mesh topology Mesh tessellation Vertex Data Vertex processor Primitive assembly Geometry processor Viewport culling & Clipping Stream output Rasterizer setup CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
28
CG Lab., CS Dept., NCTU, Jung Hong Chuang
Stream Output After vertices are processed by vertex shader and, optionally, geometry shader, these can be output in a stream in addition to being sent on to the rasterization stage. Rasterization can be turned off entirely Non-graphical stream processor Data can be sent back through pipeline Allow interactive processing Useful for simulating particle effects Vertex Data Video Memory Vertex processor Primitive assembly Geometry processor Viewport culling & Clipping Stream output Rasterizer setup CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
29
Notes on GPU programming
CPU Access main memory. Can be accessed directly. General purpose. Unrestricted input and output. One (usually). GPU (Graphics Processing Unit) Access texture memory. Can only be accessed through graphics API. Specific purpose vector processor. Input and output are restricted. Several (can be used as parallel computing). CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
30
Notes on GPU programming
GPU is not a general purpose processor It is part of the rendering pipeline Carefully study the spec. of GPU before using it Only several programs are suitable to be run on GPU * New API for non-graphics applications Nvidia’s CUDA Cooperate between vertex and fragment shaders In most cases, the vertex and fragment shaders are used together to solve the problem CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
31
Notes on GPU programming
Multi-pass rendering Several programs require multiple rendering passes to solve the problems Multiple shaders. The result of current rendering pass will be used in the next pass. Use pBuffer or frame buffer object (FBO) to store the result. CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
32
Notes on GPU programming
Example : Particle system Two pass Update particle states Render the particles Vertex shader (NULL) Fragment shader (Update particle states) pBuffer (Particle states) Texture maps (Particle states) Vertex shader (Fetch particle position) Fragment shader (Fetch particle color) Frame buffer CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
33
CG Lab., CS Dept., NCTU, Jung Hong Chuang
Shading languages Shaders are programmed using C-like shading languages HLSL, Cg, GLSL Compiled to a machine-independent assembly language, which is converted to the actual machine language in a separate step. CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
34
CG Lab., CS Dept., NCTU, Jung Hong Chuang
Extending OpenGL OpenGL version OpenGL 1.0 ~ 1.5 (in about 10 years) Use OpenGL extension to support new programmable pipeline features Use assembly-like codes, which is loaded to shaders by OpenGL extensions OpenGL 2.0 in 2004 Include OpenGL Shading Language (GLSL) C-like Add more language features and data types that make easier to program shaders Simpler (than Cg) to develop OpenGL shaders using GLSL CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
35
CG Lab., CS Dept., NCTU, Jung Hong Chuang
Extending OpenGL Cg Similar to GLSL, but has different targeted users Supports shaders that are portable across multiple platforms, including OpenGL and Microsoft’s DirectX Virtually identical to Microsoft’s High Level Shading Language (HLSL) Interface between Cg and OpenGL is more sophisticated than the interface between GLSL and OpenGL. CG (U), Chap 6, GPU and Shaders CG Lab., CS Dept., NCTU, Jung Hong Chuang
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.