Merge commit 'origin/st-shader-varients'
[profile/ivi/mesa.git] / src / gallium / drivers / svga / svga_state_vdecl.c
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 "pipe/p_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_screen_buffer.h"
37
38 #include "svga_hw_reg.h"
39
40
41 static int
42 upload_user_buffers( struct svga_context *svga )
43 {
44    enum pipe_error ret = PIPE_OK;
45    int i;
46    int nr;
47
48    if (0) 
49       debug_printf("%s: %d\n", __FUNCTION__, svga->curr.num_vertex_buffers);
50
51    nr = svga->curr.num_vertex_buffers;
52
53    for (i = 0; i < nr; i++) 
54    {
55       if (svga_buffer_is_user_buffer(svga->curr.vb[i].buffer))
56       {
57          struct pipe_buffer *upload_buffer = NULL;
58          unsigned offset = /*svga->curr.vb[i].buffer_offset*/ 0;
59          unsigned size = svga->curr.vb[i].buffer->size /*- offset*/;
60          unsigned upload_offset;
61
62          ret = u_upload_buffer( svga->upload_vb,
63                                 offset,
64                                 size,
65                                 svga->curr.vb[i].buffer,
66                                 &upload_offset,
67                                 &upload_buffer );
68          if (ret)
69             return ret;
70
71          if (0)
72             debug_printf("%s: %d: orig buf %p upl buf %p ofs %d sz %d\n", 
73                          __FUNCTION__, 
74                          i,
75                          svga->curr.vb[i].buffer,
76                          upload_buffer, upload_offset, size);
77
78          /* Make sure we release the old buffer and end up with the
79           * correct refcount on the uploaded buffer.
80           */
81          pipe_buffer_reference( &svga->curr.vb[i].buffer, NULL );
82          svga->curr.vb[i].buffer = upload_buffer;
83          svga->curr.vb[i].buffer_offset = upload_offset;
84       }
85    }
86
87    if (0)
88       debug_printf("%s: DONE\n", __FUNCTION__);
89
90    return ret;
91 }
92
93
94 /***********************************************************************
95  */
96
97
98 static int emit_hw_vs_vdecl( struct svga_context *svga,
99                              unsigned dirty )
100 {
101    const struct pipe_vertex_element *ve = svga->curr.ve;
102    SVGA3dVertexDecl decl;
103    unsigned i;
104
105    assert(svga->curr.num_vertex_elements >=
106           svga->curr.vs->base.info.file_count[TGSI_FILE_INPUT]);
107
108    svga_hwtnl_reset_vdecl( svga->hwtnl, 
109                            svga->curr.num_vertex_elements );
110
111    for (i = 0; i < svga->curr.num_vertex_elements; i++) {
112       const struct pipe_vertex_buffer *vb = &svga->curr.vb[ve[i].vertex_buffer_index];
113       unsigned usage, index;
114
115
116       svga_generate_vdecl_semantics( i, &usage, &index );
117
118       /* SVGA_NEW_VELEMENT
119        */
120       decl.identity.type = svga->state.sw.ve_format[i];
121       decl.identity.method = SVGA3D_DECLMETHOD_DEFAULT;
122       decl.identity.usage = usage;
123       decl.identity.usageIndex = index;
124       decl.array.stride = vb->stride;
125       decl.array.offset = (vb->buffer_offset +
126                            ve[i].src_offset);
127
128       svga_hwtnl_vdecl( svga->hwtnl,
129                         i,
130                         &decl,
131                         vb->buffer );
132    }
133
134    return 0;
135 }
136
137
138 static int emit_hw_vdecl( struct svga_context *svga,
139                           unsigned dirty )
140 {
141    int ret = 0;
142
143    /* SVGA_NEW_NEED_SWTNL
144     */
145    if (svga->state.sw.need_swtnl)
146       return 0; /* Do not emit during swtnl */
147
148    /* If we get to here, we know that we're going to draw.  Upload
149     * userbuffers now and try to combine multiple userbuffers from
150     * multiple draw calls into a single host buffer for performance.
151     */
152    if (svga->curr.any_user_vertex_buffers &&
153        SVGA_COMBINE_USERBUFFERS)
154    {
155       ret = upload_user_buffers( svga );
156       if (ret)
157          return ret;
158
159       svga->curr.any_user_vertex_buffers = FALSE;
160    }
161
162    return emit_hw_vs_vdecl( svga, dirty );
163 }
164
165
166 struct svga_tracked_state svga_hw_vdecl = 
167 {
168    "hw vertex decl state (hwtnl version)",
169    ( SVGA_NEW_NEED_SWTNL |
170      SVGA_NEW_VELEMENT |
171      SVGA_NEW_VBUFFER |
172      SVGA_NEW_RAST |
173      SVGA_NEW_FS |
174      SVGA_NEW_VS ),
175    emit_hw_vdecl
176 };
177
178
179
180
181
182