1/**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * 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
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29/**
30 * Transform feedback functions.
31 *
32 * \author Brian Paul
33 *         Marek Olšák
34 */
35
36
37#include "main/bufferobj.h"
38#include "main/context.h"
39#include "main/mfeatures.h"
40#include "main/transformfeedback.h"
41
42#include "st_cb_bufferobjects.h"
43#include "st_cb_xformfb.h"
44#include "st_context.h"
45
46#include "pipe/p_context.h"
47#include "util/u_draw.h"
48#include "util/u_inlines.h"
49#include "cso_cache/cso_context.h"
50
51#if FEATURE_EXT_transform_feedback
52
53struct st_transform_feedback_object {
54   struct gl_transform_feedback_object base;
55
56   unsigned num_targets;
57   struct pipe_stream_output_target *targets[PIPE_MAX_SO_BUFFERS];
58
59   /* This encapsulates the count that can be used as a source for draw_vbo.
60    * It contains a stream output target from the last call of
61    * EndTransformFeedback. */
62   struct pipe_stream_output_target *draw_count;
63};
64
65static INLINE struct st_transform_feedback_object *
66st_transform_feedback_object(struct gl_transform_feedback_object *obj)
67{
68   return (struct st_transform_feedback_object *) obj;
69}
70
71static struct gl_transform_feedback_object *
72st_new_transform_feedback(struct gl_context *ctx, GLuint name)
73{
74   struct st_transform_feedback_object *obj;
75
76   obj = CALLOC_STRUCT(st_transform_feedback_object);
77   if (!obj)
78      return NULL;
79
80   obj->base.Name = name;
81   obj->base.RefCount = 1;
82   return &obj->base;
83}
84
85
86static void
87st_delete_transform_feedback(struct gl_context *ctx,
88                             struct gl_transform_feedback_object *obj)
89{
90   struct st_transform_feedback_object *sobj =
91         st_transform_feedback_object(obj);
92   unsigned i;
93
94   pipe_so_target_reference(&sobj->draw_count, NULL);
95
96   /* Unreference targets. */
97   for (i = 0; i < sobj->num_targets; i++) {
98      pipe_so_target_reference(&sobj->targets[i], NULL);
99   }
100
101   for (i = 0; i < Elements(sobj->base.Buffers); i++) {
102      _mesa_reference_buffer_object(ctx, &sobj->base.Buffers[i], NULL);
103   }
104
105   free(obj);
106}
107
108
109/* XXX Do we really need the mode? */
110static void
111st_begin_transform_feedback(struct gl_context *ctx, GLenum mode,
112                            struct gl_transform_feedback_object *obj)
113{
114   struct st_context *st = st_context(ctx);
115   struct pipe_context *pipe = st->pipe;
116   struct st_transform_feedback_object *sobj =
117         st_transform_feedback_object(obj);
118   unsigned i, max_num_targets;
119
120   max_num_targets = MIN2(Elements(sobj->base.Buffers),
121                          Elements(sobj->targets));
122
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]);
126
127      if (bo) {
128         /* Check whether we need to recreate the target. */
129         if (!sobj->targets[i] ||
130             sobj->targets[i] == sobj->draw_count ||
131             sobj->targets[i]->buffer != bo->buffer ||
132             sobj->targets[i]->buffer_offset != sobj->base.Offset[i] ||
133             sobj->targets[i]->buffer_size != sobj->base.Size[i]) {
134            /* Create a new target. */
135            struct pipe_stream_output_target *so_target =
136                  pipe->create_stream_output_target(pipe, bo->buffer,
137                                                    sobj->base.Offset[i],
138                                                    sobj->base.Size[i]);
139
140            pipe_so_target_reference(&sobj->targets[i], NULL);
141            sobj->targets[i] = so_target;
142         }
143
144         sobj->num_targets = i+1;
145      } else {
146         pipe_so_target_reference(&sobj->targets[i], NULL);
147      }
148   }
149
150   /* Start writing at the beginning of each target. */
151   cso_set_stream_outputs(st->cso_context, sobj->num_targets, sobj->targets,
152                          0);
153}
154
155
156static void
157st_pause_transform_feedback(struct gl_context *ctx,
158                           struct gl_transform_feedback_object *obj)
159{
160   struct st_context *st = st_context(ctx);
161   cso_set_stream_outputs(st->cso_context, 0, NULL, 0);
162}
163
164
165static void
166st_resume_transform_feedback(struct gl_context *ctx,
167                             struct gl_transform_feedback_object *obj)
168{
169   struct st_context *st = st_context(ctx);
170   struct st_transform_feedback_object *sobj =
171         st_transform_feedback_object(obj);
172
173   cso_set_stream_outputs(st->cso_context, sobj->num_targets, sobj->targets,
174                          ~0);
175}
176
177
178static struct pipe_stream_output_target *
179st_transform_feedback_get_draw_target(struct gl_transform_feedback_object *obj)
180{
181   struct st_transform_feedback_object *sobj =
182         st_transform_feedback_object(obj);
183   unsigned i;
184
185   for (i = 0; i < Elements(sobj->targets); i++) {
186      if (sobj->targets[i]) {
187         return sobj->targets[i];
188      }
189   }
190
191   assert(0);
192   return NULL;
193}
194
195
196static void
197st_end_transform_feedback(struct gl_context *ctx,
198                          struct gl_transform_feedback_object *obj)
199{
200   struct st_context *st = st_context(ctx);
201   struct st_transform_feedback_object *sobj =
202         st_transform_feedback_object(obj);
203
204   cso_set_stream_outputs(st->cso_context, 0, NULL, 0);
205
206   pipe_so_target_reference(&sobj->draw_count,
207                            st_transform_feedback_get_draw_target(obj));
208}
209
210
211void
212st_transform_feedback_draw_init(struct gl_transform_feedback_object *obj,
213                                struct pipe_draw_info *out)
214{
215   struct st_transform_feedback_object *sobj =
216         st_transform_feedback_object(obj);
217
218   out->count_from_stream_output = sobj->draw_count;
219}
220
221
222void
223st_init_xformfb_functions(struct dd_function_table *functions)
224{
225   functions->NewTransformFeedback = st_new_transform_feedback;
226   functions->DeleteTransformFeedback = st_delete_transform_feedback;
227   functions->BeginTransformFeedback = st_begin_transform_feedback;
228   functions->EndTransformFeedback = st_end_transform_feedback;
229   functions->PauseTransformFeedback = st_pause_transform_feedback;
230   functions->ResumeTransformFeedback = st_resume_transform_feedback;
231}
232
233#endif /* FEATURE_EXT_transform_feedback */
234