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