Pixel shaders: programming the GPUBy: Venkata Nanda Kishore
What is a Shader?Set of Software InstructionsDegree of FlexibilityTraits of an entity
Entities,Traits And DegreeEntities:	Pixels	Vertices	Geometric shapesTraits:	Pixels: z-buffer, alpha values etc.Vertices: Texture co-ordinates, Color
Types of ShadersInitially, Shaders are just pixel shadersTypes of Shaders:Pixel Shaders
Vertex Shaders
Geometric ShadersBut the name remained as it was!
GRAPHICS PIPELINEGeometric ShadersRasteriserVertexShadersPixel Shaders
Effect of ShadersReplaced hard coded effectsGives a programmable alternativeFixed Function PipelineProgrammable Function Pipeline3.Parallel Programming
Programming ShadersHLSL (Direct 3d)GLSL (OpenGl)Cg(Nvidia + Microsoft)
CgWhat is Cg?Is it similar to C?Is it the right question?Can we use it as a general programming language?
Background of CgUse of Assembly level languageWhat is assembly level?What is high level,low level and middle level?How easy is it?
Example of assembly level languagefib: movedx, [esp+8]cmpedx, 0 ja @f moveax, 0 ret@@: cmpedx, 2 ja @fmoveax, 1ret @@: push ebxmovebx, 1movecx, 1 @@: lea eax, [ebx+ecx] cmpedx, 3 jbe @f movebx, ecxmovecx, eaxdecedxjmp@b@@: pop ebx ret
Advantage of CgPortableEasyOptimize the code
Games that Use CgHIT MAN :  Blood MoneyBATTLE FIELD 2FAR CRYRACERAnd many more
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
Syntax and Semantics(2)Functions and Control Structures: Similar to CStandard Cg Library: Specialised GPU programming tasksEg. Texture Mapping functions tex1D and tex2DCg Runtime Library:Can be used with OpenGl or DirectX
Example ShaderstructVertIn			    //Input vertex{ float4 pos : POSITION; float4 color : COLOR0; };            // output vertexstructVertOut{ float4 pos : POSITION; float4 color : COLOR0; }; // vertex shader main entryVertOutmain(VertIn IN, uniform float4x4 modelViewProj) { VertOut OUT; OUT.pos= mul(modelViewProj, IN.pos); //positionOUT.color= IN.color; // copy input color to outputOUT.color.z= 1.0f; // blue component of color = 1.0freturn OUT;	 }
Loading Pixel Shaders in OpenGL1.Loading extensions:glEnable(GL_FRAGMENT_PROGRAM_ARB);2.Create Shader Number:glGenProgramsARB(1, &shader_num); glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader_num);
Actually Loading the ShaderglProgramStringARB(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/

Pixel shaders

  • 1.
    Pixel shaders: programmingthe GPUBy: Venkata Nanda Kishore
  • 2.
    What is aShader?Set of Software InstructionsDegree of FlexibilityTraits of an entity
  • 3.
    Entities,Traits And DegreeEntities: Pixels Vertices GeometricshapesTraits: Pixels: z-buffer, alpha values etc.Vertices: Texture co-ordinates, Color
  • 4.
    Types of ShadersInitially,Shaders are just pixel shadersTypes of Shaders:Pixel Shaders
  • 5.
  • 6.
    Geometric ShadersBut thename remained as it was!
  • 7.
  • 8.
    Effect of ShadersReplacedhard coded effectsGives a programmable alternativeFixed Function PipelineProgrammable Function Pipeline3.Parallel Programming
  • 9.
    Programming ShadersHLSL (Direct3d)GLSL (OpenGl)Cg(Nvidia + Microsoft)
  • 10.
    CgWhat is Cg?Isit similar to C?Is it the right question?Can we use it as a general programming language?
  • 11.
    Background of CgUseof Assembly level languageWhat is assembly level?What is high level,low level and middle level?How easy is it?
  • 12.
    Example of assemblylevel languagefib: movedx, [esp+8]cmpedx, 0 ja @f moveax, 0 ret@@: cmpedx, 2 ja @fmoveax, 1ret @@: push ebxmovebx, 1movecx, 1 @@: lea eax, [ebx+ecx] cmpedx, 3 jbe @f movebx, ecxmovecx, eaxdecedxjmp@b@@: pop ebx ret
  • 13.
  • 14.
    Games that UseCgHIT MAN : Blood MoneyBATTLE FIELD 2FAR CRYRACERAnd many more
  • 15.
    Syntax and Semantics(1)DataTypes:int, float, half, fixed, bool, sampler*Operators: Arithmetic and logical operators as in CAdditional: arithmetic operations for vector and matrix operrations
  • 16.
    Syntax and Semantics(2)Functionsand Control Structures: Similar to CStandard Cg Library: Specialised GPU programming tasksEg. Texture Mapping functions tex1D and tex2DCg Runtime Library:Can be used with OpenGl or DirectX
  • 17.
    Example ShaderstructVertIn //Input vertex{ float4 pos : POSITION; float4 color : COLOR0; }; // output vertexstructVertOut{ float4 pos : POSITION; float4 color : COLOR0; }; // vertex shader main entryVertOutmain(VertIn IN, uniform float4x4 modelViewProj) { VertOut OUT; OUT.pos= mul(modelViewProj, IN.pos); //positionOUT.color= IN.color; // copy input color to outputOUT.color.z= 1.0f; // blue component of color = 1.0freturn OUT; }
  • 18.
    Loading Pixel Shadersin OpenGL1.Loading extensions:glEnable(GL_FRAGMENT_PROGRAM_ARB);2.Create Shader Number:glGenProgramsARB(1, &shader_num); glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader_num);
  • 19.
    Actually Loading theShaderglProgramStringARB(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/