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