
Assembly Shader Language. Chapter 9. What is ASM Shader?. GPU is Graphical Processing Unit of video card .
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Chapter 9
Pixel Shader is to manage color for every pixel in the rendered 3D object. Suppose that we have three texture pictures, then we can do the following different pixel operations by ASM pixel Shader
1. Show texture1 2. Show texture2, 3. Show texture3
All those can be done by very simple ASM Shader code
ASM Pixel Shader has the following variables:
1. r0, r1, … are temporary variables. If r0 is used as the last calculation result, it is the color output.
device.setTexture(0, texture0); // this is t0
device.setTexture(1, texture1); // this is t1
However, we must call
device.SetTextureStageState(1, TextureStageStates.TextureCoordinateIndex, 0);
To enable ASM Shader to get data of t1.
ps.1.0tex t0tex t2mov r1, 1-t0mul r0, r1, t2mov r0, 1-r0;
ps.1.0
;transform texture coords tu, tv into color (r,g,b)
; r=tu, g=tv, b =0
texcoord t0
mov r0, t0
private PixelShader CreatePixelShader(string strFilename) {
GraphicsStream code = ShaderLoader.FromFile(strFilename, null, ShaderFlags.None); return new PixelShader(device, code); }
PixelShader shad1 = CreatePixelShader( "shad1.psh");PixelShader shad2 = CreatePixelShader( "shad2.psh"); . . . . . . .PixelShader shad8 = CreatePixelShader( "shad6.psh"); PixelShader shad9 = CreatePixelShader( "shad7.psh");
if(shader_flag==1)device.PixelShader = shad2
Vertex Shader is to manage position for every point before drawing. For example, we can use vertex shader to do the same dolphin animation.
Because we need to pass multiple vertices data to Shader, we need use VertexElement to define VertexDeclararion object, which make possible to enable shader to know the vertex input data from application.
public VertexElement(short stream,short offset, DeclarationType declType, DeclarationMethod declMethod, DeclarationUsage declUsage,byte usageIndex);
The following table are some DeclarationType samples
The following table are some DeclarationUsage samples
Method device.SetVertexShaderConstant(…) is to pass data to GPU Shader. It has the following formats
Note1: The first integer argument will determine the data name in GPU Shader Note 2: Any input data beginning with letter 'c'.
All matrices, including Viewpoint matrix, Projective matrix and any matrix that determines the position of 3D object must be passed to Shader too. However, any matrix must be transposed before calling SetVertexShaderConstant() function. We no longer need to call the followings:
device.Transform.View = MatrixView;
device.Transform.Projection = MatrixProjective;
device.Transform.World = MatrixPosition;
Actually they are disabled by Shader operations
Load Shader code from vertex shader file with extension *.vsh
GraphicsStream stream =ShaderLoader.FromFile( "DolphinTween.vsh", null, ShaderFlags.None);
VertexShader dolphinVertexShader = new VertexShader(device, stream)
Then in Render() function, we call
device.VertexDeclaration = dolphinVertexDeclaration;device.VertexShader = dolphinVertexShader; // pass codeSetVShaderParameters(); // pass data
Note: We still need to call functions like:
device.SetStreamSource(…);device.Indices = dolphinIndexBuffer;device.DrawIndexedPrimitives(…)
VertexDeclaration decl;VertexShader vShader ;
new VertexElement[] velements = new VertexElement[] { new VertexElement(0, 0, DeclarationType.Float3,
DeclarationMethod.Default, DeclarationUsage.Position, 0),
new VertexElement(0, 12, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0), VertexElement.VertexDeclarationEnd };
decl = new VertexDeclaration(device, velements)
vShader = new VertexShader(device, ShaderLoader.FromFile( "rotation.vsh", null, ShaderFlags.None));
Note: In shader, matrix mat will be c4
;---------------------------------------------------------; Constants specified by the app; c4 = matWorldViewProjection;---------------------------------------------------------vs.1.1
dcl_position0 v0dcl_color0 v1
; rotation
mov r0, v0m4x4 oPos, r0, c4
; color
mov oD0, v1
We have three given Dolphin meshes.
In the last chapter, we directly to do the mesh interpolation.
Now we want design Shader operation to do the exactly same Dolphin mesh interpolation.
Device device;
Matrix worldMatrix = Matrix.Identity;Matrix viewMatrix = Matrix.Identity; Matrix projectionMatrix = Matrix.Identity;
Material dolphinMtrl;VertexBuffer dolphinVertexBuffer1 = null;VertexBuffer dolphinVertexBuffer2 = null; VertexBuffer dolphinVertexBuffer3 = null;IndexBuffer dolphinIndexBuffer = null;
int numDolphinVertices = 0;int numDolphinFaces = 0;
VertexDeclaration dolphinVertexDeclaration = null; VertexShader dolphinVertexShader = null;Vector3 vLight = new Vector3(0, -1, 0);
new VertexElement(1, 12, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 1),
private void SetDolphinVertexBuffer(){
numDolphinVertices = dolphinMesh1.NumberVertices ;numDolphinFaces = dolphinMesh1.NumberFaces ;
// Copy vertices for mesh 01pMeshSourceVB = dolphinMesh1.VertexBuffer;
for(int k=0; k<numDolphinVertices; k++){ dst[k].p = src[k].Position; dst[k].n = src[k].Normal;} dolphinVertexBuffer2.Unlock();pMeshSourceVB.Unlock();pMeshSourceVB.Dispose();
dolphinVertexBuffer3.Unlock();
private void SetVShaderParameters() // pass data to GPU{
Vector4 fLight = new Vector4(0.0f, 1.0f, 0.0f, 0.0f);
Matrix mat = matDolphin * viewMatrix * projectionMatrix;Matrix matTranspose= Matrix.TransposeMatrix(mat);Matrix matCamera = matDolphin * viewMatrix;Matrix matCameraTranspose = Matrix.TransposeMatrix(matCamera);Matrix matViewTranspose= Matrix.TransposeMatrix(viewMatrix);Matrix matProjTranspose= Matrix.TransposeMatrix(projectionMatrix); Vector4 vZero = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);Vector4 vOne = new Vector4(1.0f, 0.5f, 0.2f, 0.05f);
// Set the vertex shader constants
device.SetVertexShaderConstant(0, vZero );device.SetVertexShaderConstant(1, vOne );device.SetVertexShaderConstant(2, vWeight);device.SetVertexShaderConstant(4, matTranspose ) ;device.SetVertexShaderConstant(8, matCameraTranspose); device.SetVertexShaderConstant(12, matViewTranspose );device.SetVertexShaderConstant(19, fLightDolphinSpace );device.SetVertexShaderConstant(20, fLight );device.SetVertexShaderConstant(21, fDiffuse);device.SetVertexShaderConstant(22, fAmbient);
}
Constants specified by the last function
; c0 = ( 0, 0, 0, 0 ); c1 = ( 1, 0.5, 2, 4 ); c2 = ( weight1, weight2, weight3, 0 ); c4-c7 = matWorldViewProjection; c8-c11 = matWorldView; c19 = light direction (in model space); c21 = material diffuse color * light diffuse color; c22 = material ambient color; Actually they are memory address
Vertex components (as specifiedin the vertex DECL)
; v0 = Position; v3 = Normal; v6 = Texcoords
They are called built-in registers, beginning with letter ‘o’
Input variables in Shader have names begin with letter ‘c’. Free variables in Shader have names begin with letter ‘r’. We also can declare variables begin with letter ‘v’ .
vs.1.1 ; vertex shader version
dcl_position0 v0dcl_position1 v1dcl_position2 v2dcl_normal1 v4dcl_normal2 v5dcl_texcoord0 v6
; Tween the 3 positions (v0,v1,v2) into one position
mul r0, v0, c2.xmul r1, v1, c2.ymul r2, v2, c2.zadd r3, r0, r1add r3, r3, r2
; Transform position to the clipping space
m4x4 oPos, r3, c4
; Lighting calculation;Tween the 3 normals (v3,v4,v5) into one normal
mul r0, v3, c2.xmul r1, v4, c2.ymul r2, v5, c2.zadd r3, r0, r1add r3, r3, r2
; Do the lighting calculation
dp3 r1.x, r3, c19 ; r1 = normal dot lightmax r1.x, r1.x, c0.x ; if dot < 0 then dot = 0mul r0, r1.x, c21 ; Multiply with diffuseadd r0, r0, c22 ; Add in ambientmin oD0, r0, c1.x ; clamp if > 1
;Texture coordinates
; Gen tex coords from vertex xz position
mul oT0.xy, c1.y, r9.xz