draw: free vertex info from geometry streams.
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_pstipple.c
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * Polygon stipple stage: implement polygon stipple with texture map and
30 * fragment program. The fragment program samples the texture using the
31 * fragment window coordinate register and does a fragment kill for the
32 * stipple-failing fragments.
33 *
34 * Authors: Brian Paul
35 */
36
37
38 #include "pipe/p_context.h"
39 #include "pipe/p_defines.h"
40 #include "pipe/p_shader_tokens.h"
41 #include "util/u_inlines.h"
42
43 #include "util/format/u_format.h"
44 #include "util/u_math.h"
45 #include "util/u_memory.h"
46 #include "util/u_pstipple.h"
47 #include "util/u_sampler.h"
48
49 #include "tgsi/tgsi_transform.h"
50
51 #include "draw_context.h"
52 #include "draw_pipe.h"
53
54 #include "nir.h"
55 #include "nir/nir_draw_helpers.h"
56
57 /** Approx number of new tokens for instructions in pstip_transform_inst() */
58 #define NUM_NEW_TOKENS 53
59
60
61 /**
62 * Subclass of pipe_shader_state to carry extra fragment shader info.
63 */
64 struct pstip_fragment_shader
65 {
66 struct pipe_shader_state state;
67 void *driver_fs;
68 void *pstip_fs;
69 uint sampler_unit;
70 };
71
72
73 /**
74 * Subclass of draw_stage
75 */
76 struct pstip_stage
77 {
78 struct draw_stage stage;
79
80 void *sampler_cso;
81 struct pipe_resource *texture;
82 struct pipe_sampler_view *sampler_view;
83 uint num_samplers;
84 uint num_sampler_views;
85
86 /*
87 * Currently bound state
88 */
89 struct pstip_fragment_shader *fs;
90 struct {
91 void *samplers[PIPE_MAX_SAMPLERS];
92 struct pipe_sampler_view *sampler_views[PIPE_MAX_SHADER_SAMPLER_VIEWS];
93 const struct pipe_poly_stipple *stipple;
94 } state;
95
96 /*
97 * Driver interface/override functions
98 */
99 void * (*driver_create_fs_state)(struct pipe_context *,
100 const struct pipe_shader_state *);
101 void (*driver_bind_fs_state)(struct pipe_context *, void *);
102 void (*driver_delete_fs_state)(struct pipe_context *, void *);
103
104 void (*driver_bind_sampler_states)(struct pipe_context *,
105 enum pipe_shader_type,
106 unsigned, unsigned, void **);
107
108 void (*driver_set_sampler_views)(struct pipe_context *,
109 enum pipe_shader_type shader,
110 unsigned start, unsigned count,
111 struct pipe_sampler_view **);
112
113 void (*driver_set_polygon_stipple)(struct pipe_context *,
114 const struct pipe_poly_stipple *);
115
116 struct pipe_context *pipe;
117 };
118
119
120 /**
121 * Generate the frag shader we'll use for doing polygon stipple.
122 * This will be the user's shader prefixed with a TEX and KIL instruction.
123 */
124 static boolean
125 generate_pstip_fs(struct pstip_stage *pstip)
126 {
127 struct pipe_context *pipe = pstip->pipe;
128 struct pipe_screen *screen = pipe->screen;
129 const struct pipe_shader_state *orig_fs = &pstip->fs->state;
130 /*struct draw_context *draw = pstip->stage.draw;*/
131 struct pipe_shader_state pstip_fs;
132 enum tgsi_file_type wincoord_file;
133
134 wincoord_file = screen->get_param(screen, PIPE_CAP_TGSI_FS_POSITION_IS_SYSVAL) ?
135 TGSI_FILE_SYSTEM_VALUE : TGSI_FILE_INPUT;
136
137 pstip_fs = *orig_fs; /* copy to init */
138 if (orig_fs->type == PIPE_SHADER_IR_TGSI) {
139 pstip_fs.tokens = util_pstipple_create_fragment_shader(orig_fs->tokens,
140 &pstip->fs->sampler_unit,
141 0,
142 wincoord_file);
143 if (pstip_fs.tokens == NULL)
144 return FALSE;
145 } else {
146 #ifdef LLVM_AVAILABLE
147 pstip_fs.ir.nir = nir_shader_clone(NULL, orig_fs->ir.nir);
148 nir_lower_pstipple_fs(pstip_fs.ir.nir,
149 &pstip->fs->sampler_unit, 0, wincoord_file == TGSI_FILE_SYSTEM_VALUE);
150 #endif
151 }
152
153 assert(pstip->fs->sampler_unit < PIPE_MAX_SAMPLERS);
154
155 pstip->fs->pstip_fs = pstip->driver_create_fs_state(pipe, &pstip_fs);
156
157 FREE((void *)pstip_fs.tokens);
158
159 if (!pstip->fs->pstip_fs)
160 return FALSE;
161
162 return TRUE;
163 }
164
165
166 /**
167 * When we're about to draw our first stipple polygon in a batch, this function
168 * is called to tell the driver to bind our modified fragment shader.
169 */
170 static boolean
171 bind_pstip_fragment_shader(struct pstip_stage *pstip)
172 {
173 struct draw_context *draw = pstip->stage.draw;
174 if (!pstip->fs->pstip_fs &&
175 !generate_pstip_fs(pstip))
176 return FALSE;
177
178 draw->suspend_flushing = TRUE;
179 pstip->driver_bind_fs_state(pstip->pipe, pstip->fs->pstip_fs);
180 draw->suspend_flushing = FALSE;
181 return TRUE;
182 }
183
184
185 static inline struct pstip_stage *
186 pstip_stage( struct draw_stage *stage )
187 {
188 return (struct pstip_stage *) stage;
189 }
190
191
192 static void
193 pstip_first_tri(struct draw_stage *stage, struct prim_header *header)
194 {
195 struct pstip_stage *pstip = pstip_stage(stage);
196 struct pipe_context *pipe = pstip->pipe;
197 struct draw_context *draw = stage->draw;
198 uint num_samplers;
199 uint num_sampler_views;
200
201 assert(stage->draw->rasterizer->poly_stipple_enable);
202
203 /* bind our fragprog */
204 if (!bind_pstip_fragment_shader(pstip)) {
205 stage->tri = draw_pipe_passthrough_tri;
206 stage->tri(stage, header);
207 return;
208 }
209
210 /* how many samplers? */
211 /* we'll use sampler/texture[pstip->sampler_unit] for the stipple */
212 num_samplers = MAX2(pstip->num_samplers, pstip->fs->sampler_unit + 1);
213 num_sampler_views = MAX2(pstip->num_sampler_views, num_samplers);
214
215 /* plug in our sampler, texture */
216 pstip->state.samplers[pstip->fs->sampler_unit] = pstip->sampler_cso;
217 pipe_sampler_view_reference(&pstip->state.sampler_views[pstip->fs->sampler_unit],
218 pstip->sampler_view);
219
220 assert(num_samplers <= PIPE_MAX_SAMPLERS);
221
222 draw->suspend_flushing = TRUE;
223
224 pstip->driver_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
225 num_samplers, pstip->state.samplers);
226
227 pstip->driver_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,
228 num_sampler_views, pstip->state.sampler_views);
229
230 draw->suspend_flushing = FALSE;
231
232 /* now really draw first triangle */
233 stage->tri = draw_pipe_passthrough_tri;
234 stage->tri(stage, header);
235 }
236
237
238 static void
239 pstip_flush(struct draw_stage *stage, unsigned flags)
240 {
241 struct draw_context *draw = stage->draw;
242 struct pstip_stage *pstip = pstip_stage(stage);
243 struct pipe_context *pipe = pstip->pipe;
244
245 stage->tri = pstip_first_tri;
246 stage->next->flush( stage->next, flags );
247
248 /* restore original frag shader, texture, sampler state */
249 draw->suspend_flushing = TRUE;
250 pstip->driver_bind_fs_state(pipe, pstip->fs ? pstip->fs->driver_fs : NULL);
251
252 pstip->driver_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
253 pstip->num_samplers,
254 pstip->state.samplers);
255
256 pstip->driver_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,
257 pstip->num_sampler_views,
258 pstip->state.sampler_views);
259
260 draw->suspend_flushing = FALSE;
261 }
262
263
264 static void
265 pstip_reset_stipple_counter(struct draw_stage *stage)
266 {
267 stage->next->reset_stipple_counter( stage->next );
268 }
269
270
271 static void
272 pstip_destroy(struct draw_stage *stage)
273 {
274 struct pstip_stage *pstip = pstip_stage(stage);
275 uint i;
276
277 for (i = 0; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; i++) {
278 pipe_sampler_view_reference(&pstip->state.sampler_views[i], NULL);
279 }
280
281 pstip->pipe->delete_sampler_state(pstip->pipe, pstip->sampler_cso);
282
283 pipe_resource_reference(&pstip->texture, NULL);
284
285 if (pstip->sampler_view) {
286 pipe_sampler_view_reference(&pstip->sampler_view, NULL);
287 }
288
289 draw_free_temp_verts( stage );
290 FREE( stage );
291 }
292
293
294 /** Create a new polygon stipple drawing stage object */
295 static struct pstip_stage *
296 draw_pstip_stage(struct draw_context *draw, struct pipe_context *pipe)
297 {
298 struct pstip_stage *pstip = CALLOC_STRUCT(pstip_stage);
299 if (!pstip)
300 goto fail;
301
302 pstip->pipe = pipe;
303
304 pstip->stage.draw = draw;
305 pstip->stage.name = "pstip";
306 pstip->stage.next = NULL;
307 pstip->stage.point = draw_pipe_passthrough_point;
308 pstip->stage.line = draw_pipe_passthrough_line;
309 pstip->stage.tri = pstip_first_tri;
310 pstip->stage.flush = pstip_flush;
311 pstip->stage.reset_stipple_counter = pstip_reset_stipple_counter;
312 pstip->stage.destroy = pstip_destroy;
313
314 if (!draw_alloc_temp_verts( &pstip->stage, 8 ))
315 goto fail;
316
317 return pstip;
318
319 fail:
320 if (pstip)
321 pstip->stage.destroy( &pstip->stage );
322
323 return NULL;
324 }
325
326
327 static struct pstip_stage *
328 pstip_stage_from_pipe(struct pipe_context *pipe)
329 {
330 struct draw_context *draw = (struct draw_context *) pipe->draw;
331 return pstip_stage(draw->pipeline.pstipple);
332 }
333
334
335 /**
336 * This function overrides the driver's create_fs_state() function and
337 * will typically be called by the gallium frontend.
338 */
339 static void *
340 pstip_create_fs_state(struct pipe_context *pipe,
341 const struct pipe_shader_state *fs)
342 {
343 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
344 struct pstip_fragment_shader *pstipfs = CALLOC_STRUCT(pstip_fragment_shader);
345
346 if (pstipfs) {
347 pstipfs->state.type = fs->type;
348 if (fs->type == PIPE_SHADER_IR_TGSI)
349 pstipfs->state.tokens = tgsi_dup_tokens(fs->tokens);
350 else
351 pstipfs->state.ir.nir = nir_shader_clone(NULL, fs->ir.nir);
352
353 /* pass-through */
354 pstipfs->driver_fs = pstip->driver_create_fs_state(pstip->pipe, fs);
355 }
356
357 return pstipfs;
358 }
359
360
361 static void
362 pstip_bind_fs_state(struct pipe_context *pipe, void *fs)
363 {
364 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
365 struct pstip_fragment_shader *pstipfs = (struct pstip_fragment_shader *) fs;
366 /* save current */
367 pstip->fs = pstipfs;
368 /* pass-through */
369 pstip->driver_bind_fs_state(pstip->pipe,
370 (pstipfs ? pstipfs->driver_fs : NULL));
371 }
372
373
374 static void
375 pstip_delete_fs_state(struct pipe_context *pipe, void *fs)
376 {
377 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
378 struct pstip_fragment_shader *pstipfs = (struct pstip_fragment_shader *) fs;
379 /* pass-through */
380 pstip->driver_delete_fs_state(pstip->pipe, pstipfs->driver_fs);
381
382 if (pstipfs->pstip_fs)
383 pstip->driver_delete_fs_state(pstip->pipe, pstipfs->pstip_fs);
384
385 if (pstipfs->state.type == PIPE_SHADER_IR_TGSI)
386 FREE((void*)pstipfs->state.tokens);
387 else
388 ralloc_free(pstipfs->state.ir.nir);
389 FREE(pstipfs);
390 }
391
392
393 static void
394 pstip_bind_sampler_states(struct pipe_context *pipe,
395 enum pipe_shader_type shader,
396 unsigned start, unsigned num, void **sampler)
397 {
398 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
399 uint i;
400
401 assert(start == 0);
402
403 if (shader == PIPE_SHADER_FRAGMENT) {
404 /* save current */
405 memcpy(pstip->state.samplers, sampler, num * sizeof(void *));
406 for (i = num; i < PIPE_MAX_SAMPLERS; i++) {
407 pstip->state.samplers[i] = NULL;
408 }
409 pstip->num_samplers = num;
410 }
411
412 /* pass-through */
413 pstip->driver_bind_sampler_states(pstip->pipe, shader, start, num, sampler);
414 }
415
416
417 static void
418 pstip_set_sampler_views(struct pipe_context *pipe,
419 enum pipe_shader_type shader,
420 unsigned start, unsigned num,
421 struct pipe_sampler_view **views)
422 {
423 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
424 uint i;
425
426 if (shader == PIPE_SHADER_FRAGMENT) {
427 /* save current */
428 for (i = 0; i < num; i++) {
429 pipe_sampler_view_reference(&pstip->state.sampler_views[start + i],
430 views[i]);
431 }
432 pstip->num_sampler_views = num;
433 }
434
435 /* pass-through */
436 pstip->driver_set_sampler_views(pstip->pipe, shader, start, num, views);
437 }
438
439
440 static void
441 pstip_set_polygon_stipple(struct pipe_context *pipe,
442 const struct pipe_poly_stipple *stipple)
443 {
444 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
445
446 /* save current */
447 pstip->state.stipple = stipple;
448
449 /* pass-through */
450 pstip->driver_set_polygon_stipple(pstip->pipe, stipple);
451
452 util_pstipple_update_stipple_texture(pstip->pipe, pstip->texture,
453 pstip->state.stipple->stipple);
454 }
455
456
457 /**
458 * Called by drivers that want to install this polygon stipple stage
459 * into the draw module's pipeline. This will not be used if the
460 * hardware has native support for polygon stipple.
461 */
462 boolean
463 draw_install_pstipple_stage(struct draw_context *draw,
464 struct pipe_context *pipe)
465 {
466 struct pstip_stage *pstip;
467
468 pipe->draw = (void *) draw;
469
470 /*
471 * Create / install pgon stipple drawing / prim stage
472 */
473 pstip = draw_pstip_stage( draw, pipe );
474 if (!pstip)
475 goto fail;
476
477 draw->pipeline.pstipple = &pstip->stage;
478
479 /* save original driver functions */
480 pstip->driver_create_fs_state = pipe->create_fs_state;
481 pstip->driver_bind_fs_state = pipe->bind_fs_state;
482 pstip->driver_delete_fs_state = pipe->delete_fs_state;
483
484 pstip->driver_bind_sampler_states = pipe->bind_sampler_states;
485 pstip->driver_set_sampler_views = pipe->set_sampler_views;
486 pstip->driver_set_polygon_stipple = pipe->set_polygon_stipple;
487
488 /* create special texture, sampler state */
489 pstip->texture = util_pstipple_create_stipple_texture(pipe, NULL);
490 if (!pstip->texture)
491 goto fail;
492
493 pstip->sampler_view = util_pstipple_create_sampler_view(pipe,
494 pstip->texture);
495 if (!pstip->sampler_view)
496 goto fail;
497
498 pstip->sampler_cso = util_pstipple_create_sampler(pipe);
499 if (!pstip->sampler_cso)
500 goto fail;
501
502 /* override the driver's functions */
503 pipe->create_fs_state = pstip_create_fs_state;
504 pipe->bind_fs_state = pstip_bind_fs_state;
505 pipe->delete_fs_state = pstip_delete_fs_state;
506
507 pipe->bind_sampler_states = pstip_bind_sampler_states;
508 pipe->set_sampler_views = pstip_set_sampler_views;
509 pipe->set_polygon_stipple = pstip_set_polygon_stipple;
510
511 return TRUE;
512
513 fail:
514 if (pstip)
515 pstip->stage.destroy( &pstip->stage );
516
517 return FALSE;
518 }