v3d: Create separate sampler states for the various blend formats.
[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 struct pipe_sampler_view *psview = texstate->textures[unit];
217 struct v3d_sampler_view *sview = v3d_sampler_view(psview);
218 int variant = 0;
219
220 if (sampler->border_color_variants)
221 variant = sview->sampler_variant;
222
223 cl_aligned_reloc(&job->indirect, uniforms,
224 v3d_resource(sampler->sampler_state)->bo,
225 sampler->sampler_state_offset[variant] |
226 v3d_tmu_config_data_get_value(data));
227 }
228
229 struct v3d_cl_reloc
230 v3d_write_uniforms(struct v3d_context *v3d, struct v3d_compiled_shader *shader,
231 enum pipe_shader_type stage)
232 {
233 struct v3d_constbuf_stateobj *cb = &v3d->constbuf[stage];
234 struct v3d_texture_stateobj *texstate = &v3d->tex[stage];
235 struct v3d_uniform_list *uinfo = &shader->prog_data.base->uniforms;
236 struct v3d_job *job = v3d->job;
237 const uint32_t *gallium_uniforms = cb->cb[0].user_buffer;
238 struct v3d_bo *ubo = v3d_upload_ubo(v3d, shader, gallium_uniforms);
239
240 /* We always need to return some space for uniforms, because the HW
241 * will be prefetching, even if we don't read any in the program.
242 */
243 v3d_cl_ensure_space(&job->indirect, MAX2(uinfo->count, 1) * 4, 4);
244
245 struct v3d_cl_reloc uniform_stream = cl_get_address(&job->indirect);
246 v3d_bo_reference(uniform_stream.bo);
247
248 struct v3d_cl_out *uniforms =
249 cl_start(&job->indirect);
250
251 for (int i = 0; i < uinfo->count; i++) {
252 uint32_t data = uinfo->data[i];
253
254 switch (uinfo->contents[i]) {
255 case QUNIFORM_CONSTANT:
256 cl_aligned_u32(&uniforms, data);
257 break;
258 case QUNIFORM_UNIFORM:
259 cl_aligned_u32(&uniforms, gallium_uniforms[data]);
260 break;
261 case QUNIFORM_VIEWPORT_X_SCALE:
262 cl_aligned_f(&uniforms, v3d->viewport.scale[0] * 256.0f);
263 break;
264 case QUNIFORM_VIEWPORT_Y_SCALE:
265 cl_aligned_f(&uniforms, v3d->viewport.scale[1] * 256.0f);
266 break;
267
268 case QUNIFORM_VIEWPORT_Z_OFFSET:
269 cl_aligned_f(&uniforms, v3d->viewport.translate[2]);
270 break;
271 case QUNIFORM_VIEWPORT_Z_SCALE:
272 cl_aligned_f(&uniforms, v3d->viewport.scale[2]);
273 break;
274
275 case QUNIFORM_USER_CLIP_PLANE:
276 cl_aligned_f(&uniforms,
277 v3d->clip.ucp[data / 4][data % 4]);
278 break;
279
280 case QUNIFORM_TMU_CONFIG_P0:
281 write_tmu_p0(job, &uniforms, texstate, data);
282 break;
283
284 case QUNIFORM_TMU_CONFIG_P1:
285 write_tmu_p1(job, &uniforms, texstate, data);
286 break;
287
288 case QUNIFORM_IMAGE_TMU_CONFIG_P0:
289 write_image_tmu_p0(job, &uniforms,
290 &v3d->shaderimg[stage], data);
291 break;
292
293 case QUNIFORM_TEXTURE_CONFIG_P1:
294 write_texture_p1(job, &uniforms, texstate,
295 data);
296 break;
297
298 case QUNIFORM_TEXRECT_SCALE_X:
299 case QUNIFORM_TEXRECT_SCALE_Y:
300 cl_aligned_u32(&uniforms,
301 get_texrect_scale(texstate,
302 uinfo->contents[i],
303 data));
304 break;
305
306 case QUNIFORM_TEXTURE_WIDTH:
307 case QUNIFORM_TEXTURE_HEIGHT:
308 case QUNIFORM_TEXTURE_DEPTH:
309 case QUNIFORM_TEXTURE_ARRAY_SIZE:
310 case QUNIFORM_TEXTURE_LEVELS:
311 cl_aligned_u32(&uniforms,
312 get_texture_size(texstate,
313 uinfo->contents[i],
314 data));
315 break;
316
317 case QUNIFORM_IMAGE_WIDTH:
318 case QUNIFORM_IMAGE_HEIGHT:
319 case QUNIFORM_IMAGE_DEPTH:
320 case QUNIFORM_IMAGE_ARRAY_SIZE:
321 cl_aligned_u32(&uniforms,
322 get_image_size(&v3d->shaderimg[stage],
323 uinfo->contents[i],
324 data));
325 break;
326
327 case QUNIFORM_ALPHA_REF:
328 cl_aligned_f(&uniforms,
329 v3d->zsa->base.alpha.ref_value);
330 break;
331
332 case QUNIFORM_UBO_ADDR:
333 if (data == 0) {
334 cl_aligned_reloc(&job->indirect, &uniforms,
335 ubo, 0);
336 } else {
337 int ubo_index = data;
338 struct v3d_resource *rsc =
339 v3d_resource(cb->cb[ubo_index].buffer);
340
341 cl_aligned_reloc(&job->indirect, &uniforms,
342 rsc->bo,
343 cb->cb[ubo_index].buffer_offset);
344 }
345 break;
346
347 case QUNIFORM_SSBO_OFFSET: {
348 struct pipe_shader_buffer *sb =
349 &v3d->ssbo[stage].sb[data];
350
351 cl_aligned_reloc(&job->indirect, &uniforms,
352 v3d_resource(sb->buffer)->bo,
353 sb->buffer_offset);
354 break;
355 }
356
357 case QUNIFORM_GET_BUFFER_SIZE:
358 cl_aligned_u32(&uniforms,
359 v3d->ssbo[stage].sb[data].buffer_size);
360 break;
361
362 case QUNIFORM_TEXTURE_FIRST_LEVEL:
363 cl_aligned_f(&uniforms,
364 texstate->textures[data]->u.tex.first_level);
365 break;
366
367 case QUNIFORM_SPILL_OFFSET:
368 cl_aligned_reloc(&job->indirect, &uniforms,
369 v3d->prog.spill_bo, 0);
370 break;
371
372 case QUNIFORM_SPILL_SIZE_PER_THREAD:
373 cl_aligned_u32(&uniforms,
374 v3d->prog.spill_size_per_thread);
375 break;
376
377 default:
378 assert(quniform_contents_is_texture_p0(uinfo->contents[i]));
379
380 write_texture_p0(job, &uniforms, texstate,
381 uinfo->contents[i] -
382 QUNIFORM_TEXTURE_CONFIG_P0_0,
383 data);
384 break;
385
386 }
387 #if 0
388 uint32_t written_val = *((uint32_t *)uniforms - 1);
389 fprintf(stderr, "shader %p[%d]: 0x%08x / 0x%08x (%f) ",
390 shader, i, __gen_address_offset(&uniform_stream) + i * 4,
391 written_val, uif(written_val));
392 vir_dump_uniform(uinfo->contents[i], data);
393 fprintf(stderr, "\n");
394 #endif
395 }
396
397 cl_end(&job->indirect, uniforms);
398
399 v3d_bo_unreference(&ubo);
400
401 return uniform_stream;
402 }
403
404 void
405 v3d_set_shader_uniform_dirty_flags(struct v3d_compiled_shader *shader)
406 {
407 uint32_t dirty = 0;
408
409 for (int i = 0; i < shader->prog_data.base->uniforms.count; i++) {
410 switch (shader->prog_data.base->uniforms.contents[i]) {
411 case QUNIFORM_CONSTANT:
412 break;
413 case QUNIFORM_UNIFORM:
414 case QUNIFORM_UBO_ADDR:
415 dirty |= VC5_DIRTY_CONSTBUF;
416 break;
417
418 case QUNIFORM_VIEWPORT_X_SCALE:
419 case QUNIFORM_VIEWPORT_Y_SCALE:
420 case QUNIFORM_VIEWPORT_Z_OFFSET:
421 case QUNIFORM_VIEWPORT_Z_SCALE:
422 dirty |= VC5_DIRTY_VIEWPORT;
423 break;
424
425 case QUNIFORM_USER_CLIP_PLANE:
426 dirty |= VC5_DIRTY_CLIP;
427 break;
428
429 case QUNIFORM_TMU_CONFIG_P0:
430 case QUNIFORM_TMU_CONFIG_P1:
431 case QUNIFORM_TEXTURE_CONFIG_P1:
432 case QUNIFORM_TEXTURE_FIRST_LEVEL:
433 case QUNIFORM_TEXRECT_SCALE_X:
434 case QUNIFORM_TEXRECT_SCALE_Y:
435 case QUNIFORM_TEXTURE_WIDTH:
436 case QUNIFORM_TEXTURE_HEIGHT:
437 case QUNIFORM_TEXTURE_DEPTH:
438 case QUNIFORM_TEXTURE_ARRAY_SIZE:
439 case QUNIFORM_TEXTURE_LEVELS:
440 case QUNIFORM_SPILL_OFFSET:
441 case QUNIFORM_SPILL_SIZE_PER_THREAD:
442 /* We could flag this on just the stage we're
443 * compiling for, but it's not passed in.
444 */
445 dirty |= VC5_DIRTY_FRAGTEX | VC5_DIRTY_VERTTEX;
446 break;
447
448 case QUNIFORM_SSBO_OFFSET:
449 case QUNIFORM_GET_BUFFER_SIZE:
450 dirty |= VC5_DIRTY_SSBO;
451 break;
452
453 case QUNIFORM_IMAGE_TMU_CONFIG_P0:
454 case QUNIFORM_IMAGE_WIDTH:
455 case QUNIFORM_IMAGE_HEIGHT:
456 case QUNIFORM_IMAGE_DEPTH:
457 case QUNIFORM_IMAGE_ARRAY_SIZE:
458 dirty |= VC5_DIRTY_SHADER_IMAGE;
459 break;
460
461 case QUNIFORM_ALPHA_REF:
462 dirty |= VC5_DIRTY_ZSA;
463 break;
464
465 default:
466 assert(quniform_contents_is_texture_p0(shader->prog_data.base->uniforms.contents[i]));
467 dirty |= VC5_DIRTY_FRAGTEX | VC5_DIRTY_VERTTEX;
468 break;
469 }
470 }
471
472 shader->uniform_dirty_bits = dirty;
473 }