svga: add work-around for Sauerbraten Z fighting issue
[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 "os/os_process.h"
29 #include "pipe/p_defines.h"
30 #include "util/u_inlines.h"
31 #include "pipe/p_screen.h"
32 #include "util/u_memory.h"
33 #include "util/u_bitmask.h"
34 #include "util/u_string.h"
35
36 #include "svga_context.h"
37 #include "svga_screen.h"
38 #include "svga_surface.h"
39 #include "svga_resource_texture.h"
40 #include "svga_resource_buffer.h"
41 #include "svga_resource.h"
42 #include "svga_winsys.h"
43 #include "svga_swtnl.h"
44 #include "svga_draw.h"
45 #include "svga_debug.h"
46 #include "svga_state.h"
47
48 DEBUG_GET_ONCE_BOOL_OPTION(no_swtnl, "SVGA_NO_SWTNL", FALSE)
49 DEBUG_GET_ONCE_BOOL_OPTION(force_swtnl, "SVGA_FORCE_SWTNL", FALSE);
50 DEBUG_GET_ONCE_BOOL_OPTION(use_min_mipmap, "SVGA_USE_MIN_MIPMAP", FALSE);
51 DEBUG_GET_ONCE_NUM_OPTION(disable_shader, "SVGA_DISABLE_SHADER", ~0);
52 DEBUG_GET_ONCE_BOOL_OPTION(no_line_width, "SVGA_NO_LINE_WIDTH", FALSE);
53 DEBUG_GET_ONCE_BOOL_OPTION(force_hw_line_stipple, "SVGA_FORCE_HW_LINE_STIPPLE", FALSE);
54
55 static void svga_destroy( struct pipe_context *pipe )
56 {
57 struct svga_context *svga = svga_context( pipe );
58 struct svga_winsys_screen *sws = svga_screen(pipe->screen)->sws;
59 unsigned shader;
60
61 util_blitter_destroy(svga->blitter);
62
63 svga_cleanup_framebuffer( svga );
64 svga_cleanup_tss_binding( svga );
65
66 svga_hwtnl_destroy( svga->hwtnl );
67
68 svga_cleanup_vertex_state(svga);
69
70 svga->swc->destroy(svga->swc);
71
72 svga_destroy_swtnl( svga );
73
74 util_bitmask_destroy( svga->shader_id_bm );
75
76 for (shader = 0; shader < PIPE_SHADER_TYPES; ++shader) {
77 pipe_resource_reference( &svga->curr.cbufs[shader].buffer, NULL );
78 sws->surface_reference(sws, &svga->state.hw_draw.hw_cb[shader], NULL);
79 }
80
81 FREE( svga );
82 }
83
84
85 /**
86 * Check the process name to see if we're running with an app that
87 * needs any particular work-arounds.
88 */
89 static void
90 check_for_workarounds(struct svga_context *svga)
91 {
92 char name[1000];
93
94 if (!os_get_process_name(name, sizeof(name)))
95 return;
96
97 if (util_strcmp(name, "sauer_client") == 0) {
98 /*
99 * Sauerbraten uses a two-pass rendering algorithm. The first pass
100 * draws a depth map. The second pass draws the colors. On the second
101 * pass we wind up using the swtnl path because the game tries to use
102 * a GLbyte[3] normal vector array (which the SVGA3D protocol does not
103 * support.) The vertices of the first and second passes don't quite
104 * match so we see some depth/Z-fighting issues. This work-around
105 * causes us to map GLbyte[3] to SVGA3D_DECLTYPE_UBYTE4N and avoid the
106 * swtnl path. Despite not correctly converting normal vectors from
107 * GLbyte[3] to float[4], the rendering looks OK.
108 */
109 debug_printf("Enabling sauerbraten GLbyte[3] work-around\n");
110 svga->workaround.use_decltype_ubyte4n = TRUE;
111 }
112 }
113
114
115 struct pipe_context *svga_context_create( struct pipe_screen *screen,
116 void *priv )
117 {
118 struct svga_screen *svgascreen = svga_screen(screen);
119 struct svga_context *svga = NULL;
120 enum pipe_error ret;
121
122 svga = CALLOC_STRUCT(svga_context);
123 if (svga == NULL)
124 goto no_svga;
125
126 svga->pipe.screen = screen;
127 svga->pipe.priv = priv;
128 svga->pipe.destroy = svga_destroy;
129 svga->pipe.clear = svga_clear;
130
131 svga->swc = svgascreen->sws->context_create(svgascreen->sws);
132 if(!svga->swc)
133 goto no_swc;
134
135 svga_init_resource_functions(svga);
136 svga_init_blend_functions(svga);
137 svga_init_blit_functions(svga);
138 svga_init_depth_stencil_functions(svga);
139 svga_init_draw_functions(svga);
140 svga_init_flush_functions(svga);
141 svga_init_misc_functions(svga);
142 svga_init_rasterizer_functions(svga);
143 svga_init_sampler_functions(svga);
144 svga_init_fs_functions(svga);
145 svga_init_vs_functions(svga);
146 svga_init_vertex_functions(svga);
147 svga_init_constbuffer_functions(svga);
148 svga_init_query_functions(svga);
149 svga_init_surface_functions(svga);
150
151
152 /* debug */
153 svga->debug.no_swtnl = debug_get_option_no_swtnl();
154 svga->debug.force_swtnl = debug_get_option_force_swtnl();
155 svga->debug.use_min_mipmap = debug_get_option_use_min_mipmap();
156 svga->debug.disable_shader = debug_get_option_disable_shader();
157 svga->debug.no_line_width = debug_get_option_no_line_width();
158 svga->debug.force_hw_line_stipple = debug_get_option_force_hw_line_stipple();
159
160 svga->shader_id_bm = util_bitmask_create();
161 if (svga->shader_id_bm == NULL)
162 goto no_shader_bm;
163
164 svga->hwtnl = svga_hwtnl_create(svga);
165 if (svga->hwtnl == NULL)
166 goto no_hwtnl;
167
168 if (!svga_init_swtnl(svga))
169 goto no_swtnl;
170
171 ret = svga_emit_initial_state( svga );
172 if (ret != PIPE_OK)
173 goto no_state;
174
175 /* Avoid shortcircuiting state with initial value of zero.
176 */
177 memset(&svga->state.hw_clear, 0xcd, sizeof(svga->state.hw_clear));
178 memset(&svga->state.hw_clear.framebuffer, 0x0,
179 sizeof(svga->state.hw_clear.framebuffer));
180
181 memset(&svga->state.hw_draw, 0xcd, sizeof(svga->state.hw_draw));
182 memset(&svga->state.hw_draw.views, 0x0, sizeof(svga->state.hw_draw.views));
183 svga->state.hw_draw.num_views = 0;
184 memset(&svga->state.hw_draw.hw_cb, 0x0, sizeof(svga->state.hw_draw.hw_cb));
185
186 svga->dirty = ~0;
187
188 LIST_INITHEAD(&svga->dirty_buffers);
189
190 check_for_workarounds(svga);
191
192 return &svga->pipe;
193
194 no_state:
195 svga_destroy_swtnl(svga);
196 no_swtnl:
197 svga_hwtnl_destroy( svga->hwtnl );
198 no_hwtnl:
199 util_bitmask_destroy( svga->shader_id_bm );
200 no_shader_bm:
201 svga->swc->destroy(svga->swc);
202 no_swc:
203 FREE(svga);
204 no_svga:
205 return NULL;
206 }
207
208
209 void svga_context_flush( struct svga_context *svga,
210 struct pipe_fence_handle **pfence )
211 {
212 struct svga_screen *svgascreen = svga_screen(svga->pipe.screen);
213 struct pipe_fence_handle *fence = NULL;
214
215 svga->curr.nr_fbs = 0;
216
217 /* Ensure that texture dma uploads are processed
218 * before submitting commands.
219 */
220 svga_context_flush_buffers(svga);
221
222 /* Flush pending commands to hardware:
223 */
224 svga->swc->flush(svga->swc, &fence);
225
226 svga_screen_cache_flush(svgascreen, fence);
227
228 /* To force the re-emission of rendertargets and texture sampler bindings on
229 * the next command buffer.
230 */
231 svga->rebind.rendertargets = TRUE;
232 svga->rebind.texture_samplers = TRUE;
233 if (svga_have_gb_objects(svga)) {
234 svga->rebind.vs = TRUE;
235 svga->rebind.fs = TRUE;
236 }
237
238 if (SVGA_DEBUG & DEBUG_SYNC) {
239 if (fence)
240 svga->pipe.screen->fence_finish( svga->pipe.screen, fence,
241 PIPE_TIMEOUT_INFINITE);
242 }
243
244 if(pfence)
245 svgascreen->sws->fence_reference(svgascreen->sws, pfence, fence);
246
247 svgascreen->sws->fence_reference(svgascreen->sws, &fence, NULL);
248 }
249
250
251 void svga_hwtnl_flush_retry( struct svga_context *svga )
252 {
253 enum pipe_error ret = PIPE_OK;
254
255 ret = svga_hwtnl_flush( svga->hwtnl );
256 if (ret == PIPE_ERROR_OUT_OF_MEMORY) {
257 svga_context_flush( svga, NULL );
258 ret = svga_hwtnl_flush( svga->hwtnl );
259 }
260
261 assert(ret == 0);
262 }
263
264
265 /**
266 * Flush the primitive queue if this buffer is referred.
267 *
268 * Otherwise DMA commands on the referred buffer will be emitted too late.
269 */
270 void svga_hwtnl_flush_buffer( struct svga_context *svga,
271 struct pipe_resource *buffer )
272 {
273 if (svga_hwtnl_is_buffer_referred(svga->hwtnl, buffer)) {
274 svga_hwtnl_flush_retry(svga);
275 }
276 }
277
278
279 /* Emit all operations pending on host surfaces.
280 */
281 void svga_surfaces_flush(struct svga_context *svga)
282 {
283 struct svga_screen *svgascreen = svga_screen(svga->pipe.screen);
284 unsigned i;
285
286 /* Emit buffered drawing commands.
287 */
288 svga_hwtnl_flush_retry( svga );
289
290 /* Emit back-copy from render target view to texture.
291 */
292 for (i = 0; i < svgascreen->max_color_buffers; i++) {
293 if (svga->curr.framebuffer.cbufs[i])
294 svga_propagate_surface(svga, svga->curr.framebuffer.cbufs[i]);
295 }
296
297 if (svga->curr.framebuffer.zsbuf)
298 svga_propagate_surface(svga, svga->curr.framebuffer.zsbuf);
299
300 }
301
302
303 struct svga_winsys_context *
304 svga_winsys_context( struct pipe_context *pipe )
305 {
306 return svga_context( pipe )->swc;
307 }