2 * Copyright © 2014-2017 Broadcom
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:
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
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
24 #include "util/u_pack_color.h"
25 #include "util/format_srgb.h"
27 #include "v3d_context.h"
28 #include "compiler/v3d_compiler.h"
29 #include "broadcom/cle/v3d_packet_v33_pack.h"
32 get_texrect_scale(struct v3d_texture_stateobj
*texstate
,
33 enum quniform_contents contents
,
36 struct pipe_sampler_view
*texture
= texstate
->textures
[data
];
39 if (contents
== QUNIFORM_TEXRECT_SCALE_X
)
40 dim
= texture
->texture
->width0
;
42 dim
= texture
->texture
->height0
;
44 return fui(1.0f
/ dim
);
48 get_texture_size(struct v3d_texture_stateobj
*texstate
,
49 enum quniform_contents contents
,
52 struct pipe_sampler_view
*texture
= texstate
->textures
[data
];
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;
70 unreachable("Bad texture size field");
75 get_image_size(struct v3d_shaderimg_stateobj
*shaderimg
,
76 enum quniform_contents contents
,
79 struct v3d_image_view
*image
= &shaderimg
->si
[data
];
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
;
94 unreachable("Bad texture size field");
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
)
103 if (!shader
->prog_data
.base
->ubo_size
)
106 struct v3d_bo
*ubo
= v3d_bo_alloc(v3d
->screen
,
107 shader
->prog_data
.base
->ubo_size
,
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
);
121 * Writes the V3D 3.x P0 (CFG_MODE=1) texture parameter.
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
128 write_texture_p0(struct v3d_job
*job
,
129 struct v3d_cl_out
**uniforms
,
130 struct v3d_texture_stateobj
*texstate
,
132 uint32_t shader_data
)
134 struct pipe_sampler_state
*psampler
= texstate
->samplers
[unit
];
135 struct v3d_sampler_state
*sampler
= v3d_sampler_state(psampler
);
137 cl_aligned_u32(uniforms
, shader_data
| sampler
->p0
);
140 /** Writes the V3D 3.x P1 (CFG_MODE=1) texture parameter. */
142 write_texture_p1(struct v3d_job
*job
,
143 struct v3d_cl_out
**uniforms
,
144 struct v3d_texture_stateobj
*texstate
,
147 /* Extract the texture unit from the top bits, and the compiler's
148 * packed p1 from the bottom.
150 uint32_t unit
= data
>> 5;
151 uint32_t p1
= data
& 0x1f;
153 struct pipe_sampler_view
*psview
= texstate
->textures
[unit
];
154 struct v3d_sampler_view
*sview
= v3d_sampler_view(psview
);
156 struct V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1 unpacked
= {
157 .texture_state_record_base_address
= texstate
->texture_state
[unit
],
161 V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1_pack(&job
->indirect
,
165 cl_aligned_u32(uniforms
, p1
| packed
| sview
->p1
);
168 /** Writes the V3D 4.x TMU configuration parameter 0. */
170 write_tmu_p0(struct v3d_job
*job
,
171 struct v3d_cl_out
**uniforms
,
172 struct v3d_texture_stateobj
*texstate
,
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
);
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
);
186 write_image_tmu_p0(struct v3d_job
*job
,
187 struct v3d_cl_out
**uniforms
,
188 struct v3d_shaderimg_stateobj
*img
,
191 /* Extract the image unit from the top bits, and the compiler's
192 * packed p0 from the bottom.
194 uint32_t unit
= data
>> 24;
195 uint32_t p0
= data
& 0x00ffffff;
197 struct v3d_image_view
*iview
= &img
->si
[unit
];
198 struct v3d_resource
*rsc
= v3d_resource(iview
->base
.resource
);
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
);
206 /** Writes the V3D 4.x TMU configuration parameter 1. */
208 write_tmu_p1(struct v3d_job
*job
,
209 struct v3d_cl_out
**uniforms
,
210 struct v3d_texture_stateobj
*texstate
,
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
);
217 cl_aligned_reloc(&job
->indirect
, uniforms
, sampler
->bo
,
218 v3d_tmu_config_data_get_value(data
));
222 v3d_write_uniforms(struct v3d_context
*v3d
, struct v3d_compiled_shader
*shader
,
223 enum pipe_shader_type stage
)
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
);
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.
235 v3d_cl_ensure_space(&job
->indirect
, MAX2(uinfo
->count
, 1) * 4, 4);
237 struct v3d_cl_reloc uniform_stream
= cl_get_address(&job
->indirect
);
238 v3d_bo_reference(uniform_stream
.bo
);
240 struct v3d_cl_out
*uniforms
=
241 cl_start(&job
->indirect
);
243 for (int i
= 0; i
< uinfo
->count
; i
++) {
244 uint32_t data
= uinfo
->data
[i
];
246 switch (uinfo
->contents
[i
]) {
247 case QUNIFORM_CONSTANT
:
248 cl_aligned_u32(&uniforms
, data
);
250 case QUNIFORM_UNIFORM
:
251 cl_aligned_u32(&uniforms
, gallium_uniforms
[data
]);
253 case QUNIFORM_VIEWPORT_X_SCALE
:
254 cl_aligned_f(&uniforms
, v3d
->viewport
.scale
[0] * 256.0f
);
256 case QUNIFORM_VIEWPORT_Y_SCALE
:
257 cl_aligned_f(&uniforms
, v3d
->viewport
.scale
[1] * 256.0f
);
260 case QUNIFORM_VIEWPORT_Z_OFFSET
:
261 cl_aligned_f(&uniforms
, v3d
->viewport
.translate
[2]);
263 case QUNIFORM_VIEWPORT_Z_SCALE
:
264 cl_aligned_f(&uniforms
, v3d
->viewport
.scale
[2]);
267 case QUNIFORM_USER_CLIP_PLANE
:
268 cl_aligned_f(&uniforms
,
269 v3d
->clip
.ucp
[data
/ 4][data
% 4]);
272 case QUNIFORM_TMU_CONFIG_P0
:
273 write_tmu_p0(job
, &uniforms
, texstate
, data
);
276 case QUNIFORM_TMU_CONFIG_P1
:
277 write_tmu_p1(job
, &uniforms
, texstate
, data
);
280 case QUNIFORM_IMAGE_TMU_CONFIG_P0
:
281 write_image_tmu_p0(job
, &uniforms
,
282 &v3d
->shaderimg
[stage
], data
);
285 case QUNIFORM_TEXTURE_CONFIG_P1
:
286 write_texture_p1(job
, &uniforms
, texstate
,
290 case QUNIFORM_TEXRECT_SCALE_X
:
291 case QUNIFORM_TEXRECT_SCALE_Y
:
292 cl_aligned_u32(&uniforms
,
293 get_texrect_scale(texstate
,
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
,
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
],
319 case QUNIFORM_ALPHA_REF
:
320 cl_aligned_f(&uniforms
,
321 v3d
->zsa
->base
.alpha
.ref_value
);
324 case QUNIFORM_UBO_ADDR
:
326 cl_aligned_reloc(&job
->indirect
, &uniforms
,
329 int ubo_index
= data
;
330 struct v3d_resource
*rsc
=
331 v3d_resource(cb
->cb
[ubo_index
].buffer
);
333 cl_aligned_reloc(&job
->indirect
, &uniforms
,
335 cb
->cb
[ubo_index
].buffer_offset
);
339 case QUNIFORM_SSBO_OFFSET
: {
340 struct pipe_shader_buffer
*sb
=
341 &v3d
->ssbo
[stage
].sb
[data
];
343 cl_aligned_reloc(&job
->indirect
, &uniforms
,
344 v3d_resource(sb
->buffer
)->bo
,
349 case QUNIFORM_GET_BUFFER_SIZE
:
350 cl_aligned_u32(&uniforms
,
351 v3d
->ssbo
[stage
].sb
[data
].buffer_size
);
354 case QUNIFORM_TEXTURE_FIRST_LEVEL
:
355 cl_aligned_f(&uniforms
,
356 texstate
->textures
[data
]->u
.tex
.first_level
);
359 case QUNIFORM_SPILL_OFFSET
:
360 cl_aligned_reloc(&job
->indirect
, &uniforms
,
361 v3d
->prog
.spill_bo
, 0);
364 case QUNIFORM_SPILL_SIZE_PER_THREAD
:
365 cl_aligned_u32(&uniforms
,
366 v3d
->prog
.spill_size_per_thread
);
370 assert(quniform_contents_is_texture_p0(uinfo
->contents
[i
]));
372 write_texture_p0(job
, &uniforms
, texstate
,
374 QUNIFORM_TEXTURE_CONFIG_P0_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");
389 cl_end(&job
->indirect
, uniforms
);
391 v3d_bo_unreference(&ubo
);
393 return uniform_stream
;
397 v3d_set_shader_uniform_dirty_flags(struct v3d_compiled_shader
*shader
)
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
:
405 case QUNIFORM_UNIFORM
:
406 case QUNIFORM_UBO_ADDR
:
407 dirty
|= VC5_DIRTY_CONSTBUF
;
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
;
417 case QUNIFORM_USER_CLIP_PLANE
:
418 dirty
|= VC5_DIRTY_CLIP
;
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.
437 dirty
|= VC5_DIRTY_FRAGTEX
| VC5_DIRTY_VERTTEX
;
440 case QUNIFORM_SSBO_OFFSET
:
441 case QUNIFORM_GET_BUFFER_SIZE
:
442 dirty
|= VC5_DIRTY_SSBO
;
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
;
453 case QUNIFORM_ALPHA_REF
:
454 dirty
|= VC5_DIRTY_ZSA
;
458 assert(quniform_contents_is_texture_p0(shader
->prog_data
.base
->uniforms
.contents
[i
]));
459 dirty
|= VC5_DIRTY_FRAGTEX
| VC5_DIRTY_VERTTEX
;
464 shader
->uniform_dirty_bits
= dirty
;