mesa: remove FEATURE_feedback define.
[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 /**
50 * Set the (private) draw module's post-transformed vertex format when in
51 * GL_SELECT or GL_FEEDBACK mode or for glRasterPos.
52 */
53 static void
54 set_feedback_vertex_format(struct gl_context *ctx)
55 {
56 #if 0
57 struct st_context *st = st_context(ctx);
58 struct vertex_info vinfo;
59 GLuint i;
60
61 memset(&vinfo, 0, sizeof(vinfo));
62
63 if (ctx->RenderMode == GL_SELECT) {
64 assert(ctx->RenderMode == GL_SELECT);
65 vinfo.num_attribs = 1;
66 vinfo.format[0] = FORMAT_4F;
67 vinfo.interp_mode[0] = INTERP_LINEAR;
68 }
69 else {
70 /* GL_FEEDBACK, or glRasterPos */
71 /* emit all attribs (pos, color, texcoord) as GLfloat[4] */
72 vinfo.num_attribs = st->state.vs->cso->state.num_outputs;
73 for (i = 0; i < vinfo.num_attribs; i++) {
74 vinfo.format[i] = FORMAT_4F;
75 vinfo.interp_mode[i] = INTERP_LINEAR;
76 }
77 }
78
79 draw_set_vertex_info(st->draw, &vinfo);
80 #endif
81 }
82
83
84 /**
85 * Called by VBO to draw arrays when in selection or feedback mode and
86 * to implement glRasterPos.
87 * This is very much like the normal draw_vbo() function above.
88 * Look at code refactoring some day.
89 */
90 void
91 st_feedback_draw_vbo(struct gl_context *ctx,
92 const struct _mesa_prim *prims,
93 GLuint nr_prims,
94 const struct _mesa_index_buffer *ib,
95 GLboolean index_bounds_valid,
96 GLuint min_index,
97 GLuint max_index,
98 struct gl_transform_feedback_object *tfb_vertcount)
99 {
100 struct st_context *st = st_context(ctx);
101 struct pipe_context *pipe = st->pipe;
102 struct draw_context *draw = st->draw;
103 const struct st_vertex_program *vp;
104 const struct pipe_shader_state *vs;
105 struct pipe_vertex_buffer vbuffers[PIPE_MAX_SHADER_INPUTS];
106 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
107 struct pipe_index_buffer ibuffer;
108 struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS] = {NULL};
109 struct pipe_transfer *ib_transfer = NULL;
110 const struct gl_client_array **arrays = ctx->Array._DrawArrays;
111 GLuint attr, i;
112 const GLubyte *low_addr = NULL;
113 const void *mapped_indices = NULL;
114
115 assert(draw);
116
117 st_validate_state(st);
118
119 if (!index_bounds_valid)
120 vbo_get_minmax_indices(ctx, prims, ib, &min_index, &max_index, nr_prims);
121
122 /* must get these after state validation! */
123 vp = st->vp;
124 vs = &st->vp_variant->tgsi;
125
126 if (!st->vp_variant->draw_shader) {
127 st->vp_variant->draw_shader = draw_create_vertex_shader(draw, vs);
128 }
129
130 /*
131 * Set up the draw module's state.
132 *
133 * We'd like to do this less frequently, but the normal state-update
134 * code sends state updates to the pipe, not to our private draw module.
135 */
136 assert(draw);
137 draw_set_viewport_state(draw, &st->state.viewport);
138 draw_set_clip_state(draw, &st->state.clip);
139 draw_set_rasterizer_state(draw, &st->state.rasterizer, NULL);
140 draw_bind_vertex_shader(draw, st->vp_variant->draw_shader);
141 set_feedback_vertex_format(ctx);
142
143 /* Find the lowest address of the arrays we're drawing */
144 if (vp->num_inputs) {
145 low_addr = arrays[vp->index_to_input[0]]->Ptr;
146
147 for (attr = 1; attr < vp->num_inputs; attr++) {
148 const GLubyte *start = arrays[vp->index_to_input[attr]]->Ptr;
149 low_addr = MIN2(low_addr, start);
150 }
151 }
152
153 /* loop over TGSI shader inputs to determine vertex buffer
154 * and attribute info
155 */
156 for (attr = 0; attr < vp->num_inputs; attr++) {
157 const GLuint mesaAttr = vp->index_to_input[attr];
158 struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
159 void *map;
160
161 if (bufobj && bufobj->Name) {
162 /* Attribute data is in a VBO.
163 * Recall that for VBOs, the gl_client_array->Ptr field is
164 * really an offset from the start of the VBO, not a pointer.
165 */
166 struct st_buffer_object *stobj = st_buffer_object(bufobj);
167 assert(stobj->buffer);
168
169 vbuffers[attr].buffer = NULL;
170 vbuffers[attr].user_buffer = NULL;
171 pipe_resource_reference(&vbuffers[attr].buffer, stobj->buffer);
172 vbuffers[attr].buffer_offset = pointer_to_offset(low_addr);
173 velements[attr].src_offset = arrays[mesaAttr]->Ptr - low_addr;
174
175 /* map the attrib buffer */
176 map = pipe_buffer_map(pipe, vbuffers[attr].buffer,
177 PIPE_TRANSFER_READ,
178 &vb_transfer[attr]);
179 draw_set_mapped_vertex_buffer(draw, attr, map);
180 }
181 else {
182 vbuffers[attr].buffer = NULL;
183 vbuffers[attr].user_buffer = arrays[mesaAttr]->Ptr;
184 vbuffers[attr].buffer_offset = 0;
185 velements[attr].src_offset = 0;
186
187 draw_set_mapped_vertex_buffer(draw, attr, vbuffers[attr].user_buffer);
188 }
189
190 /* common-case setup */
191 vbuffers[attr].stride = arrays[mesaAttr]->StrideB; /* in bytes */
192 velements[attr].instance_divisor = 0;
193 velements[attr].vertex_buffer_index = attr;
194 velements[attr].src_format =
195 st_pipe_vertex_format(arrays[mesaAttr]->Type,
196 arrays[mesaAttr]->Size,
197 arrays[mesaAttr]->Format,
198 arrays[mesaAttr]->Normalized,
199 arrays[mesaAttr]->Integer);
200 assert(velements[attr].src_format);
201
202 /* tell draw about this attribute */
203 #if 0
204 draw_set_vertex_buffer(draw, attr, &vbuffer[attr]);
205 #endif
206 }
207
208 draw_set_vertex_buffers(draw, vp->num_inputs, vbuffers);
209 draw_set_vertex_elements(draw, vp->num_inputs, velements);
210
211 memset(&ibuffer, 0, sizeof(ibuffer));
212 if (ib) {
213 struct gl_buffer_object *bufobj = ib->obj;
214
215 ibuffer.index_size = vbo_sizeof_ib_type(ib->type);
216 if (ibuffer.index_size == 0)
217 goto out_unref_vertex;
218
219 if (bufobj && bufobj->Name) {
220 struct st_buffer_object *stobj = st_buffer_object(bufobj);
221
222 pipe_resource_reference(&ibuffer.buffer, stobj->buffer);
223 ibuffer.offset = pointer_to_offset(ib->ptr);
224
225 mapped_indices = pipe_buffer_map(pipe, stobj->buffer,
226 PIPE_TRANSFER_READ, &ib_transfer);
227 }
228 else {
229 /* skip setting ibuffer.buffer as the draw module does not use it */
230 mapped_indices = ib->ptr;
231 }
232
233 draw_set_indexes(draw,
234 (ubyte *) mapped_indices + ibuffer.offset,
235 ibuffer.index_size);
236 }
237
238 /* set the constant buffer */
239 draw_set_mapped_constant_buffer(st->draw, PIPE_SHADER_VERTEX, 0,
240 st->state.constants[PIPE_SHADER_VERTEX].ptr,
241 st->state.constants[PIPE_SHADER_VERTEX].size);
242
243
244 /* draw here */
245 for (i = 0; i < nr_prims; i++) {
246 draw_arrays(draw, prims[i].mode, prims[i].start, prims[i].count);
247 }
248
249
250 /*
251 * unmap vertex/index buffers
252 */
253 if (ib) {
254 draw_set_indexes(draw, NULL, 0);
255 if (ib_transfer)
256 pipe_buffer_unmap(pipe, ib_transfer);
257 pipe_resource_reference(&ibuffer.buffer, NULL);
258 }
259
260 out_unref_vertex:
261 for (attr = 0; attr < vp->num_inputs; attr++) {
262 if (vb_transfer[attr])
263 pipe_buffer_unmap(pipe, vb_transfer[attr]);
264 draw_set_mapped_vertex_buffer(draw, attr, NULL);
265 pipe_resource_reference(&vbuffers[attr].buffer, NULL);
266 }
267 draw_set_vertex_buffers(draw, 0, NULL);
268 }