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