Pixel shaders

19
Pixel shaders: programming the GPU By: Venkata Nanda Kishore

description

 

Transcript of Pixel shaders

Page 1: Pixel shaders

Pixel shaders: programming the GPU

By: Venkata Nanda Kishore

Page 2: Pixel shaders

What is a Shader?

Set of Software InstructionsDegree of FlexibilityTraits of an entity

Page 3: Pixel shaders

Entities,Traits And Degree

Entities:PixelsVerticesGeometric shapes

Traits:Pixels: z-buffer, alpha values etc.Vertices: Texture co-ordinates, Color

Page 4: Pixel shaders

Types of Shaders

Initially, Shaders are just pixel shaders

Types of Shaders:Pixel ShadersVertex ShadersGeometric Shaders

But the name remained as it was!

Page 5: Pixel shaders

GRAPHICS PIPELINE

VertexShaders RasteriserGeometric

Shaders Pixel Shaders

Page 6: Pixel shaders

Effect of Shaders

1. Replaced hard coded effects2. Gives a programmable alternativeFixed Function PipelineProgrammable Function Pipeline3.Parallel Programming

Page 7: Pixel shaders

Programming Shaders

• HLSL (Direct 3d)• GLSL (OpenGl)• Cg(Nvidia + Microsoft)

Page 8: Pixel shaders

Cg

• What is Cg?• Is it similar to C?• Is it the right question?• Can we use it as a general programming

language?

Page 9: Pixel shaders

Background of Cg

• Use of Assembly level language• What is assembly level?• What is high level,low level and middle level?• How easy is it?

Page 10: Pixel shaders

Example of assembly level languagefib: mov edx, [esp+8] cmp edx, 0 ja @f mov eax, 0 ret @@: cmp edx, 2 ja @f mov eax, 1 ret @@: push ebx mov ebx, 1 mov ecx, 1 @@: lea eax, [ebx+ecx] cmp edx, 3 jbe @f mov ebx, ecx mov ecx, eax dec edx jmp @b @@: pop ebx ret

Page 11: Pixel shaders

Advantage of Cg

• Portable• Easy• Optimize the code

Page 12: Pixel shaders

Games that Use Cg

HIT MAN : Blood MoneyBATTLE FIELD 2FAR CRYRACERAnd many more

Page 13: Pixel shaders

Syntax and Semantics(1)

Data Types:int, float, half, fixed, bool, sampler*Operators: Arithmetic and logical operators as in CAdditional: arithmetic operations for vector and matrix operrations

Page 14: Pixel shaders

Syntax and Semantics(2)

Functions and Control Structures: Similar to C

Standard Cg Library: Specialised GPU programming tasksEg. Texture Mapping functions tex1D and tex2D

Cg Runtime Library:Can be used with OpenGl or DirectX

Page 15: Pixel shaders

Example Shaderstruct VertIn //Input vertex { float4 pos : POSITION; float4 color : COLOR0; }; // output vertex struct VertOut { float4 pos : POSITION; float4 color : COLOR0; }; // vertex shader main entry VertOut main(VertIn IN, uniform float4x4 modelViewProj) { VertOut OUT; OUT.pos = mul(modelViewProj, IN.pos); //positionOUT.color = IN.color; // copy input color to output OUT.color.z = 1.0f; // blue component of color = 1.0f return OUT; }

Page 16: Pixel shaders

Loading Pixel Shaders in OpenGL

1.Loading extensions:glEnable(GL_FRAGMENT_PROGRAM_ARB);

2.Create Shader Number:glGenProgramsARB(1, &shader_num); glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader_num);

Page 17: Pixel shaders

Actually Loading the Shader

• glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(program_string), program_string);

• glDisable(GL_FRAGMENT_PROGRAM_ARB);

• http://joshbeam.com/articles/cg_pixel_shaders_in_opengl/

Page 18: Pixel shaders

Examples

https://support.steampowered.com/kb_article.php?ref=4360-TPJL-2065Gives what games need what version of pixel shadershttp://www.nvidia.com/object/feature_pixelshader.htmlhttp://www.toymaker.info/Games/html/pixel_shaders.html