• 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/format_srgb.h"
26 
27 #include "vc5_context.h"
28 #include "compiler/v3d_compiler.h"
29 #include "broadcom/cle/v3d_packet_v33_pack.h"
30 
31 #if 0
32 
33 #define SWIZ(x,y,z,w) {          \
34         PIPE_SWIZZLE_##x, \
35         PIPE_SWIZZLE_##y, \
36         PIPE_SWIZZLE_##z, \
37         PIPE_SWIZZLE_##w  \
38 }
39 
40 static void
41 write_texture_border_color(struct vc5_job *job,
42                            struct vc5_cl_out **uniforms,
43                            struct vc5_texture_stateobj *texstate,
44                            uint32_t unit)
45 {
46         struct pipe_sampler_state *sampler = texstate->samplers[unit];
47         struct pipe_sampler_view *texture = texstate->textures[unit];
48         struct vc5_resource *rsc = vc5_resource(texture->texture);
49         union util_color uc;
50 
51         const struct util_format_description *tex_format_desc =
52                 util_format_description(texture->format);
53 
54         float border_color[4];
55         for (int i = 0; i < 4; i++)
56                 border_color[i] = sampler->border_color.f[i];
57         if (util_format_is_srgb(texture->format)) {
58                 for (int i = 0; i < 3; i++)
59                         border_color[i] =
60                                 util_format_linear_to_srgb_float(border_color[i]);
61         }
62 
63         /* Turn the border color into the layout of channels that it would
64          * have when stored as texture contents.
65          */
66         float storage_color[4];
67         util_format_unswizzle_4f(storage_color,
68                                  border_color,
69                                  tex_format_desc->swizzle);
70 
71         /* Now, pack so that when the vc5_format-sampled texture contents are
72          * replaced with our border color, the vc5_get_format_swizzle()
73          * swizzling will get the right channels.
74          */
75         if (util_format_is_depth_or_stencil(texture->format)) {
76                 uc.ui[0] = util_pack_z(PIPE_FORMAT_Z24X8_UNORM,
77                                        sampler->border_color.f[0]) << 8;
78         } else {
79                 switch (rsc->vc5_format) {
80                 default:
81                 case VC5_TEXTURE_TYPE_RGBA8888:
82                         util_pack_color(storage_color,
83                                         PIPE_FORMAT_R8G8B8A8_UNORM, &uc);
84                         break;
85                 case VC5_TEXTURE_TYPE_RGBA4444:
86                         util_pack_color(storage_color,
87                                         PIPE_FORMAT_A8B8G8R8_UNORM, &uc);
88                         break;
89                 case VC5_TEXTURE_TYPE_RGB565:
90                         util_pack_color(storage_color,
91                                         PIPE_FORMAT_B8G8R8A8_UNORM, &uc);
92                         break;
93                 case VC5_TEXTURE_TYPE_ALPHA:
94                         uc.ui[0] = float_to_ubyte(storage_color[0]) << 24;
95                         break;
96                 case VC5_TEXTURE_TYPE_LUMALPHA:
97                         uc.ui[0] = ((float_to_ubyte(storage_color[1]) << 24) |
98                                     (float_to_ubyte(storage_color[0]) << 0));
99                         break;
100                 }
101         }
102 
103         cl_aligned_u32(uniforms, uc.ui[0]);
104 }
105 #endif
106 
107 static uint32_t
get_texrect_scale(struct vc5_texture_stateobj * texstate,enum quniform_contents contents,uint32_t data)108 get_texrect_scale(struct vc5_texture_stateobj *texstate,
109                   enum quniform_contents contents,
110                   uint32_t data)
111 {
112         struct pipe_sampler_view *texture = texstate->textures[data];
113         uint32_t dim;
114 
115         if (contents == QUNIFORM_TEXRECT_SCALE_X)
116                 dim = texture->texture->width0;
117         else
118                 dim = texture->texture->height0;
119 
120         return fui(1.0f / dim);
121 }
122 
123 static uint32_t
get_texture_size(struct vc5_texture_stateobj * texstate,enum quniform_contents contents,uint32_t data)124 get_texture_size(struct vc5_texture_stateobj *texstate,
125                  enum quniform_contents contents,
126                  uint32_t data)
127 {
128         struct pipe_sampler_view *texture = texstate->textures[data];
129 
130         switch (contents) {
131         case QUNIFORM_TEXTURE_WIDTH:
132                 return u_minify(texture->texture->width0,
133                                 texture->u.tex.first_level);
134         case QUNIFORM_TEXTURE_HEIGHT:
135                 return u_minify(texture->texture->height0,
136                                 texture->u.tex.first_level);
137         case QUNIFORM_TEXTURE_DEPTH:
138                 return u_minify(texture->texture->depth0,
139                                 texture->u.tex.first_level);
140         case QUNIFORM_TEXTURE_ARRAY_SIZE:
141                 return texture->texture->array_size;
142         case QUNIFORM_TEXTURE_LEVELS:
143                 return (texture->u.tex.last_level -
144                         texture->u.tex.first_level) + 1;
145         default:
146                 unreachable("Bad texture size field");
147         }
148 }
149 
150 static struct vc5_bo *
vc5_upload_ubo(struct vc5_context * vc5,struct vc5_compiled_shader * shader,const uint32_t * gallium_uniforms)151 vc5_upload_ubo(struct vc5_context *vc5,
152                struct vc5_compiled_shader *shader,
153                const uint32_t *gallium_uniforms)
154 {
155         if (!shader->prog_data.base->ubo_size)
156                 return NULL;
157 
158         struct vc5_bo *ubo = vc5_bo_alloc(vc5->screen,
159                                           shader->prog_data.base->ubo_size,
160                                           "ubo");
161         void *data = vc5_bo_map(ubo);
162         for (uint32_t i = 0; i < shader->prog_data.base->num_ubo_ranges; i++) {
163                 memcpy(data + shader->prog_data.base->ubo_ranges[i].dst_offset,
164                        ((const void *)gallium_uniforms +
165                         shader->prog_data.base->ubo_ranges[i].src_offset),
166                        shader->prog_data.base->ubo_ranges[i].size);
167         }
168 
169         return ubo;
170 }
171 
172 /**
173  *  Writes the V3D 3.x P0 (CFG_MODE=1) texture parameter.
174  *
175  * Some bits of this field are dependent on the type of sample being done by
176  * the shader, while other bits are dependent on the sampler state.  We OR the
177  * two together here.
178  */
179 static void
write_texture_p0(struct vc5_job * job,struct vc5_cl_out ** uniforms,struct vc5_texture_stateobj * texstate,uint32_t unit,uint32_t shader_data)180 write_texture_p0(struct vc5_job *job,
181                  struct vc5_cl_out **uniforms,
182                  struct vc5_texture_stateobj *texstate,
183                  uint32_t unit,
184                  uint32_t shader_data)
185 {
186         struct pipe_sampler_state *psampler = texstate->samplers[unit];
187         struct vc5_sampler_state *sampler = vc5_sampler_state(psampler);
188 
189         cl_aligned_u32(uniforms, shader_data | sampler->p0);
190 }
191 
192 /** Writes the V3D 3.x P1 (CFG_MODE=1) texture parameter. */
193 static void
write_texture_p1(struct vc5_job * job,struct vc5_cl_out ** uniforms,struct vc5_texture_stateobj * texstate,uint32_t data)194 write_texture_p1(struct vc5_job *job,
195                  struct vc5_cl_out **uniforms,
196                  struct vc5_texture_stateobj *texstate,
197                  uint32_t data)
198 {
199         /* Extract the texture unit from the top bits, and the compiler's
200          * packed p1 from the bottom.
201          */
202         uint32_t unit = data >> 5;
203         uint32_t p1 = data & 0x1f;
204 
205         struct pipe_sampler_view *psview = texstate->textures[unit];
206         struct vc5_sampler_view *sview = vc5_sampler_view(psview);
207 
208         struct V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1 unpacked = {
209                 .texture_state_record_base_address = texstate->texture_state[unit],
210         };
211 
212         uint32_t packed;
213         V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1_pack(&job->indirect,
214                                                          (uint8_t *)&packed,
215                                                          &unpacked);
216 
217         cl_aligned_u32(uniforms, p1 | packed | sview->p1);
218 }
219 
220 /** Writes the V3D 4.x TMU configuration parameter 0. */
221 static void
write_tmu_p0(struct vc5_job * job,struct vc5_cl_out ** uniforms,struct vc5_texture_stateobj * texstate,uint32_t data)222 write_tmu_p0(struct vc5_job *job,
223              struct vc5_cl_out **uniforms,
224              struct vc5_texture_stateobj *texstate,
225              uint32_t data)
226 {
227         /* Extract the texture unit from the top bits, and the compiler's
228          * packed p0 from the bottom.
229          */
230         uint32_t unit = data >> 24;
231         uint32_t p0 = data & 0x00ffffff;
232 
233         struct pipe_sampler_view *psview = texstate->textures[unit];
234         struct vc5_sampler_view *sview = vc5_sampler_view(psview);
235         struct vc5_resource *rsc = vc5_resource(psview->texture);
236 
237         cl_aligned_reloc(&job->indirect, uniforms, sview->bo, p0);
238         vc5_job_add_bo(job, rsc->bo);
239 }
240 
241 /** Writes the V3D 4.x TMU configuration parameter 1. */
242 static void
write_tmu_p1(struct vc5_job * job,struct vc5_cl_out ** uniforms,struct vc5_texture_stateobj * texstate,uint32_t data)243 write_tmu_p1(struct vc5_job *job,
244              struct vc5_cl_out **uniforms,
245              struct vc5_texture_stateobj *texstate,
246              uint32_t data)
247 {
248         /* Extract the texture unit from the top bits, and the compiler's
249          * packed p1 from the bottom.
250          */
251         uint32_t unit = data >> 24;
252         uint32_t p0 = data & 0x00ffffff;
253 
254         struct pipe_sampler_state *psampler = texstate->samplers[unit];
255         struct vc5_sampler_state *sampler = vc5_sampler_state(psampler);
256 
257         cl_aligned_reloc(&job->indirect, uniforms, sampler->bo, p0);
258 }
259 
260 struct vc5_cl_reloc
vc5_write_uniforms(struct vc5_context * vc5,struct vc5_compiled_shader * shader,struct vc5_constbuf_stateobj * cb,struct vc5_texture_stateobj * texstate)261 vc5_write_uniforms(struct vc5_context *vc5, struct vc5_compiled_shader *shader,
262                    struct vc5_constbuf_stateobj *cb,
263                    struct vc5_texture_stateobj *texstate)
264 {
265         struct v3d_uniform_list *uinfo = &shader->prog_data.base->uniforms;
266         struct vc5_job *job = vc5->job;
267         const uint32_t *gallium_uniforms = cb->cb[0].user_buffer;
268         struct vc5_bo *ubo = vc5_upload_ubo(vc5, shader, gallium_uniforms);
269 
270         /* We always need to return some space for uniforms, because the HW
271          * will be prefetching, even if we don't read any in the program.
272          */
273         vc5_cl_ensure_space(&job->indirect, MAX2(uinfo->count, 1) * 4, 4);
274 
275         struct vc5_cl_reloc uniform_stream = cl_get_address(&job->indirect);
276         vc5_bo_reference(uniform_stream.bo);
277 
278         struct vc5_cl_out *uniforms =
279                 cl_start(&job->indirect);
280 
281         for (int i = 0; i < uinfo->count; i++) {
282 
283                 switch (uinfo->contents[i]) {
284                 case QUNIFORM_CONSTANT:
285                         cl_aligned_u32(&uniforms, uinfo->data[i]);
286                         break;
287                 case QUNIFORM_UNIFORM:
288                         cl_aligned_u32(&uniforms,
289                                        gallium_uniforms[uinfo->data[i]]);
290                         break;
291                 case QUNIFORM_VIEWPORT_X_SCALE:
292                         cl_aligned_f(&uniforms, vc5->viewport.scale[0] * 256.0f);
293                         break;
294                 case QUNIFORM_VIEWPORT_Y_SCALE:
295                         cl_aligned_f(&uniforms, vc5->viewport.scale[1] * 256.0f);
296                         break;
297 
298                 case QUNIFORM_VIEWPORT_Z_OFFSET:
299                         cl_aligned_f(&uniforms, vc5->viewport.translate[2]);
300                         break;
301                 case QUNIFORM_VIEWPORT_Z_SCALE:
302                         cl_aligned_f(&uniforms, vc5->viewport.scale[2]);
303                         break;
304 
305                 case QUNIFORM_USER_CLIP_PLANE:
306                         cl_aligned_f(&uniforms,
307                                      vc5->clip.ucp[uinfo->data[i] / 4][uinfo->data[i] % 4]);
308                         break;
309 
310                 case QUNIFORM_TMU_CONFIG_P0:
311                         write_tmu_p0(job, &uniforms, texstate,
312                                          uinfo->data[i]);
313                         break;
314 
315                 case QUNIFORM_TMU_CONFIG_P1:
316                         write_tmu_p1(job, &uniforms, texstate,
317                                          uinfo->data[i]);
318                         break;
319 
320                 case QUNIFORM_TEXTURE_CONFIG_P1:
321                         write_texture_p1(job, &uniforms, texstate,
322                                          uinfo->data[i]);
323                         break;
324 
325 #if 0
326                 case QUNIFORM_TEXTURE_FIRST_LEVEL:
327                         write_texture_first_level(job, &uniforms, texstate,
328                                                   uinfo->data[i]);
329                         break;
330 #endif
331 
332                 case QUNIFORM_TEXRECT_SCALE_X:
333                 case QUNIFORM_TEXRECT_SCALE_Y:
334                         cl_aligned_u32(&uniforms,
335                                        get_texrect_scale(texstate,
336                                                          uinfo->contents[i],
337                                                          uinfo->data[i]));
338                         break;
339 
340                 case QUNIFORM_TEXTURE_WIDTH:
341                 case QUNIFORM_TEXTURE_HEIGHT:
342                 case QUNIFORM_TEXTURE_DEPTH:
343                 case QUNIFORM_TEXTURE_ARRAY_SIZE:
344                 case QUNIFORM_TEXTURE_LEVELS:
345                         cl_aligned_u32(&uniforms,
346                                        get_texture_size(texstate,
347                                                         uinfo->contents[i],
348                                                         uinfo->data[i]));
349                         break;
350 
351                 case QUNIFORM_STENCIL:
352                         cl_aligned_u32(&uniforms,
353                                        vc5->zsa->stencil_uniforms[uinfo->data[i]] |
354                                        (uinfo->data[i] <= 1 ?
355                                         (vc5->stencil_ref.ref_value[uinfo->data[i]] << 8) :
356                                         0));
357                         break;
358 
359                 case QUNIFORM_ALPHA_REF:
360                         cl_aligned_f(&uniforms,
361                                      vc5->zsa->base.alpha.ref_value);
362                         break;
363 
364                 case QUNIFORM_SAMPLE_MASK:
365                         cl_aligned_u32(&uniforms, vc5->sample_mask);
366                         break;
367 
368                 case QUNIFORM_UBO_ADDR:
369                         if (uinfo->data[i] == 0) {
370                                 cl_aligned_reloc(&job->indirect, &uniforms,
371                                                  ubo, 0);
372                         } else {
373                                 int ubo_index = uinfo->data[i];
374                                 struct vc5_resource *rsc =
375                                         vc5_resource(cb->cb[ubo_index].buffer);
376 
377                                 cl_aligned_reloc(&job->indirect, &uniforms,
378                                                  rsc->bo,
379                                                  cb->cb[ubo_index].buffer_offset);
380                         }
381                         break;
382 
383                 case QUNIFORM_TEXTURE_FIRST_LEVEL:
384                         cl_aligned_f(&uniforms,
385                                      texstate->textures[uinfo->data[i]]->u.tex.first_level);
386                         break;
387 
388                 case QUNIFORM_TEXTURE_BORDER_COLOR:
389                         /* XXX */
390                         break;
391 
392                 default:
393                         assert(quniform_contents_is_texture_p0(uinfo->contents[i]));
394 
395                         write_texture_p0(job, &uniforms, texstate,
396                                          uinfo->contents[i] -
397                                          QUNIFORM_TEXTURE_CONFIG_P0_0,
398                                          uinfo->data[i]);
399                         break;
400 
401                 }
402 #if 0
403                 uint32_t written_val = *((uint32_t *)uniforms - 1);
404                 fprintf(stderr, "shader %p[%d]: 0x%08x / 0x%08x (%f)\n",
405                         shader, i, __gen_address_offset(&uniform_stream) + i * 4,
406                         written_val, uif(written_val));
407 #endif
408         }
409 
410         cl_end(&job->indirect, uniforms);
411 
412         vc5_bo_unreference(&ubo);
413 
414         return uniform_stream;
415 }
416 
417 void
vc5_set_shader_uniform_dirty_flags(struct vc5_compiled_shader * shader)418 vc5_set_shader_uniform_dirty_flags(struct vc5_compiled_shader *shader)
419 {
420         uint32_t dirty = 0;
421 
422         for (int i = 0; i < shader->prog_data.base->uniforms.count; i++) {
423                 switch (shader->prog_data.base->uniforms.contents[i]) {
424                 case QUNIFORM_CONSTANT:
425                         break;
426                 case QUNIFORM_UNIFORM:
427                 case QUNIFORM_UBO_ADDR:
428                         dirty |= VC5_DIRTY_CONSTBUF;
429                         break;
430 
431                 case QUNIFORM_VIEWPORT_X_SCALE:
432                 case QUNIFORM_VIEWPORT_Y_SCALE:
433                 case QUNIFORM_VIEWPORT_Z_OFFSET:
434                 case QUNIFORM_VIEWPORT_Z_SCALE:
435                         dirty |= VC5_DIRTY_VIEWPORT;
436                         break;
437 
438                 case QUNIFORM_USER_CLIP_PLANE:
439                         dirty |= VC5_DIRTY_CLIP;
440                         break;
441 
442                 case QUNIFORM_TMU_CONFIG_P0:
443                 case QUNIFORM_TMU_CONFIG_P1:
444                 case QUNIFORM_TEXTURE_CONFIG_P1:
445                 case QUNIFORM_TEXTURE_BORDER_COLOR:
446                 case QUNIFORM_TEXTURE_FIRST_LEVEL:
447                 case QUNIFORM_TEXRECT_SCALE_X:
448                 case QUNIFORM_TEXRECT_SCALE_Y:
449                 case QUNIFORM_TEXTURE_WIDTH:
450                 case QUNIFORM_TEXTURE_HEIGHT:
451                 case QUNIFORM_TEXTURE_DEPTH:
452                 case QUNIFORM_TEXTURE_ARRAY_SIZE:
453                 case QUNIFORM_TEXTURE_LEVELS:
454                         /* We could flag this on just the stage we're
455                          * compiling for, but it's not passed in.
456                          */
457                         dirty |= VC5_DIRTY_FRAGTEX | VC5_DIRTY_VERTTEX;
458                         break;
459 
460                 case QUNIFORM_STENCIL:
461                 case QUNIFORM_ALPHA_REF:
462                         dirty |= VC5_DIRTY_ZSA;
463                         break;
464 
465                 case QUNIFORM_SAMPLE_MASK:
466                         dirty |= VC5_DIRTY_SAMPLE_MASK;
467                         break;
468 
469                 default:
470                         assert(quniform_contents_is_texture_p0(shader->prog_data.base->uniforms.contents[i]));
471                         dirty |= VC5_DIRTY_FRAGTEX | VC5_DIRTY_VERTTEX;
472                         break;
473                 }
474         }
475 
476         shader->uniform_dirty_bits = dirty;
477 }
478