• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2011 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 
24 #include "brw_context.h"
25 #include "brw_state.h"
26 #include "brw_defines.h"
27 #include "intel_batchbuffer.h"
28 #include "main/fbobject.h"
29 #include "main/framebuffer.h"
30 #include "main/viewport.h"
31 
32 static void
gen8_upload_sf_clip_viewport(struct brw_context * brw)33 gen8_upload_sf_clip_viewport(struct brw_context *brw)
34 {
35    struct gl_context *ctx = &brw->ctx;
36    const struct gen_device_info *devinfo = &brw->screen->devinfo;
37    float y_scale, y_bias;
38 
39    /* BRW_NEW_VIEWPORT_COUNT */
40    const unsigned viewport_count = brw->clip.viewport_count;
41 
42    /* _NEW_BUFFERS */
43    struct gl_framebuffer *fb = ctx->DrawBuffer;
44    const bool render_to_fbo = _mesa_is_user_fbo(fb);
45    const uint32_t fb_width = _mesa_geometric_width(ctx->DrawBuffer);
46    const uint32_t fb_height = _mesa_geometric_height(ctx->DrawBuffer);
47 
48    float *vp = brw_state_batch(brw, AUB_TRACE_SF_VP_STATE,
49                                16 * 4 * viewport_count,
50                                64, &brw->sf.vp_offset);
51    /* Also assign to clip.vp_offset in case something uses it. */
52    brw->clip.vp_offset = brw->sf.vp_offset;
53 
54    /* _NEW_BUFFERS */
55    if (render_to_fbo) {
56       y_scale = 1.0;
57       y_bias = 0;
58    } else {
59       y_scale = -1.0;
60       y_bias = (float)fb_height;
61    }
62 
63    for (unsigned i = 0; i < viewport_count; i++) {
64       float scale[3], translate[3];
65       _mesa_get_viewport_xform(ctx, i, scale, translate);
66 
67       /* _NEW_VIEWPORT: Viewport Matrix Elements */
68       vp[0] = scale[0];                        /* m00 */
69       vp[1] = scale[1] * y_scale;              /* m11 */
70       vp[2] = scale[2];                        /* m22 */
71       vp[3] = translate[0];                    /* m30 */
72       vp[4] = translate[1] * y_scale + y_bias; /* m31 */
73       vp[5] = translate[2];                    /* m32 */
74 
75       /* Reserved */
76       vp[6] = 0;
77       vp[7] = 0;
78 
79       brw_calculate_guardband_size(devinfo, fb_width, fb_height,
80                                    vp[0], vp[1], vp[3], vp[4],
81                                    &vp[8], &vp[9], &vp[10], &vp[11]);
82 
83       /* _NEW_VIEWPORT | _NEW_BUFFERS: Screen Space Viewport
84        * The hardware will take the intersection of the drawing rectangle,
85        * scissor rectangle, and the viewport extents. We don't need to be
86        * smart, and can therefore just program the viewport extents.
87        */
88       float viewport_Xmax = ctx->ViewportArray[i].X + ctx->ViewportArray[i].Width;
89       float viewport_Ymax = ctx->ViewportArray[i].Y + ctx->ViewportArray[i].Height;
90       if (render_to_fbo) {
91          vp[12] = ctx->ViewportArray[i].X;
92          vp[13] = viewport_Xmax - 1;
93          vp[14] = ctx->ViewportArray[i].Y;
94          vp[15] = viewport_Ymax - 1;
95       } else {
96          vp[12] = ctx->ViewportArray[i].X;
97          vp[13] = viewport_Xmax - 1;
98          vp[14] = fb_height - viewport_Ymax;
99          vp[15] = fb_height - ctx->ViewportArray[i].Y - 1;
100       }
101 
102       vp += 16;
103    }
104 
105    BEGIN_BATCH(2);
106    OUT_BATCH(_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CL << 16 | (2 - 2));
107    OUT_BATCH(brw->sf.vp_offset);
108    ADVANCE_BATCH();
109 }
110 
111 const struct brw_tracked_state gen8_sf_clip_viewport = {
112    .dirty = {
113       .mesa = _NEW_BUFFERS |
114               _NEW_VIEWPORT,
115       .brw = BRW_NEW_BATCH |
116              BRW_NEW_BLORP |
117              BRW_NEW_VIEWPORT_COUNT,
118    },
119    .emit = gen8_upload_sf_clip_viewport,
120 };
121