1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Transform feedback functions.
37 #include "main/bufferobj.h"
38 #include "main/context.h"
39 #include "main/transformfeedback.h"
41 #include "st_cb_bufferobjects.h"
42 #include "st_cb_xformfb.h"
43 #include "st_context.h"
45 #include "pipe/p_context.h"
46 #include "util/u_draw.h"
47 #include "util/u_inlines.h"
48 #include "cso_cache/cso_context.h"
50 struct st_transform_feedback_object
{
51 struct gl_transform_feedback_object base
;
54 struct pipe_stream_output_target
*targets
[PIPE_MAX_SO_BUFFERS
];
56 /* This encapsulates the count that can be used as a source for draw_vbo.
57 * It contains stream output targets from the last call of
58 * EndTransformFeedback for each stream. */
59 struct pipe_stream_output_target
*draw_count
[MAX_VERTEX_STREAMS
];
62 static inline struct st_transform_feedback_object
*
63 st_transform_feedback_object(struct gl_transform_feedback_object
*obj
)
65 return (struct st_transform_feedback_object
*) obj
;
68 static struct gl_transform_feedback_object
*
69 st_new_transform_feedback(struct gl_context
*ctx
, GLuint name
)
71 struct st_transform_feedback_object
*obj
;
73 obj
= CALLOC_STRUCT(st_transform_feedback_object
);
77 _mesa_init_transform_feedback_object(&obj
->base
, name
);
84 st_delete_transform_feedback(struct gl_context
*ctx
,
85 struct gl_transform_feedback_object
*obj
)
87 struct st_transform_feedback_object
*sobj
=
88 st_transform_feedback_object(obj
);
91 for (i
= 0; i
< ARRAY_SIZE(sobj
->draw_count
); i
++)
92 pipe_so_target_reference(&sobj
->draw_count
[i
], NULL
);
94 /* Unreference targets. */
95 for (i
= 0; i
< sobj
->num_targets
; i
++) {
96 pipe_so_target_reference(&sobj
->targets
[i
], NULL
);
99 for (i
= 0; i
< ARRAY_SIZE(sobj
->base
.Buffers
); i
++) {
100 _mesa_reference_buffer_object(ctx
, &sobj
->base
.Buffers
[i
], NULL
);
108 /* XXX Do we really need the mode? */
110 st_begin_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
111 struct gl_transform_feedback_object
*obj
)
113 struct st_context
*st
= st_context(ctx
);
114 struct pipe_context
*pipe
= st
->pipe
;
115 struct st_transform_feedback_object
*sobj
=
116 st_transform_feedback_object(obj
);
117 unsigned i
, max_num_targets
;
118 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
120 max_num_targets
= MIN2(ARRAY_SIZE(sobj
->base
.Buffers
),
121 ARRAY_SIZE(sobj
->targets
));
123 /* Convert the transform feedback state into the gallium representation. */
124 for (i
= 0; i
< max_num_targets
; i
++) {
125 struct st_buffer_object
*bo
= st_buffer_object(sobj
->base
.Buffers
[i
]);
127 if (bo
&& bo
->buffer
) {
128 unsigned stream
= obj
->program
->sh
.LinkedTransformFeedback
->
131 /* Check whether we need to recreate the target. */
132 if (!sobj
->targets
[i
] ||
133 sobj
->targets
[i
] == sobj
->draw_count
[stream
] ||
134 sobj
->targets
[i
]->buffer
!= bo
->buffer
||
135 sobj
->targets
[i
]->buffer_offset
!= sobj
->base
.Offset
[i
] ||
136 sobj
->targets
[i
]->buffer_size
!= sobj
->base
.Size
[i
]) {
137 /* Create a new target. */
138 struct pipe_stream_output_target
*so_target
=
139 pipe
->create_stream_output_target(pipe
, bo
->buffer
,
140 sobj
->base
.Offset
[i
],
143 pipe_so_target_reference(&sobj
->targets
[i
], NULL
);
144 sobj
->targets
[i
] = so_target
;
147 sobj
->num_targets
= i
+1;
149 pipe_so_target_reference(&sobj
->targets
[i
], NULL
);
153 /* Start writing at the beginning of each target. */
154 cso_set_stream_outputs(st
->cso_context
, sobj
->num_targets
,
155 sobj
->targets
, offsets
);
160 st_pause_transform_feedback(struct gl_context
*ctx
,
161 struct gl_transform_feedback_object
*obj
)
163 struct st_context
*st
= st_context(ctx
);
164 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, NULL
);
169 st_resume_transform_feedback(struct gl_context
*ctx
,
170 struct gl_transform_feedback_object
*obj
)
172 struct st_context
*st
= st_context(ctx
);
173 struct st_transform_feedback_object
*sobj
=
174 st_transform_feedback_object(obj
);
175 unsigned offsets
[PIPE_MAX_SO_BUFFERS
];
178 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++)
179 offsets
[i
] = (unsigned)-1;
181 cso_set_stream_outputs(st
->cso_context
, sobj
->num_targets
,
182 sobj
->targets
, offsets
);
187 st_end_transform_feedback(struct gl_context
*ctx
,
188 struct gl_transform_feedback_object
*obj
)
190 struct st_context
*st
= st_context(ctx
);
191 struct st_transform_feedback_object
*sobj
=
192 st_transform_feedback_object(obj
);
195 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, NULL
);
197 /* The next call to glDrawTransformFeedbackStream should use the vertex
198 * count from the last call to glEndTransformFeedback.
199 * Therefore, save the targets for each stream.
201 * NULL means the vertex counter is 0 (initial state).
203 for (i
= 0; i
< ARRAY_SIZE(sobj
->draw_count
); i
++)
204 pipe_so_target_reference(&sobj
->draw_count
[i
], NULL
);
206 for (i
= 0; i
< ARRAY_SIZE(sobj
->targets
); i
++) {
207 unsigned stream
= obj
->program
->sh
.LinkedTransformFeedback
->
210 /* Is it not bound or already set for this stream? */
211 if (!sobj
->targets
[i
] || sobj
->draw_count
[stream
])
214 pipe_so_target_reference(&sobj
->draw_count
[stream
], sobj
->targets
[i
]);
220 st_transform_feedback_draw_init(struct gl_transform_feedback_object
*obj
,
221 unsigned stream
, struct pipe_draw_info
*out
)
223 struct st_transform_feedback_object
*sobj
=
224 st_transform_feedback_object(obj
);
226 out
->count_from_stream_output
= sobj
->draw_count
[stream
];
227 return out
->count_from_stream_output
!= NULL
;
232 st_init_xformfb_functions(struct dd_function_table
*functions
)
234 functions
->NewTransformFeedback
= st_new_transform_feedback
;
235 functions
->DeleteTransformFeedback
= st_delete_transform_feedback
;
236 functions
->BeginTransformFeedback
= st_begin_transform_feedback
;
237 functions
->EndTransformFeedback
= st_end_transform_feedback
;
238 functions
->PauseTransformFeedback
= st_pause_transform_feedback
;
239 functions
->ResumeTransformFeedback
= st_resume_transform_feedback
;