lp_context.c 7.32 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**************************************************************************
 * 
 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 * Copyright 2008 VMware, Inc.  All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 **************************************************************************/

/* Author:
 *    Keith Whitwell <keith@tungstengraphics.com>
 */

#include "draw/draw_context.h"
34
#include "draw/draw_vbuf.h"
35
#include "pipe/p_defines.h"
36
#include "util/u_inlines.h"
37
38
#include "util/u_math.h"
#include "util/u_memory.h"
39
#include "util/u_simple_list.h"
40
41
42
#include "lp_clear.h"
#include "lp_context.h"
#include "lp_flush.h"
43
#include "lp_perf.h"
44
45
46
#include "lp_state.h"
#include "lp_surface.h"
#include "lp_query.h"
47
#include "lp_setup.h"
48

49

Brian Paul's avatar
Brian Paul committed
50
DEBUG_GET_ONCE_BOOL_OPTION(lp_no_rast, "LP_NO_RAST", FALSE)
51
52


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/** shared by all contexts */
unsigned llvmpipe_variant_count;


/**
 * This function is called by the gallivm "garbage collector" when
 * the LLVM global data structures are freed.  We must free all LLVM-related
 * data.  Specifically, all JIT'd shader variants.
 */
static void
garbage_collect_callback(void *cb_data)
{
   struct llvmpipe_context *lp = (struct llvmpipe_context *) cb_data;
   struct lp_fs_variant_list_item *li;

   /* Free all the context's shader variants */
   li = first_elem(&lp->fs_variants_list);
   while (!at_end(&lp->fs_variants_list, li)) {
      struct lp_fs_variant_list_item *next = next_elem(li);
      llvmpipe_remove_shader_variant(lp, li->base);
      li = next;
   }

   /* Free all the context's primitive setup variants */
   lp_delete_setup_variants(lp);

   /* release references to setup variants, shaders */
   lp_setup_set_setup_variant(lp->setup, NULL);
   lp_setup_set_fs_variant(lp->setup, NULL);
   lp_setup_reset(lp->setup);

   /* This type will be recreated upon demand */
   lp->jit_context_ptr_type = NULL;

   /* mark all state as dirty to ensure new shaders are jit'd, etc. */
   lp->dirty = ~0;
}



93
94
95
static void llvmpipe_destroy( struct pipe_context *pipe )
{
   struct llvmpipe_context *llvmpipe = llvmpipe_context( pipe );
96
   uint i, j;
97

98
99
   lp_print_counters();

100
101
102
   gallivm_remove_garbage_collector_callback(garbage_collect_callback,
                                             llvmpipe);

Keith Whitwell's avatar
Keith Whitwell committed
103
104
   /* This will also destroy llvmpipe->setup:
    */
105
106
107
   if (llvmpipe->draw)
      draw_destroy( llvmpipe->draw );

108
109
110
   for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
      pipe_surface_reference(&llvmpipe->framebuffer.cbufs[i], NULL);
   }
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
124
      for (j = 0; j < Elements(llvmpipe->constants[i]); j++) {
         pipe_resource_reference(&llvmpipe->constants[i][j], NULL);
125
126
127
      }
   }

128
129
130
131
   for (i = 0; i < llvmpipe->num_vertex_buffers; i++) {
      pipe_resource_reference(&llvmpipe->vertex_buffer[i].buffer, NULL);
   }

132
   gallivm_destroy(llvmpipe->gallivm);
133

134
   align_free( llvmpipe );
135
136
}

137
138
139
140
static void
do_flush( struct pipe_context *pipe,
          struct pipe_fence_handle **fence)
{
141
   llvmpipe_flush(pipe, fence, __FUNCTION__);
142
143
}

144

145
146
147
148
149
150
151
152
153
154
155
static void
llvmpipe_render_condition ( struct pipe_context *pipe,
                            struct pipe_query *query,
                            uint mode )
{
   struct llvmpipe_context *llvmpipe = llvmpipe_context( pipe );

   llvmpipe->render_cond_query = query;
   llvmpipe->render_cond_mode = mode;
}

156
struct pipe_context *
157
llvmpipe_create_context( struct pipe_screen *screen, void *priv )
158
{
159
   struct llvmpipe_context *llvmpipe;
160

161
162
163
164
   llvmpipe = align_malloc(sizeof(struct llvmpipe_context), 16);
   if (!llvmpipe)
      return NULL;

165
166
   util_init_math();

167
168
   memset(llvmpipe, 0, sizeof *llvmpipe);

169
   make_empty_list(&llvmpipe->fs_variants_list);
170

171
   make_empty_list(&llvmpipe->setup_variants_list);
172

173

174
175
   llvmpipe->pipe.winsys = screen->winsys;
   llvmpipe->pipe.screen = screen;
176
   llvmpipe->pipe.priv = priv;
177

178
179
   /* Init the pipe context methods */
   llvmpipe->pipe.destroy = llvmpipe_destroy;
180
181
   llvmpipe->pipe.set_framebuffer_state = llvmpipe_set_framebuffer_state;
   llvmpipe->pipe.clear = llvmpipe_clear;
182
   llvmpipe->pipe.flush = do_flush;
183

184
185
   llvmpipe->pipe.render_condition = llvmpipe_render_condition;

186
   llvmpipe_init_blend_funcs(llvmpipe);
187
   llvmpipe_init_clip_funcs(llvmpipe);
188
   llvmpipe_init_draw_funcs(llvmpipe);
189
   llvmpipe_init_sampler_funcs(llvmpipe);
190
   llvmpipe_init_query_funcs( llvmpipe );
191
   llvmpipe_init_vertex_funcs(llvmpipe);
192
   llvmpipe_init_so_funcs(llvmpipe);
193
194
   llvmpipe_init_fs_funcs(llvmpipe);
   llvmpipe_init_vs_funcs(llvmpipe);
195
   llvmpipe_init_gs_funcs(llvmpipe);
196
   llvmpipe_init_rasterizer_funcs(llvmpipe);
197
   llvmpipe_init_context_resource_funcs( &llvmpipe->pipe );
198
   llvmpipe_init_surface_functions(llvmpipe);
199

200
201
   llvmpipe->gallivm = gallivm_create();

202
203
204
   /*
    * Create drawing context and plug our rendering stage into it.
    */
205
   llvmpipe->draw = draw_create_gallivm(&llvmpipe->pipe, llvmpipe->gallivm);
206
   if (!llvmpipe->draw)
207
208
      goto fail;

209
   /* FIXME: devise alternative to draw_texture_samplers */
210

211
   if (debug_get_option_lp_no_rast())
212
213
      llvmpipe->no_rast = TRUE;

214
   llvmpipe->setup = lp_setup_create( &llvmpipe->pipe,
215
                                      llvmpipe->draw );
216
   if (!llvmpipe->setup)
217
218
      goto fail;

219
220
221
   /* plug in AA line/point stages */
   draw_install_aaline_stage(llvmpipe->draw, &llvmpipe->pipe);
   draw_install_aapoint_stage(llvmpipe->draw, &llvmpipe->pipe);
222
   draw_install_pstipple_stage(llvmpipe->draw, &llvmpipe->pipe);
223

224
225
226
   /* convert points and lines into triangles: 
    * (otherwise, draw points and lines natively)
    */
227
228
   draw_wide_point_sprites(llvmpipe->draw, FALSE);
   draw_enable_point_sprites(llvmpipe->draw, FALSE);
229
   draw_wide_point_threshold(llvmpipe->draw, 10000.0);
230
   draw_wide_line_threshold(llvmpipe->draw, 10000.0);
231

232
233
   lp_reset_counters();

234
235
236
   gallivm_register_garbage_collector_callback(garbage_collect_callback,
                                               llvmpipe);

237
238
239
240
241
242
243
   return &llvmpipe->pipe;

 fail:
   llvmpipe_destroy(&llvmpipe->pipe);
   return NULL;
}