2 * Mesa 3-D graphics library
4 * Copyright (C) 2010 VMware, Inc. All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
27 * Transform feedback support.
39 #include "transformfeedback.h"
40 #include "shaderapi.h"
41 #include "shaderobj.h"
43 #include "program/program.h"
44 #include "program/prog_parameter.h"
46 struct using_program_tuple
48 struct gl_program
*prog
;
53 active_xfb_object_references_program(GLuint key
, void *data
, void *user_data
)
55 struct using_program_tuple
*callback_data
= user_data
;
56 struct gl_transform_feedback_object
*obj
= data
;
57 if (obj
->Active
&& obj
->program
== callback_data
->prog
)
58 callback_data
->found
= true;
62 * Return true if any active transform feedback object is using a program.
65 _mesa_transform_feedback_is_using_program(struct gl_context
*ctx
,
66 struct gl_shader_program
*shProg
)
68 if (!shProg
->last_vert_prog
)
71 struct using_program_tuple callback_data
;
72 callback_data
.found
= false;
73 callback_data
.prog
= shProg
->last_vert_prog
;
75 _mesa_HashWalkLocked(ctx
->TransformFeedback
.Objects
,
76 active_xfb_object_references_program
, &callback_data
);
78 /* Also check DefaultObject, as it's not in the Objects hash table. */
79 active_xfb_object_references_program(0, ctx
->TransformFeedback
.DefaultObject
,
82 return callback_data
.found
;
86 * Do reference counting of transform feedback buffers.
89 reference_transform_feedback_object(struct gl_transform_feedback_object
**ptr
,
90 struct gl_transform_feedback_object
*obj
)
96 /* Unreference the old object */
97 struct gl_transform_feedback_object
*oldObj
= *ptr
;
99 assert(oldObj
->RefCount
> 0);
102 if (oldObj
->RefCount
== 0) {
103 GET_CURRENT_CONTEXT(ctx
);
105 ctx
->Driver
.DeleteTransformFeedback(ctx
, oldObj
);
113 assert(obj
->RefCount
> 0);
115 /* reference new object */
117 obj
->EverBound
= GL_TRUE
;
124 * Per-context init for transform feedback.
127 _mesa_init_transform_feedback(struct gl_context
*ctx
)
129 /* core mesa expects this, even a dummy one, to be available */
130 assert(ctx
->Driver
.NewTransformFeedback
);
132 ctx
->TransformFeedback
.DefaultObject
=
133 ctx
->Driver
.NewTransformFeedback(ctx
, 0);
135 assert(ctx
->TransformFeedback
.DefaultObject
->RefCount
== 1);
137 reference_transform_feedback_object(&ctx
->TransformFeedback
.CurrentObject
,
138 ctx
->TransformFeedback
.DefaultObject
);
140 assert(ctx
->TransformFeedback
.DefaultObject
->RefCount
== 2);
142 ctx
->TransformFeedback
.Objects
= _mesa_NewHashTable();
144 _mesa_reference_buffer_object(ctx
,
145 &ctx
->TransformFeedback
.CurrentBuffer
, NULL
);
151 * Callback for _mesa_HashDeleteAll().
154 delete_cb(GLuint key
, void *data
, void *userData
)
156 struct gl_context
*ctx
= (struct gl_context
*) userData
;
157 struct gl_transform_feedback_object
*obj
=
158 (struct gl_transform_feedback_object
*) data
;
160 ctx
->Driver
.DeleteTransformFeedback(ctx
, obj
);
165 * Per-context free/clean-up for transform feedback.
168 _mesa_free_transform_feedback(struct gl_context
*ctx
)
170 /* core mesa expects this, even a dummy one, to be available */
171 assert(ctx
->Driver
.NewTransformFeedback
);
173 _mesa_reference_buffer_object(ctx
,
174 &ctx
->TransformFeedback
.CurrentBuffer
,
177 /* Delete all feedback objects */
178 _mesa_HashDeleteAll(ctx
->TransformFeedback
.Objects
, delete_cb
, ctx
);
179 _mesa_DeleteHashTable(ctx
->TransformFeedback
.Objects
);
181 /* Delete the default feedback object */
182 assert(ctx
->Driver
.DeleteTransformFeedback
);
183 ctx
->Driver
.DeleteTransformFeedback(ctx
,
184 ctx
->TransformFeedback
.DefaultObject
);
186 ctx
->TransformFeedback
.CurrentObject
= NULL
;
190 /** Initialize the fields of a gl_transform_feedback_object. */
192 _mesa_init_transform_feedback_object(struct gl_transform_feedback_object
*obj
,
197 obj
->EverBound
= GL_FALSE
;
201 * Delete a transform feedback object. Called via
202 * ctx->Driver->DeleteTransformFeedback, if not overwritten by driver. In
203 * the latter case, called from the driver after all driver-specific clean-up
206 * \param ctx GL context to wich transform feedback object belongs.
207 * \param obj Transform feedback object due to be deleted.
210 _mesa_delete_transform_feedback_object(struct gl_context
*ctx
,
211 struct gl_transform_feedback_object
214 for (unsigned i
= 0; i
< ARRAY_SIZE(obj
->Buffers
); i
++) {
215 _mesa_reference_buffer_object(ctx
, &obj
->Buffers
[i
], NULL
);
222 /** Default fallback for ctx->Driver.NewTransformFeedback() */
223 static struct gl_transform_feedback_object
*
224 new_transform_feedback_fallback(struct gl_context
*ctx
, GLuint name
)
226 struct gl_transform_feedback_object
*obj
;
228 obj
= CALLOC_STRUCT(gl_transform_feedback_object
);
232 _mesa_init_transform_feedback_object(obj
, name
);
236 /** Default fallback for ctx->Driver.BeginTransformFeedback() */
238 begin_transform_feedback_fallback(struct gl_context
*ctx
, GLenum mode
,
239 struct gl_transform_feedback_object
*obj
)
244 /** Default fallback for ctx->Driver.EndTransformFeedback() */
246 end_transform_feedback_fallback(struct gl_context
*ctx
,
247 struct gl_transform_feedback_object
*obj
)
252 /** Default fallback for ctx->Driver.PauseTransformFeedback() */
254 pause_transform_feedback_fallback(struct gl_context
*ctx
,
255 struct gl_transform_feedback_object
*obj
)
260 /** Default fallback for ctx->Driver.ResumeTransformFeedback() */
262 resume_transform_feedback_fallback(struct gl_context
*ctx
,
263 struct gl_transform_feedback_object
*obj
)
270 * Plug in default device driver functions for transform feedback.
271 * Most drivers will override some/all of these.
274 _mesa_init_transform_feedback_functions(struct dd_function_table
*driver
)
276 driver
->NewTransformFeedback
= new_transform_feedback_fallback
;
277 driver
->DeleteTransformFeedback
= _mesa_delete_transform_feedback_object
;
278 driver
->BeginTransformFeedback
= begin_transform_feedback_fallback
;
279 driver
->EndTransformFeedback
= end_transform_feedback_fallback
;
280 driver
->PauseTransformFeedback
= pause_transform_feedback_fallback
;
281 driver
->ResumeTransformFeedback
= resume_transform_feedback_fallback
;
286 * Fill in the correct Size value for each buffer in \c obj.
288 * From the GL 4.3 spec, section 6.1.1 ("Binding Buffer Objects to Indexed
291 * BindBufferBase binds the entire buffer, even when the size of the buffer
292 * is changed after the binding is established. It is equivalent to calling
293 * BindBufferRange with offset zero, while size is determined by the size of
294 * the bound buffer at the time the binding is used.
296 * Regardless of the size specified with BindBufferRange, or indirectly with
297 * BindBufferBase, the GL will never read or write beyond the end of a bound
298 * buffer. In some cases this constraint may result in visibly different
299 * behavior when a buffer overflow would otherwise result, such as described
300 * for transform feedback operations in section 13.2.2.
303 compute_transform_feedback_buffer_sizes(
304 struct gl_transform_feedback_object
*obj
)
307 for (i
= 0; i
< MAX_FEEDBACK_BUFFERS
; ++i
) {
308 GLintptr offset
= obj
->Offset
[i
];
309 GLsizeiptr buffer_size
310 = obj
->Buffers
[i
] == NULL
? 0 : obj
->Buffers
[i
]->Size
;
311 GLsizeiptr available_space
312 = buffer_size
<= offset
? 0 : buffer_size
- offset
;
313 GLsizeiptr computed_size
;
314 if (obj
->RequestedSize
[i
] == 0) {
315 /* No size was specified at the time the buffer was bound, so allow
316 * writing to all available space in the buffer.
318 computed_size
= available_space
;
320 /* A size was specified at the time the buffer was bound, however
321 * it's possible that the buffer has shrunk since then. So only
322 * allow writing to the minimum of the specified size and the space
325 computed_size
= MIN2(available_space
, obj
->RequestedSize
[i
]);
328 /* Legal sizes must be multiples of four, so round down if necessary. */
329 obj
->Size
[i
] = computed_size
& ~0x3;
335 * Compute the maximum number of vertices that can be written to the currently
336 * enabled transform feedback buffers without overflowing any of them.
339 _mesa_compute_max_transform_feedback_vertices(struct gl_context
*ctx
,
340 const struct gl_transform_feedback_object
*obj
,
341 const struct gl_transform_feedback_info
*info
)
343 unsigned max_index
= 0xffffffff;
346 for (i
= 0; i
< ctx
->Const
.MaxTransformFeedbackBuffers
; i
++) {
347 if ((info
->ActiveBuffers
>> i
) & 1) {
348 unsigned stride
= info
->Buffers
[i
].Stride
;
349 unsigned max_for_this_buffer
;
351 /* Skip any inactive buffers, which have a stride of 0. */
355 max_for_this_buffer
= obj
->Size
[i
] / (4 * stride
);
356 max_index
= MIN2(max_index
, max_for_this_buffer
);
365 ** Begin API functions
370 * Figure out which stage of the pipeline is the source of transform feedback
371 * data given the current context state, and return its gl_program.
373 * If no active program can generate transform feedback data (i.e. no vertex
374 * shader is active), returns NULL.
376 static struct gl_program
*
377 get_xfb_source(struct gl_context
*ctx
)
380 for (i
= MESA_SHADER_GEOMETRY
; i
>= MESA_SHADER_VERTEX
; i
--) {
381 if (ctx
->_Shader
->CurrentProgram
[i
] != NULL
)
382 return ctx
->_Shader
->CurrentProgram
[i
];
388 static ALWAYS_INLINE
void
389 begin_transform_feedback(struct gl_context
*ctx
, GLenum mode
, bool no_error
)
391 struct gl_transform_feedback_object
*obj
;
392 struct gl_transform_feedback_info
*info
= NULL
;
393 struct gl_program
*source
;
395 unsigned vertices_per_prim
;
397 obj
= ctx
->TransformFeedback
.CurrentObject
;
399 /* Figure out what pipeline stage is the source of data for transform
402 source
= get_xfb_source(ctx
);
403 if (!no_error
&& source
== NULL
) {
404 _mesa_error(ctx
, GL_INVALID_OPERATION
,
405 "glBeginTransformFeedback(no program active)");
409 info
= source
->sh
.LinkedTransformFeedback
;
411 if (!no_error
&& info
->NumOutputs
== 0) {
412 _mesa_error(ctx
, GL_INVALID_OPERATION
,
413 "glBeginTransformFeedback(no varyings to record)");
419 vertices_per_prim
= 1;
422 vertices_per_prim
= 2;
425 vertices_per_prim
= 3;
429 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginTransformFeedback(mode)");
432 /* Stop compiler warnings */
433 unreachable("Error in API use when using KHR_no_error");
439 _mesa_error(ctx
, GL_INVALID_OPERATION
,
440 "glBeginTransformFeedback(already active)");
444 for (i
= 0; i
< ctx
->Const
.MaxTransformFeedbackBuffers
; i
++) {
445 if ((info
->ActiveBuffers
>> i
) & 1) {
446 if (obj
->BufferNames
[i
] == 0) {
447 _mesa_error(ctx
, GL_INVALID_OPERATION
,
448 "glBeginTransformFeedback(binding point %d does not "
449 "have a buffer object bound)", i
);
456 FLUSH_VERTICES(ctx
, 0);
457 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
459 obj
->Active
= GL_TRUE
;
460 ctx
->TransformFeedback
.Mode
= mode
;
462 compute_transform_feedback_buffer_sizes(obj
);
464 if (_mesa_is_gles3(ctx
)) {
465 /* In GLES3, we are required to track the usage of the transform
466 * feedback buffer and report INVALID_OPERATION if a draw call tries to
467 * exceed it. So compute the maximum number of vertices that we can
468 * write without overflowing any of the buffers currently being used for
471 unsigned max_vertices
472 = _mesa_compute_max_transform_feedback_vertices(ctx
, obj
, info
);
473 obj
->GlesRemainingPrims
= max_vertices
/ vertices_per_prim
;
476 if (obj
->program
!= source
) {
477 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedbackProg
;
478 _mesa_reference_program_(ctx
, &obj
->program
, source
);
479 obj
->program
= source
;
482 assert(ctx
->Driver
.BeginTransformFeedback
);
483 ctx
->Driver
.BeginTransformFeedback(ctx
, mode
, obj
);
488 _mesa_BeginTransformFeedback_no_error(GLenum mode
)
490 GET_CURRENT_CONTEXT(ctx
);
491 begin_transform_feedback(ctx
, mode
, true);
496 _mesa_BeginTransformFeedback(GLenum mode
)
498 GET_CURRENT_CONTEXT(ctx
);
499 begin_transform_feedback(ctx
, mode
, false);
504 end_transform_feedback(struct gl_context
*ctx
,
505 struct gl_transform_feedback_object
*obj
)
507 FLUSH_VERTICES(ctx
, 0);
508 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
510 assert(ctx
->Driver
.EndTransformFeedback
);
511 ctx
->Driver
.EndTransformFeedback(ctx
, obj
);
513 _mesa_reference_program_(ctx
, &obj
->program
, NULL
);
514 ctx
->TransformFeedback
.CurrentObject
->Active
= GL_FALSE
;
515 ctx
->TransformFeedback
.CurrentObject
->Paused
= GL_FALSE
;
516 ctx
->TransformFeedback
.CurrentObject
->EndedAnytime
= GL_TRUE
;
521 _mesa_EndTransformFeedback_no_error(void)
523 GET_CURRENT_CONTEXT(ctx
);
524 end_transform_feedback(ctx
, ctx
->TransformFeedback
.CurrentObject
);
529 _mesa_EndTransformFeedback(void)
531 struct gl_transform_feedback_object
*obj
;
532 GET_CURRENT_CONTEXT(ctx
);
534 obj
= ctx
->TransformFeedback
.CurrentObject
;
537 _mesa_error(ctx
, GL_INVALID_OPERATION
,
538 "glEndTransformFeedback(not active)");
542 end_transform_feedback(ctx
, obj
);
547 * Helper used by BindBufferRange() and BindBufferBase().
550 bind_buffer_range(struct gl_context
*ctx
,
551 struct gl_transform_feedback_object
*obj
,
553 struct gl_buffer_object
*bufObj
,
554 GLintptr offset
, GLsizeiptr size
,
557 /* Note: no need to FLUSH_VERTICES or flag NewTransformFeedback, because
558 * transform feedback buffers can't be changed while transform feedback is
563 /* The general binding point */
564 _mesa_reference_buffer_object(ctx
,
565 &ctx
->TransformFeedback
.CurrentBuffer
,
569 /* The per-attribute binding point */
570 _mesa_set_transform_feedback_binding(ctx
, obj
, index
, bufObj
, offset
, size
);
575 * Validate the buffer object to receive transform feedback results. Plus,
576 * validate the starting offset to place the results, and max size.
577 * Called from the glBindBufferRange() and glTransformFeedbackBufferRange
581 _mesa_validate_buffer_range_xfb(struct gl_context
*ctx
,
582 struct gl_transform_feedback_object
*obj
,
583 GLuint index
, struct gl_buffer_object
*bufObj
,
584 GLintptr offset
, GLsizeiptr size
, bool dsa
)
586 const char *gl_methd_name
;
588 gl_methd_name
= "glTransformFeedbackBufferRange";
590 gl_methd_name
= "glBindBufferRange";
594 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(transform feedback active)",
599 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
600 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
601 * generated if index is greater than or equal to the number of binding
602 * points for transform feedback, as described in section 6.7.1."
604 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d out of bounds)",
605 gl_methd_name
, index
);
610 /* OpenGL 4.5 core profile, 6.7, pdf page 103: multiple of 4 */
611 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d must be a multiple of "
612 "four)", gl_methd_name
, (int) size
);
617 /* OpenGL 4.5 core profile, 6.7, pdf page 103: multiple of 4 */
618 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset=%d must be a multiple "
619 "of four)", gl_methd_name
, (int) offset
);
624 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
625 * generated by BindBufferRange if offset is negative."
627 * OpenGL 4.5 core profile, 13.2, pdf page 445: "An INVALID_VALUE error
628 * is generated by TransformFeedbackBufferRange if offset is negative."
630 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset=%d must be >= 0)",
636 if (size
<= 0 && (dsa
|| bufObj
)) {
637 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
638 * generated by BindBufferRange if buffer is non-zero and size is less
639 * than or equal to zero."
641 * OpenGL 4.5 core profile, 13.2, pdf page 445: "An INVALID_VALUE error
642 * is generated by TransformFeedbackBufferRange if size is less than or
645 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d must be > 0)",
646 gl_methd_name
, (int) size
);
655 * Specify a buffer object to receive transform feedback results.
656 * As above, but start at offset = 0.
657 * Called from the glBindBufferBase() and glTransformFeedbackBufferBase()
661 _mesa_bind_buffer_base_transform_feedback(struct gl_context
*ctx
,
662 struct gl_transform_feedback_object
*obj
,
664 struct gl_buffer_object
*bufObj
,
668 _mesa_error(ctx
, GL_INVALID_OPERATION
,
669 "%s(transform feedback active)",
670 dsa
? "glTransformFeedbackBufferBase" : "glBindBufferBase");
674 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
675 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d out of bounds)",
676 dsa
? "glTransformFeedbackBufferBase" : "glBindBufferBase",
681 bind_buffer_range(ctx
, obj
, index
, bufObj
, 0, 0, dsa
);
685 * Wrapper around lookup_transform_feedback_object that throws
686 * GL_INVALID_OPERATION if id is not in the hash table. After calling
687 * _mesa_error, it returns NULL.
689 static struct gl_transform_feedback_object
*
690 lookup_transform_feedback_object_err(struct gl_context
*ctx
,
691 GLuint xfb
, const char* func
)
693 struct gl_transform_feedback_object
*obj
;
695 obj
= _mesa_lookup_transform_feedback_object(ctx
, xfb
);
697 _mesa_error(ctx
, GL_INVALID_OPERATION
,
698 "%s(xfb=%u: non-generated object name)", func
, xfb
);
705 * Wrapper around _mesa_lookup_bufferobj that throws GL_INVALID_VALUE if id
706 * is not in the hash table. Specialised version for the
707 * transform-feedback-related functions. After calling _mesa_error, it
710 static struct gl_buffer_object
*
711 lookup_transform_feedback_bufferobj_err(struct gl_context
*ctx
,
712 GLuint buffer
, const char* func
,
715 struct gl_buffer_object
*bufObj
= NULL
;
719 /* OpenGL 4.5 core profile, 13.2, pdf page 444: buffer must be zero or the
720 * name of an existing buffer object.
723 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
725 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid buffer=%u)", func
,
735 _mesa_TransformFeedbackBufferBase(GLuint xfb
, GLuint index
, GLuint buffer
)
737 GET_CURRENT_CONTEXT(ctx
);
738 struct gl_transform_feedback_object
*obj
;
739 struct gl_buffer_object
*bufObj
;
741 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
742 "glTransformFeedbackBufferBase");
748 bufObj
= lookup_transform_feedback_bufferobj_err(ctx
, buffer
,
749 "glTransformFeedbackBufferBase",
755 _mesa_bind_buffer_base_transform_feedback(ctx
, obj
, index
, bufObj
, true);
759 _mesa_TransformFeedbackBufferRange(GLuint xfb
, GLuint index
, GLuint buffer
,
760 GLintptr offset
, GLsizeiptr size
)
762 GET_CURRENT_CONTEXT(ctx
);
763 struct gl_transform_feedback_object
*obj
;
764 struct gl_buffer_object
*bufObj
;
766 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
767 "glTransformFeedbackBufferRange");
773 bufObj
= lookup_transform_feedback_bufferobj_err(ctx
, buffer
,
774 "glTransformFeedbackBufferRange",
780 if (!_mesa_validate_buffer_range_xfb(ctx
, obj
, index
, bufObj
, offset
,
784 /* The per-attribute binding point */
785 _mesa_set_transform_feedback_binding(ctx
, obj
, index
, bufObj
, offset
,
790 * Specify a buffer object to receive transform feedback results, plus the
791 * offset in the buffer to start placing results.
792 * This function is part of GL_EXT_transform_feedback, but not GL3.
794 static ALWAYS_INLINE
void
795 bind_buffer_offset(struct gl_context
*ctx
,
796 struct gl_transform_feedback_object
*obj
, GLuint index
,
797 GLuint buffer
, GLintptr offset
, bool no_error
)
799 struct gl_buffer_object
*bufObj
;
804 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
805 if (!no_error
&& !bufObj
) {
806 _mesa_error(ctx
, GL_INVALID_OPERATION
,
807 "glBindBufferOffsetEXT(invalid buffer=%u)", buffer
);
812 _mesa_bind_buffer_range_xfb(ctx
, obj
, index
, bufObj
, offset
, 0);
817 _mesa_BindBufferOffsetEXT_no_error(GLenum target
, GLuint index
, GLuint buffer
,
820 GET_CURRENT_CONTEXT(ctx
);
821 bind_buffer_offset(ctx
, ctx
->TransformFeedback
.CurrentObject
, index
, buffer
,
827 _mesa_BindBufferOffsetEXT(GLenum target
, GLuint index
, GLuint buffer
,
830 struct gl_transform_feedback_object
*obj
;
831 GET_CURRENT_CONTEXT(ctx
);
833 if (target
!= GL_TRANSFORM_FEEDBACK_BUFFER
) {
834 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferOffsetEXT(target)");
838 obj
= ctx
->TransformFeedback
.CurrentObject
;
841 _mesa_error(ctx
, GL_INVALID_OPERATION
,
842 "glBindBufferOffsetEXT(transform feedback active)");
846 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
847 _mesa_error(ctx
, GL_INVALID_VALUE
,
848 "glBindBufferOffsetEXT(index=%d)", index
);
853 /* must be multiple of four */
854 _mesa_error(ctx
, GL_INVALID_VALUE
,
855 "glBindBufferOffsetEXT(offset=%d)", (int) offset
);
859 bind_buffer_offset(ctx
, obj
, index
, buffer
, offset
, false);
864 * This function specifies the transform feedback outputs to be written
865 * to the feedback buffer(s), and in what order.
867 static ALWAYS_INLINE
void
868 transform_feedback_varyings(struct gl_context
*ctx
,
869 struct gl_shader_program
*shProg
, GLsizei count
,
870 const GLchar
*const *varyings
, GLenum bufferMode
)
874 /* free existing varyings, if any */
875 for (i
= 0; i
< (GLint
) shProg
->TransformFeedback
.NumVarying
; i
++) {
876 free(shProg
->TransformFeedback
.VaryingNames
[i
]);
878 free(shProg
->TransformFeedback
.VaryingNames
);
880 /* allocate new memory for varying names */
881 shProg
->TransformFeedback
.VaryingNames
=
882 malloc(count
* sizeof(GLchar
*));
884 if (!shProg
->TransformFeedback
.VaryingNames
) {
885 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTransformFeedbackVaryings()");
889 /* Save the new names and the count */
890 for (i
= 0; i
< count
; i
++) {
891 shProg
->TransformFeedback
.VaryingNames
[i
] = strdup(varyings
[i
]);
893 shProg
->TransformFeedback
.NumVarying
= count
;
895 shProg
->TransformFeedback
.BufferMode
= bufferMode
;
897 /* No need to invoke FLUSH_VERTICES or flag NewTransformFeedback since
898 * the varyings won't be used until shader link time.
904 _mesa_TransformFeedbackVaryings_no_error(GLuint program
, GLsizei count
,
905 const GLchar
*const *varyings
,
908 GET_CURRENT_CONTEXT(ctx
);
910 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, program
);
911 transform_feedback_varyings(ctx
, shProg
, count
, varyings
, bufferMode
);
915 _mesa_TransformFeedbackVaryings(GLuint program
, GLsizei count
,
916 const GLchar
* const *varyings
,
919 struct gl_shader_program
*shProg
;
921 GET_CURRENT_CONTEXT(ctx
);
923 /* From the ARB_transform_feedback2 specification:
924 * "The error INVALID_OPERATION is generated by TransformFeedbackVaryings
925 * if the current transform feedback object is active, even if paused."
927 if (ctx
->TransformFeedback
.CurrentObject
->Active
) {
928 _mesa_error(ctx
, GL_INVALID_OPERATION
,
929 "glTransformFeedbackVaryings(current object is active)");
933 switch (bufferMode
) {
934 case GL_INTERLEAVED_ATTRIBS
:
936 case GL_SEPARATE_ATTRIBS
:
939 _mesa_error(ctx
, GL_INVALID_ENUM
,
940 "glTransformFeedbackVaryings(bufferMode)");
945 (bufferMode
== GL_SEPARATE_ATTRIBS
&&
946 (GLuint
) count
> ctx
->Const
.MaxTransformFeedbackBuffers
)) {
947 _mesa_error(ctx
, GL_INVALID_VALUE
,
948 "glTransformFeedbackVaryings(count=%d)", count
);
952 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
953 "glTransformFeedbackVaryings");
957 if (ctx
->Extensions
.ARB_transform_feedback3
) {
958 if (bufferMode
== GL_INTERLEAVED_ATTRIBS
) {
959 unsigned buffers
= 1;
961 for (i
= 0; i
< count
; i
++) {
962 if (strcmp(varyings
[i
], "gl_NextBuffer") == 0)
966 if (buffers
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
967 _mesa_error(ctx
, GL_INVALID_OPERATION
,
968 "glTransformFeedbackVaryings(too many gl_NextBuffer "
973 for (i
= 0; i
< count
; i
++) {
974 if (strcmp(varyings
[i
], "gl_NextBuffer") == 0 ||
975 strcmp(varyings
[i
], "gl_SkipComponents1") == 0 ||
976 strcmp(varyings
[i
], "gl_SkipComponents2") == 0 ||
977 strcmp(varyings
[i
], "gl_SkipComponents3") == 0 ||
978 strcmp(varyings
[i
], "gl_SkipComponents4") == 0) {
979 _mesa_error(ctx
, GL_INVALID_OPERATION
,
980 "glTransformFeedbackVaryings(SEPARATE_ATTRIBS,"
989 transform_feedback_varyings(ctx
, shProg
, count
, varyings
, bufferMode
);
994 * Get info about the transform feedback outputs which are to be written
995 * to the feedback buffer(s).
998 _mesa_GetTransformFeedbackVarying(GLuint program
, GLuint index
,
999 GLsizei bufSize
, GLsizei
*length
,
1000 GLsizei
*size
, GLenum
*type
, GLchar
*name
)
1002 const struct gl_shader_program
*shProg
;
1003 struct gl_program_resource
*res
;
1004 GET_CURRENT_CONTEXT(ctx
);
1006 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1007 "glGetTransformFeedbackVarying");
1011 res
= _mesa_program_resource_find_index((struct gl_shader_program
*) shProg
,
1012 GL_TRANSFORM_FEEDBACK_VARYING
,
1015 _mesa_error(ctx
, GL_INVALID_VALUE
,
1016 "glGetTransformFeedbackVarying(index=%u)", index
);
1020 /* return the varying's name and length */
1021 _mesa_copy_string(name
, bufSize
, length
, _mesa_program_resource_name(res
));
1023 /* return the datatype and value's size (in datatype units) */
1025 _mesa_program_resource_prop((struct gl_shader_program
*) shProg
,
1026 res
, index
, GL_TYPE
, (GLint
*) type
,
1027 "glGetTransformFeedbackVarying");
1029 _mesa_program_resource_prop((struct gl_shader_program
*) shProg
,
1030 res
, index
, GL_ARRAY_SIZE
, (GLint
*) size
,
1031 "glGetTransformFeedbackVarying");
1036 struct gl_transform_feedback_object
*
1037 _mesa_lookup_transform_feedback_object(struct gl_context
*ctx
, GLuint name
)
1039 /* OpenGL 4.5 core profile, 13.2 pdf page 444: "xfb must be zero, indicating
1040 * the default transform feedback object, or the name of an existing
1041 * transform feedback object."
1044 return ctx
->TransformFeedback
.DefaultObject
;
1047 return (struct gl_transform_feedback_object
*)
1048 _mesa_HashLookupLocked(ctx
->TransformFeedback
.Objects
, name
);
1052 create_transform_feedbacks(struct gl_context
*ctx
, GLsizei n
, GLuint
*ids
,
1059 func
= "glCreateTransformFeedbacks";
1061 func
= "glGenTransformFeedbacks";
1064 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", func
);
1071 /* we don't need contiguous IDs, but this might be faster */
1072 first
= _mesa_HashFindFreeKeyBlock(ctx
->TransformFeedback
.Objects
, n
);
1075 for (i
= 0; i
< n
; i
++) {
1076 struct gl_transform_feedback_object
*obj
1077 = ctx
->Driver
.NewTransformFeedback(ctx
, first
+ i
);
1079 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1083 _mesa_HashInsertLocked(ctx
->TransformFeedback
.Objects
, first
+ i
,
1086 /* this is normally done at bind time in the non-dsa case */
1087 obj
->EverBound
= GL_TRUE
;
1092 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1097 * Create new transform feedback objects. Transform feedback objects
1098 * encapsulate the state related to transform feedback to allow quickly
1099 * switching state (and drawing the results, below).
1100 * Part of GL_ARB_transform_feedback2.
1103 _mesa_GenTransformFeedbacks(GLsizei n
, GLuint
*names
)
1105 GET_CURRENT_CONTEXT(ctx
);
1107 /* GenTransformFeedbacks should just reserve the object names that a
1108 * subsequent call to BindTransformFeedback should actively create. For
1109 * the sake of simplicity, we reserve the names and create the objects
1113 create_transform_feedbacks(ctx
, n
, names
, false);
1117 * Create new transform feedback objects. Transform feedback objects
1118 * encapsulate the state related to transform feedback to allow quickly
1119 * switching state (and drawing the results, below).
1120 * Part of GL_ARB_direct_state_access.
1123 _mesa_CreateTransformFeedbacks(GLsizei n
, GLuint
*names
)
1125 GET_CURRENT_CONTEXT(ctx
);
1127 create_transform_feedbacks(ctx
, n
, names
, true);
1132 * Is the given ID a transform feedback object?
1133 * Part of GL_ARB_transform_feedback2.
1135 GLboolean GLAPIENTRY
1136 _mesa_IsTransformFeedback(GLuint name
)
1138 struct gl_transform_feedback_object
*obj
;
1139 GET_CURRENT_CONTEXT(ctx
);
1141 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1146 obj
= _mesa_lookup_transform_feedback_object(ctx
, name
);
1150 return obj
->EverBound
;
1155 * Bind the given transform feedback object.
1156 * Part of GL_ARB_transform_feedback2.
1158 static ALWAYS_INLINE
void
1159 bind_transform_feedback(struct gl_context
*ctx
, GLuint name
, bool no_error
)
1161 struct gl_transform_feedback_object
*obj
;
1163 obj
= _mesa_lookup_transform_feedback_object(ctx
, name
);
1164 if (!no_error
&& !obj
) {
1165 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1166 "glBindTransformFeedback(name=%u)", name
);
1170 reference_transform_feedback_object(&ctx
->TransformFeedback
.CurrentObject
,
1176 _mesa_BindTransformFeedback_no_error(GLenum target
, GLuint name
)
1178 GET_CURRENT_CONTEXT(ctx
);
1179 bind_transform_feedback(ctx
, name
, true);
1184 _mesa_BindTransformFeedback(GLenum target
, GLuint name
)
1186 GET_CURRENT_CONTEXT(ctx
);
1188 if (target
!= GL_TRANSFORM_FEEDBACK
) {
1189 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindTransformFeedback(target)");
1193 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1194 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1195 "glBindTransformFeedback(transform is active, or not paused)");
1199 bind_transform_feedback(ctx
, name
, false);
1204 * Delete the given transform feedback objects.
1205 * Part of GL_ARB_transform_feedback2.
1208 _mesa_DeleteTransformFeedbacks(GLsizei n
, const GLuint
*names
)
1211 GET_CURRENT_CONTEXT(ctx
);
1214 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteTransformFeedbacks(n < 0)");
1221 for (i
= 0; i
< n
; i
++) {
1223 struct gl_transform_feedback_object
*obj
1224 = _mesa_lookup_transform_feedback_object(ctx
, names
[i
]);
1227 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1228 "glDeleteTransformFeedbacks(object %u is active)",
1232 _mesa_HashRemoveLocked(ctx
->TransformFeedback
.Objects
, names
[i
]);
1233 /* unref, but object may not be deleted until later */
1234 if (obj
== ctx
->TransformFeedback
.CurrentObject
) {
1235 reference_transform_feedback_object(
1236 &ctx
->TransformFeedback
.CurrentObject
,
1237 ctx
->TransformFeedback
.DefaultObject
);
1239 reference_transform_feedback_object(&obj
, NULL
);
1247 * Pause transform feedback.
1248 * Part of GL_ARB_transform_feedback2.
1251 pause_transform_feedback(struct gl_context
*ctx
,
1252 struct gl_transform_feedback_object
*obj
)
1254 FLUSH_VERTICES(ctx
, 0);
1255 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
1257 assert(ctx
->Driver
.PauseTransformFeedback
);
1258 ctx
->Driver
.PauseTransformFeedback(ctx
, obj
);
1260 obj
->Paused
= GL_TRUE
;
1265 _mesa_PauseTransformFeedback_no_error(void)
1267 GET_CURRENT_CONTEXT(ctx
);
1268 pause_transform_feedback(ctx
, ctx
->TransformFeedback
.CurrentObject
);
1273 _mesa_PauseTransformFeedback(void)
1275 struct gl_transform_feedback_object
*obj
;
1276 GET_CURRENT_CONTEXT(ctx
);
1278 obj
= ctx
->TransformFeedback
.CurrentObject
;
1280 if (!_mesa_is_xfb_active_and_unpaused(ctx
)) {
1281 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1282 "glPauseTransformFeedback(feedback not active or already paused)");
1286 pause_transform_feedback(ctx
, obj
);
1291 * Resume transform feedback.
1292 * Part of GL_ARB_transform_feedback2.
1295 resume_transform_feedback(struct gl_context
*ctx
,
1296 struct gl_transform_feedback_object
*obj
)
1298 FLUSH_VERTICES(ctx
, 0);
1299 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
1301 obj
->Paused
= GL_FALSE
;
1303 assert(ctx
->Driver
.ResumeTransformFeedback
);
1304 ctx
->Driver
.ResumeTransformFeedback(ctx
, obj
);
1309 _mesa_ResumeTransformFeedback_no_error(void)
1311 GET_CURRENT_CONTEXT(ctx
);
1312 resume_transform_feedback(ctx
, ctx
->TransformFeedback
.CurrentObject
);
1317 _mesa_ResumeTransformFeedback(void)
1319 struct gl_transform_feedback_object
*obj
;
1320 GET_CURRENT_CONTEXT(ctx
);
1322 obj
= ctx
->TransformFeedback
.CurrentObject
;
1324 if (!obj
->Active
|| !obj
->Paused
) {
1325 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1326 "glResumeTransformFeedback(feedback not active or not paused)");
1330 /* From the ARB_transform_feedback2 specification:
1331 * "The error INVALID_OPERATION is generated by ResumeTransformFeedback if
1332 * the program object being used by the current transform feedback object
1335 if (obj
->program
!= get_xfb_source(ctx
)) {
1336 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1337 "glResumeTransformFeedback(wrong program bound)");
1341 resume_transform_feedback(ctx
, obj
);
1344 extern void GLAPIENTRY
1345 _mesa_GetTransformFeedbackiv(GLuint xfb
, GLenum pname
, GLint
*param
)
1347 struct gl_transform_feedback_object
*obj
;
1348 GET_CURRENT_CONTEXT(ctx
);
1350 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
1351 "glGetTransformFeedbackiv");
1357 case GL_TRANSFORM_FEEDBACK_PAUSED
:
1358 *param
= obj
->Paused
;
1360 case GL_TRANSFORM_FEEDBACK_ACTIVE
:
1361 *param
= obj
->Active
;
1364 _mesa_error(ctx
, GL_INVALID_ENUM
,
1365 "glGetTransformFeedbackiv(pname=%i)", pname
);
1369 extern void GLAPIENTRY
1370 _mesa_GetTransformFeedbacki_v(GLuint xfb
, GLenum pname
, GLuint index
,
1373 struct gl_transform_feedback_object
*obj
;
1374 GET_CURRENT_CONTEXT(ctx
);
1376 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
1377 "glGetTransformFeedbacki_v");
1382 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
1383 _mesa_error(ctx
, GL_INVALID_VALUE
,
1384 "glGetTransformFeedbacki_v(index=%i)", index
);
1389 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING
:
1390 *param
= obj
->BufferNames
[index
];
1393 _mesa_error(ctx
, GL_INVALID_ENUM
,
1394 "glGetTransformFeedbacki_v(pname=%i)", pname
);
1398 extern void GLAPIENTRY
1399 _mesa_GetTransformFeedbacki64_v(GLuint xfb
, GLenum pname
, GLuint index
,
1402 struct gl_transform_feedback_object
*obj
;
1403 GET_CURRENT_CONTEXT(ctx
);
1405 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
1406 "glGetTransformFeedbacki64_v");
1411 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
1412 _mesa_error(ctx
, GL_INVALID_VALUE
,
1413 "glGetTransformFeedbacki64_v(index=%i)", index
);
1418 * This follows the same general rules used for BindBufferBase:
1420 * "To query the starting offset or size of the range of a buffer
1421 * object binding in an indexed array, call GetInteger64i_v with
1422 * target set to respectively the starting offset or binding size
1423 * name from table 6.5 for that array. Index must be in the range
1424 * zero to the number of bind points supported minus one. If the
1425 * starting offset or size was not specified when the buffer object
1426 * was bound (e.g. if it was bound with BindBufferBase), or if no
1427 * buffer object is bound to the target array at index, zero is
1430 if (obj
->RequestedSize
[index
] == 0 &&
1431 (pname
== GL_TRANSFORM_FEEDBACK_BUFFER_START
||
1432 pname
== GL_TRANSFORM_FEEDBACK_BUFFER_SIZE
)) {
1437 compute_transform_feedback_buffer_sizes(obj
);
1439 case GL_TRANSFORM_FEEDBACK_BUFFER_START
:
1440 assert(obj
->RequestedSize
[index
] > 0);
1441 *param
= obj
->Offset
[index
];
1443 case GL_TRANSFORM_FEEDBACK_BUFFER_SIZE
:
1444 assert(obj
->RequestedSize
[index
] > 0);
1445 *param
= obj
->Size
[index
];
1448 _mesa_error(ctx
, GL_INVALID_ENUM
,
1449 "glGetTransformFeedbacki64_v(pname=%i)", pname
);