summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/vc4/vc4_uniforms.c
blob: 4715a7fffd5bad742e017e18b569f44b47fb00ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 * Copyright © 2014-2015 Broadcom
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "util/u_pack_color.h"
#include "util/format_srgb.h"

#include "vc4_context.h"
#include "vc4_qir.h"

static void
write_texture_p0(struct vc4_context *vc4,
                 struct vc4_cl_out **uniforms,
                 struct vc4_texture_stateobj *texstate,
                 uint32_t unit)
{
        struct vc4_sampler_view *sview =
                vc4_sampler_view(texstate->textures[unit]);
        struct vc4_resource *rsc = vc4_resource(sview->base.texture);

        cl_reloc(vc4, &vc4->uniforms, uniforms, rsc->bo, sview->texture_p0);
}

static void
write_texture_p1(struct vc4_context *vc4,
                 struct vc4_cl_out **uniforms,
                 struct vc4_texture_stateobj *texstate,
                 uint32_t unit)
{
        struct vc4_sampler_view *sview =
                vc4_sampler_view(texstate->textures[unit]);
        struct vc4_sampler_state *sampler =
                vc4_sampler_state(texstate->samplers[unit]);

        cl_aligned_u32(uniforms, sview->texture_p1 | sampler->texture_p1);
}

static void
write_texture_p2(struct vc4_context *vc4,
                 struct vc4_cl_out **uniforms,
                 struct vc4_texture_stateobj *texstate,
                 uint32_t data)
{
        uint32_t unit = data & 0xffff;
        struct pipe_sampler_view *texture = texstate->textures[unit];
        struct vc4_resource *rsc = vc4_resource(texture->texture);

        cl_aligned_u32(uniforms,
               VC4_SET_FIELD(VC4_TEX_P2_PTYPE_CUBE_MAP_STRIDE,
                             VC4_TEX_P2_PTYPE) |
               VC4_SET_FIELD(rsc->cube_map_stride >> 12, VC4_TEX_P2_CMST) |
               VC4_SET_FIELD((data >> 16) & 1, VC4_TEX_P2_BSLOD));
}

static void
write_texture_msaa_addr(struct vc4_context *vc4,
                 struct vc4_cl_out **uniforms,
                        struct vc4_texture_stateobj *texstate,
                        uint32_t unit)
{
        struct pipe_sampler_view *texture = texstate->textures[unit];
        struct vc4_resource *rsc = vc4_resource(texture->texture);

        cl_aligned_reloc(vc4, &vc4->uniforms, uniforms, rsc->bo, 0);
}


#define SWIZ(x,y,z,w) {          \
        PIPE_SWIZZLE_##x, \
        PIPE_SWIZZLE_##y, \
        PIPE_SWIZZLE_##z, \
        PIPE_SWIZZLE_##w  \
}

static void
write_texture_border_color(struct vc4_context *vc4,
                           struct vc4_cl_out **uniforms,
                           struct vc4_texture_stateobj *texstate,
                           uint32_t unit)
{
        struct pipe_sampler_state *sampler = texstate->samplers[unit];
        struct pipe_sampler_view *texture = texstate->textures[unit];
        struct vc4_resource *rsc = vc4_resource(texture->texture);
        union util_color uc;

        const struct util_format_description *tex_format_desc =
                util_format_description(texture->format);

        float border_color[4];
        for (int i = 0; i < 4; i++)
                border_color[i] = sampler->border_color.f[i];
        if (util_format_is_srgb(texture->format)) {
                for (int i = 0; i < 3; i++)
                        border_color[i] =
                                util_format_linear_to_srgb_float(border_color[i]);
        }

        /* Turn the border color into the layout of channels that it would
         * have when stored as texture contents.
         */
        float storage_color[4];
        util_format_unswizzle_4f(storage_color,
                                 border_color,
                                 tex_format_desc->swizzle);

        /* Now, pack so that when the vc4_format-sampled texture contents are
         * replaced with our border color, the vc4_get_format_swizzle()
         * swizzling will get the right channels.
         */
        if (util_format_is_depth_or_stencil(texture->format)) {
                uc.ui[0] = util_pack_z(PIPE_FORMAT_Z24X8_UNORM,
                                       sampler->border_color.f[0]) << 8;
        } else {
                switch (rsc->vc4_format) {
                default:
                case VC4_TEXTURE_TYPE_RGBA8888:
                        util_pack_color(storage_color,
                                        PIPE_FORMAT_R8G8B8A8_UNORM, &uc);
                        break;
                case VC4_TEXTURE_TYPE_RGBA4444:
                        util_pack_color(storage_color,
                                        PIPE_FORMAT_A8B8G8R8_UNORM, &uc);
                        break;
                case VC4_TEXTURE_TYPE_RGB565:
                        util_pack_color(storage_color,
                                        PIPE_FORMAT_B8G8R8A8_UNORM, &uc);
                        break;
                case VC4_TEXTURE_TYPE_ALPHA:
                        uc.ui[0] = float_to_ubyte(storage_color[0]) << 24;
                        break;
                case VC4_TEXTURE_TYPE_LUMALPHA:
                        uc.ui[0] = ((float_to_ubyte(storage_color[1]) << 24) |
                                    (float_to_ubyte(storage_color[0]) << 0));
                        break;
                }
        }

        cl_aligned_u32(uniforms, uc.ui[0]);
}

static uint32_t
get_texrect_scale(struct vc4_texture_stateobj *texstate,
                  enum quniform_contents contents,
                  uint32_t data)
{
        struct pipe_sampler_view *texture = texstate->textures[data];
        uint32_t dim;

        if (contents == QUNIFORM_TEXRECT_SCALE_X)
                dim = texture->texture->width0;
        else
                dim = texture->texture->height0;

        return fui(1.0f / dim);
}

static struct vc4_bo *
vc4_upload_ubo(struct vc4_context *vc4,
               struct vc4_compiled_shader *shader,
               const uint32_t *gallium_uniforms)
{
        if (!shader->ubo_size)
                return NULL;

        struct vc4_bo *ubo = vc4_bo_alloc(vc4->screen, shader->ubo_size, "ubo");
        uint32_t *data = vc4_bo_map(ubo);
        for (uint32_t i = 0; i < shader->num_ubo_ranges; i++) {
                memcpy(data + shader->ubo_ranges[i].dst_offset,
                       gallium_uniforms + shader->ubo_ranges[i].src_offset,
                       shader->ubo_ranges[i].size);
        }

        return ubo;
}

void
vc4_write_uniforms(struct vc4_context *vc4, struct vc4_compiled_shader *shader,
                   struct vc4_constbuf_stateobj *cb,
                   struct vc4_texture_stateobj *texstate)
{
        struct vc4_shader_uniform_info *uinfo = &shader->uniforms;
        const uint32_t *gallium_uniforms = cb->cb[0].user_buffer;
        struct vc4_bo *ubo = vc4_upload_ubo(vc4, shader, gallium_uniforms);

        cl_ensure_space(&vc4->uniforms, (uinfo->count +
                                         uinfo->num_texture_samples) * 4);

        struct vc4_cl_out *uniforms =
                cl_start_shader_reloc(&vc4->uniforms,
                                      uinfo->num_texture_samples);

        for (int i = 0; i < uinfo->count; i++) {

                switch (uinfo->contents[i]) {
                case QUNIFORM_CONSTANT:
                        cl_aligned_u32(&uniforms, uinfo->data[i]);
                        break;
                case QUNIFORM_UNIFORM:
                        cl_aligned_u32(&uniforms,
                                       gallium_uniforms[uinfo->data[i]]);
                        break;
                case QUNIFORM_VIEWPORT_X_SCALE:
                        cl_aligned_f(&uniforms, vc4->viewport.scale[0] * 16.0f);
                        break;
                case QUNIFORM_VIEWPORT_Y_SCALE:
                        cl_aligned_f(&uniforms, vc4->viewport.scale[1] * 16.0f);
                        break;

                case QUNIFORM_VIEWPORT_Z_OFFSET:
                        cl_aligned_f(&uniforms, vc4->viewport.translate[2]);
                        break;
                case QUNIFORM_VIEWPORT_Z_SCALE:
                        cl_aligned_f(&uniforms, vc4->viewport.scale[2]);
                        break;

                case QUNIFORM_USER_CLIP_PLANE:
                        cl_aligned_f(&uniforms,
                                     vc4->clip.ucp[uinfo->data[i] / 4][uinfo->data[i] % 4]);
                        break;

                case QUNIFORM_TEXTURE_CONFIG_P0:
                        write_texture_p0(vc4, &uniforms, texstate,
                                         uinfo->data[i]);
                        break;

                case QUNIFORM_TEXTURE_CONFIG_P1:
                        write_texture_p1(vc4, &uniforms, texstate,
                                         uinfo->data[i]);
                        break;

                case QUNIFORM_TEXTURE_CONFIG_P2:
                        write_texture_p2(vc4, &uniforms, texstate,
                                         uinfo->data[i]);
                        break;

                case QUNIFORM_UBO_ADDR:
                        cl_aligned_reloc(vc4, &vc4->uniforms, &uniforms, ubo, 0);
                        break;

                case QUNIFORM_TEXTURE_MSAA_ADDR:
                        write_texture_msaa_addr(vc4, &uniforms,
                                                texstate, uinfo->data[i]);
                        break;

                case QUNIFORM_TEXTURE_BORDER_COLOR:
                        write_texture_border_color(vc4, &uniforms,
                                                   texstate, uinfo->data[i]);
                        break;

                case QUNIFORM_TEXRECT_SCALE_X:
                case QUNIFORM_TEXRECT_SCALE_Y:
                        cl_aligned_u32(&uniforms,
                                       get_texrect_scale(texstate,
                                                         uinfo->contents[i],
                                                         uinfo->data[i]));
                        break;

                case QUNIFORM_BLEND_CONST_COLOR_X:
                case QUNIFORM_BLEND_CONST_COLOR_Y:
                case QUNIFORM_BLEND_CONST_COLOR_Z:
                case QUNIFORM_BLEND_CONST_COLOR_W:
                        cl_aligned_f(&uniforms,
                                     CLAMP(vc4->blend_color.f.color[uinfo->contents[i] -
                                                                    QUNIFORM_BLEND_CONST_COLOR_X],
                                           0, 1));
                        break;

                case QUNIFORM_BLEND_CONST_COLOR_RGBA: {
                        const uint8_t *format_swiz =
                                vc4_get_format_swizzle(vc4->framebuffer.cbufs[0]->format);
                        uint32_t color = 0;
                        for (int i = 0; i < 4; i++) {
                                if (format_swiz[i] >= 4)
                                        continue;

                                color |= (vc4->blend_color.ub[format_swiz[i]] <<
                                          (i * 8));
                        }
                        cl_aligned_u32(&uniforms, color);
                        break;
                }

                case QUNIFORM_BLEND_CONST_COLOR_AAAA: {
                        uint8_t a = vc4->blend_color.ub[3];
                        cl_aligned_u32(&uniforms, ((a) |
                                                   (a << 8) |
                                                   (a << 16) |
                                                   (a << 24)));
                        break;
                }

                case QUNIFORM_STENCIL:
                        cl_aligned_u32(&uniforms,
                                       vc4->zsa->stencil_uniforms[uinfo->data[i]] |
                                       (uinfo->data[i] <= 1 ?
                                        (vc4->stencil_ref.ref_value[uinfo->data[i]] << 8) :
                                        0));
                        break;

                case QUNIFORM_ALPHA_REF:
                        cl_aligned_f(&uniforms,
                                     vc4->zsa->base.alpha.ref_value);
                        break;

                case QUNIFORM_SAMPLE_MASK:
                        cl_aligned_u32(&uniforms, vc4->sample_mask);
                        break;
                }
#if 0
                uint32_t written_val = *((uint32_t *)uniforms - 1);
                fprintf(stderr, "%p: %d / 0x%08x (%f)\n",
                        shader, i, written_val, uif(written_val));
#endif
        }

        cl_end(&vc4->uniforms, uniforms);

        vc4_bo_unreference(&ubo);
}

void
vc4_set_shader_uniform_dirty_flags(struct vc4_compiled_shader *shader)
{
        uint32_t dirty = 0;

        for (int i = 0; i < shader->uniforms.count; i++) {
                switch (shader->uniforms.contents[i]) {
                case QUNIFORM_CONSTANT:
                        break;
                case QUNIFORM_UNIFORM:
                case QUNIFORM_UBO_ADDR:
                        dirty |= VC4_DIRTY_CONSTBUF;
                        break;

                case QUNIFORM_VIEWPORT_X_SCALE:
                case QUNIFORM_VIEWPORT_Y_SCALE:
                case QUNIFORM_VIEWPORT_Z_OFFSET:
                case QUNIFORM_VIEWPORT_Z_SCALE:
                        dirty |= VC4_DIRTY_VIEWPORT;
                        break;

                case QUNIFORM_USER_CLIP_PLANE:
                        dirty |= VC4_DIRTY_CLIP;
                        break;

                case QUNIFORM_TEXTURE_CONFIG_P0:
                case QUNIFORM_TEXTURE_CONFIG_P1:
                case QUNIFORM_TEXTURE_CONFIG_P2:
                case QUNIFORM_TEXTURE_BORDER_COLOR:
                case QUNIFORM_TEXTURE_MSAA_ADDR:
                case QUNIFORM_TEXRECT_SCALE_X:
                case QUNIFORM_TEXRECT_SCALE_Y:
                        dirty |= VC4_DIRTY_TEXSTATE;
                        break;

                case QUNIFORM_BLEND_CONST_COLOR_X:
                case QUNIFORM_BLEND_CONST_COLOR_Y:
                case QUNIFORM_BLEND_CONST_COLOR_Z:
                case QUNIFORM_BLEND_CONST_COLOR_W:
                case QUNIFORM_BLEND_CONST_COLOR_RGBA:
                case QUNIFORM_BLEND_CONST_COLOR_AAAA:
                        dirty |= VC4_DIRTY_BLEND_COLOR;
                        break;

                case QUNIFORM_STENCIL:
                case QUNIFORM_ALPHA_REF:
                        dirty |= VC4_DIRTY_ZSA;
                        break;

                case QUNIFORM_SAMPLE_MASK:
                        dirty |= VC4_DIRTY_SAMPLE_MASK;
                        break;
                }
        }

        shader->uniform_dirty_bits = dirty;
}