transformfeedback.c revision af69d88d
1/* 2 * Mesa 3-D graphics library 3 * 4 * Copyright (C) 2010 VMware, Inc. All Rights Reserved. 5 * 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: 12 * 13 * The above copyright notice and this permission notice shall be included 14 * in all copies or substantial portions of the Software. 15 * 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. 23 */ 24 25 26/* 27 * Transform feedback support. 28 * 29 * Authors: 30 * Brian Paul 31 */ 32 33 34#include "buffers.h" 35#include "context.h" 36#include "hash.h" 37#include "macros.h" 38#include "mtypes.h" 39#include "transformfeedback.h" 40#include "shaderapi.h" 41#include "shaderobj.h" 42#include "main/dispatch.h" 43 44#include "program/prog_parameter.h" 45 46struct using_program_tuple 47{ 48 struct gl_shader_program *shProg; 49 bool found; 50}; 51 52static void 53active_xfb_object_references_program(GLuint key, void *data, void *user_data) 54{ 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; 59} 60 61/** 62 * Return true if any active transform feedback object is using a program. 63 */ 64bool 65_mesa_transform_feedback_is_using_program(struct gl_context *ctx, 66 struct gl_shader_program *shProg) 67{ 68 struct using_program_tuple callback_data; 69 callback_data.shProg = shProg; 70 callback_data.found = false; 71 72 _mesa_HashWalk(ctx->TransformFeedback.Objects, 73 active_xfb_object_references_program, &callback_data); 74 75 /* Also check DefaultObject, as it's not in the Objects hash table. */ 76 active_xfb_object_references_program(0, ctx->TransformFeedback.DefaultObject, 77 &callback_data); 78 79 return callback_data.found; 80} 81 82/** 83 * Do reference counting of transform feedback buffers. 84 */ 85static void 86reference_transform_feedback_object(struct gl_transform_feedback_object **ptr, 87 struct gl_transform_feedback_object *obj) 88{ 89 if (*ptr == obj) 90 return; 91 92 if (*ptr) { 93 /* Unreference the old object */ 94 struct gl_transform_feedback_object *oldObj = *ptr; 95 96 ASSERT(oldObj->RefCount > 0); 97 oldObj->RefCount--; 98 99 if (oldObj->RefCount == 0) { 100 GET_CURRENT_CONTEXT(ctx); 101 if (ctx) 102 ctx->Driver.DeleteTransformFeedback(ctx, oldObj); 103 } 104 105 *ptr = NULL; 106 } 107 ASSERT(!*ptr); 108 109 if (obj) { 110 /* reference new object */ 111 if (obj->RefCount == 0) { 112 _mesa_problem(NULL, "referencing deleted transform feedback object"); 113 *ptr = NULL; 114 } 115 else { 116 obj->RefCount++; 117 obj->EverBound = GL_TRUE; 118 *ptr = obj; 119 } 120 } 121} 122 123 124/** 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 129 */ 130GLboolean 131_mesa_validate_transform_feedback_buffers(struct gl_context *ctx) 132{ 133 /* XXX to do */ 134 return GL_TRUE; 135} 136 137 138 139/** 140 * Per-context init for transform feedback. 141 */ 142void 143_mesa_init_transform_feedback(struct gl_context *ctx) 144{ 145 /* core mesa expects this, even a dummy one, to be available */ 146 ASSERT(ctx->Driver.NewTransformFeedback); 147 148 ctx->TransformFeedback.DefaultObject = 149 ctx->Driver.NewTransformFeedback(ctx, 0); 150 151 assert(ctx->TransformFeedback.DefaultObject->RefCount == 1); 152 153 reference_transform_feedback_object(&ctx->TransformFeedback.CurrentObject, 154 ctx->TransformFeedback.DefaultObject); 155 156 assert(ctx->TransformFeedback.DefaultObject->RefCount == 2); 157 158 ctx->TransformFeedback.Objects = _mesa_NewHashTable(); 159 160 _mesa_reference_buffer_object(ctx, 161 &ctx->TransformFeedback.CurrentBuffer, 162 ctx->Shared->NullBufferObj); 163} 164 165 166 167/** 168 * Callback for _mesa_HashDeleteAll(). 169 */ 170static void 171delete_cb(GLuint key, void *data, void *userData) 172{ 173 struct gl_context *ctx = (struct gl_context *) userData; 174 struct gl_transform_feedback_object *obj = 175 (struct gl_transform_feedback_object *) data; 176 177 ctx->Driver.DeleteTransformFeedback(ctx, obj); 178} 179 180 181/** 182 * Per-context free/clean-up for transform feedback. 183 */ 184void 185_mesa_free_transform_feedback(struct gl_context *ctx) 186{ 187 /* core mesa expects this, even a dummy one, to be available */ 188 ASSERT(ctx->Driver.NewTransformFeedback); 189 190 _mesa_reference_buffer_object(ctx, 191 &ctx->TransformFeedback.CurrentBuffer, 192 NULL); 193 194 /* Delete all feedback objects */ 195 _mesa_HashDeleteAll(ctx->TransformFeedback.Objects, delete_cb, ctx); 196 _mesa_DeleteHashTable(ctx->TransformFeedback.Objects); 197 198 /* Delete the default feedback object */ 199 assert(ctx->Driver.DeleteTransformFeedback); 200 ctx->Driver.DeleteTransformFeedback(ctx, 201 ctx->TransformFeedback.DefaultObject); 202 203 ctx->TransformFeedback.CurrentObject = NULL; 204} 205 206 207/** Initialize the fields of a gl_transform_feedback_object. */ 208void 209_mesa_init_transform_feedback_object(struct gl_transform_feedback_object *obj, 210 GLuint name) 211{ 212 if (!obj) 213 return; 214 215 obj->Name = name; 216 obj->RefCount = 1; 217 obj->EverBound = GL_FALSE; 218} 219 220 221/** Default fallback for ctx->Driver.NewTransformFeedback() */ 222static struct gl_transform_feedback_object * 223new_transform_feedback(struct gl_context *ctx, GLuint name) 224{ 225 struct gl_transform_feedback_object *obj; 226 obj = CALLOC_STRUCT(gl_transform_feedback_object); 227 _mesa_init_transform_feedback_object(obj, name); 228 return obj; 229} 230 231/** Default fallback for ctx->Driver.DeleteTransformFeedback() */ 232static void 233delete_transform_feedback(struct gl_context *ctx, 234 struct gl_transform_feedback_object *obj) 235{ 236 GLuint i; 237 238 for (i = 0; i < Elements(obj->Buffers); i++) { 239 _mesa_reference_buffer_object(ctx, &obj->Buffers[i], NULL); 240 } 241 242 free(obj->Label); 243 free(obj); 244} 245 246 247/** Default fallback for ctx->Driver.BeginTransformFeedback() */ 248static void 249begin_transform_feedback(struct gl_context *ctx, GLenum mode, 250 struct gl_transform_feedback_object *obj) 251{ 252 /* nop */ 253} 254 255/** Default fallback for ctx->Driver.EndTransformFeedback() */ 256static void 257end_transform_feedback(struct gl_context *ctx, 258 struct gl_transform_feedback_object *obj) 259{ 260 /* nop */ 261} 262 263/** Default fallback for ctx->Driver.PauseTransformFeedback() */ 264static void 265pause_transform_feedback(struct gl_context *ctx, 266 struct gl_transform_feedback_object *obj) 267{ 268 /* nop */ 269} 270 271/** Default fallback for ctx->Driver.ResumeTransformFeedback() */ 272static void 273resume_transform_feedback(struct gl_context *ctx, 274 struct gl_transform_feedback_object *obj) 275{ 276 /* nop */ 277} 278 279 280/** 281 * Plug in default device driver functions for transform feedback. 282 * Most drivers will override some/all of these. 283 */ 284void 285_mesa_init_transform_feedback_functions(struct dd_function_table *driver) 286{ 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; 293} 294 295 296/** 297 * Fill in the correct Size value for each buffer in \c obj. 298 * 299 * From the GL 4.3 spec, section 6.1.1 ("Binding Buffer Objects to Indexed 300 * Targets"): 301 * 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. 306 * 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. 312 */ 313static void 314compute_transform_feedback_buffer_sizes( 315 struct gl_transform_feedback_object *obj) 316{ 317 unsigned i = 0; 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. 328 */ 329 computed_size = available_space; 330 } else { 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 334 * available. 335 */ 336 computed_size = MIN2(available_space, obj->RequestedSize[i]); 337 } 338 339 /* Legal sizes must be multiples of four, so round down if necessary. */ 340 obj->Size[i] = computed_size & ~0x3; 341 } 342} 343 344 345/** 346 * Compute the maximum number of vertices that can be written to the currently 347 * enabled transform feedback buffers without overflowing any of them. 348 */ 349unsigned 350_mesa_compute_max_transform_feedback_vertices( 351 const struct gl_transform_feedback_object *obj, 352 const struct gl_transform_feedback_info *info) 353{ 354 unsigned max_index = 0xffffffff; 355 unsigned i; 356 357 for (i = 0; i < info->NumBuffers; ++i) { 358 unsigned stride = info->BufferStride[i]; 359 unsigned max_for_this_buffer; 360 361 /* Skip any inactive buffers, which have a stride of 0. */ 362 if (stride == 0) 363 continue; 364 365 max_for_this_buffer = obj->Size[i] / (4 * stride); 366 max_index = MIN2(max_index, max_for_this_buffer); 367 } 368 369 return max_index; 370} 371 372 373/** 374 ** Begin API functions 375 **/ 376 377 378/** 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. 381 * 382 * If no active program can generate transform feedback data (i.e. no vertex 383 * shader is active), returns NULL. 384 */ 385static struct gl_shader_program * 386get_xfb_source(struct gl_context *ctx) 387{ 388 int i; 389 for (i = MESA_SHADER_GEOMETRY; i >= MESA_SHADER_VERTEX; i--) { 390 if (ctx->_Shader->CurrentProgram[i] != NULL) 391 return ctx->_Shader->CurrentProgram[i]; 392 } 393 return NULL; 394} 395 396 397void GLAPIENTRY 398_mesa_BeginTransformFeedback(GLenum mode) 399{ 400 struct gl_transform_feedback_object *obj; 401 struct gl_transform_feedback_info *info = NULL; 402 struct gl_shader_program *source; 403 GLuint i; 404 unsigned vertices_per_prim; 405 GET_CURRENT_CONTEXT(ctx); 406 407 obj = ctx->TransformFeedback.CurrentObject; 408 409 /* Figure out what pipeline stage is the source of data for transform 410 * feedback. 411 */ 412 source = get_xfb_source(ctx); 413 if (source == NULL) { 414 _mesa_error(ctx, GL_INVALID_OPERATION, 415 "glBeginTransformFeedback(no program active)"); 416 return; 417 } 418 419 info = &source->LinkedTransformFeedback; 420 421 if (info->NumOutputs == 0) { 422 _mesa_error(ctx, GL_INVALID_OPERATION, 423 "glBeginTransformFeedback(no varyings to record)"); 424 return; 425 } 426 427 switch (mode) { 428 case GL_POINTS: 429 vertices_per_prim = 1; 430 break; 431 case GL_LINES: 432 vertices_per_prim = 2; 433 break; 434 case GL_TRIANGLES: 435 vertices_per_prim = 3; 436 break; 437 default: 438 _mesa_error(ctx, GL_INVALID_ENUM, "glBeginTransformFeedback(mode)"); 439 return; 440 } 441 442 if (obj->Active) { 443 _mesa_error(ctx, GL_INVALID_OPERATION, 444 "glBeginTransformFeedback(already active)"); 445 return; 446 } 447 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); 453 return; 454 } 455 } 456 457 FLUSH_VERTICES(ctx, 0); 458 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback; 459 460 obj->Active = GL_TRUE; 461 ctx->TransformFeedback.Mode = mode; 462 463 compute_transform_feedback_buffer_sizes(obj); 464 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 470 * feedback. 471 */ 472 unsigned max_vertices 473 = _mesa_compute_max_transform_feedback_vertices(obj, info); 474 obj->GlesRemainingPrims = max_vertices / vertices_per_prim; 475 } 476 477 if (obj->shader_program != source) { 478 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedbackProg; 479 obj->shader_program = source; 480 } 481 482 assert(ctx->Driver.BeginTransformFeedback); 483 ctx->Driver.BeginTransformFeedback(ctx, mode, obj); 484} 485 486 487void GLAPIENTRY 488_mesa_EndTransformFeedback(void) 489{ 490 struct gl_transform_feedback_object *obj; 491 GET_CURRENT_CONTEXT(ctx); 492 493 obj = ctx->TransformFeedback.CurrentObject; 494 495 if (!obj->Active) { 496 _mesa_error(ctx, GL_INVALID_OPERATION, 497 "glEndTransformFeedback(not active)"); 498 return; 499 } 500 501 FLUSH_VERTICES(ctx, 0); 502 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback; 503 504 ctx->TransformFeedback.CurrentObject->Active = GL_FALSE; 505 ctx->TransformFeedback.CurrentObject->Paused = GL_FALSE; 506 ctx->TransformFeedback.CurrentObject->EndedAnytime = GL_TRUE; 507 508 assert(ctx->Driver.EndTransformFeedback); 509 ctx->Driver.EndTransformFeedback(ctx, obj); 510} 511 512 513/** 514 * Helper used by BindBufferRange() and BindBufferBase(). 515 */ 516static void 517bind_buffer_range(struct gl_context *ctx, GLuint index, 518 struct gl_buffer_object *bufObj, 519 GLintptr offset, GLsizeiptr size) 520{ 521 struct gl_transform_feedback_object *obj = 522 ctx->TransformFeedback.CurrentObject; 523 524 /* Note: no need to FLUSH_VERTICES or flag NewTransformFeedback, because 525 * transform feedback buffers can't be changed while transform feedback is 526 * active. 527 */ 528 529 /* The general binding point */ 530 _mesa_reference_buffer_object(ctx, 531 &ctx->TransformFeedback.CurrentBuffer, 532 bufObj); 533 534 /* The per-attribute binding point */ 535 _mesa_set_transform_feedback_binding(ctx, obj, index, bufObj, offset, size); 536} 537 538 539/** 540 * Specify a buffer object to receive transform feedback results. Plus, 541 * specify the starting offset to place the results, and max size. 542 * Called from the glBindBufferRange() function. 543 */ 544void 545_mesa_bind_buffer_range_transform_feedback(struct gl_context *ctx, 546 GLuint index, 547 struct gl_buffer_object *bufObj, 548 GLintptr offset, 549 GLsizeiptr size) 550{ 551 struct gl_transform_feedback_object *obj; 552 553 obj = ctx->TransformFeedback.CurrentObject; 554 555 if (obj->Active) { 556 _mesa_error(ctx, GL_INVALID_OPERATION, 557 "glBindBufferRange(transform feedback active)"); 558 return; 559 } 560 561 if (index >= ctx->Const.MaxTransformFeedbackBuffers) { 562 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index); 563 return; 564 } 565 566 if (size & 0x3) { 567 /* must a multiple of four */ 568 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)", (int) size); 569 return; 570 } 571 572 if (offset & 0x3) { 573 /* must be multiple of four */ 574 _mesa_error(ctx, GL_INVALID_VALUE, 575 "glBindBufferRange(offset=%d)", (int) offset); 576 return; 577 } 578 579 bind_buffer_range(ctx, index, bufObj, offset, size); 580} 581 582 583/** 584 * Specify a buffer object to receive transform feedback results. 585 * As above, but start at offset = 0. 586 * Called from the glBindBufferBase() function. 587 */ 588void 589_mesa_bind_buffer_base_transform_feedback(struct gl_context *ctx, 590 GLuint index, 591 struct gl_buffer_object *bufObj) 592{ 593 struct gl_transform_feedback_object *obj; 594 595 obj = ctx->TransformFeedback.CurrentObject; 596 597 if (obj->Active) { 598 _mesa_error(ctx, GL_INVALID_OPERATION, 599 "glBindBufferBase(transform feedback active)"); 600 return; 601 } 602 603 if (index >= ctx->Const.MaxTransformFeedbackBuffers) { 604 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index); 605 return; 606 } 607 608 bind_buffer_range(ctx, index, bufObj, 0, 0); 609} 610 611 612/** 613 * Specify a buffer object to receive transform feedback results, plus the 614 * offset in the buffer to start placing results. 615 * This function is part of GL_EXT_transform_feedback, but not GL3. 616 */ 617void GLAPIENTRY 618_mesa_BindBufferOffsetEXT(GLenum target, GLuint index, GLuint buffer, 619 GLintptr offset) 620{ 621 struct gl_transform_feedback_object *obj; 622 struct gl_buffer_object *bufObj; 623 GET_CURRENT_CONTEXT(ctx); 624 625 if (target != GL_TRANSFORM_FEEDBACK_BUFFER) { 626 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferOffsetEXT(target)"); 627 return; 628 } 629 630 obj = ctx->TransformFeedback.CurrentObject; 631 632 if (obj->Active) { 633 _mesa_error(ctx, GL_INVALID_OPERATION, 634 "glBindBufferOffsetEXT(transform feedback active)"); 635 return; 636 } 637 638 if (index >= ctx->Const.MaxTransformFeedbackBuffers) { 639 _mesa_error(ctx, GL_INVALID_VALUE, 640 "glBindBufferOffsetEXT(index=%d)", index); 641 return; 642 } 643 644 if (offset & 0x3) { 645 /* must be multiple of four */ 646 _mesa_error(ctx, GL_INVALID_VALUE, 647 "glBindBufferOffsetEXT(offset=%d)", (int) offset); 648 return; 649 } 650 651 if (buffer == 0) { 652 bufObj = ctx->Shared->NullBufferObj; 653 } else { 654 bufObj = _mesa_lookup_bufferobj(ctx, buffer); 655 } 656 657 if (!bufObj) { 658 _mesa_error(ctx, GL_INVALID_OPERATION, 659 "glBindBufferOffsetEXT(invalid buffer=%u)", buffer); 660 return; 661 } 662 663 bind_buffer_range(ctx, index, bufObj, offset, 0); 664} 665 666 667/** 668 * This function specifies the transform feedback outputs to be written 669 * to the feedback buffer(s), and in what order. 670 */ 671void GLAPIENTRY 672_mesa_TransformFeedbackVaryings(GLuint program, GLsizei count, 673 const GLchar * const *varyings, 674 GLenum bufferMode) 675{ 676 struct gl_shader_program *shProg; 677 GLint i; 678 GET_CURRENT_CONTEXT(ctx); 679 680 /* From the ARB_transform_feedback2 specification: 681 * "The error INVALID_OPERATION is generated by TransformFeedbackVaryings 682 * if the current transform feedback object is active, even if paused." 683 */ 684 if (ctx->TransformFeedback.CurrentObject->Active) { 685 _mesa_error(ctx, GL_INVALID_OPERATION, 686 "glTransformFeedbackVaryings(current object is active)"); 687 return; 688 } 689 690 switch (bufferMode) { 691 case GL_INTERLEAVED_ATTRIBS: 692 break; 693 case GL_SEPARATE_ATTRIBS: 694 break; 695 default: 696 _mesa_error(ctx, GL_INVALID_ENUM, 697 "glTransformFeedbackVaryings(bufferMode)"); 698 return; 699 } 700 701 if (count < 0 || 702 (bufferMode == GL_SEPARATE_ATTRIBS && 703 (GLuint) count > ctx->Const.MaxTransformFeedbackBuffers)) { 704 _mesa_error(ctx, GL_INVALID_VALUE, 705 "glTransformFeedbackVaryings(count=%d)", count); 706 return; 707 } 708 709 shProg = _mesa_lookup_shader_program(ctx, program); 710 if (!shProg) { 711 _mesa_error(ctx, GL_INVALID_VALUE, 712 "glTransformFeedbackVaryings(program=%u)", program); 713 return; 714 } 715 716 if (ctx->Extensions.ARB_transform_feedback3) { 717 if (bufferMode == GL_INTERLEAVED_ATTRIBS) { 718 unsigned buffers = 1; 719 720 for (i = 0; i < count; i++) { 721 if (strcmp(varyings[i], "gl_NextBuffer") == 0) 722 buffers++; 723 } 724 725 if (buffers > ctx->Const.MaxTransformFeedbackBuffers) { 726 _mesa_error(ctx, GL_INVALID_OPERATION, 727 "glTransformFeedbackVaryings(too many gl_NextBuffer " 728 "occurences)"); 729 return; 730 } 731 } else { 732 for (i = 0; i < count; i++) { 733 if (strcmp(varyings[i], "gl_NextBuffer") == 0 || 734 strcmp(varyings[i], "gl_SkipComponents1") == 0 || 735 strcmp(varyings[i], "gl_SkipComponents2") == 0 || 736 strcmp(varyings[i], "gl_SkipComponents3") == 0 || 737 strcmp(varyings[i], "gl_SkipComponents4") == 0) { 738 _mesa_error(ctx, GL_INVALID_OPERATION, 739 "glTransformFeedbackVaryings(SEPARATE_ATTRIBS," 740 "varying=%s)", 741 varyings[i]); 742 return; 743 } 744 } 745 } 746 } 747 748 /* free existing varyings, if any */ 749 for (i = 0; i < (GLint) shProg->TransformFeedback.NumVarying; i++) { 750 free(shProg->TransformFeedback.VaryingNames[i]); 751 } 752 free(shProg->TransformFeedback.VaryingNames); 753 754 /* allocate new memory for varying names */ 755 shProg->TransformFeedback.VaryingNames = 756 malloc(count * sizeof(GLchar *)); 757 758 if (!shProg->TransformFeedback.VaryingNames) { 759 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTransformFeedbackVaryings()"); 760 return; 761 } 762 763 /* Save the new names and the count */ 764 for (i = 0; i < count; i++) { 765 shProg->TransformFeedback.VaryingNames[i] = _mesa_strdup(varyings[i]); 766 } 767 shProg->TransformFeedback.NumVarying = count; 768 769 shProg->TransformFeedback.BufferMode = bufferMode; 770 771 /* No need to invoke FLUSH_VERTICES or flag NewTransformFeedback since 772 * the varyings won't be used until shader link time. 773 */ 774} 775 776 777/** 778 * Get info about the transform feedback outputs which are to be written 779 * to the feedback buffer(s). 780 */ 781void GLAPIENTRY 782_mesa_GetTransformFeedbackVarying(GLuint program, GLuint index, 783 GLsizei bufSize, GLsizei *length, 784 GLsizei *size, GLenum *type, GLchar *name) 785{ 786 const struct gl_shader_program *shProg; 787 const struct gl_transform_feedback_info *linked_xfb_info; 788 GET_CURRENT_CONTEXT(ctx); 789 790 shProg = _mesa_lookup_shader_program(ctx, program); 791 if (!shProg) { 792 _mesa_error(ctx, GL_INVALID_VALUE, 793 "glGetTransformFeedbackVarying(program=%u)", program); 794 return; 795 } 796 797 linked_xfb_info = &shProg->LinkedTransformFeedback; 798 if (index >= (GLuint) linked_xfb_info->NumVarying) { 799 _mesa_error(ctx, GL_INVALID_VALUE, 800 "glGetTransformFeedbackVarying(index=%u)", index); 801 return; 802 } 803 804 /* return the varying's name and length */ 805 _mesa_copy_string(name, bufSize, length, 806 linked_xfb_info->Varyings[index].Name); 807 808 /* return the datatype and value's size (in datatype units) */ 809 if (type) 810 *type = linked_xfb_info->Varyings[index].Type; 811 if (size) 812 *size = linked_xfb_info->Varyings[index].Size; 813} 814 815 816 817struct gl_transform_feedback_object * 818_mesa_lookup_transform_feedback_object(struct gl_context *ctx, GLuint name) 819{ 820 if (name == 0) { 821 return ctx->TransformFeedback.DefaultObject; 822 } 823 else 824 return (struct gl_transform_feedback_object *) 825 _mesa_HashLookup(ctx->TransformFeedback.Objects, name); 826} 827 828 829/** 830 * Create new transform feedback objects. Transform feedback objects 831 * encapsulate the state related to transform feedback to allow quickly 832 * switching state (and drawing the results, below). 833 * Part of GL_ARB_transform_feedback2. 834 */ 835void GLAPIENTRY 836_mesa_GenTransformFeedbacks(GLsizei n, GLuint *names) 837{ 838 GLuint first; 839 GET_CURRENT_CONTEXT(ctx); 840 841 if (n < 0) { 842 _mesa_error(ctx, GL_INVALID_VALUE, "glGenTransformFeedbacks(n < 0)"); 843 return; 844 } 845 846 if (!names) 847 return; 848 849 /* we don't need contiguous IDs, but this might be faster */ 850 first = _mesa_HashFindFreeKeyBlock(ctx->TransformFeedback.Objects, n); 851 if (first) { 852 GLsizei i; 853 for (i = 0; i < n; i++) { 854 struct gl_transform_feedback_object *obj 855 = ctx->Driver.NewTransformFeedback(ctx, first + i); 856 if (!obj) { 857 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenTransformFeedbacks"); 858 return; 859 } 860 names[i] = first + i; 861 _mesa_HashInsert(ctx->TransformFeedback.Objects, first + i, obj); 862 } 863 } 864 else { 865 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenTransformFeedbacks"); 866 } 867} 868 869 870/** 871 * Is the given ID a transform feedback object? 872 * Part of GL_ARB_transform_feedback2. 873 */ 874GLboolean GLAPIENTRY 875_mesa_IsTransformFeedback(GLuint name) 876{ 877 struct gl_transform_feedback_object *obj; 878 GET_CURRENT_CONTEXT(ctx); 879 880 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE); 881 882 if (name == 0) 883 return GL_FALSE; 884 885 obj = _mesa_lookup_transform_feedback_object(ctx, name); 886 if (obj == NULL) 887 return GL_FALSE; 888 889 return obj->EverBound; 890} 891 892 893/** 894 * Bind the given transform feedback object. 895 * Part of GL_ARB_transform_feedback2. 896 */ 897void GLAPIENTRY 898_mesa_BindTransformFeedback(GLenum target, GLuint name) 899{ 900 struct gl_transform_feedback_object *obj; 901 GET_CURRENT_CONTEXT(ctx); 902 903 if (target != GL_TRANSFORM_FEEDBACK) { 904 _mesa_error(ctx, GL_INVALID_ENUM, "glBindTransformFeedback(target)"); 905 return; 906 } 907 908 if (_mesa_is_xfb_active_and_unpaused(ctx)) { 909 _mesa_error(ctx, GL_INVALID_OPERATION, 910 "glBindTransformFeedback(transform is active, or not paused)"); 911 return; 912 } 913 914 obj = _mesa_lookup_transform_feedback_object(ctx, name); 915 if (!obj) { 916 _mesa_error(ctx, GL_INVALID_OPERATION, 917 "glBindTransformFeedback(name=%u)", name); 918 return; 919 } 920 921 reference_transform_feedback_object(&ctx->TransformFeedback.CurrentObject, 922 obj); 923} 924 925 926/** 927 * Delete the given transform feedback objects. 928 * Part of GL_ARB_transform_feedback2. 929 */ 930void GLAPIENTRY 931_mesa_DeleteTransformFeedbacks(GLsizei n, const GLuint *names) 932{ 933 GLint i; 934 GET_CURRENT_CONTEXT(ctx); 935 936 if (n < 0) { 937 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteTransformFeedbacks(n < 0)"); 938 return; 939 } 940 941 if (!names) 942 return; 943 944 for (i = 0; i < n; i++) { 945 if (names[i] > 0) { 946 struct gl_transform_feedback_object *obj 947 = _mesa_lookup_transform_feedback_object(ctx, names[i]); 948 if (obj) { 949 if (obj->Active) { 950 _mesa_error(ctx, GL_INVALID_OPERATION, 951 "glDeleteTransformFeedbacks(object %u is active)", 952 names[i]); 953 return; 954 } 955 _mesa_HashRemove(ctx->TransformFeedback.Objects, names[i]); 956 /* unref, but object may not be deleted until later */ 957 reference_transform_feedback_object(&obj, NULL); 958 } 959 } 960 } 961} 962 963 964/** 965 * Pause transform feedback. 966 * Part of GL_ARB_transform_feedback2. 967 */ 968void GLAPIENTRY 969_mesa_PauseTransformFeedback(void) 970{ 971 struct gl_transform_feedback_object *obj; 972 GET_CURRENT_CONTEXT(ctx); 973 974 obj = ctx->TransformFeedback.CurrentObject; 975 976 if (!_mesa_is_xfb_active_and_unpaused(ctx)) { 977 _mesa_error(ctx, GL_INVALID_OPERATION, 978 "glPauseTransformFeedback(feedback not active or already paused)"); 979 return; 980 } 981 982 FLUSH_VERTICES(ctx, 0); 983 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback; 984 985 obj->Paused = GL_TRUE; 986 987 assert(ctx->Driver.PauseTransformFeedback); 988 ctx->Driver.PauseTransformFeedback(ctx, obj); 989} 990 991 992/** 993 * Resume transform feedback. 994 * Part of GL_ARB_transform_feedback2. 995 */ 996void GLAPIENTRY 997_mesa_ResumeTransformFeedback(void) 998{ 999 struct gl_transform_feedback_object *obj; 1000 GET_CURRENT_CONTEXT(ctx); 1001 1002 obj = ctx->TransformFeedback.CurrentObject; 1003 1004 if (!obj->Active || !obj->Paused) { 1005 _mesa_error(ctx, GL_INVALID_OPERATION, 1006 "glResumeTransformFeedback(feedback not active or not paused)"); 1007 return; 1008 } 1009 1010 /* From the ARB_transform_feedback2 specification: 1011 * "The error INVALID_OPERATION is generated by ResumeTransformFeedback if 1012 * the program object being used by the current transform feedback object 1013 * is not active." 1014 */ 1015 if (obj->shader_program != get_xfb_source(ctx)) { 1016 _mesa_error(ctx, GL_INVALID_OPERATION, 1017 "glResumeTransformFeedback(wrong program bound)"); 1018 return; 1019 } 1020 1021 FLUSH_VERTICES(ctx, 0); 1022 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback; 1023 1024 obj->Paused = GL_FALSE; 1025 1026 assert(ctx->Driver.ResumeTransformFeedback); 1027 ctx->Driver.ResumeTransformFeedback(ctx, obj); 1028} 1029