2 * Copyright (C) 2009-2010 Francisco Jerez.
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:
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.
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.
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"
36 nv20_emit_clip_plane(GLcontext
*ctx
, int emit
)
40 static inline unsigned
41 get_material_bitmask(unsigned m
)
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
;
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
;
67 nv20_emit_color_material(GLcontext
*ctx
, int emit
)
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
);
73 BEGIN_RING(chan
, kelvin
, NV20TCL_COLOR_MATERIAL
, 1);
74 OUT_RING(chan
, ctx
->Light
.ColorMaterialEnabled
? mask
: 0);
78 get_fog_mode_signed(unsigned mode
)
82 return NV20TCL_FOG_MODE_LINEAR_SIGNED
;
84 return NV20TCL_FOG_MODE_EXP_SIGNED
;
86 return NV20TCL_FOG_MODE_EXP2_SIGNED
;
93 get_fog_mode_unsigned(unsigned mode
)
97 return NV20TCL_FOG_MODE_LINEAR_UNSIGNED
;
99 return NV20TCL_FOG_MODE_EXP_UNSIGNED
;
101 return NV20TCL_FOG_MODE_EXP2_UNSIGNED
;
108 get_fog_source(unsigned 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
;
121 nv20_emit_fog(GLcontext
*ctx
, int emit
)
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
;
131 nv10_get_fog_coeff(ctx
, k
);
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
));
141 BEGIN_RING(chan
, kelvin
, NV20TCL_FOG_EQUATION_CONSTANT
, 3);
142 OUT_RINGf(chan
, k
[0]);
143 OUT_RINGf(chan
, k
[1]);
144 OUT_RINGf(chan
, k
[2]);
148 nv20_emit_light_model(GLcontext
*ctx
, int emit
)
150 struct nouveau_channel
*chan
= context_chan(ctx
);
151 struct nouveau_grobj
*kelvin
= context_eng3d(ctx
);
152 struct gl_lightmodel
*m
= &ctx
->Light
.Model
;
154 BEGIN_RING(chan
, kelvin
, NV20TCL_SEPARATE_SPECULAR_ENABLE
, 1);
155 OUT_RING(chan
, m
->ColorControl
== GL_SEPARATE_SPECULAR_COLOR
? 1 : 0);
157 BEGIN_RING(chan
, kelvin
, NV20TCL_LIGHT_MODEL
, 1);
158 OUT_RING(chan
, ((m
->LocalViewer
?
159 NV20TCL_LIGHT_MODEL_VIEWER_LOCAL
:
160 NV20TCL_LIGHT_MODEL_VIEWER_NONLOCAL
) |
161 (NEED_SECONDARY_COLOR(ctx
) ?
162 NV20TCL_LIGHT_MODEL_SEPARATE_SPECULAR
:
165 BEGIN_RING(chan
, kelvin
, NV20TCL_LIGHT_MODEL_TWO_SIDE_ENABLE
, 1);
166 OUT_RING(chan
, ctx
->Light
.Model
.TwoSide
? 1 : 0);
170 nv20_emit_light_source(GLcontext
*ctx
, int emit
)
172 const int i
= emit
- NOUVEAU_STATE_LIGHT_SOURCE0
;
173 struct nouveau_channel
*chan
= context_chan(ctx
);
174 struct nouveau_grobj
*kelvin
= context_eng3d(ctx
);
175 struct gl_light
*l
= &ctx
->Light
.Light
[i
];
177 if (l
->_Flags
& LIGHT_POSITIONAL
) {
178 BEGIN_RING(chan
, kelvin
, NV20TCL_LIGHT_POSITION_X(i
), 3);
179 OUT_RINGf(chan
, l
->_Position
[0]);
180 OUT_RINGf(chan
, l
->_Position
[1]);
181 OUT_RINGf(chan
, l
->_Position
[2]);
183 BEGIN_RING(chan
, kelvin
, NV20TCL_LIGHT_ATTENUATION_CONSTANT(i
), 3);
184 OUT_RINGf(chan
, l
->ConstantAttenuation
);
185 OUT_RINGf(chan
, l
->LinearAttenuation
);
186 OUT_RINGf(chan
, l
->QuadraticAttenuation
);
189 BEGIN_RING(chan
, kelvin
, NV20TCL_LIGHT_DIRECTION_X(i
), 3);
190 OUT_RINGf(chan
, l
->_VP_inf_norm
[0]);
191 OUT_RINGf(chan
, l
->_VP_inf_norm
[1]);
192 OUT_RINGf(chan
, l
->_VP_inf_norm
[2]);
194 BEGIN_RING(chan
, kelvin
, NV20TCL_LIGHT_HALF_VECTOR_X(i
), 3);
195 OUT_RINGf(chan
, l
->_h_inf_norm
[0]);
196 OUT_RINGf(chan
, l
->_h_inf_norm
[1]);
197 OUT_RINGf(chan
, l
->_h_inf_norm
[2]);
200 if (l
->_Flags
& LIGHT_SPOT
) {
203 nv10_get_spot_coeff(l
, k
);
205 BEGIN_RING(chan
, kelvin
, NV20TCL_LIGHT_SPOT_CUTOFF_A(i
), 7);
206 OUT_RINGf(chan
, k
[0]);
207 OUT_RINGf(chan
, k
[1]);
208 OUT_RINGf(chan
, k
[2]);
209 OUT_RINGf(chan
, k
[3]);
210 OUT_RINGf(chan
, k
[4]);
211 OUT_RINGf(chan
, k
[5]);
212 OUT_RINGf(chan
, k
[6]);
216 #define USE_COLOR_MATERIAL(attr, side) \
217 (ctx->Light.ColorMaterialEnabled && \
218 ctx->Light.ColorMaterialBitmask & (1 << MAT_ATTRIB_##attr(side)))
221 nv20_emit_material_ambient(GLcontext
*ctx
, int emit
)
223 const int side
= emit
- NOUVEAU_STATE_MATERIAL_FRONT_AMBIENT
;
224 struct nouveau_channel
*chan
= context_chan(ctx
);
225 struct nouveau_grobj
*kelvin
= context_eng3d(ctx
);
226 float (*mat
)[4] = ctx
->Light
.Material
.Attrib
;
227 uint32_t m_scene
[] = { NV20TCL_LIGHT_MODEL_FRONT_AMBIENT_R
,
228 NV20TCL_LIGHT_MODEL_BACK_AMBIENT_R
};
229 uint32_t m_factor
[] = { NV20TCL_MATERIAL_FACTOR_FRONT_R
,
230 NV20TCL_MATERIAL_FACTOR_BACK_R
};
231 float c_scene
[3], c_factor
[3];
234 if (USE_COLOR_MATERIAL(AMBIENT
, side
)) {
235 COPY_3V(c_scene
, mat
[MAT_ATTRIB_EMISSION(side
)]);
236 COPY_3V(c_factor
, ctx
->Light
.Model
.Ambient
);
238 } else if (USE_COLOR_MATERIAL(EMISSION
, side
)) {
239 SCALE_3V(c_scene
, mat
[MAT_ATTRIB_AMBIENT(side
)],
240 ctx
->Light
.Model
.Ambient
);
241 ASSIGN_3V(c_factor
, 1, 1, 1);
244 COPY_3V(c_scene
, ctx
->Light
._BaseColor
[side
]);
248 BEGIN_RING(chan
, kelvin
, m_scene
[side
], 3);
249 OUT_RINGf(chan
, c_scene
[0]);
250 OUT_RINGf(chan
, c_scene
[1]);
251 OUT_RINGf(chan
, c_scene
[2]);
253 if (ctx
->Light
.ColorMaterialEnabled
) {
254 BEGIN_RING(chan
, kelvin
, m_factor
[side
], 3);
255 OUT_RINGf(chan
, c_factor
[0]);
256 OUT_RINGf(chan
, c_factor
[1]);
257 OUT_RINGf(chan
, c_factor
[2]);
260 foreach(l
, &ctx
->Light
.EnabledList
) {
261 const int i
= l
- ctx
->Light
.Light
;
262 uint32_t m_light
[] = { NV20TCL_LIGHT_FRONT_AMBIENT_R(i
),
263 NV20TCL_LIGHT_BACK_AMBIENT_R(i
) };
264 float *c_light
= (USE_COLOR_MATERIAL(AMBIENT
, side
) ?
266 l
->_MatAmbient
[side
]);
268 BEGIN_RING(chan
, kelvin
, m_light
[side
], 3);
269 OUT_RINGf(chan
, c_light
[0]);
270 OUT_RINGf(chan
, c_light
[1]);
271 OUT_RINGf(chan
, c_light
[2]);
276 nv20_emit_material_diffuse(GLcontext
*ctx
, int emit
)
278 const int side
= emit
- NOUVEAU_STATE_MATERIAL_FRONT_DIFFUSE
;
279 struct nouveau_channel
*chan
= context_chan(ctx
);
280 struct nouveau_grobj
*kelvin
= context_eng3d(ctx
);
281 GLfloat (*mat
)[4] = ctx
->Light
.Material
.Attrib
;
282 uint32_t m_factor
[] = { NV20TCL_MATERIAL_FACTOR_FRONT_A
,
283 NV20TCL_MATERIAL_FACTOR_BACK_A
};
286 BEGIN_RING(chan
, kelvin
, m_factor
[side
], 1);
287 OUT_RINGf(chan
, mat
[MAT_ATTRIB_DIFFUSE(side
)][3]);
289 foreach(l
, &ctx
->Light
.EnabledList
) {
290 const int i
= l
- ctx
->Light
.Light
;
291 uint32_t m_light
[] = { NV20TCL_LIGHT_FRONT_DIFFUSE_R(i
),
292 NV20TCL_LIGHT_BACK_DIFFUSE_R(i
) };
293 float *c_light
= (USE_COLOR_MATERIAL(DIFFUSE
, side
) ?
295 l
->_MatDiffuse
[side
]);
297 BEGIN_RING(chan
, kelvin
, m_light
[side
], 3);
298 OUT_RINGf(chan
, c_light
[0]);
299 OUT_RINGf(chan
, c_light
[1]);
300 OUT_RINGf(chan
, c_light
[2]);
305 nv20_emit_material_specular(GLcontext
*ctx
, int emit
)
307 const int side
= emit
- NOUVEAU_STATE_MATERIAL_FRONT_SPECULAR
;
308 struct nouveau_channel
*chan
= context_chan(ctx
);
309 struct nouveau_grobj
*kelvin
= context_eng3d(ctx
);
312 foreach(l
, &ctx
->Light
.EnabledList
) {
313 const int i
= l
- ctx
->Light
.Light
;
314 uint32_t m_light
[] = { NV20TCL_LIGHT_FRONT_SPECULAR_R(i
),
315 NV20TCL_LIGHT_BACK_SPECULAR_R(i
) };
316 float *c_light
= (USE_COLOR_MATERIAL(SPECULAR
, side
) ?
318 l
->_MatSpecular
[side
]);
320 BEGIN_RING(chan
, kelvin
, m_light
[side
], 3);
321 OUT_RINGf(chan
, c_light
[0]);
322 OUT_RINGf(chan
, c_light
[1]);
323 OUT_RINGf(chan
, c_light
[2]);
328 nv20_emit_material_shininess(GLcontext
*ctx
, int emit
)
330 const int side
= emit
- NOUVEAU_STATE_MATERIAL_FRONT_SHININESS
;
331 struct nouveau_channel
*chan
= context_chan(ctx
);
332 struct nouveau_grobj
*kelvin
= context_eng3d(ctx
);
333 float (*mat
)[4] = ctx
->Light
.Material
.Attrib
;
334 uint32_t mthd
[] = { NV20TCL_FRONT_MATERIAL_SHININESS(0),
335 NV20TCL_BACK_MATERIAL_SHININESS(0) };
338 nv10_get_shininess_coeff(
339 CLAMP(mat
[MAT_ATTRIB_SHININESS(side
)][0], 0, 1024),
342 BEGIN_RING(chan
, kelvin
, mthd
[side
], 6);
343 OUT_RINGf(chan
, k
[0]);
344 OUT_RINGf(chan
, k
[1]);
345 OUT_RINGf(chan
, k
[2]);
346 OUT_RINGf(chan
, k
[3]);
347 OUT_RINGf(chan
, k
[4]);
348 OUT_RINGf(chan
, k
[5]);
352 nv20_emit_modelview(GLcontext
*ctx
, int emit
)
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
= ctx
->ModelviewMatrixStack
.Top
;
359 if (nctx
->fallback
!= HWTNL
)
362 if (ctx
->Light
._NeedEyeCoords
|| ctx
->Fog
.Enabled
) {
363 BEGIN_RING(chan
, kelvin
, NV20TCL_MODELVIEW0_MATRIX(0), 16);
364 OUT_RINGm(chan
, m
->m
);
367 if (ctx
->Light
.Enabled
) {
370 BEGIN_RING(chan
, kelvin
,
371 NV20TCL_INVERSE_MODELVIEW0_MATRIX(0), 12);
372 for (i
= 0; i
< 3; i
++)
373 for (j
= 0; j
< 4; j
++)
374 OUT_RINGf(chan
, m
->inv
[4*i
+ j
]);
379 nv20_emit_projection(GLcontext
*ctx
, int emit
)
381 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
382 struct nouveau_channel
*chan
= context_chan(ctx
);
383 struct nouveau_grobj
*kelvin
= context_eng3d(ctx
);
386 _math_matrix_ctr(&m
);
387 get_viewport_scale(ctx
, m
.m
);
389 if (nctx
->fallback
== HWTNL
)
390 _math_matrix_mul_matrix(&m
, &m
, &ctx
->_ModelProjectMatrix
);
392 BEGIN_RING(chan
, kelvin
, NV20TCL_PROJECTION_MATRIX(0), 16);
393 OUT_RINGm(chan
, m
.m
);
395 _math_matrix_dtr(&m
);