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.

sp_tile_cache.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /**************************************************************************
  2. *
  3. * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
  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 TUNGSTEN GRAPHICS 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. * Texture tile caching.
  29. *
  30. * Author:
  31. * Brian Paul
  32. */
  33. #include "pipe/p_inlines.h"
  34. #include "util/u_memory.h"
  35. #include "util/u_tile.h"
  36. #include "sp_context.h"
  37. #include "sp_surface.h"
  38. #include "sp_texture.h"
  39. #include "sp_tile_cache.h"
  40. #define NUM_ENTRIES 50
  41. /** XXX move these */
  42. #define MAX_WIDTH 2048
  43. #define MAX_HEIGHT 2048
  44. struct softpipe_tile_cache
  45. {
  46. struct pipe_screen *screen;
  47. struct pipe_surface *surface; /**< the surface we're caching */
  48. struct pipe_transfer *transfer;
  49. void *transfer_map;
  50. struct pipe_texture *texture; /**< if caching a texture */
  51. struct softpipe_cached_tile entries[NUM_ENTRIES];
  52. uint clear_flags[(MAX_WIDTH / TILE_SIZE) * (MAX_HEIGHT / TILE_SIZE) / 32];
  53. float clear_color[4];
  54. uint clear_val;
  55. boolean depth_stencil; /** Is the surface a depth/stencil format? */
  56. struct pipe_transfer *tex_trans;
  57. void *tex_trans_map;
  58. int tex_face, tex_level, tex_z;
  59. struct softpipe_cached_tile tile; /**< scratch tile for clears */
  60. };
  61. /**
  62. * Return the position in the cache for the tile that contains win pos (x,y).
  63. * We currently use a direct mapped cache so this is like a hack key.
  64. * At some point we should investige something more sophisticated, like
  65. * a LRU replacement policy.
  66. */
  67. #define CACHE_POS(x, y) \
  68. (((x) / TILE_SIZE + ((y) / TILE_SIZE) * 5) % NUM_ENTRIES)
  69. /**
  70. * Is the tile at (x,y) in cleared state?
  71. */
  72. static INLINE uint
  73. is_clear_flag_set(const uint *bitvec, int x, int y)
  74. {
  75. int pos, bit;
  76. x /= TILE_SIZE;
  77. y /= TILE_SIZE;
  78. pos = y * (MAX_WIDTH / TILE_SIZE) + x;
  79. assert(pos / 32 < (MAX_WIDTH / TILE_SIZE) * (MAX_HEIGHT / TILE_SIZE) / 32);
  80. bit = bitvec[pos / 32] & (1 << (pos & 31));
  81. return bit;
  82. }
  83. /**
  84. * Mark the tile at (x,y) as not cleared.
  85. */
  86. static INLINE void
  87. clear_clear_flag(uint *bitvec, int x, int y)
  88. {
  89. int pos;
  90. x /= TILE_SIZE;
  91. y /= TILE_SIZE;
  92. pos = y * (MAX_WIDTH / TILE_SIZE) + x;
  93. assert(pos / 32 < (MAX_WIDTH / TILE_SIZE) * (MAX_HEIGHT / TILE_SIZE) / 32);
  94. bitvec[pos / 32] &= ~(1 << (pos & 31));
  95. }
  96. struct softpipe_tile_cache *
  97. sp_create_tile_cache( struct pipe_screen *screen )
  98. {
  99. struct softpipe_tile_cache *tc;
  100. uint pos;
  101. tc = CALLOC_STRUCT( softpipe_tile_cache );
  102. if (tc) {
  103. tc->screen = screen;
  104. for (pos = 0; pos < NUM_ENTRIES; pos++) {
  105. tc->entries[pos].x =
  106. tc->entries[pos].y = -1;
  107. }
  108. }
  109. return tc;
  110. }
  111. void
  112. sp_destroy_tile_cache(struct softpipe_tile_cache *tc)
  113. {
  114. struct pipe_screen *screen;
  115. uint pos;
  116. for (pos = 0; pos < NUM_ENTRIES; pos++) {
  117. /*assert(tc->entries[pos].x < 0);*/
  118. }
  119. if (tc->transfer) {
  120. screen = tc->transfer->texture->screen;
  121. screen->tex_transfer_release(screen, &tc->transfer);
  122. }
  123. if (tc->tex_trans) {
  124. screen = tc->tex_trans->texture->screen;
  125. screen->tex_transfer_release(screen, &tc->tex_trans);
  126. }
  127. FREE( tc );
  128. }
  129. /**
  130. * Specify the surface to cache.
  131. */
  132. void
  133. sp_tile_cache_set_surface(struct softpipe_tile_cache *tc,
  134. struct pipe_surface *ps)
  135. {
  136. assert(!tc->texture);
  137. if (tc->transfer) {
  138. struct pipe_screen *screen = tc->transfer->texture->screen;
  139. if (ps == tc->surface)
  140. return;
  141. if (tc->transfer_map) {
  142. tc->screen->transfer_unmap(tc->screen, tc->transfer);
  143. tc->transfer_map = NULL;
  144. }
  145. screen->tex_transfer_release(screen, &tc->transfer);
  146. }
  147. tc->surface = ps;
  148. if (ps) {
  149. struct pipe_screen *screen = ps->texture->screen;
  150. tc->transfer = screen->get_tex_transfer(screen, ps->texture, ps->face,
  151. ps->level, ps->zslice,
  152. PIPE_TRANSFER_READ_WRITE,
  153. 0, 0, ps->width, ps->height);
  154. tc->depth_stencil = (ps->format == PIPE_FORMAT_S8Z24_UNORM ||
  155. ps->format == PIPE_FORMAT_X8Z24_UNORM ||
  156. ps->format == PIPE_FORMAT_Z24S8_UNORM ||
  157. ps->format == PIPE_FORMAT_Z24X8_UNORM ||
  158. ps->format == PIPE_FORMAT_Z16_UNORM ||
  159. ps->format == PIPE_FORMAT_Z32_UNORM ||
  160. ps->format == PIPE_FORMAT_S8_UNORM);
  161. }
  162. }
  163. /**
  164. * Return the transfer being cached.
  165. */
  166. struct pipe_surface *
  167. sp_tile_cache_get_surface(struct softpipe_tile_cache *tc)
  168. {
  169. return tc->surface;
  170. }
  171. void
  172. sp_tile_cache_map_transfers(struct softpipe_tile_cache *tc)
  173. {
  174. if (tc->transfer && !tc->transfer_map)
  175. tc->transfer_map = tc->screen->transfer_map(tc->screen, tc->transfer);
  176. if (tc->tex_trans && !tc->tex_trans_map)
  177. tc->tex_trans_map = tc->screen->transfer_map(tc->screen, tc->tex_trans);
  178. }
  179. void
  180. sp_tile_cache_unmap_transfers(struct softpipe_tile_cache *tc)
  181. {
  182. if (tc->transfer_map) {
  183. tc->screen->transfer_unmap(tc->screen, tc->transfer);
  184. tc->transfer_map = NULL;
  185. }
  186. if (tc->tex_trans_map) {
  187. tc->screen->transfer_unmap(tc->screen, tc->tex_trans);
  188. tc->tex_trans_map = NULL;
  189. }
  190. }
  191. /**
  192. * Specify the texture to cache.
  193. */
  194. void
  195. sp_tile_cache_set_texture(struct pipe_context *pipe,
  196. struct softpipe_tile_cache *tc,
  197. struct pipe_texture *texture)
  198. {
  199. uint i;
  200. assert(!tc->transfer);
  201. pipe_texture_reference(&tc->texture, texture);
  202. if (tc->transfer) {
  203. struct pipe_screen *screen = tc->transfer->texture->screen;
  204. if (tc->tex_trans_map) {
  205. tc->screen->transfer_unmap(tc->screen, tc->tex_trans);
  206. tc->tex_trans_map = NULL;
  207. }
  208. screen->tex_transfer_release(screen, &tc->tex_trans);
  209. }
  210. /* mark as entries as invalid/empty */
  211. /* XXX we should try to avoid this when the teximage hasn't changed */
  212. for (i = 0; i < NUM_ENTRIES; i++) {
  213. tc->entries[i].x = -1;
  214. }
  215. tc->tex_face = -1; /* any invalid value here */
  216. }
  217. /**
  218. * Set pixels in a tile to the given clear color/value, float.
  219. */
  220. static void
  221. clear_tile_rgba(struct softpipe_cached_tile *tile,
  222. enum pipe_format format,
  223. const float clear_value[4])
  224. {
  225. if (clear_value[0] == 0.0 &&
  226. clear_value[1] == 0.0 &&
  227. clear_value[2] == 0.0 &&
  228. clear_value[3] == 0.0) {
  229. memset(tile->data.color, 0, sizeof(tile->data.color));
  230. }
  231. else {
  232. uint i, j;
  233. for (i = 0; i < TILE_SIZE; i++) {
  234. for (j = 0; j < TILE_SIZE; j++) {
  235. tile->data.color[i][j][0] = clear_value[0];
  236. tile->data.color[i][j][1] = clear_value[1];
  237. tile->data.color[i][j][2] = clear_value[2];
  238. tile->data.color[i][j][3] = clear_value[3];
  239. }
  240. }
  241. }
  242. }
  243. /**
  244. * Set a tile to a solid value/color.
  245. */
  246. static void
  247. clear_tile(struct softpipe_cached_tile *tile,
  248. enum pipe_format format,
  249. uint clear_value)
  250. {
  251. uint i, j;
  252. switch (pf_get_size(format)) {
  253. case 1:
  254. memset(tile->data.any, 0, TILE_SIZE * TILE_SIZE);
  255. break;
  256. case 2:
  257. if (clear_value == 0) {
  258. memset(tile->data.any, 0, 2 * TILE_SIZE * TILE_SIZE);
  259. }
  260. else {
  261. for (i = 0; i < TILE_SIZE; i++) {
  262. for (j = 0; j < TILE_SIZE; j++) {
  263. tile->data.depth16[i][j] = (ushort) clear_value;
  264. }
  265. }
  266. }
  267. break;
  268. case 4:
  269. if (clear_value == 0) {
  270. memset(tile->data.any, 0, 4 * TILE_SIZE * TILE_SIZE);
  271. }
  272. else {
  273. for (i = 0; i < TILE_SIZE; i++) {
  274. for (j = 0; j < TILE_SIZE; j++) {
  275. tile->data.color32[i][j] = clear_value;
  276. }
  277. }
  278. }
  279. break;
  280. default:
  281. assert(0);
  282. }
  283. }
  284. /**
  285. * Actually clear the tiles which were flagged as being in a clear state.
  286. */
  287. static void
  288. sp_tile_cache_flush_clear(struct pipe_context *pipe,
  289. struct softpipe_tile_cache *tc)
  290. {
  291. struct pipe_transfer *pt = tc->transfer;
  292. const uint w = tc->transfer->width;
  293. const uint h = tc->transfer->height;
  294. uint x, y;
  295. uint numCleared = 0;
  296. /* clear the scratch tile to the clear value */
  297. clear_tile(&tc->tile, pt->format, tc->clear_val);
  298. /* push the tile to all positions marked as clear */
  299. for (y = 0; y < h; y += TILE_SIZE) {
  300. for (x = 0; x < w; x += TILE_SIZE) {
  301. if (is_clear_flag_set(tc->clear_flags, x, y)) {
  302. pipe_put_tile_raw(pt,
  303. x, y, TILE_SIZE, TILE_SIZE,
  304. tc->tile.data.color32, 0/*STRIDE*/);
  305. /* do this? */
  306. clear_clear_flag(tc->clear_flags, x, y);
  307. numCleared++;
  308. }
  309. }
  310. }
  311. #if 0
  312. debug_printf("num cleared: %u\n", numCleared);
  313. #endif
  314. }
  315. /**
  316. * Flush the tile cache: write all dirty tiles back to the transfer.
  317. * any tiles "flagged" as cleared will be "really" cleared.
  318. */
  319. void
  320. sp_flush_tile_cache(struct softpipe_context *softpipe,
  321. struct softpipe_tile_cache *tc)
  322. {
  323. struct pipe_transfer *pt = tc->transfer;
  324. int inuse = 0, pos;
  325. if (pt) {
  326. /* caching a drawing transfer */
  327. for (pos = 0; pos < NUM_ENTRIES; pos++) {
  328. struct softpipe_cached_tile *tile = tc->entries + pos;
  329. if (tile->x >= 0) {
  330. if (tc->depth_stencil) {
  331. pipe_put_tile_raw(pt,
  332. tile->x, tile->y, TILE_SIZE, TILE_SIZE,
  333. tile->data.depth32, 0/*STRIDE*/);
  334. }
  335. else {
  336. pipe_put_tile_rgba(pt,
  337. tile->x, tile->y, TILE_SIZE, TILE_SIZE,
  338. (float *) tile->data.color);
  339. }
  340. tile->x = tile->y = -1; /* mark as empty */
  341. inuse++;
  342. }
  343. }
  344. #if TILE_CLEAR_OPTIMIZATION
  345. sp_tile_cache_flush_clear(&softpipe->pipe, tc);
  346. #endif
  347. }
  348. else if (tc->texture) {
  349. /* caching a texture, mark all entries as empty */
  350. for (pos = 0; pos < NUM_ENTRIES; pos++) {
  351. tc->entries[pos].x = -1;
  352. }
  353. tc->tex_face = -1;
  354. }
  355. #if 0
  356. debug_printf("flushed tiles in use: %d\n", inuse);
  357. #endif
  358. }
  359. /**
  360. * Get a tile from the cache.
  361. * \param x, y position of tile, in pixels
  362. */
  363. struct softpipe_cached_tile *
  364. sp_get_cached_tile(struct softpipe_context *softpipe,
  365. struct softpipe_tile_cache *tc, int x, int y)
  366. {
  367. struct pipe_transfer *pt = tc->transfer;
  368. /* tile pos in framebuffer: */
  369. const int tile_x = x & ~(TILE_SIZE - 1);
  370. const int tile_y = y & ~(TILE_SIZE - 1);
  371. /* cache pos/entry: */
  372. const int pos = CACHE_POS(x, y);
  373. struct softpipe_cached_tile *tile = tc->entries + pos;
  374. if (tile_x != tile->x ||
  375. tile_y != tile->y) {
  376. if (tile->x != -1) {
  377. /* put dirty tile back in framebuffer */
  378. if (tc->depth_stencil) {
  379. pipe_put_tile_raw(pt,
  380. tile->x, tile->y, TILE_SIZE, TILE_SIZE,
  381. tile->data.depth32, 0/*STRIDE*/);
  382. }
  383. else {
  384. pipe_put_tile_rgba(pt,
  385. tile->x, tile->y, TILE_SIZE, TILE_SIZE,
  386. (float *) tile->data.color);
  387. }
  388. }
  389. tile->x = tile_x;
  390. tile->y = tile_y;
  391. if (is_clear_flag_set(tc->clear_flags, x, y)) {
  392. /* don't get tile from framebuffer, just clear it */
  393. if (tc->depth_stencil) {
  394. clear_tile(tile, pt->format, tc->clear_val);
  395. }
  396. else {
  397. clear_tile_rgba(tile, pt->format, tc->clear_color);
  398. }
  399. clear_clear_flag(tc->clear_flags, x, y);
  400. }
  401. else {
  402. /* get new tile data from transfer */
  403. if (tc->depth_stencil) {
  404. pipe_get_tile_raw(pt,
  405. tile->x, tile->y, TILE_SIZE, TILE_SIZE,
  406. tile->data.depth32, 0/*STRIDE*/);
  407. }
  408. else {
  409. pipe_get_tile_rgba(pt,
  410. tile->x, tile->y, TILE_SIZE, TILE_SIZE,
  411. (float *) tile->data.color);
  412. }
  413. }
  414. }
  415. return tile;
  416. }
  417. /**
  418. * Given the texture face, level, zslice, x and y values, compute
  419. * the cache entry position/index where we'd hope to find the
  420. * cached texture tile.
  421. * This is basically a direct-map cache.
  422. * XXX There's probably lots of ways in which we can improve this.
  423. */
  424. static INLINE uint
  425. tex_cache_pos(int x, int y, int z, int face, int level)
  426. {
  427. uint entry = x + y * 9 + z * 3 + face + level * 7;
  428. return entry % NUM_ENTRIES;
  429. }
  430. /**
  431. * Similar to sp_get_cached_tile() but for textures.
  432. * Tiles are read-only and indexed with more params.
  433. */
  434. const struct softpipe_cached_tile *
  435. sp_get_cached_tile_tex(struct softpipe_context *sp,
  436. struct softpipe_tile_cache *tc, int x, int y, int z,
  437. int face, int level)
  438. {
  439. struct pipe_screen *screen = sp->pipe.screen;
  440. /* tile pos in framebuffer: */
  441. const int tile_x = x & ~(TILE_SIZE - 1);
  442. const int tile_y = y & ~(TILE_SIZE - 1);
  443. /* cache pos/entry: */
  444. const uint pos = tex_cache_pos(x / TILE_SIZE, y / TILE_SIZE, z,
  445. face, level);
  446. struct softpipe_cached_tile *tile = tc->entries + pos;
  447. if (tc->texture) {
  448. struct softpipe_texture *spt = softpipe_texture(tc->texture);
  449. if (spt->modified) {
  450. /* texture was modified, invalidate all cached tiles */
  451. uint p;
  452. for (p = 0; p < NUM_ENTRIES; p++) {
  453. tile = tc->entries + p;
  454. tile->x = -1;
  455. }
  456. spt->modified = FALSE;
  457. }
  458. }
  459. if (tile_x != tile->x ||
  460. tile_y != tile->y ||
  461. z != tile->z ||
  462. face != tile->face ||
  463. level != tile->level) {
  464. /* cache miss */
  465. #if 0
  466. printf("miss at %u x=%d y=%d z=%d face=%d level=%d\n", pos,
  467. x/TILE_SIZE, y/TILE_SIZE, z, face, level);
  468. #endif
  469. /* check if we need to get a new transfer */
  470. if (!tc->tex_trans ||
  471. tc->tex_face != face ||
  472. tc->tex_level != level ||
  473. tc->tex_z != z) {
  474. /* get new transfer (view into texture) */
  475. if (tc->transfer) {
  476. if (tc->tex_trans_map)
  477. tc->screen->transfer_unmap(tc->screen, tc->tex_trans);
  478. screen->tex_transfer_release(screen, &tc->tex_trans);
  479. }
  480. tc->tex_trans = screen->get_tex_transfer(screen, tc->texture, face, level, z,
  481. PIPE_TRANSFER_READ, 0, 0,
  482. tc->texture->width[level],
  483. tc->texture->height[level]);
  484. tc->tex_trans_map = screen->transfer_map(screen, tc->tex_trans);
  485. tc->tex_face = face;
  486. tc->tex_level = level;
  487. tc->tex_z = z;
  488. }
  489. /* get tile from the transfer (view into texture) */
  490. pipe_get_tile_rgba(tc->tex_trans,
  491. tile_x, tile_y, TILE_SIZE, TILE_SIZE,
  492. (float *) tile->data.color);
  493. tile->x = tile_x;
  494. tile->y = tile_y;
  495. tile->z = z;
  496. tile->face = face;
  497. tile->level = level;
  498. }
  499. return tile;
  500. }
  501. /**
  502. * When a whole surface is being cleared to a value we can avoid
  503. * fetching tiles above.
  504. * Save the color and set a 'clearflag' for each tile of the screen.
  505. */
  506. void
  507. sp_tile_cache_clear(struct softpipe_tile_cache *tc, uint clearValue)
  508. {
  509. uint r, g, b, a;
  510. uint pos;
  511. tc->clear_val = clearValue;
  512. switch (tc->transfer->format) {
  513. case PIPE_FORMAT_R8G8B8A8_UNORM:
  514. r = (clearValue >> 24) & 0xff;
  515. g = (clearValue >> 16) & 0xff;
  516. b = (clearValue >> 8) & 0xff;
  517. a = (clearValue ) & 0xff;
  518. break;
  519. case PIPE_FORMAT_A8R8G8B8_UNORM:
  520. r = (clearValue >> 16) & 0xff;
  521. g = (clearValue >> 8) & 0xff;
  522. b = (clearValue ) & 0xff;
  523. a = (clearValue >> 24) & 0xff;
  524. break;
  525. case PIPE_FORMAT_B8G8R8A8_UNORM:
  526. r = (clearValue >> 8) & 0xff;
  527. g = (clearValue >> 16) & 0xff;
  528. b = (clearValue >> 24) & 0xff;
  529. a = (clearValue ) & 0xff;
  530. break;
  531. default:
  532. r = g = b = a = 0;
  533. }
  534. tc->clear_color[0] = r / 255.0f;
  535. tc->clear_color[1] = g / 255.0f;
  536. tc->clear_color[2] = b / 255.0f;
  537. tc->clear_color[3] = a / 255.0f;
  538. #if TILE_CLEAR_OPTIMIZATION
  539. /* set flags to indicate all the tiles are cleared */
  540. memset(tc->clear_flags, 255, sizeof(tc->clear_flags));
  541. #else
  542. /* disable the optimization */
  543. memset(tc->clear_flags, 0, sizeof(tc->clear_flags));
  544. #endif
  545. for (pos = 0; pos < NUM_ENTRIES; pos++) {
  546. struct softpipe_cached_tile *tile = tc->entries + pos;
  547. tile->x = tile->y = -1;
  548. }
  549. }