1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <[email protected]>
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/list.h"
40 #include "util/u_upload_mgr.h"
41 #include "lp_clear.h"
42 #include "lp_context.h"
43 #include "lp_flush.h"
44 #include "lp_perf.h"
45 #include "lp_state.h"
46 #include "lp_surface.h"
47 #include "lp_query.h"
48 #include "lp_setup.h"
49 #include "lp_screen.h"
50 #include "lp_fence.h"
51
52 static void
llvmpipe_destroy(struct pipe_context * pipe)53 llvmpipe_destroy(struct pipe_context *pipe)
54 {
55 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
56 struct llvmpipe_screen *lp_screen = llvmpipe_screen(pipe->screen);
57 uint i;
58
59 mtx_lock(&lp_screen->ctx_mutex);
60 list_del(&llvmpipe->list);
61 mtx_unlock(&lp_screen->ctx_mutex);
62 lp_print_counters();
63
64 if (llvmpipe->csctx) {
65 lp_csctx_destroy(llvmpipe->csctx);
66 }
67 if (llvmpipe->task_ctx) {
68 lp_csctx_destroy(llvmpipe->task_ctx);
69 }
70 if (llvmpipe->mesh_ctx) {
71 lp_csctx_destroy(llvmpipe->mesh_ctx);
72 }
73 if (llvmpipe->blitter) {
74 util_blitter_destroy(llvmpipe->blitter);
75 }
76
77 if (llvmpipe->pipe.stream_uploader)
78 u_upload_destroy(llvmpipe->pipe.stream_uploader);
79
80 /* This will also destroy llvmpipe->setup:
81 */
82 if (llvmpipe->draw)
83 draw_destroy(llvmpipe->draw);
84
85 util_unreference_framebuffer_state(&llvmpipe->framebuffer);
86
87 for (enum pipe_shader_type s = PIPE_SHADER_VERTEX; s < PIPE_SHADER_MESH_TYPES; s++) {
88 for (i = 0; i < ARRAY_SIZE(llvmpipe->sampler_views[0]); i++) {
89 pipe_sampler_view_reference(&llvmpipe->sampler_views[s][i], NULL);
90 }
91 for (i = 0; i < LP_MAX_TGSI_SHADER_IMAGES; i++) {
92 pipe_resource_reference(&llvmpipe->images[s][i].resource, NULL);
93 }
94 for (i = 0; i < LP_MAX_TGSI_SHADER_BUFFERS; i++) {
95 pipe_resource_reference(&llvmpipe->ssbos[s][i].buffer, NULL);
96 }
97 for (i = 0; i < ARRAY_SIZE(llvmpipe->constants[s]); i++) {
98 pipe_resource_reference(&llvmpipe->constants[s][i].buffer, NULL);
99 }
100 }
101
102 for (i = 0; i < llvmpipe->num_vertex_buffers; i++) {
103 pipe_vertex_buffer_unreference(&llvmpipe->vertex_buffer[i]);
104 }
105
106 lp_delete_setup_variants(llvmpipe);
107
108 llvmpipe_sampler_matrix_destroy(llvmpipe);
109
110 lp_context_destroy(&llvmpipe->context);
111
112 align_free(llvmpipe);
113 }
114
115
116 static void
do_flush(struct pipe_context * pipe,struct pipe_fence_handle ** fence,unsigned flags)117 do_flush(struct pipe_context *pipe,
118 struct pipe_fence_handle **fence,
119 unsigned flags)
120 {
121 llvmpipe_flush(pipe, fence, __func__);
122 }
123
124
125 static void
llvmpipe_fence_server_sync(struct pipe_context * pipe,struct pipe_fence_handle * fence)126 llvmpipe_fence_server_sync(struct pipe_context *pipe,
127 struct pipe_fence_handle *fence)
128 {
129 struct lp_fence *f = (struct lp_fence *)fence;
130
131 if (!f->issued)
132 return;
133 lp_fence_wait(f);
134 }
135
136
137 static void
llvmpipe_render_condition(struct pipe_context * pipe,struct pipe_query * query,bool condition,enum pipe_render_cond_flag mode)138 llvmpipe_render_condition(struct pipe_context *pipe,
139 struct pipe_query *query,
140 bool condition,
141 enum pipe_render_cond_flag mode)
142 {
143 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
144
145 llvmpipe->render_cond_query = query;
146 llvmpipe->render_cond_mode = mode;
147 llvmpipe->render_cond_cond = condition;
148 }
149
150
151 static void
llvmpipe_render_condition_mem(struct pipe_context * pipe,struct pipe_resource * buffer,unsigned offset,bool condition)152 llvmpipe_render_condition_mem(struct pipe_context *pipe,
153 struct pipe_resource *buffer,
154 unsigned offset,
155 bool condition)
156 {
157 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
158
159 llvmpipe->render_cond_buffer = llvmpipe_resource(buffer);
160 llvmpipe->render_cond_offset = offset;
161 llvmpipe->render_cond_cond = condition;
162 }
163
164
165 static void
llvmpipe_texture_barrier(struct pipe_context * pipe,unsigned flags)166 llvmpipe_texture_barrier(struct pipe_context *pipe, unsigned flags)
167 {
168 llvmpipe_finish(pipe, "barrier");
169 }
170
171
172 static void
lp_draw_disk_cache_find_shader(void * cookie,struct lp_cached_code * cache,unsigned char ir_sha1_cache_key[20])173 lp_draw_disk_cache_find_shader(void *cookie,
174 struct lp_cached_code *cache,
175 unsigned char ir_sha1_cache_key[20])
176 {
177 struct llvmpipe_screen *screen = cookie;
178 lp_disk_cache_find_shader(screen, cache, ir_sha1_cache_key);
179 }
180
181
182 static void
lp_draw_disk_cache_insert_shader(void * cookie,struct lp_cached_code * cache,unsigned char ir_sha1_cache_key[20])183 lp_draw_disk_cache_insert_shader(void *cookie,
184 struct lp_cached_code *cache,
185 unsigned char ir_sha1_cache_key[20])
186 {
187 struct llvmpipe_screen *screen = cookie;
188 lp_disk_cache_insert_shader(screen, cache, ir_sha1_cache_key);
189 }
190
191
192 static enum pipe_reset_status
llvmpipe_get_device_reset_status(struct pipe_context * pipe)193 llvmpipe_get_device_reset_status(struct pipe_context *pipe)
194 {
195 return PIPE_NO_RESET;
196 }
197
198
199 struct pipe_context *
llvmpipe_create_context(struct pipe_screen * screen,void * priv,unsigned flags)200 llvmpipe_create_context(struct pipe_screen *screen, void *priv,
201 unsigned flags)
202 {
203 struct llvmpipe_context *llvmpipe;
204 struct llvmpipe_screen *lp_screen = llvmpipe_screen(screen);
205
206 if (!llvmpipe_screen_late_init(lp_screen))
207 return NULL;
208
209 llvmpipe = align_malloc(sizeof(struct llvmpipe_context), 16);
210 if (!llvmpipe)
211 return NULL;
212
213 memset(llvmpipe, 0, sizeof *llvmpipe);
214
215 list_inithead(&llvmpipe->fs_variants_list.list);
216
217 list_inithead(&llvmpipe->setup_variants_list.list);
218
219 list_inithead(&llvmpipe->cs_variants_list.list);
220
221 llvmpipe->pipe.screen = screen;
222 llvmpipe->pipe.priv = priv;
223
224 /* Init the pipe context methods */
225 llvmpipe->pipe.destroy = llvmpipe_destroy;
226 llvmpipe->pipe.set_framebuffer_state = llvmpipe_set_framebuffer_state;
227 llvmpipe->pipe.clear = llvmpipe_clear;
228 llvmpipe->pipe.flush = do_flush;
229 llvmpipe->pipe.texture_barrier = llvmpipe_texture_barrier;
230
231 llvmpipe->pipe.render_condition = llvmpipe_render_condition;
232 llvmpipe->pipe.render_condition_mem = llvmpipe_render_condition_mem;
233
234 llvmpipe->pipe.fence_server_sync = llvmpipe_fence_server_sync;
235 llvmpipe->pipe.get_device_reset_status = llvmpipe_get_device_reset_status;
236 llvmpipe_init_blend_funcs(llvmpipe);
237 llvmpipe_init_clip_funcs(llvmpipe);
238 llvmpipe_init_draw_funcs(llvmpipe);
239 llvmpipe_init_compute_funcs(llvmpipe);
240 llvmpipe_init_sampler_funcs(llvmpipe);
241 llvmpipe_init_query_funcs(llvmpipe);
242 llvmpipe_init_vertex_funcs(llvmpipe);
243 llvmpipe_init_so_funcs(llvmpipe);
244 llvmpipe_init_fs_funcs(llvmpipe);
245 llvmpipe_init_vs_funcs(llvmpipe);
246 llvmpipe_init_gs_funcs(llvmpipe);
247 llvmpipe_init_tess_funcs(llvmpipe);
248 llvmpipe_init_task_funcs(llvmpipe);
249 llvmpipe_init_mesh_funcs(llvmpipe);
250 llvmpipe_init_rasterizer_funcs(llvmpipe);
251 llvmpipe_init_context_resource_funcs(&llvmpipe->pipe);
252 llvmpipe_init_surface_functions(llvmpipe);
253
254 llvmpipe_init_sampler_matrix(llvmpipe);
255
256 #ifdef HAVE_LIBDRM
257 llvmpipe_init_fence_funcs(&llvmpipe->pipe);
258 #endif
259
260 #ifdef USE_GLOBAL_LLVM_CONTEXT
261 llvmpipe->context.ref = LLVMGetGlobalContext();
262 llvmpipe->context.owned = false;
263 #if LLVM_VERSION_MAJOR == 15
264 if (llvmpipe->context.ref) {
265 LLVMContextSetOpaquePointers(llvmpipe->context.ref, false);
266 }
267 #endif
268 #else
269 lp_context_create(&llvmpipe->context);
270 #endif
271
272 if (!llvmpipe->context.ref)
273 goto fail;
274
275 /*
276 * Create drawing context and plug our rendering stage into it.
277 */
278 llvmpipe->draw = draw_create_with_llvm_context(&llvmpipe->pipe,
279 &llvmpipe->context);
280 if (!llvmpipe->draw)
281 goto fail;
282
283 draw_set_disk_cache_callbacks(llvmpipe->draw,
284 lp_screen,
285 lp_draw_disk_cache_find_shader,
286 lp_draw_disk_cache_insert_shader);
287
288 draw_set_constant_buffer_stride(llvmpipe->draw,
289 lp_get_constant_buffer_stride(screen));
290
291 /* FIXME: devise alternative to draw_texture_samplers */
292
293 llvmpipe->setup = lp_setup_create(&llvmpipe->pipe, llvmpipe->draw);
294 if (!llvmpipe->setup)
295 goto fail;
296
297 llvmpipe->csctx = lp_csctx_create(&llvmpipe->pipe);
298 if (!llvmpipe->csctx)
299 goto fail;
300
301 llvmpipe->task_ctx = lp_csctx_create(&llvmpipe->pipe);
302 if (!llvmpipe->task_ctx)
303 goto fail;
304
305 llvmpipe->mesh_ctx = lp_csctx_create(&llvmpipe->pipe);
306 if (!llvmpipe->mesh_ctx)
307 goto fail;
308
309 llvmpipe->pipe.stream_uploader = u_upload_create_default(&llvmpipe->pipe);
310 if (!llvmpipe->pipe.stream_uploader)
311 goto fail;
312
313 llvmpipe->pipe.const_uploader = llvmpipe->pipe.stream_uploader;
314
315 llvmpipe->blitter = util_blitter_create(&llvmpipe->pipe);
316 if (!llvmpipe->blitter) {
317 goto fail;
318 }
319
320 /* must be done before installing Draw stages */
321 util_blitter_cache_all_shaders(llvmpipe->blitter);
322
323 /* plug in AA line/point stages */
324 draw_install_aaline_stage(llvmpipe->draw, &llvmpipe->pipe);
325 draw_install_aapoint_stage(llvmpipe->draw, &llvmpipe->pipe, nir_type_bool32);
326 draw_install_pstipple_stage(llvmpipe->draw, &llvmpipe->pipe);
327
328 /* convert points and lines into triangles:
329 * (otherwise, draw points and lines natively)
330 */
331 draw_wide_point_sprites(llvmpipe->draw, false);
332 draw_enable_point_sprites(llvmpipe->draw, false);
333 draw_wide_point_threshold(llvmpipe->draw, 10000.0);
334 draw_wide_line_threshold(llvmpipe->draw, 10000.0);
335
336 /* initial state for clipping - enabled, with no guardband */
337 draw_set_driver_clipping(llvmpipe->draw, false, false, false, true);
338
339 lp_reset_counters();
340
341 /* If llvmpipe_set_scissor_states() is never called, we still need to
342 * make sure that derived scissor state is computed.
343 * See https://bugs.freedesktop.org/show_bug.cgi?id=101709
344 */
345 llvmpipe->dirty |= LP_NEW_SCISSOR;
346
347 mtx_lock(&lp_screen->ctx_mutex);
348 list_addtail(&llvmpipe->list, &lp_screen->ctx_list);
349 mtx_unlock(&lp_screen->ctx_mutex);
350 return &llvmpipe->pipe;
351
352 fail:
353 llvmpipe_destroy(&llvmpipe->pipe);
354 return NULL;
355 }
356