util: remove LIST_INITHEAD macro
[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_surface.h"
38 #include "svga_resource_texture.h"
39 #include "svga_resource_buffer.h"
40 #include "svga_resource.h"
41 #include "svga_winsys.h"
42 #include "svga_swtnl.h"
43 #include "svga_draw.h"
44 #include "svga_debug.h"
45 #include "svga_state.h"
46 #include "svga_winsys.h"
47
48 #define CONST0_UPLOAD_DEFAULT_SIZE 65536
49
50 DEBUG_GET_ONCE_BOOL_OPTION(no_swtnl, "SVGA_NO_SWTNL", FALSE)
51 DEBUG_GET_ONCE_BOOL_OPTION(force_swtnl, "SVGA_FORCE_SWTNL", FALSE);
52 DEBUG_GET_ONCE_BOOL_OPTION(use_min_mipmap, "SVGA_USE_MIN_MIPMAP", FALSE);
53 DEBUG_GET_ONCE_BOOL_OPTION(no_line_width, "SVGA_NO_LINE_WIDTH", FALSE);
54 DEBUG_GET_ONCE_BOOL_OPTION(force_hw_line_stipple, "SVGA_FORCE_HW_LINE_STIPPLE", FALSE);
55
56
57 static void
58 svga_destroy(struct pipe_context *pipe)
59 {
60 struct svga_context *svga = svga_context(pipe);
61 unsigned shader, i;
62
63 /* free any alternate rasterizer states used for point sprite */
64 for (i = 0; i < ARRAY_SIZE(svga->rasterizer_no_cull); i++) {
65 if (svga->rasterizer_no_cull[i]) {
66 pipe->delete_rasterizer_state(pipe, svga->rasterizer_no_cull[i]);
67 }
68 }
69
70 /* free depthstencil_disable state */
71 if (svga->depthstencil_disable) {
72 pipe->delete_depth_stencil_alpha_state(pipe, svga->depthstencil_disable);
73 }
74
75 /* free HW constant buffers */
76 for (shader = 0; shader < ARRAY_SIZE(svga->state.hw_draw.constbuf); shader++) {
77 pipe_resource_reference(&svga->state.hw_draw.constbuf[shader], NULL);
78 }
79
80 pipe->delete_blend_state(pipe, svga->noop_blend);
81
82 /* free query gb object */
83 if (svga->gb_query) {
84 pipe->destroy_query(pipe, NULL);
85 svga->gb_query = NULL;
86 }
87
88 util_blitter_destroy(svga->blitter);
89
90 svga_cleanup_sampler_state(svga);
91 svga_cleanup_framebuffer(svga);
92 svga_cleanup_tss_binding(svga);
93 svga_cleanup_vertex_state(svga);
94
95 svga_destroy_swtnl(svga);
96 svga_hwtnl_destroy(svga->hwtnl);
97
98 svga->swc->destroy(svga->swc);
99
100 util_bitmask_destroy(svga->blend_object_id_bm);
101 util_bitmask_destroy(svga->ds_object_id_bm);
102 util_bitmask_destroy(svga->input_element_object_id_bm);
103 util_bitmask_destroy(svga->rast_object_id_bm);
104 util_bitmask_destroy(svga->sampler_object_id_bm);
105 util_bitmask_destroy(svga->sampler_view_id_bm);
106 util_bitmask_destroy(svga->shader_id_bm);
107 util_bitmask_destroy(svga->surface_view_id_bm);
108 util_bitmask_destroy(svga->stream_output_id_bm);
109 util_bitmask_destroy(svga->query_id_bm);
110 u_upload_destroy(svga->const0_upload);
111 u_upload_destroy(svga->pipe.stream_uploader);
112 u_upload_destroy(svga->pipe.const_uploader);
113 svga_texture_transfer_map_upload_destroy(svga);
114
115 /* free user's constant buffers */
116 for (shader = 0; shader < PIPE_SHADER_TYPES; ++shader) {
117 for (i = 0; i < ARRAY_SIZE(svga->curr.constbufs[shader]); ++i) {
118 pipe_resource_reference(&svga->curr.constbufs[shader][i].buffer, NULL);
119 }
120 }
121
122 FREE(svga);
123 }
124
125
126 struct pipe_context *
127 svga_context_create(struct pipe_screen *screen, void *priv, unsigned flags)
128 {
129 struct svga_screen *svgascreen = svga_screen(screen);
130 struct svga_context *svga = NULL;
131 enum pipe_error ret;
132
133 SVGA_STATS_TIME_PUSH(svgascreen->sws, SVGA_STATS_TIME_CREATECONTEXT);
134
135 svga = CALLOC_STRUCT(svga_context);
136 if (!svga)
137 goto done;
138
139 list_inithead(&svga->dirty_buffers);
140
141 svga->pipe.screen = screen;
142 svga->pipe.priv = priv;
143 svga->pipe.destroy = svga_destroy;
144 svga->pipe.stream_uploader = u_upload_create(&svga->pipe, 1024 * 1024,
145 PIPE_BIND_VERTEX_BUFFER |
146 PIPE_BIND_INDEX_BUFFER,
147 PIPE_USAGE_STREAM, 0);
148 if (!svga->pipe.stream_uploader)
149 goto cleanup;
150
151 u_upload_disable_persistent(svga->pipe.stream_uploader);
152
153 svga->pipe.const_uploader = u_upload_create(&svga->pipe, 128 * 1024,
154 PIPE_BIND_CONSTANT_BUFFER,
155 PIPE_USAGE_STREAM, 0);
156 if (!svga->pipe.const_uploader)
157 goto cleanup;
158
159 u_upload_disable_persistent(svga->pipe.const_uploader);
160
161 svga->swc = svgascreen->sws->context_create(svgascreen->sws);
162 if (!svga->swc)
163 goto cleanup;
164
165 svga_init_resource_functions(svga);
166 svga_init_blend_functions(svga);
167 svga_init_blit_functions(svga);
168 svga_init_depth_stencil_functions(svga);
169 svga_init_draw_functions(svga);
170 svga_init_flush_functions(svga);
171 svga_init_misc_functions(svga);
172 svga_init_rasterizer_functions(svga);
173 svga_init_sampler_functions(svga);
174 svga_init_fs_functions(svga);
175 svga_init_vs_functions(svga);
176 svga_init_gs_functions(svga);
177 svga_init_vertex_functions(svga);
178 svga_init_constbuffer_functions(svga);
179 svga_init_query_functions(svga);
180 svga_init_surface_functions(svga);
181 svga_init_stream_output_functions(svga);
182 svga_init_clear_functions(svga);
183
184 /* init misc state */
185 svga->curr.sample_mask = ~0;
186
187 /* debug */
188 svga->debug.no_swtnl = debug_get_option_no_swtnl();
189 svga->debug.force_swtnl = debug_get_option_force_swtnl();
190 svga->debug.use_min_mipmap = debug_get_option_use_min_mipmap();
191 svga->debug.no_line_width = debug_get_option_no_line_width();
192 svga->debug.force_hw_line_stipple = debug_get_option_force_hw_line_stipple();
193
194 if (!(svga->blend_object_id_bm = util_bitmask_create()))
195 goto cleanup;
196
197 if (!(svga->ds_object_id_bm = util_bitmask_create()))
198 goto cleanup;
199
200 if (!(svga->input_element_object_id_bm = util_bitmask_create()))
201 goto cleanup;
202
203 if (!(svga->rast_object_id_bm = util_bitmask_create()))
204 goto cleanup;
205
206 if (!(svga->sampler_object_id_bm = util_bitmask_create()))
207 goto cleanup;
208
209 if (!(svga->sampler_view_id_bm = util_bitmask_create()))
210 goto cleanup;
211
212 if (!(svga->shader_id_bm = util_bitmask_create()))
213 goto cleanup;
214
215 if (!(svga->surface_view_id_bm = util_bitmask_create()))
216 goto cleanup;
217
218 if (!(svga->stream_output_id_bm = util_bitmask_create()))
219 goto cleanup;
220
221 if (!(svga->query_id_bm = util_bitmask_create()))
222 goto cleanup;
223
224 svga->hwtnl = svga_hwtnl_create(svga);
225 if (svga->hwtnl == NULL)
226 goto cleanup;
227
228 if (!svga_init_swtnl(svga))
229 goto cleanup;
230
231 ret = svga_emit_initial_state(svga);
232 if (ret != PIPE_OK)
233 goto cleanup;
234
235 svga->const0_upload = u_upload_create(&svga->pipe,
236 CONST0_UPLOAD_DEFAULT_SIZE,
237 PIPE_BIND_CONSTANT_BUFFER |
238 PIPE_BIND_CUSTOM,
239 PIPE_USAGE_STREAM, 0);
240 if (!svga->const0_upload)
241 goto cleanup;
242
243 u_upload_disable_persistent(svga->const0_upload);
244
245 if (!svga_texture_transfer_map_upload_create(svga))
246 goto cleanup;
247
248 /* Avoid shortcircuiting state with initial value of zero.
249 */
250 memset(&svga->state.hw_clear, 0xcd, sizeof(svga->state.hw_clear));
251 memset(&svga->state.hw_clear.framebuffer, 0x0,
252 sizeof(svga->state.hw_clear.framebuffer));
253 svga->state.hw_clear.num_rendertargets = 0;
254 svga->state.hw_clear.dsv = NULL;
255
256 memset(&svga->state.hw_draw, 0xcd, sizeof(svga->state.hw_draw));
257 memset(&svga->state.hw_draw.views, 0x0, sizeof(svga->state.hw_draw.views));
258 memset(&svga->state.hw_draw.num_samplers, 0,
259 sizeof(svga->state.hw_draw.num_samplers));
260 memset(&svga->state.hw_draw.num_sampler_views, 0,
261 sizeof(svga->state.hw_draw.num_sampler_views));
262 memset(svga->state.hw_draw.sampler_views, 0,
263 sizeof(svga->state.hw_draw.sampler_views));
264 svga->state.hw_draw.num_views = 0;
265 svga->state.hw_draw.num_backed_views = 0;
266 svga->state.hw_draw.rasterizer_discard = FALSE;
267
268 /* Initialize the shader pointers */
269 svga->state.hw_draw.vs = NULL;
270 svga->state.hw_draw.gs = NULL;
271 svga->state.hw_draw.fs = NULL;
272
273 /* Initialize the currently bound buffer resources */
274 memset(svga->state.hw_draw.constbuf, 0,
275 sizeof(svga->state.hw_draw.constbuf));
276 memset(svga->state.hw_draw.default_constbuf_size, 0,
277 sizeof(svga->state.hw_draw.default_constbuf_size));
278 memset(svga->state.hw_draw.enabled_constbufs, 0,
279 sizeof(svga->state.hw_draw.enabled_constbufs));
280 svga->state.hw_draw.ib = NULL;
281 svga->state.hw_draw.num_vbuffers = 0;
282 memset(svga->state.hw_draw.vbuffers, 0,
283 sizeof(svga->state.hw_draw.vbuffers));
284 svga->state.hw_draw.const0_buffer = NULL;
285 svga->state.hw_draw.const0_handle = NULL;
286
287 /* Create a no-operation blend state which we will bind whenever the
288 * requested blend state is impossible (e.g. due to having an integer
289 * render target attached).
290 *
291 * XXX: We will probably actually need 16 of these, one for each possible
292 * RGBA color mask (4 bits). Then, we would bind the one with a color mask
293 * matching the blend state it is replacing.
294 */
295 {
296 struct pipe_blend_state noop_tmpl = {0};
297 unsigned i;
298
299 for (i = 0; i < PIPE_MAX_COLOR_BUFS; ++i) {
300 // Set the color mask to all-ones. Later this may change.
301 noop_tmpl.rt[i].colormask = PIPE_MASK_RGBA;
302 }
303 svga->noop_blend = svga->pipe.create_blend_state(&svga->pipe, &noop_tmpl);
304 }
305
306 svga->dirty = ~0;
307 svga->pred.query_id = SVGA3D_INVALID_ID;
308 svga->disable_rasterizer = FALSE;
309
310 goto done;
311
312 cleanup:
313 svga_destroy_swtnl(svga);
314
315 if (svga->const0_upload)
316 u_upload_destroy(svga->const0_upload);
317 if (svga->pipe.const_uploader)
318 u_upload_destroy(svga->pipe.const_uploader);
319 if (svga->pipe.stream_uploader)
320 u_upload_destroy(svga->pipe.stream_uploader);
321 svga_texture_transfer_map_upload_destroy(svga);
322 if (svga->hwtnl)
323 svga_hwtnl_destroy(svga->hwtnl);
324 if (svga->swc)
325 svga->swc->destroy(svga->swc);
326 util_bitmask_destroy(svga->blend_object_id_bm);
327 util_bitmask_destroy(svga->ds_object_id_bm);
328 util_bitmask_destroy(svga->input_element_object_id_bm);
329 util_bitmask_destroy(svga->rast_object_id_bm);
330 util_bitmask_destroy(svga->sampler_object_id_bm);
331 util_bitmask_destroy(svga->sampler_view_id_bm);
332 util_bitmask_destroy(svga->shader_id_bm);
333 util_bitmask_destroy(svga->surface_view_id_bm);
334 util_bitmask_destroy(svga->stream_output_id_bm);
335 util_bitmask_destroy(svga->query_id_bm);
336 FREE(svga);
337 svga = NULL;
338
339 done:
340 SVGA_STATS_TIME_POP(svgascreen->sws);
341 return svga ? &svga->pipe:NULL;
342 }
343
344
345 void
346 svga_context_flush(struct svga_context *svga,
347 struct pipe_fence_handle **pfence)
348 {
349 struct svga_screen *svgascreen = svga_screen(svga->pipe.screen);
350 struct pipe_fence_handle *fence = NULL;
351 uint64_t t0;
352
353 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CONTEXTFLUSH);
354
355 svga->curr.nr_fbs = 0;
356
357 /* Unmap the 0th/default constant buffer. The u_upload_unmap() function
358 * will call pipe_context::transfer_flush_region() to indicate the
359 * region of the buffer which was modified (and needs to be uploaded).
360 */
361 if (svga->state.hw_draw.const0_handle) {
362 assert(svga->state.hw_draw.const0_buffer);
363 u_upload_unmap(svga->const0_upload);
364 pipe_resource_reference(&svga->state.hw_draw.const0_buffer, NULL);
365 svga->state.hw_draw.const0_handle = NULL;
366 }
367
368 /* Ensure that texture dma uploads are processed
369 * before submitting commands.
370 */
371 svga_context_flush_buffers(svga);
372
373 svga->hud.command_buffer_size +=
374 svga->swc->get_command_buffer_size(svga->swc);
375
376 /* Flush pending commands to hardware:
377 */
378 t0 = svga_get_time(svga);
379 svga->swc->flush(svga->swc, &fence);
380 svga->hud.flush_time += (svga_get_time(svga) - t0);
381
382 svga->hud.num_flushes++;
383
384 svga_screen_cache_flush(svgascreen, svga, fence);
385
386 SVGA3D_ResetLastCommand(svga->swc);
387
388 /* To force the re-emission of rendertargets and texture sampler bindings on
389 * the next command buffer.
390 */
391 svga->rebind.flags.rendertargets = TRUE;
392 svga->rebind.flags.texture_samplers = TRUE;
393
394 if (svga_have_gb_objects(svga)) {
395
396 svga->rebind.flags.constbufs = TRUE;
397 svga->rebind.flags.vs = TRUE;
398 svga->rebind.flags.fs = TRUE;
399 svga->rebind.flags.gs = TRUE;
400
401 if (svga_need_to_rebind_resources(svga)) {
402 svga->rebind.flags.query = TRUE;
403 }
404 }
405
406 if (SVGA_DEBUG & DEBUG_SYNC) {
407 if (fence)
408 svga->pipe.screen->fence_finish(svga->pipe.screen, NULL, fence,
409 PIPE_TIMEOUT_INFINITE);
410 }
411
412 if (pfence)
413 svgascreen->sws->fence_reference(svgascreen->sws, pfence, fence);
414
415 svgascreen->sws->fence_reference(svgascreen->sws, &fence, NULL);
416
417 SVGA_STATS_TIME_POP(svga_sws(svga));
418 }
419
420
421 /**
422 * Flush pending commands and wait for completion with a fence.
423 */
424 void
425 svga_context_finish(struct svga_context *svga)
426 {
427 struct pipe_screen *screen = svga->pipe.screen;
428 struct pipe_fence_handle *fence = NULL;
429
430 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CONTEXTFINISH);
431
432 svga_context_flush(svga, &fence);
433 screen->fence_finish(screen, NULL, fence, PIPE_TIMEOUT_INFINITE);
434 screen->fence_reference(screen, &fence, NULL);
435
436 SVGA_STATS_TIME_POP(svga_sws(svga));
437 }
438
439
440 /**
441 * Emit pending drawing commands to the command buffer.
442 * If the command buffer overflows, we flush it and retry.
443 * \sa svga_hwtnl_flush()
444 */
445 void
446 svga_hwtnl_flush_retry(struct svga_context *svga)
447 {
448 enum pipe_error ret = PIPE_OK;
449
450 ret = svga_hwtnl_flush(svga->hwtnl);
451 if (ret == PIPE_ERROR_OUT_OF_MEMORY) {
452 svga_context_flush(svga, NULL);
453 ret = svga_hwtnl_flush(svga->hwtnl);
454 }
455
456 assert(ret == PIPE_OK);
457 }
458
459
460 /**
461 * Flush the primitive queue if this buffer is referred.
462 *
463 * Otherwise DMA commands on the referred buffer will be emitted too late.
464 */
465 void
466 svga_hwtnl_flush_buffer(struct svga_context *svga,
467 struct pipe_resource *buffer)
468 {
469 if (svga_hwtnl_is_buffer_referred(svga->hwtnl, buffer)) {
470 svga_hwtnl_flush_retry(svga);
471 }
472 }
473
474
475 /**
476 * Emit all operations pending on host surfaces.
477 */
478 void
479 svga_surfaces_flush(struct svga_context *svga)
480 {
481 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_SURFACEFLUSH);
482
483 /* Emit buffered drawing commands.
484 */
485 svga_hwtnl_flush_retry(svga);
486
487 /* Emit back-copy from render target views to textures.
488 */
489 svga_propagate_rendertargets(svga);
490
491 SVGA_STATS_TIME_POP(svga_sws(svga));
492 }
493
494
495 struct svga_winsys_context *
496 svga_winsys_context(struct pipe_context *pipe)
497 {
498 return svga_context(pipe)->swc;
499 }