freedreno: gallium driver for adreno
[mesa.git] / src / gallium / drivers / freedreno / freedreno_texture.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2012 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
34 #include "freedreno_texture.h"
35 #include "freedreno_util.h"
36
37 static enum sq_tex_clamp
38 tex_clamp(unsigned wrap)
39 {
40 switch (wrap) {
41 case PIPE_TEX_WRAP_REPEAT:
42 return SQ_TEX_WRAP;
43 case PIPE_TEX_WRAP_CLAMP:
44 return SQ_TEX_CLAMP_HALF_BORDER;
45 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
46 return SQ_TEX_CLAMP_LAST_TEXEL;
47 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
48 return SQ_TEX_CLAMP_BORDER;
49 case PIPE_TEX_WRAP_MIRROR_REPEAT:
50 return SQ_TEX_MIRROR;
51 case PIPE_TEX_WRAP_MIRROR_CLAMP:
52 return SQ_TEX_MIRROR_ONCE_HALF_BORDER;
53 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
54 return SQ_TEX_MIRROR_ONCE_LAST_TEXEL;
55 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
56 return SQ_TEX_MIRROR_ONCE_BORDER;
57 default:
58 DBG("invalid wrap: %u", wrap);
59 return 0;
60 }
61 }
62
63 static enum sq_tex_filter
64 tex_filter(unsigned filter)
65 {
66 switch (filter) {
67 case PIPE_TEX_FILTER_NEAREST:
68 return SQ_TEX_FILTER_POINT;
69 case PIPE_TEX_FILTER_LINEAR:
70 return SQ_TEX_FILTER_BILINEAR;
71 default:
72 DBG("invalid filter: %u", filter);
73 return 0;
74 }
75 }
76
77 static void *
78 fd_sampler_state_create(struct pipe_context *pctx,
79 const struct pipe_sampler_state *cso)
80 {
81 struct fd_sampler_stateobj *so = CALLOC_STRUCT(fd_sampler_stateobj);
82
83 if (!so)
84 return NULL;
85
86 so->base = *cso;
87
88 /* SQ_TEX0_PITCH() must be OR'd in later when we know the bound texture: */
89 so->tex0 =
90 SQ_TEX0_CLAMP_X(tex_clamp(cso->wrap_s)) |
91 SQ_TEX0_CLAMP_Y(tex_clamp(cso->wrap_t)) |
92 SQ_TEX0_CLAMP_Z(tex_clamp(cso->wrap_r));
93
94 so->tex3 =
95 SQ_TEX3_XY_MAG_FILTER(tex_filter(cso->mag_img_filter)) |
96 SQ_TEX3_XY_MIN_FILTER(tex_filter(cso->min_img_filter));
97
98 so->tex4 = 0x00000000; /* ??? */
99 so->tex5 = 0x00000200; /* ??? */
100
101 return so;
102 }
103
104 static void
105 fd_sampler_state_delete(struct pipe_context *pctx, void *hwcso)
106 {
107 FREE(hwcso);
108 }
109
110 static struct pipe_sampler_view *
111 fd_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc,
112 const struct pipe_sampler_view *cso)
113 {
114 struct fd_pipe_sampler_view *so = CALLOC_STRUCT(fd_pipe_sampler_view);
115 struct fd_resource *rsc = fd_resource(prsc);
116
117 if (!so)
118 return NULL;
119
120 so->base = *cso;
121 pipe_reference(NULL, &prsc->reference);
122 so->base.texture = prsc;
123 so->base.reference.count = 1;
124 so->base.context = pctx;
125
126 so->tex_resource = rsc;
127 so->fmt = fd_pipe2surface(cso->format);
128
129 so->tex0 = SQ_TEX0_PITCH(rsc->pitch);
130 so->tex2 =
131 SQ_TEX2_HEIGHT(prsc->height0) |
132 SQ_TEX2_WIDTH(prsc->width0);
133 so->tex3 = fd_tex_swiz(cso->format, cso->swizzle_r, cso->swizzle_g,
134 cso->swizzle_b, cso->swizzle_a);
135
136 return &so->base;
137 }
138
139 static void
140 fd_sampler_view_destroy(struct pipe_context *pctx,
141 struct pipe_sampler_view *view)
142 {
143 pipe_resource_reference(&view->texture, NULL);
144 FREE(view);
145 }
146
147 static void bind_sampler_states(struct fd_texture_stateobj *prog,
148 unsigned nr, void **hwcso)
149 {
150 unsigned i;
151
152 for (i = 0; i < nr; i++) {
153 prog->samplers[i] = hwcso[i];
154 prog->dirty_samplers |= (1 << i);
155 }
156
157 for (; i < prog->num_samplers; i++) {
158 prog->samplers[i] = NULL;
159 prog->dirty_samplers |= (1 << i);
160 }
161
162 prog->num_samplers = nr;
163 }
164
165 static void set_sampler_views(struct fd_texture_stateobj *prog,
166 unsigned nr, struct pipe_sampler_view **views)
167 {
168 unsigned i;
169
170 for (i = 0; i < nr; i++) {
171 pipe_sampler_view_reference(&prog->textures[i], views[i]);
172 prog->dirty_samplers |= (1 << i);
173 }
174
175 for (; i < prog->num_textures; i++) {
176 pipe_sampler_view_reference(&prog->textures[i], NULL);
177 prog->dirty_samplers |= (1 << i);
178 }
179
180 prog->num_textures = nr;
181 }
182
183 static void
184 fd_fragtex_sampler_states_bind(struct pipe_context *pctx,
185 unsigned nr, void **hwcso)
186 {
187 struct fd_context *ctx = fd_context(pctx);
188 bind_sampler_states(&ctx->fragtex, nr, hwcso);
189 ctx->dirty |= FD_DIRTY_FRAGTEX;
190 }
191
192
193 static void
194 fd_fragtex_set_sampler_views(struct pipe_context *pctx, unsigned nr,
195 struct pipe_sampler_view **views)
196 {
197 struct fd_context *ctx = fd_context(pctx);
198 set_sampler_views(&ctx->fragtex, nr, views);
199 ctx->dirty |= FD_DIRTY_FRAGTEX;
200 }
201
202 static void
203 fd_verttex_sampler_states_bind(struct pipe_context *pctx,
204 unsigned nr, void **hwcso)
205 {
206 struct fd_context *ctx = fd_context(pctx);
207 bind_sampler_states(&ctx->verttex, nr, hwcso);
208 ctx->dirty |= FD_DIRTY_VERTTEX;
209 }
210
211
212 static void
213 fd_verttex_set_sampler_views(struct pipe_context *pctx, unsigned nr,
214 struct pipe_sampler_view **views)
215 {
216 struct fd_context *ctx = fd_context(pctx);
217 set_sampler_views(&ctx->verttex, nr, views);
218 ctx->dirty |= FD_DIRTY_VERTTEX;
219 }
220
221 static bool
222 tex_cmp(struct fd_texture_stateobj *tex1, unsigned samp_id1,
223 struct fd_texture_stateobj *tex2, unsigned samp_id2)
224 {
225 if ((samp_id1 >= tex1->num_samplers) ||
226 (samp_id2 >= tex2->num_samplers))
227 return false;
228
229 if ((tex1 == tex2) && (samp_id1 == samp_id2))
230 return true;
231
232 if (tex1->textures[samp_id1]->texture != tex2->textures[samp_id2]->texture)
233 return false;
234
235 if (memcmp(&tex1->samplers[samp_id1]->base, &tex2->samplers[samp_id2]->base,
236 sizeof(tex1->samplers[samp_id1]->base)))
237 return false;
238
239 return true;
240 }
241
242 /* map gallium sampler-id to hw const-idx.. adreno uses a flat address
243 * space of samplers (const-idx), so we need to map the gallium sampler-id
244 * which is per-shader to a global const-idx space.
245 */
246 unsigned
247 fd_get_const_idx(struct fd_context *ctx, struct fd_texture_stateobj *tex,
248 unsigned samp_id)
249 {
250 unsigned i, const_idx = 0;
251
252 /* TODO maybe worth having some sort of cache, because we need to
253 * do this loop thru all the samplers both when patching shaders
254 * and also when emitting sampler state..
255 */
256
257 for (i = 0; i < ctx->verttex.num_samplers; i++) {
258 if (tex_cmp(&ctx->verttex, i, tex, samp_id))
259 return const_idx;
260 const_idx++;
261 }
262
263 for (i = 0; i < ctx->fragtex.num_samplers; i++) {
264 if (tex_cmp(&ctx->fragtex, i, tex, samp_id))
265 return const_idx;
266 const_idx++;
267 }
268
269 return const_idx;
270 }
271
272 void
273 fd_texture_init(struct pipe_context *pctx)
274 {
275 pctx->create_sampler_state = fd_sampler_state_create;
276 pctx->delete_sampler_state = fd_sampler_state_delete;
277
278 pctx->create_sampler_view = fd_sampler_view_create;
279 pctx->sampler_view_destroy = fd_sampler_view_destroy;
280
281 pctx->bind_fragment_sampler_states = fd_fragtex_sampler_states_bind;
282 pctx->set_fragment_sampler_views = fd_fragtex_set_sampler_views;
283
284 pctx->bind_vertex_sampler_states = fd_verttex_sampler_states_bind;
285 pctx->set_vertex_sampler_views = fd_verttex_set_sampler_views;
286 }