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