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