1 /*
2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "pipe/p_state.h"
28 #include "util/format/u_format.h"
29 #include "util/u_inlines.h"
30 #include "util/u_memory.h"
31 #include "util/u_string.h"
32
33 #include "fd4_format.h"
34 #include "fd4_texture.h"
35
36 static enum a4xx_tex_clamp
tex_clamp(unsigned wrap,bool * needs_border)37 tex_clamp(unsigned wrap, bool *needs_border)
38 {
39 switch (wrap) {
40 case PIPE_TEX_WRAP_REPEAT:
41 return A4XX_TEX_REPEAT;
42 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
43 return A4XX_TEX_CLAMP_TO_EDGE;
44 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
45 *needs_border = true;
46 return A4XX_TEX_CLAMP_TO_BORDER;
47 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
48 /* only works for PoT.. need to emulate otherwise! */
49 return A4XX_TEX_MIRROR_CLAMP;
50 case PIPE_TEX_WRAP_MIRROR_REPEAT:
51 return A4XX_TEX_MIRROR_REPEAT;
52 case PIPE_TEX_WRAP_MIRROR_CLAMP:
53 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
54 /* these two we could perhaps emulate, but we currently
55 * just don't advertise PIPE_CAP_TEXTURE_MIRROR_CLAMP
56 */
57 default:
58 DBG("invalid wrap: %u", wrap);
59 return 0;
60 }
61 }
62
63 static enum a4xx_tex_filter
tex_filter(unsigned filter,bool aniso)64 tex_filter(unsigned filter, bool aniso)
65 {
66 switch (filter) {
67 case PIPE_TEX_FILTER_NEAREST:
68 return A4XX_TEX_NEAREST;
69 case PIPE_TEX_FILTER_LINEAR:
70 return aniso ? A4XX_TEX_ANISO : A4XX_TEX_LINEAR;
71 default:
72 DBG("invalid filter: %u", filter);
73 return 0;
74 }
75 }
76
77 static void *
fd4_sampler_state_create(struct pipe_context * pctx,const struct pipe_sampler_state * cso)78 fd4_sampler_state_create(struct pipe_context *pctx,
79 const struct pipe_sampler_state *cso)
80 {
81 struct fd4_sampler_stateobj *so = CALLOC_STRUCT(fd4_sampler_stateobj);
82 unsigned aniso = util_last_bit(MIN2(cso->max_anisotropy >> 1, 8));
83 bool miplinear = false;
84
85 if (!so)
86 return NULL;
87
88 if (cso->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR)
89 miplinear = true;
90
91 so->base = *cso;
92
93 so->needs_border = false;
94 so->texsamp0 =
95 COND(miplinear, A4XX_TEX_SAMP_0_MIPFILTER_LINEAR_NEAR) |
96 A4XX_TEX_SAMP_0_XY_MAG(tex_filter(cso->mag_img_filter, aniso)) |
97 A4XX_TEX_SAMP_0_XY_MIN(tex_filter(cso->min_img_filter, aniso)) |
98 A4XX_TEX_SAMP_0_ANISO(aniso) |
99 A4XX_TEX_SAMP_0_WRAP_S(tex_clamp(cso->wrap_s, &so->needs_border)) |
100 A4XX_TEX_SAMP_0_WRAP_T(tex_clamp(cso->wrap_t, &so->needs_border)) |
101 A4XX_TEX_SAMP_0_WRAP_R(tex_clamp(cso->wrap_r, &so->needs_border));
102
103 so->texsamp1 =
104 // COND(miplinear, A4XX_TEX_SAMP_1_MIPFILTER_LINEAR_FAR) |
105 COND(!cso->seamless_cube_map, A4XX_TEX_SAMP_1_CUBEMAPSEAMLESSFILTOFF) |
106 COND(!cso->normalized_coords, A4XX_TEX_SAMP_1_UNNORM_COORDS);
107
108 if (cso->min_mip_filter != PIPE_TEX_MIPFILTER_NONE) {
109 so->texsamp0 |= A4XX_TEX_SAMP_0_LOD_BIAS(cso->lod_bias);
110 so->texsamp1 |= A4XX_TEX_SAMP_1_MIN_LOD(cso->min_lod) |
111 A4XX_TEX_SAMP_1_MAX_LOD(cso->max_lod);
112 }
113
114 if (cso->compare_mode)
115 so->texsamp1 |=
116 A4XX_TEX_SAMP_1_COMPARE_FUNC(cso->compare_func); /* maps 1:1 */
117
118 return so;
119 }
120
121 static enum a4xx_tex_type
tex_type(unsigned target)122 tex_type(unsigned target)
123 {
124 switch (target) {
125 default:
126 assert(0);
127 case PIPE_BUFFER:
128 case PIPE_TEXTURE_1D:
129 case PIPE_TEXTURE_1D_ARRAY:
130 return A4XX_TEX_1D;
131 case PIPE_TEXTURE_RECT:
132 case PIPE_TEXTURE_2D:
133 case PIPE_TEXTURE_2D_ARRAY:
134 return A4XX_TEX_2D;
135 case PIPE_TEXTURE_3D:
136 return A4XX_TEX_3D;
137 case PIPE_TEXTURE_CUBE:
138 case PIPE_TEXTURE_CUBE_ARRAY:
139 return A4XX_TEX_CUBE;
140 }
141 }
142
143 static bool
use_astc_srgb_workaround(struct pipe_context * pctx,enum pipe_format format)144 use_astc_srgb_workaround(struct pipe_context *pctx, enum pipe_format format)
145 {
146 return (fd_screen(pctx->screen)->gpu_id == 420) &&
147 (util_format_description(format)->layout == UTIL_FORMAT_LAYOUT_ASTC);
148 }
149
150 static struct pipe_sampler_view *
fd4_sampler_view_create(struct pipe_context * pctx,struct pipe_resource * prsc,const struct pipe_sampler_view * cso)151 fd4_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc,
152 const struct pipe_sampler_view *cso)
153 {
154 struct fd4_pipe_sampler_view *so = CALLOC_STRUCT(fd4_pipe_sampler_view);
155 struct fd_resource *rsc = fd_resource(prsc);
156 enum pipe_format format = cso->format;
157 unsigned lvl, layers = 0;
158
159 if (!so)
160 return NULL;
161
162 if (format == PIPE_FORMAT_X32_S8X24_UINT) {
163 rsc = rsc->stencil;
164 format = rsc->b.b.format;
165 }
166
167 so->base = *cso;
168 pipe_reference(NULL, &prsc->reference);
169 so->base.texture = prsc;
170 so->base.reference.count = 1;
171 so->base.context = pctx;
172
173 so->texconst0 = A4XX_TEX_CONST_0_TYPE(tex_type(cso->target)) |
174 A4XX_TEX_CONST_0_FMT(fd4_pipe2tex(format)) |
175 fd4_tex_swiz(format, cso->swizzle_r, cso->swizzle_g,
176 cso->swizzle_b, cso->swizzle_a);
177
178 if (util_format_is_srgb(format)) {
179 if (use_astc_srgb_workaround(pctx, format))
180 so->astc_srgb = true;
181 so->texconst0 |= A4XX_TEX_CONST_0_SRGB;
182 }
183
184 if (cso->target == PIPE_BUFFER) {
185 unsigned elements = cso->u.buf.size / util_format_get_blocksize(format);
186
187 lvl = 0;
188 so->texconst1 =
189 A4XX_TEX_CONST_1_WIDTH(elements) | A4XX_TEX_CONST_1_HEIGHT(1);
190 so->texconst2 = A4XX_TEX_CONST_2_PITCH(elements * rsc->layout.cpp);
191 so->offset = cso->u.buf.offset;
192 } else {
193 unsigned miplevels;
194
195 lvl = fd_sampler_first_level(cso);
196 miplevels = fd_sampler_last_level(cso) - lvl;
197 layers = cso->u.tex.last_layer - cso->u.tex.first_layer + 1;
198
199 so->texconst0 |= A4XX_TEX_CONST_0_MIPLVLS(miplevels);
200 so->texconst1 = A4XX_TEX_CONST_1_WIDTH(u_minify(prsc->width0, lvl)) |
201 A4XX_TEX_CONST_1_HEIGHT(u_minify(prsc->height0, lvl));
202 so->texconst2 = A4XX_TEX_CONST_2_PITCHALIGN(rsc->layout.pitchalign - 5) |
203 A4XX_TEX_CONST_2_PITCH(fd_resource_pitch(rsc, lvl));
204 so->offset = fd_resource_offset(rsc, lvl, cso->u.tex.first_layer);
205 }
206
207 /* NOTE: since we sample z24s8 using 8888_UINT format, the swizzle
208 * we get isn't quite right. Use SWAP(XYZW) as a cheap and cheerful
209 * way to re-arrange things so stencil component is where the swiz
210 * expects.
211 *
212 * Note that gallium expects stencil sampler to return (s,s,s,s)
213 * which isn't quite true. To make that happen we'd have to massage
214 * the swizzle. But in practice only the .x component is used.
215 */
216 if (format == PIPE_FORMAT_X24S8_UINT)
217 so->texconst2 |= A4XX_TEX_CONST_2_SWAP(XYZW);
218
219 switch (cso->target) {
220 case PIPE_TEXTURE_1D_ARRAY:
221 case PIPE_TEXTURE_2D_ARRAY:
222 so->texconst3 = A4XX_TEX_CONST_3_DEPTH(layers) |
223 A4XX_TEX_CONST_3_LAYERSZ(rsc->layout.layer_size);
224 break;
225 case PIPE_TEXTURE_CUBE:
226 case PIPE_TEXTURE_CUBE_ARRAY:
227 so->texconst3 = A4XX_TEX_CONST_3_DEPTH(layers / 6) |
228 A4XX_TEX_CONST_3_LAYERSZ(rsc->layout.layer_size);
229 break;
230 case PIPE_TEXTURE_3D:
231 so->texconst3 =
232 A4XX_TEX_CONST_3_DEPTH(u_minify(prsc->depth0, lvl)) |
233 A4XX_TEX_CONST_3_LAYERSZ(fd_resource_slice(rsc, lvl)->size0);
234 so->texconst4 = A4XX_TEX_CONST_4_LAYERSZ(
235 fd_resource_slice(rsc, prsc->last_level)->size0);
236 break;
237 default:
238 so->texconst3 = 0x00000000;
239 break;
240 }
241
242 return &so->base;
243 }
244
245 static void
fd4_set_sampler_views(struct pipe_context * pctx,enum pipe_shader_type shader,unsigned start,unsigned nr,unsigned unbind_num_trailing_slots,bool take_ownership,struct pipe_sampler_view ** views)246 fd4_set_sampler_views(struct pipe_context *pctx, enum pipe_shader_type shader,
247 unsigned start, unsigned nr,
248 unsigned unbind_num_trailing_slots,
249 bool take_ownership,
250 struct pipe_sampler_view **views)
251 {
252 struct fd_context *ctx = fd_context(pctx);
253 struct fd4_context *fd4_ctx = fd4_context(ctx);
254 uint16_t astc_srgb = 0;
255 unsigned i;
256
257 for (i = 0; i < nr; i++) {
258 if (views[i]) {
259 struct fd4_pipe_sampler_view *view = fd4_pipe_sampler_view(views[i]);
260 if (view->astc_srgb)
261 astc_srgb |= (1 << i);
262 }
263 }
264
265 fd_set_sampler_views(pctx, shader, start, nr, unbind_num_trailing_slots,
266 take_ownership, views);
267
268 if (shader == PIPE_SHADER_FRAGMENT) {
269 fd4_ctx->fastc_srgb = astc_srgb;
270 } else if (shader == PIPE_SHADER_VERTEX) {
271 fd4_ctx->vastc_srgb = astc_srgb;
272 }
273 }
274
275 void
fd4_texture_init(struct pipe_context * pctx)276 fd4_texture_init(struct pipe_context *pctx)
277 {
278 pctx->create_sampler_state = fd4_sampler_state_create;
279 pctx->bind_sampler_states = fd_sampler_states_bind;
280 pctx->create_sampler_view = fd4_sampler_view_create;
281 pctx->set_sampler_views = fd4_set_sampler_views;
282 }
283