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 #include "util/u_memory.h"
48 struct using_program_tuple
50 struct gl_program
*prog
;
55 active_xfb_object_references_program(GLuint key
, void *data
, void *user_data
)
57 struct using_program_tuple
*callback_data
= user_data
;
58 struct gl_transform_feedback_object
*obj
= data
;
59 if (obj
->Active
&& obj
->program
== callback_data
->prog
)
60 callback_data
->found
= true;
64 * Return true if any active transform feedback object is using a program.
67 _mesa_transform_feedback_is_using_program(struct gl_context
*ctx
,
68 struct gl_shader_program
*shProg
)
70 if (!shProg
->last_vert_prog
)
73 struct using_program_tuple callback_data
;
74 callback_data
.found
= false;
75 callback_data
.prog
= shProg
->last_vert_prog
;
77 _mesa_HashWalkLocked(ctx
->TransformFeedback
.Objects
,
78 active_xfb_object_references_program
, &callback_data
);
80 /* Also check DefaultObject, as it's not in the Objects hash table. */
81 active_xfb_object_references_program(0, ctx
->TransformFeedback
.DefaultObject
,
84 return callback_data
.found
;
88 * Do reference counting of transform feedback buffers.
91 reference_transform_feedback_object(struct gl_transform_feedback_object
**ptr
,
92 struct gl_transform_feedback_object
*obj
)
98 /* Unreference the old object */
99 struct gl_transform_feedback_object
*oldObj
= *ptr
;
101 assert(oldObj
->RefCount
> 0);
104 if (oldObj
->RefCount
== 0) {
105 GET_CURRENT_CONTEXT(ctx
);
107 ctx
->Driver
.DeleteTransformFeedback(ctx
, oldObj
);
115 assert(obj
->RefCount
> 0);
117 /* reference new object */
119 obj
->EverBound
= GL_TRUE
;
126 * Per-context init for transform feedback.
129 _mesa_init_transform_feedback(struct gl_context
*ctx
)
131 /* core mesa expects this, even a dummy one, to be available */
132 assert(ctx
->Driver
.NewTransformFeedback
);
134 ctx
->TransformFeedback
.DefaultObject
=
135 ctx
->Driver
.NewTransformFeedback(ctx
, 0);
137 assert(ctx
->TransformFeedback
.DefaultObject
->RefCount
== 1);
139 reference_transform_feedback_object(&ctx
->TransformFeedback
.CurrentObject
,
140 ctx
->TransformFeedback
.DefaultObject
);
142 assert(ctx
->TransformFeedback
.DefaultObject
->RefCount
== 2);
144 ctx
->TransformFeedback
.Objects
= _mesa_NewHashTable();
146 _mesa_reference_buffer_object(ctx
,
147 &ctx
->TransformFeedback
.CurrentBuffer
, NULL
);
153 * Callback for _mesa_HashDeleteAll().
156 delete_cb(GLuint key
, void *data
, void *userData
)
158 struct gl_context
*ctx
= (struct gl_context
*) userData
;
159 struct gl_transform_feedback_object
*obj
=
160 (struct gl_transform_feedback_object
*) data
;
162 ctx
->Driver
.DeleteTransformFeedback(ctx
, obj
);
167 * Per-context free/clean-up for transform feedback.
170 _mesa_free_transform_feedback(struct gl_context
*ctx
)
172 /* core mesa expects this, even a dummy one, to be available */
173 assert(ctx
->Driver
.NewTransformFeedback
);
175 _mesa_reference_buffer_object(ctx
,
176 &ctx
->TransformFeedback
.CurrentBuffer
,
179 /* Delete all feedback objects */
180 _mesa_HashDeleteAll(ctx
->TransformFeedback
.Objects
, delete_cb
, ctx
);
181 _mesa_DeleteHashTable(ctx
->TransformFeedback
.Objects
);
183 /* Delete the default feedback object */
184 assert(ctx
->Driver
.DeleteTransformFeedback
);
185 ctx
->Driver
.DeleteTransformFeedback(ctx
,
186 ctx
->TransformFeedback
.DefaultObject
);
188 ctx
->TransformFeedback
.CurrentObject
= NULL
;
192 /** Initialize the fields of a gl_transform_feedback_object. */
194 _mesa_init_transform_feedback_object(struct gl_transform_feedback_object
*obj
,
199 obj
->EverBound
= GL_FALSE
;
203 * Delete a transform feedback object. Called via
204 * ctx->Driver->DeleteTransformFeedback, if not overwritten by driver. In
205 * the latter case, called from the driver after all driver-specific clean-up
208 * \param ctx GL context to wich transform feedback object belongs.
209 * \param obj Transform feedback object due to be deleted.
212 _mesa_delete_transform_feedback_object(struct gl_context
*ctx
,
213 struct gl_transform_feedback_object
216 for (unsigned i
= 0; i
< ARRAY_SIZE(obj
->Buffers
); i
++) {
217 _mesa_reference_buffer_object(ctx
, &obj
->Buffers
[i
], NULL
);
224 /** Default fallback for ctx->Driver.NewTransformFeedback() */
225 static struct gl_transform_feedback_object
*
226 new_transform_feedback_fallback(struct gl_context
*ctx
, GLuint name
)
228 struct gl_transform_feedback_object
*obj
;
230 obj
= CALLOC_STRUCT(gl_transform_feedback_object
);
234 _mesa_init_transform_feedback_object(obj
, name
);
238 /** Default fallback for ctx->Driver.BeginTransformFeedback() */
240 begin_transform_feedback_fallback(struct gl_context
*ctx
, GLenum mode
,
241 struct gl_transform_feedback_object
*obj
)
246 /** Default fallback for ctx->Driver.EndTransformFeedback() */
248 end_transform_feedback_fallback(struct gl_context
*ctx
,
249 struct gl_transform_feedback_object
*obj
)
254 /** Default fallback for ctx->Driver.PauseTransformFeedback() */
256 pause_transform_feedback_fallback(struct gl_context
*ctx
,
257 struct gl_transform_feedback_object
*obj
)
262 /** Default fallback for ctx->Driver.ResumeTransformFeedback() */
264 resume_transform_feedback_fallback(struct gl_context
*ctx
,
265 struct gl_transform_feedback_object
*obj
)
272 * Plug in default device driver functions for transform feedback.
273 * Most drivers will override some/all of these.
276 _mesa_init_transform_feedback_functions(struct dd_function_table
*driver
)
278 driver
->NewTransformFeedback
= new_transform_feedback_fallback
;
279 driver
->DeleteTransformFeedback
= _mesa_delete_transform_feedback_object
;
280 driver
->BeginTransformFeedback
= begin_transform_feedback_fallback
;
281 driver
->EndTransformFeedback
= end_transform_feedback_fallback
;
282 driver
->PauseTransformFeedback
= pause_transform_feedback_fallback
;
283 driver
->ResumeTransformFeedback
= resume_transform_feedback_fallback
;
288 * Fill in the correct Size value for each buffer in \c obj.
290 * From the GL 4.3 spec, section 6.1.1 ("Binding Buffer Objects to Indexed
293 * BindBufferBase binds the entire buffer, even when the size of the buffer
294 * is changed after the binding is established. It is equivalent to calling
295 * BindBufferRange with offset zero, while size is determined by the size of
296 * the bound buffer at the time the binding is used.
298 * Regardless of the size specified with BindBufferRange, or indirectly with
299 * BindBufferBase, the GL will never read or write beyond the end of a bound
300 * buffer. In some cases this constraint may result in visibly different
301 * behavior when a buffer overflow would otherwise result, such as described
302 * for transform feedback operations in section 13.2.2.
305 compute_transform_feedback_buffer_sizes(
306 struct gl_transform_feedback_object
*obj
)
309 for (i
= 0; i
< MAX_FEEDBACK_BUFFERS
; ++i
) {
310 GLintptr offset
= obj
->Offset
[i
];
311 GLsizeiptr buffer_size
312 = obj
->Buffers
[i
] == NULL
? 0 : obj
->Buffers
[i
]->Size
;
313 GLsizeiptr available_space
314 = buffer_size
<= offset
? 0 : buffer_size
- offset
;
315 GLsizeiptr computed_size
;
316 if (obj
->RequestedSize
[i
] == 0) {
317 /* No size was specified at the time the buffer was bound, so allow
318 * writing to all available space in the buffer.
320 computed_size
= available_space
;
322 /* A size was specified at the time the buffer was bound, however
323 * it's possible that the buffer has shrunk since then. So only
324 * allow writing to the minimum of the specified size and the space
327 computed_size
= MIN2(available_space
, obj
->RequestedSize
[i
]);
330 /* Legal sizes must be multiples of four, so round down if necessary. */
331 obj
->Size
[i
] = computed_size
& ~0x3;
337 * Compute the maximum number of vertices that can be written to the currently
338 * enabled transform feedback buffers without overflowing any of them.
341 _mesa_compute_max_transform_feedback_vertices(struct gl_context
*ctx
,
342 const struct gl_transform_feedback_object
*obj
,
343 const struct gl_transform_feedback_info
*info
)
345 unsigned max_index
= 0xffffffff;
348 for (i
= 0; i
< ctx
->Const
.MaxTransformFeedbackBuffers
; i
++) {
349 if ((info
->ActiveBuffers
>> i
) & 1) {
350 unsigned stride
= info
->Buffers
[i
].Stride
;
351 unsigned max_for_this_buffer
;
353 /* Skip any inactive buffers, which have a stride of 0. */
357 max_for_this_buffer
= obj
->Size
[i
] / (4 * stride
);
358 max_index
= MIN2(max_index
, max_for_this_buffer
);
367 ** Begin API functions
372 * Figure out which stage of the pipeline is the source of transform feedback
373 * data given the current context state, and return its gl_program.
375 * If no active program can generate transform feedback data (i.e. no vertex
376 * shader is active), returns NULL.
378 static struct gl_program
*
379 get_xfb_source(struct gl_context
*ctx
)
382 for (i
= MESA_SHADER_GEOMETRY
; i
>= MESA_SHADER_VERTEX
; i
--) {
383 if (ctx
->_Shader
->CurrentProgram
[i
] != NULL
)
384 return ctx
->_Shader
->CurrentProgram
[i
];
390 static ALWAYS_INLINE
void
391 begin_transform_feedback(struct gl_context
*ctx
, GLenum mode
, bool no_error
)
393 struct gl_transform_feedback_object
*obj
;
394 struct gl_transform_feedback_info
*info
= NULL
;
395 struct gl_program
*source
;
397 unsigned vertices_per_prim
;
399 obj
= ctx
->TransformFeedback
.CurrentObject
;
401 /* Figure out what pipeline stage is the source of data for transform
404 source
= get_xfb_source(ctx
);
405 if (!no_error
&& source
== NULL
) {
406 _mesa_error(ctx
, GL_INVALID_OPERATION
,
407 "glBeginTransformFeedback(no program active)");
411 info
= source
->sh
.LinkedTransformFeedback
;
413 if (!no_error
&& info
->NumOutputs
== 0) {
414 _mesa_error(ctx
, GL_INVALID_OPERATION
,
415 "glBeginTransformFeedback(no varyings to record)");
421 vertices_per_prim
= 1;
424 vertices_per_prim
= 2;
427 vertices_per_prim
= 3;
431 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginTransformFeedback(mode)");
434 /* Stop compiler warnings */
435 unreachable("Error in API use when using KHR_no_error");
441 _mesa_error(ctx
, GL_INVALID_OPERATION
,
442 "glBeginTransformFeedback(already active)");
446 for (i
= 0; i
< ctx
->Const
.MaxTransformFeedbackBuffers
; i
++) {
447 if ((info
->ActiveBuffers
>> i
) & 1) {
448 if (obj
->BufferNames
[i
] == 0) {
449 _mesa_error(ctx
, GL_INVALID_OPERATION
,
450 "glBeginTransformFeedback(binding point %d does not "
451 "have a buffer object bound)", i
);
458 FLUSH_VERTICES(ctx
, 0);
459 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
461 obj
->Active
= GL_TRUE
;
462 ctx
->TransformFeedback
.Mode
= mode
;
464 compute_transform_feedback_buffer_sizes(obj
);
466 if (_mesa_is_gles3(ctx
)) {
467 /* In GLES3, we are required to track the usage of the transform
468 * feedback buffer and report INVALID_OPERATION if a draw call tries to
469 * exceed it. So compute the maximum number of vertices that we can
470 * write without overflowing any of the buffers currently being used for
473 unsigned max_vertices
474 = _mesa_compute_max_transform_feedback_vertices(ctx
, obj
, info
);
475 obj
->GlesRemainingPrims
= max_vertices
/ vertices_per_prim
;
478 if (obj
->program
!= source
) {
479 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedbackProg
;
480 _mesa_reference_program_(ctx
, &obj
->program
, source
);
481 obj
->program
= source
;
484 assert(ctx
->Driver
.BeginTransformFeedback
);
485 ctx
->Driver
.BeginTransformFeedback(ctx
, mode
, obj
);
490 _mesa_BeginTransformFeedback_no_error(GLenum mode
)
492 GET_CURRENT_CONTEXT(ctx
);
493 begin_transform_feedback(ctx
, mode
, true);
498 _mesa_BeginTransformFeedback(GLenum mode
)
500 GET_CURRENT_CONTEXT(ctx
);
501 begin_transform_feedback(ctx
, mode
, false);
506 end_transform_feedback(struct gl_context
*ctx
,
507 struct gl_transform_feedback_object
*obj
)
509 FLUSH_VERTICES(ctx
, 0);
510 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
512 assert(ctx
->Driver
.EndTransformFeedback
);
513 ctx
->Driver
.EndTransformFeedback(ctx
, obj
);
515 _mesa_reference_program_(ctx
, &obj
->program
, NULL
);
516 ctx
->TransformFeedback
.CurrentObject
->Active
= GL_FALSE
;
517 ctx
->TransformFeedback
.CurrentObject
->Paused
= GL_FALSE
;
518 ctx
->TransformFeedback
.CurrentObject
->EndedAnytime
= GL_TRUE
;
523 _mesa_EndTransformFeedback_no_error(void)
525 GET_CURRENT_CONTEXT(ctx
);
526 end_transform_feedback(ctx
, ctx
->TransformFeedback
.CurrentObject
);
531 _mesa_EndTransformFeedback(void)
533 struct gl_transform_feedback_object
*obj
;
534 GET_CURRENT_CONTEXT(ctx
);
536 obj
= ctx
->TransformFeedback
.CurrentObject
;
539 _mesa_error(ctx
, GL_INVALID_OPERATION
,
540 "glEndTransformFeedback(not active)");
544 end_transform_feedback(ctx
, obj
);
549 * Helper used by BindBufferRange() and BindBufferBase().
552 bind_buffer_range(struct gl_context
*ctx
,
553 struct gl_transform_feedback_object
*obj
,
555 struct gl_buffer_object
*bufObj
,
556 GLintptr offset
, GLsizeiptr size
,
559 /* Note: no need to FLUSH_VERTICES or flag NewTransformFeedback, because
560 * transform feedback buffers can't be changed while transform feedback is
565 /* The general binding point */
566 _mesa_reference_buffer_object(ctx
,
567 &ctx
->TransformFeedback
.CurrentBuffer
,
571 /* The per-attribute binding point */
572 _mesa_set_transform_feedback_binding(ctx
, obj
, index
, bufObj
, offset
, size
);
577 * Validate the buffer object to receive transform feedback results. Plus,
578 * validate the starting offset to place the results, and max size.
579 * Called from the glBindBufferRange() and glTransformFeedbackBufferRange
583 _mesa_validate_buffer_range_xfb(struct gl_context
*ctx
,
584 struct gl_transform_feedback_object
*obj
,
585 GLuint index
, struct gl_buffer_object
*bufObj
,
586 GLintptr offset
, GLsizeiptr size
, bool dsa
)
588 const char *gl_methd_name
;
590 gl_methd_name
= "glTransformFeedbackBufferRange";
592 gl_methd_name
= "glBindBufferRange";
596 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(transform feedback active)",
601 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
602 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
603 * generated if index is greater than or equal to the number of binding
604 * points for transform feedback, as described in section 6.7.1."
606 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d out of bounds)",
607 gl_methd_name
, index
);
612 /* OpenGL 4.5 core profile, 6.7, pdf page 103: multiple of 4 */
613 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d must be a multiple of "
614 "four)", gl_methd_name
, (int) size
);
619 /* OpenGL 4.5 core profile, 6.7, pdf page 103: multiple of 4 */
620 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset=%d must be a multiple "
621 "of four)", gl_methd_name
, (int) offset
);
626 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
627 * generated by BindBufferRange if offset is negative."
629 * OpenGL 4.5 core profile, 13.2, pdf page 445: "An INVALID_VALUE error
630 * is generated by TransformFeedbackBufferRange if offset is negative."
632 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset=%d must be >= 0)",
638 if (size
<= 0 && (dsa
|| bufObj
)) {
639 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
640 * generated by BindBufferRange if buffer is non-zero and size is less
641 * than or equal to zero."
643 * OpenGL 4.5 core profile, 13.2, pdf page 445: "An INVALID_VALUE error
644 * is generated by TransformFeedbackBufferRange if size is less than or
647 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d must be > 0)",
648 gl_methd_name
, (int) size
);
657 * Specify a buffer object to receive transform feedback results.
658 * As above, but start at offset = 0.
659 * Called from the glBindBufferBase() and glTransformFeedbackBufferBase()
663 _mesa_bind_buffer_base_transform_feedback(struct gl_context
*ctx
,
664 struct gl_transform_feedback_object
*obj
,
666 struct gl_buffer_object
*bufObj
,
670 _mesa_error(ctx
, GL_INVALID_OPERATION
,
671 "%s(transform feedback active)",
672 dsa
? "glTransformFeedbackBufferBase" : "glBindBufferBase");
676 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
677 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d out of bounds)",
678 dsa
? "glTransformFeedbackBufferBase" : "glBindBufferBase",
683 bind_buffer_range(ctx
, obj
, index
, bufObj
, 0, 0, dsa
);
687 * Wrapper around lookup_transform_feedback_object that throws
688 * GL_INVALID_OPERATION if id is not in the hash table. After calling
689 * _mesa_error, it returns NULL.
691 static struct gl_transform_feedback_object
*
692 lookup_transform_feedback_object_err(struct gl_context
*ctx
,
693 GLuint xfb
, const char* func
)
695 struct gl_transform_feedback_object
*obj
;
697 obj
= _mesa_lookup_transform_feedback_object(ctx
, xfb
);
699 _mesa_error(ctx
, GL_INVALID_OPERATION
,
700 "%s(xfb=%u: non-generated object name)", func
, xfb
);
707 * Wrapper around _mesa_lookup_bufferobj that throws GL_INVALID_VALUE if id
708 * is not in the hash table. Specialised version for the
709 * transform-feedback-related functions. After calling _mesa_error, it
712 static struct gl_buffer_object
*
713 lookup_transform_feedback_bufferobj_err(struct gl_context
*ctx
,
714 GLuint buffer
, const char* func
,
717 struct gl_buffer_object
*bufObj
= NULL
;
721 /* OpenGL 4.5 core profile, 13.2, pdf page 444: buffer must be zero or the
722 * name of an existing buffer object.
725 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
727 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid buffer=%u)", func
,
737 _mesa_TransformFeedbackBufferBase(GLuint xfb
, GLuint index
, GLuint buffer
)
739 GET_CURRENT_CONTEXT(ctx
);
740 struct gl_transform_feedback_object
*obj
;
741 struct gl_buffer_object
*bufObj
;
743 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
744 "glTransformFeedbackBufferBase");
750 bufObj
= lookup_transform_feedback_bufferobj_err(ctx
, buffer
,
751 "glTransformFeedbackBufferBase",
757 _mesa_bind_buffer_base_transform_feedback(ctx
, obj
, index
, bufObj
, true);
761 _mesa_TransformFeedbackBufferRange(GLuint xfb
, GLuint index
, GLuint buffer
,
762 GLintptr offset
, GLsizeiptr size
)
764 GET_CURRENT_CONTEXT(ctx
);
765 struct gl_transform_feedback_object
*obj
;
766 struct gl_buffer_object
*bufObj
;
768 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
769 "glTransformFeedbackBufferRange");
775 bufObj
= lookup_transform_feedback_bufferobj_err(ctx
, buffer
,
776 "glTransformFeedbackBufferRange",
782 if (!_mesa_validate_buffer_range_xfb(ctx
, obj
, index
, bufObj
, offset
,
786 /* The per-attribute binding point */
787 _mesa_set_transform_feedback_binding(ctx
, obj
, index
, bufObj
, offset
,
792 * Specify a buffer object to receive transform feedback results, plus the
793 * offset in the buffer to start placing results.
794 * This function is part of GL_EXT_transform_feedback, but not GL3.
796 static ALWAYS_INLINE
void
797 bind_buffer_offset(struct gl_context
*ctx
,
798 struct gl_transform_feedback_object
*obj
, GLuint index
,
799 GLuint buffer
, GLintptr offset
, bool no_error
)
801 struct gl_buffer_object
*bufObj
;
806 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
807 if (!no_error
&& !bufObj
) {
808 _mesa_error(ctx
, GL_INVALID_OPERATION
,
809 "glBindBufferOffsetEXT(invalid buffer=%u)", buffer
);
814 _mesa_bind_buffer_range_xfb(ctx
, obj
, index
, bufObj
, offset
, 0);
819 _mesa_BindBufferOffsetEXT_no_error(GLenum target
, GLuint index
, GLuint buffer
,
822 GET_CURRENT_CONTEXT(ctx
);
823 bind_buffer_offset(ctx
, ctx
->TransformFeedback
.CurrentObject
, index
, buffer
,
829 _mesa_BindBufferOffsetEXT(GLenum target
, GLuint index
, GLuint buffer
,
832 struct gl_transform_feedback_object
*obj
;
833 GET_CURRENT_CONTEXT(ctx
);
835 if (target
!= GL_TRANSFORM_FEEDBACK_BUFFER
) {
836 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferOffsetEXT(target)");
840 obj
= ctx
->TransformFeedback
.CurrentObject
;
843 _mesa_error(ctx
, GL_INVALID_OPERATION
,
844 "glBindBufferOffsetEXT(transform feedback active)");
848 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
849 _mesa_error(ctx
, GL_INVALID_VALUE
,
850 "glBindBufferOffsetEXT(index=%d)", index
);
855 /* must be multiple of four */
856 _mesa_error(ctx
, GL_INVALID_VALUE
,
857 "glBindBufferOffsetEXT(offset=%d)", (int) offset
);
861 bind_buffer_offset(ctx
, obj
, index
, buffer
, offset
, false);
866 * This function specifies the transform feedback outputs to be written
867 * to the feedback buffer(s), and in what order.
869 static ALWAYS_INLINE
void
870 transform_feedback_varyings(struct gl_context
*ctx
,
871 struct gl_shader_program
*shProg
, GLsizei count
,
872 const GLchar
*const *varyings
, GLenum bufferMode
)
876 /* free existing varyings, if any */
877 for (i
= 0; i
< (GLint
) shProg
->TransformFeedback
.NumVarying
; i
++) {
878 free(shProg
->TransformFeedback
.VaryingNames
[i
]);
880 free(shProg
->TransformFeedback
.VaryingNames
);
882 /* allocate new memory for varying names */
883 shProg
->TransformFeedback
.VaryingNames
=
884 malloc(count
* sizeof(GLchar
*));
886 if (!shProg
->TransformFeedback
.VaryingNames
) {
887 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTransformFeedbackVaryings()");
891 /* Save the new names and the count */
892 for (i
= 0; i
< count
; i
++) {
893 shProg
->TransformFeedback
.VaryingNames
[i
] = strdup(varyings
[i
]);
895 shProg
->TransformFeedback
.NumVarying
= count
;
897 shProg
->TransformFeedback
.BufferMode
= bufferMode
;
899 /* No need to invoke FLUSH_VERTICES or flag NewTransformFeedback since
900 * the varyings won't be used until shader link time.
906 _mesa_TransformFeedbackVaryings_no_error(GLuint program
, GLsizei count
,
907 const GLchar
*const *varyings
,
910 GET_CURRENT_CONTEXT(ctx
);
912 struct gl_shader_program
*shProg
= _mesa_lookup_shader_program(ctx
, program
);
913 transform_feedback_varyings(ctx
, shProg
, count
, varyings
, bufferMode
);
917 _mesa_TransformFeedbackVaryings(GLuint program
, GLsizei count
,
918 const GLchar
* const *varyings
,
921 struct gl_shader_program
*shProg
;
923 GET_CURRENT_CONTEXT(ctx
);
925 /* From the ARB_transform_feedback2 specification:
926 * "The error INVALID_OPERATION is generated by TransformFeedbackVaryings
927 * if the current transform feedback object is active, even if paused."
929 if (ctx
->TransformFeedback
.CurrentObject
->Active
) {
930 _mesa_error(ctx
, GL_INVALID_OPERATION
,
931 "glTransformFeedbackVaryings(current object is active)");
935 switch (bufferMode
) {
936 case GL_INTERLEAVED_ATTRIBS
:
938 case GL_SEPARATE_ATTRIBS
:
941 _mesa_error(ctx
, GL_INVALID_ENUM
,
942 "glTransformFeedbackVaryings(bufferMode)");
947 (bufferMode
== GL_SEPARATE_ATTRIBS
&&
948 (GLuint
) count
> ctx
->Const
.MaxTransformFeedbackBuffers
)) {
949 _mesa_error(ctx
, GL_INVALID_VALUE
,
950 "glTransformFeedbackVaryings(count=%d)", count
);
954 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
955 "glTransformFeedbackVaryings");
959 if (ctx
->Extensions
.ARB_transform_feedback3
) {
960 if (bufferMode
== GL_INTERLEAVED_ATTRIBS
) {
961 unsigned buffers
= 1;
963 for (i
= 0; i
< count
; i
++) {
964 if (strcmp(varyings
[i
], "gl_NextBuffer") == 0)
968 if (buffers
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
969 _mesa_error(ctx
, GL_INVALID_OPERATION
,
970 "glTransformFeedbackVaryings(too many gl_NextBuffer "
975 for (i
= 0; i
< count
; i
++) {
976 if (strcmp(varyings
[i
], "gl_NextBuffer") == 0 ||
977 strcmp(varyings
[i
], "gl_SkipComponents1") == 0 ||
978 strcmp(varyings
[i
], "gl_SkipComponents2") == 0 ||
979 strcmp(varyings
[i
], "gl_SkipComponents3") == 0 ||
980 strcmp(varyings
[i
], "gl_SkipComponents4") == 0) {
981 _mesa_error(ctx
, GL_INVALID_OPERATION
,
982 "glTransformFeedbackVaryings(SEPARATE_ATTRIBS,"
991 transform_feedback_varyings(ctx
, shProg
, count
, varyings
, bufferMode
);
996 * Get info about the transform feedback outputs which are to be written
997 * to the feedback buffer(s).
1000 _mesa_GetTransformFeedbackVarying(GLuint program
, GLuint index
,
1001 GLsizei bufSize
, GLsizei
*length
,
1002 GLsizei
*size
, GLenum
*type
, GLchar
*name
)
1004 const struct gl_shader_program
*shProg
;
1005 struct gl_program_resource
*res
;
1006 GET_CURRENT_CONTEXT(ctx
);
1008 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
1009 "glGetTransformFeedbackVarying");
1013 res
= _mesa_program_resource_find_index((struct gl_shader_program
*) shProg
,
1014 GL_TRANSFORM_FEEDBACK_VARYING
,
1017 _mesa_error(ctx
, GL_INVALID_VALUE
,
1018 "glGetTransformFeedbackVarying(index=%u)", index
);
1022 /* return the varying's name and length */
1023 _mesa_copy_string(name
, bufSize
, length
, _mesa_program_resource_name(res
));
1025 /* return the datatype and value's size (in datatype units) */
1027 _mesa_program_resource_prop((struct gl_shader_program
*) shProg
,
1028 res
, index
, GL_TYPE
, (GLint
*) type
,
1029 "glGetTransformFeedbackVarying");
1031 _mesa_program_resource_prop((struct gl_shader_program
*) shProg
,
1032 res
, index
, GL_ARRAY_SIZE
, (GLint
*) size
,
1033 "glGetTransformFeedbackVarying");
1038 struct gl_transform_feedback_object
*
1039 _mesa_lookup_transform_feedback_object(struct gl_context
*ctx
, GLuint name
)
1041 /* OpenGL 4.5 core profile, 13.2 pdf page 444: "xfb must be zero, indicating
1042 * the default transform feedback object, or the name of an existing
1043 * transform feedback object."
1046 return ctx
->TransformFeedback
.DefaultObject
;
1049 return (struct gl_transform_feedback_object
*)
1050 _mesa_HashLookupLocked(ctx
->TransformFeedback
.Objects
, name
);
1054 create_transform_feedbacks(struct gl_context
*ctx
, GLsizei n
, GLuint
*ids
,
1061 func
= "glCreateTransformFeedbacks";
1063 func
= "glGenTransformFeedbacks";
1066 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", func
);
1073 /* we don't need contiguous IDs, but this might be faster */
1074 first
= _mesa_HashFindFreeKeyBlock(ctx
->TransformFeedback
.Objects
, n
);
1077 for (i
= 0; i
< n
; i
++) {
1078 struct gl_transform_feedback_object
*obj
1079 = ctx
->Driver
.NewTransformFeedback(ctx
, first
+ i
);
1081 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1085 _mesa_HashInsertLocked(ctx
->TransformFeedback
.Objects
, first
+ i
,
1088 /* this is normally done at bind time in the non-dsa case */
1089 obj
->EverBound
= GL_TRUE
;
1094 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1099 * Create new transform feedback objects. Transform feedback objects
1100 * encapsulate the state related to transform feedback to allow quickly
1101 * switching state (and drawing the results, below).
1102 * Part of GL_ARB_transform_feedback2.
1105 _mesa_GenTransformFeedbacks(GLsizei n
, GLuint
*names
)
1107 GET_CURRENT_CONTEXT(ctx
);
1109 /* GenTransformFeedbacks should just reserve the object names that a
1110 * subsequent call to BindTransformFeedback should actively create. For
1111 * the sake of simplicity, we reserve the names and create the objects
1115 create_transform_feedbacks(ctx
, n
, names
, false);
1119 * Create new transform feedback objects. Transform feedback objects
1120 * encapsulate the state related to transform feedback to allow quickly
1121 * switching state (and drawing the results, below).
1122 * Part of GL_ARB_direct_state_access.
1125 _mesa_CreateTransformFeedbacks(GLsizei n
, GLuint
*names
)
1127 GET_CURRENT_CONTEXT(ctx
);
1129 create_transform_feedbacks(ctx
, n
, names
, true);
1134 * Is the given ID a transform feedback object?
1135 * Part of GL_ARB_transform_feedback2.
1137 GLboolean GLAPIENTRY
1138 _mesa_IsTransformFeedback(GLuint name
)
1140 struct gl_transform_feedback_object
*obj
;
1141 GET_CURRENT_CONTEXT(ctx
);
1143 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1148 obj
= _mesa_lookup_transform_feedback_object(ctx
, name
);
1152 return obj
->EverBound
;
1157 * Bind the given transform feedback object.
1158 * Part of GL_ARB_transform_feedback2.
1160 static ALWAYS_INLINE
void
1161 bind_transform_feedback(struct gl_context
*ctx
, GLuint name
, bool no_error
)
1163 struct gl_transform_feedback_object
*obj
;
1165 obj
= _mesa_lookup_transform_feedback_object(ctx
, name
);
1166 if (!no_error
&& !obj
) {
1167 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1168 "glBindTransformFeedback(name=%u)", name
);
1172 reference_transform_feedback_object(&ctx
->TransformFeedback
.CurrentObject
,
1178 _mesa_BindTransformFeedback_no_error(GLenum target
, GLuint name
)
1180 GET_CURRENT_CONTEXT(ctx
);
1181 bind_transform_feedback(ctx
, name
, true);
1186 _mesa_BindTransformFeedback(GLenum target
, GLuint name
)
1188 GET_CURRENT_CONTEXT(ctx
);
1190 if (target
!= GL_TRANSFORM_FEEDBACK
) {
1191 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindTransformFeedback(target)");
1195 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1196 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1197 "glBindTransformFeedback(transform is active, or not paused)");
1201 bind_transform_feedback(ctx
, name
, false);
1206 * Delete the given transform feedback objects.
1207 * Part of GL_ARB_transform_feedback2.
1210 _mesa_DeleteTransformFeedbacks(GLsizei n
, const GLuint
*names
)
1213 GET_CURRENT_CONTEXT(ctx
);
1216 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteTransformFeedbacks(n < 0)");
1223 for (i
= 0; i
< n
; i
++) {
1225 struct gl_transform_feedback_object
*obj
1226 = _mesa_lookup_transform_feedback_object(ctx
, names
[i
]);
1229 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1230 "glDeleteTransformFeedbacks(object %u is active)",
1234 _mesa_HashRemoveLocked(ctx
->TransformFeedback
.Objects
, names
[i
]);
1235 /* unref, but object may not be deleted until later */
1236 if (obj
== ctx
->TransformFeedback
.CurrentObject
) {
1237 reference_transform_feedback_object(
1238 &ctx
->TransformFeedback
.CurrentObject
,
1239 ctx
->TransformFeedback
.DefaultObject
);
1241 reference_transform_feedback_object(&obj
, NULL
);
1249 * Pause transform feedback.
1250 * Part of GL_ARB_transform_feedback2.
1253 pause_transform_feedback(struct gl_context
*ctx
,
1254 struct gl_transform_feedback_object
*obj
)
1256 FLUSH_VERTICES(ctx
, 0);
1257 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
1259 assert(ctx
->Driver
.PauseTransformFeedback
);
1260 ctx
->Driver
.PauseTransformFeedback(ctx
, obj
);
1262 obj
->Paused
= GL_TRUE
;
1267 _mesa_PauseTransformFeedback_no_error(void)
1269 GET_CURRENT_CONTEXT(ctx
);
1270 pause_transform_feedback(ctx
, ctx
->TransformFeedback
.CurrentObject
);
1275 _mesa_PauseTransformFeedback(void)
1277 struct gl_transform_feedback_object
*obj
;
1278 GET_CURRENT_CONTEXT(ctx
);
1280 obj
= ctx
->TransformFeedback
.CurrentObject
;
1282 if (!_mesa_is_xfb_active_and_unpaused(ctx
)) {
1283 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1284 "glPauseTransformFeedback(feedback not active or already paused)");
1288 pause_transform_feedback(ctx
, obj
);
1293 * Resume transform feedback.
1294 * Part of GL_ARB_transform_feedback2.
1297 resume_transform_feedback(struct gl_context
*ctx
,
1298 struct gl_transform_feedback_object
*obj
)
1300 FLUSH_VERTICES(ctx
, 0);
1301 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
1303 obj
->Paused
= GL_FALSE
;
1305 assert(ctx
->Driver
.ResumeTransformFeedback
);
1306 ctx
->Driver
.ResumeTransformFeedback(ctx
, obj
);
1311 _mesa_ResumeTransformFeedback_no_error(void)
1313 GET_CURRENT_CONTEXT(ctx
);
1314 resume_transform_feedback(ctx
, ctx
->TransformFeedback
.CurrentObject
);
1319 _mesa_ResumeTransformFeedback(void)
1321 struct gl_transform_feedback_object
*obj
;
1322 GET_CURRENT_CONTEXT(ctx
);
1324 obj
= ctx
->TransformFeedback
.CurrentObject
;
1326 if (!obj
->Active
|| !obj
->Paused
) {
1327 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1328 "glResumeTransformFeedback(feedback not active or not paused)");
1332 /* From the ARB_transform_feedback2 specification:
1333 * "The error INVALID_OPERATION is generated by ResumeTransformFeedback if
1334 * the program object being used by the current transform feedback object
1337 if (obj
->program
!= get_xfb_source(ctx
)) {
1338 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1339 "glResumeTransformFeedback(wrong program bound)");
1343 resume_transform_feedback(ctx
, obj
);
1346 extern void GLAPIENTRY
1347 _mesa_GetTransformFeedbackiv(GLuint xfb
, GLenum pname
, GLint
*param
)
1349 struct gl_transform_feedback_object
*obj
;
1350 GET_CURRENT_CONTEXT(ctx
);
1352 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
1353 "glGetTransformFeedbackiv");
1359 case GL_TRANSFORM_FEEDBACK_PAUSED
:
1360 *param
= obj
->Paused
;
1362 case GL_TRANSFORM_FEEDBACK_ACTIVE
:
1363 *param
= obj
->Active
;
1366 _mesa_error(ctx
, GL_INVALID_ENUM
,
1367 "glGetTransformFeedbackiv(pname=%i)", pname
);
1371 extern void GLAPIENTRY
1372 _mesa_GetTransformFeedbacki_v(GLuint xfb
, GLenum pname
, GLuint index
,
1375 struct gl_transform_feedback_object
*obj
;
1376 GET_CURRENT_CONTEXT(ctx
);
1378 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
1379 "glGetTransformFeedbacki_v");
1384 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
1385 _mesa_error(ctx
, GL_INVALID_VALUE
,
1386 "glGetTransformFeedbacki_v(index=%i)", index
);
1391 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING
:
1392 *param
= obj
->BufferNames
[index
];
1395 _mesa_error(ctx
, GL_INVALID_ENUM
,
1396 "glGetTransformFeedbacki_v(pname=%i)", pname
);
1400 extern void GLAPIENTRY
1401 _mesa_GetTransformFeedbacki64_v(GLuint xfb
, GLenum pname
, GLuint index
,
1404 struct gl_transform_feedback_object
*obj
;
1405 GET_CURRENT_CONTEXT(ctx
);
1407 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
1408 "glGetTransformFeedbacki64_v");
1413 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
1414 _mesa_error(ctx
, GL_INVALID_VALUE
,
1415 "glGetTransformFeedbacki64_v(index=%i)", index
);
1420 * This follows the same general rules used for BindBufferBase:
1422 * "To query the starting offset or size of the range of a buffer
1423 * object binding in an indexed array, call GetInteger64i_v with
1424 * target set to respectively the starting offset or binding size
1425 * name from table 6.5 for that array. Index must be in the range
1426 * zero to the number of bind points supported minus one. If the
1427 * starting offset or size was not specified when the buffer object
1428 * was bound (e.g. if it was bound with BindBufferBase), or if no
1429 * buffer object is bound to the target array at index, zero is
1432 if (obj
->RequestedSize
[index
] == 0 &&
1433 (pname
== GL_TRANSFORM_FEEDBACK_BUFFER_START
||
1434 pname
== GL_TRANSFORM_FEEDBACK_BUFFER_SIZE
)) {
1439 compute_transform_feedback_buffer_sizes(obj
);
1441 case GL_TRANSFORM_FEEDBACK_BUFFER_START
:
1442 assert(obj
->RequestedSize
[index
] > 0);
1443 *param
= obj
->Offset
[index
];
1445 case GL_TRANSFORM_FEEDBACK_BUFFER_SIZE
:
1446 assert(obj
->RequestedSize
[index
] > 0);
1447 *param
= obj
->Size
[index
];
1450 _mesa_error(ctx
, GL_INVALID_ENUM
,
1451 "glGetTransformFeedbacki64_v(pname=%i)", pname
);