Merge branch '7.8'
[mesa.git] / src / gallium / drivers / svga / svga_context.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 "pipe/p_defines.h"
29 #include "util/u_inlines.h"
30 #include "pipe/p_screen.h"
31 #include "util/u_memory.h"
32 #include "util/u_bitmask.h"
33 #include "util/u_upload_mgr.h"
34
35 #include "svga_context.h"
36 #include "svga_screen.h"
37 #include "svga_screen_texture.h"
38 #include "svga_screen_buffer.h"
39 #include "svga_winsys.h"
40 #include "svga_swtnl.h"
41 #include "svga_draw.h"
42 #include "svga_debug.h"
43 #include "svga_state.h"
44
45
46 static void svga_destroy( struct pipe_context *pipe )
47 {
48 struct svga_context *svga = svga_context( pipe );
49 unsigned shader;
50
51 svga_cleanup_framebuffer( svga );
52 svga_cleanup_tss_binding( svga );
53
54 svga_hwtnl_destroy( svga->hwtnl );
55
56 svga_cleanup_vertex_state(svga);
57
58 svga->swc->destroy(svga->swc);
59
60 svga_destroy_swtnl( svga );
61
62 u_upload_destroy( svga->upload_vb );
63 u_upload_destroy( svga->upload_ib );
64
65 util_bitmask_destroy( svga->vs_bm );
66 util_bitmask_destroy( svga->fs_bm );
67
68 for(shader = 0; shader < PIPE_SHADER_TYPES; ++shader)
69 pipe_buffer_reference( &svga->curr.cb[shader], NULL );
70
71 FREE( svga );
72 }
73
74 static unsigned int
75 svga_is_texture_referenced( struct pipe_context *pipe,
76 struct pipe_texture *texture,
77 unsigned face, unsigned level)
78 {
79 struct svga_texture *tex = svga_texture(texture);
80 struct svga_screen *ss = svga_screen(pipe->screen);
81
82 /**
83 * The screen does not cache texture writes.
84 */
85
86 if (!tex->handle || ss->sws->surface_is_flushed(ss->sws, tex->handle))
87 return PIPE_UNREFERENCED;
88
89 /**
90 * sws->surface_is_flushed() does not distinguish between read references
91 * and write references. So assume a reference is both.
92 */
93
94 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
95 }
96
97 static unsigned int
98 svga_is_buffer_referenced( struct pipe_context *pipe,
99 struct pipe_buffer *buf)
100
101 {
102 struct svga_screen *ss = svga_screen(pipe->screen);
103 struct svga_buffer *sbuf = svga_buffer(buf);
104
105 /**
106 * XXX: Check this.
107 * The screen may cache buffer writes, but when we map, we map out
108 * of those cached writes, so we don't need to set a
109 * PIPE_REFERENCED_FOR_WRITE flag for cached buffers.
110 */
111
112 if (!sbuf->handle || ss->sws->surface_is_flushed(ss->sws, sbuf->handle))
113 return PIPE_UNREFERENCED;
114
115 /**
116 * sws->surface_is_flushed() does not distinguish between read references
117 * and write references. So assume a reference is both,
118 * however, we make an exception for index- and vertex buffers, to avoid
119 * a flush in st_bufferobj_get_subdata, during display list replay.
120 */
121
122 if (sbuf->base.usage & (PIPE_BUFFER_USAGE_VERTEX | PIPE_BUFFER_USAGE_INDEX))
123 return PIPE_REFERENCED_FOR_READ;
124
125 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
126 }
127
128
129 struct pipe_context *svga_context_create( struct pipe_screen *screen,
130 void *priv )
131 {
132 struct svga_screen *svgascreen = svga_screen(screen);
133 struct svga_context *svga = NULL;
134 enum pipe_error ret;
135
136 svga = CALLOC_STRUCT(svga_context);
137 if (svga == NULL)
138 goto no_svga;
139
140 svga->pipe.winsys = screen->winsys;
141 svga->pipe.screen = screen;
142 svga->pipe.priv = priv;
143 svga->pipe.destroy = svga_destroy;
144 svga->pipe.clear = svga_clear;
145
146 svga->pipe.is_texture_referenced = svga_is_texture_referenced;
147 svga->pipe.is_buffer_referenced = svga_is_buffer_referenced;
148
149 svga->swc = svgascreen->sws->context_create(svgascreen->sws);
150 if(!svga->swc)
151 goto no_swc;
152
153 svga_init_blend_functions(svga);
154 svga_init_blit_functions(svga);
155 svga_init_depth_stencil_functions(svga);
156 svga_init_draw_functions(svga);
157 svga_init_flush_functions(svga);
158 svga_init_misc_functions(svga);
159 svga_init_rasterizer_functions(svga);
160 svga_init_sampler_functions(svga);
161 svga_init_fs_functions(svga);
162 svga_init_vs_functions(svga);
163 svga_init_vertex_functions(svga);
164 svga_init_constbuffer_functions(svga);
165 svga_init_query_functions(svga);
166
167 svga_init_texture_functions(&svga->pipe);
168
169 /* debug */
170 svga->debug.no_swtnl = debug_get_bool_option("SVGA_NO_SWTNL", FALSE);
171 svga->debug.force_swtnl = debug_get_bool_option("SVGA_FORCE_SWTNL", FALSE);
172 svga->debug.use_min_mipmap = debug_get_bool_option("SVGA_USE_MIN_MIPMAP", FALSE);
173 svga->debug.disable_shader = debug_get_num_option("SVGA_DISABLE_SHADER", ~0);
174
175 if (!svga_init_swtnl(svga))
176 goto no_swtnl;
177
178 svga->fs_bm = util_bitmask_create();
179 if (svga->fs_bm == NULL)
180 goto no_fs_bm;
181
182 svga->vs_bm = util_bitmask_create();
183 if (svga->vs_bm == NULL)
184 goto no_vs_bm;
185
186 svga->upload_ib = u_upload_create( svga->pipe.screen,
187 32 * 1024,
188 16,
189 PIPE_BUFFER_USAGE_INDEX );
190 if (svga->upload_ib == NULL)
191 goto no_upload_ib;
192
193 svga->upload_vb = u_upload_create( svga->pipe.screen,
194 128 * 1024,
195 16,
196 PIPE_BUFFER_USAGE_VERTEX );
197 if (svga->upload_vb == NULL)
198 goto no_upload_vb;
199
200 svga->hwtnl = svga_hwtnl_create( svga,
201 svga->upload_ib,
202 svga->swc );
203 if (svga->hwtnl == NULL)
204 goto no_hwtnl;
205
206
207 ret = svga_emit_initial_state( svga );
208 if (ret)
209 goto no_state;
210
211 /* Avoid shortcircuiting state with initial value of zero.
212 */
213 memset(&svga->state.hw_clear, 0xcd, sizeof(svga->state.hw_clear));
214 memset(&svga->state.hw_clear.framebuffer, 0x0,
215 sizeof(svga->state.hw_clear.framebuffer));
216
217 memset(&svga->state.hw_draw, 0xcd, sizeof(svga->state.hw_draw));
218 memset(&svga->state.hw_draw.views, 0x0, sizeof(svga->state.hw_draw.views));
219 svga->state.hw_draw.num_views = 0;
220
221 svga->dirty = ~0;
222
223 LIST_INITHEAD(&svga->dirty_buffers);
224
225 return &svga->pipe;
226
227 no_state:
228 svga_hwtnl_destroy( svga->hwtnl );
229 no_hwtnl:
230 u_upload_destroy( svga->upload_vb );
231 no_upload_vb:
232 u_upload_destroy( svga->upload_ib );
233 no_upload_ib:
234 util_bitmask_destroy( svga->vs_bm );
235 no_vs_bm:
236 util_bitmask_destroy( svga->fs_bm );
237 no_fs_bm:
238 svga_destroy_swtnl(svga);
239 no_swtnl:
240 svga->swc->destroy(svga->swc);
241 no_swc:
242 FREE(svga);
243 no_svga:
244 return NULL;
245 }
246
247
248 void svga_context_flush( struct svga_context *svga,
249 struct pipe_fence_handle **pfence )
250 {
251 struct svga_screen *svgascreen = svga_screen(svga->pipe.screen);
252
253 svga->curr.nr_fbs = 0;
254
255 /* Unmap upload manager buffers:
256 */
257 u_upload_flush(svga->upload_vb);
258 u_upload_flush(svga->upload_ib);
259
260 /* Flush screen, to ensure that texture dma uploads are processed
261 * before submitting commands.
262 */
263 svga_screen_flush(svgascreen, NULL);
264
265 svga_context_flush_buffers(svga);
266
267 /* Flush pending commands to hardware:
268 */
269 svga->swc->flush(svga->swc, pfence);
270
271 if (SVGA_DEBUG & DEBUG_SYNC) {
272 if (pfence && *pfence)
273 svga->pipe.screen->fence_finish( svga->pipe.screen, *pfence, 0);
274 }
275 }
276
277
278 void svga_hwtnl_flush_retry( struct svga_context *svga )
279 {
280 enum pipe_error ret = PIPE_OK;
281
282 ret = svga_hwtnl_flush( svga->hwtnl );
283 if (ret == PIPE_ERROR_OUT_OF_MEMORY) {
284 svga_context_flush( svga, NULL );
285 ret = svga_hwtnl_flush( svga->hwtnl );
286 }
287
288 assert(ret == 0);
289 }
290