r300_fragprog: Use nqssa+dce and program_pair for emit
[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(GLuint f)
39 {
40 switch ( f ) {
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(GLuint f)
55 {
56 switch ( f ) {
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 0x%x\n",f);
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->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->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->Color.BlendSrcRGB)<<24);
130 blend|=(nv04_blend_func(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 nv04_emit_fog_color(ctx);
271 break;
272 // case GL_HISTOGRAM:
273 // case GL_INDEX_LOGIC_OP:
274 // case GL_LIGHT0:
275 // case GL_LIGHT1:
276 // case GL_LIGHT2:
277 // case GL_LIGHT3:
278 // case GL_LIGHT4:
279 // case GL_LIGHT5:
280 // case GL_LIGHT6:
281 // case GL_LIGHT7:
282 // case GL_LIGHTING:
283 // case GL_LINE_SMOOTH:
284 // case GL_LINE_STIPPLE:
285 // case GL_MAP1_COLOR_4:
286 // case GL_MAP1_INDEX:
287 // case GL_MAP1_NORMAL:
288 // case GL_MAP1_TEXTURE_COORD_1:
289 // case GL_MAP1_TEXTURE_COORD_2:
290 // case GL_MAP1_TEXTURE_COORD_3:
291 // case GL_MAP1_TEXTURE_COORD_4:
292 // case GL_MAP1_VERTEX_3:
293 // case GL_MAP1_VERTEX_4:
294 // case GL_MAP2_COLOR_4:
295 // case GL_MAP2_INDEX:
296 // case GL_MAP2_NORMAL:
297 // case GL_MAP2_TEXTURE_COORD_1:
298 // case GL_MAP2_TEXTURE_COORD_2:
299 // case GL_MAP2_TEXTURE_COORD_3:
300 // case GL_MAP2_TEXTURE_COORD_4:
301 // case GL_MAP2_VERTEX_3:
302 // case GL_MAP2_VERTEX_4:
303 // case GL_MINMAX:
304 // case GL_NORMALIZE:
305 // case GL_POINT_SMOOTH:
306 // case GL_POLYGON_OFFSET_POINT:
307 // case GL_POLYGON_OFFSET_LINE:
308 // case GL_POLYGON_OFFSET_FILL:
309 // case GL_POLYGON_SMOOTH:
310 // case GL_POLYGON_STIPPLE:
311 // case GL_POST_COLOR_MATRIX_COLOR_TABLE:
312 // case GL_POST_CONVOLUTION_COLOR_TABLE:
313 // case GL_RESCALE_NORMAL:
314 // case GL_SCISSOR_TEST:
315 // case GL_SEPARABLE_2D:
316 // case GL_STENCIL_TEST:
317 // case GL_TEXTURE_GEN_Q:
318 // case GL_TEXTURE_GEN_R:
319 // case GL_TEXTURE_GEN_S:
320 // case GL_TEXTURE_GEN_T:
321 // case GL_TEXTURE_1D:
322 // case GL_TEXTURE_2D:
323 // case GL_TEXTURE_3D:
324 }
325 }
326
327 static void nv04Fogfv(GLcontext *ctx, GLenum pname, const GLfloat *params)
328 {
329 nv04_emit_blend(ctx);
330 nv04_emit_fog_color(ctx);
331 }
332
333 static void nv04Hint(GLcontext *ctx, GLenum target, GLenum mode)
334 {
335 switch(target)
336 {
337 case GL_PERSPECTIVE_CORRECTION_HINT:nv04_emit_blend(ctx);break;
338 default:break;
339 }
340 }
341
342 static void nv04LineStipple(GLcontext *ctx, GLint factor, GLushort pattern )
343 {
344 /* TODO not even in your dreams */
345 }
346
347 static void nv04LineWidth(GLcontext *ctx, GLfloat width)
348 {
349 /* TODO */
350 }
351
352 static void nv04LogicOpcode(GLcontext *ctx, GLenum opcode)
353 {
354 /* TODO */
355 }
356
357 static void nv04PointParameterfv(GLcontext *ctx, GLenum pname, const GLfloat *params)
358 {
359 /* TODO */
360 }
361
362 static void nv04PointSize(GLcontext *ctx, GLfloat size)
363 {
364 /* TODO */
365 }
366
367 static void nv04PolygonMode(GLcontext *ctx, GLenum face, GLenum mode)
368 {
369 /* TODO */
370 }
371
372 /** Set the scale and units used to calculate depth values */
373 static void nv04PolygonOffset(GLcontext *ctx, GLfloat factor, GLfloat units)
374 {
375 /* TODO */
376 }
377
378 /** Set the polygon stippling pattern */
379 static void nv04PolygonStipple(GLcontext *ctx, const GLubyte *mask )
380 {
381 /* TODO */
382 }
383
384 /* Specifies the current buffer for reading */
385 void (*ReadBuffer)( GLcontext *ctx, GLenum buffer );
386 /** Set rasterization mode */
387 void (*RenderMode)(GLcontext *ctx, GLenum mode );
388
389 /** Define the scissor box */
390 static void nv04Scissor(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
391 {
392 /* TODO */
393 }
394
395 /** Select flat or smooth shading */
396 static void nv04ShadeModel(GLcontext *ctx, GLenum mode)
397 {
398 nv04_emit_blend(ctx);
399 }
400
401 /** OpenGL 2.0 two-sided StencilFunc */
402 static void nv04StencilFuncSeparate(GLcontext *ctx, GLenum face, GLenum func,
403 GLint ref, GLuint mask)
404 {
405 /* TODO */
406 }
407
408 /** OpenGL 2.0 two-sided StencilMask */
409 static void nv04StencilMaskSeparate(GLcontext *ctx, GLenum face, GLuint mask)
410 {
411 /* TODO */
412 }
413
414 /** OpenGL 2.0 two-sided StencilOp */
415 static void nv04StencilOpSeparate(GLcontext *ctx, GLenum face, GLenum fail,
416 GLenum zfail, GLenum zpass)
417 {
418 /* TODO */
419 }
420
421 /** Control the generation of texture coordinates */
422 void (*TexGen)(GLcontext *ctx, GLenum coord, GLenum pname,
423 const GLfloat *params);
424 /** Set texture environment parameters */
425 void (*TexEnv)(GLcontext *ctx, GLenum target, GLenum pname,
426 const GLfloat *param);
427 /** Set texture parameters */
428 void (*TexParameter)(GLcontext *ctx, GLenum target,
429 struct gl_texture_object *texObj,
430 GLenum pname, const GLfloat *params);
431
432 /* Update anything that depends on the window position/size */
433 static void nv04WindowMoved(nouveauContextPtr nmesa)
434 {
435 }
436
437 /* Initialise any card-specific non-GL related state */
438 static GLboolean nv04InitCard(nouveauContextPtr nmesa)
439 {
440 nouveauObjectOnSubchannel(nmesa, NvSub3D, Nv3D);
441 nouveauObjectOnSubchannel(nmesa, NvSubCtxSurf3D, NvCtxSurf3D);
442
443 BEGIN_RING_SIZE(NvSubCtxSurf3D, NV04_CONTEXT_SURFACES_3D_DMA_NOTIFY, 3);
444 OUT_RING(NvDmaFB);
445 OUT_RING(NvDmaFB);
446 OUT_RING(NvDmaFB);
447 BEGIN_RING_SIZE(NvSub3D, NV04_DX5_TEXTURED_TRIANGLE_SURFACE, 1);
448 OUT_RING(NvCtxSurf3D);
449 return GL_TRUE;
450 }
451
452 /* Update buffer offset/pitch/format */
453 static GLboolean nv04BindBuffers(nouveauContextPtr nmesa, int num_color,
454 nouveau_renderbuffer_t **color,
455 nouveau_renderbuffer_t *depth)
456 {
457 GLuint x, y, w, h;
458 uint32_t depth_pitch=(depth?depth->pitch:0+15)&~15+16;
459 if (depth_pitch<256) depth_pitch=256;
460
461 w = color[0]->mesa.Width;
462 h = color[0]->mesa.Height;
463 x = nmesa->drawX;
464 y = nmesa->drawY;
465
466 BEGIN_RING_SIZE(NvSubCtxSurf3D, NV04_CONTEXT_SURFACES_3D_FORMAT, 1);
467 if (color[0]->mesa._ActualFormat == GL_RGBA8)
468 OUT_RING(0x108/*A8R8G8B8*/);
469 else
470 OUT_RING(0x103/*R5G6B5*/);
471
472 /* FIXME pitches have to be aligned ! */
473 BEGIN_RING_SIZE(NvSubCtxSurf3D, NV04_CONTEXT_SURFACES_3D_PITCH, 2);
474 OUT_RING(color[0]->pitch|(depth_pitch<<16));
475 OUT_RING(color[0]->offset);
476 if (depth) {
477 BEGIN_RING_SIZE(NvSubCtxSurf3D, NV04_CONTEXT_SURFACES_3D_OFFSET_ZETA, 1);
478 OUT_RING(depth->offset);
479 }
480
481 // BEGIN_RING_SIZE(NvSubCtxSurf3D, NV04_CONTEXT_SURFACES_3D_CLIP_HORIZONTAL, 2);
482 // OUT_RING((w<<16)|x);
483 // OUT_RING((h<<16)|y);
484
485
486 /* FIXME not sure... */
487 /* BEGIN_RING_SIZE(NvSubCtxSurf3D, NV04_CONTEXT_SURFACES_3D_CLIP_SIZE, 1);
488 OUT_RING((h<<16)|w);*/
489
490 return GL_TRUE;
491 }
492
493 void nv04InitStateFuncs(GLcontext *ctx, struct dd_function_table *func)
494 {
495 nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
496
497 func->AlphaFunc = nv04AlphaFunc;
498 func->BlendColor = nv04BlendColor;
499 func->BlendEquationSeparate = nv04BlendEquationSeparate;
500 func->BlendFuncSeparate = nv04BlendFuncSeparate;
501 func->Clear = nv04Clear;
502 func->ClearColor = nv04ClearColor;
503 func->ClearDepth = nv04ClearDepth;
504 func->ClearStencil = nv04ClearStencil;
505 func->ClipPlane = nv04ClipPlane;
506 func->ColorMask = nv04ColorMask;
507 func->ColorMaterial = nv04ColorMaterial;
508 func->CullFace = nv04CullFace;
509 func->FrontFace = nv04FrontFace;
510 func->DepthFunc = nv04DepthFunc;
511 func->DepthMask = nv04DepthMask;
512 func->DepthRange = nv04DepthRange;
513 func->Enable = nv04Enable;
514 func->Fogfv = nv04Fogfv;
515 func->Hint = nv04Hint;
516 /* func->Lightfv = nv04Lightfv;*/
517 /* func->LightModelfv = nv04LightModelfv; */
518 func->LineStipple = nv04LineStipple; /* Not for NV04 */
519 func->LineWidth = nv04LineWidth;
520 func->LogicOpcode = nv04LogicOpcode;
521 func->PointParameterfv = nv04PointParameterfv;
522 func->PointSize = nv04PointSize;
523 func->PolygonMode = nv04PolygonMode;
524 func->PolygonOffset = nv04PolygonOffset;
525 func->PolygonStipple = nv04PolygonStipple; /* Not for NV04 */
526 /* func->ReadBuffer = nv04ReadBuffer;*/
527 /* func->RenderMode = nv04RenderMode;*/
528 func->Scissor = nv04Scissor;
529 func->ShadeModel = nv04ShadeModel;
530 func->StencilFuncSeparate = nv04StencilFuncSeparate;
531 func->StencilMaskSeparate = nv04StencilMaskSeparate;
532 func->StencilOpSeparate = nv04StencilOpSeparate;
533 /* func->TexGen = nv04TexGen;*/
534 /* func->TexParameter = nv04TexParameter;*/
535 /* func->TextureMatrix = nv04TextureMatrix;*/
536
537 nmesa->hw_func.InitCard = nv04InitCard;
538 nmesa->hw_func.BindBuffers = nv04BindBuffers;
539 nmesa->hw_func.WindowMoved = nv04WindowMoved;
540 }