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.

draw_pipe_clip.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. * \brief Clipping stage
  29. *
  30. * \author Keith Whitwell <keith@tungstengraphics.com>
  31. */
  32. #include "util/u_memory.h"
  33. #include "util/u_math.h"
  34. #include "pipe/p_shader_tokens.h"
  35. #include "draw_vs.h"
  36. #include "draw_pipe.h"
  37. #ifndef IS_NEGATIVE
  38. #define IS_NEGATIVE(X) ((X) < 0.0)
  39. #endif
  40. #ifndef DIFFERENT_SIGNS
  41. #define DIFFERENT_SIGNS(x, y) ((x) * (y) <= 0.0F && (x) - (y) != 0.0F)
  42. #endif
  43. #ifndef MAX_CLIPPED_VERTICES
  44. #define MAX_CLIPPED_VERTICES ((2 * (6 + PIPE_MAX_CLIP_PLANES))+1)
  45. #endif
  46. struct clipper {
  47. struct draw_stage stage; /**< base class */
  48. /* Basically duplicate some of the flatshading logic here:
  49. */
  50. boolean flat;
  51. uint num_color_attribs;
  52. uint color_attribs[4]; /* front/back primary/secondary colors */
  53. float (*plane)[4];
  54. };
  55. /* This is a bit confusing:
  56. */
  57. static INLINE struct clipper *clipper_stage( struct draw_stage *stage )
  58. {
  59. return (struct clipper *)stage;
  60. }
  61. #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
  62. /* All attributes are float[4], so this is easy:
  63. */
  64. static void interp_attr( float *fdst,
  65. float t,
  66. const float *fin,
  67. const float *fout )
  68. {
  69. fdst[0] = LINTERP( t, fout[0], fin[0] );
  70. fdst[1] = LINTERP( t, fout[1], fin[1] );
  71. fdst[2] = LINTERP( t, fout[2], fin[2] );
  72. fdst[3] = LINTERP( t, fout[3], fin[3] );
  73. }
  74. static void copy_colors( struct draw_stage *stage,
  75. struct vertex_header *dst,
  76. const struct vertex_header *src )
  77. {
  78. const struct clipper *clipper = clipper_stage(stage);
  79. uint i;
  80. for (i = 0; i < clipper->num_color_attribs; i++) {
  81. const uint attr = clipper->color_attribs[i];
  82. COPY_4FV(dst->data[attr], src->data[attr]);
  83. }
  84. }
  85. /* Interpolate between two vertices to produce a third.
  86. */
  87. static void interp( const struct clipper *clip,
  88. struct vertex_header *dst,
  89. float t,
  90. const struct vertex_header *out,
  91. const struct vertex_header *in )
  92. {
  93. const unsigned nr_attrs = clip->stage.draw->vs.num_vs_outputs;
  94. const unsigned pos_attr = clip->stage.draw->vs.position_output;
  95. unsigned j;
  96. /* Vertex header.
  97. */
  98. {
  99. dst->clipmask = 0;
  100. dst->edgeflag = 0; /* will get overwritten later */
  101. dst->pad = 0;
  102. dst->vertex_id = UNDEFINED_VERTEX_ID;
  103. }
  104. /* Clip coordinates: interpolate normally
  105. */
  106. {
  107. interp_attr(dst->clip, t, in->clip, out->clip);
  108. }
  109. /* Do the projective divide and insert window coordinates:
  110. */
  111. {
  112. const float *pos = dst->clip;
  113. const float *scale = clip->stage.draw->viewport.scale;
  114. const float *trans = clip->stage.draw->viewport.translate;
  115. const float oow = 1.0f / pos[3];
  116. dst->data[pos_attr][0] = pos[0] * oow * scale[0] + trans[0];
  117. dst->data[pos_attr][1] = pos[1] * oow * scale[1] + trans[1];
  118. dst->data[pos_attr][2] = pos[2] * oow * scale[2] + trans[2];
  119. dst->data[pos_attr][3] = oow;
  120. }
  121. /* Other attributes
  122. */
  123. for (j = 0; j < nr_attrs; j++) {
  124. if (j != pos_attr)
  125. interp_attr(dst->data[j], t, in->data[j], out->data[j]);
  126. }
  127. }
  128. static void emit_poly( struct draw_stage *stage,
  129. struct vertex_header **inlist,
  130. unsigned n,
  131. const struct prim_header *origPrim)
  132. {
  133. struct prim_header header;
  134. unsigned i;
  135. const ushort edge_first = DRAW_PIPE_EDGE_FLAG_2;
  136. const ushort edge_middle = DRAW_PIPE_EDGE_FLAG_0;
  137. const ushort edge_last = DRAW_PIPE_EDGE_FLAG_1;
  138. /* later stages may need the determinant, but only the sign matters */
  139. header.det = origPrim->det;
  140. header.flags = DRAW_PIPE_RESET_STIPPLE | edge_first | edge_middle;
  141. header.pad = 0;
  142. for (i = 2; i < n; i++, header.flags = edge_middle) {
  143. header.v[0] = inlist[i-1];
  144. header.v[1] = inlist[i];
  145. header.v[2] = inlist[0]; /* keep in v[2] for flatshading */
  146. if (i == n-1)
  147. header.flags |= edge_last;
  148. if (0) {
  149. const struct draw_vertex_shader *vs = stage->draw->vs.vertex_shader;
  150. uint j, k;
  151. debug_printf("Clipped tri:\n");
  152. for (j = 0; j < 3; j++) {
  153. for (k = 0; k < vs->info.num_outputs; k++) {
  154. debug_printf(" Vert %d: Attr %d: %f %f %f %f\n", j, k,
  155. header.v[j]->data[k][0],
  156. header.v[j]->data[k][1],
  157. header.v[j]->data[k][2],
  158. header.v[j]->data[k][3]);
  159. }
  160. }
  161. }
  162. stage->next->tri( stage->next, &header );
  163. }
  164. }
  165. static INLINE float
  166. dot4(const float *a, const float *b)
  167. {
  168. return (a[0]*b[0] +
  169. a[1]*b[1] +
  170. a[2]*b[2] +
  171. a[3]*b[3]);
  172. }
  173. /* Clip a triangle against the viewport and user clip planes.
  174. */
  175. static void
  176. do_clip_tri( struct draw_stage *stage,
  177. struct prim_header *header,
  178. unsigned clipmask )
  179. {
  180. struct clipper *clipper = clipper_stage( stage );
  181. struct vertex_header *a[MAX_CLIPPED_VERTICES];
  182. struct vertex_header *b[MAX_CLIPPED_VERTICES];
  183. struct vertex_header **inlist = a;
  184. struct vertex_header **outlist = b;
  185. unsigned tmpnr = 0;
  186. unsigned n = 3;
  187. unsigned i;
  188. inlist[0] = header->v[0];
  189. inlist[1] = header->v[1];
  190. inlist[2] = header->v[2];
  191. while (clipmask && n >= 3) {
  192. const unsigned plane_idx = ffs(clipmask)-1;
  193. const float *plane = clipper->plane[plane_idx];
  194. struct vertex_header *vert_prev = inlist[0];
  195. float dp_prev = dot4( vert_prev->clip, plane );
  196. unsigned outcount = 0;
  197. clipmask &= ~(1<<plane_idx);
  198. inlist[n] = inlist[0]; /* prevent rotation of vertices */
  199. for (i = 1; i <= n; i++) {
  200. struct vertex_header *vert = inlist[i];
  201. float dp = dot4( vert->clip, plane );
  202. if (!IS_NEGATIVE(dp_prev)) {
  203. outlist[outcount++] = vert_prev;
  204. }
  205. if (DIFFERENT_SIGNS(dp, dp_prev)) {
  206. struct vertex_header *new_vert = clipper->stage.tmp[tmpnr++];
  207. outlist[outcount++] = new_vert;
  208. if (IS_NEGATIVE(dp)) {
  209. /* Going out of bounds. Avoid division by zero as we
  210. * know dp != dp_prev from DIFFERENT_SIGNS, above.
  211. */
  212. float t = dp / (dp - dp_prev);
  213. interp( clipper, new_vert, t, vert, vert_prev );
  214. /* Force edgeflag true in this case:
  215. */
  216. new_vert->edgeflag = 1;
  217. } else {
  218. /* Coming back in.
  219. */
  220. float t = dp_prev / (dp_prev - dp);
  221. interp( clipper, new_vert, t, vert_prev, vert );
  222. /* Copy starting vert's edgeflag:
  223. */
  224. new_vert->edgeflag = vert_prev->edgeflag;
  225. }
  226. }
  227. vert_prev = vert;
  228. dp_prev = dp;
  229. }
  230. {
  231. struct vertex_header **tmp = inlist;
  232. inlist = outlist;
  233. outlist = tmp;
  234. n = outcount;
  235. }
  236. }
  237. /* If flat-shading, copy color to new provoking vertex.
  238. */
  239. if (clipper->flat && inlist[0] != header->v[2]) {
  240. if (1) {
  241. inlist[0] = dup_vert(stage, inlist[0], tmpnr++);
  242. }
  243. copy_colors(stage, inlist[0], header->v[2]);
  244. }
  245. /* Emit the polygon as triangles to the setup stage:
  246. */
  247. if (n >= 3)
  248. emit_poly( stage, inlist, n, header );
  249. }
  250. /* Clip a line against the viewport and user clip planes.
  251. */
  252. static void
  253. do_clip_line( struct draw_stage *stage,
  254. struct prim_header *header,
  255. unsigned clipmask )
  256. {
  257. const struct clipper *clipper = clipper_stage( stage );
  258. struct vertex_header *v0 = header->v[0];
  259. struct vertex_header *v1 = header->v[1];
  260. const float *pos0 = v0->clip;
  261. const float *pos1 = v1->clip;
  262. float t0 = 0.0F;
  263. float t1 = 0.0F;
  264. struct prim_header newprim;
  265. while (clipmask) {
  266. const unsigned plane_idx = ffs(clipmask)-1;
  267. const float *plane = clipper->plane[plane_idx];
  268. const float dp0 = dot4( pos0, plane );
  269. const float dp1 = dot4( pos1, plane );
  270. if (dp1 < 0.0F) {
  271. float t = dp1 / (dp1 - dp0);
  272. t1 = MAX2(t1, t);
  273. }
  274. if (dp0 < 0.0F) {
  275. float t = dp0 / (dp0 - dp1);
  276. t0 = MAX2(t0, t);
  277. }
  278. if (t0 + t1 >= 1.0F)
  279. return; /* discard */
  280. clipmask &= ~(1 << plane_idx); /* turn off this plane's bit */
  281. }
  282. if (v0->clipmask) {
  283. interp( clipper, stage->tmp[0], t0, v0, v1 );
  284. if (clipper->flat)
  285. copy_colors(stage, stage->tmp[0], v0);
  286. newprim.v[0] = stage->tmp[0];
  287. }
  288. else {
  289. newprim.v[0] = v0;
  290. }
  291. if (v1->clipmask) {
  292. interp( clipper, stage->tmp[1], t1, v1, v0 );
  293. newprim.v[1] = stage->tmp[1];
  294. }
  295. else {
  296. newprim.v[1] = v1;
  297. }
  298. stage->next->line( stage->next, &newprim );
  299. }
  300. static void
  301. clip_point( struct draw_stage *stage,
  302. struct prim_header *header )
  303. {
  304. if (header->v[0]->clipmask == 0)
  305. stage->next->point( stage->next, header );
  306. }
  307. static void
  308. clip_line( struct draw_stage *stage,
  309. struct prim_header *header )
  310. {
  311. unsigned clipmask = (header->v[0]->clipmask |
  312. header->v[1]->clipmask);
  313. if (clipmask == 0) {
  314. /* no clipping needed */
  315. stage->next->line( stage->next, header );
  316. }
  317. else if ((header->v[0]->clipmask &
  318. header->v[1]->clipmask) == 0) {
  319. do_clip_line(stage, header, clipmask);
  320. }
  321. /* else, totally clipped */
  322. }
  323. static void
  324. clip_tri( struct draw_stage *stage,
  325. struct prim_header *header )
  326. {
  327. unsigned clipmask = (header->v[0]->clipmask |
  328. header->v[1]->clipmask |
  329. header->v[2]->clipmask);
  330. if (clipmask == 0) {
  331. /* no clipping needed */
  332. stage->next->tri( stage->next, header );
  333. }
  334. else if ((header->v[0]->clipmask &
  335. header->v[1]->clipmask &
  336. header->v[2]->clipmask) == 0) {
  337. do_clip_tri(stage, header, clipmask);
  338. }
  339. }
  340. /* Update state. Could further delay this until we hit the first
  341. * primitive that really requires clipping.
  342. */
  343. static void
  344. clip_init_state( struct draw_stage *stage )
  345. {
  346. struct clipper *clipper = clipper_stage( stage );
  347. clipper->flat = stage->draw->rasterizer->flatshade ? TRUE : FALSE;
  348. if (clipper->flat) {
  349. const struct draw_vertex_shader *vs = stage->draw->vs.vertex_shader;
  350. uint i;
  351. clipper->num_color_attribs = 0;
  352. for (i = 0; i < vs->info.num_outputs; i++) {
  353. if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_COLOR ||
  354. vs->info.output_semantic_name[i] == TGSI_SEMANTIC_BCOLOR) {
  355. clipper->color_attribs[clipper->num_color_attribs++] = i;
  356. }
  357. }
  358. }
  359. stage->tri = clip_tri;
  360. stage->line = clip_line;
  361. }
  362. static void clip_first_tri( struct draw_stage *stage,
  363. struct prim_header *header )
  364. {
  365. clip_init_state( stage );
  366. stage->tri( stage, header );
  367. }
  368. static void clip_first_line( struct draw_stage *stage,
  369. struct prim_header *header )
  370. {
  371. clip_init_state( stage );
  372. stage->line( stage, header );
  373. }
  374. static void clip_flush( struct draw_stage *stage,
  375. unsigned flags )
  376. {
  377. stage->tri = clip_first_tri;
  378. stage->line = clip_first_line;
  379. stage->next->flush( stage->next, flags );
  380. }
  381. static void clip_reset_stipple_counter( struct draw_stage *stage )
  382. {
  383. stage->next->reset_stipple_counter( stage->next );
  384. }
  385. static void clip_destroy( struct draw_stage *stage )
  386. {
  387. draw_free_temp_verts( stage );
  388. FREE( stage );
  389. }
  390. /**
  391. * Allocate a new clipper stage.
  392. * \return pointer to new stage object
  393. */
  394. struct draw_stage *draw_clip_stage( struct draw_context *draw )
  395. {
  396. struct clipper *clipper = CALLOC_STRUCT(clipper);
  397. if (clipper == NULL)
  398. goto fail;
  399. if (!draw_alloc_temp_verts( &clipper->stage, MAX_CLIPPED_VERTICES+1 ))
  400. goto fail;
  401. clipper->stage.draw = draw;
  402. clipper->stage.point = clip_point;
  403. clipper->stage.line = clip_first_line;
  404. clipper->stage.tri = clip_first_tri;
  405. clipper->stage.flush = clip_flush;
  406. clipper->stage.reset_stipple_counter = clip_reset_stipple_counter;
  407. clipper->stage.destroy = clip_destroy;
  408. clipper->plane = draw->plane;
  409. return &clipper->stage;
  410. fail:
  411. if (clipper)
  412. clipper->stage.destroy( &clipper->stage );
  413. return NULL;
  414. }