Clone of mesa.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

lp_bld_interp.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /**************************************************************************
  2. *
  3. * Copyright 2009 VMware, Inc.
  4. * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  22. * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
  23. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  24. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  25. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. **************************************************************************/
  28. /**
  29. * @file
  30. * Position and shader input interpolation.
  31. *
  32. * @author Jose Fonseca <jfonseca@vmware.com>
  33. */
  34. #include "pipe/p_shader_tokens.h"
  35. #include "util/u_debug.h"
  36. #include "util/u_memory.h"
  37. #include "util/u_math.h"
  38. #include "tgsi/tgsi_parse.h"
  39. #include "lp_bld_debug.h"
  40. #include "lp_bld_const.h"
  41. #include "lp_bld_arit.h"
  42. #include "lp_bld_swizzle.h"
  43. #include "lp_bld_interp.h"
  44. /*
  45. * The shader JIT function operates on blocks of quads.
  46. * Each block has 2x2 quads and each quad has 2x2 pixels.
  47. *
  48. * We iterate over the quads in order 0, 1, 2, 3:
  49. *
  50. * #################
  51. * # | # | #
  52. * #---0---#---1---#
  53. * # | # | #
  54. * #################
  55. * # | # | #
  56. * #---2---#---3---#
  57. * # | # | #
  58. * #################
  59. *
  60. * Within each quad, we have four pixels which are represented in SOA
  61. * order:
  62. *
  63. * #########
  64. * # 0 | 1 #
  65. * #---+---#
  66. * # 2 | 3 #
  67. * #########
  68. *
  69. * So the green channel (for example) of the four pixels is stored in
  70. * a single vector register: {g0, g1, g2, g3}.
  71. */
  72. static void
  73. attrib_name(LLVMValueRef val, unsigned attrib, unsigned chan, const char *suffix)
  74. {
  75. if(attrib == 0)
  76. lp_build_name(val, "pos.%c%s", "xyzw"[chan], suffix);
  77. else
  78. lp_build_name(val, "input%u.%c%s", attrib - 1, "xyzw"[chan], suffix);
  79. }
  80. /**
  81. * Initialize the bld->a0, dadx, dady fields. This involves fetching
  82. * those values from the arrays which are passed into the JIT function.
  83. */
  84. static void
  85. coeffs_init(struct lp_build_interp_soa_context *bld,
  86. LLVMValueRef a0_ptr,
  87. LLVMValueRef dadx_ptr,
  88. LLVMValueRef dady_ptr)
  89. {
  90. LLVMBuilderRef builder = bld->base.builder;
  91. unsigned attrib;
  92. unsigned chan;
  93. for(attrib = 0; attrib < bld->num_attribs; ++attrib) {
  94. unsigned mask = bld->mask[attrib];
  95. unsigned mode = bld->mode[attrib];
  96. for(chan = 0; chan < NUM_CHANNELS; ++chan) {
  97. if(mask & (1 << chan)) {
  98. LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), attrib*NUM_CHANNELS + chan, 0);
  99. LLVMValueRef a0 = NULL;
  100. LLVMValueRef dadx = NULL;
  101. LLVMValueRef dady = NULL;
  102. switch( mode ) {
  103. case TGSI_INTERPOLATE_PERSPECTIVE:
  104. /* fall-through */
  105. case TGSI_INTERPOLATE_LINEAR:
  106. dadx = LLVMBuildLoad(builder, LLVMBuildGEP(builder, dadx_ptr, &index, 1, ""), "");
  107. dady = LLVMBuildLoad(builder, LLVMBuildGEP(builder, dady_ptr, &index, 1, ""), "");
  108. dadx = lp_build_broadcast_scalar(&bld->base, dadx);
  109. dady = lp_build_broadcast_scalar(&bld->base, dady);
  110. attrib_name(dadx, attrib, chan, ".dadx");
  111. attrib_name(dady, attrib, chan, ".dady");
  112. /* fall-through */
  113. case TGSI_INTERPOLATE_CONSTANT:
  114. a0 = LLVMBuildLoad(builder, LLVMBuildGEP(builder, a0_ptr, &index, 1, ""), "");
  115. a0 = lp_build_broadcast_scalar(&bld->base, a0);
  116. attrib_name(a0, attrib, chan, ".a0");
  117. break;
  118. default:
  119. assert(0);
  120. break;
  121. }
  122. bld->a0 [attrib][chan] = a0;
  123. bld->dadx[attrib][chan] = dadx;
  124. bld->dady[attrib][chan] = dady;
  125. }
  126. }
  127. }
  128. }
  129. /**
  130. * Emit LLVM code to compute the fragment shader input attribute values.
  131. * For example, for a color input, we'll compute red, green, blue and alpha
  132. * values for the four pixels in a quad.
  133. * Recall that we're operating on 4-element vectors so each arithmetic
  134. * operation is operating on the four pixels in a quad.
  135. */
  136. static void
  137. attribs_init(struct lp_build_interp_soa_context *bld)
  138. {
  139. LLVMValueRef x = bld->pos[0];
  140. LLVMValueRef y = bld->pos[1];
  141. LLVMValueRef oow = NULL;
  142. unsigned attrib;
  143. unsigned chan;
  144. for(attrib = 0; attrib < bld->num_attribs; ++attrib) {
  145. unsigned mask = bld->mask[attrib];
  146. unsigned mode = bld->mode[attrib];
  147. for(chan = 0; chan < NUM_CHANNELS; ++chan) {
  148. if(mask & (1 << chan)) {
  149. LLVMValueRef a0 = bld->a0 [attrib][chan];
  150. LLVMValueRef dadx = bld->dadx[attrib][chan];
  151. LLVMValueRef dady = bld->dady[attrib][chan];
  152. LLVMValueRef res;
  153. res = a0;
  154. if (mode != TGSI_INTERPOLATE_CONSTANT) {
  155. /* res = res + x * dadx */
  156. res = lp_build_add(&bld->base, res, lp_build_mul(&bld->base, x, dadx));
  157. /* res = res + y * dady */
  158. res = lp_build_add(&bld->base, res, lp_build_mul(&bld->base, y, dady));
  159. }
  160. /* Keep the value of the attribue before perspective divide for faster updates */
  161. bld->attribs_pre[attrib][chan] = res;
  162. if (mode == TGSI_INTERPOLATE_PERSPECTIVE) {
  163. LLVMValueRef w = bld->pos[3];
  164. assert(attrib != 0);
  165. if(!oow)
  166. oow = lp_build_rcp(&bld->base, w);
  167. res = lp_build_mul(&bld->base, res, oow);
  168. }
  169. attrib_name(res, attrib, chan, "");
  170. bld->attribs[attrib][chan] = res;
  171. }
  172. }
  173. }
  174. }
  175. /**
  176. * Increment the shader input attribute values.
  177. * This is called when we move from one quad to the next.
  178. */
  179. static void
  180. attribs_update(struct lp_build_interp_soa_context *bld, int quad_index)
  181. {
  182. LLVMValueRef oow = NULL;
  183. unsigned attrib;
  184. unsigned chan;
  185. assert(quad_index < 4);
  186. for(attrib = 0; attrib < bld->num_attribs; ++attrib) {
  187. unsigned mask = bld->mask[attrib];
  188. unsigned mode = bld->mode[attrib];
  189. if (mode != TGSI_INTERPOLATE_CONSTANT) {
  190. for(chan = 0; chan < NUM_CHANNELS; ++chan) {
  191. if(mask & (1 << chan)) {
  192. LLVMValueRef dadx = bld->dadx[attrib][chan];
  193. LLVMValueRef dady = bld->dady[attrib][chan];
  194. LLVMValueRef res;
  195. res = bld->attribs_pre[attrib][chan];
  196. if (quad_index == 1 || quad_index == 3) {
  197. /* top-right or bottom-right quad */
  198. /* build res = res + dadx + dadx */
  199. res = lp_build_add(&bld->base, res, dadx);
  200. res = lp_build_add(&bld->base, res, dadx);
  201. }
  202. if (quad_index == 2 || quad_index == 3) {
  203. /* bottom-left or bottom-right quad */
  204. /* build res = res + dady + dady */
  205. res = lp_build_add(&bld->base, res, dady);
  206. res = lp_build_add(&bld->base, res, dady);
  207. }
  208. //XXX bld->attribs_pre[attrib][chan] = res;
  209. if (mode == TGSI_INTERPOLATE_PERSPECTIVE) {
  210. LLVMValueRef w = bld->pos[3];
  211. assert(attrib != 0);
  212. if(!oow)
  213. oow = lp_build_rcp(&bld->base, w);
  214. res = lp_build_mul(&bld->base, res, oow);
  215. }
  216. attrib_name(res, attrib, chan, "");
  217. bld->attribs[attrib][chan] = res;
  218. }
  219. }
  220. }
  221. }
  222. }
  223. /**
  224. * Generate the position vectors.
  225. *
  226. * Parameter x0, y0 are the integer values with the quad upper left coordinates.
  227. */
  228. static void
  229. pos_init(struct lp_build_interp_soa_context *bld,
  230. LLVMValueRef x0,
  231. LLVMValueRef y0)
  232. {
  233. lp_build_name(x0, "pos.x");
  234. lp_build_name(y0, "pos.y");
  235. bld->attribs[0][0] = x0;
  236. bld->attribs[0][1] = y0;
  237. }
  238. /**
  239. * Update quad position values when moving to the next quad.
  240. */
  241. static void
  242. pos_update(struct lp_build_interp_soa_context *bld, int quad_index)
  243. {
  244. LLVMValueRef x = bld->attribs[0][0];
  245. LLVMValueRef y = bld->attribs[0][1];
  246. const int xstep = 2, ystep = 2;
  247. if (quad_index == 1 || quad_index == 3) {
  248. /* top-right or bottom-right quad in block */
  249. /* build x += xstep */
  250. x = lp_build_add(&bld->base, x,
  251. lp_build_const_scalar(bld->base.type, xstep));
  252. }
  253. if (quad_index == 2) {
  254. /* bottom-left quad in block */
  255. /* build y += ystep */
  256. y = lp_build_add(&bld->base, y,
  257. lp_build_const_scalar(bld->base.type, ystep));
  258. /* build x -= xstep */
  259. x = lp_build_sub(&bld->base, x,
  260. lp_build_const_scalar(bld->base.type, xstep));
  261. }
  262. lp_build_name(x, "pos.x");
  263. lp_build_name(y, "pos.y");
  264. bld->attribs[0][0] = x;
  265. bld->attribs[0][1] = y;
  266. }
  267. /**
  268. * Initialize fragment shader input attribute info.
  269. */
  270. void
  271. lp_build_interp_soa_init(struct lp_build_interp_soa_context *bld,
  272. const struct tgsi_token *tokens,
  273. boolean flatshade,
  274. LLVMBuilderRef builder,
  275. struct lp_type type,
  276. LLVMValueRef a0_ptr,
  277. LLVMValueRef dadx_ptr,
  278. LLVMValueRef dady_ptr,
  279. LLVMValueRef x0,
  280. LLVMValueRef y0)
  281. {
  282. struct tgsi_parse_context parse;
  283. struct tgsi_full_declaration *decl;
  284. memset(bld, 0, sizeof *bld);
  285. lp_build_context_init(&bld->base, builder, type);
  286. /* For convenience */
  287. bld->pos = bld->attribs[0];
  288. bld->inputs = (const LLVMValueRef (*)[NUM_CHANNELS]) bld->attribs[1];
  289. /* Position */
  290. bld->num_attribs = 1;
  291. bld->mask[0] = TGSI_WRITEMASK_ZW;
  292. bld->mode[0] = TGSI_INTERPOLATE_LINEAR;
  293. /* Inputs */
  294. tgsi_parse_init( &parse, tokens );
  295. while( !tgsi_parse_end_of_tokens( &parse ) ) {
  296. tgsi_parse_token( &parse );
  297. switch( parse.FullToken.Token.Type ) {
  298. case TGSI_TOKEN_TYPE_DECLARATION:
  299. decl = &parse.FullToken.FullDeclaration;
  300. if( decl->Declaration.File == TGSI_FILE_INPUT ) {
  301. unsigned first, last, mask;
  302. unsigned attrib;
  303. first = decl->Range.First;
  304. last = decl->Range.Last;
  305. mask = decl->Declaration.UsageMask;
  306. for( attrib = first; attrib <= last; ++attrib ) {
  307. bld->mask[1 + attrib] = mask;
  308. /* XXX: have mesa set INTERP_CONSTANT in the fragment
  309. * shader.
  310. */
  311. if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
  312. flatshade)
  313. bld->mode[1 + attrib] = TGSI_INTERPOLATE_CONSTANT;
  314. else
  315. bld->mode[1 + attrib] = decl->Declaration.Interpolate;
  316. }
  317. bld->num_attribs = MAX2(bld->num_attribs, 1 + last + 1);
  318. }
  319. break;
  320. case TGSI_TOKEN_TYPE_INSTRUCTION:
  321. case TGSI_TOKEN_TYPE_IMMEDIATE:
  322. break;
  323. default:
  324. assert( 0 );
  325. }
  326. }
  327. tgsi_parse_free( &parse );
  328. coeffs_init(bld, a0_ptr, dadx_ptr, dady_ptr);
  329. pos_init(bld, x0, y0);
  330. attribs_init(bld);
  331. }
  332. /**
  333. * Advance the position and inputs to the given quad within the block.
  334. */
  335. void
  336. lp_build_interp_soa_update(struct lp_build_interp_soa_context *bld,
  337. int quad_index)
  338. {
  339. assert(quad_index < 4);
  340. pos_update(bld, quad_index);
  341. attribs_update(bld, quad_index);
  342. }