1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
|
/*
* Mesa 3-D graphics library
*
* Copyright (C) 2010 VMware, Inc. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* Transform feedback support.
*
* Authors:
* Brian Paul
*/
#include "buffers.h"
#include "context.h"
#include "hash.h"
#include "macros.h"
#include "mtypes.h"
#include "transformfeedback.h"
#include "shaderapi.h"
#include "shaderobj.h"
#include "main/dispatch.h"
#include "program/prog_parameter.h"
struct using_program_tuple
{
struct gl_shader_program *shProg;
bool found;
};
static void
active_xfb_object_references_program(GLuint key, void *data, void *user_data)
{
struct using_program_tuple *callback_data = user_data;
struct gl_transform_feedback_object *obj = data;
if (obj->Active && obj->shader_program == callback_data->shProg)
callback_data->found = true;
}
/**
* Return true if any active transform feedback object is using a program.
*/
bool
_mesa_transform_feedback_is_using_program(struct gl_context *ctx,
struct gl_shader_program *shProg)
{
struct using_program_tuple callback_data;
callback_data.shProg = shProg;
callback_data.found = false;
_mesa_HashWalk(ctx->TransformFeedback.Objects,
active_xfb_object_references_program, &callback_data);
/* Also check DefaultObject, as it's not in the Objects hash table. */
active_xfb_object_references_program(0, ctx->TransformFeedback.DefaultObject,
&callback_data);
return callback_data.found;
}
/**
* Do reference counting of transform feedback buffers.
*/
static void
reference_transform_feedback_object(struct gl_transform_feedback_object **ptr,
struct gl_transform_feedback_object *obj)
{
if (*ptr == obj)
return;
if (*ptr) {
/* Unreference the old object */
struct gl_transform_feedback_object *oldObj = *ptr;
assert(oldObj->RefCount > 0);
oldObj->RefCount--;
if (oldObj->RefCount == 0) {
GET_CURRENT_CONTEXT(ctx);
if (ctx)
ctx->Driver.DeleteTransformFeedback(ctx, oldObj);
}
*ptr = NULL;
}
assert(!*ptr);
if (obj) {
/* reference new object */
if (obj->RefCount == 0) {
_mesa_problem(NULL, "referencing deleted transform feedback object");
*ptr = NULL;
}
else {
obj->RefCount++;
obj->EverBound = GL_TRUE;
*ptr = obj;
}
}
}
/**
* Check that all the buffer objects currently bound for transform
* feedback actually exist. Raise a GL_INVALID_OPERATION error if
* any buffers are missing.
* \return GL_TRUE for success, GL_FALSE if error
*/
GLboolean
_mesa_validate_transform_feedback_buffers(struct gl_context *ctx)
{
/* XXX to do */
return GL_TRUE;
}
/**
* Per-context init for transform feedback.
*/
void
_mesa_init_transform_feedback(struct gl_context *ctx)
{
/* core mesa expects this, even a dummy one, to be available */
assert(ctx->Driver.NewTransformFeedback);
ctx->TransformFeedback.DefaultObject =
ctx->Driver.NewTransformFeedback(ctx, 0);
assert(ctx->TransformFeedback.DefaultObject->RefCount == 1);
reference_transform_feedback_object(&ctx->TransformFeedback.CurrentObject,
ctx->TransformFeedback.DefaultObject);
assert(ctx->TransformFeedback.DefaultObject->RefCount == 2);
ctx->TransformFeedback.Objects = _mesa_NewHashTable();
_mesa_reference_buffer_object(ctx,
&ctx->TransformFeedback.CurrentBuffer,
ctx->Shared->NullBufferObj);
}
/**
* Callback for _mesa_HashDeleteAll().
*/
static void
delete_cb(GLuint key, void *data, void *userData)
{
struct gl_context *ctx = (struct gl_context *) userData;
struct gl_transform_feedback_object *obj =
(struct gl_transform_feedback_object *) data;
ctx->Driver.DeleteTransformFeedback(ctx, obj);
}
/**
* Per-context free/clean-up for transform feedback.
*/
void
_mesa_free_transform_feedback(struct gl_context *ctx)
{
/* core mesa expects this, even a dummy one, to be available */
assert(ctx->Driver.NewTransformFeedback);
_mesa_reference_buffer_object(ctx,
&ctx->TransformFeedback.CurrentBuffer,
NULL);
/* Delete all feedback objects */
_mesa_HashDeleteAll(ctx->TransformFeedback.Objects, delete_cb, ctx);
_mesa_DeleteHashTable(ctx->TransformFeedback.Objects);
/* Delete the default feedback object */
assert(ctx->Driver.DeleteTransformFeedback);
ctx->Driver.DeleteTransformFeedback(ctx,
ctx->TransformFeedback.DefaultObject);
ctx->TransformFeedback.CurrentObject = NULL;
}
/** Initialize the fields of a gl_transform_feedback_object. */
void
_mesa_init_transform_feedback_object(struct gl_transform_feedback_object *obj,
GLuint name)
{
if (!obj)
return;
obj->Name = name;
obj->RefCount = 1;
obj->EverBound = GL_FALSE;
}
/** Default fallback for ctx->Driver.NewTransformFeedback() */
static struct gl_transform_feedback_object *
new_transform_feedback(struct gl_context *ctx, GLuint name)
{
struct gl_transform_feedback_object *obj;
obj = CALLOC_STRUCT(gl_transform_feedback_object);
_mesa_init_transform_feedback_object(obj, name);
return obj;
}
/** Default fallback for ctx->Driver.DeleteTransformFeedback() */
static void
delete_transform_feedback(struct gl_context *ctx,
struct gl_transform_feedback_object *obj)
{
GLuint i;
for (i = 0; i < ARRAY_SIZE(obj->Buffers); i++) {
_mesa_reference_buffer_object(ctx, &obj->Buffers[i], NULL);
}
free(obj->Label);
free(obj);
}
/** Default fallback for ctx->Driver.BeginTransformFeedback() */
static void
begin_transform_feedback(struct gl_context *ctx, GLenum mode,
struct gl_transform_feedback_object *obj)
{
/* nop */
}
/** Default fallback for ctx->Driver.EndTransformFeedback() */
static void
end_transform_feedback(struct gl_context *ctx,
struct gl_transform_feedback_object *obj)
{
/* nop */
}
/** Default fallback for ctx->Driver.PauseTransformFeedback() */
static void
pause_transform_feedback(struct gl_context *ctx,
struct gl_transform_feedback_object *obj)
{
/* nop */
}
/** Default fallback for ctx->Driver.ResumeTransformFeedback() */
static void
resume_transform_feedback(struct gl_context *ctx,
struct gl_transform_feedback_object *obj)
{
/* nop */
}
/**
* Plug in default device driver functions for transform feedback.
* Most drivers will override some/all of these.
*/
void
_mesa_init_transform_feedback_functions(struct dd_function_table *driver)
{
driver->NewTransformFeedback = new_transform_feedback;
driver->DeleteTransformFeedback = delete_transform_feedback;
driver->BeginTransformFeedback = begin_transform_feedback;
driver->EndTransformFeedback = end_transform_feedback;
driver->PauseTransformFeedback = pause_transform_feedback;
driver->ResumeTransformFeedback = resume_transform_feedback;
}
/**
* Fill in the correct Size value for each buffer in \c obj.
*
* From the GL 4.3 spec, section 6.1.1 ("Binding Buffer Objects to Indexed
* Targets"):
*
* BindBufferBase binds the entire buffer, even when the size of the buffer
* is changed after the binding is established. It is equivalent to calling
* BindBufferRange with offset zero, while size is determined by the size of
* the bound buffer at the time the binding is used.
*
* Regardless of the size specified with BindBufferRange, or indirectly with
* BindBufferBase, the GL will never read or write beyond the end of a bound
* buffer. In some cases this constraint may result in visibly different
* behavior when a buffer overflow would otherwise result, such as described
* for transform feedback operations in section 13.2.2.
*/
static void
compute_transform_feedback_buffer_sizes(
struct gl_transform_feedback_object *obj)
{
unsigned i = 0;
for (i = 0; i < MAX_FEEDBACK_BUFFERS; ++i) {
GLintptr offset = obj->Offset[i];
GLsizeiptr buffer_size
= obj->Buffers[i] == NULL ? 0 : obj->Buffers[i]->Size;
GLsizeiptr available_space
= buffer_size <= offset ? 0 : buffer_size - offset;
GLsizeiptr computed_size;
if (obj->RequestedSize[i] == 0) {
/* No size was specified at the time the buffer was bound, so allow
* writing to all available space in the buffer.
*/
computed_size = available_space;
} else {
/* A size was specified at the time the buffer was bound, however
* it's possible that the buffer has shrunk since then. So only
* allow writing to the minimum of the specified size and the space
* available.
*/
computed_size = MIN2(available_space, obj->RequestedSize[i]);
}
/* Legal sizes must be multiples of four, so round down if necessary. */
obj->Size[i] = computed_size & ~0x3;
}
}
/**
* Compute the maximum number of vertices that can be written to the currently
* enabled transform feedback buffers without overflowing any of them.
*/
unsigned
_mesa_compute_max_transform_feedback_vertices(
const struct gl_transform_feedback_object *obj,
const struct gl_transform_feedback_info *info)
{
unsigned max_index = 0xffffffff;
unsigned i;
for (i = 0; i < info->NumBuffers; ++i) {
unsigned stride = info->BufferStride[i];
unsigned max_for_this_buffer;
/* Skip any inactive buffers, which have a stride of 0. */
if (stride == 0)
continue;
max_for_this_buffer = obj->Size[i] / (4 * stride);
max_index = MIN2(max_index, max_for_this_buffer);
}
return max_index;
}
/**
** Begin API functions
**/
/**
* Figure out which stage of the pipeline is the source of transform feedback
* data given the current context state, and return its gl_shader_program.
*
* If no active program can generate transform feedback data (i.e. no vertex
* shader is active), returns NULL.
*/
static struct gl_shader_program *
get_xfb_source(struct gl_context *ctx)
{
int i;
for (i = MESA_SHADER_GEOMETRY; i >= MESA_SHADER_VERTEX; i--) {
if (ctx->_Shader->CurrentProgram[i] != NULL)
return ctx->_Shader->CurrentProgram[i];
}
return NULL;
}
void GLAPIENTRY
_mesa_BeginTransformFeedback(GLenum mode)
{
struct gl_transform_feedback_object *obj;
struct gl_transform_feedback_info *info = NULL;
struct gl_shader_program *source;
GLuint i;
unsigned vertices_per_prim;
GET_CURRENT_CONTEXT(ctx);
obj = ctx->TransformFeedback.CurrentObject;
/* Figure out what pipeline stage is the source of data for transform
* feedback.
*/
source = get_xfb_source(ctx);
if (source == NULL) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glBeginTransformFeedback(no program active)");
return;
}
info = &source->LinkedTransformFeedback;
if (info->NumOutputs == 0) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glBeginTransformFeedback(no varyings to record)");
return;
}
switch (mode) {
case GL_POINTS:
vertices_per_prim = 1;
break;
case GL_LINES:
vertices_per_prim = 2;
break;
case GL_TRIANGLES:
vertices_per_prim = 3;
break;
default:
_mesa_error(ctx, GL_INVALID_ENUM, "glBeginTransformFeedback(mode)");
return;
}
if (obj->Active) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glBeginTransformFeedback(already active)");
return;
}
for (i = 0; i < info->NumBuffers; ++i) {
if (obj->BufferNames[i] == 0) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glBeginTransformFeedback(binding point %d does not have "
"a buffer object bound)", i);
return;
}
}
FLUSH_VERTICES(ctx, 0);
ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
obj->Active = GL_TRUE;
ctx->TransformFeedback.Mode = mode;
compute_transform_feedback_buffer_sizes(obj);
if (_mesa_is_gles3(ctx)) {
/* In GLES3, we are required to track the usage of the transform
* feedback buffer and report INVALID_OPERATION if a draw call tries to
* exceed it. So compute the maximum number of vertices that we can
* write without overflowing any of the buffers currently being used for
* feedback.
*/
unsigned max_vertices
= _mesa_compute_max_transform_feedback_vertices(obj, info);
obj->GlesRemainingPrims = max_vertices / vertices_per_prim;
}
if (obj->shader_program != source) {
ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedbackProg;
obj->shader_program = source;
}
assert(ctx->Driver.BeginTransformFeedback);
ctx->Driver.BeginTransformFeedback(ctx, mode, obj);
}
void GLAPIENTRY
_mesa_EndTransformFeedback(void)
{
struct gl_transform_feedback_object *obj;
GET_CURRENT_CONTEXT(ctx);
obj = ctx->TransformFeedback.CurrentObject;
if (!obj->Active) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glEndTransformFeedback(not active)");
return;
}
FLUSH_VERTICES(ctx, 0);
ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
ctx->TransformFeedback.CurrentObject->Active = GL_FALSE;
ctx->TransformFeedback.CurrentObject->Paused = GL_FALSE;
ctx->TransformFeedback.CurrentObject->EndedAnytime = GL_TRUE;
assert(ctx->Driver.EndTransformFeedback);
ctx->Driver.EndTransformFeedback(ctx, obj);
}
/**
* Helper used by BindBufferRange() and BindBufferBase().
*/
static void
bind_buffer_range(struct gl_context *ctx,
struct gl_transform_feedback_object *obj,
GLuint index,
struct gl_buffer_object *bufObj,
GLintptr offset, GLsizeiptr size,
bool dsa)
{
/* Note: no need to FLUSH_VERTICES or flag NewTransformFeedback, because
* transform feedback buffers can't be changed while transform feedback is
* active.
*/
if (!dsa) {
/* The general binding point */
_mesa_reference_buffer_object(ctx,
&ctx->TransformFeedback.CurrentBuffer,
bufObj);
}
/* The per-attribute binding point */
_mesa_set_transform_feedback_binding(ctx, obj, index, bufObj, offset, size);
}
/**
* Specify a buffer object to receive transform feedback results. Plus,
* specify the starting offset to place the results, and max size.
* Called from the glBindBufferRange() and glTransformFeedbackBufferRange
* functions.
*/
void
_mesa_bind_buffer_range_transform_feedback(struct gl_context *ctx,
struct gl_transform_feedback_object *obj,
GLuint index,
struct gl_buffer_object *bufObj,
GLintptr offset,
GLsizeiptr size,
bool dsa)
{
const char *gl_methd_name;
if (dsa)
gl_methd_name = "glTransformFeedbackBufferRange";
else
gl_methd_name = "glBindBufferRange";
if (obj->Active) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s(transform feedback active)",
gl_methd_name);
return;
}
if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
/* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
* generated if index is greater than or equal to the number of binding
* points for transform feedback, as described in section 6.7.1."
*/
_mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d out of bounds)",
gl_methd_name, index);
return;
}
if (size & 0x3) {
/* OpenGL 4.5 core profile, 6.7, pdf page 103: multiple of 4 */
_mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d must be a multiple of "
"four)", gl_methd_name, (int) size);
return;
}
if (offset & 0x3) {
/* OpenGL 4.5 core profile, 6.7, pdf page 103: multiple of 4 */
_mesa_error(ctx, GL_INVALID_VALUE, "%s(offset=%d must be a multiple "
"of four)", gl_methd_name, (int) offset);
return;
}
if (offset < 0) {
/* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
* generated by BindBufferRange if offset is negative."
*
* OpenGL 4.5 core profile, 13.2, pdf page 445: "An INVALID_VALUE error
* is generated by TransformFeedbackBufferRange if offset is negative."
*/
_mesa_error(ctx, GL_INVALID_VALUE, "%s(offset=%d must be >= 0)",
gl_methd_name,
(int) offset);
return;
}
if (size <= 0 && (dsa || bufObj != ctx->Shared->NullBufferObj)) {
/* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
* generated by BindBufferRange if buffer is non-zero and size is less
* than or equal to zero."
*
* OpenGL 4.5 core profile, 13.2, pdf page 445: "An INVALID_VALUE error
* is generated by TransformFeedbackBufferRange if size is less than or
* equal to zero."
*/
_mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d must be > 0)",
gl_methd_name, (int) size);
return;
}
bind_buffer_range(ctx, obj, index, bufObj, offset, size, dsa);
}
/**
* Specify a buffer object to receive transform feedback results.
* As above, but start at offset = 0.
* Called from the glBindBufferBase() and glTransformFeedbackBufferBase()
* functions.
*/
void
_mesa_bind_buffer_base_transform_feedback(struct gl_context *ctx,
struct gl_transform_feedback_object *obj,
GLuint index,
struct gl_buffer_object *bufObj,
bool dsa)
{
if (obj->Active) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"%s(transform feedback active)",
dsa ? "glTransformFeedbackBufferBase" : "glBindBufferBase");
return;
}
if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
_mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d out of bounds)",
dsa ? "glTransformFeedbackBufferBase" : "glBindBufferBase",
index);
return;
}
bind_buffer_range(ctx, obj, index, bufObj, 0, 0, dsa);
}
/**
* Wrapper around lookup_transform_feedback_object that throws
* GL_INVALID_OPERATION if id is not in the hash table. After calling
* _mesa_error, it returns NULL.
*/
static struct gl_transform_feedback_object *
lookup_transform_feedback_object_err(struct gl_context *ctx,
GLuint xfb, const char* func)
{
struct gl_transform_feedback_object *obj;
obj = _mesa_lookup_transform_feedback_object(ctx, xfb);
if (!obj) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"%s(xfb=%u: non-generated object name)", func, xfb);
}
return obj;
}
/**
* Wrapper around _mesa_lookup_bufferobj that throws GL_INVALID_VALUE if id
* is not in the hash table. Specialised version for the
* transform-feedback-related functions. After calling _mesa_error, it
* returns NULL.
*/
static struct gl_buffer_object *
lookup_transform_feedback_bufferobj_err(struct gl_context *ctx,
GLuint buffer, const char* func)
{
struct gl_buffer_object *bufObj;
/* OpenGL 4.5 core profile, 13.2, pdf page 444: buffer must be zero or the
* name of an existing buffer object.
*/
if (buffer == 0) {
bufObj = ctx->Shared->NullBufferObj;
} else {
bufObj = _mesa_lookup_bufferobj(ctx, buffer);
if (!bufObj) {
_mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid buffer=%u)", func,
buffer);
}
}
return bufObj;
}
void GLAPIENTRY
_mesa_TransformFeedbackBufferBase(GLuint xfb, GLuint index, GLuint buffer)
{
GET_CURRENT_CONTEXT(ctx);
struct gl_transform_feedback_object *obj;
struct gl_buffer_object *bufObj;
obj = lookup_transform_feedback_object_err(ctx, xfb,
"glTransformFeedbackBufferBase");
if(!obj) {
return;
}
bufObj = lookup_transform_feedback_bufferobj_err(ctx, buffer,
"glTransformFeedbackBufferBase");
if(!bufObj) {
return;
}
_mesa_bind_buffer_base_transform_feedback(ctx, obj, index, bufObj, true);
}
void GLAPIENTRY
_mesa_TransformFeedbackBufferRange(GLuint xfb, GLuint index, GLuint buffer,
GLintptr offset, GLsizeiptr size)
{
GET_CURRENT_CONTEXT(ctx);
struct gl_transform_feedback_object *obj;
struct gl_buffer_object *bufObj;
obj = lookup_transform_feedback_object_err(ctx, xfb,
"glTransformFeedbackBufferRange");
if(!obj) {
return;
}
bufObj = lookup_transform_feedback_bufferobj_err(ctx, buffer,
"glTransformFeedbackBufferRange");
if(!bufObj) {
return;
}
_mesa_bind_buffer_range_transform_feedback(ctx, obj, index, bufObj, offset,
size, true);
}
/**
* Specify a buffer object to receive transform feedback results, plus the
* offset in the buffer to start placing results.
* This function is part of GL_EXT_transform_feedback, but not GL3.
*/
void GLAPIENTRY
_mesa_BindBufferOffsetEXT(GLenum target, GLuint index, GLuint buffer,
GLintptr offset)
{
struct gl_transform_feedback_object *obj;
struct gl_buffer_object *bufObj;
GET_CURRENT_CONTEXT(ctx);
if (target != GL_TRANSFORM_FEEDBACK_BUFFER) {
_mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferOffsetEXT(target)");
return;
}
obj = ctx->TransformFeedback.CurrentObject;
if (obj->Active) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glBindBufferOffsetEXT(transform feedback active)");
return;
}
if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
_mesa_error(ctx, GL_INVALID_VALUE,
"glBindBufferOffsetEXT(index=%d)", index);
return;
}
if (offset & 0x3) {
/* must be multiple of four */
_mesa_error(ctx, GL_INVALID_VALUE,
"glBindBufferOffsetEXT(offset=%d)", (int) offset);
return;
}
if (buffer == 0) {
bufObj = ctx->Shared->NullBufferObj;
} else {
bufObj = _mesa_lookup_bufferobj(ctx, buffer);
}
if (!bufObj) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glBindBufferOffsetEXT(invalid buffer=%u)", buffer);
return;
}
bind_buffer_range(ctx, obj, index, bufObj, offset, 0, false);
}
/**
* This function specifies the transform feedback outputs to be written
* to the feedback buffer(s), and in what order.
*/
void GLAPIENTRY
_mesa_TransformFeedbackVaryings(GLuint program, GLsizei count,
const GLchar * const *varyings,
GLenum bufferMode)
{
struct gl_shader_program *shProg;
GLint i;
GET_CURRENT_CONTEXT(ctx);
/* From the ARB_transform_feedback2 specification:
* "The error INVALID_OPERATION is generated by TransformFeedbackVaryings
* if the current transform feedback object is active, even if paused."
*/
if (ctx->TransformFeedback.CurrentObject->Active) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glTransformFeedbackVaryings(current object is active)");
return;
}
switch (bufferMode) {
case GL_INTERLEAVED_ATTRIBS:
break;
case GL_SEPARATE_ATTRIBS:
break;
default:
_mesa_error(ctx, GL_INVALID_ENUM,
"glTransformFeedbackVaryings(bufferMode)");
return;
}
if (count < 0 ||
(bufferMode == GL_SEPARATE_ATTRIBS &&
(GLuint) count > ctx->Const.MaxTransformFeedbackBuffers)) {
_mesa_error(ctx, GL_INVALID_VALUE,
"glTransformFeedbackVaryings(count=%d)", count);
return;
}
shProg = _mesa_lookup_shader_program(ctx, program);
if (!shProg) {
_mesa_error(ctx, GL_INVALID_VALUE,
"glTransformFeedbackVaryings(program=%u)", program);
return;
}
if (ctx->Extensions.ARB_transform_feedback3) {
if (bufferMode == GL_INTERLEAVED_ATTRIBS) {
unsigned buffers = 1;
for (i = 0; i < count; i++) {
if (strcmp(varyings[i], "gl_NextBuffer") == 0)
buffers++;
}
if (buffers > ctx->Const.MaxTransformFeedbackBuffers) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glTransformFeedbackVaryings(too many gl_NextBuffer "
"occurences)");
return;
}
} else {
for (i = 0; i < count; i++) {
if (strcmp(varyings[i], "gl_NextBuffer") == 0 ||
strcmp(varyings[i], "gl_SkipComponents1") == 0 ||
strcmp(varyings[i], "gl_SkipComponents2") == 0 ||
strcmp(varyings[i], "gl_SkipComponents3") == 0 ||
strcmp(varyings[i], "gl_SkipComponents4") == 0) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glTransformFeedbackVaryings(SEPARATE_ATTRIBS,"
"varying=%s)",
varyings[i]);
return;
}
}
}
}
/* free existing varyings, if any */
for (i = 0; i < (GLint) shProg->TransformFeedback.NumVarying; i++) {
free(shProg->TransformFeedback.VaryingNames[i]);
}
free(shProg->TransformFeedback.VaryingNames);
/* allocate new memory for varying names */
shProg->TransformFeedback.VaryingNames =
malloc(count * sizeof(GLchar *));
if (!shProg->TransformFeedback.VaryingNames) {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glTransformFeedbackVaryings()");
return;
}
/* Save the new names and the count */
for (i = 0; i < count; i++) {
shProg->TransformFeedback.VaryingNames[i] = strdup(varyings[i]);
}
shProg->TransformFeedback.NumVarying = count;
shProg->TransformFeedback.BufferMode = bufferMode;
/* No need to invoke FLUSH_VERTICES or flag NewTransformFeedback since
* the varyings won't be used until shader link time.
*/
}
/**
* Get info about the transform feedback outputs which are to be written
* to the feedback buffer(s).
*/
void GLAPIENTRY
_mesa_GetTransformFeedbackVarying(GLuint program, GLuint index,
GLsizei bufSize, GLsizei *length,
GLsizei *size, GLenum *type, GLchar *name)
{
const struct gl_shader_program *shProg;
const struct gl_transform_feedback_info *linked_xfb_info;
GET_CURRENT_CONTEXT(ctx);
shProg = _mesa_lookup_shader_program(ctx, program);
if (!shProg) {
_mesa_error(ctx, GL_INVALID_VALUE,
"glGetTransformFeedbackVarying(program=%u)", program);
return;
}
linked_xfb_info = &shProg->LinkedTransformFeedback;
if (index >= (GLuint) linked_xfb_info->NumVarying) {
_mesa_error(ctx, GL_INVALID_VALUE,
"glGetTransformFeedbackVarying(index=%u)", index);
return;
}
/* return the varying's name and length */
_mesa_copy_string(name, bufSize, length,
linked_xfb_info->Varyings[index].Name);
/* return the datatype and value's size (in datatype units) */
if (type)
*type = linked_xfb_info->Varyings[index].Type;
if (size)
*size = linked_xfb_info->Varyings[index].Size;
}
struct gl_transform_feedback_object *
_mesa_lookup_transform_feedback_object(struct gl_context *ctx, GLuint name)
{
/* OpenGL 4.5 core profile, 13.2 pdf page 444: "xfb must be zero, indicating
* the default transform feedback object, or the name of an existing
* transform feedback object."
*/
if (name == 0) {
return ctx->TransformFeedback.DefaultObject;
}
else
return (struct gl_transform_feedback_object *)
_mesa_HashLookup(ctx->TransformFeedback.Objects, name);
}
static void
create_transform_feedbacks(struct gl_context *ctx, GLsizei n, GLuint *ids,
bool dsa)
{
GLuint first;
const char* func;
if (dsa)
func = "glCreateTransformFeedbacks";
else
func = "glGenTransformFeedbacks";
if (n < 0) {
_mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", func);
return;
}
if (!ids)
return;
/* we don't need contiguous IDs, but this might be faster */
first = _mesa_HashFindFreeKeyBlock(ctx->TransformFeedback.Objects, n);
if (first) {
GLsizei i;
for (i = 0; i < n; i++) {
struct gl_transform_feedback_object *obj
= ctx->Driver.NewTransformFeedback(ctx, first + i);
if (!obj) {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
return;
}
ids[i] = first + i;
_mesa_HashInsert(ctx->TransformFeedback.Objects, first + i, obj);
if (dsa) {
/* this is normally done at bind time in the non-dsa case */
obj->EverBound = GL_TRUE;
}
}
}
else {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
}
}
/**
* Create new transform feedback objects. Transform feedback objects
* encapsulate the state related to transform feedback to allow quickly
* switching state (and drawing the results, below).
* Part of GL_ARB_transform_feedback2.
*/
void GLAPIENTRY
_mesa_GenTransformFeedbacks(GLsizei n, GLuint *names)
{
GET_CURRENT_CONTEXT(ctx);
/* GenTransformFeedbacks should just reserve the object names that a
* subsequent call to BindTransformFeedback should actively create. For
* the sake of simplicity, we reserve the names and create the objects
* straight away.
*/
create_transform_feedbacks(ctx, n, names, false);
}
/**
* Create new transform feedback objects. Transform feedback objects
* encapsulate the state related to transform feedback to allow quickly
* switching state (and drawing the results, below).
* Part of GL_ARB_direct_state_access.
*/
void GLAPIENTRY
_mesa_CreateTransformFeedbacks(GLsizei n, GLuint *names)
{
GET_CURRENT_CONTEXT(ctx);
create_transform_feedbacks(ctx, n, names, true);
}
/**
* Is the given ID a transform feedback object?
* Part of GL_ARB_transform_feedback2.
*/
GLboolean GLAPIENTRY
_mesa_IsTransformFeedback(GLuint name)
{
struct gl_transform_feedback_object *obj;
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
if (name == 0)
return GL_FALSE;
obj = _mesa_lookup_transform_feedback_object(ctx, name);
if (obj == NULL)
return GL_FALSE;
return obj->EverBound;
}
/**
* Bind the given transform feedback object.
* Part of GL_ARB_transform_feedback2.
*/
void GLAPIENTRY
_mesa_BindTransformFeedback(GLenum target, GLuint name)
{
struct gl_transform_feedback_object *obj;
GET_CURRENT_CONTEXT(ctx);
if (target != GL_TRANSFORM_FEEDBACK) {
_mesa_error(ctx, GL_INVALID_ENUM, "glBindTransformFeedback(target)");
return;
}
if (_mesa_is_xfb_active_and_unpaused(ctx)) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glBindTransformFeedback(transform is active, or not paused)");
return;
}
obj = _mesa_lookup_transform_feedback_object(ctx, name);
if (!obj) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glBindTransformFeedback(name=%u)", name);
return;
}
reference_transform_feedback_object(&ctx->TransformFeedback.CurrentObject,
obj);
}
/**
* Delete the given transform feedback objects.
* Part of GL_ARB_transform_feedback2.
*/
void GLAPIENTRY
_mesa_DeleteTransformFeedbacks(GLsizei n, const GLuint *names)
{
GLint i;
GET_CURRENT_CONTEXT(ctx);
if (n < 0) {
_mesa_error(ctx, GL_INVALID_VALUE, "glDeleteTransformFeedbacks(n < 0)");
return;
}
if (!names)
return;
for (i = 0; i < n; i++) {
if (names[i] > 0) {
struct gl_transform_feedback_object *obj
= _mesa_lookup_transform_feedback_object(ctx, names[i]);
if (obj) {
if (obj->Active) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glDeleteTransformFeedbacks(object %u is active)",
names[i]);
return;
}
_mesa_HashRemove(ctx->TransformFeedback.Objects, names[i]);
/* unref, but object may not be deleted until later */
reference_transform_feedback_object(&obj, NULL);
}
}
}
}
/**
* Pause transform feedback.
* Part of GL_ARB_transform_feedback2.
*/
void GLAPIENTRY
_mesa_PauseTransformFeedback(void)
{
struct gl_transform_feedback_object *obj;
GET_CURRENT_CONTEXT(ctx);
obj = ctx->TransformFeedback.CurrentObject;
if (!_mesa_is_xfb_active_and_unpaused(ctx)) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glPauseTransformFeedback(feedback not active or already paused)");
return;
}
FLUSH_VERTICES(ctx, 0);
ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
obj->Paused = GL_TRUE;
assert(ctx->Driver.PauseTransformFeedback);
ctx->Driver.PauseTransformFeedback(ctx, obj);
}
/**
* Resume transform feedback.
* Part of GL_ARB_transform_feedback2.
*/
void GLAPIENTRY
_mesa_ResumeTransformFeedback(void)
{
struct gl_transform_feedback_object *obj;
GET_CURRENT_CONTEXT(ctx);
obj = ctx->TransformFeedback.CurrentObject;
if (!obj->Active || !obj->Paused) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glResumeTransformFeedback(feedback not active or not paused)");
return;
}
/* From the ARB_transform_feedback2 specification:
* "The error INVALID_OPERATION is generated by ResumeTransformFeedback if
* the program object being used by the current transform feedback object
* is not active."
*/
if (obj->shader_program != get_xfb_source(ctx)) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glResumeTransformFeedback(wrong program bound)");
return;
}
FLUSH_VERTICES(ctx, 0);
ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
obj->Paused = GL_FALSE;
assert(ctx->Driver.ResumeTransformFeedback);
ctx->Driver.ResumeTransformFeedback(ctx, obj);
}
|