aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/v3d/v3d_uniforms.c
blob: 307ccaafa3dc24c602bcd39b91ffe23452538cc6 (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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
 * Copyright © 2014-2017 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 "v3d_context.h"
#include "compiler/v3d_compiler.h"
#include "broadcom/cle/v3d_packet_v33_pack.h"

static uint32_t
get_texrect_scale(struct v3d_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 uint32_t
get_texture_size(struct v3d_texture_stateobj *texstate,
                 enum quniform_contents contents,
                 uint32_t data)
{
        struct pipe_sampler_view *texture = texstate->textures[data];

        switch (contents) {
        case QUNIFORM_TEXTURE_WIDTH:
                return u_minify(texture->texture->width0,
                                texture->u.tex.first_level);
        case QUNIFORM_TEXTURE_HEIGHT:
                return u_minify(texture->texture->height0,
                                texture->u.tex.first_level);
        case QUNIFORM_TEXTURE_DEPTH:
                return u_minify(texture->texture->depth0,
                                texture->u.tex.first_level);
        case QUNIFORM_TEXTURE_ARRAY_SIZE:
                return texture->texture->array_size;
        case QUNIFORM_TEXTURE_LEVELS:
                return (texture->u.tex.last_level -
                        texture->u.tex.first_level) + 1;
        default:
                unreachable("Bad texture size field");
        }
}

static uint32_t
get_image_size(struct v3d_shaderimg_stateobj *shaderimg,
               enum quniform_contents contents,
               uint32_t data)
{
        struct v3d_image_view *image = &shaderimg->si[data];

        switch (contents) {
        case QUNIFORM_IMAGE_WIDTH:
                return u_minify(image->base.resource->width0,
                                image->base.u.tex.level);
        case QUNIFORM_IMAGE_HEIGHT:
                return u_minify(image->base.resource->height0,
                                image->base.u.tex.level);
        case QUNIFORM_IMAGE_DEPTH:
                return u_minify(image->base.resource->depth0,
                                image->base.u.tex.level);
        case QUNIFORM_IMAGE_ARRAY_SIZE:
                return image->base.resource->array_size;
        default:
                unreachable("Bad texture size field");
        }
}

static struct v3d_bo *
v3d_upload_ubo(struct v3d_context *v3d,
               struct v3d_compiled_shader *shader,
               const uint32_t *gallium_uniforms)
{
        if (!shader->prog_data.base->ubo_size)
                return NULL;

        struct v3d_bo *ubo = v3d_bo_alloc(v3d->screen,
                                          shader->prog_data.base->ubo_size,
                                          "ubo");
        void *data = v3d_bo_map(ubo);
        for (uint32_t i = 0; i < shader->prog_data.base->num_ubo_ranges; i++) {
                memcpy(data + shader->prog_data.base->ubo_ranges[i].dst_offset,
                       ((const void *)gallium_uniforms +
                        shader->prog_data.base->ubo_ranges[i].src_offset),
                       shader->prog_data.base->ubo_ranges[i].size);
        }

        return ubo;
}

/**
 *  Writes the V3D 3.x P0 (CFG_MODE=1) texture parameter.
 *
 * Some bits of this field are dependent on the type of sample being done by
 * the shader, while other bits are dependent on the sampler state.  We OR the
 * two together here.
 */
static void
write_texture_p0(struct v3d_job *job,
                 struct v3d_cl_out **uniforms,
                 struct v3d_texture_stateobj *texstate,
                 uint32_t unit,
                 uint32_t shader_data)
{
        struct pipe_sampler_state *psampler = texstate->samplers[unit];
        struct v3d_sampler_state *sampler = v3d_sampler_state(psampler);

        cl_aligned_u32(uniforms, shader_data | sampler->p0);
}

/** Writes the V3D 3.x P1 (CFG_MODE=1) texture parameter. */
static void
write_texture_p1(struct v3d_job *job,
                 struct v3d_cl_out **uniforms,
                 struct v3d_texture_stateobj *texstate,
                 uint32_t data)
{
        /* Extract the texture unit from the top bits, and the compiler's
         * packed p1 from the bottom.
         */
        uint32_t unit = data >> 5;
        uint32_t p1 = data & 0x1f;

        struct pipe_sampler_view *psview = texstate->textures[unit];
        struct v3d_sampler_view *sview = v3d_sampler_view(psview);

        struct V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1 unpacked = {
                .texture_state_record_base_address = texstate->texture_state[unit],
        };

        uint32_t packed;
        V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1_pack(&job->indirect,
                                                         (uint8_t *)&packed,
                                                         &unpacked);

        cl_aligned_u32(uniforms, p1 | packed | sview->p1);
}

/** Writes the V3D 4.x TMU configuration parameter 0. */
static void
write_tmu_p0(struct v3d_job *job,
             struct v3d_cl_out **uniforms,
             struct v3d_texture_stateobj *texstate,
             uint32_t data)
{
        int unit  = v3d_tmu_config_data_get_unit(data);
        struct pipe_sampler_view *psview = texstate->textures[unit];
        struct v3d_sampler_view *sview = v3d_sampler_view(psview);
        struct v3d_resource *rsc = v3d_resource(sview->texture);

        cl_aligned_reloc(&job->indirect, uniforms, sview->bo,
                         v3d_tmu_config_data_get_value(data));
        v3d_job_add_bo(job, rsc->bo);
}

static void
write_image_tmu_p0(struct v3d_job *job,
                   struct v3d_cl_out **uniforms,
                   struct v3d_shaderimg_stateobj *img,
                   uint32_t data)
{
        /* Extract the image unit from the top bits, and the compiler's
         * packed p0 from the bottom.
         */
        uint32_t unit = data >> 24;
        uint32_t p0 = data & 0x00ffffff;

        struct v3d_image_view *iview = &img->si[unit];
        struct v3d_resource *rsc = v3d_resource(iview->base.resource);

        cl_aligned_reloc(&job->indirect, uniforms,
                         v3d_resource(iview->tex_state)->bo,
                         iview->tex_state_offset | p0);
        v3d_job_add_bo(job, rsc->bo);
}

/** Writes the V3D 4.x TMU configuration parameter 1. */
static void
write_tmu_p1(struct v3d_job *job,
             struct v3d_cl_out **uniforms,
             struct v3d_texture_stateobj *texstate,
             uint32_t data)
{
        uint32_t unit = v3d_tmu_config_data_get_unit(data);
        struct pipe_sampler_state *psampler = texstate->samplers[unit];
        struct v3d_sampler_state *sampler = v3d_sampler_state(psampler);
        struct pipe_sampler_view *psview = texstate->textures[unit];
        struct v3d_sampler_view *sview = v3d_sampler_view(psview);
        int variant = 0;

        if (sampler->border_color_variants)
                variant = sview->sampler_variant;

        cl_aligned_reloc(&job->indirect, uniforms,
                         v3d_resource(sampler->sampler_state)->bo,
                         sampler->sampler_state_offset[variant] |
                         v3d_tmu_config_data_get_value(data));
}

struct v3d_cl_reloc
v3d_write_uniforms(struct v3d_context *v3d, struct v3d_compiled_shader *shader,
                   enum pipe_shader_type stage)
{
        struct v3d_constbuf_stateobj *cb = &v3d->constbuf[stage];
        struct v3d_texture_stateobj *texstate = &v3d->tex[stage];
        struct v3d_uniform_list *uinfo = &shader->prog_data.base->uniforms;
        struct v3d_job *job = v3d->job;
        const uint32_t *gallium_uniforms = cb->cb[0].user_buffer;
        struct v3d_bo *ubo = v3d_upload_ubo(v3d, shader, gallium_uniforms);

        /* We always need to return some space for uniforms, because the HW
         * will be prefetching, even if we don't read any in the program.
         */
        v3d_cl_ensure_space(&job->indirect, MAX2(uinfo->count, 1) * 4, 4);

        struct v3d_cl_reloc uniform_stream = cl_get_address(&job->indirect);
        v3d_bo_reference(uniform_stream.bo);

        struct v3d_cl_out *uniforms =
                cl_start(&job->indirect);

        for (int i = 0; i < uinfo->count; i++) {
                uint32_t data = uinfo->data[i];

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

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

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

                case QUNIFORM_TMU_CONFIG_P0:
                        write_tmu_p0(job, &uniforms, texstate, data);
                        break;

                case QUNIFORM_TMU_CONFIG_P1:
                        write_tmu_p1(job, &uniforms, texstate, data);
                        break;

                case QUNIFORM_IMAGE_TMU_CONFIG_P0:
                        write_image_tmu_p0(job, &uniforms,
                                           &v3d->shaderimg[stage], data);
                        break;

                case QUNIFORM_TEXTURE_CONFIG_P1:
                        write_texture_p1(job, &uniforms, texstate,
                                         data);
                        break;

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

                case QUNIFORM_TEXTURE_WIDTH:
                case QUNIFORM_TEXTURE_HEIGHT:
                case QUNIFORM_TEXTURE_DEPTH:
                case QUNIFORM_TEXTURE_ARRAY_SIZE:
                case QUNIFORM_TEXTURE_LEVELS:
                        cl_aligned_u32(&uniforms,
                                       get_texture_size(texstate,
                                                        uinfo->contents[i],
                                                        data));
                        break;

                case QUNIFORM_IMAGE_WIDTH:
                case QUNIFORM_IMAGE_HEIGHT:
                case QUNIFORM_IMAGE_DEPTH:
                case QUNIFORM_IMAGE_ARRAY_SIZE:
                        cl_aligned_u32(&uniforms,
                                       get_image_size(&v3d->shaderimg[stage],
                                                      uinfo->contents[i],
                                                      data));
                        break;

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

                case QUNIFORM_UBO_ADDR:
                        if (data == 0) {
                                cl_aligned_reloc(&job->indirect, &uniforms,
                                                 ubo, 0);
                        } else {
                                int ubo_index = data;
                                struct v3d_resource *rsc =
                                        v3d_resource(cb->cb[ubo_index].buffer);

                                cl_aligned_reloc(&job->indirect, &uniforms,
                                                 rsc->bo,
                                                 cb->cb[ubo_index].buffer_offset);
                        }
                        break;

                case QUNIFORM_SSBO_OFFSET: {
                        struct pipe_shader_buffer *sb =
                                &v3d->ssbo[stage].sb[data];

                        cl_aligned_reloc(&job->indirect, &uniforms,
                                         v3d_resource(sb->buffer)->bo,
                                         sb->buffer_offset);
                        break;
                }

                case QUNIFORM_GET_BUFFER_SIZE:
                        cl_aligned_u32(&uniforms,
                                       v3d->ssbo[stage].sb[data].buffer_size);
                        break;

                case QUNIFORM_TEXTURE_FIRST_LEVEL:
                        cl_aligned_f(&uniforms,
                                     texstate->textures[data]->u.tex.first_level);
                        break;

                case QUNIFORM_SPILL_OFFSET:
                        cl_aligned_reloc(&job->indirect, &uniforms,
                                         v3d->prog.spill_bo, 0);
                        break;

                case QUNIFORM_SPILL_SIZE_PER_THREAD:
                        cl_aligned_u32(&uniforms,
                                       v3d->prog.spill_size_per_thread);
                        break;

                default:
                        assert(quniform_contents_is_texture_p0(uinfo->contents[i]));

                        write_texture_p0(job, &uniforms, texstate,
                                         uinfo->contents[i] -
                                         QUNIFORM_TEXTURE_CONFIG_P0_0,
                                         data);
                        break;

                }
#if 0
                uint32_t written_val = *((uint32_t *)uniforms - 1);
                fprintf(stderr, "shader %p[%d]: 0x%08x / 0x%08x (%f) ",
                        shader, i, __gen_address_offset(&uniform_stream) + i * 4,
                        written_val, uif(written_val));
                vir_dump_uniform(uinfo->contents[i], data);
                fprintf(stderr, "\n");
#endif
        }

        cl_end(&job->indirect, uniforms);

        v3d_bo_unreference(&ubo);

        return uniform_stream;
}

void
v3d_set_shader_uniform_dirty_flags(struct v3d_compiled_shader *shader)
{
        uint32_t dirty = 0;

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

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

                case QUNIFORM_USER_CLIP_PLANE:
                        dirty |= VC5_DIRTY_CLIP;
                        break;

                case QUNIFORM_TMU_CONFIG_P0:
                case QUNIFORM_TMU_CONFIG_P1:
                case QUNIFORM_TEXTURE_CONFIG_P1:
                case QUNIFORM_TEXTURE_FIRST_LEVEL:
                case QUNIFORM_TEXRECT_SCALE_X:
                case QUNIFORM_TEXRECT_SCALE_Y:
                case QUNIFORM_TEXTURE_WIDTH:
                case QUNIFORM_TEXTURE_HEIGHT:
                case QUNIFORM_TEXTURE_DEPTH:
                case QUNIFORM_TEXTURE_ARRAY_SIZE:
                case QUNIFORM_TEXTURE_LEVELS:
                case QUNIFORM_SPILL_OFFSET:
                case QUNIFORM_SPILL_SIZE_PER_THREAD:
                        /* We could flag this on just the stage we're
                         * compiling for, but it's not passed in.
                         */
                        dirty |= VC5_DIRTY_FRAGTEX | VC5_DIRTY_VERTTEX;
                        break;

                case QUNIFORM_SSBO_OFFSET:
                case QUNIFORM_GET_BUFFER_SIZE:
                        dirty |= VC5_DIRTY_SSBO;
                        break;

                case QUNIFORM_IMAGE_TMU_CONFIG_P0:
                case QUNIFORM_IMAGE_WIDTH:
                case QUNIFORM_IMAGE_HEIGHT:
                case QUNIFORM_IMAGE_DEPTH:
                case QUNIFORM_IMAGE_ARRAY_SIZE:
                        dirty |= VC5_DIRTY_SHADER_IMAGE;
                        break;

                case QUNIFORM_ALPHA_REF:
                        dirty |= VC5_DIRTY_ZSA;
                        break;

                default:
                        assert(quniform_contents_is_texture_p0(shader->prog_data.base->uniforms.contents[i]));
                        dirty |= VC5_DIRTY_FRAGTEX | VC5_DIRTY_VERTTEX;
                        break;
                }
        }

        shader->uniform_dirty_bits = dirty;
}