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 "pipe/p_inlines.h"
30 #include "pipe/p_screen.h"
31 #include "util/u_memory.h"
32 #include "util/u_upload_mgr.h"
34 #include "svga_context.h"
35 #include "svga_screen.h"
36 #include "svga_screen_texture.h"
37 #include "svga_screen_buffer.h"
38 #include "svga_winsys.h"
39 #include "svga_swtnl.h"
40 #include "svga_draw.h"
41 #include "svga_debug.h"
42 #include "svga_state.h"
45 static void svga_destroy( struct pipe_context
*pipe
)
47 struct svga_context
*svga
= svga_context( pipe
);
50 svga_cleanup_framebuffer( svga
);
51 svga_cleanup_tss_binding( svga
);
53 svga_hwtnl_destroy( svga
->hwtnl
);
55 svga_cleanup_vertex_state(svga
);
57 svga
->swc
->destroy(svga
->swc
);
59 svga_destroy_swtnl( svga
);
61 u_upload_destroy( svga
->upload_vb
);
62 u_upload_destroy( svga
->upload_ib
);
64 for(shader
= 0; shader
< PIPE_SHADER_TYPES
; ++shader
)
65 pipe_buffer_reference( &svga
->curr
.cb
[shader
], NULL
);
71 svga_is_texture_referenced( struct pipe_context
*pipe
,
72 struct pipe_texture
*texture
,
73 unsigned face
, unsigned level
)
75 struct svga_texture
*tex
= svga_texture(texture
);
76 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
79 * The screen does not cache texture writes.
82 if (!tex
->handle
|| ss
->sws
->surface_is_flushed(ss
->sws
, tex
->handle
))
83 return PIPE_UNREFERENCED
;
86 * sws->surface_is_flushed() does not distinguish between read references
87 * and write references. So assume a reference is both.
90 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
94 svga_is_buffer_referenced( struct pipe_context
*pipe
,
95 struct pipe_buffer
*buf
)
98 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
99 struct svga_buffer
*sbuf
= svga_buffer(buf
);
103 * The screen may cache buffer writes, but when we map, we map out
104 * of those cached writes, so we don't need to set a
105 * PIPE_REFERENCED_FOR_WRITE flag for cached buffers.
108 if (!sbuf
->handle
|| ss
->sws
->surface_is_flushed(ss
->sws
, sbuf
->handle
))
109 return PIPE_UNREFERENCED
;
112 * sws->surface_is_flushed() does not distinguish between read references
113 * and write references. So assume a reference is both,
114 * however, we make an exception for index- and vertex buffers, to avoid
115 * a flush in st_bufferobj_get_subdata, during display list replay.
118 if (sbuf
->base
.usage
& (PIPE_BUFFER_USAGE_VERTEX
| PIPE_BUFFER_USAGE_INDEX
))
119 return PIPE_REFERENCED_FOR_READ
;
121 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
125 struct pipe_context
*svga_context_create( struct pipe_screen
*screen
)
127 struct svga_screen
*svgascreen
= svga_screen(screen
);
128 struct svga_context
*svga
= NULL
;
131 svga
= CALLOC_STRUCT(svga_context
);
135 svga
->pipe
.winsys
= screen
->winsys
;
136 svga
->pipe
.screen
= screen
;
137 svga
->pipe
.destroy
= svga_destroy
;
138 svga
->pipe
.clear
= svga_clear
;
140 svga
->pipe
.is_texture_referenced
= svga_is_texture_referenced
;
141 svga
->pipe
.is_buffer_referenced
= svga_is_buffer_referenced
;
143 svga
->swc
= svgascreen
->sws
->context_create(svgascreen
->sws
);
147 svga_init_blend_functions(svga
);
148 svga_init_blit_functions(svga
);
149 svga_init_depth_stencil_functions(svga
);
150 svga_init_draw_functions(svga
);
151 svga_init_flush_functions(svga
);
152 svga_init_misc_functions(svga
);
153 svga_init_rasterizer_functions(svga
);
154 svga_init_sampler_functions(svga
);
155 svga_init_fs_functions(svga
);
156 svga_init_vs_functions(svga
);
157 svga_init_vertex_functions(svga
);
158 svga_init_constbuffer_functions(svga
);
159 svga_init_query_functions(svga
);
162 svga
->debug
.no_swtnl
= debug_get_bool_option("SVGA_NO_SWTNL", FALSE
);
163 svga
->debug
.force_swtnl
= debug_get_bool_option("SVGA_FORCE_SWTNL", FALSE
);
164 svga
->debug
.use_min_mipmap
= debug_get_bool_option("SVGA_USE_MIN_MIPMAP", FALSE
);
165 svga
->debug
.disable_shader
= debug_get_num_option("SVGA_DISABLE_SHADER", ~0);
167 if (!svga_init_swtnl(svga
))
170 svga
->upload_ib
= u_upload_create( svga
->pipe
.screen
,
173 PIPE_BUFFER_USAGE_INDEX
);
174 if (svga
->upload_ib
== NULL
)
177 svga
->upload_vb
= u_upload_create( svga
->pipe
.screen
,
180 PIPE_BUFFER_USAGE_VERTEX
);
181 if (svga
->upload_vb
== NULL
)
184 svga
->hwtnl
= svga_hwtnl_create( svga
,
187 if (svga
->hwtnl
== NULL
)
191 ret
= svga_emit_initial_state( svga
);
195 /* Avoid shortcircuiting state with initial value of zero.
197 memset(&svga
->state
.hw_clear
, 0xcd, sizeof(svga
->state
.hw_clear
));
198 memset(&svga
->state
.hw_clear
.framebuffer
, 0x0,
199 sizeof(svga
->state
.hw_clear
.framebuffer
));
201 memset(&svga
->state
.hw_draw
, 0xcd, sizeof(svga
->state
.hw_draw
));
202 memset(&svga
->state
.hw_draw
.views
, 0x0, sizeof(svga
->state
.hw_draw
.views
));
203 svga
->state
.hw_draw
.num_views
= 0;
206 svga
->state
.white_fs_id
= SVGA3D_INVALID_ID
;
208 LIST_INITHEAD(&svga
->dirty_buffers
);
213 svga_hwtnl_destroy( svga
->hwtnl
);
215 u_upload_destroy( svga
->upload_vb
);
217 u_upload_destroy( svga
->upload_ib
);
219 svga_destroy_swtnl(svga
);
221 svga
->swc
->destroy(svga
->swc
);
229 void svga_context_flush( struct svga_context
*svga
,
230 struct pipe_fence_handle
**pfence
)
232 struct svga_screen
*svgascreen
= svga_screen(svga
->pipe
.screen
);
234 /* Unmap upload manager buffers:
236 u_upload_flush(svga
->upload_vb
);
237 u_upload_flush(svga
->upload_ib
);
239 /* Flush screen, to ensure that texture dma uploads are processed
240 * before submitting commands.
242 svga_screen_flush(svgascreen
, NULL
);
244 svga_context_flush_buffers(svga
);
246 /* Flush pending commands to hardware:
248 svga
->swc
->flush(svga
->swc
, pfence
);
250 if (SVGA_DEBUG
& DEBUG_SYNC
) {
251 if (pfence
&& *pfence
)
252 svga
->pipe
.screen
->fence_finish( svga
->pipe
.screen
, *pfence
, 0);
257 void svga_hwtnl_flush_retry( struct svga_context
*svga
)
259 enum pipe_error ret
= PIPE_OK
;
261 ret
= svga_hwtnl_flush( svga
->hwtnl
);
262 if (ret
== PIPE_ERROR_OUT_OF_MEMORY
) {
263 svga_context_flush( svga
, NULL
);
264 ret
= svga_hwtnl_flush( svga
->hwtnl
);