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