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 void
fd_sampler_states_bind(struct pipe_context * pctx,enum pipe_shader_type shader,unsigned start,unsigned nr,void ** hwcso)52 fd_sampler_states_bind(struct pipe_context *pctx, enum pipe_shader_type shader,
53 unsigned start, unsigned nr, void **hwcso) in_dt
54 {
55 struct fd_context *ctx = fd_context(pctx);
56 struct fd_texture_stateobj *tex = &ctx->tex[shader];
57
58 for (unsigned i = 0; i < nr; i++) {
59 unsigned p = i + start;
60 tex->samplers[p] = hwcso ? hwcso[i] : NULL;
61 if (tex->samplers[p])
62 tex->valid_samplers |= (1 << p);
63 else
64 tex->valid_samplers &= ~(1 << p);
65 }
66
67 tex->num_samplers = util_last_bit(tex->valid_samplers);
68
69 fd_context_dirty_shader(ctx, shader, FD_DIRTY_SHADER_TEX);
70 }
71
72 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)73 fd_set_sampler_views(struct pipe_context *pctx, enum pipe_shader_type shader,
74 unsigned start, unsigned nr,
75 unsigned unbind_num_trailing_slots,
76 bool take_ownership,
77 struct pipe_sampler_view **views) in_dt
78 {
79 struct fd_context *ctx = fd_context(pctx);
80 struct fd_texture_stateobj *tex = &ctx->tex[shader];
81 unsigned i;
82
83 for (i = 0; i < nr; i++) {
84 struct pipe_sampler_view *view = views ? views[i] : NULL;
85 unsigned p = i + start;
86
87 if (take_ownership) {
88 pipe_sampler_view_reference(&tex->textures[p], NULL);
89 tex->textures[p] = view;
90 } else {
91 pipe_sampler_view_reference(&tex->textures[p], view);
92 }
93
94 if (tex->textures[p]) {
95 fd_resource_set_usage(tex->textures[p]->texture, FD_DIRTY_TEX);
96 fd_dirty_shader_resource(ctx, tex->textures[p]->texture,
97 shader, FD_DIRTY_SHADER_TEX, false);
98 tex->valid_textures |= (1 << p);
99 } else {
100 tex->valid_textures &= ~(1 << p);
101 }
102 }
103 for (; i < nr + unbind_num_trailing_slots; i++) {
104 unsigned p = i + start;
105 pipe_sampler_view_reference(&tex->textures[p], NULL);
106 tex->valid_textures &= ~(1 << p);
107 }
108
109 tex->num_textures = util_last_bit(tex->valid_textures);
110
111 fd_context_dirty_shader(ctx, shader, FD_DIRTY_SHADER_TEX);
112 }
113
114 void
fd_texture_init(struct pipe_context * pctx)115 fd_texture_init(struct pipe_context *pctx)
116 {
117 if (!pctx->delete_sampler_state)
118 pctx->delete_sampler_state = fd_sampler_state_delete;
119 if (!pctx->sampler_view_destroy)
120 pctx->sampler_view_destroy = fd_sampler_view_destroy;
121 }
122
123 /* helper for setting up border-color buffer for a3xx/a4xx: */
124 void
fd_setup_border_colors(struct fd_texture_stateobj * tex,void * ptr,unsigned offset)125 fd_setup_border_colors(struct fd_texture_stateobj *tex, void *ptr,
126 unsigned offset)
127 {
128 unsigned i, j;
129
130 for (i = 0; i < tex->num_samplers; i++) {
131 struct pipe_sampler_state *sampler = tex->samplers[i];
132 uint16_t *bcolor =
133 (uint16_t *)((uint8_t *)ptr + (BORDERCOLOR_SIZE * offset) +
134 (BORDERCOLOR_SIZE * i));
135 uint32_t *bcolor32 = (uint32_t *)&bcolor[16];
136
137 if (!sampler)
138 continue;
139
140 enum pipe_format format = sampler->border_color_format;
141
142 const struct util_format_description *desc =
143 util_format_description(sampler->border_color_format);
144 for (j = 0; j < 4; j++) {
145 if (desc->swizzle[j] >= 4)
146 continue;
147
148 const struct util_format_channel_description *chan =
149 &desc->channel[desc->swizzle[j]];
150 uint8_t native = desc->swizzle[j];
151 /* Special cases:
152 * - X24S8 is implemented with 8_8_8_8_UINT, so the 'native'
153 * location is actually 0 rather than 1
154 * - X32_S8X24_UINT has stencil with a secretly-S8_UINT resource
155 * so again we want 0 rather than 1
156 *
157 * In both cases, there is only one non-void format, so we don't
158 * have to be too careful.
159 *
160 * Note that this only affects a4xx -- a3xx did not support
161 * stencil texturing, and a5xx+ don't use this helper.
162 */
163 if (format == PIPE_FORMAT_X24S8_UINT ||
164 format == PIPE_FORMAT_X32_S8X24_UINT) {
165 native = 0;
166 }
167
168 if (chan->pure_integer) {
169 bcolor32[native + 4] = sampler->border_color.i[j];
170 bcolor[native + 8] = sampler->border_color.i[j];
171 } else {
172 bcolor32[native] = fui(sampler->border_color.f[j]);
173 bcolor[native] =
174 _mesa_float_to_half(sampler->border_color.f[j]);
175 }
176 }
177 }
178 }
179