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 46 struct using_program_tuple 47 { 48 struct gl_program *prog; 49 bool found; 50 }; 51 52 static void 53 active_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->program == callback_data->prog) 58 callback_data->found = true; 59 } 60 61 /** 62 * Return true if any active transform feedback object is using a program. 63 */ 64 bool 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.found = false; 70 callback_data.prog = shProg->xfb_program; 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 */ 85 static void 86 reference_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 */ 130 GLboolean 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 */ 142 void 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 */ 170 static void 171 delete_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 */ 184 void 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. */ 208 void 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() */ 222 static struct gl_transform_feedback_object * 223 new_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() */ 232 static void 233 delete_transform_feedback(struct gl_context *ctx, 234 struct gl_transform_feedback_object *obj) 235 { 236 GLuint i; 237 238 for (i = 0; i < ARRAY_SIZE(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() */ 248 static void 249 begin_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() */ 256 static void 257 end_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() */ 264 static void 265 pause_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() */ 272 static void 273 resume_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 */ 284 void 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 */ 313 static void 314 compute_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 */ 349 unsigned 350 _mesa_compute_max_transform_feedback_vertices(struct gl_context *ctx, 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 < ctx->Const.MaxTransformFeedbackBuffers; i++) { 358 if ((info->ActiveBuffers >> i) & 1) { 359 unsigned stride = info->Buffers[i].Stride; 360 unsigned max_for_this_buffer; 361 362 /* Skip any inactive buffers, which have a stride of 0. */ 363 if (stride == 0) 364 continue; 365 366 max_for_this_buffer = obj->Size[i] / (4 * stride); 367 max_index = MIN2(max_index, max_for_this_buffer); 368 } 369 } 370 371 return max_index; 372 } 373 374 375 /** 376 ** Begin API functions 377 **/ 378 379 380 /** 381 * Figure out which stage of the pipeline is the source of transform feedback 382 * data given the current context state, and return its gl_program. 383 * 384 * If no active program can generate transform feedback data (i.e. no vertex 385 * shader is active), returns NULL. 386 */ 387 static struct gl_program * 388 get_xfb_source(struct gl_context *ctx) 389 { 390 int i; 391 for (i = MESA_SHADER_GEOMETRY; i >= MESA_SHADER_VERTEX; i--) { 392 if (ctx->_Shader->CurrentProgram[i] != NULL) 393 return ctx->_Shader->CurrentProgram[i]->_LinkedShaders[i]->Program; 394 } 395 return NULL; 396 } 397 398 399 void GLAPIENTRY 400 _mesa_BeginTransformFeedback(GLenum mode) 401 { 402 struct gl_transform_feedback_object *obj; 403 struct gl_transform_feedback_info *info = NULL; 404 GLuint i; 405 unsigned vertices_per_prim; 406 GET_CURRENT_CONTEXT(ctx); 407 408 obj = ctx->TransformFeedback.CurrentObject; 409 410 /* Figure out what pipeline stage is the source of data for transform 411 * feedback. 412 */ 413 struct gl_program *source = get_xfb_source(ctx); 414 if (source == NULL) { 415 _mesa_error(ctx, GL_INVALID_OPERATION, 416 "glBeginTransformFeedback(no program active)"); 417 return; 418 } 419 420 info = source->sh.LinkedTransformFeedback; 421 422 if (info->NumOutputs == 0) { 423 _mesa_error(ctx, GL_INVALID_OPERATION, 424 "glBeginTransformFeedback(no varyings to record)"); 425 return; 426 } 427 428 switch (mode) { 429 case GL_POINTS: 430 vertices_per_prim = 1; 431 break; 432 case GL_LINES: 433 vertices_per_prim = 2; 434 break; 435 case GL_TRIANGLES: 436 vertices_per_prim = 3; 437 break; 438 default: 439 _mesa_error(ctx, GL_INVALID_ENUM, "glBeginTransformFeedback(mode)"); 440 return; 441 } 442 443 if (obj->Active) { 444 _mesa_error(ctx, GL_INVALID_OPERATION, 445 "glBeginTransformFeedback(already active)"); 446 return; 447 } 448 449 for (i = 0; i < ctx->Const.MaxTransformFeedbackBuffers; i++) { 450 if ((info->ActiveBuffers >> i) & 1) { 451 if (obj->BufferNames[i] == 0) { 452 _mesa_error(ctx, GL_INVALID_OPERATION, 453 "glBeginTransformFeedback(binding point %d does not " 454 "have a buffer object bound)", i); 455 return; 456 } 457 } 458 } 459 460 FLUSH_VERTICES(ctx, 0); 461 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback; 462 463 obj->Active = GL_TRUE; 464 ctx->TransformFeedback.Mode = mode; 465 466 compute_transform_feedback_buffer_sizes(obj); 467 468 if (_mesa_is_gles3(ctx)) { 469 /* In GLES3, we are required to track the usage of the transform 470 * feedback buffer and report INVALID_OPERATION if a draw call tries to 471 * exceed it. So compute the maximum number of vertices that we can 472 * write without overflowing any of the buffers currently being used for 473 * feedback. 474 */ 475 unsigned max_vertices 476 = _mesa_compute_max_transform_feedback_vertices(ctx, obj, info); 477 obj->GlesRemainingPrims = max_vertices / vertices_per_prim; 478 } 479 480 if (obj->program != source) { 481 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedbackProg; 482 obj->program = source; 483 } 484 485 assert(ctx->Driver.BeginTransformFeedback); 486 ctx->Driver.BeginTransformFeedback(ctx, mode, obj); 487 } 488 489 490 void GLAPIENTRY 491 _mesa_EndTransformFeedback(void) 492 { 493 struct gl_transform_feedback_object *obj; 494 GET_CURRENT_CONTEXT(ctx); 495 496 obj = ctx->TransformFeedback.CurrentObject; 497 498 if (!obj->Active) { 499 _mesa_error(ctx, GL_INVALID_OPERATION, 500 "glEndTransformFeedback(not active)"); 501 return; 502 } 503 504 FLUSH_VERTICES(ctx, 0); 505 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback; 506 507 assert(ctx->Driver.EndTransformFeedback); 508 ctx->Driver.EndTransformFeedback(ctx, obj); 509 510 ctx->TransformFeedback.CurrentObject->Active = GL_FALSE; 511 ctx->TransformFeedback.CurrentObject->Paused = GL_FALSE; 512 ctx->TransformFeedback.CurrentObject->EndedAnytime = GL_TRUE; 513 } 514 515 516 /** 517 * Helper used by BindBufferRange() and BindBufferBase(). 518 */ 519 static void 520 bind_buffer_range(struct gl_context *ctx, 521 struct gl_transform_feedback_object *obj, 522 GLuint index, 523 struct gl_buffer_object *bufObj, 524 GLintptr offset, GLsizeiptr size, 525 bool dsa) 526 { 527 /* Note: no need to FLUSH_VERTICES or flag NewTransformFeedback, because 528 * transform feedback buffers can't be changed while transform feedback is 529 * active. 530 */ 531 532 if (!dsa) { 533 /* The general binding point */ 534 _mesa_reference_buffer_object(ctx, 535 &ctx->TransformFeedback.CurrentBuffer, 536 bufObj); 537 } 538 539 /* The per-attribute binding point */ 540 _mesa_set_transform_feedback_binding(ctx, obj, index, bufObj, offset, size); 541 } 542 543 544 /** 545 * Specify a buffer object to receive transform feedback results. Plus, 546 * specify the starting offset to place the results, and max size. 547 * Called from the glBindBufferRange() and glTransformFeedbackBufferRange 548 * functions. 549 */ 550 void 551 _mesa_bind_buffer_range_transform_feedback(struct gl_context *ctx, 552 struct gl_transform_feedback_object *obj, 553 GLuint index, 554 struct gl_buffer_object *bufObj, 555 GLintptr offset, 556 GLsizeiptr size, 557 bool dsa) 558 { 559 const char *gl_methd_name; 560 if (dsa) 561 gl_methd_name = "glTransformFeedbackBufferRange"; 562 else 563 gl_methd_name = "glBindBufferRange"; 564 565 566 if (obj->Active) { 567 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(transform feedback active)", 568 gl_methd_name); 569 return; 570 } 571 572 if (index >= ctx->Const.MaxTransformFeedbackBuffers) { 573 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is 574 * generated if index is greater than or equal to the number of binding 575 * points for transform feedback, as described in section 6.7.1." 576 */ 577 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d out of bounds)", 578 gl_methd_name, index); 579 return; 580 } 581 582 if (size & 0x3) { 583 /* OpenGL 4.5 core profile, 6.7, pdf page 103: multiple of 4 */ 584 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d must be a multiple of " 585 "four)", gl_methd_name, (int) size); 586 return; 587 } 588 589 if (offset & 0x3) { 590 /* OpenGL 4.5 core profile, 6.7, pdf page 103: multiple of 4 */ 591 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset=%d must be a multiple " 592 "of four)", gl_methd_name, (int) offset); 593 return; 594 } 595 596 if (offset < 0) { 597 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is 598 * generated by BindBufferRange if offset is negative." 599 * 600 * OpenGL 4.5 core profile, 13.2, pdf page 445: "An INVALID_VALUE error 601 * is generated by TransformFeedbackBufferRange if offset is negative." 602 */ 603 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset=%d must be >= 0)", 604 gl_methd_name, 605 (int) offset); 606 return; 607 } 608 609 if (size <= 0 && (dsa || bufObj != ctx->Shared->NullBufferObj)) { 610 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is 611 * generated by BindBufferRange if buffer is non-zero and size is less 612 * than or equal to zero." 613 * 614 * OpenGL 4.5 core profile, 13.2, pdf page 445: "An INVALID_VALUE error 615 * is generated by TransformFeedbackBufferRange if size is less than or 616 * equal to zero." 617 */ 618 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d must be > 0)", 619 gl_methd_name, (int) size); 620 return; 621 } 622 623 bind_buffer_range(ctx, obj, index, bufObj, offset, size, dsa); 624 } 625 626 627 /** 628 * Specify a buffer object to receive transform feedback results. 629 * As above, but start at offset = 0. 630 * Called from the glBindBufferBase() and glTransformFeedbackBufferBase() 631 * functions. 632 */ 633 void 634 _mesa_bind_buffer_base_transform_feedback(struct gl_context *ctx, 635 struct gl_transform_feedback_object *obj, 636 GLuint index, 637 struct gl_buffer_object *bufObj, 638 bool dsa) 639 { 640 if (obj->Active) { 641 _mesa_error(ctx, GL_INVALID_OPERATION, 642 "%s(transform feedback active)", 643 dsa ? "glTransformFeedbackBufferBase" : "glBindBufferBase"); 644 return; 645 } 646 647 if (index >= ctx->Const.MaxTransformFeedbackBuffers) { 648 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d out of bounds)", 649 dsa ? "glTransformFeedbackBufferBase" : "glBindBufferBase", 650 index); 651 return; 652 } 653 654 bind_buffer_range(ctx, obj, index, bufObj, 0, 0, dsa); 655 } 656 657 /** 658 * Wrapper around lookup_transform_feedback_object that throws 659 * GL_INVALID_OPERATION if id is not in the hash table. After calling 660 * _mesa_error, it returns NULL. 661 */ 662 static struct gl_transform_feedback_object * 663 lookup_transform_feedback_object_err(struct gl_context *ctx, 664 GLuint xfb, const char* func) 665 { 666 struct gl_transform_feedback_object *obj; 667 668 obj = _mesa_lookup_transform_feedback_object(ctx, xfb); 669 if (!obj) { 670 _mesa_error(ctx, GL_INVALID_OPERATION, 671 "%s(xfb=%u: non-generated object name)", func, xfb); 672 } 673 674 return obj; 675 } 676 677 /** 678 * Wrapper around _mesa_lookup_bufferobj that throws GL_INVALID_VALUE if id 679 * is not in the hash table. Specialised version for the 680 * transform-feedback-related functions. After calling _mesa_error, it 681 * returns NULL. 682 */ 683 static struct gl_buffer_object * 684 lookup_transform_feedback_bufferobj_err(struct gl_context *ctx, 685 GLuint buffer, const char* func) 686 { 687 struct gl_buffer_object *bufObj; 688 689 /* OpenGL 4.5 core profile, 13.2, pdf page 444: buffer must be zero or the 690 * name of an existing buffer object. 691 */ 692 if (buffer == 0) { 693 bufObj = ctx->Shared->NullBufferObj; 694 } else { 695 bufObj = _mesa_lookup_bufferobj(ctx, buffer); 696 if (!bufObj) { 697 _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid buffer=%u)", func, 698 buffer); 699 } 700 } 701 702 return bufObj; 703 } 704 705 void GLAPIENTRY 706 _mesa_TransformFeedbackBufferBase(GLuint xfb, GLuint index, GLuint buffer) 707 { 708 GET_CURRENT_CONTEXT(ctx); 709 struct gl_transform_feedback_object *obj; 710 struct gl_buffer_object *bufObj; 711 712 obj = lookup_transform_feedback_object_err(ctx, xfb, 713 "glTransformFeedbackBufferBase"); 714 if(!obj) { 715 return; 716 } 717 718 bufObj = lookup_transform_feedback_bufferobj_err(ctx, buffer, 719 "glTransformFeedbackBufferBase"); 720 if(!bufObj) { 721 return; 722 } 723 724 _mesa_bind_buffer_base_transform_feedback(ctx, obj, index, bufObj, true); 725 } 726 727 void GLAPIENTRY 728 _mesa_TransformFeedbackBufferRange(GLuint xfb, GLuint index, GLuint buffer, 729 GLintptr offset, GLsizeiptr size) 730 { 731 GET_CURRENT_CONTEXT(ctx); 732 struct gl_transform_feedback_object *obj; 733 struct gl_buffer_object *bufObj; 734 735 obj = lookup_transform_feedback_object_err(ctx, xfb, 736 "glTransformFeedbackBufferRange"); 737 if(!obj) { 738 return; 739 } 740 741 bufObj = lookup_transform_feedback_bufferobj_err(ctx, buffer, 742 "glTransformFeedbackBufferRange"); 743 if(!bufObj) { 744 return; 745 } 746 747 _mesa_bind_buffer_range_transform_feedback(ctx, obj, index, bufObj, offset, 748 size, true); 749 } 750 751 /** 752 * Specify a buffer object to receive transform feedback results, plus the 753 * offset in the buffer to start placing results. 754 * This function is part of GL_EXT_transform_feedback, but not GL3. 755 */ 756 void GLAPIENTRY 757 _mesa_BindBufferOffsetEXT(GLenum target, GLuint index, GLuint buffer, 758 GLintptr offset) 759 { 760 struct gl_transform_feedback_object *obj; 761 struct gl_buffer_object *bufObj; 762 GET_CURRENT_CONTEXT(ctx); 763 764 if (target != GL_TRANSFORM_FEEDBACK_BUFFER) { 765 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferOffsetEXT(target)"); 766 return; 767 } 768 769 obj = ctx->TransformFeedback.CurrentObject; 770 771 if (obj->Active) { 772 _mesa_error(ctx, GL_INVALID_OPERATION, 773 "glBindBufferOffsetEXT(transform feedback active)"); 774 return; 775 } 776 777 if (index >= ctx->Const.MaxTransformFeedbackBuffers) { 778 _mesa_error(ctx, GL_INVALID_VALUE, 779 "glBindBufferOffsetEXT(index=%d)", index); 780 return; 781 } 782 783 if (offset & 0x3) { 784 /* must be multiple of four */ 785 _mesa_error(ctx, GL_INVALID_VALUE, 786 "glBindBufferOffsetEXT(offset=%d)", (int) offset); 787 return; 788 } 789 790 if (buffer == 0) { 791 bufObj = ctx->Shared->NullBufferObj; 792 } else { 793 bufObj = _mesa_lookup_bufferobj(ctx, buffer); 794 } 795 796 if (!bufObj) { 797 _mesa_error(ctx, GL_INVALID_OPERATION, 798 "glBindBufferOffsetEXT(invalid buffer=%u)", buffer); 799 return; 800 } 801 802 bind_buffer_range(ctx, obj, index, bufObj, offset, 0, false); 803 } 804 805 806 /** 807 * This function specifies the transform feedback outputs to be written 808 * to the feedback buffer(s), and in what order. 809 */ 810 void GLAPIENTRY 811 _mesa_TransformFeedbackVaryings(GLuint program, GLsizei count, 812 const GLchar * const *varyings, 813 GLenum bufferMode) 814 { 815 struct gl_shader_program *shProg; 816 GLint i; 817 GET_CURRENT_CONTEXT(ctx); 818 819 /* From the ARB_transform_feedback2 specification: 820 * "The error INVALID_OPERATION is generated by TransformFeedbackVaryings 821 * if the current transform feedback object is active, even if paused." 822 */ 823 if (ctx->TransformFeedback.CurrentObject->Active) { 824 _mesa_error(ctx, GL_INVALID_OPERATION, 825 "glTransformFeedbackVaryings(current object is active)"); 826 return; 827 } 828 829 switch (bufferMode) { 830 case GL_INTERLEAVED_ATTRIBS: 831 break; 832 case GL_SEPARATE_ATTRIBS: 833 break; 834 default: 835 _mesa_error(ctx, GL_INVALID_ENUM, 836 "glTransformFeedbackVaryings(bufferMode)"); 837 return; 838 } 839 840 if (count < 0 || 841 (bufferMode == GL_SEPARATE_ATTRIBS && 842 (GLuint) count > ctx->Const.MaxTransformFeedbackBuffers)) { 843 _mesa_error(ctx, GL_INVALID_VALUE, 844 "glTransformFeedbackVaryings(count=%d)", count); 845 return; 846 } 847 848 shProg = _mesa_lookup_shader_program_err(ctx, program, 849 "glTransformFeedbackVaryings"); 850 if (!shProg) 851 return; 852 853 if (ctx->Extensions.ARB_transform_feedback3) { 854 if (bufferMode == GL_INTERLEAVED_ATTRIBS) { 855 unsigned buffers = 1; 856 857 for (i = 0; i < count; i++) { 858 if (strcmp(varyings[i], "gl_NextBuffer") == 0) 859 buffers++; 860 } 861 862 if (buffers > ctx->Const.MaxTransformFeedbackBuffers) { 863 _mesa_error(ctx, GL_INVALID_OPERATION, 864 "glTransformFeedbackVaryings(too many gl_NextBuffer " 865 "occurrences)"); 866 return; 867 } 868 } else { 869 for (i = 0; i < count; i++) { 870 if (strcmp(varyings[i], "gl_NextBuffer") == 0 || 871 strcmp(varyings[i], "gl_SkipComponents1") == 0 || 872 strcmp(varyings[i], "gl_SkipComponents2") == 0 || 873 strcmp(varyings[i], "gl_SkipComponents3") == 0 || 874 strcmp(varyings[i], "gl_SkipComponents4") == 0) { 875 _mesa_error(ctx, GL_INVALID_OPERATION, 876 "glTransformFeedbackVaryings(SEPARATE_ATTRIBS," 877 "varying=%s)", 878 varyings[i]); 879 return; 880 } 881 } 882 } 883 } 884 885 /* free existing varyings, if any */ 886 for (i = 0; i < (GLint) shProg->TransformFeedback.NumVarying; i++) { 887 free(shProg->TransformFeedback.VaryingNames[i]); 888 } 889 free(shProg->TransformFeedback.VaryingNames); 890 891 /* allocate new memory for varying names */ 892 shProg->TransformFeedback.VaryingNames = 893 malloc(count * sizeof(GLchar *)); 894 895 if (!shProg->TransformFeedback.VaryingNames) { 896 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTransformFeedbackVaryings()"); 897 return; 898 } 899 900 /* Save the new names and the count */ 901 for (i = 0; i < count; i++) { 902 shProg->TransformFeedback.VaryingNames[i] = strdup(varyings[i]); 903 } 904 shProg->TransformFeedback.NumVarying = count; 905 906 shProg->TransformFeedback.BufferMode = bufferMode; 907 908 /* No need to invoke FLUSH_VERTICES or flag NewTransformFeedback since 909 * the varyings won't be used until shader link time. 910 */ 911 } 912 913 914 /** 915 * Get info about the transform feedback outputs which are to be written 916 * to the feedback buffer(s). 917 */ 918 void GLAPIENTRY 919 _mesa_GetTransformFeedbackVarying(GLuint program, GLuint index, 920 GLsizei bufSize, GLsizei *length, 921 GLsizei *size, GLenum *type, GLchar *name) 922 { 923 const struct gl_shader_program *shProg; 924 struct gl_program_resource *res; 925 GET_CURRENT_CONTEXT(ctx); 926 927 shProg = _mesa_lookup_shader_program_err(ctx, program, 928 "glGetTransformFeedbackVarying"); 929 if (!shProg) 930 return; 931 932 res = _mesa_program_resource_find_index((struct gl_shader_program *) shProg, 933 GL_TRANSFORM_FEEDBACK_VARYING, 934 index); 935 if (!res) { 936 _mesa_error(ctx, GL_INVALID_VALUE, 937 "glGetTransformFeedbackVarying(index=%u)", index); 938 return; 939 } 940 941 /* return the varying's name and length */ 942 _mesa_copy_string(name, bufSize, length, _mesa_program_resource_name(res)); 943 944 /* return the datatype and value's size (in datatype units) */ 945 if (type) 946 _mesa_program_resource_prop((struct gl_shader_program *) shProg, 947 res, index, GL_TYPE, (GLint*) type, 948 "glGetTransformFeedbackVarying"); 949 if (size) 950 _mesa_program_resource_prop((struct gl_shader_program *) shProg, 951 res, index, GL_ARRAY_SIZE, (GLint*) size, 952 "glGetTransformFeedbackVarying"); 953 } 954 955 956 957 struct gl_transform_feedback_object * 958 _mesa_lookup_transform_feedback_object(struct gl_context *ctx, GLuint name) 959 { 960 /* OpenGL 4.5 core profile, 13.2 pdf page 444: "xfb must be zero, indicating 961 * the default transform feedback object, or the name of an existing 962 * transform feedback object." 963 */ 964 if (name == 0) { 965 return ctx->TransformFeedback.DefaultObject; 966 } 967 else 968 return (struct gl_transform_feedback_object *) 969 _mesa_HashLookup(ctx->TransformFeedback.Objects, name); 970 } 971 972 static void 973 create_transform_feedbacks(struct gl_context *ctx, GLsizei n, GLuint *ids, 974 bool dsa) 975 { 976 GLuint first; 977 const char* func; 978 979 if (dsa) 980 func = "glCreateTransformFeedbacks"; 981 else 982 func = "glGenTransformFeedbacks"; 983 984 if (n < 0) { 985 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", func); 986 return; 987 } 988 989 if (!ids) 990 return; 991 992 /* we don't need contiguous IDs, but this might be faster */ 993 first = _mesa_HashFindFreeKeyBlock(ctx->TransformFeedback.Objects, n); 994 if (first) { 995 GLsizei i; 996 for (i = 0; i < n; i++) { 997 struct gl_transform_feedback_object *obj 998 = ctx->Driver.NewTransformFeedback(ctx, first + i); 999 if (!obj) { 1000 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func); 1001 return; 1002 } 1003 ids[i] = first + i; 1004 _mesa_HashInsert(ctx->TransformFeedback.Objects, first + i, obj); 1005 if (dsa) { 1006 /* this is normally done at bind time in the non-dsa case */ 1007 obj->EverBound = GL_TRUE; 1008 } 1009 } 1010 } 1011 else { 1012 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func); 1013 } 1014 } 1015 1016 /** 1017 * Create new transform feedback objects. Transform feedback objects 1018 * encapsulate the state related to transform feedback to allow quickly 1019 * switching state (and drawing the results, below). 1020 * Part of GL_ARB_transform_feedback2. 1021 */ 1022 void GLAPIENTRY 1023 _mesa_GenTransformFeedbacks(GLsizei n, GLuint *names) 1024 { 1025 GET_CURRENT_CONTEXT(ctx); 1026 1027 /* GenTransformFeedbacks should just reserve the object names that a 1028 * subsequent call to BindTransformFeedback should actively create. For 1029 * the sake of simplicity, we reserve the names and create the objects 1030 * straight away. 1031 */ 1032 1033 create_transform_feedbacks(ctx, n, names, false); 1034 } 1035 1036 /** 1037 * Create new transform feedback objects. Transform feedback objects 1038 * encapsulate the state related to transform feedback to allow quickly 1039 * switching state (and drawing the results, below). 1040 * Part of GL_ARB_direct_state_access. 1041 */ 1042 void GLAPIENTRY 1043 _mesa_CreateTransformFeedbacks(GLsizei n, GLuint *names) 1044 { 1045 GET_CURRENT_CONTEXT(ctx); 1046 1047 create_transform_feedbacks(ctx, n, names, true); 1048 } 1049 1050 1051 /** 1052 * Is the given ID a transform feedback object? 1053 * Part of GL_ARB_transform_feedback2. 1054 */ 1055 GLboolean GLAPIENTRY 1056 _mesa_IsTransformFeedback(GLuint name) 1057 { 1058 struct gl_transform_feedback_object *obj; 1059 GET_CURRENT_CONTEXT(ctx); 1060 1061 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE); 1062 1063 if (name == 0) 1064 return GL_FALSE; 1065 1066 obj = _mesa_lookup_transform_feedback_object(ctx, name); 1067 if (obj == NULL) 1068 return GL_FALSE; 1069 1070 return obj->EverBound; 1071 } 1072 1073 1074 /** 1075 * Bind the given transform feedback object. 1076 * Part of GL_ARB_transform_feedback2. 1077 */ 1078 void GLAPIENTRY 1079 _mesa_BindTransformFeedback(GLenum target, GLuint name) 1080 { 1081 struct gl_transform_feedback_object *obj; 1082 GET_CURRENT_CONTEXT(ctx); 1083 1084 if (target != GL_TRANSFORM_FEEDBACK) { 1085 _mesa_error(ctx, GL_INVALID_ENUM, "glBindTransformFeedback(target)"); 1086 return; 1087 } 1088 1089 if (_mesa_is_xfb_active_and_unpaused(ctx)) { 1090 _mesa_error(ctx, GL_INVALID_OPERATION, 1091 "glBindTransformFeedback(transform is active, or not paused)"); 1092 return; 1093 } 1094 1095 obj = _mesa_lookup_transform_feedback_object(ctx, name); 1096 if (!obj) { 1097 _mesa_error(ctx, GL_INVALID_OPERATION, 1098 "glBindTransformFeedback(name=%u)", name); 1099 return; 1100 } 1101 1102 reference_transform_feedback_object(&ctx->TransformFeedback.CurrentObject, 1103 obj); 1104 } 1105 1106 1107 /** 1108 * Delete the given transform feedback objects. 1109 * Part of GL_ARB_transform_feedback2. 1110 */ 1111 void GLAPIENTRY 1112 _mesa_DeleteTransformFeedbacks(GLsizei n, const GLuint *names) 1113 { 1114 GLint i; 1115 GET_CURRENT_CONTEXT(ctx); 1116 1117 if (n < 0) { 1118 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteTransformFeedbacks(n < 0)"); 1119 return; 1120 } 1121 1122 if (!names) 1123 return; 1124 1125 for (i = 0; i < n; i++) { 1126 if (names[i] > 0) { 1127 struct gl_transform_feedback_object *obj 1128 = _mesa_lookup_transform_feedback_object(ctx, names[i]); 1129 if (obj) { 1130 if (obj->Active) { 1131 _mesa_error(ctx, GL_INVALID_OPERATION, 1132 "glDeleteTransformFeedbacks(object %u is active)", 1133 names[i]); 1134 return; 1135 } 1136 _mesa_HashRemove(ctx->TransformFeedback.Objects, names[i]); 1137 /* unref, but object may not be deleted until later */ 1138 if (obj == ctx->TransformFeedback.CurrentObject) { 1139 reference_transform_feedback_object( 1140 &ctx->TransformFeedback.CurrentObject, 1141 ctx->TransformFeedback.DefaultObject); 1142 } 1143 reference_transform_feedback_object(&obj, NULL); 1144 } 1145 } 1146 } 1147 } 1148 1149 1150 /** 1151 * Pause transform feedback. 1152 * Part of GL_ARB_transform_feedback2. 1153 */ 1154 void GLAPIENTRY 1155 _mesa_PauseTransformFeedback(void) 1156 { 1157 struct gl_transform_feedback_object *obj; 1158 GET_CURRENT_CONTEXT(ctx); 1159 1160 obj = ctx->TransformFeedback.CurrentObject; 1161 1162 if (!_mesa_is_xfb_active_and_unpaused(ctx)) { 1163 _mesa_error(ctx, GL_INVALID_OPERATION, 1164 "glPauseTransformFeedback(feedback not active or already paused)"); 1165 return; 1166 } 1167 1168 FLUSH_VERTICES(ctx, 0); 1169 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback; 1170 1171 assert(ctx->Driver.PauseTransformFeedback); 1172 ctx->Driver.PauseTransformFeedback(ctx, obj); 1173 1174 obj->Paused = GL_TRUE; 1175 } 1176 1177 1178 /** 1179 * Resume transform feedback. 1180 * Part of GL_ARB_transform_feedback2. 1181 */ 1182 void GLAPIENTRY 1183 _mesa_ResumeTransformFeedback(void) 1184 { 1185 struct gl_transform_feedback_object *obj; 1186 GET_CURRENT_CONTEXT(ctx); 1187 1188 obj = ctx->TransformFeedback.CurrentObject; 1189 1190 if (!obj->Active || !obj->Paused) { 1191 _mesa_error(ctx, GL_INVALID_OPERATION, 1192 "glResumeTransformFeedback(feedback not active or not paused)"); 1193 return; 1194 } 1195 1196 /* From the ARB_transform_feedback2 specification: 1197 * "The error INVALID_OPERATION is generated by ResumeTransformFeedback if 1198 * the program object being used by the current transform feedback object 1199 * is not active." 1200 */ 1201 if (obj->program != get_xfb_source(ctx)) { 1202 _mesa_error(ctx, GL_INVALID_OPERATION, 1203 "glResumeTransformFeedback(wrong program bound)"); 1204 return; 1205 } 1206 1207 FLUSH_VERTICES(ctx, 0); 1208 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback; 1209 1210 obj->Paused = GL_FALSE; 1211 1212 assert(ctx->Driver.ResumeTransformFeedback); 1213 ctx->Driver.ResumeTransformFeedback(ctx, obj); 1214 } 1215 1216 extern void GLAPIENTRY 1217 _mesa_GetTransformFeedbackiv(GLuint xfb, GLenum pname, GLint *param) 1218 { 1219 struct gl_transform_feedback_object *obj; 1220 GET_CURRENT_CONTEXT(ctx); 1221 1222 obj = lookup_transform_feedback_object_err(ctx, xfb, 1223 "glGetTransformFeedbackiv"); 1224 if(!obj) { 1225 return; 1226 } 1227 1228 switch(pname) { 1229 case GL_TRANSFORM_FEEDBACK_PAUSED: 1230 *param = obj->Paused; 1231 break; 1232 case GL_TRANSFORM_FEEDBACK_ACTIVE: 1233 *param = obj->Active; 1234 break; 1235 default: 1236 _mesa_error(ctx, GL_INVALID_ENUM, 1237 "glGetTransformFeedbackiv(pname=%i)", pname); 1238 } 1239 } 1240 1241 extern void GLAPIENTRY 1242 _mesa_GetTransformFeedbacki_v(GLuint xfb, GLenum pname, GLuint index, 1243 GLint *param) 1244 { 1245 struct gl_transform_feedback_object *obj; 1246 GET_CURRENT_CONTEXT(ctx); 1247 1248 obj = lookup_transform_feedback_object_err(ctx, xfb, 1249 "glGetTransformFeedbacki_v"); 1250 if(!obj) { 1251 return; 1252 } 1253 1254 if (index >= ctx->Const.MaxTransformFeedbackBuffers) { 1255 _mesa_error(ctx, GL_INVALID_VALUE, 1256 "glGetTransformFeedbacki_v(index=%i)", index); 1257 return; 1258 } 1259 1260 switch(pname) { 1261 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING: 1262 *param = obj->BufferNames[index]; 1263 break; 1264 default: 1265 _mesa_error(ctx, GL_INVALID_ENUM, 1266 "glGetTransformFeedbacki_v(pname=%i)", pname); 1267 } 1268 } 1269 1270 extern void GLAPIENTRY 1271 _mesa_GetTransformFeedbacki64_v(GLuint xfb, GLenum pname, GLuint index, 1272 GLint64 *param) 1273 { 1274 struct gl_transform_feedback_object *obj; 1275 GET_CURRENT_CONTEXT(ctx); 1276 1277 obj = lookup_transform_feedback_object_err(ctx, xfb, 1278 "glGetTransformFeedbacki64_v"); 1279 if(!obj) { 1280 return; 1281 } 1282 1283 if (index >= ctx->Const.MaxTransformFeedbackBuffers) { 1284 _mesa_error(ctx, GL_INVALID_VALUE, 1285 "glGetTransformFeedbacki64_v(index=%i)", index); 1286 return; 1287 } 1288 1289 compute_transform_feedback_buffer_sizes(obj); 1290 switch(pname) { 1291 case GL_TRANSFORM_FEEDBACK_BUFFER_START: 1292 *param = obj->Offset[index]; 1293 break; 1294 case GL_TRANSFORM_FEEDBACK_BUFFER_SIZE: 1295 *param = obj->Size[index]; 1296 break; 1297 default: 1298 _mesa_error(ctx, GL_INVALID_ENUM, 1299 "glGetTransformFeedbacki64_v(pname=%i)", pname); 1300 } 1301 } 1302