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_texture.h"
34#include "freedreno_resource.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 saturate_s, saturate_t, saturate_r;
43	bool needs_border;
44	uint16_t seqno;
45};
46
47static inline struct fd6_sampler_stateobj *
48fd6_sampler_stateobj(struct pipe_sampler_state *samp)
49{
50	return (struct fd6_sampler_stateobj *)samp;
51}
52
53struct fd6_pipe_sampler_view {
54	struct pipe_sampler_view base;
55	uint32_t texconst0, texconst1, texconst2, texconst3, texconst5;
56	uint32_t texconst6, texconst7, texconst8, texconst9, texconst10, texconst11;
57	uint32_t offset, ubwc_offset;
58	uint16_t seqno;
59	bool ubwc_enabled;
60};
61
62static inline struct fd6_pipe_sampler_view *
63fd6_pipe_sampler_view(struct pipe_sampler_view *pview)
64{
65	return (struct fd6_pipe_sampler_view *)pview;
66}
67
68void fd6_texture_init(struct pipe_context *pctx);
69void fd6_texture_fini(struct pipe_context *pctx);
70
71static inline enum a6xx_tex_type
72fd6_tex_type(unsigned target)
73{
74	switch (target) {
75	default:
76		assert(0);
77	case PIPE_BUFFER:
78	case PIPE_TEXTURE_1D:
79	case PIPE_TEXTURE_1D_ARRAY:
80		return A6XX_TEX_1D;
81	case PIPE_TEXTURE_RECT:
82	case PIPE_TEXTURE_2D:
83	case PIPE_TEXTURE_2D_ARRAY:
84		return A6XX_TEX_2D;
85	case PIPE_TEXTURE_3D:
86		return A6XX_TEX_3D;
87	case PIPE_TEXTURE_CUBE:
88	case PIPE_TEXTURE_CUBE_ARRAY:
89		return A6XX_TEX_CUBE;
90	}
91}
92
93static inline unsigned
94fd6_border_color_offset(struct fd_context *ctx, enum pipe_shader_type type,
95		struct fd_texture_stateobj *tex)
96{
97	/* Currently we put the FS border-color state after VS.  Possibly
98	 * we could swap the order.
99	 *
100	 * This will need update for HS/DS/GS
101	 */
102	if (type != PIPE_SHADER_FRAGMENT)
103		return 0;
104
105	unsigned needs_border = false;
106
107	for (unsigned i = 0; i < tex->num_samplers; i++) {
108		if (!tex->samplers[i])
109			continue;
110
111		struct fd6_sampler_stateobj *sampler =
112			fd6_sampler_stateobj(tex->samplers[i]);
113
114		needs_border |= sampler->needs_border;
115	}
116
117	if (!needs_border)
118		return 0;
119
120	return ctx->tex[PIPE_SHADER_VERTEX].num_samplers;
121}
122
123/*
124 * Texture stateobj:
125 *
126 * The sampler and sampler-view state is mapped to a single hardware
127 * stateobj which can be emit'd as a pointer in a CP_SET_DRAW_STATE
128 * packet, to avoid the overhead of re-generating the entire cmdstream
129 * when application toggles thru multiple different texture states.
130 */
131
132struct fd6_texture_key {
133	struct {
134		/* We need to track the seqno of the rsc as well as of the
135		 * sampler view, because resource shadowing/etc can result
136		 * that the underlying bo changes (which means the previous
137		 * state was no longer valid.
138		 */
139		uint16_t rsc_seqno;
140		uint16_t seqno;
141	} view[16];
142	struct {
143		uint16_t seqno;
144	} samp[16];
145	uint8_t bcolor_offset;
146};
147
148struct fd6_texture_state {
149	struct fd6_texture_key key;
150	struct fd_ringbuffer *stateobj;
151	bool needs_border;
152};
153
154struct fd6_texture_state * fd6_texture_state(struct fd_context *ctx,
155		enum pipe_shader_type type, struct fd_texture_stateobj *tex);
156
157#endif /* FD6_TEXTURE_H_ */
158