2 * Copyright (C) 2009 Francisco Jerez.
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:
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.
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.
27 #include "nouveau_driver.h"
28 #include "nouveau_context.h"
29 #include "nouveau_texture.h"
30 #include "nouveau_util.h"
32 #include "swrast/swrast.h"
36 nouveau_alpha_func(struct gl_context
*ctx
, GLenum func
, GLfloat ref
)
38 context_dirty(ctx
, ALPHA_FUNC
);
42 nouveau_blend_color(struct gl_context
*ctx
, const GLfloat color
[4])
44 context_dirty(ctx
, BLEND_COLOR
);
48 nouveau_blend_equation_separate(struct gl_context
*ctx
, GLenum modeRGB
, GLenum modeA
)
50 context_dirty(ctx
, BLEND_EQUATION
);
54 nouveau_blend_func_separate(struct gl_context
*ctx
, GLenum sfactorRGB
,
55 GLenum dfactorRGB
, GLenum sfactorA
, GLenum dfactorA
)
57 context_dirty(ctx
, BLEND_FUNC
);
61 nouveau_clip_plane(struct gl_context
*ctx
, GLenum plane
, const GLfloat
*equation
)
63 context_dirty_i(ctx
, CLIP_PLANE
, plane
- GL_CLIP_PLANE0
);
67 nouveau_color_mask(struct gl_context
*ctx
, GLboolean rmask
, GLboolean gmask
,
68 GLboolean bmask
, GLboolean amask
)
70 context_dirty(ctx
, COLOR_MASK
);
74 nouveau_color_material(struct gl_context
*ctx
, GLenum face
, GLenum mode
)
76 context_dirty(ctx
, COLOR_MATERIAL
);
77 context_dirty(ctx
, MATERIAL_FRONT_AMBIENT
);
78 context_dirty(ctx
, MATERIAL_BACK_AMBIENT
);
79 context_dirty(ctx
, MATERIAL_FRONT_DIFFUSE
);
80 context_dirty(ctx
, MATERIAL_BACK_DIFFUSE
);
81 context_dirty(ctx
, MATERIAL_FRONT_SPECULAR
);
82 context_dirty(ctx
, MATERIAL_BACK_SPECULAR
);
86 nouveau_cull_face(struct gl_context
*ctx
, GLenum mode
)
88 context_dirty(ctx
, CULL_FACE
);
92 nouveau_front_face(struct gl_context
*ctx
, GLenum mode
)
94 context_dirty(ctx
, FRONT_FACE
);
98 nouveau_depth_func(struct gl_context
*ctx
, GLenum func
)
100 context_dirty(ctx
, DEPTH
);
104 nouveau_depth_mask(struct gl_context
*ctx
, GLboolean flag
)
106 context_dirty(ctx
, DEPTH
);
110 nouveau_read_buffer(struct gl_context
*ctx
, GLenum buffer
)
112 nouveau_validate_framebuffer(ctx
);
116 nouveau_draw_buffers(struct gl_context
*ctx
, GLsizei n
, const GLenum
*buffers
)
118 nouveau_validate_framebuffer(ctx
);
119 context_dirty(ctx
, FRAMEBUFFER
);
123 nouveau_enable(struct gl_context
*ctx
, GLenum cap
, GLboolean state
)
129 context_dirty(ctx
, ALPHA_FUNC
);
132 context_dirty(ctx
, BLEND_EQUATION
);
134 case GL_COLOR_LOGIC_OP
:
135 context_dirty(ctx
, LOGIC_OPCODE
);
137 case GL_COLOR_MATERIAL
:
138 context_dirty(ctx
, COLOR_MATERIAL
);
139 context_dirty(ctx
, MATERIAL_FRONT_AMBIENT
);
140 context_dirty(ctx
, MATERIAL_BACK_AMBIENT
);
141 context_dirty(ctx
, MATERIAL_FRONT_DIFFUSE
);
142 context_dirty(ctx
, MATERIAL_BACK_DIFFUSE
);
143 context_dirty(ctx
, MATERIAL_FRONT_SPECULAR
);
144 context_dirty(ctx
, MATERIAL_BACK_SPECULAR
);
146 case GL_COLOR_SUM_EXT
:
147 context_dirty(ctx
, FRAG
);
148 context_dirty(ctx
, LIGHT_MODEL
);
151 context_dirty(ctx
, CULL_FACE
);
154 context_dirty(ctx
, DEPTH
);
157 context_dirty(ctx
, DITHER
);
160 context_dirty(ctx
, FOG
);
161 context_dirty(ctx
, FRAG
);
162 context_dirty(ctx
, MODELVIEW
);
172 context_dirty(ctx
, MODELVIEW
);
173 context_dirty(ctx
, LIGHT_ENABLE
);
174 context_dirty_i(ctx
, LIGHT_SOURCE
, cap
- GL_LIGHT0
);
175 context_dirty(ctx
, MATERIAL_FRONT_AMBIENT
);
176 context_dirty(ctx
, MATERIAL_BACK_AMBIENT
);
177 context_dirty(ctx
, MATERIAL_FRONT_DIFFUSE
);
178 context_dirty(ctx
, MATERIAL_BACK_DIFFUSE
);
179 context_dirty(ctx
, MATERIAL_FRONT_SPECULAR
);
180 context_dirty(ctx
, MATERIAL_BACK_SPECULAR
);
181 context_dirty(ctx
, MATERIAL_FRONT_SHININESS
);
182 context_dirty(ctx
, MATERIAL_BACK_SHININESS
);
185 context_dirty(ctx
, FRAG
);
186 context_dirty(ctx
, MODELVIEW
);
187 context_dirty(ctx
, LIGHT_MODEL
);
188 context_dirty(ctx
, LIGHT_ENABLE
);
190 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
191 if (ctx
->Light
.Light
[i
].Enabled
)
192 context_dirty_i(ctx
, LIGHT_SOURCE
, i
);
195 context_dirty(ctx
, MATERIAL_FRONT_AMBIENT
);
196 context_dirty(ctx
, MATERIAL_BACK_AMBIENT
);
197 context_dirty(ctx
, MATERIAL_FRONT_DIFFUSE
);
198 context_dirty(ctx
, MATERIAL_BACK_DIFFUSE
);
199 context_dirty(ctx
, MATERIAL_FRONT_SPECULAR
);
200 context_dirty(ctx
, MATERIAL_BACK_SPECULAR
);
201 context_dirty(ctx
, MATERIAL_FRONT_SHININESS
);
202 context_dirty(ctx
, MATERIAL_BACK_SHININESS
);
205 context_dirty(ctx
, LINE_MODE
);
208 context_dirty(ctx
, LIGHT_ENABLE
);
210 case GL_POINT_SMOOTH
:
211 context_dirty(ctx
, POINT_MODE
);
213 case GL_POLYGON_OFFSET_POINT
:
214 case GL_POLYGON_OFFSET_LINE
:
215 case GL_POLYGON_OFFSET_FILL
:
216 context_dirty(ctx
, POLYGON_OFFSET
);
218 case GL_POLYGON_SMOOTH
:
219 context_dirty(ctx
, POLYGON_MODE
);
221 case GL_SCISSOR_TEST
:
222 context_dirty(ctx
, SCISSOR
);
224 case GL_STENCIL_TEST
:
225 context_dirty(ctx
, STENCIL_FUNC
);
230 case GL_TEXTURE_RECTANGLE
:
231 context_dirty_i(ctx
, TEX_ENV
, ctx
->Texture
.CurrentUnit
);
232 context_dirty_i(ctx
, TEX_OBJ
, ctx
->Texture
.CurrentUnit
);
234 case GL_TEXTURE_GEN_S
:
235 case GL_TEXTURE_GEN_T
:
236 case GL_TEXTURE_GEN_R
:
237 case GL_TEXTURE_GEN_Q
:
238 context_dirty_i(ctx
, TEX_GEN
, ctx
->Texture
.CurrentUnit
);
239 context_dirty(ctx
, MODELVIEW
);
245 nouveau_fog(struct gl_context
*ctx
, GLenum pname
, const GLfloat
*params
)
247 context_dirty(ctx
, FOG
);
251 nouveau_light(struct gl_context
*ctx
, GLenum light
, GLenum pname
, const GLfloat
*params
)
255 context_dirty(ctx
, MATERIAL_FRONT_AMBIENT
);
256 context_dirty(ctx
, MATERIAL_BACK_AMBIENT
);
259 context_dirty(ctx
, MATERIAL_FRONT_DIFFUSE
);
260 context_dirty(ctx
, MATERIAL_BACK_DIFFUSE
);
263 context_dirty(ctx
, MATERIAL_FRONT_SPECULAR
);
264 context_dirty(ctx
, MATERIAL_BACK_SPECULAR
);
268 context_dirty(ctx
, MODELVIEW
);
269 context_dirty(ctx
, LIGHT_ENABLE
);
270 context_dirty_i(ctx
, LIGHT_SOURCE
, light
- GL_LIGHT0
);
273 context_dirty_i(ctx
, LIGHT_SOURCE
, light
- GL_LIGHT0
);
279 nouveau_light_model(struct gl_context
*ctx
, GLenum pname
, const GLfloat
*params
)
281 context_dirty(ctx
, LIGHT_MODEL
);
282 context_dirty(ctx
, MODELVIEW
);
286 nouveau_line_stipple(struct gl_context
*ctx
, GLint factor
, GLushort pattern
)
288 context_dirty(ctx
, LINE_STIPPLE
);
292 nouveau_line_width(struct gl_context
*ctx
, GLfloat width
)
294 context_dirty(ctx
, LINE_MODE
);
298 nouveau_logic_opcode(struct gl_context
*ctx
, GLenum opcode
)
300 context_dirty(ctx
, LOGIC_OPCODE
);
304 nouveau_point_parameter(struct gl_context
*ctx
, GLenum pname
, const GLfloat
*params
)
306 context_dirty(ctx
, POINT_PARAMETER
);
310 nouveau_point_size(struct gl_context
*ctx
, GLfloat size
)
312 context_dirty(ctx
, POINT_MODE
);
316 nouveau_polygon_mode(struct gl_context
*ctx
, GLenum face
, GLenum mode
)
318 context_dirty(ctx
, POLYGON_MODE
);
322 nouveau_polygon_offset(struct gl_context
*ctx
, GLfloat factor
, GLfloat units
)
324 context_dirty(ctx
, POLYGON_OFFSET
);
328 nouveau_polygon_stipple(struct gl_context
*ctx
, const GLubyte
*mask
)
330 context_dirty(ctx
, POLYGON_STIPPLE
);
334 nouveau_render_mode(struct gl_context
*ctx
, GLenum mode
)
336 context_dirty(ctx
, RENDER_MODE
);
340 nouveau_shade_model(struct gl_context
*ctx
, GLenum mode
)
342 context_dirty(ctx
, SHADE_MODEL
);
346 nouveau_stencil_func_separate(struct gl_context
*ctx
, GLenum face
, GLenum func
,
347 GLint ref
, GLuint mask
)
349 context_dirty(ctx
, STENCIL_FUNC
);
353 nouveau_stencil_mask_separate(struct gl_context
*ctx
, GLenum face
, GLuint mask
)
355 context_dirty(ctx
, STENCIL_MASK
);
359 nouveau_stencil_op_separate(struct gl_context
*ctx
, GLenum face
, GLenum fail
,
360 GLenum zfail
, GLenum zpass
)
362 context_dirty(ctx
, STENCIL_OP
);
366 nouveau_tex_gen(struct gl_context
*ctx
, GLenum coord
, GLenum pname
,
367 const GLfloat
*params
)
370 case GL_TEXTURE_GEN_MODE
:
371 context_dirty_i(ctx
, TEX_GEN
, ctx
->Texture
.CurrentUnit
);
372 context_dirty(ctx
, MODELVIEW
);
375 context_dirty_i(ctx
, TEX_GEN
, ctx
->Texture
.CurrentUnit
);
381 nouveau_tex_env(struct gl_context
*ctx
, GLenum target
, GLenum pname
,
382 const GLfloat
*param
)
385 case GL_TEXTURE_FILTER_CONTROL_EXT
:
386 context_dirty_i(ctx
, TEX_OBJ
, ctx
->Texture
.CurrentUnit
);
389 context_dirty_i(ctx
, TEX_ENV
, ctx
->Texture
.CurrentUnit
);
395 nouveau_tex_parameter(struct gl_context
*ctx
,
396 struct gl_texture_object
*t
, GLenum pname
,
397 const GLfloat
*params
)
400 case GL_TEXTURE_MAG_FILTER
:
401 case GL_TEXTURE_WRAP_S
:
402 case GL_TEXTURE_WRAP_T
:
403 case GL_TEXTURE_WRAP_R
:
404 case GL_TEXTURE_MIN_LOD
:
405 case GL_TEXTURE_MAX_LOD
:
406 case GL_TEXTURE_MAX_ANISOTROPY_EXT
:
407 case GL_TEXTURE_LOD_BIAS
:
408 context_dirty_i(ctx
, TEX_OBJ
, ctx
->Texture
.CurrentUnit
);
411 case GL_TEXTURE_MIN_FILTER
:
412 case GL_TEXTURE_BASE_LEVEL
:
413 case GL_TEXTURE_MAX_LEVEL
:
414 nouveau_texture_reallocate(ctx
, t
);
415 context_dirty_i(ctx
, TEX_OBJ
, ctx
->Texture
.CurrentUnit
);
421 nouveau_emit_nothing(struct gl_context
*ctx
, int emit
)
426 nouveau_next_dirty_state(struct gl_context
*ctx
)
428 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
429 int i
= BITSET_FFS(nctx
->dirty
) - 1;
431 if (i
< 0 || i
>= context_drv(ctx
)->num_emit
)
438 nouveau_state_emit(struct gl_context
*ctx
)
440 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
441 const struct nouveau_driver
*drv
= context_drv(ctx
);
444 while ((i
= nouveau_next_dirty_state(ctx
)) >= 0) {
445 BITSET_CLEAR(nctx
->dirty
, i
);
446 drv
->emit
[i
](ctx
, i
);
449 BITSET_ZERO(nctx
->dirty
);
453 nouveau_update_state(struct gl_context
*ctx
, GLbitfield new_state
)
457 if (new_state
& (_NEW_PROJECTION
| _NEW_MODELVIEW
))
458 context_dirty(ctx
, PROJECTION
);
460 if (new_state
& _NEW_MODELVIEW
)
461 context_dirty(ctx
, MODELVIEW
);
463 if (new_state
& _NEW_TEXTURE_MATRIX
) {
464 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++)
465 context_dirty_i(ctx
, TEX_MAT
, i
);
468 if (new_state
& _NEW_SCISSOR
)
469 context_dirty(ctx
, SCISSOR
);
471 if (new_state
& _NEW_VIEWPORT
)
472 context_dirty(ctx
, VIEWPORT
);
474 if (new_state
& _NEW_CURRENT_ATTRIB
&&
475 new_state
& _NEW_LIGHT
) {
476 context_dirty(ctx
, MATERIAL_FRONT_AMBIENT
);
477 context_dirty(ctx
, MATERIAL_BACK_AMBIENT
);
478 context_dirty(ctx
, MATERIAL_FRONT_DIFFUSE
);
479 context_dirty(ctx
, MATERIAL_BACK_DIFFUSE
);
480 context_dirty(ctx
, MATERIAL_FRONT_SPECULAR
);
481 context_dirty(ctx
, MATERIAL_BACK_SPECULAR
);
482 context_dirty(ctx
, MATERIAL_FRONT_SHININESS
);
483 context_dirty(ctx
, MATERIAL_BACK_SHININESS
);
486 if (new_state
& _NEW_TEXTURE
) {
487 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
488 if (ctx
->Texture
.Unit
[i
].Sampler
)
489 context_dirty_i(ctx
, TEX_OBJ
, i
);
493 _swrast_InvalidateState(ctx
, new_state
);
494 _tnl_InvalidateState(ctx
, new_state
);
496 nouveau_state_emit(ctx
);
500 nouveau_state_init(struct gl_context
*ctx
)
502 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
504 ctx
->Driver
.AlphaFunc
= nouveau_alpha_func
;
505 ctx
->Driver
.BlendColor
= nouveau_blend_color
;
506 ctx
->Driver
.BlendEquationSeparate
= nouveau_blend_equation_separate
;
507 ctx
->Driver
.BlendFuncSeparate
= nouveau_blend_func_separate
;
508 ctx
->Driver
.ClipPlane
= nouveau_clip_plane
;
509 ctx
->Driver
.ColorMask
= nouveau_color_mask
;
510 ctx
->Driver
.ColorMaterial
= nouveau_color_material
;
511 ctx
->Driver
.CullFace
= nouveau_cull_face
;
512 ctx
->Driver
.FrontFace
= nouveau_front_face
;
513 ctx
->Driver
.DepthFunc
= nouveau_depth_func
;
514 ctx
->Driver
.DepthMask
= nouveau_depth_mask
;
515 ctx
->Driver
.ReadBuffer
= nouveau_read_buffer
;
516 ctx
->Driver
.DrawBuffers
= nouveau_draw_buffers
;
517 ctx
->Driver
.Enable
= nouveau_enable
;
518 ctx
->Driver
.Fogfv
= nouveau_fog
;
519 ctx
->Driver
.Lightfv
= nouveau_light
;
520 ctx
->Driver
.LightModelfv
= nouveau_light_model
;
521 ctx
->Driver
.LineStipple
= nouveau_line_stipple
;
522 ctx
->Driver
.LineWidth
= nouveau_line_width
;
523 ctx
->Driver
.LogicOpcode
= nouveau_logic_opcode
;
524 ctx
->Driver
.PointParameterfv
= nouveau_point_parameter
;
525 ctx
->Driver
.PointSize
= nouveau_point_size
;
526 ctx
->Driver
.PolygonMode
= nouveau_polygon_mode
;
527 ctx
->Driver
.PolygonOffset
= nouveau_polygon_offset
;
528 ctx
->Driver
.PolygonStipple
= nouveau_polygon_stipple
;
529 ctx
->Driver
.RenderMode
= nouveau_render_mode
;
530 ctx
->Driver
.ShadeModel
= nouveau_shade_model
;
531 ctx
->Driver
.StencilFuncSeparate
= nouveau_stencil_func_separate
;
532 ctx
->Driver
.StencilMaskSeparate
= nouveau_stencil_mask_separate
;
533 ctx
->Driver
.StencilOpSeparate
= nouveau_stencil_op_separate
;
534 ctx
->Driver
.TexGen
= nouveau_tex_gen
;
535 ctx
->Driver
.TexEnv
= nouveau_tex_env
;
536 ctx
->Driver
.TexParameter
= nouveau_tex_parameter
;
538 ctx
->Driver
.UpdateState
= nouveau_update_state
;
540 BITSET_ONES(nctx
->dirty
);