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/* Author:
29 *    Brian Paul
30 *    Keith Whitwell
31 */
32
33
34#include "pipe/p_defines.h"
35#include "pipe/p_context.h"
36#include "util/u_inlines.h"
37#include "util/u_draw.h"
38#include "util/u_prim.h"
39
40#include "sp_context.h"
41#include "sp_query.h"
42#include "sp_state.h"
43#include "sp_texture.h"
44#include "sp_screen.h"
45
46#include "draw/draw_context.h"
47
48/**
49 * This function handles drawing indexed and non-indexed prims,
50 * instanced and non-instanced drawing, with or without min/max element
51 * indexes.
52 * All the other drawing functions are expressed in terms of this
53 * function.
54 *
55 * For non-indexed prims, indexBuffer should be NULL.
56 * For non-instanced drawing, instanceCount should be 1.
57 * When the min/max element indexes aren't known, minIndex should be 0
58 * and maxIndex should be ~0.
59 */
60void
61softpipe_draw_vbo(struct pipe_context *pipe,
62                  const struct pipe_draw_info *info,
63                  unsigned drawid_offset,
64                  const struct pipe_draw_indirect_info *indirect,
65                  const struct pipe_draw_start_count_bias *draws,
66                  unsigned num_draws)
67{
68   if (num_draws > 1) {
69      util_draw_multi(pipe, info, drawid_offset, indirect, draws, num_draws);
70      return;
71   }
72
73   if (!indirect && (!draws[0].count || !info->instance_count))
74      return;
75
76   struct softpipe_context *sp = softpipe_context(pipe);
77   struct draw_context *draw = sp->draw;
78   const void *mapped_indices = NULL;
79   unsigned i;
80
81   if (!softpipe_check_render_cond(sp))
82      return;
83
84   if (indirect && indirect->buffer) {
85      util_draw_indirect(pipe, info, indirect);
86      return;
87   }
88
89   sp->reduced_api_prim = u_reduced_prim(info->mode);
90
91   if (sp->dirty) {
92      softpipe_update_derived(sp, sp->reduced_api_prim);
93   }
94
95   /* Map vertex buffers */
96   for (i = 0; i < sp->num_vertex_buffers; i++) {
97      const void *buf = sp->vertex_buffer[i].is_user_buffer ?
98                           sp->vertex_buffer[i].buffer.user : NULL;
99      size_t size = ~0;
100      if (!buf) {
101         if (!sp->vertex_buffer[i].buffer.resource) {
102            continue;
103         }
104         buf = softpipe_resource_data(sp->vertex_buffer[i].buffer.resource);
105         size = sp->vertex_buffer[i].buffer.resource->width0;
106      }
107      draw_set_mapped_vertex_buffer(draw, i, buf, size);
108   }
109
110   /* Map index buffer, if present */
111   if (info->index_size) {
112      unsigned available_space = ~0;
113      mapped_indices = info->has_user_indices ? info->index.user : NULL;
114      if (!mapped_indices) {
115         mapped_indices = softpipe_resource_data(info->index.resource);
116         available_space = info->index.resource->width0;
117      }
118
119      draw_set_indexes(draw,
120                       (ubyte *) mapped_indices,
121                       info->index_size, available_space);
122   }
123
124   if (softpipe_screen(sp->pipe.screen)->use_llvm) {
125      softpipe_prepare_vertex_sampling(sp,
126                                       sp->num_sampler_views[PIPE_SHADER_VERTEX],
127                                       sp->sampler_views[PIPE_SHADER_VERTEX]);
128      softpipe_prepare_geometry_sampling(sp,
129                                         sp->num_sampler_views[PIPE_SHADER_GEOMETRY],
130                                         sp->sampler_views[PIPE_SHADER_GEOMETRY]);
131   }
132
133   if (sp->gs && !sp->gs->shader.tokens) {
134      /* we have an empty geometry shader with stream output, so
135         attach the stream output info to the current vertex shader */
136      if (sp->vs) {
137         draw_vs_attach_so(sp->vs->draw_data, &sp->gs->shader.stream_output);
138      }
139   }
140   draw_collect_pipeline_statistics(draw,
141                                    sp->active_statistics_queries > 0);
142
143   /* draw! */
144   draw_vbo(draw, info, drawid_offset, indirect, draws, num_draws, 0);
145
146   /* unmap vertex/index buffers - will cause draw module to flush */
147   for (i = 0; i < sp->num_vertex_buffers; i++) {
148      draw_set_mapped_vertex_buffer(draw, i, NULL, 0);
149   }
150   if (mapped_indices) {
151      draw_set_indexes(draw, NULL, 0, 0);
152   }
153
154   if (softpipe_screen(sp->pipe.screen)->use_llvm) {
155      softpipe_cleanup_vertex_sampling(sp);
156      softpipe_cleanup_geometry_sampling(sp);
157   }
158
159   /*
160    * TODO: Flush only when a user vertex/index buffer is present
161    * (or even better, modify draw module to do this
162    * internally when this condition is seen?)
163    */
164   draw_flush(draw);
165
166   /* Note: leave drawing surfaces mapped */
167   sp->dirty_render_cache = TRUE;
168}
169