nvfx: expose GLSL
[mesa/mesa-lb.git] / src / gallium / drivers / svga / svga_context.c
blobadb7840182b07727be6322af15c876494c4af7f4
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
22 * SOFTWARE.
24 **********************************************************/
26 #include "svga_cmd.h"
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"
35 #include "svga_context.h"
36 #include "svga_screen.h"
37 #include "svga_screen_texture.h"
38 #include "svga_screen_buffer.h"
39 #include "svga_winsys.h"
40 #include "svga_swtnl.h"
41 #include "svga_draw.h"
42 #include "svga_debug.h"
43 #include "svga_state.h"
46 static void svga_destroy( struct pipe_context *pipe )
48 struct svga_context *svga = svga_context( pipe );
49 unsigned shader;
51 svga_cleanup_framebuffer( svga );
52 svga_cleanup_tss_binding( svga );
54 svga_hwtnl_destroy( svga->hwtnl );
56 svga_cleanup_vertex_state(svga);
58 svga->swc->destroy(svga->swc);
60 svga_destroy_swtnl( svga );
62 u_upload_destroy( svga->upload_vb );
63 u_upload_destroy( svga->upload_ib );
65 util_bitmask_destroy( svga->vs_bm );
66 util_bitmask_destroy( svga->fs_bm );
68 for(shader = 0; shader < PIPE_SHADER_TYPES; ++shader)
69 pipe_buffer_reference( &svga->curr.cb[shader], NULL );
71 FREE( svga );
74 static unsigned int
75 svga_is_texture_referenced( struct pipe_context *pipe,
76 struct pipe_texture *texture,
77 unsigned face, unsigned level)
79 struct svga_texture *tex = svga_texture(texture);
80 struct svga_screen *ss = svga_screen(pipe->screen);
82 /**
83 * The screen does not cache texture writes.
86 if (!tex->handle || ss->sws->surface_is_flushed(ss->sws, tex->handle))
87 return PIPE_UNREFERENCED;
89 /**
90 * sws->surface_is_flushed() does not distinguish between read references
91 * and write references. So assume a reference is both.
94 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
97 static unsigned int
98 svga_is_buffer_referenced( struct pipe_context *pipe,
99 struct pipe_buffer *buf)
102 struct svga_screen *ss = svga_screen(pipe->screen);
103 struct svga_buffer *sbuf = svga_buffer(buf);
106 * XXX: Check this.
107 * The screen may cache buffer writes, but when we map, we map out
108 * of those cached writes, so we don't need to set a
109 * PIPE_REFERENCED_FOR_WRITE flag for cached buffers.
112 if (!sbuf->handle || ss->sws->surface_is_flushed(ss->sws, sbuf->handle))
113 return PIPE_UNREFERENCED;
116 * sws->surface_is_flushed() does not distinguish between read references
117 * and write references. So assume a reference is both,
118 * however, we make an exception for index- and vertex buffers, to avoid
119 * a flush in st_bufferobj_get_subdata, during display list replay.
122 if (sbuf->base.usage & (PIPE_BUFFER_USAGE_VERTEX | PIPE_BUFFER_USAGE_INDEX))
123 return PIPE_REFERENCED_FOR_READ;
125 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
129 struct pipe_context *svga_context_create( struct pipe_screen *screen,
130 void *priv )
132 struct svga_screen *svgascreen = svga_screen(screen);
133 struct svga_context *svga = NULL;
134 enum pipe_error ret;
136 svga = CALLOC_STRUCT(svga_context);
137 if (svga == NULL)
138 goto no_svga;
140 svga->pipe.winsys = screen->winsys;
141 svga->pipe.screen = screen;
142 svga->pipe.priv = priv;
143 svga->pipe.destroy = svga_destroy;
144 svga->pipe.clear = svga_clear;
146 svga->pipe.is_texture_referenced = svga_is_texture_referenced;
147 svga->pipe.is_buffer_referenced = svga_is_buffer_referenced;
149 svga->swc = svgascreen->sws->context_create(svgascreen->sws);
150 if(!svga->swc)
151 goto no_swc;
153 svga_init_blend_functions(svga);
154 svga_init_blit_functions(svga);
155 svga_init_depth_stencil_functions(svga);
156 svga_init_draw_functions(svga);
157 svga_init_flush_functions(svga);
158 svga_init_misc_functions(svga);
159 svga_init_rasterizer_functions(svga);
160 svga_init_sampler_functions(svga);
161 svga_init_fs_functions(svga);
162 svga_init_vs_functions(svga);
163 svga_init_vertex_functions(svga);
164 svga_init_constbuffer_functions(svga);
165 svga_init_query_functions(svga);
167 svga_init_texture_functions(&svga->pipe);
169 /* debug */
170 svga->debug.no_swtnl = debug_get_bool_option("SVGA_NO_SWTNL", FALSE);
171 svga->debug.force_swtnl = debug_get_bool_option("SVGA_FORCE_SWTNL", FALSE);
172 svga->debug.use_min_mipmap = debug_get_bool_option("SVGA_USE_MIN_MIPMAP", FALSE);
173 svga->debug.disable_shader = debug_get_num_option("SVGA_DISABLE_SHADER", ~0);
175 if (!svga_init_swtnl(svga))
176 goto no_swtnl;
178 svga->fs_bm = util_bitmask_create();
179 if (svga->fs_bm == NULL)
180 goto no_fs_bm;
182 svga->vs_bm = util_bitmask_create();
183 if (svga->vs_bm == NULL)
184 goto no_vs_bm;
186 svga->upload_ib = u_upload_create( svga->pipe.screen,
187 32 * 1024,
189 PIPE_BUFFER_USAGE_INDEX );
190 if (svga->upload_ib == NULL)
191 goto no_upload_ib;
193 svga->upload_vb = u_upload_create( svga->pipe.screen,
194 128 * 1024,
196 PIPE_BUFFER_USAGE_VERTEX );
197 if (svga->upload_vb == NULL)
198 goto no_upload_vb;
200 svga->hwtnl = svga_hwtnl_create( svga,
201 svga->upload_ib,
202 svga->swc );
203 if (svga->hwtnl == NULL)
204 goto no_hwtnl;
207 ret = svga_emit_initial_state( svga );
208 if (ret)
209 goto no_state;
211 /* Avoid shortcircuiting state with initial value of zero.
213 memset(&svga->state.hw_clear, 0xcd, sizeof(svga->state.hw_clear));
214 memset(&svga->state.hw_clear.framebuffer, 0x0,
215 sizeof(svga->state.hw_clear.framebuffer));
217 memset(&svga->state.hw_draw, 0xcd, sizeof(svga->state.hw_draw));
218 memset(&svga->state.hw_draw.views, 0x0, sizeof(svga->state.hw_draw.views));
219 svga->state.hw_draw.num_views = 0;
221 svga->dirty = ~0;
223 LIST_INITHEAD(&svga->dirty_buffers);
225 return &svga->pipe;
227 no_state:
228 svga_hwtnl_destroy( svga->hwtnl );
229 no_hwtnl:
230 u_upload_destroy( svga->upload_vb );
231 no_upload_vb:
232 u_upload_destroy( svga->upload_ib );
233 no_upload_ib:
234 util_bitmask_destroy( svga->vs_bm );
235 no_vs_bm:
236 util_bitmask_destroy( svga->fs_bm );
237 no_fs_bm:
238 svga_destroy_swtnl(svga);
239 no_swtnl:
240 svga->swc->destroy(svga->swc);
241 no_swc:
242 FREE(svga);
243 no_svga:
244 return NULL;
248 void svga_context_flush( struct svga_context *svga,
249 struct pipe_fence_handle **pfence )
251 struct svga_screen *svgascreen = svga_screen(svga->pipe.screen);
253 svga->curr.nr_fbs = 0;
255 /* Unmap upload manager buffers:
257 u_upload_flush(svga->upload_vb);
258 u_upload_flush(svga->upload_ib);
260 /* Flush screen, to ensure that texture dma uploads are processed
261 * before submitting commands.
263 svga_screen_flush(svgascreen, NULL);
265 svga_context_flush_buffers(svga);
267 /* Flush pending commands to hardware:
269 svga->swc->flush(svga->swc, pfence);
271 if (SVGA_DEBUG & DEBUG_SYNC) {
272 if (pfence && *pfence)
273 svga->pipe.screen->fence_finish( svga->pipe.screen, *pfence, 0);
278 void svga_hwtnl_flush_retry( struct svga_context *svga )
280 enum pipe_error ret = PIPE_OK;
282 ret = svga_hwtnl_flush( svga->hwtnl );
283 if (ret == PIPE_ERROR_OUT_OF_MEMORY) {
284 svga_context_flush( svga, NULL );
285 ret = svga_hwtnl_flush( svga->hwtnl );
288 assert(ret == 0);