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_pushbuf
*push
= context_push(ctx
);
92 unsigned mask
= get_material_bitmask(ctx
->Light
._ColorMaterialBitmask
);
94 BEGIN_NV04(push
, NV20_3D(COLOR_MATERIAL
), 1);
95 PUSH_DATA (push
, ctx
->Light
.ColorMaterialEnabled
? mask
: 0);
99 get_fog_mode_signed(unsigned mode
)
103 return NV20_3D_FOG_MODE_LINEAR_SIGNED
;
105 return NV20_3D_FOG_MODE_EXP_SIGNED
;
107 return NV20_3D_FOG_MODE_EXP2_SIGNED
;
114 get_fog_mode_unsigned(unsigned mode
)
118 return NV20_3D_FOG_MODE_LINEAR_UNSIGNED
;
120 return NV20_3D_FOG_MODE_EXP_UNSIGNED
;
122 return NV20_3D_FOG_MODE_EXP2_UNSIGNED
;
129 get_fog_source(unsigned source
, unsigned distance_mode
)
132 case GL_FOG_COORDINATE_EXT
:
133 return NV20_3D_FOG_COORD_FOG
;
134 case GL_FRAGMENT_DEPTH_EXT
:
135 switch (distance_mode
) {
136 case GL_EYE_PLANE_ABSOLUTE_NV
:
137 return NV20_3D_FOG_COORD_DIST_ORTHOGONAL_ABS
;
139 return NV20_3D_FOG_COORD_DIST_ORTHOGONAL
;
140 case GL_EYE_RADIAL_NV
:
141 return NV20_3D_FOG_COORD_DIST_RADIAL
;
151 nv20_emit_fog(struct gl_context
*ctx
, int emit
)
153 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
154 struct nouveau_pushbuf
*push
= context_push(ctx
);
155 struct gl_fog_attrib
*f
= &ctx
->Fog
;
156 unsigned source
= nctx
->fallback
== HWTNL
?
157 f
->FogCoordinateSource
: GL_FOG_COORDINATE_EXT
;
160 nv10_get_fog_coeff(ctx
, k
);
162 BEGIN_NV04(push
, NV20_3D(FOG_MODE
), 4);
163 PUSH_DATA (push
, ((source
== GL_FRAGMENT_DEPTH_EXT
&&
164 f
->FogDistanceMode
== GL_EYE_PLANE_ABSOLUTE_NV
) ?
165 get_fog_mode_unsigned(f
->Mode
) :
166 get_fog_mode_signed(f
->Mode
)));
167 PUSH_DATA (push
, get_fog_source(source
, f
->FogDistanceMode
));
168 PUSH_DATAb(push
, f
->Enabled
);
169 PUSH_DATA (push
, pack_rgba_f(MESA_FORMAT_RGBA8888_REV
, f
->Color
));
171 BEGIN_NV04(push
, NV20_3D(FOG_COEFF(0)), 3);
172 PUSH_DATAp(push
, k
, 3);
176 nv20_emit_light_model(struct gl_context
*ctx
, int emit
)
178 struct nouveau_pushbuf
*push
= context_push(ctx
);
179 struct gl_lightmodel
*m
= &ctx
->Light
.Model
;
181 BEGIN_NV04(push
, NV20_3D(SEPARATE_SPECULAR_ENABLE
), 1);
182 PUSH_DATAb(push
, m
->ColorControl
== GL_SEPARATE_SPECULAR_COLOR
);
184 BEGIN_NV04(push
, NV20_3D(LIGHT_MODEL
), 1);
185 PUSH_DATA (push
, ((m
->LocalViewer
?
186 NV20_3D_LIGHT_MODEL_VIEWER_LOCAL
:
187 NV20_3D_LIGHT_MODEL_VIEWER_NONLOCAL
) |
188 (_mesa_need_secondary_color(ctx
) ?
189 NV20_3D_LIGHT_MODEL_SEPARATE_SPECULAR
:
192 BEGIN_NV04(push
, NV20_3D(LIGHT_MODEL_TWO_SIDE_ENABLE
), 1);
193 PUSH_DATAb(push
, ctx
->Light
.Model
.TwoSide
);
197 nv20_emit_light_source(struct gl_context
*ctx
, int emit
)
199 const int i
= emit
- NOUVEAU_STATE_LIGHT_SOURCE0
;
200 struct nouveau_pushbuf
*push
= context_push(ctx
);
201 struct gl_light
*l
= &ctx
->Light
.Light
[i
];
203 if (l
->_Flags
& LIGHT_POSITIONAL
) {
204 BEGIN_NV04(push
, NV20_3D(LIGHT_POSITION_X(i
)), 3);
205 PUSH_DATAp(push
, l
->_Position
, 3);
207 BEGIN_NV04(push
, NV20_3D(LIGHT_ATTENUATION_CONSTANT(i
)), 3);
208 PUSH_DATAf(push
, l
->ConstantAttenuation
);
209 PUSH_DATAf(push
, l
->LinearAttenuation
);
210 PUSH_DATAf(push
, l
->QuadraticAttenuation
);
213 BEGIN_NV04(push
, NV20_3D(LIGHT_DIRECTION_X(i
)), 3);
214 PUSH_DATAp(push
, l
->_VP_inf_norm
, 3);
216 BEGIN_NV04(push
, NV20_3D(LIGHT_HALF_VECTOR_X(i
)), 3);
217 PUSH_DATAp(push
, l
->_h_inf_norm
, 3);
220 if (l
->_Flags
& LIGHT_SPOT
) {
223 nv10_get_spot_coeff(l
, k
);
225 BEGIN_NV04(push
, NV20_3D(LIGHT_SPOT_CUTOFF(i
, 0)), 7);
226 PUSH_DATAp(push
, k
, 7);
230 #define USE_COLOR_MATERIAL(attr, side) \
231 (ctx->Light.ColorMaterialEnabled && \
232 ctx->Light._ColorMaterialBitmask & (1 << MAT_ATTRIB_##attr(side)))
235 nv20_emit_material_ambient(struct gl_context
*ctx
, int emit
)
237 const int side
= emit
- NOUVEAU_STATE_MATERIAL_FRONT_AMBIENT
;
238 struct nouveau_pushbuf
*push
= context_push(ctx
);
239 float (*mat
)[4] = ctx
->Light
.Material
.Attrib
;
240 float c_scene
[3], c_factor
[3];
243 if (USE_COLOR_MATERIAL(AMBIENT
, side
)) {
244 COPY_3V(c_scene
, mat
[MAT_ATTRIB_EMISSION(side
)]);
245 COPY_3V(c_factor
, ctx
->Light
.Model
.Ambient
);
247 } else if (USE_COLOR_MATERIAL(EMISSION
, side
)) {
248 SCALE_3V(c_scene
, mat
[MAT_ATTRIB_AMBIENT(side
)],
249 ctx
->Light
.Model
.Ambient
);
250 ASSIGN_3V(c_factor
, 1, 1, 1);
253 COPY_3V(c_scene
, ctx
->Light
._BaseColor
[side
]);
257 BEGIN_NV04(push
, SUBC_3D(LIGHT_MODEL_AMBIENT_R(side
)), 3);
258 PUSH_DATAp(push
, c_scene
, 3);
260 if (ctx
->Light
.ColorMaterialEnabled
) {
261 BEGIN_NV04(push
, SUBC_3D(MATERIAL_FACTOR_R(side
)), 3);
262 PUSH_DATAp(push
, c_factor
, 3);
265 foreach(l
, &ctx
->Light
.EnabledList
) {
266 const int i
= l
- ctx
->Light
.Light
;
267 float *c_light
= (USE_COLOR_MATERIAL(AMBIENT
, side
) ?
269 l
->_MatAmbient
[side
]);
271 BEGIN_NV04(push
, SUBC_3D(LIGHT_AMBIENT_R(side
, i
)), 3);
272 PUSH_DATAp(push
, c_light
, 3);
277 nv20_emit_material_diffuse(struct gl_context
*ctx
, int emit
)
279 const int side
= emit
- NOUVEAU_STATE_MATERIAL_FRONT_DIFFUSE
;
280 struct nouveau_pushbuf
*push
= context_push(ctx
);
281 GLfloat (*mat
)[4] = ctx
->Light
.Material
.Attrib
;
284 BEGIN_NV04(push
, SUBC_3D(MATERIAL_FACTOR_A(side
)), 1);
285 PUSH_DATAf(push
, mat
[MAT_ATTRIB_DIFFUSE(side
)][3]);
287 foreach(l
, &ctx
->Light
.EnabledList
) {
288 const int i
= l
- ctx
->Light
.Light
;
289 float *c_light
= (USE_COLOR_MATERIAL(DIFFUSE
, side
) ?
291 l
->_MatDiffuse
[side
]);
293 BEGIN_NV04(push
, SUBC_3D(LIGHT_DIFFUSE_R(side
, i
)), 3);
294 PUSH_DATAp(push
, c_light
, 3);
299 nv20_emit_material_specular(struct gl_context
*ctx
, int emit
)
301 const int side
= emit
- NOUVEAU_STATE_MATERIAL_FRONT_SPECULAR
;
302 struct nouveau_pushbuf
*push
= context_push(ctx
);
305 foreach(l
, &ctx
->Light
.EnabledList
) {
306 const int i
= l
- ctx
->Light
.Light
;
307 float *c_light
= (USE_COLOR_MATERIAL(SPECULAR
, side
) ?
309 l
->_MatSpecular
[side
]);
311 BEGIN_NV04(push
, SUBC_3D(LIGHT_SPECULAR_R(side
, i
)), 3);
312 PUSH_DATAp(push
, c_light
, 3);
317 nv20_emit_material_shininess(struct gl_context
*ctx
, int emit
)
319 const int side
= emit
- NOUVEAU_STATE_MATERIAL_FRONT_SHININESS
;
320 struct nouveau_pushbuf
*push
= context_push(ctx
);
321 float (*mat
)[4] = ctx
->Light
.Material
.Attrib
;
324 nv10_get_shininess_coeff(
325 CLAMP(mat
[MAT_ATTRIB_SHININESS(side
)][0], 0, 1024),
328 BEGIN_NV04(push
, SUBC_3D(MATERIAL_SHININESS(side
)), 6);
329 PUSH_DATAp(push
, k
, 6);
333 nv20_emit_modelview(struct gl_context
*ctx
, int emit
)
335 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
336 struct nouveau_pushbuf
*push
= context_push(ctx
);
337 GLmatrix
*m
= ctx
->ModelviewMatrixStack
.Top
;
339 if (nctx
->fallback
!= HWTNL
)
342 if (ctx
->Light
._NeedEyeCoords
|| ctx
->Fog
.Enabled
||
343 (ctx
->Texture
._GenFlags
& TEXGEN_NEED_EYE_COORD
)) {
344 BEGIN_NV04(push
, NV20_3D(MODELVIEW_MATRIX(0, 0)), 16);
345 PUSH_DATAm(push
, m
->m
);
348 if (ctx
->Light
.Enabled
||
349 (ctx
->Texture
._GenFlags
& TEXGEN_NEED_EYE_COORD
)) {
352 BEGIN_NV04(push
, NV20_3D(INVERSE_MODELVIEW_MATRIX(0, 0)), 12);
353 for (i
= 0; i
< 3; i
++)
354 for (j
= 0; j
< 4; j
++)
355 PUSH_DATAf(push
, 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_pushbuf
*push
= context_push(ctx
);
366 _math_matrix_ctr(&m
);
367 get_viewport_scale(ctx
, m
.m
);
369 if (nctx
->fallback
== HWTNL
)
370 _math_matrix_mul_matrix(&m
, &m
, &ctx
->_ModelProjectMatrix
);
372 BEGIN_NV04(push
, NV20_3D(PROJECTION_MATRIX(0)), 16);
373 PUSH_DATAm(push
, m
.m
);
375 _math_matrix_dtr(&m
);