1 /**********************************************************
2 * Copyright 2008-2022 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.enabled_raw_shaderbufs, 0,
318 sizeof(svga->state.hw_draw.enabled_raw_shaderbufs));
319 memset(svga->state.hw_draw.rawbufs, 0,
320 sizeof(svga->state.hw_draw.rawbufs));
321 svga->state.hw_draw.ib = NULL;
322 svga->state.hw_draw.num_vbuffers = 0;
323 memset(svga->state.hw_draw.vbuffers, 0,
324 sizeof(svga->state.hw_draw.vbuffers));
325 svga->state.hw_draw.const0_buffer = NULL;
326 svga->state.hw_draw.const0_handle = NULL;
327
328 if (svga_have_gl43(svga)) {
329 for (unsigned shader = 0; shader < PIPE_SHADER_TYPES; ++shader) {
330 for (unsigned i = 0;
331 i < ARRAY_SIZE(svga->state.hw_draw.rawbufs[shader]); i++) {
332 svga->state.hw_draw.rawbufs[shader][i].srvid = SVGA3D_INVALID_ID;
333 }
334 }
335 svga_uav_cache_init(svga);
336 svga->dummy_resource = NULL;
337 }
338
339 /* Create a no-operation blend state which we will bind whenever the
340 * requested blend state is impossible (e.g. due to having an integer
341 * render target attached).
342 *
343 * XXX: We will probably actually need 16 of these, one for each possible
344 * RGBA color mask (4 bits). Then, we would bind the one with a color mask
345 * matching the blend state it is replacing.
346 */
347 {
348 struct pipe_blend_state noop_tmpl = {0};
349 unsigned i;
350
351 for (i = 0; i < PIPE_MAX_COLOR_BUFS; ++i) {
352 // Set the color mask to all-ones. Later this may change.
353 noop_tmpl.rt[i].colormask = PIPE_MASK_RGBA;
354 }
355 svga->noop_blend = svga->pipe.create_blend_state(&svga->pipe, &noop_tmpl);
356 }
357
358 svga->dirty = SVGA_NEW_ALL;
359 svga->pred.query_id = SVGA3D_INVALID_ID;
360 svga->disable_rasterizer = false;
361
362 /**
363 * Create stream output statistics queries used in the workaround for auto
364 * draw with stream instancing.
365 */
366 svga_create_stream_output_queries(svga);
367
368 goto done;
369
370 cleanup:
371 svga_destroy_swtnl(svga);
372
373 if (svga->const0_upload)
374 u_upload_destroy(svga->const0_upload);
375 if (svga->pipe.const_uploader)
376 u_upload_destroy(svga->pipe.const_uploader);
377 if (svga->pipe.stream_uploader)
378 u_upload_destroy(svga->pipe.stream_uploader);
379 svga_texture_transfer_map_upload_destroy(svga);
380 if (svga->hwtnl)
381 svga_hwtnl_destroy(svga->hwtnl);
382 if (svga->swc)
383 svga->swc->destroy(svga->swc);
384 util_bitmask_destroy(svga->blend_object_id_bm);
385 util_bitmask_destroy(svga->ds_object_id_bm);
386 util_bitmask_destroy(svga->input_element_object_id_bm);
387 util_bitmask_destroy(svga->rast_object_id_bm);
388 util_bitmask_destroy(svga->sampler_object_id_bm);
389 util_bitmask_destroy(svga->shader_id_bm);
390 util_bitmask_destroy(svga->surface_view_id_bm);
391 util_bitmask_destroy(svga->stream_output_id_bm);
392 util_bitmask_destroy(svga->query_id_bm);
393
394 util_bitmask_destroy(svga->uav_id_bm);
395 util_bitmask_destroy(svga->uav_to_free_id_bm);
396 util_bitmask_destroy(svga->sampler_view_id_bm);
397
398 FREE(svga);
399 svga = NULL;
400
401 done:
402 SVGA_STATS_TIME_POP(svgascreen->sws);
403 return svga ? &svga->pipe:NULL;
404 }
405
406
407 void
svga_context_flush(struct svga_context * svga,struct pipe_fence_handle ** pfence)408 svga_context_flush(struct svga_context *svga,
409 struct pipe_fence_handle **pfence)
410 {
411 struct svga_screen *svgascreen = svga_screen(svga->pipe.screen);
412 struct pipe_fence_handle *fence = NULL;
413 uint64_t t0;
414
415 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CONTEXTFLUSH);
416
417 svga->curr.nr_fbs = 0;
418
419 /* Unmap the 0th/default constant buffer. The u_upload_unmap() function
420 * will call pipe_context::transfer_flush_region() to indicate the
421 * region of the buffer which was modified (and needs to be uploaded).
422 */
423 if (svga->state.hw_draw.const0_handle) {
424 assert(svga->state.hw_draw.const0_buffer);
425 u_upload_unmap(svga->const0_upload);
426 pipe_resource_reference(&svga->state.hw_draw.const0_buffer, NULL);
427 svga->state.hw_draw.const0_handle = NULL;
428 }
429
430 /* Ensure that texture dma uploads are processed
431 * before submitting commands.
432 */
433 svga_context_flush_buffers(svga);
434
435 svga->hud.command_buffer_size +=
436 svga->swc->get_command_buffer_size(svga->swc);
437
438 /* Flush pending commands to hardware:
439 */
440 t0 = svga_get_time(svga);
441 svga->swc->flush(svga->swc, &fence);
442 svga->hud.flush_time += (svga_get_time(svga) - t0);
443
444 svga->hud.num_flushes++;
445
446 svga_screen_cache_flush(svgascreen, svga, fence);
447
448 SVGA3D_ResetLastCommand(svga->swc);
449
450 /* To force the re-emission of rendertargets and texture sampler bindings on
451 * the next command buffer.
452 */
453 svga->rebind.flags.rendertargets = true;
454 svga->rebind.flags.texture_samplers = true;
455
456 if (svga_have_gb_objects(svga)) {
457
458 svga->rebind.flags.constbufs = true;
459 svga->rebind.flags.vs = true;
460 svga->rebind.flags.fs = true;
461 svga->rebind.flags.gs = true;
462
463 if (svga_have_sm5(svga)) {
464 svga->rebind.flags.tcs = true;
465 svga->rebind.flags.tes = true;
466 }
467
468 if (svga_need_to_rebind_resources(svga)) {
469 svga->rebind.flags.query = true;
470 }
471
472 if (svga_sws(svga)->have_index_vertex_buffer_offset_cmd) {
473 svga->rebind.flags.vertexbufs = true;
474 svga->rebind.flags.indexbuf = true;
475 }
476 }
477
478 if (SVGA_DEBUG & DEBUG_SYNC) {
479 if (fence)
480 svga->pipe.screen->fence_finish(svga->pipe.screen, NULL, fence,
481 OS_TIMEOUT_INFINITE);
482 }
483
484 if (pfence)
485 svgascreen->sws->fence_reference(svgascreen->sws, pfence, fence);
486
487 svgascreen->sws->fence_reference(svgascreen->sws, &fence, NULL);
488
489 SVGA_STATS_TIME_POP(svga_sws(svga));
490 }
491
492
493 /**
494 * Flush pending commands and wait for completion with a fence.
495 */
496 void
svga_context_finish(struct svga_context * svga)497 svga_context_finish(struct svga_context *svga)
498 {
499 struct pipe_screen *screen = svga->pipe.screen;
500 struct pipe_fence_handle *fence = NULL;
501
502 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CONTEXTFINISH);
503
504 svga_context_flush(svga, &fence);
505 screen->fence_finish(screen, NULL, fence, OS_TIMEOUT_INFINITE);
506 screen->fence_reference(screen, &fence, NULL);
507
508 SVGA_STATS_TIME_POP(svga_sws(svga));
509 }
510
511
512 /**
513 * Emit pending drawing commands to the command buffer.
514 * If the command buffer overflows, we flush it and retry.
515 * \sa svga_hwtnl_flush()
516 */
517 void
svga_hwtnl_flush_retry(struct svga_context * svga)518 svga_hwtnl_flush_retry(struct svga_context *svga)
519 {
520 enum pipe_error ret = PIPE_OK;
521
522 SVGA_RETRY_OOM(svga, ret, svga_hwtnl_flush(svga->hwtnl));
523 assert(ret == PIPE_OK);
524 }
525
526
527 /**
528 * Flush the primitive queue if this buffer is referred.
529 *
530 * Otherwise DMA commands on the referred buffer will be emitted too late.
531 */
532 void
svga_hwtnl_flush_buffer(struct svga_context * svga,struct pipe_resource * buffer)533 svga_hwtnl_flush_buffer(struct svga_context *svga,
534 struct pipe_resource *buffer)
535 {
536 if (svga_hwtnl_is_buffer_referred(svga->hwtnl, buffer)) {
537 svga_hwtnl_flush_retry(svga);
538 }
539 }
540
541
542 /**
543 * Emit all operations pending on host surfaces.
544 */
545 void
svga_surfaces_flush(struct svga_context * svga)546 svga_surfaces_flush(struct svga_context *svga)
547 {
548 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_SURFACEFLUSH);
549
550 /* Emit buffered drawing commands.
551 */
552 svga_hwtnl_flush_retry(svga);
553
554 /* Emit back-copy from render target views to textures.
555 */
556 svga_propagate_rendertargets(svga);
557
558 SVGA_STATS_TIME_POP(svga_sws(svga));
559 }
560
561
562 struct svga_winsys_context *
svga_winsys_context(struct pipe_context * pipe)563 svga_winsys_context(struct pipe_context *pipe)
564 {
565 return svga_context(pipe)->swc;
566 }
567