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");
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
)
79 if (!shader
->prog_data
.base
->ubo_size
)
82 struct v3d_bo
*ubo
= v3d_bo_alloc(v3d
->screen
,
83 shader
->prog_data
.base
->ubo_size
,
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
);
97 * Writes the V3D 3.x P0 (CFG_MODE=1) texture parameter.
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
104 write_texture_p0(struct v3d_job
*job
,
105 struct v3d_cl_out
**uniforms
,
106 struct v3d_texture_stateobj
*texstate
,
108 uint32_t shader_data
)
110 struct pipe_sampler_state
*psampler
= texstate
->samplers
[unit
];
111 struct v3d_sampler_state
*sampler
= v3d_sampler_state(psampler
);
113 cl_aligned_u32(uniforms
, shader_data
| sampler
->p0
);
116 /** Writes the V3D 3.x P1 (CFG_MODE=1) texture parameter. */
118 write_texture_p1(struct v3d_job
*job
,
119 struct v3d_cl_out
**uniforms
,
120 struct v3d_texture_stateobj
*texstate
,
123 /* Extract the texture unit from the top bits, and the compiler's
124 * packed p1 from the bottom.
126 uint32_t unit
= data
>> 5;
127 uint32_t p1
= data
& 0x1f;
129 struct pipe_sampler_view
*psview
= texstate
->textures
[unit
];
130 struct v3d_sampler_view
*sview
= v3d_sampler_view(psview
);
132 struct V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1 unpacked
= {
133 .texture_state_record_base_address
= texstate
->texture_state
[unit
],
137 V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1_pack(&job
->indirect
,
141 cl_aligned_u32(uniforms
, p1
| packed
| sview
->p1
);
144 /** Writes the V3D 4.x TMU configuration parameter 0. */
146 write_tmu_p0(struct v3d_job
*job
,
147 struct v3d_cl_out
**uniforms
,
148 struct v3d_texture_stateobj
*texstate
,
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(sview
->texture
);
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
);
161 /** Writes the V3D 4.x TMU configuration parameter 1. */
163 write_tmu_p1(struct v3d_job
*job
,
164 struct v3d_cl_out
**uniforms
,
165 struct v3d_texture_stateobj
*texstate
,
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
);
172 cl_aligned_reloc(&job
->indirect
, uniforms
, sampler
->bo
,
173 v3d_tmu_config_data_get_value(data
));
177 v3d_write_uniforms(struct v3d_context
*v3d
, struct v3d_compiled_shader
*shader
,
178 enum pipe_shader_type stage
)
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
);
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.
190 v3d_cl_ensure_space(&job
->indirect
, MAX2(uinfo
->count
, 1) * 4, 4);
192 struct v3d_cl_reloc uniform_stream
= cl_get_address(&job
->indirect
);
193 v3d_bo_reference(uniform_stream
.bo
);
195 struct v3d_cl_out
*uniforms
=
196 cl_start(&job
->indirect
);
198 for (int i
= 0; i
< uinfo
->count
; i
++) {
199 uint32_t data
= uinfo
->data
[i
];
201 switch (uinfo
->contents
[i
]) {
202 case QUNIFORM_CONSTANT
:
203 cl_aligned_u32(&uniforms
, data
);
205 case QUNIFORM_UNIFORM
:
206 cl_aligned_u32(&uniforms
, gallium_uniforms
[data
]);
208 case QUNIFORM_VIEWPORT_X_SCALE
:
209 cl_aligned_f(&uniforms
, v3d
->viewport
.scale
[0] * 256.0f
);
211 case QUNIFORM_VIEWPORT_Y_SCALE
:
212 cl_aligned_f(&uniforms
, v3d
->viewport
.scale
[1] * 256.0f
);
215 case QUNIFORM_VIEWPORT_Z_OFFSET
:
216 cl_aligned_f(&uniforms
, v3d
->viewport
.translate
[2]);
218 case QUNIFORM_VIEWPORT_Z_SCALE
:
219 cl_aligned_f(&uniforms
, v3d
->viewport
.scale
[2]);
222 case QUNIFORM_USER_CLIP_PLANE
:
223 cl_aligned_f(&uniforms
,
224 v3d
->clip
.ucp
[data
/ 4][data
% 4]);
227 case QUNIFORM_TMU_CONFIG_P0
:
228 write_tmu_p0(job
, &uniforms
, texstate
, data
);
231 case QUNIFORM_TMU_CONFIG_P1
:
232 write_tmu_p1(job
, &uniforms
, texstate
, data
);
235 case QUNIFORM_TEXTURE_CONFIG_P1
:
236 write_texture_p1(job
, &uniforms
, texstate
,
240 case QUNIFORM_TEXRECT_SCALE_X
:
241 case QUNIFORM_TEXRECT_SCALE_Y
:
242 cl_aligned_u32(&uniforms
,
243 get_texrect_scale(texstate
,
248 case QUNIFORM_TEXTURE_WIDTH
:
249 case QUNIFORM_TEXTURE_HEIGHT
:
250 case QUNIFORM_TEXTURE_DEPTH
:
251 case QUNIFORM_TEXTURE_ARRAY_SIZE
:
252 case QUNIFORM_TEXTURE_LEVELS
:
253 cl_aligned_u32(&uniforms
,
254 get_texture_size(texstate
,
259 case QUNIFORM_ALPHA_REF
:
260 cl_aligned_f(&uniforms
,
261 v3d
->zsa
->base
.alpha
.ref_value
);
264 case QUNIFORM_UBO_ADDR
:
266 cl_aligned_reloc(&job
->indirect
, &uniforms
,
269 int ubo_index
= data
;
270 struct v3d_resource
*rsc
=
271 v3d_resource(cb
->cb
[ubo_index
].buffer
);
273 cl_aligned_reloc(&job
->indirect
, &uniforms
,
275 cb
->cb
[ubo_index
].buffer_offset
);
279 case QUNIFORM_TEXTURE_FIRST_LEVEL
:
280 cl_aligned_f(&uniforms
,
281 texstate
->textures
[data
]->u
.tex
.first_level
);
284 case QUNIFORM_SPILL_OFFSET
:
285 cl_aligned_reloc(&job
->indirect
, &uniforms
,
286 v3d
->prog
.spill_bo
, 0);
289 case QUNIFORM_SPILL_SIZE_PER_THREAD
:
290 cl_aligned_u32(&uniforms
,
291 v3d
->prog
.spill_size_per_thread
);
295 assert(quniform_contents_is_texture_p0(uinfo
->contents
[i
]));
297 write_texture_p0(job
, &uniforms
, texstate
,
299 QUNIFORM_TEXTURE_CONFIG_P0_0
,
305 uint32_t written_val
= *((uint32_t *)uniforms
- 1);
306 fprintf(stderr
, "shader %p[%d]: 0x%08x / 0x%08x (%f) ",
307 shader
, i
, __gen_address_offset(&uniform_stream
) + i
* 4,
308 written_val
, uif(written_val
));
309 vir_dump_uniform(uinfo
->contents
[i
], data
);
310 fprintf(stderr
, "\n");
314 cl_end(&job
->indirect
, uniforms
);
316 v3d_bo_unreference(&ubo
);
318 return uniform_stream
;
322 v3d_set_shader_uniform_dirty_flags(struct v3d_compiled_shader
*shader
)
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
:
330 case QUNIFORM_UNIFORM
:
331 case QUNIFORM_UBO_ADDR
:
332 dirty
|= VC5_DIRTY_CONSTBUF
;
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
;
342 case QUNIFORM_USER_CLIP_PLANE
:
343 dirty
|= VC5_DIRTY_CLIP
;
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.
362 dirty
|= VC5_DIRTY_FRAGTEX
| VC5_DIRTY_VERTTEX
;
365 case QUNIFORM_ALPHA_REF
:
366 dirty
|= VC5_DIRTY_ZSA
;
370 assert(quniform_contents_is_texture_p0(shader
->prog_data
.base
->uniforms
.contents
[i
]));
371 dirty
|= VC5_DIRTY_FRAGTEX
| VC5_DIRTY_VERTTEX
;
376 shader
->uniform_dirty_bits
= dirty
;