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