1/*
2 * Copyright © 2014 Broadcom
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 "vc4_context.h"
25
26void
27vc4_emit_state(struct pipe_context *pctx)
28{
29        struct vc4_context *vc4 = vc4_context(pctx);
30        struct vc4_job *job = vc4->job;
31
32        if (vc4->dirty & (VC4_DIRTY_SCISSOR | VC4_DIRTY_VIEWPORT |
33                          VC4_DIRTY_RASTERIZER)) {
34                float *vpscale = vc4->viewport.scale;
35                float *vptranslate = vc4->viewport.translate;
36                float vp_minx = -fabsf(vpscale[0]) + vptranslate[0];
37                float vp_maxx = fabsf(vpscale[0]) + vptranslate[0];
38                float vp_miny = -fabsf(vpscale[1]) + vptranslate[1];
39                float vp_maxy = fabsf(vpscale[1]) + vptranslate[1];
40
41                /* Clip to the scissor if it's enabled, but still clip to the
42                 * drawable regardless since that controls where the binner
43                 * tries to put things.
44                 *
45                 * Additionally, always clip the rendering to the viewport,
46                 * since the hardware does guardband clipping, meaning
47                 * primitives would rasterize outside of the view volume.
48                 */
49                uint32_t minx, miny, maxx, maxy;
50                if (!vc4->rasterizer->base.scissor) {
51                        minx = MAX2(vp_minx, 0);
52                        miny = MAX2(vp_miny, 0);
53                        maxx = MAX2(MIN2(vp_maxx, job->draw_width), minx);
54                        maxy = MAX2(MIN2(vp_maxy, job->draw_height), miny);
55                } else {
56                        minx = MAX2(vp_minx, vc4->scissor.minx);
57                        miny = MAX2(vp_miny, vc4->scissor.miny);
58                        maxx = MAX2(MIN2(vp_maxx, vc4->scissor.maxx), minx);
59                        maxy = MAX2(MIN2(vp_maxy, vc4->scissor.maxy), miny);
60                }
61
62                cl_emit(&job->bcl, CLIP_WINDOW, clip) {
63                        clip.clip_window_left_pixel_coordinate = minx;
64                        clip.clip_window_bottom_pixel_coordinate = miny;
65                        clip.clip_window_height_in_pixels = maxy - miny;
66                        clip.clip_window_width_in_pixels = maxx - minx;
67                }
68
69                job->draw_min_x = MIN2(job->draw_min_x, minx);
70                job->draw_min_y = MIN2(job->draw_min_y, miny);
71                job->draw_max_x = MAX2(job->draw_max_x, maxx);
72                job->draw_max_y = MAX2(job->draw_max_y, maxy);
73        }
74
75        if (vc4->dirty & (VC4_DIRTY_RASTERIZER |
76                          VC4_DIRTY_ZSA |
77                          VC4_DIRTY_COMPILED_FS)) {
78                uint8_t ez_enable_mask_out = ~0;
79                uint8_t rasosm_mask_out = ~0;
80
81                struct vc4_cl_out *bcl = cl_start(&job->bcl);
82                /* HW-2905: If the RCL ends up doing a full-res load when
83                 * multisampling, then early Z tracking may end up with values
84                 * from the previous tile due to a HW bug.  Disable it to
85                 * avoid that.
86                 *
87                 * We should be able to skip this when the Z is cleared, but I
88                 * was seeing bad rendering on glxgears -samples 4 even in
89                 * that case.
90                 */
91                if (job->msaa || vc4->prog.fs->disable_early_z)
92                        ez_enable_mask_out &= ~VC4_CONFIG_BITS_EARLY_Z;
93
94                /* Don't set the rasterizer to oversample if we're doing our
95                 * binning and load/stores in single-sample mode.  This is for
96                 * the samples == 1 case, where vc4 doesn't do any
97                 * multisampling behavior.
98                 */
99                if (!job->msaa) {
100                        rasosm_mask_out &=
101                                ~VC4_CONFIG_BITS_RASTERIZER_OVERSAMPLE_4X;
102                }
103
104                cl_u8(&bcl, VC4_PACKET_CONFIGURATION_BITS);
105                cl_u8(&bcl,
106                      (vc4->rasterizer->config_bits[0] |
107                       vc4->zsa->config_bits[0]) & rasosm_mask_out);
108                cl_u8(&bcl,
109                      vc4->rasterizer->config_bits[1] |
110                      vc4->zsa->config_bits[1]);
111                cl_u8(&bcl,
112                      (vc4->rasterizer->config_bits[2] |
113                       vc4->zsa->config_bits[2]) & ez_enable_mask_out);
114                cl_end(&job->bcl, bcl);
115        }
116
117        if (vc4->dirty & VC4_DIRTY_RASTERIZER) {
118                cl_emit_prepacked(&job->bcl, &vc4->rasterizer->packed);
119        }
120
121        if (vc4->dirty & VC4_DIRTY_VIEWPORT) {
122                cl_emit(&job->bcl, CLIPPER_XY_SCALING, clip) {
123                        clip.viewport_half_width_in_1_16th_of_pixel =
124                                vc4->viewport.scale[0] * 16.0f;
125                        clip.viewport_half_height_in_1_16th_of_pixel =
126                                vc4->viewport.scale[1] * 16.0f;
127                }
128
129                cl_emit(&job->bcl, CLIPPER_Z_SCALE_AND_OFFSET, clip) {
130                        clip.viewport_z_offset_zc_to_zs =
131                                vc4->viewport.translate[2];
132                        clip.viewport_z_scale_zc_to_zs =
133                                vc4->viewport.scale[2];
134                }
135
136                cl_emit(&job->bcl, VIEWPORT_OFFSET, vp) {
137                        vp.viewport_centre_x_coordinate =
138                                vc4->viewport.translate[0];
139                        vp.viewport_centre_y_coordinate =
140                                vc4->viewport.translate[1];
141                }
142        }
143
144        if (vc4->dirty & VC4_DIRTY_FLAT_SHADE_FLAGS) {
145                cl_emit(&job->bcl, FLAT_SHADE_FLAGS, flags) {
146                        if (vc4->rasterizer->base.flatshade)
147                                flags.flat_shading_flags =
148                                        vc4->prog.fs->color_inputs;
149                }
150        }
151}
152