Merge branch 'master' of ssh://git.freedesktop.org/git/mesa/mesa into pipe-video
[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_resource_texture.h"
38 #include "svga_resource_buffer.h"
39 #include "svga_resource.h"
40 #include "svga_winsys.h"
41 #include "svga_swtnl.h"
42 #include "svga_draw.h"
43 #include "svga_debug.h"
44 #include "svga_state.h"
45
46
47 static void svga_destroy( struct pipe_context *pipe )
48 {
49 struct svga_context *svga = svga_context( pipe );
50 unsigned shader;
51
52 svga_cleanup_framebuffer( svga );
53 svga_cleanup_tss_binding( svga );
54
55 svga_hwtnl_destroy( svga->hwtnl );
56
57 svga_cleanup_vertex_state(svga);
58
59 svga->swc->destroy(svga->swc);
60
61 svga_destroy_swtnl( svga );
62
63 u_upload_destroy( svga->upload_vb );
64 u_upload_destroy( svga->upload_ib );
65
66 util_bitmask_destroy( svga->vs_bm );
67 util_bitmask_destroy( svga->fs_bm );
68
69 for(shader = 0; shader < PIPE_SHADER_TYPES; ++shader)
70 pipe_resource_reference( &svga->curr.cb[shader], NULL );
71
72 FREE( svga );
73 }
74
75
76
77 struct pipe_context *svga_context_create( struct pipe_screen *screen,
78 void *priv )
79 {
80 struct svga_screen *svgascreen = svga_screen(screen);
81 struct svga_context *svga = NULL;
82 enum pipe_error ret;
83
84 svga = CALLOC_STRUCT(svga_context);
85 if (svga == NULL)
86 goto no_svga;
87
88 svga->pipe.winsys = screen->winsys;
89 svga->pipe.screen = screen;
90 svga->pipe.priv = priv;
91 svga->pipe.destroy = svga_destroy;
92 svga->pipe.clear = svga_clear;
93
94 svga->swc = svgascreen->sws->context_create(svgascreen->sws);
95 if(!svga->swc)
96 goto no_swc;
97
98 svga_init_resource_functions(svga);
99 svga_init_blend_functions(svga);
100 svga_init_blit_functions(svga);
101 svga_init_depth_stencil_functions(svga);
102 svga_init_draw_functions(svga);
103 svga_init_flush_functions(svga);
104 svga_init_misc_functions(svga);
105 svga_init_rasterizer_functions(svga);
106 svga_init_sampler_functions(svga);
107 svga_init_fs_functions(svga);
108 svga_init_vs_functions(svga);
109 svga_init_vertex_functions(svga);
110 svga_init_constbuffer_functions(svga);
111 svga_init_query_functions(svga);
112
113
114 /* debug */
115 svga->debug.no_swtnl = debug_get_bool_option("SVGA_NO_SWTNL", FALSE);
116 svga->debug.force_swtnl = debug_get_bool_option("SVGA_FORCE_SWTNL", FALSE);
117 svga->debug.use_min_mipmap = debug_get_bool_option("SVGA_USE_MIN_MIPMAP", FALSE);
118 svga->debug.disable_shader = debug_get_num_option("SVGA_DISABLE_SHADER", ~0);
119
120 if (!svga_init_swtnl(svga))
121 goto no_swtnl;
122
123 svga->fs_bm = util_bitmask_create();
124 if (svga->fs_bm == NULL)
125 goto no_fs_bm;
126
127 svga->vs_bm = util_bitmask_create();
128 if (svga->vs_bm == NULL)
129 goto no_vs_bm;
130
131 svga->upload_ib = u_upload_create( &svga->pipe,
132 32 * 1024,
133 16,
134 PIPE_BIND_INDEX_BUFFER );
135 if (svga->upload_ib == NULL)
136 goto no_upload_ib;
137
138 svga->upload_vb = u_upload_create( &svga->pipe,
139 128 * 1024,
140 16,
141 PIPE_BIND_VERTEX_BUFFER );
142 if (svga->upload_vb == NULL)
143 goto no_upload_vb;
144
145 svga->hwtnl = svga_hwtnl_create( svga,
146 svga->upload_ib,
147 svga->swc );
148 if (svga->hwtnl == NULL)
149 goto no_hwtnl;
150
151
152 ret = svga_emit_initial_state( svga );
153 if (ret)
154 goto no_state;
155
156 /* Avoid shortcircuiting state with initial value of zero.
157 */
158 memset(&svga->state.hw_clear, 0xcd, sizeof(svga->state.hw_clear));
159 memset(&svga->state.hw_clear.framebuffer, 0x0,
160 sizeof(svga->state.hw_clear.framebuffer));
161
162 memset(&svga->state.hw_draw, 0xcd, sizeof(svga->state.hw_draw));
163 memset(&svga->state.hw_draw.views, 0x0, sizeof(svga->state.hw_draw.views));
164 svga->state.hw_draw.num_views = 0;
165
166 svga->dirty = ~0;
167
168 LIST_INITHEAD(&svga->dirty_buffers);
169
170 return &svga->pipe;
171
172 no_state:
173 svga_hwtnl_destroy( svga->hwtnl );
174 no_hwtnl:
175 u_upload_destroy( svga->upload_vb );
176 no_upload_vb:
177 u_upload_destroy( svga->upload_ib );
178 no_upload_ib:
179 util_bitmask_destroy( svga->vs_bm );
180 no_vs_bm:
181 util_bitmask_destroy( svga->fs_bm );
182 no_fs_bm:
183 svga_destroy_swtnl(svga);
184 no_swtnl:
185 svga->swc->destroy(svga->swc);
186 no_swc:
187 FREE(svga);
188 no_svga:
189 return NULL;
190 }
191
192
193 void svga_context_flush( struct svga_context *svga,
194 struct pipe_fence_handle **pfence )
195 {
196 struct svga_screen *svgascreen = svga_screen(svga->pipe.screen);
197 struct pipe_fence_handle *fence = NULL;
198
199 svga->curr.nr_fbs = 0;
200
201 /* Unmap upload manager buffers:
202 */
203 u_upload_flush(svga->upload_vb);
204 u_upload_flush(svga->upload_ib);
205
206 /* Ensure that texture dma uploads are processed
207 * before submitting commands.
208 */
209 svga_context_flush_buffers(svga);
210
211 /* Flush pending commands to hardware:
212 */
213 svga->swc->flush(svga->swc, &fence);
214
215 svga_screen_cache_flush(svgascreen, fence);
216
217 /* To force the reemission of rendertargets and texture bindings at
218 * the beginning of every command buffer.
219 */
220 svga->dirty |= SVGA_NEW_COMMAND_BUFFER;
221
222 if (SVGA_DEBUG & DEBUG_SYNC) {
223 if (fence)
224 svga->pipe.screen->fence_finish( svga->pipe.screen, fence, 0);
225 }
226
227 if(pfence)
228 *pfence = fence;
229 else
230 svgascreen->sws->fence_reference(svgascreen->sws, &fence, NULL);
231 }
232
233
234 void svga_hwtnl_flush_retry( struct svga_context *svga )
235 {
236 enum pipe_error ret = PIPE_OK;
237
238 ret = svga_hwtnl_flush( svga->hwtnl );
239 if (ret == PIPE_ERROR_OUT_OF_MEMORY) {
240 svga_context_flush( svga, NULL );
241 ret = svga_hwtnl_flush( svga->hwtnl );
242 }
243
244 assert(ret == 0);
245 }
246
247 struct svga_winsys_context *
248 svga_winsys_context( struct pipe_context *pipe )
249 {
250 return svga_context( pipe )->swc;
251 }