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_offset.c 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 polygon offset state
  29. *
  30. * \author Keith Whitwell <keith@tungstengraphics.com>
  31. * \author Brian Paul
  32. */
  33. #include "util/u_math.h"
  34. #include "util/u_memory.h"
  35. #include "draw_pipe.h"
  36. struct offset_stage {
  37. struct draw_stage stage;
  38. float scale;
  39. float units;
  40. };
  41. static INLINE struct offset_stage *offset_stage( struct draw_stage *stage )
  42. {
  43. return (struct offset_stage *) stage;
  44. }
  45. /**
  46. * Offset tri Z. Some hardware can handle this, but not usually when
  47. * doing unfilled rendering.
  48. */
  49. static void do_offset_tri( struct draw_stage *stage,
  50. struct prim_header *header )
  51. {
  52. const unsigned pos = stage->draw->vs.position_output;
  53. struct offset_stage *offset = offset_stage(stage);
  54. float inv_det = 1.0f / header->det;
  55. /* Window coords:
  56. */
  57. float *v0 = header->v[0]->data[pos];
  58. float *v1 = header->v[1]->data[pos];
  59. float *v2 = header->v[2]->data[pos];
  60. /* edge vectors e = v0 - v2, f = v1 - v2 */
  61. float ex = v0[0] - v2[0];
  62. float ey = v0[1] - v2[1];
  63. float ez = v0[2] - v2[2];
  64. float fx = v1[0] - v2[0];
  65. float fy = v1[1] - v2[1];
  66. float fz = v1[2] - v2[2];
  67. /* (a,b) = cross(e,f).xy */
  68. float a = ey*fz - ez*fy;
  69. float b = ez*fx - ex*fz;
  70. float dzdx = fabsf(a * inv_det);
  71. float dzdy = fabsf(b * inv_det);
  72. float zoffset = offset->units + MAX2(dzdx, dzdy) * offset->scale;
  73. /*
  74. * Note: we're applying the offset and clamping per-vertex.
  75. * Ideally, the offset is applied per-fragment prior to fragment shading.
  76. */
  77. v0[2] = CLAMP(v0[2] + zoffset, 0.0f, 1.0f);
  78. v1[2] = CLAMP(v1[2] + zoffset, 0.0f, 1.0f);
  79. v2[2] = CLAMP(v2[2] + zoffset, 0.0f, 1.0f);
  80. stage->next->tri( stage->next, header );
  81. }
  82. static void offset_tri( struct draw_stage *stage,
  83. struct prim_header *header )
  84. {
  85. struct prim_header tmp;
  86. tmp.det = header->det;
  87. tmp.flags = header->flags;
  88. tmp.pad = header->pad;
  89. tmp.v[0] = dup_vert(stage, header->v[0], 0);
  90. tmp.v[1] = dup_vert(stage, header->v[1], 1);
  91. tmp.v[2] = dup_vert(stage, header->v[2], 2);
  92. do_offset_tri( stage, &tmp );
  93. }
  94. static void offset_first_tri( struct draw_stage *stage,
  95. struct prim_header *header )
  96. {
  97. struct offset_stage *offset = offset_stage(stage);
  98. float mrd = 1.0f / 65535.0f; /* XXX this depends on depthbuffer bits! */
  99. offset->units = stage->draw->rasterizer->offset_units * mrd;
  100. offset->scale = stage->draw->rasterizer->offset_scale;
  101. stage->tri = offset_tri;
  102. stage->tri( stage, header );
  103. }
  104. static void offset_flush( struct draw_stage *stage,
  105. unsigned flags )
  106. {
  107. stage->tri = offset_first_tri;
  108. stage->next->flush( stage->next, flags );
  109. }
  110. static void offset_reset_stipple_counter( struct draw_stage *stage )
  111. {
  112. stage->next->reset_stipple_counter( stage->next );
  113. }
  114. static void offset_destroy( struct draw_stage *stage )
  115. {
  116. draw_free_temp_verts( stage );
  117. FREE( stage );
  118. }
  119. /**
  120. * Create polygon offset drawing stage.
  121. */
  122. struct draw_stage *draw_offset_stage( struct draw_context *draw )
  123. {
  124. struct offset_stage *offset = CALLOC_STRUCT(offset_stage);
  125. if (offset == NULL)
  126. goto fail;
  127. draw_alloc_temp_verts( &offset->stage, 3 );
  128. offset->stage.draw = draw;
  129. offset->stage.next = NULL;
  130. offset->stage.point = draw_pipe_passthrough_point;
  131. offset->stage.line = draw_pipe_passthrough_line;
  132. offset->stage.tri = offset_first_tri;
  133. offset->stage.flush = offset_flush;
  134. offset->stage.reset_stipple_counter = offset_reset_stipple_counter;
  135. offset->stage.destroy = offset_destroy;
  136. return &offset->stage;
  137. fail:
  138. if (offset)
  139. offset->stage.destroy( &offset->stage );
  140. return NULL;
  141. }