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