1848b8605Smrg/*
2848b8605Smrg * Copyright © 2014 Broadcom
3848b8605Smrg *
4848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5848b8605Smrg * copy of this software and associated documentation files (the "Software"),
6848b8605Smrg * to deal in the Software without restriction, including without limitation
7848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
9848b8605Smrg * Software is furnished to do so, subject to the following conditions:
10848b8605Smrg *
11848b8605Smrg * The above copyright notice and this permission notice (including the next
12848b8605Smrg * paragraph) shall be included in all copies or substantial portions of the
13848b8605Smrg * Software.
14848b8605Smrg *
15848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16848b8605Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19848b8605Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20848b8605Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21848b8605Smrg * IN THE SOFTWARE.
22848b8605Smrg */
23848b8605Smrg
24848b8605Smrg#include "vc4_context.h"
25848b8605Smrg
26848b8605Smrgvoid
27848b8605Smrgvc4_emit_state(struct pipe_context *pctx)
28848b8605Smrg{
29848b8605Smrg        struct vc4_context *vc4 = vc4_context(pctx);
30b8e80941Smrg        struct vc4_job *job = vc4->job;
31848b8605Smrg
32b8e80941Smrg        if (vc4->dirty & (VC4_DIRTY_SCISSOR | VC4_DIRTY_VIEWPORT |
33b8e80941Smrg                          VC4_DIRTY_RASTERIZER)) {
34b8e80941Smrg                float *vpscale = vc4->viewport.scale;
35b8e80941Smrg                float *vptranslate = vc4->viewport.translate;
36b8e80941Smrg                float vp_minx = -fabsf(vpscale[0]) + vptranslate[0];
37b8e80941Smrg                float vp_maxx = fabsf(vpscale[0]) + vptranslate[0];
38b8e80941Smrg                float vp_miny = -fabsf(vpscale[1]) + vptranslate[1];
39b8e80941Smrg                float vp_maxy = fabsf(vpscale[1]) + vptranslate[1];
40b8e80941Smrg
41b8e80941Smrg                /* Clip to the scissor if it's enabled, but still clip to the
42b8e80941Smrg                 * drawable regardless since that controls where the binner
43b8e80941Smrg                 * tries to put things.
44b8e80941Smrg                 *
45b8e80941Smrg                 * Additionally, always clip the rendering to the viewport,
46b8e80941Smrg                 * since the hardware does guardband clipping, meaning
47b8e80941Smrg                 * primitives would rasterize outside of the view volume.
48b8e80941Smrg                 */
49b8e80941Smrg                uint32_t minx, miny, maxx, maxy;
50b8e80941Smrg                if (!vc4->rasterizer->base.scissor) {
51b8e80941Smrg                        minx = MAX2(vp_minx, 0);
52b8e80941Smrg                        miny = MAX2(vp_miny, 0);
53b8e80941Smrg                        maxx = MIN2(vp_maxx, job->draw_width);
54b8e80941Smrg                        maxy = MIN2(vp_maxy, job->draw_height);
55b8e80941Smrg                } else {
56b8e80941Smrg                        minx = MAX2(vp_minx, vc4->scissor.minx);
57b8e80941Smrg                        miny = MAX2(vp_miny, vc4->scissor.miny);
58b8e80941Smrg                        maxx = MIN2(vp_maxx, vc4->scissor.maxx);
59b8e80941Smrg                        maxy = MIN2(vp_maxy, vc4->scissor.maxy);
60b8e80941Smrg                }
61b8e80941Smrg
62b8e80941Smrg                cl_emit(&job->bcl, CLIP_WINDOW, clip) {
63b8e80941Smrg                        clip.clip_window_left_pixel_coordinate = minx;
64b8e80941Smrg                        clip.clip_window_bottom_pixel_coordinate = miny;
65b8e80941Smrg                        clip.clip_window_height_in_pixels = maxy - miny;
66b8e80941Smrg                        clip.clip_window_width_in_pixels = maxx - minx;
67b8e80941Smrg                }
68b8e80941Smrg
69b8e80941Smrg                job->draw_min_x = MIN2(job->draw_min_x, minx);
70b8e80941Smrg                job->draw_min_y = MIN2(job->draw_min_y, miny);
71b8e80941Smrg                job->draw_max_x = MAX2(job->draw_max_x, maxx);
72b8e80941Smrg                job->draw_max_y = MAX2(job->draw_max_y, maxy);
73848b8605Smrg        }
74848b8605Smrg
75b8e80941Smrg        if (vc4->dirty & (VC4_DIRTY_RASTERIZER |
76b8e80941Smrg                          VC4_DIRTY_ZSA |
77b8e80941Smrg                          VC4_DIRTY_COMPILED_FS)) {
78b8e80941Smrg                uint8_t ez_enable_mask_out = ~0;
79b8e80941Smrg                uint8_t rasosm_mask_out = ~0;
80b8e80941Smrg
81b8e80941Smrg                struct vc4_cl_out *bcl = cl_start(&job->bcl);
82b8e80941Smrg                /* HW-2905: If the RCL ends up doing a full-res load when
83b8e80941Smrg                 * multisampling, then early Z tracking may end up with values
84b8e80941Smrg                 * from the previous tile due to a HW bug.  Disable it to
85b8e80941Smrg                 * avoid that.
86b8e80941Smrg                 *
87b8e80941Smrg                 * We should be able to skip this when the Z is cleared, but I
88b8e80941Smrg                 * was seeing bad rendering on glxgears -samples 4 even in
89b8e80941Smrg                 * that case.
90b8e80941Smrg                 */
91b8e80941Smrg                if (job->msaa || vc4->prog.fs->disable_early_z)
92b8e80941Smrg                        ez_enable_mask_out &= ~VC4_CONFIG_BITS_EARLY_Z;
93b8e80941Smrg
94b8e80941Smrg                /* Don't set the rasterizer to oversample if we're doing our
95b8e80941Smrg                 * binning and load/stores in single-sample mode.  This is for
96b8e80941Smrg                 * the samples == 1 case, where vc4 doesn't do any
97b8e80941Smrg                 * multisampling behavior.
98b8e80941Smrg                 */
99b8e80941Smrg                if (!job->msaa) {
100b8e80941Smrg                        rasosm_mask_out &=
101b8e80941Smrg                                ~VC4_CONFIG_BITS_RASTERIZER_OVERSAMPLE_4X;
102b8e80941Smrg                }
103b8e80941Smrg
104b8e80941Smrg                cl_u8(&bcl, VC4_PACKET_CONFIGURATION_BITS);
105b8e80941Smrg                cl_u8(&bcl,
106b8e80941Smrg                      (vc4->rasterizer->config_bits[0] |
107b8e80941Smrg                       vc4->zsa->config_bits[0]) & rasosm_mask_out);
108b8e80941Smrg                cl_u8(&bcl,
109848b8605Smrg                      vc4->rasterizer->config_bits[1] |
110848b8605Smrg                      vc4->zsa->config_bits[1]);
111b8e80941Smrg                cl_u8(&bcl,
112b8e80941Smrg                      (vc4->rasterizer->config_bits[2] |
113b8e80941Smrg                       vc4->zsa->config_bits[2]) & ez_enable_mask_out);
114b8e80941Smrg                cl_end(&job->bcl, bcl);
115b8e80941Smrg        }
116b8e80941Smrg
117b8e80941Smrg        if (vc4->dirty & VC4_DIRTY_RASTERIZER) {
118b8e80941Smrg                cl_emit_prepacked(&job->bcl, &vc4->rasterizer->packed);
119848b8605Smrg        }
120848b8605Smrg
121848b8605Smrg        if (vc4->dirty & VC4_DIRTY_VIEWPORT) {
122b8e80941Smrg                cl_emit(&job->bcl, CLIPPER_XY_SCALING, clip) {
123b8e80941Smrg                        clip.viewport_half_width_in_1_16th_of_pixel =
124b8e80941Smrg                                vc4->viewport.scale[0] * 16.0f;
125b8e80941Smrg                        clip.viewport_half_height_in_1_16th_of_pixel =
126b8e80941Smrg                                vc4->viewport.scale[1] * 16.0f;
127b8e80941Smrg                }
128848b8605Smrg
129b8e80941Smrg                cl_emit(&job->bcl, CLIPPER_Z_SCALE_AND_OFFSET, clip) {
130b8e80941Smrg                        clip.viewport_z_offset_zc_to_zs =
131b8e80941Smrg                                vc4->viewport.translate[2];
132b8e80941Smrg                        clip.viewport_z_scale_zc_to_zs =
133b8e80941Smrg                                vc4->viewport.scale[2];
134b8e80941Smrg                }
135b8e80941Smrg
136b8e80941Smrg                cl_emit(&job->bcl, VIEWPORT_OFFSET, vp) {
137b8e80941Smrg                        vp.viewport_centre_x_coordinate =
138b8e80941Smrg                                vc4->viewport.translate[0];
139b8e80941Smrg                        vp.viewport_centre_y_coordinate =
140b8e80941Smrg                                vc4->viewport.translate[1];
141b8e80941Smrg                }
142b8e80941Smrg        }
143848b8605Smrg
144b8e80941Smrg        if (vc4->dirty & VC4_DIRTY_FLAT_SHADE_FLAGS) {
145b8e80941Smrg                cl_emit(&job->bcl, FLAT_SHADE_FLAGS, flags) {
146b8e80941Smrg                        if (vc4->rasterizer->base.flatshade)
147b8e80941Smrg                                flags.flat_shading_flags =
148b8e80941Smrg                                        vc4->prog.fs->color_inputs;
149b8e80941Smrg                }
150848b8605Smrg        }
151848b8605Smrg}
152