st/mesa: subclass st_vertex_program for VP-specific members
[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 #include "st_util.h"
43
44 #include "pipe/p_context.h"
45 #include "pipe/p_defines.h"
46 #include "util/u_inlines.h"
47 #include "util/u_draw.h"
48
49 #include "draw/draw_private.h"
50 #include "draw/draw_context.h"
51
52
53 /**
54 * Set the (private) draw module's post-transformed vertex format when in
55 * GL_SELECT or GL_FEEDBACK mode or for glRasterPos.
56 */
57 static void
58 set_feedback_vertex_format(struct gl_context *ctx)
59 {
60 #if 0
61 struct st_context *st = st_context(ctx);
62 struct vertex_info vinfo;
63 GLuint i;
64
65 memset(&vinfo, 0, sizeof(vinfo));
66
67 if (ctx->RenderMode == GL_SELECT) {
68 assert(ctx->RenderMode == GL_SELECT);
69 vinfo.num_attribs = 1;
70 vinfo.format[0] = FORMAT_4F;
71 vinfo.interp_mode[0] = INTERP_LINEAR;
72 }
73 else {
74 /* GL_FEEDBACK, or glRasterPos */
75 /* emit all attribs (pos, color, texcoord) as GLfloat[4] */
76 vinfo.num_attribs = st->state.vs->cso->state.num_outputs;
77 for (i = 0; i < vinfo.num_attribs; i++) {
78 vinfo.format[i] = FORMAT_4F;
79 vinfo.interp_mode[i] = INTERP_LINEAR;
80 }
81 }
82
83 draw_set_vertex_info(st->draw, &vinfo);
84 #endif
85 }
86
87
88 /**
89 * Called by VBO to draw arrays when in selection or feedback mode and
90 * to implement glRasterPos.
91 * This function mirrors the normal st_draw_vbo().
92 * Look at code refactoring some day.
93 */
94 void
95 st_feedback_draw_vbo(struct gl_context *ctx,
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 struct gl_transform_feedback_object *tfb_vertcount,
103 unsigned stream,
104 struct gl_buffer_object *indirect)
105 {
106 struct st_context *st = st_context(ctx);
107 struct pipe_context *pipe = st->pipe;
108 struct draw_context *draw = st_get_draw_context(st);
109 const struct st_vertex_program *vp;
110 struct st_vp_variant *vp_variant;
111 struct pipe_vertex_buffer vbuffers[PIPE_MAX_SHADER_INPUTS];
112 unsigned num_vbuffers = 0;
113 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
114 struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS] = {NULL};
115 struct pipe_transfer *ib_transfer = NULL;
116 GLuint i;
117 const void *mapped_indices = NULL;
118 struct pipe_draw_info info;
119
120 if (!draw)
121 return;
122
123 /* Initialize pipe_draw_info. */
124 info.primitive_restart = false;
125 info.vertices_per_patch = ctx->TessCtrlProgram.patch_vertices;
126 info.indirect = NULL;
127 info.count_from_stream_output = NULL;
128 info.restart_index = 0;
129
130 st_flush_bitmap_cache(st);
131 st_invalidate_readpix_cache(st);
132
133 st_validate_state(st, ST_PIPELINE_RENDER);
134
135 if (!index_bounds_valid)
136 vbo_get_minmax_indices(ctx, prims, ib, &min_index, &max_index, nr_prims);
137
138 /* must get these after state validation! */
139 vp = (struct st_vertex_program *)st->vp;
140 vp_variant = st->vp_variant;
141
142 struct pipe_shader_state state = {0};
143 state.type = PIPE_SHADER_IR_TGSI;
144 state.tokens = vp_variant->tokens;
145
146 if (!vp_variant->draw_shader) {
147 vp_variant->draw_shader = draw_create_vertex_shader(draw, &state);
148 }
149
150 /*
151 * Set up the draw module's state.
152 *
153 * We'd like to do this less frequently, but the normal state-update
154 * code sends state updates to the pipe, not to our private draw module.
155 */
156 assert(draw);
157 draw_set_viewport_states(draw, 0, 1, &st->state.viewport[0]);
158 draw_set_clip_state(draw, &st->state.clip);
159 draw_set_rasterizer_state(draw, &st->state.rasterizer, NULL);
160 draw_bind_vertex_shader(draw, vp_variant->draw_shader);
161 set_feedback_vertex_format(ctx);
162
163 /* Must setup these after state validation! */
164 /* Setup arrays */
165 st_setup_arrays(st, vp, vp_variant, velements, vbuffers, &num_vbuffers);
166 /* Setup current values as userspace arrays */
167 st_setup_current_user(st, vp, vp_variant, velements, vbuffers, &num_vbuffers);
168
169 /* Map all buffers and tell draw about their mapping */
170 for (unsigned buf = 0; buf < num_vbuffers; ++buf) {
171 struct pipe_vertex_buffer *vbuffer = &vbuffers[buf];
172
173 if (vbuffer->is_user_buffer) {
174 draw_set_mapped_vertex_buffer(draw, buf, vbuffer->buffer.user, ~0);
175 } else {
176 void *map = pipe_buffer_map(pipe, vbuffer->buffer.resource,
177 PIPE_TRANSFER_READ, &vb_transfer[buf]);
178 draw_set_mapped_vertex_buffer(draw, buf, map,
179 vbuffer->buffer.resource->width0);
180 }
181 }
182
183 draw_set_vertex_buffers(draw, 0, num_vbuffers, vbuffers);
184 draw_set_vertex_elements(draw, vp->num_inputs, velements);
185
186 unsigned start = 0;
187
188 if (ib) {
189 struct gl_buffer_object *bufobj = ib->obj;
190 unsigned index_size = ib->index_size;
191
192 if (index_size == 0)
193 goto out_unref_vertex;
194
195 if (bufobj && bufobj->Name) {
196 struct st_buffer_object *stobj = st_buffer_object(bufobj);
197
198 start = pointer_to_offset(ib->ptr) / index_size;
199 mapped_indices = pipe_buffer_map(pipe, stobj->buffer,
200 PIPE_TRANSFER_READ, &ib_transfer);
201 }
202 else {
203 mapped_indices = ib->ptr;
204 }
205
206 info.index_size = ib->index_size;
207 info.min_index = min_index;
208 info.max_index = max_index;
209 info.has_user_indices = true;
210 info.index.user = mapped_indices;
211
212 draw_set_indexes(draw,
213 (ubyte *) mapped_indices,
214 index_size, ~0);
215
216 if (ctx->Array._PrimitiveRestart) {
217 info.primitive_restart = true;
218 info.restart_index = _mesa_primitive_restart_index(ctx, info.index_size);
219 }
220 } else {
221 info.index_size = 0;
222 info.has_user_indices = false;
223 }
224
225 /* set the constant buffer */
226 draw_set_mapped_constant_buffer(st->draw, PIPE_SHADER_VERTEX, 0,
227 st->state.constants[PIPE_SHADER_VERTEX].ptr,
228 st->state.constants[PIPE_SHADER_VERTEX].size);
229
230
231 /* draw here */
232 for (i = 0; i < nr_prims; i++) {
233 info.count = prims[i].count;
234
235 if (!info.count)
236 continue;
237
238 info.mode = prims[i].mode;
239 info.start = start + prims[i].start;
240 info.start_instance = prims[i].base_instance;
241 info.instance_count = prims[i].num_instances;
242 info.index_bias = prims[i].basevertex;
243 info.drawid = prims[i].draw_id;
244 if (!ib) {
245 info.min_index = info.start;
246 info.max_index = info.start + info.count - 1;
247 }
248
249 draw_vbo(draw, &info);
250 }
251
252
253 /*
254 * unmap vertex/index buffers
255 */
256 if (ib) {
257 draw_set_indexes(draw, NULL, 0, 0);
258 if (ib_transfer)
259 pipe_buffer_unmap(pipe, ib_transfer);
260 }
261
262 out_unref_vertex:
263 for (unsigned buf = 0; buf < num_vbuffers; ++buf) {
264 if (vb_transfer[buf])
265 pipe_buffer_unmap(pipe, vb_transfer[buf]);
266 draw_set_mapped_vertex_buffer(draw, buf, NULL, 0);
267 }
268 draw_set_vertex_buffers(draw, 0, num_vbuffers, NULL);
269 }