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#ifndef FD6_TEXTURE_H_
29#define FD6_TEXTURE_H_
30
31#include "pipe/p_context.h"
32
33#include "freedreno_resource.h"
34#include "freedreno_texture.h"
35
36#include "fd6_context.h"
37#include "fd6_format.h"
38
39struct fd6_sampler_stateobj {
40   struct pipe_sampler_state base;
41   uint32_t texsamp0, texsamp1, texsamp2, texsamp3;
42   bool needs_border;
43   uint16_t seqno;
44};
45
46static inline struct fd6_sampler_stateobj *
47fd6_sampler_stateobj(struct pipe_sampler_state *samp)
48{
49   return (struct fd6_sampler_stateobj *)samp;
50}
51
52struct fd6_pipe_sampler_view {
53   struct pipe_sampler_view base;
54   uint32_t texconst0, texconst1, texconst2, texconst3, texconst5;
55   uint32_t texconst6, texconst7, texconst8, texconst9, texconst10, texconst11;
56   uint32_t offset1, offset2;
57   struct fd_resource *ptr1, *ptr2;
58   uint16_t seqno;
59
60   /* For detecting when a resource has transitioned from UBWC compressed
61    * to uncompressed, which means the sampler state needs to be updated
62    */
63   uint16_t rsc_seqno;
64
65   bool needs_validate;
66};
67
68static inline struct fd6_pipe_sampler_view *
69fd6_pipe_sampler_view(struct pipe_sampler_view *pview)
70{
71   return (struct fd6_pipe_sampler_view *)pview;
72}
73
74void fd6_sampler_view_update(struct fd_context *ctx,
75                             struct fd6_pipe_sampler_view *so) assert_dt;
76
77void fd6_texture_init(struct pipe_context *pctx);
78void fd6_texture_fini(struct pipe_context *pctx);
79
80static inline enum a6xx_tex_type
81fd6_tex_type(unsigned target)
82{
83   switch (target) {
84   default:
85      assert(0);
86   case PIPE_BUFFER:
87   case PIPE_TEXTURE_1D:
88   case PIPE_TEXTURE_1D_ARRAY:
89      return A6XX_TEX_1D;
90   case PIPE_TEXTURE_RECT:
91   case PIPE_TEXTURE_2D:
92   case PIPE_TEXTURE_2D_ARRAY:
93      return A6XX_TEX_2D;
94   case PIPE_TEXTURE_3D:
95      return A6XX_TEX_3D;
96   case PIPE_TEXTURE_CUBE:
97   case PIPE_TEXTURE_CUBE_ARRAY:
98      return A6XX_TEX_CUBE;
99   }
100}
101
102static inline unsigned
103fd6_border_color_offset(struct fd_context *ctx, enum pipe_shader_type type,
104                        struct fd_texture_stateobj *tex) assert_dt
105{
106   /* Currently we put the FS border-color state after VS.  Possibly
107    * we could swap the order.
108    *
109    * This will need update for HS/DS/GS
110    */
111   if (type != PIPE_SHADER_FRAGMENT)
112      return 0;
113
114   unsigned needs_border = false;
115
116   for (unsigned i = 0; i < tex->num_samplers; i++) {
117      if (!tex->samplers[i])
118         continue;
119
120      struct fd6_sampler_stateobj *sampler =
121         fd6_sampler_stateobj(tex->samplers[i]);
122
123      needs_border |= sampler->needs_border;
124   }
125
126   if (!needs_border)
127      return 0;
128
129   return ctx->tex[PIPE_SHADER_VERTEX].num_samplers;
130}
131
132/*
133 * Texture stateobj:
134 *
135 * The sampler and sampler-view state is mapped to a single hardware
136 * stateobj which can be emit'd as a pointer in a CP_SET_DRAW_STATE
137 * packet, to avoid the overhead of re-generating the entire cmdstream
138 * when application toggles thru multiple different texture states.
139 */
140
141struct fd6_texture_key {
142   struct {
143      /* We need to track the seqno of the rsc as well as of the
144       * sampler view, because resource shadowing/etc can result
145       * that the underlying bo changes (which means the previous
146       * state was no longer valid.
147       */
148      uint16_t rsc_seqno;
149      uint16_t seqno;
150   } view[16];
151   struct {
152      uint16_t seqno;
153   } samp[16];
154   uint8_t type;
155   uint8_t bcolor_offset;
156};
157
158struct fd6_texture_state {
159   struct pipe_reference reference;
160   struct fd6_texture_key key;
161   struct fd_ringbuffer *stateobj;
162   bool needs_border;
163};
164
165struct fd6_texture_state *
166fd6_texture_state(struct fd_context *ctx, enum pipe_shader_type type,
167                  struct fd_texture_stateobj *tex) assert_dt;
168
169/* not called directly: */
170void __fd6_texture_state_describe(char *buf,
171                                  const struct fd6_texture_state *tex);
172void __fd6_texture_state_destroy(struct fd6_texture_state *tex);
173
174static inline void
175fd6_texture_state_reference(struct fd6_texture_state **ptr,
176                            struct fd6_texture_state *tex)
177{
178   struct fd6_texture_state *old_tex = *ptr;
179
180   if (pipe_reference_described(
181          &(*ptr)->reference, &tex->reference,
182          (debug_reference_descriptor)__fd6_texture_state_describe))
183      __fd6_texture_state_destroy(old_tex);
184
185   *ptr = tex;
186}
187
188#endif /* FD6_TEXTURE_H_ */
189