1cdc920a0Smrg/*
2cdc920a0Smrg * Mesa 3-D graphics library
3cdc920a0Smrg *
4cdc920a0Smrg * Copyright (C) 2009  VMware, Inc.   All Rights Reserved.
5cdc920a0Smrg *
6cdc920a0Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7cdc920a0Smrg * copy of this software and associated documentation files (the "Software"),
8cdc920a0Smrg * to deal in the Software without restriction, including without limitation
9cdc920a0Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10cdc920a0Smrg * and/or sell copies of the Software, and to permit persons to whom the
11cdc920a0Smrg * Software is furnished to do so, subject to the following conditions:
12cdc920a0Smrg *
13cdc920a0Smrg * The above copyright notice and this permission notice (including the next
14cdc920a0Smrg * paragraph) shall be included in all copies or substantial portions of the
15cdc920a0Smrg * Software.
16cdc920a0Smrg *
17cdc920a0Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18cdc920a0Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19cdc920a0Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20cdc920a0Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21cdc920a0Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22cdc920a0Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23cdc920a0Smrg * DEALINGS IN THE SOFTWARE.
24cdc920a0Smrg */
25cdc920a0Smrg
26cdc920a0Smrg/**
27cdc920a0Smrg * \file condrender.c
28cdc920a0Smrg * Conditional rendering functions
29cdc920a0Smrg *
30cdc920a0Smrg * \author Brian Paul
31cdc920a0Smrg */
32cdc920a0Smrg
33cdc920a0Smrg#include "glheader.h"
34cdc920a0Smrg#include "condrender.h"
35cdc920a0Smrg#include "enums.h"
363464ebd5Sriastradh#include "mtypes.h"
37cdc920a0Smrg#include "queryobj.h"
38cdc920a0Smrg
39cdc920a0Smrg
4001e04c3fSmrgstatic ALWAYS_INLINE void
4101e04c3fSmrgbegin_conditional_render(struct gl_context *ctx, GLuint queryId, GLenum mode,
4201e04c3fSmrg                         bool no_error)
4301e04c3fSmrg{
4401e04c3fSmrg   struct gl_query_object *q = NULL;
4501e04c3fSmrg
4601e04c3fSmrg   assert(ctx->Query.CondRenderMode == GL_NONE);
4701e04c3fSmrg
4801e04c3fSmrg   if (queryId != 0)
4901e04c3fSmrg      q = _mesa_lookup_query_object(ctx, queryId);
5001e04c3fSmrg
5101e04c3fSmrg   if (!no_error) {
5201e04c3fSmrg      /* Section 2.14 (Conditional Rendering) of the OpenGL 3.0 spec says:
5301e04c3fSmrg       *
5401e04c3fSmrg       *     "The error INVALID_VALUE is generated if <id> is not the name of an
5501e04c3fSmrg       *     existing query object query."
5601e04c3fSmrg       */
5701e04c3fSmrg      if (!q) {
5801e04c3fSmrg         _mesa_error(ctx, GL_INVALID_VALUE,
5901e04c3fSmrg                     "glBeginConditionalRender(bad queryId=%u)", queryId);
6001e04c3fSmrg         return;
6101e04c3fSmrg      }
6201e04c3fSmrg      assert(q->Id == queryId);
6301e04c3fSmrg
6401e04c3fSmrg      switch (mode) {
6501e04c3fSmrg      case GL_QUERY_WAIT:
6601e04c3fSmrg      case GL_QUERY_NO_WAIT:
6701e04c3fSmrg      case GL_QUERY_BY_REGION_WAIT:
6801e04c3fSmrg      case GL_QUERY_BY_REGION_NO_WAIT:
6901e04c3fSmrg         break; /* OK */
7001e04c3fSmrg      case GL_QUERY_WAIT_INVERTED:
7101e04c3fSmrg      case GL_QUERY_NO_WAIT_INVERTED:
7201e04c3fSmrg      case GL_QUERY_BY_REGION_WAIT_INVERTED:
7301e04c3fSmrg      case GL_QUERY_BY_REGION_NO_WAIT_INVERTED:
7401e04c3fSmrg         if (ctx->Extensions.ARB_conditional_render_inverted)
7501e04c3fSmrg            break; /* OK */
767ec681f3Smrg         FALLTHROUGH;
7701e04c3fSmrg      default:
7801e04c3fSmrg         _mesa_error(ctx, GL_INVALID_ENUM, "glBeginConditionalRender(mode=%s)",
7901e04c3fSmrg                     _mesa_enum_to_string(mode));
8001e04c3fSmrg         return;
8101e04c3fSmrg      }
8201e04c3fSmrg
8301e04c3fSmrg      /* Section 2.14 (Conditional Rendering) of the OpenGL 3.0 spec says:
8401e04c3fSmrg       *
8501e04c3fSmrg       *     "The error INVALID_OPERATION is generated if <id> is the name of a
8601e04c3fSmrg       *     query object with a target other than SAMPLES_PASSED, or <id> is
8701e04c3fSmrg       *     the name of a query currently in progress."
8801e04c3fSmrg       */
8901e04c3fSmrg      if ((q->Target != GL_SAMPLES_PASSED &&
9001e04c3fSmrg           q->Target != GL_ANY_SAMPLES_PASSED &&
9101e04c3fSmrg           q->Target != GL_ANY_SAMPLES_PASSED_CONSERVATIVE &&
9201e04c3fSmrg           q->Target != GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW_ARB &&
9301e04c3fSmrg           q->Target != GL_TRANSFORM_FEEDBACK_OVERFLOW_ARB) || q->Active) {
9401e04c3fSmrg         _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginConditionalRender()");
9501e04c3fSmrg         return;
9601e04c3fSmrg      }
9701e04c3fSmrg   }
9801e04c3fSmrg
9901e04c3fSmrg   ctx->Query.CondRenderQuery = q;
10001e04c3fSmrg   ctx->Query.CondRenderMode = mode;
10101e04c3fSmrg
10201e04c3fSmrg   if (ctx->Driver.BeginConditionalRender)
10301e04c3fSmrg      ctx->Driver.BeginConditionalRender(ctx, q, mode);
10401e04c3fSmrg}
10501e04c3fSmrg
10601e04c3fSmrg
10701e04c3fSmrgvoid GLAPIENTRY
10801e04c3fSmrg_mesa_BeginConditionalRender_no_error(GLuint queryId, GLenum mode)
10901e04c3fSmrg{
11001e04c3fSmrg   GET_CURRENT_CONTEXT(ctx);
11101e04c3fSmrg   begin_conditional_render(ctx, queryId, mode, true);
11201e04c3fSmrg}
11301e04c3fSmrg
11401e04c3fSmrg
115cdc920a0Smrgvoid GLAPIENTRY
116cdc920a0Smrg_mesa_BeginConditionalRender(GLuint queryId, GLenum mode)
117cdc920a0Smrg{
118cdc920a0Smrg   GET_CURRENT_CONTEXT(ctx);
119cdc920a0Smrg
120af69d88dSmrg   /* Section 2.14 (Conditional Rendering) of the OpenGL 3.0 spec says:
121af69d88dSmrg    *
122af69d88dSmrg    *     "If BeginConditionalRender is called while conditional rendering is
123af69d88dSmrg    *     in progress, or if EndConditionalRender is called while conditional
124af69d88dSmrg    *     rendering is not in progress, the error INVALID_OPERATION is
125af69d88dSmrg    *     generated."
126af69d88dSmrg    */
127af69d88dSmrg   if (!ctx->Extensions.NV_conditional_render || ctx->Query.CondRenderQuery) {
128cdc920a0Smrg      _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginConditionalRender()");
129cdc920a0Smrg      return;
130cdc920a0Smrg   }
131cdc920a0Smrg
13201e04c3fSmrg   begin_conditional_render(ctx, queryId, mode, false);
13301e04c3fSmrg}
134cdc920a0Smrg
135af69d88dSmrg
13601e04c3fSmrgstatic void
13701e04c3fSmrgend_conditional_render(struct gl_context *ctx)
13801e04c3fSmrg{
1397ec681f3Smrg   FLUSH_VERTICES(ctx, 0, 0);
140af69d88dSmrg
14101e04c3fSmrg   if (ctx->Driver.EndConditionalRender)
14201e04c3fSmrg      ctx->Driver.EndConditionalRender(ctx, ctx->Query.CondRenderQuery);
143cdc920a0Smrg
14401e04c3fSmrg   ctx->Query.CondRenderQuery = NULL;
14501e04c3fSmrg   ctx->Query.CondRenderMode = GL_NONE;
14601e04c3fSmrg}
147cdc920a0Smrg
148cdc920a0Smrg
14901e04c3fSmrgvoid APIENTRY
15001e04c3fSmrg_mesa_EndConditionalRender_no_error(void)
15101e04c3fSmrg{
15201e04c3fSmrg   GET_CURRENT_CONTEXT(ctx);
15301e04c3fSmrg   end_conditional_render(ctx);
154cdc920a0Smrg}
155cdc920a0Smrg
156cdc920a0Smrg
157cdc920a0Smrgvoid APIENTRY
158cdc920a0Smrg_mesa_EndConditionalRender(void)
159cdc920a0Smrg{
160cdc920a0Smrg   GET_CURRENT_CONTEXT(ctx);
161cdc920a0Smrg
162cdc920a0Smrg   if (!ctx->Extensions.NV_conditional_render || !ctx->Query.CondRenderQuery) {
163cdc920a0Smrg      _mesa_error(ctx, GL_INVALID_OPERATION, "glEndConditionalRender()");
164cdc920a0Smrg      return;
165cdc920a0Smrg   }
166cdc920a0Smrg
16701e04c3fSmrg   end_conditional_render(ctx);
168cdc920a0Smrg}
169cdc920a0Smrg
170cdc920a0Smrg
171cdc920a0Smrg/**
172cdc920a0Smrg * This function is called by software rendering commands (all point,
173cdc920a0Smrg * line triangle drawing, glClear, glDrawPixels, glCopyPixels, and
174cdc920a0Smrg * glBitmap, glBlitFramebuffer) to determine if subsequent drawing
175cdc920a0Smrg * commands should be
176cdc920a0Smrg * executed or discarded depending on the current conditional
177cdc920a0Smrg * rendering state.  Ideally, this check would be implemented by the
178cdc920a0Smrg * GPU when doing hardware rendering.  XXX should this function be
179cdc920a0Smrg * called via a new driver hook?
180cdc920a0Smrg *
181cdc920a0Smrg * \return GL_TRUE if we should render, GL_FALSE if we should discard
182cdc920a0Smrg */
183cdc920a0SmrgGLboolean
1843464ebd5Sriastradh_mesa_check_conditional_render(struct gl_context *ctx)
185cdc920a0Smrg{
186cdc920a0Smrg   struct gl_query_object *q = ctx->Query.CondRenderQuery;
187cdc920a0Smrg
188cdc920a0Smrg   if (!q) {
189cdc920a0Smrg      /* no query in progress - draw normally */
190cdc920a0Smrg      return GL_TRUE;
191cdc920a0Smrg   }
192cdc920a0Smrg
193cdc920a0Smrg   switch (ctx->Query.CondRenderMode) {
194cdc920a0Smrg   case GL_QUERY_BY_REGION_WAIT:
1957ec681f3Smrg      FALLTHROUGH;
196cdc920a0Smrg   case GL_QUERY_WAIT:
197cdc920a0Smrg      if (!q->Ready) {
198cdc920a0Smrg         ctx->Driver.WaitQuery(ctx, q);
199cdc920a0Smrg      }
200cdc920a0Smrg      return q->Result > 0;
201af69d88dSmrg   case GL_QUERY_BY_REGION_WAIT_INVERTED:
2027ec681f3Smrg      FALLTHROUGH;
203af69d88dSmrg   case GL_QUERY_WAIT_INVERTED:
204af69d88dSmrg      if (!q->Ready) {
205af69d88dSmrg         ctx->Driver.WaitQuery(ctx, q);
206af69d88dSmrg      }
207af69d88dSmrg      return q->Result == 0;
208cdc920a0Smrg   case GL_QUERY_BY_REGION_NO_WAIT:
2097ec681f3Smrg      FALLTHROUGH;
210cdc920a0Smrg   case GL_QUERY_NO_WAIT:
211af69d88dSmrg      if (!q->Ready)
212af69d88dSmrg         ctx->Driver.CheckQuery(ctx, q);
213cdc920a0Smrg      return q->Ready ? (q->Result > 0) : GL_TRUE;
214af69d88dSmrg   case GL_QUERY_BY_REGION_NO_WAIT_INVERTED:
2157ec681f3Smrg      FALLTHROUGH;
216af69d88dSmrg   case GL_QUERY_NO_WAIT_INVERTED:
217af69d88dSmrg      if (!q->Ready)
218af69d88dSmrg         ctx->Driver.CheckQuery(ctx, q);
219af69d88dSmrg      return q->Ready ? (q->Result == 0) : GL_TRUE;
220cdc920a0Smrg   default:
221cdc920a0Smrg      _mesa_problem(ctx, "Bad cond render mode %s in "
222cdc920a0Smrg                    " _mesa_check_conditional_render()",
22301e04c3fSmrg                    _mesa_enum_to_string(ctx->Query.CondRenderMode));
224cdc920a0Smrg      return GL_TRUE;
225cdc920a0Smrg   }
226cdc920a0Smrg}
227