1/*
2 * Copyright © 2014-2017 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 "util/u_format.h"
25#include "util/u_half.h"
26#include "v3d_context.h"
27#include "broadcom/common/v3d_macros.h"
28#include "broadcom/cle/v3dx_pack.h"
29#include "broadcom/compiler/v3d_compiler.h"
30
31static uint8_t
32v3d_factor(enum pipe_blendfactor factor, bool dst_alpha_one)
33{
34        /* We may get a bad blendfactor when blending is disabled. */
35        if (factor == 0)
36                return V3D_BLEND_FACTOR_ZERO;
37
38        switch (factor) {
39        case PIPE_BLENDFACTOR_ZERO:
40                return V3D_BLEND_FACTOR_ZERO;
41        case PIPE_BLENDFACTOR_ONE:
42                return V3D_BLEND_FACTOR_ONE;
43        case PIPE_BLENDFACTOR_SRC_COLOR:
44                return V3D_BLEND_FACTOR_SRC_COLOR;
45        case PIPE_BLENDFACTOR_INV_SRC_COLOR:
46                return V3D_BLEND_FACTOR_INV_SRC_COLOR;
47        case PIPE_BLENDFACTOR_DST_COLOR:
48                return V3D_BLEND_FACTOR_DST_COLOR;
49        case PIPE_BLENDFACTOR_INV_DST_COLOR:
50                return V3D_BLEND_FACTOR_INV_DST_COLOR;
51        case PIPE_BLENDFACTOR_SRC_ALPHA:
52                return V3D_BLEND_FACTOR_SRC_ALPHA;
53        case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
54                return V3D_BLEND_FACTOR_INV_SRC_ALPHA;
55        case PIPE_BLENDFACTOR_DST_ALPHA:
56                return (dst_alpha_one ?
57                        V3D_BLEND_FACTOR_ONE :
58                        V3D_BLEND_FACTOR_DST_ALPHA);
59        case PIPE_BLENDFACTOR_INV_DST_ALPHA:
60                return (dst_alpha_one ?
61                        V3D_BLEND_FACTOR_ZERO :
62                        V3D_BLEND_FACTOR_INV_DST_ALPHA);
63        case PIPE_BLENDFACTOR_CONST_COLOR:
64                return V3D_BLEND_FACTOR_CONST_COLOR;
65        case PIPE_BLENDFACTOR_INV_CONST_COLOR:
66                return V3D_BLEND_FACTOR_INV_CONST_COLOR;
67        case PIPE_BLENDFACTOR_CONST_ALPHA:
68                return V3D_BLEND_FACTOR_CONST_ALPHA;
69        case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
70                return V3D_BLEND_FACTOR_INV_CONST_ALPHA;
71        case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
72                return (dst_alpha_one ?
73                        V3D_BLEND_FACTOR_ZERO :
74                        V3D_BLEND_FACTOR_SRC_ALPHA_SATURATE);
75        default:
76                unreachable("Bad blend factor");
77        }
78}
79
80static inline uint16_t
81swizzled_border_color(const struct v3d_device_info *devinfo,
82                      struct pipe_sampler_state *sampler,
83                      struct v3d_sampler_view *sview,
84                      int chan)
85{
86        const struct util_format_description *desc =
87                util_format_description(sview->base.format);
88        uint8_t swiz = chan;
89
90        /* If we're doing swizzling in the sampler, then only rearrange the
91         * border color for the mismatch between the VC5 texture format and
92         * the PIPE_FORMAT, since GL_ARB_texture_swizzle will be handled by
93         * the sampler's swizzle.
94         *
95         * For swizzling in the shader, we don't do any pre-swizzling of the
96         * border color.
97         */
98        if (v3d_get_tex_return_size(devinfo, sview->base.format,
99                                    sampler->compare_mode) != 32)
100                swiz = desc->swizzle[swiz];
101
102        switch (swiz) {
103        case PIPE_SWIZZLE_0:
104                return util_float_to_half(0.0);
105        case PIPE_SWIZZLE_1:
106                return util_float_to_half(1.0);
107        default:
108                return util_float_to_half(sampler->border_color.f[swiz]);
109        }
110}
111
112#if V3D_VERSION < 40
113static uint32_t
114translate_swizzle(unsigned char pipe_swizzle)
115{
116        switch (pipe_swizzle) {
117        case PIPE_SWIZZLE_0:
118                return 0;
119        case PIPE_SWIZZLE_1:
120                return 1;
121        case PIPE_SWIZZLE_X:
122        case PIPE_SWIZZLE_Y:
123        case PIPE_SWIZZLE_Z:
124        case PIPE_SWIZZLE_W:
125                return 2 + pipe_swizzle;
126        default:
127                unreachable("unknown swizzle");
128        }
129}
130
131static void
132emit_one_texture(struct v3d_context *v3d, struct v3d_texture_stateobj *stage_tex,
133                 int i)
134{
135        struct v3d_job *job = v3d->job;
136        struct pipe_sampler_state *psampler = stage_tex->samplers[i];
137        struct v3d_sampler_state *sampler = v3d_sampler_state(psampler);
138        struct pipe_sampler_view *psview = stage_tex->textures[i];
139        struct v3d_sampler_view *sview = v3d_sampler_view(psview);
140        struct pipe_resource *prsc = psview->texture;
141        struct v3d_resource *rsc = v3d_resource(prsc);
142        const struct v3d_device_info *devinfo = &v3d->screen->devinfo;
143
144        stage_tex->texture_state[i].offset =
145                v3d_cl_ensure_space(&job->indirect,
146                                    cl_packet_length(TEXTURE_SHADER_STATE),
147                                    32);
148        v3d_bo_set_reference(&stage_tex->texture_state[i].bo,
149                             job->indirect.bo);
150
151        uint32_t return_size = v3d_get_tex_return_size(devinfo, psview->format,
152                                                       psampler->compare_mode);
153
154        struct V3D33_TEXTURE_SHADER_STATE unpacked = {
155                /* XXX */
156                .border_color_red = swizzled_border_color(devinfo, psampler,
157                                                          sview, 0),
158                .border_color_green = swizzled_border_color(devinfo, psampler,
159                                                            sview, 1),
160                .border_color_blue = swizzled_border_color(devinfo, psampler,
161                                                           sview, 2),
162                .border_color_alpha = swizzled_border_color(devinfo, psampler,
163                                                            sview, 3),
164
165                /* In the normal texturing path, the LOD gets clamped between
166                 * min/max, and the base_level field (set in the sampler view
167                 * from first_level) only decides where the min/mag switch
168                 * happens, so we need to use the LOD clamps to keep us
169                 * between min and max.
170                 *
171                 * For txf, the LOD clamp is still used, despite GL not
172                 * wanting that.  We will need to have a separate
173                 * TEXTURE_SHADER_STATE that ignores psview->min/max_lod to
174                 * support txf properly.
175                 */
176                .min_level_of_detail = MIN2(psview->u.tex.first_level +
177                                            MAX2(psampler->min_lod, 0),
178                                            psview->u.tex.last_level),
179                .max_level_of_detail = MIN2(psview->u.tex.first_level +
180                                            psampler->max_lod,
181                                            psview->u.tex.last_level),
182
183                .texture_base_pointer = cl_address(rsc->bo,
184                                                   rsc->slices[0].offset),
185
186                .output_32_bit = return_size == 32,
187        };
188
189        /* Set up the sampler swizzle if we're doing 16-bit sampling.  For
190         * 32-bit, we leave swizzling up to the shader compiler.
191         *
192         * Note: Contrary to the docs, the swizzle still applies even if the
193         * return size is 32.  It's just that you probably want to swizzle in
194         * the shader, because you need the Y/Z/W channels to be defined.
195         */
196        if (return_size == 32) {
197                unpacked.swizzle_r = translate_swizzle(PIPE_SWIZZLE_X);
198                unpacked.swizzle_g = translate_swizzle(PIPE_SWIZZLE_Y);
199                unpacked.swizzle_b = translate_swizzle(PIPE_SWIZZLE_Z);
200                unpacked.swizzle_a = translate_swizzle(PIPE_SWIZZLE_W);
201        } else {
202                unpacked.swizzle_r = translate_swizzle(sview->swizzle[0]);
203                unpacked.swizzle_g = translate_swizzle(sview->swizzle[1]);
204                unpacked.swizzle_b = translate_swizzle(sview->swizzle[2]);
205                unpacked.swizzle_a = translate_swizzle(sview->swizzle[3]);
206        }
207
208        int min_img_filter = psampler->min_img_filter;
209        int min_mip_filter = psampler->min_mip_filter;
210        int mag_img_filter = psampler->mag_img_filter;
211
212        if (return_size == 32) {
213                min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;
214                min_img_filter = PIPE_TEX_FILTER_NEAREST;
215                mag_img_filter = PIPE_TEX_FILTER_NEAREST;
216        }
217
218        bool min_nearest = min_img_filter == PIPE_TEX_FILTER_NEAREST;
219        switch (min_mip_filter) {
220        case PIPE_TEX_MIPFILTER_NONE:
221                unpacked.filter += min_nearest ? 2 : 0;
222                break;
223        case PIPE_TEX_MIPFILTER_NEAREST:
224                unpacked.filter += min_nearest ? 4 : 8;
225                break;
226        case PIPE_TEX_MIPFILTER_LINEAR:
227                unpacked.filter += min_nearest ? 4 : 8;
228                unpacked.filter += 2;
229                break;
230        }
231
232        if (mag_img_filter == PIPE_TEX_FILTER_NEAREST)
233                unpacked.filter++;
234
235        if (psampler->max_anisotropy > 8)
236                unpacked.filter = V3D_TMU_FILTER_ANISOTROPIC_16_1;
237        else if (psampler->max_anisotropy > 4)
238                unpacked.filter = V3D_TMU_FILTER_ANISOTROPIC_8_1;
239        else if (psampler->max_anisotropy > 2)
240                unpacked.filter = V3D_TMU_FILTER_ANISOTROPIC_4_1;
241        else if (psampler->max_anisotropy)
242                unpacked.filter = V3D_TMU_FILTER_ANISOTROPIC_2_1;
243
244        uint8_t packed[cl_packet_length(TEXTURE_SHADER_STATE)];
245        cl_packet_pack(TEXTURE_SHADER_STATE)(&job->indirect, packed, &unpacked);
246
247        for (int i = 0; i < ARRAY_SIZE(packed); i++)
248                packed[i] |= sview->texture_shader_state[i] | sampler->texture_shader_state[i];
249
250        /* TMU indirect structs need to be 32b aligned. */
251        v3d_cl_ensure_space(&job->indirect, ARRAY_SIZE(packed), 32);
252        cl_emit_prepacked(&job->indirect, &packed);
253}
254
255static void
256emit_textures(struct v3d_context *v3d, struct v3d_texture_stateobj *stage_tex)
257{
258        for (int i = 0; i < stage_tex->num_textures; i++) {
259                if (stage_tex->textures[i])
260                        emit_one_texture(v3d, stage_tex, i);
261        }
262}
263#endif /* V3D_VERSION < 40 */
264
265static uint32_t
266translate_colormask(struct v3d_context *v3d, uint32_t colormask, int rt)
267{
268        if (v3d->swap_color_rb & (1 << rt)) {
269                colormask = ((colormask & (2 | 8)) |
270                             ((colormask & 1) << 2) |
271                             ((colormask & 4) >> 2));
272        }
273
274        return (~colormask) & 0xf;
275}
276
277static void
278emit_rt_blend(struct v3d_context *v3d, struct v3d_job *job,
279              struct pipe_blend_state *blend, int rt)
280{
281        struct pipe_rt_blend_state *rtblend = &blend->rt[rt];
282
283#if V3D_VERSION >= 40
284        /* We don't need to emit blend state for disabled RTs. */
285        if (!rtblend->blend_enable)
286                return;
287#endif
288
289        cl_emit(&job->bcl, BLEND_CFG, config) {
290#if V3D_VERSION >= 40
291                if (blend->independent_blend_enable)
292                        config.render_target_mask = 1 << rt;
293                else
294                        config.render_target_mask = (1 << V3D_MAX_DRAW_BUFFERS) - 1;
295#else
296                assert(rt == 0);
297#endif
298
299                config.color_blend_mode = rtblend->rgb_func;
300                config.color_blend_dst_factor =
301                        v3d_factor(rtblend->rgb_dst_factor,
302                                   v3d->blend_dst_alpha_one);
303                config.color_blend_src_factor =
304                        v3d_factor(rtblend->rgb_src_factor,
305                                   v3d->blend_dst_alpha_one);
306
307                config.alpha_blend_mode = rtblend->alpha_func;
308                config.alpha_blend_dst_factor =
309                        v3d_factor(rtblend->alpha_dst_factor,
310                                   v3d->blend_dst_alpha_one);
311                config.alpha_blend_src_factor =
312                        v3d_factor(rtblend->alpha_src_factor,
313                                   v3d->blend_dst_alpha_one);
314        }
315}
316
317static void
318emit_flat_shade_flags(struct v3d_job *job,
319                      int varying_offset,
320                      uint32_t varyings,
321                      enum V3DX(Varying_Flags_Action) lower,
322                      enum V3DX(Varying_Flags_Action) higher)
323{
324        cl_emit(&job->bcl, FLAT_SHADE_FLAGS, flags) {
325                flags.varying_offset_v0 = varying_offset;
326                flags.flat_shade_flags_for_varyings_v024 = varyings;
327                flags.action_for_flat_shade_flags_of_lower_numbered_varyings =
328                        lower;
329                flags.action_for_flat_shade_flags_of_higher_numbered_varyings =
330                        higher;
331        }
332}
333
334#if V3D_VERSION >= 40
335static void
336emit_noperspective_flags(struct v3d_job *job,
337                         int varying_offset,
338                         uint32_t varyings,
339                         enum V3DX(Varying_Flags_Action) lower,
340                         enum V3DX(Varying_Flags_Action) higher)
341{
342        cl_emit(&job->bcl, NON_PERSPECTIVE_FLAGS, flags) {
343                flags.varying_offset_v0 = varying_offset;
344                flags.non_perspective_flags_for_varyings_v024 = varyings;
345                flags.action_for_non_perspective_flags_of_lower_numbered_varyings =
346                        lower;
347                flags.action_for_non_perspective_flags_of_higher_numbered_varyings =
348                        higher;
349        }
350}
351
352static void
353emit_centroid_flags(struct v3d_job *job,
354                    int varying_offset,
355                    uint32_t varyings,
356                    enum V3DX(Varying_Flags_Action) lower,
357                    enum V3DX(Varying_Flags_Action) higher)
358{
359        cl_emit(&job->bcl, CENTROID_FLAGS, flags) {
360                flags.varying_offset_v0 = varying_offset;
361                flags.centroid_flags_for_varyings_v024 = varyings;
362                flags.action_for_centroid_flags_of_lower_numbered_varyings =
363                        lower;
364                flags.action_for_centroid_flags_of_higher_numbered_varyings =
365                        higher;
366        }
367}
368#endif /* V3D_VERSION >= 40 */
369
370static bool
371emit_varying_flags(struct v3d_job *job, uint32_t *flags,
372                   void (*flag_emit_callback)(struct v3d_job *job,
373                                              int varying_offset,
374                                              uint32_t flags,
375                                              enum V3DX(Varying_Flags_Action) lower,
376                                              enum V3DX(Varying_Flags_Action) higher))
377{
378        struct v3d_context *v3d = job->v3d;
379        bool emitted_any = false;
380
381        for (int i = 0; i < ARRAY_SIZE(v3d->prog.fs->prog_data.fs->flat_shade_flags); i++) {
382                if (!flags[i])
383                        continue;
384
385                if (emitted_any) {
386                        flag_emit_callback(job, i, flags[i],
387                                           V3D_VARYING_FLAGS_ACTION_UNCHANGED,
388                                           V3D_VARYING_FLAGS_ACTION_UNCHANGED);
389                } else if (i == 0) {
390                        flag_emit_callback(job, i, flags[i],
391                                           V3D_VARYING_FLAGS_ACTION_UNCHANGED,
392                                           V3D_VARYING_FLAGS_ACTION_ZEROED);
393                } else {
394                        flag_emit_callback(job, i, flags[i],
395                                           V3D_VARYING_FLAGS_ACTION_ZEROED,
396                                           V3D_VARYING_FLAGS_ACTION_ZEROED);
397                }
398                emitted_any = true;
399        }
400
401        return emitted_any;
402}
403
404void
405v3dX(emit_state)(struct pipe_context *pctx)
406{
407        struct v3d_context *v3d = v3d_context(pctx);
408        struct v3d_job *job = v3d->job;
409        bool rasterizer_discard = v3d->rasterizer->base.rasterizer_discard;
410
411        if (v3d->dirty & (VC5_DIRTY_SCISSOR | VC5_DIRTY_VIEWPORT |
412                          VC5_DIRTY_RASTERIZER)) {
413                float *vpscale = v3d->viewport.scale;
414                float *vptranslate = v3d->viewport.translate;
415                float vp_minx = -fabsf(vpscale[0]) + vptranslate[0];
416                float vp_maxx = fabsf(vpscale[0]) + vptranslate[0];
417                float vp_miny = -fabsf(vpscale[1]) + vptranslate[1];
418                float vp_maxy = fabsf(vpscale[1]) + vptranslate[1];
419
420                /* Clip to the scissor if it's enabled, but still clip to the
421                 * drawable regardless since that controls where the binner
422                 * tries to put things.
423                 *
424                 * Additionally, always clip the rendering to the viewport,
425                 * since the hardware does guardband clipping, meaning
426                 * primitives would rasterize outside of the view volume.
427                 */
428                uint32_t minx, miny, maxx, maxy;
429                if (!v3d->rasterizer->base.scissor) {
430                        minx = MAX2(vp_minx, 0);
431                        miny = MAX2(vp_miny, 0);
432                        maxx = MIN2(vp_maxx, job->draw_width);
433                        maxy = MIN2(vp_maxy, job->draw_height);
434                } else {
435                        minx = MAX2(vp_minx, v3d->scissor.minx);
436                        miny = MAX2(vp_miny, v3d->scissor.miny);
437                        maxx = MIN2(vp_maxx, v3d->scissor.maxx);
438                        maxy = MIN2(vp_maxy, v3d->scissor.maxy);
439                }
440
441                cl_emit(&job->bcl, CLIP_WINDOW, clip) {
442                        clip.clip_window_left_pixel_coordinate = minx;
443                        clip.clip_window_bottom_pixel_coordinate = miny;
444                        if (maxx > minx && maxy > miny) {
445                                clip.clip_window_width_in_pixels = maxx - minx;
446                                clip.clip_window_height_in_pixels = maxy - miny;
447                        } else if (V3D_VERSION < 41) {
448                                /* The HW won't entirely clip out when scissor
449                                 * w/h is 0.  Just treat it the same as
450                                 * rasterizer discard.
451                                 */
452                                rasterizer_discard = true;
453                                clip.clip_window_width_in_pixels = 1;
454                                clip.clip_window_height_in_pixels = 1;
455                        }
456                }
457
458                job->draw_min_x = MIN2(job->draw_min_x, minx);
459                job->draw_min_y = MIN2(job->draw_min_y, miny);
460                job->draw_max_x = MAX2(job->draw_max_x, maxx);
461                job->draw_max_y = MAX2(job->draw_max_y, maxy);
462        }
463
464        if (v3d->dirty & (VC5_DIRTY_RASTERIZER |
465                          VC5_DIRTY_ZSA |
466                          VC5_DIRTY_BLEND |
467                          VC5_DIRTY_COMPILED_FS)) {
468                cl_emit(&job->bcl, CFG_BITS, config) {
469                        config.enable_forward_facing_primitive =
470                                !rasterizer_discard &&
471                                !(v3d->rasterizer->base.cull_face &
472                                  PIPE_FACE_FRONT);
473                        config.enable_reverse_facing_primitive =
474                                !rasterizer_discard &&
475                                !(v3d->rasterizer->base.cull_face &
476                                  PIPE_FACE_BACK);
477                        /* This seems backwards, but it's what gets the
478                         * clipflat test to pass.
479                         */
480                        config.clockwise_primitives =
481                                v3d->rasterizer->base.front_ccw;
482
483                        config.enable_depth_offset =
484                                v3d->rasterizer->base.offset_tri;
485
486                        /* V3D follows GL behavior where the sample mask only
487                         * applies when MSAA is enabled.  Gallium has sample
488                         * mask apply anyway, and the MSAA blit shaders will
489                         * set sample mask without explicitly setting
490                         * rasterizer oversample.  Just force it on here,
491                         * since the blit shaders are the only way to have
492                         * !multisample && samplemask != 0xf.
493                         */
494                        config.rasterizer_oversample_mode =
495                                v3d->rasterizer->base.multisample ||
496                                v3d->sample_mask != 0xf;
497
498                        config.direct3d_provoking_vertex =
499                                v3d->rasterizer->base.flatshade_first;
500
501                        config.blend_enable = v3d->blend->blend_enables;
502
503                        /* Note: EZ state may update based on the compiled FS,
504                         * along with ZSA
505                         */
506                        config.early_z_updates_enable =
507                                (job->ez_state != VC5_EZ_DISABLED);
508                        if (v3d->zsa->base.depth.enabled) {
509                                config.z_updates_enable =
510                                        v3d->zsa->base.depth.writemask;
511                                config.early_z_enable =
512                                        config.early_z_updates_enable;
513                                config.depth_test_function =
514                                        v3d->zsa->base.depth.func;
515                        } else {
516                                config.depth_test_function = PIPE_FUNC_ALWAYS;
517                        }
518
519                        config.stencil_enable =
520                                v3d->zsa->base.stencil[0].enabled;
521                }
522
523        }
524
525        if (v3d->dirty & VC5_DIRTY_RASTERIZER &&
526            v3d->rasterizer->base.offset_tri) {
527                if (job->zsbuf &&
528                    job->zsbuf->format == PIPE_FORMAT_Z16_UNORM) {
529                        cl_emit_prepacked_sized(&job->bcl,
530                                                v3d->rasterizer->depth_offset_z16,
531                                                cl_packet_length(DEPTH_OFFSET));
532                } else {
533                        cl_emit_prepacked_sized(&job->bcl,
534                                                v3d->rasterizer->depth_offset,
535                                                cl_packet_length(DEPTH_OFFSET));
536                }
537        }
538
539        if (v3d->dirty & VC5_DIRTY_RASTERIZER) {
540                cl_emit(&job->bcl, POINT_SIZE, point_size) {
541                        point_size.point_size = v3d->rasterizer->point_size;
542                }
543
544                cl_emit(&job->bcl, LINE_WIDTH, line_width) {
545                        line_width.line_width = v3d->rasterizer->base.line_width;
546                }
547        }
548
549        if (v3d->dirty & VC5_DIRTY_VIEWPORT) {
550                cl_emit(&job->bcl, CLIPPER_XY_SCALING, clip) {
551                        clip.viewport_half_width_in_1_256th_of_pixel =
552                                v3d->viewport.scale[0] * 256.0f;
553                        clip.viewport_half_height_in_1_256th_of_pixel =
554                                v3d->viewport.scale[1] * 256.0f;
555                }
556
557                cl_emit(&job->bcl, CLIPPER_Z_SCALE_AND_OFFSET, clip) {
558                        clip.viewport_z_offset_zc_to_zs =
559                                v3d->viewport.translate[2];
560                        clip.viewport_z_scale_zc_to_zs =
561                                v3d->viewport.scale[2];
562                }
563                cl_emit(&job->bcl, CLIPPER_Z_MIN_MAX_CLIPPING_PLANES, clip) {
564                        float z1 = (v3d->viewport.translate[2] -
565                                    v3d->viewport.scale[2]);
566                        float z2 = (v3d->viewport.translate[2] +
567                                    v3d->viewport.scale[2]);
568                        clip.minimum_zw = MIN2(z1, z2);
569                        clip.maximum_zw = MAX2(z1, z2);
570                }
571
572                cl_emit(&job->bcl, VIEWPORT_OFFSET, vp) {
573                        vp.viewport_centre_x_coordinate =
574                                v3d->viewport.translate[0];
575                        vp.viewport_centre_y_coordinate =
576                                v3d->viewport.translate[1];
577                }
578        }
579
580        if (v3d->dirty & VC5_DIRTY_BLEND) {
581                struct v3d_blend_state *blend = v3d->blend;
582
583                if (blend->blend_enables) {
584#if V3D_VERSION >= 40
585                        cl_emit(&job->bcl, BLEND_ENABLES, enables) {
586                                enables.mask = blend->blend_enables;
587                        }
588#endif
589
590                        if (blend->base.independent_blend_enable) {
591                                for (int i = 0; i < V3D_MAX_DRAW_BUFFERS; i++)
592                                        emit_rt_blend(v3d, job, &blend->base, i);
593                        } else {
594                                emit_rt_blend(v3d, job, &blend->base, 0);
595                        }
596                }
597        }
598
599        if (v3d->dirty & VC5_DIRTY_BLEND) {
600                struct pipe_blend_state *blend = &v3d->blend->base;
601
602                cl_emit(&job->bcl, COLOR_WRITE_MASKS, mask) {
603                        for (int i = 0; i < 4; i++) {
604                                int rt = blend->independent_blend_enable ? i : 0;
605                                int rt_mask = blend->rt[rt].colormask;
606
607                                mask.mask |= translate_colormask(v3d, rt_mask,
608                                                                 i) << (4 * i);
609                        }
610                }
611        }
612
613        /* GFXH-1431: On V3D 3.x, writing BLEND_CONFIG resets the constant
614         * color.
615         */
616        if (v3d->dirty & VC5_DIRTY_BLEND_COLOR ||
617            (V3D_VERSION < 41 && (v3d->dirty & VC5_DIRTY_BLEND))) {
618                cl_emit(&job->bcl, BLEND_CONSTANT_COLOR, color) {
619                        color.red_f16 = (v3d->swap_color_rb ?
620                                          v3d->blend_color.hf[2] :
621                                          v3d->blend_color.hf[0]);
622                        color.green_f16 = v3d->blend_color.hf[1];
623                        color.blue_f16 = (v3d->swap_color_rb ?
624                                           v3d->blend_color.hf[0] :
625                                           v3d->blend_color.hf[2]);
626                        color.alpha_f16 = v3d->blend_color.hf[3];
627                }
628        }
629
630        if (v3d->dirty & (VC5_DIRTY_ZSA | VC5_DIRTY_STENCIL_REF)) {
631                struct pipe_stencil_state *front = &v3d->zsa->base.stencil[0];
632                struct pipe_stencil_state *back = &v3d->zsa->base.stencil[1];
633
634                if (front->enabled) {
635                        cl_emit_with_prepacked(&job->bcl, STENCIL_CFG,
636                                               v3d->zsa->stencil_front, config) {
637                                config.stencil_ref_value =
638                                        v3d->stencil_ref.ref_value[0];
639                        }
640                }
641
642                if (back->enabled) {
643                        cl_emit_with_prepacked(&job->bcl, STENCIL_CFG,
644                                               v3d->zsa->stencil_back, config) {
645                                config.stencil_ref_value =
646                                        v3d->stencil_ref.ref_value[1];
647                        }
648                }
649        }
650
651#if V3D_VERSION < 40
652        /* Pre-4.x, we have texture state that depends on both the sampler and
653         * the view, so we merge them together at draw time.
654         */
655        if (v3d->dirty & VC5_DIRTY_FRAGTEX)
656                emit_textures(v3d, &v3d->tex[PIPE_SHADER_FRAGMENT]);
657
658        if (v3d->dirty & VC5_DIRTY_VERTTEX)
659                emit_textures(v3d, &v3d->tex[PIPE_SHADER_VERTEX]);
660#endif
661
662        if (v3d->dirty & VC5_DIRTY_FLAT_SHADE_FLAGS) {
663                if (!emit_varying_flags(job,
664                                        v3d->prog.fs->prog_data.fs->flat_shade_flags,
665                                        emit_flat_shade_flags)) {
666                        cl_emit(&job->bcl, ZERO_ALL_FLAT_SHADE_FLAGS, flags);
667                }
668        }
669
670#if V3D_VERSION >= 40
671        if (v3d->dirty & VC5_DIRTY_NOPERSPECTIVE_FLAGS) {
672                if (!emit_varying_flags(job,
673                                        v3d->prog.fs->prog_data.fs->noperspective_flags,
674                                        emit_noperspective_flags)) {
675                        cl_emit(&job->bcl, ZERO_ALL_NON_PERSPECTIVE_FLAGS, flags);
676                }
677        }
678
679        if (v3d->dirty & VC5_DIRTY_CENTROID_FLAGS) {
680                if (!emit_varying_flags(job,
681                                        v3d->prog.fs->prog_data.fs->centroid_flags,
682                                        emit_centroid_flags)) {
683                        cl_emit(&job->bcl, ZERO_ALL_CENTROID_FLAGS, flags);
684                }
685        }
686#endif
687
688        /* Set up the transform feedback data specs (which VPM entries to
689         * output to which buffers).
690         */
691        if (v3d->dirty & (VC5_DIRTY_STREAMOUT |
692                          VC5_DIRTY_RASTERIZER |
693                          VC5_DIRTY_PRIM_MODE)) {
694                struct v3d_streamout_stateobj *so = &v3d->streamout;
695
696                if (so->num_targets) {
697                        bool psiz_per_vertex = (v3d->prim_mode == PIPE_PRIM_POINTS &&
698                                                v3d->rasterizer->base.point_size_per_vertex);
699                        uint16_t *tf_specs = (psiz_per_vertex ?
700                                              v3d->prog.bind_vs->tf_specs_psiz :
701                                              v3d->prog.bind_vs->tf_specs);
702
703#if V3D_VERSION >= 40
704                        bool tf_enabled = (v3d->prog.bind_vs->num_tf_specs != 0 &&
705                                           v3d->active_queries);
706                        job->tf_enabled |= tf_enabled;
707
708                        cl_emit(&job->bcl, TRANSFORM_FEEDBACK_SPECS, tfe) {
709                                tfe.number_of_16_bit_output_data_specs_following =
710                                        v3d->prog.bind_vs->num_tf_specs;
711                                tfe.enable = tf_enabled;
712                        };
713#else /* V3D_VERSION < 40 */
714                        cl_emit(&job->bcl, TRANSFORM_FEEDBACK_ENABLE, tfe) {
715                                tfe.number_of_32_bit_output_buffer_address_following =
716                                        so->num_targets;
717                                tfe.number_of_16_bit_output_data_specs_following =
718                                        v3d->prog.bind_vs->num_tf_specs;
719                        };
720#endif /* V3D_VERSION < 40 */
721                        for (int i = 0; i < v3d->prog.bind_vs->num_tf_specs; i++) {
722                                cl_emit_prepacked(&job->bcl, &tf_specs[i]);
723                        }
724                } else {
725#if V3D_VERSION >= 40
726                        cl_emit(&job->bcl, TRANSFORM_FEEDBACK_SPECS, tfe) {
727                                tfe.enable = false;
728                        };
729#endif /* V3D_VERSION >= 40 */
730                }
731        }
732
733        /* Set up the trasnform feedback buffers. */
734        if (v3d->dirty & VC5_DIRTY_STREAMOUT) {
735                struct v3d_streamout_stateobj *so = &v3d->streamout;
736                for (int i = 0; i < so->num_targets; i++) {
737                        const struct pipe_stream_output_target *target =
738                                so->targets[i];
739                        struct v3d_resource *rsc = target ?
740                                v3d_resource(target->buffer) : NULL;
741                        struct pipe_shader_state *vs = &v3d->prog.bind_vs->base;
742                        struct pipe_stream_output_info *info = &vs->stream_output;
743                        uint32_t offset = (v3d->streamout.offsets[i] *
744                                           info->stride[i] * 4);
745
746#if V3D_VERSION >= 40
747                        if (!target)
748                                continue;
749
750                        cl_emit(&job->bcl, TRANSFORM_FEEDBACK_BUFFER, output) {
751                                output.buffer_address =
752                                        cl_address(rsc->bo,
753                                                   target->buffer_offset +
754                                                   offset);
755                                output.buffer_size_in_32_bit_words =
756                                        (target->buffer_size - offset) >> 2;
757                                output.buffer_number = i;
758                        }
759#else /* V3D_VERSION < 40 */
760                        cl_emit(&job->bcl, TRANSFORM_FEEDBACK_OUTPUT_ADDRESS, output) {
761                                if (target) {
762                                        output.address =
763                                                cl_address(rsc->bo,
764                                                           target->buffer_offset +
765                                                           offset);
766                                }
767                        };
768#endif /* V3D_VERSION < 40 */
769                        if (target) {
770                                v3d_job_add_write_resource(v3d->job,
771                                                           target->buffer);
772                        }
773                        /* XXX: buffer_size? */
774                }
775        }
776
777        if (v3d->dirty & VC5_DIRTY_OQ) {
778                cl_emit(&job->bcl, OCCLUSION_QUERY_COUNTER, counter) {
779                        if (v3d->active_queries && v3d->current_oq) {
780                                counter.address = cl_address(v3d->current_oq, 0);
781                        }
782                }
783        }
784
785#if V3D_VERSION >= 40
786        if (v3d->dirty & VC5_DIRTY_SAMPLE_STATE) {
787                cl_emit(&job->bcl, SAMPLE_STATE, state) {
788                        /* Note: SampleCoverage was handled at the
789                         * state_tracker level by converting to sample_mask.
790                         */
791                        state.coverage = 1.0;
792                        state.mask = job->msaa ? v3d->sample_mask : 0xf;
793                }
794        }
795#endif
796}
797