1/*
2 * Mesa 3-D graphics library
3 * Version:  7.8
4 *
5 * Copyright (C) 2009  VMware, Inc.   All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 */
26
27/**
28 * \file condrender.c
29 * Conditional rendering functions
30 *
31 * \author Brian Paul
32 */
33
34#include "glheader.h"
35#include "condrender.h"
36#include "enums.h"
37#include "mtypes.h"
38#include "queryobj.h"
39
40
41void GLAPIENTRY
42_mesa_BeginConditionalRender(GLuint queryId, GLenum mode)
43{
44   struct gl_query_object *q;
45   GET_CURRENT_CONTEXT(ctx);
46
47   if (!ctx->Extensions.NV_conditional_render || ctx->Query.CondRenderQuery ||
48       queryId == 0) {
49      _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginConditionalRender()");
50      return;
51   }
52
53   ASSERT(ctx->Query.CondRenderMode == GL_NONE);
54
55   switch (mode) {
56   case GL_QUERY_WAIT:
57   case GL_QUERY_NO_WAIT:
58   case GL_QUERY_BY_REGION_WAIT:
59   case GL_QUERY_BY_REGION_NO_WAIT:
60      /* OK */
61      break;
62   default:
63      _mesa_error(ctx, GL_INVALID_ENUM, "glBeginConditionalRender(mode=%s)",
64                  _mesa_lookup_enum_by_nr(mode));
65      return;
66   }
67
68   q = _mesa_lookup_query_object(ctx, queryId);
69   if (!q) {
70      _mesa_error(ctx, GL_INVALID_VALUE,
71                  "glBeginConditionalRender(bad queryId=%u)", queryId);
72      return;
73   }
74   ASSERT(q->Id == queryId);
75
76   if (q->Target != GL_SAMPLES_PASSED || q->Active) {
77      _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginConditionalRender()");
78      return;
79   }
80
81   ctx->Query.CondRenderQuery = q;
82   ctx->Query.CondRenderMode = mode;
83
84   if (ctx->Driver.BeginConditionalRender)
85      ctx->Driver.BeginConditionalRender(ctx, q, mode);
86}
87
88
89void APIENTRY
90_mesa_EndConditionalRender(void)
91{
92   GET_CURRENT_CONTEXT(ctx);
93
94   FLUSH_VERTICES(ctx, 0x0);
95
96   if (!ctx->Extensions.NV_conditional_render || !ctx->Query.CondRenderQuery) {
97      _mesa_error(ctx, GL_INVALID_OPERATION, "glEndConditionalRender()");
98      return;
99   }
100
101   if (ctx->Driver.EndConditionalRender)
102      ctx->Driver.EndConditionalRender(ctx, ctx->Query.CondRenderQuery);
103
104   ctx->Query.CondRenderQuery = NULL;
105   ctx->Query.CondRenderMode = GL_NONE;
106}
107
108
109/**
110 * This function is called by software rendering commands (all point,
111 * line triangle drawing, glClear, glDrawPixels, glCopyPixels, and
112 * glBitmap, glBlitFramebuffer) to determine if subsequent drawing
113 * commands should be
114 * executed or discarded depending on the current conditional
115 * rendering state.  Ideally, this check would be implemented by the
116 * GPU when doing hardware rendering.  XXX should this function be
117 * called via a new driver hook?
118 *
119 * \return GL_TRUE if we should render, GL_FALSE if we should discard
120 */
121GLboolean
122_mesa_check_conditional_render(struct gl_context *ctx)
123{
124   struct gl_query_object *q = ctx->Query.CondRenderQuery;
125
126   if (!q) {
127      /* no query in progress - draw normally */
128      return GL_TRUE;
129   }
130
131   switch (ctx->Query.CondRenderMode) {
132   case GL_QUERY_BY_REGION_WAIT:
133      /* fall-through */
134   case GL_QUERY_WAIT:
135      if (!q->Ready) {
136         ctx->Driver.WaitQuery(ctx, q);
137      }
138      return q->Result > 0;
139   case GL_QUERY_BY_REGION_NO_WAIT:
140      /* fall-through */
141   case GL_QUERY_NO_WAIT:
142      if (!q->Ready)
143         ctx->Driver.CheckQuery(ctx, q);
144      return q->Ready ? (q->Result > 0) : GL_TRUE;
145   default:
146      _mesa_problem(ctx, "Bad cond render mode %s in "
147                    " _mesa_check_conditional_render()",
148                    _mesa_lookup_enum_by_nr(ctx->Query.CondRenderMode));
149      return GL_TRUE;
150   }
151}
152