e76694d4b2b00e1134d8d5b35feaaea50bc5973f
[mesa.git] / src / mesa / state_tracker / st_draw_feedback.c
1 /**************************************************************************
2 *
3 * Copyright 2007 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 VMWARE 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 #include "main/imports.h"
29 #include "main/image.h"
30 #include "main/macros.h"
31
32 #include "vbo/vbo.h"
33
34 #include "st_context.h"
35 #include "st_atom.h"
36 #include "st_cb_bitmap.h"
37 #include "st_cb_bufferobjects.h"
38 #include "st_draw.h"
39 #include "st_program.h"
40
41 #include "pipe/p_context.h"
42 #include "pipe/p_defines.h"
43 #include "util/u_inlines.h"
44 #include "util/u_draw.h"
45
46 #include "draw/draw_private.h"
47 #include "draw/draw_context.h"
48
49
50 /**
51 * Set the (private) draw module's post-transformed vertex format when in
52 * GL_SELECT or GL_FEEDBACK mode or for glRasterPos.
53 */
54 static void
55 set_feedback_vertex_format(struct gl_context *ctx)
56 {
57 #if 0
58 struct st_context *st = st_context(ctx);
59 struct vertex_info vinfo;
60 GLuint i;
61
62 memset(&vinfo, 0, sizeof(vinfo));
63
64 if (ctx->RenderMode == GL_SELECT) {
65 assert(ctx->RenderMode == GL_SELECT);
66 vinfo.num_attribs = 1;
67 vinfo.format[0] = FORMAT_4F;
68 vinfo.interp_mode[0] = INTERP_LINEAR;
69 }
70 else {
71 /* GL_FEEDBACK, or glRasterPos */
72 /* emit all attribs (pos, color, texcoord) as GLfloat[4] */
73 vinfo.num_attribs = st->state.vs->cso->state.num_outputs;
74 for (i = 0; i < vinfo.num_attribs; i++) {
75 vinfo.format[i] = FORMAT_4F;
76 vinfo.interp_mode[i] = INTERP_LINEAR;
77 }
78 }
79
80 draw_set_vertex_info(st->draw, &vinfo);
81 #endif
82 }
83
84
85 /**
86 * Helper for drawing current vertex arrays.
87 */
88 static void
89 draw_arrays(struct draw_context *draw, unsigned mode,
90 unsigned start, unsigned count)
91 {
92 struct pipe_draw_info info;
93
94 util_draw_init_info(&info);
95
96 info.mode = mode;
97 info.start = start;
98 info.count = count;
99 info.min_index = start;
100 info.max_index = start + count - 1;
101
102 draw_vbo(draw, &info);
103 }
104
105
106 /**
107 * Called by VBO to draw arrays when in selection or feedback mode and
108 * to implement glRasterPos.
109 * This is very much like the normal draw_vbo() function above.
110 * Look at code refactoring some day.
111 */
112 void
113 st_feedback_draw_vbo(struct gl_context *ctx,
114 const struct _mesa_prim *prims,
115 GLuint nr_prims,
116 const struct _mesa_index_buffer *ib,
117 GLboolean index_bounds_valid,
118 GLuint min_index,
119 GLuint max_index,
120 struct gl_transform_feedback_object *tfb_vertcount,
121 unsigned stream,
122 struct gl_buffer_object *indirect)
123 {
124 struct st_context *st = st_context(ctx);
125 struct pipe_context *pipe = st->pipe;
126 struct draw_context *draw = st->draw;
127 const struct st_vertex_program *vp;
128 const struct pipe_shader_state *vs;
129 struct pipe_vertex_buffer vbuffers[PIPE_MAX_SHADER_INPUTS];
130 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
131 struct pipe_index_buffer ibuffer;
132 struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS] = {NULL};
133 struct pipe_transfer *ib_transfer = NULL;
134 const struct gl_client_array **arrays = ctx->Array._DrawArrays;
135 GLuint attr, i;
136 const GLubyte *low_addr = NULL;
137 const void *mapped_indices = NULL;
138
139 assert(draw);
140
141 st_flush_bitmap_cache(st);
142 st_invalidate_readpix_cache(st);
143
144 st_validate_state(st, ST_PIPELINE_RENDER);
145
146 if (!index_bounds_valid)
147 vbo_get_minmax_indices(ctx, prims, ib, &min_index, &max_index, nr_prims);
148
149 /* must get these after state validation! */
150 vp = st->vp;
151 vs = &st->vp_variant->tgsi;
152
153 if (!st->vp_variant->draw_shader) {
154 st->vp_variant->draw_shader = draw_create_vertex_shader(draw, vs);
155 }
156
157 /*
158 * Set up the draw module's state.
159 *
160 * We'd like to do this less frequently, but the normal state-update
161 * code sends state updates to the pipe, not to our private draw module.
162 */
163 assert(draw);
164 draw_set_viewport_states(draw, 0, 1, &st->state.viewport[0]);
165 draw_set_clip_state(draw, &st->state.clip);
166 draw_set_rasterizer_state(draw, &st->state.rasterizer, NULL);
167 draw_bind_vertex_shader(draw, st->vp_variant->draw_shader);
168 set_feedback_vertex_format(ctx);
169
170 /* Find the lowest address of the arrays we're drawing */
171 if (vp->num_inputs) {
172 low_addr = arrays[vp->index_to_input[0]]->Ptr;
173
174 for (attr = 1; attr < vp->num_inputs; attr++) {
175 const GLubyte *start = arrays[vp->index_to_input[attr]]->Ptr;
176 low_addr = MIN2(low_addr, start);
177 }
178 }
179
180 /* loop over TGSI shader inputs to determine vertex buffer
181 * and attribute info
182 */
183 for (attr = 0; attr < vp->num_inputs; attr++) {
184 const GLuint mesaAttr = vp->index_to_input[attr];
185 struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
186 void *map;
187
188 if (bufobj && bufobj->Name) {
189 /* Attribute data is in a VBO.
190 * Recall that for VBOs, the gl_client_array->Ptr field is
191 * really an offset from the start of the VBO, not a pointer.
192 */
193 struct st_buffer_object *stobj = st_buffer_object(bufobj);
194 assert(stobj->buffer);
195
196 vbuffers[attr].buffer = NULL;
197 vbuffers[attr].user_buffer = NULL;
198 pipe_resource_reference(&vbuffers[attr].buffer, stobj->buffer);
199 vbuffers[attr].buffer_offset = pointer_to_offset(low_addr);
200 velements[attr].src_offset = arrays[mesaAttr]->Ptr - low_addr;
201
202 /* map the attrib buffer */
203 map = pipe_buffer_map(pipe, vbuffers[attr].buffer,
204 PIPE_TRANSFER_READ,
205 &vb_transfer[attr]);
206 draw_set_mapped_vertex_buffer(draw, attr, map,
207 vbuffers[attr].buffer->width0);
208 }
209 else {
210 vbuffers[attr].buffer = NULL;
211 vbuffers[attr].user_buffer = arrays[mesaAttr]->Ptr;
212 vbuffers[attr].buffer_offset = 0;
213 velements[attr].src_offset = 0;
214
215 draw_set_mapped_vertex_buffer(draw, attr, vbuffers[attr].user_buffer,
216 ~0);
217 }
218
219 /* common-case setup */
220 vbuffers[attr].stride = arrays[mesaAttr]->StrideB; /* in bytes */
221 velements[attr].instance_divisor = 0;
222 velements[attr].vertex_buffer_index = attr;
223 velements[attr].src_format =
224 st_pipe_vertex_format(arrays[mesaAttr]->Type,
225 arrays[mesaAttr]->Size,
226 arrays[mesaAttr]->Format,
227 arrays[mesaAttr]->Normalized,
228 arrays[mesaAttr]->Integer);
229 assert(velements[attr].src_format);
230
231 /* tell draw about this attribute */
232 #if 0
233 draw_set_vertex_buffer(draw, attr, &vbuffer[attr]);
234 #endif
235 }
236
237 draw_set_vertex_buffers(draw, 0, vp->num_inputs, vbuffers);
238 draw_set_vertex_elements(draw, vp->num_inputs, velements);
239
240 memset(&ibuffer, 0, sizeof(ibuffer));
241 if (ib) {
242 struct gl_buffer_object *bufobj = ib->obj;
243
244 ibuffer.index_size = vbo_sizeof_ib_type(ib->type);
245 if (ibuffer.index_size == 0)
246 goto out_unref_vertex;
247
248 if (bufobj && bufobj->Name) {
249 struct st_buffer_object *stobj = st_buffer_object(bufobj);
250
251 pipe_resource_reference(&ibuffer.buffer, stobj->buffer);
252 ibuffer.offset = pointer_to_offset(ib->ptr);
253
254 mapped_indices = pipe_buffer_map(pipe, stobj->buffer,
255 PIPE_TRANSFER_READ, &ib_transfer);
256 }
257 else {
258 /* skip setting ibuffer.buffer as the draw module does not use it */
259 mapped_indices = ib->ptr;
260 }
261
262 draw_set_indexes(draw,
263 (ubyte *) mapped_indices + ibuffer.offset,
264 ibuffer.index_size, ~0);
265 }
266
267 /* set the constant buffer */
268 draw_set_mapped_constant_buffer(st->draw, PIPE_SHADER_VERTEX, 0,
269 st->state.constants[PIPE_SHADER_VERTEX].ptr,
270 st->state.constants[PIPE_SHADER_VERTEX].size);
271
272
273 /* draw here */
274 for (i = 0; i < nr_prims; i++) {
275 draw_arrays(draw, prims[i].mode, prims[i].start, prims[i].count);
276 }
277
278
279 /*
280 * unmap vertex/index buffers
281 */
282 if (ib) {
283 draw_set_indexes(draw, NULL, 0, 0);
284 if (ib_transfer)
285 pipe_buffer_unmap(pipe, ib_transfer);
286 pipe_resource_reference(&ibuffer.buffer, NULL);
287 }
288
289 out_unref_vertex:
290 for (attr = 0; attr < vp->num_inputs; attr++) {
291 if (vb_transfer[attr])
292 pipe_buffer_unmap(pipe, vb_transfer[attr]);
293 draw_set_mapped_vertex_buffer(draw, attr, NULL, 0);
294 pipe_resource_reference(&vbuffers[attr].buffer, NULL);
295 }
296 draw_set_vertex_buffers(draw, 0, vp->num_inputs, NULL);
297 }