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