gallivm/llvmpipe: squash merge of the llvm-context branch
[mesa.git] / src / gallium / drivers / llvmpipe / lp_context.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 * Copyright 2008 VMware, Inc. All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 /* Author:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 */
32
33 #include "draw/draw_context.h"
34 #include "draw/draw_vbuf.h"
35 #include "pipe/p_defines.h"
36 #include "util/u_inlines.h"
37 #include "util/u_math.h"
38 #include "util/u_memory.h"
39 #include "util/u_simple_list.h"
40 #include "lp_clear.h"
41 #include "lp_context.h"
42 #include "lp_flush.h"
43 #include "lp_perf.h"
44 #include "lp_state.h"
45 #include "lp_surface.h"
46 #include "lp_query.h"
47 #include "lp_setup.h"
48
49
50 DEBUG_GET_ONCE_BOOL_OPTION(lp_no_rast, "LP_NO_RAST", FALSE)
51
52
53 /** shared by all contexts */
54 unsigned llvmpipe_variant_count;
55
56
57 /**
58 * This function is called by the gallivm "garbage collector" when
59 * the LLVM global data structures are freed. We must free all LLVM-related
60 * data. Specifically, all JIT'd shader variants.
61 */
62 static void
63 garbage_collect_callback(void *cb_data)
64 {
65 struct llvmpipe_context *lp = (struct llvmpipe_context *) cb_data;
66 struct lp_fs_variant_list_item *li;
67
68 /* Free all the context's shader variants */
69 li = first_elem(&lp->fs_variants_list);
70 while (!at_end(&lp->fs_variants_list, li)) {
71 struct lp_fs_variant_list_item *next = next_elem(li);
72 llvmpipe_remove_shader_variant(lp, li->base);
73 li = next;
74 }
75
76 /* Free all the context's primitive setup variants */
77 lp_delete_setup_variants(lp);
78
79 /* release references to setup variants, shaders */
80 lp_setup_set_setup_variant(lp->setup, NULL);
81 lp_setup_set_fs_variant(lp->setup, NULL);
82 lp_setup_reset(lp->setup);
83
84 /* This type will be recreated upon demand */
85 lp->jit_context_ptr_type = NULL;
86
87 /* mark all state as dirty to ensure new shaders are jit'd, etc. */
88 lp->dirty = ~0;
89 }
90
91
92
93 static void llvmpipe_destroy( struct pipe_context *pipe )
94 {
95 struct llvmpipe_context *llvmpipe = llvmpipe_context( pipe );
96 uint i, j;
97
98 lp_print_counters();
99
100 gallivm_remove_garbage_collector_callback(garbage_collect_callback,
101 llvmpipe);
102
103 /* This will also destroy llvmpipe->setup:
104 */
105 if (llvmpipe->draw)
106 draw_destroy( llvmpipe->draw );
107
108 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
109 pipe_surface_reference(&llvmpipe->framebuffer.cbufs[i], NULL);
110 }
111
112 pipe_surface_reference(&llvmpipe->framebuffer.zsbuf, NULL);
113
114 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
115 pipe_sampler_view_reference(&llvmpipe->fragment_sampler_views[i], NULL);
116 }
117
118 for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
119 pipe_sampler_view_reference(&llvmpipe->vertex_sampler_views[i], NULL);
120 }
121
122 for (i = 0; i < Elements(llvmpipe->constants); i++) {
123 for (j = 0; j < Elements(llvmpipe->constants[i]); j++) {
124 pipe_resource_reference(&llvmpipe->constants[i][j], NULL);
125 }
126 }
127
128 gallivm_destroy(llvmpipe->gallivm);
129
130 align_free( llvmpipe );
131 }
132
133 static void
134 do_flush( struct pipe_context *pipe,
135 unsigned flags,
136 struct pipe_fence_handle **fence)
137 {
138 llvmpipe_flush(pipe, flags, fence, __FUNCTION__);
139 }
140
141
142 struct pipe_context *
143 llvmpipe_create_context( struct pipe_screen *screen, void *priv )
144 {
145 struct llvmpipe_context *llvmpipe;
146
147 llvmpipe = align_malloc(sizeof(struct llvmpipe_context), 16);
148 if (!llvmpipe)
149 return NULL;
150
151 util_init_math();
152
153 memset(llvmpipe, 0, sizeof *llvmpipe);
154
155 make_empty_list(&llvmpipe->fs_variants_list);
156
157 make_empty_list(&llvmpipe->setup_variants_list);
158
159
160 llvmpipe->pipe.winsys = screen->winsys;
161 llvmpipe->pipe.screen = screen;
162 llvmpipe->pipe.priv = priv;
163
164 /* Init the pipe context methods */
165 llvmpipe->pipe.destroy = llvmpipe_destroy;
166 llvmpipe->pipe.set_framebuffer_state = llvmpipe_set_framebuffer_state;
167 llvmpipe->pipe.clear = llvmpipe_clear;
168 llvmpipe->pipe.flush = do_flush;
169
170 llvmpipe_init_blend_funcs(llvmpipe);
171 llvmpipe_init_clip_funcs(llvmpipe);
172 llvmpipe_init_draw_funcs(llvmpipe);
173 llvmpipe_init_sampler_funcs(llvmpipe);
174 llvmpipe_init_query_funcs( llvmpipe );
175 llvmpipe_init_vertex_funcs(llvmpipe);
176 llvmpipe_init_so_funcs(llvmpipe);
177 llvmpipe_init_fs_funcs(llvmpipe);
178 llvmpipe_init_vs_funcs(llvmpipe);
179 llvmpipe_init_gs_funcs(llvmpipe);
180 llvmpipe_init_rasterizer_funcs(llvmpipe);
181 llvmpipe_init_context_resource_funcs( &llvmpipe->pipe );
182 llvmpipe_init_surface_functions(llvmpipe);
183
184 llvmpipe->gallivm = gallivm_create();
185
186 /*
187 * Create drawing context and plug our rendering stage into it.
188 */
189 llvmpipe->draw = draw_create_gallivm(&llvmpipe->pipe, llvmpipe->gallivm);
190 if (!llvmpipe->draw)
191 goto fail;
192
193 /* FIXME: devise alternative to draw_texture_samplers */
194
195 if (debug_get_option_lp_no_rast())
196 llvmpipe->no_rast = TRUE;
197
198 llvmpipe->setup = lp_setup_create( &llvmpipe->pipe,
199 llvmpipe->draw );
200 if (!llvmpipe->setup)
201 goto fail;
202
203 /* plug in AA line/point stages */
204 draw_install_aaline_stage(llvmpipe->draw, &llvmpipe->pipe);
205 draw_install_aapoint_stage(llvmpipe->draw, &llvmpipe->pipe);
206 draw_install_pstipple_stage(llvmpipe->draw, &llvmpipe->pipe);
207
208 /* convert points and lines into triangles:
209 * (otherwise, draw points and lines natively)
210 */
211 draw_wide_point_sprites(llvmpipe->draw, FALSE);
212 draw_enable_point_sprites(llvmpipe->draw, FALSE);
213 draw_wide_point_threshold(llvmpipe->draw, 10000.0);
214 draw_wide_line_threshold(llvmpipe->draw, 10000.0);
215
216 #if USE_DRAW_STAGE_PSTIPPLE
217 /* Do polygon stipple w/ texture map + frag prog? */
218 draw_install_pstipple_stage(llvmpipe->draw, &llvmpipe->pipe);
219 #endif
220
221 lp_reset_counters();
222
223 gallivm_register_garbage_collector_callback(garbage_collect_callback,
224 llvmpipe);
225
226 return &llvmpipe->pipe;
227
228 fail:
229 llvmpipe_destroy(&llvmpipe->pipe);
230 return NULL;
231 }
232