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 "vc5_context.h"
28 #include "compiler/v3d_compiler.h"
29 #include "broadcom/cle/v3d_packet_v33_pack.h"
33 #define SWIZ(x,y,z,w) { \
41 write_texture_border_color(struct vc5_job
*job
,
42 struct vc5_cl_out
**uniforms
,
43 struct vc5_texture_stateobj
*texstate
,
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
);
51 const struct util_format_description
*tex_format_desc
=
52 util_format_description(texture
->format
);
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
++)
60 util_format_linear_to_srgb_float(border_color
[i
]);
63 /* Turn the border color into the layout of channels that it would
64 * have when stored as texture contents.
66 float storage_color
[4];
67 util_format_unswizzle_4f(storage_color
,
69 tex_format_desc
->swizzle
);
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.
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;
79 switch (rsc
->vc5_format
) {
81 case VC5_TEXTURE_TYPE_RGBA8888
:
82 util_pack_color(storage_color
,
83 PIPE_FORMAT_R8G8B8A8_UNORM
, &uc
);
85 case VC5_TEXTURE_TYPE_RGBA4444
:
86 util_pack_color(storage_color
,
87 PIPE_FORMAT_A8B8G8R8_UNORM
, &uc
);
89 case VC5_TEXTURE_TYPE_RGB565
:
90 util_pack_color(storage_color
,
91 PIPE_FORMAT_B8G8R8A8_UNORM
, &uc
);
93 case VC5_TEXTURE_TYPE_ALPHA
:
94 uc
.ui
[0] = float_to_ubyte(storage_color
[0]) << 24;
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));
103 cl_aligned_u32(uniforms
, uc
.ui
[0]);
108 get_texrect_scale(struct vc5_texture_stateobj
*texstate
,
109 enum quniform_contents contents
,
112 struct pipe_sampler_view
*texture
= texstate
->textures
[data
];
115 if (contents
== QUNIFORM_TEXRECT_SCALE_X
)
116 dim
= texture
->texture
->width0
;
118 dim
= texture
->texture
->height0
;
120 return fui(1.0f
/ dim
);
124 get_texture_size(struct vc5_texture_stateobj
*texstate
,
125 enum quniform_contents contents
,
128 struct pipe_sampler_view
*texture
= texstate
->textures
[data
];
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;
146 unreachable("Bad texture size field");
150 static struct vc5_bo
*
151 vc5_upload_ubo(struct vc5_context
*vc5
,
152 struct vc5_compiled_shader
*shader
,
153 const uint32_t *gallium_uniforms
)
155 if (!shader
->prog_data
.base
->ubo_size
)
158 struct vc5_bo
*ubo
= vc5_bo_alloc(vc5
->screen
,
159 shader
->prog_data
.base
->ubo_size
,
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
);
173 * Writes the P0 (CFG_MODE=1) texture parameter.
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
180 write_texture_p0(struct vc5_job
*job
,
181 struct vc5_cl_out
**uniforms
,
182 struct vc5_texture_stateobj
*texstate
,
184 uint32_t shader_data
)
186 struct pipe_sampler_state
*psampler
= texstate
->samplers
[unit
];
187 struct vc5_sampler_state
*sampler
= vc5_sampler_state(psampler
);
189 cl_aligned_u32(uniforms
, shader_data
| sampler
->p0
);
193 write_texture_p1(struct vc5_job
*job
,
194 struct vc5_cl_out
**uniforms
,
195 struct vc5_texture_stateobj
*texstate
,
198 struct pipe_sampler_view
*psview
= texstate
->textures
[unit
];
199 struct vc5_sampler_view
*sview
= vc5_sampler_view(psview
);
201 struct V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1 unpacked
= {
202 .texture_state_record_base_address
= texstate
->texture_state
[unit
],
206 V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1_pack(&job
->indirect
,
210 cl_aligned_u32(uniforms
, packed
| sview
->p1
);
214 vc5_write_uniforms(struct vc5_context
*vc5
, struct vc5_compiled_shader
*shader
,
215 struct vc5_constbuf_stateobj
*cb
,
216 struct vc5_texture_stateobj
*texstate
)
218 struct v3d_uniform_list
*uinfo
= &shader
->prog_data
.base
->uniforms
;
219 struct vc5_job
*job
= vc5
->job
;
220 const uint32_t *gallium_uniforms
= cb
->cb
[0].user_buffer
;
221 struct vc5_bo
*ubo
= vc5_upload_ubo(vc5
, shader
, gallium_uniforms
);
223 /* We always need to return some space for uniforms, because the HW
224 * will be prefetching, even if we don't read any in the program.
226 vc5_cl_ensure_space(&job
->indirect
, MAX2(uinfo
->count
, 1) * 4, 4);
228 struct vc5_cl_reloc uniform_stream
= cl_get_address(&job
->indirect
);
229 vc5_bo_reference(uniform_stream
.bo
);
231 struct vc5_cl_out
*uniforms
=
232 cl_start(&job
->indirect
);
234 for (int i
= 0; i
< uinfo
->count
; i
++) {
236 switch (uinfo
->contents
[i
]) {
237 case QUNIFORM_CONSTANT
:
238 cl_aligned_u32(&uniforms
, uinfo
->data
[i
]);
240 case QUNIFORM_UNIFORM
:
241 cl_aligned_u32(&uniforms
,
242 gallium_uniforms
[uinfo
->data
[i
]]);
244 case QUNIFORM_VIEWPORT_X_SCALE
:
245 cl_aligned_f(&uniforms
, vc5
->viewport
.scale
[0] * 256.0f
);
247 case QUNIFORM_VIEWPORT_Y_SCALE
:
248 cl_aligned_f(&uniforms
, vc5
->viewport
.scale
[1] * 256.0f
);
251 case QUNIFORM_VIEWPORT_Z_OFFSET
:
252 cl_aligned_f(&uniforms
, vc5
->viewport
.translate
[2]);
254 case QUNIFORM_VIEWPORT_Z_SCALE
:
255 cl_aligned_f(&uniforms
, vc5
->viewport
.scale
[2]);
258 case QUNIFORM_USER_CLIP_PLANE
:
259 cl_aligned_f(&uniforms
,
260 vc5
->clip
.ucp
[uinfo
->data
[i
] / 4][uinfo
->data
[i
] % 4]);
263 case QUNIFORM_TEXTURE_CONFIG_P1
:
264 write_texture_p1(job
, &uniforms
, texstate
,
269 case QUNIFORM_TEXTURE_FIRST_LEVEL
:
270 write_texture_first_level(job
, &uniforms
, texstate
,
275 case QUNIFORM_TEXRECT_SCALE_X
:
276 case QUNIFORM_TEXRECT_SCALE_Y
:
277 cl_aligned_u32(&uniforms
,
278 get_texrect_scale(texstate
,
283 case QUNIFORM_TEXTURE_WIDTH
:
284 case QUNIFORM_TEXTURE_HEIGHT
:
285 case QUNIFORM_TEXTURE_DEPTH
:
286 case QUNIFORM_TEXTURE_ARRAY_SIZE
:
287 case QUNIFORM_TEXTURE_LEVELS
:
288 cl_aligned_u32(&uniforms
,
289 get_texture_size(texstate
,
294 case QUNIFORM_STENCIL
:
295 cl_aligned_u32(&uniforms
,
296 vc5
->zsa
->stencil_uniforms
[uinfo
->data
[i
]] |
297 (uinfo
->data
[i
] <= 1 ?
298 (vc5
->stencil_ref
.ref_value
[uinfo
->data
[i
]] << 8) :
302 case QUNIFORM_ALPHA_REF
:
303 cl_aligned_f(&uniforms
,
304 vc5
->zsa
->base
.alpha
.ref_value
);
307 case QUNIFORM_SAMPLE_MASK
:
308 cl_aligned_u32(&uniforms
, vc5
->sample_mask
);
311 case QUNIFORM_UBO_ADDR
:
312 if (uinfo
->data
[i
] == 0) {
313 cl_aligned_reloc(&job
->indirect
, &uniforms
,
316 int ubo_index
= uinfo
->data
[i
];
317 struct vc5_resource
*rsc
=
318 vc5_resource(cb
->cb
[ubo_index
].buffer
);
320 cl_aligned_reloc(&job
->indirect
, &uniforms
,
322 cb
->cb
[ubo_index
].buffer_offset
);
326 case QUNIFORM_TEXTURE_FIRST_LEVEL
:
327 cl_aligned_f(&uniforms
,
328 texstate
->textures
[uinfo
->data
[i
]]->u
.tex
.first_level
);
331 case QUNIFORM_TEXTURE_BORDER_COLOR
:
336 assert(quniform_contents_is_texture_p0(uinfo
->contents
[i
]));
338 write_texture_p0(job
, &uniforms
, texstate
,
340 QUNIFORM_TEXTURE_CONFIG_P0_0
,
346 uint32_t written_val
= *((uint32_t *)uniforms
- 1);
347 fprintf(stderr
, "shader %p[%d]: 0x%08x / 0x%08x (%f)\n",
348 shader
, i
, __gen_address_offset(&uniform_stream
) + i
* 4,
349 written_val
, uif(written_val
));
353 cl_end(&job
->indirect
, uniforms
);
355 vc5_bo_unreference(&ubo
);
357 return uniform_stream
;
361 vc5_set_shader_uniform_dirty_flags(struct vc5_compiled_shader
*shader
)
365 for (int i
= 0; i
< shader
->prog_data
.base
->uniforms
.count
; i
++) {
366 switch (shader
->prog_data
.base
->uniforms
.contents
[i
]) {
367 case QUNIFORM_CONSTANT
:
369 case QUNIFORM_UNIFORM
:
370 case QUNIFORM_UBO_ADDR
:
371 dirty
|= VC5_DIRTY_CONSTBUF
;
374 case QUNIFORM_VIEWPORT_X_SCALE
:
375 case QUNIFORM_VIEWPORT_Y_SCALE
:
376 case QUNIFORM_VIEWPORT_Z_OFFSET
:
377 case QUNIFORM_VIEWPORT_Z_SCALE
:
378 dirty
|= VC5_DIRTY_VIEWPORT
;
381 case QUNIFORM_USER_CLIP_PLANE
:
382 dirty
|= VC5_DIRTY_CLIP
;
385 case QUNIFORM_TEXTURE_CONFIG_P1
:
386 case QUNIFORM_TEXTURE_BORDER_COLOR
:
387 case QUNIFORM_TEXTURE_FIRST_LEVEL
:
388 case QUNIFORM_TEXRECT_SCALE_X
:
389 case QUNIFORM_TEXRECT_SCALE_Y
:
390 case QUNIFORM_TEXTURE_WIDTH
:
391 case QUNIFORM_TEXTURE_HEIGHT
:
392 case QUNIFORM_TEXTURE_DEPTH
:
393 case QUNIFORM_TEXTURE_ARRAY_SIZE
:
394 case QUNIFORM_TEXTURE_LEVELS
:
395 /* We could flag this on just the stage we're
396 * compiling for, but it's not passed in.
398 dirty
|= VC5_DIRTY_FRAGTEX
| VC5_DIRTY_VERTTEX
;
401 case QUNIFORM_STENCIL
:
402 case QUNIFORM_ALPHA_REF
:
403 dirty
|= VC5_DIRTY_ZSA
;
406 case QUNIFORM_SAMPLE_MASK
:
407 dirty
|= VC5_DIRTY_SAMPLE_MASK
;
411 assert(quniform_contents_is_texture_p0(shader
->prog_data
.base
->uniforms
.contents
[i
]));
412 dirty
|= VC5_DIRTY_FRAGTEX
| VC5_DIRTY_VERTTEX
;
417 shader
->uniform_dirty_bits
= dirty
;