r300_fragprog: Use nqssa+dce and program_pair for emit
[mesa.git] / src / mesa / drivers / dri / nouveau / nouveau_state.c
1 /**************************************************************************
2
3 Copyright 2006 Jeremy Kolb
4 All Rights Reserved.
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 on the rights to use, copy, modify, merge, publish, distribute, sub
10 license, and/or sell copies of the Software, and to permit persons to whom
11 the Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice (including the next
14 paragraph) shall be included in all copies or substantial portions of the
15 Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 ERIC ANHOLT OR SILICON INTEGRATED SYSTEMS CORP BE LIABLE FOR ANY CLAIM,
21 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23 USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25 **************************************************************************/
26
27 #include "nouveau_context.h"
28 #include "nouveau_state.h"
29 #include "nouveau_swtcl.h"
30 #include "nouveau_fifo.h"
31
32 #include "swrast/swrast.h"
33 #include "tnl/tnl.h"
34 #include "swrast_setup/swrast_setup.h"
35
36 #include "tnl/t_pipeline.h"
37
38 #include "mtypes.h"
39 #include "colormac.h"
40
41 static INLINE GLuint nouveauPackColor(GLuint format,
42 GLubyte r, GLubyte g,
43 GLubyte b, GLubyte a)
44 {
45 switch (format) {
46 case 2:
47 return PACK_COLOR_565( r, g, b );
48 case 4:
49 return PACK_COLOR_8888( r, g, b, a);
50 default:
51 fprintf(stderr, "unknown format %d\n", (int)format);
52 return 0;
53 }
54 }
55
56 static void nouveauCalcViewport(GLcontext *ctx)
57 {
58 /* Calculate the Viewport Matrix */
59
60 nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
61 const GLfloat *v = ctx->Viewport._WindowMap.m;
62 GLfloat *m = nmesa->viewport.m;
63 GLfloat xoffset = nmesa->drawX, yoffset = nmesa->drawY + nmesa->drawH;
64
65 nmesa->depth_scale = 1.0 / ctx->DrawBuffer->_DepthMaxF;
66
67 m[MAT_SX] = v[MAT_SX];
68 m[MAT_TX] = v[MAT_TX] + xoffset + SUBPIXEL_X;
69 m[MAT_SY] = - v[MAT_SY];
70 m[MAT_TY] = (-v[MAT_TY]) + yoffset + SUBPIXEL_Y;
71 m[MAT_SZ] = v[MAT_SZ] * nmesa->depth_scale;
72 m[MAT_TZ] = v[MAT_TZ] * nmesa->depth_scale;
73
74 nmesa->hw_func.WindowMoved(nmesa);
75 }
76
77 static void nouveauViewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
78 {
79 /*
80 * Need to send (at least on an nv35 the following:
81 * cons = 4 (this may be bytes per pixel)
82 *
83 * The viewport:
84 * 445 0x0000bee0 {size: 0x0 channel: 0x1 cmd: 0x00009ee0} <-- VIEWPORT_SETUP/HEADER ?
85 * 446 0x00000000 {size: 0x0 channel: 0x0 cmd: 0x00000000} <-- x * cons
86 * 447 0x00000c80 {size: 0x0 channel: 0x0 cmd: 0x00000c80} <-- (height + x) * cons
87 * 448 0x00000000 {size: 0x0 channel: 0x0 cmd: 0x00000000} <-- y * cons
88 * 449 0x00000960 {size: 0x0 channel: 0x0 cmd: 0x00000960} <-- (width + y) * cons
89 * 44a 0x00082a00 {size: 0x2 channel: 0x1 cmd: 0x00000a00} <-- VIEWPORT_DIMS
90 * 44b 0x04000000 <-- (Width_from_glViewport << 16) | x
91 * 44c 0x03000000 <-- (Height_from_glViewport << 16) | (win_height - height - y)
92 *
93 */
94
95 nouveauCalcViewport(ctx);
96 }
97
98 static void nouveauDepthRange(GLcontext *ctx, GLclampd near, GLclampd far)
99 {
100 nouveauCalcViewport(ctx);
101 }
102
103 static void nouveauUpdateProjectionMatrix(GLcontext *ctx)
104 {
105 }
106
107 static void nouveauUpdateModelviewMatrix(GLcontext *ctx)
108 {
109 }
110
111 static void nouveauDDUpdateHWState(GLcontext *ctx)
112 {
113 nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
114 int new_state = nmesa->new_state;
115
116 if ( new_state || nmesa->new_render_state & _NEW_TEXTURE )
117 {
118 nmesa->new_state = 0;
119
120 /* Update the various parts of the context's state.
121 */
122 /*
123 if ( new_state & NOUVEAU_NEW_ALPHA )
124 nouveauUpdateAlphaMode( ctx );
125
126 if ( new_state & NOUVEAU_NEW_DEPTH )
127 nouveauUpdateZMode( ctx );
128
129 if ( new_state & NOUVEAU_NEW_FOG )
130 nouveauUpdateFogAttrib( ctx );
131
132 if ( new_state & NOUVEAU_NEW_CLIP )
133 nouveauUpdateClipping( ctx );
134
135 if ( new_state & NOUVEAU_NEW_CULL )
136 nouveauUpdateCull( ctx );
137
138 if ( new_state & NOUVEAU_NEW_MASKS )
139 nouveauUpdateMasks( ctx );
140
141 if ( new_state & NOUVEAU_NEW_WINDOW )
142 nouveauUpdateWindow( ctx );
143
144 if ( nmesa->new_render_state & _NEW_TEXTURE ) {
145 nouveauUpdateTextureState( ctx );
146 }*/
147 }
148 }
149
150 static void nouveauDDInvalidateState(GLcontext *ctx, GLuint new_state)
151 {
152 nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
153
154 if ( new_state & _NEW_PROJECTION ) {
155 nmesa->hw_func.UpdateProjectionMatrix(ctx);
156 }
157 if ( new_state & _NEW_MODELVIEW ) {
158 nmesa->hw_func.UpdateModelviewMatrix(ctx);
159 }
160
161 _swrast_InvalidateState( ctx, new_state );
162 _swsetup_InvalidateState( ctx, new_state );
163 _vbo_InvalidateState( ctx, new_state );
164 _tnl_InvalidateState( ctx, new_state );
165 NOUVEAU_CONTEXT(ctx)->new_render_state |= new_state;
166 }
167
168 /* Initialize the context's hardware state. */
169 void nouveauDDInitState(nouveauContextPtr nmesa)
170 {
171 uint32_t type = nmesa->screen->card->type;
172 switch(type)
173 {
174 case NV_04:
175 case NV_05:
176 nv04InitStateFuncs(nmesa->glCtx, &nmesa->glCtx->Driver);
177 break;
178 case NV_10:
179 case NV_11:
180 case NV_17:
181 nv10InitStateFuncs(nmesa->glCtx, &nmesa->glCtx->Driver);
182 break;
183 case NV_20:
184 nv20InitStateFuncs(nmesa->glCtx, &nmesa->glCtx->Driver);
185 break;
186 case NV_30:
187 case NV_40:
188 case NV_44:
189 nv30InitStateFuncs(nmesa->glCtx, &nmesa->glCtx->Driver);
190 break;
191 case NV_50:
192 nv50InitStateFuncs(nmesa->glCtx, &nmesa->glCtx->Driver);
193 break;
194 default:
195 break;
196 }
197 nouveau_state_cache_init(nmesa);
198 }
199
200 /* Initialize the driver's state functions */
201 void nouveauDDInitStateFuncs(GLcontext *ctx)
202 {
203 nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
204
205 ctx->Driver.UpdateState = nouveauDDInvalidateState;
206
207 ctx->Driver.ClearIndex = NULL;
208 ctx->Driver.ClearColor = NULL; //nouveauDDClearColor;
209 ctx->Driver.ClearStencil = NULL; //nouveauDDClearStencil;
210 ctx->Driver.DrawBuffer = NULL; //nouveauDDDrawBuffer;
211 ctx->Driver.ReadBuffer = NULL; //nouveauDDReadBuffer;
212
213 ctx->Driver.IndexMask = NULL;
214 ctx->Driver.ColorMask = NULL; //nouveauDDColorMask;
215 ctx->Driver.AlphaFunc = NULL; //nouveauDDAlphaFunc;
216 ctx->Driver.BlendEquationSeparate = NULL; //nouveauDDBlendEquationSeparate;
217 ctx->Driver.BlendFuncSeparate = NULL; //nouveauDDBlendFuncSeparate;
218 ctx->Driver.ClearDepth = NULL; //nouveauDDClearDepth;
219 ctx->Driver.CullFace = NULL; //nouveauDDCullFace;
220 ctx->Driver.FrontFace = NULL; //nouveauDDFrontFace;
221 ctx->Driver.DepthFunc = NULL; //nouveauDDDepthFunc;
222 ctx->Driver.DepthMask = NULL; //nouveauDDDepthMask;
223 ctx->Driver.Enable = NULL; //nouveauDDEnable;
224 ctx->Driver.Fogfv = NULL; //nouveauDDFogfv;
225 ctx->Driver.Hint = NULL;
226 ctx->Driver.Lightfv = NULL;
227 ctx->Driver.LightModelfv = NULL; //nouveauDDLightModelfv;
228 ctx->Driver.LogicOpcode = NULL; //nouveauDDLogicOpCode;
229 ctx->Driver.PolygonMode = NULL;
230 ctx->Driver.PolygonStipple = NULL; //nouveauDDPolygonStipple;
231 ctx->Driver.RenderMode = NULL; //nouveauDDRenderMode;
232 ctx->Driver.Scissor = NULL; //nouveauDDScissor;
233 ctx->Driver.ShadeModel = NULL; //nouveauDDShadeModel;
234 ctx->Driver.StencilFuncSeparate = NULL; //nouveauDDStencilFuncSeparate;
235 ctx->Driver.StencilMaskSeparate = NULL; //nouveauDDStencilMaskSeparate;
236 ctx->Driver.StencilOpSeparate = NULL; //nouveauDDStencilOpSeparate;
237
238 ctx->Driver.DepthRange = nouveauDepthRange;
239 ctx->Driver.Viewport = nouveauViewport;
240
241 /* Pixel path fallbacks.
242 */
243 ctx->Driver.Accum = _swrast_Accum;
244 ctx->Driver.Bitmap = _swrast_Bitmap;
245 ctx->Driver.CopyPixels = _swrast_CopyPixels;
246 ctx->Driver.DrawPixels = _swrast_DrawPixels;
247 ctx->Driver.ReadPixels = _swrast_ReadPixels;
248
249 /* Swrast hooks for imaging extensions:
250 */
251 ctx->Driver.CopyColorTable = _swrast_CopyColorTable;
252 ctx->Driver.CopyColorSubTable = _swrast_CopyColorSubTable;
253 ctx->Driver.CopyConvolutionFilter1D = _swrast_CopyConvolutionFilter1D;
254 ctx->Driver.CopyConvolutionFilter2D = _swrast_CopyConvolutionFilter2D;
255
256 /* Matrix updates */
257 nmesa->hw_func.UpdateProjectionMatrix = nouveauUpdateProjectionMatrix;
258 nmesa->hw_func.UpdateModelviewMatrix = nouveauUpdateModelviewMatrix;
259 }
260
261 #define STATE_INIT(a) if (ctx->Driver.a) ctx->Driver.a
262
263 void nouveauInitState(GLcontext *ctx)
264 {
265 /*
266 * Mesa should do this for us:
267 */
268
269 STATE_INIT(AlphaFunc)( ctx,
270 ctx->Color.AlphaFunc,
271 ctx->Color.AlphaRef);
272
273 STATE_INIT(BlendColor)( ctx,
274 ctx->Color.BlendColor );
275
276 STATE_INIT(BlendEquationSeparate)( ctx,
277 ctx->Color.BlendEquationRGB,
278 ctx->Color.BlendEquationA);
279
280 STATE_INIT(BlendFuncSeparate)( ctx,
281 ctx->Color.BlendSrcRGB,
282 ctx->Color.BlendDstRGB,
283 ctx->Color.BlendSrcA,
284 ctx->Color.BlendDstA);
285
286 STATE_INIT(ClearColor)( ctx, ctx->Color.ClearColor);
287 STATE_INIT(ClearDepth)( ctx, ctx->Depth.Clear);
288 STATE_INIT(ClearStencil)( ctx, ctx->Stencil.Clear);
289
290 STATE_INIT(ColorMask)( ctx,
291 ctx->Color.ColorMask[RCOMP],
292 ctx->Color.ColorMask[GCOMP],
293 ctx->Color.ColorMask[BCOMP],
294 ctx->Color.ColorMask[ACOMP]);
295
296 STATE_INIT(CullFace)( ctx, ctx->Polygon.CullFaceMode );
297 STATE_INIT(DepthFunc)( ctx, ctx->Depth.Func );
298 STATE_INIT(DepthMask)( ctx, ctx->Depth.Mask );
299 STATE_INIT(DepthRange)( ctx, ctx->Viewport.Near, ctx->Viewport.Far );
300
301 STATE_INIT(Enable)( ctx, GL_ALPHA_TEST, ctx->Color.AlphaEnabled );
302 STATE_INIT(Enable)( ctx, GL_BLEND, ctx->Color.BlendEnabled );
303 STATE_INIT(Enable)( ctx, GL_COLOR_LOGIC_OP, ctx->Color.ColorLogicOpEnabled );
304 STATE_INIT(Enable)( ctx, GL_COLOR_SUM, ctx->Fog.ColorSumEnabled );
305 STATE_INIT(Enable)( ctx, GL_CULL_FACE, ctx->Polygon.CullFlag );
306 STATE_INIT(Enable)( ctx, GL_DEPTH_TEST, ctx->Depth.Test );
307 STATE_INIT(Enable)( ctx, GL_DITHER, ctx->Color.DitherFlag );
308 STATE_INIT(Enable)( ctx, GL_FOG, ctx->Fog.Enabled );
309 STATE_INIT(Enable)( ctx, GL_LIGHTING, ctx->Light.Enabled );
310 STATE_INIT(Enable)( ctx, GL_LINE_SMOOTH, ctx->Line.SmoothFlag );
311 STATE_INIT(Enable)( ctx, GL_LINE_STIPPLE, ctx->Line.StippleFlag );
312 STATE_INIT(Enable)( ctx, GL_POINT_SMOOTH, ctx->Point.SmoothFlag );
313 STATE_INIT(Enable)( ctx, GL_POLYGON_OFFSET_FILL, ctx->Polygon.OffsetFill);
314 STATE_INIT(Enable)( ctx, GL_POLYGON_OFFSET_LINE, ctx->Polygon.OffsetLine);
315 STATE_INIT(Enable)( ctx, GL_POLYGON_OFFSET_POINT, ctx->Polygon.OffsetPoint);
316 STATE_INIT(Enable)( ctx, GL_POLYGON_SMOOTH, ctx->Polygon.SmoothFlag );
317 STATE_INIT(Enable)( ctx, GL_POLYGON_STIPPLE, ctx->Polygon.StippleFlag );
318 STATE_INIT(Enable)( ctx, GL_SCISSOR_TEST, ctx->Scissor.Enabled );
319 STATE_INIT(Enable)( ctx, GL_STENCIL_TEST, ctx->Stencil.Enabled );
320 STATE_INIT(Enable)( ctx, GL_TEXTURE_1D, GL_FALSE );
321 STATE_INIT(Enable)( ctx, GL_TEXTURE_2D, GL_FALSE );
322 STATE_INIT(Enable)( ctx, GL_TEXTURE_RECTANGLE_NV, GL_FALSE );
323 STATE_INIT(Enable)( ctx, GL_TEXTURE_3D, GL_FALSE );
324 STATE_INIT(Enable)( ctx, GL_TEXTURE_CUBE_MAP, GL_FALSE );
325
326 STATE_INIT(Fogfv)( ctx, GL_FOG_COLOR, ctx->Fog.Color );
327 STATE_INIT(Fogfv)( ctx, GL_FOG_MODE, 0 );
328 STATE_INIT(Fogfv)( ctx, GL_FOG_DENSITY, &ctx->Fog.Density );
329 STATE_INIT(Fogfv)( ctx, GL_FOG_START, &ctx->Fog.Start );
330 STATE_INIT(Fogfv)( ctx, GL_FOG_END, &ctx->Fog.End );
331
332 STATE_INIT(FrontFace)( ctx, ctx->Polygon.FrontFace );
333
334 {
335 GLfloat f = (GLfloat)ctx->Light.Model.ColorControl;
336 STATE_INIT(LightModelfv)( ctx, GL_LIGHT_MODEL_COLOR_CONTROL, &f );
337 }
338
339 STATE_INIT(LineStipple)( ctx, ctx->Line.StippleFactor, ctx->Line.StipplePattern );
340 STATE_INIT(LineWidth)( ctx, ctx->Line.Width );
341 STATE_INIT(LogicOpcode)( ctx, ctx->Color.LogicOp );
342 STATE_INIT(PointSize)( ctx, ctx->Point.Size );
343 STATE_INIT(PolygonMode)( ctx, GL_FRONT, ctx->Polygon.FrontMode );
344 STATE_INIT(PolygonMode)( ctx, GL_BACK, ctx->Polygon.BackMode );
345 STATE_INIT(PolygonOffset)( ctx,
346 ctx->Polygon.OffsetFactor,
347 ctx->Polygon.OffsetUnits );
348 STATE_INIT(PolygonStipple)( ctx, (const GLubyte *)ctx->PolygonStipple );
349 STATE_INIT(ShadeModel)( ctx, ctx->Light.ShadeModel );
350 STATE_INIT(StencilFuncSeparate)( ctx, GL_FRONT,
351 ctx->Stencil.Function[0],
352 ctx->Stencil.Ref[0],
353 ctx->Stencil.ValueMask[0] );
354 STATE_INIT(StencilFuncSeparate)( ctx, GL_BACK,
355 ctx->Stencil.Function[1],
356 ctx->Stencil.Ref[1],
357 ctx->Stencil.ValueMask[1] );
358 STATE_INIT(StencilMaskSeparate)( ctx, GL_FRONT, ctx->Stencil.WriteMask[0] );
359 STATE_INIT(StencilMaskSeparate)( ctx, GL_BACK, ctx->Stencil.WriteMask[1] );
360 STATE_INIT(StencilOpSeparate)( ctx, GL_FRONT,
361 ctx->Stencil.FailFunc[0],
362 ctx->Stencil.ZFailFunc[0],
363 ctx->Stencil.ZPassFunc[0]);
364 STATE_INIT(StencilOpSeparate)( ctx, GL_BACK,
365 ctx->Stencil.FailFunc[1],
366 ctx->Stencil.ZFailFunc[1],
367 ctx->Stencil.ZPassFunc[1]);
368 }