Merge branch '7.8'
[mesa.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 "nouveau_class.h"
32 #include "nv10_driver.h"
33 #include "nv20_driver.h"
34
35 void
36 nv20_emit_clip_plane(GLcontext *ctx, int emit)
37 {
38 }
39
40 static inline unsigned
41 get_material_bitmask(unsigned m)
42 {
43 unsigned ret = 0;
44
45 if (m & MAT_BIT_FRONT_EMISSION)
46 ret |= NV20TCL_COLOR_MATERIAL_FRONT_EMISSION_COL1;
47 if (m & MAT_BIT_FRONT_AMBIENT)
48 ret |= NV20TCL_COLOR_MATERIAL_FRONT_AMBIENT_COL1;
49 if (m & MAT_BIT_FRONT_DIFFUSE)
50 ret |= NV20TCL_COLOR_MATERIAL_FRONT_DIFFUSE_COL1;
51 if (m & MAT_BIT_FRONT_SPECULAR)
52 ret |= NV20TCL_COLOR_MATERIAL_FRONT_SPECULAR_COL1;
53
54 if (m & MAT_BIT_BACK_EMISSION)
55 ret |= NV20TCL_COLOR_MATERIAL_BACK_EMISSION_COL1;
56 if (m & MAT_BIT_BACK_AMBIENT)
57 ret |= NV20TCL_COLOR_MATERIAL_BACK_AMBIENT_COL1;
58 if (m & MAT_BIT_BACK_DIFFUSE)
59 ret |= NV20TCL_COLOR_MATERIAL_BACK_DIFFUSE_COL1;
60 if (m & MAT_BIT_BACK_SPECULAR)
61 ret |= NV20TCL_COLOR_MATERIAL_BACK_SPECULAR_COL1;
62
63 return ret;
64 }
65
66 void
67 nv20_emit_color_material(GLcontext *ctx, int emit)
68 {
69 struct nouveau_channel *chan = context_chan(ctx);
70 struct nouveau_grobj *kelvin = context_eng3d(ctx);
71 unsigned mask = get_material_bitmask(ctx->Light.ColorMaterialBitmask);
72
73 BEGIN_RING(chan, kelvin, NV20TCL_COLOR_MATERIAL, 1);
74 OUT_RING(chan, ctx->Light.ColorMaterialEnabled ? mask : 0);
75 }
76
77 static unsigned
78 get_fog_mode_signed(unsigned mode)
79 {
80 switch (mode) {
81 case GL_LINEAR:
82 return NV20TCL_FOG_MODE_LINEAR_SIGNED;
83 case GL_EXP:
84 return NV20TCL_FOG_MODE_EXP_SIGNED;
85 case GL_EXP2:
86 return NV20TCL_FOG_MODE_EXP2_SIGNED;
87 default:
88 assert(0);
89 }
90 }
91
92 static unsigned
93 get_fog_mode_unsigned(unsigned mode)
94 {
95 switch (mode) {
96 case GL_LINEAR:
97 return NV20TCL_FOG_MODE_LINEAR_UNSIGNED;
98 case GL_EXP:
99 return NV20TCL_FOG_MODE_EXP_UNSIGNED;
100 case GL_EXP2:
101 return NV20TCL_FOG_MODE_EXP2_UNSIGNED;
102 default:
103 assert(0);
104 }
105 }
106
107 static unsigned
108 get_fog_source(unsigned source)
109 {
110 switch (source) {
111 case GL_FOG_COORDINATE_EXT:
112 return NV20TCL_FOG_COORD_FOG;
113 case GL_FRAGMENT_DEPTH_EXT:
114 return NV20TCL_FOG_COORD_DIST_ORTHOGONAL_ABS;
115 default:
116 assert(0);
117 }
118 }
119
120 void
121 nv20_emit_fog(GLcontext *ctx, int emit)
122 {
123 struct nouveau_context *nctx = to_nouveau_context(ctx);
124 struct nouveau_channel *chan = context_chan(ctx);
125 struct nouveau_grobj *kelvin = context_eng3d(ctx);
126 struct gl_fog_attrib *f = &ctx->Fog;
127 unsigned source = nctx->fallback == HWTNL ?
128 f->FogCoordinateSource : GL_FOG_COORDINATE_EXT;
129 float k[3];
130
131 nv10_get_fog_coeff(ctx, k);
132
133 BEGIN_RING(chan, kelvin, NV20TCL_FOG_MODE, 4);
134 OUT_RING(chan, (source == GL_FOG_COORDINATE_EXT ?
135 get_fog_mode_signed(f->Mode) :
136 get_fog_mode_unsigned(f->Mode)));
137 OUT_RING(chan, get_fog_source(source));
138 OUT_RING(chan, f->Enabled ? 1 : 0);
139 OUT_RING(chan, pack_rgba_f(MESA_FORMAT_RGBA8888_REV, f->Color));
140
141 BEGIN_RING(chan, kelvin, NV20TCL_FOG_EQUATION_CONSTANT, 3);
142 OUT_RINGp(chan, k, 3);
143 }
144
145 void
146 nv20_emit_light_model(GLcontext *ctx, int emit)
147 {
148 struct nouveau_channel *chan = context_chan(ctx);
149 struct nouveau_grobj *kelvin = context_eng3d(ctx);
150 struct gl_lightmodel *m = &ctx->Light.Model;
151
152 BEGIN_RING(chan, kelvin, NV20TCL_SEPARATE_SPECULAR_ENABLE, 1);
153 OUT_RING(chan, m->ColorControl == GL_SEPARATE_SPECULAR_COLOR ? 1 : 0);
154
155 BEGIN_RING(chan, kelvin, NV20TCL_LIGHT_MODEL, 1);
156 OUT_RING(chan, ((m->LocalViewer ?
157 NV20TCL_LIGHT_MODEL_VIEWER_LOCAL :
158 NV20TCL_LIGHT_MODEL_VIEWER_NONLOCAL) |
159 (NEED_SECONDARY_COLOR(ctx) ?
160 NV20TCL_LIGHT_MODEL_SEPARATE_SPECULAR :
161 0)));
162
163 BEGIN_RING(chan, kelvin, NV20TCL_LIGHT_MODEL_TWO_SIDE_ENABLE, 1);
164 OUT_RING(chan, ctx->Light.Model.TwoSide ? 1 : 0);
165 }
166
167 void
168 nv20_emit_light_source(GLcontext *ctx, int emit)
169 {
170 const int i = emit - NOUVEAU_STATE_LIGHT_SOURCE0;
171 struct nouveau_channel *chan = context_chan(ctx);
172 struct nouveau_grobj *kelvin = context_eng3d(ctx);
173 struct gl_light *l = &ctx->Light.Light[i];
174
175 if (l->_Flags & LIGHT_POSITIONAL) {
176 BEGIN_RING(chan, kelvin, NV20TCL_LIGHT_POSITION_X(i), 3);
177 OUT_RINGp(chan, l->_Position, 3);
178
179 BEGIN_RING(chan, kelvin, NV20TCL_LIGHT_ATTENUATION_CONSTANT(i), 3);
180 OUT_RINGf(chan, l->ConstantAttenuation);
181 OUT_RINGf(chan, l->LinearAttenuation);
182 OUT_RINGf(chan, l->QuadraticAttenuation);
183
184 } else {
185 BEGIN_RING(chan, kelvin, NV20TCL_LIGHT_DIRECTION_X(i), 3);
186 OUT_RINGp(chan, l->_VP_inf_norm, 3);
187
188 BEGIN_RING(chan, kelvin, NV20TCL_LIGHT_HALF_VECTOR_X(i), 3);
189 OUT_RINGp(chan, l->_h_inf_norm, 3);
190 }
191
192 if (l->_Flags & LIGHT_SPOT) {
193 float k[7];
194
195 nv10_get_spot_coeff(l, k);
196
197 BEGIN_RING(chan, kelvin, NV20TCL_LIGHT_SPOT_CUTOFF_A(i), 7);
198 OUT_RINGp(chan, k, 7);
199 }
200 }
201
202 #define USE_COLOR_MATERIAL(attr, side) \
203 (ctx->Light.ColorMaterialEnabled && \
204 ctx->Light.ColorMaterialBitmask & (1 << MAT_ATTRIB_##attr(side)))
205
206 void
207 nv20_emit_material_ambient(GLcontext *ctx, int emit)
208 {
209 const int side = emit - NOUVEAU_STATE_MATERIAL_FRONT_AMBIENT;
210 struct nouveau_channel *chan = context_chan(ctx);
211 struct nouveau_grobj *kelvin = context_eng3d(ctx);
212 float (*mat)[4] = ctx->Light.Material.Attrib;
213 uint32_t m_scene[] = { NV20TCL_LIGHT_MODEL_FRONT_AMBIENT_R,
214 NV20TCL_LIGHT_MODEL_BACK_AMBIENT_R };
215 uint32_t m_factor[] = { NV20TCL_MATERIAL_FACTOR_FRONT_R,
216 NV20TCL_MATERIAL_FACTOR_BACK_R };
217 float c_scene[3], c_factor[3];
218 struct gl_light *l;
219
220 if (USE_COLOR_MATERIAL(AMBIENT, side)) {
221 COPY_3V(c_scene, mat[MAT_ATTRIB_EMISSION(side)]);
222 COPY_3V(c_factor, ctx->Light.Model.Ambient);
223
224 } else if (USE_COLOR_MATERIAL(EMISSION, side)) {
225 SCALE_3V(c_scene, mat[MAT_ATTRIB_AMBIENT(side)],
226 ctx->Light.Model.Ambient);
227 ASSIGN_3V(c_factor, 1, 1, 1);
228
229 } else {
230 COPY_3V(c_scene, ctx->Light._BaseColor[side]);
231 ZERO_3V(c_factor);
232 }
233
234 BEGIN_RING(chan, kelvin, m_scene[side], 3);
235 OUT_RINGp(chan, c_scene, 3);
236
237 if (ctx->Light.ColorMaterialEnabled) {
238 BEGIN_RING(chan, kelvin, m_factor[side], 3);
239 OUT_RINGp(chan, c_factor, 3);
240 }
241
242 foreach(l, &ctx->Light.EnabledList) {
243 const int i = l - ctx->Light.Light;
244 uint32_t m_light[] = { NV20TCL_LIGHT_FRONT_AMBIENT_R(i),
245 NV20TCL_LIGHT_BACK_AMBIENT_R(i) };
246 float *c_light = (USE_COLOR_MATERIAL(AMBIENT, side) ?
247 l->Ambient :
248 l->_MatAmbient[side]);
249
250 BEGIN_RING(chan, kelvin, m_light[side], 3);
251 OUT_RINGp(chan, c_light, 3);
252 }
253 }
254
255 void
256 nv20_emit_material_diffuse(GLcontext *ctx, int emit)
257 {
258 const int side = emit - NOUVEAU_STATE_MATERIAL_FRONT_DIFFUSE;
259 struct nouveau_channel *chan = context_chan(ctx);
260 struct nouveau_grobj *kelvin = context_eng3d(ctx);
261 GLfloat (*mat)[4] = ctx->Light.Material.Attrib;
262 uint32_t m_factor[] = { NV20TCL_MATERIAL_FACTOR_FRONT_A,
263 NV20TCL_MATERIAL_FACTOR_BACK_A };
264 struct gl_light *l;
265
266 BEGIN_RING(chan, kelvin, m_factor[side], 1);
267 OUT_RINGf(chan, mat[MAT_ATTRIB_DIFFUSE(side)][3]);
268
269 foreach(l, &ctx->Light.EnabledList) {
270 const int i = l - ctx->Light.Light;
271 uint32_t m_light[] = { NV20TCL_LIGHT_FRONT_DIFFUSE_R(i),
272 NV20TCL_LIGHT_BACK_DIFFUSE_R(i) };
273 float *c_light = (USE_COLOR_MATERIAL(DIFFUSE, side) ?
274 l->Diffuse :
275 l->_MatDiffuse[side]);
276
277 BEGIN_RING(chan, kelvin, m_light[side], 3);
278 OUT_RINGp(chan, c_light, 3);
279 }
280 }
281
282 void
283 nv20_emit_material_specular(GLcontext *ctx, int emit)
284 {
285 const int side = emit - NOUVEAU_STATE_MATERIAL_FRONT_SPECULAR;
286 struct nouveau_channel *chan = context_chan(ctx);
287 struct nouveau_grobj *kelvin = context_eng3d(ctx);
288 struct gl_light *l;
289
290 foreach(l, &ctx->Light.EnabledList) {
291 const int i = l - ctx->Light.Light;
292 uint32_t m_light[] = { NV20TCL_LIGHT_FRONT_SPECULAR_R(i),
293 NV20TCL_LIGHT_BACK_SPECULAR_R(i) };
294 float *c_light = (USE_COLOR_MATERIAL(SPECULAR, side) ?
295 l->Specular :
296 l->_MatSpecular[side]);
297
298 BEGIN_RING(chan, kelvin, m_light[side], 3);
299 OUT_RINGp(chan, c_light, 3);
300 }
301 }
302
303 void
304 nv20_emit_material_shininess(GLcontext *ctx, int emit)
305 {
306 const int side = emit - NOUVEAU_STATE_MATERIAL_FRONT_SHININESS;
307 struct nouveau_channel *chan = context_chan(ctx);
308 struct nouveau_grobj *kelvin = context_eng3d(ctx);
309 float (*mat)[4] = ctx->Light.Material.Attrib;
310 uint32_t mthd[] = { NV20TCL_FRONT_MATERIAL_SHININESS(0),
311 NV20TCL_BACK_MATERIAL_SHININESS(0) };
312 float k[6];
313
314 nv10_get_shininess_coeff(
315 CLAMP(mat[MAT_ATTRIB_SHININESS(side)][0], 0, 1024),
316 k);
317
318 BEGIN_RING(chan, kelvin, mthd[side], 6);
319 OUT_RINGp(chan, k, 6);
320 }
321
322 void
323 nv20_emit_modelview(GLcontext *ctx, int emit)
324 {
325 struct nouveau_context *nctx = to_nouveau_context(ctx);
326 struct nouveau_channel *chan = context_chan(ctx);
327 struct nouveau_grobj *kelvin = context_eng3d(ctx);
328 GLmatrix *m = ctx->ModelviewMatrixStack.Top;
329
330 if (nctx->fallback != HWTNL)
331 return;
332
333 if (ctx->Light._NeedEyeCoords || ctx->Fog.Enabled ||
334 (ctx->Texture._GenFlags & TEXGEN_NEED_EYE_COORD)) {
335 BEGIN_RING(chan, kelvin, NV20TCL_MODELVIEW0_MATRIX(0), 16);
336 OUT_RINGm(chan, m->m);
337 }
338
339 if (ctx->Light.Enabled ||
340 (ctx->Texture._GenFlags & TEXGEN_NEED_EYE_COORD)) {
341 int i, j;
342
343 BEGIN_RING(chan, kelvin,
344 NV20TCL_INVERSE_MODELVIEW0_MATRIX(0), 12);
345 for (i = 0; i < 3; i++)
346 for (j = 0; j < 4; j++)
347 OUT_RINGf(chan, m->inv[4*i + j]);
348 }
349 }
350
351 void
352 nv20_emit_projection(GLcontext *ctx, int emit)
353 {
354 struct nouveau_context *nctx = to_nouveau_context(ctx);
355 struct nouveau_channel *chan = context_chan(ctx);
356 struct nouveau_grobj *kelvin = context_eng3d(ctx);
357 GLmatrix m;
358
359 _math_matrix_ctr(&m);
360 get_viewport_scale(ctx, m.m);
361
362 if (nctx->fallback == HWTNL)
363 _math_matrix_mul_matrix(&m, &m, &ctx->_ModelProjectMatrix);
364
365 BEGIN_RING(chan, kelvin, NV20TCL_PROJECTION_MATRIX(0), 16);
366 OUT_RINGm(chan, m.m);
367
368 _math_matrix_dtr(&m);
369 }