Home | History | Annotate | Download | only in i965
      1 /*
      2  * Copyright  2009 Intel Corporation
      3  *
      4  * Permission is hereby granted, free of charge, to any person obtaining a
      5  * copy of this software and associated documentation files (the "Software"),
      6  * to deal in the Software without restriction, including without limitation
      7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8  * and/or sell copies of the Software, and to permit persons to whom the
      9  * Software is furnished to do so, subject to the following conditions:
     10  *
     11  * The above copyright notice and this permission notice (including the next
     12  * paragraph) shall be included in all copies or substantial portions of the
     13  * Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     21  * IN THE SOFTWARE.
     22  *
     23  * Authors:
     24  *    Eric Anholt <eric (at) anholt.net>
     25  *
     26  */
     27 
     28 #include "brw_context.h"
     29 #include "brw_state.h"
     30 #include "brw_defines.h"
     31 #include "intel_batchbuffer.h"
     32 #include "main/fbobject.h"
     33 #include "main/framebuffer.h"
     34 #include "main/viewport.h"
     35 
     36 void
     37 brw_calculate_guardband_size(const struct gen_device_info *devinfo,
     38                              uint32_t fb_width, uint32_t fb_height,
     39                              float m00, float m11, float m30, float m31,
     40                              float *xmin, float *xmax,
     41                              float *ymin, float *ymax)
     42 {
     43    /* According to the "Vertex X,Y Clamping and Quantization" section of the
     44     * Strips and Fans documentation:
     45     *
     46     * "The vertex X and Y screen-space coordinates are also /clamped/ to the
     47     *  fixed-point "guardband" range supported by the rasterization hardware"
     48     *
     49     * and
     50     *
     51     * "In almost all circumstances, if an objects vertices are actually
     52     *  modified by this clamping (i.e., had X or Y coordinates outside of
     53     *  the guardband extent the rendered object will not match the intended
     54     *  result.  Therefore software should take steps to ensure that this does
     55     *  not happen - e.g., by clipping objects such that they do not exceed
     56     *  these limits after the Drawing Rectangle is applied."
     57     *
     58     * I believe the fundamental restriction is that the rasterizer (in
     59     * the SF/WM stages) have a limit on the number of pixels that can be
     60     * rasterized.  We need to ensure any coordinates beyond the rasterizer
     61     * limit are handled by the clipper.  So effectively that limit becomes
     62     * the clipper's guardband size.
     63     *
     64     * It goes on to say:
     65     *
     66     * "In addition, in order to be correctly rendered, objects must have a
     67     *  screenspace bounding box not exceeding 8K in the X or Y direction.
     68     *  This additional restriction must also be comprehended by software,
     69     *  i.e., enforced by use of clipping."
     70     *
     71     * This makes no sense.  Gen7+ hardware supports 16K render targets,
     72     * and you definitely need to be able to draw polygons that fill the
     73     * surface.  Our assumption is that the rasterizer was limited to 8K
     74     * on Sandybridge, which only supports 8K surfaces, and it was actually
     75     * increased to 16K on Ivybridge and later.
     76     *
     77     * So, limit the guardband to 16K on Gen7+ and 8K on Sandybridge.
     78     */
     79    const float gb_size = devinfo->gen >= 7 ? 16384.0f : 8192.0f;
     80 
     81    if (m00 != 0 && m11 != 0) {
     82       /* First, we compute the screen-space render area */
     83       const float ss_ra_xmin = MIN3(        0, m30 + m00, m30 - m00);
     84       const float ss_ra_xmax = MAX3( fb_width, m30 + m00, m30 - m00);
     85       const float ss_ra_ymin = MIN3(        0, m31 + m11, m31 - m11);
     86       const float ss_ra_ymax = MAX3(fb_height, m31 + m11, m31 - m11);
     87 
     88       /* We want the guardband to be centered on that */
     89       const float ss_gb_xmin = (ss_ra_xmin + ss_ra_xmax) / 2 - gb_size;
     90       const float ss_gb_xmax = (ss_ra_xmin + ss_ra_xmax) / 2 + gb_size;
     91       const float ss_gb_ymin = (ss_ra_ymin + ss_ra_ymax) / 2 - gb_size;
     92       const float ss_gb_ymax = (ss_ra_ymin + ss_ra_ymax) / 2 + gb_size;
     93 
     94       /* Now we need it in native device coordinates */
     95       const float ndc_gb_xmin = (ss_gb_xmin - m30) / m00;
     96       const float ndc_gb_xmax = (ss_gb_xmax - m30) / m00;
     97       const float ndc_gb_ymin = (ss_gb_ymin - m31) / m11;
     98       const float ndc_gb_ymax = (ss_gb_ymax - m31) / m11;
     99 
    100       /* Thanks to Y-flipping and ORIGIN_UPPER_LEFT, the Y coordinates may be
    101        * flipped upside-down.  X should be fine though.
    102        */
    103       assert(ndc_gb_xmin <= ndc_gb_xmax);
    104       *xmin = ndc_gb_xmin;
    105       *xmax = ndc_gb_xmax;
    106       *ymin = MIN2(ndc_gb_ymin, ndc_gb_ymax);
    107       *ymax = MAX2(ndc_gb_ymin, ndc_gb_ymax);
    108    } else {
    109       /* The viewport scales to 0, so nothing will be rendered. */
    110       *xmin = 0.0f;
    111       *xmax = 0.0f;
    112       *ymin = 0.0f;
    113       *ymax = 0.0f;
    114    }
    115 }
    116 
    117 static void
    118 gen6_upload_sf_and_clip_viewports(struct brw_context *brw)
    119 {
    120    struct gl_context *ctx = &brw->ctx;
    121    const struct gen_device_info *devinfo = &brw->screen->devinfo;
    122    struct gen6_sf_viewport *sfv;
    123    struct brw_clipper_viewport *clv;
    124    GLfloat y_scale, y_bias;
    125 
    126    /* BRW_NEW_VIEWPORT_COUNT */
    127    const unsigned viewport_count = brw->clip.viewport_count;
    128 
    129    /* _NEW_BUFFERS */
    130    struct gl_framebuffer *fb = ctx->DrawBuffer;
    131    const bool render_to_fbo = _mesa_is_user_fbo(fb);
    132    const uint32_t fb_width = _mesa_geometric_width(ctx->DrawBuffer);
    133    const uint32_t fb_height = _mesa_geometric_height(ctx->DrawBuffer);
    134 
    135    sfv = brw_state_batch(brw, AUB_TRACE_SF_VP_STATE,
    136                          sizeof(*sfv) * viewport_count,
    137                          32, &brw->sf.vp_offset);
    138    memset(sfv, 0, sizeof(*sfv) * viewport_count);
    139 
    140    clv = brw_state_batch(brw, AUB_TRACE_CLIP_VP_STATE,
    141                          sizeof(*clv) * viewport_count,
    142                          32, &brw->clip.vp_offset);
    143 
    144    if (render_to_fbo) {
    145       y_scale = 1.0;
    146       y_bias = 0.0;
    147    } else {
    148       y_scale = -1.0;
    149       y_bias = (float)fb_height;
    150    }
    151 
    152    for (unsigned i = 0; i < viewport_count; i++) {
    153       float scale[3], translate[3];
    154 
    155       /* _NEW_VIEWPORT */
    156       _mesa_get_viewport_xform(ctx, i, scale, translate);
    157       sfv[i].m00 = scale[0];
    158       sfv[i].m11 = scale[1] * y_scale;
    159       sfv[i].m22 = scale[2];
    160       sfv[i].m30 = translate[0];
    161       sfv[i].m31 = translate[1] * y_scale + y_bias;
    162       sfv[i].m32 = translate[2];
    163 
    164       brw_calculate_guardband_size(devinfo, fb_width, fb_height,
    165                                    sfv[i].m00, sfv[i].m11,
    166                                    sfv[i].m30, sfv[i].m31,
    167                                    &clv[i].xmin, &clv[i].xmax,
    168                                    &clv[i].ymin, &clv[i].ymax);
    169    }
    170 
    171    brw->ctx.NewDriverState |= BRW_NEW_SF_VP | BRW_NEW_CLIP_VP;
    172 }
    173 
    174 const struct brw_tracked_state gen6_sf_and_clip_viewports = {
    175    .dirty = {
    176       .mesa = _NEW_BUFFERS |
    177               _NEW_VIEWPORT,
    178       .brw = BRW_NEW_BATCH |
    179              BRW_NEW_BLORP |
    180              BRW_NEW_VIEWPORT_COUNT,
    181    },
    182    .emit = gen6_upload_sf_and_clip_viewports,
    183 };
    184 
    185 static void upload_viewport_state_pointers(struct brw_context *brw)
    186 {
    187    BEGIN_BATCH(4);
    188    OUT_BATCH(_3DSTATE_VIEWPORT_STATE_POINTERS << 16 | (4 - 2) |
    189 	     GEN6_CC_VIEWPORT_MODIFY |
    190 	     GEN6_SF_VIEWPORT_MODIFY |
    191 	     GEN6_CLIP_VIEWPORT_MODIFY);
    192    OUT_BATCH(brw->clip.vp_offset);
    193    OUT_BATCH(brw->sf.vp_offset);
    194    OUT_BATCH(brw->cc.vp_offset);
    195    ADVANCE_BATCH();
    196 }
    197 
    198 const struct brw_tracked_state gen6_viewport_state = {
    199    .dirty = {
    200       .mesa = 0,
    201       .brw = BRW_NEW_BATCH |
    202              BRW_NEW_BLORP |
    203              BRW_NEW_CC_VP |
    204              BRW_NEW_CLIP_VP |
    205              BRW_NEW_SF_VP |
    206              BRW_NEW_STATE_BASE_ADDRESS,
    207    },
    208    .emit = upload_viewport_state_pointers,
    209 };
    210