Merge branch 'master' of git+ssh://git.freedesktop.org/git/mesa/mesa
[mesa.git] / src / mesa / drivers / dri / nouveau / nv04_state.c
1 /**************************************************************************
2
3 Copyright 2007 Stephane Marchesin
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_object.h"
29 #include "nouveau_fifo.h"
30 #include "nouveau_reg.h"
31 #include "nouveau_msg.h"
32
33 #include "tnl/t_pipeline.h"
34
35 #include "mtypes.h"
36 #include "colormac.h"
37
38 static uint32_t nv04_compare_func(GLcontext *ctx,GLuint f)
39 {
40 switch ( ctx->Color.AlphaFunc ) {
41 case GL_NEVER: return 1;
42 case GL_LESS: return 2;
43 case GL_EQUAL: return 3;
44 case GL_LEQUAL: return 4;
45 case GL_GREATER: return 5;
46 case GL_NOTEQUAL: return 6;
47 case GL_GEQUAL: return 7;
48 case GL_ALWAYS: return 8;
49 }
50 WARN_ONCE("Unable to find the function\n");
51 return 0;
52 }
53
54 static uint32_t nv04_blend_func(GLcontext *ctx,GLuint f)
55 {
56 switch ( ctx->Color.AlphaFunc ) {
57 case GL_ZERO: return 0x1;
58 case GL_ONE: return 0x2;
59 case GL_SRC_COLOR: return 0x3;
60 case GL_ONE_MINUS_SRC_COLOR: return 0x4;
61 case GL_SRC_ALPHA: return 0x5;
62 case GL_ONE_MINUS_SRC_ALPHA: return 0x6;
63 case GL_DST_ALPHA: return 0x7;
64 case GL_ONE_MINUS_DST_ALPHA: return 0x8;
65 case GL_DST_COLOR: return 0x9;
66 case GL_ONE_MINUS_DST_COLOR: return 0xA;
67 case GL_SRC_ALPHA_SATURATE: return 0xB;
68 }
69 WARN_ONCE("Unable to find the function\n");
70 return 0;
71 }
72
73 static void nv04_emit_control(GLcontext *ctx)
74 {
75 nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
76 uint32_t control,cull;
77 GLubyte alpha_ref;
78
79 CLAMPED_FLOAT_TO_UBYTE(alpha_ref, ctx->Color.AlphaRef);
80 control=alpha_ref;
81 control|=(nv04_compare_func(ctx,ctx->Color.AlphaFunc)<<8);
82 control|=(ctx->Color.AlphaEnabled<<12);
83 control|=(1<<13);
84 control|=(ctx->Depth.Test<<14);
85 control|=(nv04_compare_func(ctx,ctx->Depth.Func)<<16);
86 if ((ctx->Polygon.CullFlag)&&(ctx->Polygon.CullFaceMode!=GL_FRONT_AND_BACK))
87 {
88 if ((ctx->Polygon.FrontFace==GL_CW)&&(ctx->Polygon.CullFaceMode==GL_FRONT))
89 cull=2;
90 if ((ctx->Polygon.FrontFace==GL_CW)&&(ctx->Polygon.CullFaceMode==GL_BACK))
91 cull=3;
92 if ((ctx->Polygon.FrontFace==GL_CCW)&&(ctx->Polygon.CullFaceMode==GL_FRONT))
93 cull=3;
94 if ((ctx->Polygon.FrontFace==GL_CCW)&&(ctx->Polygon.CullFaceMode==GL_BACK))
95 cull=2;
96 }
97 else
98 if (ctx->Polygon.CullFaceMode==GL_FRONT_AND_BACK)
99 cull=0;
100 else
101 cull=1;
102 control|=(cull<<20);
103 control|=(ctx->Color.DitherFlag<<22);
104 if ((ctx->Depth.Test)&&(ctx->Depth.Mask))
105 control|=(1<<24);
106
107 control|=(1<<30); // integer zbuffer format
108
109 BEGIN_RING_CACHE(NvSub3D, NV04_DX5_TEXTURED_TRIANGLE_CONTROL, 1);
110 OUT_RING_CACHE(control);
111 }
112
113 static void nv04_emit_blend(GLcontext *ctx)
114 {
115 nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
116 uint32_t blend;
117
118 blend=0x4; // texture MODULATE_ALPHA
119 blend|=0x20; // alpha is MSB
120 switch(ctx->Light.ShadeModel) {
121 case GL_SMOOTH:blend|=(1<<6);break;
122 case GL_FLAT: blend|=(2<<6);break;
123 default:break;
124 }
125 if (ctx->Hint.PerspectiveCorrection!=GL_FASTEST)
126 blend|=(1<<8);
127 blend|=(ctx->Fog.Enabled<<16);
128 blend|=(ctx->Color.BlendEnabled<<20);
129 blend|=(nv04_blend_func(ctx,ctx->Color.BlendSrcRGB)<<24);
130 blend|=(nv04_blend_func(ctx,ctx->Color.BlendDstRGB)<<28);
131
132 BEGIN_RING_CACHE(NvSub3D, NV04_DX5_TEXTURED_TRIANGLE_BLEND, 1);
133 OUT_RING_CACHE(blend);
134 }
135
136 static void nv04_emit_fog_color(GLcontext *ctx)
137 {
138 nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
139 GLubyte c[4];
140 c[0] = FLOAT_TO_UBYTE( ctx->Fog.Color[0] );
141 c[1] = FLOAT_TO_UBYTE( ctx->Fog.Color[1] );
142 c[2] = FLOAT_TO_UBYTE( ctx->Fog.Color[2] );
143 c[3] = FLOAT_TO_UBYTE( ctx->Fog.Color[3] );
144 BEGIN_RING_CACHE(NvSub3D, NV04_DX5_TEXTURED_TRIANGLE_FOG_COLOR, 1);
145 OUT_RING_CACHE(PACK_COLOR_8888_REV(c[0],c[1],c[2],c[3]));
146 }
147
148 static void nv04AlphaFunc(GLcontext *ctx, GLenum func, GLfloat ref)
149 {
150 nv04_emit_control(ctx);
151 }
152
153 static void nv04BlendColor(GLcontext *ctx, const GLfloat color[4])
154 {
155 nv04_emit_blend(ctx);
156 }
157
158 static void nv04BlendEquationSeparate(GLcontext *ctx, GLenum modeRGB, GLenum modeA)
159 {
160 nv04_emit_blend(ctx);
161 }
162
163
164 static void nv04BlendFuncSeparate(GLcontext *ctx, GLenum sfactorRGB, GLenum dfactorRGB,
165 GLenum sfactorA, GLenum dfactorA)
166 {
167 nv04_emit_blend(ctx);
168 }
169
170 static void nv04Clear(GLcontext *ctx, GLbitfield mask)
171 {
172 /* TODO */
173 }
174
175 static void nv04ClearColor(GLcontext *ctx, const GLfloat color[4])
176 {
177 /* TODO */
178 }
179
180 static void nv04ClearDepth(GLcontext *ctx, GLclampd d)
181 {
182 /* TODO */
183 }
184
185 static void nv04ClearStencil(GLcontext *ctx, GLint s)
186 {
187 /* TODO */
188 }
189
190 static void nv04ClipPlane(GLcontext *ctx, GLenum plane, const GLfloat *equation)
191 {
192 /* TODO */
193 }
194
195 static void nv04ColorMask(GLcontext *ctx, GLboolean rmask, GLboolean gmask,
196 GLboolean bmask, GLboolean amask )
197 {
198 /* TODO */
199 }
200
201 static void nv04ColorMaterial(GLcontext *ctx, GLenum face, GLenum mode)
202 {
203 /* TODO I need love */
204 }
205
206 static void nv04CullFace(GLcontext *ctx, GLenum mode)
207 {
208 nv04_emit_control(ctx);
209 }
210
211 static void nv04FrontFace(GLcontext *ctx, GLenum mode)
212 {
213 /* TODO */
214 }
215
216 static void nv04DepthFunc(GLcontext *ctx, GLenum func)
217 {
218 nv04_emit_control(ctx);
219 }
220
221 static void nv04DepthMask(GLcontext *ctx, GLboolean flag)
222 {
223 /* TODO */
224 }
225
226 static void nv04DepthRange(GLcontext *ctx, GLclampd nearval, GLclampd farval)
227 {
228 /* TODO */
229 }
230
231 /** Specify the current buffer for writing */
232 //void (*DrawBuffer)( GLcontext *ctx, GLenum buffer );
233 /** Specify the buffers for writing for fragment programs*/
234 //void (*DrawBuffers)( GLcontext *ctx, GLsizei n, const GLenum *buffers );
235
236 static void nv04Enable(GLcontext *ctx, GLenum cap, GLboolean state)
237 {
238 switch(cap)
239 {
240 case GL_ALPHA_TEST:
241 nv04_emit_control(ctx);
242 break;
243 // case GL_AUTO_NORMAL:
244 case GL_BLEND:
245 nv04_emit_blend(ctx);
246 break;
247 // case GL_CLIP_PLANE0:
248 // case GL_CLIP_PLANE1:
249 // case GL_CLIP_PLANE2:
250 // case GL_CLIP_PLANE3:
251 // case GL_CLIP_PLANE4:
252 // case GL_CLIP_PLANE5:
253 // case GL_COLOR_LOGIC_OP:
254 // case GL_COLOR_MATERIAL:
255 // case GL_COLOR_SUM_EXT:
256 // case GL_COLOR_TABLE:
257 // case GL_CONVOLUTION_1D:
258 // case GL_CONVOLUTION_2D:
259 case GL_CULL_FACE:
260 nv04_emit_control(ctx);
261 break;
262 case GL_DEPTH_TEST:
263 nv04_emit_control(ctx);
264 break;
265 case GL_DITHER:
266 nv04_emit_control(ctx);
267 break;
268 case GL_FOG:
269 nv04_emit_blend(ctx);
270 break;
271 // case GL_HISTOGRAM:
272 // case GL_INDEX_LOGIC_OP:
273 // case GL_LIGHT0:
274 // case GL_LIGHT1:
275 // case GL_LIGHT2:
276 // case GL_LIGHT3:
277 // case GL_LIGHT4:
278 // case GL_LIGHT5:
279 // case GL_LIGHT6:
280 // case GL_LIGHT7:
281 // case GL_LIGHTING:
282 // case GL_LINE_SMOOTH:
283 // case GL_LINE_STIPPLE:
284 // case GL_MAP1_COLOR_4:
285 // case GL_MAP1_INDEX:
286 // case GL_MAP1_NORMAL:
287 // case GL_MAP1_TEXTURE_COORD_1:
288 // case GL_MAP1_TEXTURE_COORD_2:
289 // case GL_MAP1_TEXTURE_COORD_3:
290 // case GL_MAP1_TEXTURE_COORD_4:
291 // case GL_MAP1_VERTEX_3:
292 // case GL_MAP1_VERTEX_4:
293 // case GL_MAP2_COLOR_4:
294 // case GL_MAP2_INDEX:
295 // case GL_MAP2_NORMAL:
296 // case GL_MAP2_TEXTURE_COORD_1:
297 // case GL_MAP2_TEXTURE_COORD_2:
298 // case GL_MAP2_TEXTURE_COORD_3:
299 // case GL_MAP2_TEXTURE_COORD_4:
300 // case GL_MAP2_VERTEX_3:
301 // case GL_MAP2_VERTEX_4:
302 // case GL_MINMAX:
303 // case GL_NORMALIZE:
304 // case GL_POINT_SMOOTH:
305 // case GL_POLYGON_OFFSET_POINT:
306 // case GL_POLYGON_OFFSET_LINE:
307 // case GL_POLYGON_OFFSET_FILL:
308 // case GL_POLYGON_SMOOTH:
309 // case GL_POLYGON_STIPPLE:
310 // case GL_POST_COLOR_MATRIX_COLOR_TABLE:
311 // case GL_POST_CONVOLUTION_COLOR_TABLE:
312 // case GL_RESCALE_NORMAL:
313 // case GL_SCISSOR_TEST:
314 // case GL_SEPARABLE_2D:
315 // case GL_STENCIL_TEST:
316 // case GL_TEXTURE_GEN_Q:
317 // case GL_TEXTURE_GEN_R:
318 // case GL_TEXTURE_GEN_S:
319 // case GL_TEXTURE_GEN_T:
320 // case GL_TEXTURE_1D:
321 // case GL_TEXTURE_2D:
322 // case GL_TEXTURE_3D:
323 }
324 }
325
326 static void nv04Fogfv(GLcontext *ctx, GLenum pname, const GLfloat *params)
327 {
328 nv04_emit_blend(ctx);
329 nv04_emit_fog_color(ctx);
330 }
331
332 static void nv04Hint(GLcontext *ctx, GLenum target, GLenum mode)
333 {
334 switch(target)
335 {
336 case GL_PERSPECTIVE_CORRECTION_HINT:nv04_emit_blend(ctx);break;
337 default:break;
338 }
339 }
340
341 static void nv04LineStipple(GLcontext *ctx, GLint factor, GLushort pattern )
342 {
343 /* TODO not even in your dreams */
344 }
345
346 static void nv04LineWidth(GLcontext *ctx, GLfloat width)
347 {
348 /* TODO */
349 }
350
351 static void nv04LogicOpcode(GLcontext *ctx, GLenum opcode)
352 {
353 /* TODO */
354 }
355
356 static void nv04PointParameterfv(GLcontext *ctx, GLenum pname, const GLfloat *params)
357 {
358 /* TODO */
359 }
360
361 static void nv04PointSize(GLcontext *ctx, GLfloat size)
362 {
363 /* TODO */
364 }
365
366 static void nv04PolygonMode(GLcontext *ctx, GLenum face, GLenum mode)
367 {
368 /* TODO */
369 }
370
371 /** Set the scale and units used to calculate depth values */
372 static void nv04PolygonOffset(GLcontext *ctx, GLfloat factor, GLfloat units)
373 {
374 /* TODO */
375 }
376
377 /** Set the polygon stippling pattern */
378 static void nv04PolygonStipple(GLcontext *ctx, const GLubyte *mask )
379 {
380 /* TODO */
381 }
382
383 /* Specifies the current buffer for reading */
384 void (*ReadBuffer)( GLcontext *ctx, GLenum buffer );
385 /** Set rasterization mode */
386 void (*RenderMode)(GLcontext *ctx, GLenum mode );
387
388 /** Define the scissor box */
389 static void nv04Scissor(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
390 {
391 /* TODO */
392 }
393
394 /** Select flat or smooth shading */
395 static void nv04ShadeModel(GLcontext *ctx, GLenum mode)
396 {
397 nv04_emit_blend(ctx);
398 }
399
400 /** OpenGL 2.0 two-sided StencilFunc */
401 static void nv04StencilFuncSeparate(GLcontext *ctx, GLenum face, GLenum func,
402 GLint ref, GLuint mask)
403 {
404 /* TODO */
405 }
406
407 /** OpenGL 2.0 two-sided StencilMask */
408 static void nv04StencilMaskSeparate(GLcontext *ctx, GLenum face, GLuint mask)
409 {
410 /* TODO */
411 }
412
413 /** OpenGL 2.0 two-sided StencilOp */
414 static void nv04StencilOpSeparate(GLcontext *ctx, GLenum face, GLenum fail,
415 GLenum zfail, GLenum zpass)
416 {
417 /* TODO */
418 }
419
420 /** Control the generation of texture coordinates */
421 void (*TexGen)(GLcontext *ctx, GLenum coord, GLenum pname,
422 const GLfloat *params);
423 /** Set texture environment parameters */
424 void (*TexEnv)(GLcontext *ctx, GLenum target, GLenum pname,
425 const GLfloat *param);
426 /** Set texture parameters */
427 void (*TexParameter)(GLcontext *ctx, GLenum target,
428 struct gl_texture_object *texObj,
429 GLenum pname, const GLfloat *params);
430
431 /* Update anything that depends on the window position/size */
432 static void nv04WindowMoved(nouveauContextPtr nmesa)
433 {
434 }
435
436 /* Initialise any card-specific non-GL related state */
437 static GLboolean nv04InitCard(nouveauContextPtr nmesa)
438 {
439 return GL_TRUE;
440 }
441
442 /* Update buffer offset/pitch/format */
443 static GLboolean nv04BindBuffers(nouveauContextPtr nmesa, int num_color,
444 nouveau_renderbuffer **color,
445 nouveau_renderbuffer *depth)
446 {
447 return GL_TRUE;
448 }
449
450 void nv04InitStateFuncs(GLcontext *ctx, struct dd_function_table *func)
451 {
452 nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
453
454 func->AlphaFunc = nv04AlphaFunc;
455 func->BlendColor = nv04BlendColor;
456 func->BlendEquationSeparate = nv04BlendEquationSeparate;
457 func->BlendFuncSeparate = nv04BlendFuncSeparate;
458 func->Clear = nv04Clear;
459 func->ClearColor = nv04ClearColor;
460 func->ClearDepth = nv04ClearDepth;
461 func->ClearStencil = nv04ClearStencil;
462 func->ClipPlane = nv04ClipPlane;
463 func->ColorMask = nv04ColorMask;
464 func->ColorMaterial = nv04ColorMaterial;
465 func->CullFace = nv04CullFace;
466 func->FrontFace = nv04FrontFace;
467 func->DepthFunc = nv04DepthFunc;
468 func->DepthMask = nv04DepthMask;
469 func->DepthRange = nv04DepthRange;
470 func->Enable = nv04Enable;
471 func->Fogfv = nv04Fogfv;
472 func->Hint = nv04Hint;
473 /* func->Lightfv = nv04Lightfv;*/
474 /* func->LightModelfv = nv04LightModelfv; */
475 func->LineStipple = nv04LineStipple; /* Not for NV04 */
476 func->LineWidth = nv04LineWidth;
477 func->LogicOpcode = nv04LogicOpcode;
478 func->PointParameterfv = nv04PointParameterfv;
479 func->PointSize = nv04PointSize;
480 func->PolygonMode = nv04PolygonMode;
481 func->PolygonOffset = nv04PolygonOffset;
482 func->PolygonStipple = nv04PolygonStipple; /* Not for NV04 */
483 /* func->ReadBuffer = nv04ReadBuffer;*/
484 /* func->RenderMode = nv04RenderMode;*/
485 func->Scissor = nv04Scissor;
486 func->ShadeModel = nv04ShadeModel;
487 func->StencilFuncSeparate = nv04StencilFuncSeparate;
488 func->StencilMaskSeparate = nv04StencilMaskSeparate;
489 func->StencilOpSeparate = nv04StencilOpSeparate;
490 /* func->TexGen = nv04TexGen;*/
491 /* func->TexParameter = nv04TexParameter;*/
492 /* func->TextureMatrix = nv04TextureMatrix;*/
493
494 nmesa->hw_func.InitCard = nv04InitCard;
495 nmesa->hw_func.BindBuffers = nv04BindBuffers;
496 nmesa->hw_func.WindowMoved = nv04WindowMoved;
497 }