PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/src/mesa/drivers/dri/i965/brw_clip_line.c

https://github.com/smowton/vgallium
C | 233 lines | 122 code | 38 blank | 73 comment | 5 complexity | d0e71acd7d146d1cab4156862d3e153c MD5 | raw file
Possible License(s): LGPL-2.0
  1. /*
  2. Copyright (C) Intel Corp. 2006. All Rights Reserved.
  3. Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
  4. develop this 3D driver.
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files (the
  7. "Software"), to deal in the Software without restriction, including
  8. without limitation the rights to use, copy, modify, merge, publish,
  9. distribute, sublicense, and/or sell copies of the Software, and to
  10. permit persons to whom the Software is furnished to do so, subject to
  11. the following conditions:
  12. The above copyright notice and this permission notice (including the
  13. next paragraph) shall be included in all copies or substantial
  14. portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  19. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. **********************************************************************/
  23. /*
  24. * Authors:
  25. * Keith Whitwell <keith@tungstengraphics.com>
  26. */
  27. #include "glheader.h"
  28. #include "macros.h"
  29. #include "enums.h"
  30. #include "shader/program.h"
  31. #include "intel_batchbuffer.h"
  32. #include "brw_defines.h"
  33. #include "brw_context.h"
  34. #include "brw_eu.h"
  35. #include "brw_util.h"
  36. #include "brw_clip.h"
  37. static void brw_clip_line_alloc_regs( struct brw_clip_compile *c )
  38. {
  39. GLuint i = 0,j;
  40. /* Register usage is static, precompute here:
  41. */
  42. c->reg.R0 = retype(brw_vec8_grf(i, 0), BRW_REGISTER_TYPE_UD); i++;
  43. if (c->key.nr_userclip) {
  44. c->reg.fixed_planes = brw_vec4_grf(i, 0);
  45. i += (6 + c->key.nr_userclip + 1) / 2;
  46. c->prog_data.curb_read_length = (6 + c->key.nr_userclip + 1) / 2;
  47. }
  48. else
  49. c->prog_data.curb_read_length = 0;
  50. /* Payload vertices plus space for more generated vertices:
  51. */
  52. for (j = 0; j < 4; j++) {
  53. c->reg.vertex[j] = brw_vec4_grf(i, 0);
  54. i += c->nr_regs;
  55. }
  56. c->reg.t = brw_vec1_grf(i, 0);
  57. c->reg.t0 = brw_vec1_grf(i, 1);
  58. c->reg.t1 = brw_vec1_grf(i, 2);
  59. c->reg.planemask = retype(brw_vec1_grf(i, 3), BRW_REGISTER_TYPE_UD);
  60. c->reg.plane_equation = brw_vec4_grf(i, 4);
  61. i++;
  62. c->reg.dp0 = brw_vec1_grf(i, 0); /* fixme - dp4 will clobber r.1,2,3 */
  63. c->reg.dp1 = brw_vec1_grf(i, 4);
  64. i++;
  65. if (!c->key.nr_userclip) {
  66. c->reg.fixed_planes = brw_vec8_grf(i, 0);
  67. i++;
  68. }
  69. c->first_tmp = i;
  70. c->last_tmp = i;
  71. c->prog_data.urb_read_length = c->nr_regs; /* ? */
  72. c->prog_data.total_grf = i;
  73. }
  74. /* Line clipping, more or less following the following algorithm:
  75. *
  76. * for (p=0;p<MAX_PLANES;p++) {
  77. * if (clipmask & (1 << p)) {
  78. * GLfloat dp0 = DOTPROD( vtx0, plane[p] );
  79. * GLfloat dp1 = DOTPROD( vtx1, plane[p] );
  80. *
  81. * if (IS_NEGATIVE(dp1)) {
  82. * GLfloat t = dp1 / (dp1 - dp0);
  83. * if (t > t1) t1 = t;
  84. * } else {
  85. * GLfloat t = dp0 / (dp0 - dp1);
  86. * if (t > t0) t0 = t;
  87. * }
  88. *
  89. * if (t0 + t1 >= 1.0)
  90. * return;
  91. * }
  92. * }
  93. *
  94. * interp( ctx, newvtx0, vtx0, vtx1, t0 );
  95. * interp( ctx, newvtx1, vtx1, vtx0, t1 );
  96. *
  97. */
  98. static void clip_and_emit_line( struct brw_clip_compile *c )
  99. {
  100. struct brw_compile *p = &c->func;
  101. struct brw_indirect vtx0 = brw_indirect(0, 0);
  102. struct brw_indirect vtx1 = brw_indirect(1, 0);
  103. struct brw_indirect newvtx0 = brw_indirect(2, 0);
  104. struct brw_indirect newvtx1 = brw_indirect(3, 0);
  105. struct brw_indirect plane_ptr = brw_indirect(4, 0);
  106. struct brw_instruction *plane_loop;
  107. struct brw_instruction *plane_active;
  108. struct brw_instruction *is_negative;
  109. struct brw_instruction *not_culled;
  110. struct brw_reg v1_null_ud = retype(vec1(brw_null_reg()), BRW_REGISTER_TYPE_UD);
  111. brw_MOV(p, get_addr_reg(vtx0), brw_address(c->reg.vertex[0]));
  112. brw_MOV(p, get_addr_reg(vtx1), brw_address(c->reg.vertex[1]));
  113. brw_MOV(p, get_addr_reg(newvtx0), brw_address(c->reg.vertex[2]));
  114. brw_MOV(p, get_addr_reg(newvtx1), brw_address(c->reg.vertex[3]));
  115. brw_MOV(p, get_addr_reg(plane_ptr), brw_clip_plane0_address(c));
  116. /* Note: init t0, t1 together:
  117. */
  118. brw_MOV(p, vec2(c->reg.t0), brw_imm_f(0));
  119. brw_clip_init_planes(c);
  120. brw_clip_init_clipmask(c);
  121. plane_loop = brw_DO(p, BRW_EXECUTE_1);
  122. {
  123. /* if (planemask & 1)
  124. */
  125. brw_set_conditionalmod(p, BRW_CONDITIONAL_NZ);
  126. brw_AND(p, v1_null_ud, c->reg.planemask, brw_imm_ud(1));
  127. plane_active = brw_IF(p, BRW_EXECUTE_1);
  128. {
  129. if (c->key.nr_userclip)
  130. brw_MOV(p, c->reg.plane_equation, deref_4f(plane_ptr, 0));
  131. else
  132. brw_MOV(p, c->reg.plane_equation, deref_4b(plane_ptr, 0));
  133. /* dp = DP4(vtx->position, plane)
  134. */
  135. brw_DP4(p, vec4(c->reg.dp0), deref_4f(vtx0, c->offset[VERT_RESULT_HPOS]), c->reg.plane_equation);
  136. /* if (IS_NEGATIVE(dp1))
  137. */
  138. brw_set_conditionalmod(p, BRW_CONDITIONAL_L);
  139. brw_DP4(p, vec4(c->reg.dp1), deref_4f(vtx1, c->offset[VERT_RESULT_HPOS]), c->reg.plane_equation);
  140. is_negative = brw_IF(p, BRW_EXECUTE_1);
  141. {
  142. brw_ADD(p, c->reg.t, c->reg.dp1, negate(c->reg.dp0));
  143. brw_math_invert(p, c->reg.t, c->reg.t);
  144. brw_MUL(p, c->reg.t, c->reg.t, c->reg.dp1);
  145. brw_CMP(p, vec1(brw_null_reg()), BRW_CONDITIONAL_G, c->reg.t, c->reg.t1 );
  146. brw_MOV(p, c->reg.t1, c->reg.t);
  147. brw_set_predicate_control(p, BRW_PREDICATE_NONE);
  148. }
  149. is_negative = brw_ELSE(p, is_negative);
  150. {
  151. /* Coming back in. We know that both cannot be negative
  152. * because the line would have been culled in that case.
  153. */
  154. brw_ADD(p, c->reg.t, c->reg.dp0, negate(c->reg.dp1));
  155. brw_math_invert(p, c->reg.t, c->reg.t);
  156. brw_MUL(p, c->reg.t, c->reg.t, c->reg.dp0);
  157. brw_CMP(p, vec1(brw_null_reg()), BRW_CONDITIONAL_G, c->reg.t, c->reg.t0 );
  158. brw_MOV(p, c->reg.t0, c->reg.t);
  159. brw_set_predicate_control(p, BRW_PREDICATE_NONE);
  160. }
  161. brw_ENDIF(p, is_negative);
  162. }
  163. brw_ENDIF(p, plane_active);
  164. /* plane_ptr++;
  165. */
  166. brw_ADD(p, get_addr_reg(plane_ptr), get_addr_reg(plane_ptr), brw_clip_plane_stride(c));
  167. /* while (planemask>>=1) != 0
  168. */
  169. brw_set_conditionalmod(p, BRW_CONDITIONAL_NZ);
  170. brw_SHR(p, c->reg.planemask, c->reg.planemask, brw_imm_ud(1));
  171. }
  172. brw_WHILE(p, plane_loop);
  173. brw_ADD(p, c->reg.t, c->reg.t0, c->reg.t1);
  174. brw_CMP(p, vec1(brw_null_reg()), BRW_CONDITIONAL_L, c->reg.t, brw_imm_f(1.0));
  175. not_culled = brw_IF(p, BRW_EXECUTE_1);
  176. {
  177. brw_clip_interp_vertex(c, newvtx0, vtx0, vtx1, c->reg.t0, GL_FALSE);
  178. brw_clip_interp_vertex(c, newvtx1, vtx1, vtx0, c->reg.t1, GL_FALSE);
  179. brw_clip_emit_vue(c, newvtx0, 1, 0, (_3DPRIM_LINESTRIP << 2) | R02_PRIM_START);
  180. brw_clip_emit_vue(c, newvtx1, 0, 1, (_3DPRIM_LINESTRIP << 2) | R02_PRIM_END);
  181. }
  182. brw_ENDIF(p, not_culled);
  183. brw_clip_kill_thread(c);
  184. }
  185. void brw_emit_line_clip( struct brw_clip_compile *c )
  186. {
  187. brw_clip_line_alloc_regs(c);
  188. if (c->key.do_flat_shading)
  189. brw_clip_copy_colors(c, 0, 1);
  190. clip_and_emit_line(c);
  191. }