1e1220bfe5279a4339d78ed96610f3780edc67ff
[mesa.git] / src / mesa / state_tracker / st_draw_feedback.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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/mfeatures.h"
32
33 #include "vbo/vbo.h"
34
35 #include "st_context.h"
36 #include "st_atom.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
45 #include "draw/draw_private.h"
46 #include "draw/draw_context.h"
47
48
49 #if FEATURE_feedback || FEATURE_rastpos
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 * Called by VBO to draw arrays when in selection or feedback mode and
88 * to implement glRasterPos.
89 * This is very much like the normal draw_vbo() function above.
90 * Look at code refactoring some day.
91 * Might move this into the failover module some day.
92 */
93 void
94 st_feedback_draw_vbo(struct gl_context *ctx,
95 const struct gl_client_array **arrays,
96 const struct _mesa_prim *prims,
97 GLuint nr_prims,
98 const struct _mesa_index_buffer *ib,
99 GLboolean index_bounds_valid,
100 GLuint min_index,
101 GLuint max_index)
102 {
103 struct st_context *st = st_context(ctx);
104 struct pipe_context *pipe = st->pipe;
105 struct draw_context *draw = st->draw;
106 const struct st_vertex_program *vp;
107 const struct pipe_shader_state *vs;
108 struct pipe_vertex_buffer vbuffers[PIPE_MAX_SHADER_INPUTS];
109 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
110 struct pipe_index_buffer ibuffer;
111 struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS];
112 struct pipe_transfer *ib_transfer = NULL;
113 GLuint attr, i;
114 const void *mapped_indices = NULL;
115
116 assert(draw);
117
118 st_validate_state(st);
119
120 if (!index_bounds_valid)
121 vbo_get_minmax_index(ctx, prims, ib, &min_index, &max_index);
122
123 /* must get these after state validation! */
124 vp = st->vp;
125 vs = &st->vp_variant->tgsi;
126
127 if (!st->vp_variant->draw_shader) {
128 st->vp_variant->draw_shader = draw_create_vertex_shader(draw, vs);
129 }
130
131 /*
132 * Set up the draw module's state.
133 *
134 * We'd like to do this less frequently, but the normal state-update
135 * code sends state updates to the pipe, not to our private draw module.
136 */
137 assert(draw);
138 draw_set_viewport_state(draw, &st->state.viewport);
139 draw_set_clip_state(draw, &st->state.clip);
140 draw_set_rasterizer_state(draw, &st->state.rasterizer, NULL);
141 draw_bind_vertex_shader(draw, st->vp_variant->draw_shader);
142 set_feedback_vertex_format(ctx);
143
144 /* loop over TGSI shader inputs to determine vertex buffer
145 * and attribute info
146 */
147 for (attr = 0; attr < vp->num_inputs; attr++) {
148 const GLuint mesaAttr = vp->index_to_input[attr];
149 struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
150 void *map;
151
152 if (bufobj && bufobj->Name) {
153 /* Attribute data is in a VBO.
154 * Recall that for VBOs, the gl_client_array->Ptr field is
155 * really an offset from the start of the VBO, not a pointer.
156 */
157 struct st_buffer_object *stobj = st_buffer_object(bufobj);
158 assert(stobj->buffer);
159
160 vbuffers[attr].buffer = NULL;
161 pipe_resource_reference(&vbuffers[attr].buffer, stobj->buffer);
162 vbuffers[attr].buffer_offset = pointer_to_offset(arrays[0]->Ptr);
163 velements[attr].src_offset = arrays[mesaAttr]->Ptr - arrays[0]->Ptr;
164 }
165 else {
166 /* attribute data is in user-space memory, not a VBO */
167 uint bytes = (arrays[mesaAttr]->Size
168 * _mesa_sizeof_type(arrays[mesaAttr]->Type)
169 * (max_index + 1));
170
171 /* wrap user data */
172 vbuffers[attr].buffer
173 = pipe_user_buffer_create(pipe->screen, (void *) arrays[mesaAttr]->Ptr,
174 bytes,
175 PIPE_BIND_VERTEX_BUFFER);
176 vbuffers[attr].buffer_offset = 0;
177 velements[attr].src_offset = 0;
178 }
179
180 /* common-case setup */
181 vbuffers[attr].stride = arrays[mesaAttr]->StrideB; /* in bytes */
182 velements[attr].instance_divisor = 0;
183 velements[attr].vertex_buffer_index = attr;
184 velements[attr].src_format =
185 st_pipe_vertex_format(arrays[mesaAttr]->Type,
186 arrays[mesaAttr]->Size,
187 arrays[mesaAttr]->Format,
188 arrays[mesaAttr]->Normalized);
189 assert(velements[attr].src_format);
190
191 /* tell draw about this attribute */
192 #if 0
193 draw_set_vertex_buffer(draw, attr, &vbuffer[attr]);
194 #endif
195
196 /* map the attrib buffer */
197 map = pipe_buffer_map(pipe, vbuffers[attr].buffer,
198 PIPE_TRANSFER_READ,
199 &vb_transfer[attr]);
200 draw_set_mapped_vertex_buffer(draw, attr, map);
201 }
202
203 draw_set_vertex_buffers(draw, vp->num_inputs, vbuffers);
204 draw_set_vertex_elements(draw, vp->num_inputs, velements);
205
206 memset(&ibuffer, 0, sizeof(ibuffer));
207 if (ib) {
208 struct gl_buffer_object *bufobj = ib->obj;
209
210 switch (ib->type) {
211 case GL_UNSIGNED_INT:
212 ibuffer.index_size = 4;
213 break;
214 case GL_UNSIGNED_SHORT:
215 ibuffer.index_size = 2;
216 break;
217 case GL_UNSIGNED_BYTE:
218 ibuffer.index_size = 1;
219 break;
220 default:
221 assert(0);
222 goto out_unref_vertex;
223 }
224
225 if (bufobj && bufobj->Name) {
226 struct st_buffer_object *stobj = st_buffer_object(bufobj);
227
228 pipe_resource_reference(&ibuffer.buffer, stobj->buffer);
229 ibuffer.offset = pointer_to_offset(ib->ptr);
230
231 mapped_indices = pipe_buffer_map(pipe, stobj->buffer,
232 PIPE_TRANSFER_READ, &ib_transfer);
233 }
234 else {
235 /* skip setting ibuffer.buffer as the draw module does not use it */
236 mapped_indices = ib->ptr;
237 }
238
239 draw_set_index_buffer(draw, &ibuffer);
240 draw_set_mapped_index_buffer(draw, mapped_indices);
241 }
242
243 /* set the constant buffer */
244 draw_set_mapped_constant_buffer(st->draw, PIPE_SHADER_VERTEX, 0,
245 st->state.constants[PIPE_SHADER_VERTEX].ptr,
246 st->state.constants[PIPE_SHADER_VERTEX].size);
247
248
249 /* draw here */
250 for (i = 0; i < nr_prims; i++) {
251 draw_arrays(draw, prims[i].mode, prims[i].start, prims[i].count);
252 }
253
254
255 /*
256 * unmap vertex/index buffers
257 */
258 if (ib) {
259 draw_set_mapped_index_buffer(draw, NULL);
260 draw_set_index_buffer(draw, NULL);
261
262 if (ib_transfer)
263 pipe_buffer_unmap(pipe, ib_transfer);
264 pipe_resource_reference(&ibuffer.buffer, NULL);
265 }
266
267 out_unref_vertex:
268 for (attr = 0; attr < vp->num_inputs; attr++) {
269 pipe_buffer_unmap(pipe, vb_transfer[attr]);
270 draw_set_mapped_vertex_buffer(draw, attr, NULL);
271 pipe_resource_reference(&vbuffers[attr].buffer, NULL);
272 }
273 draw_set_vertex_buffers(draw, 0, NULL);
274 }
275
276 #endif /* FEATURE_feedback || FEATURE_rastpos */
277