freedreno: move bind_sampler_states to per-generation
[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)
40 {
41 /* hardware probably supports more, but we can't coax all the
42 * wrap/clamp modes out of the GLESv2 blob driver.
43 *
44 * TODO once we have basics working, go back and just try
45 * different values and see what happens
46 */
47 switch (wrap) {
48 case PIPE_TEX_WRAP_REPEAT:
49 return A3XX_TEX_REPEAT;
50 case PIPE_TEX_WRAP_CLAMP:
51 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
52 return A3XX_TEX_CLAMP_TO_EDGE;
53 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
54 return A3XX_TEX_CLAMP_TO_BORDER;
55 case PIPE_TEX_WRAP_MIRROR_CLAMP:
56 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
57 /* these two we should emulate! */
58 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
59 /* only works for PoT.. need to emulate otherwise! */
60 return A3XX_TEX_MIRROR_CLAMP;
61 case PIPE_TEX_WRAP_MIRROR_REPEAT:
62 return A3XX_TEX_MIRROR_REPEAT;
63 default:
64 DBG("invalid wrap: %u", wrap);
65 return 0;
66 }
67 }
68
69 static enum a3xx_tex_filter
70 tex_filter(unsigned filter)
71 {
72 switch (filter) {
73 case PIPE_TEX_FILTER_NEAREST:
74 return A3XX_TEX_NEAREST;
75 case PIPE_TEX_FILTER_LINEAR:
76 return A3XX_TEX_LINEAR;
77 default:
78 DBG("invalid filter: %u", filter);
79 return 0;
80 }
81 }
82
83 static void *
84 fd3_sampler_state_create(struct pipe_context *pctx,
85 const struct pipe_sampler_state *cso)
86 {
87 struct fd3_sampler_stateobj *so = CALLOC_STRUCT(fd3_sampler_stateobj);
88 bool miplinear = false;
89
90 if (!so)
91 return NULL;
92
93 if (cso->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR)
94 miplinear = true;
95
96 so->base = *cso;
97
98 so->texsamp0 =
99 COND(!cso->normalized_coords, A3XX_TEX_SAMP_0_UNNORM_COORDS) |
100 COND(miplinear, A3XX_TEX_SAMP_0_MIPFILTER_LINEAR) |
101 A3XX_TEX_SAMP_0_XY_MAG(tex_filter(cso->mag_img_filter)) |
102 A3XX_TEX_SAMP_0_XY_MIN(tex_filter(cso->min_img_filter)) |
103 A3XX_TEX_SAMP_0_WRAP_S(tex_clamp(cso->wrap_s)) |
104 A3XX_TEX_SAMP_0_WRAP_T(tex_clamp(cso->wrap_t)) |
105 A3XX_TEX_SAMP_0_WRAP_R(tex_clamp(cso->wrap_r));
106
107 if (cso->compare_mode)
108 so->texsamp0 |= A3XX_TEX_SAMP_0_COMPARE_FUNC(cso->compare_func); /* maps 1:1 */
109
110 if (cso->min_mip_filter != PIPE_TEX_MIPFILTER_NONE) {
111 so->texsamp1 =
112 A3XX_TEX_SAMP_1_LOD_BIAS(cso->lod_bias) |
113 A3XX_TEX_SAMP_1_MIN_LOD(cso->min_lod) |
114 A3XX_TEX_SAMP_1_MAX_LOD(cso->max_lod);
115 } else {
116 so->texsamp1 = 0x00000000;
117 }
118
119 return so;
120 }
121
122 static void
123 fd3_sampler_states_bind(struct pipe_context *pctx,
124 unsigned shader, unsigned start,
125 unsigned nr, void **hwcso)
126 {
127 fd_sampler_states_bind(pctx, shader, start, nr, hwcso);
128 }
129
130 static enum a3xx_tex_type
131 tex_type(unsigned target)
132 {
133 switch (target) {
134 default:
135 assert(0);
136 case PIPE_BUFFER:
137 case PIPE_TEXTURE_1D:
138 case PIPE_TEXTURE_1D_ARRAY:
139 return A3XX_TEX_1D;
140 case PIPE_TEXTURE_RECT:
141 case PIPE_TEXTURE_2D:
142 case PIPE_TEXTURE_2D_ARRAY:
143 return A3XX_TEX_2D;
144 case PIPE_TEXTURE_3D:
145 return A3XX_TEX_3D;
146 case PIPE_TEXTURE_CUBE:
147 case PIPE_TEXTURE_CUBE_ARRAY:
148 return A3XX_TEX_CUBE;
149 }
150 }
151
152 static struct pipe_sampler_view *
153 fd3_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc,
154 const struct pipe_sampler_view *cso)
155 {
156 struct fd3_pipe_sampler_view *so = CALLOC_STRUCT(fd3_pipe_sampler_view);
157 struct fd_resource *rsc = fd_resource(prsc);
158 unsigned lvl = cso->u.tex.first_level;
159 unsigned miplevels = cso->u.tex.last_level - lvl;
160
161 if (!so)
162 return NULL;
163
164 so->base = *cso;
165 pipe_reference(NULL, &prsc->reference);
166 so->base.texture = prsc;
167 so->base.reference.count = 1;
168 so->base.context = pctx;
169
170 so->tex_resource = rsc;
171
172 so->texconst0 =
173 A3XX_TEX_CONST_0_TYPE(tex_type(prsc->target)) |
174 A3XX_TEX_CONST_0_FMT(fd3_pipe2tex(cso->format)) |
175 A3XX_TEX_CONST_0_MIPLVLS(miplevels) |
176 fd3_tex_swiz(cso->format, cso->swizzle_r, cso->swizzle_g,
177 cso->swizzle_b, cso->swizzle_a);
178
179 if (util_format_is_srgb(cso->format))
180 so->texconst0 |= A3XX_TEX_CONST_0_SRGB;
181
182 so->texconst1 =
183 A3XX_TEX_CONST_1_FETCHSIZE(fd3_pipe2fetchsize(cso->format)) |
184 A3XX_TEX_CONST_1_WIDTH(u_minify(prsc->width0, lvl)) |
185 A3XX_TEX_CONST_1_HEIGHT(u_minify(prsc->height0, lvl));
186 /* when emitted, A3XX_TEX_CONST_2_INDX() must be OR'd in: */
187 so->texconst2 =
188 A3XX_TEX_CONST_2_PITCH(rsc->slices[lvl].pitch * rsc->cpp);
189 switch (prsc->target) {
190 case PIPE_TEXTURE_1D_ARRAY:
191 case PIPE_TEXTURE_2D_ARRAY:
192 so->texconst3 =
193 A3XX_TEX_CONST_3_DEPTH(prsc->array_size - 1) |
194 A3XX_TEX_CONST_3_LAYERSZ1(rsc->slices[0].size0) |
195 A3XX_TEX_CONST_3_LAYERSZ2(rsc->slices[0].size0);
196 break;
197 case PIPE_TEXTURE_3D:
198 so->texconst3 =
199 A3XX_TEX_CONST_3_DEPTH(u_minify(prsc->depth0, lvl)) |
200 A3XX_TEX_CONST_3_LAYERSZ1(rsc->slices[0].size0) |
201 A3XX_TEX_CONST_3_LAYERSZ2(rsc->slices[0].size0);
202 break;
203 default:
204 so->texconst3 = 0x00000000;
205 break;
206 }
207
208 return &so->base;
209 }
210
211 void
212 fd3_texture_init(struct pipe_context *pctx)
213 {
214 pctx->create_sampler_state = fd3_sampler_state_create;
215 pctx->bind_sampler_states = fd3_sampler_states_bind;
216 pctx->create_sampler_view = fd3_sampler_view_create;
217 }