• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2 
3 /*
4  * Copyright (C) 2012-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 "fd2_texture.h"
35 #include "fd2_util.h"
36 
37 static enum sq_tex_clamp
tex_clamp(unsigned wrap)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
tex_filter(unsigned 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 *
fd2_sampler_state_create(struct pipe_context * pctx,const struct pipe_sampler_state * cso)78 fd2_sampler_state_create(struct pipe_context *pctx,
79 		const struct pipe_sampler_state *cso)
80 {
81 	struct fd2_sampler_stateobj *so = CALLOC_STRUCT(fd2_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 		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));
93 
94 	so->tex3 =
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));
97 
98 	so->tex4 = 0x00000000; /* ??? */
99 	so->tex5 = 0x00000200; /* ??? */
100 
101 	return so;
102 }
103 
104 static void
fd2_sampler_states_bind(struct pipe_context * pctx,enum pipe_shader_type shader,unsigned start,unsigned nr,void ** hwcso)105 fd2_sampler_states_bind(struct pipe_context *pctx,
106 		enum pipe_shader_type shader, unsigned start,
107 		unsigned nr, void **hwcso)
108 {
109 	if (!hwcso)
110 		nr = 0;
111 
112 	if (shader == PIPE_SHADER_FRAGMENT) {
113 		struct fd_context *ctx = fd_context(pctx);
114 
115 		/* on a2xx, since there is a flat address space for textures/samplers,
116 		 * a change in # of fragment textures/samplers will trigger patching and
117 		 * re-emitting the vertex shader:
118 		 */
119 		if (nr != ctx->tex[PIPE_SHADER_FRAGMENT].num_samplers)
120 			ctx->dirty |= FD_DIRTY_TEXSTATE;
121 	}
122 
123 	fd_sampler_states_bind(pctx, shader, start, nr, hwcso);
124 }
125 
126 static struct pipe_sampler_view *
fd2_sampler_view_create(struct pipe_context * pctx,struct pipe_resource * prsc,const struct pipe_sampler_view * cso)127 fd2_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc,
128 		const struct pipe_sampler_view *cso)
129 {
130 	struct fd2_pipe_sampler_view *so = CALLOC_STRUCT(fd2_pipe_sampler_view);
131 	struct fd_resource *rsc = fd_resource(prsc);
132 
133 	if (!so)
134 		return NULL;
135 
136 	so->base = *cso;
137 	pipe_reference(NULL, &prsc->reference);
138 	so->base.texture = prsc;
139 	so->base.reference.count = 1;
140 	so->base.context = pctx;
141 
142 	so->fmt = fd2_pipe2surface(cso->format);
143 
144 	so->tex0 = A2XX_SQ_TEX_0_PITCH(rsc->slices[0].pitch);
145 	so->tex2 =
146 		A2XX_SQ_TEX_2_HEIGHT(prsc->height0 - 1) |
147 		A2XX_SQ_TEX_2_WIDTH(prsc->width0 - 1);
148 	so->tex3 = fd2_tex_swiz(cso->format, cso->swizzle_r, cso->swizzle_g,
149 			cso->swizzle_b, cso->swizzle_a);
150 
151 	return &so->base;
152 }
153 
154 static void
fd2_set_sampler_views(struct pipe_context * pctx,enum pipe_shader_type shader,unsigned start,unsigned nr,struct pipe_sampler_view ** views)155 fd2_set_sampler_views(struct pipe_context *pctx, enum pipe_shader_type shader,
156 		unsigned start, unsigned nr,
157 		struct pipe_sampler_view **views)
158 {
159 	if (shader == PIPE_SHADER_FRAGMENT) {
160 		struct fd_context *ctx = fd_context(pctx);
161 
162 		/* on a2xx, since there is a flat address space for textures/samplers,
163 		 * a change in # of fragment textures/samplers will trigger patching and
164 		 * re-emitting the vertex shader:
165 		 */
166 		if (nr != ctx->tex[PIPE_SHADER_FRAGMENT].num_textures)
167 			ctx->dirty |= FD_DIRTY_TEXSTATE;
168 	}
169 
170 	fd_set_sampler_views(pctx, shader, start, nr, views);
171 }
172 
173 /* map gallium sampler-id to hw const-idx.. adreno uses a flat address
174  * space of samplers (const-idx), so we need to map the gallium sampler-id
175  * which is per-shader to a global const-idx space.
176  *
177  * Fragment shader sampler maps directly to const-idx, and vertex shader
178  * is offset by the # of fragment shader samplers.  If the # of fragment
179  * shader samplers changes, this shifts the vertex shader indexes.
180  *
181  * TODO maybe we can do frag shader 0..N  and vert shader N..0 to avoid
182  * this??
183  */
184 unsigned
fd2_get_const_idx(struct fd_context * ctx,struct fd_texture_stateobj * tex,unsigned samp_id)185 fd2_get_const_idx(struct fd_context *ctx, struct fd_texture_stateobj *tex,
186 		unsigned samp_id)
187 {
188 	if (tex == &ctx->tex[PIPE_SHADER_FRAGMENT])
189 		return samp_id;
190 	return samp_id + ctx->tex[PIPE_SHADER_FRAGMENT].num_samplers;
191 }
192 
193 void
fd2_texture_init(struct pipe_context * pctx)194 fd2_texture_init(struct pipe_context *pctx)
195 {
196 	pctx->create_sampler_state = fd2_sampler_state_create;
197 	pctx->bind_sampler_states = fd2_sampler_states_bind;
198 	pctx->create_sampler_view = fd2_sampler_view_create;
199 	pctx->set_sampler_views = fd2_set_sampler_views;
200 }
201