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