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