How to write a Pixel Shader CMT3311. Aim The aim of these slides is to introduce you to enough HLSL that you get a general understanding of pixel shaders.

Slides:



Advertisements
Similar presentations
Algorithm Analysis Input size Time I1 T1 I2 T2 …
Advertisements

POST-PROCESSING SET09115 Intro Graphics Programming.
CSE 781 Anti-aliasing for Texture Mapping. Quality considerations So far we just mapped one point – results in bad aliasing (resampling problems) We really.
This terms course Last term we both worked on learning 2 things –Processing –The concepts of graphics etc. This term will focus more on the basic concepts.
Ray tracing. New Concepts The recursive ray tracing algorithm Generating eye rays Non Real-time rendering.
Exploration of bump, parallax, relief and displacement mapping
November 12, 2013Computer Vision Lecture 12: Texture 1Signature Another popular method of representing shape is called the signature. In order to compute.
Using effects within a SpriteBatch or as a post render effect
Texture Mapping. Texturing  process that modifies the appearance of each point on a surface using an image or function  any aspect of appearance can.
Week 11 - Wednesday.  Image based effects  Skyboxes  Lightfields  Sprites  Billboards  Particle systems.
Week 7 - Wednesday.  What did we talk about last time?  Transparency  Gamma correction  Started texturing.
Computer Vision Lecture 16: Texture
Informationsteknologi Wednesday, December 12, 2007Computer Graphics - Class 171 Today’s class OpenGL Shading Language.
Image processing. Image operations Operations on an image –Linear filtering –Non-linear filtering –Transformations –Noise removal –Segmentation.
Introduction to Shader Programming
GLSL I May 28, 2007 (Adapted from Ed Angel’s lecture slides)
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
Mohan Sridharan Based on slides created by Edward Angel GLSL I 1 CS4395: Computer Graphics.
Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides Shaders Feb 18, 2011 Creative Commons Attribution 3.0 (Except copyrighted.
Higher Computing Data Representation.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Real-time Graphical Shader Programming with Cg (HLSL)
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8.
A Crash Course in HLSL Matt Christian.
TERRAIN SET09115 Intro to Graphics Programming. Breakdown  Basics  What do we mean by terrain?  How terrain rendering works  Generating terrain 
Information Processing Notes for beginning our Excel Unit.
Computer Graphics The Rendering Pipeline - Review CO2409 Computer Graphics Week 15.
Term 2, 2011 Week 1. CONTENTS Problem-solving methodology Programming and scripting languages – Programming languages Programming languages – Scripting.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
OpenGL Shader Language Vertex and Fragment Shading Programs.
CSC508 Convolution Operators. CSC508 Convolution Arguably the most fundamental operation of computer vision It’s a neighborhood operator –Similar to the.
Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9.
Computer Graphics Matrices
OpenGL Shading Language
Graphics Lecture 2: Slide 1 Lecture 2 Transformations for animation.
11 Adding a Bread Bat Session Session Overview  We have created a cheese sprite that bounces around the display  We now need to create a bread.
Advanced D3D10 Shader Authoring Presentation/Presenter Title Slide.
Programming with OpenGL Part 3: Shaders Ed Angel Professor of Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive.
Instructor: Mircea Nicolescu Lecture 5 CS 485 / 685 Computer Vision.
GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global.
XP New Perspectives on Macromedia Dreamweaver MX 2004 Tutorial 5 1 Adding Shared Site Elements.
An Introduction to the Cg Shading Language Marco Leon Brandeis University Computer Science Department.
Computer Science – Game DesignUC Santa Cruz Tile Engine.
The Stingray Example Program CMT3311. Stingray - an example 2D game May be useful as a simple case study Most 2D games need to solve generic problems.
COMP 175 | COMPUTER GRAPHICS Remco Chang1/XX13 – GLSL Lecture 13: OpenGL Shading Language (GLSL) COMP 175: Computer Graphics April 12, 2016.
How to use a Pixel Shader CMT3317. Pixel shaders There is NO requirement to use a pixel shader for the coursework though you can if you want to You should.
Environmental Remote Sensing GEOG 2021
Shaders, part 2 alexandri zavodny.
Ying Zhu Georgia State University
Computer Graphics Imaging
Week 11 - Wednesday CS361.
CSE 455 HW 1 Notes.
Many-core Software Development Platforms
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
EE/CSE 576 HW 1 Notes.
Computer Vision Lecture 16: Texture II
UMBC Graphics for Games
Computer Animation Texture Mapping.
EE/CSE 576 HW 1 Notes.
POWERPOINT (PPT) KEY Elements: Know these features
Programming with OpenGL Part 3: Shaders
CIS 441/541: Introduction to Computer Graphics Lecture 15: shaders
03 | Creating, Texturing and Moving Objects
CS 480/680 Computer Graphics GLSL Overview.
Language Definitions Chap. 3 of Orange Book.
ECE/CSE 576 HW 1 Notes.
© Healthcare Inspirations. All rights reserved
CS 480/680 Fall 2011 Dr. Frederick C Harris, Jr. Computer Graphics
Presentation transcript:

How to write a Pixel Shader CMT3311

Aim The aim of these slides is to introduce you to enough HLSL that you get a general understanding of pixel shaders By the end you should be able to tweak existing shader programs in a meaningful way and have enough understanding to write your own

High Level Shading Language The documentation for HLSL comes with the DirectX SDK so you need to run Microsoft Visual Studio.Net 2003 and then go to help and search for HLSL to get it. HLSL is C like but is a separate language from C or C++ or C#. It has it own little quirks and is rather cryptic

Variable types HLSL has the usual variables like int, float, bool, struct, in, out, void, string, true, false and more. Vectors can be defined in several ways e.g. one way is float3 or float4 signify vectors with 3 or 4 components Vectors can be indexed by number 0,1,2,3, color: r,g,b,a or coordinate: x,y,z,w e.g. color.r is the same as color.x or color[0]

Variables Matrices are usually specified by having rows and columns encoded in the variable e.g. float4x4 gives a 4 by 4 matrix Variables can have storage class modifiers associated with them e.g. uniform signifies a variable will not change, extern signifies a variable is defined externally (i.e. in your XNA program) etc

Semantics Semantics are used link input and output with the graphics pipeline Pixel shader input semantics include COLOR[n] and TEXCOORD[n] – i.e. input can be colour and texture coordinates – and there may be more than one. The Pixel Shader output Semantic is usually just COLOR

Structs We have to define structs to hold our input data which is very simple for pixel shaders but can get more complex for vertex shaders The following defines a struct that has a 2d vector called TexCoord and it has the semantic TEXCOORD0 which means it is to be treated as a texture coordinate. This will be the input to our pixel shader

Intrinsic functions HLSL has a set of built in functions too numerous to cover here but they include things like the trig functions sin,cos tan etc. multiplication and interpolation functions, and texture indexing functions Its best to understand these functions by looking at examples in conjunction with reading the documentation

Sampler A sampler is needed to get data from a texture It can be used in various ways, e.g. to be associated with a texture and how the texture is filtered as its is processed

Effect files A shader or effect file has the extension.fx Given all we have discussed and recalling that an effect file contains techniques and passes we can now look at our first pixel shader. See PixelShaderSimple.zip.

Simple1.fx

Output using Simple1.fx – just shows the red channel

Vector assignment and Swizzling Vector component assignment can be done in the usual way e.g. color.r =oldColor.b but also by using swizzling which allows indexing of more that one component at time and swapping of components around. Note all the following possible assignments:

Greyscale Once you are set up for pixel shading it is very easy to produce new effects The formula for converting from color to greyscale is red*.3+green*.59+blue*.11 to account for the relative brightness of colours – so we can change the pixel shader to:

Greyscale

Image Negative

Filtering In image processing many different effects can be obtained by filtering A filter just specifies how surrounding pixels in an input image will affect the pixel in the output image For example with blurring we just make the current pixel the average of its surrounding pixels

Blurring A problem we have is that texture addresses are normalised – i.e. they scale between 0-1, in the pixel shader we don’t know the real width or height So we have to guess, if the texture was 400 x 400 a pixel width is 1/400 – In the following example the current pixel and it top, bottom, left, and right neighbours are averaged – see blur5.fx What will happen at the edges?

Edge detection To do edge detection you multiply the neighbours by -1, and the current pixel by the number of neighbours and add them all together The following mask uses all eight immediate neighbours and then converts the result to greyscale. See edge9.fx

Edge detection – edge9.fx

Edge detection

Radial functions If we can work out how far we are from the centre we can apply an effect as a function of the distance from the centre Since the centre is at.5,.5 we can calculate how far we are away from it by D=sqrt(((x-.5)*(x-.5))+y-.5)*(y-.5)) We can then use interpolation to calculate values as a function of distance from the centre (using the lerp() function)

Radial blend Max distance is.7071 so we divide distance by this to make it range from 0 to 1 Note the use of the lerp() function to interpolate between two colours

Radial blend from the image to red at the border

Radial Blur This implements radial blurring

Radial blur

Radial edge detection

Radial Negative

Dynamic Pixel shaders So far the pixels shaders discussed have all been static – the image stays the same More interesting effects can be achieved if shader behaviour is varied as a function of time and is controlled by your program This can be achieved by passing variables from your program to the shader The previous shaders could work with any program – dynamic shaders however are dependant on the calling program passing values to them – in fact the majority of shaders have this dependancy

Displacement A lot of effects can be achieved by displacing pixels in the image - e.g. transitioning off the screen In the following the image is displaced according to the sin function – it makes it appear to shake like a jelly In wavyTimer.fx you can change the speed and number of cycles The XNA program supplies values for the variables time (to move the waves along) and magnitude (to specify how big the waves are)

wavyTimer.fx

Passing variables to the shader The XNA program defines variables for time and magnitude In update if the A button is pressed then magnitude is set to a non zero value In subsequent calls to update the magnitude is diminished over time to zero In draw the effect parameters are linked to the variable time and magnitude before the spriteBatch.draw is called See PixelShaderWobble.zip

Variables defined in class, set in Update(), and passed in Draw()

wavyTimer.fx – magnitude of waves is diminished over time

XNA Bloom example A much more complicated example that has four passes to produce a bloom effect can be found at: mentaspx/archive/2007/01/01/Bloom- Postprocess-Sample.aspx

Bloom example Reproduced according to the Microsoft Permissive Licence, see last slide

Microsoft Permissive Licence Microsoft Permissive License (Ms-PL) This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software. 1. Definitions The terms “reproduce,” “reproduction,” “derivative works,” and “distribution” have the same meaning here as under U.S. copyright law. A “contribution” is the original software, or any additions or changes to the software. A “contributor” is any person that distributes its contribution under this license. “Licensed patents” are a contributor’s patent claims that read directly on its contribution. 2. Grant of Rights (A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create. (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software. 3. Conditions and Limitations (A) No Trademark License- This license does not grant you rights to use any contributors’ name, logo, or trademarks. (B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically. (C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software. (D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license. (E) The software is licensed “as-is.” You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.