Merge branch '7.8'
[mesa.git] / src / mesa / drivers / dri / nouveau / nv04_state_raster.c
1 /*
2 * Copyright (C) 2009 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_util.h"
30 #include "nouveau_class.h"
31 #include "nv04_driver.h"
32
33 static unsigned
34 get_comparison_op(unsigned op)
35 {
36 switch (op) {
37 case GL_NEVER:
38 return 0x1;
39 case GL_LESS:
40 return 0x2;
41 case GL_EQUAL:
42 return 0x3;
43 case GL_LEQUAL:
44 return 0x4;
45 case GL_GREATER:
46 return 0x5;
47 case GL_NOTEQUAL:
48 return 0x6;
49 case GL_GEQUAL:
50 return 0x7;
51 case GL_ALWAYS:
52 return 0x8;
53 default:
54 assert(0);
55 }
56 }
57
58 static unsigned
59 get_stencil_op(unsigned op)
60 {
61 switch (op) {
62 case GL_KEEP:
63 return 0x1;
64 case GL_ZERO:
65 return 0x2;
66 case GL_REPLACE:
67 return 0x3;
68 case GL_INCR:
69 return 0x4;
70 case GL_DECR:
71 return 0x5;
72 case GL_INVERT:
73 return 0x6;
74 case GL_INCR_WRAP:
75 return 0x7;
76 case GL_DECR_WRAP:
77 return 0x8;
78 default:
79 assert(0);
80 }
81 }
82
83 static unsigned
84 get_texenv_mode(unsigned mode)
85 {
86 switch (mode) {
87 case GL_REPLACE:
88 return 0x1;
89 case GL_DECAL:
90 return 0x3;
91 case GL_MODULATE:
92 return 0x4;
93 default:
94 assert(0);
95 }
96 }
97
98 static unsigned
99 get_blend_func(unsigned func)
100 {
101 switch (func) {
102 case GL_ZERO:
103 return 0x1;
104 case GL_ONE:
105 return 0x2;
106 case GL_SRC_COLOR:
107 return 0x3;
108 case GL_ONE_MINUS_SRC_COLOR:
109 return 0x4;
110 case GL_SRC_ALPHA:
111 return 0x5;
112 case GL_ONE_MINUS_SRC_ALPHA:
113 return 0x6;
114 case GL_DST_ALPHA:
115 return 0x7;
116 case GL_ONE_MINUS_DST_ALPHA:
117 return 0x8;
118 case GL_DST_COLOR:
119 return 0x9;
120 case GL_ONE_MINUS_DST_COLOR:
121 return 0xa;
122 case GL_SRC_ALPHA_SATURATE:
123 return 0xb;
124 default:
125 assert(0);
126 }
127 }
128
129 void
130 nv04_defer_control(GLcontext *ctx, int emit)
131 {
132 context_dirty(ctx, CONTROL);
133 }
134
135 void
136 nv04_emit_control(GLcontext *ctx, int emit)
137 {
138 struct nouveau_channel *chan = context_chan(ctx);
139 struct nouveau_grobj *fahrenheit = nv04_context_engine(ctx);
140
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;
147
148 /* Color mask. */
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;
157
158 /* Dithering. */
159 if (ctx->Color.DitherFlag)
160 ctrl0 |= NV04_MULTITEX_TRIANGLE_CONTROL0_DITHER_ENABLE;
161
162 /* Cull mode. */
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;
167 else
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;
171
172 /* Depth test. */
173 if (ctx->Depth.Test)
174 ctrl0 |= NV04_MULTITEX_TRIANGLE_CONTROL0_Z_ENABLE;
175
176 if (ctx->Depth.Mask)
177 ctrl0 |= NV04_MULTITEX_TRIANGLE_CONTROL0_Z_WRITE;
178
179 ctrl0 |= get_comparison_op(ctx->Depth.Func) << 16;
180
181 /* Alpha test. */
182 if (ctx->Color.AlphaEnabled)
183 ctrl0 |= NV04_MULTITEX_TRIANGLE_CONTROL0_ALPHA_ENABLE;
184
185 ctrl0 |= get_comparison_op(ctx->Color.AlphaFunc) << 8 |
186 FLOAT_TO_UBYTE(ctx->Color.AlphaRef);
187
188 /* Stencil test. */
189 if (ctx->Stencil.WriteMask[0])
190 ctrl0 |= NV04_MULTITEX_TRIANGLE_CONTROL0_STENCIL_WRITE;
191
192 if (ctx->Stencil.Enabled)
193 ctrl1 |= NV04_MULTITEX_TRIANGLE_CONTROL1_STENCIL_ENABLE;
194
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;
199
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]);
203
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);
208
209 } else {
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;
214
215 /* Dithering. */
216 if (ctx->Color.DitherFlag)
217 ctrl |= NV04_TEXTURED_TRIANGLE_CONTROL_DITHER_ENABLE;
218
219 /* Cull mode. */
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;
224 else
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;
228
229 /* Depth test. */
230 if (ctx->Depth.Test)
231 ctrl |= NV04_TEXTURED_TRIANGLE_CONTROL_Z_ENABLE;
232 if (ctx->Depth.Mask)
233 ctrl |= NV04_TEXTURED_TRIANGLE_CONTROL_Z_WRITE;
234
235 ctrl |= get_comparison_op(ctx->Depth.Func) << 16;
236
237 /* Alpha test. */
238 if (ctx->Color.AlphaEnabled)
239 ctrl |= NV04_TEXTURED_TRIANGLE_CONTROL_ALPHA_ENABLE;
240
241 ctrl |= get_comparison_op(ctx->Color.AlphaFunc) << 8 |
242 FLOAT_TO_UBYTE(ctx->Color.AlphaRef);
243
244 BEGIN_RING(chan, fahrenheit, NV04_TEXTURED_TRIANGLE_CONTROL, 1);
245 OUT_RING(chan, ctrl);
246 }
247 }
248
249 void
250 nv04_defer_blend(GLcontext *ctx, int emit)
251 {
252 context_dirty(ctx, BLEND);
253 }
254
255 void
256 nv04_emit_blend(GLcontext *ctx, int emit)
257 {
258 struct nouveau_channel *chan = context_chan(ctx);
259 struct nouveau_grobj *fahrenheit = nv04_context_engine(ctx);
260
261 if (nv04_mtex_engine(fahrenheit)) {
262 uint32_t blend = 0x2 << 4 |
263 NV04_MULTITEX_TRIANGLE_BLEND_TEXTURE_PERSPECTIVE_ENABLE;
264
265 /* Alpha blending. */
266 blend |= get_blend_func(ctx->Color.BlendDstRGB) << 28 |
267 get_blend_func(ctx->Color.BlendSrcRGB) << 24;
268
269 if (ctx->Color.BlendEnabled)
270 blend |= NV04_MULTITEX_TRIANGLE_BLEND_BLEND_ENABLE;
271
272 /* Shade model. */
273 if (ctx->Light.ShadeModel == GL_SMOOTH)
274 blend |= NV04_MULTITEX_TRIANGLE_BLEND_SHADE_MODE_GOURAUD;
275 else
276 blend |= NV04_MULTITEX_TRIANGLE_BLEND_SHADE_MODE_FLAT;
277
278 /* Secondary color */
279 if (NEED_SECONDARY_COLOR(ctx))
280 blend |= NV04_MULTITEX_TRIANGLE_BLEND_SPECULAR_ENABLE;
281
282 /* Fog. */
283 if (ctx->Fog.Enabled)
284 blend |= NV04_MULTITEX_TRIANGLE_BLEND_FOG_ENABLE;
285
286 BEGIN_RING(chan, fahrenheit, NV04_MULTITEX_TRIANGLE_BLEND, 1);
287 OUT_RING(chan, blend);
288
289 BEGIN_RING(chan, fahrenheit, NV04_MULTITEX_TRIANGLE_FOGCOLOR, 1);
290 OUT_RING(chan, pack_rgba_f(MESA_FORMAT_ARGB8888,
291 ctx->Fog.Color));
292
293 } else {
294 uint32_t blend = 0x2 << 4 |
295 NV04_TEXTURED_TRIANGLE_BLEND_TEXTURE_PERSPECTIVE_ENABLE;
296
297 /* Alpha blending. */
298 blend |= get_blend_func(ctx->Color.BlendDstRGB) << 28 |
299 get_blend_func(ctx->Color.BlendSrcRGB) << 24;
300
301 if (ctx->Color.BlendEnabled)
302 blend |= NV04_TEXTURED_TRIANGLE_BLEND_BLEND_ENABLE;
303
304 /* Shade model. */
305 if (ctx->Light.ShadeModel == GL_SMOOTH)
306 blend |= NV04_TEXTURED_TRIANGLE_BLEND_SHADE_MODE_GOURAUD;
307 else
308 blend |= NV04_TEXTURED_TRIANGLE_BLEND_SHADE_MODE_FLAT;
309
310 /* Texture environment. */
311 if (ctx->Texture._EnabledUnits)
312 blend |= get_texenv_mode(ctx->Texture.Unit[0].EnvMode);
313 else
314 blend |= get_texenv_mode(GL_MODULATE);
315
316 /* Secondary color */
317 if (NEED_SECONDARY_COLOR(ctx))
318 blend |= NV04_TEXTURED_TRIANGLE_BLEND_SPECULAR_ENABLE;
319
320 /* Fog. */
321 if (ctx->Fog.Enabled)
322 blend |= NV04_TEXTURED_TRIANGLE_BLEND_FOG_ENABLE;
323
324 BEGIN_RING(chan, fahrenheit, NV04_TEXTURED_TRIANGLE_BLEND, 1);
325 OUT_RING(chan, blend);
326
327 BEGIN_RING(chan, fahrenheit, NV04_TEXTURED_TRIANGLE_FOGCOLOR, 1);
328 OUT_RING(chan, pack_rgba_f(MESA_FORMAT_ARGB8888,
329 ctx->Fog.Color));
330 }
331 }