41261195d814e99026b293f408f30210f65655ce
[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 "nv_object.xml.h"
31 #include "nv04_3d.xml.h"
32 #include "nv04_driver.h"
33 #include "main/stencil.h"
34
35 static unsigned
36 get_comparison_op(unsigned op)
37 {
38 switch (op) {
39 case GL_NEVER:
40 return 0x1;
41 case GL_LESS:
42 return 0x2;
43 case GL_EQUAL:
44 return 0x3;
45 case GL_LEQUAL:
46 return 0x4;
47 case GL_GREATER:
48 return 0x5;
49 case GL_NOTEQUAL:
50 return 0x6;
51 case GL_GEQUAL:
52 return 0x7;
53 case GL_ALWAYS:
54 return 0x8;
55 default:
56 assert(0);
57 }
58 }
59
60 static unsigned
61 get_stencil_op(unsigned op)
62 {
63 switch (op) {
64 case GL_KEEP:
65 return 0x1;
66 case GL_ZERO:
67 return 0x2;
68 case GL_REPLACE:
69 return 0x3;
70 case GL_INCR:
71 return 0x4;
72 case GL_DECR:
73 return 0x5;
74 case GL_INVERT:
75 return 0x6;
76 case GL_INCR_WRAP:
77 return 0x7;
78 case GL_DECR_WRAP:
79 return 0x8;
80 default:
81 assert(0);
82 }
83 }
84
85 static unsigned
86 get_blend_func(unsigned func)
87 {
88 switch (func) {
89 case GL_ZERO:
90 return 0x1;
91 case GL_ONE:
92 return 0x2;
93 case GL_SRC_COLOR:
94 return 0x3;
95 case GL_ONE_MINUS_SRC_COLOR:
96 return 0x4;
97 case GL_SRC_ALPHA:
98 return 0x5;
99 case GL_ONE_MINUS_SRC_ALPHA:
100 return 0x6;
101 case GL_DST_ALPHA:
102 return 0x7;
103 case GL_ONE_MINUS_DST_ALPHA:
104 return 0x8;
105 case GL_DST_COLOR:
106 return 0x9;
107 case GL_ONE_MINUS_DST_COLOR:
108 return 0xa;
109 case GL_SRC_ALPHA_SATURATE:
110 return 0xb;
111 default:
112 assert(0);
113 }
114 }
115
116 void
117 nv04_defer_control(struct gl_context *ctx, int emit)
118 {
119 context_dirty(ctx, CONTROL);
120 }
121
122 void
123 nv04_emit_control(struct gl_context *ctx, int emit)
124 {
125 struct nv04_context *nv04 = to_nv04_context(ctx);
126 int cull = ctx->Polygon.CullFaceMode;
127 int front = ctx->Polygon.FrontFace;
128
129 nv04->ctrl[0] = NV04_TEXTURED_TRIANGLE_CONTROL_Z_FORMAT_FIXED |
130 NV04_TEXTURED_TRIANGLE_CONTROL_ORIGIN_CORNER;
131 nv04->ctrl[1] = 0;
132 nv04->ctrl[2] = 0;
133
134 /* Dithering. */
135 if (ctx->Color.DitherFlag)
136 nv04->ctrl[0] |= NV04_TEXTURED_TRIANGLE_CONTROL_DITHER_ENABLE;
137
138 /* Cull mode. */
139 if (!ctx->Polygon.CullFlag)
140 nv04->ctrl[0] |= NV04_TEXTURED_TRIANGLE_CONTROL_CULL_MODE_NONE;
141 else if (cull == GL_FRONT_AND_BACK)
142 nv04->ctrl[0] |= NV04_TEXTURED_TRIANGLE_CONTROL_CULL_MODE_BOTH;
143 else
144 nv04->ctrl[0] |= (cull == GL_FRONT) ^ (front == GL_CCW) ?
145 NV04_TEXTURED_TRIANGLE_CONTROL_CULL_MODE_CW :
146 NV04_TEXTURED_TRIANGLE_CONTROL_CULL_MODE_CCW;
147
148 /* Depth test. */
149 if (ctx->Depth.Test)
150 nv04->ctrl[0] |= NV04_TEXTURED_TRIANGLE_CONTROL_Z_ENABLE;
151 if (ctx->Depth.Mask)
152 nv04->ctrl[0] |= NV04_TEXTURED_TRIANGLE_CONTROL_Z_WRITE;
153
154 nv04->ctrl[0] |= get_comparison_op(ctx->Depth.Func) << 16;
155
156 /* Alpha test. */
157 if (ctx->Color.AlphaEnabled)
158 nv04->ctrl[0] |= NV04_TEXTURED_TRIANGLE_CONTROL_ALPHA_ENABLE;
159
160 nv04->ctrl[0] |= get_comparison_op(ctx->Color.AlphaFunc) << 8 |
161 FLOAT_TO_UBYTE(ctx->Color.AlphaRef);
162
163 /* Color mask. */
164 if (ctx->Color.ColorMask[0][RCOMP])
165 nv04->ctrl[0] |= NV04_MULTITEX_TRIANGLE_CONTROL0_RED_WRITE;
166 if (ctx->Color.ColorMask[0][GCOMP])
167 nv04->ctrl[0] |= NV04_MULTITEX_TRIANGLE_CONTROL0_GREEN_WRITE;
168 if (ctx->Color.ColorMask[0][BCOMP])
169 nv04->ctrl[0] |= NV04_MULTITEX_TRIANGLE_CONTROL0_BLUE_WRITE;
170 if (ctx->Color.ColorMask[0][ACOMP])
171 nv04->ctrl[0] |= NV04_MULTITEX_TRIANGLE_CONTROL0_ALPHA_WRITE;
172
173 /* Stencil test. */
174 if (ctx->Stencil.WriteMask[0])
175 nv04->ctrl[0] |= NV04_MULTITEX_TRIANGLE_CONTROL0_STENCIL_WRITE;
176
177 if (ctx->Stencil.Enabled)
178 nv04->ctrl[1] |= NV04_MULTITEX_TRIANGLE_CONTROL1_STENCIL_ENABLE;
179
180 nv04->ctrl[1] |= get_comparison_op(ctx->Stencil.Function[0]) << 4 |
181 _mesa_get_stencil_ref(ctx, 0) << 8 |
182 ctx->Stencil.ValueMask[0] << 16 |
183 ctx->Stencil.WriteMask[0] << 24;
184
185 nv04->ctrl[2] |= get_stencil_op(ctx->Stencil.ZPassFunc[0]) << 8 |
186 get_stencil_op(ctx->Stencil.ZFailFunc[0]) << 4 |
187 get_stencil_op(ctx->Stencil.FailFunc[0]);
188 }
189
190 void
191 nv04_defer_blend(struct gl_context *ctx, int emit)
192 {
193 context_dirty(ctx, BLEND);
194 }
195
196 void
197 nv04_emit_blend(struct gl_context *ctx, int emit)
198 {
199 struct nv04_context *nv04 = to_nv04_context(ctx);
200
201 nv04->blend &= NV04_TEXTURED_TRIANGLE_BLEND_TEXTURE_MAP__MASK;
202 nv04->blend |= NV04_TEXTURED_TRIANGLE_BLEND_MASK_BIT_MSB |
203 NV04_TEXTURED_TRIANGLE_BLEND_TEXTURE_PERSPECTIVE_ENABLE;
204
205 /* Alpha blending. */
206 nv04->blend |= get_blend_func(ctx->Color.Blend[0].DstRGB) << 28 |
207 get_blend_func(ctx->Color.Blend[0].SrcRGB) << 24;
208
209 if (ctx->Color.BlendEnabled)
210 nv04->blend |= NV04_TEXTURED_TRIANGLE_BLEND_BLEND_ENABLE;
211
212 /* Shade model. */
213 if (ctx->Light.ShadeModel == GL_SMOOTH)
214 nv04->blend |= NV04_TEXTURED_TRIANGLE_BLEND_SHADE_MODE_GOURAUD;
215 else
216 nv04->blend |= NV04_TEXTURED_TRIANGLE_BLEND_SHADE_MODE_FLAT;
217
218 /* Secondary color */
219 if (_mesa_need_secondary_color(ctx))
220 nv04->blend |= NV04_TEXTURED_TRIANGLE_BLEND_SPECULAR_ENABLE;
221
222 /* Fog. */
223 if (ctx->Fog.Enabled) {
224 nv04->blend |= NV04_TEXTURED_TRIANGLE_BLEND_FOG_ENABLE;
225 nv04->fog = pack_rgba_f(MESA_FORMAT_B8G8R8A8_UNORM, ctx->Fog.Color);
226 }
227 }