gallium: pass cso_velems_state into cso_context instead of pipe_vertex_element
[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 #include "util/format/u_format.h"
49
50 #include "draw/draw_private.h"
51 #include "draw/draw_context.h"
52
53
54 /**
55 * Set the (private) draw module's post-transformed vertex format when in
56 * GL_SELECT or GL_FEEDBACK mode or for glRasterPos.
57 */
58 static void
59 set_feedback_vertex_format(struct gl_context *ctx)
60 {
61 #if 0
62 struct st_context *st = st_context(ctx);
63 struct vertex_info vinfo;
64 GLuint i;
65
66 memset(&vinfo, 0, sizeof(vinfo));
67
68 if (ctx->RenderMode == GL_SELECT) {
69 assert(ctx->RenderMode == GL_SELECT);
70 vinfo.num_attribs = 1;
71 vinfo.format[0] = FORMAT_4F;
72 vinfo.interp_mode[0] = INTERP_LINEAR;
73 }
74 else {
75 /* GL_FEEDBACK, or glRasterPos */
76 /* emit all attribs (pos, color, texcoord) as GLfloat[4] */
77 vinfo.num_attribs = st->state.vs->cso->state.num_outputs;
78 for (i = 0; i < vinfo.num_attribs; i++) {
79 vinfo.format[i] = FORMAT_4F;
80 vinfo.interp_mode[i] = INTERP_LINEAR;
81 }
82 }
83
84 draw_set_vertex_info(st->draw, &vinfo);
85 #endif
86 }
87
88
89 /**
90 * Called by VBO to draw arrays when in selection or feedback mode and
91 * to implement glRasterPos.
92 * This function mirrors the normal st_draw_vbo().
93 * Look at code refactoring some day.
94 */
95 void
96 st_feedback_draw_vbo(struct gl_context *ctx,
97 const struct _mesa_prim *prims,
98 GLuint nr_prims,
99 const struct _mesa_index_buffer *ib,
100 GLboolean index_bounds_valid,
101 GLuint min_index,
102 GLuint max_index,
103 struct gl_transform_feedback_object *tfb_vertcount,
104 unsigned stream)
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_common_variant *vp_variant;
111 struct pipe_vertex_buffer vbuffers[PIPE_MAX_SHADER_INPUTS];
112 unsigned num_vbuffers = 0;
113 struct cso_velems_state velements;
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 struct st_common_variant_key key;
140 /* We have to use memcpy to make sure that all bits are copied. */
141 memcpy(&key, &st->vp_variant->key, sizeof(key));
142 key.is_draw_shader = true;
143
144 vp = (struct st_vertex_program *)st->vp;
145 vp_variant = st_get_vp_variant(st, st->vp, &key);
146
147 /*
148 * Set up the draw module's state.
149 *
150 * We'd like to do this less frequently, but the normal state-update
151 * code sends state updates to the pipe, not to our private draw module.
152 */
153 assert(draw);
154 draw_set_viewport_states(draw, 0, 1, &st->state.viewport[0]);
155 draw_set_clip_state(draw, &st->state.clip);
156 draw_set_rasterizer_state(draw, &st->state.rasterizer, NULL);
157 draw_bind_vertex_shader(draw, vp_variant->base.driver_shader);
158 set_feedback_vertex_format(ctx);
159
160 /* Must setup these after state validation! */
161 /* Setup arrays */
162 bool uses_user_vertex_buffers;
163 st_setup_arrays(st, vp, vp_variant, &velements, vbuffers, &num_vbuffers,
164 &uses_user_vertex_buffers);
165 /* Setup current values as userspace arrays */
166 st_setup_current_user(st, vp, vp_variant, &velements, vbuffers, &num_vbuffers);
167
168 /* Map all buffers and tell draw about their mapping */
169 for (unsigned buf = 0; buf < num_vbuffers; ++buf) {
170 struct pipe_vertex_buffer *vbuffer = &vbuffers[buf];
171
172 if (vbuffer->is_user_buffer) {
173 draw_set_mapped_vertex_buffer(draw, buf, vbuffer->buffer.user, ~0);
174 } else {
175 void *map = pipe_buffer_map(pipe, vbuffer->buffer.resource,
176 PIPE_TRANSFER_READ, &vb_transfer[buf]);
177 draw_set_mapped_vertex_buffer(draw, buf, map,
178 vbuffer->buffer.resource->width0);
179 }
180 }
181
182 draw_set_vertex_buffers(draw, 0, num_vbuffers, vbuffers);
183 draw_set_vertex_elements(draw, vp->num_inputs, velements.velems);
184
185 unsigned start = 0;
186
187 if (ib) {
188 struct gl_buffer_object *bufobj = ib->obj;
189 unsigned index_size = ib->index_size;
190
191 if (index_size == 0)
192 goto out_unref_vertex;
193
194 if (bufobj && bufobj->Name) {
195 struct st_buffer_object *stobj = st_buffer_object(bufobj);
196
197 start = pointer_to_offset(ib->ptr) / index_size;
198 mapped_indices = pipe_buffer_map(pipe, stobj->buffer,
199 PIPE_TRANSFER_READ, &ib_transfer);
200 }
201 else {
202 mapped_indices = ib->ptr;
203 }
204
205 info.index_size = ib->index_size;
206 info.min_index = min_index;
207 info.max_index = max_index;
208 info.has_user_indices = true;
209 info.index.user = mapped_indices;
210
211 draw_set_indexes(draw,
212 (ubyte *) mapped_indices,
213 index_size, ~0);
214
215 if (ctx->Array._PrimitiveRestart) {
216 info.primitive_restart = true;
217 info.restart_index = _mesa_primitive_restart_index(ctx, info.index_size);
218 }
219 } else {
220 info.index_size = 0;
221 info.has_user_indices = false;
222 }
223
224 /* set constant buffers */
225 draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX, 0,
226 st->state.constants[PIPE_SHADER_VERTEX].ptr,
227 st->state.constants[PIPE_SHADER_VERTEX].size);
228
229 const struct gl_program *prog = &vp->Base.Base;
230 struct pipe_transfer *ubo_transfer[PIPE_MAX_CONSTANT_BUFFERS] = {0};
231 assert(prog->info.num_ubos <= ARRAY_SIZE(ubo_transfer));
232
233 for (unsigned i = 0; i < prog->info.num_ubos; i++) {
234 struct gl_buffer_binding *binding =
235 &st->ctx->UniformBufferBindings[prog->sh.UniformBlocks[i]->Binding];
236 struct st_buffer_object *st_obj = st_buffer_object(binding->BufferObject);
237 struct pipe_resource *buf = st_obj->buffer;
238
239 if (!buf)
240 continue;
241
242 unsigned offset = binding->Offset;
243 unsigned size = buf->width0 - offset;
244
245 /* AutomaticSize is FALSE if the buffer was set with BindBufferRange.
246 * Take the minimum just to be sure.
247 */
248 if (!binding->AutomaticSize)
249 size = MIN2(size, (unsigned) binding->Size);
250
251 void *ptr = pipe_buffer_map_range(pipe, buf, offset, size,
252 PIPE_TRANSFER_READ, &ubo_transfer[i]);
253
254 draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX, 1 + i, ptr,
255 size);
256 }
257
258 /* shader buffers */
259 /* TODO: atomic counter buffers */
260 struct pipe_transfer *ssbo_transfer[PIPE_MAX_SHADER_BUFFERS] = {0};
261
262 for (unsigned i = 0; i < prog->info.num_ssbos; i++) {
263 struct gl_buffer_binding *binding =
264 &st->ctx->ShaderStorageBufferBindings[
265 prog->sh.ShaderStorageBlocks[i]->Binding];
266 struct st_buffer_object *st_obj = st_buffer_object(binding->BufferObject);
267 struct pipe_resource *buf = st_obj->buffer;
268
269 if (!buf)
270 continue;
271
272 unsigned offset = binding->Offset;
273 unsigned size = buf->width0 - binding->Offset;
274
275 /* AutomaticSize is FALSE if the buffer was set with BindBufferRange.
276 * Take the minimum just to be sure.
277 */
278 if (!binding->AutomaticSize)
279 size = MIN2(size, (unsigned) binding->Size);
280
281 void *ptr = pipe_buffer_map_range(pipe, buf, offset, size,
282 PIPE_TRANSFER_READ, &ssbo_transfer[i]);
283
284 draw_set_mapped_shader_buffer(draw, PIPE_SHADER_VERTEX,
285 i, ptr, size);
286 }
287
288 /* samplers */
289 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
290 for (unsigned i = 0; i < st->state.num_vert_samplers; i++)
291 samplers[i] = &st->state.vert_samplers[i];
292
293 draw_set_samplers(draw, PIPE_SHADER_VERTEX, samplers,
294 st->state.num_vert_samplers);
295
296 /* sampler views */
297 draw_set_sampler_views(draw, PIPE_SHADER_VERTEX,
298 st->state.vert_sampler_views,
299 st->state.num_sampler_views[PIPE_SHADER_VERTEX]);
300
301 struct pipe_transfer *sv_transfer[PIPE_MAX_SAMPLERS][PIPE_MAX_TEXTURE_LEVELS];
302
303 for (unsigned i = 0; i < st->state.num_sampler_views[PIPE_SHADER_VERTEX]; i++) {
304 struct pipe_sampler_view *view = st->state.vert_sampler_views[i];
305 if (!view)
306 continue;
307
308 struct pipe_resource *res = view->texture;
309 unsigned width0 = res->width0;
310 unsigned num_layers = res->depth0;
311 unsigned first_level = 0;
312 unsigned last_level = 0;
313 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
314 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
315 uint32_t mip_offset[PIPE_MAX_TEXTURE_LEVELS];
316 uintptr_t mip_addr[PIPE_MAX_TEXTURE_LEVELS];
317 uintptr_t base_addr;
318
319 if (res->target != PIPE_BUFFER) {
320 first_level = view->u.tex.first_level;
321 last_level = view->u.tex.last_level;
322 num_layers = view->u.tex.last_layer - view->u.tex.first_layer + 1;
323 base_addr = UINTPTR_MAX;
324
325 for (unsigned j = first_level; j <= last_level; j++) {
326 unsigned map_layers = res->target == PIPE_TEXTURE_3D ?
327 util_num_layers(res, j) : num_layers;
328
329 sv_transfer[i][j] = NULL;
330 mip_addr[j] = (uintptr_t)
331 pipe_transfer_map_3d(pipe, res, j,
332 PIPE_TRANSFER_READ, 0, 0,
333 view->u.tex.first_layer,
334 u_minify(res->width0, j),
335 u_minify(res->height0, j),
336 map_layers, &sv_transfer[i][j]);
337 row_stride[j] = sv_transfer[i][j]->stride;
338 img_stride[j] = sv_transfer[i][j]->layer_stride;
339
340 /* Get the minimum address, because the draw module takes only
341 * 1 address for the whole texture + uint32 offsets for mip levels,
342 * so we need to convert mapped resource pointers into that scheme.
343 */
344 base_addr = MIN2(base_addr, mip_addr[j]);
345 }
346 for (unsigned j = first_level; j <= last_level; j++) {
347 /* TODO: The draw module should accept pointers for mipmap levels
348 * instead of offsets. This is unlikely to work on 64-bit archs.
349 */
350 assert(mip_addr[j] - base_addr <= UINT32_MAX);
351 mip_offset[j] = mip_addr[j] - base_addr;
352 }
353 } else {
354 width0 = view->u.buf.size / util_format_get_blocksize(view->format);
355
356 /* probably don't really need to fill that out */
357 mip_offset[0] = 0;
358 row_stride[0] = 0;
359 img_stride[0] = 0;
360
361 sv_transfer[i][0] = NULL;
362 base_addr = (uintptr_t)
363 pipe_buffer_map_range(pipe, res, view->u.buf.offset,
364 view->u.buf.size,
365 PIPE_TRANSFER_READ,
366 &sv_transfer[i][0]);
367 }
368
369 draw_set_mapped_texture(draw, PIPE_SHADER_VERTEX, i, width0,
370 res->height0, num_layers, first_level,
371 last_level, (void*)base_addr, row_stride,
372 img_stride, mip_offset);
373 }
374
375 /* shader images */
376 struct pipe_image_view images[PIPE_MAX_SHADER_IMAGES];
377 struct pipe_transfer *img_transfer[PIPE_MAX_SHADER_IMAGES] = {0};
378
379 for (unsigned i = 0; i < prog->info.num_images; i++) {
380 struct pipe_image_view *img = &images[i];
381
382 st_convert_image_from_unit(st, img, prog->sh.ImageUnits[i],
383 prog->sh.ImageAccess[i]);
384
385 struct pipe_resource *res = img->resource;
386 if (!res)
387 continue;
388
389 unsigned width, height, num_layers, row_stride, img_stride;
390 void *addr;
391
392 if (res->target != PIPE_BUFFER) {
393 width = u_minify(res->width0, img->u.tex.level);
394 height = u_minify(res->height0, img->u.tex.level);
395 num_layers = img->u.tex.last_layer - img->u.tex.first_layer + 1;
396
397 addr = pipe_transfer_map_3d(pipe, res, img->u.tex.level,
398 PIPE_TRANSFER_READ, 0, 0,
399 img->u.tex.first_layer,
400 width, height, num_layers,
401 &img_transfer[i]);
402 row_stride = img_transfer[i]->stride;
403 img_stride = img_transfer[i]->layer_stride;
404 } else {
405 width = img->u.buf.size / util_format_get_blocksize(img->format);
406
407 /* probably don't really need to fill that out */
408 row_stride = 0;
409 img_stride = 0;
410 height = num_layers = 1;
411
412 addr = pipe_buffer_map_range(pipe, res, img->u.buf.offset,
413 img->u.buf.size, PIPE_TRANSFER_READ,
414 &img_transfer[i]);
415 }
416
417 draw_set_mapped_image(draw, PIPE_SHADER_VERTEX, i, width, height,
418 num_layers, addr, row_stride, img_stride);
419 }
420 draw_set_images(draw, PIPE_SHADER_VERTEX, images, prog->info.num_images);
421
422 /* draw here */
423 for (i = 0; i < nr_prims; i++) {
424 info.count = prims[i].count;
425
426 if (!info.count)
427 continue;
428
429 info.mode = prims[i].mode;
430 info.start = start + prims[i].start;
431 info.start_instance = prims[i].base_instance;
432 info.instance_count = prims[i].num_instances;
433 info.index_bias = prims[i].basevertex;
434 info.drawid = prims[i].draw_id;
435 if (!ib) {
436 info.min_index = info.start;
437 info.max_index = info.start + info.count - 1;
438 }
439
440 draw_vbo(draw, &info);
441 }
442
443 /* unmap images */
444 for (unsigned i = 0; i < prog->info.num_images; i++) {
445 if (img_transfer[i]) {
446 draw_set_mapped_image(draw, PIPE_SHADER_VERTEX, i, 0, 0, 0, NULL, 0, 0);
447 pipe_transfer_unmap(pipe, img_transfer[i]);
448 }
449 }
450
451 /* unmap sampler views */
452 for (unsigned i = 0; i < st->state.num_sampler_views[PIPE_SHADER_VERTEX]; i++) {
453 struct pipe_sampler_view *view = st->state.vert_sampler_views[i];
454
455 if (view) {
456 if (view->texture->target != PIPE_BUFFER) {
457 for (unsigned j = view->u.tex.first_level;
458 j <= view->u.tex.last_level; j++) {
459 pipe_transfer_unmap(pipe, sv_transfer[i][j]);
460 }
461 } else {
462 pipe_transfer_unmap(pipe, sv_transfer[i][0]);
463 }
464 }
465 }
466
467 draw_set_samplers(draw, PIPE_SHADER_VERTEX, NULL, 0);
468 draw_set_sampler_views(draw, PIPE_SHADER_VERTEX, NULL, 0);
469
470 for (unsigned i = 0; i < prog->info.num_ssbos; i++) {
471 if (ssbo_transfer[i]) {
472 draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX, 1 + i,
473 NULL, 0);
474 pipe_buffer_unmap(pipe, ssbo_transfer[i]);
475 }
476 }
477
478 for (unsigned i = 0; i < prog->info.num_ubos; i++) {
479 if (ubo_transfer[i]) {
480 draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX, 1 + i,
481 NULL, 0);
482 pipe_buffer_unmap(pipe, ubo_transfer[i]);
483 }
484 }
485
486 /*
487 * unmap vertex/index buffers
488 */
489 if (ib) {
490 draw_set_indexes(draw, NULL, 0, 0);
491 if (ib_transfer)
492 pipe_buffer_unmap(pipe, ib_transfer);
493 }
494
495 out_unref_vertex:
496 for (unsigned buf = 0; buf < num_vbuffers; ++buf) {
497 if (vb_transfer[buf])
498 pipe_buffer_unmap(pipe, vb_transfer[buf]);
499 draw_set_mapped_vertex_buffer(draw, buf, NULL, 0);
500 }
501 draw_set_vertex_buffers(draw, 0, num_vbuffers, NULL);
502 }