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_string.h"
29 #include "util/u_memory.h"
30 #include "util/u_inlines.h"
31
32 #include "freedreno_texture.h"
33 #include "freedreno_context.h"
34 #include "freedreno_resource.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
bind_sampler_states(struct fd_texture_stateobj * tex,unsigned start,unsigned nr,void ** hwcso)51 static void bind_sampler_states(struct fd_texture_stateobj *tex,
52 unsigned start, unsigned nr, void **hwcso)
53 {
54 unsigned i;
55
56 for (i = 0; i < nr; i++) {
57 unsigned p = i + start;
58 tex->samplers[p] = hwcso[i];
59 if (tex->samplers[p])
60 tex->valid_samplers |= (1 << p);
61 else
62 tex->valid_samplers &= ~(1 << p);
63 }
64
65 tex->num_samplers = util_last_bit(tex->valid_samplers);
66 }
67
set_sampler_views(struct fd_texture_stateobj * tex,unsigned start,unsigned nr,struct pipe_sampler_view ** views)68 static void set_sampler_views(struct fd_texture_stateobj *tex,
69 unsigned start, unsigned nr, struct pipe_sampler_view **views)
70 {
71 unsigned i;
72 unsigned samplers = 0;
73
74 for (i = 0; i < nr; i++) {
75 struct pipe_sampler_view *view = views ? views[i] : NULL;
76 unsigned p = i + start;
77 pipe_sampler_view_reference(&tex->textures[p], view);
78 if (tex->textures[p]) {
79 fd_resource_set_usage(tex->textures[p]->texture, FD_DIRTY_TEX);
80 tex->valid_textures |= (1 << p);
81 } else {
82 tex->valid_textures &= ~(1 << p);
83 }
84 }
85
86 tex->num_textures = util_last_bit(tex->valid_textures);
87
88 for (i = 0; i < tex->num_textures; i++) {
89 uint nr_samples = fd_resource_nr_samples(tex->textures[i]->texture);
90 samplers |= (nr_samples >> 1) << (i * 2);
91 }
92
93 tex->samples = samplers;
94 }
95
96 void
fd_sampler_states_bind(struct pipe_context * pctx,enum pipe_shader_type shader,unsigned start,unsigned nr,void ** hwcso)97 fd_sampler_states_bind(struct pipe_context *pctx,
98 enum pipe_shader_type shader, unsigned start,
99 unsigned nr, void **hwcso)
100 {
101 struct fd_context *ctx = fd_context(pctx);
102
103 bind_sampler_states(&ctx->tex[shader], start, nr, hwcso);
104 ctx->dirty_shader[shader] |= FD_DIRTY_SHADER_TEX;
105 ctx->dirty |= FD_DIRTY_TEX;
106 }
107
108 void
fd_set_sampler_views(struct pipe_context * pctx,enum pipe_shader_type shader,unsigned start,unsigned nr,struct pipe_sampler_view ** views)109 fd_set_sampler_views(struct pipe_context *pctx, enum pipe_shader_type shader,
110 unsigned start, unsigned nr,
111 struct pipe_sampler_view **views)
112 {
113 struct fd_context *ctx = fd_context(pctx);
114
115 set_sampler_views(&ctx->tex[shader], start, nr, views);
116 ctx->dirty_shader[shader] |= FD_DIRTY_SHADER_TEX;
117 ctx->dirty |= FD_DIRTY_TEX;
118 }
119
120 void
fd_texture_init(struct pipe_context * pctx)121 fd_texture_init(struct pipe_context *pctx)
122 {
123 if (!pctx->delete_sampler_state)
124 pctx->delete_sampler_state = fd_sampler_state_delete;
125 if (!pctx->sampler_view_destroy)
126 pctx->sampler_view_destroy = fd_sampler_view_destroy;
127 }
128
129 /* helper for setting up border-color buffer for a3xx/a4xx: */
130 void
fd_setup_border_colors(struct fd_texture_stateobj * tex,void * ptr,unsigned offset)131 fd_setup_border_colors(struct fd_texture_stateobj *tex, void *ptr,
132 unsigned offset)
133 {
134 unsigned i, j;
135
136 for (i = 0; i < tex->num_samplers; i++) {
137 struct pipe_sampler_state *sampler = tex->samplers[i];
138 uint16_t *bcolor = (uint16_t *)((uint8_t *)ptr +
139 (BORDERCOLOR_SIZE * offset) +
140 (BORDERCOLOR_SIZE * i));
141 uint32_t *bcolor32 = (uint32_t *)&bcolor[16];
142
143 if (!sampler)
144 continue;
145
146 /*
147 * XXX HACK ALERT XXX
148 *
149 * The border colors need to be swizzled in a particular
150 * format-dependent order. Even though samplers don't know about
151 * formats, we can assume that with a GL state tracker, there's a
152 * 1:1 correspondence between sampler and texture. Take advantage
153 * of that knowledge.
154 */
155 if (i < tex->num_textures && tex->textures[i]) {
156 const struct util_format_description *desc =
157 util_format_description(tex->textures[i]->format);
158 for (j = 0; j < 4; j++) {
159 if (desc->swizzle[j] >= 4)
160 continue;
161
162 const struct util_format_channel_description *chan =
163 &desc->channel[desc->swizzle[j]];
164 if (chan->pure_integer) {
165 bcolor32[desc->swizzle[j] + 4] = sampler->border_color.i[j];
166 bcolor[desc->swizzle[j] + 8] = sampler->border_color.i[j];
167 } else {
168 bcolor32[desc->swizzle[j]] = fui(sampler->border_color.f[j]);
169 bcolor[desc->swizzle[j]] =
170 _mesa_float_to_half(sampler->border_color.f[j]);
171 }
172 }
173 }
174 }
175 }
176