6d0b262a4d3670de6f7870271a23e6f7da79fb55
[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 default:
75 assert(0);
76 }
77 }
78
79 static unsigned
80 get_texenv_mode(unsigned mode)
81 {
82 switch (mode) {
83 case GL_REPLACE:
84 return 0x1;
85 case GL_ADD:
86 return 0x2;
87 case GL_DECAL:
88 return 0x3;
89 case GL_MODULATE:
90 return 0x4;
91 default:
92 assert(0);
93 }
94 }
95
96 static unsigned
97 get_blend_func(unsigned func)
98 {
99 switch (func) {
100 case GL_ZERO:
101 return 0x1;
102 case GL_ONE:
103 return 0x2;
104 case GL_SRC_COLOR:
105 return 0x3;
106 case GL_ONE_MINUS_SRC_COLOR:
107 return 0x4;
108 case GL_SRC_ALPHA:
109 return 0x5;
110 case GL_ONE_MINUS_SRC_ALPHA:
111 return 0x6;
112 case GL_DST_ALPHA:
113 return 0x7;
114 case GL_ONE_MINUS_DST_ALPHA:
115 return 0x8;
116 case GL_DST_COLOR:
117 return 0x9;
118 case GL_ONE_MINUS_DST_COLOR:
119 return 0xa;
120 case GL_SRC_ALPHA_SATURATE:
121 return 0xb;
122 default:
123 assert(0);
124 }
125 }
126
127 void
128 nv04_defer_control(GLcontext *ctx, int emit)
129 {
130 context_dirty(ctx, CONTROL);
131 }
132
133 void
134 nv04_emit_control(GLcontext *ctx, int emit)
135 {
136 struct nouveau_channel *chan = context_chan(ctx);
137 struct nouveau_grobj *fahrenheit = nv04_context_engine(ctx);
138
139 if (nv04_mtex_engine(fahrenheit)) {
140 int cull_mode = ctx->Polygon.CullFaceMode;
141 int front_face = ctx->Polygon.FrontFace;
142 uint32_t ctrl0 = 1 << 30 |
143 NV04_MULTITEX_TRIANGLE_CONTROL0_ORIGIN;
144 uint32_t ctrl1 = 0, ctrl2 = 0;
145
146 /* Color mask. */
147 if (ctx->Color.ColorMask[0][RCOMP])
148 ctrl0 |= NV04_MULTITEX_TRIANGLE_CONTROL0_RED_WRITE;
149 if (ctx->Color.ColorMask[0][GCOMP])
150 ctrl0 |= NV04_MULTITEX_TRIANGLE_CONTROL0_GREEN_WRITE;
151 if (ctx->Color.ColorMask[0][BCOMP])
152 ctrl0 |= NV04_MULTITEX_TRIANGLE_CONTROL0_BLUE_WRITE;
153 if (ctx->Color.ColorMask[0][ACOMP])
154 ctrl0 |= NV04_MULTITEX_TRIANGLE_CONTROL0_ALPHA_WRITE;
155
156 /* Dithering. */
157 if (ctx->Color.DitherFlag)
158 ctrl0 |= NV04_MULTITEX_TRIANGLE_CONTROL0_DITHER_ENABLE;
159
160 /* Cull mode. */
161 if (!ctx->Polygon.CullFlag)
162 ctrl0 |= NV04_MULTITEX_TRIANGLE_CONTROL0_CULL_MODE_NONE;
163 else if (cull_mode == GL_FRONT_AND_BACK)
164 ctrl0 |= NV04_MULTITEX_TRIANGLE_CONTROL0_CULL_MODE_BOTH;
165 else
166 ctrl0 |= (cull_mode == GL_FRONT) ^ (front_face == GL_CCW) ?
167 NV04_MULTITEX_TRIANGLE_CONTROL0_CULL_MODE_CW :
168 NV04_MULTITEX_TRIANGLE_CONTROL0_CULL_MODE_CCW;
169
170 /* Depth test. */
171 if (ctx->Depth.Test)
172 ctrl0 |= NV04_MULTITEX_TRIANGLE_CONTROL0_Z_ENABLE;
173
174 if (ctx->Depth.Mask)
175 ctrl0 |= NV04_MULTITEX_TRIANGLE_CONTROL0_Z_WRITE;
176
177 ctrl0 |= get_comparison_op(ctx->Depth.Func) << 16;
178
179 /* Alpha test. */
180 if (ctx->Color.AlphaEnabled)
181 ctrl0 |= NV04_MULTITEX_TRIANGLE_CONTROL0_ALPHA_ENABLE;
182
183 ctrl0 |= get_comparison_op(ctx->Color.AlphaFunc) << 8 |
184 FLOAT_TO_UBYTE(ctx->Color.AlphaRef);
185
186 /* Stencil test. */
187 if (ctx->Stencil.WriteMask[0])
188 ctrl0 |= NV04_MULTITEX_TRIANGLE_CONTROL0_STENCIL_WRITE;
189
190 if (ctx->Stencil.Enabled)
191 ctrl1 |= NV04_MULTITEX_TRIANGLE_CONTROL1_STENCIL_ENABLE;
192
193 ctrl1 |= get_comparison_op(ctx->Stencil.Function[0]) << 4 |
194 ctx->Stencil.Ref[0] << 8 |
195 ctx->Stencil.ValueMask[0] << 16 |
196 ctx->Stencil.WriteMask[0] << 24;
197
198 ctrl2 |= get_stencil_op(ctx->Stencil.ZPassFunc[0]) << 8 |
199 get_stencil_op(ctx->Stencil.ZFailFunc[0]) << 4 |
200 get_stencil_op(ctx->Stencil.FailFunc[0]);
201
202 BEGIN_RING(chan, fahrenheit, NV04_MULTITEX_TRIANGLE_CONTROL0, 3);
203 OUT_RING(chan, ctrl0);
204 OUT_RING(chan, ctrl1);
205 OUT_RING(chan, ctrl2);
206
207 } else {
208 int cull_mode = ctx->Polygon.CullFaceMode;
209 int front_face = ctx->Polygon.FrontFace;
210 uint32_t ctrl = 1 << 30 |
211 NV04_TEXTURED_TRIANGLE_CONTROL_ORIGIN;
212
213 /* Dithering. */
214 if (ctx->Color.DitherFlag)
215 ctrl |= NV04_TEXTURED_TRIANGLE_CONTROL_DITHER_ENABLE;
216
217 /* Cull mode. */
218 if (!ctx->Polygon.CullFlag)
219 ctrl |= NV04_TEXTURED_TRIANGLE_CONTROL_CULL_MODE_NONE;
220 else if (cull_mode == GL_FRONT_AND_BACK)
221 ctrl |= NV04_TEXTURED_TRIANGLE_CONTROL_CULL_MODE_BOTH;
222 else
223 ctrl |= (cull_mode == GL_FRONT) ^ (front_face == GL_CCW) ?
224 NV04_TEXTURED_TRIANGLE_CONTROL_CULL_MODE_CW :
225 NV04_TEXTURED_TRIANGLE_CONTROL_CULL_MODE_CCW;
226
227 /* Depth test. */
228 if (ctx->Depth.Test)
229 ctrl |= NV04_TEXTURED_TRIANGLE_CONTROL_Z_ENABLE;
230 if (ctx->Depth.Mask)
231 ctrl |= NV04_TEXTURED_TRIANGLE_CONTROL_Z_WRITE;
232
233 ctrl |= get_comparison_op(ctx->Depth.Func) << 16;
234
235 /* Alpha test. */
236 if (ctx->Color.AlphaEnabled)
237 ctrl |= NV04_TEXTURED_TRIANGLE_CONTROL_ALPHA_ENABLE;
238
239 ctrl |= get_comparison_op(ctx->Color.AlphaFunc) << 8 |
240 FLOAT_TO_UBYTE(ctx->Color.AlphaRef);
241
242 BEGIN_RING(chan, fahrenheit, NV04_TEXTURED_TRIANGLE_CONTROL, 1);
243 OUT_RING(chan, ctrl);
244 }
245 }
246
247 void
248 nv04_defer_blend(GLcontext *ctx, int emit)
249 {
250 context_dirty(ctx, BLEND);
251 }
252
253 void
254 nv04_emit_blend(GLcontext *ctx, int emit)
255 {
256 struct nouveau_channel *chan = context_chan(ctx);
257 struct nouveau_grobj *fahrenheit = nv04_context_engine(ctx);
258
259 if (nv04_mtex_engine(fahrenheit)) {
260 uint32_t blend = 0x2 << 4 |
261 NV04_MULTITEX_TRIANGLE_BLEND_TEXTURE_PERSPECTIVE_ENABLE;
262
263 /* Alpha blending. */
264 blend |= get_blend_func(ctx->Color.BlendDstRGB) << 28 |
265 get_blend_func(ctx->Color.BlendSrcRGB) << 24;
266
267 if (ctx->Color.BlendEnabled)
268 blend |= NV04_MULTITEX_TRIANGLE_BLEND_BLEND_ENABLE;
269
270 /* Shade model. */
271 if (ctx->Light.ShadeModel == GL_SMOOTH)
272 blend |= NV04_MULTITEX_TRIANGLE_BLEND_SHADE_MODE_GOURAUD;
273 else
274 blend |= NV04_MULTITEX_TRIANGLE_BLEND_SHADE_MODE_FLAT;
275
276 /* Fog. */
277 if (ctx->Fog.Enabled)
278 blend |= NV04_MULTITEX_TRIANGLE_BLEND_FOG_ENABLE;
279
280 BEGIN_RING(chan, fahrenheit, NV04_MULTITEX_TRIANGLE_BLEND, 1);
281 OUT_RING(chan, blend);
282
283 BEGIN_RING(chan, fahrenheit, NV04_MULTITEX_TRIANGLE_FOGCOLOR, 1);
284 OUT_RING(chan, pack_rgba_f(MESA_FORMAT_ARGB8888,
285 ctx->Fog.Color));
286
287 } else {
288 uint32_t blend = 0x2 << 4 |
289 NV04_TEXTURED_TRIANGLE_BLEND_TEXTURE_PERSPECTIVE_ENABLE;
290
291 /* Alpha blending. */
292 blend |= get_blend_func(ctx->Color.BlendDstRGB) << 28 |
293 get_blend_func(ctx->Color.BlendSrcRGB) << 24;
294
295 if (ctx->Color.BlendEnabled)
296 blend |= NV04_TEXTURED_TRIANGLE_BLEND_BLEND_ENABLE;
297
298 /* Shade model. */
299 if (ctx->Light.ShadeModel == GL_SMOOTH)
300 blend |= NV04_TEXTURED_TRIANGLE_BLEND_SHADE_MODE_GOURAUD;
301 else
302 blend |= NV04_TEXTURED_TRIANGLE_BLEND_SHADE_MODE_FLAT;
303
304 /* Texture environment. */
305 blend |= get_texenv_mode(ctx->Texture.Unit[0].EnvMode);
306
307 /* Fog. */
308 if (ctx->Fog.Enabled)
309 blend |= NV04_TEXTURED_TRIANGLE_BLEND_FOG_ENABLE;
310
311 BEGIN_RING(chan, fahrenheit, NV04_TEXTURED_TRIANGLE_BLEND, 1);
312 OUT_RING(chan, blend);
313
314 BEGIN_RING(chan, fahrenheit, NV04_TEXTURED_TRIANGLE_FOGCOLOR, 1);
315 OUT_RING(chan, pack_rgba_f(MESA_FORMAT_ARGB8888,
316 ctx->Fog.Color));
317 }
318 }