vc4: Drop VC4_DIRTY_TEXSTATE in favor of the per-stage flags.
[mesa.git] / src / gallium / drivers / vc4 / vc4_uniforms.c
1 /*
2 * Copyright © 2014-2015 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 "vc4_context.h"
28 #include "vc4_qir.h"
29
30 static void
31 write_texture_p0(struct vc4_context *vc4,
32 struct vc4_cl_out **uniforms,
33 struct vc4_texture_stateobj *texstate,
34 uint32_t unit)
35 {
36 struct vc4_sampler_view *sview =
37 vc4_sampler_view(texstate->textures[unit]);
38 struct vc4_resource *rsc = vc4_resource(sview->base.texture);
39
40 cl_reloc(vc4, &vc4->uniforms, uniforms, rsc->bo, sview->texture_p0);
41 }
42
43 static void
44 write_texture_p1(struct vc4_context *vc4,
45 struct vc4_cl_out **uniforms,
46 struct vc4_texture_stateobj *texstate,
47 uint32_t unit)
48 {
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]);
53
54 cl_aligned_u32(uniforms, sview->texture_p1 | sampler->texture_p1);
55 }
56
57 static void
58 write_texture_p2(struct vc4_context *vc4,
59 struct vc4_cl_out **uniforms,
60 struct vc4_texture_stateobj *texstate,
61 uint32_t data)
62 {
63 uint32_t unit = data & 0xffff;
64 struct pipe_sampler_view *texture = texstate->textures[unit];
65 struct vc4_resource *rsc = vc4_resource(texture->texture);
66
67 cl_aligned_u32(uniforms,
68 VC4_SET_FIELD(VC4_TEX_P2_PTYPE_CUBE_MAP_STRIDE,
69 VC4_TEX_P2_PTYPE) |
70 VC4_SET_FIELD(rsc->cube_map_stride >> 12, VC4_TEX_P2_CMST) |
71 VC4_SET_FIELD((data >> 16) & 1, VC4_TEX_P2_BSLOD));
72 }
73
74 static void
75 write_texture_msaa_addr(struct vc4_context *vc4,
76 struct vc4_cl_out **uniforms,
77 struct vc4_texture_stateobj *texstate,
78 uint32_t unit)
79 {
80 struct pipe_sampler_view *texture = texstate->textures[unit];
81 struct vc4_resource *rsc = vc4_resource(texture->texture);
82
83 cl_aligned_reloc(vc4, &vc4->uniforms, uniforms, rsc->bo, 0);
84 }
85
86
87 #define SWIZ(x,y,z,w) { \
88 PIPE_SWIZZLE_##x, \
89 PIPE_SWIZZLE_##y, \
90 PIPE_SWIZZLE_##z, \
91 PIPE_SWIZZLE_##w \
92 }
93
94 static void
95 write_texture_border_color(struct vc4_context *vc4,
96 struct vc4_cl_out **uniforms,
97 struct vc4_texture_stateobj *texstate,
98 uint32_t unit)
99 {
100 struct pipe_sampler_state *sampler = texstate->samplers[unit];
101 struct pipe_sampler_view *texture = texstate->textures[unit];
102 struct vc4_resource *rsc = vc4_resource(texture->texture);
103 union util_color uc;
104
105 const struct util_format_description *tex_format_desc =
106 util_format_description(texture->format);
107
108 float border_color[4];
109 for (int i = 0; i < 4; i++)
110 border_color[i] = sampler->border_color.f[i];
111 if (util_format_is_srgb(texture->format)) {
112 for (int i = 0; i < 3; i++)
113 border_color[i] =
114 util_format_linear_to_srgb_float(border_color[i]);
115 }
116
117 /* Turn the border color into the layout of channels that it would
118 * have when stored as texture contents.
119 */
120 float storage_color[4];
121 util_format_unswizzle_4f(storage_color,
122 border_color,
123 tex_format_desc->swizzle);
124
125 /* Now, pack so that when the vc4_format-sampled texture contents are
126 * replaced with our border color, the vc4_get_format_swizzle()
127 * swizzling will get the right channels.
128 */
129 if (util_format_is_depth_or_stencil(texture->format)) {
130 uc.ui[0] = util_pack_z(PIPE_FORMAT_Z24X8_UNORM,
131 sampler->border_color.f[0]) << 8;
132 } else {
133 switch (rsc->vc4_format) {
134 default:
135 case VC4_TEXTURE_TYPE_RGBA8888:
136 util_pack_color(storage_color,
137 PIPE_FORMAT_R8G8B8A8_UNORM, &uc);
138 break;
139 case VC4_TEXTURE_TYPE_RGBA4444:
140 util_pack_color(storage_color,
141 PIPE_FORMAT_A8B8G8R8_UNORM, &uc);
142 break;
143 case VC4_TEXTURE_TYPE_RGB565:
144 util_pack_color(storage_color,
145 PIPE_FORMAT_B8G8R8A8_UNORM, &uc);
146 break;
147 case VC4_TEXTURE_TYPE_ALPHA:
148 uc.ui[0] = float_to_ubyte(storage_color[0]) << 24;
149 break;
150 case VC4_TEXTURE_TYPE_LUMALPHA:
151 uc.ui[0] = ((float_to_ubyte(storage_color[1]) << 24) |
152 (float_to_ubyte(storage_color[0]) << 0));
153 break;
154 }
155 }
156
157 cl_aligned_u32(uniforms, uc.ui[0]);
158 }
159
160 static uint32_t
161 get_texrect_scale(struct vc4_texture_stateobj *texstate,
162 enum quniform_contents contents,
163 uint32_t data)
164 {
165 struct pipe_sampler_view *texture = texstate->textures[data];
166 uint32_t dim;
167
168 if (contents == QUNIFORM_TEXRECT_SCALE_X)
169 dim = texture->texture->width0;
170 else
171 dim = texture->texture->height0;
172
173 return fui(1.0f / dim);
174 }
175
176 static struct vc4_bo *
177 vc4_upload_ubo(struct vc4_context *vc4,
178 struct vc4_compiled_shader *shader,
179 const uint32_t *gallium_uniforms)
180 {
181 if (!shader->ubo_size)
182 return NULL;
183
184 struct vc4_bo *ubo = vc4_bo_alloc(vc4->screen, shader->ubo_size, "ubo");
185 uint32_t *data = vc4_bo_map(ubo);
186 for (uint32_t i = 0; i < shader->num_ubo_ranges; i++) {
187 memcpy(data + shader->ubo_ranges[i].dst_offset,
188 gallium_uniforms + shader->ubo_ranges[i].src_offset,
189 shader->ubo_ranges[i].size);
190 }
191
192 return ubo;
193 }
194
195 void
196 vc4_write_uniforms(struct vc4_context *vc4, struct vc4_compiled_shader *shader,
197 struct vc4_constbuf_stateobj *cb,
198 struct vc4_texture_stateobj *texstate)
199 {
200 struct vc4_shader_uniform_info *uinfo = &shader->uniforms;
201 const uint32_t *gallium_uniforms = cb->cb[0].user_buffer;
202 struct vc4_bo *ubo = vc4_upload_ubo(vc4, shader, gallium_uniforms);
203
204 cl_ensure_space(&vc4->uniforms, (uinfo->count +
205 uinfo->num_texture_samples) * 4);
206
207 struct vc4_cl_out *uniforms =
208 cl_start_shader_reloc(&vc4->uniforms,
209 uinfo->num_texture_samples);
210
211 for (int i = 0; i < uinfo->count; i++) {
212
213 switch (uinfo->contents[i]) {
214 case QUNIFORM_CONSTANT:
215 cl_aligned_u32(&uniforms, uinfo->data[i]);
216 break;
217 case QUNIFORM_UNIFORM:
218 cl_aligned_u32(&uniforms,
219 gallium_uniforms[uinfo->data[i]]);
220 break;
221 case QUNIFORM_VIEWPORT_X_SCALE:
222 cl_aligned_f(&uniforms, vc4->viewport.scale[0] * 16.0f);
223 break;
224 case QUNIFORM_VIEWPORT_Y_SCALE:
225 cl_aligned_f(&uniforms, vc4->viewport.scale[1] * 16.0f);
226 break;
227
228 case QUNIFORM_VIEWPORT_Z_OFFSET:
229 cl_aligned_f(&uniforms, vc4->viewport.translate[2]);
230 break;
231 case QUNIFORM_VIEWPORT_Z_SCALE:
232 cl_aligned_f(&uniforms, vc4->viewport.scale[2]);
233 break;
234
235 case QUNIFORM_USER_CLIP_PLANE:
236 cl_aligned_f(&uniforms,
237 vc4->clip.ucp[uinfo->data[i] / 4][uinfo->data[i] % 4]);
238 break;
239
240 case QUNIFORM_TEXTURE_CONFIG_P0:
241 write_texture_p0(vc4, &uniforms, texstate,
242 uinfo->data[i]);
243 break;
244
245 case QUNIFORM_TEXTURE_CONFIG_P1:
246 write_texture_p1(vc4, &uniforms, texstate,
247 uinfo->data[i]);
248 break;
249
250 case QUNIFORM_TEXTURE_CONFIG_P2:
251 write_texture_p2(vc4, &uniforms, texstate,
252 uinfo->data[i]);
253 break;
254
255 case QUNIFORM_UBO_ADDR:
256 cl_aligned_reloc(vc4, &vc4->uniforms, &uniforms, ubo, 0);
257 break;
258
259 case QUNIFORM_TEXTURE_MSAA_ADDR:
260 write_texture_msaa_addr(vc4, &uniforms,
261 texstate, uinfo->data[i]);
262 break;
263
264 case QUNIFORM_TEXTURE_BORDER_COLOR:
265 write_texture_border_color(vc4, &uniforms,
266 texstate, uinfo->data[i]);
267 break;
268
269 case QUNIFORM_TEXRECT_SCALE_X:
270 case QUNIFORM_TEXRECT_SCALE_Y:
271 cl_aligned_u32(&uniforms,
272 get_texrect_scale(texstate,
273 uinfo->contents[i],
274 uinfo->data[i]));
275 break;
276
277 case QUNIFORM_BLEND_CONST_COLOR_X:
278 case QUNIFORM_BLEND_CONST_COLOR_Y:
279 case QUNIFORM_BLEND_CONST_COLOR_Z:
280 case QUNIFORM_BLEND_CONST_COLOR_W:
281 cl_aligned_f(&uniforms,
282 CLAMP(vc4->blend_color.f.color[uinfo->contents[i] -
283 QUNIFORM_BLEND_CONST_COLOR_X],
284 0, 1));
285 break;
286
287 case QUNIFORM_BLEND_CONST_COLOR_RGBA: {
288 const uint8_t *format_swiz =
289 vc4_get_format_swizzle(vc4->framebuffer.cbufs[0]->format);
290 uint32_t color = 0;
291 for (int i = 0; i < 4; i++) {
292 if (format_swiz[i] >= 4)
293 continue;
294
295 color |= (vc4->blend_color.ub[format_swiz[i]] <<
296 (i * 8));
297 }
298 cl_aligned_u32(&uniforms, color);
299 break;
300 }
301
302 case QUNIFORM_BLEND_CONST_COLOR_AAAA: {
303 uint8_t a = vc4->blend_color.ub[3];
304 cl_aligned_u32(&uniforms, ((a) |
305 (a << 8) |
306 (a << 16) |
307 (a << 24)));
308 break;
309 }
310
311 case QUNIFORM_STENCIL:
312 cl_aligned_u32(&uniforms,
313 vc4->zsa->stencil_uniforms[uinfo->data[i]] |
314 (uinfo->data[i] <= 1 ?
315 (vc4->stencil_ref.ref_value[uinfo->data[i]] << 8) :
316 0));
317 break;
318
319 case QUNIFORM_ALPHA_REF:
320 cl_aligned_f(&uniforms,
321 vc4->zsa->base.alpha.ref_value);
322 break;
323
324 case QUNIFORM_SAMPLE_MASK:
325 cl_aligned_u32(&uniforms, vc4->sample_mask);
326 break;
327
328 case QUNIFORM_UNIFORMS_ADDRESS:
329 /* This will be filled in by the kernel. */
330 cl_aligned_u32(&uniforms, 0xd0d0d0d0);
331 break;
332 }
333 #if 0
334 uint32_t written_val = *((uint32_t *)uniforms - 1);
335 fprintf(stderr, "%p: %d / 0x%08x (%f)\n",
336 shader, i, written_val, uif(written_val));
337 #endif
338 }
339
340 cl_end(&vc4->uniforms, uniforms);
341
342 vc4_bo_unreference(&ubo);
343 }
344
345 void
346 vc4_set_shader_uniform_dirty_flags(struct vc4_compiled_shader *shader)
347 {
348 uint32_t dirty = 0;
349
350 for (int i = 0; i < shader->uniforms.count; i++) {
351 switch (shader->uniforms.contents[i]) {
352 case QUNIFORM_CONSTANT:
353 case QUNIFORM_UNIFORMS_ADDRESS:
354 break;
355 case QUNIFORM_UNIFORM:
356 case QUNIFORM_UBO_ADDR:
357 dirty |= VC4_DIRTY_CONSTBUF;
358 break;
359
360 case QUNIFORM_VIEWPORT_X_SCALE:
361 case QUNIFORM_VIEWPORT_Y_SCALE:
362 case QUNIFORM_VIEWPORT_Z_OFFSET:
363 case QUNIFORM_VIEWPORT_Z_SCALE:
364 dirty |= VC4_DIRTY_VIEWPORT;
365 break;
366
367 case QUNIFORM_USER_CLIP_PLANE:
368 dirty |= VC4_DIRTY_CLIP;
369 break;
370
371 case QUNIFORM_TEXTURE_CONFIG_P0:
372 case QUNIFORM_TEXTURE_CONFIG_P1:
373 case QUNIFORM_TEXTURE_CONFIG_P2:
374 case QUNIFORM_TEXTURE_BORDER_COLOR:
375 case QUNIFORM_TEXTURE_MSAA_ADDR:
376 case QUNIFORM_TEXRECT_SCALE_X:
377 case QUNIFORM_TEXRECT_SCALE_Y:
378 /* We could flag this on just the stage we're
379 * compiling for, but it's not passed in.
380 */
381 dirty |= VC4_DIRTY_FRAGTEX | VC4_DIRTY_VERTTEX;
382 break;
383
384 case QUNIFORM_BLEND_CONST_COLOR_X:
385 case QUNIFORM_BLEND_CONST_COLOR_Y:
386 case QUNIFORM_BLEND_CONST_COLOR_Z:
387 case QUNIFORM_BLEND_CONST_COLOR_W:
388 case QUNIFORM_BLEND_CONST_COLOR_RGBA:
389 case QUNIFORM_BLEND_CONST_COLOR_AAAA:
390 dirty |= VC4_DIRTY_BLEND_COLOR;
391 break;
392
393 case QUNIFORM_STENCIL:
394 case QUNIFORM_ALPHA_REF:
395 dirty |= VC4_DIRTY_ZSA;
396 break;
397
398 case QUNIFORM_SAMPLE_MASK:
399 dirty |= VC4_DIRTY_SAMPLE_MASK;
400 break;
401 }
402 }
403
404 shader->uniform_dirty_bits = dirty;
405 }