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