2 * Copyright (C) 2009 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_util.h"
30 #include "nouveau_class.h"
31 #include "nv04_driver.h"
34 get_comparison_op(unsigned op
)
59 get_stencil_op(unsigned op
)
84 get_texenv_mode(unsigned mode
)
99 get_blend_func(unsigned func
)
108 case GL_ONE_MINUS_SRC_COLOR
:
112 case GL_ONE_MINUS_SRC_ALPHA
:
116 case GL_ONE_MINUS_DST_ALPHA
:
120 case GL_ONE_MINUS_DST_COLOR
:
122 case GL_SRC_ALPHA_SATURATE
:
130 nv04_defer_control(GLcontext
*ctx
, int emit
)
132 context_dirty(ctx
, CONTROL
);
136 nv04_emit_control(GLcontext
*ctx
, int emit
)
138 struct nouveau_channel
*chan
= context_chan(ctx
);
139 struct nouveau_grobj
*fahrenheit
= nv04_context_engine(ctx
);
141 if (nv04_mtex_engine(fahrenheit
)) {
142 int cull_mode
= ctx
->Polygon
.CullFaceMode
;
143 int front_face
= ctx
->Polygon
.FrontFace
;
144 uint32_t ctrl0
= 1 << 30 |
145 NV04_MULTITEX_TRIANGLE_CONTROL0_ORIGIN
;
146 uint32_t ctrl1
= 0, ctrl2
= 0;
149 if (ctx
->Color
.ColorMask
[0][RCOMP
])
150 ctrl0
|= NV04_MULTITEX_TRIANGLE_CONTROL0_RED_WRITE
;
151 if (ctx
->Color
.ColorMask
[0][GCOMP
])
152 ctrl0
|= NV04_MULTITEX_TRIANGLE_CONTROL0_GREEN_WRITE
;
153 if (ctx
->Color
.ColorMask
[0][BCOMP
])
154 ctrl0
|= NV04_MULTITEX_TRIANGLE_CONTROL0_BLUE_WRITE
;
155 if (ctx
->Color
.ColorMask
[0][ACOMP
])
156 ctrl0
|= NV04_MULTITEX_TRIANGLE_CONTROL0_ALPHA_WRITE
;
159 if (ctx
->Color
.DitherFlag
)
160 ctrl0
|= NV04_MULTITEX_TRIANGLE_CONTROL0_DITHER_ENABLE
;
163 if (!ctx
->Polygon
.CullFlag
)
164 ctrl0
|= NV04_MULTITEX_TRIANGLE_CONTROL0_CULL_MODE_NONE
;
165 else if (cull_mode
== GL_FRONT_AND_BACK
)
166 ctrl0
|= NV04_MULTITEX_TRIANGLE_CONTROL0_CULL_MODE_BOTH
;
168 ctrl0
|= (cull_mode
== GL_FRONT
) ^ (front_face
== GL_CCW
) ?
169 NV04_MULTITEX_TRIANGLE_CONTROL0_CULL_MODE_CW
:
170 NV04_MULTITEX_TRIANGLE_CONTROL0_CULL_MODE_CCW
;
174 ctrl0
|= NV04_MULTITEX_TRIANGLE_CONTROL0_Z_ENABLE
;
177 ctrl0
|= NV04_MULTITEX_TRIANGLE_CONTROL0_Z_WRITE
;
179 ctrl0
|= get_comparison_op(ctx
->Depth
.Func
) << 16;
182 if (ctx
->Color
.AlphaEnabled
)
183 ctrl0
|= NV04_MULTITEX_TRIANGLE_CONTROL0_ALPHA_ENABLE
;
185 ctrl0
|= get_comparison_op(ctx
->Color
.AlphaFunc
) << 8 |
186 FLOAT_TO_UBYTE(ctx
->Color
.AlphaRef
);
189 if (ctx
->Stencil
.WriteMask
[0])
190 ctrl0
|= NV04_MULTITEX_TRIANGLE_CONTROL0_STENCIL_WRITE
;
192 if (ctx
->Stencil
.Enabled
)
193 ctrl1
|= NV04_MULTITEX_TRIANGLE_CONTROL1_STENCIL_ENABLE
;
195 ctrl1
|= get_comparison_op(ctx
->Stencil
.Function
[0]) << 4 |
196 ctx
->Stencil
.Ref
[0] << 8 |
197 ctx
->Stencil
.ValueMask
[0] << 16 |
198 ctx
->Stencil
.WriteMask
[0] << 24;
200 ctrl2
|= get_stencil_op(ctx
->Stencil
.ZPassFunc
[0]) << 8 |
201 get_stencil_op(ctx
->Stencil
.ZFailFunc
[0]) << 4 |
202 get_stencil_op(ctx
->Stencil
.FailFunc
[0]);
204 BEGIN_RING(chan
, fahrenheit
, NV04_MULTITEX_TRIANGLE_CONTROL0
, 3);
205 OUT_RING(chan
, ctrl0
);
206 OUT_RING(chan
, ctrl1
);
207 OUT_RING(chan
, ctrl2
);
210 int cull_mode
= ctx
->Polygon
.CullFaceMode
;
211 int front_face
= ctx
->Polygon
.FrontFace
;
212 uint32_t ctrl
= 1 << 30 |
213 NV04_TEXTURED_TRIANGLE_CONTROL_ORIGIN
;
216 if (ctx
->Color
.DitherFlag
)
217 ctrl
|= NV04_TEXTURED_TRIANGLE_CONTROL_DITHER_ENABLE
;
220 if (!ctx
->Polygon
.CullFlag
)
221 ctrl
|= NV04_TEXTURED_TRIANGLE_CONTROL_CULL_MODE_NONE
;
222 else if (cull_mode
== GL_FRONT_AND_BACK
)
223 ctrl
|= NV04_TEXTURED_TRIANGLE_CONTROL_CULL_MODE_BOTH
;
225 ctrl
|= (cull_mode
== GL_FRONT
) ^ (front_face
== GL_CCW
) ?
226 NV04_TEXTURED_TRIANGLE_CONTROL_CULL_MODE_CW
:
227 NV04_TEXTURED_TRIANGLE_CONTROL_CULL_MODE_CCW
;
231 ctrl
|= NV04_TEXTURED_TRIANGLE_CONTROL_Z_ENABLE
;
233 ctrl
|= NV04_TEXTURED_TRIANGLE_CONTROL_Z_WRITE
;
235 ctrl
|= get_comparison_op(ctx
->Depth
.Func
) << 16;
238 if (ctx
->Color
.AlphaEnabled
)
239 ctrl
|= NV04_TEXTURED_TRIANGLE_CONTROL_ALPHA_ENABLE
;
241 ctrl
|= get_comparison_op(ctx
->Color
.AlphaFunc
) << 8 |
242 FLOAT_TO_UBYTE(ctx
->Color
.AlphaRef
);
244 BEGIN_RING(chan
, fahrenheit
, NV04_TEXTURED_TRIANGLE_CONTROL
, 1);
245 OUT_RING(chan
, ctrl
);
250 nv04_defer_blend(GLcontext
*ctx
, int emit
)
252 context_dirty(ctx
, BLEND
);
256 nv04_emit_blend(GLcontext
*ctx
, int emit
)
258 struct nouveau_channel
*chan
= context_chan(ctx
);
259 struct nouveau_grobj
*fahrenheit
= nv04_context_engine(ctx
);
261 if (nv04_mtex_engine(fahrenheit
)) {
262 uint32_t blend
= 0x2 << 4 |
263 NV04_MULTITEX_TRIANGLE_BLEND_TEXTURE_PERSPECTIVE_ENABLE
;
265 /* Alpha blending. */
266 blend
|= get_blend_func(ctx
->Color
.BlendDstRGB
) << 28 |
267 get_blend_func(ctx
->Color
.BlendSrcRGB
) << 24;
269 if (ctx
->Color
.BlendEnabled
)
270 blend
|= NV04_MULTITEX_TRIANGLE_BLEND_BLEND_ENABLE
;
273 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
)
274 blend
|= NV04_MULTITEX_TRIANGLE_BLEND_SHADE_MODE_GOURAUD
;
276 blend
|= NV04_MULTITEX_TRIANGLE_BLEND_SHADE_MODE_FLAT
;
278 /* Secondary color */
279 if (NEED_SECONDARY_COLOR(ctx
))
280 blend
|= NV04_MULTITEX_TRIANGLE_BLEND_SPECULAR_ENABLE
;
283 if (ctx
->Fog
.Enabled
)
284 blend
|= NV04_MULTITEX_TRIANGLE_BLEND_FOG_ENABLE
;
286 BEGIN_RING(chan
, fahrenheit
, NV04_MULTITEX_TRIANGLE_BLEND
, 1);
287 OUT_RING(chan
, blend
);
289 BEGIN_RING(chan
, fahrenheit
, NV04_MULTITEX_TRIANGLE_FOGCOLOR
, 1);
290 OUT_RING(chan
, pack_rgba_f(MESA_FORMAT_ARGB8888
,
294 uint32_t blend
= 0x2 << 4 |
295 NV04_TEXTURED_TRIANGLE_BLEND_TEXTURE_PERSPECTIVE_ENABLE
;
297 /* Alpha blending. */
298 blend
|= get_blend_func(ctx
->Color
.BlendDstRGB
) << 28 |
299 get_blend_func(ctx
->Color
.BlendSrcRGB
) << 24;
301 if (ctx
->Color
.BlendEnabled
)
302 blend
|= NV04_TEXTURED_TRIANGLE_BLEND_BLEND_ENABLE
;
305 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
)
306 blend
|= NV04_TEXTURED_TRIANGLE_BLEND_SHADE_MODE_GOURAUD
;
308 blend
|= NV04_TEXTURED_TRIANGLE_BLEND_SHADE_MODE_FLAT
;
310 /* Texture environment. */
311 if (ctx
->Texture
._EnabledUnits
)
312 blend
|= get_texenv_mode(ctx
->Texture
.Unit
[0].EnvMode
);
314 blend
|= get_texenv_mode(GL_MODULATE
);
316 /* Secondary color */
317 if (NEED_SECONDARY_COLOR(ctx
))
318 blend
|= NV04_TEXTURED_TRIANGLE_BLEND_SPECULAR_ENABLE
;
321 if (ctx
->Fog
.Enabled
)
322 blend
|= NV04_TEXTURED_TRIANGLE_BLEND_FOG_ENABLE
;
324 BEGIN_RING(chan
, fahrenheit
, NV04_TEXTURED_TRIANGLE_BLEND
, 1);
325 OUT_RING(chan
, blend
);
327 BEGIN_RING(chan
, fahrenheit
, NV04_TEXTURED_TRIANGLE_FOGCOLOR
, 1);
328 OUT_RING(chan
, pack_rgba_f(MESA_FORMAT_ARGB8888
,