blob: 30b0276cfe046484ab4da57d9d4847ca7932e373 [file] [log] [blame]
Brian344356a2008-03-14 16:00:15 -06001/**************************************************************************
2 *
José Fonseca87712852014-01-17 16:27:50 +00003 * Copyright 2007 VMware, Inc.
Brian344356a2008-03-14 16:00:15 -06004 * All Rights Reserved.
Brian Paul3ffd5292008-12-17 18:59:58 -07005 * Copyright 2008 VMware, Inc. All rights reserved.
Brian344356a2008-03-14 16:00:15 -06006 *
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.
José Fonseca87712852014-01-17 16:27:50 +000022 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
Brian344356a2008-03-14 16:00:15 -060023 * 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:
José Fonseca87712852014-01-17 16:27:50 +000030 * Keith Whitwell <keithw@vmware.com>
Brian344356a2008-03-14 16:00:15 -060031 */
32
33#include "draw/draw_context.h"
Keith Whitwell4fe0fc32009-08-23 11:22:41 +010034#include "draw/draw_vbuf.h"
Brian344356a2008-03-14 16:00:15 -060035#include "pipe/p_defines.h"
Brian Paul1a46dcc2008-08-22 15:25:21 -060036#include "util/u_math.h"
Brian Paul4f254202008-08-24 17:48:55 -060037#include "util/u_memory.h"
Brian Paul57aa5972011-07-21 09:55:22 -060038#include "util/u_pstipple.h"
José Fonseca28486882010-02-02 14:42:17 +000039#include "util/u_inlines.h"
Brian Paul1fce9d52010-05-06 15:10:51 -060040#include "tgsi/tgsi_exec.h"
Brian344356a2008-03-14 16:00:15 -060041#include "sp_clear.h"
42#include "sp_context.h"
43#include "sp_flush.h"
Brian344356a2008-03-14 16:00:15 -060044#include "sp_prim_vbuf.h"
45#include "sp_state.h"
46#include "sp_surface.h"
47#include "sp_tile_cache.h"
Keith Whitwell47800c52009-08-23 11:13:20 +010048#include "sp_tex_tile_cache.h"
Keith Whitwellb43c1822010-03-11 15:23:16 +000049#include "sp_texture.h"
Brian344356a2008-03-14 16:00:15 -060050#include "sp_query.h"
Dave Airlie02932f32012-01-05 14:25:04 +000051#include "sp_screen.h"
Roland Scheidegger6b35c2b2013-02-27 19:07:18 +010052#include "sp_tex_sample.h"
Dave Airlieeb9ad9f2016-03-22 07:59:35 +100053#include "sp_image.h"
Brian344356a2008-03-14 16:00:15 -060054
Brian Paulafcaa452009-09-10 16:42:47 -060055static void
56softpipe_destroy( struct pipe_context *pipe )
Brian344356a2008-03-14 16:00:15 -060057{
58 struct softpipe_context *softpipe = softpipe_context( pipe );
Brian Paul0a14e9f2012-08-04 08:46:41 -060059 uint i, sh;
Brian344356a2008-03-14 16:00:15 -060060
Brian Paul57aa5972011-07-21 09:55:22 -060061#if DO_PSTIPPLE_IN_HELPER_MODULE
62 if (softpipe->pstipple.sampler)
63 pipe->delete_sampler_state(pipe, softpipe->pstipple.sampler);
64
65 pipe_resource_reference(&softpipe->pstipple.texture, NULL);
66 pipe_sampler_view_reference(&softpipe->pstipple.sampler_view, NULL);
67#endif
68
Marek Olšák3d9d4b12012-09-13 00:48:58 +020069 if (softpipe->blitter) {
70 util_blitter_destroy(softpipe->blitter);
71 }
72
Keith Whitwellde818832008-05-12 14:10:03 +010073 if (softpipe->draw)
74 draw_destroy( softpipe->draw );
Brian344356a2008-03-14 16:00:15 -060075
Brian Paul37233f12011-01-20 13:46:53 -070076 if (softpipe->quad.shade)
77 softpipe->quad.shade->destroy( softpipe->quad.shade );
78
79 if (softpipe->quad.depth_test)
80 softpipe->quad.depth_test->destroy( softpipe->quad.depth_test );
81
82 if (softpipe->quad.blend)
83 softpipe->quad.blend->destroy( softpipe->quad.blend );
84
85 if (softpipe->quad.pstipple)
86 softpipe->quad.pstipple->destroy( softpipe->quad.pstipple );
Brian344356a2008-03-14 16:00:15 -060087
Younes Mantona7722602009-09-27 10:56:42 -040088 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
Brian344356a2008-03-14 16:00:15 -060089 sp_destroy_tile_cache(softpipe->cbuf_cache[i]);
Younes Mantona7722602009-09-27 10:56:42 -040090 pipe_surface_reference(&softpipe->framebuffer.cbufs[i], NULL);
91 }
Alan Hourihane904469d2009-11-20 18:10:54 +000092
Brian344356a2008-03-14 16:00:15 -060093 sp_destroy_tile_cache(softpipe->zsbuf_cache);
Younes Mantona7722602009-09-27 10:56:42 -040094 pipe_surface_reference(&softpipe->framebuffer.zsbuf, NULL);
Brian344356a2008-03-14 16:00:15 -060095
Brian Paul0a14e9f2012-08-04 08:46:41 -060096 for (sh = 0; sh < Elements(softpipe->tex_cache); sh++) {
97 for (i = 0; i < Elements(softpipe->tex_cache[0]); i++) {
98 sp_destroy_tex_tile_cache(softpipe->tex_cache[sh][i]);
99 pipe_sampler_view_reference(&softpipe->sampler_views[sh][i], NULL);
100 }
101 }
Michal Krol835bab02010-01-19 13:20:15 +0100102
Brian Paul0a14e9f2012-08-04 08:46:41 -0600103 for (sh = 0; sh < Elements(softpipe->constants); sh++) {
104 for (i = 0; i < Elements(softpipe->constants[0]); i++) {
105 if (softpipe->constants[sh][i]) {
106 pipe_resource_reference(&softpipe->constants[sh][i], NULL);
Michal Krol835bab02010-01-19 13:20:15 +0100107 }
Brian344356a2008-03-14 16:00:15 -0600108 }
109 }
110
Marek Olšák3c9aa3a2010-12-26 18:43:39 +0100111 for (i = 0; i < softpipe->num_vertex_buffers; i++) {
112 pipe_resource_reference(&softpipe->vertex_buffer[i].buffer, NULL);
113 }
114
Brian Paul1fce9d52010-05-06 15:10:51 -0600115 tgsi_exec_machine_destroy(softpipe->fs_machine);
116
Roland Scheidegger6b35c2b2013-02-27 19:07:18 +0100117 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
118 FREE(softpipe->tgsi.sampler[i]);
119 }
120
Brian344356a2008-03-14 16:00:15 -0600121 FREE( softpipe );
122}
123
Brian Paulafcaa452009-09-10 16:42:47 -0600124
125/**
126 * if (the texture is being used as a framebuffer surface)
Marek Olšák25485f42011-03-07 22:35:49 +0100127 * return SP_REFERENCED_FOR_WRITE
Brian Paulafcaa452009-09-10 16:42:47 -0600128 * else if (the texture is a bound texture source)
Marek Olšák25485f42011-03-07 22:35:49 +0100129 * return SP_REFERENCED_FOR_READ
Brian Paulafcaa452009-09-10 16:42:47 -0600130 * else
Marek Olšák25485f42011-03-07 22:35:49 +0100131 * return SP_UNREFERENCED
Brian Paulafcaa452009-09-10 16:42:47 -0600132 */
Marek Olšák25485f42011-03-07 22:35:49 +0100133unsigned int
Keith Whitwell287c94e2010-04-10 16:05:54 +0100134softpipe_is_resource_referenced( struct pipe_context *pipe,
Roland Scheidegger4c700142010-12-02 04:33:43 +0100135 struct pipe_resource *texture,
136 unsigned level, int layer)
Thomas Hellstrom9b756272009-04-15 15:53:34 +0200137{
José Fonseca5d0cf9e2009-06-15 18:42:13 +0100138 struct softpipe_context *softpipe = softpipe_context( pipe );
Brian Paul0a14e9f2012-08-04 08:46:41 -0600139 unsigned i, sh;
José Fonseca5d0cf9e2009-06-15 18:42:13 +0100140
Keith Whitwell287c94e2010-04-10 16:05:54 +0100141 if (texture->target == PIPE_BUFFER)
Marek Olšák25485f42011-03-07 22:35:49 +0100142 return SP_UNREFERENCED;
Roland Scheidegger4c700142010-12-02 04:33:43 +0100143
Brian Paul367cfca2009-11-19 11:37:50 -0700144 /* check if any of the bound drawing surfaces are this texture */
Brian Paulafcaa452009-09-10 16:42:47 -0600145 if (softpipe->dirty_render_cache) {
José Fonseca5d0cf9e2009-06-15 18:42:13 +0100146 for (i = 0; i < softpipe->framebuffer.nr_cbufs; i++) {
Brian Paulafcaa452009-09-10 16:42:47 -0600147 if (softpipe->framebuffer.cbufs[i] &&
148 softpipe->framebuffer.cbufs[i]->texture == texture) {
Marek Olšák25485f42011-03-07 22:35:49 +0100149 return SP_REFERENCED_FOR_WRITE;
Brian Paulafcaa452009-09-10 16:42:47 -0600150 }
José Fonseca5d0cf9e2009-06-15 18:42:13 +0100151 }
Brian Paulafcaa452009-09-10 16:42:47 -0600152 if (softpipe->framebuffer.zsbuf &&
153 softpipe->framebuffer.zsbuf->texture == texture) {
Marek Olšák25485f42011-03-07 22:35:49 +0100154 return SP_REFERENCED_FOR_WRITE;
Brian Paulafcaa452009-09-10 16:42:47 -0600155 }
José Fonseca5d0cf9e2009-06-15 18:42:13 +0100156 }
157
Brian Paul367cfca2009-11-19 11:37:50 -0700158 /* check if any of the tex_cache textures are this texture */
Brian Paul0a14e9f2012-08-04 08:46:41 -0600159 for (sh = 0; sh < Elements(softpipe->tex_cache); sh++) {
160 for (i = 0; i < Elements(softpipe->tex_cache[0]); i++) {
161 if (softpipe->tex_cache[sh][i] &&
162 softpipe->tex_cache[sh][i]->texture == texture)
163 return SP_REFERENCED_FOR_READ;
164 }
Zack Rusin53bd9792010-06-11 13:31:52 -0400165 }
166
Marek Olšák25485f42011-03-07 22:35:49 +0100167 return SP_UNREFERENCED;
Thomas Hellstrom9b756272009-04-15 15:53:34 +0200168}
169
Brian Paulafcaa452009-09-10 16:42:47 -0600170
Brian344356a2008-03-14 16:00:15 -0600171
Brian Paulafcaa452009-09-10 16:42:47 -0600172
Brian Paul41450b02009-12-31 14:46:15 -0700173static void
174softpipe_render_condition( struct pipe_context *pipe,
175 struct pipe_query *query,
Roland Scheidegger793e8e32013-06-14 19:48:57 +0200176 boolean condition,
Brian Paul41450b02009-12-31 14:46:15 -0700177 uint mode )
178{
179 struct softpipe_context *softpipe = softpipe_context( pipe );
180
181 softpipe->render_cond_query = query;
182 softpipe->render_cond_mode = mode;
Roland Scheidegger793e8e32013-06-14 19:48:57 +0200183 softpipe->render_cond_cond = condition;
Brian Paul41450b02009-12-31 14:46:15 -0700184}
185
186
187
Brian344356a2008-03-14 16:00:15 -0600188struct pipe_context *
Marek Olšák0fc21ec2015-07-25 18:40:59 +0200189softpipe_create_context(struct pipe_screen *screen,
190 void *priv, unsigned flags)
Brian344356a2008-03-14 16:00:15 -0600191{
Dave Airlie02932f32012-01-05 14:25:04 +0000192 struct softpipe_screen *sp_screen = softpipe_screen(screen);
Brian344356a2008-03-14 16:00:15 -0600193 struct softpipe_context *softpipe = CALLOC_STRUCT(softpipe_context);
Brian Paul0a14e9f2012-08-04 08:46:41 -0600194 uint i, sh;
Brian344356a2008-03-14 16:00:15 -0600195
Brian Paul1a46dcc2008-08-22 15:25:21 -0600196 util_init_math();
197
Roland Scheidegger6b35c2b2013-02-27 19:07:18 +0100198 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
199 softpipe->tgsi.sampler[i] = sp_create_tgsi_sampler();
200 }
201
Dave Airlieeb9ad9f2016-03-22 07:59:35 +1000202 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
203 softpipe->tgsi.image[i] = sp_create_tgsi_image();
204 }
205
Brian Paulb1d48572011-02-17 07:23:40 -0700206 softpipe->dump_fs = debug_get_bool_option( "SOFTPIPE_DUMP_FS", FALSE );
Zack Rusinac96ee12009-12-23 13:30:12 -0500207 softpipe->dump_gs = debug_get_bool_option( "SOFTPIPE_DUMP_GS", FALSE );
Brian344356a2008-03-14 16:00:15 -0600208
Brian344356a2008-03-14 16:00:15 -0600209 softpipe->pipe.screen = screen;
210 softpipe->pipe.destroy = softpipe_destroy;
Keith Whitwell7f41f542010-02-08 12:55:59 +0000211 softpipe->pipe.priv = priv;
Brian344356a2008-03-14 16:00:15 -0600212
213 /* state setters */
Brian Paul27396922010-09-25 13:58:41 -0600214 softpipe_init_blend_funcs(&softpipe->pipe);
Brian Paul63a5b7d2010-09-25 14:15:11 -0600215 softpipe_init_clip_funcs(&softpipe->pipe);
Brian Paul1e35f642010-09-25 14:19:18 -0600216 softpipe_init_query_funcs( softpipe );
Brian Paulbd13a0d2010-09-25 14:08:49 -0600217 softpipe_init_rasterizer_funcs(&softpipe->pipe);
Brian Paulc5dd2e42010-09-25 14:02:38 -0600218 softpipe_init_sampler_funcs(&softpipe->pipe);
Brian Paul279b3682010-09-25 13:54:24 -0600219 softpipe_init_shader_funcs(&softpipe->pipe);
Brian Paul5b2406c2010-09-25 14:12:12 -0600220 softpipe_init_streamout_funcs(&softpipe->pipe);
Brian Paul1e35f642010-09-25 14:19:18 -0600221 softpipe_init_texture_funcs( &softpipe->pipe );
Brian Pauleed45092010-09-25 14:06:58 -0600222 softpipe_init_vertex_funcs(&softpipe->pipe);
Dave Airlieeb9ad9f2016-03-22 07:59:35 +1000223 softpipe_init_image_funcs(&softpipe->pipe);
Roland Scheidegger057427d2010-03-01 18:46:29 +0100224
Brian344356a2008-03-14 16:00:15 -0600225 softpipe->pipe.set_framebuffer_state = softpipe_set_framebuffer_state;
Brian344356a2008-03-14 16:00:15 -0600226
Chia-I Wu6d28bf92010-07-16 04:35:58 +0800227 softpipe->pipe.draw_vbo = softpipe_draw_vbo;
Brian344356a2008-03-14 16:00:15 -0600228
229 softpipe->pipe.clear = softpipe_clear;
Marek Olšák7e023032011-03-08 00:57:48 +0100230 softpipe->pipe.flush = softpipe_flush_wrapped;
Dave Airlieeb9ad9f2016-03-22 07:59:35 +1000231 softpipe->pipe.texture_barrier = softpipe_texture_barrier;
232 softpipe->pipe.memory_barrier = softpipe_memory_barrier;
Brian Paul41450b02009-12-31 14:46:15 -0700233 softpipe->pipe.render_condition = softpipe_render_condition;
Christian König4e837f52011-07-08 16:56:11 +0200234
Brian344356a2008-03-14 16:00:15 -0600235 /*
236 * Alloc caches for accessing drawing surfaces and textures.
237 * Must be before quad stage setup!
238 */
239 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++)
Keith Whitwellb43c1822010-03-11 15:23:16 +0000240 softpipe->cbuf_cache[i] = sp_create_tile_cache( &softpipe->pipe );
241 softpipe->zsbuf_cache = sp_create_tile_cache( &softpipe->pipe );
Brian344356a2008-03-14 16:00:15 -0600242
Brian Paul0a14e9f2012-08-04 08:46:41 -0600243 /* Allocate texture caches */
244 for (sh = 0; sh < Elements(softpipe->tex_cache); sh++) {
245 for (i = 0; i < Elements(softpipe->tex_cache[0]); i++) {
246 softpipe->tex_cache[sh][i] = sp_create_tex_tile_cache(&softpipe->pipe);
247 if (!softpipe->tex_cache[sh][i])
248 goto fail;
249 }
Zack Rusin53bd9792010-06-11 13:31:52 -0400250 }
Brian344356a2008-03-14 16:00:15 -0600251
Brian Paul1fce9d52010-05-06 15:10:51 -0600252 softpipe->fs_machine = tgsi_exec_machine_create();
253
Brian344356a2008-03-14 16:00:15 -0600254 /* setup quad rendering stages */
Brian Paul3f401662009-11-19 14:02:06 -0700255 softpipe->quad.shade = sp_quad_shade_stage(softpipe);
256 softpipe->quad.depth_test = sp_quad_depth_test_stage(softpipe);
257 softpipe->quad.blend = sp_quad_blend_stage(softpipe);
Brian Paul44519e12010-04-17 11:53:35 -0600258 softpipe->quad.pstipple = sp_quad_polygon_stipple_stage(softpipe);
Brian344356a2008-03-14 16:00:15 -0600259
Brianb9604fe2008-12-02 22:40:08 -0700260
Brian344356a2008-03-14 16:00:15 -0600261 /*
262 * Create drawing context and plug our rendering stage into it.
263 */
Dave Airlie5840ec22012-01-11 10:16:56 +0000264 if (sp_screen->use_llvm)
Jakob Bornecrantz8783d422012-01-09 00:00:56 +0100265 softpipe->draw = draw_create(&softpipe->pipe);
266 else
267 softpipe->draw = draw_create_no_llvm(&softpipe->pipe);
Keith Whitwellde818832008-05-12 14:10:03 +0100268 if (!softpipe->draw)
269 goto fail;
270
Roland Scheidegger6b35c2b2013-02-27 19:07:18 +0100271 draw_texture_sampler(softpipe->draw,
272 PIPE_SHADER_VERTEX,
273 (struct tgsi_sampler *)
274 softpipe->tgsi.sampler[PIPE_SHADER_VERTEX]);
Brianb9604fe2008-12-02 22:40:08 -0700275
Roland Scheidegger6b35c2b2013-02-27 19:07:18 +0100276 draw_texture_sampler(softpipe->draw,
277 PIPE_SHADER_GEOMETRY,
278 (struct tgsi_sampler *)
279 softpipe->tgsi.sampler[PIPE_SHADER_GEOMETRY]);
Zack Rusin53bd9792010-06-11 13:31:52 -0400280
Dave Airlieeb9ad9f2016-03-22 07:59:35 +1000281 draw_image(softpipe->draw,
282 PIPE_SHADER_VERTEX,
283 (struct tgsi_image *)
284 softpipe->tgsi.image[PIPE_SHADER_VERTEX]);
285
286 draw_image(softpipe->draw,
287 PIPE_SHADER_GEOMETRY,
288 (struct tgsi_image *)
289 softpipe->tgsi.image[PIPE_SHADER_GEOMETRY]);
290
Brian Paulb1d48572011-02-17 07:23:40 -0700291 if (debug_get_bool_option( "SOFTPIPE_NO_RAST", FALSE ))
Keith Whitwella5b87f22008-04-22 11:01:41 +0100292 softpipe->no_rast = TRUE;
293
Keith Whitwell4fe0fc32009-08-23 11:22:41 +0100294 softpipe->vbuf_backend = sp_create_vbuf_backend(softpipe);
295 if (!softpipe->vbuf_backend)
296 goto fail;
297
298 softpipe->vbuf = draw_vbuf_stage(softpipe->draw, softpipe->vbuf_backend);
299 if (!softpipe->vbuf)
300 goto fail;
301
302 draw_set_rasterize_stage(softpipe->draw, softpipe->vbuf);
303 draw_set_render(softpipe->draw, softpipe->vbuf_backend);
304
Marek Olšák3d9d4b12012-09-13 00:48:58 +0200305 softpipe->blitter = util_blitter_create(&softpipe->pipe);
306 if (!softpipe->blitter) {
307 goto fail;
308 }
309
310 /* must be done before installing Draw stages */
311 util_blitter_cache_all_shaders(softpipe->blitter);
Keith Whitwell4fe0fc32009-08-23 11:22:41 +0100312
Brian344356a2008-03-14 16:00:15 -0600313 /* plug in AA line/point stages */
314 draw_install_aaline_stage(softpipe->draw, &softpipe->pipe);
315 draw_install_aapoint_stage(softpipe->draw, &softpipe->pipe);
316
Brian344356a2008-03-14 16:00:15 -0600317 /* Do polygon stipple w/ texture map + frag prog? */
Brian Paul44519e12010-04-17 11:53:35 -0600318#if DO_PSTIPPLE_IN_DRAW_MODULE
Brian344356a2008-03-14 16:00:15 -0600319 draw_install_pstipple_stage(softpipe->draw, &softpipe->pipe);
Brian Paul44519e12010-04-17 11:53:35 -0600320#endif
Brian344356a2008-03-14 16:00:15 -0600321
Brian Paul01c12ed2010-04-21 14:23:42 -0600322 draw_wide_point_sprites(softpipe->draw, TRUE);
323
Brian344356a2008-03-14 16:00:15 -0600324 sp_init_surface_functions(softpipe);
325
Brian Paul57aa5972011-07-21 09:55:22 -0600326#if DO_PSTIPPLE_IN_HELPER_MODULE
327 /* create the polgon stipple sampler */
328 softpipe->pstipple.sampler = util_pstipple_create_sampler(&softpipe->pipe);
329#endif
330
Brian344356a2008-03-14 16:00:15 -0600331 return &softpipe->pipe;
Keith Whitwellde818832008-05-12 14:10:03 +0100332
333 fail:
334 softpipe_destroy(&softpipe->pipe);
335 return NULL;
Brian344356a2008-03-14 16:00:15 -0600336}