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"
48 #define CONST0_UPLOAD_DEFAULT_SIZE 65536
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
);
58 svga_destroy(struct pipe_context
*pipe
)
60 struct svga_context
*svga
= svga_context(pipe
);
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
]);
70 /* free depthstencil_disable state */
71 if (svga
->depthstencil_disable
) {
72 pipe
->delete_depth_stencil_alpha_state(pipe
, svga
->depthstencil_disable
);
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
);
80 pipe
->delete_blend_state(pipe
, svga
->noop_blend
);
82 /* free query gb object */
84 pipe
->destroy_query(pipe
, NULL
);
85 svga
->gb_query
= NULL
;
88 util_blitter_destroy(svga
->blitter
);
90 svga_cleanup_sampler_state(svga
);
91 svga_cleanup_framebuffer(svga
);
92 svga_cleanup_tss_binding(svga
);
93 svga_cleanup_vertex_state(svga
);
95 svga_destroy_swtnl(svga
);
96 svga_hwtnl_destroy(svga
->hwtnl
);
98 svga
->swc
->destroy(svga
->swc
);
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
);
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
);
126 struct pipe_context
*
127 svga_context_create(struct pipe_screen
*screen
, void *priv
, unsigned flags
)
129 struct svga_screen
*svgascreen
= svga_screen(screen
);
130 struct svga_context
*svga
= NULL
;
133 svga
= CALLOC_STRUCT(svga_context
);
137 LIST_INITHEAD(&svga
->dirty_buffers
);
139 svga
->pipe
.screen
= screen
;
140 svga
->pipe
.priv
= priv
;
141 svga
->pipe
.destroy
= svga_destroy
;
142 svga
->pipe
.stream_uploader
= u_upload_create(&svga
->pipe
, 1024 * 1024,
143 PIPE_BIND_VERTEX_BUFFER
|
144 PIPE_BIND_INDEX_BUFFER
,
146 if (!svga
->pipe
.stream_uploader
)
149 svga
->pipe
.const_uploader
= u_upload_create(&svga
->pipe
, 128 * 1024,
150 PIPE_BIND_CONSTANT_BUFFER
,
152 if (!svga
->pipe
.const_uploader
)
155 svga
->swc
= svgascreen
->sws
->context_create(svgascreen
->sws
);
159 svga_init_resource_functions(svga
);
160 svga_init_blend_functions(svga
);
161 svga_init_blit_functions(svga
);
162 svga_init_depth_stencil_functions(svga
);
163 svga_init_draw_functions(svga
);
164 svga_init_flush_functions(svga
);
165 svga_init_misc_functions(svga
);
166 svga_init_rasterizer_functions(svga
);
167 svga_init_sampler_functions(svga
);
168 svga_init_fs_functions(svga
);
169 svga_init_vs_functions(svga
);
170 svga_init_gs_functions(svga
);
171 svga_init_vertex_functions(svga
);
172 svga_init_constbuffer_functions(svga
);
173 svga_init_query_functions(svga
);
174 svga_init_surface_functions(svga
);
175 svga_init_stream_output_functions(svga
);
176 svga_init_clear_functions(svga
);
178 /* init misc state */
179 svga
->curr
.sample_mask
= ~0;
182 svga
->debug
.no_swtnl
= debug_get_option_no_swtnl();
183 svga
->debug
.force_swtnl
= debug_get_option_force_swtnl();
184 svga
->debug
.use_min_mipmap
= debug_get_option_use_min_mipmap();
185 svga
->debug
.no_line_width
= debug_get_option_no_line_width();
186 svga
->debug
.force_hw_line_stipple
= debug_get_option_force_hw_line_stipple();
188 if (!(svga
->blend_object_id_bm
= util_bitmask_create()))
191 if (!(svga
->ds_object_id_bm
= util_bitmask_create()))
194 if (!(svga
->input_element_object_id_bm
= util_bitmask_create()))
197 if (!(svga
->rast_object_id_bm
= util_bitmask_create()))
200 if (!(svga
->sampler_object_id_bm
= util_bitmask_create()))
203 if (!(svga
->sampler_view_id_bm
= util_bitmask_create()))
206 if (!(svga
->shader_id_bm
= util_bitmask_create()))
209 if (!(svga
->surface_view_id_bm
= util_bitmask_create()))
212 if (!(svga
->stream_output_id_bm
= util_bitmask_create()))
215 if (!(svga
->query_id_bm
= util_bitmask_create()))
218 svga
->hwtnl
= svga_hwtnl_create(svga
);
219 if (svga
->hwtnl
== NULL
)
222 if (!svga_init_swtnl(svga
))
225 ret
= svga_emit_initial_state(svga
);
229 svga
->const0_upload
= u_upload_create(&svga
->pipe
,
230 CONST0_UPLOAD_DEFAULT_SIZE
,
231 PIPE_BIND_CONSTANT_BUFFER
,
233 if (!svga
->const0_upload
)
236 if (!svga_texture_transfer_map_upload_create(svga
))
239 /* Avoid shortcircuiting state with initial value of zero.
241 memset(&svga
->state
.hw_clear
, 0xcd, sizeof(svga
->state
.hw_clear
));
242 memset(&svga
->state
.hw_clear
.framebuffer
, 0x0,
243 sizeof(svga
->state
.hw_clear
.framebuffer
));
245 memset(&svga
->state
.hw_draw
, 0xcd, sizeof(svga
->state
.hw_draw
));
246 memset(&svga
->state
.hw_draw
.views
, 0x0, sizeof(svga
->state
.hw_draw
.views
));
247 memset(&svga
->state
.hw_draw
.num_samplers
, 0,
248 sizeof(svga
->state
.hw_draw
.num_samplers
));
249 memset(&svga
->state
.hw_draw
.num_sampler_views
, 0,
250 sizeof(svga
->state
.hw_draw
.num_sampler_views
));
251 memset(svga
->state
.hw_draw
.sampler_views
, 0,
252 sizeof(svga
->state
.hw_draw
.sampler_views
));
253 svga
->state
.hw_draw
.num_views
= 0;
254 svga
->state
.hw_draw
.num_rendertargets
= 0;
255 svga
->state
.hw_draw
.dsv
= NULL
;
256 svga
->state
.hw_draw
.rasterizer_discard
= FALSE
;
258 /* Initialize the shader pointers */
259 svga
->state
.hw_draw
.vs
= NULL
;
260 svga
->state
.hw_draw
.gs
= NULL
;
261 svga
->state
.hw_draw
.fs
= NULL
;
263 /* Initialize the currently bound buffer resources */
264 memset(svga
->state
.hw_draw
.constbuf
, 0,
265 sizeof(svga
->state
.hw_draw
.constbuf
));
266 memset(svga
->state
.hw_draw
.default_constbuf_size
, 0,
267 sizeof(svga
->state
.hw_draw
.default_constbuf_size
));
268 memset(svga
->state
.hw_draw
.enabled_constbufs
, 0,
269 sizeof(svga
->state
.hw_draw
.enabled_constbufs
));
270 svga
->state
.hw_draw
.ib
= NULL
;
271 svga
->state
.hw_draw
.num_vbuffers
= 0;
272 memset(svga
->state
.hw_draw
.vbuffers
, 0,
273 sizeof(svga
->state
.hw_draw
.vbuffers
));
274 svga
->state
.hw_draw
.const0_buffer
= NULL
;
275 svga
->state
.hw_draw
.const0_handle
= NULL
;
277 /* Create a no-operation blend state which we will bind whenever the
278 * requested blend state is impossible (e.g. due to having an integer
279 * render target attached).
281 * XXX: We will probably actually need 16 of these, one for each possible
282 * RGBA color mask (4 bits). Then, we would bind the one with a color mask
283 * matching the blend state it is replacing.
286 struct pipe_blend_state noop_tmpl
= {0};
289 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; ++i
) {
290 // Set the color mask to all-ones. Later this may change.
291 noop_tmpl
.rt
[i
].colormask
= PIPE_MASK_RGBA
;
293 svga
->noop_blend
= svga
->pipe
.create_blend_state(&svga
->pipe
, &noop_tmpl
);
297 svga
->pred
.query_id
= SVGA3D_INVALID_ID
;
298 svga
->disable_rasterizer
= FALSE
;
303 svga_destroy_swtnl(svga
);
305 if (svga
->const0_upload
)
306 u_upload_destroy(svga
->const0_upload
);
307 if (svga
->pipe
.const_uploader
)
308 u_upload_destroy(svga
->pipe
.const_uploader
);
309 if (svga
->pipe
.stream_uploader
)
310 u_upload_destroy(svga
->pipe
.stream_uploader
);
311 svga_texture_transfer_map_upload_destroy(svga
);
313 svga_hwtnl_destroy(svga
->hwtnl
);
315 svga
->swc
->destroy(svga
->swc
);
316 util_bitmask_destroy(svga
->blend_object_id_bm
);
317 util_bitmask_destroy(svga
->ds_object_id_bm
);
318 util_bitmask_destroy(svga
->input_element_object_id_bm
);
319 util_bitmask_destroy(svga
->rast_object_id_bm
);
320 util_bitmask_destroy(svga
->sampler_object_id_bm
);
321 util_bitmask_destroy(svga
->sampler_view_id_bm
);
322 util_bitmask_destroy(svga
->shader_id_bm
);
323 util_bitmask_destroy(svga
->surface_view_id_bm
);
324 util_bitmask_destroy(svga
->stream_output_id_bm
);
325 util_bitmask_destroy(svga
->query_id_bm
);
332 svga_context_flush(struct svga_context
*svga
,
333 struct pipe_fence_handle
**pfence
)
335 struct svga_screen
*svgascreen
= svga_screen(svga
->pipe
.screen
);
336 struct pipe_fence_handle
*fence
= NULL
;
339 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_CONTEXTFLUSH
);
341 svga
->curr
.nr_fbs
= 0;
343 /* Unmap the 0th/default constant buffer. The u_upload_unmap() function
344 * will call pipe_context::transfer_flush_region() to indicate the
345 * region of the buffer which was modified (and needs to be uploaded).
347 if (svga
->state
.hw_draw
.const0_handle
) {
348 assert(svga
->state
.hw_draw
.const0_buffer
);
349 u_upload_unmap(svga
->const0_upload
);
350 pipe_resource_reference(&svga
->state
.hw_draw
.const0_buffer
, NULL
);
351 svga
->state
.hw_draw
.const0_handle
= NULL
;
354 /* Ensure that texture dma uploads are processed
355 * before submitting commands.
357 svga_context_flush_buffers(svga
);
359 svga
->hud
.command_buffer_size
+=
360 svga
->swc
->get_command_buffer_size(svga
->swc
);
362 /* Flush pending commands to hardware:
364 t0
= svga_get_time(svga
);
365 svga
->swc
->flush(svga
->swc
, &fence
);
366 svga
->hud
.flush_time
+= (svga_get_time(svga
) - t0
);
368 svga
->hud
.num_flushes
++;
370 svga_screen_cache_flush(svgascreen
, svga
, fence
);
372 SVGA3D_ResetLastCommand(svga
->swc
);
374 /* To force the re-emission of rendertargets and texture sampler bindings on
375 * the next command buffer.
377 svga
->rebind
.flags
.rendertargets
= TRUE
;
378 svga
->rebind
.flags
.texture_samplers
= TRUE
;
380 if (svga_have_gb_objects(svga
)) {
382 svga
->rebind
.flags
.constbufs
= TRUE
;
383 svga
->rebind
.flags
.vs
= TRUE
;
384 svga
->rebind
.flags
.fs
= TRUE
;
385 svga
->rebind
.flags
.gs
= TRUE
;
387 if (svga_need_to_rebind_resources(svga
)) {
388 svga
->rebind
.flags
.query
= TRUE
;
392 if (SVGA_DEBUG
& DEBUG_SYNC
) {
394 svga
->pipe
.screen
->fence_finish(svga
->pipe
.screen
, NULL
, fence
,
395 PIPE_TIMEOUT_INFINITE
);
399 svgascreen
->sws
->fence_reference(svgascreen
->sws
, pfence
, fence
);
401 svgascreen
->sws
->fence_reference(svgascreen
->sws
, &fence
, NULL
);
403 SVGA_STATS_TIME_POP(svga_sws(svga
));
408 * Flush pending commands and wait for completion with a fence.
411 svga_context_finish(struct svga_context
*svga
)
413 struct pipe_screen
*screen
= svga
->pipe
.screen
;
414 struct pipe_fence_handle
*fence
= NULL
;
416 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_CONTEXTFINISH
);
418 svga_context_flush(svga
, &fence
);
419 screen
->fence_finish(screen
, NULL
, fence
, PIPE_TIMEOUT_INFINITE
);
420 screen
->fence_reference(screen
, &fence
, NULL
);
422 SVGA_STATS_TIME_POP(svga_sws(svga
));
427 * Emit pending drawing commands to the command buffer.
428 * If the command buffer overflows, we flush it and retry.
429 * \sa svga_hwtnl_flush()
432 svga_hwtnl_flush_retry(struct svga_context
*svga
)
434 enum pipe_error ret
= PIPE_OK
;
436 ret
= svga_hwtnl_flush(svga
->hwtnl
);
437 if (ret
== PIPE_ERROR_OUT_OF_MEMORY
) {
438 svga_context_flush(svga
, NULL
);
439 ret
= svga_hwtnl_flush(svga
->hwtnl
);
442 assert(ret
== PIPE_OK
);
447 * Flush the primitive queue if this buffer is referred.
449 * Otherwise DMA commands on the referred buffer will be emitted too late.
452 svga_hwtnl_flush_buffer(struct svga_context
*svga
,
453 struct pipe_resource
*buffer
)
455 if (svga_hwtnl_is_buffer_referred(svga
->hwtnl
, buffer
)) {
456 svga_hwtnl_flush_retry(svga
);
462 * Emit all operations pending on host surfaces.
465 svga_surfaces_flush(struct svga_context
*svga
)
467 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_SURFACEFLUSH
);
469 /* Emit buffered drawing commands.
471 svga_hwtnl_flush_retry(svga
);
473 /* Emit back-copy from render target views to textures.
475 svga_propagate_rendertargets(svga
);
477 SVGA_STATS_TIME_POP(svga_sws(svga
));
481 struct svga_winsys_context
*
482 svga_winsys_context(struct pipe_context
*pipe
)
484 return svga_context(pipe
)->swc
;