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 "fdl/fd6_format_table.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   struct fd_resource *ptr1, *ptr2;
55   uint16_t seqno;
56
57   /* TEX_CONST descriptor, with just offsets from the BOs in the iova dwords. */
58   uint32_t descriptor[FDL6_TEX_CONST_DWORDS];
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 unsigned
81fd6_border_color_offset(struct fd_context *ctx, enum pipe_shader_type type,
82                        struct fd_texture_stateobj *tex) assert_dt
83{
84   /* Currently we put the FS border-color state after VS.  Possibly
85    * we could swap the order.
86    *
87    * This will need update for HS/DS/GS
88    */
89   if (type != PIPE_SHADER_FRAGMENT)
90      return 0;
91
92   unsigned needs_border = false;
93
94   for (unsigned i = 0; i < tex->num_samplers; i++) {
95      if (!tex->samplers[i])
96         continue;
97
98      struct fd6_sampler_stateobj *sampler =
99         fd6_sampler_stateobj(tex->samplers[i]);
100
101      needs_border |= sampler->needs_border;
102   }
103
104   if (!needs_border)
105      return 0;
106
107   return ctx->tex[PIPE_SHADER_VERTEX].num_samplers;
108}
109
110/*
111 * Texture stateobj:
112 *
113 * The sampler and sampler-view state is mapped to a single hardware
114 * stateobj which can be emit'd as a pointer in a CP_SET_DRAW_STATE
115 * packet, to avoid the overhead of re-generating the entire cmdstream
116 * when application toggles thru multiple different texture states.
117 */
118
119struct fd6_texture_key {
120   struct {
121      /* We need to track the seqno of the rsc as well as of the
122       * sampler view, because resource shadowing/etc can result
123       * that the underlying bo changes (which means the previous
124       * state was no longer valid.
125       */
126      uint16_t rsc_seqno;
127      uint16_t seqno;
128   } view[16];
129   struct {
130      uint16_t seqno;
131   } samp[16];
132   uint8_t type;
133   uint8_t bcolor_offset;
134};
135
136struct fd6_texture_state {
137   struct pipe_reference reference;
138   struct fd6_texture_key key;
139   struct fd_ringbuffer *stateobj;
140   bool needs_border;
141};
142
143struct fd6_texture_state *
144fd6_texture_state(struct fd_context *ctx, enum pipe_shader_type type,
145                  struct fd_texture_stateobj *tex) assert_dt;
146
147/* not called directly: */
148void __fd6_texture_state_describe(char *buf,
149                                  const struct fd6_texture_state *tex);
150void __fd6_texture_state_destroy(struct fd6_texture_state *tex);
151
152static inline void
153fd6_texture_state_reference(struct fd6_texture_state **ptr,
154                            struct fd6_texture_state *tex)
155{
156   struct fd6_texture_state *old_tex = *ptr;
157
158   if (pipe_reference_described(
159          &(*ptr)->reference, &tex->reference,
160          (debug_reference_descriptor)__fd6_texture_state_describe))
161      __fd6_texture_state_destroy(old_tex);
162
163   *ptr = tex;
164}
165
166#endif /* FD6_TEXTURE_H_ */
167