• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/u_upload_mgr.h"
26 
27 #include "v3d_context.h"
28 #include "compiler/v3d_compiler.h"
29 
30 /* We don't expect that the packets we use in this file change across across
31  * hw versions, so we just include directly the v33 header
32  */
33 #include "broadcom/cle/v3d_packet_v33_pack.h"
34 
35 static uint32_t
get_texrect_scale(struct v3d_texture_stateobj * texstate,enum quniform_contents contents,uint32_t data)36 get_texrect_scale(struct v3d_texture_stateobj *texstate,
37                   enum quniform_contents contents,
38                   uint32_t data)
39 {
40         struct pipe_sampler_view *texture = texstate->textures[data];
41         uint32_t dim;
42 
43         if (contents == QUNIFORM_TEXRECT_SCALE_X)
44                 dim = texture->texture->width0;
45         else
46                 dim = texture->texture->height0;
47 
48         return fui(1.0f / dim);
49 }
50 
51 static uint32_t
get_texture_size(struct v3d_texture_stateobj * texstate,enum quniform_contents contents,uint32_t data)52 get_texture_size(struct v3d_texture_stateobj *texstate,
53                  enum quniform_contents contents,
54                  uint32_t data)
55 {
56         struct pipe_sampler_view *texture = texstate->textures[data];
57         switch (contents) {
58         case QUNIFORM_TEXTURE_WIDTH:
59                 if (texture->target == PIPE_BUFFER) {
60                         return texture->u.buf.size /
61                                 util_format_get_blocksize(texture->format);
62                 } else {
63                         return u_minify(texture->texture->width0,
64                                         texture->u.tex.first_level);
65                 }
66         case QUNIFORM_TEXTURE_HEIGHT:
67                 return u_minify(texture->texture->height0,
68                                 texture->u.tex.first_level);
69         case QUNIFORM_TEXTURE_DEPTH:
70                 assert(texture->target != PIPE_BUFFER);
71                 return u_minify(texture->texture->depth0,
72                                 texture->u.tex.first_level);
73         case QUNIFORM_TEXTURE_ARRAY_SIZE:
74                 assert(texture->target != PIPE_BUFFER);
75                 return texture->texture->array_size;
76         case QUNIFORM_TEXTURE_LEVELS:
77                 assert(texture->target != PIPE_BUFFER);
78                 return (texture->u.tex.last_level -
79                         texture->u.tex.first_level) + 1;
80         default:
81                 unreachable("Bad texture size field");
82         }
83 }
84 
85 static uint32_t
get_image_size(struct v3d_shaderimg_stateobj * shaderimg,enum quniform_contents contents,uint32_t data)86 get_image_size(struct v3d_shaderimg_stateobj *shaderimg,
87                enum quniform_contents contents,
88                uint32_t data)
89 {
90         struct v3d_image_view *image = &shaderimg->si[data];
91 
92         switch (contents) {
93         case QUNIFORM_IMAGE_WIDTH:
94                 if (image->base.resource->target == PIPE_BUFFER) {
95                         return image->base.u.buf.size /
96                                 util_format_get_blocksize(image->base.format);
97                 } else {
98                         return u_minify(image->base.resource->width0,
99                                         image->base.u.tex.level);
100                 }
101         case QUNIFORM_IMAGE_HEIGHT:
102                 assert(image->base.resource->target != PIPE_BUFFER);
103                 return u_minify(image->base.resource->height0,
104                                 image->base.u.tex.level);
105         case QUNIFORM_IMAGE_DEPTH:
106                 assert(image->base.resource->target != PIPE_BUFFER);
107                 return u_minify(image->base.resource->depth0,
108                                 image->base.u.tex.level);
109         case QUNIFORM_IMAGE_ARRAY_SIZE:
110                 assert(image->base.resource->target != PIPE_BUFFER);
111                 return image->base.resource->array_size;
112         default:
113                 unreachable("Bad texture size field");
114         }
115 }
116 
117 /**
118  *  Writes the V3D 3.x P0 (CFG_MODE=1) texture parameter.
119  *
120  * Some bits of this field are dependent on the type of sample being done by
121  * the shader, while other bits are dependent on the sampler state.  We OR the
122  * two together here.
123  */
124 static void
write_texture_p0(struct v3d_job * job,struct v3d_cl_out ** uniforms,struct v3d_texture_stateobj * texstate,uint32_t unit,uint32_t shader_data)125 write_texture_p0(struct v3d_job *job,
126                  struct v3d_cl_out **uniforms,
127                  struct v3d_texture_stateobj *texstate,
128                  uint32_t unit,
129                  uint32_t shader_data)
130 {
131         struct pipe_sampler_state *psampler = texstate->samplers[unit];
132         struct v3d_sampler_state *sampler = v3d_sampler_state(psampler);
133 
134         cl_aligned_u32(uniforms, shader_data | sampler->p0);
135 }
136 
137 /** Writes the V3D 3.x P1 (CFG_MODE=1) texture parameter. */
138 static void
write_texture_p1(struct v3d_job * job,struct v3d_cl_out ** uniforms,struct v3d_texture_stateobj * texstate,uint32_t data)139 write_texture_p1(struct v3d_job *job,
140                  struct v3d_cl_out **uniforms,
141                  struct v3d_texture_stateobj *texstate,
142                  uint32_t data)
143 {
144         /* Extract the texture unit from the top bits, and the compiler's
145          * packed p1 from the bottom.
146          */
147         uint32_t unit = data >> 5;
148         uint32_t p1 = data & 0x1f;
149 
150         struct pipe_sampler_view *psview = texstate->textures[unit];
151         struct v3d_sampler_view *sview = v3d_sampler_view(psview);
152 
153         struct V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1 unpacked = {
154                 .texture_state_record_base_address = texstate->texture_state[unit],
155         };
156 
157         uint32_t packed;
158         V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1_pack(&job->indirect,
159                                                          (uint8_t *)&packed,
160                                                          &unpacked);
161 
162         cl_aligned_u32(uniforms, p1 | packed | sview->p1);
163 }
164 
165 /** Writes the V3D 4.x TMU configuration parameter 0. */
166 static void
write_tmu_p0(struct v3d_job * job,struct v3d_cl_out ** uniforms,struct v3d_texture_stateobj * texstate,uint32_t data)167 write_tmu_p0(struct v3d_job *job,
168              struct v3d_cl_out **uniforms,
169              struct v3d_texture_stateobj *texstate,
170              uint32_t data)
171 {
172         int unit = v3d_unit_data_get_unit(data);
173         struct pipe_sampler_view *psview = texstate->textures[unit];
174         struct v3d_sampler_view *sview = v3d_sampler_view(psview);
175         /* GL_OES_texture_buffer spec:
176          *     "If no buffer object is bound to the buffer texture, the
177          *      results of the texel access are undefined."
178          *
179          * This can be interpreted as allowing any result to come back, but
180          * not terminate the program (and some tests interpret that).
181          *
182          * FIXME: just return is not a full valid solution, as it could still
183          * try to get a wrong address for the shader state address. Perhaps we
184          * would need to set up a BO with a "default texture state"
185          */
186         if (sview == NULL)
187                 return;
188 
189         struct v3d_resource *rsc = v3d_resource(sview->texture);
190 
191         cl_aligned_reloc(&job->indirect, uniforms, sview->bo,
192                          v3d_unit_data_get_offset(data));
193         v3d_job_add_bo(job, rsc->bo);
194 }
195 
196 static void
write_image_tmu_p0(struct v3d_job * job,struct v3d_cl_out ** uniforms,struct v3d_shaderimg_stateobj * img,uint32_t data)197 write_image_tmu_p0(struct v3d_job *job,
198                    struct v3d_cl_out **uniforms,
199                    struct v3d_shaderimg_stateobj *img,
200                    uint32_t data)
201 {
202         /* Extract the image unit from the top bits, and the compiler's
203          * packed p0 from the bottom.
204          */
205         uint32_t unit = data >> 24;
206         uint32_t p0 = data & 0x00ffffff;
207 
208         struct v3d_image_view *iview = &img->si[unit];
209         struct v3d_resource *rsc = v3d_resource(iview->base.resource);
210 
211         cl_aligned_reloc(&job->indirect, uniforms,
212                          v3d_resource(iview->tex_state)->bo,
213                          iview->tex_state_offset | p0);
214         v3d_job_add_bo(job, rsc->bo);
215 }
216 
217 /** Writes the V3D 4.x TMU configuration parameter 1. */
218 static void
write_tmu_p1(struct v3d_job * job,struct v3d_cl_out ** uniforms,struct v3d_texture_stateobj * texstate,uint32_t data)219 write_tmu_p1(struct v3d_job *job,
220              struct v3d_cl_out **uniforms,
221              struct v3d_texture_stateobj *texstate,
222              uint32_t data)
223 {
224         uint32_t unit = v3d_unit_data_get_unit(data);
225         struct pipe_sampler_state *psampler = texstate->samplers[unit];
226         struct v3d_sampler_state *sampler = v3d_sampler_state(psampler);
227         struct pipe_sampler_view *psview = texstate->textures[unit];
228         struct v3d_sampler_view *sview = v3d_sampler_view(psview);
229         int variant = 0;
230 
231         /* If we are being asked by the compiler to write parameter 1, then we
232          * need that. So if we are at this point, we should expect to have a
233          * sampler and psampler. As an additional assert, we can check that we
234          * are not on a texel buffer case, as these don't have a sampler.
235          */
236         assert(psview->target != PIPE_BUFFER);
237         assert(sampler);
238         assert(psampler);
239 
240         if (sampler->border_color_variants)
241                 variant = sview->sampler_variant;
242 
243         cl_aligned_reloc(&job->indirect, uniforms,
244                          v3d_resource(sampler->sampler_state)->bo,
245                          sampler->sampler_state_offset[variant] |
246                          v3d_unit_data_get_offset(data));
247 }
248 
249 struct v3d_cl_reloc
v3d_write_uniforms(struct v3d_context * v3d,struct v3d_job * job,struct v3d_compiled_shader * shader,enum pipe_shader_type stage)250 v3d_write_uniforms(struct v3d_context *v3d, struct v3d_job *job,
251                    struct v3d_compiled_shader *shader,
252                    enum pipe_shader_type stage)
253 {
254         struct v3d_constbuf_stateobj *cb = &v3d->constbuf[stage];
255         struct v3d_texture_stateobj *texstate = &v3d->tex[stage];
256         struct v3d_uniform_list *uinfo = &shader->prog_data.base->uniforms;
257         const uint32_t *gallium_uniforms = cb->cb[0].user_buffer;
258 
259         /* The hardware always pre-fetches the next uniform (also when there
260          * aren't any), so we always allocate space for an extra slot. This
261          * fixes MMU exceptions reported since Linux kernel 5.4 when the
262          * uniforms fill up the tail bytes of a page in the indirect
263          * BO. In that scenario, when the hardware pre-fetches after reading
264          * the last uniform it will read beyond the end of the page and trigger
265          * the MMU exception.
266          */
267         v3d_cl_ensure_space(&job->indirect, (uinfo->count + 1) * 4, 4);
268 
269         struct v3d_cl_reloc uniform_stream = cl_get_address(&job->indirect);
270         v3d_bo_reference(uniform_stream.bo);
271 
272         struct v3d_cl_out *uniforms =
273                 cl_start(&job->indirect);
274 
275         for (int i = 0; i < uinfo->count; i++) {
276                 uint32_t data = uinfo->data[i];
277 
278                 switch (uinfo->contents[i]) {
279                 case QUNIFORM_CONSTANT:
280                         cl_aligned_u32(&uniforms, data);
281                         break;
282                 case QUNIFORM_UNIFORM:
283                         cl_aligned_u32(&uniforms, gallium_uniforms[data]);
284                         break;
285                 case QUNIFORM_VIEWPORT_X_SCALE:
286                         cl_aligned_f(&uniforms, v3d->viewport.scale[0] * 256.0f);
287                         break;
288                 case QUNIFORM_VIEWPORT_Y_SCALE:
289                         cl_aligned_f(&uniforms, v3d->viewport.scale[1] * 256.0f);
290                         break;
291 
292                 case QUNIFORM_VIEWPORT_Z_OFFSET:
293                         cl_aligned_f(&uniforms, v3d->viewport.translate[2]);
294                         break;
295                 case QUNIFORM_VIEWPORT_Z_SCALE:
296                         cl_aligned_f(&uniforms, v3d->viewport.scale[2]);
297                         break;
298 
299                 case QUNIFORM_USER_CLIP_PLANE:
300                         cl_aligned_f(&uniforms,
301                                      v3d->clip.ucp[data / 4][data % 4]);
302                         break;
303 
304                 case QUNIFORM_TMU_CONFIG_P0:
305                         write_tmu_p0(job, &uniforms, texstate, data);
306                         break;
307 
308                 case QUNIFORM_TMU_CONFIG_P1:
309                         write_tmu_p1(job, &uniforms, texstate, data);
310                         break;
311 
312                 case QUNIFORM_IMAGE_TMU_CONFIG_P0:
313                         write_image_tmu_p0(job, &uniforms,
314                                            &v3d->shaderimg[stage], data);
315                         break;
316 
317                 case QUNIFORM_TEXTURE_CONFIG_P1:
318                         write_texture_p1(job, &uniforms, texstate,
319                                          data);
320                         break;
321 
322                 case QUNIFORM_TEXRECT_SCALE_X:
323                 case QUNIFORM_TEXRECT_SCALE_Y:
324                         cl_aligned_u32(&uniforms,
325                                        get_texrect_scale(texstate,
326                                                          uinfo->contents[i],
327                                                          data));
328                         break;
329 
330                 case QUNIFORM_TEXTURE_WIDTH:
331                 case QUNIFORM_TEXTURE_HEIGHT:
332                 case QUNIFORM_TEXTURE_DEPTH:
333                 case QUNIFORM_TEXTURE_ARRAY_SIZE:
334                 case QUNIFORM_TEXTURE_LEVELS:
335                         cl_aligned_u32(&uniforms,
336                                        get_texture_size(texstate,
337                                                         uinfo->contents[i],
338                                                         data));
339                         break;
340 
341                 case QUNIFORM_IMAGE_WIDTH:
342                 case QUNIFORM_IMAGE_HEIGHT:
343                 case QUNIFORM_IMAGE_DEPTH:
344                 case QUNIFORM_IMAGE_ARRAY_SIZE:
345                         cl_aligned_u32(&uniforms,
346                                        get_image_size(&v3d->shaderimg[stage],
347                                                       uinfo->contents[i],
348                                                       data));
349                         break;
350 
351                 case QUNIFORM_LINE_WIDTH:
352                         cl_aligned_f(&uniforms,
353                                      v3d->rasterizer->base.line_width);
354                         break;
355 
356                 case QUNIFORM_AA_LINE_WIDTH:
357                         cl_aligned_f(&uniforms, v3d_get_real_line_width(v3d));
358                         break;
359 
360                 case QUNIFORM_UBO_ADDR: {
361                         uint32_t unit = v3d_unit_data_get_unit(data);
362                         /* Constant buffer 0 may be a system memory pointer,
363                          * in which case we want to upload a shadow copy to
364                          * the GPU.
365                         */
366                         if (!cb->cb[unit].buffer) {
367                                 u_upload_data(v3d->uploader, 0,
368                                               cb->cb[unit].buffer_size, 16,
369                                               cb->cb[unit].user_buffer,
370                                               &cb->cb[unit].buffer_offset,
371                                               &cb->cb[unit].buffer);
372                         }
373 
374                         cl_aligned_reloc(&job->indirect, &uniforms,
375                                          v3d_resource(cb->cb[unit].buffer)->bo,
376                                          cb->cb[unit].buffer_offset +
377                                          v3d_unit_data_get_offset(data));
378                         break;
379                 }
380 
381                 case QUNIFORM_SSBO_OFFSET: {
382                         struct pipe_shader_buffer *sb =
383                                 &v3d->ssbo[stage].sb[data];
384 
385                         cl_aligned_reloc(&job->indirect, &uniforms,
386                                          v3d_resource(sb->buffer)->bo,
387                                          sb->buffer_offset);
388                         break;
389                 }
390 
391                 case QUNIFORM_GET_SSBO_SIZE:
392                         cl_aligned_u32(&uniforms,
393                                        v3d->ssbo[stage].sb[data].buffer_size);
394                         break;
395 
396                 case QUNIFORM_TEXTURE_FIRST_LEVEL:
397                         cl_aligned_f(&uniforms,
398                                      texstate->textures[data]->u.tex.first_level);
399                         break;
400 
401                 case QUNIFORM_SPILL_OFFSET:
402                         cl_aligned_reloc(&job->indirect, &uniforms,
403                                          v3d->prog.spill_bo, 0);
404                         break;
405 
406                 case QUNIFORM_SPILL_SIZE_PER_THREAD:
407                         cl_aligned_u32(&uniforms,
408                                        v3d->prog.spill_size_per_thread);
409                         break;
410 
411                 case QUNIFORM_NUM_WORK_GROUPS:
412                         cl_aligned_u32(&uniforms,
413                                        v3d->compute_num_workgroups[data]);
414                         break;
415 
416                 case QUNIFORM_SHARED_OFFSET:
417                         cl_aligned_reloc(&job->indirect, &uniforms,
418                                          v3d->compute_shared_memory, 0);
419                         break;
420 
421                 case QUNIFORM_FB_LAYERS:
422                         cl_aligned_u32(&uniforms, job->num_layers);
423                         break;
424 
425                 default:
426                         assert(quniform_contents_is_texture_p0(uinfo->contents[i]));
427 
428                         write_texture_p0(job, &uniforms, texstate,
429                                          uinfo->contents[i] -
430                                          QUNIFORM_TEXTURE_CONFIG_P0_0,
431                                          data);
432                         break;
433 
434                 }
435 #if 0
436                 uint32_t written_val = *((uint32_t *)uniforms - 1);
437                 fprintf(stderr, "shader %p[%d]: 0x%08x / 0x%08x (%f) ",
438                         shader, i, __gen_address_offset(&uniform_stream) + i * 4,
439                         written_val, uif(written_val));
440                 vir_dump_uniform(uinfo->contents[i], data);
441                 fprintf(stderr, "\n");
442 #endif
443         }
444 
445         cl_end(&job->indirect, uniforms);
446 
447         return uniform_stream;
448 }
449 
450 void
v3d_set_shader_uniform_dirty_flags(struct v3d_compiled_shader * shader)451 v3d_set_shader_uniform_dirty_flags(struct v3d_compiled_shader *shader)
452 {
453         uint32_t dirty = 0;
454 
455         for (int i = 0; i < shader->prog_data.base->uniforms.count; i++) {
456                 switch (shader->prog_data.base->uniforms.contents[i]) {
457                 case QUNIFORM_CONSTANT:
458                         break;
459                 case QUNIFORM_UNIFORM:
460                 case QUNIFORM_UBO_ADDR:
461                         dirty |= V3D_DIRTY_CONSTBUF;
462                         break;
463 
464                 case QUNIFORM_VIEWPORT_X_SCALE:
465                 case QUNIFORM_VIEWPORT_Y_SCALE:
466                 case QUNIFORM_VIEWPORT_Z_OFFSET:
467                 case QUNIFORM_VIEWPORT_Z_SCALE:
468                         dirty |= V3D_DIRTY_VIEWPORT;
469                         break;
470 
471                 case QUNIFORM_USER_CLIP_PLANE:
472                         dirty |= V3D_DIRTY_CLIP;
473                         break;
474 
475                 case QUNIFORM_TMU_CONFIG_P0:
476                 case QUNIFORM_TMU_CONFIG_P1:
477                 case QUNIFORM_TEXTURE_CONFIG_P1:
478                 case QUNIFORM_TEXTURE_FIRST_LEVEL:
479                 case QUNIFORM_TEXRECT_SCALE_X:
480                 case QUNIFORM_TEXRECT_SCALE_Y:
481                 case QUNIFORM_TEXTURE_WIDTH:
482                 case QUNIFORM_TEXTURE_HEIGHT:
483                 case QUNIFORM_TEXTURE_DEPTH:
484                 case QUNIFORM_TEXTURE_ARRAY_SIZE:
485                 case QUNIFORM_TEXTURE_LEVELS:
486                 case QUNIFORM_SPILL_OFFSET:
487                 case QUNIFORM_SPILL_SIZE_PER_THREAD:
488                         /* We could flag this on just the stage we're
489                          * compiling for, but it's not passed in.
490                          */
491                         dirty |= V3D_DIRTY_FRAGTEX | V3D_DIRTY_VERTTEX |
492                                  V3D_DIRTY_GEOMTEX | V3D_DIRTY_COMPTEX;
493                         break;
494 
495                 case QUNIFORM_SSBO_OFFSET:
496                 case QUNIFORM_GET_SSBO_SIZE:
497                         dirty |= V3D_DIRTY_SSBO;
498                         break;
499 
500                 case QUNIFORM_IMAGE_TMU_CONFIG_P0:
501                 case QUNIFORM_IMAGE_WIDTH:
502                 case QUNIFORM_IMAGE_HEIGHT:
503                 case QUNIFORM_IMAGE_DEPTH:
504                 case QUNIFORM_IMAGE_ARRAY_SIZE:
505                         dirty |= V3D_DIRTY_SHADER_IMAGE;
506                         break;
507 
508                 case QUNIFORM_LINE_WIDTH:
509                 case QUNIFORM_AA_LINE_WIDTH:
510                         dirty |= V3D_DIRTY_RASTERIZER;
511                         break;
512 
513                 case QUNIFORM_NUM_WORK_GROUPS:
514                 case QUNIFORM_SHARED_OFFSET:
515                         /* Compute always recalculates uniforms. */
516                         break;
517 
518                 case QUNIFORM_FB_LAYERS:
519                         dirty |= V3D_DIRTY_FRAMEBUFFER;
520                         break;
521 
522                 default:
523                         assert(quniform_contents_is_texture_p0(shader->prog_data.base->uniforms.contents[i]));
524                         dirty |= V3D_DIRTY_FRAGTEX | V3D_DIRTY_VERTTEX |
525                                  V3D_DIRTY_GEOMTEX | V3D_DIRTY_COMPTEX;
526                         break;
527                 }
528         }
529 
530         shader->uniform_dirty_bits = dirty;
531 }
532