Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / drivers / dri / nouveau / nv20_state_tnl.c
1 /*
2  * Copyright (C) 2009-2010 Francisco Jerez.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  */
26
27 #include "nouveau_driver.h"
28 #include "nouveau_context.h"
29 #include "nouveau_gldefs.h"
30 #include "nouveau_util.h"
31 #include "nv20_3d.xml.h"
32 #include "nv10_driver.h"
33 #include "nv20_driver.h"
34
35 #define LIGHT_MODEL_AMBIENT_R(side)                     \
36         ((side) ? NV20_3D_LIGHT_MODEL_BACK_AMBIENT_R :  \
37          NV20_3D_LIGHT_MODEL_FRONT_AMBIENT_R)
38 #define LIGHT_AMBIENT_R(side, i)                        \
39         ((side) ? NV20_3D_LIGHT_BACK_AMBIENT_R(i) :     \
40          NV20_3D_LIGHT_FRONT_AMBIENT_R(i))
41 #define LIGHT_DIFFUSE_R(side, i)                        \
42         ((side) ? NV20_3D_LIGHT_BACK_DIFFUSE_R(i) :     \
43          NV20_3D_LIGHT_FRONT_DIFFUSE_R(i))
44 #define LIGHT_SPECULAR_R(side, i)                       \
45         ((side) ? NV20_3D_LIGHT_BACK_SPECULAR_R(i) :    \
46          NV20_3D_LIGHT_FRONT_SPECULAR_R(i))
47 #define MATERIAL_FACTOR_R(side)                         \
48         ((side) ? NV20_3D_MATERIAL_FACTOR_BACK_R :      \
49          NV20_3D_MATERIAL_FACTOR_FRONT_R)
50 #define MATERIAL_FACTOR_A(side)                         \
51         ((side) ? NV20_3D_MATERIAL_FACTOR_BACK_A :      \
52          NV20_3D_MATERIAL_FACTOR_FRONT_A)
53 #define MATERIAL_SHININESS(side)                        \
54         ((side) ? NV20_3D_BACK_MATERIAL_SHININESS(0) :  \
55          NV20_3D_FRONT_MATERIAL_SHININESS(0))
56
57 void
58 nv20_emit_clip_plane(struct gl_context *ctx, int emit)
59 {
60 }
61
62 static inline unsigned
63 get_material_bitmask(unsigned m)
64 {
65         unsigned ret = 0;
66
67         if (m & MAT_BIT_FRONT_EMISSION)
68                 ret |= NV20_3D_COLOR_MATERIAL_FRONT_EMISSION_COL1;
69         if (m & MAT_BIT_FRONT_AMBIENT)
70                 ret |= NV20_3D_COLOR_MATERIAL_FRONT_AMBIENT_COL1;
71         if (m & MAT_BIT_FRONT_DIFFUSE)
72                 ret |= NV20_3D_COLOR_MATERIAL_FRONT_DIFFUSE_COL1;
73         if (m & MAT_BIT_FRONT_SPECULAR)
74                 ret |= NV20_3D_COLOR_MATERIAL_FRONT_SPECULAR_COL1;
75
76         if (m & MAT_BIT_BACK_EMISSION)
77                 ret |= NV20_3D_COLOR_MATERIAL_BACK_EMISSION_COL1;
78         if (m & MAT_BIT_BACK_AMBIENT)
79                 ret |= NV20_3D_COLOR_MATERIAL_BACK_AMBIENT_COL1;
80         if (m & MAT_BIT_BACK_DIFFUSE)
81                 ret |= NV20_3D_COLOR_MATERIAL_BACK_DIFFUSE_COL1;
82         if (m & MAT_BIT_BACK_SPECULAR)
83                 ret |= NV20_3D_COLOR_MATERIAL_BACK_SPECULAR_COL1;
84
85         return ret;
86 }
87
88 void
89 nv20_emit_color_material(struct gl_context *ctx, int emit)
90 {
91         struct nouveau_channel *chan = context_chan(ctx);
92         struct nouveau_grobj *kelvin = context_eng3d(ctx);
93         unsigned mask = get_material_bitmask(ctx->Light.ColorMaterialBitmask);
94
95         BEGIN_RING(chan, kelvin, NV20_3D_COLOR_MATERIAL, 1);
96         OUT_RING(chan, ctx->Light.ColorMaterialEnabled ? mask : 0);
97 }
98
99 static unsigned
100 get_fog_mode_signed(unsigned mode)
101 {
102         switch (mode) {
103         case GL_LINEAR:
104                 return NV20_3D_FOG_MODE_LINEAR_SIGNED;
105         case GL_EXP:
106                 return NV20_3D_FOG_MODE_EXP_SIGNED;
107         case GL_EXP2:
108                 return NV20_3D_FOG_MODE_EXP2_SIGNED;
109         default:
110                 assert(0);
111         }
112 }
113
114 static unsigned
115 get_fog_mode_unsigned(unsigned mode)
116 {
117         switch (mode) {
118         case GL_LINEAR:
119                 return NV20_3D_FOG_MODE_LINEAR_UNSIGNED;
120         case GL_EXP:
121                 return NV20_3D_FOG_MODE_EXP_UNSIGNED;
122         case GL_EXP2:
123                 return NV20_3D_FOG_MODE_EXP2_UNSIGNED;
124         default:
125                 assert(0);
126         }
127 }
128
129 static unsigned
130 get_fog_source(unsigned source)
131 {
132         switch (source) {
133         case GL_FOG_COORDINATE_EXT:
134                 return NV20_3D_FOG_COORD_FOG;
135         case GL_FRAGMENT_DEPTH_EXT:
136                 return NV20_3D_FOG_COORD_DIST_ORTHOGONAL_ABS;
137         default:
138                 assert(0);
139         }
140 }
141
142 void
143 nv20_emit_fog(struct gl_context *ctx, int emit)
144 {
145         struct nouveau_context *nctx = to_nouveau_context(ctx);
146         struct nouveau_channel *chan = context_chan(ctx);
147         struct nouveau_grobj *kelvin = context_eng3d(ctx);
148         struct gl_fog_attrib *f = &ctx->Fog;
149         unsigned source = nctx->fallback == HWTNL ?
150                 f->FogCoordinateSource : GL_FOG_COORDINATE_EXT;
151         float k[3];
152
153         nv10_get_fog_coeff(ctx, k);
154
155         BEGIN_RING(chan, kelvin, NV20_3D_FOG_MODE, 4);
156         OUT_RING(chan, (source == GL_FOG_COORDINATE_EXT ?
157                         get_fog_mode_signed(f->Mode) :
158                         get_fog_mode_unsigned(f->Mode)));
159         OUT_RING(chan, get_fog_source(source));
160         OUT_RINGb(chan, f->Enabled);
161         OUT_RING(chan, pack_rgba_f(MESA_FORMAT_RGBA8888_REV, f->Color));
162
163         BEGIN_RING(chan, kelvin, NV20_3D_FOG_COEFF(0), 3);
164         OUT_RINGp(chan, k, 3);
165 }
166
167 void
168 nv20_emit_light_model(struct gl_context *ctx, int emit)
169 {
170         struct nouveau_channel *chan = context_chan(ctx);
171         struct nouveau_grobj *kelvin = context_eng3d(ctx);
172         struct gl_lightmodel *m = &ctx->Light.Model;
173
174         BEGIN_RING(chan, kelvin, NV20_3D_SEPARATE_SPECULAR_ENABLE, 1);
175         OUT_RINGb(chan, m->ColorControl == GL_SEPARATE_SPECULAR_COLOR);
176
177         BEGIN_RING(chan, kelvin, NV20_3D_LIGHT_MODEL, 1);
178         OUT_RING(chan, ((m->LocalViewer ?
179                          NV20_3D_LIGHT_MODEL_VIEWER_LOCAL :
180                          NV20_3D_LIGHT_MODEL_VIEWER_NONLOCAL) |
181                         (_mesa_need_secondary_color(ctx) ?
182                          NV20_3D_LIGHT_MODEL_SEPARATE_SPECULAR :
183                          0)));
184
185         BEGIN_RING(chan, kelvin, NV20_3D_LIGHT_MODEL_TWO_SIDE_ENABLE, 1);
186         OUT_RINGb(chan, ctx->Light.Model.TwoSide);
187 }
188
189 void
190 nv20_emit_light_source(struct gl_context *ctx, int emit)
191 {
192         const int i = emit - NOUVEAU_STATE_LIGHT_SOURCE0;
193         struct nouveau_channel *chan = context_chan(ctx);
194         struct nouveau_grobj *kelvin = context_eng3d(ctx);
195         struct gl_light *l = &ctx->Light.Light[i];
196
197         if (l->_Flags & LIGHT_POSITIONAL) {
198                 BEGIN_RING(chan, kelvin, NV20_3D_LIGHT_POSITION_X(i), 3);
199                 OUT_RINGp(chan, l->_Position, 3);
200
201                 BEGIN_RING(chan, kelvin, NV20_3D_LIGHT_ATTENUATION_CONSTANT(i), 3);
202                 OUT_RINGf(chan, l->ConstantAttenuation);
203                 OUT_RINGf(chan, l->LinearAttenuation);
204                 OUT_RINGf(chan, l->QuadraticAttenuation);
205
206         } else {
207                 BEGIN_RING(chan, kelvin, NV20_3D_LIGHT_DIRECTION_X(i), 3);
208                 OUT_RINGp(chan, l->_VP_inf_norm, 3);
209
210                 BEGIN_RING(chan, kelvin, NV20_3D_LIGHT_HALF_VECTOR_X(i), 3);
211                 OUT_RINGp(chan, l->_h_inf_norm, 3);
212         }
213
214         if (l->_Flags & LIGHT_SPOT) {
215                 float k[7];
216
217                 nv10_get_spot_coeff(l, k);
218
219                 BEGIN_RING(chan, kelvin, NV20_3D_LIGHT_SPOT_CUTOFF(i, 0), 7);
220                 OUT_RINGp(chan, k, 7);
221         }
222 }
223
224 #define USE_COLOR_MATERIAL(attr, side)                                  \
225         (ctx->Light.ColorMaterialEnabled &&                             \
226          ctx->Light.ColorMaterialBitmask & (1 << MAT_ATTRIB_##attr(side)))
227
228 void
229 nv20_emit_material_ambient(struct gl_context *ctx, int emit)
230 {
231         const int side = emit - NOUVEAU_STATE_MATERIAL_FRONT_AMBIENT;
232         struct nouveau_channel *chan = context_chan(ctx);
233         struct nouveau_grobj *kelvin = context_eng3d(ctx);
234         float (*mat)[4] = ctx->Light.Material.Attrib;
235         float c_scene[3], c_factor[3];
236         struct gl_light *l;
237
238         if (USE_COLOR_MATERIAL(AMBIENT, side)) {
239                 COPY_3V(c_scene, mat[MAT_ATTRIB_EMISSION(side)]);
240                 COPY_3V(c_factor, ctx->Light.Model.Ambient);
241
242         } else if (USE_COLOR_MATERIAL(EMISSION, side)) {
243                 SCALE_3V(c_scene, mat[MAT_ATTRIB_AMBIENT(side)],
244                          ctx->Light.Model.Ambient);
245                 ASSIGN_3V(c_factor, 1, 1, 1);
246
247         } else {
248                 COPY_3V(c_scene, ctx->Light._BaseColor[side]);
249                 ZERO_3V(c_factor);
250         }
251
252         BEGIN_RING(chan, kelvin, LIGHT_MODEL_AMBIENT_R(side), 3);
253         OUT_RINGp(chan, c_scene, 3);
254
255         if (ctx->Light.ColorMaterialEnabled) {
256                 BEGIN_RING(chan, kelvin, MATERIAL_FACTOR_R(side), 3);
257                 OUT_RINGp(chan, c_factor, 3);
258         }
259
260         foreach(l, &ctx->Light.EnabledList) {
261                 const int i = l - ctx->Light.Light;
262                 float *c_light = (USE_COLOR_MATERIAL(AMBIENT, side) ?
263                                   l->Ambient :
264                                   l->_MatAmbient[side]);
265
266                 BEGIN_RING(chan, kelvin, LIGHT_AMBIENT_R(side, i), 3);
267                 OUT_RINGp(chan, c_light, 3);
268         }
269 }
270
271 void
272 nv20_emit_material_diffuse(struct gl_context *ctx, int emit)
273 {
274         const int side = emit - NOUVEAU_STATE_MATERIAL_FRONT_DIFFUSE;
275         struct nouveau_channel *chan = context_chan(ctx);
276         struct nouveau_grobj *kelvin = context_eng3d(ctx);
277         GLfloat (*mat)[4] = ctx->Light.Material.Attrib;
278         struct gl_light *l;
279
280         BEGIN_RING(chan, kelvin, MATERIAL_FACTOR_A(side), 1);
281         OUT_RINGf(chan, mat[MAT_ATTRIB_DIFFUSE(side)][3]);
282
283         foreach(l, &ctx->Light.EnabledList) {
284                 const int i = l - ctx->Light.Light;
285                 float *c_light = (USE_COLOR_MATERIAL(DIFFUSE, side) ?
286                                   l->Diffuse :
287                                   l->_MatDiffuse[side]);
288
289                 BEGIN_RING(chan, kelvin, LIGHT_DIFFUSE_R(side, i), 3);
290                 OUT_RINGp(chan, c_light, 3);
291         }
292 }
293
294 void
295 nv20_emit_material_specular(struct gl_context *ctx, int emit)
296 {
297         const int side = emit - NOUVEAU_STATE_MATERIAL_FRONT_SPECULAR;
298         struct nouveau_channel *chan = context_chan(ctx);
299         struct nouveau_grobj *kelvin = context_eng3d(ctx);
300         struct gl_light *l;
301
302         foreach(l, &ctx->Light.EnabledList) {
303                 const int i = l - ctx->Light.Light;
304                 float *c_light = (USE_COLOR_MATERIAL(SPECULAR, side) ?
305                                   l->Specular :
306                                   l->_MatSpecular[side]);
307
308                 BEGIN_RING(chan, kelvin, LIGHT_SPECULAR_R(side, i), 3);
309                 OUT_RINGp(chan, c_light, 3);
310         }
311 }
312
313 void
314 nv20_emit_material_shininess(struct gl_context *ctx, int emit)
315 {
316         const int side = emit - NOUVEAU_STATE_MATERIAL_FRONT_SHININESS;
317         struct nouveau_channel *chan = context_chan(ctx);
318         struct nouveau_grobj *kelvin = context_eng3d(ctx);
319         float (*mat)[4] = ctx->Light.Material.Attrib;
320         float k[6];
321
322         nv10_get_shininess_coeff(
323                 CLAMP(mat[MAT_ATTRIB_SHININESS(side)][0], 0, 1024),
324                 k);
325
326         BEGIN_RING(chan, kelvin, MATERIAL_SHININESS(side), 6);
327         OUT_RINGp(chan, k, 6);
328 }
329
330 void
331 nv20_emit_modelview(struct gl_context *ctx, int emit)
332 {
333         struct nouveau_context *nctx = to_nouveau_context(ctx);
334         struct nouveau_channel *chan = context_chan(ctx);
335         struct nouveau_grobj *kelvin = context_eng3d(ctx);
336         GLmatrix *m = ctx->ModelviewMatrixStack.Top;
337
338         if (nctx->fallback != HWTNL)
339                 return;
340
341         if (ctx->Light._NeedEyeCoords || ctx->Fog.Enabled ||
342             (ctx->Texture._GenFlags & TEXGEN_NEED_EYE_COORD)) {
343                 BEGIN_RING(chan, kelvin, NV20_3D_MODELVIEW_MATRIX(0, 0), 16);
344                 OUT_RINGm(chan, m->m);
345         }
346
347         if (ctx->Light.Enabled ||
348             (ctx->Texture._GenFlags & TEXGEN_NEED_EYE_COORD)) {
349                 int i, j;
350
351                 BEGIN_RING(chan, kelvin,
352                            NV20_3D_INVERSE_MODELVIEW_MATRIX(0, 0), 12);
353                 for (i = 0; i < 3; i++)
354                         for (j = 0; j < 4; j++)
355                                 OUT_RINGf(chan, m->inv[4*i + j]);
356         }
357 }
358
359 void
360 nv20_emit_projection(struct gl_context *ctx, int emit)
361 {
362         struct nouveau_context *nctx = to_nouveau_context(ctx);
363         struct nouveau_channel *chan = context_chan(ctx);
364         struct nouveau_grobj *kelvin = context_eng3d(ctx);
365         GLmatrix m;
366
367         _math_matrix_ctr(&m);
368         get_viewport_scale(ctx, m.m);
369
370         if (nctx->fallback == HWTNL)
371                 _math_matrix_mul_matrix(&m, &m, &ctx->_ModelProjectMatrix);
372
373         BEGIN_RING(chan, kelvin, NV20_3D_PROJECTION_MATRIX(0), 16);
374         OUT_RINGm(chan, m.m);
375
376         _math_matrix_dtr(&m);
377 }