The Gewiz Language by Kyle Hegeman Chris Beckenbach Laura Grier Programming Language Paradigms CIS6930CT Dr. Eggen April 25, 2002 The Gewiz Language Introduction The latest generation of video cards contain a programmable processor. Two of these are the Nvidia Gforce3 and the ATI Radeon 7500+ cards. The programming features are used to customize the graphics processing being performed by the card. Vertex Program A vertex program is an assembly language program that is executed on the Graphics Processing Unit of a video card with the purpose of customizing the display of the vertex. • Assembly language interface to T&L unit • GPU instruction set to perform all vertex math • Reads an untransformed, unlit vertex • Creates a transformed vertex • Optionally creates • Lights a vertex • Creates texture coordinates • Creates fog coordinates • Creates point sizes More information can be found about vertex programs at: http://developer.nvidia.com/view.asp?IO=vertex_programs Proposal This is a high level language that abstracts the programmer from the low level details of programming for a particular card. When the program is compiled, the compiler will generate a series of assembly language instructions. These instructions can then be downloaded to the video card using the OpenGL graphics API. The assembly instructions generated change the lighting of the graphics predefined (in the input parameters) by the graphics designer. They do not create the actual graphics. The following assembly language instructions are supported: MOV MAD (in limited form) MUL ADD RCP RSQ (in limited form) DP3 DP4 move (assignment) multiply and add multiply add reciprocal (inverse) reciprocal square root dot product of 3 floats dot product of 4 floats A full explanation of these assembly language instructions can be found in the VertexPrograms.pdf found at: http://developer.nvidia.com/view.asp?IO=vertex_programs Extensions beyond the scope of this project could be made to target the DirectX API. Hardware Documentation Nvidia documentation for vertex programming http://oss.sgi.com/projects/ogl-sample/registry/NV/vertex_program.txt Language Design Data Types There are 3 data types supported by the language. float vertex – contains 4 floats matrix – contains 3 or 4 vectors Data Type Prefixes These are used instead of keywords to specify the data type. Prefix % ‘ “ Data Type float vertex matrix Example %Normalized ‘Position “Perspective Data Access The data types names must begin with the prefix, followed by a letter, then any number of letters, underscores, or digits following that. A user can access any of the types that make up that particular data type: “matrixname[0] // access first vector of matrix “matrixname[1].xy // access first and second floats of second vector of matrix ‘vectorname.y // access second float of vector These same access rules apply to the functions, so if the function normalize returns a vector, a float can be accessed in the following manner: normalize(‘vectorname).z // access the third float of the vector normalize has returned Input Parameters This list is a set of vertex types that maps to the v[] vertex attribute registers. These are used to pass the vertex program information about the vertex from the OpenGL API. A vertex can have up to 8 textures when multi-texturing is enabled, thus the long list of texture coordinates. Name input_position input_weight input_normal input_color0 input_color1 input_fog input_texture0 input_texture1 input_texture2 input_texture3 input_texture4 input_texture5 input_texture6 input_texture7 Description Position of the vertex Weight of the vertex Surface normal at the vertex Primary vertex color Secondary vertex color Fog coordinate Texture coordinate Texture coordinate Texture coordinate Texture coordinate Texture coordinate Texture coordinate Texture coordinate Texture coordinate Output Parameters This list is a vertex types that maps to the o[] vertex output registers. These hardware registers are used to pass the results of the vertex program back to the hardware for display purposes. A vertex program must assign a value to the output_position parameter to be valid. This is a requirement of the underlying hardware. Name output_ position output_color0 output_color1 output_back_color0 output_back_color1 output_fog output_point_size output_texture0 output_texture1 output_texture2 output_texture3 output_texture4 output_texture5 output_texture6 output_texture7 Description Homogenous Position of the vertex Primary Color Secondary Color Primary Color for Back Face Secondary Color for Back Face Fog Coordinate Point Size Texture Coordinate Texture Coordinate Texture Coordinate Texture Coordinate Texture Coordinate Texture Coordinate Texture Coordinate Texture Coordinate Operators This is a list of operators implemented by GeWiz. Other operators are mathematically viable, however they were not because they are not practical on the current hardware. For example any operation that would require the creation of a temporary matrix is not supported because that would require 4 temporary registers and the hardware only has 12. To support this feature would require making the user aware of how certain operations are implemented on the hardware. Instead matrices treated as read-only aliases to the c[] registers. Operator overloading has been implemented. + / * cross dot .xyzw Add Subtract Divide Multiply Cross Product Dot Product Order // Comment Add a vector to a vector or a float to a float Subtract a vector from a vector or a float from a float Divide a vertex by a float or a float by a float Multiply a vertex and a matrix, two vertices, two floats and a float by a vertex. Perform a cross product on two vectors Performs a dot product on two vectors This operator allows the user to specify the order of components of a vector. It also allows for a 3 component vector The rest of the line is ignored by the compiler Functions This is a list of the current functions. Due to time constraints only two predefined math functions exist, and function overloading was not implemented, as was suggested by the powerpoint slides. Function Matrix Return matrix Vertex vertex Normalize Inverse vertex float Parameters Indices to input parameters Indices to input parameters vertex float Description Creates a matrix with data from the input program registers (c[]) Creates a vertex with data from the input program registers (c[]) Takes a vector and returns a normalized version Takes a float and returns its inverse Known Bugs Some floats may not get assigned in these scenarios (the number of floats on both sides don’t have to match currently). ‘vect.xy = vector(1).wzy; vect.wzy = vector(1).x; Making sure unassigned variables were not used on the right hand side of assignment statements at compile time became too complex to implement (the powerpoint slides indicated that we had done that). The static semantics check for beginning and ending program name is broken. Program Original program from nutty.org DP4 R0.x,v[OPOS],c[0]; DP4 R0.y,v[OPOS],c[1]; DP4 R0.z,v[OPOS],c[2]; DP4 R0.w,v[OPOS],c[3]; DP4 R1.x,R0,c[4]; DP4 R1.y,R0,c[5]; DP4 R1.z,R0,c[6]; DP4 R1.w,R0,c[7]; - ModelView transformation - ModelView transformation - ModelView transformation - ModelView transformation - Projection transformation - Projection transformation - Projection transformation - Projection transformation DP3 R2.x, c[0], v[NRML]; DP3 R2.y, c[1], v[NRML]; DP3 R2.z, c[2], v[NRML]; - Normal transformation - Normal transformation - Normal transformation DP3 R2.w, c[8], R2; - Lighting calculation 1 MUL R3.xyzw,v[COL0].xyzw,R2.wwww; -Lighting calculation MOV o[COL0],R3; MOV o[HPOS],R1; - Output - Output Program 1 GeWiz version vertex_program main(): //Calculate the vertex color based on the direction to the light in world coordinates output_color0 = input_color0 * (Vector(8) dot (input_normal * Matrix(0 TO 2))); //Project the input vertex into homogenous screen coordinates output_position = input_position * Matrix(0 TO 3) * Matrix(4 TO 7); end main; Program 2 - Toon Shading from www.nutty.org DP4 R0.x,v[OPOS],c[0]; DP4 R0.y,v[OPOS],c[1]; DP4 R0.z,v[OPOS],c[2]; DP4 R0.w,v[OPOS],c[3]; - ModelView transformation - ModelView transformation - ModelView transformation - ModelView transformation DP4 o[HPOS].x,R0,c[4]; DP4 o[HPOS].y,R0,c[5]; DP4 o[HPOS].z,R0,c[6]; DP4 o[HPOS].w,R0,c[7]; - Projection transformation. Store immediatly in output. - Projection transformation - Projection transformation - Projection transformation DP3 R2.x, c[0], v[NRML]; DP3 R2.y, c[1], v[NRML]; DP3 R2.z, c[2], v[NRML]; - Normal transformation - Normal transformation - Normal transformation ADD R3,c[9],-R0; DP3 R3.w,R3,R3; RSQ R3.w,R3.w; - Get Vector from eye to vertex. - Get magnitude of vector. - Calculate reciprocal square root. MUL R3,R3,R3.w; - Multiply by recip square root. To make it normalized. DP3 o[TEX0].x,R2,c[8]; DP3 o[TEX1].x,R2,R3; - Dot product of normal, light vector, tone lookup. - Dot product of normal, eye vector, outline lookup. MOV o[COL0],v[COL0]; - Mov input color straight into output color. Toon Shading – GeWiz version vertex_program main(): 'pos = input_position * Matrix(0 TO 3); output_position = 'pos * Matrix(4 to 7); 'norm = input_normal * Matrix(0 TO 2); output_texture0.x = 'norm dot Vector(8); output_texture1.x = 'norm dot normalize(Vector(9) - 'pos); output_color0 = input_color0; end main;