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_stipple.c 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. /* Authors: Keith Whitwell <keith@tungstengraphics.com>
  28. */
  29. /* Implement line stipple by cutting lines up into smaller lines.
  30. * There are hundreds of ways to implement line stipple, this is one
  31. * choice that should work in all situations, requires no state
  32. * manipulations, but with a penalty in terms of large amounts of
  33. * generated geometry.
  34. */
  35. #include "pipe/p_util.h"
  36. #include "pipe/p_defines.h"
  37. #include "pipe/p_shader_tokens.h"
  38. #include "draw_pipe.h"
  39. /** Subclass of draw_stage */
  40. struct stipple_stage {
  41. struct draw_stage stage;
  42. float counter;
  43. uint pattern;
  44. uint factor;
  45. };
  46. static INLINE struct stipple_stage *
  47. stipple_stage(struct draw_stage *stage)
  48. {
  49. return (struct stipple_stage *) stage;
  50. }
  51. /**
  52. * Compute interpolated vertex attributes for 'dst' at position 't'
  53. * between 'v0' and 'v1'.
  54. * XXX using linear interpolation for all attribs at this time.
  55. */
  56. static void
  57. screen_interp( struct draw_context *draw,
  58. struct vertex_header *dst,
  59. float t,
  60. const struct vertex_header *v0,
  61. const struct vertex_header *v1 )
  62. {
  63. uint attr;
  64. for (attr = 0; attr < draw->num_vs_outputs; attr++) {
  65. const float *val0 = v0->data[attr];
  66. const float *val1 = v1->data[attr];
  67. float *newv = dst->data[attr];
  68. uint i;
  69. for (i = 0; i < 4; i++) {
  70. newv[i] = val0[i] + t * (val1[i] - val0[i]);
  71. }
  72. }
  73. }
  74. static void
  75. emit_segment(struct draw_stage *stage, struct prim_header *header,
  76. float t0, float t1)
  77. {
  78. struct vertex_header *v0new = dup_vert(stage, header->v[0], 0);
  79. struct vertex_header *v1new = dup_vert(stage, header->v[1], 1);
  80. struct prim_header newprim = *header;
  81. if (t0 > 0.0) {
  82. screen_interp( stage->draw, v0new, t0, header->v[0], header->v[1] );
  83. newprim.v[0] = v0new;
  84. }
  85. if (t1 < 1.0) {
  86. screen_interp( stage->draw, v1new, t1, header->v[0], header->v[1] );
  87. newprim.v[1] = v1new;
  88. }
  89. stage->next->line( stage->next, &newprim );
  90. }
  91. static INLINE unsigned
  92. stipple_test(int counter, ushort pattern, int factor)
  93. {
  94. int b = (counter / factor) & 0xf;
  95. return (1 << b) & pattern;
  96. }
  97. static void
  98. stipple_line(struct draw_stage *stage, struct prim_header *header)
  99. {
  100. struct stipple_stage *stipple = stipple_stage(stage);
  101. struct vertex_header *v0 = header->v[0];
  102. struct vertex_header *v1 = header->v[1];
  103. const float *pos0 = v0->data[0];
  104. const float *pos1 = v1->data[0];
  105. float start = 0;
  106. int state = 0;
  107. float x0 = pos0[0];
  108. float x1 = pos1[0];
  109. float y0 = pos0[1];
  110. float y1 = pos1[1];
  111. float dx = x0 > x1 ? x0 - x1 : x1 - x0;
  112. float dy = y0 > y1 ? y0 - y1 : y1 - y0;
  113. float length = MAX2(dx, dy);
  114. int i;
  115. /* XXX ToDo: intead of iterating pixel-by-pixel, use a look-up table.
  116. */
  117. for (i = 0; i < length; i++) {
  118. int result = stipple_test( (int) stipple->counter+i,
  119. (ushort) stipple->pattern, stipple->factor );
  120. if (result != state) {
  121. /* changing from "off" to "on" or vice versa */
  122. if (state) {
  123. if (start != i) {
  124. /* finishing an "on" segment */
  125. emit_segment( stage, header, start / length, i / length );
  126. }
  127. }
  128. else {
  129. /* starting an "on" segment */
  130. start = (float) i;
  131. }
  132. state = result;
  133. }
  134. }
  135. if (state && start < length)
  136. emit_segment( stage, header, start / length, 1.0 );
  137. stipple->counter += length;
  138. }
  139. static void
  140. reset_stipple_counter(struct draw_stage *stage)
  141. {
  142. struct stipple_stage *stipple = stipple_stage(stage);
  143. stipple->counter = 0;
  144. stage->next->reset_stipple_counter( stage->next );
  145. }
  146. static void
  147. stipple_first_line(struct draw_stage *stage,
  148. struct prim_header *header)
  149. {
  150. struct stipple_stage *stipple = stipple_stage(stage);
  151. struct draw_context *draw = stage->draw;
  152. stipple->pattern = draw->rasterizer->line_stipple_pattern;
  153. stipple->factor = draw->rasterizer->line_stipple_factor + 1;
  154. stage->line = stipple_line;
  155. stage->line( stage, header );
  156. }
  157. static void
  158. stipple_flush(struct draw_stage *stage, unsigned flags)
  159. {
  160. stage->line = stipple_first_line;
  161. stage->next->flush( stage->next, flags );
  162. }
  163. static void
  164. stipple_destroy( struct draw_stage *stage )
  165. {
  166. draw_free_temp_verts( stage );
  167. FREE( stage );
  168. }
  169. /**
  170. * Create line stippler stage
  171. */
  172. struct draw_stage *draw_stipple_stage( struct draw_context *draw )
  173. {
  174. struct stipple_stage *stipple = CALLOC_STRUCT(stipple_stage);
  175. draw_alloc_temp_verts( &stipple->stage, 2 );
  176. stipple->stage.draw = draw;
  177. stipple->stage.next = NULL;
  178. stipple->stage.point = draw_pipe_passthrough_point;
  179. stipple->stage.line = stipple_first_line;
  180. stipple->stage.tri = draw_pipe_passthrough_tri;
  181. stipple->stage.reset_stipple_counter = reset_stipple_counter;
  182. stipple->stage.flush = stipple_flush;
  183. stipple->stage.destroy = stipple_destroy;
  184. return &stipple->stage;
  185. }