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 Olk 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 stream output targets from the last call of 58 * EndTransformFeedback for each stream. */ 59 struct pipe_stream_output_target *draw_count[MAX_VERTEX_STREAMS]; 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->base, 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 for (i = 0; i < ARRAY_SIZE(sobj->draw_count); i++) 92 pipe_so_target_reference(&sobj->draw_count[i], 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 < ARRAY_SIZE(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 unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0}; 118 119 max_num_targets = MIN2(ARRAY_SIZE(sobj->base.Buffers), 120 ARRAY_SIZE(sobj->targets)); 121 122 /* Convert the transform feedback state into the gallium representation. */ 123 for (i = 0; i < max_num_targets; i++) { 124 struct st_buffer_object *bo = st_buffer_object(sobj->base.Buffers[i]); 125 126 if (bo && bo->buffer) { 127 unsigned stream = obj->program->sh.LinkedTransformFeedback-> 128 Buffers[i].Stream; 129 130 /* Check whether we need to recreate the target. */ 131 if (!sobj->targets[i] || 132 sobj->targets[i] == sobj->draw_count[stream] || 133 sobj->targets[i]->buffer != bo->buffer || 134 sobj->targets[i]->buffer_offset != sobj->base.Offset[i] || 135 sobj->targets[i]->buffer_size != sobj->base.Size[i]) { 136 /* Create a new target. */ 137 struct pipe_stream_output_target *so_target = 138 pipe->create_stream_output_target(pipe, bo->buffer, 139 sobj->base.Offset[i], 140 sobj->base.Size[i]); 141 142 pipe_so_target_reference(&sobj->targets[i], NULL); 143 sobj->targets[i] = so_target; 144 } 145 146 sobj->num_targets = i+1; 147 } else { 148 pipe_so_target_reference(&sobj->targets[i], NULL); 149 } 150 } 151 152 /* Start writing at the beginning of each target. */ 153 cso_set_stream_outputs(st->cso_context, sobj->num_targets, 154 sobj->targets, offsets); 155 } 156 157 158 static void 159 st_pause_transform_feedback(struct gl_context *ctx, 160 struct gl_transform_feedback_object *obj) 161 { 162 struct st_context *st = st_context(ctx); 163 cso_set_stream_outputs(st->cso_context, 0, NULL, NULL); 164 } 165 166 167 static void 168 st_resume_transform_feedback(struct gl_context *ctx, 169 struct gl_transform_feedback_object *obj) 170 { 171 struct st_context *st = st_context(ctx); 172 struct st_transform_feedback_object *sobj = 173 st_transform_feedback_object(obj); 174 unsigned offsets[PIPE_MAX_SO_BUFFERS]; 175 unsigned i; 176 177 for (i = 0; i < PIPE_MAX_SO_BUFFERS; i++) 178 offsets[i] = (unsigned)-1; 179 180 cso_set_stream_outputs(st->cso_context, sobj->num_targets, 181 sobj->targets, offsets); 182 } 183 184 185 static void 186 st_end_transform_feedback(struct gl_context *ctx, 187 struct gl_transform_feedback_object *obj) 188 { 189 struct st_context *st = st_context(ctx); 190 struct st_transform_feedback_object *sobj = 191 st_transform_feedback_object(obj); 192 unsigned i; 193 194 cso_set_stream_outputs(st->cso_context, 0, NULL, NULL); 195 196 /* The next call to glDrawTransformFeedbackStream should use the vertex 197 * count from the last call to glEndTransformFeedback. 198 * Therefore, save the targets for each stream. 199 * 200 * NULL means the vertex counter is 0 (initial state). 201 */ 202 for (i = 0; i < ARRAY_SIZE(sobj->draw_count); i++) 203 pipe_so_target_reference(&sobj->draw_count[i], NULL); 204 205 for (i = 0; i < ARRAY_SIZE(sobj->targets); i++) { 206 unsigned stream = obj->program->sh.LinkedTransformFeedback-> 207 Buffers[i].Stream; 208 209 /* Is it not bound or already set for this stream? */ 210 if (!sobj->targets[i] || sobj->draw_count[stream]) 211 continue; 212 213 pipe_so_target_reference(&sobj->draw_count[stream], sobj->targets[i]); 214 } 215 } 216 217 218 bool 219 st_transform_feedback_draw_init(struct gl_transform_feedback_object *obj, 220 unsigned stream, struct pipe_draw_info *out) 221 { 222 struct st_transform_feedback_object *sobj = 223 st_transform_feedback_object(obj); 224 225 out->count_from_stream_output = sobj->draw_count[stream]; 226 return out->count_from_stream_output != NULL; 227 } 228 229 230 void 231 st_init_xformfb_functions(struct dd_function_table *functions) 232 { 233 functions->NewTransformFeedback = st_new_transform_feedback; 234 functions->DeleteTransformFeedback = st_delete_transform_feedback; 235 functions->BeginTransformFeedback = st_begin_transform_feedback; 236 functions->EndTransformFeedback = st_end_transform_feedback; 237 functions->PauseTransformFeedback = st_pause_transform_feedback; 238 functions->ResumeTransformFeedback = st_resume_transform_feedback; 239 } 240