1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
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:
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
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
26 * Rob Clark <robclark@freedesktop.org>
29 #include "pipe/p_state.h"
30 #include "util/u_string.h"
31 #include "util/u_memory.h"
32 #include "util/u_inlines.h"
34 #include "freedreno_texture.h"
35 #include "freedreno_util.h"
37 static enum sq_tex_clamp
38 tex_clamp(unsigned wrap
)
41 case PIPE_TEX_WRAP_REPEAT
:
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
:
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
;
58 DBG("invalid wrap: %u", wrap
);
63 static enum sq_tex_filter
64 tex_filter(unsigned 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
;
72 DBG("invalid filter: %u", filter
);
78 fd_sampler_state_create(struct pipe_context
*pctx
,
79 const struct pipe_sampler_state
*cso
)
81 struct fd_sampler_stateobj
*so
= CALLOC_STRUCT(fd_sampler_stateobj
);
88 /* SQ_TEX0_PITCH() must be OR'd in later when we know the bound texture: */
90 A2XX_SQ_TEX_0_CLAMP_X(tex_clamp(cso
->wrap_s
)) |
91 A2XX_SQ_TEX_0_CLAMP_Y(tex_clamp(cso
->wrap_t
)) |
92 A2XX_SQ_TEX_0_CLAMP_Z(tex_clamp(cso
->wrap_r
));
95 A2XX_SQ_TEX_3_XY_MAG_FILTER(tex_filter(cso
->mag_img_filter
)) |
96 A2XX_SQ_TEX_3_XY_MIN_FILTER(tex_filter(cso
->min_img_filter
));
98 so
->tex4
= 0x00000000; /* ??? */
99 so
->tex5
= 0x00000200; /* ??? */
105 fd_sampler_state_delete(struct pipe_context
*pctx
, void *hwcso
)
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
)
114 struct fd_pipe_sampler_view
*so
= CALLOC_STRUCT(fd_pipe_sampler_view
);
115 struct fd_resource
*rsc
= fd_resource(prsc
);
121 pipe_reference(NULL
, &prsc
->reference
);
122 so
->base
.texture
= prsc
;
123 so
->base
.reference
.count
= 1;
124 so
->base
.context
= pctx
;
126 so
->tex_resource
= rsc
;
127 so
->fmt
= fd_pipe2surface(cso
->format
);
129 so
->tex0
= A2XX_SQ_TEX_0_PITCH(rsc
->pitch
);
131 A2XX_SQ_TEX_2_HEIGHT(prsc
->height0
- 1) |
132 A2XX_SQ_TEX_2_WIDTH(prsc
->width0
- 1);
133 so
->tex3
= fd_tex_swiz(cso
->format
, cso
->swizzle_r
, cso
->swizzle_g
,
134 cso
->swizzle_b
, cso
->swizzle_a
);
140 fd_sampler_view_destroy(struct pipe_context
*pctx
,
141 struct pipe_sampler_view
*view
)
143 pipe_resource_reference(&view
->texture
, NULL
);
147 static void bind_sampler_states(struct fd_texture_stateobj
*prog
,
148 unsigned nr
, void **hwcso
)
152 for (i
= 0; i
< nr
; i
++) {
153 prog
->samplers
[i
] = hwcso
[i
];
154 prog
->dirty_samplers
|= (1 << i
);
157 for (; i
< prog
->num_samplers
; i
++) {
158 prog
->samplers
[i
] = NULL
;
159 prog
->dirty_samplers
|= (1 << i
);
162 prog
->num_samplers
= nr
;
165 static void set_sampler_views(struct fd_texture_stateobj
*prog
,
166 unsigned nr
, struct pipe_sampler_view
**views
)
170 for (i
= 0; i
< nr
; i
++) {
171 pipe_sampler_view_reference(&prog
->textures
[i
], views
[i
]);
172 prog
->dirty_samplers
|= (1 << i
);
175 for (; i
< prog
->num_textures
; i
++) {
176 pipe_sampler_view_reference(&prog
->textures
[i
], NULL
);
177 prog
->dirty_samplers
|= (1 << i
);
180 prog
->num_textures
= nr
;
184 fd_fragtex_sampler_states_bind(struct pipe_context
*pctx
,
185 unsigned nr
, void **hwcso
)
187 struct fd_context
*ctx
= fd_context(pctx
);
189 /* on a2xx, since there is a flat address space for textures/samplers,
190 * a change in # of fragment textures/samplers will trigger patching and
191 * re-emitting the vertex shader:
193 if (nr
!= ctx
->fragtex
.num_samplers
)
194 ctx
->dirty
|= FD_DIRTY_TEXSTATE
;
196 bind_sampler_states(&ctx
->fragtex
, nr
, hwcso
);
197 ctx
->dirty
|= FD_DIRTY_FRAGTEX
;
202 fd_fragtex_set_sampler_views(struct pipe_context
*pctx
, unsigned nr
,
203 struct pipe_sampler_view
**views
)
205 struct fd_context
*ctx
= fd_context(pctx
);
207 /* on a2xx, since there is a flat address space for textures/samplers,
208 * a change in # of fragment textures/samplers will trigger patching and
209 * re-emitting the vertex shader:
211 if (nr
!= ctx
->fragtex
.num_textures
)
212 ctx
->dirty
|= FD_DIRTY_TEXSTATE
;
214 set_sampler_views(&ctx
->fragtex
, nr
, views
);
215 ctx
->dirty
|= FD_DIRTY_FRAGTEX
;
219 fd_verttex_sampler_states_bind(struct pipe_context
*pctx
,
220 unsigned nr
, void **hwcso
)
222 struct fd_context
*ctx
= fd_context(pctx
);
223 bind_sampler_states(&ctx
->verttex
, nr
, hwcso
);
224 ctx
->dirty
|= FD_DIRTY_VERTTEX
;
229 fd_verttex_set_sampler_views(struct pipe_context
*pctx
, unsigned nr
,
230 struct pipe_sampler_view
**views
)
232 struct fd_context
*ctx
= fd_context(pctx
);
233 set_sampler_views(&ctx
->verttex
, nr
, views
);
234 ctx
->dirty
|= FD_DIRTY_VERTTEX
;
237 /* map gallium sampler-id to hw const-idx.. adreno uses a flat address
238 * space of samplers (const-idx), so we need to map the gallium sampler-id
239 * which is per-shader to a global const-idx space.
241 * Fragment shader sampler maps directly to const-idx, and vertex shader
242 * is offset by the # of fragment shader samplers. If the # of fragment
243 * shader samplers changes, this shifts the vertex shader indexes.
245 * TODO maybe we can do frag shader 0..N and vert shader N..0 to avoid
249 fd_get_const_idx(struct fd_context
*ctx
, struct fd_texture_stateobj
*tex
,
252 if (tex
== &ctx
->fragtex
)
254 return samp_id
+ ctx
->fragtex
.num_samplers
;
258 fd_texture_init(struct pipe_context
*pctx
)
260 pctx
->create_sampler_state
= fd_sampler_state_create
;
261 pctx
->delete_sampler_state
= fd_sampler_state_delete
;
263 pctx
->create_sampler_view
= fd_sampler_view_create
;
264 pctx
->sampler_view_destroy
= fd_sampler_view_destroy
;
266 pctx
->bind_fragment_sampler_states
= fd_fragtex_sampler_states_bind
;
267 pctx
->set_fragment_sampler_views
= fd_fragtex_set_sampler_views
;
269 pctx
->bind_vertex_sampler_states
= fd_verttex_sampler_states_bind
;
270 pctx
->set_vertex_sampler_views
= fd_verttex_set_sampler_views
;