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