st/mesa: advertise ARB_shading_language_packing for GLSL >= 1.30
[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/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 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 a stream output target from the last call of
59 * EndTransformFeedback. */
60 struct pipe_stream_output_target *draw_count;
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 obj->base.Name = name;
79 obj->base.RefCount = 1;
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 pipe_so_target_reference(&sobj->draw_count, NULL);
93
94 /* Unreference targets. */
95 for (i = 0; i < sobj->num_targets; i++) {
96 pipe_so_target_reference(&sobj->targets[i], NULL);
97 }
98
99 for (i = 0; i < Elements(sobj->base.Buffers); i++) {
100 _mesa_reference_buffer_object(ctx, &sobj->base.Buffers[i], NULL);
101 }
102
103 free(obj);
104 }
105
106
107 /* XXX Do we really need the mode? */
108 static void
109 st_begin_transform_feedback(struct gl_context *ctx, GLenum mode,
110 struct gl_transform_feedback_object *obj)
111 {
112 struct st_context *st = st_context(ctx);
113 struct pipe_context *pipe = st->pipe;
114 struct st_transform_feedback_object *sobj =
115 st_transform_feedback_object(obj);
116 unsigned i, max_num_targets;
117
118 max_num_targets = MIN2(Elements(sobj->base.Buffers),
119 Elements(sobj->targets));
120
121 /* Convert the transform feedback state into the gallium representation. */
122 for (i = 0; i < max_num_targets; i++) {
123 struct st_buffer_object *bo = st_buffer_object(sobj->base.Buffers[i]);
124
125 if (bo) {
126 /* Check whether we need to recreate the target. */
127 if (!sobj->targets[i] ||
128 sobj->targets[i] == sobj->draw_count ||
129 sobj->targets[i]->buffer != bo->buffer ||
130 sobj->targets[i]->buffer_offset != sobj->base.Offset[i] ||
131 sobj->targets[i]->buffer_size != sobj->base.Size[i]) {
132 /* Create a new target. */
133 struct pipe_stream_output_target *so_target =
134 pipe->create_stream_output_target(pipe, bo->buffer,
135 sobj->base.Offset[i],
136 sobj->base.Size[i]);
137
138 pipe_so_target_reference(&sobj->targets[i], NULL);
139 sobj->targets[i] = so_target;
140 }
141
142 sobj->num_targets = i+1;
143 } else {
144 pipe_so_target_reference(&sobj->targets[i], NULL);
145 }
146 }
147
148 /* Start writing at the beginning of each target. */
149 cso_set_stream_outputs(st->cso_context, sobj->num_targets, sobj->targets,
150 0);
151 }
152
153
154 static void
155 st_pause_transform_feedback(struct gl_context *ctx,
156 struct gl_transform_feedback_object *obj)
157 {
158 struct st_context *st = st_context(ctx);
159 cso_set_stream_outputs(st->cso_context, 0, NULL, 0);
160 }
161
162
163 static void
164 st_resume_transform_feedback(struct gl_context *ctx,
165 struct gl_transform_feedback_object *obj)
166 {
167 struct st_context *st = st_context(ctx);
168 struct st_transform_feedback_object *sobj =
169 st_transform_feedback_object(obj);
170
171 cso_set_stream_outputs(st->cso_context, sobj->num_targets, sobj->targets,
172 ~0);
173 }
174
175
176 static struct pipe_stream_output_target *
177 st_transform_feedback_get_draw_target(struct gl_transform_feedback_object *obj)
178 {
179 struct st_transform_feedback_object *sobj =
180 st_transform_feedback_object(obj);
181 unsigned i;
182
183 for (i = 0; i < Elements(sobj->targets); i++) {
184 if (sobj->targets[i]) {
185 return sobj->targets[i];
186 }
187 }
188
189 assert(0);
190 return NULL;
191 }
192
193
194 static void
195 st_end_transform_feedback(struct gl_context *ctx,
196 struct gl_transform_feedback_object *obj)
197 {
198 struct st_context *st = st_context(ctx);
199 struct st_transform_feedback_object *sobj =
200 st_transform_feedback_object(obj);
201
202 cso_set_stream_outputs(st->cso_context, 0, NULL, 0);
203
204 pipe_so_target_reference(&sobj->draw_count,
205 st_transform_feedback_get_draw_target(obj));
206 }
207
208
209 void
210 st_transform_feedback_draw_init(struct gl_transform_feedback_object *obj,
211 struct pipe_draw_info *out)
212 {
213 struct st_transform_feedback_object *sobj =
214 st_transform_feedback_object(obj);
215
216 out->count_from_stream_output = sobj->draw_count;
217 }
218
219
220 void
221 st_init_xformfb_functions(struct dd_function_table *functions)
222 {
223 functions->NewTransformFeedback = st_new_transform_feedback;
224 functions->DeleteTransformFeedback = st_delete_transform_feedback;
225 functions->BeginTransformFeedback = st_begin_transform_feedback;
226 functions->EndTransformFeedback = st_end_transform_feedback;
227 functions->PauseTransformFeedback = st_pause_transform_feedback;
228 functions->ResumeTransformFeedback = st_resume_transform_feedback;
229 }