1/**********************************************************
2 * Copyright 2008-2009 VMware, Inc.  All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26#include "util/u_inlines.h"
27#include "pipe/p_defines.h"
28#include "util/u_math.h"
29#include "util/u_upload_mgr.h"
30
31#include "svga_context.h"
32#include "svga_state.h"
33#include "svga_draw.h"
34#include "svga_tgsi.h"
35#include "svga_screen.h"
36#include "svga_resource_buffer.h"
37
38#include "svga_hw_reg.h"
39
40
41/***********************************************************************
42 */
43
44
45static enum pipe_error
46emit_hw_vs_vdecl(struct svga_context *svga, unsigned dirty)
47{
48   const struct pipe_vertex_element *ve = svga->curr.velems->velem;
49   SVGA3dVertexDecl decl;
50   unsigned i;
51   unsigned neg_bias = 0;
52
53   assert(svga->curr.velems->count >=
54          svga->curr.vs->base.info.file_count[TGSI_FILE_INPUT]);
55
56   svga_hwtnl_reset_vdecl( svga->hwtnl,
57                           svga->curr.velems->count );
58
59   /**
60    * We can't set the VDECL offset to something negative, so we
61    * must calculate a common negative additional index bias, and modify
62    * the VDECL offsets accordingly so they *all* end up positive.
63    *
64    * Note that the exact value of the negative index bias is not that
65    * important, since we compensate for it when we calculate the vertex
66    * buffer offset below. The important thing is that all vertex buffer
67    * offsets remain positive.
68    *
69    * Note that we use a negative bias variable in order to make the
70    * rounding maths more easy to follow, and to avoid int / unsigned
71    * confusion.
72    */
73
74   for (i = 0; i < svga->curr.velems->count; i++) {
75      const struct pipe_vertex_buffer *vb =
76         &svga->curr.vb[ve[i].vertex_buffer_index];
77      struct svga_buffer *buffer;
78      unsigned int offset = vb->buffer_offset + ve[i].src_offset;
79      unsigned tmp_neg_bias = 0;
80
81      if (!vb->buffer)
82         continue;
83
84      buffer = svga_buffer(vb->buffer);
85      if (buffer->uploaded.start > offset) {
86         tmp_neg_bias = buffer->uploaded.start - offset;
87         if (vb->stride)
88            tmp_neg_bias = (tmp_neg_bias + vb->stride - 1) / vb->stride;
89         neg_bias = MAX2(neg_bias, tmp_neg_bias);
90      }
91   }
92
93   for (i = 0; i < svga->curr.velems->count; i++) {
94      const struct pipe_vertex_buffer *vb =
95         &svga->curr.vb[ve[i].vertex_buffer_index];
96      unsigned usage, index;
97      struct svga_buffer *buffer;
98
99      if (!vb->buffer)
100         continue;
101
102      buffer= svga_buffer(vb->buffer);
103      svga_generate_vdecl_semantics( i, &usage, &index );
104
105      /* SVGA_NEW_VELEMENT
106       */
107      decl.identity.type = svga->state.sw.ve_format[i];
108      decl.identity.method = SVGA3D_DECLMETHOD_DEFAULT;
109      decl.identity.usage = usage;
110      decl.identity.usageIndex = index;
111      decl.array.stride = vb->stride;
112
113      /* Compensate for partially uploaded vbo, and
114       * for the negative index bias.
115       */
116      decl.array.offset = (vb->buffer_offset
117                           + ve[i].src_offset
118			   + neg_bias * vb->stride
119			   - buffer->uploaded.start);
120
121      assert(decl.array.offset >= 0);
122
123      svga_hwtnl_vdecl( svga->hwtnl,
124                        i,
125                        &decl,
126                        buffer->uploaded.buffer ? buffer->uploaded.buffer :
127                        vb->buffer );
128   }
129
130   svga_hwtnl_set_index_bias( svga->hwtnl, -neg_bias );
131   return PIPE_OK;
132}
133
134
135static enum pipe_error
136emit_hw_vdecl(struct svga_context *svga, unsigned dirty)
137{
138   /* SVGA_NEW_NEED_SWTNL
139    */
140   if (svga->state.sw.need_swtnl)
141      return PIPE_OK; /* Do not emit during swtnl */
142
143   return emit_hw_vs_vdecl( svga, dirty );
144}
145
146
147struct svga_tracked_state svga_hw_vdecl =
148{
149   "hw vertex decl state (hwtnl version)",
150   ( SVGA_NEW_NEED_SWTNL |
151     SVGA_NEW_VELEMENT |
152     SVGA_NEW_VBUFFER |
153     SVGA_NEW_RAST |
154     SVGA_NEW_FS |
155     SVGA_NEW_VS ),
156   emit_hw_vdecl
157};
158
159
160
161
162
163
164