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"
42 #include "main/dispatch.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_HashWalk(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 /* reference new object */
114 if (obj
->RefCount
== 0) {
115 _mesa_problem(NULL
, "referencing deleted transform feedback object");
120 obj
->EverBound
= GL_TRUE
;
128 * Check that all the buffer objects currently bound for transform
129 * feedback actually exist. Raise a GL_INVALID_OPERATION error if
130 * any buffers are missing.
131 * \return GL_TRUE for success, GL_FALSE if error
134 _mesa_validate_transform_feedback_buffers(struct gl_context
*ctx
)
143 * Per-context init for transform feedback.
146 _mesa_init_transform_feedback(struct gl_context
*ctx
)
148 /* core mesa expects this, even a dummy one, to be available */
149 assert(ctx
->Driver
.NewTransformFeedback
);
151 ctx
->TransformFeedback
.DefaultObject
=
152 ctx
->Driver
.NewTransformFeedback(ctx
, 0);
154 assert(ctx
->TransformFeedback
.DefaultObject
->RefCount
== 1);
156 reference_transform_feedback_object(&ctx
->TransformFeedback
.CurrentObject
,
157 ctx
->TransformFeedback
.DefaultObject
);
159 assert(ctx
->TransformFeedback
.DefaultObject
->RefCount
== 2);
161 ctx
->TransformFeedback
.Objects
= _mesa_NewHashTable();
163 _mesa_reference_buffer_object(ctx
,
164 &ctx
->TransformFeedback
.CurrentBuffer
,
165 ctx
->Shared
->NullBufferObj
);
171 * Callback for _mesa_HashDeleteAll().
174 delete_cb(GLuint key
, void *data
, void *userData
)
176 struct gl_context
*ctx
= (struct gl_context
*) userData
;
177 struct gl_transform_feedback_object
*obj
=
178 (struct gl_transform_feedback_object
*) data
;
180 ctx
->Driver
.DeleteTransformFeedback(ctx
, obj
);
185 * Per-context free/clean-up for transform feedback.
188 _mesa_free_transform_feedback(struct gl_context
*ctx
)
190 /* core mesa expects this, even a dummy one, to be available */
191 assert(ctx
->Driver
.NewTransformFeedback
);
193 _mesa_reference_buffer_object(ctx
,
194 &ctx
->TransformFeedback
.CurrentBuffer
,
197 /* Delete all feedback objects */
198 _mesa_HashDeleteAll(ctx
->TransformFeedback
.Objects
, delete_cb
, ctx
);
199 _mesa_DeleteHashTable(ctx
->TransformFeedback
.Objects
);
201 /* Delete the default feedback object */
202 assert(ctx
->Driver
.DeleteTransformFeedback
);
203 ctx
->Driver
.DeleteTransformFeedback(ctx
,
204 ctx
->TransformFeedback
.DefaultObject
);
206 ctx
->TransformFeedback
.CurrentObject
= NULL
;
210 /** Initialize the fields of a gl_transform_feedback_object. */
212 _mesa_init_transform_feedback_object(struct gl_transform_feedback_object
*obj
,
220 obj
->EverBound
= GL_FALSE
;
224 /** Default fallback for ctx->Driver.NewTransformFeedback() */
225 static struct gl_transform_feedback_object
*
226 new_transform_feedback(struct gl_context
*ctx
, GLuint name
)
228 struct gl_transform_feedback_object
*obj
;
229 obj
= CALLOC_STRUCT(gl_transform_feedback_object
);
230 _mesa_init_transform_feedback_object(obj
, name
);
234 /** Default fallback for ctx->Driver.DeleteTransformFeedback() */
236 delete_transform_feedback(struct gl_context
*ctx
,
237 struct gl_transform_feedback_object
*obj
)
241 for (i
= 0; i
< ARRAY_SIZE(obj
->Buffers
); i
++) {
242 _mesa_reference_buffer_object(ctx
, &obj
->Buffers
[i
], NULL
);
250 /** Default fallback for ctx->Driver.BeginTransformFeedback() */
252 begin_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
253 struct gl_transform_feedback_object
*obj
)
258 /** Default fallback for ctx->Driver.EndTransformFeedback() */
260 end_transform_feedback(struct gl_context
*ctx
,
261 struct gl_transform_feedback_object
*obj
)
266 /** Default fallback for ctx->Driver.PauseTransformFeedback() */
268 pause_transform_feedback(struct gl_context
*ctx
,
269 struct gl_transform_feedback_object
*obj
)
274 /** Default fallback for ctx->Driver.ResumeTransformFeedback() */
276 resume_transform_feedback(struct gl_context
*ctx
,
277 struct gl_transform_feedback_object
*obj
)
284 * Plug in default device driver functions for transform feedback.
285 * Most drivers will override some/all of these.
288 _mesa_init_transform_feedback_functions(struct dd_function_table
*driver
)
290 driver
->NewTransformFeedback
= new_transform_feedback
;
291 driver
->DeleteTransformFeedback
= delete_transform_feedback
;
292 driver
->BeginTransformFeedback
= begin_transform_feedback
;
293 driver
->EndTransformFeedback
= end_transform_feedback
;
294 driver
->PauseTransformFeedback
= pause_transform_feedback
;
295 driver
->ResumeTransformFeedback
= resume_transform_feedback
;
300 * Fill in the correct Size value for each buffer in \c obj.
302 * From the GL 4.3 spec, section 6.1.1 ("Binding Buffer Objects to Indexed
305 * BindBufferBase binds the entire buffer, even when the size of the buffer
306 * is changed after the binding is established. It is equivalent to calling
307 * BindBufferRange with offset zero, while size is determined by the size of
308 * the bound buffer at the time the binding is used.
310 * Regardless of the size specified with BindBufferRange, or indirectly with
311 * BindBufferBase, the GL will never read or write beyond the end of a bound
312 * buffer. In some cases this constraint may result in visibly different
313 * behavior when a buffer overflow would otherwise result, such as described
314 * for transform feedback operations in section 13.2.2.
317 compute_transform_feedback_buffer_sizes(
318 struct gl_transform_feedback_object
*obj
)
321 for (i
= 0; i
< MAX_FEEDBACK_BUFFERS
; ++i
) {
322 GLintptr offset
= obj
->Offset
[i
];
323 GLsizeiptr buffer_size
324 = obj
->Buffers
[i
] == NULL
? 0 : obj
->Buffers
[i
]->Size
;
325 GLsizeiptr available_space
326 = buffer_size
<= offset
? 0 : buffer_size
- offset
;
327 GLsizeiptr computed_size
;
328 if (obj
->RequestedSize
[i
] == 0) {
329 /* No size was specified at the time the buffer was bound, so allow
330 * writing to all available space in the buffer.
332 computed_size
= available_space
;
334 /* A size was specified at the time the buffer was bound, however
335 * it's possible that the buffer has shrunk since then. So only
336 * allow writing to the minimum of the specified size and the space
339 computed_size
= MIN2(available_space
, obj
->RequestedSize
[i
]);
342 /* Legal sizes must be multiples of four, so round down if necessary. */
343 obj
->Size
[i
] = computed_size
& ~0x3;
349 * Compute the maximum number of vertices that can be written to the currently
350 * enabled transform feedback buffers without overflowing any of them.
353 _mesa_compute_max_transform_feedback_vertices(struct gl_context
*ctx
,
354 const struct gl_transform_feedback_object
*obj
,
355 const struct gl_transform_feedback_info
*info
)
357 unsigned max_index
= 0xffffffff;
360 for (i
= 0; i
< ctx
->Const
.MaxTransformFeedbackBuffers
; i
++) {
361 if ((info
->ActiveBuffers
>> i
) & 1) {
362 unsigned stride
= info
->Buffers
[i
].Stride
;
363 unsigned max_for_this_buffer
;
365 /* Skip any inactive buffers, which have a stride of 0. */
369 max_for_this_buffer
= obj
->Size
[i
] / (4 * stride
);
370 max_index
= MIN2(max_index
, max_for_this_buffer
);
379 ** Begin API functions
384 * Figure out which stage of the pipeline is the source of transform feedback
385 * data given the current context state, and return its gl_program.
387 * If no active program can generate transform feedback data (i.e. no vertex
388 * shader is active), returns NULL.
390 static struct gl_program
*
391 get_xfb_source(struct gl_context
*ctx
)
394 for (i
= MESA_SHADER_GEOMETRY
; i
>= MESA_SHADER_VERTEX
; i
--) {
395 if (ctx
->_Shader
->CurrentProgram
[i
] != NULL
)
396 return ctx
->_Shader
->CurrentProgram
[i
];
403 _mesa_BeginTransformFeedback(GLenum mode
)
405 struct gl_transform_feedback_object
*obj
;
406 struct gl_transform_feedback_info
*info
= NULL
;
408 unsigned vertices_per_prim
;
409 GET_CURRENT_CONTEXT(ctx
);
411 obj
= ctx
->TransformFeedback
.CurrentObject
;
413 /* Figure out what pipeline stage is the source of data for transform
416 struct gl_program
*source
= get_xfb_source(ctx
);
417 if (source
== NULL
) {
418 _mesa_error(ctx
, GL_INVALID_OPERATION
,
419 "glBeginTransformFeedback(no program active)");
423 info
= source
->sh
.LinkedTransformFeedback
;
425 if (info
->NumOutputs
== 0) {
426 _mesa_error(ctx
, GL_INVALID_OPERATION
,
427 "glBeginTransformFeedback(no varyings to record)");
433 vertices_per_prim
= 1;
436 vertices_per_prim
= 2;
439 vertices_per_prim
= 3;
442 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginTransformFeedback(mode)");
447 _mesa_error(ctx
, GL_INVALID_OPERATION
,
448 "glBeginTransformFeedback(already active)");
452 for (i
= 0; i
< ctx
->Const
.MaxTransformFeedbackBuffers
; i
++) {
453 if ((info
->ActiveBuffers
>> i
) & 1) {
454 if (obj
->BufferNames
[i
] == 0) {
455 _mesa_error(ctx
, GL_INVALID_OPERATION
,
456 "glBeginTransformFeedback(binding point %d does not "
457 "have a buffer object bound)", i
);
463 FLUSH_VERTICES(ctx
, 0);
464 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
466 obj
->Active
= GL_TRUE
;
467 ctx
->TransformFeedback
.Mode
= mode
;
469 compute_transform_feedback_buffer_sizes(obj
);
471 if (_mesa_is_gles3(ctx
)) {
472 /* In GLES3, we are required to track the usage of the transform
473 * feedback buffer and report INVALID_OPERATION if a draw call tries to
474 * exceed it. So compute the maximum number of vertices that we can
475 * write without overflowing any of the buffers currently being used for
478 unsigned max_vertices
479 = _mesa_compute_max_transform_feedback_vertices(ctx
, obj
, info
);
480 obj
->GlesRemainingPrims
= max_vertices
/ vertices_per_prim
;
483 if (obj
->program
!= source
) {
484 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedbackProg
;
485 obj
->program
= source
;
488 assert(ctx
->Driver
.BeginTransformFeedback
);
489 ctx
->Driver
.BeginTransformFeedback(ctx
, mode
, obj
);
494 _mesa_EndTransformFeedback(void)
496 struct gl_transform_feedback_object
*obj
;
497 GET_CURRENT_CONTEXT(ctx
);
499 obj
= ctx
->TransformFeedback
.CurrentObject
;
502 _mesa_error(ctx
, GL_INVALID_OPERATION
,
503 "glEndTransformFeedback(not active)");
507 FLUSH_VERTICES(ctx
, 0);
508 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
510 assert(ctx
->Driver
.EndTransformFeedback
);
511 ctx
->Driver
.EndTransformFeedback(ctx
, obj
);
513 ctx
->TransformFeedback
.CurrentObject
->Active
= GL_FALSE
;
514 ctx
->TransformFeedback
.CurrentObject
->Paused
= GL_FALSE
;
515 ctx
->TransformFeedback
.CurrentObject
->EndedAnytime
= GL_TRUE
;
520 * Helper used by BindBufferRange() and BindBufferBase().
523 bind_buffer_range(struct gl_context
*ctx
,
524 struct gl_transform_feedback_object
*obj
,
526 struct gl_buffer_object
*bufObj
,
527 GLintptr offset
, GLsizeiptr size
,
530 /* Note: no need to FLUSH_VERTICES or flag NewTransformFeedback, because
531 * transform feedback buffers can't be changed while transform feedback is
536 /* The general binding point */
537 _mesa_reference_buffer_object(ctx
,
538 &ctx
->TransformFeedback
.CurrentBuffer
,
542 /* The per-attribute binding point */
543 _mesa_set_transform_feedback_binding(ctx
, obj
, index
, bufObj
, offset
, size
);
548 * Specify a buffer object to receive transform feedback results. Plus,
549 * specify the starting offset to place the results, and max size.
550 * Called from the glBindBufferRange() and glTransformFeedbackBufferRange
554 _mesa_bind_buffer_range_transform_feedback(struct gl_context
*ctx
,
555 struct gl_transform_feedback_object
*obj
,
557 struct gl_buffer_object
*bufObj
,
562 const char *gl_methd_name
;
564 gl_methd_name
= "glTransformFeedbackBufferRange";
566 gl_methd_name
= "glBindBufferRange";
570 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(transform feedback active)",
575 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
576 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
577 * generated if index is greater than or equal to the number of binding
578 * points for transform feedback, as described in section 6.7.1."
580 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d out of bounds)",
581 gl_methd_name
, index
);
586 /* OpenGL 4.5 core profile, 6.7, pdf page 103: multiple of 4 */
587 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d must be a multiple of "
588 "four)", gl_methd_name
, (int) size
);
593 /* OpenGL 4.5 core profile, 6.7, pdf page 103: multiple of 4 */
594 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset=%d must be a multiple "
595 "of four)", gl_methd_name
, (int) offset
);
600 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
601 * generated by BindBufferRange if offset is negative."
603 * OpenGL 4.5 core profile, 13.2, pdf page 445: "An INVALID_VALUE error
604 * is generated by TransformFeedbackBufferRange if offset is negative."
606 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset=%d must be >= 0)",
612 if (size
<= 0 && (dsa
|| bufObj
!= ctx
->Shared
->NullBufferObj
)) {
613 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
614 * generated by BindBufferRange if buffer is non-zero and size is less
615 * than or equal to zero."
617 * OpenGL 4.5 core profile, 13.2, pdf page 445: "An INVALID_VALUE error
618 * is generated by TransformFeedbackBufferRange if size is less than or
621 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d must be > 0)",
622 gl_methd_name
, (int) size
);
626 bind_buffer_range(ctx
, obj
, index
, bufObj
, offset
, size
, dsa
);
631 * Specify a buffer object to receive transform feedback results.
632 * As above, but start at offset = 0.
633 * Called from the glBindBufferBase() and glTransformFeedbackBufferBase()
637 _mesa_bind_buffer_base_transform_feedback(struct gl_context
*ctx
,
638 struct gl_transform_feedback_object
*obj
,
640 struct gl_buffer_object
*bufObj
,
644 _mesa_error(ctx
, GL_INVALID_OPERATION
,
645 "%s(transform feedback active)",
646 dsa
? "glTransformFeedbackBufferBase" : "glBindBufferBase");
650 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
651 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d out of bounds)",
652 dsa
? "glTransformFeedbackBufferBase" : "glBindBufferBase",
657 bind_buffer_range(ctx
, obj
, index
, bufObj
, 0, 0, dsa
);
661 * Wrapper around lookup_transform_feedback_object that throws
662 * GL_INVALID_OPERATION if id is not in the hash table. After calling
663 * _mesa_error, it returns NULL.
665 static struct gl_transform_feedback_object
*
666 lookup_transform_feedback_object_err(struct gl_context
*ctx
,
667 GLuint xfb
, const char* func
)
669 struct gl_transform_feedback_object
*obj
;
671 obj
= _mesa_lookup_transform_feedback_object(ctx
, xfb
);
673 _mesa_error(ctx
, GL_INVALID_OPERATION
,
674 "%s(xfb=%u: non-generated object name)", func
, xfb
);
681 * Wrapper around _mesa_lookup_bufferobj that throws GL_INVALID_VALUE if id
682 * is not in the hash table. Specialised version for the
683 * transform-feedback-related functions. After calling _mesa_error, it
686 static struct gl_buffer_object
*
687 lookup_transform_feedback_bufferobj_err(struct gl_context
*ctx
,
688 GLuint buffer
, const char* func
)
690 struct gl_buffer_object
*bufObj
;
692 /* OpenGL 4.5 core profile, 13.2, pdf page 444: buffer must be zero or the
693 * name of an existing buffer object.
696 bufObj
= ctx
->Shared
->NullBufferObj
;
698 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
700 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid buffer=%u)", func
,
709 _mesa_TransformFeedbackBufferBase(GLuint xfb
, GLuint index
, GLuint buffer
)
711 GET_CURRENT_CONTEXT(ctx
);
712 struct gl_transform_feedback_object
*obj
;
713 struct gl_buffer_object
*bufObj
;
715 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
716 "glTransformFeedbackBufferBase");
721 bufObj
= lookup_transform_feedback_bufferobj_err(ctx
, buffer
,
722 "glTransformFeedbackBufferBase");
727 _mesa_bind_buffer_base_transform_feedback(ctx
, obj
, index
, bufObj
, true);
731 _mesa_TransformFeedbackBufferRange(GLuint xfb
, GLuint index
, GLuint buffer
,
732 GLintptr offset
, GLsizeiptr size
)
734 GET_CURRENT_CONTEXT(ctx
);
735 struct gl_transform_feedback_object
*obj
;
736 struct gl_buffer_object
*bufObj
;
738 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
739 "glTransformFeedbackBufferRange");
744 bufObj
= lookup_transform_feedback_bufferobj_err(ctx
, buffer
,
745 "glTransformFeedbackBufferRange");
750 _mesa_bind_buffer_range_transform_feedback(ctx
, obj
, index
, bufObj
, offset
,
755 * Specify a buffer object to receive transform feedback results, plus the
756 * offset in the buffer to start placing results.
757 * This function is part of GL_EXT_transform_feedback, but not GL3.
760 _mesa_BindBufferOffsetEXT(GLenum target
, GLuint index
, GLuint buffer
,
763 struct gl_transform_feedback_object
*obj
;
764 struct gl_buffer_object
*bufObj
;
765 GET_CURRENT_CONTEXT(ctx
);
767 if (target
!= GL_TRANSFORM_FEEDBACK_BUFFER
) {
768 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferOffsetEXT(target)");
772 obj
= ctx
->TransformFeedback
.CurrentObject
;
775 _mesa_error(ctx
, GL_INVALID_OPERATION
,
776 "glBindBufferOffsetEXT(transform feedback active)");
780 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
781 _mesa_error(ctx
, GL_INVALID_VALUE
,
782 "glBindBufferOffsetEXT(index=%d)", index
);
787 /* must be multiple of four */
788 _mesa_error(ctx
, GL_INVALID_VALUE
,
789 "glBindBufferOffsetEXT(offset=%d)", (int) offset
);
794 bufObj
= ctx
->Shared
->NullBufferObj
;
796 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
800 _mesa_error(ctx
, GL_INVALID_OPERATION
,
801 "glBindBufferOffsetEXT(invalid buffer=%u)", buffer
);
805 bind_buffer_range(ctx
, obj
, index
, bufObj
, offset
, 0, false);
810 * This function specifies the transform feedback outputs to be written
811 * to the feedback buffer(s), and in what order.
814 _mesa_TransformFeedbackVaryings(GLuint program
, GLsizei count
,
815 const GLchar
* const *varyings
,
818 struct gl_shader_program
*shProg
;
820 GET_CURRENT_CONTEXT(ctx
);
822 /* From the ARB_transform_feedback2 specification:
823 * "The error INVALID_OPERATION is generated by TransformFeedbackVaryings
824 * if the current transform feedback object is active, even if paused."
826 if (ctx
->TransformFeedback
.CurrentObject
->Active
) {
827 _mesa_error(ctx
, GL_INVALID_OPERATION
,
828 "glTransformFeedbackVaryings(current object is active)");
832 switch (bufferMode
) {
833 case GL_INTERLEAVED_ATTRIBS
:
835 case GL_SEPARATE_ATTRIBS
:
838 _mesa_error(ctx
, GL_INVALID_ENUM
,
839 "glTransformFeedbackVaryings(bufferMode)");
844 (bufferMode
== GL_SEPARATE_ATTRIBS
&&
845 (GLuint
) count
> ctx
->Const
.MaxTransformFeedbackBuffers
)) {
846 _mesa_error(ctx
, GL_INVALID_VALUE
,
847 "glTransformFeedbackVaryings(count=%d)", count
);
851 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
852 "glTransformFeedbackVaryings");
856 if (ctx
->Extensions
.ARB_transform_feedback3
) {
857 if (bufferMode
== GL_INTERLEAVED_ATTRIBS
) {
858 unsigned buffers
= 1;
860 for (i
= 0; i
< count
; i
++) {
861 if (strcmp(varyings
[i
], "gl_NextBuffer") == 0)
865 if (buffers
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
866 _mesa_error(ctx
, GL_INVALID_OPERATION
,
867 "glTransformFeedbackVaryings(too many gl_NextBuffer "
872 for (i
= 0; i
< count
; i
++) {
873 if (strcmp(varyings
[i
], "gl_NextBuffer") == 0 ||
874 strcmp(varyings
[i
], "gl_SkipComponents1") == 0 ||
875 strcmp(varyings
[i
], "gl_SkipComponents2") == 0 ||
876 strcmp(varyings
[i
], "gl_SkipComponents3") == 0 ||
877 strcmp(varyings
[i
], "gl_SkipComponents4") == 0) {
878 _mesa_error(ctx
, GL_INVALID_OPERATION
,
879 "glTransformFeedbackVaryings(SEPARATE_ATTRIBS,"
888 /* free existing varyings, if any */
889 for (i
= 0; i
< (GLint
) shProg
->TransformFeedback
.NumVarying
; i
++) {
890 free(shProg
->TransformFeedback
.VaryingNames
[i
]);
892 free(shProg
->TransformFeedback
.VaryingNames
);
894 /* allocate new memory for varying names */
895 shProg
->TransformFeedback
.VaryingNames
=
896 malloc(count
* sizeof(GLchar
*));
898 if (!shProg
->TransformFeedback
.VaryingNames
) {
899 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTransformFeedbackVaryings()");
903 /* Save the new names and the count */
904 for (i
= 0; i
< count
; i
++) {
905 shProg
->TransformFeedback
.VaryingNames
[i
] = strdup(varyings
[i
]);
907 shProg
->TransformFeedback
.NumVarying
= count
;
909 shProg
->TransformFeedback
.BufferMode
= bufferMode
;
911 /* No need to invoke FLUSH_VERTICES or flag NewTransformFeedback since
912 * the varyings won't be used until shader link time.
918 * Get info about the transform feedback outputs which are to be written
919 * to the feedback buffer(s).
922 _mesa_GetTransformFeedbackVarying(GLuint program
, GLuint index
,
923 GLsizei bufSize
, GLsizei
*length
,
924 GLsizei
*size
, GLenum
*type
, GLchar
*name
)
926 const struct gl_shader_program
*shProg
;
927 struct gl_program_resource
*res
;
928 GET_CURRENT_CONTEXT(ctx
);
930 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
931 "glGetTransformFeedbackVarying");
935 res
= _mesa_program_resource_find_index((struct gl_shader_program
*) shProg
,
936 GL_TRANSFORM_FEEDBACK_VARYING
,
939 _mesa_error(ctx
, GL_INVALID_VALUE
,
940 "glGetTransformFeedbackVarying(index=%u)", index
);
944 /* return the varying's name and length */
945 _mesa_copy_string(name
, bufSize
, length
, _mesa_program_resource_name(res
));
947 /* return the datatype and value's size (in datatype units) */
949 _mesa_program_resource_prop((struct gl_shader_program
*) shProg
,
950 res
, index
, GL_TYPE
, (GLint
*) type
,
951 "glGetTransformFeedbackVarying");
953 _mesa_program_resource_prop((struct gl_shader_program
*) shProg
,
954 res
, index
, GL_ARRAY_SIZE
, (GLint
*) size
,
955 "glGetTransformFeedbackVarying");
960 struct gl_transform_feedback_object
*
961 _mesa_lookup_transform_feedback_object(struct gl_context
*ctx
, GLuint name
)
963 /* OpenGL 4.5 core profile, 13.2 pdf page 444: "xfb must be zero, indicating
964 * the default transform feedback object, or the name of an existing
965 * transform feedback object."
968 return ctx
->TransformFeedback
.DefaultObject
;
971 return (struct gl_transform_feedback_object
*)
972 _mesa_HashLookup(ctx
->TransformFeedback
.Objects
, name
);
976 create_transform_feedbacks(struct gl_context
*ctx
, GLsizei n
, GLuint
*ids
,
983 func
= "glCreateTransformFeedbacks";
985 func
= "glGenTransformFeedbacks";
988 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", func
);
995 /* we don't need contiguous IDs, but this might be faster */
996 first
= _mesa_HashFindFreeKeyBlock(ctx
->TransformFeedback
.Objects
, n
);
999 for (i
= 0; i
< n
; i
++) {
1000 struct gl_transform_feedback_object
*obj
1001 = ctx
->Driver
.NewTransformFeedback(ctx
, first
+ i
);
1003 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1007 _mesa_HashInsert(ctx
->TransformFeedback
.Objects
, first
+ i
, obj
);
1009 /* this is normally done at bind time in the non-dsa case */
1010 obj
->EverBound
= GL_TRUE
;
1015 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1020 * Create new transform feedback objects. Transform feedback objects
1021 * encapsulate the state related to transform feedback to allow quickly
1022 * switching state (and drawing the results, below).
1023 * Part of GL_ARB_transform_feedback2.
1026 _mesa_GenTransformFeedbacks(GLsizei n
, GLuint
*names
)
1028 GET_CURRENT_CONTEXT(ctx
);
1030 /* GenTransformFeedbacks should just reserve the object names that a
1031 * subsequent call to BindTransformFeedback should actively create. For
1032 * the sake of simplicity, we reserve the names and create the objects
1036 create_transform_feedbacks(ctx
, n
, names
, false);
1040 * Create new transform feedback objects. Transform feedback objects
1041 * encapsulate the state related to transform feedback to allow quickly
1042 * switching state (and drawing the results, below).
1043 * Part of GL_ARB_direct_state_access.
1046 _mesa_CreateTransformFeedbacks(GLsizei n
, GLuint
*names
)
1048 GET_CURRENT_CONTEXT(ctx
);
1050 create_transform_feedbacks(ctx
, n
, names
, true);
1055 * Is the given ID a transform feedback object?
1056 * Part of GL_ARB_transform_feedback2.
1058 GLboolean GLAPIENTRY
1059 _mesa_IsTransformFeedback(GLuint name
)
1061 struct gl_transform_feedback_object
*obj
;
1062 GET_CURRENT_CONTEXT(ctx
);
1064 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1069 obj
= _mesa_lookup_transform_feedback_object(ctx
, name
);
1073 return obj
->EverBound
;
1078 * Bind the given transform feedback object.
1079 * Part of GL_ARB_transform_feedback2.
1082 _mesa_BindTransformFeedback(GLenum target
, GLuint name
)
1084 struct gl_transform_feedback_object
*obj
;
1085 GET_CURRENT_CONTEXT(ctx
);
1087 if (target
!= GL_TRANSFORM_FEEDBACK
) {
1088 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindTransformFeedback(target)");
1092 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1093 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1094 "glBindTransformFeedback(transform is active, or not paused)");
1098 obj
= _mesa_lookup_transform_feedback_object(ctx
, name
);
1100 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1101 "glBindTransformFeedback(name=%u)", name
);
1105 reference_transform_feedback_object(&ctx
->TransformFeedback
.CurrentObject
,
1111 * Delete the given transform feedback objects.
1112 * Part of GL_ARB_transform_feedback2.
1115 _mesa_DeleteTransformFeedbacks(GLsizei n
, const GLuint
*names
)
1118 GET_CURRENT_CONTEXT(ctx
);
1121 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteTransformFeedbacks(n < 0)");
1128 for (i
= 0; i
< n
; i
++) {
1130 struct gl_transform_feedback_object
*obj
1131 = _mesa_lookup_transform_feedback_object(ctx
, names
[i
]);
1134 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1135 "glDeleteTransformFeedbacks(object %u is active)",
1139 _mesa_HashRemove(ctx
->TransformFeedback
.Objects
, names
[i
]);
1140 /* unref, but object may not be deleted until later */
1141 if (obj
== ctx
->TransformFeedback
.CurrentObject
) {
1142 reference_transform_feedback_object(
1143 &ctx
->TransformFeedback
.CurrentObject
,
1144 ctx
->TransformFeedback
.DefaultObject
);
1146 reference_transform_feedback_object(&obj
, NULL
);
1154 * Pause transform feedback.
1155 * Part of GL_ARB_transform_feedback2.
1158 _mesa_PauseTransformFeedback(void)
1160 struct gl_transform_feedback_object
*obj
;
1161 GET_CURRENT_CONTEXT(ctx
);
1163 obj
= ctx
->TransformFeedback
.CurrentObject
;
1165 if (!_mesa_is_xfb_active_and_unpaused(ctx
)) {
1166 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1167 "glPauseTransformFeedback(feedback not active or already paused)");
1171 FLUSH_VERTICES(ctx
, 0);
1172 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
1174 assert(ctx
->Driver
.PauseTransformFeedback
);
1175 ctx
->Driver
.PauseTransformFeedback(ctx
, obj
);
1177 obj
->Paused
= GL_TRUE
;
1182 * Resume transform feedback.
1183 * Part of GL_ARB_transform_feedback2.
1186 _mesa_ResumeTransformFeedback(void)
1188 struct gl_transform_feedback_object
*obj
;
1189 GET_CURRENT_CONTEXT(ctx
);
1191 obj
= ctx
->TransformFeedback
.CurrentObject
;
1193 if (!obj
->Active
|| !obj
->Paused
) {
1194 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1195 "glResumeTransformFeedback(feedback not active or not paused)");
1199 /* From the ARB_transform_feedback2 specification:
1200 * "The error INVALID_OPERATION is generated by ResumeTransformFeedback if
1201 * the program object being used by the current transform feedback object
1204 if (obj
->program
!= get_xfb_source(ctx
)) {
1205 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1206 "glResumeTransformFeedback(wrong program bound)");
1210 FLUSH_VERTICES(ctx
, 0);
1211 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
1213 obj
->Paused
= GL_FALSE
;
1215 assert(ctx
->Driver
.ResumeTransformFeedback
);
1216 ctx
->Driver
.ResumeTransformFeedback(ctx
, obj
);
1219 extern void GLAPIENTRY
1220 _mesa_GetTransformFeedbackiv(GLuint xfb
, GLenum pname
, GLint
*param
)
1222 struct gl_transform_feedback_object
*obj
;
1223 GET_CURRENT_CONTEXT(ctx
);
1225 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
1226 "glGetTransformFeedbackiv");
1232 case GL_TRANSFORM_FEEDBACK_PAUSED
:
1233 *param
= obj
->Paused
;
1235 case GL_TRANSFORM_FEEDBACK_ACTIVE
:
1236 *param
= obj
->Active
;
1239 _mesa_error(ctx
, GL_INVALID_ENUM
,
1240 "glGetTransformFeedbackiv(pname=%i)", pname
);
1244 extern void GLAPIENTRY
1245 _mesa_GetTransformFeedbacki_v(GLuint xfb
, GLenum pname
, GLuint index
,
1248 struct gl_transform_feedback_object
*obj
;
1249 GET_CURRENT_CONTEXT(ctx
);
1251 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
1252 "glGetTransformFeedbacki_v");
1257 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
1258 _mesa_error(ctx
, GL_INVALID_VALUE
,
1259 "glGetTransformFeedbacki_v(index=%i)", index
);
1264 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING
:
1265 *param
= obj
->BufferNames
[index
];
1268 _mesa_error(ctx
, GL_INVALID_ENUM
,
1269 "glGetTransformFeedbacki_v(pname=%i)", pname
);
1273 extern void GLAPIENTRY
1274 _mesa_GetTransformFeedbacki64_v(GLuint xfb
, GLenum pname
, GLuint index
,
1277 struct gl_transform_feedback_object
*obj
;
1278 GET_CURRENT_CONTEXT(ctx
);
1280 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
1281 "glGetTransformFeedbacki64_v");
1286 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
1287 _mesa_error(ctx
, GL_INVALID_VALUE
,
1288 "glGetTransformFeedbacki64_v(index=%i)", index
);
1292 compute_transform_feedback_buffer_sizes(obj
);
1294 case GL_TRANSFORM_FEEDBACK_BUFFER_START
:
1295 *param
= obj
->Offset
[index
];
1297 case GL_TRANSFORM_FEEDBACK_BUFFER_SIZE
:
1298 *param
= obj
->Size
[index
];
1301 _mesa_error(ctx
, GL_INVALID_ENUM
,
1302 "glGetTransformFeedbacki64_v(pname=%i)", pname
);