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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  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. * RGBA/float tile get/put functions.
  29. * Usable both by drivers and state trackers.
  30. */
  31. #include "pipe/p_defines.h"
  32. #include "pipe/p_inlines.h"
  33. #include "util/u_format.h"
  34. #include "util/u_math.h"
  35. #include "util/u_memory.h"
  36. #include "util/u_rect.h"
  37. #include "util/u_tile.h"
  38. /**
  39. * Move raw block of pixels from transfer object to user memory.
  40. */
  41. void
  42. pipe_get_tile_raw(struct pipe_transfer *pt,
  43. uint x, uint y, uint w, uint h,
  44. void *dst, int dst_stride)
  45. {
  46. struct pipe_screen *screen = pt->texture->screen;
  47. const void *src;
  48. if (dst_stride == 0)
  49. dst_stride = util_format_get_stride(pt->texture->format, w);
  50. if (pipe_clip_tile(x, y, &w, &h, pt))
  51. return;
  52. src = screen->transfer_map(screen, pt);
  53. assert(src);
  54. if(!src)
  55. return;
  56. util_copy_rect(dst, pt->texture->format, dst_stride, 0, 0, w, h, src, pt->stride, x, y);
  57. screen->transfer_unmap(screen, pt);
  58. }
  59. /**
  60. * Move raw block of pixels from user memory to transfer object.
  61. */
  62. void
  63. pipe_put_tile_raw(struct pipe_transfer *pt,
  64. uint x, uint y, uint w, uint h,
  65. const void *src, int src_stride)
  66. {
  67. struct pipe_screen *screen = pt->texture->screen;
  68. void *dst;
  69. enum pipe_format format = pt->texture->format;
  70. if (src_stride == 0)
  71. src_stride = util_format_get_stride(format, w);
  72. if (pipe_clip_tile(x, y, &w, &h, pt))
  73. return;
  74. dst = screen->transfer_map(screen, pt);
  75. assert(dst);
  76. if(!dst)
  77. return;
  78. util_copy_rect(dst, format, pt->stride, x, y, w, h, src, src_stride, 0, 0);
  79. screen->transfer_unmap(screen, pt);
  80. }
  81. /** Convert short in [-32768,32767] to GLfloat in [-1.0,1.0] */
  82. #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
  83. #define UNCLAMPED_FLOAT_TO_SHORT(us, f) \
  84. us = ( (short) ( CLAMP((f), -1.0, 1.0) * 32767.0F) )
  85. /*** PIPE_FORMAT_A8R8G8B8_UNORM ***/
  86. static void
  87. a8r8g8b8_get_tile_rgba(const unsigned *src,
  88. unsigned w, unsigned h,
  89. float *p,
  90. unsigned dst_stride)
  91. {
  92. unsigned i, j;
  93. for (i = 0; i < h; i++) {
  94. float *pRow = p;
  95. for (j = 0; j < w; j++, pRow += 4) {
  96. const unsigned pixel = *src++;
  97. pRow[0] = ubyte_to_float((pixel >> 16) & 0xff);
  98. pRow[1] = ubyte_to_float((pixel >> 8) & 0xff);
  99. pRow[2] = ubyte_to_float((pixel >> 0) & 0xff);
  100. pRow[3] = ubyte_to_float((pixel >> 24) & 0xff);
  101. }
  102. p += dst_stride;
  103. }
  104. }
  105. static void
  106. a8r8g8b8_put_tile_rgba(unsigned *dst,
  107. unsigned w, unsigned h,
  108. const float *p,
  109. unsigned src_stride)
  110. {
  111. unsigned i, j;
  112. for (i = 0; i < h; i++) {
  113. const float *pRow = p;
  114. for (j = 0; j < w; j++, pRow += 4) {
  115. unsigned r, g, b, a;
  116. r = float_to_ubyte(pRow[0]);
  117. g = float_to_ubyte(pRow[1]);
  118. b = float_to_ubyte(pRow[2]);
  119. a = float_to_ubyte(pRow[3]);
  120. *dst++ = (a << 24) | (r << 16) | (g << 8) | b;
  121. }
  122. p += src_stride;
  123. }
  124. }
  125. /*** PIPE_FORMAT_X8R8G8B8_UNORM ***/
  126. static void
  127. x8r8g8b8_get_tile_rgba(const unsigned *src,
  128. unsigned w, unsigned h,
  129. float *p,
  130. unsigned dst_stride)
  131. {
  132. unsigned i, j;
  133. for (i = 0; i < h; i++) {
  134. float *pRow = p;
  135. for (j = 0; j < w; j++, pRow += 4) {
  136. const unsigned pixel = *src++;
  137. pRow[0] = ubyte_to_float((pixel >> 16) & 0xff);
  138. pRow[1] = ubyte_to_float((pixel >> 8) & 0xff);
  139. pRow[2] = ubyte_to_float((pixel >> 0) & 0xff);
  140. pRow[3] = 1.0F;
  141. }
  142. p += dst_stride;
  143. }
  144. }
  145. static void
  146. x8r8g8b8_put_tile_rgba(unsigned *dst,
  147. unsigned w, unsigned h,
  148. const float *p,
  149. unsigned src_stride)
  150. {
  151. unsigned i, j;
  152. for (i = 0; i < h; i++) {
  153. const float *pRow = p;
  154. for (j = 0; j < w; j++, pRow += 4) {
  155. unsigned r, g, b;
  156. r = float_to_ubyte(pRow[0]);
  157. g = float_to_ubyte(pRow[1]);
  158. b = float_to_ubyte(pRow[2]);
  159. *dst++ = (0xff << 24) | (r << 16) | (g << 8) | b;
  160. }
  161. p += src_stride;
  162. }
  163. }
  164. /*** PIPE_FORMAT_B8G8R8A8_UNORM ***/
  165. static void
  166. b8g8r8a8_get_tile_rgba(const unsigned *src,
  167. unsigned w, unsigned h,
  168. float *p,
  169. unsigned dst_stride)
  170. {
  171. unsigned i, j;
  172. for (i = 0; i < h; i++) {
  173. float *pRow = p;
  174. for (j = 0; j < w; j++, pRow += 4) {
  175. const unsigned pixel = *src++;
  176. pRow[0] = ubyte_to_float((pixel >> 8) & 0xff);
  177. pRow[1] = ubyte_to_float((pixel >> 16) & 0xff);
  178. pRow[2] = ubyte_to_float((pixel >> 24) & 0xff);
  179. pRow[3] = ubyte_to_float((pixel >> 0) & 0xff);
  180. }
  181. p += dst_stride;
  182. }
  183. }
  184. static void
  185. b8g8r8a8_put_tile_rgba(unsigned *dst,
  186. unsigned w, unsigned h,
  187. const float *p,
  188. unsigned src_stride)
  189. {
  190. unsigned i, j;
  191. for (i = 0; i < h; i++) {
  192. const float *pRow = p;
  193. for (j = 0; j < w; j++, pRow += 4) {
  194. unsigned r, g, b, a;
  195. r = float_to_ubyte(pRow[0]);
  196. g = float_to_ubyte(pRow[1]);
  197. b = float_to_ubyte(pRow[2]);
  198. a = float_to_ubyte(pRow[3]);
  199. *dst++ = (b << 24) | (g << 16) | (r << 8) | a;
  200. }
  201. p += src_stride;
  202. }
  203. }
  204. /*** PIPE_FORMAT_R8G8B8A8_UNORM ***/
  205. static void
  206. r8g8b8a8_get_tile_rgba(const unsigned *src,
  207. unsigned w, unsigned h,
  208. float *p,
  209. unsigned dst_stride)
  210. {
  211. unsigned i, j;
  212. for (i = 0; i < h; i++) {
  213. float *pRow = p;
  214. for (j = 0; j < w; j++, pRow += 4) {
  215. const unsigned pixel = *src++;
  216. pRow[0] = ubyte_to_float((pixel >> 24) & 0xff);
  217. pRow[1] = ubyte_to_float((pixel >> 16) & 0xff);
  218. pRow[2] = ubyte_to_float((pixel >> 8) & 0xff);
  219. pRow[3] = ubyte_to_float((pixel >> 0) & 0xff);
  220. }
  221. p += dst_stride;
  222. }
  223. }
  224. static void
  225. r8g8b8a8_put_tile_rgba(unsigned *dst,
  226. unsigned w, unsigned h,
  227. const float *p,
  228. unsigned src_stride)
  229. {
  230. unsigned i, j;
  231. for (i = 0; i < h; i++) {
  232. const float *pRow = p;
  233. for (j = 0; j < w; j++, pRow += 4) {
  234. unsigned r, g, b, a;
  235. r = float_to_ubyte(pRow[0]);
  236. g = float_to_ubyte(pRow[1]);
  237. b = float_to_ubyte(pRow[2]);
  238. a = float_to_ubyte(pRow[3]);
  239. *dst++ = (r << 24) | (g << 16) | (b << 8) | a;
  240. }
  241. p += src_stride;
  242. }
  243. }
  244. /*** PIPE_FORMAT_A1R5G5B5_UNORM ***/
  245. static void
  246. a1r5g5b5_get_tile_rgba(const ushort *src,
  247. unsigned w, unsigned h,
  248. float *p,
  249. unsigned dst_stride)
  250. {
  251. unsigned i, j;
  252. for (i = 0; i < h; i++) {
  253. float *pRow = p;
  254. for (j = 0; j < w; j++, pRow += 4) {
  255. const ushort pixel = *src++;
  256. pRow[0] = ((pixel >> 10) & 0x1f) * (1.0f / 31.0f);
  257. pRow[1] = ((pixel >> 5) & 0x1f) * (1.0f / 31.0f);
  258. pRow[2] = ((pixel ) & 0x1f) * (1.0f / 31.0f);
  259. pRow[3] = ((pixel >> 15) ) * 1.0f;
  260. }
  261. p += dst_stride;
  262. }
  263. }
  264. static void
  265. a1r5g5b5_put_tile_rgba(ushort *dst,
  266. unsigned w, unsigned h,
  267. const float *p,
  268. unsigned src_stride)
  269. {
  270. unsigned i, j;
  271. for (i = 0; i < h; i++) {
  272. const float *pRow = p;
  273. for (j = 0; j < w; j++, pRow += 4) {
  274. unsigned r, g, b, a;
  275. r = float_to_ubyte(pRow[0]);
  276. g = float_to_ubyte(pRow[1]);
  277. b = float_to_ubyte(pRow[2]);
  278. a = float_to_ubyte(pRow[3]);
  279. r = r >> 3; /* 5 bits */
  280. g = g >> 3; /* 5 bits */
  281. b = b >> 3; /* 5 bits */
  282. a = a >> 7; /* 1 bit */
  283. *dst++ = (a << 15) | (r << 10) | (g << 5) | b;
  284. }
  285. p += src_stride;
  286. }
  287. }
  288. /*** PIPE_FORMAT_A4R4G4B4_UNORM ***/
  289. static void
  290. a4r4g4b4_get_tile_rgba(const ushort *src,
  291. unsigned w, unsigned h,
  292. float *p,
  293. unsigned dst_stride)
  294. {
  295. unsigned i, j;
  296. for (i = 0; i < h; i++) {
  297. float *pRow = p;
  298. for (j = 0; j < w; j++, pRow += 4) {
  299. const ushort pixel = *src++;
  300. pRow[0] = ((pixel >> 8) & 0xf) * (1.0f / 15.0f);
  301. pRow[1] = ((pixel >> 4) & 0xf) * (1.0f / 15.0f);
  302. pRow[2] = ((pixel ) & 0xf) * (1.0f / 15.0f);
  303. pRow[3] = ((pixel >> 12) ) * (1.0f / 15.0f);
  304. }
  305. p += dst_stride;
  306. }
  307. }
  308. static void
  309. a4r4g4b4_put_tile_rgba(ushort *dst,
  310. unsigned w, unsigned h,
  311. const float *p,
  312. unsigned src_stride)
  313. {
  314. unsigned i, j;
  315. for (i = 0; i < h; i++) {
  316. const float *pRow = p;
  317. for (j = 0; j < w; j++, pRow += 4) {
  318. unsigned r, g, b, a;
  319. r = float_to_ubyte(pRow[0]);
  320. g = float_to_ubyte(pRow[1]);
  321. b = float_to_ubyte(pRow[2]);
  322. a = float_to_ubyte(pRow[3]);
  323. r >>= 4;
  324. g >>= 4;
  325. b >>= 4;
  326. a >>= 4;
  327. *dst++ = (a << 12) | (r << 16) | (g << 4) | b;
  328. }
  329. p += src_stride;
  330. }
  331. }
  332. /*** PIPE_FORMAT_R5G6B5_UNORM ***/
  333. static void
  334. r5g6b5_get_tile_rgba(const ushort *src,
  335. unsigned w, unsigned h,
  336. float *p,
  337. unsigned dst_stride)
  338. {
  339. unsigned i, j;
  340. for (i = 0; i < h; i++) {
  341. float *pRow = p;
  342. for (j = 0; j < w; j++, pRow += 4) {
  343. const ushort pixel = *src++;
  344. pRow[0] = ((pixel >> 11) & 0x1f) * (1.0f / 31.0f);
  345. pRow[1] = ((pixel >> 5) & 0x3f) * (1.0f / 63.0f);
  346. pRow[2] = ((pixel ) & 0x1f) * (1.0f / 31.0f);
  347. pRow[3] = 1.0f;
  348. }
  349. p += dst_stride;
  350. }
  351. }
  352. static void
  353. r5g6b5_put_tile_rgba(ushort *dst,
  354. unsigned w, unsigned h,
  355. const float *p,
  356. unsigned src_stride)
  357. {
  358. unsigned i, j;
  359. for (i = 0; i < h; i++) {
  360. const float *pRow = p;
  361. for (j = 0; j < w; j++, pRow += 4) {
  362. uint r = (uint) (CLAMP(pRow[0], 0.0, 1.0) * 31.0);
  363. uint g = (uint) (CLAMP(pRow[1], 0.0, 1.0) * 63.0);
  364. uint b = (uint) (CLAMP(pRow[2], 0.0, 1.0) * 31.0);
  365. *dst++ = (r << 11) | (g << 5) | (b);
  366. }
  367. p += src_stride;
  368. }
  369. }
  370. /*** PIPE_FORMAT_R8G8B8_UNORM ***/
  371. static void
  372. r8g8b8_get_tile_rgba(const ubyte *src,
  373. unsigned w, unsigned h,
  374. float *p,
  375. unsigned dst_stride)
  376. {
  377. unsigned i, j;
  378. for (i = 0; i < h; i++) {
  379. float *pRow = p;
  380. for (j = 0; j < w; j++, pRow += 4) {
  381. pRow[0] = ubyte_to_float(src[0]);
  382. pRow[1] = ubyte_to_float(src[1]);
  383. pRow[2] = ubyte_to_float(src[2]);
  384. pRow[3] = 1.0f;
  385. src += 3;
  386. }
  387. p += dst_stride;
  388. }
  389. }
  390. static void
  391. r8g8b8_put_tile_rgba(ubyte *dst,
  392. unsigned w, unsigned h,
  393. const float *p,
  394. unsigned src_stride)
  395. {
  396. unsigned i, j;
  397. for (i = 0; i < h; i++) {
  398. const float *pRow = p;
  399. for (j = 0; j < w; j++, pRow += 4) {
  400. dst[0] = float_to_ubyte(pRow[0]);
  401. dst[1] = float_to_ubyte(pRow[1]);
  402. dst[2] = float_to_ubyte(pRow[2]);
  403. dst += 3;
  404. }
  405. p += src_stride;
  406. }
  407. }
  408. /*** PIPE_FORMAT_Z16_UNORM ***/
  409. /**
  410. * Return each Z value as four floats in [0,1].
  411. */
  412. static void
  413. z16_get_tile_rgba(const ushort *src,
  414. unsigned w, unsigned h,
  415. float *p,
  416. unsigned dst_stride)
  417. {
  418. const float scale = 1.0f / 65535.0f;
  419. unsigned i, j;
  420. for (i = 0; i < h; i++) {
  421. float *pRow = p;
  422. for (j = 0; j < w; j++, pRow += 4) {
  423. pRow[0] =
  424. pRow[1] =
  425. pRow[2] =
  426. pRow[3] = *src++ * scale;
  427. }
  428. p += dst_stride;
  429. }
  430. }
  431. /*** PIPE_FORMAT_L8_UNORM ***/
  432. static void
  433. l8_get_tile_rgba(const ubyte *src,
  434. unsigned w, unsigned h,
  435. float *p,
  436. unsigned dst_stride)
  437. {
  438. unsigned i, j;
  439. for (i = 0; i < h; i++) {
  440. float *pRow = p;
  441. for (j = 0; j < w; j++, src++, pRow += 4) {
  442. pRow[0] =
  443. pRow[1] =
  444. pRow[2] = ubyte_to_float(*src);
  445. pRow[3] = 1.0;
  446. }
  447. p += dst_stride;
  448. }
  449. }
  450. static void
  451. l8_put_tile_rgba(ubyte *dst,
  452. unsigned w, unsigned h,
  453. const float *p,
  454. unsigned src_stride)
  455. {
  456. unsigned i, j;
  457. for (i = 0; i < h; i++) {
  458. const float *pRow = p;
  459. for (j = 0; j < w; j++, pRow += 4) {
  460. unsigned r;
  461. r = float_to_ubyte(pRow[0]);
  462. *dst++ = (ubyte) r;
  463. }
  464. p += src_stride;
  465. }
  466. }
  467. /*** PIPE_FORMAT_A8_UNORM ***/
  468. static void
  469. a8_get_tile_rgba(const ubyte *src,
  470. unsigned w, unsigned h,
  471. float *p,
  472. unsigned dst_stride)
  473. {
  474. unsigned i, j;
  475. for (i = 0; i < h; i++) {
  476. float *pRow = p;
  477. for (j = 0; j < w; j++, src++, pRow += 4) {
  478. pRow[0] =
  479. pRow[1] =
  480. pRow[2] = 0.0;
  481. pRow[3] = ubyte_to_float(*src);
  482. }
  483. p += dst_stride;
  484. }
  485. }
  486. static void
  487. a8_put_tile_rgba(ubyte *dst,
  488. unsigned w, unsigned h,
  489. const float *p,
  490. unsigned src_stride)
  491. {
  492. unsigned i, j;
  493. for (i = 0; i < h; i++) {
  494. const float *pRow = p;
  495. for (j = 0; j < w; j++, pRow += 4) {
  496. unsigned a;
  497. a = float_to_ubyte(pRow[3]);
  498. *dst++ = (ubyte) a;
  499. }
  500. p += src_stride;
  501. }
  502. }
  503. /*** PIPE_FORMAT_R16_SNORM ***/
  504. static void
  505. r16_get_tile_rgba(const short *src,
  506. unsigned w, unsigned h,
  507. float *p,
  508. unsigned dst_stride)
  509. {
  510. unsigned i, j;
  511. for (i = 0; i < h; i++) {
  512. float *pRow = p;
  513. for (j = 0; j < w; j++, src++, pRow += 4) {
  514. pRow[0] = SHORT_TO_FLOAT(src[0]);
  515. pRow[1] =
  516. pRow[2] = 0.0;
  517. pRow[3] = 1.0;
  518. }
  519. p += dst_stride;
  520. }
  521. }
  522. static void
  523. r16_put_tile_rgba(short *dst,
  524. unsigned w, unsigned h,
  525. const float *p,
  526. unsigned src_stride)
  527. {
  528. unsigned i, j;
  529. for (i = 0; i < h; i++) {
  530. const float *pRow = p;
  531. for (j = 0; j < w; j++, dst++, pRow += 4) {
  532. UNCLAMPED_FLOAT_TO_SHORT(dst[0], pRow[0]);
  533. }
  534. p += src_stride;
  535. }
  536. }
  537. /*** PIPE_FORMAT_R16G16B16A16_SNORM ***/
  538. static void
  539. r16g16b16a16_get_tile_rgba(const short *src,
  540. unsigned w, unsigned h,
  541. float *p,
  542. unsigned dst_stride)
  543. {
  544. unsigned i, j;
  545. for (i = 0; i < h; i++) {
  546. float *pRow = p;
  547. for (j = 0; j < w; j++, src += 4, pRow += 4) {
  548. pRow[0] = SHORT_TO_FLOAT(src[0]);
  549. pRow[1] = SHORT_TO_FLOAT(src[1]);
  550. pRow[2] = SHORT_TO_FLOAT(src[2]);
  551. pRow[3] = SHORT_TO_FLOAT(src[3]);
  552. }
  553. p += dst_stride;
  554. }
  555. }
  556. static void
  557. r16g16b16a16_put_tile_rgba(short *dst,
  558. unsigned w, unsigned h,
  559. const float *p,
  560. unsigned src_stride)
  561. {
  562. unsigned i, j;
  563. for (i = 0; i < h; i++) {
  564. const float *pRow = p;
  565. for (j = 0; j < w; j++, dst += 4, pRow += 4) {
  566. UNCLAMPED_FLOAT_TO_SHORT(dst[0], pRow[0]);
  567. UNCLAMPED_FLOAT_TO_SHORT(dst[1], pRow[1]);
  568. UNCLAMPED_FLOAT_TO_SHORT(dst[2], pRow[2]);
  569. UNCLAMPED_FLOAT_TO_SHORT(dst[3], pRow[3]);
  570. }
  571. p += src_stride;
  572. }
  573. }
  574. /*** PIPE_FORMAT_R8G8B8A8_SRGB ***/
  575. /**
  576. * Convert an 8-bit sRGB value from non-linear space to a
  577. * linear RGB value in [0, 1].
  578. * Implemented with a 256-entry lookup table.
  579. */
  580. static INLINE float
  581. srgb_to_linear(ubyte cs8)
  582. {
  583. static float table[256];
  584. static boolean tableReady = FALSE;
  585. if (!tableReady) {
  586. /* compute lookup table now */
  587. uint i;
  588. for (i = 0; i < 256; i++) {
  589. const float cs = ubyte_to_float(i);
  590. if (cs <= 0.04045) {
  591. table[i] = cs / 12.92f;
  592. }
  593. else {
  594. table[i] = (float) powf((cs + 0.055) / 1.055, 2.4);
  595. }
  596. }
  597. tableReady = TRUE;
  598. }
  599. return table[cs8];
  600. }
  601. /**
  602. * Convert linear float in [0,1] to an srgb ubyte value in [0,255].
  603. * XXX this hasn't been tested (render to srgb surface).
  604. * XXX this needs optimization.
  605. */
  606. static INLINE ubyte
  607. linear_to_srgb(float cl)
  608. {
  609. if (cl >= 1.0F)
  610. return 255;
  611. else if (cl >= 0.0031308F)
  612. return float_to_ubyte(1.055F * powf(cl, 0.41666F) - 0.055F);
  613. else if (cl > 0.0F)
  614. return float_to_ubyte(12.92F * cl);
  615. else
  616. return 0.0;
  617. }
  618. static void
  619. a8r8g8b8_srgb_get_tile_rgba(const unsigned *src,
  620. unsigned w, unsigned h,
  621. float *p,
  622. unsigned dst_stride)
  623. {
  624. unsigned i, j;
  625. for (i = 0; i < h; i++) {
  626. float *pRow = p;
  627. for (j = 0; j < w; j++, pRow += 4) {
  628. const unsigned pixel = *src++;
  629. pRow[0] = srgb_to_linear((pixel >> 16) & 0xff);
  630. pRow[1] = srgb_to_linear((pixel >> 8) & 0xff);
  631. pRow[2] = srgb_to_linear((pixel >> 0) & 0xff);
  632. pRow[3] = ubyte_to_float((pixel >> 24) & 0xff);
  633. }
  634. p += dst_stride;
  635. }
  636. }
  637. static void
  638. a8r8g8b8_srgb_put_tile_rgba(unsigned *dst,
  639. unsigned w, unsigned h,
  640. const float *p,
  641. unsigned src_stride)
  642. {
  643. unsigned i, j;
  644. for (i = 0; i < h; i++) {
  645. const float *pRow = p;
  646. for (j = 0; j < w; j++, pRow += 4) {
  647. unsigned r, g, b, a;
  648. r = linear_to_srgb(pRow[0]);
  649. g = linear_to_srgb(pRow[1]);
  650. b = linear_to_srgb(pRow[2]);
  651. a = float_to_ubyte(pRow[3]);
  652. *dst++ = (a << 24) | (r << 16) | (g << 8) | b;
  653. }
  654. p += src_stride;
  655. }
  656. }
  657. /*** PIPE_FORMAT_A8L8_SRGB ***/
  658. static void
  659. a8l8_srgb_get_tile_rgba(const ushort *src,
  660. unsigned w, unsigned h,
  661. float *p,
  662. unsigned dst_stride)
  663. {
  664. unsigned i, j;
  665. for (i = 0; i < h; i++) {
  666. float *pRow = p;
  667. for (j = 0; j < w; j++, pRow += 4) {
  668. ushort p = *src++;
  669. pRow[0] =
  670. pRow[1] =
  671. pRow[2] = srgb_to_linear(p & 0xff);
  672. pRow[3] = ubyte_to_float(p >> 8);
  673. }
  674. p += dst_stride;
  675. }
  676. }
  677. static void
  678. a8l8_srgb_put_tile_rgba(ushort *dst,
  679. unsigned w, unsigned h,
  680. const float *p,
  681. unsigned src_stride)
  682. {
  683. unsigned i, j;
  684. for (i = 0; i < h; i++) {
  685. const float *pRow = p;
  686. for (j = 0; j < w; j++, pRow += 4) {
  687. unsigned r, a;
  688. r = linear_to_srgb(pRow[0]);
  689. a = float_to_ubyte(pRow[3]);
  690. *dst++ = (a << 8) | r;
  691. }
  692. p += src_stride;
  693. }
  694. }
  695. /*** PIPE_FORMAT_L8_SRGB ***/
  696. static void
  697. l8_srgb_get_tile_rgba(const ubyte *src,
  698. unsigned w, unsigned h,
  699. float *p,
  700. unsigned dst_stride)
  701. {
  702. unsigned i, j;
  703. for (i = 0; i < h; i++) {
  704. float *pRow = p;
  705. for (j = 0; j < w; j++, src++, pRow += 4) {
  706. pRow[0] =
  707. pRow[1] =
  708. pRow[2] = srgb_to_linear(*src);
  709. pRow[3] = 1.0;
  710. }
  711. p += dst_stride;
  712. }
  713. }
  714. static void
  715. l8_srgb_put_tile_rgba(ubyte *dst,
  716. unsigned w, unsigned h,
  717. const float *p,
  718. unsigned src_stride)
  719. {
  720. unsigned i, j;
  721. for (i = 0; i < h; i++) {
  722. const float *pRow = p;
  723. for (j = 0; j < w; j++, pRow += 4) {
  724. unsigned r;
  725. r = linear_to_srgb(pRow[0]);
  726. *dst++ = (ubyte) r;
  727. }
  728. p += src_stride;
  729. }
  730. }
  731. /*** PIPE_FORMAT_I8_UNORM ***/
  732. static void
  733. i8_get_tile_rgba(const ubyte *src,
  734. unsigned w, unsigned h,
  735. float *p,
  736. unsigned dst_stride)
  737. {
  738. unsigned i, j;
  739. for (i = 0; i < h; i++) {
  740. float *pRow = p;
  741. for (j = 0; j < w; j++, src++, pRow += 4) {
  742. pRow[0] =
  743. pRow[1] =
  744. pRow[2] =
  745. pRow[3] = ubyte_to_float(*src);
  746. }
  747. p += dst_stride;
  748. }
  749. }
  750. static void
  751. i8_put_tile_rgba(ubyte *dst,
  752. unsigned w, unsigned h,
  753. const float *p,
  754. unsigned src_stride)
  755. {
  756. unsigned i, j;
  757. for (i = 0; i < h; i++) {
  758. const float *pRow = p;
  759. for (j = 0; j < w; j++, pRow += 4) {
  760. unsigned r;
  761. r = float_to_ubyte(pRow[0]);
  762. *dst++ = (ubyte) r;
  763. }
  764. p += src_stride;
  765. }
  766. }
  767. /*** PIPE_FORMAT_A8L8_UNORM ***/
  768. static void
  769. a8l8_get_tile_rgba(const ushort *src,
  770. unsigned w, unsigned h,
  771. float *p,
  772. unsigned dst_stride)
  773. {
  774. unsigned i, j;
  775. for (i = 0; i < h; i++) {
  776. float *pRow = p;
  777. for (j = 0; j < w; j++, pRow += 4) {
  778. ushort p = *src++;
  779. pRow[0] =
  780. pRow[1] =
  781. pRow[2] = ubyte_to_float(p & 0xff);
  782. pRow[3] = ubyte_to_float(p >> 8);
  783. }
  784. p += dst_stride;
  785. }
  786. }
  787. static void
  788. a8l8_put_tile_rgba(ushort *dst,
  789. unsigned w, unsigned h,
  790. const float *p,
  791. unsigned src_stride)
  792. {
  793. unsigned i, j;
  794. for (i = 0; i < h; i++) {
  795. const float *pRow = p;
  796. for (j = 0; j < w; j++, pRow += 4) {
  797. unsigned r, a;
  798. r = float_to_ubyte(pRow[0]);
  799. a = float_to_ubyte(pRow[3]);
  800. *dst++ = (a << 8) | r;
  801. }
  802. p += src_stride;
  803. }
  804. }
  805. /*** PIPE_FORMAT_Z32_UNORM ***/
  806. /**
  807. * Return each Z value as four floats in [0,1].
  808. */
  809. static void
  810. z32_get_tile_rgba(const unsigned *src,
  811. unsigned w, unsigned h,
  812. float *p,
  813. unsigned dst_stride)
  814. {
  815. const double scale = 1.0 / (double) 0xffffffff;
  816. unsigned i, j;
  817. for (i = 0; i < h; i++) {
  818. float *pRow = p;
  819. for (j = 0; j < w; j++, pRow += 4) {
  820. pRow[0] =
  821. pRow[1] =
  822. pRow[2] =
  823. pRow[3] = (float) (*src++ * scale);
  824. }
  825. p += dst_stride;
  826. }
  827. }
  828. /*** PIPE_FORMAT_S8Z24_UNORM ***/
  829. /**
  830. * Return Z component as four float in [0,1]. Stencil part ignored.
  831. */
  832. static void
  833. s8z24_get_tile_rgba(const unsigned *src,
  834. unsigned w, unsigned h,
  835. float *p,
  836. unsigned dst_stride)
  837. {
  838. const double scale = 1.0 / ((1 << 24) - 1);
  839. unsigned i, j;
  840. for (i = 0; i < h; i++) {
  841. float *pRow = p;
  842. for (j = 0; j < w; j++, pRow += 4) {
  843. pRow[0] =
  844. pRow[1] =
  845. pRow[2] =
  846. pRow[3] = (float) (scale * (*src++ & 0xffffff));
  847. }
  848. p += dst_stride;
  849. }
  850. }
  851. /*** PIPE_FORMAT_Z24S8_UNORM ***/
  852. /**
  853. * Return Z component as four float in [0,1]. Stencil part ignored.
  854. */
  855. static void
  856. z24s8_get_tile_rgba(const unsigned *src,
  857. unsigned w, unsigned h,
  858. float *p,
  859. unsigned dst_stride)
  860. {
  861. const double scale = 1.0 / ((1 << 24) - 1);
  862. unsigned i, j;
  863. for (i = 0; i < h; i++) {
  864. float *pRow = p;
  865. for (j = 0; j < w; j++, pRow += 4) {
  866. pRow[0] =
  867. pRow[1] =
  868. pRow[2] =
  869. pRow[3] = (float) (scale * (*src++ >> 8));
  870. }
  871. p += dst_stride;
  872. }
  873. }
  874. /*** PIPE_FORMAT_Z32_FLOAT ***/
  875. /**
  876. * Return each Z value as four floats in [0,1].
  877. */
  878. static void
  879. z32f_get_tile_rgba(const float *src,
  880. unsigned w, unsigned h,
  881. float *p,
  882. unsigned dst_stride)
  883. {
  884. unsigned i, j;
  885. for (i = 0; i < h; i++) {
  886. float *pRow = p;
  887. for (j = 0; j < w; j++, pRow += 4) {
  888. pRow[0] =
  889. pRow[1] =
  890. pRow[2] =
  891. pRow[3] = *src++;
  892. }
  893. p += dst_stride;
  894. }
  895. }
  896. /*** PIPE_FORMAT_YCBCR / PIPE_FORMAT_YCBCR_REV ***/
  897. /**
  898. * Convert YCbCr (or YCrCb) to RGBA.
  899. */
  900. static void
  901. ycbcr_get_tile_rgba(const ushort *src,
  902. unsigned w, unsigned h,
  903. float *p,
  904. unsigned dst_stride,
  905. boolean rev)
  906. {
  907. const float scale = 1.0f / 255.0f;
  908. unsigned i, j;
  909. for (i = 0; i < h; i++) {
  910. float *pRow = p;
  911. /* do two texels at a time */
  912. for (j = 0; j < (w & ~1); j += 2, src += 2) {
  913. const ushort t0 = src[0];
  914. const ushort t1 = src[1];
  915. const ubyte y0 = (t0 >> 8) & 0xff; /* luminance */
  916. const ubyte y1 = (t1 >> 8) & 0xff; /* luminance */
  917. ubyte cb, cr;
  918. float r, g, b;
  919. if (rev) {
  920. cb = t1 & 0xff; /* chroma U */
  921. cr = t0 & 0xff; /* chroma V */
  922. }
  923. else {
  924. cb = t0 & 0xff; /* chroma U */
  925. cr = t1 & 0xff; /* chroma V */
  926. }
  927. /* even pixel: y0,cr,cb */
  928. r = 1.164f * (y0-16) + 1.596f * (cr-128);
  929. g = 1.164f * (y0-16) - 0.813f * (cr-128) - 0.391f * (cb-128);
  930. b = 1.164f * (y0-16) + 2.018f * (cb-128);
  931. pRow[0] = r * scale;
  932. pRow[1] = g * scale;
  933. pRow[2] = b * scale;
  934. pRow[3] = 1.0f;
  935. pRow += 4;
  936. /* odd pixel: use y1,cr,cb */
  937. r = 1.164f * (y1-16) + 1.596f * (cr-128);
  938. g = 1.164f * (y1-16) - 0.813f * (cr-128) - 0.391f * (cb-128);
  939. b = 1.164f * (y1-16) + 2.018f * (cb-128);
  940. pRow[0] = r * scale;
  941. pRow[1] = g * scale;
  942. pRow[2] = b * scale;
  943. pRow[3] = 1.0f;
  944. pRow += 4;
  945. }
  946. /* do the last texel */
  947. if (w & 1) {
  948. const ushort t0 = src[0];
  949. const ushort t1 = src[1];
  950. const ubyte y0 = (t0 >> 8) & 0xff; /* luminance */
  951. ubyte cb, cr;
  952. float r, g, b;
  953. if (rev) {
  954. cb = t1 & 0xff; /* chroma U */
  955. cr = t0 & 0xff; /* chroma V */
  956. }
  957. else {
  958. cb = t0 & 0xff; /* chroma U */
  959. cr = t1 & 0xff; /* chroma V */
  960. }
  961. /* even pixel: y0,cr,cb */
  962. r = 1.164f * (y0-16) + 1.596f * (cr-128);
  963. g = 1.164f * (y0-16) - 0.813f * (cr-128) - 0.391f * (cb-128);
  964. b = 1.164f * (y0-16) + 2.018f * (cb-128);
  965. pRow[0] = r * scale;
  966. pRow[1] = g * scale;
  967. pRow[2] = b * scale;
  968. pRow[3] = 1.0f;
  969. pRow += 4;
  970. }
  971. p += dst_stride;
  972. }
  973. }
  974. void
  975. pipe_tile_raw_to_rgba(enum pipe_format format,
  976. void *src,
  977. uint w, uint h,
  978. float *dst, unsigned dst_stride)
  979. {
  980. switch (format) {
  981. case PIPE_FORMAT_A8R8G8B8_UNORM:
  982. a8r8g8b8_get_tile_rgba((unsigned *) src, w, h, dst, dst_stride);
  983. break;
  984. case PIPE_FORMAT_X8R8G8B8_UNORM:
  985. x8r8g8b8_get_tile_rgba((unsigned *) src, w, h, dst, dst_stride);
  986. break;
  987. case PIPE_FORMAT_B8G8R8A8_UNORM:
  988. b8g8r8a8_get_tile_rgba((unsigned *) src, w, h, dst, dst_stride);
  989. break;
  990. case PIPE_FORMAT_R8G8B8A8_UNORM:
  991. r8g8b8a8_get_tile_rgba((unsigned *) src, w, h, dst, dst_stride);
  992. break;
  993. case PIPE_FORMAT_A1R5G5B5_UNORM:
  994. a1r5g5b5_get_tile_rgba((ushort *) src, w, h, dst, dst_stride);
  995. break;
  996. case PIPE_FORMAT_A4R4G4B4_UNORM:
  997. a4r4g4b4_get_tile_rgba((ushort *) src, w, h, dst, dst_stride);
  998. break;
  999. case PIPE_FORMAT_R5G6B5_UNORM:
  1000. r5g6b5_get_tile_rgba((ushort *) src, w, h, dst, dst_stride);
  1001. break;
  1002. case PIPE_FORMAT_R8G8B8_UNORM:
  1003. r8g8b8_get_tile_rgba((ubyte *) src, w, h, dst, dst_stride);
  1004. break;
  1005. case PIPE_FORMAT_L8_UNORM:
  1006. l8_get_tile_rgba((ubyte *) src, w, h, dst, dst_stride);
  1007. break;
  1008. case PIPE_FORMAT_A8_UNORM:
  1009. a8_get_tile_rgba((ubyte *) src, w, h, dst, dst_stride);
  1010. break;
  1011. case PIPE_FORMAT_I8_UNORM:
  1012. i8_get_tile_rgba((ubyte *) src, w, h, dst, dst_stride);
  1013. break;
  1014. case PIPE_FORMAT_A8L8_UNORM:
  1015. a8l8_get_tile_rgba((ushort *) src, w, h, dst, dst_stride);
  1016. break;
  1017. case PIPE_FORMAT_R16_SNORM:
  1018. r16_get_tile_rgba((short *) src, w, h, dst, dst_stride);
  1019. break;
  1020. case PIPE_FORMAT_R16G16B16A16_SNORM:
  1021. r16g16b16a16_get_tile_rgba((short *) src, w, h, dst, dst_stride);
  1022. break;
  1023. case PIPE_FORMAT_A8R8G8B8_SRGB:
  1024. a8r8g8b8_srgb_get_tile_rgba((unsigned *) src, w, h, dst, dst_stride);
  1025. break;
  1026. case PIPE_FORMAT_A8L8_SRGB:
  1027. a8l8_srgb_get_tile_rgba((ushort *) src, w, h, dst, dst_stride);
  1028. break;
  1029. case PIPE_FORMAT_L8_SRGB:
  1030. l8_srgb_get_tile_rgba((ubyte *) src, w, h, dst, dst_stride);
  1031. break;
  1032. case PIPE_FORMAT_Z16_UNORM:
  1033. z16_get_tile_rgba((ushort *) src, w, h, dst, dst_stride);
  1034. break;
  1035. case PIPE_FORMAT_Z32_UNORM:
  1036. z32_get_tile_rgba((unsigned *) src, w, h, dst, dst_stride);
  1037. break;
  1038. case PIPE_FORMAT_S8Z24_UNORM:
  1039. case PIPE_FORMAT_X8Z24_UNORM:
  1040. s8z24_get_tile_rgba((unsigned *) src, w, h, dst, dst_stride);
  1041. break;
  1042. case PIPE_FORMAT_Z24S8_UNORM:
  1043. case PIPE_FORMAT_Z24X8_UNORM:
  1044. z24s8_get_tile_rgba((unsigned *) src, w, h, dst, dst_stride);
  1045. break;
  1046. case PIPE_FORMAT_Z32_FLOAT:
  1047. z32f_get_tile_rgba((float *) src, w, h, dst, dst_stride);
  1048. break;
  1049. case PIPE_FORMAT_YCBCR:
  1050. ycbcr_get_tile_rgba((ushort *) src, w, h, dst, dst_stride, FALSE);
  1051. break;
  1052. case PIPE_FORMAT_YCBCR_REV:
  1053. ycbcr_get_tile_rgba((ushort *) src, w, h, dst, dst_stride, TRUE);
  1054. break;
  1055. default:
  1056. util_format_read_4f(format,
  1057. dst, dst_stride * sizeof(float),
  1058. src, util_format_get_stride(format, w),
  1059. 0, 0, w, h);
  1060. }
  1061. }
  1062. void
  1063. pipe_get_tile_rgba(struct pipe_transfer *pt,
  1064. uint x, uint y, uint w, uint h,
  1065. float *p)
  1066. {
  1067. unsigned dst_stride = w * 4;
  1068. void *packed;
  1069. enum pipe_format format = pt->texture->format;
  1070. if (pipe_clip_tile(x, y, &w, &h, pt))
  1071. return;
  1072. packed = MALLOC(util_format_get_nblocks(format, w, h) * util_format_get_blocksize(format));
  1073. if (!packed)
  1074. return;
  1075. if(format == PIPE_FORMAT_YCBCR || format == PIPE_FORMAT_YCBCR_REV)
  1076. assert((x & 1) == 0);
  1077. pipe_get_tile_raw(pt, x, y, w, h, packed, 0);
  1078. pipe_tile_raw_to_rgba(format, packed, w, h, p, dst_stride);
  1079. FREE(packed);
  1080. }
  1081. void
  1082. pipe_put_tile_rgba(struct pipe_transfer *pt,
  1083. uint x, uint y, uint w, uint h,
  1084. const float *p)
  1085. {
  1086. unsigned src_stride = w * 4;
  1087. void *packed;
  1088. enum pipe_format format = pt->texture->format;
  1089. if (pipe_clip_tile(x, y, &w, &h, pt))
  1090. return;
  1091. packed = MALLOC(util_format_get_nblocks(format, w, h) * util_format_get_blocksize(format));
  1092. if (!packed)
  1093. return;
  1094. switch (format) {
  1095. case PIPE_FORMAT_A8R8G8B8_UNORM:
  1096. a8r8g8b8_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);
  1097. break;
  1098. case PIPE_FORMAT_X8R8G8B8_UNORM:
  1099. x8r8g8b8_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);
  1100. break;
  1101. case PIPE_FORMAT_B8G8R8A8_UNORM:
  1102. b8g8r8a8_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);
  1103. break;
  1104. case PIPE_FORMAT_R8G8B8A8_UNORM:
  1105. r8g8b8a8_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);
  1106. break;
  1107. case PIPE_FORMAT_A1R5G5B5_UNORM:
  1108. a1r5g5b5_put_tile_rgba((ushort *) packed, w, h, p, src_stride);
  1109. break;
  1110. case PIPE_FORMAT_R5G6B5_UNORM:
  1111. r5g6b5_put_tile_rgba((ushort *) packed, w, h, p, src_stride);
  1112. break;
  1113. case PIPE_FORMAT_R8G8B8_UNORM:
  1114. r8g8b8_put_tile_rgba((ubyte *) packed, w, h, p, src_stride);
  1115. break;
  1116. case PIPE_FORMAT_A4R4G4B4_UNORM:
  1117. a4r4g4b4_put_tile_rgba((ushort *) packed, w, h, p, src_stride);
  1118. break;
  1119. case PIPE_FORMAT_L8_UNORM:
  1120. l8_put_tile_rgba((ubyte *) packed, w, h, p, src_stride);
  1121. break;
  1122. case PIPE_FORMAT_A8_UNORM:
  1123. a8_put_tile_rgba((ubyte *) packed, w, h, p, src_stride);
  1124. break;
  1125. case PIPE_FORMAT_I8_UNORM:
  1126. i8_put_tile_rgba((ubyte *) packed, w, h, p, src_stride);
  1127. break;
  1128. case PIPE_FORMAT_A8L8_UNORM:
  1129. a8l8_put_tile_rgba((ushort *) packed, w, h, p, src_stride);
  1130. break;
  1131. case PIPE_FORMAT_R16_SNORM:
  1132. r16_put_tile_rgba((short *) packed, w, h, p, src_stride);
  1133. break;
  1134. case PIPE_FORMAT_R16G16B16A16_SNORM:
  1135. r16g16b16a16_put_tile_rgba((short *) packed, w, h, p, src_stride);
  1136. break;
  1137. case PIPE_FORMAT_A8R8G8B8_SRGB:
  1138. a8r8g8b8_srgb_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);
  1139. break;
  1140. case PIPE_FORMAT_A8L8_SRGB:
  1141. a8l8_srgb_put_tile_rgba((ushort *) packed, w, h, p, src_stride);
  1142. break;
  1143. case PIPE_FORMAT_L8_SRGB:
  1144. l8_srgb_put_tile_rgba((ubyte *) packed, w, h, p, src_stride);
  1145. break;
  1146. case PIPE_FORMAT_Z16_UNORM:
  1147. /*z16_put_tile_rgba((ushort *) packed, w, h, p, src_stride);*/
  1148. break;
  1149. case PIPE_FORMAT_Z32_UNORM:
  1150. /*z32_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
  1151. break;
  1152. case PIPE_FORMAT_S8Z24_UNORM:
  1153. case PIPE_FORMAT_X8Z24_UNORM:
  1154. /*s8z24_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
  1155. break;
  1156. case PIPE_FORMAT_Z24S8_UNORM:
  1157. case PIPE_FORMAT_Z24X8_UNORM:
  1158. /*z24s8_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
  1159. break;
  1160. default:
  1161. util_format_write_4f(format,
  1162. p, src_stride * sizeof(float),
  1163. packed, util_format_get_stride(format, w),
  1164. 0, 0, w, h);
  1165. }
  1166. pipe_put_tile_raw(pt, x, y, w, h, packed, 0);
  1167. FREE(packed);
  1168. }
  1169. /**
  1170. * Get a block of Z values, converted to 32-bit range.
  1171. */
  1172. void
  1173. pipe_get_tile_z(struct pipe_transfer *pt,
  1174. uint x, uint y, uint w, uint h,
  1175. uint *z)
  1176. {
  1177. struct pipe_screen *screen = pt->texture->screen;
  1178. const uint dstStride = w;
  1179. ubyte *map;
  1180. uint *pDest = z;
  1181. uint i, j;
  1182. enum pipe_format format = pt->texture->format;
  1183. if (pipe_clip_tile(x, y, &w, &h, pt))
  1184. return;
  1185. map = (ubyte *)screen->transfer_map(screen, pt);
  1186. if (!map) {
  1187. assert(0);
  1188. return;
  1189. }
  1190. switch (format) {
  1191. case PIPE_FORMAT_Z32_UNORM:
  1192. {
  1193. const uint *ptrc
  1194. = (const uint *)(map + y * pt->stride + x*4);
  1195. for (i = 0; i < h; i++) {
  1196. memcpy(pDest, ptrc, 4 * w);
  1197. pDest += dstStride;
  1198. ptrc += pt->stride/4;
  1199. }
  1200. }
  1201. break;
  1202. case PIPE_FORMAT_S8Z24_UNORM:
  1203. case PIPE_FORMAT_X8Z24_UNORM:
  1204. {
  1205. const uint *ptrc
  1206. = (const uint *)(map + y * pt->stride + x*4);
  1207. for (i = 0; i < h; i++) {
  1208. for (j = 0; j < w; j++) {
  1209. /* convert 24-bit Z to 32-bit Z */
  1210. pDest[j] = (ptrc[j] << 8) | ((ptrc[j] >> 16) & 0xff);
  1211. }
  1212. pDest += dstStride;
  1213. ptrc += pt->stride/4;
  1214. }
  1215. }
  1216. break;
  1217. case PIPE_FORMAT_Z24S8_UNORM:
  1218. case PIPE_FORMAT_Z24X8_UNORM:
  1219. {
  1220. const uint *ptrc
  1221. = (const uint *)(map + y * pt->stride + x*4);
  1222. for (i = 0; i < h; i++) {
  1223. for (j = 0; j < w; j++) {
  1224. /* convert 24-bit Z to 32-bit Z */
  1225. pDest[j] = (ptrc[j] & 0xffffff00) | ((ptrc[j] >> 24) & 0xff);
  1226. }
  1227. pDest += dstStride;
  1228. ptrc += pt->stride/4;
  1229. }
  1230. }
  1231. break;
  1232. case PIPE_FORMAT_Z16_UNORM:
  1233. {
  1234. const ushort *ptrc
  1235. = (const ushort *)(map + y * pt->stride + x*2);
  1236. for (i = 0; i < h; i++) {
  1237. for (j = 0; j < w; j++) {
  1238. /* convert 16-bit Z to 32-bit Z */
  1239. pDest[j] = (ptrc[j] << 16) | ptrc[j];
  1240. }
  1241. pDest += dstStride;
  1242. ptrc += pt->stride/2;
  1243. }
  1244. }
  1245. break;
  1246. default:
  1247. assert(0);
  1248. }
  1249. screen->transfer_unmap(screen, pt);
  1250. }
  1251. void
  1252. pipe_put_tile_z(struct pipe_transfer *pt,
  1253. uint x, uint y, uint w, uint h,
  1254. const uint *zSrc)
  1255. {
  1256. struct pipe_screen *screen = pt->texture->screen;
  1257. const uint srcStride = w;
  1258. const uint *ptrc = zSrc;
  1259. ubyte *map;
  1260. uint i, j;
  1261. enum pipe_format format = pt->texture->format;
  1262. if (pipe_clip_tile(x, y, &w, &h, pt))
  1263. return;
  1264. map = (ubyte *)screen->transfer_map(screen, pt);
  1265. if (!map) {
  1266. assert(0);
  1267. return;
  1268. }
  1269. switch (format) {
  1270. case PIPE_FORMAT_Z32_UNORM:
  1271. {
  1272. uint *pDest = (uint *) (map + y * pt->stride + x*4);
  1273. for (i = 0; i < h; i++) {
  1274. memcpy(pDest, ptrc, 4 * w);
  1275. pDest += pt->stride/4;
  1276. ptrc += srcStride;
  1277. }
  1278. }
  1279. break;
  1280. case PIPE_FORMAT_S8Z24_UNORM:
  1281. {
  1282. uint *pDest = (uint *) (map + y * pt->stride + x*4);
  1283. assert((pt->usage & PIPE_TRANSFER_READ_WRITE) == PIPE_TRANSFER_READ_WRITE);
  1284. for (i = 0; i < h; i++) {
  1285. for (j = 0; j < w; j++) {
  1286. /* convert 32-bit Z to 24-bit Z, preserve stencil */
  1287. pDest[j] = (pDest[j] & 0xff000000) | ptrc[j] >> 8;
  1288. }
  1289. pDest += pt->stride/4;
  1290. ptrc += srcStride;
  1291. }
  1292. }
  1293. break;
  1294. case PIPE_FORMAT_X8Z24_UNORM:
  1295. {
  1296. uint *pDest = (uint *) (map + y * pt->stride + x*4);
  1297. for (i = 0; i < h; i++) {
  1298. for (j = 0; j < w; j++) {
  1299. /* convert 32-bit Z to 24-bit Z (0 stencil) */
  1300. pDest[j] = ptrc[j] >> 8;
  1301. }
  1302. pDest += pt->stride/4;
  1303. ptrc += srcStride;
  1304. }
  1305. }
  1306. break;
  1307. case PIPE_FORMAT_Z24S8_UNORM:
  1308. {
  1309. uint *pDest = (uint *) (map + y * pt->stride + x*4);
  1310. assert((pt->usage & PIPE_TRANSFER_READ_WRITE) == PIPE_TRANSFER_READ_WRITE);
  1311. for (i = 0; i < h; i++) {
  1312. for (j = 0; j < w; j++) {
  1313. /* convert 32-bit Z to 24-bit Z, preserve stencil */
  1314. pDest[j] = (pDest[j] & 0xff) | (ptrc[j] & 0xffffff00);
  1315. }
  1316. pDest += pt->stride/4;
  1317. ptrc += srcStride;
  1318. }
  1319. }
  1320. break;
  1321. case PIPE_FORMAT_Z24X8_UNORM:
  1322. {
  1323. uint *pDest = (uint *) (map + y * pt->stride + x*4);
  1324. for (i = 0; i < h; i++) {
  1325. for (j = 0; j < w; j++) {
  1326. /* convert 32-bit Z to 24-bit Z (0 stencil) */
  1327. pDest[j] = ptrc[j] & 0xffffff00;
  1328. }
  1329. pDest += pt->stride/4;
  1330. ptrc += srcStride;
  1331. }
  1332. }
  1333. break;
  1334. case PIPE_FORMAT_Z16_UNORM:
  1335. {
  1336. ushort *pDest = (ushort *) (map + y * pt->stride + x*2);
  1337. for (i = 0; i < h; i++) {
  1338. for (j = 0; j < w; j++) {
  1339. /* convert 32-bit Z to 16-bit Z */
  1340. pDest[j] = ptrc[j] >> 16;
  1341. }
  1342. pDest += pt->stride/2;
  1343. ptrc += srcStride;
  1344. }
  1345. }
  1346. break;
  1347. default:
  1348. assert(0);
  1349. }
  1350. screen->transfer_unmap(screen, pt);
  1351. }