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