1 /*
2 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "pipe/p_state.h"
28 #include "util/u_inlines.h"
29 #include "util/u_memory.h"
30 #include "util/u_string.h"
31
32 #include "freedreno_context.h"
33 #include "freedreno_resource.h"
34 #include "freedreno_texture.h"
35 #include "freedreno_util.h"
36
37 static void
fd_sampler_state_delete(struct pipe_context * pctx,void * hwcso)38 fd_sampler_state_delete(struct pipe_context *pctx, void *hwcso)
39 {
40 FREE(hwcso);
41 }
42
43 static void
fd_sampler_view_destroy(struct pipe_context * pctx,struct pipe_sampler_view * view)44 fd_sampler_view_destroy(struct pipe_context *pctx,
45 struct pipe_sampler_view *view)
46 {
47 pipe_resource_reference(&view->texture, NULL);
48 FREE(view);
49 }
50
51 static void
bind_sampler_states(struct fd_texture_stateobj * tex,unsigned start,unsigned nr,void ** hwcso)52 bind_sampler_states(struct fd_texture_stateobj *tex, unsigned start,
53 unsigned nr, void **hwcso)
54 {
55 unsigned i;
56
57 for (i = 0; i < nr; i++) {
58 unsigned p = i + start;
59 tex->samplers[p] = hwcso ? hwcso[i] : NULL;
60 if (tex->samplers[p])
61 tex->valid_samplers |= (1 << p);
62 else
63 tex->valid_samplers &= ~(1 << p);
64 }
65
66 tex->num_samplers = util_last_bit(tex->valid_samplers);
67 }
68
69 static void
set_sampler_views(struct fd_texture_stateobj * tex,unsigned start,unsigned nr,unsigned unbind_num_trailing_slots,bool take_ownership,struct pipe_sampler_view ** views)70 set_sampler_views(struct fd_texture_stateobj *tex, unsigned start, unsigned nr,
71 unsigned unbind_num_trailing_slots, bool take_ownership,
72 struct pipe_sampler_view **views)
73 {
74 unsigned i;
75
76 for (i = 0; i < nr; i++) {
77 struct pipe_sampler_view *view = views ? views[i] : NULL;
78 unsigned p = i + start;
79
80 if (take_ownership) {
81 pipe_sampler_view_reference(&tex->textures[p], NULL);
82 tex->textures[p] = view;
83 } else {
84 pipe_sampler_view_reference(&tex->textures[p], view);
85 }
86
87 if (tex->textures[p]) {
88 fd_resource_set_usage(tex->textures[p]->texture, FD_DIRTY_TEX);
89 tex->valid_textures |= (1 << p);
90 } else {
91 tex->valid_textures &= ~(1 << p);
92 }
93 }
94 for (; i < nr + unbind_num_trailing_slots; i++) {
95 unsigned p = i + start;
96 pipe_sampler_view_reference(&tex->textures[p], NULL);
97 tex->valid_textures &= ~(1 << p);
98 }
99
100 tex->num_textures = util_last_bit(tex->valid_textures);
101 }
102
103 void
fd_sampler_states_bind(struct pipe_context * pctx,enum pipe_shader_type shader,unsigned start,unsigned nr,void ** hwcso)104 fd_sampler_states_bind(struct pipe_context *pctx, enum pipe_shader_type shader,
105 unsigned start, unsigned nr, void **hwcso) in_dt
106 {
107 struct fd_context *ctx = fd_context(pctx);
108
109 bind_sampler_states(&ctx->tex[shader], start, nr, hwcso);
110 fd_context_dirty_shader(ctx, shader, FD_DIRTY_SHADER_TEX);
111 }
112
113 void
fd_set_sampler_views(struct pipe_context * pctx,enum pipe_shader_type shader,unsigned start,unsigned nr,unsigned unbind_num_trailing_slots,bool take_ownership,struct pipe_sampler_view ** views)114 fd_set_sampler_views(struct pipe_context *pctx, enum pipe_shader_type shader,
115 unsigned start, unsigned nr,
116 unsigned unbind_num_trailing_slots,
117 bool take_ownership,
118 struct pipe_sampler_view **views) in_dt
119 {
120 struct fd_context *ctx = fd_context(pctx);
121
122 set_sampler_views(&ctx->tex[shader], start, nr, unbind_num_trailing_slots,
123 take_ownership, views);
124 fd_context_dirty_shader(ctx, shader, FD_DIRTY_SHADER_TEX);
125 }
126
127 void
fd_texture_init(struct pipe_context * pctx)128 fd_texture_init(struct pipe_context *pctx)
129 {
130 if (!pctx->delete_sampler_state)
131 pctx->delete_sampler_state = fd_sampler_state_delete;
132 if (!pctx->sampler_view_destroy)
133 pctx->sampler_view_destroy = fd_sampler_view_destroy;
134 }
135
136 /* helper for setting up border-color buffer for a3xx/a4xx: */
137 void
fd_setup_border_colors(struct fd_texture_stateobj * tex,void * ptr,unsigned offset)138 fd_setup_border_colors(struct fd_texture_stateobj *tex, void *ptr,
139 unsigned offset)
140 {
141 unsigned i, j;
142
143 for (i = 0; i < tex->num_samplers; i++) {
144 struct pipe_sampler_state *sampler = tex->samplers[i];
145 uint16_t *bcolor =
146 (uint16_t *)((uint8_t *)ptr + (BORDERCOLOR_SIZE * offset) +
147 (BORDERCOLOR_SIZE * i));
148 uint32_t *bcolor32 = (uint32_t *)&bcolor[16];
149
150 if (!sampler)
151 continue;
152
153 /*
154 * XXX HACK ALERT XXX
155 *
156 * The border colors need to be swizzled in a particular
157 * format-dependent order. Even though samplers don't know about
158 * formats, we can assume that with a GL state tracker, there's a
159 * 1:1 correspondence between sampler and texture. Take advantage
160 * of that knowledge.
161 */
162 if (i < tex->num_textures && tex->textures[i]) {
163 const struct util_format_description *desc =
164 util_format_description(tex->textures[i]->format);
165 for (j = 0; j < 4; j++) {
166 if (desc->swizzle[j] >= 4)
167 continue;
168
169 const struct util_format_channel_description *chan =
170 &desc->channel[desc->swizzle[j]];
171 uint8_t native = desc->swizzle[j];
172 /* Special cases:
173 * - X24S8 is implemented with 8_8_8_8_UINT, so the 'native'
174 * location is actually 0 rather than 1
175 * - X32_S8X24_UINT has stencil with a secretly-S8_UINT resource
176 * so again we want 0 rather than 1
177 *
178 * In both cases, there is only one non-void format, so we don't
179 * have to be too careful.
180 *
181 * Note that this only affects a4xx -- a3xx did not support
182 * stencil texturing, and a5xx+ don't use this helper.
183 */
184 if (tex->textures[i]->format == PIPE_FORMAT_X24S8_UINT ||
185 tex->textures[i]->format == PIPE_FORMAT_X32_S8X24_UINT) {
186 native = 0;
187 }
188
189 if (chan->pure_integer) {
190 bcolor32[native + 4] = sampler->border_color.i[j];
191 bcolor[native + 8] = sampler->border_color.i[j];
192 } else {
193 bcolor32[native] = fui(sampler->border_color.f[j]);
194 bcolor[native] =
195 _mesa_float_to_half(sampler->border_color.f[j]);
196 }
197 }
198 }
199 }
200 }
201