xref: /aosp_15_r20/external/mesa3d/src/mesa/main/transformfeedback.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 2010  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 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 
26 /*
27  * Transform feedback support.
28  *
29  * Authors:
30  *   Brian Paul
31  */
32 
33 
34 #include "buffers.h"
35 #include "context.h"
36 #include "draw_validate.h"
37 #include "hash.h"
38 #include "macros.h"
39 #include "mtypes.h"
40 #include "transformfeedback.h"
41 #include "shaderapi.h"
42 #include "shaderobj.h"
43 
44 #include "program/program.h"
45 #include "program/prog_parameter.h"
46 
47 #include "util/u_memory.h"
48 #include "util/u_inlines.h"
49 
50 #include "api_exec_decl.h"
51 
52 #include "cso_cache/cso_context.h"
53 struct using_program_tuple
54 {
55    struct gl_program *prog;
56    bool found;
57 };
58 
59 static void
active_xfb_object_references_program(void * data,void * user_data)60 active_xfb_object_references_program(void *data, void *user_data)
61 {
62    struct using_program_tuple *callback_data = user_data;
63    struct gl_transform_feedback_object *obj = data;
64    if (obj->Active && obj->program == callback_data->prog)
65       callback_data->found = true;
66 }
67 
68 /**
69  * Return true if any active transform feedback object is using a program.
70  */
71 bool
_mesa_transform_feedback_is_using_program(struct gl_context * ctx,struct gl_shader_program * shProg)72 _mesa_transform_feedback_is_using_program(struct gl_context *ctx,
73                                           struct gl_shader_program *shProg)
74 {
75    if (!shProg->last_vert_prog)
76       return false;
77 
78    struct using_program_tuple callback_data;
79    callback_data.found = false;
80    callback_data.prog = shProg->last_vert_prog;
81 
82    _mesa_HashWalkLocked(&ctx->TransformFeedback.Objects,
83                         active_xfb_object_references_program, &callback_data);
84 
85    /* Also check DefaultObject, as it's not in the Objects hash table. */
86    active_xfb_object_references_program(ctx->TransformFeedback.DefaultObject,
87                                         &callback_data);
88 
89    return callback_data.found;
90 }
91 
92 static struct gl_transform_feedback_object *
new_transform_feedback(struct gl_context * ctx,GLuint name)93 new_transform_feedback(struct gl_context *ctx, GLuint name)
94 {
95    struct gl_transform_feedback_object *obj;
96 
97    obj = CALLOC_STRUCT(gl_transform_feedback_object);
98    if (!obj)
99       return NULL;
100 
101    obj->Name = name;
102    obj->RefCount = 1;
103    obj->EverBound = GL_FALSE;
104 
105    return obj;
106 }
107 
108 static void
delete_transform_feedback(struct gl_context * ctx,struct gl_transform_feedback_object * obj)109 delete_transform_feedback(struct gl_context *ctx,
110                              struct gl_transform_feedback_object *obj)
111 {
112    unsigned i;
113 
114    for (i = 0; i < ARRAY_SIZE(obj->draw_count); i++)
115       pipe_so_target_reference(&obj->draw_count[i], NULL);
116 
117    /* Unreference targets. */
118    for (i = 0; i < obj->num_targets; i++) {
119       pipe_so_target_reference(&obj->targets[i], NULL);
120    }
121 
122    for (unsigned i = 0; i < ARRAY_SIZE(obj->Buffers); i++) {
123       _mesa_reference_buffer_object(ctx, &obj->Buffers[i], NULL);
124    }
125 
126    free(obj->Label);
127    FREE(obj);
128 }
129 
130 /**
131  * Do reference counting of transform feedback buffers.
132  */
133 static void
reference_transform_feedback_object(struct gl_transform_feedback_object ** ptr,struct gl_transform_feedback_object * obj)134 reference_transform_feedback_object(struct gl_transform_feedback_object **ptr,
135                                     struct gl_transform_feedback_object *obj)
136 {
137    if (*ptr == obj)
138       return;
139 
140    if (*ptr) {
141       /* Unreference the old object */
142       struct gl_transform_feedback_object *oldObj = *ptr;
143 
144       assert(oldObj->RefCount > 0);
145       oldObj->RefCount--;
146 
147       if (oldObj->RefCount == 0) {
148          GET_CURRENT_CONTEXT(ctx);
149          if (ctx)
150             delete_transform_feedback(ctx, oldObj);
151       }
152 
153       *ptr = NULL;
154    }
155    assert(!*ptr);
156 
157    if (obj) {
158       assert(obj->RefCount > 0);
159 
160       /* reference new object */
161       obj->RefCount++;
162       obj->EverBound = GL_TRUE;
163       *ptr = obj;
164    }
165 }
166 
167 
168 /**
169  * Per-context init for transform feedback.
170  */
171 void
_mesa_init_transform_feedback(struct gl_context * ctx)172 _mesa_init_transform_feedback(struct gl_context *ctx)
173 {
174    /* core mesa expects this, even a dummy one, to be available */
175    ctx->TransformFeedback.DefaultObject =
176       new_transform_feedback(ctx, 0);
177 
178    assert(ctx->TransformFeedback.DefaultObject->RefCount == 1);
179 
180    reference_transform_feedback_object(&ctx->TransformFeedback.CurrentObject,
181                                        ctx->TransformFeedback.DefaultObject);
182 
183    assert(ctx->TransformFeedback.DefaultObject->RefCount == 2);
184 
185    _mesa_InitHashTable(&ctx->TransformFeedback.Objects);
186 
187    _mesa_reference_buffer_object(ctx,
188                                  &ctx->TransformFeedback.CurrentBuffer, NULL);
189 }
190 
191 
192 
193 /**
194  * Callback for _mesa_DeleteHashTable().
195  */
196 static void
delete_cb(void * data,void * userData)197 delete_cb(void *data, void *userData)
198 {
199    struct gl_context *ctx = (struct gl_context *) userData;
200    struct gl_transform_feedback_object *obj =
201       (struct gl_transform_feedback_object *) data;
202 
203    delete_transform_feedback(ctx, obj);
204 }
205 
206 
207 /**
208  * Per-context free/clean-up for transform feedback.
209  */
210 void
_mesa_free_transform_feedback(struct gl_context * ctx)211 _mesa_free_transform_feedback(struct gl_context *ctx)
212 {
213    /* core mesa expects this, even a dummy one, to be available */
214    _mesa_reference_buffer_object(ctx,
215                                  &ctx->TransformFeedback.CurrentBuffer,
216                                  NULL);
217 
218    /* Delete all feedback objects */
219    _mesa_DeinitHashTable(&ctx->TransformFeedback.Objects, delete_cb, ctx);
220 
221    /* Delete the default feedback object */
222    delete_transform_feedback(ctx,
223                              ctx->TransformFeedback.DefaultObject);
224 
225    ctx->TransformFeedback.CurrentObject = NULL;
226 }
227 
228 /**
229  * Fill in the correct Size value for each buffer in \c obj.
230  *
231  * From the GL 4.3 spec, section 6.1.1 ("Binding Buffer Objects to Indexed
232  * Targets"):
233  *
234  *   BindBufferBase binds the entire buffer, even when the size of the buffer
235  *   is changed after the binding is established. It is equivalent to calling
236  *   BindBufferRange with offset zero, while size is determined by the size of
237  *   the bound buffer at the time the binding is used.
238  *
239  *   Regardless of the size specified with BindBufferRange, or indirectly with
240  *   BindBufferBase, the GL will never read or write beyond the end of a bound
241  *   buffer. In some cases this constraint may result in visibly different
242  *   behavior when a buffer overflow would otherwise result, such as described
243  *   for transform feedback operations in section 13.2.2.
244  */
245 static void
compute_transform_feedback_buffer_sizes(struct gl_transform_feedback_object * obj)246 compute_transform_feedback_buffer_sizes(
247       struct gl_transform_feedback_object *obj)
248 {
249    unsigned i = 0;
250    for (i = 0; i < MAX_FEEDBACK_BUFFERS; ++i) {
251       GLintptr offset = obj->Offset[i];
252       GLsizeiptr buffer_size
253          = obj->Buffers[i] == NULL ? 0 : obj->Buffers[i]->Size;
254       GLsizeiptr available_space
255          = buffer_size <= offset ? 0 : buffer_size - offset;
256       GLsizeiptr computed_size;
257       if (obj->RequestedSize[i] == 0) {
258          /* No size was specified at the time the buffer was bound, so allow
259           * writing to all available space in the buffer.
260           */
261          computed_size = available_space;
262       } else {
263          /* A size was specified at the time the buffer was bound, however
264           * it's possible that the buffer has shrunk since then.  So only
265           * allow writing to the minimum of the specified size and the space
266           * available.
267           */
268          computed_size = MIN2(available_space, obj->RequestedSize[i]);
269       }
270 
271       /* Legal sizes must be multiples of four, so round down if necessary. */
272       obj->Size[i] = computed_size & ~0x3;
273    }
274 }
275 
276 
277 /**
278  * Compute the maximum number of vertices that can be written to the currently
279  * enabled transform feedback buffers without overflowing any of them.
280  */
281 unsigned
_mesa_compute_max_transform_feedback_vertices(struct gl_context * ctx,const struct gl_transform_feedback_object * obj,const struct gl_transform_feedback_info * info)282 _mesa_compute_max_transform_feedback_vertices(struct gl_context *ctx,
283       const struct gl_transform_feedback_object *obj,
284       const struct gl_transform_feedback_info *info)
285 {
286    unsigned max_index = 0xffffffff;
287    unsigned i;
288 
289    for (i = 0; i < ctx->Const.MaxTransformFeedbackBuffers; i++) {
290       if ((info->ActiveBuffers >> i) & 1) {
291          unsigned stride = info->Buffers[i].Stride;
292          unsigned max_for_this_buffer;
293 
294          /* Skip any inactive buffers, which have a stride of 0. */
295          if (stride == 0)
296             continue;
297 
298          max_for_this_buffer = obj->Size[i] / (4 * stride);
299          max_index = MIN2(max_index, max_for_this_buffer);
300       }
301    }
302 
303    return max_index;
304 }
305 
306 
307 /**
308  ** Begin API functions
309  **/
310 
311 
312 /**
313  * Figure out which stage of the pipeline is the source of transform feedback
314  * data given the current context state, and return its gl_program.
315  *
316  * If no active program can generate transform feedback data (i.e. no vertex
317  * shader is active), returns NULL.
318  */
319 static struct gl_program *
get_xfb_source(struct gl_context * ctx)320 get_xfb_source(struct gl_context *ctx)
321 {
322    int i;
323    for (i = MESA_SHADER_GEOMETRY; i >= MESA_SHADER_VERTEX; i--) {
324       if (ctx->_Shader->CurrentProgram[i] != NULL)
325          return ctx->_Shader->CurrentProgram[i];
326    }
327    return NULL;
328 }
329 
330 
331 static ALWAYS_INLINE void
begin_transform_feedback(struct gl_context * ctx,GLenum mode,bool no_error)332 begin_transform_feedback(struct gl_context *ctx, GLenum mode, bool no_error)
333 {
334    struct gl_transform_feedback_object *obj;
335    struct gl_transform_feedback_info *info = NULL;
336    struct gl_program *source;
337    GLuint i;
338    unsigned vertices_per_prim;
339 
340    obj = ctx->TransformFeedback.CurrentObject;
341 
342    /* Figure out what pipeline stage is the source of data for transform
343     * feedback.
344     */
345    source = get_xfb_source(ctx);
346    if (!no_error && source == NULL) {
347       _mesa_error(ctx, GL_INVALID_OPERATION,
348                   "glBeginTransformFeedback(no program active)");
349       return;
350    }
351 
352    info = source->sh.LinkedTransformFeedback;
353 
354    if (!no_error && info->NumOutputs == 0) {
355       _mesa_error(ctx, GL_INVALID_OPERATION,
356                   "glBeginTransformFeedback(no varyings to record)");
357       return;
358    }
359 
360    switch (mode) {
361    case GL_POINTS:
362       vertices_per_prim = 1;
363       break;
364    case GL_LINES:
365       vertices_per_prim = 2;
366       break;
367    case GL_TRIANGLES:
368       vertices_per_prim = 3;
369       break;
370    default:
371       if (!no_error) {
372          _mesa_error(ctx, GL_INVALID_ENUM, "glBeginTransformFeedback(mode)");
373          return;
374       } else {
375          /* Stop compiler warnings */
376          unreachable("Error in API use when using KHR_no_error");
377       }
378    }
379 
380    if (!no_error) {
381       if (obj->Active) {
382          _mesa_error(ctx, GL_INVALID_OPERATION,
383                      "glBeginTransformFeedback(already active)");
384          return;
385       }
386 
387       for (i = 0; i < ctx->Const.MaxTransformFeedbackBuffers; i++) {
388          if ((info->ActiveBuffers >> i) & 1) {
389             if (obj->BufferNames[i] == 0) {
390                _mesa_error(ctx, GL_INVALID_OPERATION,
391                            "glBeginTransformFeedback(binding point %d does not "
392                            "have a buffer object bound)", i);
393                return;
394             }
395          }
396       }
397    }
398 
399    FLUSH_VERTICES(ctx, 0, 0);
400 
401    obj->Active = GL_TRUE;
402    ctx->TransformFeedback.Mode = mode;
403 
404    compute_transform_feedback_buffer_sizes(obj);
405 
406    if (_mesa_is_gles3(ctx)) {
407       /* In GLES3, we are required to track the usage of the transform
408        * feedback buffer and report INVALID_OPERATION if a draw call tries to
409        * exceed it.  So compute the maximum number of vertices that we can
410        * write without overflowing any of the buffers currently being used for
411        * feedback.
412        */
413       unsigned max_vertices
414          = _mesa_compute_max_transform_feedback_vertices(ctx, obj, info);
415       obj->GlesRemainingPrims = max_vertices / vertices_per_prim;
416    }
417 
418    if (obj->program != source) {
419       _mesa_reference_program_(ctx, &obj->program, source);
420       obj->program = source;
421    }
422 
423    struct pipe_context *pipe = ctx->pipe;
424    unsigned max_num_targets;
425    unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0};
426 
427    max_num_targets = MIN2(ARRAY_SIZE(obj->Buffers),
428                           ARRAY_SIZE(obj->targets));
429 
430    /* Convert the transform feedback state into the gallium representation. */
431    for (i = 0; i < max_num_targets; i++) {
432       struct gl_buffer_object *bo = obj->Buffers[i];
433 
434       if (bo && bo->buffer) {
435          unsigned stream = obj->program->sh.LinkedTransformFeedback->
436             Buffers[i].Stream;
437 
438          /* Check whether we need to recreate the target. */
439          if (!obj->targets[i] ||
440              obj->targets[i] == obj->draw_count[stream] ||
441              obj->targets[i]->buffer != bo->buffer ||
442              obj->targets[i]->buffer_offset != obj->Offset[i] ||
443              obj->targets[i]->buffer_size != obj->Size[i]) {
444             /* Create a new target. */
445             struct pipe_stream_output_target *so_target =
446                   pipe->create_stream_output_target(pipe, bo->buffer,
447                                                     obj->Offset[i],
448                                                     obj->Size[i]);
449 
450             pipe_so_target_reference(&obj->targets[i], NULL);
451             obj->targets[i] = so_target;
452          }
453 
454          obj->num_targets = i+1;
455       } else {
456          pipe_so_target_reference(&obj->targets[i], NULL);
457       }
458    }
459 
460    /* Start writing at the beginning of each target. */
461    cso_set_stream_outputs(ctx->cso_context, obj->num_targets,
462                           obj->targets, offsets);
463    _mesa_update_valid_to_render_state(ctx);
464 }
465 
466 
467 void GLAPIENTRY
_mesa_BeginTransformFeedback_no_error(GLenum mode)468 _mesa_BeginTransformFeedback_no_error(GLenum mode)
469 {
470    GET_CURRENT_CONTEXT(ctx);
471    begin_transform_feedback(ctx, mode, true);
472 }
473 
474 
475 void GLAPIENTRY
_mesa_BeginTransformFeedback(GLenum mode)476 _mesa_BeginTransformFeedback(GLenum mode)
477 {
478    GET_CURRENT_CONTEXT(ctx);
479    begin_transform_feedback(ctx, mode, false);
480 }
481 
482 
483 static void
end_transform_feedback(struct gl_context * ctx,struct gl_transform_feedback_object * obj)484 end_transform_feedback(struct gl_context *ctx,
485                        struct gl_transform_feedback_object *obj)
486 {
487    unsigned i;
488    FLUSH_VERTICES(ctx, 0, 0);
489 
490    cso_set_stream_outputs(ctx->cso_context, 0, NULL, NULL);
491 
492    /* The next call to glDrawTransformFeedbackStream should use the vertex
493     * count from the last call to glEndTransformFeedback.
494     * Therefore, save the targets for each stream.
495     *
496     * NULL means the vertex counter is 0 (initial state).
497     */
498    for (i = 0; i < ARRAY_SIZE(obj->draw_count); i++)
499       pipe_so_target_reference(&obj->draw_count[i], NULL);
500 
501    for (i = 0; i < ARRAY_SIZE(obj->targets); i++) {
502       unsigned stream = obj->program->sh.LinkedTransformFeedback->
503          Buffers[i].Stream;
504 
505       /* Is it not bound or already set for this stream? */
506       if (!obj->targets[i] || obj->draw_count[stream])
507          continue;
508 
509       pipe_so_target_reference(&obj->draw_count[stream], obj->targets[i]);
510    }
511 
512    _mesa_reference_program_(ctx, &obj->program, NULL);
513    ctx->TransformFeedback.CurrentObject->Active = GL_FALSE;
514    ctx->TransformFeedback.CurrentObject->Paused = GL_FALSE;
515    ctx->TransformFeedback.CurrentObject->EndedAnytime = GL_TRUE;
516    _mesa_update_valid_to_render_state(ctx);
517 }
518 
519 
520 void GLAPIENTRY
_mesa_EndTransformFeedback_no_error(void)521 _mesa_EndTransformFeedback_no_error(void)
522 {
523    GET_CURRENT_CONTEXT(ctx);
524    end_transform_feedback(ctx, ctx->TransformFeedback.CurrentObject);
525 }
526 
527 
528 void GLAPIENTRY
_mesa_EndTransformFeedback(void)529 _mesa_EndTransformFeedback(void)
530 {
531    struct gl_transform_feedback_object *obj;
532    GET_CURRENT_CONTEXT(ctx);
533 
534    obj = ctx->TransformFeedback.CurrentObject;
535 
536    if (!obj->Active) {
537       _mesa_error(ctx, GL_INVALID_OPERATION,
538                   "glEndTransformFeedback(not active)");
539       return;
540    }
541 
542    end_transform_feedback(ctx, obj);
543 }
544 
545 
546 /**
547  * Helper used by BindBufferRange() and BindBufferBase().
548  */
549 static void
bind_buffer_range(struct gl_context * ctx,struct gl_transform_feedback_object * obj,GLuint index,struct gl_buffer_object * bufObj,GLintptr offset,GLsizeiptr size,bool dsa)550 bind_buffer_range(struct gl_context *ctx,
551                   struct gl_transform_feedback_object *obj,
552                   GLuint index,
553                   struct gl_buffer_object *bufObj,
554                   GLintptr offset, GLsizeiptr size,
555                   bool dsa)
556 {
557    /* Note: no need to FLUSH_VERTICES because
558     * transform feedback buffers can't be changed while transform feedback is
559     * active.
560     */
561 
562    if (!dsa) {
563       /* The general binding point */
564       _mesa_reference_buffer_object(ctx,
565                                     &ctx->TransformFeedback.CurrentBuffer,
566                                     bufObj);
567    }
568 
569    /* The per-attribute binding point */
570    _mesa_set_transform_feedback_binding(ctx, obj, index, bufObj, offset, size);
571 }
572 
573 
574 /**
575  * Validate the buffer object to receive transform feedback results.  Plus,
576  * validate the starting offset to place the results, and max size.
577  * Called from the glBindBufferRange() and glTransformFeedbackBufferRange
578  * functions.
579  */
580 bool
_mesa_validate_buffer_range_xfb(struct gl_context * ctx,struct gl_transform_feedback_object * obj,GLuint index,struct gl_buffer_object * bufObj,GLintptr offset,GLsizeiptr size,bool dsa)581 _mesa_validate_buffer_range_xfb(struct gl_context *ctx,
582                                 struct gl_transform_feedback_object *obj,
583                                 GLuint index, struct gl_buffer_object *bufObj,
584                                 GLintptr offset, GLsizeiptr size, bool dsa)
585 {
586    const char *gl_methd_name;
587    if (dsa)
588       gl_methd_name = "glTransformFeedbackBufferRange";
589    else
590       gl_methd_name = "glBindBufferRange";
591 
592 
593    if (obj->Active) {
594       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(transform feedback active)",
595                   gl_methd_name);
596       return false;
597    }
598 
599    if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
600       /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
601        * generated if index is greater than or equal to the number of binding
602        * points for transform feedback, as described in section 6.7.1."
603        */
604       _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d out of bounds)",
605                   gl_methd_name, index);
606       return false;
607    }
608 
609    if (size & 0x3) {
610       /* OpenGL 4.5 core profile, 6.7, pdf page 103: multiple of 4 */
611       _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d must be a multiple of "
612                   "four)", gl_methd_name, (int) size);
613       return false;
614    }
615 
616    if (offset & 0x3) {
617       /* OpenGL 4.5 core profile, 6.7, pdf page 103: multiple of 4 */
618       _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset=%d must be a multiple "
619                   "of four)", gl_methd_name, (int) offset);
620       return false;
621    }
622 
623    if (offset < 0) {
624       /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
625        * generated by BindBufferRange if offset is negative."
626        *
627        * OpenGL 4.5 core profile, 13.2, pdf page 445: "An INVALID_VALUE error
628        * is generated by TransformFeedbackBufferRange if offset is negative."
629        */
630       _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset=%d must be >= 0)",
631                   gl_methd_name,
632                   (int) offset);
633       return false;
634    }
635 
636    if (size <= 0 && (dsa || bufObj)) {
637       /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
638        * generated by BindBufferRange if buffer is non-zero and size is less
639        * than or equal to zero."
640        *
641        * OpenGL 4.5 core profile, 13.2, pdf page 445: "An INVALID_VALUE error
642        * is generated by TransformFeedbackBufferRange if size is less than or
643        * equal to zero."
644        */
645       _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d must be > 0)",
646                   gl_methd_name, (int) size);
647       return false;
648    }
649 
650    return true;
651 }
652 
653 
654 /**
655  * Specify a buffer object to receive transform feedback results.
656  * As above, but start at offset = 0.
657  * Called from the glBindBufferBase() and glTransformFeedbackBufferBase()
658  * functions.
659  */
660 void
_mesa_bind_buffer_base_transform_feedback(struct gl_context * ctx,struct gl_transform_feedback_object * obj,GLuint index,struct gl_buffer_object * bufObj,bool dsa)661 _mesa_bind_buffer_base_transform_feedback(struct gl_context *ctx,
662                                           struct gl_transform_feedback_object *obj,
663                                           GLuint index,
664                                           struct gl_buffer_object *bufObj,
665                                           bool dsa)
666 {
667    if (obj->Active) {
668       _mesa_error(ctx, GL_INVALID_OPERATION,
669                   "%s(transform feedback active)",
670                   dsa ? "glTransformFeedbackBufferBase" : "glBindBufferBase");
671       return;
672    }
673 
674    if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
675       _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d out of bounds)",
676                   dsa ? "glTransformFeedbackBufferBase" : "glBindBufferBase",
677                   index);
678       return;
679    }
680 
681    bind_buffer_range(ctx, obj, index, bufObj, 0, 0, dsa);
682 }
683 
684 /**
685  * Wrapper around lookup_transform_feedback_object that throws
686  * GL_INVALID_OPERATION if id is not in the hash table. After calling
687  * _mesa_error, it returns NULL.
688  */
689 static struct gl_transform_feedback_object *
lookup_transform_feedback_object_err(struct gl_context * ctx,GLuint xfb,const char * func)690 lookup_transform_feedback_object_err(struct gl_context *ctx,
691                                      GLuint xfb, const char* func)
692 {
693    struct gl_transform_feedback_object *obj;
694 
695    obj = _mesa_lookup_transform_feedback_object(ctx, xfb);
696    if (!obj) {
697       _mesa_error(ctx, GL_INVALID_OPERATION,
698                   "%s(xfb=%u: non-generated object name)", func, xfb);
699    }
700 
701    return obj;
702 }
703 
704 /**
705  * Wrapper around _mesa_lookup_bufferobj that throws GL_INVALID_VALUE if id
706  * is not in the hash table. Specialised version for the
707  * transform-feedback-related functions. After calling _mesa_error, it
708  * returns NULL.
709  */
710 static struct gl_buffer_object *
lookup_transform_feedback_bufferobj_err(struct gl_context * ctx,GLuint buffer,const char * func,bool * error)711 lookup_transform_feedback_bufferobj_err(struct gl_context *ctx,
712                                         GLuint buffer, const char* func,
713                                         bool *error)
714 {
715    struct gl_buffer_object *bufObj = NULL;
716 
717    *error = false;
718 
719    /* OpenGL 4.5 core profile, 13.2, pdf page 444: buffer must be zero or the
720     * name of an existing buffer object.
721     */
722    if (buffer) {
723       bufObj = _mesa_lookup_bufferobj(ctx, buffer);
724       if (!bufObj) {
725          _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid buffer=%u)", func,
726                      buffer);
727          *error = true;
728       }
729    }
730 
731    return bufObj;
732 }
733 
734 void GLAPIENTRY
_mesa_TransformFeedbackBufferBase(GLuint xfb,GLuint index,GLuint buffer)735 _mesa_TransformFeedbackBufferBase(GLuint xfb, GLuint index, GLuint buffer)
736 {
737    GET_CURRENT_CONTEXT(ctx);
738    struct gl_transform_feedback_object *obj;
739    struct gl_buffer_object *bufObj;
740 
741    obj = lookup_transform_feedback_object_err(ctx, xfb,
742                                               "glTransformFeedbackBufferBase");
743    if (!obj) {
744       return;
745    }
746 
747    bool error;
748    bufObj = lookup_transform_feedback_bufferobj_err(ctx, buffer,
749                                               "glTransformFeedbackBufferBase",
750                                                     &error);
751    if (error) {
752       return;
753    }
754 
755    _mesa_bind_buffer_base_transform_feedback(ctx, obj, index, bufObj, true);
756 }
757 
758 void GLAPIENTRY
_mesa_TransformFeedbackBufferRange(GLuint xfb,GLuint index,GLuint buffer,GLintptr offset,GLsizeiptr size)759 _mesa_TransformFeedbackBufferRange(GLuint xfb, GLuint index, GLuint buffer,
760                                    GLintptr offset, GLsizeiptr size)
761 {
762    GET_CURRENT_CONTEXT(ctx);
763    struct gl_transform_feedback_object *obj;
764    struct gl_buffer_object *bufObj;
765 
766    obj = lookup_transform_feedback_object_err(ctx, xfb,
767                                               "glTransformFeedbackBufferRange");
768    if (!obj) {
769       return;
770    }
771 
772    bool error;
773    bufObj = lookup_transform_feedback_bufferobj_err(ctx, buffer,
774                                               "glTransformFeedbackBufferRange",
775                                                     &error);
776    if (error) {
777       return;
778    }
779 
780    if (!_mesa_validate_buffer_range_xfb(ctx, obj, index, bufObj, offset,
781                                         size, true))
782       return;
783 
784    /* The per-attribute binding point */
785    _mesa_set_transform_feedback_binding(ctx, obj, index, bufObj, offset,
786                                         size);
787 }
788 
789 /**
790  * Specify a buffer object to receive transform feedback results, plus the
791  * offset in the buffer to start placing results.
792  * This function is part of GL_EXT_transform_feedback, but not GL3.
793  */
794 static ALWAYS_INLINE void
bind_buffer_offset(struct gl_context * ctx,struct gl_transform_feedback_object * obj,GLuint index,GLuint buffer,GLintptr offset,bool no_error)795 bind_buffer_offset(struct gl_context *ctx,
796                    struct gl_transform_feedback_object *obj, GLuint index,
797                    GLuint buffer, GLintptr offset, bool no_error)
798 {
799    struct gl_buffer_object *bufObj;
800 
801    if (buffer == 0) {
802       bufObj = NULL;
803    } else {
804       bufObj = _mesa_lookup_bufferobj(ctx, buffer);
805       if (!no_error && !bufObj) {
806          _mesa_error(ctx, GL_INVALID_OPERATION,
807                      "glBindBufferOffsetEXT(invalid buffer=%u)", buffer);
808          return;
809       }
810    }
811 
812    _mesa_bind_buffer_range_xfb(ctx, obj, index, bufObj, offset, 0);
813 }
814 
815 
816 void GLAPIENTRY
_mesa_BindBufferOffsetEXT_no_error(GLenum target,GLuint index,GLuint buffer,GLintptr offset)817 _mesa_BindBufferOffsetEXT_no_error(GLenum target, GLuint index, GLuint buffer,
818                                    GLintptr offset)
819 {
820    GET_CURRENT_CONTEXT(ctx);
821    bind_buffer_offset(ctx, ctx->TransformFeedback.CurrentObject, index, buffer,
822                       offset, true);
823 }
824 
825 
826 void GLAPIENTRY
_mesa_BindBufferOffsetEXT(GLenum target,GLuint index,GLuint buffer,GLintptr offset)827 _mesa_BindBufferOffsetEXT(GLenum target, GLuint index, GLuint buffer,
828                           GLintptr offset)
829 {
830    struct gl_transform_feedback_object *obj;
831    GET_CURRENT_CONTEXT(ctx);
832 
833    if (target != GL_TRANSFORM_FEEDBACK_BUFFER) {
834       _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferOffsetEXT(target)");
835       return;
836    }
837 
838    obj = ctx->TransformFeedback.CurrentObject;
839 
840    if (obj->Active) {
841       _mesa_error(ctx, GL_INVALID_OPERATION,
842                   "glBindBufferOffsetEXT(transform feedback active)");
843       return;
844    }
845 
846    if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
847       _mesa_error(ctx, GL_INVALID_VALUE,
848                   "glBindBufferOffsetEXT(index=%d)", index);
849       return;
850    }
851 
852    if (offset & 0x3) {
853       /* must be multiple of four */
854       _mesa_error(ctx, GL_INVALID_VALUE,
855                   "glBindBufferOffsetEXT(offset=%d)", (int) offset);
856       return;
857    }
858 
859    bind_buffer_offset(ctx, obj, index, buffer, offset, false);
860 }
861 
862 
863 /**
864  * This function specifies the transform feedback outputs to be written
865  * to the feedback buffer(s), and in what order.
866  */
867 static ALWAYS_INLINE void
transform_feedback_varyings(struct gl_context * ctx,struct gl_shader_program * shProg,GLsizei count,const GLchar * const * varyings,GLenum bufferMode)868 transform_feedback_varyings(struct gl_context *ctx,
869                             struct gl_shader_program *shProg, GLsizei count,
870                             const GLchar *const *varyings, GLenum bufferMode)
871 {
872    GLint i;
873 
874    /* free existing varyings, if any */
875    for (i = 0; i < (GLint) shProg->TransformFeedback.NumVarying; i++) {
876       free(shProg->TransformFeedback.VaryingNames[i]);
877    }
878    free(shProg->TransformFeedback.VaryingNames);
879 
880    /* allocate new memory for varying names */
881    shProg->TransformFeedback.VaryingNames =
882       malloc(count * sizeof(GLchar *));
883 
884    if (!shProg->TransformFeedback.VaryingNames) {
885       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTransformFeedbackVaryings()");
886       return;
887    }
888 
889    /* Save the new names and the count */
890    for (i = 0; i < count; i++) {
891       shProg->TransformFeedback.VaryingNames[i] = strdup(varyings[i]);
892    }
893    shProg->TransformFeedback.NumVarying = count;
894 
895    shProg->TransformFeedback.BufferMode = bufferMode;
896 
897    /* No need to invoke FLUSH_VERTICES since
898     * the varyings won't be used until shader link time.
899     */
900 }
901 
902 
903 void GLAPIENTRY
_mesa_TransformFeedbackVaryings_no_error(GLuint program,GLsizei count,const GLchar * const * varyings,GLenum bufferMode)904 _mesa_TransformFeedbackVaryings_no_error(GLuint program, GLsizei count,
905                                          const GLchar *const *varyings,
906                                          GLenum bufferMode)
907 {
908    GET_CURRENT_CONTEXT(ctx);
909 
910    struct gl_shader_program *shProg = _mesa_lookup_shader_program(ctx, program);
911    transform_feedback_varyings(ctx, shProg, count, varyings, bufferMode);
912 }
913 
914 void GLAPIENTRY
_mesa_TransformFeedbackVaryings(GLuint program,GLsizei count,const GLchar * const * varyings,GLenum bufferMode)915 _mesa_TransformFeedbackVaryings(GLuint program, GLsizei count,
916                                 const GLchar * const *varyings,
917                                 GLenum bufferMode)
918 {
919    struct gl_shader_program *shProg;
920    GLint i;
921    GET_CURRENT_CONTEXT(ctx);
922 
923    /* From the ARB_transform_feedback2 specification:
924     * "The error INVALID_OPERATION is generated by TransformFeedbackVaryings
925     *  if the current transform feedback object is active, even if paused."
926     */
927    if (ctx->TransformFeedback.CurrentObject->Active) {
928       _mesa_error(ctx, GL_INVALID_OPERATION,
929                "glTransformFeedbackVaryings(current object is active)");
930       return;
931    }
932 
933    switch (bufferMode) {
934    case GL_INTERLEAVED_ATTRIBS:
935       break;
936    case GL_SEPARATE_ATTRIBS:
937       break;
938    default:
939       _mesa_error(ctx, GL_INVALID_ENUM,
940                   "glTransformFeedbackVaryings(bufferMode)");
941       return;
942    }
943 
944    if (count < 0 ||
945        (bufferMode == GL_SEPARATE_ATTRIBS &&
946         (GLuint) count > ctx->Const.MaxTransformFeedbackBuffers)) {
947       _mesa_error(ctx, GL_INVALID_VALUE,
948                   "glTransformFeedbackVaryings(count=%d)", count);
949       return;
950    }
951 
952    shProg = _mesa_lookup_shader_program_err(ctx, program,
953                                             "glTransformFeedbackVaryings");
954    if (!shProg)
955       return;
956 
957    if (ctx->Extensions.ARB_transform_feedback3) {
958       if (bufferMode == GL_INTERLEAVED_ATTRIBS) {
959          unsigned buffers = 1;
960 
961          for (i = 0; i < count; i++) {
962             if (strcmp(varyings[i], "gl_NextBuffer") == 0)
963                buffers++;
964          }
965 
966          if (buffers > ctx->Const.MaxTransformFeedbackBuffers) {
967             _mesa_error(ctx, GL_INVALID_OPERATION,
968                         "glTransformFeedbackVaryings(too many gl_NextBuffer "
969                         "occurrences)");
970             return;
971          }
972       } else {
973          for (i = 0; i < count; i++) {
974             if (strcmp(varyings[i], "gl_NextBuffer") == 0 ||
975                 strcmp(varyings[i], "gl_SkipComponents1") == 0 ||
976                 strcmp(varyings[i], "gl_SkipComponents2") == 0 ||
977                 strcmp(varyings[i], "gl_SkipComponents3") == 0 ||
978                 strcmp(varyings[i], "gl_SkipComponents4") == 0) {
979                _mesa_error(ctx, GL_INVALID_OPERATION,
980                            "glTransformFeedbackVaryings(SEPARATE_ATTRIBS,"
981                            "varying=%s)",
982                            varyings[i]);
983                return;
984             }
985          }
986       }
987    }
988 
989    transform_feedback_varyings(ctx, shProg, count, varyings, bufferMode);
990 }
991 
992 
993 /**
994  * Get info about the transform feedback outputs which are to be written
995  * to the feedback buffer(s).
996  */
997 void GLAPIENTRY
_mesa_GetTransformFeedbackVarying(GLuint program,GLuint index,GLsizei bufSize,GLsizei * length,GLsizei * size,GLenum * type,GLchar * name)998 _mesa_GetTransformFeedbackVarying(GLuint program, GLuint index,
999                                   GLsizei bufSize, GLsizei *length,
1000                                   GLsizei *size, GLenum *type, GLchar *name)
1001 {
1002    const struct gl_shader_program *shProg;
1003    struct gl_program_resource *res;
1004    GET_CURRENT_CONTEXT(ctx);
1005 
1006    shProg = _mesa_lookup_shader_program_err(ctx, program,
1007                                             "glGetTransformFeedbackVarying");
1008    if (!shProg)
1009       return;
1010 
1011    res = _mesa_program_resource_find_index((struct gl_shader_program *) shProg,
1012                                            GL_TRANSFORM_FEEDBACK_VARYING,
1013                                            index);
1014    if (!res) {
1015       _mesa_error(ctx, GL_INVALID_VALUE,
1016                   "glGetTransformFeedbackVarying(index=%u)", index);
1017       return;
1018    }
1019 
1020    /* return the varying's name and length */
1021    _mesa_copy_string(name, bufSize, length, _mesa_program_resource_name(res));
1022 
1023    /* return the datatype and value's size (in datatype units) */
1024    if (type)
1025       _mesa_program_resource_prop((struct gl_shader_program *) shProg,
1026                                   res, index, GL_TYPE, (GLint*) type,
1027                                   false, "glGetTransformFeedbackVarying");
1028    if (size)
1029       _mesa_program_resource_prop((struct gl_shader_program *) shProg,
1030                                   res, index, GL_ARRAY_SIZE, (GLint*) size,
1031                                   false, "glGetTransformFeedbackVarying");
1032 }
1033 
1034 
1035 
1036 struct gl_transform_feedback_object *
_mesa_lookup_transform_feedback_object(struct gl_context * ctx,GLuint name)1037 _mesa_lookup_transform_feedback_object(struct gl_context *ctx, GLuint name)
1038 {
1039    /* OpenGL 4.5 core profile, 13.2 pdf page 444: "xfb must be zero, indicating
1040     * the default transform feedback object, or the name of an existing
1041     * transform feedback object."
1042     */
1043    if (name == 0) {
1044       return ctx->TransformFeedback.DefaultObject;
1045    }
1046    else
1047       return (struct gl_transform_feedback_object *)
1048          _mesa_HashLookupLocked(&ctx->TransformFeedback.Objects, name);
1049 }
1050 
1051 static void
create_transform_feedbacks(struct gl_context * ctx,GLsizei n,GLuint * ids,bool dsa)1052 create_transform_feedbacks(struct gl_context *ctx, GLsizei n, GLuint *ids,
1053                            bool dsa)
1054 {
1055    const char* func;
1056 
1057    if (dsa)
1058       func = "glCreateTransformFeedbacks";
1059    else
1060       func = "glGenTransformFeedbacks";
1061 
1062    if (n < 0) {
1063       _mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", func);
1064       return;
1065    }
1066 
1067    if (!ids)
1068       return;
1069 
1070    if (_mesa_HashFindFreeKeys(&ctx->TransformFeedback.Objects, ids, n)) {
1071       GLsizei i;
1072       for (i = 0; i < n; i++) {
1073          struct gl_transform_feedback_object *obj
1074             = new_transform_feedback(ctx, ids[i]);
1075          if (!obj) {
1076             _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
1077             return;
1078          }
1079          _mesa_HashInsertLocked(&ctx->TransformFeedback.Objects, ids[i], obj);
1080          if (dsa) {
1081             /* this is normally done at bind time in the non-dsa case */
1082             obj->EverBound = GL_TRUE;
1083          }
1084       }
1085    }
1086    else {
1087       _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
1088    }
1089 }
1090 
1091 /**
1092  * Create new transform feedback objects.   Transform feedback objects
1093  * encapsulate the state related to transform feedback to allow quickly
1094  * switching state (and drawing the results, below).
1095  * Part of GL_ARB_transform_feedback2.
1096  */
1097 void GLAPIENTRY
_mesa_GenTransformFeedbacks(GLsizei n,GLuint * names)1098 _mesa_GenTransformFeedbacks(GLsizei n, GLuint *names)
1099 {
1100    GET_CURRENT_CONTEXT(ctx);
1101 
1102    /* GenTransformFeedbacks should just reserve the object names that a
1103     * subsequent call to BindTransformFeedback should actively create. For
1104     * the sake of simplicity, we reserve the names and create the objects
1105     * straight away.
1106     */
1107 
1108    create_transform_feedbacks(ctx, n, names, false);
1109 }
1110 
1111 /**
1112  * Create new transform feedback objects.   Transform feedback objects
1113  * encapsulate the state related to transform feedback to allow quickly
1114  * switching state (and drawing the results, below).
1115  * Part of GL_ARB_direct_state_access.
1116  */
1117 void GLAPIENTRY
_mesa_CreateTransformFeedbacks(GLsizei n,GLuint * names)1118 _mesa_CreateTransformFeedbacks(GLsizei n, GLuint *names)
1119 {
1120    GET_CURRENT_CONTEXT(ctx);
1121 
1122    create_transform_feedbacks(ctx, n, names, true);
1123 }
1124 
1125 
1126 /**
1127  * Is the given ID a transform feedback object?
1128  * Part of GL_ARB_transform_feedback2.
1129  */
1130 GLboolean GLAPIENTRY
_mesa_IsTransformFeedback(GLuint name)1131 _mesa_IsTransformFeedback(GLuint name)
1132 {
1133    struct gl_transform_feedback_object *obj;
1134    GET_CURRENT_CONTEXT(ctx);
1135 
1136    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1137 
1138    if (name == 0)
1139       return GL_FALSE;
1140 
1141    obj = _mesa_lookup_transform_feedback_object(ctx, name);
1142    if (obj == NULL)
1143       return GL_FALSE;
1144 
1145    return obj->EverBound;
1146 }
1147 
1148 
1149 /**
1150  * Bind the given transform feedback object.
1151  * Part of GL_ARB_transform_feedback2.
1152  */
1153 static ALWAYS_INLINE void
bind_transform_feedback(struct gl_context * ctx,GLuint name,bool no_error)1154 bind_transform_feedback(struct gl_context *ctx, GLuint name, bool no_error)
1155 {
1156    struct gl_transform_feedback_object *obj;
1157 
1158    obj = _mesa_lookup_transform_feedback_object(ctx, name);
1159    if (!no_error && !obj) {
1160       _mesa_error(ctx, GL_INVALID_OPERATION,
1161                   "glBindTransformFeedback(name=%u)", name);
1162       return;
1163    }
1164 
1165    reference_transform_feedback_object(&ctx->TransformFeedback.CurrentObject,
1166                                        obj);
1167 }
1168 
1169 
1170 void GLAPIENTRY
_mesa_BindTransformFeedback_no_error(GLenum target,GLuint name)1171 _mesa_BindTransformFeedback_no_error(GLenum target, GLuint name)
1172 {
1173    GET_CURRENT_CONTEXT(ctx);
1174    bind_transform_feedback(ctx, name, true);
1175 }
1176 
1177 
1178 void GLAPIENTRY
_mesa_BindTransformFeedback(GLenum target,GLuint name)1179 _mesa_BindTransformFeedback(GLenum target, GLuint name)
1180 {
1181    GET_CURRENT_CONTEXT(ctx);
1182 
1183    if (target != GL_TRANSFORM_FEEDBACK) {
1184       _mesa_error(ctx, GL_INVALID_ENUM, "glBindTransformFeedback(target)");
1185       return;
1186    }
1187 
1188    if (_mesa_is_xfb_active_and_unpaused(ctx)) {
1189       _mesa_error(ctx, GL_INVALID_OPERATION,
1190               "glBindTransformFeedback(transform is active, or not paused)");
1191       return;
1192    }
1193 
1194    bind_transform_feedback(ctx, name, false);
1195 }
1196 
1197 
1198 /**
1199  * Delete the given transform feedback objects.
1200  * Part of GL_ARB_transform_feedback2.
1201  */
1202 void GLAPIENTRY
_mesa_DeleteTransformFeedbacks(GLsizei n,const GLuint * names)1203 _mesa_DeleteTransformFeedbacks(GLsizei n, const GLuint *names)
1204 {
1205    GLint i;
1206    GET_CURRENT_CONTEXT(ctx);
1207 
1208    if (n < 0) {
1209       _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteTransformFeedbacks(n < 0)");
1210       return;
1211    }
1212 
1213    if (!names)
1214       return;
1215 
1216    for (i = 0; i < n; i++) {
1217       if (names[i] > 0) {
1218          struct gl_transform_feedback_object *obj
1219             = _mesa_lookup_transform_feedback_object(ctx, names[i]);
1220          if (obj) {
1221             if (obj->Active) {
1222                _mesa_error(ctx, GL_INVALID_OPERATION,
1223                            "glDeleteTransformFeedbacks(object %u is active)",
1224                            names[i]);
1225                return;
1226             }
1227             _mesa_HashRemoveLocked(&ctx->TransformFeedback.Objects, names[i]);
1228             /* unref, but object may not be deleted until later */
1229             if (obj == ctx->TransformFeedback.CurrentObject) {
1230                reference_transform_feedback_object(
1231                      &ctx->TransformFeedback.CurrentObject,
1232                      ctx->TransformFeedback.DefaultObject);
1233             }
1234             reference_transform_feedback_object(&obj, NULL);
1235          }
1236       }
1237    }
1238 }
1239 
1240 
1241 /**
1242  * Pause transform feedback.
1243  * Part of GL_ARB_transform_feedback2.
1244  */
1245 static void
pause_transform_feedback(struct gl_context * ctx,struct gl_transform_feedback_object * obj)1246 pause_transform_feedback(struct gl_context *ctx,
1247                          struct gl_transform_feedback_object *obj)
1248 {
1249    FLUSH_VERTICES(ctx, 0, 0);
1250 
1251    cso_set_stream_outputs(ctx->cso_context, 0, NULL, NULL);
1252 
1253    obj->Paused = GL_TRUE;
1254    _mesa_update_valid_to_render_state(ctx);
1255 }
1256 
1257 
1258 void GLAPIENTRY
_mesa_PauseTransformFeedback_no_error(void)1259 _mesa_PauseTransformFeedback_no_error(void)
1260 {
1261    GET_CURRENT_CONTEXT(ctx);
1262    pause_transform_feedback(ctx, ctx->TransformFeedback.CurrentObject);
1263 }
1264 
1265 
1266 void GLAPIENTRY
_mesa_PauseTransformFeedback(void)1267 _mesa_PauseTransformFeedback(void)
1268 {
1269    struct gl_transform_feedback_object *obj;
1270    GET_CURRENT_CONTEXT(ctx);
1271 
1272    obj = ctx->TransformFeedback.CurrentObject;
1273 
1274    if (!_mesa_is_xfb_active_and_unpaused(ctx)) {
1275       _mesa_error(ctx, GL_INVALID_OPERATION,
1276            "glPauseTransformFeedback(feedback not active or already paused)");
1277       return;
1278    }
1279 
1280    pause_transform_feedback(ctx, obj);
1281 }
1282 
1283 
1284 /**
1285  * Resume transform feedback.
1286  * Part of GL_ARB_transform_feedback2.
1287  */
1288 static void
resume_transform_feedback(struct gl_context * ctx,struct gl_transform_feedback_object * obj)1289 resume_transform_feedback(struct gl_context *ctx,
1290                           struct gl_transform_feedback_object *obj)
1291 {
1292    FLUSH_VERTICES(ctx, 0, 0);
1293 
1294    obj->Paused = GL_FALSE;
1295 
1296    unsigned offsets[PIPE_MAX_SO_BUFFERS];
1297    unsigned i;
1298 
1299    for (i = 0; i < PIPE_MAX_SO_BUFFERS; i++)
1300       offsets[i] = (unsigned)-1;
1301 
1302    cso_set_stream_outputs(ctx->cso_context, obj->num_targets,
1303                           obj->targets, offsets);
1304    _mesa_update_valid_to_render_state(ctx);
1305 }
1306 
1307 
1308 void GLAPIENTRY
_mesa_ResumeTransformFeedback_no_error(void)1309 _mesa_ResumeTransformFeedback_no_error(void)
1310 {
1311    GET_CURRENT_CONTEXT(ctx);
1312    resume_transform_feedback(ctx, ctx->TransformFeedback.CurrentObject);
1313 }
1314 
1315 
1316 void GLAPIENTRY
_mesa_ResumeTransformFeedback(void)1317 _mesa_ResumeTransformFeedback(void)
1318 {
1319    struct gl_transform_feedback_object *obj;
1320    GET_CURRENT_CONTEXT(ctx);
1321 
1322    obj = ctx->TransformFeedback.CurrentObject;
1323 
1324    if (!obj->Active || !obj->Paused) {
1325       _mesa_error(ctx, GL_INVALID_OPERATION,
1326                "glResumeTransformFeedback(feedback not active or not paused)");
1327       return;
1328    }
1329 
1330    /* From the ARB_transform_feedback2 specification:
1331     * "The error INVALID_OPERATION is generated by ResumeTransformFeedback if
1332     *  the program object being used by the current transform feedback object
1333     *  is not active."
1334     */
1335    if (obj->program != get_xfb_source(ctx)) {
1336       _mesa_error(ctx, GL_INVALID_OPERATION,
1337                   "glResumeTransformFeedback(wrong program bound)");
1338       return;
1339    }
1340 
1341    resume_transform_feedback(ctx, obj);
1342 }
1343 
1344 extern void GLAPIENTRY
_mesa_GetTransformFeedbackiv(GLuint xfb,GLenum pname,GLint * param)1345 _mesa_GetTransformFeedbackiv(GLuint xfb, GLenum pname, GLint *param)
1346 {
1347     struct gl_transform_feedback_object *obj;
1348     GET_CURRENT_CONTEXT(ctx);
1349 
1350     obj = lookup_transform_feedback_object_err(ctx, xfb,
1351                                                "glGetTransformFeedbackiv");
1352     if (!obj) {
1353        return;
1354     }
1355 
1356     switch(pname) {
1357     case GL_TRANSFORM_FEEDBACK_PAUSED:
1358        *param = obj->Paused;
1359        break;
1360     case GL_TRANSFORM_FEEDBACK_ACTIVE:
1361        *param = obj->Active;
1362        break;
1363     default:
1364        _mesa_error(ctx, GL_INVALID_ENUM,
1365                    "glGetTransformFeedbackiv(pname=%i)", pname);
1366     }
1367 }
1368 
1369 extern void GLAPIENTRY
_mesa_GetTransformFeedbacki_v(GLuint xfb,GLenum pname,GLuint index,GLint * param)1370 _mesa_GetTransformFeedbacki_v(GLuint xfb, GLenum pname, GLuint index,
1371                               GLint *param)
1372 {
1373    struct gl_transform_feedback_object *obj;
1374    GET_CURRENT_CONTEXT(ctx);
1375 
1376    obj = lookup_transform_feedback_object_err(ctx, xfb,
1377                                               "glGetTransformFeedbacki_v");
1378    if (!obj) {
1379       return;
1380    }
1381 
1382    if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
1383       _mesa_error(ctx, GL_INVALID_VALUE,
1384                   "glGetTransformFeedbacki_v(index=%i)", index);
1385       return;
1386    }
1387 
1388    switch(pname) {
1389    case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
1390       *param = obj->BufferNames[index];
1391       break;
1392    default:
1393       _mesa_error(ctx, GL_INVALID_ENUM,
1394                   "glGetTransformFeedbacki_v(pname=%i)", pname);
1395    }
1396 }
1397 
1398 extern void GLAPIENTRY
_mesa_GetTransformFeedbacki64_v(GLuint xfb,GLenum pname,GLuint index,GLint64 * param)1399 _mesa_GetTransformFeedbacki64_v(GLuint xfb, GLenum pname, GLuint index,
1400                                 GLint64 *param)
1401 {
1402    struct gl_transform_feedback_object *obj;
1403    GET_CURRENT_CONTEXT(ctx);
1404 
1405    obj = lookup_transform_feedback_object_err(ctx, xfb,
1406                                               "glGetTransformFeedbacki64_v");
1407    if (!obj) {
1408       return;
1409    }
1410 
1411    if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
1412       _mesa_error(ctx, GL_INVALID_VALUE,
1413                   "glGetTransformFeedbacki64_v(index=%i)", index);
1414       return;
1415    }
1416 
1417    /**
1418     * This follows the same general rules used for BindBufferBase:
1419     *
1420     *   "To query the starting offset or size of the range of a buffer
1421     *    object binding in an indexed array, call GetInteger64i_v with
1422     *    target set to respectively the starting offset or binding size
1423     *    name from table 6.5 for that array. Index must be in the range
1424     *    zero to the number of bind points supported minus one. If the
1425     *    starting offset or size was not specified when the buffer object
1426     *    was bound (e.g. if it was bound with BindBufferBase), or if no
1427     *    buffer object is bound to the target array at index, zero is
1428     *    returned."
1429     */
1430    if (obj->RequestedSize[index] == 0 &&
1431        (pname == GL_TRANSFORM_FEEDBACK_BUFFER_START ||
1432         pname == GL_TRANSFORM_FEEDBACK_BUFFER_SIZE)) {
1433       *param = 0;
1434       return;
1435    }
1436 
1437    compute_transform_feedback_buffer_sizes(obj);
1438    switch(pname) {
1439    case GL_TRANSFORM_FEEDBACK_BUFFER_START:
1440       assert(obj->RequestedSize[index] > 0);
1441       *param = obj->Offset[index];
1442       break;
1443    case GL_TRANSFORM_FEEDBACK_BUFFER_SIZE:
1444       assert(obj->RequestedSize[index] > 0);
1445       *param = obj->Size[index];
1446       break;
1447    default:
1448       _mesa_error(ctx, GL_INVALID_ENUM,
1449                   "glGetTransformFeedbacki64_v(pname=%i)", pname);
1450    }
1451 }
1452