v3d: Add VIR dumping of TMU config p0/p1.
[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 struct v3d_bo *
75 v3d_upload_ubo(struct v3d_context *v3d,
76 struct v3d_compiled_shader *shader,
77 const uint32_t *gallium_uniforms)
78 {
79 if (!shader->prog_data.base->ubo_size)
80 return NULL;
81
82 struct v3d_bo *ubo = v3d_bo_alloc(v3d->screen,
83 shader->prog_data.base->ubo_size,
84 "ubo");
85 void *data = v3d_bo_map(ubo);
86 for (uint32_t i = 0; i < shader->prog_data.base->num_ubo_ranges; i++) {
87 memcpy(data + shader->prog_data.base->ubo_ranges[i].dst_offset,
88 ((const void *)gallium_uniforms +
89 shader->prog_data.base->ubo_ranges[i].src_offset),
90 shader->prog_data.base->ubo_ranges[i].size);
91 }
92
93 return ubo;
94 }
95
96 /**
97 * Writes the V3D 3.x P0 (CFG_MODE=1) texture parameter.
98 *
99 * Some bits of this field are dependent on the type of sample being done by
100 * the shader, while other bits are dependent on the sampler state. We OR the
101 * two together here.
102 */
103 static void
104 write_texture_p0(struct v3d_job *job,
105 struct v3d_cl_out **uniforms,
106 struct v3d_texture_stateobj *texstate,
107 uint32_t unit,
108 uint32_t shader_data)
109 {
110 struct pipe_sampler_state *psampler = texstate->samplers[unit];
111 struct v3d_sampler_state *sampler = v3d_sampler_state(psampler);
112
113 cl_aligned_u32(uniforms, shader_data | sampler->p0);
114 }
115
116 /** Writes the V3D 3.x P1 (CFG_MODE=1) texture parameter. */
117 static void
118 write_texture_p1(struct v3d_job *job,
119 struct v3d_cl_out **uniforms,
120 struct v3d_texture_stateobj *texstate,
121 uint32_t data)
122 {
123 /* Extract the texture unit from the top bits, and the compiler's
124 * packed p1 from the bottom.
125 */
126 uint32_t unit = data >> 5;
127 uint32_t p1 = data & 0x1f;
128
129 struct pipe_sampler_view *psview = texstate->textures[unit];
130 struct v3d_sampler_view *sview = v3d_sampler_view(psview);
131
132 struct V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1 unpacked = {
133 .texture_state_record_base_address = texstate->texture_state[unit],
134 };
135
136 uint32_t packed;
137 V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1_pack(&job->indirect,
138 (uint8_t *)&packed,
139 &unpacked);
140
141 cl_aligned_u32(uniforms, p1 | packed | sview->p1);
142 }
143
144 /** Writes the V3D 4.x TMU configuration parameter 0. */
145 static void
146 write_tmu_p0(struct v3d_job *job,
147 struct v3d_cl_out **uniforms,
148 struct v3d_texture_stateobj *texstate,
149 uint32_t data)
150 {
151 int unit = v3d_tmu_config_data_get_unit(data);
152 struct pipe_sampler_view *psview = texstate->textures[unit];
153 struct v3d_sampler_view *sview = v3d_sampler_view(psview);
154 struct v3d_resource *rsc = v3d_resource(psview->texture);
155
156 cl_aligned_reloc(&job->indirect, uniforms, sview->bo,
157 v3d_tmu_config_data_get_value(data));
158 v3d_job_add_bo(job, rsc->bo);
159 }
160
161 /** Writes the V3D 4.x TMU configuration parameter 1. */
162 static void
163 write_tmu_p1(struct v3d_job *job,
164 struct v3d_cl_out **uniforms,
165 struct v3d_texture_stateobj *texstate,
166 uint32_t data)
167 {
168 uint32_t unit = v3d_tmu_config_data_get_unit(data);
169 struct pipe_sampler_state *psampler = texstate->samplers[unit];
170 struct v3d_sampler_state *sampler = v3d_sampler_state(psampler);
171
172 cl_aligned_reloc(&job->indirect, uniforms, sampler->bo,
173 v3d_tmu_config_data_get_value(data));
174 }
175
176 struct v3d_cl_reloc
177 v3d_write_uniforms(struct v3d_context *v3d, struct v3d_compiled_shader *shader,
178 enum pipe_shader_type stage)
179 {
180 struct v3d_constbuf_stateobj *cb = &v3d->constbuf[stage];
181 struct v3d_texture_stateobj *texstate = &v3d->tex[stage];
182 struct v3d_uniform_list *uinfo = &shader->prog_data.base->uniforms;
183 struct v3d_job *job = v3d->job;
184 const uint32_t *gallium_uniforms = cb->cb[0].user_buffer;
185 struct v3d_bo *ubo = v3d_upload_ubo(v3d, shader, gallium_uniforms);
186
187 /* We always need to return some space for uniforms, because the HW
188 * will be prefetching, even if we don't read any in the program.
189 */
190 v3d_cl_ensure_space(&job->indirect, MAX2(uinfo->count, 1) * 4, 4);
191
192 struct v3d_cl_reloc uniform_stream = cl_get_address(&job->indirect);
193 v3d_bo_reference(uniform_stream.bo);
194
195 struct v3d_cl_out *uniforms =
196 cl_start(&job->indirect);
197
198 for (int i = 0; i < uinfo->count; i++) {
199
200 switch (uinfo->contents[i]) {
201 case QUNIFORM_CONSTANT:
202 cl_aligned_u32(&uniforms, uinfo->data[i]);
203 break;
204 case QUNIFORM_UNIFORM:
205 cl_aligned_u32(&uniforms,
206 gallium_uniforms[uinfo->data[i]]);
207 break;
208 case QUNIFORM_VIEWPORT_X_SCALE:
209 cl_aligned_f(&uniforms, v3d->viewport.scale[0] * 256.0f);
210 break;
211 case QUNIFORM_VIEWPORT_Y_SCALE:
212 cl_aligned_f(&uniforms, v3d->viewport.scale[1] * 256.0f);
213 break;
214
215 case QUNIFORM_VIEWPORT_Z_OFFSET:
216 cl_aligned_f(&uniforms, v3d->viewport.translate[2]);
217 break;
218 case QUNIFORM_VIEWPORT_Z_SCALE:
219 cl_aligned_f(&uniforms, v3d->viewport.scale[2]);
220 break;
221
222 case QUNIFORM_USER_CLIP_PLANE:
223 cl_aligned_f(&uniforms,
224 v3d->clip.ucp[uinfo->data[i] / 4][uinfo->data[i] % 4]);
225 break;
226
227 case QUNIFORM_TMU_CONFIG_P0:
228 write_tmu_p0(job, &uniforms, texstate,
229 uinfo->data[i]);
230 break;
231
232 case QUNIFORM_TMU_CONFIG_P1:
233 write_tmu_p1(job, &uniforms, texstate,
234 uinfo->data[i]);
235 break;
236
237 case QUNIFORM_TEXTURE_CONFIG_P1:
238 write_texture_p1(job, &uniforms, texstate,
239 uinfo->data[i]);
240 break;
241
242 case QUNIFORM_TEXRECT_SCALE_X:
243 case QUNIFORM_TEXRECT_SCALE_Y:
244 cl_aligned_u32(&uniforms,
245 get_texrect_scale(texstate,
246 uinfo->contents[i],
247 uinfo->data[i]));
248 break;
249
250 case QUNIFORM_TEXTURE_WIDTH:
251 case QUNIFORM_TEXTURE_HEIGHT:
252 case QUNIFORM_TEXTURE_DEPTH:
253 case QUNIFORM_TEXTURE_ARRAY_SIZE:
254 case QUNIFORM_TEXTURE_LEVELS:
255 cl_aligned_u32(&uniforms,
256 get_texture_size(texstate,
257 uinfo->contents[i],
258 uinfo->data[i]));
259 break;
260
261 case QUNIFORM_ALPHA_REF:
262 cl_aligned_f(&uniforms,
263 v3d->zsa->base.alpha.ref_value);
264 break;
265
266 case QUNIFORM_UBO_ADDR:
267 if (uinfo->data[i] == 0) {
268 cl_aligned_reloc(&job->indirect, &uniforms,
269 ubo, 0);
270 } else {
271 int ubo_index = uinfo->data[i];
272 struct v3d_resource *rsc =
273 v3d_resource(cb->cb[ubo_index].buffer);
274
275 cl_aligned_reloc(&job->indirect, &uniforms,
276 rsc->bo,
277 cb->cb[ubo_index].buffer_offset);
278 }
279 break;
280
281 case QUNIFORM_TEXTURE_FIRST_LEVEL:
282 cl_aligned_f(&uniforms,
283 texstate->textures[uinfo->data[i]]->u.tex.first_level);
284 break;
285
286 case QUNIFORM_SPILL_OFFSET:
287 cl_aligned_reloc(&job->indirect, &uniforms,
288 v3d->prog.spill_bo, 0);
289 break;
290
291 case QUNIFORM_SPILL_SIZE_PER_THREAD:
292 cl_aligned_u32(&uniforms,
293 v3d->prog.spill_size_per_thread);
294 break;
295
296 default:
297 assert(quniform_contents_is_texture_p0(uinfo->contents[i]));
298
299 write_texture_p0(job, &uniforms, texstate,
300 uinfo->contents[i] -
301 QUNIFORM_TEXTURE_CONFIG_P0_0,
302 uinfo->data[i]);
303 break;
304
305 }
306 #if 0
307 uint32_t written_val = *((uint32_t *)uniforms - 1);
308 fprintf(stderr, "shader %p[%d]: 0x%08x / 0x%08x (%f)\n",
309 shader, i, __gen_address_offset(&uniform_stream) + i * 4,
310 written_val, uif(written_val));
311 #endif
312 }
313
314 cl_end(&job->indirect, uniforms);
315
316 v3d_bo_unreference(&ubo);
317
318 return uniform_stream;
319 }
320
321 void
322 v3d_set_shader_uniform_dirty_flags(struct v3d_compiled_shader *shader)
323 {
324 uint32_t dirty = 0;
325
326 for (int i = 0; i < shader->prog_data.base->uniforms.count; i++) {
327 switch (shader->prog_data.base->uniforms.contents[i]) {
328 case QUNIFORM_CONSTANT:
329 break;
330 case QUNIFORM_UNIFORM:
331 case QUNIFORM_UBO_ADDR:
332 dirty |= VC5_DIRTY_CONSTBUF;
333 break;
334
335 case QUNIFORM_VIEWPORT_X_SCALE:
336 case QUNIFORM_VIEWPORT_Y_SCALE:
337 case QUNIFORM_VIEWPORT_Z_OFFSET:
338 case QUNIFORM_VIEWPORT_Z_SCALE:
339 dirty |= VC5_DIRTY_VIEWPORT;
340 break;
341
342 case QUNIFORM_USER_CLIP_PLANE:
343 dirty |= VC5_DIRTY_CLIP;
344 break;
345
346 case QUNIFORM_TMU_CONFIG_P0:
347 case QUNIFORM_TMU_CONFIG_P1:
348 case QUNIFORM_TEXTURE_CONFIG_P1:
349 case QUNIFORM_TEXTURE_FIRST_LEVEL:
350 case QUNIFORM_TEXRECT_SCALE_X:
351 case QUNIFORM_TEXRECT_SCALE_Y:
352 case QUNIFORM_TEXTURE_WIDTH:
353 case QUNIFORM_TEXTURE_HEIGHT:
354 case QUNIFORM_TEXTURE_DEPTH:
355 case QUNIFORM_TEXTURE_ARRAY_SIZE:
356 case QUNIFORM_TEXTURE_LEVELS:
357 case QUNIFORM_SPILL_OFFSET:
358 case QUNIFORM_SPILL_SIZE_PER_THREAD:
359 /* We could flag this on just the stage we're
360 * compiling for, but it's not passed in.
361 */
362 dirty |= VC5_DIRTY_FRAGTEX | VC5_DIRTY_VERTTEX;
363 break;
364
365 case QUNIFORM_ALPHA_REF:
366 dirty |= VC5_DIRTY_ZSA;
367 break;
368
369 default:
370 assert(quniform_contents_is_texture_p0(shader->prog_data.base->uniforms.contents[i]));
371 dirty |= VC5_DIRTY_FRAGTEX | VC5_DIRTY_VERTTEX;
372 break;
373 }
374 }
375
376 shader->uniform_dirty_bits = dirty;
377 }