2 * Copyright © 2014-2015 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 "vc4_context.h"
31 write_texture_p0(struct vc4_context
*vc4
,
32 struct vc4_cl_out
**uniforms
,
33 struct vc4_texture_stateobj
*texstate
,
36 struct vc4_sampler_view
*sview
=
37 vc4_sampler_view(texstate
->textures
[unit
]);
38 struct vc4_resource
*rsc
= vc4_resource(sview
->base
.texture
);
40 cl_reloc(vc4
, &vc4
->uniforms
, uniforms
, rsc
->bo
, sview
->texture_p0
);
44 write_texture_p1(struct vc4_context
*vc4
,
45 struct vc4_cl_out
**uniforms
,
46 struct vc4_texture_stateobj
*texstate
,
49 struct vc4_sampler_view
*sview
=
50 vc4_sampler_view(texstate
->textures
[unit
]);
51 struct vc4_sampler_state
*sampler
=
52 vc4_sampler_state(texstate
->samplers
[unit
]);
54 cl_aligned_u32(uniforms
, sview
->texture_p1
| sampler
->texture_p1
);
58 write_texture_p2(struct vc4_context
*vc4
,
59 struct vc4_cl_out
**uniforms
,
60 struct vc4_texture_stateobj
*texstate
,
63 uint32_t unit
= data
& 0xffff;
64 struct pipe_sampler_view
*texture
= texstate
->textures
[unit
];
65 struct vc4_resource
*rsc
= vc4_resource(texture
->texture
);
67 cl_aligned_u32(uniforms
,
68 VC4_SET_FIELD(VC4_TEX_P2_PTYPE_CUBE_MAP_STRIDE
,
70 VC4_SET_FIELD(rsc
->cube_map_stride
>> 12, VC4_TEX_P2_CMST
) |
71 VC4_SET_FIELD((data
>> 16) & 1, VC4_TEX_P2_BSLOD
));
75 #define SWIZ(x,y,z,w) { \
76 UTIL_FORMAT_SWIZZLE_##x, \
77 UTIL_FORMAT_SWIZZLE_##y, \
78 UTIL_FORMAT_SWIZZLE_##z, \
79 UTIL_FORMAT_SWIZZLE_##w \
83 write_texture_border_color(struct vc4_context
*vc4
,
84 struct vc4_cl_out
**uniforms
,
85 struct vc4_texture_stateobj
*texstate
,
88 struct pipe_sampler_state
*sampler
= texstate
->samplers
[unit
];
89 struct pipe_sampler_view
*texture
= texstate
->textures
[unit
];
90 struct vc4_resource
*rsc
= vc4_resource(texture
->texture
);
93 const struct util_format_description
*tex_format_desc
=
94 util_format_description(texture
->format
);
96 float border_color
[4];
97 for (int i
= 0; i
< 4; i
++)
98 border_color
[i
] = sampler
->border_color
.f
[i
];
99 if (util_format_is_srgb(texture
->format
)) {
100 for (int i
= 0; i
< 3; i
++)
102 util_format_linear_to_srgb_float(border_color
[i
]);
105 /* Turn the border color into the layout of channels that it would
106 * have when stored as texture contents.
108 float storage_color
[4];
109 util_format_unswizzle_4f(storage_color
,
111 tex_format_desc
->swizzle
);
113 /* Now, pack so that when the vc4_format-sampled texture contents are
114 * replaced with our border color, the vc4_get_format_swizzle()
115 * swizzling will get the right channels.
117 if (util_format_is_depth_or_stencil(texture
->format
)) {
118 uc
.ui
[0] = util_pack_z(PIPE_FORMAT_Z24X8_UNORM
,
119 sampler
->border_color
.f
[0]) << 8;
121 switch (rsc
->vc4_format
) {
123 case VC4_TEXTURE_TYPE_RGBA8888
:
124 util_pack_color(storage_color
,
125 PIPE_FORMAT_R8G8B8A8_UNORM
, &uc
);
127 case VC4_TEXTURE_TYPE_RGBA4444
:
128 util_pack_color(storage_color
,
129 PIPE_FORMAT_A8B8G8R8_UNORM
, &uc
);
131 case VC4_TEXTURE_TYPE_RGB565
:
132 util_pack_color(storage_color
,
133 PIPE_FORMAT_B8G8R8A8_UNORM
, &uc
);
135 case VC4_TEXTURE_TYPE_ALPHA
:
136 uc
.ui
[0] = float_to_ubyte(storage_color
[0]) << 24;
138 case VC4_TEXTURE_TYPE_LUMALPHA
:
139 uc
.ui
[0] = ((float_to_ubyte(storage_color
[1]) << 24) |
140 (float_to_ubyte(storage_color
[0]) << 0));
145 cl_aligned_u32(uniforms
, uc
.ui
[0]);
149 get_texrect_scale(struct vc4_texture_stateobj
*texstate
,
150 enum quniform_contents contents
,
153 struct pipe_sampler_view
*texture
= texstate
->textures
[data
];
156 if (contents
== QUNIFORM_TEXRECT_SCALE_X
)
157 dim
= texture
->texture
->width0
;
159 dim
= texture
->texture
->height0
;
161 return fui(1.0f
/ dim
);
164 static struct vc4_bo
*
165 vc4_upload_ubo(struct vc4_context
*vc4
,
166 struct vc4_compiled_shader
*shader
,
167 const uint32_t *gallium_uniforms
)
169 if (!shader
->ubo_size
)
172 struct vc4_bo
*ubo
= vc4_bo_alloc(vc4
->screen
, shader
->ubo_size
, "ubo");
173 uint32_t *data
= vc4_bo_map(ubo
);
174 for (uint32_t i
= 0; i
< shader
->num_ubo_ranges
; i
++) {
175 memcpy(data
+ shader
->ubo_ranges
[i
].dst_offset
,
176 gallium_uniforms
+ shader
->ubo_ranges
[i
].src_offset
,
177 shader
->ubo_ranges
[i
].size
);
184 vc4_write_uniforms(struct vc4_context
*vc4
, struct vc4_compiled_shader
*shader
,
185 struct vc4_constbuf_stateobj
*cb
,
186 struct vc4_texture_stateobj
*texstate
)
188 struct vc4_shader_uniform_info
*uinfo
= &shader
->uniforms
;
189 const uint32_t *gallium_uniforms
= cb
->cb
[0].user_buffer
;
190 struct vc4_bo
*ubo
= vc4_upload_ubo(vc4
, shader
, gallium_uniforms
);
192 cl_ensure_space(&vc4
->uniforms
, (uinfo
->count
+
193 uinfo
->num_texture_samples
) * 4);
195 struct vc4_cl_out
*uniforms
=
196 cl_start_shader_reloc(&vc4
->uniforms
,
197 uinfo
->num_texture_samples
);
199 for (int i
= 0; i
< uinfo
->count
; i
++) {
201 switch (uinfo
->contents
[i
]) {
202 case QUNIFORM_CONSTANT
:
203 cl_aligned_u32(&uniforms
, uinfo
->data
[i
]);
205 case QUNIFORM_UNIFORM
:
206 cl_aligned_u32(&uniforms
,
207 gallium_uniforms
[uinfo
->data
[i
]]);
209 case QUNIFORM_VIEWPORT_X_SCALE
:
210 cl_aligned_f(&uniforms
, vc4
->viewport
.scale
[0] * 16.0f
);
212 case QUNIFORM_VIEWPORT_Y_SCALE
:
213 cl_aligned_f(&uniforms
, vc4
->viewport
.scale
[1] * 16.0f
);
216 case QUNIFORM_VIEWPORT_Z_OFFSET
:
217 cl_aligned_f(&uniforms
, vc4
->viewport
.translate
[2]);
219 case QUNIFORM_VIEWPORT_Z_SCALE
:
220 cl_aligned_f(&uniforms
, vc4
->viewport
.scale
[2]);
223 case QUNIFORM_USER_CLIP_PLANE
:
224 cl_aligned_f(&uniforms
,
225 vc4
->clip
.ucp
[uinfo
->data
[i
] / 4][uinfo
->data
[i
] % 4]);
228 case QUNIFORM_TEXTURE_CONFIG_P0
:
229 write_texture_p0(vc4
, &uniforms
, texstate
,
233 case QUNIFORM_TEXTURE_CONFIG_P1
:
234 write_texture_p1(vc4
, &uniforms
, texstate
,
238 case QUNIFORM_TEXTURE_CONFIG_P2
:
239 write_texture_p2(vc4
, &uniforms
, texstate
,
243 case QUNIFORM_UBO_ADDR
:
244 cl_aligned_reloc(vc4
, &vc4
->uniforms
, &uniforms
, ubo
, 0);
247 case QUNIFORM_TEXTURE_BORDER_COLOR
:
248 write_texture_border_color(vc4
, &uniforms
,
249 texstate
, uinfo
->data
[i
]);
252 case QUNIFORM_TEXRECT_SCALE_X
:
253 case QUNIFORM_TEXRECT_SCALE_Y
:
254 cl_aligned_u32(&uniforms
,
255 get_texrect_scale(texstate
,
260 case QUNIFORM_BLEND_CONST_COLOR
:
261 cl_aligned_f(&uniforms
,
262 CLAMP(vc4
->blend_color
.color
[uinfo
->data
[i
]], 0, 1));
265 case QUNIFORM_STENCIL
:
266 cl_aligned_u32(&uniforms
,
267 vc4
->zsa
->stencil_uniforms
[uinfo
->data
[i
]] |
268 (uinfo
->data
[i
] <= 1 ?
269 (vc4
->stencil_ref
.ref_value
[uinfo
->data
[i
]] << 8) :
273 case QUNIFORM_ALPHA_REF
:
274 cl_aligned_f(&uniforms
,
275 vc4
->zsa
->base
.alpha
.ref_value
);
279 uint32_t written_val
= *((uint32_t *)uniforms
- 1);
280 fprintf(stderr
, "%p: %d / 0x%08x (%f)\n",
281 shader
, i
, written_val
, uif(written_val
));
285 cl_end(&vc4
->uniforms
, uniforms
);
287 vc4_bo_unreference(&ubo
);
291 vc4_set_shader_uniform_dirty_flags(struct vc4_compiled_shader
*shader
)
295 for (int i
= 0; i
< shader
->uniforms
.count
; i
++) {
296 switch (shader
->uniforms
.contents
[i
]) {
297 case QUNIFORM_CONSTANT
:
299 case QUNIFORM_UNIFORM
:
300 case QUNIFORM_UBO_ADDR
:
301 dirty
|= VC4_DIRTY_CONSTBUF
;
304 case QUNIFORM_VIEWPORT_X_SCALE
:
305 case QUNIFORM_VIEWPORT_Y_SCALE
:
306 case QUNIFORM_VIEWPORT_Z_OFFSET
:
307 case QUNIFORM_VIEWPORT_Z_SCALE
:
308 dirty
|= VC4_DIRTY_VIEWPORT
;
311 case QUNIFORM_USER_CLIP_PLANE
:
312 dirty
|= VC4_DIRTY_CLIP
;
315 case QUNIFORM_TEXTURE_CONFIG_P0
:
316 case QUNIFORM_TEXTURE_CONFIG_P1
:
317 case QUNIFORM_TEXTURE_CONFIG_P2
:
318 case QUNIFORM_TEXTURE_BORDER_COLOR
:
319 case QUNIFORM_TEXRECT_SCALE_X
:
320 case QUNIFORM_TEXRECT_SCALE_Y
:
321 dirty
|= VC4_DIRTY_TEXSTATE
;
324 case QUNIFORM_BLEND_CONST_COLOR
:
325 dirty
|= VC4_DIRTY_BLEND_COLOR
;
328 case QUNIFORM_STENCIL
:
329 case QUNIFORM_ALPHA_REF
:
330 dirty
|= VC4_DIRTY_ZSA
;
335 shader
->uniform_dirty_bits
= dirty
;