1 /**************************************************************************
3 * Copyright 2003 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 #include "main/glheader.h"
31 #include "main/context.h"
33 #include "pipe/p_defines.h"
34 #include "st_context.h"
36 #include "st_program.h"
37 #include "st_manager.h"
40 /* The list state update functions. */
41 static const struct st_tracked_state
*atoms
[] =
43 #define ST_STATE(FLAG, st_update) &st_update,
44 #include "st_atom_list.h"
49 void st_init_atoms( struct st_context
*st
)
51 STATIC_ASSERT(ARRAY_SIZE(atoms
) <= 64);
55 void st_destroy_atoms( struct st_context
*st
)
61 /* Too complex to figure out, just check every time:
63 static void check_program_state( struct st_context
*st
)
65 struct gl_context
*ctx
= st
->ctx
;
66 struct st_vertex_program
*old_vp
= st
->vp
;
67 struct st_tessctrl_program
*old_tcp
= st
->tcp
;
68 struct st_tesseval_program
*old_tep
= st
->tep
;
69 struct st_geometry_program
*old_gp
= st
->gp
;
70 struct st_fragment_program
*old_fp
= st
->fp
;
72 struct gl_program
*new_vp
= ctx
->VertexProgram
._Current
;
73 struct gl_program
*new_tcp
= ctx
->TessCtrlProgram
._Current
;
74 struct gl_program
*new_tep
= ctx
->TessEvalProgram
._Current
;
75 struct gl_program
*new_gp
= ctx
->GeometryProgram
._Current
;
76 struct gl_program
*new_fp
= ctx
->FragmentProgram
._Current
;
79 /* Flag states used by both new and old shaders to unbind shader resources
80 * properly when transitioning to shaders that don't use them.
82 if (unlikely(new_vp
!= &old_vp
->Base
)) {
84 dirty
|= old_vp
->affected_states
;
86 dirty
|= ST_NEW_VERTEX_PROGRAM(st
, st_vertex_program(new_vp
));
89 if (unlikely(new_tcp
!= &old_tcp
->Base
)) {
91 dirty
|= old_tcp
->affected_states
;
93 dirty
|= st_tessctrl_program(new_tcp
)->affected_states
;
96 if (unlikely(new_tep
!= &old_tep
->Base
)) {
98 dirty
|= old_tep
->affected_states
;
100 dirty
|= st_tesseval_program(new_tep
)->affected_states
;
103 if (unlikely(new_gp
!= &old_gp
->Base
)) {
105 dirty
|= old_gp
->affected_states
;
107 dirty
|= st_geometry_program(new_gp
)->affected_states
;
110 if (unlikely(new_fp
!= &old_fp
->Base
)) {
112 dirty
|= old_fp
->affected_states
;
114 dirty
|= st_fragment_program(new_fp
)->affected_states
;
118 st
->gfx_shaders_may_be_dirty
= false;
121 static void check_attrib_edgeflag(struct st_context
*st
)
123 const struct gl_vertex_array
**arrays
= st
->ctx
->Array
._DrawArrays
;
124 GLboolean vertdata_edgeflags
, edgeflag_culls_prims
, edgeflags_enabled
;
125 struct gl_program
*vp
= st
->ctx
->VertexProgram
._Current
;
130 edgeflags_enabled
= st
->ctx
->Polygon
.FrontMode
!= GL_FILL
||
131 st
->ctx
->Polygon
.BackMode
!= GL_FILL
;
133 vertdata_edgeflags
= edgeflags_enabled
&&
134 arrays
[VERT_ATTRIB_EDGEFLAG
]->StrideB
!= 0;
135 if (vertdata_edgeflags
!= st
->vertdata_edgeflags
) {
136 st
->vertdata_edgeflags
= vertdata_edgeflags
;
138 st
->dirty
|= ST_NEW_VERTEX_PROGRAM(st
, st_vertex_program(vp
));
141 edgeflag_culls_prims
= edgeflags_enabled
&& !vertdata_edgeflags
&&
142 !st
->ctx
->Current
.Attrib
[VERT_ATTRIB_EDGEFLAG
][0];
143 if (edgeflag_culls_prims
!= st
->edgeflag_culls_prims
) {
144 st
->edgeflag_culls_prims
= edgeflag_culls_prims
;
145 st
->dirty
|= ST_NEW_RASTERIZER
;
150 /***********************************************************************
151 * Update all derived state:
154 void st_validate_state( struct st_context
*st
, enum st_pipeline pipeline
)
156 struct gl_context
*ctx
= st
->ctx
;
157 uint64_t dirty
, pipeline_mask
;
158 uint32_t dirty_lo
, dirty_hi
;
160 /* Get Mesa driver state.
162 * Inactive states are shader states not used by shaders at the moment.
164 st
->dirty
|= ctx
->NewDriverState
& st
->active_states
& ST_ALL_STATES_MASK
;
165 ctx
->NewDriverState
= 0;
167 /* Get pipeline state. */
169 case ST_PIPELINE_RENDER
:
170 if (st
->ctx
->API
== API_OPENGL_COMPAT
)
171 check_attrib_edgeflag(st
);
173 check_program_state(st
);
174 st_manager_validate_framebuffers(st
);
176 pipeline_mask
= ST_PIPELINE_RENDER_STATE_MASK
;
179 case ST_PIPELINE_CLEAR
:
180 st_manager_validate_framebuffers(st
);
181 pipeline_mask
= ST_PIPELINE_CLEAR_STATE_MASK
;
184 case ST_PIPELINE_UPDATE_FRAMEBUFFER
:
185 st_manager_validate_framebuffers(st
);
186 pipeline_mask
= ST_PIPELINE_UPDATE_FB_STATE_MASK
;
189 case ST_PIPELINE_COMPUTE
: {
190 struct st_compute_program
*old_cp
= st
->cp
;
191 struct gl_program
*new_cp
= ctx
->ComputeProgram
._Current
;
193 if (new_cp
!= &old_cp
->Base
) {
195 st
->dirty
|= old_cp
->affected_states
;
197 st
->dirty
|= st_compute_program(new_cp
)->affected_states
;
200 st
->compute_shader_may_be_dirty
= false;
203 * We add the ST_NEW_FB_STATE bit here as well, because glBindFramebuffer
204 * acts as a barrier that breaks feedback loops between the framebuffer
205 * and textures bound to the framebuffer, even when those textures are
206 * accessed by compute shaders; so we must inform the driver of new
209 pipeline_mask
= ST_PIPELINE_COMPUTE_STATE_MASK
| ST_NEW_FB_STATE
;
214 unreachable("Invalid pipeline specified");
217 dirty
= st
->dirty
& pipeline_mask
;
222 dirty_hi
= dirty
>> 32;
226 * Don't use u_bit_scan64, it may be slower on 32-bit.
229 atoms
[u_bit_scan(&dirty_lo
)]->update(st
);
231 atoms
[32 + u_bit_scan(&dirty_hi
)]->update(st
);
233 /* Clear the render or compute state bits. */
234 st
->dirty
&= ~pipeline_mask
;