2 * Mesa 3-D graphics library
4 * Copyright (C) 2010 VMware, Inc. All Rights Reserved.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
27 * Transform feedback support.
39 #include "transformfeedback.h"
40 #include "shaderapi.h"
41 #include "shaderobj.h"
43 #include "program/program.h"
44 #include "program/prog_parameter.h"
46 struct using_program_tuple
48 struct gl_program
*prog
;
53 active_xfb_object_references_program(GLuint key
, void *data
, void *user_data
)
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;
62 * Return true if any active transform feedback object is using a program.
65 _mesa_transform_feedback_is_using_program(struct gl_context
*ctx
,
66 struct gl_shader_program
*shProg
)
68 if (!shProg
->last_vert_prog
)
71 struct using_program_tuple callback_data
;
72 callback_data
.found
= false;
73 callback_data
.prog
= shProg
->last_vert_prog
;
75 _mesa_HashWalkLocked(ctx
->TransformFeedback
.Objects
,
76 active_xfb_object_references_program
, &callback_data
);
78 /* Also check DefaultObject, as it's not in the Objects hash table. */
79 active_xfb_object_references_program(0, ctx
->TransformFeedback
.DefaultObject
,
82 return callback_data
.found
;
86 * Do reference counting of transform feedback buffers.
89 reference_transform_feedback_object(struct gl_transform_feedback_object
**ptr
,
90 struct gl_transform_feedback_object
*obj
)
96 /* Unreference the old object */
97 struct gl_transform_feedback_object
*oldObj
= *ptr
;
99 assert(oldObj
->RefCount
> 0);
102 if (oldObj
->RefCount
== 0) {
103 GET_CURRENT_CONTEXT(ctx
);
105 ctx
->Driver
.DeleteTransformFeedback(ctx
, oldObj
);
113 assert(obj
->RefCount
> 0);
115 /* reference new object */
117 obj
->EverBound
= GL_TRUE
;
124 * Per-context init for transform feedback.
127 _mesa_init_transform_feedback(struct gl_context
*ctx
)
129 /* core mesa expects this, even a dummy one, to be available */
130 assert(ctx
->Driver
.NewTransformFeedback
);
132 ctx
->TransformFeedback
.DefaultObject
=
133 ctx
->Driver
.NewTransformFeedback(ctx
, 0);
135 assert(ctx
->TransformFeedback
.DefaultObject
->RefCount
== 1);
137 reference_transform_feedback_object(&ctx
->TransformFeedback
.CurrentObject
,
138 ctx
->TransformFeedback
.DefaultObject
);
140 assert(ctx
->TransformFeedback
.DefaultObject
->RefCount
== 2);
142 ctx
->TransformFeedback
.Objects
= _mesa_NewHashTable();
144 _mesa_reference_buffer_object(ctx
,
145 &ctx
->TransformFeedback
.CurrentBuffer
,
146 ctx
->Shared
->NullBufferObj
);
152 * Callback for _mesa_HashDeleteAll().
155 delete_cb(GLuint key
, void *data
, void *userData
)
157 struct gl_context
*ctx
= (struct gl_context
*) userData
;
158 struct gl_transform_feedback_object
*obj
=
159 (struct gl_transform_feedback_object
*) data
;
161 ctx
->Driver
.DeleteTransformFeedback(ctx
, obj
);
166 * Per-context free/clean-up for transform feedback.
169 _mesa_free_transform_feedback(struct gl_context
*ctx
)
171 /* core mesa expects this, even a dummy one, to be available */
172 assert(ctx
->Driver
.NewTransformFeedback
);
174 _mesa_reference_buffer_object(ctx
,
175 &ctx
->TransformFeedback
.CurrentBuffer
,
178 /* Delete all feedback objects */
179 _mesa_HashDeleteAll(ctx
->TransformFeedback
.Objects
, delete_cb
, ctx
);
180 _mesa_DeleteHashTable(ctx
->TransformFeedback
.Objects
);
182 /* Delete the default feedback object */
183 assert(ctx
->Driver
.DeleteTransformFeedback
);
184 ctx
->Driver
.DeleteTransformFeedback(ctx
,
185 ctx
->TransformFeedback
.DefaultObject
);
187 ctx
->TransformFeedback
.CurrentObject
= NULL
;
191 /** Initialize the fields of a gl_transform_feedback_object. */
193 _mesa_init_transform_feedback_object(struct gl_transform_feedback_object
*obj
,
198 obj
->EverBound
= GL_FALSE
;
202 * Delete a transform feedback object. Called via
203 * ctx->Driver->DeleteTransformFeedback, if not overwritten by driver. In
204 * the latter case, called from the driver after all driver-specific clean-up
207 * \param ctx GL context to wich transform feedback object belongs.
208 * \param obj Transform feedback object due to be deleted.
211 _mesa_delete_transform_feedback_object(struct gl_context
*ctx
,
212 struct gl_transform_feedback_object
215 for (unsigned i
= 0; i
< ARRAY_SIZE(obj
->Buffers
); i
++) {
216 _mesa_reference_buffer_object(ctx
, &obj
->Buffers
[i
], NULL
);
223 /** Default fallback for ctx->Driver.NewTransformFeedback() */
224 static struct gl_transform_feedback_object
*
225 new_transform_feedback_fallback(struct gl_context
*ctx
, GLuint name
)
227 struct gl_transform_feedback_object
*obj
;
229 obj
= CALLOC_STRUCT(gl_transform_feedback_object
);
233 _mesa_init_transform_feedback_object(obj
, name
);
237 /** Default fallback for ctx->Driver.BeginTransformFeedback() */
239 begin_transform_feedback_fallback(struct gl_context
*ctx
, GLenum mode
,
240 struct gl_transform_feedback_object
*obj
)
245 /** Default fallback for ctx->Driver.EndTransformFeedback() */
247 end_transform_feedback_fallback(struct gl_context
*ctx
,
248 struct gl_transform_feedback_object
*obj
)
253 /** Default fallback for ctx->Driver.PauseTransformFeedback() */
255 pause_transform_feedback_fallback(struct gl_context
*ctx
,
256 struct gl_transform_feedback_object
*obj
)
261 /** Default fallback for ctx->Driver.ResumeTransformFeedback() */
263 resume_transform_feedback_fallback(struct gl_context
*ctx
,
264 struct gl_transform_feedback_object
*obj
)
271 * Plug in default device driver functions for transform feedback.
272 * Most drivers will override some/all of these.
275 _mesa_init_transform_feedback_functions(struct dd_function_table
*driver
)
277 driver
->NewTransformFeedback
= new_transform_feedback_fallback
;
278 driver
->DeleteTransformFeedback
= _mesa_delete_transform_feedback_object
;
279 driver
->BeginTransformFeedback
= begin_transform_feedback_fallback
;
280 driver
->EndTransformFeedback
= end_transform_feedback_fallback
;
281 driver
->PauseTransformFeedback
= pause_transform_feedback_fallback
;
282 driver
->ResumeTransformFeedback
= resume_transform_feedback_fallback
;
287 * Fill in the correct Size value for each buffer in \c obj.
289 * From the GL 4.3 spec, section 6.1.1 ("Binding Buffer Objects to Indexed
292 * BindBufferBase binds the entire buffer, even when the size of the buffer
293 * is changed after the binding is established. It is equivalent to calling
294 * BindBufferRange with offset zero, while size is determined by the size of
295 * the bound buffer at the time the binding is used.
297 * Regardless of the size specified with BindBufferRange, or indirectly with
298 * BindBufferBase, the GL will never read or write beyond the end of a bound
299 * buffer. In some cases this constraint may result in visibly different
300 * behavior when a buffer overflow would otherwise result, such as described
301 * for transform feedback operations in section 13.2.2.
304 compute_transform_feedback_buffer_sizes(
305 struct gl_transform_feedback_object
*obj
)
308 for (i
= 0; i
< MAX_FEEDBACK_BUFFERS
; ++i
) {
309 GLintptr offset
= obj
->Offset
[i
];
310 GLsizeiptr buffer_size
311 = obj
->Buffers
[i
] == NULL
? 0 : obj
->Buffers
[i
]->Size
;
312 GLsizeiptr available_space
313 = buffer_size
<= offset
? 0 : buffer_size
- offset
;
314 GLsizeiptr computed_size
;
315 if (obj
->RequestedSize
[i
] == 0) {
316 /* No size was specified at the time the buffer was bound, so allow
317 * writing to all available space in the buffer.
319 computed_size
= available_space
;
321 /* A size was specified at the time the buffer was bound, however
322 * it's possible that the buffer has shrunk since then. So only
323 * allow writing to the minimum of the specified size and the space
326 computed_size
= MIN2(available_space
, obj
->RequestedSize
[i
]);
329 /* Legal sizes must be multiples of four, so round down if necessary. */
330 obj
->Size
[i
] = computed_size
& ~0x3;
336 * Compute the maximum number of vertices that can be written to the currently
337 * enabled transform feedback buffers without overflowing any of them.
340 _mesa_compute_max_transform_feedback_vertices(struct gl_context
*ctx
,
341 const struct gl_transform_feedback_object
*obj
,
342 const struct gl_transform_feedback_info
*info
)
344 unsigned max_index
= 0xffffffff;
347 for (i
= 0; i
< ctx
->Const
.MaxTransformFeedbackBuffers
; i
++) {
348 if ((info
->ActiveBuffers
>> i
) & 1) {
349 unsigned stride
= info
->Buffers
[i
].Stride
;
350 unsigned max_for_this_buffer
;
352 /* Skip any inactive buffers, which have a stride of 0. */
356 max_for_this_buffer
= obj
->Size
[i
] / (4 * stride
);
357 max_index
= MIN2(max_index
, max_for_this_buffer
);
366 ** Begin API functions
371 * Figure out which stage of the pipeline is the source of transform feedback
372 * data given the current context state, and return its gl_program.
374 * If no active program can generate transform feedback data (i.e. no vertex
375 * shader is active), returns NULL.
377 static struct gl_program
*
378 get_xfb_source(struct gl_context
*ctx
)
381 for (i
= MESA_SHADER_GEOMETRY
; i
>= MESA_SHADER_VERTEX
; i
--) {
382 if (ctx
->_Shader
->CurrentProgram
[i
] != NULL
)
383 return ctx
->_Shader
->CurrentProgram
[i
];
389 static ALWAYS_INLINE
void
390 begin_transform_feedback(struct gl_context
*ctx
, GLenum mode
, bool no_error
)
392 struct gl_transform_feedback_object
*obj
;
393 struct gl_transform_feedback_info
*info
= NULL
;
394 struct gl_program
*source
;
396 unsigned vertices_per_prim
;
398 obj
= ctx
->TransformFeedback
.CurrentObject
;
400 /* Figure out what pipeline stage is the source of data for transform
403 source
= get_xfb_source(ctx
);
404 if (!no_error
&& source
== NULL
) {
405 _mesa_error(ctx
, GL_INVALID_OPERATION
,
406 "glBeginTransformFeedback(no program active)");
410 info
= source
->sh
.LinkedTransformFeedback
;
412 if (!no_error
&& info
->NumOutputs
== 0) {
413 _mesa_error(ctx
, GL_INVALID_OPERATION
,
414 "glBeginTransformFeedback(no varyings to record)");
420 vertices_per_prim
= 1;
423 vertices_per_prim
= 2;
426 vertices_per_prim
= 3;
430 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginTransformFeedback(mode)");
433 /* Stop compiler warnings */
434 unreachable("Error in API use when using KHR_no_error");
440 _mesa_error(ctx
, GL_INVALID_OPERATION
,
441 "glBeginTransformFeedback(already active)");
445 for (i
= 0; i
< ctx
->Const
.MaxTransformFeedbackBuffers
; i
++) {
446 if ((info
->ActiveBuffers
>> i
) & 1) {
447 if (obj
->BufferNames
[i
] == 0) {
448 _mesa_error(ctx
, GL_INVALID_OPERATION
,
449 "glBeginTransformFeedback(binding point %d does not "
450 "have a buffer object bound)", i
);
457 FLUSH_VERTICES(ctx
, 0);
458 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
460 obj
->Active
= GL_TRUE
;
461 ctx
->TransformFeedback
.Mode
= mode
;
463 compute_transform_feedback_buffer_sizes(obj
);
465 if (_mesa_is_gles3(ctx
)) {
466 /* In GLES3, we are required to track the usage of the transform
467 * feedback buffer and report INVALID_OPERATION if a draw call tries to
468 * exceed it. So compute the maximum number of vertices that we can
469 * write without overflowing any of the buffers currently being used for
472 unsigned max_vertices
473 = _mesa_compute_max_transform_feedback_vertices(ctx
, obj
, info
);
474 obj
->GlesRemainingPrims
= max_vertices
/ vertices_per_prim
;
477 if (obj
->program
!= source
) {
478 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedbackProg
;
479 _mesa_reference_program_(ctx
, &obj
->program
, source
);
480 obj
->program
= source
;
483 assert(ctx
->Driver
.BeginTransformFeedback
);
484 ctx
->Driver
.BeginTransformFeedback(ctx
, mode
, obj
);
489 _mesa_BeginTransformFeedback_no_error(GLenum mode
)
491 GET_CURRENT_CONTEXT(ctx
);
492 begin_transform_feedback(ctx
, mode
, true);
497 _mesa_BeginTransformFeedback(GLenum mode
)
499 GET_CURRENT_CONTEXT(ctx
);
500 begin_transform_feedback(ctx
, mode
, false);
505 end_transform_feedback(struct gl_context
*ctx
,
506 struct gl_transform_feedback_object
*obj
)
508 FLUSH_VERTICES(ctx
, 0);
509 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
511 assert(ctx
->Driver
.EndTransformFeedback
);
512 ctx
->Driver
.EndTransformFeedback(ctx
, obj
);
514 _mesa_reference_program_(ctx
, &obj
->program
, NULL
);
515 ctx
->TransformFeedback
.CurrentObject
->Active
= GL_FALSE
;
516 ctx
->TransformFeedback
.CurrentObject
->Paused
= GL_FALSE
;
517 ctx
->TransformFeedback
.CurrentObject
->EndedAnytime
= GL_TRUE
;
522 _mesa_EndTransformFeedback_no_error(void)
524 GET_CURRENT_CONTEXT(ctx
);
525 end_transform_feedback(ctx
, ctx
->TransformFeedback
.CurrentObject
);
530 _mesa_EndTransformFeedback(void)
532 struct gl_transform_feedback_object
*obj
;
533 GET_CURRENT_CONTEXT(ctx
);
535 obj
= ctx
->TransformFeedback
.CurrentObject
;
538 _mesa_error(ctx
, GL_INVALID_OPERATION
,
539 "glEndTransformFeedback(not active)");
543 end_transform_feedback(ctx
, obj
);
548 * Helper used by BindBufferRange() and BindBufferBase().
551 bind_buffer_range(struct gl_context
*ctx
,
552 struct gl_transform_feedback_object
*obj
,
554 struct gl_buffer_object
*bufObj
,
555 GLintptr offset
, GLsizeiptr size
,
558 /* Note: no need to FLUSH_VERTICES or flag NewTransformFeedback, because
559 * transform feedback buffers can't be changed while transform feedback is
564 /* The general binding point */
565 _mesa_reference_buffer_object(ctx
,
566 &ctx
->TransformFeedback
.CurrentBuffer
,
570 /* The per-attribute binding point */
571 _mesa_set_transform_feedback_binding(ctx
, obj
, index
, bufObj
, offset
, size
);
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
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
)
587 const char *gl_methd_name
;
589 gl_methd_name
= "glTransformFeedbackBufferRange";
591 gl_methd_name
= "glBindBufferRange";
595 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(transform feedback active)",
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."
605 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d out of bounds)",
606 gl_methd_name
, index
);
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
);
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
);
625 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
626 * generated by BindBufferRange if offset is negative."
628 * OpenGL 4.5 core profile, 13.2, pdf page 445: "An INVALID_VALUE error
629 * is generated by TransformFeedbackBufferRange if offset is negative."
631 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset=%d must be >= 0)",
637 if (size
<= 0 && (dsa
|| bufObj
!= ctx
->Shared
->NullBufferObj
)) {
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."
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
646 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d must be > 0)",
647 gl_methd_name
, (int) size
);
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()
662 _mesa_bind_buffer_base_transform_feedback(struct gl_context
*ctx
,
663 struct gl_transform_feedback_object
*obj
,
665 struct gl_buffer_object
*bufObj
,
669 _mesa_error(ctx
, GL_INVALID_OPERATION
,
670 "%s(transform feedback active)",
671 dsa
? "glTransformFeedbackBufferBase" : "glBindBufferBase");
675 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
676 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d out of bounds)",
677 dsa
? "glTransformFeedbackBufferBase" : "glBindBufferBase",
682 bind_buffer_range(ctx
, obj
, index
, bufObj
, 0, 0, dsa
);
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.
690 static struct gl_transform_feedback_object
*
691 lookup_transform_feedback_object_err(struct gl_context
*ctx
,
692 GLuint xfb
, const char* func
)
694 struct gl_transform_feedback_object
*obj
;
696 obj
= _mesa_lookup_transform_feedback_object(ctx
, xfb
);
698 _mesa_error(ctx
, GL_INVALID_OPERATION
,
699 "%s(xfb=%u: non-generated object name)", func
, xfb
);
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
711 static struct gl_buffer_object
*
712 lookup_transform_feedback_bufferobj_err(struct gl_context
*ctx
,
713 GLuint buffer
, const char* func
)
715 struct gl_buffer_object
*bufObj
;
717 /* OpenGL 4.5 core profile, 13.2, pdf page 444: buffer must be zero or the
718 * name of an existing buffer object.
721 bufObj
= ctx
->Shared
->NullBufferObj
;
723 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
725 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid buffer=%u)", func
,
734 _mesa_TransformFeedbackBufferBase(GLuint xfb
, GLuint index
, GLuint buffer
)
736 GET_CURRENT_CONTEXT(ctx
);
737 struct gl_transform_feedback_object
*obj
;
738 struct gl_buffer_object
*bufObj
;
740 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
741 "glTransformFeedbackBufferBase");
746 bufObj
= lookup_transform_feedback_bufferobj_err(ctx
, buffer
,
747 "glTransformFeedbackBufferBase");
752 _mesa_bind_buffer_base_transform_feedback(ctx
, obj
, index
, bufObj
, true);
756 _mesa_TransformFeedbackBufferRange(GLuint xfb
, GLuint index
, GLuint buffer
,
757 GLintptr offset
, GLsizeiptr size
)
759 GET_CURRENT_CONTEXT(ctx
);
760 struct gl_transform_feedback_object
*obj
;
761 struct gl_buffer_object
*bufObj
;
763 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
764 "glTransformFeedbackBufferRange");
769 bufObj
= lookup_transform_feedback_bufferobj_err(ctx
, buffer
,
770 "glTransformFeedbackBufferRange");
775 if (!_mesa_validate_buffer_range_xfb(ctx
, obj
, index
, bufObj
, offset
,
779 /* The per-attribute binding point */
780 _mesa_set_transform_feedback_binding(ctx
, obj
, index
, bufObj
, offset
,
785 * Specify a buffer object to receive transform feedback results, plus the
786 * offset in the buffer to start placing results.
787 * This function is part of GL_EXT_transform_feedback, but not GL3.
789 static ALWAYS_INLINE
void
790 bind_buffer_offset(struct gl_context
*ctx
,
791 struct gl_transform_feedback_object
*obj
, GLuint index
,
792 GLuint buffer
, GLintptr offset
, bool no_error
)
794 struct gl_buffer_object
*bufObj
;
797 bufObj
= ctx
->Shared
->NullBufferObj
;
799 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
800 if (!no_error
&& !bufObj
) {
801 _mesa_error(ctx
, GL_INVALID_OPERATION
,
802 "glBindBufferOffsetEXT(invalid buffer=%u)", buffer
);
807 _mesa_bind_buffer_range_xfb(ctx
, obj
, index
, bufObj
, offset
, 0);
812 _mesa_BindBufferOffsetEXT_no_error(GLenum target
, GLuint index
, GLuint buffer
,
815 GET_CURRENT_CONTEXT(ctx
);
816 bind_buffer_offset(ctx
, ctx
->TransformFeedback
.CurrentObject
, index
, buffer
,
822 _mesa_BindBufferOffsetEXT(GLenum target
, GLuint index
, GLuint buffer
,
825 struct gl_transform_feedback_object
*obj
;
826 GET_CURRENT_CONTEXT(ctx
);
828 if (target
!= GL_TRANSFORM_FEEDBACK_BUFFER
) {
829 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferOffsetEXT(target)");
833 obj
= ctx
->TransformFeedback
.CurrentObject
;
836 _mesa_error(ctx
, GL_INVALID_OPERATION
,
837 "glBindBufferOffsetEXT(transform feedback active)");
841 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
842 _mesa_error(ctx
, GL_INVALID_VALUE
,
843 "glBindBufferOffsetEXT(index=%d)", index
);
848 /* must be multiple of four */
849 _mesa_error(ctx
, GL_INVALID_VALUE
,
850 "glBindBufferOffsetEXT(offset=%d)", (int) offset
);
854 bind_buffer_offset(ctx
, obj
, index
, buffer
, offset
, false);
859 * This function specifies the transform feedback outputs to be written
860 * to the feedback buffer(s), and in what order.
862 static ALWAYS_INLINE
void
863 transform_feedback_varyings(struct gl_context
*ctx
,
864 struct gl_shader_program
*shProg
, GLsizei count
,
865 const GLchar
*const *varyings
, GLenum bufferMode
)
869 /* free existing varyings, if any */
870 for (i
= 0; i
< (GLint
) shProg
->TransformFeedback
.NumVarying
; i
++) {
871 free(shProg
->TransformFeedback
.VaryingNames
[i
]);
873 free(shProg
->TransformFeedback
.VaryingNames
);
875 /* allocate new memory for varying names */
876 shProg
->TransformFeedback
.VaryingNames
=
877 malloc(count
* sizeof(GLchar
*));
879 if (!shProg
->TransformFeedback
.VaryingNames
) {
880 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTransformFeedbackVaryings()");
884 /* Save the new names and the count */
885 for (i
= 0; i
< count
; i
++) {
886 shProg
->TransformFeedback
.VaryingNames
[i
] = strdup(varyings
[i
]);
888 shProg
->TransformFeedback
.NumVarying
= count
;
890 shProg
->TransformFeedback
.BufferMode
= bufferMode
;
892 /* No need to invoke FLUSH_VERTICES or flag NewTransformFeedback since
893 * the varyings won't be used until shader link time.
899 _mesa_TransformFeedbackVaryings_no_error(GLuint program
, GLsizei count
,
900 const GLchar
*const *varyings
,
903 GET_CURRENT_CONTEXT(ctx
);
905 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, program
);
906 transform_feedback_varyings(ctx
, shProg
, count
, varyings
, bufferMode
);
910 _mesa_TransformFeedbackVaryings(GLuint program
, GLsizei count
,
911 const GLchar
* const *varyings
,
914 struct gl_shader_program
*shProg
;
916 GET_CURRENT_CONTEXT(ctx
);
918 /* From the ARB_transform_feedback2 specification:
919 * "The error INVALID_OPERATION is generated by TransformFeedbackVaryings
920 * if the current transform feedback object is active, even if paused."
922 if (ctx
->TransformFeedback
.CurrentObject
->Active
) {
923 _mesa_error(ctx
, GL_INVALID_OPERATION
,
924 "glTransformFeedbackVaryings(current object is active)");
928 switch (bufferMode
) {
929 case GL_INTERLEAVED_ATTRIBS
:
931 case GL_SEPARATE_ATTRIBS
:
934 _mesa_error(ctx
, GL_INVALID_ENUM
,
935 "glTransformFeedbackVaryings(bufferMode)");
940 (bufferMode
== GL_SEPARATE_ATTRIBS
&&
941 (GLuint
) count
> ctx
->Const
.MaxTransformFeedbackBuffers
)) {
942 _mesa_error(ctx
, GL_INVALID_VALUE
,
943 "glTransformFeedbackVaryings(count=%d)", count
);
947 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
948 "glTransformFeedbackVaryings");
952 if (ctx
->Extensions
.ARB_transform_feedback3
) {
953 if (bufferMode
== GL_INTERLEAVED_ATTRIBS
) {
954 unsigned buffers
= 1;
956 for (i
= 0; i
< count
; i
++) {
957 if (strcmp(varyings
[i
], "gl_NextBuffer") == 0)
961 if (buffers
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
962 _mesa_error(ctx
, GL_INVALID_OPERATION
,
963 "glTransformFeedbackVaryings(too many gl_NextBuffer "
968 for (i
= 0; i
< count
; i
++) {
969 if (strcmp(varyings
[i
], "gl_NextBuffer") == 0 ||
970 strcmp(varyings
[i
], "gl_SkipComponents1") == 0 ||
971 strcmp(varyings
[i
], "gl_SkipComponents2") == 0 ||
972 strcmp(varyings
[i
], "gl_SkipComponents3") == 0 ||
973 strcmp(varyings
[i
], "gl_SkipComponents4") == 0) {
974 _mesa_error(ctx
, GL_INVALID_OPERATION
,
975 "glTransformFeedbackVaryings(SEPARATE_ATTRIBS,"
984 transform_feedback_varyings(ctx
, shProg
, count
, varyings
, bufferMode
);
989 * Get info about the transform feedback outputs which are to be written
990 * to the feedback buffer(s).
993 _mesa_GetTransformFeedbackVarying(GLuint program
, GLuint index
,
994 GLsizei bufSize
, GLsizei
*length
,
995 GLsizei
*size
, GLenum
*type
, GLchar
*name
)
997 const struct gl_shader_program
*shProg
;
998 struct gl_program_resource
*res
;
999 GET_CURRENT_CONTEXT(ctx
);
1001 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1002 "glGetTransformFeedbackVarying");
1006 res
= _mesa_program_resource_find_index((struct gl_shader_program
*) shProg
,
1007 GL_TRANSFORM_FEEDBACK_VARYING
,
1010 _mesa_error(ctx
, GL_INVALID_VALUE
,
1011 "glGetTransformFeedbackVarying(index=%u)", index
);
1015 /* return the varying's name and length */
1016 _mesa_copy_string(name
, bufSize
, length
, _mesa_program_resource_name(res
));
1018 /* return the datatype and value's size (in datatype units) */
1020 _mesa_program_resource_prop((struct gl_shader_program
*) shProg
,
1021 res
, index
, GL_TYPE
, (GLint
*) type
,
1022 "glGetTransformFeedbackVarying");
1024 _mesa_program_resource_prop((struct gl_shader_program
*) shProg
,
1025 res
, index
, GL_ARRAY_SIZE
, (GLint
*) size
,
1026 "glGetTransformFeedbackVarying");
1031 struct gl_transform_feedback_object
*
1032 _mesa_lookup_transform_feedback_object(struct gl_context
*ctx
, GLuint name
)
1034 /* OpenGL 4.5 core profile, 13.2 pdf page 444: "xfb must be zero, indicating
1035 * the default transform feedback object, or the name of an existing
1036 * transform feedback object."
1039 return ctx
->TransformFeedback
.DefaultObject
;
1042 return (struct gl_transform_feedback_object
*)
1043 _mesa_HashLookupLocked(ctx
->TransformFeedback
.Objects
, name
);
1047 create_transform_feedbacks(struct gl_context
*ctx
, GLsizei n
, GLuint
*ids
,
1054 func
= "glCreateTransformFeedbacks";
1056 func
= "glGenTransformFeedbacks";
1059 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", func
);
1066 /* we don't need contiguous IDs, but this might be faster */
1067 first
= _mesa_HashFindFreeKeyBlock(ctx
->TransformFeedback
.Objects
, n
);
1070 for (i
= 0; i
< n
; i
++) {
1071 struct gl_transform_feedback_object
*obj
1072 = ctx
->Driver
.NewTransformFeedback(ctx
, first
+ i
);
1074 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1078 _mesa_HashInsertLocked(ctx
->TransformFeedback
.Objects
, first
+ i
,
1081 /* this is normally done at bind time in the non-dsa case */
1082 obj
->EverBound
= GL_TRUE
;
1087 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
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.
1098 _mesa_GenTransformFeedbacks(GLsizei n
, GLuint
*names
)
1100 GET_CURRENT_CONTEXT(ctx
);
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
1108 create_transform_feedbacks(ctx
, n
, names
, false);
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.
1118 _mesa_CreateTransformFeedbacks(GLsizei n
, GLuint
*names
)
1120 GET_CURRENT_CONTEXT(ctx
);
1122 create_transform_feedbacks(ctx
, n
, names
, true);
1127 * Is the given ID a transform feedback object?
1128 * Part of GL_ARB_transform_feedback2.
1130 GLboolean GLAPIENTRY
1131 _mesa_IsTransformFeedback(GLuint name
)
1133 struct gl_transform_feedback_object
*obj
;
1134 GET_CURRENT_CONTEXT(ctx
);
1136 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1141 obj
= _mesa_lookup_transform_feedback_object(ctx
, name
);
1145 return obj
->EverBound
;
1150 * Bind the given transform feedback object.
1151 * Part of GL_ARB_transform_feedback2.
1153 static ALWAYS_INLINE
void
1154 bind_transform_feedback(struct gl_context
*ctx
, GLuint name
, bool no_error
)
1156 struct gl_transform_feedback_object
*obj
;
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
);
1165 reference_transform_feedback_object(&ctx
->TransformFeedback
.CurrentObject
,
1171 _mesa_BindTransformFeedback_no_error(GLenum target
, GLuint name
)
1173 GET_CURRENT_CONTEXT(ctx
);
1174 bind_transform_feedback(ctx
, name
, true);
1179 _mesa_BindTransformFeedback(GLenum target
, GLuint name
)
1181 GET_CURRENT_CONTEXT(ctx
);
1183 if (target
!= GL_TRANSFORM_FEEDBACK
) {
1184 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindTransformFeedback(target)");
1188 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1189 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1190 "glBindTransformFeedback(transform is active, or not paused)");
1194 bind_transform_feedback(ctx
, name
, false);
1199 * Delete the given transform feedback objects.
1200 * Part of GL_ARB_transform_feedback2.
1203 _mesa_DeleteTransformFeedbacks(GLsizei n
, const GLuint
*names
)
1206 GET_CURRENT_CONTEXT(ctx
);
1209 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteTransformFeedbacks(n < 0)");
1216 for (i
= 0; i
< n
; i
++) {
1218 struct gl_transform_feedback_object
*obj
1219 = _mesa_lookup_transform_feedback_object(ctx
, names
[i
]);
1222 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1223 "glDeleteTransformFeedbacks(object %u is active)",
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
);
1234 reference_transform_feedback_object(&obj
, NULL
);
1242 * Pause transform feedback.
1243 * Part of GL_ARB_transform_feedback2.
1246 pause_transform_feedback(struct gl_context
*ctx
,
1247 struct gl_transform_feedback_object
*obj
)
1249 FLUSH_VERTICES(ctx
, 0);
1250 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
1252 assert(ctx
->Driver
.PauseTransformFeedback
);
1253 ctx
->Driver
.PauseTransformFeedback(ctx
, obj
);
1255 obj
->Paused
= GL_TRUE
;
1260 _mesa_PauseTransformFeedback_no_error(void)
1262 GET_CURRENT_CONTEXT(ctx
);
1263 pause_transform_feedback(ctx
, ctx
->TransformFeedback
.CurrentObject
);
1268 _mesa_PauseTransformFeedback(void)
1270 struct gl_transform_feedback_object
*obj
;
1271 GET_CURRENT_CONTEXT(ctx
);
1273 obj
= ctx
->TransformFeedback
.CurrentObject
;
1275 if (!_mesa_is_xfb_active_and_unpaused(ctx
)) {
1276 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1277 "glPauseTransformFeedback(feedback not active or already paused)");
1281 pause_transform_feedback(ctx
, obj
);
1286 * Resume transform feedback.
1287 * Part of GL_ARB_transform_feedback2.
1290 resume_transform_feedback(struct gl_context
*ctx
,
1291 struct gl_transform_feedback_object
*obj
)
1293 FLUSH_VERTICES(ctx
, 0);
1294 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
1296 obj
->Paused
= GL_FALSE
;
1298 assert(ctx
->Driver
.ResumeTransformFeedback
);
1299 ctx
->Driver
.ResumeTransformFeedback(ctx
, obj
);
1304 _mesa_ResumeTransformFeedback_no_error(void)
1306 GET_CURRENT_CONTEXT(ctx
);
1307 resume_transform_feedback(ctx
, ctx
->TransformFeedback
.CurrentObject
);
1312 _mesa_ResumeTransformFeedback(void)
1314 struct gl_transform_feedback_object
*obj
;
1315 GET_CURRENT_CONTEXT(ctx
);
1317 obj
= ctx
->TransformFeedback
.CurrentObject
;
1319 if (!obj
->Active
|| !obj
->Paused
) {
1320 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1321 "glResumeTransformFeedback(feedback not active or not paused)");
1325 /* From the ARB_transform_feedback2 specification:
1326 * "The error INVALID_OPERATION is generated by ResumeTransformFeedback if
1327 * the program object being used by the current transform feedback object
1330 if (obj
->program
!= get_xfb_source(ctx
)) {
1331 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1332 "glResumeTransformFeedback(wrong program bound)");
1336 resume_transform_feedback(ctx
, obj
);
1339 extern void GLAPIENTRY
1340 _mesa_GetTransformFeedbackiv(GLuint xfb
, GLenum pname
, GLint
*param
)
1342 struct gl_transform_feedback_object
*obj
;
1343 GET_CURRENT_CONTEXT(ctx
);
1345 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
1346 "glGetTransformFeedbackiv");
1352 case GL_TRANSFORM_FEEDBACK_PAUSED
:
1353 *param
= obj
->Paused
;
1355 case GL_TRANSFORM_FEEDBACK_ACTIVE
:
1356 *param
= obj
->Active
;
1359 _mesa_error(ctx
, GL_INVALID_ENUM
,
1360 "glGetTransformFeedbackiv(pname=%i)", pname
);
1364 extern void GLAPIENTRY
1365 _mesa_GetTransformFeedbacki_v(GLuint xfb
, GLenum pname
, GLuint index
,
1368 struct gl_transform_feedback_object
*obj
;
1369 GET_CURRENT_CONTEXT(ctx
);
1371 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
1372 "glGetTransformFeedbacki_v");
1377 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
1378 _mesa_error(ctx
, GL_INVALID_VALUE
,
1379 "glGetTransformFeedbacki_v(index=%i)", index
);
1384 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING
:
1385 *param
= obj
->BufferNames
[index
];
1388 _mesa_error(ctx
, GL_INVALID_ENUM
,
1389 "glGetTransformFeedbacki_v(pname=%i)", pname
);
1393 extern void GLAPIENTRY
1394 _mesa_GetTransformFeedbacki64_v(GLuint xfb
, GLenum pname
, GLuint index
,
1397 struct gl_transform_feedback_object
*obj
;
1398 GET_CURRENT_CONTEXT(ctx
);
1400 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
1401 "glGetTransformFeedbacki64_v");
1406 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
1407 _mesa_error(ctx
, GL_INVALID_VALUE
,
1408 "glGetTransformFeedbacki64_v(index=%i)", index
);
1413 * This follows the same general rules used for BindBufferBase:
1415 * "To query the starting offset or size of the range of a buffer
1416 * object binding in an indexed array, call GetInteger64i_v with
1417 * target set to respectively the starting offset or binding size
1418 * name from table 6.5 for that array. Index must be in the range
1419 * zero to the number of bind points supported minus one. If the
1420 * starting offset or size was not specified when the buffer object
1421 * was bound (e.g. if it was bound with BindBufferBase), or if no
1422 * buffer object is bound to the target array at index, zero is
1425 if (obj
->RequestedSize
[index
] == 0 &&
1426 (pname
== GL_TRANSFORM_FEEDBACK_BUFFER_START
||
1427 pname
== GL_TRANSFORM_FEEDBACK_BUFFER_SIZE
)) {
1432 compute_transform_feedback_buffer_sizes(obj
);
1434 case GL_TRANSFORM_FEEDBACK_BUFFER_START
:
1435 assert(obj
->RequestedSize
[index
] > 0);
1436 *param
= obj
->Offset
[index
];
1438 case GL_TRANSFORM_FEEDBACK_BUFFER_SIZE
:
1439 assert(obj
->RequestedSize
[index
] > 0);
1440 *param
= obj
->Size
[index
];
1443 _mesa_error(ctx
, GL_INVALID_ENUM
,
1444 "glGetTransformFeedbacki64_v(pname=%i)", pname
);