mesa: Add support for inverted s/w conditional rendering
[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_lookup_enum_by_nr(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) || q->Active) {
103 _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginConditionalRender()");
104 return;
105 }
106
107 ctx->Query.CondRenderQuery = q;
108 ctx->Query.CondRenderMode = mode;
109
110 if (ctx->Driver.BeginConditionalRender)
111 ctx->Driver.BeginConditionalRender(ctx, q, mode);
112 }
113
114
115 void APIENTRY
116 _mesa_EndConditionalRender(void)
117 {
118 GET_CURRENT_CONTEXT(ctx);
119
120 FLUSH_VERTICES(ctx, 0x0);
121
122 if (!ctx->Extensions.NV_conditional_render || !ctx->Query.CondRenderQuery) {
123 _mesa_error(ctx, GL_INVALID_OPERATION, "glEndConditionalRender()");
124 return;
125 }
126
127 if (ctx->Driver.EndConditionalRender)
128 ctx->Driver.EndConditionalRender(ctx, ctx->Query.CondRenderQuery);
129
130 ctx->Query.CondRenderQuery = NULL;
131 ctx->Query.CondRenderMode = GL_NONE;
132 }
133
134
135 /**
136 * This function is called by software rendering commands (all point,
137 * line triangle drawing, glClear, glDrawPixels, glCopyPixels, and
138 * glBitmap, glBlitFramebuffer) to determine if subsequent drawing
139 * commands should be
140 * executed or discarded depending on the current conditional
141 * rendering state. Ideally, this check would be implemented by the
142 * GPU when doing hardware rendering. XXX should this function be
143 * called via a new driver hook?
144 *
145 * \return GL_TRUE if we should render, GL_FALSE if we should discard
146 */
147 GLboolean
148 _mesa_check_conditional_render(struct gl_context *ctx)
149 {
150 struct gl_query_object *q = ctx->Query.CondRenderQuery;
151
152 if (!q) {
153 /* no query in progress - draw normally */
154 return GL_TRUE;
155 }
156
157 switch (ctx->Query.CondRenderMode) {
158 case GL_QUERY_BY_REGION_WAIT:
159 /* fall-through */
160 case GL_QUERY_WAIT:
161 if (!q->Ready) {
162 ctx->Driver.WaitQuery(ctx, q);
163 }
164 return q->Result > 0;
165 case GL_QUERY_BY_REGION_WAIT_INVERTED:
166 /* fall-through */
167 case GL_QUERY_WAIT_INVERTED:
168 if (!q->Ready) {
169 ctx->Driver.WaitQuery(ctx, q);
170 }
171 return q->Result == 0;
172 case GL_QUERY_BY_REGION_NO_WAIT:
173 /* fall-through */
174 case GL_QUERY_NO_WAIT:
175 if (!q->Ready)
176 ctx->Driver.CheckQuery(ctx, q);
177 return q->Ready ? (q->Result > 0) : GL_TRUE;
178 case GL_QUERY_BY_REGION_NO_WAIT_INVERTED:
179 /* fall-through */
180 case GL_QUERY_NO_WAIT_INVERTED:
181 if (!q->Ready)
182 ctx->Driver.CheckQuery(ctx, q);
183 return q->Ready ? (q->Result == 0) : GL_TRUE;
184 default:
185 _mesa_problem(ctx, "Bad cond render mode %s in "
186 " _mesa_check_conditional_render()",
187 _mesa_lookup_enum_by_nr(ctx->Query.CondRenderMode));
188 return GL_TRUE;
189 }
190 }