freedreno/a3xx: fix typo mixup w/ mipfilter
[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
34 #include "fd3_texture.h"
35 #include "fd3_util.h"
36
37 static enum a3xx_tex_clamp
38 tex_clamp(unsigned wrap)
39 {
40 /* hardware probably supports more, but we can't coax all the
41 * wrap/clamp modes out of the GLESv2 blob driver.
42 *
43 * TODO once we have basics working, go back and just try
44 * different values and see what happens
45 */
46 switch (wrap) {
47 case PIPE_TEX_WRAP_REPEAT:
48 return A3XX_TEX_REPEAT;
49 case PIPE_TEX_WRAP_CLAMP:
50 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
51 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
52 return A3XX_TEX_CLAMP_TO_EDGE;
53 case PIPE_TEX_WRAP_MIRROR_CLAMP:
54 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
55 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
56 case PIPE_TEX_WRAP_MIRROR_REPEAT:
57 return A3XX_TEX_MIRROR_REPEAT;
58 default:
59 DBG("invalid wrap: %u", wrap);
60 return 0;
61 }
62 }
63
64 static enum a3xx_tex_filter
65 tex_filter(unsigned filter)
66 {
67 switch (filter) {
68 case PIPE_TEX_FILTER_NEAREST:
69 return A3XX_TEX_NEAREST;
70 case PIPE_TEX_FILTER_LINEAR:
71 return A3XX_TEX_LINEAR;
72 default:
73 DBG("invalid filter: %u", filter);
74 return 0;
75 }
76 }
77
78 static void *
79 fd3_sampler_state_create(struct pipe_context *pctx,
80 const struct pipe_sampler_state *cso)
81 {
82 struct fd3_sampler_stateobj *so = CALLOC_STRUCT(fd3_sampler_stateobj);
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->texsamp0 =
94 COND(!cso->normalized_coords, A3XX_TEX_SAMP_0_UNNORM_COORDS) |
95 COND(miplinear, A3XX_TEX_SAMP_0_MIPFILTER_LINEAR) |
96 A3XX_TEX_SAMP_0_XY_MAG(tex_filter(cso->mag_img_filter)) |
97 A3XX_TEX_SAMP_0_XY_MIN(tex_filter(cso->min_img_filter)) |
98 A3XX_TEX_SAMP_0_WRAP_S(tex_clamp(cso->wrap_s)) |
99 A3XX_TEX_SAMP_0_WRAP_T(tex_clamp(cso->wrap_t)) |
100 A3XX_TEX_SAMP_0_WRAP_R(tex_clamp(cso->wrap_r));
101
102 /* when mip-map is not disabled, this gets set.. I guess possibly
103 * it is the LOD related params, but I think I need GLES3 blob driver
104 * to be able to tell..
105 */
106 if (cso->min_mip_filter != PIPE_TEX_MIPFILTER_NONE) {
107 so->texsamp1 = 0x003ff000;
108 } else {
109 so->texsamp1 = 0x00000000;
110 }
111
112 return so;
113 }
114
115 static enum a3xx_tex_type
116 tex_type(unsigned target)
117 {
118 switch (target) {
119 default:
120 assert(0);
121 case PIPE_BUFFER:
122 case PIPE_TEXTURE_1D:
123 case PIPE_TEXTURE_1D_ARRAY:
124 return A3XX_TEX_1D;
125 case PIPE_TEXTURE_RECT:
126 case PIPE_TEXTURE_2D:
127 case PIPE_TEXTURE_2D_ARRAY:
128 return A3XX_TEX_2D;
129 case PIPE_TEXTURE_3D:
130 return A3XX_TEX_3D;
131 case PIPE_TEXTURE_CUBE:
132 case PIPE_TEXTURE_CUBE_ARRAY:
133 return A3XX_TEX_CUBE;
134 }
135 }
136
137 static struct pipe_sampler_view *
138 fd3_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc,
139 const struct pipe_sampler_view *cso)
140 {
141 struct fd3_pipe_sampler_view *so = CALLOC_STRUCT(fd3_pipe_sampler_view);
142 struct fd_resource *rsc = fd_resource(prsc);
143 unsigned miplevels = cso->u.tex.last_level - cso->u.tex.first_level;
144
145 if (!so)
146 return NULL;
147
148 so->base = *cso;
149 pipe_reference(NULL, &prsc->reference);
150 so->base.texture = prsc;
151 so->base.reference.count = 1;
152 so->base.context = pctx;
153
154 so->tex_resource = rsc;
155 so->mipaddrs = 1 + miplevels;
156
157 so->texconst0 =
158 A3XX_TEX_CONST_0_TYPE(tex_type(prsc->target)) |
159 A3XX_TEX_CONST_0_FMT(fd3_pipe2tex(cso->format)) |
160 A3XX_TEX_CONST_0_MIPLVLS(miplevels) |
161 fd3_tex_swiz(cso->format, cso->swizzle_r, cso->swizzle_g,
162 cso->swizzle_b, cso->swizzle_a);
163 so->texconst1 =
164 A3XX_TEX_CONST_1_FETCHSIZE(fd3_pipe2fetchsize(cso->format)) |
165 A3XX_TEX_CONST_1_WIDTH(prsc->width0) |
166 A3XX_TEX_CONST_1_HEIGHT(prsc->height0);
167 /* when emitted, A3XX_TEX_CONST_2_INDX() must be OR'd in: */
168 so->texconst2 =
169 A3XX_TEX_CONST_2_PITCH(rsc->slices[0].pitch * rsc->cpp);
170 so->texconst3 = 0x00000000; /* ??? */
171
172 return &so->base;
173 }
174
175 void
176 fd3_texture_init(struct pipe_context *pctx)
177 {
178 pctx->create_sampler_state = fd3_sampler_state_create;
179 pctx->create_sampler_view = fd3_sampler_view_create;
180 }