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