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