Merge remote branch 'origin/master' into pipe-video
[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 int index_bias,
48 unsigned min_index,
49 unsigned max_index,
50 unsigned prim,
51 unsigned start,
52 unsigned count,
53 boolean do_retry )
54 {
55 enum pipe_error ret = 0;
56
57 svga_hwtnl_set_unfilled( svga->hwtnl,
58 svga->curr.rast->hw_unfilled );
59
60 svga_hwtnl_set_flatshade( svga->hwtnl,
61 svga->curr.rast->templ.flatshade,
62 svga->curr.rast->templ.flatshade_first );
63
64
65 ret = svga_update_state( svga, SVGA_STATE_HW_DRAW );
66 if (ret)
67 goto retry;
68
69 ret = svga_hwtnl_draw_range_elements( svga->hwtnl,
70 index_buffer, index_size, index_bias,
71 min_index, max_index,
72 prim, start, count );
73 if (ret)
74 goto retry;
75
76 return PIPE_OK;
77
78 retry:
79 svga_context_flush( svga, NULL );
80
81 if (do_retry)
82 {
83 return retry_draw_range_elements( svga,
84 index_buffer, index_size, index_bias,
85 min_index, max_index,
86 prim, start, count,
87 FALSE );
88 }
89
90 return ret;
91 }
92
93
94 static enum pipe_error
95 retry_draw_arrays( struct svga_context *svga,
96 unsigned prim,
97 unsigned start,
98 unsigned count,
99 boolean do_retry )
100 {
101 enum pipe_error ret;
102
103 svga_hwtnl_set_unfilled( svga->hwtnl,
104 svga->curr.rast->hw_unfilled );
105
106 svga_hwtnl_set_flatshade( svga->hwtnl,
107 svga->curr.rast->templ.flatshade,
108 svga->curr.rast->templ.flatshade_first );
109
110 ret = svga_update_state( svga, SVGA_STATE_HW_DRAW );
111 if (ret)
112 goto retry;
113
114 ret = svga_hwtnl_draw_arrays( svga->hwtnl, prim,
115 start, count );
116 if (ret)
117 goto retry;
118
119 return 0;
120
121 retry:
122 if (ret == PIPE_ERROR_OUT_OF_MEMORY && do_retry)
123 {
124 svga_context_flush( svga, NULL );
125
126 return retry_draw_arrays( svga,
127 prim,
128 start,
129 count,
130 FALSE );
131 }
132
133 return ret;
134 }
135
136
137 static void
138 svga_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info)
139 {
140 struct svga_context *svga = svga_context( pipe );
141 unsigned reduced_prim = u_reduced_prim( info->mode );
142 unsigned count = info->count;
143 enum pipe_error ret = 0;
144
145 if (!u_trim_pipe_prim( info->mode, &count ))
146 return;
147
148 if (svga->state.sw.need_swtnl != svga->prev_draw_swtnl) {
149 /* We're switching between SW and HW drawing. Do a flush to avoid
150 * mixing HW and SW rendering with the same vertex buffer.
151 */
152 pipe->flush(pipe, NULL);
153 svga->prev_draw_swtnl = svga->state.sw.need_swtnl;
154 }
155
156 /*
157 * Mark currently bound target surfaces as dirty
158 * doesn't really matter if it is done before drawing.
159 *
160 * TODO If we ever normaly return something other then
161 * true we should not mark it as dirty then.
162 */
163 svga_mark_surfaces_dirty(svga_context(pipe));
164
165 if (svga->curr.reduced_prim != reduced_prim) {
166 svga->curr.reduced_prim = reduced_prim;
167 svga->dirty |= SVGA_NEW_REDUCED_PRIMITIVE;
168 }
169
170 svga_update_state_retry( svga, SVGA_STATE_NEED_SWTNL );
171
172 #ifdef DEBUG
173 if (svga->curr.vs->base.id == svga->debug.disable_shader ||
174 svga->curr.fs->base.id == svga->debug.disable_shader)
175 return;
176 #endif
177
178 if (svga->state.sw.need_swtnl) {
179 ret = svga_swtnl_draw_vbo( svga, info );
180 }
181 else {
182 if (info->indexed && svga->curr.ib.buffer) {
183 unsigned offset;
184
185 assert(svga->curr.ib.offset % svga->curr.ib.index_size == 0);
186 offset = svga->curr.ib.offset / svga->curr.ib.index_size;
187
188 ret = retry_draw_range_elements( svga,
189 svga->curr.ib.buffer,
190 svga->curr.ib.index_size,
191 info->index_bias,
192 info->min_index,
193 info->max_index,
194 info->mode,
195 info->start + offset,
196 info->count,
197 TRUE );
198 }
199 else {
200 ret = retry_draw_arrays( svga,
201 info->mode,
202 info->start,
203 info->count,
204 TRUE );
205 }
206 }
207
208 if (SVGA_DEBUG & DEBUG_FLUSH) {
209 svga_hwtnl_flush_retry( svga );
210 svga_context_flush(svga, NULL);
211 }
212 }
213
214
215 void svga_init_draw_functions( struct svga_context *svga )
216 {
217 svga->pipe.draw_vbo = svga_draw_vbo;
218 }