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_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 /** Default fallback for ctx->Driver.NewTransformFeedback() */
203 static struct gl_transform_feedback_object
*
204 new_transform_feedback_fallback(struct gl_context
*ctx
, GLuint name
)
206 struct gl_transform_feedback_object
*obj
;
208 obj
= CALLOC_STRUCT(gl_transform_feedback_object
);
212 _mesa_init_transform_feedback_object(obj
, name
);
216 /** Default fallback for ctx->Driver.DeleteTransformFeedback() */
218 delete_transform_feedback_fallback(struct gl_context
*ctx
,
219 struct gl_transform_feedback_object
*obj
)
223 for (i
= 0; i
< ARRAY_SIZE(obj
->Buffers
); i
++) {
224 _mesa_reference_buffer_object(ctx
, &obj
->Buffers
[i
], NULL
);
232 /** Default fallback for ctx->Driver.BeginTransformFeedback() */
234 begin_transform_feedback_fallback(struct gl_context
*ctx
, GLenum mode
,
235 struct gl_transform_feedback_object
*obj
)
240 /** Default fallback for ctx->Driver.EndTransformFeedback() */
242 end_transform_feedback_fallback(struct gl_context
*ctx
,
243 struct gl_transform_feedback_object
*obj
)
248 /** Default fallback for ctx->Driver.PauseTransformFeedback() */
250 pause_transform_feedback_fallback(struct gl_context
*ctx
,
251 struct gl_transform_feedback_object
*obj
)
256 /** Default fallback for ctx->Driver.ResumeTransformFeedback() */
258 resume_transform_feedback_fallback(struct gl_context
*ctx
,
259 struct gl_transform_feedback_object
*obj
)
266 * Plug in default device driver functions for transform feedback.
267 * Most drivers will override some/all of these.
270 _mesa_init_transform_feedback_functions(struct dd_function_table
*driver
)
272 driver
->NewTransformFeedback
= new_transform_feedback_fallback
;
273 driver
->DeleteTransformFeedback
= delete_transform_feedback_fallback
;
274 driver
->BeginTransformFeedback
= begin_transform_feedback_fallback
;
275 driver
->EndTransformFeedback
= end_transform_feedback_fallback
;
276 driver
->PauseTransformFeedback
= pause_transform_feedback_fallback
;
277 driver
->ResumeTransformFeedback
= resume_transform_feedback_fallback
;
282 * Fill in the correct Size value for each buffer in \c obj.
284 * From the GL 4.3 spec, section 6.1.1 ("Binding Buffer Objects to Indexed
287 * BindBufferBase binds the entire buffer, even when the size of the buffer
288 * is changed after the binding is established. It is equivalent to calling
289 * BindBufferRange with offset zero, while size is determined by the size of
290 * the bound buffer at the time the binding is used.
292 * Regardless of the size specified with BindBufferRange, or indirectly with
293 * BindBufferBase, the GL will never read or write beyond the end of a bound
294 * buffer. In some cases this constraint may result in visibly different
295 * behavior when a buffer overflow would otherwise result, such as described
296 * for transform feedback operations in section 13.2.2.
299 compute_transform_feedback_buffer_sizes(
300 struct gl_transform_feedback_object
*obj
)
303 for (i
= 0; i
< MAX_FEEDBACK_BUFFERS
; ++i
) {
304 GLintptr offset
= obj
->Offset
[i
];
305 GLsizeiptr buffer_size
306 = obj
->Buffers
[i
] == NULL
? 0 : obj
->Buffers
[i
]->Size
;
307 GLsizeiptr available_space
308 = buffer_size
<= offset
? 0 : buffer_size
- offset
;
309 GLsizeiptr computed_size
;
310 if (obj
->RequestedSize
[i
] == 0) {
311 /* No size was specified at the time the buffer was bound, so allow
312 * writing to all available space in the buffer.
314 computed_size
= available_space
;
316 /* A size was specified at the time the buffer was bound, however
317 * it's possible that the buffer has shrunk since then. So only
318 * allow writing to the minimum of the specified size and the space
321 computed_size
= MIN2(available_space
, obj
->RequestedSize
[i
]);
324 /* Legal sizes must be multiples of four, so round down if necessary. */
325 obj
->Size
[i
] = computed_size
& ~0x3;
331 * Compute the maximum number of vertices that can be written to the currently
332 * enabled transform feedback buffers without overflowing any of them.
335 _mesa_compute_max_transform_feedback_vertices(struct gl_context
*ctx
,
336 const struct gl_transform_feedback_object
*obj
,
337 const struct gl_transform_feedback_info
*info
)
339 unsigned max_index
= 0xffffffff;
342 for (i
= 0; i
< ctx
->Const
.MaxTransformFeedbackBuffers
; i
++) {
343 if ((info
->ActiveBuffers
>> i
) & 1) {
344 unsigned stride
= info
->Buffers
[i
].Stride
;
345 unsigned max_for_this_buffer
;
347 /* Skip any inactive buffers, which have a stride of 0. */
351 max_for_this_buffer
= obj
->Size
[i
] / (4 * stride
);
352 max_index
= MIN2(max_index
, max_for_this_buffer
);
361 ** Begin API functions
366 * Figure out which stage of the pipeline is the source of transform feedback
367 * data given the current context state, and return its gl_program.
369 * If no active program can generate transform feedback data (i.e. no vertex
370 * shader is active), returns NULL.
372 static struct gl_program
*
373 get_xfb_source(struct gl_context
*ctx
)
376 for (i
= MESA_SHADER_GEOMETRY
; i
>= MESA_SHADER_VERTEX
; i
--) {
377 if (ctx
->_Shader
->CurrentProgram
[i
] != NULL
)
378 return ctx
->_Shader
->CurrentProgram
[i
];
385 _mesa_BeginTransformFeedback(GLenum mode
)
387 struct gl_transform_feedback_object
*obj
;
388 struct gl_transform_feedback_info
*info
= NULL
;
390 unsigned vertices_per_prim
;
391 GET_CURRENT_CONTEXT(ctx
);
393 obj
= ctx
->TransformFeedback
.CurrentObject
;
395 /* Figure out what pipeline stage is the source of data for transform
398 struct gl_program
*source
= get_xfb_source(ctx
);
399 if (source
== NULL
) {
400 _mesa_error(ctx
, GL_INVALID_OPERATION
,
401 "glBeginTransformFeedback(no program active)");
405 info
= source
->sh
.LinkedTransformFeedback
;
407 if (info
->NumOutputs
== 0) {
408 _mesa_error(ctx
, GL_INVALID_OPERATION
,
409 "glBeginTransformFeedback(no varyings to record)");
415 vertices_per_prim
= 1;
418 vertices_per_prim
= 2;
421 vertices_per_prim
= 3;
424 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginTransformFeedback(mode)");
429 _mesa_error(ctx
, GL_INVALID_OPERATION
,
430 "glBeginTransformFeedback(already active)");
434 for (i
= 0; i
< ctx
->Const
.MaxTransformFeedbackBuffers
; i
++) {
435 if ((info
->ActiveBuffers
>> i
) & 1) {
436 if (obj
->BufferNames
[i
] == 0) {
437 _mesa_error(ctx
, GL_INVALID_OPERATION
,
438 "glBeginTransformFeedback(binding point %d does not "
439 "have a buffer object bound)", i
);
445 FLUSH_VERTICES(ctx
, 0);
446 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
448 obj
->Active
= GL_TRUE
;
449 ctx
->TransformFeedback
.Mode
= mode
;
451 compute_transform_feedback_buffer_sizes(obj
);
453 if (_mesa_is_gles3(ctx
)) {
454 /* In GLES3, we are required to track the usage of the transform
455 * feedback buffer and report INVALID_OPERATION if a draw call tries to
456 * exceed it. So compute the maximum number of vertices that we can
457 * write without overflowing any of the buffers currently being used for
460 unsigned max_vertices
461 = _mesa_compute_max_transform_feedback_vertices(ctx
, obj
, info
);
462 obj
->GlesRemainingPrims
= max_vertices
/ vertices_per_prim
;
465 if (obj
->program
!= source
) {
466 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedbackProg
;
467 obj
->program
= source
;
470 assert(ctx
->Driver
.BeginTransformFeedback
);
471 ctx
->Driver
.BeginTransformFeedback(ctx
, mode
, obj
);
476 _mesa_EndTransformFeedback(void)
478 struct gl_transform_feedback_object
*obj
;
479 GET_CURRENT_CONTEXT(ctx
);
481 obj
= ctx
->TransformFeedback
.CurrentObject
;
484 _mesa_error(ctx
, GL_INVALID_OPERATION
,
485 "glEndTransformFeedback(not active)");
489 FLUSH_VERTICES(ctx
, 0);
490 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
492 assert(ctx
->Driver
.EndTransformFeedback
);
493 ctx
->Driver
.EndTransformFeedback(ctx
, obj
);
495 ctx
->TransformFeedback
.CurrentObject
->Active
= GL_FALSE
;
496 ctx
->TransformFeedback
.CurrentObject
->Paused
= GL_FALSE
;
497 ctx
->TransformFeedback
.CurrentObject
->EndedAnytime
= GL_TRUE
;
502 * Helper used by BindBufferRange() and BindBufferBase().
505 bind_buffer_range(struct gl_context
*ctx
,
506 struct gl_transform_feedback_object
*obj
,
508 struct gl_buffer_object
*bufObj
,
509 GLintptr offset
, GLsizeiptr size
,
512 /* Note: no need to FLUSH_VERTICES or flag NewTransformFeedback, because
513 * transform feedback buffers can't be changed while transform feedback is
518 /* The general binding point */
519 _mesa_reference_buffer_object(ctx
,
520 &ctx
->TransformFeedback
.CurrentBuffer
,
524 /* The per-attribute binding point */
525 _mesa_set_transform_feedback_binding(ctx
, obj
, index
, bufObj
, offset
, size
);
530 * Validate the buffer object to receive transform feedback results. Plus,
531 * validate the starting offset to place the results, and max size.
532 * Called from the glBindBufferRange() and glTransformFeedbackBufferRange
536 _mesa_validate_buffer_range_xfb(struct gl_context
*ctx
,
537 struct gl_transform_feedback_object
*obj
,
538 GLuint index
, struct gl_buffer_object
*bufObj
,
539 GLintptr offset
, GLsizeiptr size
, bool dsa
)
541 const char *gl_methd_name
;
543 gl_methd_name
= "glTransformFeedbackBufferRange";
545 gl_methd_name
= "glBindBufferRange";
549 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(transform feedback active)",
554 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
555 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
556 * generated if index is greater than or equal to the number of binding
557 * points for transform feedback, as described in section 6.7.1."
559 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d out of bounds)",
560 gl_methd_name
, index
);
565 /* OpenGL 4.5 core profile, 6.7, pdf page 103: multiple of 4 */
566 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d must be a multiple of "
567 "four)", gl_methd_name
, (int) size
);
572 /* OpenGL 4.5 core profile, 6.7, pdf page 103: multiple of 4 */
573 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset=%d must be a multiple "
574 "of four)", gl_methd_name
, (int) offset
);
579 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
580 * generated by BindBufferRange if offset is negative."
582 * OpenGL 4.5 core profile, 13.2, pdf page 445: "An INVALID_VALUE error
583 * is generated by TransformFeedbackBufferRange if offset is negative."
585 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset=%d must be >= 0)",
591 if (size
<= 0 && (dsa
|| bufObj
!= ctx
->Shared
->NullBufferObj
)) {
592 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
593 * generated by BindBufferRange if buffer is non-zero and size is less
594 * than or equal to zero."
596 * OpenGL 4.5 core profile, 13.2, pdf page 445: "An INVALID_VALUE error
597 * is generated by TransformFeedbackBufferRange if size is less than or
600 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d must be > 0)",
601 gl_methd_name
, (int) size
);
610 * Specify a buffer object to receive transform feedback results.
611 * As above, but start at offset = 0.
612 * Called from the glBindBufferBase() and glTransformFeedbackBufferBase()
616 _mesa_bind_buffer_base_transform_feedback(struct gl_context
*ctx
,
617 struct gl_transform_feedback_object
*obj
,
619 struct gl_buffer_object
*bufObj
,
623 _mesa_error(ctx
, GL_INVALID_OPERATION
,
624 "%s(transform feedback active)",
625 dsa
? "glTransformFeedbackBufferBase" : "glBindBufferBase");
629 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
630 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d out of bounds)",
631 dsa
? "glTransformFeedbackBufferBase" : "glBindBufferBase",
636 bind_buffer_range(ctx
, obj
, index
, bufObj
, 0, 0, dsa
);
640 * Wrapper around lookup_transform_feedback_object that throws
641 * GL_INVALID_OPERATION if id is not in the hash table. After calling
642 * _mesa_error, it returns NULL.
644 static struct gl_transform_feedback_object
*
645 lookup_transform_feedback_object_err(struct gl_context
*ctx
,
646 GLuint xfb
, const char* func
)
648 struct gl_transform_feedback_object
*obj
;
650 obj
= _mesa_lookup_transform_feedback_object(ctx
, xfb
);
652 _mesa_error(ctx
, GL_INVALID_OPERATION
,
653 "%s(xfb=%u: non-generated object name)", func
, xfb
);
660 * Wrapper around _mesa_lookup_bufferobj that throws GL_INVALID_VALUE if id
661 * is not in the hash table. Specialised version for the
662 * transform-feedback-related functions. After calling _mesa_error, it
665 static struct gl_buffer_object
*
666 lookup_transform_feedback_bufferobj_err(struct gl_context
*ctx
,
667 GLuint buffer
, const char* func
)
669 struct gl_buffer_object
*bufObj
;
671 /* OpenGL 4.5 core profile, 13.2, pdf page 444: buffer must be zero or the
672 * name of an existing buffer object.
675 bufObj
= ctx
->Shared
->NullBufferObj
;
677 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
679 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid buffer=%u)", func
,
688 _mesa_TransformFeedbackBufferBase(GLuint xfb
, GLuint index
, GLuint buffer
)
690 GET_CURRENT_CONTEXT(ctx
);
691 struct gl_transform_feedback_object
*obj
;
692 struct gl_buffer_object
*bufObj
;
694 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
695 "glTransformFeedbackBufferBase");
700 bufObj
= lookup_transform_feedback_bufferobj_err(ctx
, buffer
,
701 "glTransformFeedbackBufferBase");
706 _mesa_bind_buffer_base_transform_feedback(ctx
, obj
, index
, bufObj
, true);
710 _mesa_TransformFeedbackBufferRange(GLuint xfb
, GLuint index
, GLuint buffer
,
711 GLintptr offset
, GLsizeiptr size
)
713 GET_CURRENT_CONTEXT(ctx
);
714 struct gl_transform_feedback_object
*obj
;
715 struct gl_buffer_object
*bufObj
;
717 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
718 "glTransformFeedbackBufferRange");
723 bufObj
= lookup_transform_feedback_bufferobj_err(ctx
, buffer
,
724 "glTransformFeedbackBufferRange");
729 if (!_mesa_validate_buffer_range_xfb(ctx
, obj
, index
, bufObj
, offset
,
733 /* The per-attribute binding point */
734 _mesa_set_transform_feedback_binding(ctx
, obj
, index
, bufObj
, offset
,
739 * Specify a buffer object to receive transform feedback results, plus the
740 * offset in the buffer to start placing results.
741 * This function is part of GL_EXT_transform_feedback, but not GL3.
744 _mesa_BindBufferOffsetEXT(GLenum target
, GLuint index
, GLuint buffer
,
747 struct gl_transform_feedback_object
*obj
;
748 struct gl_buffer_object
*bufObj
;
749 GET_CURRENT_CONTEXT(ctx
);
751 if (target
!= GL_TRANSFORM_FEEDBACK_BUFFER
) {
752 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferOffsetEXT(target)");
756 obj
= ctx
->TransformFeedback
.CurrentObject
;
759 _mesa_error(ctx
, GL_INVALID_OPERATION
,
760 "glBindBufferOffsetEXT(transform feedback active)");
764 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
765 _mesa_error(ctx
, GL_INVALID_VALUE
,
766 "glBindBufferOffsetEXT(index=%d)", index
);
771 /* must be multiple of four */
772 _mesa_error(ctx
, GL_INVALID_VALUE
,
773 "glBindBufferOffsetEXT(offset=%d)", (int) offset
);
778 bufObj
= ctx
->Shared
->NullBufferObj
;
780 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
782 _mesa_error(ctx
, GL_INVALID_OPERATION
,
783 "glBindBufferOffsetEXT(invalid buffer=%u)", buffer
);
788 _mesa_bind_buffer_range_xfb(ctx
, obj
, index
, bufObj
, offset
, 0);
793 * This function specifies the transform feedback outputs to be written
794 * to the feedback buffer(s), and in what order.
797 _mesa_TransformFeedbackVaryings(GLuint program
, GLsizei count
,
798 const GLchar
* const *varyings
,
801 struct gl_shader_program
*shProg
;
803 GET_CURRENT_CONTEXT(ctx
);
805 /* From the ARB_transform_feedback2 specification:
806 * "The error INVALID_OPERATION is generated by TransformFeedbackVaryings
807 * if the current transform feedback object is active, even if paused."
809 if (ctx
->TransformFeedback
.CurrentObject
->Active
) {
810 _mesa_error(ctx
, GL_INVALID_OPERATION
,
811 "glTransformFeedbackVaryings(current object is active)");
815 switch (bufferMode
) {
816 case GL_INTERLEAVED_ATTRIBS
:
818 case GL_SEPARATE_ATTRIBS
:
821 _mesa_error(ctx
, GL_INVALID_ENUM
,
822 "glTransformFeedbackVaryings(bufferMode)");
827 (bufferMode
== GL_SEPARATE_ATTRIBS
&&
828 (GLuint
) count
> ctx
->Const
.MaxTransformFeedbackBuffers
)) {
829 _mesa_error(ctx
, GL_INVALID_VALUE
,
830 "glTransformFeedbackVaryings(count=%d)", count
);
834 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
835 "glTransformFeedbackVaryings");
839 if (ctx
->Extensions
.ARB_transform_feedback3
) {
840 if (bufferMode
== GL_INTERLEAVED_ATTRIBS
) {
841 unsigned buffers
= 1;
843 for (i
= 0; i
< count
; i
++) {
844 if (strcmp(varyings
[i
], "gl_NextBuffer") == 0)
848 if (buffers
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
849 _mesa_error(ctx
, GL_INVALID_OPERATION
,
850 "glTransformFeedbackVaryings(too many gl_NextBuffer "
855 for (i
= 0; i
< count
; i
++) {
856 if (strcmp(varyings
[i
], "gl_NextBuffer") == 0 ||
857 strcmp(varyings
[i
], "gl_SkipComponents1") == 0 ||
858 strcmp(varyings
[i
], "gl_SkipComponents2") == 0 ||
859 strcmp(varyings
[i
], "gl_SkipComponents3") == 0 ||
860 strcmp(varyings
[i
], "gl_SkipComponents4") == 0) {
861 _mesa_error(ctx
, GL_INVALID_OPERATION
,
862 "glTransformFeedbackVaryings(SEPARATE_ATTRIBS,"
871 /* free existing varyings, if any */
872 for (i
= 0; i
< (GLint
) shProg
->TransformFeedback
.NumVarying
; i
++) {
873 free(shProg
->TransformFeedback
.VaryingNames
[i
]);
875 free(shProg
->TransformFeedback
.VaryingNames
);
877 /* allocate new memory for varying names */
878 shProg
->TransformFeedback
.VaryingNames
=
879 malloc(count
* sizeof(GLchar
*));
881 if (!shProg
->TransformFeedback
.VaryingNames
) {
882 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTransformFeedbackVaryings()");
886 /* Save the new names and the count */
887 for (i
= 0; i
< count
; i
++) {
888 shProg
->TransformFeedback
.VaryingNames
[i
] = strdup(varyings
[i
]);
890 shProg
->TransformFeedback
.NumVarying
= count
;
892 shProg
->TransformFeedback
.BufferMode
= bufferMode
;
894 /* No need to invoke FLUSH_VERTICES or flag NewTransformFeedback since
895 * the varyings won't be used until shader link time.
901 * Get info about the transform feedback outputs which are to be written
902 * to the feedback buffer(s).
905 _mesa_GetTransformFeedbackVarying(GLuint program
, GLuint index
,
906 GLsizei bufSize
, GLsizei
*length
,
907 GLsizei
*size
, GLenum
*type
, GLchar
*name
)
909 const struct gl_shader_program
*shProg
;
910 struct gl_program_resource
*res
;
911 GET_CURRENT_CONTEXT(ctx
);
913 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
914 "glGetTransformFeedbackVarying");
918 res
= _mesa_program_resource_find_index((struct gl_shader_program
*) shProg
,
919 GL_TRANSFORM_FEEDBACK_VARYING
,
922 _mesa_error(ctx
, GL_INVALID_VALUE
,
923 "glGetTransformFeedbackVarying(index=%u)", index
);
927 /* return the varying's name and length */
928 _mesa_copy_string(name
, bufSize
, length
, _mesa_program_resource_name(res
));
930 /* return the datatype and value's size (in datatype units) */
932 _mesa_program_resource_prop((struct gl_shader_program
*) shProg
,
933 res
, index
, GL_TYPE
, (GLint
*) type
,
934 "glGetTransformFeedbackVarying");
936 _mesa_program_resource_prop((struct gl_shader_program
*) shProg
,
937 res
, index
, GL_ARRAY_SIZE
, (GLint
*) size
,
938 "glGetTransformFeedbackVarying");
943 struct gl_transform_feedback_object
*
944 _mesa_lookup_transform_feedback_object(struct gl_context
*ctx
, GLuint name
)
946 /* OpenGL 4.5 core profile, 13.2 pdf page 444: "xfb must be zero, indicating
947 * the default transform feedback object, or the name of an existing
948 * transform feedback object."
951 return ctx
->TransformFeedback
.DefaultObject
;
954 return (struct gl_transform_feedback_object
*)
955 _mesa_HashLookupLocked(ctx
->TransformFeedback
.Objects
, name
);
959 create_transform_feedbacks(struct gl_context
*ctx
, GLsizei n
, GLuint
*ids
,
966 func
= "glCreateTransformFeedbacks";
968 func
= "glGenTransformFeedbacks";
971 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", func
);
978 /* we don't need contiguous IDs, but this might be faster */
979 first
= _mesa_HashFindFreeKeyBlock(ctx
->TransformFeedback
.Objects
, n
);
982 for (i
= 0; i
< n
; i
++) {
983 struct gl_transform_feedback_object
*obj
984 = ctx
->Driver
.NewTransformFeedback(ctx
, first
+ i
);
986 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
990 _mesa_HashInsertLocked(ctx
->TransformFeedback
.Objects
, first
+ i
,
993 /* this is normally done at bind time in the non-dsa case */
994 obj
->EverBound
= GL_TRUE
;
999 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1004 * Create new transform feedback objects. Transform feedback objects
1005 * encapsulate the state related to transform feedback to allow quickly
1006 * switching state (and drawing the results, below).
1007 * Part of GL_ARB_transform_feedback2.
1010 _mesa_GenTransformFeedbacks(GLsizei n
, GLuint
*names
)
1012 GET_CURRENT_CONTEXT(ctx
);
1014 /* GenTransformFeedbacks should just reserve the object names that a
1015 * subsequent call to BindTransformFeedback should actively create. For
1016 * the sake of simplicity, we reserve the names and create the objects
1020 create_transform_feedbacks(ctx
, n
, names
, false);
1024 * Create new transform feedback objects. Transform feedback objects
1025 * encapsulate the state related to transform feedback to allow quickly
1026 * switching state (and drawing the results, below).
1027 * Part of GL_ARB_direct_state_access.
1030 _mesa_CreateTransformFeedbacks(GLsizei n
, GLuint
*names
)
1032 GET_CURRENT_CONTEXT(ctx
);
1034 create_transform_feedbacks(ctx
, n
, names
, true);
1039 * Is the given ID a transform feedback object?
1040 * Part of GL_ARB_transform_feedback2.
1042 GLboolean GLAPIENTRY
1043 _mesa_IsTransformFeedback(GLuint name
)
1045 struct gl_transform_feedback_object
*obj
;
1046 GET_CURRENT_CONTEXT(ctx
);
1048 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1053 obj
= _mesa_lookup_transform_feedback_object(ctx
, name
);
1057 return obj
->EverBound
;
1062 * Bind the given transform feedback object.
1063 * Part of GL_ARB_transform_feedback2.
1065 static ALWAYS_INLINE
void
1066 bind_transform_feedback(struct gl_context
*ctx
, GLuint name
, bool no_error
)
1068 struct gl_transform_feedback_object
*obj
;
1070 obj
= _mesa_lookup_transform_feedback_object(ctx
, name
);
1071 if (!no_error
&& !obj
) {
1072 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1073 "glBindTransformFeedback(name=%u)", name
);
1077 reference_transform_feedback_object(&ctx
->TransformFeedback
.CurrentObject
,
1083 _mesa_BindTransformFeedback_no_error(GLenum target
, GLuint name
)
1085 GET_CURRENT_CONTEXT(ctx
);
1086 bind_transform_feedback(ctx
, name
, true);
1091 _mesa_BindTransformFeedback(GLenum target
, GLuint name
)
1093 GET_CURRENT_CONTEXT(ctx
);
1095 if (target
!= GL_TRANSFORM_FEEDBACK
) {
1096 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindTransformFeedback(target)");
1100 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1101 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1102 "glBindTransformFeedback(transform is active, or not paused)");
1106 bind_transform_feedback(ctx
, name
, false);
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_HashRemoveLocked(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
);