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 static ALWAYS_INLINE void
begin_conditional_render(struct gl_context * ctx,GLuint queryId,GLenum mode,bool no_error)41 begin_conditional_render(struct gl_context *ctx, GLuint queryId, GLenum mode,
42                          bool no_error)
43 {
44    struct gl_query_object *q = NULL;
45 
46    assert(ctx->Query.CondRenderMode == GL_NONE);
47 
48    if (queryId != 0)
49       q = _mesa_lookup_query_object(ctx, queryId);
50 
51    if (!no_error) {
52       /* Section 2.14 (Conditional Rendering) of the OpenGL 3.0 spec says:
53        *
54        *     "The error INVALID_VALUE is generated if <id> is not the name of an
55        *     existing query object query."
56        */
57       if (!q) {
58          _mesa_error(ctx, GL_INVALID_VALUE,
59                      "glBeginConditionalRender(bad queryId=%u)", queryId);
60          return;
61       }
62       assert(q->Id == queryId);
63 
64       switch (mode) {
65       case GL_QUERY_WAIT:
66       case GL_QUERY_NO_WAIT:
67       case GL_QUERY_BY_REGION_WAIT:
68       case GL_QUERY_BY_REGION_NO_WAIT:
69          break; /* OK */
70       case GL_QUERY_WAIT_INVERTED:
71       case GL_QUERY_NO_WAIT_INVERTED:
72       case GL_QUERY_BY_REGION_WAIT_INVERTED:
73       case GL_QUERY_BY_REGION_NO_WAIT_INVERTED:
74          if (ctx->Extensions.ARB_conditional_render_inverted)
75             break; /* OK */
76          /* fallthrough - invalid */
77       default:
78          _mesa_error(ctx, GL_INVALID_ENUM, "glBeginConditionalRender(mode=%s)",
79                      _mesa_enum_to_string(mode));
80          return;
81       }
82 
83       /* Section 2.14 (Conditional Rendering) of the OpenGL 3.0 spec says:
84        *
85        *     "The error INVALID_OPERATION is generated if <id> is the name of a
86        *     query object with a target other than SAMPLES_PASSED, or <id> is
87        *     the name of a query currently in progress."
88        */
89       if ((q->Target != GL_SAMPLES_PASSED &&
90            q->Target != GL_ANY_SAMPLES_PASSED &&
91            q->Target != GL_ANY_SAMPLES_PASSED_CONSERVATIVE &&
92            q->Target != GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW_ARB &&
93            q->Target != GL_TRANSFORM_FEEDBACK_OVERFLOW_ARB) || q->Active) {
94          _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginConditionalRender()");
95          return;
96       }
97    }
98 
99    ctx->Query.CondRenderQuery = q;
100    ctx->Query.CondRenderMode = mode;
101 
102    if (ctx->Driver.BeginConditionalRender)
103       ctx->Driver.BeginConditionalRender(ctx, q, mode);
104 }
105 
106 
107 void GLAPIENTRY
_mesa_BeginConditionalRender_no_error(GLuint queryId,GLenum mode)108 _mesa_BeginConditionalRender_no_error(GLuint queryId, GLenum mode)
109 {
110    GET_CURRENT_CONTEXT(ctx);
111    begin_conditional_render(ctx, queryId, mode, true);
112 }
113 
114 
115 void GLAPIENTRY
_mesa_BeginConditionalRender(GLuint queryId,GLenum mode)116 _mesa_BeginConditionalRender(GLuint queryId, GLenum mode)
117 {
118    GET_CURRENT_CONTEXT(ctx);
119 
120    /* Section 2.14 (Conditional Rendering) of the OpenGL 3.0 spec says:
121     *
122     *     "If BeginConditionalRender is called while conditional rendering is
123     *     in progress, or if EndConditionalRender is called while conditional
124     *     rendering is not in progress, the error INVALID_OPERATION is
125     *     generated."
126     */
127    if (!ctx->Extensions.NV_conditional_render || ctx->Query.CondRenderQuery) {
128       _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginConditionalRender()");
129       return;
130    }
131 
132    begin_conditional_render(ctx, queryId, mode, false);
133 }
134 
135 
136 static void
end_conditional_render(struct gl_context * ctx)137 end_conditional_render(struct gl_context *ctx)
138 {
139    FLUSH_VERTICES(ctx, 0x0);
140 
141    if (ctx->Driver.EndConditionalRender)
142       ctx->Driver.EndConditionalRender(ctx, ctx->Query.CondRenderQuery);
143 
144    ctx->Query.CondRenderQuery = NULL;
145    ctx->Query.CondRenderMode = GL_NONE;
146 }
147 
148 
149 void APIENTRY
_mesa_EndConditionalRender_no_error(void)150 _mesa_EndConditionalRender_no_error(void)
151 {
152    GET_CURRENT_CONTEXT(ctx);
153    end_conditional_render(ctx);
154 }
155 
156 
157 void APIENTRY
_mesa_EndConditionalRender(void)158 _mesa_EndConditionalRender(void)
159 {
160    GET_CURRENT_CONTEXT(ctx);
161 
162    if (!ctx->Extensions.NV_conditional_render || !ctx->Query.CondRenderQuery) {
163       _mesa_error(ctx, GL_INVALID_OPERATION, "glEndConditionalRender()");
164       return;
165    }
166 
167    end_conditional_render(ctx);
168 }
169 
170 
171 /**
172  * This function is called by software rendering commands (all point,
173  * line triangle drawing, glClear, glDrawPixels, glCopyPixels, and
174  * glBitmap, glBlitFramebuffer) to determine if subsequent drawing
175  * commands should be
176  * executed or discarded depending on the current conditional
177  * rendering state.  Ideally, this check would be implemented by the
178  * GPU when doing hardware rendering.  XXX should this function be
179  * called via a new driver hook?
180  *
181  * \return GL_TRUE if we should render, GL_FALSE if we should discard
182  */
183 GLboolean
_mesa_check_conditional_render(struct gl_context * ctx)184 _mesa_check_conditional_render(struct gl_context *ctx)
185 {
186    struct gl_query_object *q = ctx->Query.CondRenderQuery;
187 
188    if (!q) {
189       /* no query in progress - draw normally */
190       return GL_TRUE;
191    }
192 
193    switch (ctx->Query.CondRenderMode) {
194    case GL_QUERY_BY_REGION_WAIT:
195       /* fall-through */
196    case GL_QUERY_WAIT:
197       if (!q->Ready) {
198          ctx->Driver.WaitQuery(ctx, q);
199       }
200       return q->Result > 0;
201    case GL_QUERY_BY_REGION_WAIT_INVERTED:
202       /* fall-through */
203    case GL_QUERY_WAIT_INVERTED:
204       if (!q->Ready) {
205          ctx->Driver.WaitQuery(ctx, q);
206       }
207       return q->Result == 0;
208    case GL_QUERY_BY_REGION_NO_WAIT:
209       /* fall-through */
210    case GL_QUERY_NO_WAIT:
211       if (!q->Ready)
212          ctx->Driver.CheckQuery(ctx, q);
213       return q->Ready ? (q->Result > 0) : GL_TRUE;
214    case GL_QUERY_BY_REGION_NO_WAIT_INVERTED:
215       /* fall-through */
216    case GL_QUERY_NO_WAIT_INVERTED:
217       if (!q->Ready)
218          ctx->Driver.CheckQuery(ctx, q);
219       return q->Ready ? (q->Result == 0) : GL_TRUE;
220    default:
221       _mesa_problem(ctx, "Bad cond render mode %s in "
222                     " _mesa_check_conditional_render()",
223                     _mesa_enum_to_string(ctx->Query.CondRenderMode));
224       return GL_TRUE;
225    }
226 }
227