Shaders (some slides taken from David M. course)

Similar documents
CS GPU and GPGPU Programming Lecture 7: Shading and Compute APIs 1. Markus Hadwiger, KAUST

Shaders Part II. Traditional Rendering Pipeline

Lecture 2. Shaders, GLSL and GPGPU

Graphics Hardware. Instructor Stephen J. Guy

Module Contact: Dr Stephen Laycock, CMP Copyright of the University of East Anglia Version 1

X. GPU Programming. Jacobs University Visualization and Computer Graphics Lab : Advanced Graphics - Chapter X 1

Shaders. Slide credit to Prof. Zwicker

Tutorial on GPU Programming #2. Joong-Youn Lee Supercomputing Center, KISTI

Graphics Hardware. Graphics Processing Unit (GPU) is a Subsidiary hardware. With massively multi-threaded many-core. Dedicated to 2D and 3D graphics

Pipeline Operations. CS 4620 Lecture Steve Marschner. Cornell CS4620 Spring 2018 Lecture 11

Real - Time Rendering. Graphics pipeline. Michal Červeňanský Juraj Starinský

Basics of GPU-Based Programming

Pipeline Operations. CS 4620 Lecture 14

Cg 2.0. Mark Kilgard

Shader Series Primer: Fundamentals of the Programmable Pipeline in XNA Game Studio Express

CIS 536/636 Introduction to Computer Graphics. Kansas State University. CIS 536/636 Introduction to Computer Graphics

2.11 Particle Systems

COMP371 COMPUTER GRAPHICS

Graphics Hardware, Graphics APIs, and Computation on GPUs. Mark Segal

CHAPTER 1 Graphics Systems and Models 3

CS 354R: Computer Game Technology

The Application Stage. The Game Loop, Resource Management and Renderer Design

12.2 Programmable Graphics Hardware

Rendering Objects. Need to transform all geometry then

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

CS 381 Computer Graphics, Fall 2008 Midterm Exam Solutions. The Midterm Exam was given in class on Thursday, October 23, 2008.

Programmable Graphics Hardware

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

Programming Graphics Hardware

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

Evolution of GPUs Chris Seitz

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

Introduction to Shaders.

Lecture 5 Vertex and Fragment Shaders-1. CITS3003 Graphics & Animation

Software Engineering. ò Look up Design Patterns. ò Code Refactoring. ò Code Documentation? ò One of the lessons to learn for good design:

Could you make the XNA functions yourself?

Graphics Processing Unit Architecture (GPU Arch)

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

1.2.3 The Graphics Hardware Pipeline

CS GPU and GPGPU Programming Lecture 2: Introduction; GPU Architecture 1. Markus Hadwiger, KAUST

Rendering approaches. 1.image-oriented. 2.object-oriented. foreach pixel... 3D rendering pipeline. foreach object...

Rasterization Overview

Pipeline Operations. CS 4620 Lecture 10

CS427 Multicore Architecture and Parallel Computing

SHADER PROGRAMMING. Based on Jian Huang s lecture on Shader Programming

DirectX Programming #4. Kang, Seongtae Computer Graphics, 2009 Spring

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

Lab 3 Shadow Mapping. Giuseppe Maggiore

Supplement to Lecture 22

Mattan Erez. The University of Texas at Austin

A Trip Down The (2011) Rasterization Pipeline

Programming with OpenGL Part 3: Shaders. Ed Angel Professor of Emeritus of Computer Science University of New Mexico

CS 4620 Program 3: Pipeline

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

CS451Real-time Rendering Pipeline

Spring 2009 Prof. Hyesoon Kim

- Rasterization. Geometry. Scan Conversion. Rasterization

CSE 167: Lecture #8: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

Spring 2011 Prof. Hyesoon Kim

Graphics Hardware. Computer Graphics COMP 770 (236) Spring Instructor: Brandon Lloyd 2/26/07 1

3D Programming. 3D Programming Concepts. Outline. 3D Concepts. 3D Concepts -- Coordinate Systems. 3D Concepts Displaying 3D Models

Computer Graphics (CS 543) Lecture 10: Soft Shadows (Maps and Volumes), Normal and Bump Mapping

CS4621/5621 Fall Computer Graphics Practicum Intro to OpenGL/GLSL

Beginning Direct3D Game Programming: 1. The History of Direct3D Graphics

Computer Graphics Coursework 1

Introduction to Visualization and Computer Graphics

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

Objectives Shading in OpenGL. Front and Back Faces. OpenGL shading. Introduce the OpenGL shading methods. Discuss polygonal shading

OPENGL RENDERING PIPELINE

Blis: Better Language for Image Stuff Project Proposal Programming Languages and Translators, Spring 2017

Advanced Rendering Techniques


General Purpose Computation (CAD/CAM/CAE) on the GPU (a.k.a. Topics in Manufacturing)

Shader Programming CgFX, OpenGL 2.0. Michael Haller 2003

MXwendler Fragment Shader Development Reference Version 1.0

CS 428: Fall Introduction to. OpenGL primer. Andrew Nealen, Rutgers, /13/2010 1

OpenGl Pipeline. triangles, lines, points, images. Per-vertex ops. Primitive assembly. Texturing. Rasterization. Per-fragment ops.

Cg Toolkit. Cg 2.1 October 2008 Release Notes

Programmable Graphics Hardware

CS GPU and GPGPU Programming Lecture 3: GPU Architecture 2. Markus Hadwiger, KAUST

3D Rendering Pipeline

CS 130 Final. Fall 2015

CSE 167: Introduction to Computer Graphics Lecture #6: Lights. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2014

CSE4030 Introduction to Computer Graphics

COMP30019 Graphics and Interaction Rendering pipeline & object modelling

Lecture outline. COMP30019 Graphics and Interaction Rendering pipeline & object modelling. Introduction to modelling

Deferred Rendering Due: Wednesday November 15 at 10pm

Interactive Computer Graphics A TOP-DOWN APPROACH WITH SHADER-BASED OPENGL

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

Introduction to the OpenGL Shading Language

Drawing a Crowd. David Gosselin Pedro V. Sander Jason L. Mitchell. 3D Application Research Group ATI Research

CS130 : Computer Graphics. Tamar Shinar Computer Science & Engineering UC Riverside

Advanced Rendering & Shading

EDAF80 Introduction to Computer Graphics. Seminar 3. Shaders. Michael Doggett. Slides by Carl Johan Gribel,

CSE 167: Introduction to Computer Graphics Lecture #7: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Spring Quarter 2016

PROFESSIONAL. WebGL Programming DEVELOPING 3D GRAPHICS FOR THE WEB. Andreas Anyuru WILEY. John Wiley & Sons, Ltd.

lecture 18 - ray tracing - environment mapping - refraction

Graphics and Interaction Rendering pipeline & object modelling

Objectives. Introduce the OpenGL shading Methods 1) Light and material functions on MV.js 2) per vertex vs per fragment shading 3) Where to carry out

CSE 167: Introduction to Computer Graphics Lecture #5: Rasterization. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2015

Transcription:

Shaders (some slides taken from David M. course) Doron Nussbaum Doron Nussbaum COMP 3501 - Shaders 1 Traditional Rendering Pipeline Traditional pipeline (older graphics cards) restricts developer to texture and the 3-term lighting model Pixel information was interpolated to vertices Shaders Lift the restrictions Allow developers to manipulate outcome Result vertex shaders Pixel/fragment shaders Vertex Operations Transformation and Lighting Primitive Assembly Viewport Culling Clipping Pixel Operations Rasterization Pixel Colouring Texture Mapping Per pixel operation (Blending) Frame buffer operations Doron Nussbaum COMP 3501 - Shaders 2

Vertex Shader Act in parallel across all vertices Responsible for transforming Initial lighting of vertex Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation Compute temporary variables to be consumed by fragment shader Per-vertex processing only information from one vertex available Transformation and Lighting Primitive Assembly Viewport Culling Clipping Vertex Shader Doron Nussbaum COMP 3501 - Shaders 3 Fragment (pixel) Shader Act in parallel across all fragments Rasterization Responsible for computing depth and color information Only information from current fragment available Operations on interpolated values Texture access (multipass shaders) Pixel Colouring Texture Mapping Per pixel operation (Blending) Pixel Shader Produces Colour Frame buffer operations Doron Nussbaum COMP 3501 - Shaders 4

Shading languages (last) High level shading languages available Three main ones: Cg (NVIDIA's "C for graphics") HLSL (MS's "high level shading language") GLSL (OpenGL's shading language) All quite similar, differ in details of API DirectX shader code is added during runtime Shader code is written in a separate file Requires transfer of data to the shader code Doron Nussbaum COMP 3501 - Shaders 5 The Graphics Pipeline Transformations & Lighting Vertex Shading Clipping Rasterization and Interpolation Texturin g Pixel Shading Frame Buffer Operations Doron Nussbaum COMP 3501 - Shaders 6 6

Vertex Shader(s) Replaces fixed functionality of vertex processor Normally get: vertex transformation normal transformation illumination Now, can write programs that do anything same program on all vertices Doron Nussbaum COMP 3501 - Shaders 7 7 Vertex Shaders Input: Built-in attributes color normal position texture coordinate User-defined attributes Texture maps Doron Nussbaum COMP 3501 - Shaders 8 8

Vertex Shaders Transform and light a single vertex Output: position color user-defined variables (for fragment shader) Doron Nussbaum COMP 3501 - Shaders 9 9 Vertex Shader Changing vertex position in a shader create simple animations Use time as input, compute x(t), θ(t) perform displacement mapping (to mesh resolution) Generally, modify the geometry at runtime Doron Nussbaum COMP 3501 - Shaders 10 10

Rasterization and Interpolation Turns the space between vertices into fragments Computes input values for fragments by interpolating values from vertices Doron Nussbaum COMP 3501 - Shaders 11 11 Pixel Shader Operates on fragments Input: color texture coordinates user-defined variables (from vertex shader) texture maps Cannot access other pixels although, texture can be exploited for this Doron Nussbaum COMP 3501 - Shaders 12 12

Pixel Shader Output: color (RGBA) depth Note, cannot change position at this point But, can compute color in sophisticated way, in parallel at each pixel Doron Nussbaum COMP 3501 - Shaders 13 13 Shader input Previously mentioned inputs available pervertex (per-pixel) Also have shader parameters that can be set (like global variables) E.g., direction of sun, location of viewer (same for all primitives) E.g., specific parameters (material glossiness, what texture to use) Doron Nussbaum COMP 3501 - Shaders 14 14

Phong and Gouraud Shading Traditional Gouraud shading: per-vertex lighting, interpolated to pixels Various problems: Visible mesh boundaries Strange effects at low mesh resolution Phong shading: normals interpolated, perpixel lighting: done in pixel shader Doron Nussbaum COMP 3501 - Shaders 15 15 Render to Texture Shaders are memoryless Also, limit to how many instructions in shader (varies with card) Can render display buffer to texture, then use texture as input to later (or same) shader Or, just display texture on object eg, mirror Doron Nussbaum COMP 3501 - Shaders 16 16

Render to Texture Simple postprocessing effects: render to texture apply pixel shader involving texture draw texture to screen Create quad covering screen, apply texture Color modification, darken/brighten, fade/dissolve Doron Nussbaum COMP 3501 - Shaders 17 17 Shaders in DirectX Write a single.fx file with: list of shader parameters structure definitions for shader I/O vertex shader pixel shader technique and pass definition Doron Nussbaum COMP 3501 - Shaders 18 18

Data flow Vertex Shader Input Vertex Shader Pixel Shader Input Pixel Shader Pixel Shader Output 19 Doron Nussbaum COMP 3501 - Shaders 19 Shader I/O Semantics Markers that suggest how data is passed into and out of the shaders e.g., put "position" data in POSITION semantic Example vertex shader input: struct vs_in { float4 pos : POSITION0; float4 col : COLOR0; }; Doron Nussbaum COMP 3501 - Shaders 20 20

Available semantics Differ depending on what the structure is position semantic not available for pixel shader output, for example TEXCOORD semantic for user-defined variables Can be used multiple times: TEXCOORD0 for first, then TEXCOORD1, TEXCOORD2... Doron Nussbaum COMP 3501 - Shaders 21 21 Vertex Shader Input Semantics POSITION vertex location in space COLOR for the vertex color NORMAL surface normal TEXCOORD texture coordinates, also "generic" PSIZE point size (used with point sprites, chiefly for particle system effects) Doron Nussbaum COMP 3501 - Shaders 22 22

Vertex Shader Output Semantics subset of the inputs POSITION COLOR TEXCOORD PSIZE Doron Nussbaum COMP 3501 - Shaders 23 23 Pixel Shader Input Semantics Only COLOR and TEXCOORD are allowed Note: considered good practice to use vertex shader output as pixel shader input but permitted semantics different! notably, POSITION required for vs_out, prohibited by ps_in "Illegal" semantics ignored by compiler in definition, but DO NOT USE in ps code Doron Nussbaum COMP 3501 - Shaders 24 24

Pixel Shader Output Semantics COLOR DEPTH depth value for depth test In routine operation, one color value (ultimate pixel color) and one depth value Can have more outputs if multipass shader, unconventional operation Doron Nussbaum COMP 3501 - Shaders 25 25 Techniques and Passes technique mytechnique { // one or more passes, ordinarily one pass mypass { vertexshader = compile vs_1_1 myvs(); pixelshader = compile ps_2_0 myps(); } } Doron Nussbaum COMP 3501 - Shaders 26 26

Shader Code Written in C-like HLSL if, for, = for assignment... scalar, vector, and matrix types int, float float2, float3, float4 float4x4 Texture type (2D texture) Doron Nussbaum COMP 3501 - Shaders 27 27 Intrinsic Functions Some functions are built in Various mathematical functions math (exp, pow, log) trigonometric (sin, cos, tan, asin...) vector (dot, cross) "housekeeping" (clamp, lerp, abs, max) Functions for dealing with texture Partial list (common ones) in textbook Doron Nussbaum COMP 3501 - Shaders 28 28

Shader Code Take control of the pipeline Very abstract right now, but examples to come in the following weeks texture procedural texture basic lighting (Phong shading) specialized lighting particle systems Doron Nussbaum COMP 3501 - Shaders 29 29 Connecting your Shader May need a custom vertex format, if your vertex shader input is different from all existing vertex formats Set all your parameters Link to project, configure Doron Nussbaum COMP 3501 - Shaders 30 30

Recap Can program parts of the graphics pipeline vertex shader output is position, color, texture coordinates pixel shader output is color for DirectX, use HLSL, a C-like language with many built-in functions Doron Nussbaum COMP 3501 - Shaders 31 Recap Things to know: graphics pipeline tasks of vertex and pixel shaders rasterization and interpolation vertex and pixel shader input and output semantics To learn by practice: writing your own shaders Doron Nussbaum COMP 3501 - Shaders 32