Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lp_rast.c 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. #include <limits.h>
  28. #include "util/u_memory.h"
  29. #include "util/u_math.h"
  30. #include "util/u_rect.h"
  31. #include "util/u_surface.h"
  32. #include "util/u_pack_color.h"
  33. #include "lp_scene_queue.h"
  34. #include "lp_debug.h"
  35. #include "lp_fence.h"
  36. #include "lp_perf.h"
  37. #include "lp_query.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. #ifdef DEBUG
  44. int jit_line = 0;
  45. const struct lp_rast_state *jit_state = NULL;
  46. #endif
  47. /**
  48. * Begin rasterizing a scene.
  49. * Called once per scene by one thread.
  50. */
  51. static void
  52. lp_rast_begin( struct lp_rasterizer *rast,
  53. struct lp_scene *scene )
  54. {
  55. rast->curr_scene = scene;
  56. LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
  57. lp_scene_begin_rasterization( scene );
  58. lp_scene_bin_iter_begin( scene );
  59. }
  60. static void
  61. lp_rast_end( struct lp_rasterizer *rast )
  62. {
  63. lp_scene_end_rasterization( rast->curr_scene );
  64. rast->curr_scene = NULL;
  65. #ifdef DEBUG
  66. if (0)
  67. debug_printf("Post render scene: tile unswizzle: %u tile swizzle: %u\n",
  68. lp_tile_unswizzle_count, lp_tile_swizzle_count);
  69. #endif
  70. }
  71. /**
  72. * Begining rasterization of a tile.
  73. * \param x window X position of the tile, in pixels
  74. * \param y window Y position of the tile, in pixels
  75. */
  76. static void
  77. lp_rast_tile_begin(struct lp_rasterizer_task *task,
  78. const struct cmd_bin *bin)
  79. {
  80. const struct lp_scene *scene = task->scene;
  81. enum lp_texture_usage usage;
  82. LP_DBG(DEBUG_RAST, "%s %d,%d\n", __FUNCTION__, bin->x, bin->y);
  83. task->bin = bin;
  84. task->x = bin->x * TILE_SIZE;
  85. task->y = bin->y * TILE_SIZE;
  86. /* reset pointers to color tile(s) */
  87. memset(task->color_tiles, 0, sizeof(task->color_tiles));
  88. /* get pointer to depth/stencil tile */
  89. {
  90. struct pipe_surface *zsbuf = task->scene->fb.zsbuf;
  91. if (zsbuf) {
  92. struct llvmpipe_resource *lpt = llvmpipe_resource(zsbuf->texture);
  93. if (scene->has_depthstencil_clear)
  94. usage = LP_TEX_USAGE_WRITE_ALL;
  95. else
  96. usage = LP_TEX_USAGE_READ_WRITE;
  97. /* "prime" the tile: convert data from linear to tiled if necessary
  98. * and update the tile's layout info.
  99. */
  100. (void) llvmpipe_get_texture_tile(lpt,
  101. zsbuf->face + zsbuf->zslice,
  102. zsbuf->level,
  103. usage,
  104. task->x,
  105. task->y);
  106. /* Get actual pointer to the tile data. Note that depth/stencil
  107. * data is tiled differently than color data.
  108. */
  109. task->depth_tile = lp_rast_get_depth_block_pointer(task,
  110. task->x,
  111. task->y);
  112. assert(task->depth_tile);
  113. }
  114. else {
  115. task->depth_tile = NULL;
  116. }
  117. }
  118. }
  119. /**
  120. * Clear the rasterizer's current color tile.
  121. * This is a bin command called during bin processing.
  122. */
  123. static void
  124. lp_rast_clear_color(struct lp_rasterizer_task *task,
  125. const union lp_rast_cmd_arg arg)
  126. {
  127. const struct lp_scene *scene = task->scene;
  128. const uint8_t *clear_color = arg.clear_color;
  129. unsigned i;
  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. if (clear_color[0] == clear_color[1] &&
  136. clear_color[1] == clear_color[2] &&
  137. clear_color[2] == clear_color[3]) {
  138. /* clear to grayscale value {x, x, x, x} */
  139. for (i = 0; i < scene->fb.nr_cbufs; i++) {
  140. uint8_t *ptr =
  141. lp_rast_get_color_tile_pointer(task, i, LP_TEX_USAGE_WRITE_ALL);
  142. memset(ptr, clear_color[0], TILE_SIZE * TILE_SIZE * 4);
  143. }
  144. }
  145. else {
  146. /* Non-gray color.
  147. * Note: if the swizzled tile layout changes (see TILE_PIXEL) this code
  148. * will need to change. It'll be pretty obvious when clearing no longer
  149. * works.
  150. */
  151. const unsigned chunk = TILE_SIZE / 4;
  152. for (i = 0; i < scene->fb.nr_cbufs; i++) {
  153. uint8_t *c =
  154. lp_rast_get_color_tile_pointer(task, i, LP_TEX_USAGE_WRITE_ALL);
  155. unsigned j;
  156. for (j = 0; j < 4 * TILE_SIZE; j++) {
  157. memset(c, clear_color[0], chunk);
  158. c += chunk;
  159. memset(c, clear_color[1], chunk);
  160. c += chunk;
  161. memset(c, clear_color[2], chunk);
  162. c += chunk;
  163. memset(c, clear_color[3], chunk);
  164. c += chunk;
  165. }
  166. }
  167. }
  168. LP_COUNT(nr_color_tile_clear);
  169. }
  170. /**
  171. * Clear the rasterizer's current z/stencil tile.
  172. * This is a bin command called during bin processing.
  173. */
  174. static void
  175. lp_rast_clear_zstencil(struct lp_rasterizer_task *task,
  176. const union lp_rast_cmd_arg arg)
  177. {
  178. const struct lp_scene *scene = task->scene;
  179. uint32_t clear_value = arg.clear_zstencil.value;
  180. uint32_t clear_mask = arg.clear_zstencil.mask;
  181. const unsigned height = TILE_SIZE / TILE_VECTOR_HEIGHT;
  182. const unsigned width = TILE_SIZE * TILE_VECTOR_HEIGHT;
  183. const unsigned block_size = scene->zsbuf.blocksize;
  184. const unsigned dst_stride = scene->zsbuf.stride * TILE_VECTOR_HEIGHT;
  185. uint8_t *dst;
  186. unsigned i, j;
  187. LP_DBG(DEBUG_RAST, "%s: value=0x%08x, mask=0x%08x\n",
  188. __FUNCTION__, clear_value, clear_mask);
  189. /*
  190. * Clear the aera of the swizzled depth/depth buffer matching this tile, in
  191. * stripes of TILE_VECTOR_HEIGHT x TILE_SIZE at a time.
  192. *
  193. * The swizzled depth format is such that the depths for
  194. * TILE_VECTOR_HEIGHT x TILE_VECTOR_WIDTH pixels have consecutive offsets.
  195. */
  196. dst = task->depth_tile;
  197. clear_value &= clear_mask;
  198. switch (block_size) {
  199. case 1:
  200. assert(clear_mask == 0xff);
  201. memset(dst, (uint8_t) clear_value, height * width);
  202. break;
  203. case 2:
  204. if (clear_mask == 0xffff) {
  205. for (i = 0; i < height; i++) {
  206. uint16_t *row = (uint16_t *)dst;
  207. for (j = 0; j < width; j++)
  208. *row++ = (uint16_t) clear_value;
  209. dst += dst_stride;
  210. }
  211. }
  212. else {
  213. for (i = 0; i < height; i++) {
  214. uint16_t *row = (uint16_t *)dst;
  215. for (j = 0; j < width; j++) {
  216. uint16_t tmp = ~clear_mask & *row;
  217. *row++ = clear_value | tmp;
  218. }
  219. dst += dst_stride;
  220. }
  221. }
  222. break;
  223. case 4:
  224. if (clear_mask == 0xffffffff) {
  225. for (i = 0; i < height; i++) {
  226. uint32_t *row = (uint32_t *)dst;
  227. for (j = 0; j < width; j++)
  228. *row++ = clear_value;
  229. dst += dst_stride;
  230. }
  231. }
  232. else {
  233. for (i = 0; i < height; i++) {
  234. uint32_t *row = (uint32_t *)dst;
  235. for (j = 0; j < width; j++) {
  236. uint32_t tmp = ~clear_mask & *row;
  237. *row++ = clear_value | tmp;
  238. }
  239. dst += dst_stride;
  240. }
  241. }
  242. break;
  243. default:
  244. assert(0);
  245. break;
  246. }
  247. }
  248. /**
  249. * Convert the color tile from tiled to linear layout.
  250. * This is generally only done when we're flushing the scene just prior to
  251. * SwapBuffers. If we didn't do this here, we'd have to convert the entire
  252. * tiled color buffer to linear layout in the llvmpipe_texture_unmap()
  253. * function. It's better to do it here to take advantage of
  254. * threading/parallelism.
  255. * This is a bin command which is stored in all bins.
  256. */
  257. static void
  258. lp_rast_store_linear_color( struct lp_rasterizer_task *task )
  259. {
  260. const struct lp_scene *scene = task->scene;
  261. unsigned buf;
  262. for (buf = 0; buf < scene->fb.nr_cbufs; buf++) {
  263. struct pipe_surface *cbuf = scene->fb.cbufs[buf];
  264. const unsigned face_slice = cbuf->face + cbuf->zslice;
  265. const unsigned level = cbuf->level;
  266. struct llvmpipe_resource *lpt = llvmpipe_resource(cbuf->texture);
  267. if (!task->color_tiles[buf])
  268. continue;
  269. llvmpipe_unswizzle_cbuf_tile(lpt,
  270. face_slice,
  271. level,
  272. task->x, task->y,
  273. task->color_tiles[buf]);
  274. }
  275. }
  276. /**
  277. * Run the shader on all blocks in a tile. This is used when a tile is
  278. * completely contained inside a triangle.
  279. * This is a bin command called during bin processing.
  280. */
  281. static void
  282. lp_rast_shade_tile(struct lp_rasterizer_task *task,
  283. const union lp_rast_cmd_arg arg)
  284. {
  285. const struct lp_scene *scene = task->scene;
  286. const struct lp_rast_shader_inputs *inputs = arg.shade_tile;
  287. const struct lp_rast_state *state = task->state;
  288. struct lp_fragment_shader_variant *variant = state->variant;
  289. const unsigned tile_x = task->x, tile_y = task->y;
  290. unsigned x, y;
  291. if (inputs->disable) {
  292. /* This command was partially binned and has been disabled */
  293. return;
  294. }
  295. LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
  296. /* render the whole 64x64 tile in 4x4 chunks */
  297. for (y = 0; y < TILE_SIZE; y += 4){
  298. for (x = 0; x < TILE_SIZE; x += 4) {
  299. uint8_t *color[PIPE_MAX_COLOR_BUFS];
  300. uint32_t *depth;
  301. unsigned i;
  302. /* color buffer */
  303. for (i = 0; i < scene->fb.nr_cbufs; i++)
  304. color[i] = lp_rast_get_color_block_pointer(task, i,
  305. tile_x + x, tile_y + y);
  306. /* depth buffer */
  307. depth = lp_rast_get_depth_block_pointer(task, tile_x + x, tile_y + y);
  308. /* run shader on 4x4 block */
  309. BEGIN_JIT_CALL(state);
  310. variant->jit_function[RAST_WHOLE]( &state->jit_context,
  311. tile_x + x, tile_y + y,
  312. inputs->frontfacing,
  313. inputs->a0,
  314. inputs->dadx,
  315. inputs->dady,
  316. color,
  317. depth,
  318. 0xffff,
  319. &task->vis_counter);
  320. END_JIT_CALL();
  321. }
  322. }
  323. }
  324. /**
  325. * Run the shader on all blocks in a tile. This is used when a tile is
  326. * completely contained inside a triangle, and the shader is opaque.
  327. * This is a bin command called during bin processing.
  328. */
  329. static void
  330. lp_rast_shade_tile_opaque(struct lp_rasterizer_task *task,
  331. const union lp_rast_cmd_arg arg)
  332. {
  333. const struct lp_scene *scene = task->scene;
  334. unsigned i;
  335. LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
  336. /* this will prevent converting the layout from tiled to linear */
  337. for (i = 0; i < scene->fb.nr_cbufs; i++) {
  338. (void)lp_rast_get_color_tile_pointer(task, i, LP_TEX_USAGE_WRITE_ALL);
  339. }
  340. lp_rast_shade_tile(task, arg);
  341. }
  342. /**
  343. * Compute shading for a 4x4 block of pixels inside a triangle.
  344. * This is a bin command called during bin processing.
  345. * \param x X position of quad in window coords
  346. * \param y Y position of quad in window coords
  347. */
  348. void
  349. lp_rast_shade_quads_mask(struct lp_rasterizer_task *task,
  350. const struct lp_rast_shader_inputs *inputs,
  351. unsigned x, unsigned y,
  352. unsigned mask)
  353. {
  354. const struct lp_rast_state *state = task->state;
  355. struct lp_fragment_shader_variant *variant = state->variant;
  356. const struct lp_scene *scene = task->scene;
  357. uint8_t *color[PIPE_MAX_COLOR_BUFS];
  358. void *depth;
  359. unsigned i;
  360. assert(state);
  361. /* Sanity checks */
  362. assert(x % TILE_VECTOR_WIDTH == 0);
  363. assert(y % TILE_VECTOR_HEIGHT == 0);
  364. assert((x % 4) == 0);
  365. assert((y % 4) == 0);
  366. /* color buffer */
  367. for (i = 0; i < scene->fb.nr_cbufs; i++) {
  368. color[i] = lp_rast_get_color_block_pointer(task, i, x, y);
  369. assert(lp_check_alignment(color[i], 16));
  370. }
  371. /* depth buffer */
  372. depth = lp_rast_get_depth_block_pointer(task, x, y);
  373. assert(lp_check_alignment(state->jit_context.blend_color, 16));
  374. /* run shader on 4x4 block */
  375. BEGIN_JIT_CALL(state);
  376. variant->jit_function[RAST_EDGE_TEST](&state->jit_context,
  377. x, y,
  378. inputs->frontfacing,
  379. inputs->a0,
  380. inputs->dadx,
  381. inputs->dady,
  382. color,
  383. depth,
  384. mask,
  385. &task->vis_counter);
  386. END_JIT_CALL();
  387. }
  388. /**
  389. * Begin a new occlusion query.
  390. * This is a bin command put in all bins.
  391. * Called per thread.
  392. */
  393. static void
  394. lp_rast_begin_query(struct lp_rasterizer_task *task,
  395. const union lp_rast_cmd_arg arg)
  396. {
  397. struct llvmpipe_query *pq = arg.query_obj;
  398. assert(task->query == NULL);
  399. task->vis_counter = 0;
  400. task->query = pq;
  401. }
  402. /**
  403. * End the current occlusion query.
  404. * This is a bin command put in all bins.
  405. * Called per thread.
  406. */
  407. static void
  408. lp_rast_end_query(struct lp_rasterizer_task *task,
  409. const union lp_rast_cmd_arg arg)
  410. {
  411. task->query->count[task->thread_index] += task->vis_counter;
  412. task->query = NULL;
  413. }
  414. void
  415. lp_rast_set_state(struct lp_rasterizer_task *task,
  416. const union lp_rast_cmd_arg arg)
  417. {
  418. task->state = arg.state;
  419. }
  420. /**
  421. * Set top row and left column of the tile's pixels to white. For debugging.
  422. */
  423. static void
  424. outline_tile(uint8_t *tile)
  425. {
  426. const uint8_t val = 0xff;
  427. unsigned i;
  428. for (i = 0; i < TILE_SIZE; i++) {
  429. TILE_PIXEL(tile, i, 0, 0) = val;
  430. TILE_PIXEL(tile, i, 0, 1) = val;
  431. TILE_PIXEL(tile, i, 0, 2) = val;
  432. TILE_PIXEL(tile, i, 0, 3) = val;
  433. TILE_PIXEL(tile, 0, i, 0) = val;
  434. TILE_PIXEL(tile, 0, i, 1) = val;
  435. TILE_PIXEL(tile, 0, i, 2) = val;
  436. TILE_PIXEL(tile, 0, i, 3) = val;
  437. }
  438. }
  439. /**
  440. * Draw grid of gray lines at 16-pixel intervals across the tile to
  441. * show the sub-tile boundaries. For debugging.
  442. */
  443. static void
  444. outline_subtiles(uint8_t *tile)
  445. {
  446. const uint8_t val = 0x80;
  447. const unsigned step = 16;
  448. unsigned i, j;
  449. for (i = 0; i < TILE_SIZE; i += step) {
  450. for (j = 0; j < TILE_SIZE; j++) {
  451. TILE_PIXEL(tile, i, j, 0) = val;
  452. TILE_PIXEL(tile, i, j, 1) = val;
  453. TILE_PIXEL(tile, i, j, 2) = val;
  454. TILE_PIXEL(tile, i, j, 3) = val;
  455. TILE_PIXEL(tile, j, i, 0) = val;
  456. TILE_PIXEL(tile, j, i, 1) = val;
  457. TILE_PIXEL(tile, j, i, 2) = val;
  458. TILE_PIXEL(tile, j, i, 3) = val;
  459. }
  460. }
  461. outline_tile(tile);
  462. }
  463. /**
  464. * Called when we're done writing to a color tile.
  465. */
  466. static void
  467. lp_rast_tile_end(struct lp_rasterizer_task *task)
  468. {
  469. #ifdef DEBUG
  470. if (LP_DEBUG & (DEBUG_SHOW_SUBTILES | DEBUG_SHOW_TILES)) {
  471. const struct lp_scene *scene = task->scene;
  472. unsigned buf;
  473. for (buf = 0; buf < scene->fb.nr_cbufs; buf++) {
  474. uint8_t *color = lp_rast_get_color_block_pointer(task, buf,
  475. task->x, task->y);
  476. if (LP_DEBUG & DEBUG_SHOW_SUBTILES)
  477. outline_subtiles(color);
  478. else if (LP_DEBUG & DEBUG_SHOW_TILES)
  479. outline_tile(color);
  480. }
  481. }
  482. #else
  483. (void) outline_subtiles;
  484. #endif
  485. lp_rast_store_linear_color(task);
  486. if (task->query) {
  487. union lp_rast_cmd_arg dummy = {0};
  488. lp_rast_end_query(task, dummy);
  489. }
  490. /* debug */
  491. memset(task->color_tiles, 0, sizeof(task->color_tiles));
  492. task->depth_tile = NULL;
  493. task->bin = NULL;
  494. }
  495. static lp_rast_cmd_func dispatch[LP_RAST_OP_MAX] =
  496. {
  497. lp_rast_clear_color,
  498. lp_rast_clear_zstencil,
  499. lp_rast_triangle_1,
  500. lp_rast_triangle_2,
  501. lp_rast_triangle_3,
  502. lp_rast_triangle_4,
  503. lp_rast_triangle_5,
  504. lp_rast_triangle_6,
  505. lp_rast_triangle_7,
  506. lp_rast_triangle_8,
  507. lp_rast_triangle_3_4,
  508. lp_rast_triangle_3_16,
  509. lp_rast_triangle_4_16,
  510. lp_rast_shade_tile,
  511. lp_rast_shade_tile_opaque,
  512. lp_rast_begin_query,
  513. lp_rast_end_query,
  514. lp_rast_set_state,
  515. };
  516. static void
  517. do_rasterize_bin(struct lp_rasterizer_task *task,
  518. const struct cmd_bin *bin)
  519. {
  520. const struct cmd_block *block;
  521. unsigned k;
  522. if (0)
  523. lp_debug_bin(bin);
  524. for (block = bin->head; block; block = block->next) {
  525. for (k = 0; k < block->count; k++) {
  526. dispatch[block->cmd[k]]( task, block->arg[k] );
  527. }
  528. }
  529. }
  530. /**
  531. * Rasterize commands for a single bin.
  532. * \param x, y position of the bin's tile in the framebuffer
  533. * Must be called between lp_rast_begin() and lp_rast_end().
  534. * Called per thread.
  535. */
  536. static void
  537. rasterize_bin(struct lp_rasterizer_task *task,
  538. const struct cmd_bin *bin )
  539. {
  540. lp_rast_tile_begin( task, bin );
  541. do_rasterize_bin(task, bin);
  542. lp_rast_tile_end(task);
  543. /* Debug/Perf flags:
  544. */
  545. if (bin->head->count == 1) {
  546. if (bin->head->cmd[0] == LP_RAST_OP_SHADE_TILE_OPAQUE)
  547. LP_COUNT(nr_pure_shade_opaque_64);
  548. else if (bin->head->cmd[0] == LP_RAST_OP_SHADE_TILE)
  549. LP_COUNT(nr_pure_shade_64);
  550. }
  551. }
  552. /* An empty bin is one that just loads the contents of the tile and
  553. * stores them again unchanged. This typically happens when bins have
  554. * been flushed for some reason in the middle of a frame, or when
  555. * incremental updates are being made to a render target.
  556. *
  557. * Try to avoid doing pointless work in this case.
  558. */
  559. static boolean
  560. is_empty_bin( const struct cmd_bin *bin )
  561. {
  562. return bin->head == NULL;
  563. }
  564. /**
  565. * Rasterize/execute all bins within a scene.
  566. * Called per thread.
  567. */
  568. static void
  569. rasterize_scene(struct lp_rasterizer_task *task,
  570. struct lp_scene *scene)
  571. {
  572. task->scene = scene;
  573. /* loop over scene bins, rasterize each */
  574. #if 0
  575. {
  576. unsigned i, j;
  577. for (i = 0; i < scene->tiles_x; i++) {
  578. for (j = 0; j < scene->tiles_y; j++) {
  579. struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
  580. rasterize_bin(task, bin, i, j);
  581. }
  582. }
  583. }
  584. #else
  585. {
  586. struct cmd_bin *bin;
  587. assert(scene);
  588. while ((bin = lp_scene_bin_iter_next(scene))) {
  589. if (!is_empty_bin( bin ))
  590. rasterize_bin(task, bin);
  591. }
  592. }
  593. #endif
  594. if (scene->fence) {
  595. lp_fence_signal(scene->fence);
  596. }
  597. task->scene = NULL;
  598. }
  599. /**
  600. * Called by setup module when it has something for us to render.
  601. */
  602. void
  603. lp_rast_queue_scene( struct lp_rasterizer *rast,
  604. struct lp_scene *scene)
  605. {
  606. LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
  607. if (rast->num_threads == 0) {
  608. /* no threading */
  609. lp_rast_begin( rast, scene );
  610. rasterize_scene( &rast->tasks[0], scene );
  611. lp_rast_end( rast );
  612. rast->curr_scene = NULL;
  613. }
  614. else {
  615. /* threaded rendering! */
  616. unsigned i;
  617. lp_scene_enqueue( rast->full_scenes, scene );
  618. /* signal the threads that there's work to do */
  619. for (i = 0; i < rast->num_threads; i++) {
  620. pipe_semaphore_signal(&rast->tasks[i].work_ready);
  621. }
  622. }
  623. LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
  624. }
  625. void
  626. lp_rast_finish( struct lp_rasterizer *rast )
  627. {
  628. if (rast->num_threads == 0) {
  629. /* nothing to do */
  630. }
  631. else {
  632. int i;
  633. /* wait for work to complete */
  634. for (i = 0; i < rast->num_threads; i++) {
  635. pipe_semaphore_wait(&rast->tasks[i].work_done);
  636. }
  637. }
  638. }
  639. /**
  640. * This is the thread's main entrypoint.
  641. * It's a simple loop:
  642. * 1. wait for work
  643. * 2. do work
  644. * 3. signal that we're done
  645. */
  646. static PIPE_THREAD_ROUTINE( thread_func, init_data )
  647. {
  648. struct lp_rasterizer_task *task = (struct lp_rasterizer_task *) init_data;
  649. struct lp_rasterizer *rast = task->rast;
  650. boolean debug = false;
  651. while (1) {
  652. /* wait for work */
  653. if (debug)
  654. debug_printf("thread %d waiting for work\n", task->thread_index);
  655. pipe_semaphore_wait(&task->work_ready);
  656. if (rast->exit_flag)
  657. break;
  658. if (task->thread_index == 0) {
  659. /* thread[0]:
  660. * - get next scene to rasterize
  661. * - map the framebuffer surfaces
  662. */
  663. lp_rast_begin( rast,
  664. lp_scene_dequeue( rast->full_scenes, TRUE ) );
  665. }
  666. /* Wait for all threads to get here so that threads[1+] don't
  667. * get a null rast->curr_scene pointer.
  668. */
  669. pipe_barrier_wait( &rast->barrier );
  670. /* do work */
  671. if (debug)
  672. debug_printf("thread %d doing work\n", task->thread_index);
  673. rasterize_scene(task,
  674. rast->curr_scene);
  675. /* wait for all threads to finish with this scene */
  676. pipe_barrier_wait( &rast->barrier );
  677. /* XXX: shouldn't be necessary:
  678. */
  679. if (task->thread_index == 0) {
  680. lp_rast_end( rast );
  681. }
  682. /* signal done with work */
  683. if (debug)
  684. debug_printf("thread %d done working\n", task->thread_index);
  685. pipe_semaphore_signal(&task->work_done);
  686. }
  687. return NULL;
  688. }
  689. /**
  690. * Initialize semaphores and spawn the threads.
  691. */
  692. static void
  693. create_rast_threads(struct lp_rasterizer *rast)
  694. {
  695. unsigned i;
  696. /* NOTE: if num_threads is zero, we won't use any threads */
  697. for (i = 0; i < rast->num_threads; i++) {
  698. pipe_semaphore_init(&rast->tasks[i].work_ready, 0);
  699. pipe_semaphore_init(&rast->tasks[i].work_done, 0);
  700. rast->threads[i] = pipe_thread_create(thread_func,
  701. (void *) &rast->tasks[i]);
  702. }
  703. }
  704. /**
  705. * Create new lp_rasterizer. If num_threads is zero, don't create any
  706. * new threads, do rendering synchronously.
  707. * \param num_threads number of rasterizer threads to create
  708. */
  709. struct lp_rasterizer *
  710. lp_rast_create( unsigned num_threads )
  711. {
  712. struct lp_rasterizer *rast;
  713. unsigned i;
  714. rast = CALLOC_STRUCT(lp_rasterizer);
  715. if(!rast)
  716. return NULL;
  717. rast->full_scenes = lp_scene_queue_create();
  718. for (i = 0; i < Elements(rast->tasks); i++) {
  719. struct lp_rasterizer_task *task = &rast->tasks[i];
  720. task->rast = rast;
  721. task->thread_index = i;
  722. }
  723. rast->num_threads = num_threads;
  724. create_rast_threads(rast);
  725. /* for synchronizing rasterization threads */
  726. pipe_barrier_init( &rast->barrier, rast->num_threads );
  727. memset(lp_swizzled_cbuf, 0, sizeof lp_swizzled_cbuf);
  728. memset(lp_dummy_tile, 0, sizeof lp_dummy_tile);
  729. return rast;
  730. }
  731. /* Shutdown:
  732. */
  733. void lp_rast_destroy( struct lp_rasterizer *rast )
  734. {
  735. unsigned i;
  736. /* Set exit_flag and signal each thread's work_ready semaphore.
  737. * Each thread will be woken up, notice that the exit_flag is set and
  738. * break out of its main loop. The thread will then exit.
  739. */
  740. rast->exit_flag = TRUE;
  741. for (i = 0; i < rast->num_threads; i++) {
  742. pipe_semaphore_signal(&rast->tasks[i].work_ready);
  743. }
  744. /* Wait for threads to terminate before cleaning up per-thread data */
  745. for (i = 0; i < rast->num_threads; i++) {
  746. pipe_thread_wait(rast->threads[i]);
  747. }
  748. /* Clean up per-thread data */
  749. for (i = 0; i < rast->num_threads; i++) {
  750. pipe_semaphore_destroy(&rast->tasks[i].work_ready);
  751. pipe_semaphore_destroy(&rast->tasks[i].work_done);
  752. }
  753. /* for synchronizing rasterization threads */
  754. pipe_barrier_destroy( &rast->barrier );
  755. lp_scene_queue_destroy(rast->full_scenes);
  756. FREE(rast);
  757. }
  758. /** Return number of rasterization threads */
  759. unsigned
  760. lp_rast_get_num_threads( struct lp_rasterizer *rast )
  761. {
  762. return rast->num_threads;
  763. }