condrender.c revision cdc920a0
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"
37cdc920a0Smrg#include "queryobj.h"
38cdc920a0Smrg
39cdc920a0Smrg
40cdc920a0Smrgvoid GLAPIENTRY
41cdc920a0Smrg_mesa_BeginConditionalRender(GLuint queryId, GLenum mode)
42cdc920a0Smrg{
43cdc920a0Smrg   struct gl_query_object *q;
44cdc920a0Smrg   GET_CURRENT_CONTEXT(ctx);
45cdc920a0Smrg
46cdc920a0Smrg   if (!ctx->Extensions.NV_conditional_render || ctx->Query.CondRenderQuery) {
47cdc920a0Smrg      _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginConditionalRender()");
48cdc920a0Smrg      return;
49cdc920a0Smrg   }
50cdc920a0Smrg
51cdc920a0Smrg   ASSERT(ctx->Query.CondRenderMode == GL_NONE);
52cdc920a0Smrg
53cdc920a0Smrg   switch (mode) {
54cdc920a0Smrg   case GL_QUERY_WAIT:
55cdc920a0Smrg   case GL_QUERY_NO_WAIT:
56cdc920a0Smrg   case GL_QUERY_BY_REGION_WAIT:
57cdc920a0Smrg   case GL_QUERY_BY_REGION_NO_WAIT:
58cdc920a0Smrg      /* OK */
59cdc920a0Smrg      break;
60cdc920a0Smrg   default:
61cdc920a0Smrg      _mesa_error(ctx, GL_INVALID_ENUM, "glBeginConditionalRender(mode=%s)",
62cdc920a0Smrg                  _mesa_lookup_enum_by_nr(mode));
63cdc920a0Smrg      return;
64cdc920a0Smrg   }
65cdc920a0Smrg
66cdc920a0Smrg   q = _mesa_lookup_query_object(ctx, queryId);
67cdc920a0Smrg   if (!q) {
68cdc920a0Smrg      _mesa_error(ctx, GL_INVALID_VALUE,
69cdc920a0Smrg                  "glBeginConditionalRender(bad queryId=%u)", queryId);
70cdc920a0Smrg      return;
71cdc920a0Smrg   }
72cdc920a0Smrg   ASSERT(q->Id == queryId);
73cdc920a0Smrg
74cdc920a0Smrg   if (q->Target != GL_SAMPLES_PASSED) {
75cdc920a0Smrg      _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginConditionalRender()");
76cdc920a0Smrg      return;
77cdc920a0Smrg   }
78cdc920a0Smrg
79cdc920a0Smrg   ctx->Query.CondRenderQuery = q;
80cdc920a0Smrg   ctx->Query.CondRenderMode = mode;
81cdc920a0Smrg
82cdc920a0Smrg   if (ctx->Driver.BeginConditionalRender)
83cdc920a0Smrg      ctx->Driver.BeginConditionalRender(ctx, q, mode);
84cdc920a0Smrg}
85cdc920a0Smrg
86cdc920a0Smrg
87cdc920a0Smrgvoid APIENTRY
88cdc920a0Smrg_mesa_EndConditionalRender(void)
89cdc920a0Smrg{
90cdc920a0Smrg   GET_CURRENT_CONTEXT(ctx);
91cdc920a0Smrg
92cdc920a0Smrg   FLUSH_VERTICES(ctx, 0x0);
93cdc920a0Smrg
94cdc920a0Smrg   if (!ctx->Extensions.NV_conditional_render || !ctx->Query.CondRenderQuery) {
95cdc920a0Smrg      _mesa_error(ctx, GL_INVALID_OPERATION, "glEndConditionalRender()");
96cdc920a0Smrg      return;
97cdc920a0Smrg   }
98cdc920a0Smrg
99cdc920a0Smrg   if (ctx->Driver.EndConditionalRender)
100cdc920a0Smrg      ctx->Driver.EndConditionalRender(ctx, ctx->Query.CondRenderQuery);
101cdc920a0Smrg
102cdc920a0Smrg   ctx->Query.CondRenderQuery = NULL;
103cdc920a0Smrg   ctx->Query.CondRenderMode = GL_NONE;
104cdc920a0Smrg}
105cdc920a0Smrg
106cdc920a0Smrg
107cdc920a0Smrg/**
108cdc920a0Smrg * This function is called by software rendering commands (all point,
109cdc920a0Smrg * line triangle drawing, glClear, glDrawPixels, glCopyPixels, and
110cdc920a0Smrg * glBitmap, glBlitFramebuffer) to determine if subsequent drawing
111cdc920a0Smrg * commands should be
112cdc920a0Smrg * executed or discarded depending on the current conditional
113cdc920a0Smrg * rendering state.  Ideally, this check would be implemented by the
114cdc920a0Smrg * GPU when doing hardware rendering.  XXX should this function be
115cdc920a0Smrg * called via a new driver hook?
116cdc920a0Smrg *
117cdc920a0Smrg * \return GL_TRUE if we should render, GL_FALSE if we should discard
118cdc920a0Smrg */
119cdc920a0SmrgGLboolean
120cdc920a0Smrg_mesa_check_conditional_render(GLcontext *ctx)
121cdc920a0Smrg{
122cdc920a0Smrg   struct gl_query_object *q = ctx->Query.CondRenderQuery;
123cdc920a0Smrg
124cdc920a0Smrg   if (!q) {
125cdc920a0Smrg      /* no query in progress - draw normally */
126cdc920a0Smrg      return GL_TRUE;
127cdc920a0Smrg   }
128cdc920a0Smrg
129cdc920a0Smrg   switch (ctx->Query.CondRenderMode) {
130cdc920a0Smrg   case GL_QUERY_BY_REGION_WAIT:
131cdc920a0Smrg      /* fall-through */
132cdc920a0Smrg   case GL_QUERY_WAIT:
133cdc920a0Smrg      if (!q->Ready) {
134cdc920a0Smrg         ctx->Driver.WaitQuery(ctx, q);
135cdc920a0Smrg      }
136cdc920a0Smrg      return q->Result > 0;
137cdc920a0Smrg   case GL_QUERY_BY_REGION_NO_WAIT:
138cdc920a0Smrg      /* fall-through */
139cdc920a0Smrg   case GL_QUERY_NO_WAIT:
140cdc920a0Smrg      return q->Ready ? (q->Result > 0) : GL_TRUE;
141cdc920a0Smrg   default:
142cdc920a0Smrg      _mesa_problem(ctx, "Bad cond render mode %s in "
143cdc920a0Smrg                    " _mesa_check_conditional_render()",
144cdc920a0Smrg                    _mesa_lookup_enum_by_nr(ctx->Query.CondRenderMode));
145cdc920a0Smrg      return GL_TRUE;
146cdc920a0Smrg   }
147cdc920a0Smrg}
148