1 /*
2 * Copyright © 2013 Rob Clark <[email protected]>
3 * SPDX-License-Identifier: MIT
4 *
5 * Authors:
6 * Rob Clark <[email protected]>
7 */
8
9 #include "pipe/p_state.h"
10 #include "util/format/u_format.h"
11 #include "util/u_inlines.h"
12 #include "util/u_memory.h"
13 #include "util/u_string.h"
14
15 #include "fd3_format.h"
16 #include "fd3_texture.h"
17
18 static enum a3xx_tex_clamp
tex_clamp(unsigned wrap,bool * needs_border)19 tex_clamp(unsigned wrap, bool *needs_border)
20 {
21 switch (wrap) {
22 case PIPE_TEX_WRAP_REPEAT:
23 return A3XX_TEX_REPEAT;
24 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
25 return A3XX_TEX_CLAMP_TO_EDGE;
26 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
27 *needs_border = true;
28 return A3XX_TEX_CLAMP_TO_BORDER;
29 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
30 /* only works for PoT.. need to emulate otherwise! */
31 return A3XX_TEX_MIRROR_CLAMP;
32 case PIPE_TEX_WRAP_MIRROR_REPEAT:
33 return A3XX_TEX_MIRROR_REPEAT;
34 case PIPE_TEX_WRAP_MIRROR_CLAMP:
35 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
36 /* these two we could perhaps emulate, but we currently
37 * just don't advertise PIPE_CAP_TEXTURE_MIRROR_CLAMP
38 */
39 default:
40 DBG("invalid wrap: %u", wrap);
41 return 0;
42 }
43 }
44
45 static enum a3xx_tex_filter
tex_filter(unsigned filter,bool aniso)46 tex_filter(unsigned filter, bool aniso)
47 {
48 switch (filter) {
49 case PIPE_TEX_FILTER_NEAREST:
50 return A3XX_TEX_NEAREST;
51 case PIPE_TEX_FILTER_LINEAR:
52 return aniso ? A3XX_TEX_ANISO : A3XX_TEX_LINEAR;
53 default:
54 DBG("invalid filter: %u", filter);
55 return 0;
56 }
57 }
58
59 static void *
fd3_sampler_state_create(struct pipe_context * pctx,const struct pipe_sampler_state * cso)60 fd3_sampler_state_create(struct pipe_context *pctx,
61 const struct pipe_sampler_state *cso)
62 {
63 struct fd3_sampler_stateobj *so = CALLOC_STRUCT(fd3_sampler_stateobj);
64 unsigned aniso = util_last_bit(MIN2(cso->max_anisotropy >> 1, 8));
65 bool miplinear = false;
66
67 if (!so)
68 return NULL;
69
70 if (cso->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR)
71 miplinear = true;
72
73 so->base = *cso;
74
75 so->needs_border = false;
76 so->texsamp0 =
77 COND(cso->unnormalized_coords, A3XX_TEX_SAMP_0_UNNORM_COORDS) |
78 COND(!cso->seamless_cube_map, A3XX_TEX_SAMP_0_CUBEMAPSEAMLESSFILTOFF) |
79 COND(miplinear, A3XX_TEX_SAMP_0_MIPFILTER_LINEAR) |
80 A3XX_TEX_SAMP_0_XY_MAG(tex_filter(cso->mag_img_filter, aniso)) |
81 A3XX_TEX_SAMP_0_XY_MIN(tex_filter(cso->min_img_filter, aniso)) |
82 A3XX_TEX_SAMP_0_ANISO(aniso) |
83 A3XX_TEX_SAMP_0_WRAP_S(tex_clamp(cso->wrap_s, &so->needs_border)) |
84 A3XX_TEX_SAMP_0_WRAP_T(tex_clamp(cso->wrap_t, &so->needs_border)) |
85 A3XX_TEX_SAMP_0_WRAP_R(tex_clamp(cso->wrap_r, &so->needs_border));
86
87 if (cso->compare_mode)
88 so->texsamp0 |=
89 A3XX_TEX_SAMP_0_COMPARE_FUNC(cso->compare_func); /* maps 1:1 */
90
91 so->texsamp1 = A3XX_TEX_SAMP_1_LOD_BIAS(cso->lod_bias);
92
93 if (cso->min_mip_filter != PIPE_TEX_MIPFILTER_NONE) {
94 so->texsamp1 |= A3XX_TEX_SAMP_1_MIN_LOD(cso->min_lod) |
95 A3XX_TEX_SAMP_1_MAX_LOD(cso->max_lod);
96 } else {
97 /* If we're not doing mipmap filtering, we still need a slightly > 0
98 * LOD clamp so the HW can decide between min and mag filtering of
99 * level 0.
100 */
101 so->texsamp1 |= A3XX_TEX_SAMP_1_MIN_LOD(MIN2(cso->min_lod, 0.125f)) |
102 A3XX_TEX_SAMP_1_MAX_LOD(MIN2(cso->max_lod, 0.125f));
103 }
104
105 return so;
106 }
107
108 static enum a3xx_tex_type
tex_type(unsigned target)109 tex_type(unsigned target)
110 {
111 switch (target) {
112 default:
113 unreachable("Unsupported target");
114 case PIPE_BUFFER:
115 case PIPE_TEXTURE_1D:
116 case PIPE_TEXTURE_1D_ARRAY:
117 return A3XX_TEX_1D;
118 case PIPE_TEXTURE_RECT:
119 case PIPE_TEXTURE_2D:
120 case PIPE_TEXTURE_2D_ARRAY:
121 return A3XX_TEX_2D;
122 case PIPE_TEXTURE_3D:
123 return A3XX_TEX_3D;
124 case PIPE_TEXTURE_CUBE:
125 case PIPE_TEXTURE_CUBE_ARRAY:
126 return A3XX_TEX_CUBE;
127 }
128 }
129
130 static struct pipe_sampler_view *
fd3_sampler_view_create(struct pipe_context * pctx,struct pipe_resource * prsc,const struct pipe_sampler_view * cso)131 fd3_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc,
132 const struct pipe_sampler_view *cso)
133 {
134 struct fd3_pipe_sampler_view *so = CALLOC_STRUCT(fd3_pipe_sampler_view);
135 struct fd_resource *rsc = fd_resource(prsc);
136 unsigned lvl;
137
138 if (!so)
139 return NULL;
140
141 so->base = *cso;
142 pipe_reference(NULL, &prsc->reference);
143 so->base.texture = prsc;
144 so->base.reference.count = 1;
145 so->base.context = pctx;
146
147 so->texconst0 = A3XX_TEX_CONST_0_TILE_MODE(rsc->layout.tile_mode) |
148 A3XX_TEX_CONST_0_TYPE(tex_type(prsc->target)) |
149 A3XX_TEX_CONST_0_FMT(fd3_pipe2tex(cso->format)) |
150 fd3_tex_swiz(cso->format, cso->swizzle_r, cso->swizzle_g,
151 cso->swizzle_b, cso->swizzle_a);
152
153 if (prsc->target == PIPE_BUFFER || util_format_is_pure_integer(cso->format))
154 so->texconst0 |= A3XX_TEX_CONST_0_NOCONVERT;
155 if (util_format_is_srgb(cso->format))
156 so->texconst0 |= A3XX_TEX_CONST_0_SRGB;
157
158 if (prsc->target == PIPE_BUFFER) {
159 lvl = 0;
160 so->texconst1 =
161 A3XX_TEX_CONST_1_WIDTH(cso->u.buf.size /
162 util_format_get_blocksize(cso->format)) |
163 A3XX_TEX_CONST_1_HEIGHT(1);
164 } else {
165 unsigned miplevels;
166
167 lvl = fd_sampler_first_level(cso);
168 miplevels = fd_sampler_last_level(cso) - lvl;
169
170 so->texconst0 |= A3XX_TEX_CONST_0_MIPLVLS(miplevels);
171 so->texconst1 = A3XX_TEX_CONST_1_PITCHALIGN(rsc->layout.pitchalign - 4) |
172 A3XX_TEX_CONST_1_WIDTH(u_minify(prsc->width0, lvl)) |
173 A3XX_TEX_CONST_1_HEIGHT(u_minify(prsc->height0, lvl));
174 }
175 /* when emitted, A3XX_TEX_CONST_2_INDX() must be OR'd in: */
176 struct fdl_slice *slice = fd_resource_slice(rsc, lvl);
177 so->texconst2 = A3XX_TEX_CONST_2_PITCH(fd_resource_pitch(rsc, lvl));
178 switch (prsc->target) {
179 case PIPE_TEXTURE_1D_ARRAY:
180 case PIPE_TEXTURE_2D_ARRAY:
181 so->texconst3 = A3XX_TEX_CONST_3_DEPTH(prsc->array_size - 1) |
182 A3XX_TEX_CONST_3_LAYERSZ1(slice->size0);
183 break;
184 case PIPE_TEXTURE_3D:
185 so->texconst3 = A3XX_TEX_CONST_3_DEPTH(u_minify(prsc->depth0, lvl)) |
186 A3XX_TEX_CONST_3_LAYERSZ1(slice->size0);
187 so->texconst3 |= A3XX_TEX_CONST_3_LAYERSZ2(
188 fd_resource_slice(rsc, prsc->last_level)->size0);
189 break;
190 default:
191 so->texconst3 = 0x00000000;
192 break;
193 }
194
195 return &so->base;
196 }
197
198 void
fd3_texture_init(struct pipe_context * pctx)199 fd3_texture_init(struct pipe_context *pctx)
200 {
201 pctx->create_sampler_state = fd3_sampler_state_create;
202 pctx->bind_sampler_states = fd_sampler_states_bind;
203 pctx->create_sampler_view = fd3_sampler_view_create;
204 pctx->set_sampler_views = fd_set_sampler_views;
205 }
206