Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  Copyright (C) Intel Corp.  2006.  All Rights Reserved.
  3.  Intel funded Tungsten Graphics to
  4.  develop this 3D driver.
  5.  
  6.  Permission is hereby granted, free of charge, to any person obtaining
  7.  a 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, sublicense, 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
  16.  portions of the Software.
  17.  
  18.  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19.  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20.  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21.  IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  22.  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23.  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24.  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  
  26.  **********************************************************************/
  27.  /*
  28.   * Authors:
  29.   *   Keith Whitwell <keithw@vmware.com>
  30.   */
  31.  
  32.  
  33. #include "main/glheader.h"
  34. #include "main/macros.h"
  35. #include "main/enums.h"
  36. #include "program/program.h"
  37.  
  38. #include "intel_batchbuffer.h"
  39.  
  40. #include "brw_defines.h"
  41. #include "brw_context.h"
  42. #include "brw_eu.h"
  43. #include "brw_clip.h"
  44.  
  45.  
  46.  
  47.  
  48. struct brw_reg get_tmp( struct brw_clip_compile *c )
  49. {
  50.    struct brw_reg tmp = brw_vec4_grf(c->last_tmp, 0);
  51.  
  52.    if (++c->last_tmp > c->prog_data.total_grf)
  53.       c->prog_data.total_grf = c->last_tmp;
  54.  
  55.    return tmp;
  56. }
  57.  
  58. static void release_tmp( struct brw_clip_compile *c, struct brw_reg tmp )
  59. {
  60.    if (tmp.nr == c->last_tmp-1)
  61.       c->last_tmp--;
  62. }
  63.  
  64.  
  65. static struct brw_reg make_plane_ud(GLuint x, GLuint y, GLuint z, GLuint w)
  66. {
  67.    return brw_imm_ud((w<<24) | (z<<16) | (y<<8) | x);
  68. }
  69.  
  70.  
  71. void brw_clip_init_planes( struct brw_clip_compile *c )
  72. {
  73.    struct brw_codegen *p = &c->func;
  74.  
  75.    if (!c->key.nr_userclip) {
  76.       brw_MOV(p, get_element_ud(c->reg.fixed_planes, 0), make_plane_ud( 0,    0, 0xff, 1));
  77.       brw_MOV(p, get_element_ud(c->reg.fixed_planes, 1), make_plane_ud( 0,    0,    1, 1));
  78.       brw_MOV(p, get_element_ud(c->reg.fixed_planes, 2), make_plane_ud( 0, 0xff,    0, 1));
  79.       brw_MOV(p, get_element_ud(c->reg.fixed_planes, 3), make_plane_ud( 0,    1,    0, 1));
  80.       brw_MOV(p, get_element_ud(c->reg.fixed_planes, 4), make_plane_ud(0xff,  0,    0, 1));
  81.       brw_MOV(p, get_element_ud(c->reg.fixed_planes, 5), make_plane_ud( 1,    0,    0, 1));
  82.    }
  83. }
  84.  
  85.  
  86.  
  87. #define W 3
  88.  
  89. /* Project 'pos' to screen space (or back again), overwrite with results:
  90.  */
  91. void brw_clip_project_position(struct brw_clip_compile *c, struct brw_reg pos )
  92. {
  93.    struct brw_codegen *p = &c->func;
  94.  
  95.    /* calc rhw
  96.     */
  97.    brw_math_invert(p, get_element(pos, W), get_element(pos, W));
  98.  
  99.    /* value.xyz *= value.rhw
  100.     */
  101.    brw_set_default_access_mode(p, BRW_ALIGN_16);
  102.    brw_MUL(p, brw_writemask(pos, WRITEMASK_XYZ), pos, brw_swizzle1(pos, W));
  103.    brw_set_default_access_mode(p, BRW_ALIGN_1);
  104. }
  105.  
  106.  
  107. static void brw_clip_project_vertex( struct brw_clip_compile *c,
  108.                                      struct brw_indirect vert_addr )
  109. {
  110.    struct brw_codegen *p = &c->func;
  111.    struct brw_reg tmp = get_tmp(c);
  112.    GLuint hpos_offset = brw_varying_to_offset(&c->vue_map, VARYING_SLOT_POS);
  113.    GLuint ndc_offset = brw_varying_to_offset(&c->vue_map,
  114.                                              BRW_VARYING_SLOT_NDC);
  115.  
  116.    /* Fixup position.  Extract from the original vertex and re-project
  117.     * to screen space:
  118.     */
  119.    brw_MOV(p, tmp, deref_4f(vert_addr, hpos_offset));
  120.    brw_clip_project_position(c, tmp);
  121.    brw_MOV(p, deref_4f(vert_addr, ndc_offset), tmp);
  122.        
  123.    release_tmp(c, tmp);
  124. }
  125.  
  126.  
  127.  
  128.  
  129. /* Interpolate between two vertices and put the result into a0.0.
  130.  * Increment a0.0 accordingly.
  131.  *
  132.  * Beware that dest_ptr can be equal to v0_ptr!
  133.  */
  134. void brw_clip_interp_vertex( struct brw_clip_compile *c,
  135.                              struct brw_indirect dest_ptr,
  136.                              struct brw_indirect v0_ptr, /* from */
  137.                              struct brw_indirect v1_ptr, /* to */
  138.                              struct brw_reg t0,
  139.                              bool force_edgeflag)
  140. {
  141.    struct brw_codegen *p = &c->func;
  142.    struct brw_reg t_nopersp, v0_ndc_copy;
  143.    GLuint slot;
  144.  
  145.    /* Just copy the vertex header:
  146.     */
  147.    /*
  148.     * After CLIP stage, only first 256 bits of the VUE are read
  149.     * back on Ironlake, so needn't change it
  150.     */
  151.    brw_copy_indirect_to_indirect(p, dest_ptr, v0_ptr, 1);
  152.  
  153.  
  154.    /* First handle the 3D and NDC interpolation, in case we
  155.     * need noperspective interpolation. Doing it early has no
  156.     * performance impact in any case.
  157.     */
  158.  
  159.    /* Take a copy of the v0 NDC coordinates, in case dest == v0. */
  160.    if (c->has_noperspective_shading) {
  161.       GLuint offset = brw_varying_to_offset(&c->vue_map,
  162.                                                  BRW_VARYING_SLOT_NDC);
  163.       v0_ndc_copy = get_tmp(c);
  164.       brw_MOV(p, v0_ndc_copy, deref_4f(v0_ptr, offset));
  165.    }
  166.  
  167.    /* Compute the new 3D position
  168.     *
  169.     * dest_hpos = v0_hpos * (1 - t0) + v1_hpos * t0
  170.     */
  171.    {
  172.       GLuint delta = brw_varying_to_offset(&c->vue_map, VARYING_SLOT_POS);
  173.       struct brw_reg tmp = get_tmp(c);
  174.       brw_MUL(p, vec4(brw_null_reg()), deref_4f(v1_ptr, delta), t0);
  175.       brw_MAC(p, tmp, negate(deref_4f(v0_ptr, delta)), t0);
  176.       brw_ADD(p, deref_4f(dest_ptr, delta), deref_4f(v0_ptr, delta), tmp);
  177.       release_tmp(c, tmp);
  178.    }
  179.  
  180.    /* Recreate the projected (NDC) coordinate in the new vertex header */
  181.    brw_clip_project_vertex(c, dest_ptr);
  182.  
  183.    /* If we have noperspective attributes,
  184.     * we need to compute the screen-space t
  185.     */
  186.    if (c->has_noperspective_shading) {
  187.       GLuint delta = brw_varying_to_offset(&c->vue_map,
  188.                                                 BRW_VARYING_SLOT_NDC);
  189.       struct brw_reg tmp = get_tmp(c);
  190.       t_nopersp = get_tmp(c);
  191.  
  192.       /* t_nopersp = vec4(v1.xy, dest.xy) */
  193.       brw_MOV(p, t_nopersp, deref_4f(v1_ptr, delta));
  194.       brw_MOV(p, tmp, deref_4f(dest_ptr, delta));
  195.       brw_set_default_access_mode(p, BRW_ALIGN_16);
  196.       brw_MOV(p,
  197.               brw_writemask(t_nopersp, WRITEMASK_ZW),
  198.               brw_swizzle(tmp, 0, 1, 0, 1));
  199.  
  200.       /* t_nopersp = vec4(v1.xy, dest.xy) - v0.xyxy */
  201.       brw_ADD(p, t_nopersp, t_nopersp,
  202.               negate(brw_swizzle(v0_ndc_copy, 0, 1, 0, 1)));
  203.  
  204.       /* Add the absolute values of the X and Y deltas so that if
  205.        * the points aren't in the same place on the screen we get
  206.        * nonzero values to divide.
  207.        *
  208.        * After that, we have vert1 - vert0 in t_nopersp.x and
  209.        * vertnew - vert0 in t_nopersp.y
  210.        *
  211.        * t_nopersp = vec2(|v1.x  -v0.x| + |v1.y  -v0.y|,
  212.        *                  |dest.x-v0.x| + |dest.y-v0.y|)
  213.        */
  214.       brw_ADD(p,
  215.               brw_writemask(t_nopersp, WRITEMASK_XY),
  216.               brw_abs(brw_swizzle(t_nopersp, 0, 2, 0, 0)),
  217.               brw_abs(brw_swizzle(t_nopersp, 1, 3, 0, 0)));
  218.       brw_set_default_access_mode(p, BRW_ALIGN_1);
  219.  
  220.       /* If the points are in the same place, just substitute a
  221.        * value to avoid divide-by-zero
  222.        */
  223.       brw_CMP(p, vec1(brw_null_reg()), BRW_CONDITIONAL_EQ,
  224.               vec1(t_nopersp),
  225.               brw_imm_f(0));
  226.       brw_IF(p, BRW_EXECUTE_1);
  227.       brw_MOV(p, t_nopersp, brw_imm_vf4(1, 0, 0, 0));
  228.       brw_ENDIF(p);
  229.  
  230.       /* Now compute t_nopersp = t_nopersp.y/t_nopersp.x and broadcast it. */
  231.       brw_math_invert(p, get_element(t_nopersp, 0), get_element(t_nopersp, 0));
  232.       brw_MUL(p, vec1(t_nopersp), vec1(t_nopersp),
  233.             vec1(suboffset(t_nopersp, 1)));
  234.       brw_set_default_access_mode(p, BRW_ALIGN_16);
  235.       brw_MOV(p, t_nopersp, brw_swizzle(t_nopersp, 0, 0, 0, 0));
  236.       brw_set_default_access_mode(p, BRW_ALIGN_1);
  237.  
  238.       release_tmp(c, tmp);
  239.       release_tmp(c, v0_ndc_copy);
  240.    }
  241.  
  242.    /* Now we can iterate over each attribute
  243.     * (could be done in pairs?)
  244.     */
  245.    for (slot = 0; slot < c->vue_map.num_slots; slot++) {
  246.       int varying = c->vue_map.slot_to_varying[slot];
  247.       GLuint delta = brw_vue_slot_to_offset(slot);
  248.  
  249.       /* HPOS, NDC already handled above */
  250.       if (varying == VARYING_SLOT_POS || varying == BRW_VARYING_SLOT_NDC)
  251.          continue;
  252.  
  253.  
  254.       if (varying == VARYING_SLOT_EDGE) {
  255.          if (force_edgeflag)
  256.             brw_MOV(p, deref_4f(dest_ptr, delta), brw_imm_f(1));
  257.          else
  258.             brw_MOV(p, deref_4f(dest_ptr, delta), deref_4f(v0_ptr, delta));
  259.       } else if (varying == VARYING_SLOT_PSIZ) {
  260.          /* PSIZ doesn't need interpolation because it isn't used by the
  261.           * fragment shader.
  262.           */
  263.       } else if (varying < VARYING_SLOT_MAX) {
  264.          /* This is a true vertex result (and not a special value for the VUE
  265.           * header), so interpolate:
  266.           *
  267.           *        New = attr0 + t*attr1 - t*attr0
  268.           *
  269.           * Unless the attribute is flat shaded -- in which case just copy
  270.           * from one of the sources (doesn't matter which; already copied from pv)
  271.           */
  272.          GLuint interp = c->key.interpolation_mode.mode[slot];
  273.  
  274.          if (interp != INTERP_QUALIFIER_FLAT) {
  275.             struct brw_reg tmp = get_tmp(c);
  276.             struct brw_reg t =
  277.                interp == INTERP_QUALIFIER_NOPERSPECTIVE ? t_nopersp : t0;
  278.  
  279.             brw_MUL(p,
  280.                   vec4(brw_null_reg()),
  281.                   deref_4f(v1_ptr, delta),
  282.                   t);
  283.  
  284.             brw_MAC(p,
  285.                   tmp,
  286.                   negate(deref_4f(v0_ptr, delta)),
  287.                   t);
  288.  
  289.             brw_ADD(p,
  290.                   deref_4f(dest_ptr, delta),
  291.                   deref_4f(v0_ptr, delta),
  292.                   tmp);
  293.  
  294.             release_tmp(c, tmp);
  295.          }
  296.          else {
  297.             brw_MOV(p,
  298.                   deref_4f(dest_ptr, delta),
  299.                   deref_4f(v0_ptr, delta));
  300.          }
  301.       }
  302.    }
  303.  
  304.    if (c->vue_map.num_slots % 2) {
  305.       GLuint delta = brw_vue_slot_to_offset(c->vue_map.num_slots);
  306.  
  307.       brw_MOV(p, deref_4f(dest_ptr, delta), brw_imm_f(0));
  308.    }
  309.  
  310.    if (c->has_noperspective_shading)
  311.       release_tmp(c, t_nopersp);
  312. }
  313.  
  314. void brw_clip_emit_vue(struct brw_clip_compile *c,
  315.                        struct brw_indirect vert,
  316.                        enum brw_urb_write_flags flags,
  317.                        GLuint header)
  318. {
  319.    struct brw_codegen *p = &c->func;
  320.    bool allocate = flags & BRW_URB_WRITE_ALLOCATE;
  321.  
  322.    brw_clip_ff_sync(c);
  323.  
  324.    /* Any URB entry that is allocated must subsequently be used or discarded,
  325.     * so it doesn't make sense to mark EOT and ALLOCATE at the same time.
  326.     */
  327.    assert(!(allocate && (flags & BRW_URB_WRITE_EOT)));
  328.  
  329.    /* Copy the vertex from vertn into m1..mN+1:
  330.     */
  331.    brw_copy_from_indirect(p, brw_message_reg(1), vert, c->nr_regs);
  332.  
  333.    /* Overwrite PrimType and PrimStart in the message header, for
  334.     * each vertex in turn:
  335.     */
  336.    brw_MOV(p, get_element_ud(c->reg.R0, 2), brw_imm_ud(header));
  337.  
  338.  
  339.    /* Send each vertex as a separate write to the urb.  This
  340.     * is different to the concept in brw_sf_emit.c, where
  341.     * subsequent writes are used to build up a single urb
  342.     * entry.  Each of these writes instantiates a separate
  343.     * urb entry - (I think... what about 'allocate'?)
  344.     */
  345.    brw_urb_WRITE(p,
  346.                  allocate ? c->reg.R0 : retype(brw_null_reg(), BRW_REGISTER_TYPE_UD),
  347.                  0,
  348.                  c->reg.R0,
  349.                  flags,
  350.                  c->nr_regs + 1, /* msg length */
  351.                  allocate ? 1 : 0, /* response_length */
  352.                  0,             /* urb offset */
  353.                  BRW_URB_SWIZZLE_NONE);
  354. }
  355.  
  356.  
  357.  
  358. void brw_clip_kill_thread(struct brw_clip_compile *c)
  359. {
  360.    struct brw_codegen *p = &c->func;
  361.  
  362.    brw_clip_ff_sync(c);
  363.    /* Send an empty message to kill the thread and release any
  364.     * allocated urb entry:
  365.     */
  366.    brw_urb_WRITE(p,
  367.                  retype(brw_null_reg(), BRW_REGISTER_TYPE_UD),
  368.                  0,
  369.                  c->reg.R0,
  370.                  BRW_URB_WRITE_UNUSED | BRW_URB_WRITE_EOT_COMPLETE,
  371.                  1,             /* msg len */
  372.                  0,             /* response len */
  373.                  0,
  374.                  BRW_URB_SWIZZLE_NONE);
  375. }
  376.  
  377.  
  378.  
  379.  
  380. struct brw_reg brw_clip_plane0_address( struct brw_clip_compile *c )
  381. {
  382.    return brw_address(c->reg.fixed_planes);
  383. }
  384.  
  385.  
  386. struct brw_reg brw_clip_plane_stride( struct brw_clip_compile *c )
  387. {
  388.    if (c->key.nr_userclip) {
  389.       return brw_imm_uw(16);
  390.    }
  391.    else {
  392.       return brw_imm_uw(4);
  393.    }
  394. }
  395.  
  396.  
  397. /* Distribute flatshaded attributes from provoking vertex prior to
  398.  * clipping.
  399.  */
  400. void brw_clip_copy_flatshaded_attributes( struct brw_clip_compile *c,
  401.                            GLuint to, GLuint from )
  402. {
  403.    struct brw_codegen *p = &c->func;
  404.  
  405.    for (int i = 0; i < c->vue_map.num_slots; i++) {
  406.       if (c->key.interpolation_mode.mode[i] == INTERP_QUALIFIER_FLAT) {
  407.          brw_MOV(p,
  408.                  byte_offset(c->reg.vertex[to], brw_vue_slot_to_offset(i)),
  409.                  byte_offset(c->reg.vertex[from], brw_vue_slot_to_offset(i)));
  410.       }
  411.    }
  412. }
  413.  
  414.  
  415.  
  416. void brw_clip_init_clipmask( struct brw_clip_compile *c )
  417. {
  418.    struct brw_codegen *p = &c->func;
  419.    struct brw_reg incoming = get_element_ud(c->reg.R0, 2);
  420.  
  421.    /* Shift so that lowest outcode bit is rightmost:
  422.     */
  423.    brw_SHR(p, c->reg.planemask, incoming, brw_imm_ud(26));
  424.  
  425.    if (c->key.nr_userclip) {
  426.       struct brw_reg tmp = retype(vec1(get_tmp(c)), BRW_REGISTER_TYPE_UD);
  427.  
  428.       /* Rearrange userclip outcodes so that they come directly after
  429.        * the fixed plane bits.
  430.        */
  431.       if (p->devinfo->gen == 5 || p->devinfo->is_g4x)
  432.          brw_AND(p, tmp, incoming, brw_imm_ud(0xff<<14));
  433.       else
  434.          brw_AND(p, tmp, incoming, brw_imm_ud(0x3f<<14));
  435.  
  436.       brw_SHR(p, tmp, tmp, brw_imm_ud(8));
  437.       brw_OR(p, c->reg.planemask, c->reg.planemask, tmp);
  438.  
  439.       release_tmp(c, tmp);
  440.    }
  441. }
  442.  
  443. void brw_clip_ff_sync(struct brw_clip_compile *c)
  444. {
  445.     struct brw_codegen *p = &c->func;
  446.  
  447.     if (p->devinfo->gen == 5) {
  448.         brw_AND(p, brw_null_reg(), c->reg.ff_sync, brw_imm_ud(0x1));
  449.         brw_inst_set_cond_modifier(p->devinfo, brw_last_inst, BRW_CONDITIONAL_Z);
  450.         brw_IF(p, BRW_EXECUTE_1);
  451.         {
  452.             brw_OR(p, c->reg.ff_sync, c->reg.ff_sync, brw_imm_ud(0x1));
  453.             brw_ff_sync(p,
  454.                         c->reg.R0,
  455.                         0,
  456.                         c->reg.R0,
  457.                         1, /* allocate */
  458.                         1, /* response length */
  459.                         0 /* eot */);
  460.         }
  461.         brw_ENDIF(p);
  462.         brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);
  463.     }
  464. }
  465.  
  466. void brw_clip_init_ff_sync(struct brw_clip_compile *c)
  467. {
  468.     struct brw_codegen *p = &c->func;
  469.  
  470.     if (p->devinfo->gen == 5) {
  471.         brw_MOV(p, c->reg.ff_sync, brw_imm_ud(0));
  472.     }
  473. }
  474.