i965/gen7+: Implement fast color clears for MSAA buffers.
[mesa.git] / src / mesa / vbo / vbo_exec.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2005 Brian Paul 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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 */
27
28
29 #include "main/api_arrayelt.h"
30 #include "main/glheader.h"
31 #include "main/mtypes.h"
32 #include "main/vtxfmt.h"
33 #include "vbo_context.h"
34
35
36
37 void vbo_exec_init( struct gl_context *ctx )
38 {
39 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
40
41 exec->ctx = ctx;
42
43 /* Initialize the arrayelt helper
44 */
45 if (!ctx->aelt_context &&
46 !_ae_create_context( ctx ))
47 return;
48
49 vbo_exec_vtx_init( exec );
50
51 ctx->Driver.NeedFlush = 0;
52 ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
53 ctx->Driver.BeginVertices = vbo_exec_BeginVertices;
54 ctx->Driver.FlushVertices = vbo_exec_FlushVertices;
55
56 vbo_exec_invalidate_state( ctx, ~0 );
57 }
58
59
60 void vbo_exec_destroy( struct gl_context *ctx )
61 {
62 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
63
64 if (ctx->aelt_context) {
65 _ae_destroy_context( ctx );
66 ctx->aelt_context = NULL;
67 }
68
69 vbo_exec_vtx_destroy( exec );
70 }
71
72
73 /**
74 * Really want to install these callbacks to a central facility to be
75 * invoked according to the state flags. That will have to wait for a
76 * mesa rework:
77 */
78 void vbo_exec_invalidate_state( struct gl_context *ctx, GLuint new_state )
79 {
80 struct vbo_context *vbo = vbo_context(ctx);
81 struct vbo_exec_context *exec = &vbo->exec;
82
83 if (!exec->validating && new_state & (_NEW_PROGRAM|_NEW_ARRAY)) {
84 exec->array.recalculate_inputs = GL_TRUE;
85
86 /* If we ended up here because a VAO was deleted, the _DrawArrays
87 * pointer which pointed to the VAO might be invalid now, so set it
88 * to NULL. This prevents crashes in driver functions like Clear
89 * where driver state validation might occur, but the vbo module is
90 * still in an invalid state.
91 *
92 * Drivers should skip vertex array state validation if _DrawArrays
93 * is NULL. It also has no effect on performance, because attrib
94 * bindings will be recalculated anyway.
95 */
96 if (vbo->last_draw_method == DRAW_ARRAYS) {
97 ctx->Array._DrawArrays = NULL;
98 vbo->last_draw_method = DRAW_NONE;
99 }
100 }
101
102 if (new_state & _NEW_EVAL)
103 exec->eval.recalculate_maps = GL_TRUE;
104
105 _ae_invalidate_state(ctx, new_state);
106 }
107
108
109 /**
110 * Figure out the number of transform feedback primitives that will be output
111 * considering the drawing mode, number of vertices, and instance count,
112 * assuming that no geometry shading is done and primitive restart is not
113 * used.
114 *
115 * This is used by driver back-ends in implementing the PRIMITIVES_GENERATED
116 * and TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN queries. It is also used to
117 * pre-validate draw calls in GLES3 (where draw calls only succeed if there is
118 * enough room in the transform feedback buffer for the result).
119 */
120 size_t
121 vbo_count_tessellated_primitives(GLenum mode, GLuint count,
122 GLuint num_instances)
123 {
124 size_t num_primitives;
125 switch (mode) {
126 case GL_POINTS:
127 num_primitives = count;
128 break;
129 case GL_LINE_STRIP:
130 num_primitives = count >= 2 ? count - 1 : 0;
131 break;
132 case GL_LINE_LOOP:
133 num_primitives = count >= 2 ? count : 0;
134 break;
135 case GL_LINES:
136 num_primitives = count / 2;
137 break;
138 case GL_TRIANGLE_STRIP:
139 case GL_TRIANGLE_FAN:
140 case GL_POLYGON:
141 num_primitives = count >= 3 ? count - 2 : 0;
142 break;
143 case GL_TRIANGLES:
144 num_primitives = count / 3;
145 break;
146 case GL_QUAD_STRIP:
147 num_primitives = count >= 4 ? ((count / 2) - 1) * 2 : 0;
148 break;
149 case GL_QUADS:
150 num_primitives = (count / 4) * 2;
151 break;
152 case GL_LINES_ADJACENCY:
153 num_primitives = count / 4;
154 break;
155 case GL_LINE_STRIP_ADJACENCY:
156 num_primitives = count >= 4 ? count - 3 : 0;
157 break;
158 case GL_TRIANGLES_ADJACENCY:
159 num_primitives = count / 6;
160 break;
161 case GL_TRIANGLE_STRIP_ADJACENCY:
162 num_primitives = count >= 6 ? (count - 4) / 2 : 0;
163 break;
164 default:
165 assert(!"Unexpected primitive type in count_tessellated_primitives");
166 num_primitives = 0;
167 break;
168 }
169 return num_primitives * num_instances;
170 }
171
172
173
174 /**
175 * In some degenarate cases we can improve our ability to merge
176 * consecutive primitives. For example:
177 * glBegin(GL_LINE_STRIP);
178 * glVertex(1);
179 * glVertex(1);
180 * glEnd();
181 * glBegin(GL_LINE_STRIP);
182 * glVertex(1);
183 * glVertex(1);
184 * glEnd();
185 * Can be merged as a GL_LINES prim with four vertices.
186 *
187 * This function converts 2-vertex line strips/loops into GL_LINES, etc.
188 */
189 void
190 vbo_try_prim_conversion(struct _mesa_prim *p)
191 {
192 if (p->mode == GL_LINE_STRIP && p->count == 2) {
193 /* convert 2-vertex line strip to a separate line */
194 p->mode = GL_LINES;
195 }
196 else if ((p->mode == GL_TRIANGLE_STRIP || p->mode == GL_TRIANGLE_FAN)
197 && p->count == 3) {
198 /* convert 3-vertex tri strip or fan to a separate triangle */
199 p->mode = GL_TRIANGLES;
200 }
201
202 /* Note: we can't convert a 4-vertex quad strip to a separate quad
203 * because the vertex ordering is different. We'd have to muck
204 * around in the vertex data to make it work.
205 */
206 }
207
208
209 /**
210 * Helper function for determining if two subsequent glBegin/glEnd
211 * primitives can be combined. This is only possible for GL_POINTS,
212 * GL_LINES, GL_TRIANGLES and GL_QUADS.
213 * If we return true, it means that we can concatenate p1 onto p0 (and
214 * discard p1).
215 */
216 bool
217 vbo_can_merge_prims(const struct _mesa_prim *p0, const struct _mesa_prim *p1)
218 {
219 if (!p0->begin ||
220 !p1->begin ||
221 !p0->end ||
222 !p1->end)
223 return false;
224
225 /* The prim mode must match (ex: both GL_TRIANGLES) */
226 if (p0->mode != p1->mode)
227 return false;
228
229 /* p1's vertices must come right after p0 */
230 if (p0->start + p0->count != p1->start)
231 return false;
232
233 if (p0->basevertex != p1->basevertex ||
234 p0->num_instances != p1->num_instances ||
235 p0->base_instance != p1->base_instance)
236 return false;
237
238 /* can always merge subsequent GL_POINTS primitives */
239 if (p0->mode == GL_POINTS)
240 return true;
241
242 /* independent lines with no extra vertices */
243 if (p0->mode == GL_LINES && p0->count % 2 == 0 && p1->count % 2 == 0)
244 return true;
245
246 /* independent tris */
247 if (p0->mode == GL_TRIANGLES && p0->count % 3 == 0 && p1->count % 3 == 0)
248 return true;
249
250 /* independent quads */
251 if (p0->mode == GL_QUADS && p0->count % 4 == 0 && p1->count % 4 == 0)
252 return true;
253
254 return false;
255 }
256
257
258 /**
259 * If we've determined that p0 and p1 can be merged, this function
260 * concatenates p1 onto p0.
261 */
262 void
263 vbo_merge_prims(struct _mesa_prim *p0, const struct _mesa_prim *p1)
264 {
265 assert(vbo_can_merge_prims(p0, p1));
266
267 p0->count += p1->count;
268 p0->end = p1->end;
269 }