v3d: SHARED but not necessarily SCANOUT buffers on RO must be linear.
[mesa.git] / src / gallium / drivers / v3d / v3d_uniforms.c
1 /*
2 * Copyright © 2014-2017 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 "v3d_context.h"
28 #include "compiler/v3d_compiler.h"
29 #include "broadcom/cle/v3d_packet_v33_pack.h"
30
31 static uint32_t
32 get_texrect_scale(struct v3d_texture_stateobj *texstate,
33 enum quniform_contents contents,
34 uint32_t data)
35 {
36 struct pipe_sampler_view *texture = texstate->textures[data];
37 uint32_t dim;
38
39 if (contents == QUNIFORM_TEXRECT_SCALE_X)
40 dim = texture->texture->width0;
41 else
42 dim = texture->texture->height0;
43
44 return fui(1.0f / dim);
45 }
46
47 static uint32_t
48 get_texture_size(struct v3d_texture_stateobj *texstate,
49 enum quniform_contents contents,
50 uint32_t data)
51 {
52 struct pipe_sampler_view *texture = texstate->textures[data];
53
54 switch (contents) {
55 case QUNIFORM_TEXTURE_WIDTH:
56 return u_minify(texture->texture->width0,
57 texture->u.tex.first_level);
58 case QUNIFORM_TEXTURE_HEIGHT:
59 return u_minify(texture->texture->height0,
60 texture->u.tex.first_level);
61 case QUNIFORM_TEXTURE_DEPTH:
62 return u_minify(texture->texture->depth0,
63 texture->u.tex.first_level);
64 case QUNIFORM_TEXTURE_ARRAY_SIZE:
65 return texture->texture->array_size;
66 case QUNIFORM_TEXTURE_LEVELS:
67 return (texture->u.tex.last_level -
68 texture->u.tex.first_level) + 1;
69 default:
70 unreachable("Bad texture size field");
71 }
72 }
73
74 static uint32_t
75 get_image_size(struct v3d_shaderimg_stateobj *shaderimg,
76 enum quniform_contents contents,
77 uint32_t data)
78 {
79 struct v3d_image_view *image = &shaderimg->si[data];
80
81 switch (contents) {
82 case QUNIFORM_IMAGE_WIDTH:
83 return u_minify(image->base.resource->width0,
84 image->base.u.tex.level);
85 case QUNIFORM_IMAGE_HEIGHT:
86 return u_minify(image->base.resource->height0,
87 image->base.u.tex.level);
88 case QUNIFORM_IMAGE_DEPTH:
89 return u_minify(image->base.resource->depth0,
90 image->base.u.tex.level);
91 case QUNIFORM_IMAGE_ARRAY_SIZE:
92 return image->base.resource->array_size;
93 default:
94 unreachable("Bad texture size field");
95 }
96 }
97
98 static struct v3d_bo *
99 v3d_upload_ubo(struct v3d_context *v3d,
100 struct v3d_compiled_shader *shader,
101 const uint32_t *gallium_uniforms)
102 {
103 if (!shader->prog_data.base->ubo_size)
104 return NULL;
105
106 struct v3d_bo *ubo = v3d_bo_alloc(v3d->screen,
107 shader->prog_data.base->ubo_size,
108 "ubo");
109 void *data = v3d_bo_map(ubo);
110 for (uint32_t i = 0; i < shader->prog_data.base->num_ubo_ranges; i++) {
111 memcpy(data + shader->prog_data.base->ubo_ranges[i].dst_offset,
112 ((const void *)gallium_uniforms +
113 shader->prog_data.base->ubo_ranges[i].src_offset),
114 shader->prog_data.base->ubo_ranges[i].size);
115 }
116
117 return ubo;
118 }
119
120 /**
121 * Writes the V3D 3.x P0 (CFG_MODE=1) texture parameter.
122 *
123 * Some bits of this field are dependent on the type of sample being done by
124 * the shader, while other bits are dependent on the sampler state. We OR the
125 * two together here.
126 */
127 static void
128 write_texture_p0(struct v3d_job *job,
129 struct v3d_cl_out **uniforms,
130 struct v3d_texture_stateobj *texstate,
131 uint32_t unit,
132 uint32_t shader_data)
133 {
134 struct pipe_sampler_state *psampler = texstate->samplers[unit];
135 struct v3d_sampler_state *sampler = v3d_sampler_state(psampler);
136
137 cl_aligned_u32(uniforms, shader_data | sampler->p0);
138 }
139
140 /** Writes the V3D 3.x P1 (CFG_MODE=1) texture parameter. */
141 static void
142 write_texture_p1(struct v3d_job *job,
143 struct v3d_cl_out **uniforms,
144 struct v3d_texture_stateobj *texstate,
145 uint32_t data)
146 {
147 /* Extract the texture unit from the top bits, and the compiler's
148 * packed p1 from the bottom.
149 */
150 uint32_t unit = data >> 5;
151 uint32_t p1 = data & 0x1f;
152
153 struct pipe_sampler_view *psview = texstate->textures[unit];
154 struct v3d_sampler_view *sview = v3d_sampler_view(psview);
155
156 struct V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1 unpacked = {
157 .texture_state_record_base_address = texstate->texture_state[unit],
158 };
159
160 uint32_t packed;
161 V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1_pack(&job->indirect,
162 (uint8_t *)&packed,
163 &unpacked);
164
165 cl_aligned_u32(uniforms, p1 | packed | sview->p1);
166 }
167
168 /** Writes the V3D 4.x TMU configuration parameter 0. */
169 static void
170 write_tmu_p0(struct v3d_job *job,
171 struct v3d_cl_out **uniforms,
172 struct v3d_texture_stateobj *texstate,
173 uint32_t data)
174 {
175 int unit = v3d_tmu_config_data_get_unit(data);
176 struct pipe_sampler_view *psview = texstate->textures[unit];
177 struct v3d_sampler_view *sview = v3d_sampler_view(psview);
178 struct v3d_resource *rsc = v3d_resource(sview->texture);
179
180 cl_aligned_reloc(&job->indirect, uniforms, sview->bo,
181 v3d_tmu_config_data_get_value(data));
182 v3d_job_add_bo(job, rsc->bo);
183 }
184
185 static void
186 write_image_tmu_p0(struct v3d_job *job,
187 struct v3d_cl_out **uniforms,
188 struct v3d_shaderimg_stateobj *img,
189 uint32_t data)
190 {
191 /* Extract the image unit from the top bits, and the compiler's
192 * packed p0 from the bottom.
193 */
194 uint32_t unit = data >> 24;
195 uint32_t p0 = data & 0x00ffffff;
196
197 struct v3d_image_view *iview = &img->si[unit];
198 struct v3d_resource *rsc = v3d_resource(iview->base.resource);
199
200 cl_aligned_reloc(&job->indirect, uniforms,
201 v3d_resource(iview->tex_state)->bo,
202 iview->tex_state_offset | p0);
203 v3d_job_add_bo(job, rsc->bo);
204 }
205
206 /** Writes the V3D 4.x TMU configuration parameter 1. */
207 static void
208 write_tmu_p1(struct v3d_job *job,
209 struct v3d_cl_out **uniforms,
210 struct v3d_texture_stateobj *texstate,
211 uint32_t data)
212 {
213 uint32_t unit = v3d_tmu_config_data_get_unit(data);
214 struct pipe_sampler_state *psampler = texstate->samplers[unit];
215 struct v3d_sampler_state *sampler = v3d_sampler_state(psampler);
216
217 cl_aligned_reloc(&job->indirect, uniforms, sampler->bo,
218 v3d_tmu_config_data_get_value(data));
219 }
220
221 struct v3d_cl_reloc
222 v3d_write_uniforms(struct v3d_context *v3d, struct v3d_compiled_shader *shader,
223 enum pipe_shader_type stage)
224 {
225 struct v3d_constbuf_stateobj *cb = &v3d->constbuf[stage];
226 struct v3d_texture_stateobj *texstate = &v3d->tex[stage];
227 struct v3d_uniform_list *uinfo = &shader->prog_data.base->uniforms;
228 struct v3d_job *job = v3d->job;
229 const uint32_t *gallium_uniforms = cb->cb[0].user_buffer;
230 struct v3d_bo *ubo = v3d_upload_ubo(v3d, shader, gallium_uniforms);
231
232 /* We always need to return some space for uniforms, because the HW
233 * will be prefetching, even if we don't read any in the program.
234 */
235 v3d_cl_ensure_space(&job->indirect, MAX2(uinfo->count, 1) * 4, 4);
236
237 struct v3d_cl_reloc uniform_stream = cl_get_address(&job->indirect);
238 v3d_bo_reference(uniform_stream.bo);
239
240 struct v3d_cl_out *uniforms =
241 cl_start(&job->indirect);
242
243 for (int i = 0; i < uinfo->count; i++) {
244 uint32_t data = uinfo->data[i];
245
246 switch (uinfo->contents[i]) {
247 case QUNIFORM_CONSTANT:
248 cl_aligned_u32(&uniforms, data);
249 break;
250 case QUNIFORM_UNIFORM:
251 cl_aligned_u32(&uniforms, gallium_uniforms[data]);
252 break;
253 case QUNIFORM_VIEWPORT_X_SCALE:
254 cl_aligned_f(&uniforms, v3d->viewport.scale[0] * 256.0f);
255 break;
256 case QUNIFORM_VIEWPORT_Y_SCALE:
257 cl_aligned_f(&uniforms, v3d->viewport.scale[1] * 256.0f);
258 break;
259
260 case QUNIFORM_VIEWPORT_Z_OFFSET:
261 cl_aligned_f(&uniforms, v3d->viewport.translate[2]);
262 break;
263 case QUNIFORM_VIEWPORT_Z_SCALE:
264 cl_aligned_f(&uniforms, v3d->viewport.scale[2]);
265 break;
266
267 case QUNIFORM_USER_CLIP_PLANE:
268 cl_aligned_f(&uniforms,
269 v3d->clip.ucp[data / 4][data % 4]);
270 break;
271
272 case QUNIFORM_TMU_CONFIG_P0:
273 write_tmu_p0(job, &uniforms, texstate, data);
274 break;
275
276 case QUNIFORM_TMU_CONFIG_P1:
277 write_tmu_p1(job, &uniforms, texstate, data);
278 break;
279
280 case QUNIFORM_IMAGE_TMU_CONFIG_P0:
281 write_image_tmu_p0(job, &uniforms,
282 &v3d->shaderimg[stage], data);
283 break;
284
285 case QUNIFORM_TEXTURE_CONFIG_P1:
286 write_texture_p1(job, &uniforms, texstate,
287 data);
288 break;
289
290 case QUNIFORM_TEXRECT_SCALE_X:
291 case QUNIFORM_TEXRECT_SCALE_Y:
292 cl_aligned_u32(&uniforms,
293 get_texrect_scale(texstate,
294 uinfo->contents[i],
295 data));
296 break;
297
298 case QUNIFORM_TEXTURE_WIDTH:
299 case QUNIFORM_TEXTURE_HEIGHT:
300 case QUNIFORM_TEXTURE_DEPTH:
301 case QUNIFORM_TEXTURE_ARRAY_SIZE:
302 case QUNIFORM_TEXTURE_LEVELS:
303 cl_aligned_u32(&uniforms,
304 get_texture_size(texstate,
305 uinfo->contents[i],
306 data));
307 break;
308
309 case QUNIFORM_IMAGE_WIDTH:
310 case QUNIFORM_IMAGE_HEIGHT:
311 case QUNIFORM_IMAGE_DEPTH:
312 case QUNIFORM_IMAGE_ARRAY_SIZE:
313 cl_aligned_u32(&uniforms,
314 get_image_size(&v3d->shaderimg[stage],
315 uinfo->contents[i],
316 data));
317 break;
318
319 case QUNIFORM_ALPHA_REF:
320 cl_aligned_f(&uniforms,
321 v3d->zsa->base.alpha.ref_value);
322 break;
323
324 case QUNIFORM_UBO_ADDR:
325 if (data == 0) {
326 cl_aligned_reloc(&job->indirect, &uniforms,
327 ubo, 0);
328 } else {
329 int ubo_index = data;
330 struct v3d_resource *rsc =
331 v3d_resource(cb->cb[ubo_index].buffer);
332
333 cl_aligned_reloc(&job->indirect, &uniforms,
334 rsc->bo,
335 cb->cb[ubo_index].buffer_offset);
336 }
337 break;
338
339 case QUNIFORM_SSBO_OFFSET: {
340 struct pipe_shader_buffer *sb =
341 &v3d->ssbo[stage].sb[data];
342
343 cl_aligned_reloc(&job->indirect, &uniforms,
344 v3d_resource(sb->buffer)->bo,
345 sb->buffer_offset);
346 break;
347 }
348
349 case QUNIFORM_GET_BUFFER_SIZE:
350 cl_aligned_u32(&uniforms,
351 v3d->ssbo[stage].sb[data].buffer_size);
352 break;
353
354 case QUNIFORM_TEXTURE_FIRST_LEVEL:
355 cl_aligned_f(&uniforms,
356 texstate->textures[data]->u.tex.first_level);
357 break;
358
359 case QUNIFORM_SPILL_OFFSET:
360 cl_aligned_reloc(&job->indirect, &uniforms,
361 v3d->prog.spill_bo, 0);
362 break;
363
364 case QUNIFORM_SPILL_SIZE_PER_THREAD:
365 cl_aligned_u32(&uniforms,
366 v3d->prog.spill_size_per_thread);
367 break;
368
369 default:
370 assert(quniform_contents_is_texture_p0(uinfo->contents[i]));
371
372 write_texture_p0(job, &uniforms, texstate,
373 uinfo->contents[i] -
374 QUNIFORM_TEXTURE_CONFIG_P0_0,
375 data);
376 break;
377
378 }
379 #if 0
380 uint32_t written_val = *((uint32_t *)uniforms - 1);
381 fprintf(stderr, "shader %p[%d]: 0x%08x / 0x%08x (%f) ",
382 shader, i, __gen_address_offset(&uniform_stream) + i * 4,
383 written_val, uif(written_val));
384 vir_dump_uniform(uinfo->contents[i], data);
385 fprintf(stderr, "\n");
386 #endif
387 }
388
389 cl_end(&job->indirect, uniforms);
390
391 v3d_bo_unreference(&ubo);
392
393 return uniform_stream;
394 }
395
396 void
397 v3d_set_shader_uniform_dirty_flags(struct v3d_compiled_shader *shader)
398 {
399 uint32_t dirty = 0;
400
401 for (int i = 0; i < shader->prog_data.base->uniforms.count; i++) {
402 switch (shader->prog_data.base->uniforms.contents[i]) {
403 case QUNIFORM_CONSTANT:
404 break;
405 case QUNIFORM_UNIFORM:
406 case QUNIFORM_UBO_ADDR:
407 dirty |= VC5_DIRTY_CONSTBUF;
408 break;
409
410 case QUNIFORM_VIEWPORT_X_SCALE:
411 case QUNIFORM_VIEWPORT_Y_SCALE:
412 case QUNIFORM_VIEWPORT_Z_OFFSET:
413 case QUNIFORM_VIEWPORT_Z_SCALE:
414 dirty |= VC5_DIRTY_VIEWPORT;
415 break;
416
417 case QUNIFORM_USER_CLIP_PLANE:
418 dirty |= VC5_DIRTY_CLIP;
419 break;
420
421 case QUNIFORM_TMU_CONFIG_P0:
422 case QUNIFORM_TMU_CONFIG_P1:
423 case QUNIFORM_TEXTURE_CONFIG_P1:
424 case QUNIFORM_TEXTURE_FIRST_LEVEL:
425 case QUNIFORM_TEXRECT_SCALE_X:
426 case QUNIFORM_TEXRECT_SCALE_Y:
427 case QUNIFORM_TEXTURE_WIDTH:
428 case QUNIFORM_TEXTURE_HEIGHT:
429 case QUNIFORM_TEXTURE_DEPTH:
430 case QUNIFORM_TEXTURE_ARRAY_SIZE:
431 case QUNIFORM_TEXTURE_LEVELS:
432 case QUNIFORM_SPILL_OFFSET:
433 case QUNIFORM_SPILL_SIZE_PER_THREAD:
434 /* We could flag this on just the stage we're
435 * compiling for, but it's not passed in.
436 */
437 dirty |= VC5_DIRTY_FRAGTEX | VC5_DIRTY_VERTTEX;
438 break;
439
440 case QUNIFORM_SSBO_OFFSET:
441 case QUNIFORM_GET_BUFFER_SIZE:
442 dirty |= VC5_DIRTY_SSBO;
443 break;
444
445 case QUNIFORM_IMAGE_TMU_CONFIG_P0:
446 case QUNIFORM_IMAGE_WIDTH:
447 case QUNIFORM_IMAGE_HEIGHT:
448 case QUNIFORM_IMAGE_DEPTH:
449 case QUNIFORM_IMAGE_ARRAY_SIZE:
450 dirty |= VC5_DIRTY_SHADER_IMAGE;
451 break;
452
453 case QUNIFORM_ALPHA_REF:
454 dirty |= VC5_DIRTY_ZSA;
455 break;
456
457 default:
458 assert(quniform_contents_is_texture_p0(shader->prog_data.base->uniforms.contents[i]));
459 dirty |= VC5_DIRTY_FRAGTEX | VC5_DIRTY_VERTTEX;
460 break;
461 }
462 }
463
464 shader->uniform_dirty_bits = dirty;
465 }