1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2007 Brian Paul 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 shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <stdbool.h>
26 #include "glheader.h"
27 #include "draw_validate.h"
28 #include "arrayobj.h"
29 #include "bufferobj.h"
30 #include "context.h"
31
32 #include "mtypes.h"
33 #include "pipelineobj.h"
34 #include "enums.h"
35 #include "state.h"
36 #include "transformfeedback.h"
37 #include "uniforms.h"
38 #include "program/prog_print.h"
39
40
41 /**
42 * Compute the bitmask of allowed primitive types (ValidPrimMask) depending
43 * on shaders and current states. This is used by draw validation.
44 *
45 * If some combinations of shaders and states are invalid, ValidPrimMask is
46 * set to 0, which will always set GL_INVALID_OPERATION in draw calls
47 * except for invalid enums, which will set GL_INVALID_ENUM, minimizing
48 * the number of gl_context variables that have to be read by draw calls.
49 */
50 void
_mesa_update_valid_to_render_state(struct gl_context * ctx)51 _mesa_update_valid_to_render_state(struct gl_context *ctx)
52 {
53 struct gl_pipeline_object *shader = ctx->_Shader;
54 unsigned mask = ctx->SupportedPrimMask;
55 bool drawpix_valid = true;
56
57 if (_mesa_is_no_error_enabled(ctx)) {
58 ctx->ValidPrimMask = mask;
59 ctx->ValidPrimMaskIndexed = mask;
60 ctx->DrawPixValid = drawpix_valid;
61 return;
62 }
63
64 /* Start with an empty mask and set this to the trimmed mask at the end. */
65 ctx->ValidPrimMask = 0;
66 ctx->ValidPrimMaskIndexed = 0;
67 ctx->DrawPixValid = false;
68
69 /* The default error is GL_INVALID_OPERATION if mode is a valid enum.
70 * It can be overriden by following code if we should return a different
71 * error.
72 */
73 ctx->DrawGLError = GL_INVALID_OPERATION;
74
75 if (!ctx->DrawBuffer ||
76 ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
77 ctx->DrawGLError = GL_INVALID_FRAMEBUFFER_OPERATION;
78 return;
79 }
80
81 /* A pipeline object is bound */
82 if (shader->Name && !shader->Validated &&
83 !_mesa_validate_program_pipeline(ctx, shader))
84 return;
85
86 /* If a program is active and SSO not in use, check if validation of
87 * samplers succeeded for the active program. */
88 if (shader->ActiveProgram && shader != ctx->Pipeline.Current &&
89 !_mesa_sampler_uniforms_are_valid(shader->ActiveProgram, NULL, 0))
90 return;
91
92 /* The ARB_blend_func_extended spec's ERRORS section says:
93 *
94 * "The error INVALID_OPERATION is generated by Begin or any procedure
95 * that implicitly calls Begin if any draw buffer has a blend function
96 * requiring the second color input (SRC1_COLOR, ONE_MINUS_SRC1_COLOR,
97 * SRC1_ALPHA or ONE_MINUS_SRC1_ALPHA), and a framebuffer is bound that
98 * has more than the value of MAX_DUAL_SOURCE_DRAW_BUFFERS-1 active
99 * color attachements."
100 */
101 unsigned max_dual_source_buffers = ctx->Const.MaxDualSourceDrawBuffers;
102 unsigned num_color_buffers = ctx->DrawBuffer->_NumColorDrawBuffers;
103
104 if (num_color_buffers > max_dual_source_buffers &&
105 ctx->Color._BlendUsesDualSrc &
106 BITFIELD_RANGE(max_dual_source_buffers,
107 num_color_buffers - max_dual_source_buffers))
108 return;
109
110 if (ctx->Color.BlendEnabled &&
111 ctx->Color._AdvancedBlendMode != BLEND_NONE) {
112 /* The KHR_blend_equation_advanced spec says:
113 *
114 * "If any non-NONE draw buffer uses a blend equation found in table
115 * X.1 or X.2, the error INVALID_OPERATION is generated by Begin or
116 * any operation that implicitly calls Begin (such as DrawElements)
117 * if:
118 *
119 * * the draw buffer for color output zero selects multiple color
120 * buffers (e.g., FRONT_AND_BACK in the default framebuffer); or
121 *
122 * * the draw buffer for any other color output is not NONE."
123 */
124 if (ctx->DrawBuffer->ColorDrawBuffer[0] == GL_FRONT_AND_BACK)
125 return;
126
127 for (unsigned i = 1; i < num_color_buffers; i++) {
128 if (ctx->DrawBuffer->ColorDrawBuffer[i] != GL_NONE)
129 return;
130 }
131
132 /* The KHR_blend_equation_advanced spec says:
133 *
134 * "Advanced blending equations require the use of a fragment shader
135 * with a matching "blend_support" layout qualifier. If the current
136 * blend equation is found in table X.1 or X.2, and the active
137 * fragment shader does not include the layout qualifier matching
138 * the blend equation or "blend_support_all_equations", the error
139 * INVALID_OPERATION is generated [...]"
140 */
141 const struct gl_program *prog =
142 ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
143 const GLbitfield blend_support = !prog ? 0 : prog->info.fs.advanced_blend_modes;
144
145 if ((blend_support & BITFIELD_BIT(ctx->Color._AdvancedBlendMode)) == 0)
146 return;
147 }
148
149 if (ctx->API == API_OPENGL_COMPAT) {
150 if (!shader->CurrentProgram[MESA_SHADER_FRAGMENT]) {
151 if (ctx->FragmentProgram.Enabled &&
152 !_mesa_arb_fragment_program_enabled(ctx))
153 return;
154
155 /* If drawing to integer-valued color buffers, there must be an
156 * active fragment shader (GL_EXT_texture_integer).
157 */
158 if (ctx->DrawBuffer->_IntegerBuffers)
159 return;
160 }
161 }
162
163 /* DrawPixels/CopyPixels/Bitmap is valid after this point. */
164 ctx->DrawPixValid = true;
165
166 /* Section 11.2 (Tessellation) of the ES 3.2 spec says:
167 *
168 * "An INVALID_OPERATION error is generated by any command that
169 * transfers vertices to the GL if the current program state has
170 * one but not both of a tessellation control shader and tessellation
171 * evaluation shader."
172 *
173 * The OpenGL spec argues that this is allowed because a tess ctrl shader
174 * without a tess eval shader can be used with transform feedback.
175 * However, glBeginTransformFeedback doesn't allow GL_PATCHES and
176 * therefore doesn't allow tessellation.
177 *
178 * Further investigation showed that this is indeed a spec bug and
179 * a tess ctrl shader without a tess eval shader shouldn't have been
180 * allowed, because there is no API in GL 4.0 that can make use this
181 * to produce something useful.
182 *
183 * Also, all vendors except one don't support a tess ctrl shader without
184 * a tess eval shader anyway.
185 */
186 if (shader->CurrentProgram[MESA_SHADER_TESS_CTRL] &&
187 !shader->CurrentProgram[MESA_SHADER_TESS_EVAL])
188 return;
189
190 switch (ctx->API) {
191 case API_OPENGLES2:
192 /* Section 11.2 (Tessellation) of the ES 3.2 spec says:
193 *
194 * "An INVALID_OPERATION error is generated by any command that
195 * transfers vertices to the GL if the current program state has
196 * one but not both of a tessellation control shader and tessellation
197 * evaluation shader."
198 */
199 if (_mesa_is_gles3(ctx) &&
200 shader->CurrentProgram[MESA_SHADER_TESS_EVAL] &&
201 !shader->CurrentProgram[MESA_SHADER_TESS_CTRL])
202 return;
203
204 /* From GL_EXT_color_buffer_float:
205 *
206 * "Blending applies only if the color buffer has a fixed-point or
207 * or floating-point format. If the color buffer has an integer
208 * format, proceed to the next operation. Furthermore, an
209 * INVALID_OPERATION error is generated by DrawArrays and the other
210 * drawing commands defined in section 2.8.3 (10.5 in ES 3.1) if
211 * blending is enabled (see below) and any draw buffer has 32-bit
212 * floating-point format components."
213 *
214 * However GL_EXT_float_blend removes this text.
215 */
216 if (!ctx->Extensions.EXT_float_blend &&
217 (ctx->DrawBuffer->_FP32Buffers & ctx->Color.BlendEnabled))
218 return;
219 break;
220
221 case API_OPENGL_CORE:
222 /* Section 10.4 (Drawing Commands Using Vertex Arrays) of the OpenGL 4.5
223 * Core Profile spec says:
224 *
225 * "An INVALID_OPERATION error is generated if no vertex array
226 * object is bound (see section 10.3.1)."
227 */
228 if (ctx->Array.VAO == ctx->Array.DefaultVAO)
229 return;
230 break;
231
232 case API_OPENGLES:
233 break;
234
235 case API_OPENGL_COMPAT:
236 /* Check invalid ARB vertex programs. */
237 if (!shader->CurrentProgram[MESA_SHADER_VERTEX] &&
238 ctx->VertexProgram.Enabled &&
239 !_mesa_arb_vertex_program_enabled(ctx))
240 return;
241 break;
242
243 default:
244 unreachable("Invalid API value in _mesa_update_valid_to_render_state");
245 }
246
247 /* From the GL_NV_fill_rectangle spec:
248 *
249 * "An INVALID_OPERATION error is generated by Begin or any Draw command if
250 * only one of the front and back polygon mode is FILL_RECTANGLE_NV."
251 */
252 if ((ctx->Polygon.FrontMode == GL_FILL_RECTANGLE_NV) !=
253 (ctx->Polygon.BackMode == GL_FILL_RECTANGLE_NV))
254 return;
255
256 /* From GL_INTEL_conservative_rasterization spec:
257 *
258 * The conservative rasterization option applies only to polygons with
259 * PolygonMode state set to FILL. Draw requests for polygons with different
260 * PolygonMode setting or for other primitive types (points/lines) generate
261 * INVALID_OPERATION error.
262 */
263 if (ctx->IntelConservativeRasterization) {
264 if (ctx->Polygon.FrontMode != GL_FILL ||
265 ctx->Polygon.BackMode != GL_FILL) {
266 return;
267 } else {
268 mask &= (1 << GL_TRIANGLES) |
269 (1 << GL_TRIANGLE_STRIP) |
270 (1 << GL_TRIANGLE_FAN) |
271 (1 << GL_QUADS) |
272 (1 << GL_QUAD_STRIP) |
273 (1 << GL_POLYGON) |
274 (1 << GL_TRIANGLES_ADJACENCY) |
275 (1 << GL_TRIANGLE_STRIP_ADJACENCY);
276 }
277 }
278
279 /* From the GL_EXT_transform_feedback spec:
280 *
281 * "The error INVALID_OPERATION is generated if Begin, or any command
282 * that performs an explicit Begin, is called when:
283 *
284 * * a geometry shader is not active and <mode> does not match the
285 * allowed begin modes for the current transform feedback state as
286 * given by table X.1.
287 *
288 * * a geometry shader is active and the output primitive type of the
289 * geometry shader does not match the allowed begin modes for the
290 * current transform feedback state as given by table X.1.
291 *
292 */
293 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
294 if(shader->CurrentProgram[MESA_SHADER_GEOMETRY]) {
295 switch (shader->CurrentProgram[MESA_SHADER_GEOMETRY]->
296 info.gs.output_primitive) {
297 case GL_POINTS:
298 if (ctx->TransformFeedback.Mode != GL_POINTS)
299 mask = 0;
300 break;
301 case GL_LINE_STRIP:
302 if (ctx->TransformFeedback.Mode != GL_LINES)
303 mask = 0;
304 break;
305 case GL_TRIANGLE_STRIP:
306 if (ctx->TransformFeedback.Mode != GL_TRIANGLES)
307 mask = 0;
308 break;
309 default:
310 mask = 0;
311 }
312 }
313 else if (shader->CurrentProgram[MESA_SHADER_TESS_EVAL]) {
314 struct gl_program *tes =
315 shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
316 if (tes->info.tess.point_mode) {
317 if (ctx->TransformFeedback.Mode != GL_POINTS)
318 mask = 0;
319 } else if (tes->info.tess._primitive_mode == TESS_PRIMITIVE_ISOLINES) {
320 if (ctx->TransformFeedback.Mode != GL_LINES)
321 mask = 0;
322 } else {
323 if (ctx->TransformFeedback.Mode != GL_TRIANGLES)
324 mask = 0;
325 }
326 }
327 else {
328 switch (ctx->TransformFeedback.Mode) {
329 case GL_POINTS:
330 mask &= 1 << GL_POINTS;
331 break;
332 case GL_LINES:
333 mask &= (1 << GL_LINES) |
334 (1 << GL_LINE_LOOP) |
335 (1 << GL_LINE_STRIP);
336 break;
337 case GL_TRIANGLES:
338 /* TODO: This doesn't look right, but it matches the original code. */
339 mask &= ~((1 << GL_POINTS) |
340 (1 << GL_LINES) |
341 (1 << GL_LINE_LOOP) |
342 (1 << GL_LINE_STRIP));
343 break;
344 }
345 }
346
347 if (!mask)
348 return;
349 }
350
351 /* From the OpenGL 4.5 specification, section 11.3.1:
352 *
353 * The error INVALID_OPERATION is generated if Begin, or any command that
354 * implicitly calls Begin, is called when a geometry shader is active and:
355 *
356 * * the input primitive type of the current geometry shader is
357 * POINTS and <mode> is not POINTS,
358 *
359 * * the input primitive type of the current geometry shader is
360 * LINES and <mode> is not LINES, LINE_STRIP, or LINE_LOOP,
361 *
362 * * the input primitive type of the current geometry shader is
363 * TRIANGLES and <mode> is not TRIANGLES, TRIANGLE_STRIP or
364 * TRIANGLE_FAN,
365 *
366 * * the input primitive type of the current geometry shader is
367 * LINES_ADJACENCY_ARB and <mode> is not LINES_ADJACENCY_ARB or
368 * LINE_STRIP_ADJACENCY_ARB, or
369 *
370 * * the input primitive type of the current geometry shader is
371 * TRIANGLES_ADJACENCY_ARB and <mode> is not
372 * TRIANGLES_ADJACENCY_ARB or TRIANGLE_STRIP_ADJACENCY_ARB.
373 *
374 * The GL spec doesn't mention any interaction with tessellation, which
375 * is clearly a spec bug. The same rule should apply, but instead of
376 * the draw primitive mode, the tessellation evaluation shader primitive
377 * mode should be used for the checking.
378 */
379 if (shader->CurrentProgram[MESA_SHADER_GEOMETRY]) {
380 const GLenum geom_mode =
381 shader->CurrentProgram[MESA_SHADER_GEOMETRY]->
382 info.gs.input_primitive;
383 struct gl_program *tes =
384 shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
385
386 if (tes) {
387 bool valid;
388
389 if (tes->info.tess.point_mode)
390 valid = geom_mode == GL_POINTS;
391 else if (tes->info.tess._primitive_mode == TESS_PRIMITIVE_ISOLINES)
392 valid = geom_mode == GL_LINES;
393 else
394 /* the GL_QUADS mode generates triangles too */
395 valid = geom_mode == GL_TRIANGLES;
396
397 /* TES and GS use incompatible primitive types. Discard all draws. */
398 if (!valid)
399 return;
400 } else {
401 switch (geom_mode) {
402 case GL_POINTS:
403 mask &= 1 << GL_POINTS;
404 break;
405 case GL_LINES:
406 mask &= (1 << GL_LINES) |
407 (1 << GL_LINE_LOOP) |
408 (1 << GL_LINE_STRIP);
409 break;
410 case GL_TRIANGLES:
411 mask &= (1 << GL_TRIANGLES) |
412 (1 << GL_TRIANGLE_STRIP) |
413 (1 << GL_TRIANGLE_FAN);
414 break;
415 case GL_LINES_ADJACENCY:
416 mask &= (1 << GL_LINES_ADJACENCY) |
417 (1 << GL_LINE_STRIP_ADJACENCY);
418 break;
419 case GL_TRIANGLES_ADJACENCY:
420 mask &= (1 << GL_TRIANGLES_ADJACENCY) |
421 (1 << GL_TRIANGLE_STRIP_ADJACENCY);
422 break;
423 }
424 }
425 }
426
427 /* From the OpenGL 4.0 (Core Profile) spec (section 2.12):
428 *
429 * "Tessellation operates only on patch primitives. If tessellation is
430 * active, any command that transfers vertices to the GL will
431 * generate an INVALID_OPERATION error if the primitive mode is not
432 * PATCHES.
433 * Patch primitives are not supported by pipeline stages below the
434 * tessellation evaluation shader. If there is no active program
435 * object or the active program object does not contain a tessellation
436 * evaluation shader, the error INVALID_OPERATION is generated by any
437 * command that transfers vertices to the GL if the primitive mode is
438 * PATCHES."
439 *
440 */
441 if (shader->CurrentProgram[MESA_SHADER_TESS_EVAL] ||
442 shader->CurrentProgram[MESA_SHADER_TESS_CTRL]) {
443 mask &= 1 << GL_PATCHES;
444 }
445 else {
446 mask &= ~(1 << GL_PATCHES);
447 }
448
449 #ifdef DEBUG
450 if (shader->Flags & GLSL_LOG) {
451 struct gl_program **prog = shader->CurrentProgram;
452
453 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
454 if (prog[i] == NULL || prog[i]->_Used)
455 continue;
456
457 /* This is the first time this shader is being used.
458 * Append shader's constants/uniforms to log file.
459 *
460 * Only log data for the program target that matches the shader
461 * target. It's possible to have a program bound to the vertex
462 * shader target that also supplied a fragment shader. If that
463 * program isn't also bound to the fragment shader target we don't
464 * want to log its fragment data.
465 */
466 _mesa_append_uniforms_to_file(prog[i]);
467 }
468
469 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
470 if (prog[i] != NULL)
471 prog[i]->_Used = GL_TRUE;
472 }
473 }
474 #endif
475
476 /* Non-indexed draws are valid after this point. */
477 ctx->ValidPrimMask = mask;
478
479 /* Section 2.14.2 (Transform Feedback Primitive Capture) of the OpenGL ES
480 * 3.1 spec says:
481 *
482 * The error INVALID_OPERATION is also generated by DrawElements,
483 * DrawElementsInstanced, and DrawRangeElements while transform feedback
484 * is active and not paused, regardless of mode.
485 *
486 * The OES_geometry_shader_spec says:
487 *
488 * Issues:
489 *
490 * ...
491 *
492 * (13) Does this extension change how transform feedback operates
493 * compared to unextended OpenGL ES 3.0 or 3.1?
494 *
495 * RESOLVED: Yes... Since we no longer require being able to predict how
496 * much geometry will be generated, we also lift the restriction that
497 * only DrawArray* commands are supported and also support the
498 * DrawElements* commands for transform feedback.
499 *
500 * This should also be reflected in the body of the spec, but that appears
501 * to have been overlooked. The body of the spec only explicitly allows
502 * the indirect versions.
503 */
504 if (_mesa_is_gles3(ctx) &&
505 !_mesa_has_OES_geometry_shader(ctx) &&
506 _mesa_is_xfb_active_and_unpaused(ctx))
507 return;
508
509 ctx->ValidPrimMaskIndexed = mask;
510 }
511