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_inlines.h"
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "util/u_prim.h"
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "sp_context.h"
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "sp_query.h"
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "sp_state.h"
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "sp_texture.h"
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "draw/draw_context.h"
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * This function handles drawing indexed and non-indexed prims,
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * instanced and non-instanced drawing, with or without min/max element
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * indexes.
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * All the other drawing functions are expressed in terms of this
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * function.
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * For non-indexed prims, indexBuffer should be NULL.
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * For non-instanced drawing, instanceCount should be 1.
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * When the min/max element indexes aren't known, minIndex should be 0
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * and maxIndex should be ~0.
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgsoftpipe_draw_vbo(struct pipe_context *pipe,
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                  const struct pipe_draw_info *info)
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct softpipe_context *sp = softpipe_context(pipe);
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct draw_context *draw = sp->draw;
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const void *mapped_indices = NULL;
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   unsigned i;
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (!softpipe_check_render_cond(sp))
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return;
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   sp->reduced_api_prim = u_reduced_prim(info->mode);
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (sp->dirty) {
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      softpipe_update_derived(sp, sp->reduced_api_prim);
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   softpipe_map_transfers(sp);
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Map vertex buffers */
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   for (i = 0; i < sp->num_vertex_buffers; i++) {
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      const void *buf = sp->vertex_buffer[i].user_buffer;
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (!buf)
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         buf = softpipe_resource(sp->vertex_buffer[i].buffer)->data;
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      draw_set_mapped_vertex_buffer(draw, i, buf);
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Map index buffer, if present */
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (info->indexed) {
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      mapped_indices = sp->index_buffer.user_buffer;
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (!mapped_indices)
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         mapped_indices = softpipe_resource(sp->index_buffer.buffer)->data;
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      draw_set_indexes(draw,
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                       (ubyte *) mapped_indices + sp->index_buffer.offset,
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                       sp->index_buffer.index_size);
95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   for (i = 0; i < sp->num_so_targets; i++) {
99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      void *buf = softpipe_resource(sp->so_targets[i]->target.buffer)->data;
100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      sp->so_targets[i]->mapping = buf;
101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   draw_set_mapped_so_targets(draw, sp->num_so_targets,
104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                              sp->so_targets);
105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* draw! */
107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   draw_vbo(draw, info);
108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* unmap vertex/index buffers - will cause draw module to flush */
110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   for (i = 0; i < sp->num_vertex_buffers; i++) {
111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      draw_set_mapped_vertex_buffer(draw, i, NULL);
112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (mapped_indices) {
114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      draw_set_indexes(draw, NULL, 0);
115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   draw_set_mapped_so_targets(draw, 0, NULL);
118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /*
120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * TODO: Flush only when a user vertex/index buffer is present
121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * (or even better, modify draw module to do this
122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * internally when this condition is seen?)
123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   draw_flush(draw);
125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Note: leave drawing surfaces mapped */
127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   sp->dirty_render_cache = TRUE;
128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
129