freedreno/a3xx: add support to emulate GL_CLAMP
[mesa.git] / src / gallium / drivers / freedreno / a3xx / fd3_texture.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include "pipe/p_state.h"
30 #include "util/u_string.h"
31 #include "util/u_memory.h"
32 #include "util/u_inlines.h"
33 #include "util/u_format.h"
34
35 #include "fd3_texture.h"
36 #include "fd3_util.h"
37
38 static enum a3xx_tex_clamp
39 tex_clamp(unsigned wrap, bool clamp_to_edge)
40 {
41 /* Hardware does not support _CLAMP, but we emulate it: */
42 if (wrap == PIPE_TEX_WRAP_CLAMP) {
43 wrap = (clamp_to_edge) ?
44 PIPE_TEX_WRAP_CLAMP_TO_EDGE : PIPE_TEX_WRAP_CLAMP_TO_BORDER;
45 }
46
47 switch (wrap) {
48 case PIPE_TEX_WRAP_REPEAT:
49 return A3XX_TEX_REPEAT;
50 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
51 return A3XX_TEX_CLAMP_TO_EDGE;
52 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
53 return A3XX_TEX_CLAMP_TO_BORDER;
54 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
55 /* only works for PoT.. need to emulate otherwise! */
56 return A3XX_TEX_MIRROR_CLAMP;
57 case PIPE_TEX_WRAP_MIRROR_REPEAT:
58 return A3XX_TEX_MIRROR_REPEAT;
59 case PIPE_TEX_WRAP_MIRROR_CLAMP:
60 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
61 /* these two we could perhaps emulate, but we currently
62 * just don't advertise PIPE_CAP_TEXTURE_MIRROR_CLAMP
63 */
64 default:
65 DBG("invalid wrap: %u", wrap);
66 return 0;
67 }
68 }
69
70 static enum a3xx_tex_filter
71 tex_filter(unsigned filter)
72 {
73 switch (filter) {
74 case PIPE_TEX_FILTER_NEAREST:
75 return A3XX_TEX_NEAREST;
76 case PIPE_TEX_FILTER_LINEAR:
77 return A3XX_TEX_LINEAR;
78 default:
79 DBG("invalid filter: %u", filter);
80 return 0;
81 }
82 }
83
84 static void *
85 fd3_sampler_state_create(struct pipe_context *pctx,
86 const struct pipe_sampler_state *cso)
87 {
88 struct fd3_sampler_stateobj *so = CALLOC_STRUCT(fd3_sampler_stateobj);
89 bool miplinear = false;
90 bool clamp_to_edge;
91
92 if (!so)
93 return NULL;
94
95 if (cso->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR)
96 miplinear = true;
97
98 so->base = *cso;
99
100 /*
101 * For nearest filtering, _CLAMP means _CLAMP_TO_EDGE; for linear
102 * filtering, _CLAMP means _CLAMP_TO_BORDER while additionally
103 * clamping the texture coordinates to [0.0, 1.0].
104 *
105 * The clamping will be taken care of in the shaders. There are two
106 * filters here, but let the minification one has a say.
107 */
108 clamp_to_edge = (cso->min_img_filter == PIPE_TEX_FILTER_NEAREST);
109 if (!clamp_to_edge) {
110 so->saturate_s = (cso->wrap_s == PIPE_TEX_WRAP_CLAMP);
111 so->saturate_t = (cso->wrap_t == PIPE_TEX_WRAP_CLAMP);
112 so->saturate_r = (cso->wrap_r == PIPE_TEX_WRAP_CLAMP);
113 }
114
115 so->texsamp0 =
116 COND(!cso->normalized_coords, A3XX_TEX_SAMP_0_UNNORM_COORDS) |
117 COND(miplinear, A3XX_TEX_SAMP_0_MIPFILTER_LINEAR) |
118 A3XX_TEX_SAMP_0_XY_MAG(tex_filter(cso->mag_img_filter)) |
119 A3XX_TEX_SAMP_0_XY_MIN(tex_filter(cso->min_img_filter)) |
120 A3XX_TEX_SAMP_0_WRAP_S(tex_clamp(cso->wrap_s, clamp_to_edge)) |
121 A3XX_TEX_SAMP_0_WRAP_T(tex_clamp(cso->wrap_t, clamp_to_edge)) |
122 A3XX_TEX_SAMP_0_WRAP_R(tex_clamp(cso->wrap_r, clamp_to_edge));
123
124 if (cso->compare_mode)
125 so->texsamp0 |= A3XX_TEX_SAMP_0_COMPARE_FUNC(cso->compare_func); /* maps 1:1 */
126
127 if (cso->min_mip_filter != PIPE_TEX_MIPFILTER_NONE) {
128 so->texsamp1 =
129 A3XX_TEX_SAMP_1_LOD_BIAS(cso->lod_bias) |
130 A3XX_TEX_SAMP_1_MIN_LOD(cso->min_lod) |
131 A3XX_TEX_SAMP_1_MAX_LOD(cso->max_lod);
132 } else {
133 so->texsamp1 = 0x00000000;
134 }
135
136 return so;
137 }
138
139 static void
140 fd3_sampler_states_bind(struct pipe_context *pctx,
141 unsigned shader, unsigned start,
142 unsigned nr, void **hwcso)
143 {
144 struct fd_context *ctx = fd_context(pctx);
145 struct fd3_context *fd3_ctx = fd3_context(ctx);
146 unsigned saturate_s = 0, saturate_t = 0, saturate_r = 0;
147 unsigned i;
148
149 for (i = 0; i < nr; i++) {
150 if (hwcso[i]) {
151 struct fd3_sampler_stateobj *sampler =
152 fd3_sampler_stateobj(hwcso[i]);
153 if (sampler->saturate_s)
154 saturate_s |= (1 << i);
155 if (sampler->saturate_t)
156 saturate_t |= (1 << i);
157 if (sampler->saturate_r)
158 saturate_r |= (1 << i);
159 }
160 }
161
162 fd_sampler_states_bind(pctx, shader, start, nr, hwcso);
163
164 if (shader == PIPE_SHADER_FRAGMENT) {
165 fd3_ctx->fsaturate_s = saturate_s;
166 fd3_ctx->fsaturate_t = saturate_t;
167 fd3_ctx->fsaturate_r = saturate_r;
168 } else if (shader == PIPE_SHADER_VERTEX) {
169 fd3_ctx->vsaturate_s = saturate_s;
170 fd3_ctx->vsaturate_t = saturate_t;
171 fd3_ctx->vsaturate_r = saturate_r;
172 }
173 }
174
175 static enum a3xx_tex_type
176 tex_type(unsigned target)
177 {
178 switch (target) {
179 default:
180 assert(0);
181 case PIPE_BUFFER:
182 case PIPE_TEXTURE_1D:
183 case PIPE_TEXTURE_1D_ARRAY:
184 return A3XX_TEX_1D;
185 case PIPE_TEXTURE_RECT:
186 case PIPE_TEXTURE_2D:
187 case PIPE_TEXTURE_2D_ARRAY:
188 return A3XX_TEX_2D;
189 case PIPE_TEXTURE_3D:
190 return A3XX_TEX_3D;
191 case PIPE_TEXTURE_CUBE:
192 case PIPE_TEXTURE_CUBE_ARRAY:
193 return A3XX_TEX_CUBE;
194 }
195 }
196
197 static struct pipe_sampler_view *
198 fd3_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc,
199 const struct pipe_sampler_view *cso)
200 {
201 struct fd3_pipe_sampler_view *so = CALLOC_STRUCT(fd3_pipe_sampler_view);
202 struct fd_resource *rsc = fd_resource(prsc);
203 unsigned lvl = cso->u.tex.first_level;
204 unsigned miplevels = cso->u.tex.last_level - lvl;
205
206 if (!so)
207 return NULL;
208
209 so->base = *cso;
210 pipe_reference(NULL, &prsc->reference);
211 so->base.texture = prsc;
212 so->base.reference.count = 1;
213 so->base.context = pctx;
214
215 so->tex_resource = rsc;
216
217 so->texconst0 =
218 A3XX_TEX_CONST_0_TYPE(tex_type(prsc->target)) |
219 A3XX_TEX_CONST_0_FMT(fd3_pipe2tex(cso->format)) |
220 A3XX_TEX_CONST_0_MIPLVLS(miplevels) |
221 fd3_tex_swiz(cso->format, cso->swizzle_r, cso->swizzle_g,
222 cso->swizzle_b, cso->swizzle_a);
223
224 if (util_format_is_srgb(cso->format))
225 so->texconst0 |= A3XX_TEX_CONST_0_SRGB;
226
227 so->texconst1 =
228 A3XX_TEX_CONST_1_FETCHSIZE(fd3_pipe2fetchsize(cso->format)) |
229 A3XX_TEX_CONST_1_WIDTH(u_minify(prsc->width0, lvl)) |
230 A3XX_TEX_CONST_1_HEIGHT(u_minify(prsc->height0, lvl));
231 /* when emitted, A3XX_TEX_CONST_2_INDX() must be OR'd in: */
232 so->texconst2 =
233 A3XX_TEX_CONST_2_PITCH(rsc->slices[lvl].pitch * rsc->cpp);
234 switch (prsc->target) {
235 case PIPE_TEXTURE_1D_ARRAY:
236 case PIPE_TEXTURE_2D_ARRAY:
237 so->texconst3 =
238 A3XX_TEX_CONST_3_DEPTH(prsc->array_size - 1) |
239 A3XX_TEX_CONST_3_LAYERSZ1(rsc->slices[0].size0) |
240 A3XX_TEX_CONST_3_LAYERSZ2(rsc->slices[0].size0);
241 break;
242 case PIPE_TEXTURE_3D:
243 so->texconst3 =
244 A3XX_TEX_CONST_3_DEPTH(u_minify(prsc->depth0, lvl)) |
245 A3XX_TEX_CONST_3_LAYERSZ1(rsc->slices[0].size0) |
246 A3XX_TEX_CONST_3_LAYERSZ2(rsc->slices[0].size0);
247 break;
248 default:
249 so->texconst3 = 0x00000000;
250 break;
251 }
252
253 return &so->base;
254 }
255
256 void
257 fd3_texture_init(struct pipe_context *pctx)
258 {
259 pctx->create_sampler_state = fd3_sampler_state_create;
260 pctx->bind_sampler_states = fd3_sampler_states_bind;
261 pctx->create_sampler_view = fd3_sampler_view_create;
262 }