mesa/st: Avoid extra references in the feedback draw function scope.
[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/arrayobj.h"
30 #include "main/image.h"
31 #include "main/macros.h"
32 #include "main/varray.h"
33
34 #include "vbo/vbo.h"
35
36 #include "st_context.h"
37 #include "st_atom.h"
38 #include "st_cb_bitmap.h"
39 #include "st_cb_bufferobjects.h"
40 #include "st_draw.h"
41 #include "st_program.h"
42
43 #include "pipe/p_context.h"
44 #include "pipe/p_defines.h"
45 #include "util/u_inlines.h"
46 #include "util/u_draw.h"
47
48 #include "draw/draw_private.h"
49 #include "draw/draw_context.h"
50
51
52 /**
53 * Set the (private) draw module's post-transformed vertex format when in
54 * GL_SELECT or GL_FEEDBACK mode or for glRasterPos.
55 */
56 static void
57 set_feedback_vertex_format(struct gl_context *ctx)
58 {
59 #if 0
60 struct st_context *st = st_context(ctx);
61 struct vertex_info vinfo;
62 GLuint i;
63
64 memset(&vinfo, 0, sizeof(vinfo));
65
66 if (ctx->RenderMode == GL_SELECT) {
67 assert(ctx->RenderMode == GL_SELECT);
68 vinfo.num_attribs = 1;
69 vinfo.format[0] = FORMAT_4F;
70 vinfo.interp_mode[0] = INTERP_LINEAR;
71 }
72 else {
73 /* GL_FEEDBACK, or glRasterPos */
74 /* emit all attribs (pos, color, texcoord) as GLfloat[4] */
75 vinfo.num_attribs = st->state.vs->cso->state.num_outputs;
76 for (i = 0; i < vinfo.num_attribs; i++) {
77 vinfo.format[i] = FORMAT_4F;
78 vinfo.interp_mode[i] = INTERP_LINEAR;
79 }
80 }
81
82 draw_set_vertex_info(st->draw, &vinfo);
83 #endif
84 }
85
86
87 /**
88 * Helper for drawing current vertex arrays.
89 */
90 static void
91 draw_arrays(struct draw_context *draw, unsigned mode,
92 unsigned start, unsigned count)
93 {
94 struct pipe_draw_info info;
95
96 util_draw_init_info(&info);
97
98 info.mode = mode;
99 info.start = start;
100 info.count = count;
101 info.min_index = start;
102 info.max_index = start + count - 1;
103
104 draw_vbo(draw, &info);
105 }
106
107
108 /**
109 * Called by VBO to draw arrays when in selection or feedback mode and
110 * to implement glRasterPos.
111 * This is very much like the normal draw_vbo() function above.
112 * Look at code refactoring some day.
113 */
114 void
115 st_feedback_draw_vbo(struct gl_context *ctx,
116 const struct _mesa_prim *prims,
117 GLuint nr_prims,
118 const struct _mesa_index_buffer *ib,
119 GLboolean index_bounds_valid,
120 GLuint min_index,
121 GLuint max_index,
122 struct gl_transform_feedback_object *tfb_vertcount,
123 unsigned stream,
124 struct gl_buffer_object *indirect)
125 {
126 struct st_context *st = st_context(ctx);
127 struct pipe_context *pipe = st->pipe;
128 struct draw_context *draw = st_get_draw_context(st);
129 const struct st_vertex_program *vp;
130 const struct pipe_shader_state *vs;
131 struct pipe_vertex_buffer vbuffers[PIPE_MAX_SHADER_INPUTS];
132 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
133 struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS] = {NULL};
134 struct pipe_transfer *ib_transfer = NULL;
135 GLuint attr, i;
136 const void *mapped_indices = NULL;
137
138 if (!draw)
139 return;
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 /* loop over TGSI shader inputs to determine vertex buffer
171 * and attribute info
172 */
173 for (attr = 0; attr < vp->num_inputs; attr++) {
174 const GLuint mesaAttr = vp->index_to_input[attr];
175 const struct gl_vertex_buffer_binding *binding;
176 const struct gl_array_attributes *attrib;
177 void *map;
178
179 _mesa_draw_attrib_and_binding(ctx, mesaAttr, &attrib, &binding);
180
181 if (_mesa_is_bufferobj(binding->BufferObj)) {
182 /* Attribute data is in a VBO. */
183 struct st_buffer_object *stobj = st_buffer_object(binding->BufferObj);
184 assert(stobj->buffer);
185
186 vbuffers[attr].buffer.resource = NULL;
187 vbuffers[attr].is_user_buffer = false;
188 vbuffers[attr].buffer.resource = stobj->buffer;
189 vbuffers[attr].buffer_offset = _mesa_draw_binding_offset(binding);
190 velements[attr].src_offset =
191 _mesa_draw_attributes_relative_offset(attrib);
192
193 /* map the attrib buffer */
194 map = pipe_buffer_map(pipe, vbuffers[attr].buffer.resource,
195 PIPE_TRANSFER_READ,
196 &vb_transfer[attr]);
197 draw_set_mapped_vertex_buffer(draw, attr, map,
198 vbuffers[attr].buffer.resource->width0);
199 }
200 else {
201 /* Attribute data is in a user space array. */
202 vbuffers[attr].buffer.user = attrib->Ptr;
203 vbuffers[attr].is_user_buffer = true;
204 vbuffers[attr].buffer_offset = 0;
205 velements[attr].src_offset = 0;
206
207 draw_set_mapped_vertex_buffer(draw, attr,
208 vbuffers[attr].buffer.user, ~0);
209 }
210
211 /* common-case setup */
212 vbuffers[attr].stride = binding->Stride; /* in bytes */
213 velements[attr].instance_divisor = 0;
214 velements[attr].vertex_buffer_index = attr;
215 velements[attr].src_format = st_pipe_vertex_format(&attrib->Format);
216 assert(velements[attr].src_format);
217
218 /* tell draw about this attribute */
219 #if 0
220 draw_set_vertex_buffer(draw, attr, &vbuffer[attr]);
221 #endif
222 }
223
224 draw_set_vertex_buffers(draw, 0, vp->num_inputs, vbuffers);
225 draw_set_vertex_elements(draw, vp->num_inputs, velements);
226
227 unsigned start = 0;
228
229 if (ib) {
230 struct gl_buffer_object *bufobj = ib->obj;
231 unsigned index_size = ib->index_size;
232
233 if (index_size == 0)
234 goto out_unref_vertex;
235
236 if (bufobj && bufobj->Name) {
237 struct st_buffer_object *stobj = st_buffer_object(bufobj);
238
239 start = pointer_to_offset(ib->ptr) / index_size;
240 mapped_indices = pipe_buffer_map(pipe, stobj->buffer,
241 PIPE_TRANSFER_READ, &ib_transfer);
242 }
243 else {
244 mapped_indices = ib->ptr;
245 }
246
247 draw_set_indexes(draw,
248 (ubyte *) mapped_indices,
249 index_size, ~0);
250 }
251
252 /* set the constant buffer */
253 draw_set_mapped_constant_buffer(st->draw, PIPE_SHADER_VERTEX, 0,
254 st->state.constants[PIPE_SHADER_VERTEX].ptr,
255 st->state.constants[PIPE_SHADER_VERTEX].size);
256
257
258 /* draw here */
259 for (i = 0; i < nr_prims; i++) {
260 draw_arrays(draw, prims[i].mode, start + prims[i].start, prims[i].count);
261 }
262
263
264 /*
265 * unmap vertex/index buffers
266 */
267 if (ib) {
268 draw_set_indexes(draw, NULL, 0, 0);
269 if (ib_transfer)
270 pipe_buffer_unmap(pipe, ib_transfer);
271 }
272
273 out_unref_vertex:
274 for (attr = 0; attr < vp->num_inputs; attr++) {
275 if (vb_transfer[attr])
276 pipe_buffer_unmap(pipe, vb_transfer[attr]);
277 draw_set_mapped_vertex_buffer(draw, attr, NULL, 0);
278 }
279 draw_set_vertex_buffers(draw, 0, vp->num_inputs, NULL);
280 }