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_draw.h"
37#include "util/u_prim.h"
38
39#include "lp_context.h"
40#include "lp_state.h"
41#include "lp_query.h"
42
43#include "draw/draw_context.h"
44
45
46
47/**
48 * Draw vertex arrays, with optional indexing, optional instancing.
49 * All the other drawing functions are implemented in terms of this function.
50 * Basically, map the vertex buffers (and drawing surfaces), then hand off
51 * the drawing to the 'draw' module.
52 */
53static void
54llvmpipe_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info,
55                  unsigned drawid_offset,
56                  const struct pipe_draw_indirect_info *indirect,
57                  const struct pipe_draw_start_count_bias *draws,
58                  unsigned num_draws)
59{
60   if (!indirect && (!draws[0].count || !info->instance_count))
61      return;
62
63   struct llvmpipe_context *lp = llvmpipe_context(pipe);
64   struct draw_context *draw = lp->draw;
65   const void *mapped_indices = NULL;
66   unsigned i;
67
68   if (!llvmpipe_check_render_cond(lp))
69      return;
70
71   if (indirect && indirect->buffer) {
72      util_draw_indirect(pipe, info, indirect);
73      return;
74   }
75
76   if (lp->dirty)
77      llvmpipe_update_derived( lp );
78
79   /*
80    * Map vertex buffers
81    */
82   for (i = 0; i < lp->num_vertex_buffers; i++) {
83      const void *buf = lp->vertex_buffer[i].is_user_buffer ?
84                           lp->vertex_buffer[i].buffer.user : NULL;
85      size_t size = ~0;
86      if (!buf) {
87         if (!lp->vertex_buffer[i].buffer.resource) {
88            continue;
89         }
90         buf = llvmpipe_resource_data(lp->vertex_buffer[i].buffer.resource);
91         size = lp->vertex_buffer[i].buffer.resource->width0;
92      }
93      draw_set_mapped_vertex_buffer(draw, i, buf, size);
94   }
95
96   /* Map index buffer, if present */
97   if (info->index_size) {
98      unsigned available_space = ~0;
99      mapped_indices = info->has_user_indices ? info->index.user : NULL;
100      if (!mapped_indices) {
101         mapped_indices = llvmpipe_resource_data(info->index.resource);
102         available_space = info->index.resource->width0;
103      }
104      draw_set_indexes(draw,
105                       (ubyte *) mapped_indices,
106                       info->index_size, available_space);
107   }
108
109   llvmpipe_prepare_vertex_sampling(lp,
110                                    lp->num_sampler_views[PIPE_SHADER_VERTEX],
111                                    lp->sampler_views[PIPE_SHADER_VERTEX]);
112   llvmpipe_prepare_geometry_sampling(lp,
113                                      lp->num_sampler_views[PIPE_SHADER_GEOMETRY],
114                                      lp->sampler_views[PIPE_SHADER_GEOMETRY]);
115   llvmpipe_prepare_tess_ctrl_sampling(lp,
116                                       lp->num_sampler_views[PIPE_SHADER_TESS_CTRL],
117                                       lp->sampler_views[PIPE_SHADER_TESS_CTRL]);
118   llvmpipe_prepare_tess_eval_sampling(lp,
119                                       lp->num_sampler_views[PIPE_SHADER_TESS_EVAL],
120                                       lp->sampler_views[PIPE_SHADER_TESS_EVAL]);
121
122   llvmpipe_prepare_vertex_images(lp,
123                                  lp->num_images[PIPE_SHADER_VERTEX],
124                                  lp->images[PIPE_SHADER_VERTEX]);
125   llvmpipe_prepare_geometry_images(lp,
126                                    lp->num_images[PIPE_SHADER_GEOMETRY],
127                                    lp->images[PIPE_SHADER_GEOMETRY]);
128   llvmpipe_prepare_tess_ctrl_images(lp,
129                                     lp->num_images[PIPE_SHADER_TESS_CTRL],
130                                     lp->images[PIPE_SHADER_TESS_CTRL]);
131   llvmpipe_prepare_tess_eval_images(lp,
132                                     lp->num_images[PIPE_SHADER_TESS_EVAL],
133                                     lp->images[PIPE_SHADER_TESS_EVAL]);
134   if (lp->gs && lp->gs->no_tokens) {
135      /* we have an empty geometry shader with stream output, so
136         attach the stream output info to the current vertex shader */
137      if (lp->vs) {
138         draw_vs_attach_so(lp->vs, &lp->gs->stream_output);
139      }
140   }
141   draw_collect_pipeline_statistics(draw,
142                                    lp->active_statistics_queries > 0 &&
143                                    !lp->queries_disabled);
144
145   draw_collect_primitives_generated(draw,
146                                     lp->active_primgen_queries &&
147                                     !lp->queries_disabled);
148
149   /* draw! */
150   draw_vbo(draw, info, drawid_offset, indirect, draws, num_draws,
151            lp->patch_vertices);
152
153   /*
154    * unmap vertex/index buffers
155    */
156   for (i = 0; i < lp->num_vertex_buffers; i++) {
157      draw_set_mapped_vertex_buffer(draw, i, NULL, 0);
158   }
159   if (mapped_indices) {
160      draw_set_indexes(draw, NULL, 0, 0);
161   }
162
163   if (lp->gs && lp->gs->no_tokens) {
164      /* we have attached stream output to the vs for rendering,
165         now lets reset it */
166      if (lp->vs) {
167         draw_vs_reset_so(lp->vs);
168      }
169   }
170
171   llvmpipe_cleanup_stage_sampling(lp, PIPE_SHADER_VERTEX);
172   llvmpipe_cleanup_stage_sampling(lp, PIPE_SHADER_GEOMETRY);
173   llvmpipe_cleanup_stage_sampling(lp, PIPE_SHADER_TESS_CTRL);
174   llvmpipe_cleanup_stage_sampling(lp, PIPE_SHADER_TESS_EVAL);
175
176   llvmpipe_cleanup_stage_images(lp, PIPE_SHADER_VERTEX);
177   llvmpipe_cleanup_stage_images(lp, PIPE_SHADER_GEOMETRY);
178   llvmpipe_cleanup_stage_images(lp, PIPE_SHADER_TESS_CTRL);
179   llvmpipe_cleanup_stage_images(lp, PIPE_SHADER_TESS_EVAL);
180
181   /*
182    * TODO: Flush only when a user vertex/index buffer is present
183    * (or even better, modify draw module to do this
184    * internally when this condition is seen?)
185    */
186   draw_flush(draw);
187}
188
189
190void
191llvmpipe_init_draw_funcs(struct llvmpipe_context *llvmpipe)
192{
193   llvmpipe->pipe.draw_vbo = llvmpipe_draw_vbo;
194}
195