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/mfeatures.h"
40 #include "main/transformfeedback.h"
42 #include "st_cb_bufferobjects.h"
43 #include "st_cb_xformfb.h"
44 #include "st_context.h"
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"
51 #if FEATURE_EXT_transform_feedback
53 struct st_transform_feedback_object
{
54 struct gl_transform_feedback_object base
;
57 struct pipe_stream_output_target
*targets
[PIPE_MAX_SO_BUFFERS
];
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
;
65 static INLINE
struct st_transform_feedback_object
*
66 st_transform_feedback_object(struct gl_transform_feedback_object
*obj
)
68 return (struct st_transform_feedback_object
*) obj
;
71 static struct gl_transform_feedback_object
*
72 st_new_transform_feedback(struct gl_context
*ctx
, GLuint name
)
74 struct st_transform_feedback_object
*obj
;
76 obj
= CALLOC_STRUCT(st_transform_feedback_object
);
80 obj
->base
.Name
= name
;
81 obj
->base
.RefCount
= 1;
87 st_delete_transform_feedback(struct gl_context
*ctx
,
88 struct gl_transform_feedback_object
*obj
)
90 struct st_transform_feedback_object
*sobj
=
91 st_transform_feedback_object(obj
);
94 pipe_so_target_reference(&sobj
->draw_count
, NULL
);
96 /* Unreference targets. */
97 for (i
= 0; i
< sobj
->num_targets
; i
++) {
98 pipe_so_target_reference(&sobj
->targets
[i
], NULL
);
101 for (i
= 0; i
< Elements(sobj
->base
.Buffers
); i
++) {
102 _mesa_reference_buffer_object(ctx
, &sobj
->base
.Buffers
[i
], NULL
);
109 /* XXX Do we really need the mode? */
111 st_begin_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
112 struct gl_transform_feedback_object
*obj
)
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
;
120 max_num_targets
= MIN2(Elements(sobj
->base
.Buffers
),
121 Elements(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
]);
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
],
140 pipe_so_target_reference(&sobj
->targets
[i
], NULL
);
141 sobj
->targets
[i
] = so_target
;
144 sobj
->num_targets
= i
+1;
146 pipe_so_target_reference(&sobj
->targets
[i
], NULL
);
150 /* Start writing at the beginning of each target. */
151 cso_set_stream_outputs(st
->cso_context
, sobj
->num_targets
, sobj
->targets
,
157 st_pause_transform_feedback(struct gl_context
*ctx
,
158 struct gl_transform_feedback_object
*obj
)
160 struct st_context
*st
= st_context(ctx
);
161 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, 0);
166 st_resume_transform_feedback(struct gl_context
*ctx
,
167 struct gl_transform_feedback_object
*obj
)
169 struct st_context
*st
= st_context(ctx
);
170 struct st_transform_feedback_object
*sobj
=
171 st_transform_feedback_object(obj
);
173 cso_set_stream_outputs(st
->cso_context
, sobj
->num_targets
, sobj
->targets
,
178 static struct pipe_stream_output_target
*
179 st_transform_feedback_get_draw_target(struct gl_transform_feedback_object
*obj
)
181 struct st_transform_feedback_object
*sobj
=
182 st_transform_feedback_object(obj
);
185 for (i
= 0; i
< Elements(sobj
->targets
); i
++) {
186 if (sobj
->targets
[i
]) {
187 return sobj
->targets
[i
];
197 st_end_transform_feedback(struct gl_context
*ctx
,
198 struct gl_transform_feedback_object
*obj
)
200 struct st_context
*st
= st_context(ctx
);
201 struct st_transform_feedback_object
*sobj
=
202 st_transform_feedback_object(obj
);
204 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, 0);
206 pipe_so_target_reference(&sobj
->draw_count
,
207 st_transform_feedback_get_draw_target(obj
));
212 st_transform_feedback_draw_init(struct gl_transform_feedback_object
*obj
,
213 struct pipe_draw_info
*out
)
215 struct st_transform_feedback_object
*sobj
=
216 st_transform_feedback_object(obj
);
218 out
->count_from_stream_output
= sobj
->draw_count
;
223 st_init_xformfb_functions(struct dd_function_table
*functions
)
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
;
233 #endif /* FEATURE_EXT_transform_feedback */