1//
2//------------------------------------------------------------
3// Copyright(c) 2009-2010 by Digital Media Professionals Inc.
4// All rights reserved.
5//------------------------------------------------------------
6// This source code is the confidential and proprietary
7// of Digital Media Professionals Inc.
8//------------------------------------------------------------
9//
10//
11// VShader.asm
12// This shader emulates the OpenGL1.1 vertex processing fixed pipeline.
13// Performs calculations assuming there is one light and it is treated as a point light source.
14//
15
16// Input registers map
17#define aPosition       v0
18#define aNormal         v1
19
20// Output registers map
21#define vPosition       o0
22
23// Constant registers
24#define CONSTANT        c93
25
26def     CONSTANT[0], 0.0, 1.0, 2.0, 3.0
27def     CONSTANT[1], 0.125, 0.00390625, 0.5, 0.25
28
29#define CONST_0                 CONSTANT[0].x
30#define CONST_1                 CONSTANT[0].y
31#define CONST_2                 CONSTANT[0].z
32#define CONST_3                 CONSTANT[0].w
33#define CONST_HALF              CONSTANT[1].z
34#define CONST_QUARTER           CONSTANT[1].w
35#define CONST_1_0               CONSTANT[0].yx
36#define CONST_1__4              CONSTANT[1].w
37#define CONST_1__8              CONSTANT[1].x
38#define CONST_1__256            CONSTANT[1].y
39
40#pragma bind_symbol( aPosition.xyz, v0, v0 )
41#pragma bind_symbol( aNormal.xyz, v1, v1 )
42
43#pragma output_map( position, o0 )
44#pragma output_map( color, o1 )
45
46#pragma bind_symbol( uProjection, c0, c3 )
47#pragma bind_symbol( uModelView, c4, c7 )
48#pragma bind_symbol( uLightPos, c8, c8 )
49#pragma bind_symbol( uDiff, c9, c9 )
50#pragma bind_symbol( uAmb, c10, c10 )
51#pragma bind_symbol( uSpec, c11, c11 )
52#pragma bind_symbol( uMatShiniess.x, c12, c12 )
53
54main:
55    // Converts vertex coordinates configured in object coordinates into perspective coordinates, and then converts them to clip coordinates.
56    //
57    // The vertex coordinates in the perspective coordinate system are stored in `r15`, and output to `o0` in the clip coordinate system.
58    //
59    m4x4    r15,        v0,         c4
60	m4x4    o0,         r15,        c0
61    // Converts a normal vector configured in object coordinates into perspective coordinates.
62    // Here, the upper-left3x3 matrix of the model view matrix is used for conversion.
63    //
64    // The vertex processing does not normalize the normal vector because the specified lines are normalized by the application, and the conversion matrix does not have a scaling component.
65    //
66    //
67	m3x3    r14.xyz,       v1,         c4
68    // Creates a normalized light vector.
69    // `-r15` represents the normal vector, because `r15` is the vector in perspective coordinates.
70    //
71    // The variable `c8` holds the light position in perspective coordinates. The application converts the light position into perspective coordinates, and stores that in `c8`.
72    //
73    // From the above, c8 to r15 becomes the light vector. (not normalized at this time)
74    add     r0,         c8,         -r15
75    // The following 3 lines normalize r0 (light vector).
76    dp3     r0.w,       r0,         r0
77    rsq     r0.w,       r0.w
78    mul     r0,         r0,         r0.w
79    // Prepare a half-vector for the specular component.
80    // First, prepare a normalized view vector.
81    mov     r1,         -r15
82    dp3     r1.w,       r1,         r1
83    rsq     r1.w,       r1.w
84    mul     r1,         r1,         r1.w
85    // Adds the view vector and light vector (the light vector is normalized), and normalizes the result.
86    add     r2,         r0,         r1
87    //  r2 is a normalized half-vector.
88    dp3     r2.w,       r2,         r2
89    rsq     r2.w,       r2.w
90    mul     r2,         r2,         r2.w
91    // Calculate NL and NH.
92    // NL is the inner product of the normal and light vector and stored in r3.x.
93    dp3     r3.x,       r14,        r0
94    // NH is the inner product of the normal and half vector and stored in r3.y.
95    dp3     r3.y,       r14,        r2
96    // NL and NH are clamped at greater than 0.
97    max     r3,         r3,         CONST_0
98    // If `NL` or `NH` is negative, they are deemed to not have specular contribution, so `NH` and `NL` are clamped to `0.0` when <tt>NL < 0.0 || NH < 0.0</tt>.
99    //
100    cmp     2,          2,          r3,     CONST_0
101    ifc     1,          1,          2
102        mov r3.y,       CONST_0
103    endif
104    // Calculates the final vertex color.
105    // First, the sum of the global ambient and `light0` ambient is stored in `r10`.
106    //The application sets that sum in c10.
107    mov     r10,        c10
108    // Add the diffuse contribution.
109    mad     r10,        r3.x,       c9,     r10
110    // then add the specular contribution.
111    // NH is raised to the power of shininess.
112    // shininess is set in c12.x.
113    // First, perform power calculations,
114    pow     r6,         r3.y,       c12.x
115    // then add the specular contribution.
116    mad     r10,        r6.x,       c11,    r10
117    // Sets the vertex color in o1.
118    mov     o1,         r10
119    end
120endmain:
121