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