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 "nv20_3d.xml.h"
32 #include "nv10_driver.h"
33 #include "nv20_driver.h"
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))
58 nv20_emit_clip_plane(struct gl_context
*ctx
, int emit
)
62 static inline unsigned
63 get_material_bitmask(unsigned m
)
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
;
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
;
89 nv20_emit_color_material(struct gl_context
*ctx
, int emit
)
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
);
95 BEGIN_RING(chan
, kelvin
, NV20_3D_COLOR_MATERIAL
, 1);
96 OUT_RING(chan
, ctx
->Light
.ColorMaterialEnabled
? mask
: 0);
100 get_fog_mode_signed(unsigned mode
)
104 return NV20_3D_FOG_MODE_LINEAR_SIGNED
;
106 return NV20_3D_FOG_MODE_EXP_SIGNED
;
108 return NV20_3D_FOG_MODE_EXP2_SIGNED
;
115 get_fog_mode_unsigned(unsigned mode
)
119 return NV20_3D_FOG_MODE_LINEAR_UNSIGNED
;
121 return NV20_3D_FOG_MODE_EXP_UNSIGNED
;
123 return NV20_3D_FOG_MODE_EXP2_UNSIGNED
;
130 get_fog_source(unsigned 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
;
143 nv20_emit_fog(struct gl_context
*ctx
, int emit
)
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
;
153 nv10_get_fog_coeff(ctx
, k
);
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_RING(chan
, f
->Enabled
? 1 : 0);
161 OUT_RING(chan
, pack_rgba_f(MESA_FORMAT_RGBA8888_REV
, f
->Color
));
163 BEGIN_RING(chan
, kelvin
, NV20_3D_FOG_COEFF(0), 3);
164 OUT_RINGp(chan
, k
, 3);
168 nv20_emit_light_model(struct gl_context
*ctx
, int emit
)
170 struct nouveau_channel
*chan
= context_chan(ctx
);
171 struct nouveau_grobj
*kelvin
= context_eng3d(ctx
);
172 struct gl_lightmodel
*m
= &ctx
->Light
.Model
;
174 BEGIN_RING(chan
, kelvin
, NV20_3D_SEPARATE_SPECULAR_ENABLE
, 1);
175 OUT_RING(chan
, m
->ColorControl
== GL_SEPARATE_SPECULAR_COLOR
? 1 : 0);
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 (NEED_SECONDARY_COLOR(ctx
) ?
182 NV20_3D_LIGHT_MODEL_SEPARATE_SPECULAR
:
185 BEGIN_RING(chan
, kelvin
, NV20_3D_LIGHT_MODEL_TWO_SIDE_ENABLE
, 1);
186 OUT_RING(chan
, ctx
->Light
.Model
.TwoSide
? 1 : 0);
190 nv20_emit_light_source(struct gl_context
*ctx
, int emit
)
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
];
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);
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
);
207 BEGIN_RING(chan
, kelvin
, NV20_3D_LIGHT_DIRECTION_X(i
), 3);
208 OUT_RINGp(chan
, l
->_VP_inf_norm
, 3);
210 BEGIN_RING(chan
, kelvin
, NV20_3D_LIGHT_HALF_VECTOR_X(i
), 3);
211 OUT_RINGp(chan
, l
->_h_inf_norm
, 3);
214 if (l
->_Flags
& LIGHT_SPOT
) {
217 nv10_get_spot_coeff(l
, k
);
219 BEGIN_RING(chan
, kelvin
, NV20_3D_LIGHT_SPOT_CUTOFF(i
, 0), 7);
220 OUT_RINGp(chan
, k
, 7);
224 #define USE_COLOR_MATERIAL(attr, side) \
225 (ctx->Light.ColorMaterialEnabled && \
226 ctx->Light.ColorMaterialBitmask & (1 << MAT_ATTRIB_##attr(side)))
229 nv20_emit_material_ambient(struct gl_context
*ctx
, int emit
)
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];
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
);
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);
248 COPY_3V(c_scene
, ctx
->Light
._BaseColor
[side
]);
252 BEGIN_RING(chan
, kelvin
, LIGHT_MODEL_AMBIENT_R(side
), 3);
253 OUT_RINGp(chan
, c_scene
, 3);
255 if (ctx
->Light
.ColorMaterialEnabled
) {
256 BEGIN_RING(chan
, kelvin
, MATERIAL_FACTOR_R(side
), 3);
257 OUT_RINGp(chan
, c_factor
, 3);
260 foreach(l
, &ctx
->Light
.EnabledList
) {
261 const int i
= l
- ctx
->Light
.Light
;
262 float *c_light
= (USE_COLOR_MATERIAL(AMBIENT
, side
) ?
264 l
->_MatAmbient
[side
]);
266 BEGIN_RING(chan
, kelvin
, LIGHT_AMBIENT_R(side
, i
), 3);
267 OUT_RINGp(chan
, c_light
, 3);
272 nv20_emit_material_diffuse(struct gl_context
*ctx
, int emit
)
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
;
280 BEGIN_RING(chan
, kelvin
, MATERIAL_FACTOR_A(side
), 1);
281 OUT_RINGf(chan
, mat
[MAT_ATTRIB_DIFFUSE(side
)][3]);
283 foreach(l
, &ctx
->Light
.EnabledList
) {
284 const int i
= l
- ctx
->Light
.Light
;
285 float *c_light
= (USE_COLOR_MATERIAL(DIFFUSE
, side
) ?
287 l
->_MatDiffuse
[side
]);
289 BEGIN_RING(chan
, kelvin
, LIGHT_DIFFUSE_R(side
, i
), 3);
290 OUT_RINGp(chan
, c_light
, 3);
295 nv20_emit_material_specular(struct gl_context
*ctx
, int emit
)
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
);
302 foreach(l
, &ctx
->Light
.EnabledList
) {
303 const int i
= l
- ctx
->Light
.Light
;
304 float *c_light
= (USE_COLOR_MATERIAL(SPECULAR
, side
) ?
306 l
->_MatSpecular
[side
]);
308 BEGIN_RING(chan
, kelvin
, LIGHT_SPECULAR_R(side
, i
), 3);
309 OUT_RINGp(chan
, c_light
, 3);
314 nv20_emit_material_shininess(struct gl_context
*ctx
, int emit
)
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
;
322 nv10_get_shininess_coeff(
323 CLAMP(mat
[MAT_ATTRIB_SHININESS(side
)][0], 0, 1024),
326 BEGIN_RING(chan
, kelvin
, MATERIAL_SHININESS(side
), 6);
327 OUT_RINGp(chan
, k
, 6);
331 nv20_emit_modelview(struct gl_context
*ctx
, int emit
)
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
;
338 if (nctx
->fallback
!= HWTNL
)
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
);
347 if (ctx
->Light
.Enabled
||
348 (ctx
->Texture
._GenFlags
& TEXGEN_NEED_EYE_COORD
)) {
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
]);
360 nv20_emit_projection(struct gl_context
*ctx
, int emit
)
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
);
367 _math_matrix_ctr(&m
);
368 get_viewport_scale(ctx
, m
.m
);
370 if (nctx
->fallback
== HWTNL
)
371 _math_matrix_mul_matrix(&m
, &m
, &ctx
->_ModelProjectMatrix
);
373 BEGIN_RING(chan
, kelvin
, NV20_3D_PROJECTION_MATRIX(0), 16);
374 OUT_RINGm(chan
, m
.m
);
376 _math_matrix_dtr(&m
);