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