lp_rast.c revision 67e377bda6431b613836fdc04680a49b75e4b751
1/**************************************************************************
2 *
3 * Copyright 2009 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#include <limits.h>
29#include "util/u_memory.h"
30#include "util/u_math.h"
31#include "util/u_cpu_detect.h"
32#include "util/u_surface.h"
33
34#include "lp_scene_queue.h"
35#include "lp_debug.h"
36#include "lp_fence.h"
37#include "lp_perf.h"
38#include "lp_rast.h"
39#include "lp_rast_priv.h"
40#include "lp_tile_soa.h"
41#include "gallivm/lp_bld_debug.h"
42#include "lp_scene.h"
43
44
45/* Begin rasterizing a scene:
46 */
47static boolean
48lp_rast_begin( struct lp_rasterizer *rast,
49               struct lp_scene *scene )
50{
51   const struct pipe_framebuffer_state *fb = &scene->fb;
52   boolean write_color = fb->nr_cbufs != 0;
53   boolean write_zstencil = fb->zsbuf != NULL;
54   int i;
55
56   rast->curr_scene = scene;
57
58   LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
59
60   rast->state.nr_cbufs = scene->fb.nr_cbufs;
61   rast->state.write_zstencil = write_zstencil;
62   rast->state.write_color = write_color;
63
64   for (i = 0; i < rast->state.nr_cbufs; i++) {
65      struct pipe_surface *cbuf = scene->fb.cbufs[i];
66      rast->cbuf[i].map = scene->cbuf_map[i];
67      rast->cbuf[i].format = cbuf->texture->format;
68      rast->cbuf[i].width = cbuf->width;
69      rast->cbuf[i].height = cbuf->height;
70      rast->cbuf[i].stride = llvmpipe_texture_stride(cbuf->texture, cbuf->level);
71   }
72
73   if (write_zstencil) {
74      struct pipe_surface *zsbuf = scene->fb.zsbuf;
75      rast->zsbuf.map = scene->zsbuf_map;
76      rast->zsbuf.stride = llvmpipe_texture_stride(zsbuf->texture, zsbuf->level);
77      rast->zsbuf.blocksize =
78         util_format_get_blocksize(zsbuf->texture->format);
79   }
80
81   lp_scene_bin_iter_begin( scene );
82
83   return TRUE;
84}
85
86
87static void
88lp_rast_end( struct lp_rasterizer *rast )
89{
90   int i;
91
92   lp_scene_reset( rast->curr_scene );
93
94   for (i = 0; i < rast->state.nr_cbufs; i++)
95      rast->cbuf[i].map = NULL;
96
97   rast->zsbuf.map = NULL;
98   rast->curr_scene = NULL;
99}
100
101/**
102 * Begining rasterization of a tile.
103 * \param x  window X position of the tile, in pixels
104 * \param y  window Y position of the tile, in pixels
105 */
106static void
107lp_rast_start_tile(struct lp_rasterizer_task *task,
108                   unsigned x, unsigned y)
109{
110   LP_DBG(DEBUG_RAST, "%s %d,%d\n", __FUNCTION__, x, y);
111
112   task->x = x;
113   task->y = y;
114}
115
116
117/**
118 * Clear the rasterizer's current color tile.
119 * This is a bin command called during bin processing.
120 */
121void
122lp_rast_clear_color(struct lp_rasterizer_task *task,
123                    const union lp_rast_cmd_arg arg)
124{
125   struct lp_rasterizer *rast = task->rast;
126   const uint8_t *clear_color = arg.clear_color;
127   uint8_t **color_tile = task->tile.color;
128   unsigned i;
129
130   LP_DBG(DEBUG_RAST, "%s 0x%x,0x%x,0x%x,0x%x\n", __FUNCTION__,
131              clear_color[0],
132              clear_color[1],
133              clear_color[2],
134              clear_color[3]);
135
136   if (clear_color[0] == clear_color[1] &&
137       clear_color[1] == clear_color[2] &&
138       clear_color[2] == clear_color[3]) {
139      /* clear to grayscale value {x, x, x, x} */
140      for (i = 0; i < rast->state.nr_cbufs; i++) {
141	 memset(color_tile[i], clear_color[0], TILE_SIZE * TILE_SIZE * 4);
142      }
143   }
144   else {
145      /* Non-gray color.
146       * Note: if the swizzled tile layout changes (see TILE_PIXEL) this code
147       * will need to change.  It'll be pretty obvious when clearing no longer
148       * works.
149       */
150      const unsigned chunk = TILE_SIZE / 4;
151      for (i = 0; i < rast->state.nr_cbufs; i++) {
152         uint8_t *c = color_tile[i];
153         unsigned j;
154         for (j = 0; j < 4 * TILE_SIZE; j++) {
155            memset(c, clear_color[0], chunk);
156            c += chunk;
157            memset(c, clear_color[1], chunk);
158            c += chunk;
159            memset(c, clear_color[2], chunk);
160            c += chunk;
161            memset(c, clear_color[3], chunk);
162            c += chunk;
163         }
164         assert(c - color_tile[i] == TILE_SIZE * TILE_SIZE * 4);
165      }
166   }
167
168   LP_COUNT(nr_color_tile_clear);
169}
170
171
172/**
173 * Clear the rasterizer's current z/stencil tile.
174 * This is a bin command called during bin processing.
175 */
176void
177lp_rast_clear_zstencil(struct lp_rasterizer_task *task,
178                       const union lp_rast_cmd_arg arg)
179{
180   struct lp_rasterizer *rast = task->rast;
181   const unsigned tile_x = task->x;
182   const unsigned tile_y = task->y;
183   const unsigned height = TILE_SIZE / TILE_VECTOR_HEIGHT;
184   const unsigned width = TILE_SIZE * TILE_VECTOR_HEIGHT;
185   unsigned block_size = rast->zsbuf.blocksize;
186   uint8_t *dst;
187   unsigned dst_stride = rast->zsbuf.stride * TILE_VECTOR_HEIGHT;
188   unsigned i, j;
189
190   LP_DBG(DEBUG_RAST, "%s 0x%x\n", __FUNCTION__, arg.clear_zstencil);
191
192   /*assert(rast->zsbuf.map);*/
193   if (!rast->zsbuf.map)
194      return;
195
196   LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
197
198   /*
199    * Clear the aera of the swizzled depth/depth buffer matching this tile, in
200    * stripes of TILE_VECTOR_HEIGHT x TILE_SIZE at a time.
201    *
202    * The swizzled depth format is such that the depths for
203    * TILE_VECTOR_HEIGHT x TILE_VECTOR_WIDTH pixels have consecutive offsets.
204    */
205
206   dst = lp_rast_depth_pointer(rast, tile_x, tile_y);
207
208   switch (block_size) {
209   case 1:
210      memset(dst, (uint8_t) arg.clear_zstencil, height * width);
211      break;
212   case 2:
213      for (i = 0; i < height; i++) {
214         uint16_t *row = (uint16_t *)dst;
215         for (j = 0; j < width; j++)
216            *row++ = (uint16_t) arg.clear_zstencil;
217         dst += dst_stride;
218      }
219      break;
220   case 4:
221      for (i = 0; i < height; i++) {
222         uint32_t *row = (uint32_t *)dst;
223         for (j = 0; j < width; j++)
224            *row++ = arg.clear_zstencil;
225         dst += dst_stride;
226      }
227      break;
228   default:
229      assert(0);
230      break;
231   }
232}
233
234
235/**
236 * Load tile color from the framebuffer surface.
237 * This is a bin command called during bin processing.
238 */
239void
240lp_rast_load_color(struct lp_rasterizer_task *task,
241                   const union lp_rast_cmd_arg arg)
242{
243   struct lp_rasterizer *rast = task->rast;
244   const unsigned x = task->x, y = task->y;
245   unsigned i;
246
247   LP_DBG(DEBUG_RAST, "%s at %u, %u\n", __FUNCTION__, x, y);
248
249   for (i = 0; i < rast->state.nr_cbufs; i++) {
250      if (x >= rast->cbuf[i].width || y >= rast->cbuf[i].height)
251	 continue;
252
253      lp_tile_read_4ub(rast->cbuf[i].format,
254		       task->tile.color[i],
255		       rast->cbuf[i].map,
256		       rast->cbuf[i].stride,
257		       x, y,
258		       TILE_SIZE, TILE_SIZE);
259
260      LP_COUNT(nr_color_tile_load);
261   }
262}
263
264
265void
266lp_rast_set_state(struct lp_rasterizer_task *task,
267                  const union lp_rast_cmd_arg arg)
268{
269   const struct lp_rast_state *state = arg.set_state;
270
271   LP_DBG(DEBUG_RAST, "%s %p\n", __FUNCTION__, (void *) state);
272
273   /* just set the current state pointer for this rasterizer */
274   task->current_state = state;
275}
276
277
278
279/**
280 * Run the shader on all blocks in a tile.  This is used when a tile is
281 * completely contained inside a triangle.
282 * This is a bin command called during bin processing.
283 */
284void
285lp_rast_shade_tile(struct lp_rasterizer_task *task,
286                   const union lp_rast_cmd_arg arg)
287{
288   struct lp_rasterizer *rast = task->rast;
289   const struct lp_rast_state *state = task->current_state;
290   struct lp_rast_tile *tile = &task->tile;
291   const struct lp_rast_shader_inputs *inputs = arg.shade_tile;
292   const unsigned tile_x = task->x, tile_y = task->y;
293   unsigned x, y;
294
295   LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
296
297   /* render the whole 64x64 tile in 4x4 chunks */
298   for (y = 0; y < TILE_SIZE; y += 4){
299      for (x = 0; x < TILE_SIZE; x += 4) {
300         uint8_t *color[PIPE_MAX_COLOR_BUFS];
301         uint32_t *depth;
302         unsigned block_offset, i;
303
304         /* offset of the 16x16 pixel block within the tile */
305         block_offset = ((y / 4) * (16 * 16) + (x / 4) * 16);
306
307         /* color buffer */
308         for (i = 0; i < rast->state.nr_cbufs; i++)
309            color[i] = tile->color[i] + 4 * block_offset;
310
311         /* depth buffer */
312         depth = lp_rast_depth_pointer(rast, tile_x + x, tile_y + y);
313
314         /* run shader */
315         state->jit_function[RAST_WHOLE]( &state->jit_context,
316                                          tile_x + x, tile_y + y,
317                                          inputs->facing,
318                                          inputs->a0,
319                                          inputs->dadx,
320                                          inputs->dady,
321                                          color,
322                                          depth,
323                                          INT_MIN, INT_MIN, INT_MIN,
324                                          NULL, NULL, NULL );
325      }
326   }
327}
328
329
330/**
331 * Compute shading for a 4x4 block of pixels.
332 * This is a bin command called during bin processing.
333 */
334void lp_rast_shade_quads( struct lp_rasterizer_task *task,
335                          const struct lp_rast_shader_inputs *inputs,
336                          unsigned x, unsigned y,
337                          int32_t c1, int32_t c2, int32_t c3)
338{
339   const struct lp_rast_state *state = task->current_state;
340   struct lp_rasterizer *rast = task->rast;
341   struct lp_rast_tile *tile = &task->tile;
342   uint8_t *color[PIPE_MAX_COLOR_BUFS];
343   void *depth;
344   unsigned i;
345   unsigned ix, iy;
346   int block_offset;
347
348   assert(state);
349
350   /* Sanity checks */
351   assert(x % TILE_VECTOR_WIDTH == 0);
352   assert(y % TILE_VECTOR_HEIGHT == 0);
353
354   assert((x % 4) == 0);
355   assert((y % 4) == 0);
356
357   ix = x % TILE_SIZE;
358   iy = y % TILE_SIZE;
359
360   /* offset of the 16x16 pixel block within the tile */
361   block_offset = ((iy / 4) * (16 * 16) + (ix / 4) * 16);
362
363   /* color buffer */
364   for (i = 0; i < rast->state.nr_cbufs; i++)
365      color[i] = tile->color[i] + 4 * block_offset;
366
367   /* depth buffer */
368   depth = lp_rast_depth_pointer(rast, x, y);
369
370
371   assert(lp_check_alignment(tile->color[0], 16));
372   assert(lp_check_alignment(state->jit_context.blend_color, 16));
373
374   assert(lp_check_alignment(inputs->step[0], 16));
375   assert(lp_check_alignment(inputs->step[1], 16));
376   assert(lp_check_alignment(inputs->step[2], 16));
377
378   /* run shader */
379   state->jit_function[RAST_EDGE_TEST]( &state->jit_context,
380                                        x, y,
381                                        inputs->facing,
382                                        inputs->a0,
383                                        inputs->dadx,
384                                        inputs->dady,
385                                        color,
386                                        depth,
387                                        c1, c2, c3,
388                                        inputs->step[0],
389                                        inputs->step[1],
390                                        inputs->step[2]);
391}
392
393
394/**
395 * Set top row and left column of the tile's pixels to white.  For debugging.
396 */
397static void
398outline_tile(uint8_t *tile)
399{
400   const uint8_t val = 0xff;
401   unsigned i;
402
403   for (i = 0; i < TILE_SIZE; i++) {
404      TILE_PIXEL(tile, i, 0, 0) = val;
405      TILE_PIXEL(tile, i, 0, 1) = val;
406      TILE_PIXEL(tile, i, 0, 2) = val;
407      TILE_PIXEL(tile, i, 0, 3) = val;
408
409      TILE_PIXEL(tile, 0, i, 0) = val;
410      TILE_PIXEL(tile, 0, i, 1) = val;
411      TILE_PIXEL(tile, 0, i, 2) = val;
412      TILE_PIXEL(tile, 0, i, 3) = val;
413   }
414}
415
416
417/**
418 * Draw grid of gray lines at 16-pixel intervals across the tile to
419 * show the sub-tile boundaries.  For debugging.
420 */
421static void
422outline_subtiles(uint8_t *tile)
423{
424   const uint8_t val = 0x80;
425   const unsigned step = 16;
426   unsigned i, j;
427
428   for (i = 0; i < TILE_SIZE; i += step) {
429      for (j = 0; j < TILE_SIZE; j++) {
430         TILE_PIXEL(tile, i, j, 0) = val;
431         TILE_PIXEL(tile, i, j, 1) = val;
432         TILE_PIXEL(tile, i, j, 2) = val;
433         TILE_PIXEL(tile, i, j, 3) = val;
434
435         TILE_PIXEL(tile, j, i, 0) = val;
436         TILE_PIXEL(tile, j, i, 1) = val;
437         TILE_PIXEL(tile, j, i, 2) = val;
438         TILE_PIXEL(tile, j, i, 3) = val;
439      }
440   }
441
442   outline_tile(tile);
443}
444
445
446
447/**
448 * Write the rasterizer's color tile to the framebuffer.
449 */
450static void
451lp_rast_store_color(struct lp_rasterizer_task *task)
452{
453   struct lp_rasterizer *rast = task->rast;
454   const unsigned x = task->x, y = task->y;
455   unsigned i;
456
457   for (i = 0; i < rast->state.nr_cbufs; i++) {
458      if (x >= rast->cbuf[i].width)
459	 continue;
460
461      if (y >= rast->cbuf[i].height)
462	 continue;
463
464      LP_DBG(DEBUG_RAST, "%s [%u] %d,%d\n", __FUNCTION__,
465	     task->thread_index, x, y);
466
467      if (LP_DEBUG & DEBUG_SHOW_SUBTILES)
468         outline_subtiles(task->tile.color[i]);
469      else if (LP_DEBUG & DEBUG_SHOW_TILES)
470         outline_tile(task->tile.color[i]);
471
472      lp_tile_write_4ub(rast->cbuf[i].format,
473			task->tile.color[i],
474			rast->cbuf[i].map,
475			rast->cbuf[i].stride,
476			x, y,
477			TILE_SIZE, TILE_SIZE);
478
479      LP_COUNT(nr_color_tile_store);
480   }
481}
482
483
484
485/**
486 * Signal on a fence.  This is called during bin execution/rasterization.
487 * Called per thread.
488 */
489void
490lp_rast_fence(struct lp_rasterizer_task *task,
491              const union lp_rast_cmd_arg arg)
492{
493   struct lp_fence *fence = arg.fence;
494   lp_fence_signal(fence);
495}
496
497
498
499
500/**
501 * Rasterize commands for a single bin.
502 * \param x, y  position of the bin's tile in the framebuffer
503 * Must be called between lp_rast_begin() and lp_rast_end().
504 * Called per thread.
505 */
506static void
507rasterize_bin(struct lp_rasterizer_task *task,
508              const struct cmd_bin *bin,
509              int x, int y)
510{
511   const struct cmd_block_list *commands = &bin->commands;
512   struct cmd_block *block;
513   unsigned k;
514
515   lp_rast_start_tile( task, x * TILE_SIZE, y * TILE_SIZE );
516
517   /* simply execute each of the commands in the block list */
518   for (block = commands->head; block; block = block->next) {
519      for (k = 0; k < block->count; k++) {
520         block->cmd[k]( task, block->arg[k] );
521      }
522   }
523
524   /* Write the rasterizer's tiles to the framebuffer.
525    */
526   if (task->rast->state.write_color)
527      lp_rast_store_color(task);
528
529   /* Free data for this bin.
530    */
531   lp_scene_bin_reset( task->rast->curr_scene, x, y);
532}
533
534
535#define RAST(x) { lp_rast_##x, #x }
536
537static struct {
538   lp_rast_cmd cmd;
539   const char *name;
540} cmd_names[] =
541{
542   RAST(load_color),
543   RAST(clear_color),
544   RAST(clear_zstencil),
545   RAST(triangle),
546   RAST(shade_tile),
547   RAST(set_state),
548   RAST(fence),
549};
550
551static void
552debug_bin( const struct cmd_bin *bin )
553{
554   const struct cmd_block *head = bin->commands.head;
555   int i, j;
556
557   for (i = 0; i < head->count; i++) {
558      debug_printf("%d: ", i);
559      for (j = 0; j < Elements(cmd_names); j++) {
560         if (head->cmd[i] == cmd_names[j].cmd) {
561            debug_printf("%s\n", cmd_names[j].name);
562            break;
563         }
564      }
565      if (j == Elements(cmd_names))
566         debug_printf("...other\n");
567   }
568
569}
570
571/* An empty bin is one that just loads the contents of the tile and
572 * stores them again unchanged.  This typically happens when bins have
573 * been flushed for some reason in the middle of a frame, or when
574 * incremental updates are being made to a render target.
575 *
576 * Try to avoid doing pointless work in this case.
577 */
578static boolean
579is_empty_bin( const struct cmd_bin *bin )
580{
581   const struct cmd_block *head = bin->commands.head;
582   int i;
583
584   if (0)
585      debug_bin(bin);
586
587   /* We emit at most two load-tile commands at the start of the first
588    * command block.  In addition we seem to emit a couple of
589    * set-state commands even in empty bins.
590    *
591    * As a heuristic, if a bin has more than 4 commands, consider it
592    * non-empty.
593    */
594   if (head->next != NULL ||
595       head->count > 4) {
596      return FALSE;
597   }
598
599   for (i = 0; i < head->count; i++)
600      if (head->cmd[i] != lp_rast_load_color &&
601          head->cmd[i] != lp_rast_set_state) {
602         return FALSE;
603      }
604
605   return TRUE;
606}
607
608
609
610/**
611 * Rasterize/execute all bins within a scene.
612 * Called per thread.
613 */
614static void
615rasterize_scene(struct lp_rasterizer_task *task,
616                struct lp_scene *scene)
617{
618   /* loop over scene bins, rasterize each */
619#if 0
620   {
621      unsigned i, j;
622      for (i = 0; i < scene->tiles_x; i++) {
623         for (j = 0; j < scene->tiles_y; j++) {
624            struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
625            rasterize_bin(task, bin, i, j);
626         }
627      }
628   }
629#else
630   {
631      struct cmd_bin *bin;
632      int x, y;
633
634      assert(scene);
635      while ((bin = lp_scene_bin_iter_next(scene, &x, &y))) {
636         if (!is_empty_bin( bin ))
637            rasterize_bin(task, bin, x, y);
638      }
639   }
640#endif
641}
642
643
644/**
645 * Called by setup module when it has something for us to render.
646 */
647void
648lp_rast_queue_scene( struct lp_rasterizer *rast,
649                     struct lp_scene *scene)
650{
651   LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
652
653   if (rast->num_threads == 0) {
654      /* no threading */
655
656      lp_rast_begin( rast, scene );
657
658      rasterize_scene( &rast->tasks[0], scene );
659
660      lp_scene_reset( scene );
661      rast->curr_scene = NULL;
662   }
663   else {
664      /* threaded rendering! */
665      unsigned i;
666
667      lp_scene_enqueue( rast->full_scenes, scene );
668
669      /* signal the threads that there's work to do */
670      for (i = 0; i < rast->num_threads; i++) {
671         pipe_semaphore_signal(&rast->tasks[i].work_ready);
672      }
673   }
674
675   LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
676}
677
678
679void
680lp_rast_finish( struct lp_rasterizer *rast )
681{
682   if (rast->num_threads == 0) {
683      /* nothing to do */
684   }
685   else {
686      int i;
687
688      /* wait for work to complete */
689      for (i = 0; i < rast->num_threads; i++) {
690         pipe_semaphore_wait(&rast->tasks[i].work_done);
691      }
692   }
693}
694
695
696/**
697 * This is the thread's main entrypoint.
698 * It's a simple loop:
699 *   1. wait for work
700 *   2. do work
701 *   3. signal that we're done
702 */
703static PIPE_THREAD_ROUTINE( thread_func, init_data )
704{
705   struct lp_rasterizer_task *task = (struct lp_rasterizer_task *) init_data;
706   struct lp_rasterizer *rast = task->rast;
707   boolean debug = false;
708
709   while (1) {
710      /* wait for work */
711      if (debug)
712         debug_printf("thread %d waiting for work\n", task->thread_index);
713      pipe_semaphore_wait(&task->work_ready);
714
715      if (rast->exit_flag)
716         break;
717
718      if (task->thread_index == 0) {
719         /* thread[0]:
720          *  - get next scene to rasterize
721          *  - map the framebuffer surfaces
722          */
723         lp_rast_begin( rast,
724                        lp_scene_dequeue( rast->full_scenes, TRUE ) );
725      }
726
727      /* Wait for all threads to get here so that threads[1+] don't
728       * get a null rast->curr_scene pointer.
729       */
730      pipe_barrier_wait( &rast->barrier );
731
732      /* do work */
733      if (debug)
734         debug_printf("thread %d doing work\n", task->thread_index);
735
736      rasterize_scene(task,
737                      rast->curr_scene);
738
739      /* wait for all threads to finish with this scene */
740      pipe_barrier_wait( &rast->barrier );
741
742      /* XXX: shouldn't be necessary:
743       */
744      if (task->thread_index == 0) {
745         lp_rast_end( rast );
746      }
747
748      /* signal done with work */
749      if (debug)
750         debug_printf("thread %d done working\n", task->thread_index);
751
752      pipe_semaphore_signal(&task->work_done);
753   }
754
755   return NULL;
756}
757
758
759/**
760 * Initialize semaphores and spawn the threads.
761 */
762static void
763create_rast_threads(struct lp_rasterizer *rast)
764{
765   unsigned i;
766
767#ifdef PIPE_OS_WINDOWS
768   /* Multithreading not supported on windows until conditions and barriers are
769    * properly implemented. */
770   rast->num_threads = 0;
771#else
772   rast->num_threads = util_cpu_caps.nr_cpus;
773   rast->num_threads = debug_get_num_option("LP_NUM_THREADS", rast->num_threads);
774   rast->num_threads = MIN2(rast->num_threads, MAX_THREADS);
775#endif
776
777   /* NOTE: if num_threads is zero, we won't use any threads */
778   for (i = 0; i < rast->num_threads; i++) {
779      pipe_semaphore_init(&rast->tasks[i].work_ready, 0);
780      pipe_semaphore_init(&rast->tasks[i].work_done, 0);
781      rast->threads[i] = pipe_thread_create(thread_func,
782                                            (void *) &rast->tasks[i]);
783   }
784}
785
786
787
788/**
789 * Create new lp_rasterizer.
790 * \param empty  the queue to put empty scenes on after we've finished
791 *               processing them.
792 */
793struct lp_rasterizer *
794lp_rast_create( void )
795{
796   struct lp_rasterizer *rast;
797   unsigned i, cbuf;
798
799   rast = CALLOC_STRUCT(lp_rasterizer);
800   if(!rast)
801      return NULL;
802
803   rast->full_scenes = lp_scene_queue_create();
804
805   for (i = 0; i < Elements(rast->tasks); i++) {
806      struct lp_rasterizer_task *task = &rast->tasks[i];
807
808      for (cbuf = 0; cbuf < PIPE_MAX_COLOR_BUFS; cbuf++ )
809	 task->tile.color[cbuf] = align_malloc(TILE_SIZE * TILE_SIZE * 4, 16);
810
811      task->rast = rast;
812      task->thread_index = i;
813   }
814
815   create_rast_threads(rast);
816
817   /* for synchronizing rasterization threads */
818   pipe_barrier_init( &rast->barrier, rast->num_threads );
819
820   return rast;
821}
822
823
824/* Shutdown:
825 */
826void lp_rast_destroy( struct lp_rasterizer *rast )
827{
828   unsigned i, cbuf;
829
830   for (i = 0; i < Elements(rast->tasks); i++) {
831      for (cbuf = 0; cbuf < PIPE_MAX_COLOR_BUFS; cbuf++ )
832	 align_free(rast->tasks[i].tile.color[cbuf]);
833   }
834
835   /* Set exit_flag and signal each thread's work_ready semaphore.
836    * Each thread will be woken up, notice that the exit_flag is set and
837    * break out of its main loop.  The thread will then exit.
838    */
839   rast->exit_flag = TRUE;
840   for (i = 0; i < rast->num_threads; i++) {
841      pipe_semaphore_signal(&rast->tasks[i].work_ready);
842   }
843
844   /* Wait for threads to terminate before cleaning up per-thread data */
845   for (i = 0; i < rast->num_threads; i++) {
846      pipe_thread_wait(rast->threads[i]);
847   }
848
849   /* Clean up per-thread data */
850   for (i = 0; i < rast->num_threads; i++) {
851      pipe_semaphore_destroy(&rast->tasks[i].work_ready);
852      pipe_semaphore_destroy(&rast->tasks[i].work_done);
853   }
854
855   /* for synchronizing rasterization threads */
856   pipe_barrier_destroy( &rast->barrier );
857
858   FREE(rast);
859}
860
861
862/** Return number of rasterization threads */
863unsigned
864lp_rast_get_num_threads( struct lp_rasterizer *rast )
865{
866   return rast->num_threads;
867}
868