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