1/*
2 * Copyright (C) 2016 Rob Clark <robclark@freedesktop.org>
3 * Copyright © 2018 Google, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * Authors:
25 *    Rob Clark <robclark@freedesktop.org>
26 */
27
28#include "pipe/p_state.h"
29#include "util/u_memory.h"
30#include "util/u_string.h"
31
32#include "fd6_context.h"
33#include "fd6_format.h"
34#include "fd6_pack.h"
35#include "fd6_rasterizer.h"
36
37struct fd_ringbuffer *
38__fd6_setup_rasterizer_stateobj(struct fd_context *ctx,
39                                const struct pipe_rasterizer_state *cso,
40                                bool primitive_restart)
41{
42   struct fd_ringbuffer *ring = fd_ringbuffer_new_object(ctx->pipe, 26 * 4);
43   float psize_min, psize_max;
44
45   if (cso->point_size_per_vertex) {
46      psize_min = util_get_min_point_size(cso);
47      psize_max = 4092;
48   } else {
49      /* Force the point size to be as if the vertex output was disabled. */
50      psize_min = cso->point_size;
51      psize_max = cso->point_size;
52   }
53
54   OUT_REG(ring, A6XX_GRAS_CL_CNTL(.znear_clip_disable = !cso->depth_clip_near,
55                                   .zfar_clip_disable = !cso->depth_clip_far,
56                                   .unk5 = !cso->depth_clip_near ||
57                                           !cso->depth_clip_far,
58                                   .vp_clip_code_ignore = 1,
59                                   .zero_gb_scale_z = cso->clip_halfz));
60
61   OUT_REG(ring,
62           A6XX_GRAS_SU_CNTL(.linehalfwidth = cso->line_width / 2.0,
63                             .poly_offset = cso->offset_tri,
64                             .line_mode = cso->multisample ? RECTANGULAR : BRESENHAM,
65                             .cull_front = cso->cull_face & PIPE_FACE_FRONT,
66                             .cull_back = cso->cull_face & PIPE_FACE_BACK,
67                             .front_cw = !cso->front_ccw, ));
68
69   OUT_REG(ring,
70           A6XX_GRAS_SU_POINT_MINMAX(.min = psize_min, .max = psize_max, ),
71           A6XX_GRAS_SU_POINT_SIZE(cso->point_size));
72
73   OUT_REG(ring, A6XX_GRAS_SU_POLY_OFFSET_SCALE(cso->offset_scale),
74           A6XX_GRAS_SU_POLY_OFFSET_OFFSET(cso->offset_units),
75           A6XX_GRAS_SU_POLY_OFFSET_OFFSET_CLAMP(cso->offset_clamp));
76
77   OUT_REG(ring,
78           A6XX_PC_PRIMITIVE_CNTL_0(.provoking_vtx_last = !cso->flatshade_first,
79                                    .primitive_restart = primitive_restart, ));
80
81   enum a6xx_polygon_mode mode = POLYMODE6_TRIANGLES;
82   switch (cso->fill_front) {
83   case PIPE_POLYGON_MODE_POINT:
84      mode = POLYMODE6_POINTS;
85      break;
86   case PIPE_POLYGON_MODE_LINE:
87      mode = POLYMODE6_LINES;
88      break;
89   default:
90      assert(cso->fill_front == PIPE_POLYGON_MODE_FILL);
91      break;
92   }
93
94   OUT_REG(ring, A6XX_VPC_POLYGON_MODE(mode));
95   OUT_REG(ring, A6XX_PC_POLYGON_MODE(mode));
96
97   if (ctx->screen->info->a6xx.has_shading_rate) {
98      OUT_REG(ring, A6XX_RB_UNKNOWN_8A00());
99      OUT_REG(ring, A6XX_RB_UNKNOWN_8A10());
100      OUT_REG(ring, A6XX_RB_UNKNOWN_8A20());
101      OUT_REG(ring, A6XX_RB_UNKNOWN_8A30());
102   }
103
104   return ring;
105}
106
107void *
108fd6_rasterizer_state_create(struct pipe_context *pctx,
109                            const struct pipe_rasterizer_state *cso)
110{
111   struct fd6_rasterizer_stateobj *so;
112
113   so = CALLOC_STRUCT(fd6_rasterizer_stateobj);
114   if (!so)
115      return NULL;
116
117   so->base = *cso;
118
119   return so;
120}
121
122void
123fd6_rasterizer_state_delete(struct pipe_context *pctx, void *hwcso)
124{
125   struct fd6_rasterizer_stateobj *so = hwcso;
126
127   for (unsigned i = 0; i < ARRAY_SIZE(so->stateobjs); i++)
128      if (so->stateobjs[i])
129         fd_ringbuffer_del(so->stateobjs[i]);
130
131   FREE(hwcso);
132}
133