vc4: Avoid leaking indirect array access UBOs.
[mesa.git] / src / gallium / drivers / vc4 / vc4_uniforms.c
1 /*
2 * Copyright © 2014-2015 Broadcom
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "util/u_pack_color.h"
25 #include "util/format_srgb.h"
26
27 #include "vc4_context.h"
28 #include "vc4_qir.h"
29
30 static void
31 write_texture_p0(struct vc4_context *vc4,
32 struct vc4_cl_out **uniforms,
33 struct vc4_texture_stateobj *texstate,
34 uint32_t unit)
35 {
36 struct vc4_sampler_view *sview =
37 vc4_sampler_view(texstate->textures[unit]);
38 struct vc4_resource *rsc = vc4_resource(sview->base.texture);
39
40 cl_reloc(vc4, &vc4->uniforms, uniforms, rsc->bo, sview->texture_p0);
41 }
42
43 static void
44 write_texture_p1(struct vc4_context *vc4,
45 struct vc4_cl_out **uniforms,
46 struct vc4_texture_stateobj *texstate,
47 uint32_t unit)
48 {
49 struct vc4_sampler_view *sview =
50 vc4_sampler_view(texstate->textures[unit]);
51 struct vc4_sampler_state *sampler =
52 vc4_sampler_state(texstate->samplers[unit]);
53
54 cl_aligned_u32(uniforms, sview->texture_p1 | sampler->texture_p1);
55 }
56
57 static void
58 write_texture_p2(struct vc4_context *vc4,
59 struct vc4_cl_out **uniforms,
60 struct vc4_texture_stateobj *texstate,
61 uint32_t data)
62 {
63 uint32_t unit = data & 0xffff;
64 struct pipe_sampler_view *texture = texstate->textures[unit];
65 struct vc4_resource *rsc = vc4_resource(texture->texture);
66
67 cl_aligned_u32(uniforms,
68 VC4_SET_FIELD(VC4_TEX_P2_PTYPE_CUBE_MAP_STRIDE,
69 VC4_TEX_P2_PTYPE) |
70 VC4_SET_FIELD(rsc->cube_map_stride >> 12, VC4_TEX_P2_CMST) |
71 VC4_SET_FIELD((data >> 16) & 1, VC4_TEX_P2_BSLOD));
72 }
73
74
75 #define SWIZ(x,y,z,w) { \
76 UTIL_FORMAT_SWIZZLE_##x, \
77 UTIL_FORMAT_SWIZZLE_##y, \
78 UTIL_FORMAT_SWIZZLE_##z, \
79 UTIL_FORMAT_SWIZZLE_##w \
80 }
81
82 static void
83 write_texture_border_color(struct vc4_context *vc4,
84 struct vc4_cl_out **uniforms,
85 struct vc4_texture_stateobj *texstate,
86 uint32_t unit)
87 {
88 struct pipe_sampler_state *sampler = texstate->samplers[unit];
89 struct pipe_sampler_view *texture = texstate->textures[unit];
90 struct vc4_resource *rsc = vc4_resource(texture->texture);
91 union util_color uc;
92
93 const struct util_format_description *tex_format_desc =
94 util_format_description(texture->format);
95
96 float border_color[4];
97 for (int i = 0; i < 4; i++)
98 border_color[i] = sampler->border_color.f[i];
99 if (util_format_is_srgb(texture->format)) {
100 for (int i = 0; i < 3; i++)
101 border_color[i] =
102 util_format_linear_to_srgb_float(border_color[i]);
103 }
104
105 /* Turn the border color into the layout of channels that it would
106 * have when stored as texture contents.
107 */
108 float storage_color[4];
109 util_format_unswizzle_4f(storage_color,
110 border_color,
111 tex_format_desc->swizzle);
112
113 /* Now, pack so that when the vc4_format-sampled texture contents are
114 * replaced with our border color, the vc4_get_format_swizzle()
115 * swizzling will get the right channels.
116 */
117 if (util_format_is_depth_or_stencil(texture->format)) {
118 uc.ui[0] = util_pack_z(PIPE_FORMAT_Z24X8_UNORM,
119 sampler->border_color.f[0]) << 8;
120 } else {
121 switch (rsc->vc4_format) {
122 default:
123 case VC4_TEXTURE_TYPE_RGBA8888:
124 util_pack_color(storage_color,
125 PIPE_FORMAT_R8G8B8A8_UNORM, &uc);
126 break;
127 case VC4_TEXTURE_TYPE_RGBA4444:
128 util_pack_color(storage_color,
129 PIPE_FORMAT_A8B8G8R8_UNORM, &uc);
130 break;
131 case VC4_TEXTURE_TYPE_RGB565:
132 util_pack_color(storage_color,
133 PIPE_FORMAT_B8G8R8A8_UNORM, &uc);
134 break;
135 case VC4_TEXTURE_TYPE_ALPHA:
136 uc.ui[0] = float_to_ubyte(storage_color[0]) << 24;
137 break;
138 case VC4_TEXTURE_TYPE_LUMALPHA:
139 uc.ui[0] = ((float_to_ubyte(storage_color[1]) << 24) |
140 (float_to_ubyte(storage_color[0]) << 0));
141 break;
142 }
143 }
144
145 cl_aligned_u32(uniforms, uc.ui[0]);
146 }
147
148 static uint32_t
149 get_texrect_scale(struct vc4_texture_stateobj *texstate,
150 enum quniform_contents contents,
151 uint32_t data)
152 {
153 struct pipe_sampler_view *texture = texstate->textures[data];
154 uint32_t dim;
155
156 if (contents == QUNIFORM_TEXRECT_SCALE_X)
157 dim = texture->texture->width0;
158 else
159 dim = texture->texture->height0;
160
161 return fui(1.0f / dim);
162 }
163
164 static struct vc4_bo *
165 vc4_upload_ubo(struct vc4_context *vc4,
166 struct vc4_compiled_shader *shader,
167 const uint32_t *gallium_uniforms)
168 {
169 if (!shader->ubo_size)
170 return NULL;
171
172 struct vc4_bo *ubo = vc4_bo_alloc(vc4->screen, shader->ubo_size, "ubo");
173 uint32_t *data = vc4_bo_map(ubo);
174 for (uint32_t i = 0; i < shader->num_ubo_ranges; i++) {
175 memcpy(data + shader->ubo_ranges[i].dst_offset,
176 gallium_uniforms + shader->ubo_ranges[i].src_offset,
177 shader->ubo_ranges[i].size);
178 }
179
180 return ubo;
181 }
182
183 void
184 vc4_write_uniforms(struct vc4_context *vc4, struct vc4_compiled_shader *shader,
185 struct vc4_constbuf_stateobj *cb,
186 struct vc4_texture_stateobj *texstate)
187 {
188 struct vc4_shader_uniform_info *uinfo = &shader->uniforms;
189 const uint32_t *gallium_uniforms = cb->cb[0].user_buffer;
190 struct vc4_bo *ubo = vc4_upload_ubo(vc4, shader, gallium_uniforms);
191
192 cl_ensure_space(&vc4->uniforms, (uinfo->count +
193 uinfo->num_texture_samples) * 4);
194
195 struct vc4_cl_out *uniforms =
196 cl_start_shader_reloc(&vc4->uniforms,
197 uinfo->num_texture_samples);
198
199 for (int i = 0; i < uinfo->count; i++) {
200
201 switch (uinfo->contents[i]) {
202 case QUNIFORM_CONSTANT:
203 cl_aligned_u32(&uniforms, uinfo->data[i]);
204 break;
205 case QUNIFORM_UNIFORM:
206 cl_aligned_u32(&uniforms,
207 gallium_uniforms[uinfo->data[i]]);
208 break;
209 case QUNIFORM_VIEWPORT_X_SCALE:
210 cl_aligned_f(&uniforms, vc4->viewport.scale[0] * 16.0f);
211 break;
212 case QUNIFORM_VIEWPORT_Y_SCALE:
213 cl_aligned_f(&uniforms, vc4->viewport.scale[1] * 16.0f);
214 break;
215
216 case QUNIFORM_VIEWPORT_Z_OFFSET:
217 cl_aligned_f(&uniforms, vc4->viewport.translate[2]);
218 break;
219 case QUNIFORM_VIEWPORT_Z_SCALE:
220 cl_aligned_f(&uniforms, vc4->viewport.scale[2]);
221 break;
222
223 case QUNIFORM_USER_CLIP_PLANE:
224 cl_aligned_f(&uniforms,
225 vc4->clip.ucp[uinfo->data[i] / 4][uinfo->data[i] % 4]);
226 break;
227
228 case QUNIFORM_TEXTURE_CONFIG_P0:
229 write_texture_p0(vc4, &uniforms, texstate,
230 uinfo->data[i]);
231 break;
232
233 case QUNIFORM_TEXTURE_CONFIG_P1:
234 write_texture_p1(vc4, &uniforms, texstate,
235 uinfo->data[i]);
236 break;
237
238 case QUNIFORM_TEXTURE_CONFIG_P2:
239 write_texture_p2(vc4, &uniforms, texstate,
240 uinfo->data[i]);
241 break;
242
243 case QUNIFORM_UBO_ADDR:
244 cl_aligned_reloc(vc4, &vc4->uniforms, &uniforms, ubo, 0);
245 break;
246
247 case QUNIFORM_TEXTURE_BORDER_COLOR:
248 write_texture_border_color(vc4, &uniforms,
249 texstate, uinfo->data[i]);
250 break;
251
252 case QUNIFORM_TEXRECT_SCALE_X:
253 case QUNIFORM_TEXRECT_SCALE_Y:
254 cl_aligned_u32(&uniforms,
255 get_texrect_scale(texstate,
256 uinfo->contents[i],
257 uinfo->data[i]));
258 break;
259
260 case QUNIFORM_BLEND_CONST_COLOR:
261 cl_aligned_f(&uniforms,
262 CLAMP(vc4->blend_color.color[uinfo->data[i]], 0, 1));
263 break;
264
265 case QUNIFORM_STENCIL:
266 cl_aligned_u32(&uniforms,
267 vc4->zsa->stencil_uniforms[uinfo->data[i]] |
268 (uinfo->data[i] <= 1 ?
269 (vc4->stencil_ref.ref_value[uinfo->data[i]] << 8) :
270 0));
271 break;
272
273 case QUNIFORM_ALPHA_REF:
274 cl_aligned_f(&uniforms,
275 vc4->zsa->base.alpha.ref_value);
276 break;
277 }
278 #if 0
279 uint32_t written_val = *((uint32_t *)uniforms - 1);
280 fprintf(stderr, "%p: %d / 0x%08x (%f)\n",
281 shader, i, written_val, uif(written_val));
282 #endif
283 }
284
285 cl_end(&vc4->uniforms, uniforms);
286
287 vc4_bo_unreference(&ubo);
288 }
289
290 void
291 vc4_set_shader_uniform_dirty_flags(struct vc4_compiled_shader *shader)
292 {
293 uint32_t dirty = 0;
294
295 for (int i = 0; i < shader->uniforms.count; i++) {
296 switch (shader->uniforms.contents[i]) {
297 case QUNIFORM_CONSTANT:
298 break;
299 case QUNIFORM_UNIFORM:
300 case QUNIFORM_UBO_ADDR:
301 dirty |= VC4_DIRTY_CONSTBUF;
302 break;
303
304 case QUNIFORM_VIEWPORT_X_SCALE:
305 case QUNIFORM_VIEWPORT_Y_SCALE:
306 case QUNIFORM_VIEWPORT_Z_OFFSET:
307 case QUNIFORM_VIEWPORT_Z_SCALE:
308 dirty |= VC4_DIRTY_VIEWPORT;
309 break;
310
311 case QUNIFORM_USER_CLIP_PLANE:
312 dirty |= VC4_DIRTY_CLIP;
313 break;
314
315 case QUNIFORM_TEXTURE_CONFIG_P0:
316 case QUNIFORM_TEXTURE_CONFIG_P1:
317 case QUNIFORM_TEXTURE_CONFIG_P2:
318 case QUNIFORM_TEXTURE_BORDER_COLOR:
319 case QUNIFORM_TEXRECT_SCALE_X:
320 case QUNIFORM_TEXRECT_SCALE_Y:
321 dirty |= VC4_DIRTY_TEXSTATE;
322 break;
323
324 case QUNIFORM_BLEND_CONST_COLOR:
325 dirty |= VC4_DIRTY_BLEND_COLOR;
326 break;
327
328 case QUNIFORM_STENCIL:
329 case QUNIFORM_ALPHA_REF:
330 dirty |= VC4_DIRTY_ZSA;
331 break;
332 }
333 }
334
335 shader->uniform_dirty_bits = dirty;
336 }