1 /**************************************************************************
3 Copyright 2007 Stephane Marchesin
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:
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
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.
25 **************************************************************************/
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"
33 #include "tnl/t_pipeline.h"
38 static uint32_t nv04_compare_func(GLuint 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;
50 WARN_ONCE("Unable to find the function\n");
54 static uint32_t nv04_blend_func(GLuint 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;
69 WARN_ONCE("Unable to find the function 0x%x\n",f
);
73 static void nv04_emit_control(GLcontext
*ctx
)
75 nouveauContextPtr nmesa
= NOUVEAU_CONTEXT(ctx
);
76 uint32_t control
,cull
;
79 CLAMPED_FLOAT_TO_UBYTE(alpha_ref
, ctx
->Color
.AlphaRef
);
81 control
|=(nv04_compare_func(ctx
->Color
.AlphaFunc
)<<8);
82 control
|=(ctx
->Color
.AlphaEnabled
<<12);
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
))
88 if ((ctx
->Polygon
.FrontFace
==GL_CW
)&&(ctx
->Polygon
.CullFaceMode
==GL_FRONT
))
90 if ((ctx
->Polygon
.FrontFace
==GL_CW
)&&(ctx
->Polygon
.CullFaceMode
==GL_BACK
))
92 if ((ctx
->Polygon
.FrontFace
==GL_CCW
)&&(ctx
->Polygon
.CullFaceMode
==GL_FRONT
))
94 if ((ctx
->Polygon
.FrontFace
==GL_CCW
)&&(ctx
->Polygon
.CullFaceMode
==GL_BACK
))
98 if (ctx
->Polygon
.CullFaceMode
==GL_FRONT_AND_BACK
)
103 control
|=(ctx
->Color
.DitherFlag
<<22);
104 if ((ctx
->Depth
.Test
)&&(ctx
->Depth
.Mask
))
107 control
|=(1<<30); // integer zbuffer format
109 BEGIN_RING_CACHE(NvSub3D
, NV04_DX5_TEXTURED_TRIANGLE_CONTROL
, 1);
110 OUT_RING_CACHE(control
);
113 static void nv04_emit_blend(GLcontext
*ctx
)
115 nouveauContextPtr nmesa
= NOUVEAU_CONTEXT(ctx
);
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;
125 if (ctx
->Hint
.PerspectiveCorrection
!=GL_FASTEST
)
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);
132 BEGIN_RING_CACHE(NvSub3D
, NV04_DX5_TEXTURED_TRIANGLE_BLEND
, 1);
133 OUT_RING_CACHE(blend
);
136 static void nv04_emit_fog_color(GLcontext
*ctx
)
138 nouveauContextPtr nmesa
= NOUVEAU_CONTEXT(ctx
);
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]));
148 static void nv04AlphaFunc(GLcontext
*ctx
, GLenum func
, GLfloat ref
)
150 nv04_emit_control(ctx
);
153 static void nv04BlendColor(GLcontext
*ctx
, const GLfloat color
[4])
155 nv04_emit_blend(ctx
);
158 static void nv04BlendEquationSeparate(GLcontext
*ctx
, GLenum modeRGB
, GLenum modeA
)
160 nv04_emit_blend(ctx
);
164 static void nv04BlendFuncSeparate(GLcontext
*ctx
, GLenum sfactorRGB
, GLenum dfactorRGB
,
165 GLenum sfactorA
, GLenum dfactorA
)
167 nv04_emit_blend(ctx
);
170 static void nv04Clear(GLcontext
*ctx
, GLbitfield mask
)
175 static void nv04ClearColor(GLcontext
*ctx
, const GLfloat color
[4])
180 static void nv04ClearDepth(GLcontext
*ctx
, GLclampd d
)
185 static void nv04ClearStencil(GLcontext
*ctx
, GLint s
)
190 static void nv04ClipPlane(GLcontext
*ctx
, GLenum plane
, const GLfloat
*equation
)
195 static void nv04ColorMask(GLcontext
*ctx
, GLboolean rmask
, GLboolean gmask
,
196 GLboolean bmask
, GLboolean amask
)
201 static void nv04ColorMaterial(GLcontext
*ctx
, GLenum face
, GLenum mode
)
203 /* TODO I need love */
206 static void nv04CullFace(GLcontext
*ctx
, GLenum mode
)
208 nv04_emit_control(ctx
);
211 static void nv04FrontFace(GLcontext
*ctx
, GLenum mode
)
216 static void nv04DepthFunc(GLcontext
*ctx
, GLenum func
)
218 nv04_emit_control(ctx
);
221 static void nv04DepthMask(GLcontext
*ctx
, GLboolean flag
)
226 static void nv04DepthRange(GLcontext
*ctx
, GLclampd nearval
, GLclampd farval
)
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 );
236 static void nv04Enable(GLcontext
*ctx
, GLenum cap
, GLboolean state
)
241 nv04_emit_control(ctx
);
243 // case GL_AUTO_NORMAL:
245 nv04_emit_blend(ctx
);
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:
260 nv04_emit_control(ctx
);
263 nv04_emit_control(ctx
);
266 nv04_emit_control(ctx
);
269 nv04_emit_blend(ctx
);
270 nv04_emit_fog_color(ctx
);
272 // case GL_HISTOGRAM:
273 // case GL_INDEX_LOGIC_OP:
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:
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:
327 static void nv04Fogfv(GLcontext
*ctx
, GLenum pname
, const GLfloat
*params
)
329 nv04_emit_blend(ctx
);
330 nv04_emit_fog_color(ctx
);
333 static void nv04Hint(GLcontext
*ctx
, GLenum target
, GLenum mode
)
337 case GL_PERSPECTIVE_CORRECTION_HINT
:nv04_emit_blend(ctx
);break;
342 static void nv04LineStipple(GLcontext
*ctx
, GLint factor
, GLushort pattern
)
344 /* TODO not even in your dreams */
347 static void nv04LineWidth(GLcontext
*ctx
, GLfloat width
)
352 static void nv04LogicOpcode(GLcontext
*ctx
, GLenum opcode
)
357 static void nv04PointParameterfv(GLcontext
*ctx
, GLenum pname
, const GLfloat
*params
)
362 static void nv04PointSize(GLcontext
*ctx
, GLfloat size
)
367 static void nv04PolygonMode(GLcontext
*ctx
, GLenum face
, GLenum mode
)
372 /** Set the scale and units used to calculate depth values */
373 static void nv04PolygonOffset(GLcontext
*ctx
, GLfloat factor
, GLfloat units
)
378 /** Set the polygon stippling pattern */
379 static void nv04PolygonStipple(GLcontext
*ctx
, const GLubyte
*mask
)
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
);
389 /** Define the scissor box */
390 static void nv04Scissor(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
395 /** Select flat or smooth shading */
396 static void nv04ShadeModel(GLcontext
*ctx
, GLenum mode
)
398 nv04_emit_blend(ctx
);
401 /** OpenGL 2.0 two-sided StencilFunc */
402 static void nv04StencilFuncSeparate(GLcontext
*ctx
, GLenum face
, GLenum func
,
403 GLint ref
, GLuint mask
)
408 /** OpenGL 2.0 two-sided StencilMask */
409 static void nv04StencilMaskSeparate(GLcontext
*ctx
, GLenum face
, GLuint mask
)
414 /** OpenGL 2.0 two-sided StencilOp */
415 static void nv04StencilOpSeparate(GLcontext
*ctx
, GLenum face
, GLenum fail
,
416 GLenum zfail
, GLenum zpass
)
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
);
432 /* Update anything that depends on the window position/size */
433 static void nv04WindowMoved(nouveauContextPtr nmesa
)
437 /* Initialise any card-specific non-GL related state */
438 static GLboolean
nv04InitCard(nouveauContextPtr nmesa
)
440 nouveauObjectOnSubchannel(nmesa
, NvSub3D
, Nv3D
);
441 nouveauObjectOnSubchannel(nmesa
, NvSubCtxSurf3D
, NvCtxSurf3D
);
443 BEGIN_RING_SIZE(NvSubCtxSurf3D
, NV04_CONTEXT_SURFACES_3D_DMA_NOTIFY
, 3);
447 BEGIN_RING_SIZE(NvSub3D
, NV04_DX5_TEXTURED_TRIANGLE_SURFACE
, 1);
448 OUT_RING(NvCtxSurf3D
);
452 /* Update buffer offset/pitch/format */
453 static GLboolean
nv04BindBuffers(nouveauContextPtr nmesa
, int num_color
,
454 nouveau_renderbuffer
**color
,
455 nouveau_renderbuffer
*depth
)
458 uint32_t depth_pitch
=(depth
?depth
->pitch
:0+15)&~15+16;
459 if (depth_pitch
<256) depth_pitch
=256;
461 w
= color
[0]->mesa
.Width
;
462 h
= color
[0]->mesa
.Height
;
466 BEGIN_RING_SIZE(NvSubCtxSurf3D
, NV04_CONTEXT_SURFACES_3D_FORMAT
, 1);
467 if (color
[0]->mesa
._ActualFormat
== GL_RGBA8
)
468 OUT_RING(0x108/*A8R8G8B8*/);
470 OUT_RING(0x103/*R5G6B5*/);
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
);
477 BEGIN_RING_SIZE(NvSubCtxSurf3D
, NV04_CONTEXT_SURFACES_3D_OFFSET_ZETA
, 1);
478 OUT_RING(depth
->offset
);
481 // BEGIN_RING_SIZE(NvSubCtxSurf3D, NV04_CONTEXT_SURFACES_3D_CLIP_HORIZONTAL, 2);
482 // OUT_RING((w<<16)|x);
483 // OUT_RING((h<<16)|y);
486 /* FIXME not sure... */
487 /* BEGIN_RING_SIZE(NvSubCtxSurf3D, NV04_CONTEXT_SURFACES_3D_CLIP_SIZE, 1);
488 OUT_RING((h<<16)|w);*/
493 void nv04InitStateFuncs(GLcontext
*ctx
, struct dd_function_table
*func
)
495 nouveauContextPtr nmesa
= NOUVEAU_CONTEXT(ctx
);
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;*/
537 nmesa
->hw_func
.InitCard
= nv04InitCard
;
538 nmesa
->hw_func
.BindBuffers
= nv04BindBuffers
;
539 nmesa
->hw_func
.WindowMoved
= nv04WindowMoved
;