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