1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**************************************************************************
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * All Rights Reserved.
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Permission is hereby granted, free of charge, to any person obtaining a
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * copy of this software and associated documentation files (the
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * "Software"), to deal in the Software without restriction, including
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * without limitation the rights to use, copy, modify, merge, publish,
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * distribute, sub license, and/or sell copies of the Software, and to
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * permit persons to whom the Software is furnished to do so, subject to
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the following conditions:
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The above copyright notice and this permission notice (including the
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * next paragraph) shall be included in all copies or substantial portions
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * of the Software.
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org **************************************************************************/
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/* Author:
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *    Brian Paul
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *    Keith Whitwell
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "pipe/p_defines.h"
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "pipe/p_context.h"
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "util/u_prim.h"
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "lp_context.h"
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "lp_state.h"
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "lp_query.h"
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "draw/draw_context.h"
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Draw vertex arrays, with optional indexing, optional instancing.
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * All the other drawing functions are implemented in terms of this function.
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Basically, map the vertex buffers (and drawing surfaces), then hand off
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the drawing to the 'draw' module.
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgllvmpipe_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info)
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct llvmpipe_context *lp = llvmpipe_context(pipe);
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct draw_context *draw = lp->draw;
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const void *mapped_indices = NULL;
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   unsigned i;
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (!llvmpipe_check_render_cond(lp))
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return;
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (lp->dirty)
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      llvmpipe_update_derived( lp );
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /*
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * Map vertex buffers
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   for (i = 0; i < lp->num_vertex_buffers; i++) {
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      const void *buf = lp->vertex_buffer[i].user_buffer;
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (!buf)
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         buf = llvmpipe_resource_data(lp->vertex_buffer[i].buffer);
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      draw_set_mapped_vertex_buffer(draw, i, buf);
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Map index buffer, if present */
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (info->indexed) {
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      mapped_indices = lp->index_buffer.user_buffer;
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (!mapped_indices)
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         mapped_indices = llvmpipe_resource_data(lp->index_buffer.buffer);
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      draw_set_indexes(draw,
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                       (ubyte *) mapped_indices + lp->index_buffer.offset,
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                       lp->index_buffer.index_size);
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   llvmpipe_prepare_vertex_sampling(lp,
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                    lp->num_sampler_views[PIPE_SHADER_VERTEX],
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                    lp->sampler_views[PIPE_SHADER_VERTEX]);
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* draw! */
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   draw_vbo(draw, info);
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /*
95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * unmap vertex/index buffers
96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   for (i = 0; i < lp->num_vertex_buffers; i++) {
98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      draw_set_mapped_vertex_buffer(draw, i, NULL);
99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (mapped_indices) {
101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      draw_set_indexes(draw, NULL, 0);
102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   llvmpipe_cleanup_vertex_sampling(lp);
104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /*
106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * TODO: Flush only when a user vertex/index buffer is present
107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * (or even better, modify draw module to do this
108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * internally when this condition is seen?)
109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   draw_flush(draw);
111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgllvmpipe_init_draw_funcs(struct llvmpipe_context *llvmpipe)
116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   llvmpipe->pipe.draw_vbo = llvmpipe_draw_vbo;
118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
119