1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2021 Collabora Ltd.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#ifndef PANVK_VARYINGS_H
25bf215546Sopenharmony_ci#define PANVK_VARYINGS_H
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "util/bitset.h"
28bf215546Sopenharmony_ci#include "util/format/u_format.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "compiler/shader_enums.h"
31bf215546Sopenharmony_ci#include "panfrost-job.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci#include "pan_pool.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_cistruct pan_pool;
36bf215546Sopenharmony_cistruct panvk_device;
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_cienum panvk_varying_buf_id {
39bf215546Sopenharmony_ci   PANVK_VARY_BUF_GENERAL,
40bf215546Sopenharmony_ci   PANVK_VARY_BUF_POSITION,
41bf215546Sopenharmony_ci   PANVK_VARY_BUF_PSIZ,
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci   /* Keep last */
44bf215546Sopenharmony_ci   PANVK_VARY_BUF_MAX,
45bf215546Sopenharmony_ci};
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_cistruct panvk_varying {
48bf215546Sopenharmony_ci   unsigned buf;
49bf215546Sopenharmony_ci   unsigned offset;
50bf215546Sopenharmony_ci   enum pipe_format format;
51bf215546Sopenharmony_ci};
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_cistruct panvk_varying_buf {
54bf215546Sopenharmony_ci   mali_ptr address;
55bf215546Sopenharmony_ci   void *cpu;
56bf215546Sopenharmony_ci   unsigned stride;
57bf215546Sopenharmony_ci   unsigned size;
58bf215546Sopenharmony_ci};
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_cistruct panvk_varyings_info {
61bf215546Sopenharmony_ci   struct panvk_varying varying[VARYING_SLOT_MAX];
62bf215546Sopenharmony_ci   BITSET_DECLARE(active, VARYING_SLOT_MAX);
63bf215546Sopenharmony_ci   struct panvk_varying_buf buf[VARYING_SLOT_MAX];
64bf215546Sopenharmony_ci   struct {
65bf215546Sopenharmony_ci      unsigned count;
66bf215546Sopenharmony_ci      gl_varying_slot loc[VARYING_SLOT_MAX];
67bf215546Sopenharmony_ci   } stage[MESA_SHADER_STAGES];
68bf215546Sopenharmony_ci   unsigned buf_mask;
69bf215546Sopenharmony_ci};
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_cistatic inline unsigned
72bf215546Sopenharmony_cipanvk_varying_buf_index(const struct panvk_varyings_info *varyings,
73bf215546Sopenharmony_ci                        enum panvk_varying_buf_id b)
74bf215546Sopenharmony_ci{
75bf215546Sopenharmony_ci   return util_bitcount(varyings->buf_mask & BITFIELD_MASK(b));
76bf215546Sopenharmony_ci}
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_cistatic inline enum panvk_varying_buf_id
79bf215546Sopenharmony_cipanvk_varying_buf_id(gl_varying_slot loc)
80bf215546Sopenharmony_ci{
81bf215546Sopenharmony_ci   switch (loc) {
82bf215546Sopenharmony_ci   case VARYING_SLOT_POS:
83bf215546Sopenharmony_ci      return PANVK_VARY_BUF_POSITION;
84bf215546Sopenharmony_ci   case VARYING_SLOT_PSIZ:
85bf215546Sopenharmony_ci      return PANVK_VARY_BUF_PSIZ;
86bf215546Sopenharmony_ci   default:
87bf215546Sopenharmony_ci      return PANVK_VARY_BUF_GENERAL;
88bf215546Sopenharmony_ci   }
89bf215546Sopenharmony_ci}
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_cistatic inline unsigned
92bf215546Sopenharmony_cipanvk_varying_size(const struct panvk_varyings_info *varyings,
93bf215546Sopenharmony_ci                   gl_varying_slot loc)
94bf215546Sopenharmony_ci{
95bf215546Sopenharmony_ci   switch (loc) {
96bf215546Sopenharmony_ci   case VARYING_SLOT_POS:
97bf215546Sopenharmony_ci      return sizeof(float) * 4;
98bf215546Sopenharmony_ci   case VARYING_SLOT_PSIZ:
99bf215546Sopenharmony_ci      return sizeof(uint16_t);
100bf215546Sopenharmony_ci   default:
101bf215546Sopenharmony_ci      return util_format_get_blocksize(varyings->varying[loc].format);
102bf215546Sopenharmony_ci   }
103bf215546Sopenharmony_ci}
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_cistatic inline unsigned
106bf215546Sopenharmony_cipanvk_varyings_buf_count(struct panvk_varyings_info *varyings)
107bf215546Sopenharmony_ci{
108bf215546Sopenharmony_ci   return util_bitcount(varyings->buf_mask);
109bf215546Sopenharmony_ci}
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_cistatic inline void
112bf215546Sopenharmony_cipanvk_varyings_alloc(struct panvk_varyings_info *varyings,
113bf215546Sopenharmony_ci                     struct pan_pool *varying_mem_pool,
114bf215546Sopenharmony_ci                     unsigned vertex_count)
115bf215546Sopenharmony_ci{
116bf215546Sopenharmony_ci   for (unsigned i = 0; i < PANVK_VARY_BUF_MAX; i++) {
117bf215546Sopenharmony_ci      if (!(varyings->buf_mask & (1 << i))) continue;
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci      unsigned buf_idx = panvk_varying_buf_index(varyings, i);
120bf215546Sopenharmony_ci      unsigned size = varyings->buf[buf_idx].stride * vertex_count;
121bf215546Sopenharmony_ci      if (!size)
122bf215546Sopenharmony_ci         continue;
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci      struct panfrost_ptr ptr =
125bf215546Sopenharmony_ci         pan_pool_alloc_aligned(varying_mem_pool, size, 64);
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci      varyings->buf[buf_idx].size = size;
128bf215546Sopenharmony_ci      varyings->buf[buf_idx].address = ptr.gpu;
129bf215546Sopenharmony_ci      varyings->buf[buf_idx].cpu = ptr.cpu;
130bf215546Sopenharmony_ci   }
131bf215546Sopenharmony_ci}
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci#endif
134