st/mesa: remove struct st_vp_variant in favor of st_common_variant
[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 struct gl_buffer_object *indirect)
106 {
107 struct st_context *st = st_context(ctx);
108 struct pipe_context *pipe = st->pipe;
109 struct draw_context *draw = st_get_draw_context(st);
110 const struct st_vertex_program *vp;
111 struct st_common_variant *vp_variant;
112 struct pipe_vertex_buffer vbuffers[PIPE_MAX_SHADER_INPUTS];
113 unsigned num_vbuffers = 0;
114 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
115 struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS] = {NULL};
116 struct pipe_transfer *ib_transfer = NULL;
117 GLuint i;
118 const void *mapped_indices = NULL;
119 struct pipe_draw_info info;
120
121 if (!draw)
122 return;
123
124 /* Initialize pipe_draw_info. */
125 info.primitive_restart = false;
126 info.vertices_per_patch = ctx->TessCtrlProgram.patch_vertices;
127 info.indirect = NULL;
128 info.count_from_stream_output = NULL;
129 info.restart_index = 0;
130
131 st_flush_bitmap_cache(st);
132 st_invalidate_readpix_cache(st);
133
134 st_validate_state(st, ST_PIPELINE_RENDER);
135
136 if (!index_bounds_valid)
137 vbo_get_minmax_indices(ctx, prims, ib, &min_index, &max_index, nr_prims);
138
139 /* must get these after state validation! */
140 struct st_common_variant_key key;
141 /* We have to use memcpy to make sure that all bits are copied. */
142 memcpy(&key, &st->vp_variant->key, sizeof(key));
143 key.is_draw_shader = true;
144
145 vp = (struct st_vertex_program *)st->vp;
146 vp_variant = st_get_vp_variant(st, st->vp, &key);
147
148 /*
149 * Set up the draw module's state.
150 *
151 * We'd like to do this less frequently, but the normal state-update
152 * code sends state updates to the pipe, not to our private draw module.
153 */
154 assert(draw);
155 draw_set_viewport_states(draw, 0, 1, &st->state.viewport[0]);
156 draw_set_clip_state(draw, &st->state.clip);
157 draw_set_rasterizer_state(draw, &st->state.rasterizer, NULL);
158 draw_bind_vertex_shader(draw, vp_variant->base.driver_shader);
159 set_feedback_vertex_format(ctx);
160
161 /* Must setup these after state validation! */
162 /* Setup arrays */
163 st_setup_arrays(st, vp, vp_variant, velements, vbuffers, &num_vbuffers);
164 /* Setup current values as userspace arrays */
165 st_setup_current_user(st, vp, vp_variant, velements, vbuffers, &num_vbuffers);
166
167 /* Map all buffers and tell draw about their mapping */
168 for (unsigned buf = 0; buf < num_vbuffers; ++buf) {
169 struct pipe_vertex_buffer *vbuffer = &vbuffers[buf];
170
171 if (vbuffer->is_user_buffer) {
172 draw_set_mapped_vertex_buffer(draw, buf, vbuffer->buffer.user, ~0);
173 } else {
174 void *map = pipe_buffer_map(pipe, vbuffer->buffer.resource,
175 PIPE_TRANSFER_READ, &vb_transfer[buf]);
176 draw_set_mapped_vertex_buffer(draw, buf, map,
177 vbuffer->buffer.resource->width0);
178 }
179 }
180
181 draw_set_vertex_buffers(draw, 0, num_vbuffers, vbuffers);
182 draw_set_vertex_elements(draw, vp->num_inputs, velements);
183
184 unsigned start = 0;
185
186 if (ib) {
187 struct gl_buffer_object *bufobj = ib->obj;
188 unsigned index_size = ib->index_size;
189
190 if (index_size == 0)
191 goto out_unref_vertex;
192
193 if (bufobj && bufobj->Name) {
194 struct st_buffer_object *stobj = st_buffer_object(bufobj);
195
196 start = pointer_to_offset(ib->ptr) / index_size;
197 mapped_indices = pipe_buffer_map(pipe, stobj->buffer,
198 PIPE_TRANSFER_READ, &ib_transfer);
199 }
200 else {
201 mapped_indices = ib->ptr;
202 }
203
204 info.index_size = ib->index_size;
205 info.min_index = min_index;
206 info.max_index = max_index;
207 info.has_user_indices = true;
208 info.index.user = mapped_indices;
209
210 draw_set_indexes(draw,
211 (ubyte *) mapped_indices,
212 index_size, ~0);
213
214 if (ctx->Array._PrimitiveRestart) {
215 info.primitive_restart = true;
216 info.restart_index = _mesa_primitive_restart_index(ctx, info.index_size);
217 }
218 } else {
219 info.index_size = 0;
220 info.has_user_indices = false;
221 }
222
223 /* set constant buffers */
224 draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX, 0,
225 st->state.constants[PIPE_SHADER_VERTEX].ptr,
226 st->state.constants[PIPE_SHADER_VERTEX].size);
227
228 const struct gl_program *prog = &vp->Base.Base;
229 struct pipe_transfer *ubo_transfer[PIPE_MAX_CONSTANT_BUFFERS] = {0};
230 assert(prog->info.num_ubos <= ARRAY_SIZE(ubo_transfer));
231
232 for (unsigned i = 0; i < prog->info.num_ubos; i++) {
233 struct gl_buffer_binding *binding =
234 &st->ctx->UniformBufferBindings[prog->sh.UniformBlocks[i]->Binding];
235 struct st_buffer_object *st_obj = st_buffer_object(binding->BufferObject);
236 struct pipe_resource *buf = st_obj->buffer;
237
238 if (!buf)
239 continue;
240
241 unsigned offset = binding->Offset;
242 unsigned size = buf->width0 - offset;
243
244 /* AutomaticSize is FALSE if the buffer was set with BindBufferRange.
245 * Take the minimum just to be sure.
246 */
247 if (!binding->AutomaticSize)
248 size = MIN2(size, (unsigned) binding->Size);
249
250 void *ptr = pipe_buffer_map_range(pipe, buf, offset, size,
251 PIPE_TRANSFER_READ, &ubo_transfer[i]);
252
253 draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX, 1 + i, ptr,
254 size);
255 }
256
257 /* shader buffers */
258 /* TODO: atomic counter buffers */
259 struct pipe_transfer *ssbo_transfer[PIPE_MAX_SHADER_BUFFERS] = {0};
260 unsigned ssbo_first_slot = st->has_hw_atomics ? 0 :
261 st->ctx->Const.Program[MESA_SHADER_VERTEX].MaxAtomicBuffers;
262
263 for (unsigned i = 0; i < prog->info.num_ssbos; i++) {
264 struct gl_buffer_binding *binding =
265 &st->ctx->ShaderStorageBufferBindings[
266 prog->sh.ShaderStorageBlocks[i]->Binding];
267 struct st_buffer_object *st_obj = st_buffer_object(binding->BufferObject);
268 struct pipe_resource *buf = st_obj->buffer;
269
270 if (!buf)
271 continue;
272
273 unsigned offset = binding->Offset;
274 unsigned size = buf->width0 - binding->Offset;
275
276 /* AutomaticSize is FALSE if the buffer was set with BindBufferRange.
277 * Take the minimum just to be sure.
278 */
279 if (!binding->AutomaticSize)
280 size = MIN2(size, (unsigned) binding->Size);
281
282 void *ptr = pipe_buffer_map_range(pipe, buf, offset, size,
283 PIPE_TRANSFER_READ, &ssbo_transfer[i]);
284
285 draw_set_mapped_shader_buffer(draw, PIPE_SHADER_VERTEX,
286 ssbo_first_slot + i, ptr, size);
287 }
288
289 /* samplers */
290 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
291 for (unsigned i = 0; i < st->state.num_vert_samplers; i++)
292 samplers[i] = &st->state.vert_samplers[i];
293
294 draw_set_samplers(draw, PIPE_SHADER_VERTEX, samplers,
295 st->state.num_vert_samplers);
296
297 /* sampler views */
298 draw_set_sampler_views(draw, PIPE_SHADER_VERTEX,
299 st->state.vert_sampler_views,
300 st->state.num_sampler_views[PIPE_SHADER_VERTEX]);
301
302 struct pipe_transfer *sv_transfer[PIPE_MAX_SAMPLERS][PIPE_MAX_TEXTURE_LEVELS];
303
304 for (unsigned i = 0; i < st->state.num_sampler_views[PIPE_SHADER_VERTEX]; i++) {
305 struct pipe_sampler_view *view = st->state.vert_sampler_views[i];
306 if (!view)
307 continue;
308
309 struct pipe_resource *res = view->texture;
310 unsigned width0 = res->width0;
311 unsigned num_layers = res->depth0;
312 unsigned first_level = 0;
313 unsigned last_level = 0;
314 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
315 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
316 uint32_t mip_offset[PIPE_MAX_TEXTURE_LEVELS];
317 uintptr_t mip_addr[PIPE_MAX_TEXTURE_LEVELS];
318 uintptr_t base_addr;
319
320 if (res->target != PIPE_BUFFER) {
321 first_level = view->u.tex.first_level;
322 last_level = view->u.tex.last_level;
323 num_layers = view->u.tex.last_layer - view->u.tex.first_layer + 1;
324 base_addr = UINTPTR_MAX;
325
326 for (unsigned j = first_level; j <= last_level; j++) {
327 unsigned map_layers = res->target == PIPE_TEXTURE_3D ?
328 util_num_layers(res, j) : num_layers;
329
330 sv_transfer[i][j] = NULL;
331 mip_addr[j] = (uintptr_t)
332 pipe_transfer_map_3d(pipe, res, j,
333 PIPE_TRANSFER_READ, 0, 0,
334 view->u.tex.first_layer,
335 u_minify(res->width0, j),
336 u_minify(res->height0, j),
337 map_layers, &sv_transfer[i][j]);
338 row_stride[j] = sv_transfer[i][j]->stride;
339 img_stride[j] = sv_transfer[i][j]->layer_stride;
340
341 /* Get the minimum address, because the draw module takes only
342 * 1 address for the whole texture + uint32 offsets for mip levels,
343 * so we need to convert mapped resource pointers into that scheme.
344 */
345 base_addr = MIN2(base_addr, mip_addr[j]);
346 }
347 for (unsigned j = first_level; j <= last_level; j++) {
348 /* TODO: The draw module should accept pointers for mipmap levels
349 * instead of offsets. This is unlikely to work on 64-bit archs.
350 */
351 assert(mip_addr[j] - base_addr <= UINT32_MAX);
352 mip_offset[j] = mip_addr[j] - base_addr;
353 }
354 } else {
355 width0 = view->u.buf.size / util_format_get_blocksize(view->format);
356
357 /* probably don't really need to fill that out */
358 mip_offset[0] = 0;
359 row_stride[0] = 0;
360 img_stride[0] = 0;
361
362 sv_transfer[i][0] = NULL;
363 base_addr = (uintptr_t)
364 pipe_buffer_map_range(pipe, res, view->u.buf.offset,
365 view->u.buf.size,
366 PIPE_TRANSFER_READ,
367 &sv_transfer[i][0]);
368 }
369
370 draw_set_mapped_texture(draw, PIPE_SHADER_VERTEX, i, width0,
371 res->height0, num_layers, first_level,
372 last_level, (void*)base_addr, row_stride,
373 img_stride, mip_offset);
374 }
375
376 /* shader images */
377 struct pipe_image_view images[PIPE_MAX_SHADER_IMAGES];
378 struct pipe_transfer *img_transfer[PIPE_MAX_SHADER_IMAGES] = {0};
379
380 for (unsigned i = 0; i < prog->info.num_images; i++) {
381 struct pipe_image_view *img = &images[i];
382
383 st_convert_image_from_unit(st, img, prog->sh.ImageUnits[i],
384 prog->sh.ImageAccess[i]);
385
386 struct pipe_resource *res = img->resource;
387 if (!res)
388 continue;
389
390 unsigned width, height, num_layers, row_stride, img_stride;
391 void *addr;
392
393 if (res->target != PIPE_BUFFER) {
394 width = u_minify(res->width0, img->u.tex.level);
395 height = u_minify(res->height0, img->u.tex.level);
396 num_layers = img->u.tex.last_layer - img->u.tex.first_layer + 1;
397
398 addr = pipe_transfer_map_3d(pipe, res, img->u.tex.level,
399 PIPE_TRANSFER_READ, 0, 0,
400 img->u.tex.first_layer,
401 width, height, num_layers,
402 &img_transfer[i]);
403 row_stride = img_transfer[i]->stride;
404 img_stride = img_transfer[i]->layer_stride;
405 } else {
406 width = img->u.buf.size / util_format_get_blocksize(img->format);
407
408 /* probably don't really need to fill that out */
409 row_stride = 0;
410 img_stride = 0;
411 height = num_layers = 1;
412
413 addr = pipe_buffer_map_range(pipe, res, img->u.buf.offset,
414 img->u.buf.size, PIPE_TRANSFER_READ,
415 &img_transfer[i]);
416 }
417
418 draw_set_mapped_image(draw, PIPE_SHADER_VERTEX, i, width, height,
419 num_layers, addr, row_stride, img_stride);
420 }
421 draw_set_images(draw, PIPE_SHADER_VERTEX, images, prog->info.num_images);
422
423 /* draw here */
424 for (i = 0; i < nr_prims; i++) {
425 info.count = prims[i].count;
426
427 if (!info.count)
428 continue;
429
430 info.mode = prims[i].mode;
431 info.start = start + prims[i].start;
432 info.start_instance = prims[i].base_instance;
433 info.instance_count = prims[i].num_instances;
434 info.index_bias = prims[i].basevertex;
435 info.drawid = prims[i].draw_id;
436 if (!ib) {
437 info.min_index = info.start;
438 info.max_index = info.start + info.count - 1;
439 }
440
441 draw_vbo(draw, &info);
442 }
443
444 /* unmap images */
445 for (unsigned i = 0; i < prog->info.num_images; i++) {
446 if (img_transfer[i]) {
447 draw_set_mapped_image(draw, PIPE_SHADER_VERTEX, i, 0, 0, 0, NULL, 0, 0);
448 pipe_transfer_unmap(pipe, img_transfer[i]);
449 }
450 }
451
452 /* unmap sampler views */
453 for (unsigned i = 0; i < st->state.num_sampler_views[PIPE_SHADER_VERTEX]; i++) {
454 struct pipe_sampler_view *view = st->state.vert_sampler_views[i];
455
456 if (view) {
457 if (view->texture->target != PIPE_BUFFER) {
458 for (unsigned j = view->u.tex.first_level;
459 j <= view->u.tex.last_level; j++) {
460 pipe_transfer_unmap(pipe, sv_transfer[i][j]);
461 }
462 } else {
463 pipe_transfer_unmap(pipe, sv_transfer[i][0]);
464 }
465 }
466 }
467
468 draw_set_samplers(draw, PIPE_SHADER_VERTEX, NULL, 0);
469 draw_set_sampler_views(draw, PIPE_SHADER_VERTEX, NULL, 0);
470
471 for (unsigned i = 0; i < prog->info.num_ssbos; i++) {
472 if (ssbo_transfer[i]) {
473 draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX, 1 + i,
474 NULL, 0);
475 pipe_buffer_unmap(pipe, ssbo_transfer[i]);
476 }
477 }
478
479 for (unsigned i = 0; i < prog->info.num_ubos; i++) {
480 if (ubo_transfer[i]) {
481 draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX, 1 + i,
482 NULL, 0);
483 pipe_buffer_unmap(pipe, ubo_transfer[i]);
484 }
485 }
486
487 /*
488 * unmap vertex/index buffers
489 */
490 if (ib) {
491 draw_set_indexes(draw, NULL, 0, 0);
492 if (ib_transfer)
493 pipe_buffer_unmap(pipe, ib_transfer);
494 }
495
496 out_unref_vertex:
497 for (unsigned buf = 0; buf < num_vbuffers; ++buf) {
498 if (vb_transfer[buf])
499 pipe_buffer_unmap(pipe, vb_transfer[buf]);
500 draw_set_mapped_vertex_buffer(draw, buf, NULL, 0);
501 }
502 draw_set_vertex_buffers(draw, 0, num_vbuffers, NULL);
503 }