Squashed commit of the following:
[profile/ivi/mesa.git] / src / gallium / drivers / svga / svga_pipe_draw.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 "svga_cmd.h"
27
28 #include "util/u_inlines.h"
29 #include "util/u_prim.h"
30 #include "util/u_time.h"
31 #include "indices/u_indices.h"
32
33 #include "svga_hw_reg.h"
34 #include "svga_context.h"
35 #include "svga_screen.h"
36 #include "svga_draw.h"
37 #include "svga_state.h"
38 #include "svga_swtnl.h"
39 #include "svga_debug.h"
40
41
42
43 static enum pipe_error
44 retry_draw_range_elements( struct svga_context *svga,
45                            struct pipe_resource *index_buffer,
46                            unsigned index_size,
47                            unsigned min_index,
48                            unsigned max_index,
49                            unsigned prim, 
50                            unsigned start, 
51                            unsigned count,
52                            boolean do_retry )
53 {
54    enum pipe_error ret = 0;
55
56    svga_hwtnl_set_unfilled( svga->hwtnl,
57                             svga->curr.rast->hw_unfilled );
58
59    svga_hwtnl_set_flatshade( svga->hwtnl,
60                              svga->curr.rast->templ.flatshade,
61                              svga->curr.rast->templ.flatshade_first );
62
63
64    ret = svga_update_state( svga, SVGA_STATE_HW_DRAW );
65    if (ret)
66       goto retry;
67
68    ret = svga_hwtnl_draw_range_elements( svga->hwtnl,
69                                          index_buffer, index_size,
70                                          min_index, max_index,
71                                          prim, start, count, 0 );
72    if (ret)
73       goto retry;
74
75    if (svga->curr.any_user_vertex_buffers) {
76       ret = svga_hwtnl_flush( svga->hwtnl );
77       if (ret)
78          goto retry;
79    }
80
81    return PIPE_OK;
82
83 retry:
84    svga_context_flush( svga, NULL );
85
86    if (do_retry)
87    {
88       return retry_draw_range_elements( svga,
89                                         index_buffer, index_size,
90                                         min_index, max_index,
91                                         prim, start, count,
92                                         FALSE );
93    }
94
95    return ret;
96 }
97
98
99 static enum pipe_error
100 retry_draw_arrays( struct svga_context *svga,
101                    unsigned prim, 
102                    unsigned start, 
103                    unsigned count,
104                    boolean do_retry )
105 {
106    enum pipe_error ret;
107
108    svga_hwtnl_set_unfilled( svga->hwtnl,
109                             svga->curr.rast->hw_unfilled );
110
111    svga_hwtnl_set_flatshade( svga->hwtnl,
112                              svga->curr.rast->templ.flatshade,
113                              svga->curr.rast->templ.flatshade_first );
114
115    ret = svga_update_state( svga, SVGA_STATE_HW_DRAW );
116    if (ret)
117       goto retry;
118
119    ret = svga_hwtnl_draw_arrays( svga->hwtnl, prim,
120                                  start, count );
121    if (ret)
122       goto retry;
123
124    if (svga->curr.any_user_vertex_buffers) {
125       ret = svga_hwtnl_flush( svga->hwtnl );
126       if (ret)
127          goto retry;
128    }
129
130    return 0;
131
132 retry:
133    if (ret == PIPE_ERROR_OUT_OF_MEMORY && do_retry) 
134    {
135       svga_context_flush( svga, NULL );
136
137       return retry_draw_arrays( svga,
138                                 prim,
139                                 start,
140                                 count,
141                                 FALSE );
142    }
143
144    return ret;
145 }
146
147
148
149
150
151 static void
152 svga_draw_range_elements( struct pipe_context *pipe,
153                           struct pipe_resource *index_buffer,
154                           unsigned index_size,
155                           unsigned min_index,
156                           unsigned max_index,
157                           unsigned prim, unsigned start, unsigned count)
158 {
159    struct svga_context *svga = svga_context( pipe );
160    unsigned reduced_prim = u_reduced_prim(prim);
161    enum pipe_error ret = 0;
162
163    if (!u_trim_pipe_prim( prim, &count ))
164       return;
165
166    /*
167     * Mark currently bound target surfaces as dirty
168     * doesn't really matter if it is done before drawing.
169     *
170     * TODO If we ever normaly return something other then
171     * true we should not mark it as dirty then.
172     */
173    svga_mark_surfaces_dirty(svga_context(pipe));
174
175    if (svga->curr.reduced_prim != reduced_prim) {
176       svga->curr.reduced_prim = reduced_prim;
177       svga->dirty |= SVGA_NEW_REDUCED_PRIMITIVE;
178    }
179    
180    svga_update_state_retry( svga, SVGA_STATE_NEED_SWTNL );
181
182 #ifdef DEBUG
183    if (svga->curr.vs->base.id == svga->debug.disable_shader ||
184        svga->curr.fs->base.id == svga->debug.disable_shader)
185       return;
186 #endif
187
188    if (svga->state.sw.need_swtnl)
189    {
190       ret = svga_swtnl_draw_range_elements( svga, 
191                                             index_buffer, 
192                                             index_size,
193                                             min_index, max_index,
194                                             prim,
195                                             start, count );
196    }
197    else {
198       if (index_buffer) {
199          ret = retry_draw_range_elements( svga,
200                                           index_buffer,
201                                           index_size,
202                                           min_index,
203                                           max_index,
204                                           prim,
205                                           start,
206                                           count,
207                                           TRUE );
208       }
209       else {
210          ret = retry_draw_arrays( svga, 
211                                   prim, 
212                                   start, 
213                                   count,
214                                   TRUE );
215       }
216    }
217
218    if (SVGA_DEBUG & DEBUG_FLUSH) {
219       svga_hwtnl_flush_retry( svga );
220       svga_context_flush(svga, NULL);
221    }
222 }
223
224
225 static void
226 svga_draw_elements( struct pipe_context *pipe,
227                     struct pipe_resource *index_buffer,
228                     unsigned index_size,
229                     unsigned prim, unsigned start, unsigned count)
230 {
231    svga_draw_range_elements( pipe, index_buffer,
232                              index_size,
233                              0, 0xffffffff,
234                              prim, start, count );
235 }
236
237 static void
238 svga_draw_arrays( struct pipe_context *pipe,
239                   unsigned prim, unsigned start, unsigned count)
240 {
241    svga_draw_range_elements(pipe, NULL, 0, 
242                             start, start + count - 1, 
243                             prim, 
244                             start, count);
245 }
246
247
248 void svga_init_draw_functions( struct svga_context *svga )
249 {
250    svga->pipe.draw_arrays = svga_draw_arrays;
251    svga->pipe.draw_elements = svga_draw_elements;
252    svga->pipe.draw_range_elements = svga_draw_range_elements;
253 }