st/mesa: Use the new _mesa_init_transform_feedback_object() helper.
[mesa.git] / src / mesa / state_tracker / st_cb_xformfb.c
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/transformfeedback.h"
40
41 #include "st_cb_bufferobjects.h"
42 #include "st_cb_xformfb.h"
43 #include "st_context.h"
44
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"
49
50 struct st_transform_feedback_object {
51 struct gl_transform_feedback_object base;
52
53 unsigned num_targets;
54 struct pipe_stream_output_target *targets[PIPE_MAX_SO_BUFFERS];
55
56 /* This encapsulates the count that can be used as a source for draw_vbo.
57 * It contains a stream output target from the last call of
58 * EndTransformFeedback. */
59 struct pipe_stream_output_target *draw_count;
60 };
61
62 static INLINE struct st_transform_feedback_object *
63 st_transform_feedback_object(struct gl_transform_feedback_object *obj)
64 {
65 return (struct st_transform_feedback_object *) obj;
66 }
67
68 static struct gl_transform_feedback_object *
69 st_new_transform_feedback(struct gl_context *ctx, GLuint name)
70 {
71 struct st_transform_feedback_object *obj;
72
73 obj = CALLOC_STRUCT(st_transform_feedback_object);
74 if (!obj)
75 return NULL;
76
77 _mesa_init_transform_feedback_object(obj, name);
78
79 return &obj->base;
80 }
81
82
83 static void
84 st_delete_transform_feedback(struct gl_context *ctx,
85 struct gl_transform_feedback_object *obj)
86 {
87 struct st_transform_feedback_object *sobj =
88 st_transform_feedback_object(obj);
89 unsigned i;
90
91 pipe_so_target_reference(&sobj->draw_count, NULL);
92
93 /* Unreference targets. */
94 for (i = 0; i < sobj->num_targets; i++) {
95 pipe_so_target_reference(&sobj->targets[i], NULL);
96 }
97
98 for (i = 0; i < Elements(sobj->base.Buffers); i++) {
99 _mesa_reference_buffer_object(ctx, &sobj->base.Buffers[i], NULL);
100 }
101
102 free(obj);
103 }
104
105
106 /* XXX Do we really need the mode? */
107 static void
108 st_begin_transform_feedback(struct gl_context *ctx, GLenum mode,
109 struct gl_transform_feedback_object *obj)
110 {
111 struct st_context *st = st_context(ctx);
112 struct pipe_context *pipe = st->pipe;
113 struct st_transform_feedback_object *sobj =
114 st_transform_feedback_object(obj);
115 unsigned i, max_num_targets;
116
117 max_num_targets = MIN2(Elements(sobj->base.Buffers),
118 Elements(sobj->targets));
119
120 /* Convert the transform feedback state into the gallium representation. */
121 for (i = 0; i < max_num_targets; i++) {
122 struct st_buffer_object *bo = st_buffer_object(sobj->base.Buffers[i]);
123
124 if (bo) {
125 /* Check whether we need to recreate the target. */
126 if (!sobj->targets[i] ||
127 sobj->targets[i] == sobj->draw_count ||
128 sobj->targets[i]->buffer != bo->buffer ||
129 sobj->targets[i]->buffer_offset != sobj->base.Offset[i] ||
130 sobj->targets[i]->buffer_size != sobj->base.Size[i]) {
131 /* Create a new target. */
132 struct pipe_stream_output_target *so_target =
133 pipe->create_stream_output_target(pipe, bo->buffer,
134 sobj->base.Offset[i],
135 sobj->base.Size[i]);
136
137 pipe_so_target_reference(&sobj->targets[i], NULL);
138 sobj->targets[i] = so_target;
139 }
140
141 sobj->num_targets = i+1;
142 } else {
143 pipe_so_target_reference(&sobj->targets[i], NULL);
144 }
145 }
146
147 /* Start writing at the beginning of each target. */
148 cso_set_stream_outputs(st->cso_context, sobj->num_targets, sobj->targets,
149 0);
150 }
151
152
153 static void
154 st_pause_transform_feedback(struct gl_context *ctx,
155 struct gl_transform_feedback_object *obj)
156 {
157 struct st_context *st = st_context(ctx);
158 cso_set_stream_outputs(st->cso_context, 0, NULL, 0);
159 }
160
161
162 static void
163 st_resume_transform_feedback(struct gl_context *ctx,
164 struct gl_transform_feedback_object *obj)
165 {
166 struct st_context *st = st_context(ctx);
167 struct st_transform_feedback_object *sobj =
168 st_transform_feedback_object(obj);
169
170 cso_set_stream_outputs(st->cso_context, sobj->num_targets, sobj->targets,
171 ~0);
172 }
173
174
175 static struct pipe_stream_output_target *
176 st_transform_feedback_get_draw_target(struct gl_transform_feedback_object *obj)
177 {
178 struct st_transform_feedback_object *sobj =
179 st_transform_feedback_object(obj);
180 unsigned i;
181
182 for (i = 0; i < Elements(sobj->targets); i++) {
183 if (sobj->targets[i]) {
184 return sobj->targets[i];
185 }
186 }
187
188 assert(0);
189 return NULL;
190 }
191
192
193 static void
194 st_end_transform_feedback(struct gl_context *ctx,
195 struct gl_transform_feedback_object *obj)
196 {
197 struct st_context *st = st_context(ctx);
198 struct st_transform_feedback_object *sobj =
199 st_transform_feedback_object(obj);
200
201 cso_set_stream_outputs(st->cso_context, 0, NULL, 0);
202
203 pipe_so_target_reference(&sobj->draw_count,
204 st_transform_feedback_get_draw_target(obj));
205 }
206
207
208 void
209 st_transform_feedback_draw_init(struct gl_transform_feedback_object *obj,
210 struct pipe_draw_info *out)
211 {
212 struct st_transform_feedback_object *sobj =
213 st_transform_feedback_object(obj);
214
215 out->count_from_stream_output = sobj->draw_count;
216 }
217
218
219 void
220 st_init_xformfb_functions(struct dd_function_table *functions)
221 {
222 functions->NewTransformFeedback = st_new_transform_feedback;
223 functions->DeleteTransformFeedback = st_delete_transform_feedback;
224 functions->BeginTransformFeedback = st_begin_transform_feedback;
225 functions->EndTransformFeedback = st_end_transform_feedback;
226 functions->PauseTransformFeedback = st_pause_transform_feedback;
227 functions->ResumeTransformFeedback = st_resume_transform_feedback;
228 }