Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Shading Languages & HW Giovanni Civati Dept. of Information Tecnology University of Milan, Italy Milan, 26 th May 2004."— Presentation transcript:

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

2 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)

3 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

4 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

5 The die Hardware R300 Large amount taken up by the FPPU

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

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

8 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

9 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

10 Vertex processing Hardware R300 – The 3D Pipeline

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

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

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

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

15 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

16 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

17 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

18 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

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

20 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

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

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

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

24 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

25 Shading Languages Programmable Pipeline Rendering pipeline

26 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

27 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

28 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

29 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

30 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)

31 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)

32 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)

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

34 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)

35 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)

36 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)

37 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)

38 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)

39 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)

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

41 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)

42 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)

43 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)

44 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)

45 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)

46 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

47 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

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

49 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

50 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

51 Shading Languages Workflow High-level shading languages (Cg)

52 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)

53 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)

54 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)

55 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)

56 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)

57 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)

58 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)

59 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)

60 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)

61 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)


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

Similar presentations


Ads by Google