mesa: Track transform feedback overflow query objects.
[mesa.git] / src / mesa / main / condrender.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2009 VMware, Inc. 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 (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 NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26 /**
27 * \file condrender.c
28 * Conditional rendering functions
29 *
30 * \author Brian Paul
31 */
32
33 #include "glheader.h"
34 #include "condrender.h"
35 #include "enums.h"
36 #include "mtypes.h"
37 #include "queryobj.h"
38
39
40 void GLAPIENTRY
41 _mesa_BeginConditionalRender(GLuint queryId, GLenum mode)
42 {
43 struct gl_query_object *q = NULL;
44 GET_CURRENT_CONTEXT(ctx);
45
46 /* Section 2.14 (Conditional Rendering) of the OpenGL 3.0 spec says:
47 *
48 * "If BeginConditionalRender is called while conditional rendering is
49 * in progress, or if EndConditionalRender is called while conditional
50 * rendering is not in progress, the error INVALID_OPERATION is
51 * generated."
52 */
53 if (!ctx->Extensions.NV_conditional_render || ctx->Query.CondRenderQuery) {
54 _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginConditionalRender()");
55 return;
56 }
57
58 assert(ctx->Query.CondRenderMode == GL_NONE);
59
60 /* Section 2.14 (Conditional Rendering) of the OpenGL 3.0 spec says:
61 *
62 * "The error INVALID_VALUE is generated if <id> is not the name of an
63 * existing query object query."
64 */
65 if (queryId != 0)
66 q = _mesa_lookup_query_object(ctx, queryId);
67
68 if (!q) {
69 _mesa_error(ctx, GL_INVALID_VALUE,
70 "glBeginConditionalRender(bad queryId=%u)", queryId);
71 return;
72 }
73 assert(q->Id == queryId);
74
75 switch (mode) {
76 case GL_QUERY_WAIT:
77 case GL_QUERY_NO_WAIT:
78 case GL_QUERY_BY_REGION_WAIT:
79 case GL_QUERY_BY_REGION_NO_WAIT:
80 break; /* OK */
81 case GL_QUERY_WAIT_INVERTED:
82 case GL_QUERY_NO_WAIT_INVERTED:
83 case GL_QUERY_BY_REGION_WAIT_INVERTED:
84 case GL_QUERY_BY_REGION_NO_WAIT_INVERTED:
85 if (ctx->Extensions.ARB_conditional_render_inverted)
86 break; /* OK */
87 /* fallthrough - invalid */
88 default:
89 _mesa_error(ctx, GL_INVALID_ENUM, "glBeginConditionalRender(mode=%s)",
90 _mesa_enum_to_string(mode));
91 return;
92 }
93
94 /* Section 2.14 (Conditional Rendering) of the OpenGL 3.0 spec says:
95 *
96 * "The error INVALID_OPERATION is generated if <id> is the name of a
97 * query object with a target other than SAMPLES_PASSED, or <id> is the
98 * name of a query currently in progress."
99 */
100 if ((q->Target != GL_SAMPLES_PASSED &&
101 q->Target != GL_ANY_SAMPLES_PASSED &&
102 q->Target != GL_ANY_SAMPLES_PASSED_CONSERVATIVE &&
103 q->Target != GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW_ARB &&
104 q->Target != GL_TRANSFORM_FEEDBACK_OVERFLOW_ARB) || q->Active) {
105 _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginConditionalRender()");
106 return;
107 }
108
109 ctx->Query.CondRenderQuery = q;
110 ctx->Query.CondRenderMode = mode;
111
112 if (ctx->Driver.BeginConditionalRender)
113 ctx->Driver.BeginConditionalRender(ctx, q, mode);
114 }
115
116
117 void APIENTRY
118 _mesa_EndConditionalRender(void)
119 {
120 GET_CURRENT_CONTEXT(ctx);
121
122 FLUSH_VERTICES(ctx, 0x0);
123
124 if (!ctx->Extensions.NV_conditional_render || !ctx->Query.CondRenderQuery) {
125 _mesa_error(ctx, GL_INVALID_OPERATION, "glEndConditionalRender()");
126 return;
127 }
128
129 if (ctx->Driver.EndConditionalRender)
130 ctx->Driver.EndConditionalRender(ctx, ctx->Query.CondRenderQuery);
131
132 ctx->Query.CondRenderQuery = NULL;
133 ctx->Query.CondRenderMode = GL_NONE;
134 }
135
136
137 /**
138 * This function is called by software rendering commands (all point,
139 * line triangle drawing, glClear, glDrawPixels, glCopyPixels, and
140 * glBitmap, glBlitFramebuffer) to determine if subsequent drawing
141 * commands should be
142 * executed or discarded depending on the current conditional
143 * rendering state. Ideally, this check would be implemented by the
144 * GPU when doing hardware rendering. XXX should this function be
145 * called via a new driver hook?
146 *
147 * \return GL_TRUE if we should render, GL_FALSE if we should discard
148 */
149 GLboolean
150 _mesa_check_conditional_render(struct gl_context *ctx)
151 {
152 struct gl_query_object *q = ctx->Query.CondRenderQuery;
153
154 if (!q) {
155 /* no query in progress - draw normally */
156 return GL_TRUE;
157 }
158
159 switch (ctx->Query.CondRenderMode) {
160 case GL_QUERY_BY_REGION_WAIT:
161 /* fall-through */
162 case GL_QUERY_WAIT:
163 if (!q->Ready) {
164 ctx->Driver.WaitQuery(ctx, q);
165 }
166 return q->Result > 0;
167 case GL_QUERY_BY_REGION_WAIT_INVERTED:
168 /* fall-through */
169 case GL_QUERY_WAIT_INVERTED:
170 if (!q->Ready) {
171 ctx->Driver.WaitQuery(ctx, q);
172 }
173 return q->Result == 0;
174 case GL_QUERY_BY_REGION_NO_WAIT:
175 /* fall-through */
176 case GL_QUERY_NO_WAIT:
177 if (!q->Ready)
178 ctx->Driver.CheckQuery(ctx, q);
179 return q->Ready ? (q->Result > 0) : GL_TRUE;
180 case GL_QUERY_BY_REGION_NO_WAIT_INVERTED:
181 /* fall-through */
182 case GL_QUERY_NO_WAIT_INVERTED:
183 if (!q->Ready)
184 ctx->Driver.CheckQuery(ctx, q);
185 return q->Ready ? (q->Result == 0) : GL_TRUE;
186 default:
187 _mesa_problem(ctx, "Bad cond render mode %s in "
188 " _mesa_check_conditional_render()",
189 _mesa_enum_to_string(ctx->Query.CondRenderMode));
190 return GL_TRUE;
191 }
192 }