2 * Mesa 3-D graphics library
4 * Copyright (C) 2010 VMware, Inc. All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
27 * Transform feedback support.
39 #include "transformfeedback.h"
40 #include "shaderapi.h"
41 #include "shaderobj.h"
42 #include "main/dispatch.h"
44 #include "program/prog_parameter.h"
46 struct using_program_tuple
48 struct gl_shader_program
*shProg
;
53 active_xfb_object_references_program(GLuint key
, void *data
, void *user_data
)
55 struct using_program_tuple
*callback_data
= user_data
;
56 struct gl_transform_feedback_object
*obj
= data
;
57 if (obj
->Active
&& obj
->shader_program
== callback_data
->shProg
)
58 callback_data
->found
= true;
62 * Return true if any active transform feedback object is using a program.
65 _mesa_transform_feedback_is_using_program(struct gl_context
*ctx
,
66 struct gl_shader_program
*shProg
)
68 struct using_program_tuple callback_data
;
69 callback_data
.shProg
= shProg
;
70 callback_data
.found
= false;
72 _mesa_HashWalk(ctx
->TransformFeedback
.Objects
,
73 active_xfb_object_references_program
, &callback_data
);
75 /* Also check DefaultObject, as it's not in the Objects hash table. */
76 active_xfb_object_references_program(0, ctx
->TransformFeedback
.DefaultObject
,
79 return callback_data
.found
;
83 * Do reference counting of transform feedback buffers.
86 reference_transform_feedback_object(struct gl_transform_feedback_object
**ptr
,
87 struct gl_transform_feedback_object
*obj
)
93 /* Unreference the old object */
94 struct gl_transform_feedback_object
*oldObj
= *ptr
;
96 assert(oldObj
->RefCount
> 0);
99 if (oldObj
->RefCount
== 0) {
100 GET_CURRENT_CONTEXT(ctx
);
102 ctx
->Driver
.DeleteTransformFeedback(ctx
, oldObj
);
110 /* reference new object */
111 if (obj
->RefCount
== 0) {
112 _mesa_problem(NULL
, "referencing deleted transform feedback object");
117 obj
->EverBound
= GL_TRUE
;
125 * Check that all the buffer objects currently bound for transform
126 * feedback actually exist. Raise a GL_INVALID_OPERATION error if
127 * any buffers are missing.
128 * \return GL_TRUE for success, GL_FALSE if error
131 _mesa_validate_transform_feedback_buffers(struct gl_context
*ctx
)
140 * Per-context init for transform feedback.
143 _mesa_init_transform_feedback(struct gl_context
*ctx
)
145 /* core mesa expects this, even a dummy one, to be available */
146 assert(ctx
->Driver
.NewTransformFeedback
);
148 ctx
->TransformFeedback
.DefaultObject
=
149 ctx
->Driver
.NewTransformFeedback(ctx
, 0);
151 assert(ctx
->TransformFeedback
.DefaultObject
->RefCount
== 1);
153 reference_transform_feedback_object(&ctx
->TransformFeedback
.CurrentObject
,
154 ctx
->TransformFeedback
.DefaultObject
);
156 assert(ctx
->TransformFeedback
.DefaultObject
->RefCount
== 2);
158 ctx
->TransformFeedback
.Objects
= _mesa_NewHashTable();
160 _mesa_reference_buffer_object(ctx
,
161 &ctx
->TransformFeedback
.CurrentBuffer
,
162 ctx
->Shared
->NullBufferObj
);
168 * Callback for _mesa_HashDeleteAll().
171 delete_cb(GLuint key
, void *data
, void *userData
)
173 struct gl_context
*ctx
= (struct gl_context
*) userData
;
174 struct gl_transform_feedback_object
*obj
=
175 (struct gl_transform_feedback_object
*) data
;
177 ctx
->Driver
.DeleteTransformFeedback(ctx
, obj
);
182 * Per-context free/clean-up for transform feedback.
185 _mesa_free_transform_feedback(struct gl_context
*ctx
)
187 /* core mesa expects this, even a dummy one, to be available */
188 assert(ctx
->Driver
.NewTransformFeedback
);
190 _mesa_reference_buffer_object(ctx
,
191 &ctx
->TransformFeedback
.CurrentBuffer
,
194 /* Delete all feedback objects */
195 _mesa_HashDeleteAll(ctx
->TransformFeedback
.Objects
, delete_cb
, ctx
);
196 _mesa_DeleteHashTable(ctx
->TransformFeedback
.Objects
);
198 /* Delete the default feedback object */
199 assert(ctx
->Driver
.DeleteTransformFeedback
);
200 ctx
->Driver
.DeleteTransformFeedback(ctx
,
201 ctx
->TransformFeedback
.DefaultObject
);
203 ctx
->TransformFeedback
.CurrentObject
= NULL
;
207 /** Initialize the fields of a gl_transform_feedback_object. */
209 _mesa_init_transform_feedback_object(struct gl_transform_feedback_object
*obj
,
217 obj
->EverBound
= GL_FALSE
;
221 /** Default fallback for ctx->Driver.NewTransformFeedback() */
222 static struct gl_transform_feedback_object
*
223 new_transform_feedback(struct gl_context
*ctx
, GLuint name
)
225 struct gl_transform_feedback_object
*obj
;
226 obj
= CALLOC_STRUCT(gl_transform_feedback_object
);
227 _mesa_init_transform_feedback_object(obj
, name
);
231 /** Default fallback for ctx->Driver.DeleteTransformFeedback() */
233 delete_transform_feedback(struct gl_context
*ctx
,
234 struct gl_transform_feedback_object
*obj
)
238 for (i
= 0; i
< ARRAY_SIZE(obj
->Buffers
); i
++) {
239 _mesa_reference_buffer_object(ctx
, &obj
->Buffers
[i
], NULL
);
247 /** Default fallback for ctx->Driver.BeginTransformFeedback() */
249 begin_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
250 struct gl_transform_feedback_object
*obj
)
255 /** Default fallback for ctx->Driver.EndTransformFeedback() */
257 end_transform_feedback(struct gl_context
*ctx
,
258 struct gl_transform_feedback_object
*obj
)
263 /** Default fallback for ctx->Driver.PauseTransformFeedback() */
265 pause_transform_feedback(struct gl_context
*ctx
,
266 struct gl_transform_feedback_object
*obj
)
271 /** Default fallback for ctx->Driver.ResumeTransformFeedback() */
273 resume_transform_feedback(struct gl_context
*ctx
,
274 struct gl_transform_feedback_object
*obj
)
281 * Plug in default device driver functions for transform feedback.
282 * Most drivers will override some/all of these.
285 _mesa_init_transform_feedback_functions(struct dd_function_table
*driver
)
287 driver
->NewTransformFeedback
= new_transform_feedback
;
288 driver
->DeleteTransformFeedback
= delete_transform_feedback
;
289 driver
->BeginTransformFeedback
= begin_transform_feedback
;
290 driver
->EndTransformFeedback
= end_transform_feedback
;
291 driver
->PauseTransformFeedback
= pause_transform_feedback
;
292 driver
->ResumeTransformFeedback
= resume_transform_feedback
;
297 * Fill in the correct Size value for each buffer in \c obj.
299 * From the GL 4.3 spec, section 6.1.1 ("Binding Buffer Objects to Indexed
302 * BindBufferBase binds the entire buffer, even when the size of the buffer
303 * is changed after the binding is established. It is equivalent to calling
304 * BindBufferRange with offset zero, while size is determined by the size of
305 * the bound buffer at the time the binding is used.
307 * Regardless of the size specified with BindBufferRange, or indirectly with
308 * BindBufferBase, the GL will never read or write beyond the end of a bound
309 * buffer. In some cases this constraint may result in visibly different
310 * behavior when a buffer overflow would otherwise result, such as described
311 * for transform feedback operations in section 13.2.2.
314 compute_transform_feedback_buffer_sizes(
315 struct gl_transform_feedback_object
*obj
)
318 for (i
= 0; i
< MAX_FEEDBACK_BUFFERS
; ++i
) {
319 GLintptr offset
= obj
->Offset
[i
];
320 GLsizeiptr buffer_size
321 = obj
->Buffers
[i
] == NULL
? 0 : obj
->Buffers
[i
]->Size
;
322 GLsizeiptr available_space
323 = buffer_size
<= offset
? 0 : buffer_size
- offset
;
324 GLsizeiptr computed_size
;
325 if (obj
->RequestedSize
[i
] == 0) {
326 /* No size was specified at the time the buffer was bound, so allow
327 * writing to all available space in the buffer.
329 computed_size
= available_space
;
331 /* A size was specified at the time the buffer was bound, however
332 * it's possible that the buffer has shrunk since then. So only
333 * allow writing to the minimum of the specified size and the space
336 computed_size
= MIN2(available_space
, obj
->RequestedSize
[i
]);
339 /* Legal sizes must be multiples of four, so round down if necessary. */
340 obj
->Size
[i
] = computed_size
& ~0x3;
346 * Compute the maximum number of vertices that can be written to the currently
347 * enabled transform feedback buffers without overflowing any of them.
350 _mesa_compute_max_transform_feedback_vertices(
351 const struct gl_transform_feedback_object
*obj
,
352 const struct gl_transform_feedback_info
*info
)
354 unsigned max_index
= 0xffffffff;
357 for (i
= 0; i
< info
->NumBuffers
; ++i
) {
358 unsigned stride
= info
->BufferStride
[i
];
359 unsigned max_for_this_buffer
;
361 /* Skip any inactive buffers, which have a stride of 0. */
365 max_for_this_buffer
= obj
->Size
[i
] / (4 * stride
);
366 max_index
= MIN2(max_index
, max_for_this_buffer
);
374 ** Begin API functions
379 * Figure out which stage of the pipeline is the source of transform feedback
380 * data given the current context state, and return its gl_shader_program.
382 * If no active program can generate transform feedback data (i.e. no vertex
383 * shader is active), returns NULL.
385 static struct gl_shader_program
*
386 get_xfb_source(struct gl_context
*ctx
)
389 for (i
= MESA_SHADER_GEOMETRY
; i
>= MESA_SHADER_VERTEX
; i
--) {
390 if (ctx
->_Shader
->CurrentProgram
[i
] != NULL
)
391 return ctx
->_Shader
->CurrentProgram
[i
];
398 _mesa_BeginTransformFeedback(GLenum mode
)
400 struct gl_transform_feedback_object
*obj
;
401 struct gl_transform_feedback_info
*info
= NULL
;
402 struct gl_shader_program
*source
;
404 unsigned vertices_per_prim
;
405 GET_CURRENT_CONTEXT(ctx
);
407 obj
= ctx
->TransformFeedback
.CurrentObject
;
409 /* Figure out what pipeline stage is the source of data for transform
412 source
= get_xfb_source(ctx
);
413 if (source
== NULL
) {
414 _mesa_error(ctx
, GL_INVALID_OPERATION
,
415 "glBeginTransformFeedback(no program active)");
419 info
= &source
->LinkedTransformFeedback
;
421 if (info
->NumOutputs
== 0) {
422 _mesa_error(ctx
, GL_INVALID_OPERATION
,
423 "glBeginTransformFeedback(no varyings to record)");
429 vertices_per_prim
= 1;
432 vertices_per_prim
= 2;
435 vertices_per_prim
= 3;
438 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBeginTransformFeedback(mode)");
443 _mesa_error(ctx
, GL_INVALID_OPERATION
,
444 "glBeginTransformFeedback(already active)");
448 for (i
= 0; i
< info
->NumBuffers
; ++i
) {
449 if (obj
->BufferNames
[i
] == 0) {
450 _mesa_error(ctx
, GL_INVALID_OPERATION
,
451 "glBeginTransformFeedback(binding point %d does not have "
452 "a buffer object bound)", i
);
457 FLUSH_VERTICES(ctx
, 0);
458 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
460 obj
->Active
= GL_TRUE
;
461 ctx
->TransformFeedback
.Mode
= mode
;
463 compute_transform_feedback_buffer_sizes(obj
);
465 if (_mesa_is_gles3(ctx
)) {
466 /* In GLES3, we are required to track the usage of the transform
467 * feedback buffer and report INVALID_OPERATION if a draw call tries to
468 * exceed it. So compute the maximum number of vertices that we can
469 * write without overflowing any of the buffers currently being used for
472 unsigned max_vertices
473 = _mesa_compute_max_transform_feedback_vertices(obj
, info
);
474 obj
->GlesRemainingPrims
= max_vertices
/ vertices_per_prim
;
477 if (obj
->shader_program
!= source
) {
478 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedbackProg
;
479 obj
->shader_program
= source
;
482 assert(ctx
->Driver
.BeginTransformFeedback
);
483 ctx
->Driver
.BeginTransformFeedback(ctx
, mode
, obj
);
488 _mesa_EndTransformFeedback(void)
490 struct gl_transform_feedback_object
*obj
;
491 GET_CURRENT_CONTEXT(ctx
);
493 obj
= ctx
->TransformFeedback
.CurrentObject
;
496 _mesa_error(ctx
, GL_INVALID_OPERATION
,
497 "glEndTransformFeedback(not active)");
501 FLUSH_VERTICES(ctx
, 0);
502 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
504 ctx
->TransformFeedback
.CurrentObject
->Active
= GL_FALSE
;
505 ctx
->TransformFeedback
.CurrentObject
->Paused
= GL_FALSE
;
506 ctx
->TransformFeedback
.CurrentObject
->EndedAnytime
= GL_TRUE
;
508 assert(ctx
->Driver
.EndTransformFeedback
);
509 ctx
->Driver
.EndTransformFeedback(ctx
, obj
);
514 * Helper used by BindBufferRange() and BindBufferBase().
517 bind_buffer_range(struct gl_context
*ctx
,
518 struct gl_transform_feedback_object
*obj
,
520 struct gl_buffer_object
*bufObj
,
521 GLintptr offset
, GLsizeiptr size
,
524 /* Note: no need to FLUSH_VERTICES or flag NewTransformFeedback, because
525 * transform feedback buffers can't be changed while transform feedback is
530 /* The general binding point */
531 _mesa_reference_buffer_object(ctx
,
532 &ctx
->TransformFeedback
.CurrentBuffer
,
536 /* The per-attribute binding point */
537 _mesa_set_transform_feedback_binding(ctx
, obj
, index
, bufObj
, offset
, size
);
542 * Specify a buffer object to receive transform feedback results. Plus,
543 * specify the starting offset to place the results, and max size.
544 * Called from the glBindBufferRange() and glTransformFeedbackBufferRange
548 _mesa_bind_buffer_range_transform_feedback(struct gl_context
*ctx
,
549 struct gl_transform_feedback_object
*obj
,
551 struct gl_buffer_object
*bufObj
,
556 const char *gl_methd_name
;
558 gl_methd_name
= "glTransformFeedbackBufferRange";
560 gl_methd_name
= "glBindBufferRange";
564 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(transform feedback active)",
569 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
570 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
571 * generated if index is greater than or equal to the number of binding
572 * points for transform feedback, as described in section 6.7.1."
574 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d out of bounds)",
575 gl_methd_name
, index
);
580 /* OpenGL 4.5 core profile, 6.7, pdf page 103: multiple of 4 */
581 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d must be a multiple of "
582 "four)", gl_methd_name
, (int) size
);
587 /* OpenGL 4.5 core profile, 6.7, pdf page 103: multiple of 4 */
588 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset=%d must be a multiple "
589 "of four)", gl_methd_name
, (int) offset
);
594 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
595 * generated by BindBufferRange if offset is negative."
597 * OpenGL 4.5 core profile, 13.2, pdf page 445: "An INVALID_VALUE error
598 * is generated by TransformFeedbackBufferRange if offset is negative."
600 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset=%d must be >= 0)",
606 if (size
<= 0 && (dsa
|| bufObj
!= ctx
->Shared
->NullBufferObj
)) {
607 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
608 * generated by BindBufferRange if buffer is non-zero and size is less
609 * than or equal to zero."
611 * OpenGL 4.5 core profile, 13.2, pdf page 445: "An INVALID_VALUE error
612 * is generated by TransformFeedbackBufferRange if size is less than or
615 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d must be > 0)",
616 gl_methd_name
, (int) size
);
620 bind_buffer_range(ctx
, obj
, index
, bufObj
, offset
, size
, dsa
);
625 * Specify a buffer object to receive transform feedback results.
626 * As above, but start at offset = 0.
627 * Called from the glBindBufferBase() and glTransformFeedbackBufferBase()
631 _mesa_bind_buffer_base_transform_feedback(struct gl_context
*ctx
,
632 struct gl_transform_feedback_object
*obj
,
634 struct gl_buffer_object
*bufObj
,
638 _mesa_error(ctx
, GL_INVALID_OPERATION
,
639 "%s(transform feedback active)",
640 dsa
? "glTransformFeedbackBufferBase" : "glBindBufferBase");
644 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
645 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d out of bounds)",
646 dsa
? "glTransformFeedbackBufferBase" : "glBindBufferBase",
651 bind_buffer_range(ctx
, obj
, index
, bufObj
, 0, 0, dsa
);
655 * Wrapper around lookup_transform_feedback_object that throws
656 * GL_INVALID_OPERATION if id is not in the hash table. After calling
657 * _mesa_error, it returns NULL.
659 static struct gl_transform_feedback_object
*
660 lookup_transform_feedback_object_err(struct gl_context
*ctx
,
661 GLuint xfb
, const char* func
)
663 struct gl_transform_feedback_object
*obj
;
665 obj
= _mesa_lookup_transform_feedback_object(ctx
, xfb
);
667 _mesa_error(ctx
, GL_INVALID_OPERATION
,
668 "%s(xfb=%u: non-generated object name)", func
, xfb
);
675 * Wrapper around _mesa_lookup_bufferobj that throws GL_INVALID_VALUE if id
676 * is not in the hash table. Specialised version for the
677 * transform-feedback-related functions. After calling _mesa_error, it
680 static struct gl_buffer_object
*
681 lookup_transform_feedback_bufferobj_err(struct gl_context
*ctx
,
682 GLuint buffer
, const char* func
)
684 struct gl_buffer_object
*bufObj
;
686 /* OpenGL 4.5 core profile, 13.2, pdf page 444: buffer must be zero or the
687 * name of an existing buffer object.
690 bufObj
= ctx
->Shared
->NullBufferObj
;
692 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
694 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid buffer=%u)", func
,
703 _mesa_TransformFeedbackBufferBase(GLuint xfb
, GLuint index
, GLuint buffer
)
705 GET_CURRENT_CONTEXT(ctx
);
706 struct gl_transform_feedback_object
*obj
;
707 struct gl_buffer_object
*bufObj
;
709 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
710 "glTransformFeedbackBufferBase");
715 bufObj
= lookup_transform_feedback_bufferobj_err(ctx
, buffer
,
716 "glTransformFeedbackBufferBase");
721 _mesa_bind_buffer_base_transform_feedback(ctx
, obj
, index
, bufObj
, true);
725 _mesa_TransformFeedbackBufferRange(GLuint xfb
, GLuint index
, GLuint buffer
,
726 GLintptr offset
, GLsizeiptr size
)
728 GET_CURRENT_CONTEXT(ctx
);
729 struct gl_transform_feedback_object
*obj
;
730 struct gl_buffer_object
*bufObj
;
732 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
733 "glTransformFeedbackBufferRange");
738 bufObj
= lookup_transform_feedback_bufferobj_err(ctx
, buffer
,
739 "glTransformFeedbackBufferRange");
744 _mesa_bind_buffer_range_transform_feedback(ctx
, obj
, index
, bufObj
, offset
,
749 * Specify a buffer object to receive transform feedback results, plus the
750 * offset in the buffer to start placing results.
751 * This function is part of GL_EXT_transform_feedback, but not GL3.
754 _mesa_BindBufferOffsetEXT(GLenum target
, GLuint index
, GLuint buffer
,
757 struct gl_transform_feedback_object
*obj
;
758 struct gl_buffer_object
*bufObj
;
759 GET_CURRENT_CONTEXT(ctx
);
761 if (target
!= GL_TRANSFORM_FEEDBACK_BUFFER
) {
762 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferOffsetEXT(target)");
766 obj
= ctx
->TransformFeedback
.CurrentObject
;
769 _mesa_error(ctx
, GL_INVALID_OPERATION
,
770 "glBindBufferOffsetEXT(transform feedback active)");
774 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
775 _mesa_error(ctx
, GL_INVALID_VALUE
,
776 "glBindBufferOffsetEXT(index=%d)", index
);
781 /* must be multiple of four */
782 _mesa_error(ctx
, GL_INVALID_VALUE
,
783 "glBindBufferOffsetEXT(offset=%d)", (int) offset
);
788 bufObj
= ctx
->Shared
->NullBufferObj
;
790 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
794 _mesa_error(ctx
, GL_INVALID_OPERATION
,
795 "glBindBufferOffsetEXT(invalid buffer=%u)", buffer
);
799 bind_buffer_range(ctx
, obj
, index
, bufObj
, offset
, 0, false);
804 * This function specifies the transform feedback outputs to be written
805 * to the feedback buffer(s), and in what order.
808 _mesa_TransformFeedbackVaryings(GLuint program
, GLsizei count
,
809 const GLchar
* const *varyings
,
812 struct gl_shader_program
*shProg
;
814 GET_CURRENT_CONTEXT(ctx
);
816 /* From the ARB_transform_feedback2 specification:
817 * "The error INVALID_OPERATION is generated by TransformFeedbackVaryings
818 * if the current transform feedback object is active, even if paused."
820 if (ctx
->TransformFeedback
.CurrentObject
->Active
) {
821 _mesa_error(ctx
, GL_INVALID_OPERATION
,
822 "glTransformFeedbackVaryings(current object is active)");
826 switch (bufferMode
) {
827 case GL_INTERLEAVED_ATTRIBS
:
829 case GL_SEPARATE_ATTRIBS
:
832 _mesa_error(ctx
, GL_INVALID_ENUM
,
833 "glTransformFeedbackVaryings(bufferMode)");
838 (bufferMode
== GL_SEPARATE_ATTRIBS
&&
839 (GLuint
) count
> ctx
->Const
.MaxTransformFeedbackBuffers
)) {
840 _mesa_error(ctx
, GL_INVALID_VALUE
,
841 "glTransformFeedbackVaryings(count=%d)", count
);
845 shProg
= _mesa_lookup_shader_program(ctx
, program
);
847 _mesa_error(ctx
, GL_INVALID_VALUE
,
848 "glTransformFeedbackVaryings(program=%u)", program
);
852 if (ctx
->Extensions
.ARB_transform_feedback3
) {
853 if (bufferMode
== GL_INTERLEAVED_ATTRIBS
) {
854 unsigned buffers
= 1;
856 for (i
= 0; i
< count
; i
++) {
857 if (strcmp(varyings
[i
], "gl_NextBuffer") == 0)
861 if (buffers
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
862 _mesa_error(ctx
, GL_INVALID_OPERATION
,
863 "glTransformFeedbackVaryings(too many gl_NextBuffer "
868 for (i
= 0; i
< count
; i
++) {
869 if (strcmp(varyings
[i
], "gl_NextBuffer") == 0 ||
870 strcmp(varyings
[i
], "gl_SkipComponents1") == 0 ||
871 strcmp(varyings
[i
], "gl_SkipComponents2") == 0 ||
872 strcmp(varyings
[i
], "gl_SkipComponents3") == 0 ||
873 strcmp(varyings
[i
], "gl_SkipComponents4") == 0) {
874 _mesa_error(ctx
, GL_INVALID_OPERATION
,
875 "glTransformFeedbackVaryings(SEPARATE_ATTRIBS,"
884 /* free existing varyings, if any */
885 for (i
= 0; i
< (GLint
) shProg
->TransformFeedback
.NumVarying
; i
++) {
886 free(shProg
->TransformFeedback
.VaryingNames
[i
]);
888 free(shProg
->TransformFeedback
.VaryingNames
);
890 /* allocate new memory for varying names */
891 shProg
->TransformFeedback
.VaryingNames
=
892 malloc(count
* sizeof(GLchar
*));
894 if (!shProg
->TransformFeedback
.VaryingNames
) {
895 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTransformFeedbackVaryings()");
899 /* Save the new names and the count */
900 for (i
= 0; i
< count
; i
++) {
901 shProg
->TransformFeedback
.VaryingNames
[i
] = strdup(varyings
[i
]);
903 shProg
->TransformFeedback
.NumVarying
= count
;
905 shProg
->TransformFeedback
.BufferMode
= bufferMode
;
907 /* No need to invoke FLUSH_VERTICES or flag NewTransformFeedback since
908 * the varyings won't be used until shader link time.
914 * Get info about the transform feedback outputs which are to be written
915 * to the feedback buffer(s).
918 _mesa_GetTransformFeedbackVarying(GLuint program
, GLuint index
,
919 GLsizei bufSize
, GLsizei
*length
,
920 GLsizei
*size
, GLenum
*type
, GLchar
*name
)
922 const struct gl_shader_program
*shProg
;
923 const struct gl_transform_feedback_info
*linked_xfb_info
;
924 GET_CURRENT_CONTEXT(ctx
);
926 shProg
= _mesa_lookup_shader_program(ctx
, program
);
928 _mesa_error(ctx
, GL_INVALID_VALUE
,
929 "glGetTransformFeedbackVarying(program=%u)", program
);
933 linked_xfb_info
= &shProg
->LinkedTransformFeedback
;
934 if (index
>= (GLuint
) linked_xfb_info
->NumVarying
) {
935 _mesa_error(ctx
, GL_INVALID_VALUE
,
936 "glGetTransformFeedbackVarying(index=%u)", index
);
940 /* return the varying's name and length */
941 _mesa_copy_string(name
, bufSize
, length
,
942 linked_xfb_info
->Varyings
[index
].Name
);
944 /* return the datatype and value's size (in datatype units) */
946 *type
= linked_xfb_info
->Varyings
[index
].Type
;
948 *size
= linked_xfb_info
->Varyings
[index
].Size
;
953 struct gl_transform_feedback_object
*
954 _mesa_lookup_transform_feedback_object(struct gl_context
*ctx
, GLuint name
)
956 /* OpenGL 4.5 core profile, 13.2 pdf page 444: "xfb must be zero, indicating
957 * the default transform feedback object, or the name of an existing
958 * transform feedback object."
961 return ctx
->TransformFeedback
.DefaultObject
;
964 return (struct gl_transform_feedback_object
*)
965 _mesa_HashLookup(ctx
->TransformFeedback
.Objects
, name
);
969 create_transform_feedbacks(struct gl_context
*ctx
, GLsizei n
, GLuint
*ids
,
976 func
= "glCreateTransformFeedbacks";
978 func
= "glGenTransformFeedbacks";
981 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", func
);
988 /* we don't need contiguous IDs, but this might be faster */
989 first
= _mesa_HashFindFreeKeyBlock(ctx
->TransformFeedback
.Objects
, n
);
992 for (i
= 0; i
< n
; i
++) {
993 struct gl_transform_feedback_object
*obj
994 = ctx
->Driver
.NewTransformFeedback(ctx
, first
+ i
);
996 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1000 _mesa_HashInsert(ctx
->TransformFeedback
.Objects
, first
+ i
, obj
);
1002 /* this is normally done at bind time in the non-dsa case */
1003 obj
->EverBound
= GL_TRUE
;
1008 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1013 * Create new transform feedback objects. Transform feedback objects
1014 * encapsulate the state related to transform feedback to allow quickly
1015 * switching state (and drawing the results, below).
1016 * Part of GL_ARB_transform_feedback2.
1019 _mesa_GenTransformFeedbacks(GLsizei n
, GLuint
*names
)
1021 GET_CURRENT_CONTEXT(ctx
);
1023 /* GenTransformFeedbacks should just reserve the object names that a
1024 * subsequent call to BindTransformFeedback should actively create. For
1025 * the sake of simplicity, we reserve the names and create the objects
1029 create_transform_feedbacks(ctx
, n
, names
, false);
1033 * Create new transform feedback objects. Transform feedback objects
1034 * encapsulate the state related to transform feedback to allow quickly
1035 * switching state (and drawing the results, below).
1036 * Part of GL_ARB_direct_state_access.
1039 _mesa_CreateTransformFeedbacks(GLsizei n
, GLuint
*names
)
1041 GET_CURRENT_CONTEXT(ctx
);
1043 create_transform_feedbacks(ctx
, n
, names
, true);
1048 * Is the given ID a transform feedback object?
1049 * Part of GL_ARB_transform_feedback2.
1051 GLboolean GLAPIENTRY
1052 _mesa_IsTransformFeedback(GLuint name
)
1054 struct gl_transform_feedback_object
*obj
;
1055 GET_CURRENT_CONTEXT(ctx
);
1057 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1062 obj
= _mesa_lookup_transform_feedback_object(ctx
, name
);
1066 return obj
->EverBound
;
1071 * Bind the given transform feedback object.
1072 * Part of GL_ARB_transform_feedback2.
1075 _mesa_BindTransformFeedback(GLenum target
, GLuint name
)
1077 struct gl_transform_feedback_object
*obj
;
1078 GET_CURRENT_CONTEXT(ctx
);
1080 if (target
!= GL_TRANSFORM_FEEDBACK
) {
1081 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindTransformFeedback(target)");
1085 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
1086 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1087 "glBindTransformFeedback(transform is active, or not paused)");
1091 obj
= _mesa_lookup_transform_feedback_object(ctx
, name
);
1093 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1094 "glBindTransformFeedback(name=%u)", name
);
1098 reference_transform_feedback_object(&ctx
->TransformFeedback
.CurrentObject
,
1104 * Delete the given transform feedback objects.
1105 * Part of GL_ARB_transform_feedback2.
1108 _mesa_DeleteTransformFeedbacks(GLsizei n
, const GLuint
*names
)
1111 GET_CURRENT_CONTEXT(ctx
);
1114 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteTransformFeedbacks(n < 0)");
1121 for (i
= 0; i
< n
; i
++) {
1123 struct gl_transform_feedback_object
*obj
1124 = _mesa_lookup_transform_feedback_object(ctx
, names
[i
]);
1127 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1128 "glDeleteTransformFeedbacks(object %u is active)",
1132 _mesa_HashRemove(ctx
->TransformFeedback
.Objects
, names
[i
]);
1133 /* unref, but object may not be deleted until later */
1134 reference_transform_feedback_object(&obj
, NULL
);
1142 * Pause transform feedback.
1143 * Part of GL_ARB_transform_feedback2.
1146 _mesa_PauseTransformFeedback(void)
1148 struct gl_transform_feedback_object
*obj
;
1149 GET_CURRENT_CONTEXT(ctx
);
1151 obj
= ctx
->TransformFeedback
.CurrentObject
;
1153 if (!_mesa_is_xfb_active_and_unpaused(ctx
)) {
1154 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1155 "glPauseTransformFeedback(feedback not active or already paused)");
1159 FLUSH_VERTICES(ctx
, 0);
1160 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
1162 obj
->Paused
= GL_TRUE
;
1164 assert(ctx
->Driver
.PauseTransformFeedback
);
1165 ctx
->Driver
.PauseTransformFeedback(ctx
, obj
);
1170 * Resume transform feedback.
1171 * Part of GL_ARB_transform_feedback2.
1174 _mesa_ResumeTransformFeedback(void)
1176 struct gl_transform_feedback_object
*obj
;
1177 GET_CURRENT_CONTEXT(ctx
);
1179 obj
= ctx
->TransformFeedback
.CurrentObject
;
1181 if (!obj
->Active
|| !obj
->Paused
) {
1182 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1183 "glResumeTransformFeedback(feedback not active or not paused)");
1187 /* From the ARB_transform_feedback2 specification:
1188 * "The error INVALID_OPERATION is generated by ResumeTransformFeedback if
1189 * the program object being used by the current transform feedback object
1192 if (obj
->shader_program
!= get_xfb_source(ctx
)) {
1193 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1194 "glResumeTransformFeedback(wrong program bound)");
1198 FLUSH_VERTICES(ctx
, 0);
1199 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
1201 obj
->Paused
= GL_FALSE
;
1203 assert(ctx
->Driver
.ResumeTransformFeedback
);
1204 ctx
->Driver
.ResumeTransformFeedback(ctx
, obj
);
1207 extern void GLAPIENTRY
1208 _mesa_GetTransformFeedbackiv(GLuint xfb
, GLenum pname
, GLint
*param
)
1210 struct gl_transform_feedback_object
*obj
;
1211 GET_CURRENT_CONTEXT(ctx
);
1213 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
1214 "glGetTransformFeedbackiv");
1220 case GL_TRANSFORM_FEEDBACK_PAUSED
:
1221 *param
= obj
->Paused
;
1223 case GL_TRANSFORM_FEEDBACK_ACTIVE
:
1224 *param
= obj
->Active
;
1227 _mesa_error(ctx
, GL_INVALID_ENUM
,
1228 "glGetTransformFeedbackiv(pname=%i)", pname
);
1232 extern void GLAPIENTRY
1233 _mesa_GetTransformFeedbacki_v(GLuint xfb
, GLenum pname
, GLuint index
,
1236 struct gl_transform_feedback_object
*obj
;
1237 GET_CURRENT_CONTEXT(ctx
);
1239 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
1240 "glGetTransformFeedbacki_v");
1245 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
1246 _mesa_error(ctx
, GL_INVALID_VALUE
,
1247 "glGetTransformFeedbacki_v(index=%i)", index
);
1252 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING
:
1253 *param
= obj
->BufferNames
[index
];
1256 _mesa_error(ctx
, GL_INVALID_ENUM
,
1257 "glGetTransformFeedbacki_v(pname=%i)", pname
);
1261 extern void GLAPIENTRY
1262 _mesa_GetTransformFeedbacki64_v(GLuint xfb
, GLenum pname
, GLuint index
,
1265 struct gl_transform_feedback_object
*obj
;
1266 GET_CURRENT_CONTEXT(ctx
);
1268 obj
= lookup_transform_feedback_object_err(ctx
, xfb
,
1269 "glGetTransformFeedbacki64_v");
1274 if (index
>= ctx
->Const
.MaxTransformFeedbackBuffers
) {
1275 _mesa_error(ctx
, GL_INVALID_VALUE
,
1276 "glGetTransformFeedbacki64_v(index=%i)", index
);
1281 case GL_TRANSFORM_FEEDBACK_BUFFER_START
:
1282 *param
= obj
->Offset
[index
];
1284 case GL_TRANSFORM_FEEDBACK_BUFFER_SIZE
:
1285 *param
= obj
->RequestedSize
[index
];
1288 _mesa_error(ctx
, GL_INVALID_ENUM
,
1289 "glGetTransformFeedbacki64_v(pname=%i)", pname
);