6ec6d5c16f4067d749ea09ee9e4fd9ff97166ffb
[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 struct st_vp_variant *vp_variant;
131 const struct pipe_shader_state *vs;
132 struct pipe_vertex_buffer vbuffers[PIPE_MAX_SHADER_INPUTS];
133 unsigned num_vbuffers = 0;
134 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
135 struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS] = {NULL};
136 struct pipe_transfer *ib_transfer = NULL;
137 GLuint i;
138 const void *mapped_indices = NULL;
139
140 if (!draw)
141 return;
142
143 st_flush_bitmap_cache(st);
144 st_invalidate_readpix_cache(st);
145
146 st_validate_state(st, ST_PIPELINE_RENDER);
147
148 if (!index_bounds_valid)
149 vbo_get_minmax_indices(ctx, prims, ib, &min_index, &max_index, nr_prims);
150
151 /* must get these after state validation! */
152 vp = st->vp;
153 vp_variant = st->vp_variant;
154 vs = &vp_variant->tgsi;
155
156 if (!vp_variant->draw_shader) {
157 vp_variant->draw_shader = draw_create_vertex_shader(draw, vs);
158 }
159
160 /*
161 * Set up the draw module's state.
162 *
163 * We'd like to do this less frequently, but the normal state-update
164 * code sends state updates to the pipe, not to our private draw module.
165 */
166 assert(draw);
167 draw_set_viewport_states(draw, 0, 1, &st->state.viewport[0]);
168 draw_set_clip_state(draw, &st->state.clip);
169 draw_set_rasterizer_state(draw, &st->state.rasterizer, NULL);
170 draw_bind_vertex_shader(draw, vp_variant->draw_shader);
171 set_feedback_vertex_format(ctx);
172
173 /* Must setup these after state validation! */
174 /* Setup arrays */
175 st_setup_arrays(st, vp, vp_variant, velements, vbuffers, &num_vbuffers);
176 /* Setup current values as userspace arrays */
177 st_setup_current_user(st, vp, vp_variant, velements, vbuffers, &num_vbuffers);
178
179 /* Map all buffers and tell draw about their mapping */
180 for (unsigned buf = 0; buf < num_vbuffers; ++buf) {
181 struct pipe_vertex_buffer *vbuffer = &vbuffers[buf];
182
183 if (vbuffer->is_user_buffer) {
184 draw_set_mapped_vertex_buffer(draw, buf, vbuffer->buffer.user, ~0);
185 } else {
186 void *map = pipe_buffer_map(pipe, vbuffer->buffer.resource,
187 PIPE_TRANSFER_READ, &vb_transfer[buf]);
188 draw_set_mapped_vertex_buffer(draw, buf, map,
189 vbuffer->buffer.resource->width0);
190 }
191 }
192
193 draw_set_vertex_buffers(draw, 0, num_vbuffers, vbuffers);
194 draw_set_vertex_elements(draw, vp->num_inputs, velements);
195
196 unsigned start = 0;
197
198 if (ib) {
199 struct gl_buffer_object *bufobj = ib->obj;
200 unsigned index_size = ib->index_size;
201
202 if (index_size == 0)
203 goto out_unref_vertex;
204
205 if (bufobj && bufobj->Name) {
206 struct st_buffer_object *stobj = st_buffer_object(bufobj);
207
208 start = pointer_to_offset(ib->ptr) / index_size;
209 mapped_indices = pipe_buffer_map(pipe, stobj->buffer,
210 PIPE_TRANSFER_READ, &ib_transfer);
211 }
212 else {
213 mapped_indices = ib->ptr;
214 }
215
216 draw_set_indexes(draw,
217 (ubyte *) mapped_indices,
218 index_size, ~0);
219 }
220
221 /* set the constant buffer */
222 draw_set_mapped_constant_buffer(st->draw, PIPE_SHADER_VERTEX, 0,
223 st->state.constants[PIPE_SHADER_VERTEX].ptr,
224 st->state.constants[PIPE_SHADER_VERTEX].size);
225
226
227 /* draw here */
228 for (i = 0; i < nr_prims; i++) {
229 draw_arrays(draw, prims[i].mode, start + prims[i].start, prims[i].count);
230 }
231
232
233 /*
234 * unmap vertex/index buffers
235 */
236 if (ib) {
237 draw_set_indexes(draw, NULL, 0, 0);
238 if (ib_transfer)
239 pipe_buffer_unmap(pipe, ib_transfer);
240 }
241
242 out_unref_vertex:
243 for (unsigned buf = 0; buf < num_vbuffers; ++buf) {
244 if (vb_transfer[buf])
245 pipe_buffer_unmap(pipe, vb_transfer[buf]);
246 draw_set_mapped_vertex_buffer(draw, buf, NULL, 0);
247 }
248 draw_set_vertex_buffers(draw, 0, num_vbuffers, NULL);
249 }