Merge branch 'gallium-embedded'
[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 {
131 struct svga_screen *svgascreen = svga_screen(screen);
132 struct svga_context *svga = NULL;
133 enum pipe_error ret;
134
135 svga = CALLOC_STRUCT(svga_context);
136 if (svga == NULL)
137 goto no_svga;
138
139 svga->pipe.winsys = screen->winsys;
140 svga->pipe.screen = screen;
141 svga->pipe.destroy = svga_destroy;
142 svga->pipe.clear = svga_clear;
143
144 svga->pipe.is_texture_referenced = svga_is_texture_referenced;
145 svga->pipe.is_buffer_referenced = svga_is_buffer_referenced;
146
147 svga->swc = svgascreen->sws->context_create(svgascreen->sws);
148 if(!svga->swc)
149 goto no_swc;
150
151 svga_init_blend_functions(svga);
152 svga_init_blit_functions(svga);
153 svga_init_depth_stencil_functions(svga);
154 svga_init_draw_functions(svga);
155 svga_init_flush_functions(svga);
156 svga_init_misc_functions(svga);
157 svga_init_rasterizer_functions(svga);
158 svga_init_sampler_functions(svga);
159 svga_init_fs_functions(svga);
160 svga_init_vs_functions(svga);
161 svga_init_vertex_functions(svga);
162 svga_init_constbuffer_functions(svga);
163 svga_init_query_functions(svga);
164
165 /* debug */
166 svga->debug.no_swtnl = debug_get_bool_option("SVGA_NO_SWTNL", FALSE);
167 svga->debug.force_swtnl = debug_get_bool_option("SVGA_FORCE_SWTNL", FALSE);
168 svga->debug.use_min_mipmap = debug_get_bool_option("SVGA_USE_MIN_MIPMAP", FALSE);
169 svga->debug.disable_shader = debug_get_num_option("SVGA_DISABLE_SHADER", ~0);
170
171 if (!svga_init_swtnl(svga))
172 goto no_swtnl;
173
174 svga->fs_bm = util_bitmask_create();
175 if (svga->fs_bm == NULL)
176 goto no_fs_bm;
177
178 svga->vs_bm = util_bitmask_create();
179 if (svga->vs_bm == NULL)
180 goto no_vs_bm;
181
182 svga->upload_ib = u_upload_create( svga->pipe.screen,
183 32 * 1024,
184 16,
185 PIPE_BUFFER_USAGE_INDEX );
186 if (svga->upload_ib == NULL)
187 goto no_upload_ib;
188
189 svga->upload_vb = u_upload_create( svga->pipe.screen,
190 128 * 1024,
191 16,
192 PIPE_BUFFER_USAGE_VERTEX );
193 if (svga->upload_vb == NULL)
194 goto no_upload_vb;
195
196 svga->hwtnl = svga_hwtnl_create( svga,
197 svga->upload_ib,
198 svga->swc );
199 if (svga->hwtnl == NULL)
200 goto no_hwtnl;
201
202
203 ret = svga_emit_initial_state( svga );
204 if (ret)
205 goto no_state;
206
207 /* Avoid shortcircuiting state with initial value of zero.
208 */
209 memset(&svga->state.hw_clear, 0xcd, sizeof(svga->state.hw_clear));
210 memset(&svga->state.hw_clear.framebuffer, 0x0,
211 sizeof(svga->state.hw_clear.framebuffer));
212
213 memset(&svga->state.hw_draw, 0xcd, sizeof(svga->state.hw_draw));
214 memset(&svga->state.hw_draw.views, 0x0, sizeof(svga->state.hw_draw.views));
215 svga->state.hw_draw.num_views = 0;
216
217 svga->dirty = ~0;
218
219 LIST_INITHEAD(&svga->dirty_buffers);
220
221 return &svga->pipe;
222
223 no_state:
224 svga_hwtnl_destroy( svga->hwtnl );
225 no_hwtnl:
226 u_upload_destroy( svga->upload_vb );
227 no_upload_vb:
228 u_upload_destroy( svga->upload_ib );
229 no_upload_ib:
230 util_bitmask_destroy( svga->vs_bm );
231 no_vs_bm:
232 util_bitmask_destroy( svga->fs_bm );
233 no_fs_bm:
234 svga_destroy_swtnl(svga);
235 no_swtnl:
236 svga->swc->destroy(svga->swc);
237 no_swc:
238 FREE(svga);
239 no_svga:
240 return NULL;
241 }
242
243
244 void svga_context_flush( struct svga_context *svga,
245 struct pipe_fence_handle **pfence )
246 {
247 struct svga_screen *svgascreen = svga_screen(svga->pipe.screen);
248
249 svga->curr.nr_fbs = 0;
250
251 /* Unmap upload manager buffers:
252 */
253 u_upload_flush(svga->upload_vb);
254 u_upload_flush(svga->upload_ib);
255
256 /* Flush screen, to ensure that texture dma uploads are processed
257 * before submitting commands.
258 */
259 svga_screen_flush(svgascreen, NULL);
260
261 svga_context_flush_buffers(svga);
262
263 /* Flush pending commands to hardware:
264 */
265 svga->swc->flush(svga->swc, pfence);
266
267 if (SVGA_DEBUG & DEBUG_SYNC) {
268 if (pfence && *pfence)
269 svga->pipe.screen->fence_finish( svga->pipe.screen, *pfence, 0);
270 }
271 }
272
273
274 void svga_hwtnl_flush_retry( struct svga_context *svga )
275 {
276 enum pipe_error ret = PIPE_OK;
277
278 ret = svga_hwtnl_flush( svga->hwtnl );
279 if (ret == PIPE_ERROR_OUT_OF_MEMORY) {
280 svga_context_flush( svga, NULL );
281 ret = svga_hwtnl_flush( svga->hwtnl );
282 }
283
284 assert(ret == 0);
285 }
286