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