1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 **********************************************************/
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"
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"
49 #define CONST0_UPLOAD_DEFAULT_SIZE 65536
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
);
59 svga_destroy(struct pipe_context
*pipe
)
61 struct svga_context
*svga
= svga_context(pipe
);
64 /* free any alternate rasterizer states used for point sprite */
65 for (i
= 0; i
< ARRAY_SIZE(svga
->rasterizer_no_cull
); i
++) {
66 if (svga
->rasterizer_no_cull
[i
]) {
67 pipe
->delete_rasterizer_state(pipe
, svga
->rasterizer_no_cull
[i
]);
71 /* free depthstencil_disable state */
72 if (svga
->depthstencil_disable
) {
73 pipe
->delete_depth_stencil_alpha_state(pipe
, svga
->depthstencil_disable
);
76 /* free HW constant buffers */
77 for (shader
= 0; shader
< ARRAY_SIZE(svga
->state
.hw_draw
.constbuf
); shader
++) {
78 for (i
= 0; i
< ARRAY_SIZE(svga
->state
.hw_draw
.constbuf
[0]); i
++) {
79 pipe_resource_reference(&svga
->state
.hw_draw
.constbuf
[shader
][i
], NULL
);
83 pipe
->delete_blend_state(pipe
, svga
->noop_blend
);
85 /* destroy stream output statistics queries */
86 svga_destroy_stream_output_queries(svga
);
88 /* free query gb object */
90 pipe
->destroy_query(pipe
, NULL
);
91 svga
->gb_query
= NULL
;
94 util_blitter_destroy(svga
->blitter
);
96 svga_cleanup_sampler_state(svga
);
97 svga_cleanup_framebuffer(svga
);
98 svga_cleanup_tss_binding(svga
);
99 svga_cleanup_vertex_state(svga
);
100 svga_cleanup_tcs_state(svga
);
102 svga_destroy_swtnl(svga
);
103 svga_hwtnl_destroy(svga
->hwtnl
);
105 svga
->swc
->destroy(svga
->swc
);
107 util_bitmask_destroy(svga
->blend_object_id_bm
);
108 util_bitmask_destroy(svga
->ds_object_id_bm
);
109 util_bitmask_destroy(svga
->input_element_object_id_bm
);
110 util_bitmask_destroy(svga
->rast_object_id_bm
);
111 util_bitmask_destroy(svga
->sampler_object_id_bm
);
112 util_bitmask_destroy(svga
->sampler_view_id_bm
);
113 util_bitmask_destroy(svga
->shader_id_bm
);
114 util_bitmask_destroy(svga
->surface_view_id_bm
);
115 util_bitmask_destroy(svga
->stream_output_id_bm
);
116 util_bitmask_destroy(svga
->query_id_bm
);
117 u_upload_destroy(svga
->const0_upload
);
118 u_upload_destroy(svga
->pipe
.stream_uploader
);
119 u_upload_destroy(svga
->pipe
.const_uploader
);
120 svga_texture_transfer_map_upload_destroy(svga
);
122 /* free user's constant buffers */
123 for (shader
= 0; shader
< PIPE_SHADER_TYPES
; ++shader
) {
124 for (i
= 0; i
< ARRAY_SIZE(svga
->curr
.constbufs
[shader
]); ++i
) {
125 pipe_resource_reference(&svga
->curr
.constbufs
[shader
][i
].buffer
, NULL
);
133 struct pipe_context
*
134 svga_context_create(struct pipe_screen
*screen
, void *priv
, unsigned flags
)
136 struct svga_screen
*svgascreen
= svga_screen(screen
);
137 struct svga_context
*svga
= NULL
;
140 SVGA_STATS_TIME_PUSH(svgascreen
->sws
, SVGA_STATS_TIME_CREATECONTEXT
);
142 svga
= CALLOC_STRUCT(svga_context
);
146 list_inithead(&svga
->dirty_buffers
);
148 svga
->pipe
.screen
= screen
;
149 svga
->pipe
.priv
= priv
;
150 svga
->pipe
.destroy
= svga_destroy
;
151 svga
->pipe
.stream_uploader
= u_upload_create(&svga
->pipe
, 1024 * 1024,
152 PIPE_BIND_VERTEX_BUFFER
|
153 PIPE_BIND_INDEX_BUFFER
,
154 PIPE_USAGE_STREAM
, 0);
155 if (!svga
->pipe
.stream_uploader
)
158 u_upload_disable_persistent(svga
->pipe
.stream_uploader
);
160 svga
->pipe
.const_uploader
= u_upload_create(&svga
->pipe
, 128 * 1024,
161 PIPE_BIND_CONSTANT_BUFFER
,
162 PIPE_USAGE_STREAM
, 0);
163 if (!svga
->pipe
.const_uploader
)
166 u_upload_disable_persistent(svga
->pipe
.const_uploader
);
168 svga
->swc
= svgascreen
->sws
->context_create(svgascreen
->sws
);
172 svga_init_resource_functions(svga
);
173 svga_init_blend_functions(svga
);
174 svga_init_blit_functions(svga
);
175 svga_init_depth_stencil_functions(svga
);
176 svga_init_draw_functions(svga
);
177 svga_init_flush_functions(svga
);
178 svga_init_misc_functions(svga
);
179 svga_init_rasterizer_functions(svga
);
180 svga_init_sampler_functions(svga
);
181 svga_init_fs_functions(svga
);
182 svga_init_vs_functions(svga
);
183 svga_init_gs_functions(svga
);
184 svga_init_ts_functions(svga
);
185 svga_init_vertex_functions(svga
);
186 svga_init_constbuffer_functions(svga
);
187 svga_init_query_functions(svga
);
188 svga_init_surface_functions(svga
);
189 svga_init_stream_output_functions(svga
);
190 svga_init_clear_functions(svga
);
191 svga_init_tracked_state(svga
);
193 /* init misc state */
194 svga
->curr
.sample_mask
= ~0;
197 svga
->debug
.no_swtnl
= debug_get_option_no_swtnl();
198 svga
->debug
.force_swtnl
= debug_get_option_force_swtnl();
199 svga
->debug
.use_min_mipmap
= debug_get_option_use_min_mipmap();
200 svga
->debug
.no_line_width
= debug_get_option_no_line_width();
201 svga
->debug
.force_hw_line_stipple
= debug_get_option_force_hw_line_stipple();
203 if (!(svga
->blend_object_id_bm
= util_bitmask_create()))
206 if (!(svga
->ds_object_id_bm
= util_bitmask_create()))
209 if (!(svga
->input_element_object_id_bm
= util_bitmask_create()))
212 if (!(svga
->rast_object_id_bm
= util_bitmask_create()))
215 if (!(svga
->sampler_object_id_bm
= util_bitmask_create()))
218 if (!(svga
->sampler_view_id_bm
= util_bitmask_create()))
221 if (!(svga
->shader_id_bm
= util_bitmask_create()))
224 if (!(svga
->surface_view_id_bm
= util_bitmask_create()))
227 if (!(svga
->stream_output_id_bm
= util_bitmask_create()))
230 if (!(svga
->query_id_bm
= util_bitmask_create()))
233 svga
->hwtnl
= svga_hwtnl_create(svga
);
234 if (svga
->hwtnl
== NULL
)
237 if (!svga_init_swtnl(svga
))
240 ret
= svga_emit_initial_state(svga
);
244 svga
->const0_upload
= u_upload_create(&svga
->pipe
,
245 CONST0_UPLOAD_DEFAULT_SIZE
,
246 PIPE_BIND_CONSTANT_BUFFER
|
248 PIPE_USAGE_STREAM
, 0);
249 if (!svga
->const0_upload
)
252 u_upload_disable_persistent(svga
->const0_upload
);
254 if (!svga_texture_transfer_map_upload_create(svga
))
257 /* Avoid shortcircuiting state with initial value of zero.
259 memset(&svga
->state
.hw_clear
, 0xcd, sizeof(svga
->state
.hw_clear
));
260 memset(&svga
->state
.hw_clear
.framebuffer
, 0x0,
261 sizeof(svga
->state
.hw_clear
.framebuffer
));
262 memset(&svga
->state
.hw_clear
.rtv
, 0, sizeof(svga
->state
.hw_clear
.rtv
));
263 svga
->state
.hw_clear
.num_rendertargets
= 0;
264 svga
->state
.hw_clear
.dsv
= NULL
;
266 memset(&svga
->state
.hw_draw
, 0xcd, sizeof(svga
->state
.hw_draw
));
267 memset(&svga
->state
.hw_draw
.views
, 0x0, sizeof(svga
->state
.hw_draw
.views
));
268 memset(&svga
->state
.hw_draw
.num_samplers
, 0,
269 sizeof(svga
->state
.hw_draw
.num_samplers
));
270 memset(&svga
->state
.hw_draw
.num_sampler_views
, 0,
271 sizeof(svga
->state
.hw_draw
.num_sampler_views
));
272 memset(svga
->state
.hw_draw
.sampler_views
, 0,
273 sizeof(svga
->state
.hw_draw
.sampler_views
));
274 svga
->state
.hw_draw
.num_views
= 0;
275 svga
->state
.hw_draw
.num_backed_views
= 0;
276 svga
->state
.hw_draw
.rasterizer_discard
= FALSE
;
278 /* Initialize the shader pointers */
279 svga
->state
.hw_draw
.vs
= NULL
;
280 svga
->state
.hw_draw
.gs
= NULL
;
281 svga
->state
.hw_draw
.fs
= NULL
;
282 svga
->state
.hw_draw
.tcs
= NULL
;
283 svga
->state
.hw_draw
.tes
= NULL
;
285 /* Initialize the currently bound buffer resources */
286 memset(svga
->state
.hw_draw
.constbuf
, 0,
287 sizeof(svga
->state
.hw_draw
.constbuf
));
288 memset(svga
->state
.hw_draw
.default_constbuf_size
, 0,
289 sizeof(svga
->state
.hw_draw
.default_constbuf_size
));
290 memset(svga
->state
.hw_draw
.enabled_constbufs
, 0,
291 sizeof(svga
->state
.hw_draw
.enabled_constbufs
));
292 svga
->state
.hw_draw
.ib
= NULL
;
293 svga
->state
.hw_draw
.num_vbuffers
= 0;
294 memset(svga
->state
.hw_draw
.vbuffers
, 0,
295 sizeof(svga
->state
.hw_draw
.vbuffers
));
296 svga
->state
.hw_draw
.const0_buffer
= NULL
;
297 svga
->state
.hw_draw
.const0_handle
= NULL
;
299 /* Create a no-operation blend state which we will bind whenever the
300 * requested blend state is impossible (e.g. due to having an integer
301 * render target attached).
303 * XXX: We will probably actually need 16 of these, one for each possible
304 * RGBA color mask (4 bits). Then, we would bind the one with a color mask
305 * matching the blend state it is replacing.
308 struct pipe_blend_state noop_tmpl
= {0};
311 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; ++i
) {
312 // Set the color mask to all-ones. Later this may change.
313 noop_tmpl
.rt
[i
].colormask
= PIPE_MASK_RGBA
;
315 svga
->noop_blend
= svga
->pipe
.create_blend_state(&svga
->pipe
, &noop_tmpl
);
318 svga
->dirty
= SVGA_NEW_ALL
;
319 svga
->pred
.query_id
= SVGA3D_INVALID_ID
;
320 svga
->disable_rasterizer
= FALSE
;
323 * Create stream output statistics queries used in the workaround for auto
324 * draw with stream instancing.
326 svga_create_stream_output_queries(svga
);
331 svga_destroy_swtnl(svga
);
333 if (svga
->const0_upload
)
334 u_upload_destroy(svga
->const0_upload
);
335 if (svga
->pipe
.const_uploader
)
336 u_upload_destroy(svga
->pipe
.const_uploader
);
337 if (svga
->pipe
.stream_uploader
)
338 u_upload_destroy(svga
->pipe
.stream_uploader
);
339 svga_texture_transfer_map_upload_destroy(svga
);
341 svga_hwtnl_destroy(svga
->hwtnl
);
343 svga
->swc
->destroy(svga
->swc
);
344 util_bitmask_destroy(svga
->blend_object_id_bm
);
345 util_bitmask_destroy(svga
->ds_object_id_bm
);
346 util_bitmask_destroy(svga
->input_element_object_id_bm
);
347 util_bitmask_destroy(svga
->rast_object_id_bm
);
348 util_bitmask_destroy(svga
->sampler_object_id_bm
);
349 util_bitmask_destroy(svga
->sampler_view_id_bm
);
350 util_bitmask_destroy(svga
->shader_id_bm
);
351 util_bitmask_destroy(svga
->surface_view_id_bm
);
352 util_bitmask_destroy(svga
->stream_output_id_bm
);
353 util_bitmask_destroy(svga
->query_id_bm
);
358 SVGA_STATS_TIME_POP(svgascreen
->sws
);
359 return svga
? &svga
->pipe
:NULL
;
364 svga_context_flush(struct svga_context
*svga
,
365 struct pipe_fence_handle
**pfence
)
367 struct svga_screen
*svgascreen
= svga_screen(svga
->pipe
.screen
);
368 struct pipe_fence_handle
*fence
= NULL
;
371 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_CONTEXTFLUSH
);
373 svga
->curr
.nr_fbs
= 0;
375 /* Unmap the 0th/default constant buffer. The u_upload_unmap() function
376 * will call pipe_context::transfer_flush_region() to indicate the
377 * region of the buffer which was modified (and needs to be uploaded).
379 if (svga
->state
.hw_draw
.const0_handle
) {
380 assert(svga
->state
.hw_draw
.const0_buffer
);
381 u_upload_unmap(svga
->const0_upload
);
382 pipe_resource_reference(&svga
->state
.hw_draw
.const0_buffer
, NULL
);
383 svga
->state
.hw_draw
.const0_handle
= NULL
;
386 /* Ensure that texture dma uploads are processed
387 * before submitting commands.
389 svga_context_flush_buffers(svga
);
391 svga
->hud
.command_buffer_size
+=
392 svga
->swc
->get_command_buffer_size(svga
->swc
);
394 /* Flush pending commands to hardware:
396 t0
= svga_get_time(svga
);
397 svga
->swc
->flush(svga
->swc
, &fence
);
398 svga
->hud
.flush_time
+= (svga_get_time(svga
) - t0
);
400 svga
->hud
.num_flushes
++;
402 svga_screen_cache_flush(svgascreen
, svga
, fence
);
404 SVGA3D_ResetLastCommand(svga
->swc
);
406 /* To force the re-emission of rendertargets and texture sampler bindings on
407 * the next command buffer.
409 svga
->rebind
.flags
.rendertargets
= TRUE
;
410 svga
->rebind
.flags
.texture_samplers
= TRUE
;
412 if (svga_have_gb_objects(svga
)) {
414 svga
->rebind
.flags
.constbufs
= TRUE
;
415 svga
->rebind
.flags
.vs
= TRUE
;
416 svga
->rebind
.flags
.fs
= TRUE
;
417 svga
->rebind
.flags
.gs
= TRUE
;
419 if (svga_have_sm5(svga
)) {
420 svga
->rebind
.flags
.tcs
= TRUE
;
421 svga
->rebind
.flags
.tes
= TRUE
;
424 if (svga_need_to_rebind_resources(svga
)) {
425 svga
->rebind
.flags
.query
= TRUE
;
429 if (SVGA_DEBUG
& DEBUG_SYNC
) {
431 svga
->pipe
.screen
->fence_finish(svga
->pipe
.screen
, NULL
, fence
,
432 PIPE_TIMEOUT_INFINITE
);
436 svgascreen
->sws
->fence_reference(svgascreen
->sws
, pfence
, fence
);
438 svgascreen
->sws
->fence_reference(svgascreen
->sws
, &fence
, NULL
);
440 SVGA_STATS_TIME_POP(svga_sws(svga
));
445 * Flush pending commands and wait for completion with a fence.
448 svga_context_finish(struct svga_context
*svga
)
450 struct pipe_screen
*screen
= svga
->pipe
.screen
;
451 struct pipe_fence_handle
*fence
= NULL
;
453 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_CONTEXTFINISH
);
455 svga_context_flush(svga
, &fence
);
456 screen
->fence_finish(screen
, NULL
, fence
, PIPE_TIMEOUT_INFINITE
);
457 screen
->fence_reference(screen
, &fence
, NULL
);
459 SVGA_STATS_TIME_POP(svga_sws(svga
));
464 * Emit pending drawing commands to the command buffer.
465 * If the command buffer overflows, we flush it and retry.
466 * \sa svga_hwtnl_flush()
469 svga_hwtnl_flush_retry(struct svga_context
*svga
)
471 enum pipe_error ret
= PIPE_OK
;
473 SVGA_RETRY_OOM(svga
, ret
, svga_hwtnl_flush(svga
->hwtnl
));
474 assert(ret
== PIPE_OK
);
479 * Flush the primitive queue if this buffer is referred.
481 * Otherwise DMA commands on the referred buffer will be emitted too late.
484 svga_hwtnl_flush_buffer(struct svga_context
*svga
,
485 struct pipe_resource
*buffer
)
487 if (svga_hwtnl_is_buffer_referred(svga
->hwtnl
, buffer
)) {
488 svga_hwtnl_flush_retry(svga
);
494 * Emit all operations pending on host surfaces.
497 svga_surfaces_flush(struct svga_context
*svga
)
499 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_SURFACEFLUSH
);
501 /* Emit buffered drawing commands.
503 svga_hwtnl_flush_retry(svga
);
505 /* Emit back-copy from render target views to textures.
507 svga_propagate_rendertargets(svga
);
509 SVGA_STATS_TIME_POP(svga_sws(svga
));
513 struct svga_winsys_context
*
514 svga_winsys_context(struct pipe_context
*pipe
)
516 return svga_context(pipe
)->swc
;