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_shader_program
*shProg
;
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
->shader_program
== callback_data
->shProg
)
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 struct using_program_tuple callback_data
;
69 callback_data
.shProg
= shProg
;
70 callback_data
.found
= false;
72 _mesa_HashWalk(ctx
->TransformFeedback
.Objects
,
73 active_xfb_object_references_program
, &callback_data
);
75 /* Also check DefaultObject, as it's not in the Objects hash table. */
76 active_xfb_object_references_program(0, ctx
->TransformFeedback
.DefaultObject
,
79 return callback_data
.found
;
83 * Do reference counting of transform feedback buffers.
86 reference_transform_feedback_object(struct gl_transform_feedback_object
**ptr
,
87 struct gl_transform_feedback_object
*obj
)
93 /* Unreference the old object */
94 struct gl_transform_feedback_object
*oldObj
= *ptr
;
96 assert(oldObj
->RefCount
> 0);
99 if (oldObj
->RefCount
== 0) {
100 GET_CURRENT_CONTEXT(ctx
);
102 ctx
->Driver
.DeleteTransformFeedback(ctx
, oldObj
);
110 /* reference new object */
111 if (obj
->RefCount
== 0) {
112 _mesa_problem(NULL
, "referencing deleted transform feedback object");
117 obj
->EverBound
= GL_TRUE
;
125 * Check that all the buffer objects currently bound for transform
126 * feedback actually exist. Raise a GL_INVALID_OPERATION error if
127 * any buffers are missing.
128 * \return GL_TRUE for success, GL_FALSE if error
131 _mesa_validate_transform_feedback_buffers(struct gl_context
*ctx
)
140 * Per-context init for transform feedback.
143 _mesa_init_transform_feedback(struct gl_context
*ctx
)
145 /* core mesa expects this, even a dummy one, to be available */
146 assert(ctx
->Driver
.NewTransformFeedback
);
148 ctx
->TransformFeedback
.DefaultObject
=
149 ctx
->Driver
.NewTransformFeedback(ctx
, 0);
151 assert(ctx
->TransformFeedback
.DefaultObject
->RefCount
== 1);
153 reference_transform_feedback_object(&ctx
->TransformFeedback
.CurrentObject
,
154 ctx
->TransformFeedback
.DefaultObject
);
156 assert(ctx
->TransformFeedback
.DefaultObject
->RefCount
== 2);
158 ctx
->TransformFeedback
.Objects
= _mesa_NewHashTable();
160 _mesa_reference_buffer_object(ctx
,
161 &ctx
->TransformFeedback
.CurrentBuffer
,
162 ctx
->Shared
->NullBufferObj
);
168 * Callback for _mesa_HashDeleteAll().
171 delete_cb(GLuint key
, void *data
, void *userData
)
173 struct gl_context
*ctx
= (struct gl_context
*) userData
;
174 struct gl_transform_feedback_object
*obj
=
175 (struct gl_transform_feedback_object
*) data
;
177 ctx
->Driver
.DeleteTransformFeedback(ctx
, obj
);
182 * Per-context free/clean-up for transform feedback.
185 _mesa_free_transform_feedback(struct gl_context
*ctx
)
187 /* core mesa expects this, even a dummy one, to be available */
188 assert(ctx
->Driver
.NewTransformFeedback
);
190 _mesa_reference_buffer_object(ctx
,
191 &ctx
->TransformFeedback
.CurrentBuffer
,
194 /* Delete all feedback objects */
195 _mesa_HashDeleteAll(ctx
->TransformFeedback
.Objects
, delete_cb
, ctx
);
196 _mesa_DeleteHashTable(ctx
->TransformFeedback
.Objects
);
198 /* Delete the default feedback object */
199 assert(ctx
->Driver
.DeleteTransformFeedback
);
200 ctx
->Driver
.DeleteTransformFeedback(ctx
,
201 ctx
->TransformFeedback
.DefaultObject
);
203 ctx
->TransformFeedback
.CurrentObject
= NULL
;
207 /** Initialize the fields of a gl_transform_feedback_object. */
209 _mesa_init_transform_feedback_object(struct gl_transform_feedback_object
*obj
,
217 obj
->EverBound
= GL_FALSE
;
221 /** Default fallback for ctx->Driver.NewTransformFeedback() */
222 static struct gl_transform_feedback_object
*
223 new_transform_feedback(struct gl_context
*ctx
, GLuint name
)
225 struct gl_transform_feedback_object
*obj
;
226 obj
= CALLOC_STRUCT(gl_transform_feedback_object
);
227 _mesa_init_transform_feedback_object(obj
, name
);
231 /** Default fallback for ctx->Driver.DeleteTransformFeedback() */
233 delete_transform_feedback(struct gl_context
*ctx
,
234 struct gl_transform_feedback_object
*obj
)
238 for (i
= 0; i
< ARRAY_SIZE(obj
->Buffers
); i
++) {
239 _mesa_reference_buffer_object(ctx
, &obj
->Buffers
[i
], NULL
);
247 /** Default fallback for ctx->Driver.BeginTransformFeedback() */
249 begin_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
250 struct gl_transform_feedback_object
*obj
)
255 /** Default fallback for ctx->Driver.EndTransformFeedback() */
257 end_transform_feedback(struct gl_context
*ctx
,
258 struct gl_transform_feedback_object
*obj
)
263 /** Default fallback for ctx->Driver.PauseTransformFeedback() */
265 pause_transform_feedback(struct gl_context
*ctx
,
266 struct gl_transform_feedback_object
*obj
)
271 /** Default fallback for ctx->Driver.ResumeTransformFeedback() */
273 resume_transform_feedback(struct gl_context
*ctx
,
274 struct gl_transform_feedback_object
*obj
)
281 * Plug in default device driver functions for transform feedback.
282 * Most drivers will override some/all of these.
285 _mesa_init_transform_feedback_functions(struct dd_function_table
*driver
)
287 driver
->NewTransformFeedback
= new_transform_feedback
;
288 driver
->DeleteTransformFeedback
= delete_transform_feedback
;
289 driver
->BeginTransformFeedback
= begin_transform_feedback
;
290 driver
->EndTransformFeedback
= end_transform_feedback
;
291 driver
->PauseTransformFeedback
= pause_transform_feedback
;
292 driver
->ResumeTransformFeedback
= resume_transform_feedback
;
297 * Fill in the correct Size value for each buffer in \c obj.
299 * From the GL 4.3 spec, section 6.1.1 ("Binding Buffer Objects to Indexed
302 * BindBufferBase binds the entire buffer, even when the size of the buffer
303 * is changed after the binding is established. It is equivalent to calling
304 * BindBufferRange with offset zero, while size is determined by the size of
305 * the bound buffer at the time the binding is used.
307 * Regardless of the size specified with BindBufferRange, or indirectly with
308 * BindBufferBase, the GL will never read or write beyond the end of a bound
309 * buffer. In some cases this constraint may result in visibly different
310 * behavior when a buffer overflow would otherwise result, such as described
311 * for transform feedback operations in section 13.2.2.
314 compute_transform_feedback_buffer_sizes(
315 struct gl_transform_feedback_object
*obj
)
318 for (i
= 0; i
< MAX_FEEDBACK_BUFFERS
; ++i
) {
319 GLintptr offset
= obj
->Offset
[i
];
320 GLsizeiptr buffer_size
321 = obj
->Buffers
[i
] == NULL
? 0 : obj
->Buffers
[i
]->Size
;
322 GLsizeiptr available_space
323 = buffer_size
<= offset
? 0 : buffer_size
- offset
;
324 GLsizeiptr computed_size
;
325 if (obj
->RequestedSize
[i
] == 0) {
326 /* No size was specified at the time the buffer was bound, so allow
327 * writing to all available space in the buffer.
329 computed_size
= available_space
;
331 /* A size was specified at the time the buffer was bound, however
332 * it's possible that the buffer has shrunk since then. So only
333 * allow writing to the minimum of the specified size and the space
336 computed_size
= MIN2(available_space
, obj
->RequestedSize
[i
]);
339 /* Legal sizes must be multiples of four, so round down if necessary. */
340 obj
->Size
[i
] = computed_size
& ~0x3;
346 * Compute the maximum number of vertices that can be written to the currently
347 * enabled transform feedback buffers without overflowing any of them.
350 _mesa_compute_max_transform_feedback_vertices(struct gl_context
*ctx
,
351 const struct gl_transform_feedback_object
*obj
,
352 const struct gl_transform_feedback_info
*info
)
354 unsigned max_index
= 0xffffffff;
357 for (i
= 0; i
< ctx
->Const
.MaxTransformFeedbackBuffers
; i
++) {
358 if ((info
->ActiveBuffers
>> i
) & 1) {
359 unsigned stride
= info
->Buffers
[i
].Stride
;
360 unsigned max_for_this_buffer
;
362 /* Skip any inactive buffers, which have a stride of 0. */
366 max_for_this_buffer
= obj
->Size
[i
] / (4 * stride
);
367 max_index
= MIN2(max_index
, max_for_this_buffer
);
376 ** Begin API functions
381 * Figure out which stage of the pipeline is the source of transform feedback
382 * data given the current context state, and return its gl_shader_program.
384 * If no active program can generate transform feedback data (i.e. no vertex
385 * shader is active), returns NULL.
387 static struct gl_shader_program
*
388 get_xfb_source(struct gl_context
*ctx
)
391 for (i
= MESA_SHADER_GEOMETRY
; i
>= MESA_SHADER_VERTEX
; i
--) {
392 if (ctx
->_Shader
->CurrentProgram
[i
] != NULL
)
393 return ctx
->_Shader
->CurrentProgram
[i
];
400 _mesa_BeginTransformFeedback(GLenum mode
)
402 struct gl_transform_feedback_object
*obj
;
403 struct gl_transform_feedback_info
*info
= NULL
;
404 struct gl_shader_program
*source
;
406 unsigned vertices_per_prim
;
407 GET_CURRENT_CONTEXT(ctx
);
409 obj
= ctx
->TransformFeedback
.CurrentObject
;
411 /* Figure out what pipeline stage is the source of data for transform
414 source
= get_xfb_source(ctx
);
415 if (source
== NULL
) {
416 _mesa_error(ctx
, GL_INVALID_OPERATION
,
417 "glBeginTransformFeedback(no program active)");
421 info
= &source
->LinkedTransformFeedback
;
423 if (info
->NumOutputs
== 0) {
424 _mesa_error(ctx
, GL_INVALID_OPERATION
,
425 "glBeginTransformFeedback(no varyings to record)");
431 vertices_per_prim
= 1;
434 vertices_per_prim
= 2;
437 vertices_per_prim
= 3;
440 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginTransformFeedback(mode)");
445 _mesa_error(ctx
, GL_INVALID_OPERATION
,
446 "glBeginTransformFeedback(already active)");
450 for (i
= 0; i
< ctx
->Const
.MaxTransformFeedbackBuffers
; i
++) {
451 if ((info
->ActiveBuffers
>> i
) & 1) {
452 if (obj
->BufferNames
[i
] == 0) {
453 _mesa_error(ctx
, GL_INVALID_OPERATION
,
454 "glBeginTransformFeedback(binding point %d does not "
455 "have a buffer object bound)", i
);
461 FLUSH_VERTICES(ctx
, 0);
462 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
464 obj
->Active
= GL_TRUE
;
465 ctx
->TransformFeedback
.Mode
= mode
;
467 compute_transform_feedback_buffer_sizes(obj
);
469 if (_mesa_is_gles3(ctx
)) {
470 /* In GLES3, we are required to track the usage of the transform
471 * feedback buffer and report INVALID_OPERATION if a draw call tries to
472 * exceed it. So compute the maximum number of vertices that we can
473 * write without overflowing any of the buffers currently being used for
476 unsigned max_vertices
477 = _mesa_compute_max_transform_feedback_vertices(ctx
, obj
, info
);
478 obj
->GlesRemainingPrims
= max_vertices
/ vertices_per_prim
;
481 if (obj
->shader_program
!= source
) {
482 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedbackProg
;
483 obj
->shader_program
= source
;
486 assert(ctx
->Driver
.BeginTransformFeedback
);
487 ctx
->Driver
.BeginTransformFeedback(ctx
, mode
, obj
);
492 _mesa_EndTransformFeedback(void)
494 struct gl_transform_feedback_object
*obj
;
495 GET_CURRENT_CONTEXT(ctx
);
497 obj
= ctx
->TransformFeedback
.CurrentObject
;
500 _mesa_error(ctx
, GL_INVALID_OPERATION
,
501 "glEndTransformFeedback(not active)");
505 FLUSH_VERTICES(ctx
, 0);
506 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
508 assert(ctx
->Driver
.EndTransformFeedback
);
509 ctx
->Driver
.EndTransformFeedback(ctx
, obj
);
511 ctx
->TransformFeedback
.CurrentObject
->Active
= GL_FALSE
;
512 ctx
->TransformFeedback
.CurrentObject
->Paused
= GL_FALSE
;
513 ctx
->TransformFeedback
.CurrentObject
->EndedAnytime
= GL_TRUE
;
518 * Helper used by BindBufferRange() and BindBufferBase().
521 bind_buffer_range(struct gl_context
*ctx
,
522 struct gl_transform_feedback_object
*obj
,
524 struct gl_buffer_object
*bufObj
,
525 GLintptr offset
, GLsizeiptr size
,
528 /* Note: no need to FLUSH_VERTICES or flag NewTransformFeedback, because
529 * transform feedback buffers can't be changed while transform feedback is
534 /* The general binding point */
535 _mesa_reference_buffer_object(ctx
,
536 &ctx
->TransformFeedback
.CurrentBuffer
,
540 /* The per-attribute binding point */
541 _mesa_set_transform_feedback_binding(ctx
, obj
, index
, bufObj
, offset
, size
);
546 * Specify a buffer object to receive transform feedback results. Plus,
547 * specify the starting offset to place the results, and max size.
548 * Called from the glBindBufferRange() and glTransformFeedbackBufferRange
552 _mesa_bind_buffer_range_transform_feedback(struct gl_context
*ctx
,
553 struct gl_transform_feedback_object
*obj
,
555 struct gl_buffer_object
*bufObj
,
560 const char *gl_methd_name
;
562 gl_methd_name
= "glTransformFeedbackBufferRange";
564 gl_methd_name
= "glBindBufferRange";
568 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(transform feedback active)",
573 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
574 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
575 * generated if index is greater than or equal to the number of binding
576 * points for transform feedback, as described in section 6.7.1."
578 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d out of bounds)",
579 gl_methd_name
, index
);
584 /* OpenGL 4.5 core profile, 6.7, pdf page 103: multiple of 4 */
585 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d must be a multiple of "
586 "four)", gl_methd_name
, (int) size
);
591 /* OpenGL 4.5 core profile, 6.7, pdf page 103: multiple of 4 */
592 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset=%d must be a multiple "
593 "of four)", gl_methd_name
, (int) offset
);
598 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
599 * generated by BindBufferRange if offset is negative."
601 * OpenGL 4.5 core profile, 13.2, pdf page 445: "An INVALID_VALUE error
602 * is generated by TransformFeedbackBufferRange if offset is negative."
604 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset=%d must be >= 0)",
610 if (size
<= 0 && (dsa
|| bufObj
!= ctx
->Shared
->NullBufferObj
)) {
611 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
612 * generated by BindBufferRange if buffer is non-zero and size is less
613 * than or equal to zero."
615 * OpenGL 4.5 core profile, 13.2, pdf page 445: "An INVALID_VALUE error
616 * is generated by TransformFeedbackBufferRange if size is less than or
619 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d must be > 0)",
620 gl_methd_name
, (int) size
);
624 bind_buffer_range(ctx
, obj
, index
, bufObj
, offset
, size
, dsa
);
629 * Specify a buffer object to receive transform feedback results.
630 * As above, but start at offset = 0.
631 * Called from the glBindBufferBase() and glTransformFeedbackBufferBase()
635 _mesa_bind_buffer_base_transform_feedback(struct gl_context
*ctx
,
636 struct gl_transform_feedback_object
*obj
,
638 struct gl_buffer_object
*bufObj
,
642 _mesa_error(ctx
, GL_INVALID_OPERATION
,
643 "%s(transform feedback active)",
644 dsa
? "glTransformFeedbackBufferBase" : "glBindBufferBase");
648 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
649 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d out of bounds)",
650 dsa
? "glTransformFeedbackBufferBase" : "glBindBufferBase",
655 bind_buffer_range(ctx
, obj
, index
, bufObj
, 0, 0, dsa
);
659 * Wrapper around lookup_transform_feedback_object that throws
660 * GL_INVALID_OPERATION if id is not in the hash table. After calling
661 * _mesa_error, it returns NULL.
663 static struct gl_transform_feedback_object
*
664 lookup_transform_feedback_object_err(struct gl_context
*ctx
,
665 GLuint xfb
, const char* func
)
667 struct gl_transform_feedback_object
*obj
;
669 obj
= _mesa_lookup_transform_feedback_object(ctx
, xfb
);
671 _mesa_error(ctx
, GL_INVALID_OPERATION
,
672 "%s(xfb=%u: non-generated object name)", func
, xfb
);
679 * Wrapper around _mesa_lookup_bufferobj that throws GL_INVALID_VALUE if id
680 * is not in the hash table. Specialised version for the
681 * transform-feedback-related functions. After calling _mesa_error, it
684 static struct gl_buffer_object
*
685 lookup_transform_feedback_bufferobj_err(struct gl_context
*ctx
,
686 GLuint buffer
, const char* func
)
688 struct gl_buffer_object
*bufObj
;
690 /* OpenGL 4.5 core profile, 13.2, pdf page 444: buffer must be zero or the
691 * name of an existing buffer object.
694 bufObj
= ctx
->Shared
->NullBufferObj
;
696 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
698 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid buffer=%u)", func
,
707 _mesa_TransformFeedbackBufferBase(GLuint xfb
, GLuint index
, GLuint buffer
)
709 GET_CURRENT_CONTEXT(ctx
);
710 struct gl_transform_feedback_object
*obj
;
711 struct gl_buffer_object
*bufObj
;
713 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
714 "glTransformFeedbackBufferBase");
719 bufObj
= lookup_transform_feedback_bufferobj_err(ctx
, buffer
,
720 "glTransformFeedbackBufferBase");
725 _mesa_bind_buffer_base_transform_feedback(ctx
, obj
, index
, bufObj
, true);
729 _mesa_TransformFeedbackBufferRange(GLuint xfb
, GLuint index
, GLuint buffer
,
730 GLintptr offset
, GLsizeiptr size
)
732 GET_CURRENT_CONTEXT(ctx
);
733 struct gl_transform_feedback_object
*obj
;
734 struct gl_buffer_object
*bufObj
;
736 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
737 "glTransformFeedbackBufferRange");
742 bufObj
= lookup_transform_feedback_bufferobj_err(ctx
, buffer
,
743 "glTransformFeedbackBufferRange");
748 _mesa_bind_buffer_range_transform_feedback(ctx
, obj
, index
, bufObj
, offset
,
753 * Specify a buffer object to receive transform feedback results, plus the
754 * offset in the buffer to start placing results.
755 * This function is part of GL_EXT_transform_feedback, but not GL3.
758 _mesa_BindBufferOffsetEXT(GLenum target
, GLuint index
, GLuint buffer
,
761 struct gl_transform_feedback_object
*obj
;
762 struct gl_buffer_object
*bufObj
;
763 GET_CURRENT_CONTEXT(ctx
);
765 if (target
!= GL_TRANSFORM_FEEDBACK_BUFFER
) {
766 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferOffsetEXT(target)");
770 obj
= ctx
->TransformFeedback
.CurrentObject
;
773 _mesa_error(ctx
, GL_INVALID_OPERATION
,
774 "glBindBufferOffsetEXT(transform feedback active)");
778 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
779 _mesa_error(ctx
, GL_INVALID_VALUE
,
780 "glBindBufferOffsetEXT(index=%d)", index
);
785 /* must be multiple of four */
786 _mesa_error(ctx
, GL_INVALID_VALUE
,
787 "glBindBufferOffsetEXT(offset=%d)", (int) offset
);
792 bufObj
= ctx
->Shared
->NullBufferObj
;
794 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
798 _mesa_error(ctx
, GL_INVALID_OPERATION
,
799 "glBindBufferOffsetEXT(invalid buffer=%u)", buffer
);
803 bind_buffer_range(ctx
, obj
, index
, bufObj
, offset
, 0, false);
808 * This function specifies the transform feedback outputs to be written
809 * to the feedback buffer(s), and in what order.
812 _mesa_TransformFeedbackVaryings(GLuint program
, GLsizei count
,
813 const GLchar
* const *varyings
,
816 struct gl_shader_program
*shProg
;
818 GET_CURRENT_CONTEXT(ctx
);
820 /* From the ARB_transform_feedback2 specification:
821 * "The error INVALID_OPERATION is generated by TransformFeedbackVaryings
822 * if the current transform feedback object is active, even if paused."
824 if (ctx
->TransformFeedback
.CurrentObject
->Active
) {
825 _mesa_error(ctx
, GL_INVALID_OPERATION
,
826 "glTransformFeedbackVaryings(current object is active)");
830 switch (bufferMode
) {
831 case GL_INTERLEAVED_ATTRIBS
:
833 case GL_SEPARATE_ATTRIBS
:
836 _mesa_error(ctx
, GL_INVALID_ENUM
,
837 "glTransformFeedbackVaryings(bufferMode)");
842 (bufferMode
== GL_SEPARATE_ATTRIBS
&&
843 (GLuint
) count
> ctx
->Const
.MaxTransformFeedbackBuffers
)) {
844 _mesa_error(ctx
, GL_INVALID_VALUE
,
845 "glTransformFeedbackVaryings(count=%d)", count
);
849 shProg
= _mesa_lookup_shader_program(ctx
, program
);
851 _mesa_error(ctx
, GL_INVALID_VALUE
,
852 "glTransformFeedbackVaryings(program=%u)", program
);
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(ctx
, program
);
932 _mesa_error(ctx
, GL_INVALID_VALUE
,
933 "glGetTransformFeedbackVarying(program=%u)", program
);
937 res
= _mesa_program_resource_find_index((struct gl_shader_program
*) shProg
,
938 GL_TRANSFORM_FEEDBACK_VARYING
,
941 _mesa_error(ctx
, GL_INVALID_VALUE
,
942 "glGetTransformFeedbackVarying(index=%u)", index
);
946 /* return the varying's name and length */
947 _mesa_copy_string(name
, bufSize
, length
, _mesa_program_resource_name(res
));
949 /* return the datatype and value's size (in datatype units) */
951 _mesa_program_resource_prop((struct gl_shader_program
*) shProg
,
952 res
, index
, GL_TYPE
, (GLint
*) type
,
953 "glGetTransformFeedbackVarying");
955 _mesa_program_resource_prop((struct gl_shader_program
*) shProg
,
956 res
, index
, GL_ARRAY_SIZE
, (GLint
*) size
,
957 "glGetTransformFeedbackVarying");
962 struct gl_transform_feedback_object
*
963 _mesa_lookup_transform_feedback_object(struct gl_context
*ctx
, GLuint name
)
965 /* OpenGL 4.5 core profile, 13.2 pdf page 444: "xfb must be zero, indicating
966 * the default transform feedback object, or the name of an existing
967 * transform feedback object."
970 return ctx
->TransformFeedback
.DefaultObject
;
973 return (struct gl_transform_feedback_object
*)
974 _mesa_HashLookup(ctx
->TransformFeedback
.Objects
, name
);
978 create_transform_feedbacks(struct gl_context
*ctx
, GLsizei n
, GLuint
*ids
,
985 func
= "glCreateTransformFeedbacks";
987 func
= "glGenTransformFeedbacks";
990 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", func
);
997 /* we don't need contiguous IDs, but this might be faster */
998 first
= _mesa_HashFindFreeKeyBlock(ctx
->TransformFeedback
.Objects
, n
);
1001 for (i
= 0; i
< n
; i
++) {
1002 struct gl_transform_feedback_object
*obj
1003 = ctx
->Driver
.NewTransformFeedback(ctx
, first
+ i
);
1005 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1009 _mesa_HashInsert(ctx
->TransformFeedback
.Objects
, first
+ i
, obj
);
1011 /* this is normally done at bind time in the non-dsa case */
1012 obj
->EverBound
= GL_TRUE
;
1017 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1022 * Create new transform feedback objects. Transform feedback objects
1023 * encapsulate the state related to transform feedback to allow quickly
1024 * switching state (and drawing the results, below).
1025 * Part of GL_ARB_transform_feedback2.
1028 _mesa_GenTransformFeedbacks(GLsizei n
, GLuint
*names
)
1030 GET_CURRENT_CONTEXT(ctx
);
1032 /* GenTransformFeedbacks should just reserve the object names that a
1033 * subsequent call to BindTransformFeedback should actively create. For
1034 * the sake of simplicity, we reserve the names and create the objects
1038 create_transform_feedbacks(ctx
, n
, names
, false);
1042 * Create new transform feedback objects. Transform feedback objects
1043 * encapsulate the state related to transform feedback to allow quickly
1044 * switching state (and drawing the results, below).
1045 * Part of GL_ARB_direct_state_access.
1048 _mesa_CreateTransformFeedbacks(GLsizei n
, GLuint
*names
)
1050 GET_CURRENT_CONTEXT(ctx
);
1052 create_transform_feedbacks(ctx
, n
, names
, true);
1057 * Is the given ID a transform feedback object?
1058 * Part of GL_ARB_transform_feedback2.
1060 GLboolean GLAPIENTRY
1061 _mesa_IsTransformFeedback(GLuint name
)
1063 struct gl_transform_feedback_object
*obj
;
1064 GET_CURRENT_CONTEXT(ctx
);
1066 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1071 obj
= _mesa_lookup_transform_feedback_object(ctx
, name
);
1075 return obj
->EverBound
;
1080 * Bind the given transform feedback object.
1081 * Part of GL_ARB_transform_feedback2.
1084 _mesa_BindTransformFeedback(GLenum target
, GLuint name
)
1086 struct gl_transform_feedback_object
*obj
;
1087 GET_CURRENT_CONTEXT(ctx
);
1089 if (target
!= GL_TRANSFORM_FEEDBACK
) {
1090 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindTransformFeedback(target)");
1094 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1095 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1096 "glBindTransformFeedback(transform is active, or not paused)");
1100 obj
= _mesa_lookup_transform_feedback_object(ctx
, name
);
1102 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1103 "glBindTransformFeedback(name=%u)", name
);
1107 reference_transform_feedback_object(&ctx
->TransformFeedback
.CurrentObject
,
1113 * Delete the given transform feedback objects.
1114 * Part of GL_ARB_transform_feedback2.
1117 _mesa_DeleteTransformFeedbacks(GLsizei n
, const GLuint
*names
)
1120 GET_CURRENT_CONTEXT(ctx
);
1123 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteTransformFeedbacks(n < 0)");
1130 for (i
= 0; i
< n
; i
++) {
1132 struct gl_transform_feedback_object
*obj
1133 = _mesa_lookup_transform_feedback_object(ctx
, names
[i
]);
1136 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1137 "glDeleteTransformFeedbacks(object %u is active)",
1141 _mesa_HashRemove(ctx
->TransformFeedback
.Objects
, names
[i
]);
1142 /* unref, but object may not be deleted until later */
1143 if (obj
== ctx
->TransformFeedback
.CurrentObject
) {
1144 reference_transform_feedback_object(
1145 &ctx
->TransformFeedback
.CurrentObject
,
1146 ctx
->TransformFeedback
.DefaultObject
);
1148 reference_transform_feedback_object(&obj
, NULL
);
1156 * Pause transform feedback.
1157 * Part of GL_ARB_transform_feedback2.
1160 _mesa_PauseTransformFeedback(void)
1162 struct gl_transform_feedback_object
*obj
;
1163 GET_CURRENT_CONTEXT(ctx
);
1165 obj
= ctx
->TransformFeedback
.CurrentObject
;
1167 if (!_mesa_is_xfb_active_and_unpaused(ctx
)) {
1168 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1169 "glPauseTransformFeedback(feedback not active or already paused)");
1173 FLUSH_VERTICES(ctx
, 0);
1174 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
1176 assert(ctx
->Driver
.PauseTransformFeedback
);
1177 ctx
->Driver
.PauseTransformFeedback(ctx
, obj
);
1179 obj
->Paused
= GL_TRUE
;
1184 * Resume transform feedback.
1185 * Part of GL_ARB_transform_feedback2.
1188 _mesa_ResumeTransformFeedback(void)
1190 struct gl_transform_feedback_object
*obj
;
1191 GET_CURRENT_CONTEXT(ctx
);
1193 obj
= ctx
->TransformFeedback
.CurrentObject
;
1195 if (!obj
->Active
|| !obj
->Paused
) {
1196 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1197 "glResumeTransformFeedback(feedback not active or not paused)");
1201 /* From the ARB_transform_feedback2 specification:
1202 * "The error INVALID_OPERATION is generated by ResumeTransformFeedback if
1203 * the program object being used by the current transform feedback object
1206 if (obj
->shader_program
!= get_xfb_source(ctx
)) {
1207 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1208 "glResumeTransformFeedback(wrong program bound)");
1212 FLUSH_VERTICES(ctx
, 0);
1213 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
1215 obj
->Paused
= GL_FALSE
;
1217 assert(ctx
->Driver
.ResumeTransformFeedback
);
1218 ctx
->Driver
.ResumeTransformFeedback(ctx
, obj
);
1221 extern void GLAPIENTRY
1222 _mesa_GetTransformFeedbackiv(GLuint xfb
, GLenum pname
, GLint
*param
)
1224 struct gl_transform_feedback_object
*obj
;
1225 GET_CURRENT_CONTEXT(ctx
);
1227 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
1228 "glGetTransformFeedbackiv");
1234 case GL_TRANSFORM_FEEDBACK_PAUSED
:
1235 *param
= obj
->Paused
;
1237 case GL_TRANSFORM_FEEDBACK_ACTIVE
:
1238 *param
= obj
->Active
;
1241 _mesa_error(ctx
, GL_INVALID_ENUM
,
1242 "glGetTransformFeedbackiv(pname=%i)", pname
);
1246 extern void GLAPIENTRY
1247 _mesa_GetTransformFeedbacki_v(GLuint xfb
, GLenum pname
, GLuint index
,
1250 struct gl_transform_feedback_object
*obj
;
1251 GET_CURRENT_CONTEXT(ctx
);
1253 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
1254 "glGetTransformFeedbacki_v");
1259 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
1260 _mesa_error(ctx
, GL_INVALID_VALUE
,
1261 "glGetTransformFeedbacki_v(index=%i)", index
);
1266 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING
:
1267 *param
= obj
->BufferNames
[index
];
1270 _mesa_error(ctx
, GL_INVALID_ENUM
,
1271 "glGetTransformFeedbacki_v(pname=%i)", pname
);
1275 extern void GLAPIENTRY
1276 _mesa_GetTransformFeedbacki64_v(GLuint xfb
, GLenum pname
, GLuint index
,
1279 struct gl_transform_feedback_object
*obj
;
1280 GET_CURRENT_CONTEXT(ctx
);
1282 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
1283 "glGetTransformFeedbacki64_v");
1288 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
1289 _mesa_error(ctx
, GL_INVALID_VALUE
,
1290 "glGetTransformFeedbacki64_v(index=%i)", index
);
1295 case GL_TRANSFORM_FEEDBACK_BUFFER_START
:
1296 *param
= obj
->Offset
[index
];
1298 case GL_TRANSFORM_FEEDBACK_BUFFER_SIZE
:
1299 *param
= obj
->RequestedSize
[index
];
1302 _mesa_error(ctx
, GL_INVALID_ENUM
,
1303 "glGetTransformFeedbacki64_v(pname=%i)", pname
);