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