Shading Languages & HW Giovanni Civati Dept. of Information Tecnology University of Milan, Italy Milan, 26 th May 2004.

Slides:



Advertisements
Similar presentations
Fragment level programmability in OpenGL Evan Hart
Advertisements

COMPUTER GRAPHICS SOFTWARE.
Perspective aperture ygyg yryr n zgzg y s = y g (n/z g ) ysys y s = y r (n/z r ) zrzr.
Understanding the graphics pipeline Lecture 2 Original Slides by: Suresh Venkatasubramanian Updates by Joseph Kider.
Graphics Pipeline.
Computer Graphic Creator: Mohsen Asghari Session 2 Fall 2014.
CS-378: Game Technology Lecture #9: More Mapping Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic, Jessica.
9/25/2001CS 638, Fall 2001 Today Shadow Volume Algorithms Vertex and Pixel Shaders.
The Programmable Graphics Hardware Pipeline Doug James Asst. Professor CS & Robotics.
CS5500 Computer Graphics © Chun-Fa Chang, Spring 2007 CS5500 Computer Graphics April 19, 2007.
Introduction to Shader Programming
A Crash Course on Programmable Graphics Hardware Li-Yi Wei 2005 at Tsinghua University, Beijing.
Status – Week 277 Victor Moya.
GPU Simulator Victor Moya. Summary Rendering pipeline for 3D graphics. Rendering pipeline for 3D graphics. Graphic Processors. Graphic Processors. GPU.
ARB Fragment Program in GPULib. Summary Fragment program arquitecture New instructions.  Emulating instructions not supported directly New Required GL.
Status – Week 283 Victor Moya. 3D Graphics Pipeline Akeley & Hanrahan course. Akeley & Hanrahan course. Fixed vs Programmable. Fixed vs Programmable.
The Graphics Pipeline CS2150 Anthony Jones. Introduction What is this lecture about? – The graphics pipeline as a whole – With examples from the video.
The programmable pipeline Lecture 10 Slide Courtesy to Dr. Suresh Venkatasubramanian.
Vertex & Pixel Shaders CPS124 – Computer Graphics Ferdinand Schober.
Status – Week 260 Victor Moya. Summary shSim. shSim. GPU design. GPU design. Future Work. Future Work. Rumors and News. Rumors and News. Imagine. Imagine.
Cg Overview Cg is the High Level language from NVIDIA for programming GPUs, developed in close collaboration with Microsoft Cg is 100% compatible with.
Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides Shaders Feb 18, 2011 Creative Commons Attribution 3.0 (Except copyrighted.
GPU Graphics Processing Unit. Graphics Pipeline Scene Transformations Lighting & Shading ViewingTransformations Rasterization GPUs evolved as hardware.
Cg Kevin Bjorke GDC NVIDIA CONFIDENTIAL A Whole New World with Cg Graphics Program Written in Cg “C” for Graphics Compiled & Optimized Low Level,
Under the Hood: 3D Pipeline. Motherboard & Chipset PCI Express x16.
Cg – C for Graphics Eric Vidal CS 280. History General graphics processing languages – Renderman shading language (1988) Assembly languages for graphics.
REAL-TIME VOLUME GRAPHICS Christof Rezk Salama Computer Graphics and Multimedia Group, University of Siegen, Germany Eurographics 2006 Real-Time Volume.
Shader generation and compilation for a programmable GPU Student: Jordi Roca Monfort Advisor: Agustín Fernández Jiménez Co-advisor: Carlos González Rodríguez.
GPU Programming Robert Hero Quick Overview (The Old Way) Graphics cards process Triangles Graphics cards process Triangles Quads.
GPU Acceleration in Registration Danny Ruijters 26 April 2007.
Programmable Pipelines. Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
Real-time Graphical Shader Programming with Cg (HLSL)
Geometric Objects and Transformations. Coordinate systems rial.html.
Graphics Graphics Korea University cgvr.korea.ac.kr 1 Using Vertex Shader in DirectX 8.1 강 신 진
GPU Shading and Rendering Shading Technology 8:30 Introduction (:30–Olano) 9:00 Direct3D 10 (:45–Blythe) Languages, Systems and Demos 10:30 RapidMind.
Programmable Pipelines. 2 Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
Chris Kerkhoff Matthew Sullivan 10/16/2009.  Shaders are simple programs that describe the traits of either a vertex or a pixel.  Shaders replace a.
A Crash Course in HLSL Matt Christian.
CS 450: COMPUTER GRAPHICS REVIEW: INTRODUCTION TO COMPUTER GRAPHICS – PART 2 SPRING 2015 DR. MICHAEL J. REALE.
The Graphics Rendering Pipeline 3D SCENE Collection of 3D primitives IMAGE Array of pixels Primitives: Basic geometric structures (points, lines, triangles,
Real-Time Shading Using Programmable Graphics Hardware Introduction, Setup and Examples Wan-Chun Ma National Taiwan University.
1 Dr. Scott Schaefer Programmable Shaders. 2/30 Graphics Cards Performance Nvidia Geforce 6800 GTX 1  6.4 billion pixels/sec Nvidia Geforce 7900 GTX.
CSE 690: GPGPU Lecture 6: Cg Tutorial Klaus Mueller Computer Science, Stony Brook University.
Computer Graphics The Rendering Pipeline - Review CO2409 Computer Graphics Week 15.
GRAPHICS PIPELINE & SHADERS SET09115 Intro to Graphics Programming.
CS662 Computer Graphics Game Technologies Jim X. Chen, Ph.D. Computer Science Department George Mason University.
Programmable Pipelines Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University.
Advanced Computer Graphics Spring 2014 K. H. Ko School of Mechatronics Gwangju Institute of Science and Technology.
Review on Graphics Basics. Outline Polygon rendering pipeline Affine transformations Projective transformations Lighting and shading From vertices to.
David Luebke 1 1/25/2016 Programmable Graphics Hardware.
COMPUTER GRAPHICS CS 482 – FALL 2015 SEPTEMBER 29, 2015 RENDERING RASTERIZATION RAY CASTING PROGRAMMABLE SHADERS.
09/25/03CS679 - Fall Copyright Univ. of Wisconsin Last Time Shadows Stage 2 outline.
What are shaders? In the field of computer graphics, a shader is a computer program that runs on the graphics processing unit(GPU) and is used to do shading.
GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global.
An Introduction to the Cg Shading Language Marco Leon Brandeis University Computer Science Department.
GLSL Review Monday, Nov OpenGL pipeline Command Stream Vertex Processing Geometry processing Rasterization Fragment processing Fragment Ops/Blending.
Computer Science – Game DesignUC Santa Cruz Tile Engine.
COMP 175 | COMPUTER GRAPHICS Remco Chang1/XX13 – GLSL Lecture 13: OpenGL Shading Language (GLSL) COMP 175: Computer Graphics April 12, 2016.
Programmable Shaders Dr. Scott Schaefer.
Programmable Pipelines
A Crash Course on Programmable Graphics Hardware
Graphics Processing Unit
Chapter 6 GPU, Shaders, and Shading Languages
Introduction to Programmable Hardware
Graphics Processing Unit
Where does the Vertex Engine fit?
CS5500 Computer Graphics April 17, 2006 CS5500 Computer Graphics
RADEON™ 9700 Architecture and 3D Performance
Computer Graphics Introduction to Shaders
CIS 441/541: Introduction to Computer Graphics Lecture 15: shaders
Presentation transcript:

Shading Languages & HW Giovanni Civati Dept. of Information Tecnology University of Milan, Italy Milan, 26 th May 2004

Introduction Ten years ago graphics hardware were capable of performing only 2D operations Pc graphics hardware evolution was determined by video games 1995: introduction of low-cost graphics hardware for 3D acceleration (triangle rastering, texture) 1999: introduction of GPU (NVidia GeForce, followed by Ati Radeon and NVidia GeForce2) 2001: introduction of GPU with programmable pipeline (vertex and pixel shader, DirectX 8.1) Today: second generation GPU with programmable pipeline (DirectX 9)

3D graphics cards 2 Major productors: –Ati (Radeon) –NVidia (GeForce) Known problems Library compability Raw performance (fill rate, triangle throughput) Driver quality Driver stability, etc… Hardware

ATI Radeon 9700 (R300) 0.15-micron GPU 110+ million transistor 8 pixel rendering pipelines, 1 texture unit per pipeline, can do 16 textures per pass 4 programmable vect4 vertex shader pipelines 256-bit DDR memory bus up to 256MB of memory on board, clocked at over 300MHz (resulting in a minimum of 19.2GB/s of memory bandwidth) AGP 8X Support Full DX9 Pixel and Vertex Shader Support (DirectX 9 Compliant means that the pipeline is fully floating point from start to finish) Hardware R300

The die Hardware R300 Large amount taken up by the FPPU

The 3D Pipeline Hardware R300 – The 3D Pipeline Other products have a similar pipeline (e.g. NV30) (due to the compliancy with DirectX 9)

High-Performance Graphics Memory Hardware R300 – The 3D Pipeline

First step: Sending commands and data to be executed on the GPU AGP 8x (AGP 3.0) running at 66Mhz with 32- bit results in a total of 2.1 GB/s (between R300 and the North Bridge) Hardware R300 – The 3D Pipeline

Vertex Processing Data sent in the form of vertices First operation (known as T&L) is the transformation stage (requiring a lot of highly repetitive floating-point matrix math) Lighting calculation for each vertex Shape changing, look, behavior (e.g. matrix palette skinning, realistic fur, etc.) Hardware R300 – The 3D Pipeline

Vertex processing Hardware R300 – The 3D Pipeline

Vertex Processing Vertex shader 2.0 support –Increased number of instructions (1024 in a single pass) –Flow control support Hardware R300 – The 3D Pipeline

Pixel rendering pipeline Hardware R300 – The 3D Pipeline Eight 128-bit floating point pixel rendering pipelines

Pixel rendering pipeline Hardware R300 – The 3D Pipeline Photorealistic images need FP Color

Pixel Shader textures per pass 160 Pixel Shader instructions per pass (R300) Hardware R300 – The 3D Pipeline

Pixel Shader 2.0 Hardware R300 – The 3D Pipeline R300 allows to compile and run the code on the GPU itself ATI's RenderMonkey Application will take code from any high level language and compile it to R300 assembly

Pixel Shader 2.0 3D Games are currently far from being lifelike: Low-quality textures Lack of polygon details But… …the major limitation is lighting! Hardware R300 – The 3D Pipeline

Pixel Shader 2.0 With 32-bit integer pipeline each RBGA value was limited to 8-bits E.g. increasing the light source brightness (see image)… Hardware R300 – The 3D Pipeline

Pixel Shader 2.0 …the entire scene gets brighter. The brightest light sources in the scene are no longer proportionally brighter than the rest of the scene… Hardware R300 – The 3D Pipeline

Pixel Shader 2.0 The same applies to when darkening a scene Hardware R300 – The 3D Pipeline

Pixel Shader 2.0 The same scene resulting from the use of floating point color (offering a wide range of brightness levels) Hardware R300 – The 3D Pipeline

R300 3D Pipeline Multisampling –Supersampling Fixed anisotropic Filtering Hardware R300 – The 3D Pipeline Other features Video through the 3D Pipeline

Shading Languages Rendering pipeline (on current GPUs) Low-level languages (Assembly-like) –Vertex programming –Fragment programming High-level shading languages (Languages)

Shading Languages Traditional OpenGL Pipeline (by Kurt Akeley) Rendering pipeline

Shading Languages Programmable Pipeline Most parts of the rendering pipeline can be programmed Shading programs to change hardware behavior –Transform and lighting: vertex shaders / vertex programs –Fragment processing: pixel shaders / fragment programs History: from fixed-function pipeline to configurable pipeline –Steps towards programmability Rendering pipeline

Shading Languages Programmable Pipeline Rendering pipeline

Shading Languages Issues How are vertex and pixel shaders specified? –Low-level, assembly-like –High-level language Data flow between components –Per-vertex data (for vertex shader) –Per-fragment data (for pixel shader) –Uniform (constant) data: e.g. modelview matrix, material parameters Rendering pipeline

Shading Languages Low-level languages Similarity to assembler –Close to hardware functionality –Input: vertex/fragment attributes –Output: new vertex/fragment attributes –Sequence of instructions on registers –Very limited control flow (if any) –Platform-dependent Low-level languages

Shading Languages Low-level languages Current low-level APIs: –OpenGL extensions: GL_ARB_vertex_program, GL_ARB_fragment_program –DirectX 9: Vertex Shader 2.0, Pixel Shader 2.0 Older low-level APIs: –DirectX 8.x: Vertex Shader 1.x, Pixel Shader 1.x –OpenGL extensions: GL_ATI_fragment_shader, GL_NV_vertex_program, … Low-level languages

Shading Languages Low-level languages Low-level APIs offer best performance & functionality Help to understand the graphics hardware (ATI’s r300, NVIDIA’s nv30,...) Help to understand high-level APIs (Cg, HLSL,...) Much easier than directly specifying configurable graphics pipeline (e.g. register combiners) Low-level languages

Shading Languages Vertex Programming applications Customized computation of vertex attributes Computation of anything that can be interpolated linearly between vertices Limitations: –Vertices can neither be generated nor destroyed –No information about topology or ordering of vertices is available Low-level languages (vertex programming)

Shading Languages OpenGL: GL_ARB_vertex_program Circumvents the traditional vertex pipeline What is replaced by a vertex program? –Vertex transformations –Vertex weighting/blending –Normal transformations –Color material –Per-vertex lighting –Texture coordinate generation –Texture matrix transformations –Per-vertex point size computations –Per-vertex fog coordinate computations –Client-defined clip planes Low-level languages (vertex programming - OpenGL)

Shading Languages What is not replaced? –Clipping to the view frustum –Perspective divide (division by w) –Viewport transformation –Depth range transformation –Front and back color selection –Clamping colors –Primitive assembly and per-fragment operations –Evaluators Low-level languages (vertex programming - OpenGL)

Shading Languages Machine Model Low-level languages (vertex programming - OpenGL)

Shading Languages Variables Vertex attributes (vertex.position, vertex.color, …) –Explicit binding: ATTRIB name = vertex.*; State variables –state.material.* (diffuse,...), state.matrix.* (modelview[n], …), … Program results and output variables –result.color.* (primary, secondary, …), result.position, … OUTPUT name = result.*; Low-level languages (vertex programming - OpenGL)

Shading Languages Instructions Instruction set –27 instructions –Operate on floating-point scalars or 4-vectors –Basic syntax: OP destination [, source1 [, source2 [, source3]]]; # comm. –Example: MOV result.position, vertex.position; # sets result.position –Numerical operations: ADD, MUL, LOG, EXP, … –Modifiers: swizzle, negate, mask, saturation Low-level languages (vertex programming - OpenGL)

Shading Languages Example: Transformation to clip coordinates !!ARBvp1.0 ATTRIB pos = vertex.position; ATTRIB col = vertex.color; OUTPUT clippos = result.position; OUTPUT newcol = result.color; PARAM modelviewproj[4] = { state.matrix.mvp }; DP4 clippos.x, modelviewproj[0], pos; DP4 clippos.y, modelviewproj[1], pos; DP4 clippos.z, modelviewproj[2], pos; DP4 clippos.w, modelviewproj[3], pos; MOV newcol, col; END Low-level languages (vertex programming - OpenGL)

Shading Languages DirectX 9: Vertex Shader 2.0 Vertex Shader 2.0 introduced in DirectX 9.0 Similar functionality and limitations as GL_ARB_vertex_program Similar registers and syntax Additional functionality: static flow control –Control of flow determined by constants (not by per-vertex attributes) –Conditional blocks, repetition, subroutines Low-level languages (vertex programming – DirectX 9)

Shading Languages Fragment Programming applications Customized computation of fragment attributes Computation of anything that should be computed per pixel Limitations: –Fragments cannot be generated –Position of fragments cannot be changed –No information about geometric primitive is available Low-level languages (fragment programming)

Shading Languages OpenGL: GL_ARB_fragment_program Circumvents the traditional fragment pipeline What is replaced by a pixel program? –Texturing –Color sum –Fog for the rasterization of points, lines, polygons, pixel rectangles, and bitmaps What is not replaced? –Fragment tests (alpha, stencil, and depth tests) –Blending Low-level languages (fragment programming - OpenGL)

Shading Languages Machine Model Low-level languages (fragment programming - OpenGL)

Shading Languages Instructions Instruction set –Similar to vertex program instructions –Operate on floating-point scalars or 4-vectors Texture sampling OP destination, source, texture[index], type; Texture types: 1D, 2D, 3D, CUBE, RECT TEX result.color, fragment.texcoord[1], texture[0], 2D; samples 2D texture in unit 0 with texturecoordinate set 1 and writes the result in result.color Low-level languages (fragment programming - OpenGL)

Shading Languages Example !!ARBfp1.0 ATTRIB tex = fragment.texcoord; ATTRIB col = fragment.color.primary; OUTPUT outColor = result.color; TEMP tmp; TXP tmp, tex, texture[0], 2D; MUL outColor, tmp, col; END Low-level languages (fragment programming - OpenGL)

Shading Languages DirectX 9: Pixel Shader 2.0 Pixel Shader 2.0 introduced in DirectX 9.0 Similar functionality and limitations as GL_ARB_fragment_program Similar registers and syntax Low-level languages (fragment programming – DirectX 9)

Shading Languages Pixel Shader 2.0 Declaration of texture samplers dcl_type s* Declaration of input color and texture coordinates dcl v*[.mask] dcl t*[.mask] Instruction set –Operate on floating-point scalars or 4-vectors op destination [, source1 [, source2 [, source3]]] Texture sampling op destination, source, sn Low-level languages (fragment programming – DirectX 9)

Shading Languages Example ps_2_0 dcl_2d s0 dcl t0.xy texld r1, t0, s0 mov oC0, r1 Low-level languages (fragment programming – DirectX 9)

Shading Languages Why? –Avoids programming, debugging, and maintenance of long assembly shaders –Easy to read –Easier to modify existing shaders –Automatic code optimization –Wide range of platforms –Shaders often inspired RenderMan shading language High-level shading languages

Shading Languages Assembly vs. High-Level Language High-level shading languages Assembly … dp3 r0, r0, r1 max r1.x, c5.x, r0.x pow r0.x, r1.x, c4.x mul r0, c3.x, r0.x mov r1, c2 add r1, c1, r1 mad r0, c0.x, r1, r0... High-Level Language … float4 cSpec = pow(max(0, dot(Nf, H)), phongExp).xxx; float4 cPlastic = Cd * (cAmbi + cDiff) + Cs * cSpec; … Blinn-Phong shader expressed in both assembly and high-level language

Shading Languages Data flow through Pipeline Vertex shader program Fragment shader program Connectors High-level shading languages

Shading Languages High-Level Shading Languages Cg (“C for Graphics”) –By NVIDIA HLSL (“High-level shading language”) –Part of DirectX 9 (Microsoft) OpenGL 2.0 Shading Language –Proposal by 3D Labs High-level shading languages

Shading Languages High-level shading languages (Cg) Cg Typical concepts for a high-level shading language Language is (almost) identical to DirectX HLSL Syntax, operators, functions from C/C++ Conditionals and flow control Backends according to hardware profiles Support for GPU-specific features (compare to low-level) –Vector and matrix operations –Hardware data types for maximum performance –Access to GPU functions: mul, sqrt, dot, … –Mathematical functions for graphics, e.g. reflect –Profiles for particular hardware feature sets

Shading Languages Workflow High-level shading languages (Cg)

Shading Languages Phong Shading in Cg: Vertex Shader First part of pipeline Connectors: what kind of data is transferred to / from vertex program? Actual vertex shader High-level shading languages (Cg)

Shading Languages Phong Shading in Cg: Connectors Describe input and output Varying data Specified as struct Extensible Adapted to respective implementation Only important data is transferred Pre-defined registers: POSITION, NORMAL,... High-level shading languages (Cg)

Shading Languages Phong Shading in Cg: Connectors // data from application to vertex program struct vertexIn { float3 Position : POSITION; // pre-defined registers float4 UV : TEXCOORD0; float4 Normal : NORMAL; }; // vertex shader to pixel shader struct vertexOut { float4 HPosition : POSITION; float4 TexCoord : TEXCOORD0; float3 LightVec : TEXCOORD1; float3 WorldNormal : TEXCOORD2; float3 WorldPos : TEXCOORD3; float3 WorldView : TEXCOORD4; }; High-level shading languages (Cg)

Shading Languages Phong Shading in Cg: Vertex Shader Vertex shader is a function that requires –Varying application-to-vertex input parameters –Vertex-to-fragment output structure Optional uniform input parameters –Constant for a larger number of primitives –Passed to the Cg program by the application through the runtime library Vertex shader for Phong shading: –Compute position, normal vector, viewing vector, and light vector in world coordinates High-level shading languages (Cg)

Shading Languages Phong Shading in Cg: Vertex Shader // vertex shader vertexOut mainVS(vertexIn IN, // vertex input from app uniform float4x4 WorldViewProj,// constant parameters uniform float4x4 WorldIT, // from app: various uniform float4x4 World, // transformation uniform float4x4 ViewIT, // matrices and a uniform float3 LightPos // point-light source ) { vertexOut OUT; // output of the vertex shader OUT.WorldNormal = mul(WorldIT, IN.Normal).xyz; // position in object coords: float4 Po = float4(IN.Position.x,IN.Position.y, IN.Position.z,1.0); float3 Pw = mul(World, Po).xyz; // pos in world coords High-level shading languages (Cg)

Shading Languages Phong Shading in Cg: Vertex Shader OUT.WorldPos = Pw; // pos in world coords OUT.LightVec = LightPos - Pw; // light vector OUT.TexCoord = IN.UV; // original tex coords // view vector in world coords: OUT.WorldView = normalize(ViewIT[3].xyz - Pw); // pos in clip coords: OUT.HPosition = mul(WorldViewProj, Po); return OUT; // output of vertex shader } High-level shading languages (Cg)

Shading Languages Phong Shading in Cg: Pixel Shader Second part of pipeline Connectors: from vertex to pixel shader, from pixel shader to frame buffer Actual pixel shader High-level shading languages (Cg)

Shading Languages Phong Shading in Cg: Pixel Shader Pixel shader is a function that requires –Varying vertex-to-fragment input parameters –Fragment-to-pixel output structure Optional uniform input parameters –Constant for a larger number of fragments –Passed to the Cg program by the application through the runtime library Pixel shader for Phong shading: –Normalize light, viewing, and normal vectors –Compute halfway vector –Add specular, diffuse, and ambient contributions High-level shading languages (Cg)

Shading Languages Phong Shading in Cg: Pixel Shader // final pixel output: // data from pixel shader to frame buffer struct pixelOut { float4 col : COLOR; }; // pixel shader pixelOut mainPS(vertexOut IN, // input from vertex shader uniform float SpecExpon, // constant parameters from uniform float4 AmbiColor, // application uniform float4 SurfColor, uniform float4 LightColor ) { pixelOut OUT; // output of the pixel shader float3 Ln = normalize(IN.LightVec); float3 Nn = normalize(IN.WorldNormal); float3 Vn = normalize(IN.WorldView); float3 Hn = normalize(Vn + Ln); High-level shading languages (Cg)

Shading Languages Phong Shading in Cg: Pixel Shader // scalar product between light and normal vectors: float ldn = dot(Ln,Nn); // scalar product between halfway and normal vectors: float hdn = dot(Hn,Nn); // specialized “lit” function computes weights for // diffuse and specular parts: float4 litV = lit(ldn,hdn,SpecExpon); float4 diffContrib = SurfColor * ( litV.y * LightColor + AmbiColor); float4 specContrib = litV.y*litV.z * LightColor; // sum of diffuse and specular contributions: float4 result = diffContrib + specContrib; OUT.col = result; return OUT; // output of pixel shader } High-level shading languages (Cg)