1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2014-2017 Broadcom
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "util/u_pack_color.h"
25bf215546Sopenharmony_ci#include "util/u_upload_mgr.h"
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "v3d_context.h"
28bf215546Sopenharmony_ci#include "compiler/v3d_compiler.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci/* We don't expect that the packets we use in this file change across across
31bf215546Sopenharmony_ci * hw versions, so we just include directly the v33 header
32bf215546Sopenharmony_ci */
33bf215546Sopenharmony_ci#include "broadcom/cle/v3d_packet_v33_pack.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_cistatic uint32_t
36bf215546Sopenharmony_ciget_texrect_scale(struct v3d_texture_stateobj *texstate,
37bf215546Sopenharmony_ci                  enum quniform_contents contents,
38bf215546Sopenharmony_ci                  uint32_t data)
39bf215546Sopenharmony_ci{
40bf215546Sopenharmony_ci        struct pipe_sampler_view *texture = texstate->textures[data];
41bf215546Sopenharmony_ci        uint32_t dim;
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci        if (contents == QUNIFORM_TEXRECT_SCALE_X)
44bf215546Sopenharmony_ci                dim = texture->texture->width0;
45bf215546Sopenharmony_ci        else
46bf215546Sopenharmony_ci                dim = texture->texture->height0;
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci        return fui(1.0f / dim);
49bf215546Sopenharmony_ci}
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_cistatic uint32_t
52bf215546Sopenharmony_ciget_texture_size(struct v3d_texture_stateobj *texstate,
53bf215546Sopenharmony_ci                 enum quniform_contents contents,
54bf215546Sopenharmony_ci                 uint32_t data)
55bf215546Sopenharmony_ci{
56bf215546Sopenharmony_ci        struct pipe_sampler_view *texture = texstate->textures[data];
57bf215546Sopenharmony_ci        switch (contents) {
58bf215546Sopenharmony_ci        case QUNIFORM_TEXTURE_WIDTH:
59bf215546Sopenharmony_ci                if (texture->target == PIPE_BUFFER) {
60bf215546Sopenharmony_ci                        return texture->u.buf.size /
61bf215546Sopenharmony_ci                                util_format_get_blocksize(texture->format);
62bf215546Sopenharmony_ci                } else {
63bf215546Sopenharmony_ci                        return u_minify(texture->texture->width0,
64bf215546Sopenharmony_ci                                        texture->u.tex.first_level);
65bf215546Sopenharmony_ci                }
66bf215546Sopenharmony_ci        case QUNIFORM_TEXTURE_HEIGHT:
67bf215546Sopenharmony_ci                return u_minify(texture->texture->height0,
68bf215546Sopenharmony_ci                                texture->u.tex.first_level);
69bf215546Sopenharmony_ci        case QUNIFORM_TEXTURE_DEPTH:
70bf215546Sopenharmony_ci                assert(texture->target != PIPE_BUFFER);
71bf215546Sopenharmony_ci                return u_minify(texture->texture->depth0,
72bf215546Sopenharmony_ci                                texture->u.tex.first_level);
73bf215546Sopenharmony_ci        case QUNIFORM_TEXTURE_ARRAY_SIZE:
74bf215546Sopenharmony_ci                assert(texture->target != PIPE_BUFFER);
75bf215546Sopenharmony_ci                return texture->texture->array_size;
76bf215546Sopenharmony_ci        case QUNIFORM_TEXTURE_LEVELS:
77bf215546Sopenharmony_ci                assert(texture->target != PIPE_BUFFER);
78bf215546Sopenharmony_ci                return (texture->u.tex.last_level -
79bf215546Sopenharmony_ci                        texture->u.tex.first_level) + 1;
80bf215546Sopenharmony_ci        default:
81bf215546Sopenharmony_ci                unreachable("Bad texture size field");
82bf215546Sopenharmony_ci        }
83bf215546Sopenharmony_ci}
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_cistatic uint32_t
86bf215546Sopenharmony_ciget_image_size(struct v3d_shaderimg_stateobj *shaderimg,
87bf215546Sopenharmony_ci               enum quniform_contents contents,
88bf215546Sopenharmony_ci               uint32_t data)
89bf215546Sopenharmony_ci{
90bf215546Sopenharmony_ci        struct v3d_image_view *image = &shaderimg->si[data];
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci        switch (contents) {
93bf215546Sopenharmony_ci        case QUNIFORM_IMAGE_WIDTH:
94bf215546Sopenharmony_ci                if (image->base.resource->target == PIPE_BUFFER) {
95bf215546Sopenharmony_ci                        return image->base.u.buf.size /
96bf215546Sopenharmony_ci                                util_format_get_blocksize(image->base.format);
97bf215546Sopenharmony_ci                } else {
98bf215546Sopenharmony_ci                        return u_minify(image->base.resource->width0,
99bf215546Sopenharmony_ci                                        image->base.u.tex.level);
100bf215546Sopenharmony_ci                }
101bf215546Sopenharmony_ci        case QUNIFORM_IMAGE_HEIGHT:
102bf215546Sopenharmony_ci                assert(image->base.resource->target != PIPE_BUFFER);
103bf215546Sopenharmony_ci                return u_minify(image->base.resource->height0,
104bf215546Sopenharmony_ci                                image->base.u.tex.level);
105bf215546Sopenharmony_ci        case QUNIFORM_IMAGE_DEPTH:
106bf215546Sopenharmony_ci                assert(image->base.resource->target != PIPE_BUFFER);
107bf215546Sopenharmony_ci                return u_minify(image->base.resource->depth0,
108bf215546Sopenharmony_ci                                image->base.u.tex.level);
109bf215546Sopenharmony_ci        case QUNIFORM_IMAGE_ARRAY_SIZE:
110bf215546Sopenharmony_ci                assert(image->base.resource->target != PIPE_BUFFER);
111bf215546Sopenharmony_ci                return image->base.resource->array_size;
112bf215546Sopenharmony_ci        default:
113bf215546Sopenharmony_ci                unreachable("Bad texture size field");
114bf215546Sopenharmony_ci        }
115bf215546Sopenharmony_ci}
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci/**
118bf215546Sopenharmony_ci *  Writes the V3D 3.x P0 (CFG_MODE=1) texture parameter.
119bf215546Sopenharmony_ci *
120bf215546Sopenharmony_ci * Some bits of this field are dependent on the type of sample being done by
121bf215546Sopenharmony_ci * the shader, while other bits are dependent on the sampler state.  We OR the
122bf215546Sopenharmony_ci * two together here.
123bf215546Sopenharmony_ci */
124bf215546Sopenharmony_cistatic void
125bf215546Sopenharmony_ciwrite_texture_p0(struct v3d_job *job,
126bf215546Sopenharmony_ci                 struct v3d_cl_out **uniforms,
127bf215546Sopenharmony_ci                 struct v3d_texture_stateobj *texstate,
128bf215546Sopenharmony_ci                 uint32_t unit,
129bf215546Sopenharmony_ci                 uint32_t shader_data)
130bf215546Sopenharmony_ci{
131bf215546Sopenharmony_ci        struct pipe_sampler_state *psampler = texstate->samplers[unit];
132bf215546Sopenharmony_ci        struct v3d_sampler_state *sampler = v3d_sampler_state(psampler);
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci        cl_aligned_u32(uniforms, shader_data | sampler->p0);
135bf215546Sopenharmony_ci}
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ci/** Writes the V3D 3.x P1 (CFG_MODE=1) texture parameter. */
138bf215546Sopenharmony_cistatic void
139bf215546Sopenharmony_ciwrite_texture_p1(struct v3d_job *job,
140bf215546Sopenharmony_ci                 struct v3d_cl_out **uniforms,
141bf215546Sopenharmony_ci                 struct v3d_texture_stateobj *texstate,
142bf215546Sopenharmony_ci                 uint32_t data)
143bf215546Sopenharmony_ci{
144bf215546Sopenharmony_ci        /* Extract the texture unit from the top bits, and the compiler's
145bf215546Sopenharmony_ci         * packed p1 from the bottom.
146bf215546Sopenharmony_ci         */
147bf215546Sopenharmony_ci        uint32_t unit = data >> 5;
148bf215546Sopenharmony_ci        uint32_t p1 = data & 0x1f;
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_ci        struct pipe_sampler_view *psview = texstate->textures[unit];
151bf215546Sopenharmony_ci        struct v3d_sampler_view *sview = v3d_sampler_view(psview);
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci        struct V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1 unpacked = {
154bf215546Sopenharmony_ci                .texture_state_record_base_address = texstate->texture_state[unit],
155bf215546Sopenharmony_ci        };
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_ci        uint32_t packed;
158bf215546Sopenharmony_ci        V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1_pack(&job->indirect,
159bf215546Sopenharmony_ci                                                         (uint8_t *)&packed,
160bf215546Sopenharmony_ci                                                         &unpacked);
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_ci        cl_aligned_u32(uniforms, p1 | packed | sview->p1);
163bf215546Sopenharmony_ci}
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci/** Writes the V3D 4.x TMU configuration parameter 0. */
166bf215546Sopenharmony_cistatic void
167bf215546Sopenharmony_ciwrite_tmu_p0(struct v3d_job *job,
168bf215546Sopenharmony_ci             struct v3d_cl_out **uniforms,
169bf215546Sopenharmony_ci             struct v3d_texture_stateobj *texstate,
170bf215546Sopenharmony_ci             uint32_t data)
171bf215546Sopenharmony_ci{
172bf215546Sopenharmony_ci        int unit = v3d_unit_data_get_unit(data);
173bf215546Sopenharmony_ci        struct pipe_sampler_view *psview = texstate->textures[unit];
174bf215546Sopenharmony_ci        struct v3d_sampler_view *sview = v3d_sampler_view(psview);
175bf215546Sopenharmony_ci        /* GL_OES_texture_buffer spec:
176bf215546Sopenharmony_ci         *     "If no buffer object is bound to the buffer texture, the
177bf215546Sopenharmony_ci         *      results of the texel access are undefined."
178bf215546Sopenharmony_ci         *
179bf215546Sopenharmony_ci         * This can be interpreted as allowing any result to come back, but
180bf215546Sopenharmony_ci         * not terminate the program (and some tests interpret that).
181bf215546Sopenharmony_ci         *
182bf215546Sopenharmony_ci         * FIXME: just return is not a full valid solution, as it could still
183bf215546Sopenharmony_ci         * try to get a wrong address for the shader state address. Perhaps we
184bf215546Sopenharmony_ci         * would need to set up a BO with a "default texture state"
185bf215546Sopenharmony_ci         */
186bf215546Sopenharmony_ci        if (sview == NULL)
187bf215546Sopenharmony_ci                return;
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_ci        struct v3d_resource *rsc = v3d_resource(sview->texture);
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_ci        cl_aligned_reloc(&job->indirect, uniforms, sview->bo,
192bf215546Sopenharmony_ci                         v3d_unit_data_get_offset(data));
193bf215546Sopenharmony_ci        v3d_job_add_bo(job, rsc->bo);
194bf215546Sopenharmony_ci}
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_cistatic void
197bf215546Sopenharmony_ciwrite_image_tmu_p0(struct v3d_job *job,
198bf215546Sopenharmony_ci                   struct v3d_cl_out **uniforms,
199bf215546Sopenharmony_ci                   struct v3d_shaderimg_stateobj *img,
200bf215546Sopenharmony_ci                   uint32_t data)
201bf215546Sopenharmony_ci{
202bf215546Sopenharmony_ci        /* Extract the image unit from the top bits, and the compiler's
203bf215546Sopenharmony_ci         * packed p0 from the bottom.
204bf215546Sopenharmony_ci         */
205bf215546Sopenharmony_ci        uint32_t unit = data >> 24;
206bf215546Sopenharmony_ci        uint32_t p0 = data & 0x00ffffff;
207bf215546Sopenharmony_ci
208bf215546Sopenharmony_ci        struct v3d_image_view *iview = &img->si[unit];
209bf215546Sopenharmony_ci        struct v3d_resource *rsc = v3d_resource(iview->base.resource);
210bf215546Sopenharmony_ci
211bf215546Sopenharmony_ci        cl_aligned_reloc(&job->indirect, uniforms,
212bf215546Sopenharmony_ci                         v3d_resource(iview->tex_state)->bo,
213bf215546Sopenharmony_ci                         iview->tex_state_offset | p0);
214bf215546Sopenharmony_ci        v3d_job_add_bo(job, rsc->bo);
215bf215546Sopenharmony_ci}
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_ci/** Writes the V3D 4.x TMU configuration parameter 1. */
218bf215546Sopenharmony_cistatic void
219bf215546Sopenharmony_ciwrite_tmu_p1(struct v3d_job *job,
220bf215546Sopenharmony_ci             struct v3d_cl_out **uniforms,
221bf215546Sopenharmony_ci             struct v3d_texture_stateobj *texstate,
222bf215546Sopenharmony_ci             uint32_t data)
223bf215546Sopenharmony_ci{
224bf215546Sopenharmony_ci        uint32_t unit = v3d_unit_data_get_unit(data);
225bf215546Sopenharmony_ci        struct pipe_sampler_state *psampler = texstate->samplers[unit];
226bf215546Sopenharmony_ci        struct v3d_sampler_state *sampler = v3d_sampler_state(psampler);
227bf215546Sopenharmony_ci        struct pipe_sampler_view *psview = texstate->textures[unit];
228bf215546Sopenharmony_ci        struct v3d_sampler_view *sview = v3d_sampler_view(psview);
229bf215546Sopenharmony_ci        int variant = 0;
230bf215546Sopenharmony_ci
231bf215546Sopenharmony_ci        /* If we are being asked by the compiler to write parameter 1, then we
232bf215546Sopenharmony_ci         * need that. So if we are at this point, we should expect to have a
233bf215546Sopenharmony_ci         * sampler and psampler. As an additional assert, we can check that we
234bf215546Sopenharmony_ci         * are not on a texel buffer case, as these don't have a sampler.
235bf215546Sopenharmony_ci         */
236bf215546Sopenharmony_ci        assert(psview->target != PIPE_BUFFER);
237bf215546Sopenharmony_ci        assert(sampler);
238bf215546Sopenharmony_ci        assert(psampler);
239bf215546Sopenharmony_ci
240bf215546Sopenharmony_ci        if (sampler->border_color_variants)
241bf215546Sopenharmony_ci                variant = sview->sampler_variant;
242bf215546Sopenharmony_ci
243bf215546Sopenharmony_ci        cl_aligned_reloc(&job->indirect, uniforms,
244bf215546Sopenharmony_ci                         v3d_resource(sampler->sampler_state)->bo,
245bf215546Sopenharmony_ci                         sampler->sampler_state_offset[variant] |
246bf215546Sopenharmony_ci                         v3d_unit_data_get_offset(data));
247bf215546Sopenharmony_ci}
248bf215546Sopenharmony_ci
249bf215546Sopenharmony_cistruct v3d_cl_reloc
250bf215546Sopenharmony_civ3d_write_uniforms(struct v3d_context *v3d, struct v3d_job *job,
251bf215546Sopenharmony_ci                   struct v3d_compiled_shader *shader,
252bf215546Sopenharmony_ci                   enum pipe_shader_type stage)
253bf215546Sopenharmony_ci{
254bf215546Sopenharmony_ci        struct v3d_constbuf_stateobj *cb = &v3d->constbuf[stage];
255bf215546Sopenharmony_ci        struct v3d_texture_stateobj *texstate = &v3d->tex[stage];
256bf215546Sopenharmony_ci        struct v3d_uniform_list *uinfo = &shader->prog_data.base->uniforms;
257bf215546Sopenharmony_ci        const uint32_t *gallium_uniforms = cb->cb[0].user_buffer;
258bf215546Sopenharmony_ci
259bf215546Sopenharmony_ci        /* The hardware always pre-fetches the next uniform (also when there
260bf215546Sopenharmony_ci         * aren't any), so we always allocate space for an extra slot. This
261bf215546Sopenharmony_ci         * fixes MMU exceptions reported since Linux kernel 5.4 when the
262bf215546Sopenharmony_ci         * uniforms fill up the tail bytes of a page in the indirect
263bf215546Sopenharmony_ci         * BO. In that scenario, when the hardware pre-fetches after reading
264bf215546Sopenharmony_ci         * the last uniform it will read beyond the end of the page and trigger
265bf215546Sopenharmony_ci         * the MMU exception.
266bf215546Sopenharmony_ci         */
267bf215546Sopenharmony_ci        v3d_cl_ensure_space(&job->indirect, (uinfo->count + 1) * 4, 4);
268bf215546Sopenharmony_ci
269bf215546Sopenharmony_ci        struct v3d_cl_reloc uniform_stream = cl_get_address(&job->indirect);
270bf215546Sopenharmony_ci        v3d_bo_reference(uniform_stream.bo);
271bf215546Sopenharmony_ci
272bf215546Sopenharmony_ci        struct v3d_cl_out *uniforms =
273bf215546Sopenharmony_ci                cl_start(&job->indirect);
274bf215546Sopenharmony_ci
275bf215546Sopenharmony_ci        for (int i = 0; i < uinfo->count; i++) {
276bf215546Sopenharmony_ci                uint32_t data = uinfo->data[i];
277bf215546Sopenharmony_ci
278bf215546Sopenharmony_ci                switch (uinfo->contents[i]) {
279bf215546Sopenharmony_ci                case QUNIFORM_CONSTANT:
280bf215546Sopenharmony_ci                        cl_aligned_u32(&uniforms, data);
281bf215546Sopenharmony_ci                        break;
282bf215546Sopenharmony_ci                case QUNIFORM_UNIFORM:
283bf215546Sopenharmony_ci                        cl_aligned_u32(&uniforms, gallium_uniforms[data]);
284bf215546Sopenharmony_ci                        break;
285bf215546Sopenharmony_ci                case QUNIFORM_VIEWPORT_X_SCALE:
286bf215546Sopenharmony_ci                        cl_aligned_f(&uniforms, v3d->viewport.scale[0] * 256.0f);
287bf215546Sopenharmony_ci                        break;
288bf215546Sopenharmony_ci                case QUNIFORM_VIEWPORT_Y_SCALE:
289bf215546Sopenharmony_ci                        cl_aligned_f(&uniforms, v3d->viewport.scale[1] * 256.0f);
290bf215546Sopenharmony_ci                        break;
291bf215546Sopenharmony_ci
292bf215546Sopenharmony_ci                case QUNIFORM_VIEWPORT_Z_OFFSET:
293bf215546Sopenharmony_ci                        cl_aligned_f(&uniforms, v3d->viewport.translate[2]);
294bf215546Sopenharmony_ci                        break;
295bf215546Sopenharmony_ci                case QUNIFORM_VIEWPORT_Z_SCALE:
296bf215546Sopenharmony_ci                        cl_aligned_f(&uniforms, v3d->viewport.scale[2]);
297bf215546Sopenharmony_ci                        break;
298bf215546Sopenharmony_ci
299bf215546Sopenharmony_ci                case QUNIFORM_USER_CLIP_PLANE:
300bf215546Sopenharmony_ci                        cl_aligned_f(&uniforms,
301bf215546Sopenharmony_ci                                     v3d->clip.ucp[data / 4][data % 4]);
302bf215546Sopenharmony_ci                        break;
303bf215546Sopenharmony_ci
304bf215546Sopenharmony_ci                case QUNIFORM_TMU_CONFIG_P0:
305bf215546Sopenharmony_ci                        write_tmu_p0(job, &uniforms, texstate, data);
306bf215546Sopenharmony_ci                        break;
307bf215546Sopenharmony_ci
308bf215546Sopenharmony_ci                case QUNIFORM_TMU_CONFIG_P1:
309bf215546Sopenharmony_ci                        write_tmu_p1(job, &uniforms, texstate, data);
310bf215546Sopenharmony_ci                        break;
311bf215546Sopenharmony_ci
312bf215546Sopenharmony_ci                case QUNIFORM_IMAGE_TMU_CONFIG_P0:
313bf215546Sopenharmony_ci                        write_image_tmu_p0(job, &uniforms,
314bf215546Sopenharmony_ci                                           &v3d->shaderimg[stage], data);
315bf215546Sopenharmony_ci                        break;
316bf215546Sopenharmony_ci
317bf215546Sopenharmony_ci                case QUNIFORM_TEXTURE_CONFIG_P1:
318bf215546Sopenharmony_ci                        write_texture_p1(job, &uniforms, texstate,
319bf215546Sopenharmony_ci                                         data);
320bf215546Sopenharmony_ci                        break;
321bf215546Sopenharmony_ci
322bf215546Sopenharmony_ci                case QUNIFORM_TEXRECT_SCALE_X:
323bf215546Sopenharmony_ci                case QUNIFORM_TEXRECT_SCALE_Y:
324bf215546Sopenharmony_ci                        cl_aligned_u32(&uniforms,
325bf215546Sopenharmony_ci                                       get_texrect_scale(texstate,
326bf215546Sopenharmony_ci                                                         uinfo->contents[i],
327bf215546Sopenharmony_ci                                                         data));
328bf215546Sopenharmony_ci                        break;
329bf215546Sopenharmony_ci
330bf215546Sopenharmony_ci                case QUNIFORM_TEXTURE_WIDTH:
331bf215546Sopenharmony_ci                case QUNIFORM_TEXTURE_HEIGHT:
332bf215546Sopenharmony_ci                case QUNIFORM_TEXTURE_DEPTH:
333bf215546Sopenharmony_ci                case QUNIFORM_TEXTURE_ARRAY_SIZE:
334bf215546Sopenharmony_ci                case QUNIFORM_TEXTURE_LEVELS:
335bf215546Sopenharmony_ci                        cl_aligned_u32(&uniforms,
336bf215546Sopenharmony_ci                                       get_texture_size(texstate,
337bf215546Sopenharmony_ci                                                        uinfo->contents[i],
338bf215546Sopenharmony_ci                                                        data));
339bf215546Sopenharmony_ci                        break;
340bf215546Sopenharmony_ci
341bf215546Sopenharmony_ci                case QUNIFORM_IMAGE_WIDTH:
342bf215546Sopenharmony_ci                case QUNIFORM_IMAGE_HEIGHT:
343bf215546Sopenharmony_ci                case QUNIFORM_IMAGE_DEPTH:
344bf215546Sopenharmony_ci                case QUNIFORM_IMAGE_ARRAY_SIZE:
345bf215546Sopenharmony_ci                        cl_aligned_u32(&uniforms,
346bf215546Sopenharmony_ci                                       get_image_size(&v3d->shaderimg[stage],
347bf215546Sopenharmony_ci                                                      uinfo->contents[i],
348bf215546Sopenharmony_ci                                                      data));
349bf215546Sopenharmony_ci                        break;
350bf215546Sopenharmony_ci
351bf215546Sopenharmony_ci                case QUNIFORM_LINE_WIDTH:
352bf215546Sopenharmony_ci                        cl_aligned_f(&uniforms,
353bf215546Sopenharmony_ci                                     v3d->rasterizer->base.line_width);
354bf215546Sopenharmony_ci                        break;
355bf215546Sopenharmony_ci
356bf215546Sopenharmony_ci                case QUNIFORM_AA_LINE_WIDTH:
357bf215546Sopenharmony_ci                        cl_aligned_f(&uniforms, v3d_get_real_line_width(v3d));
358bf215546Sopenharmony_ci                        break;
359bf215546Sopenharmony_ci
360bf215546Sopenharmony_ci                case QUNIFORM_UBO_ADDR: {
361bf215546Sopenharmony_ci                        uint32_t unit = v3d_unit_data_get_unit(data);
362bf215546Sopenharmony_ci                        /* Constant buffer 0 may be a system memory pointer,
363bf215546Sopenharmony_ci                         * in which case we want to upload a shadow copy to
364bf215546Sopenharmony_ci                         * the GPU.
365bf215546Sopenharmony_ci                        */
366bf215546Sopenharmony_ci                        if (!cb->cb[unit].buffer) {
367bf215546Sopenharmony_ci                                u_upload_data(v3d->uploader, 0,
368bf215546Sopenharmony_ci                                              cb->cb[unit].buffer_size, 16,
369bf215546Sopenharmony_ci                                              cb->cb[unit].user_buffer,
370bf215546Sopenharmony_ci                                              &cb->cb[unit].buffer_offset,
371bf215546Sopenharmony_ci                                              &cb->cb[unit].buffer);
372bf215546Sopenharmony_ci                        }
373bf215546Sopenharmony_ci
374bf215546Sopenharmony_ci                        cl_aligned_reloc(&job->indirect, &uniforms,
375bf215546Sopenharmony_ci                                         v3d_resource(cb->cb[unit].buffer)->bo,
376bf215546Sopenharmony_ci                                         cb->cb[unit].buffer_offset +
377bf215546Sopenharmony_ci                                         v3d_unit_data_get_offset(data));
378bf215546Sopenharmony_ci                        break;
379bf215546Sopenharmony_ci                }
380bf215546Sopenharmony_ci
381bf215546Sopenharmony_ci                case QUNIFORM_SSBO_OFFSET: {
382bf215546Sopenharmony_ci                        struct pipe_shader_buffer *sb =
383bf215546Sopenharmony_ci                                &v3d->ssbo[stage].sb[data];
384bf215546Sopenharmony_ci
385bf215546Sopenharmony_ci                        cl_aligned_reloc(&job->indirect, &uniforms,
386bf215546Sopenharmony_ci                                         v3d_resource(sb->buffer)->bo,
387bf215546Sopenharmony_ci                                         sb->buffer_offset);
388bf215546Sopenharmony_ci                        break;
389bf215546Sopenharmony_ci                }
390bf215546Sopenharmony_ci
391bf215546Sopenharmony_ci                case QUNIFORM_GET_SSBO_SIZE:
392bf215546Sopenharmony_ci                        cl_aligned_u32(&uniforms,
393bf215546Sopenharmony_ci                                       v3d->ssbo[stage].sb[data].buffer_size);
394bf215546Sopenharmony_ci                        break;
395bf215546Sopenharmony_ci
396bf215546Sopenharmony_ci                case QUNIFORM_TEXTURE_FIRST_LEVEL:
397bf215546Sopenharmony_ci                        cl_aligned_f(&uniforms,
398bf215546Sopenharmony_ci                                     texstate->textures[data]->u.tex.first_level);
399bf215546Sopenharmony_ci                        break;
400bf215546Sopenharmony_ci
401bf215546Sopenharmony_ci                case QUNIFORM_SPILL_OFFSET:
402bf215546Sopenharmony_ci                        cl_aligned_reloc(&job->indirect, &uniforms,
403bf215546Sopenharmony_ci                                         v3d->prog.spill_bo, 0);
404bf215546Sopenharmony_ci                        break;
405bf215546Sopenharmony_ci
406bf215546Sopenharmony_ci                case QUNIFORM_SPILL_SIZE_PER_THREAD:
407bf215546Sopenharmony_ci                        cl_aligned_u32(&uniforms,
408bf215546Sopenharmony_ci                                       v3d->prog.spill_size_per_thread);
409bf215546Sopenharmony_ci                        break;
410bf215546Sopenharmony_ci
411bf215546Sopenharmony_ci                case QUNIFORM_NUM_WORK_GROUPS:
412bf215546Sopenharmony_ci                        cl_aligned_u32(&uniforms,
413bf215546Sopenharmony_ci                                       v3d->compute_num_workgroups[data]);
414bf215546Sopenharmony_ci                        break;
415bf215546Sopenharmony_ci
416bf215546Sopenharmony_ci                case QUNIFORM_SHARED_OFFSET:
417bf215546Sopenharmony_ci                        cl_aligned_reloc(&job->indirect, &uniforms,
418bf215546Sopenharmony_ci                                         v3d->compute_shared_memory, 0);
419bf215546Sopenharmony_ci                        break;
420bf215546Sopenharmony_ci
421bf215546Sopenharmony_ci                case QUNIFORM_FB_LAYERS:
422bf215546Sopenharmony_ci                        cl_aligned_u32(&uniforms, job->num_layers);
423bf215546Sopenharmony_ci                        break;
424bf215546Sopenharmony_ci
425bf215546Sopenharmony_ci                default:
426bf215546Sopenharmony_ci                        assert(quniform_contents_is_texture_p0(uinfo->contents[i]));
427bf215546Sopenharmony_ci
428bf215546Sopenharmony_ci                        write_texture_p0(job, &uniforms, texstate,
429bf215546Sopenharmony_ci                                         uinfo->contents[i] -
430bf215546Sopenharmony_ci                                         QUNIFORM_TEXTURE_CONFIG_P0_0,
431bf215546Sopenharmony_ci                                         data);
432bf215546Sopenharmony_ci                        break;
433bf215546Sopenharmony_ci
434bf215546Sopenharmony_ci                }
435bf215546Sopenharmony_ci#if 0
436bf215546Sopenharmony_ci                uint32_t written_val = *((uint32_t *)uniforms - 1);
437bf215546Sopenharmony_ci                fprintf(stderr, "shader %p[%d]: 0x%08x / 0x%08x (%f) ",
438bf215546Sopenharmony_ci                        shader, i, __gen_address_offset(&uniform_stream) + i * 4,
439bf215546Sopenharmony_ci                        written_val, uif(written_val));
440bf215546Sopenharmony_ci                vir_dump_uniform(uinfo->contents[i], data);
441bf215546Sopenharmony_ci                fprintf(stderr, "\n");
442bf215546Sopenharmony_ci#endif
443bf215546Sopenharmony_ci        }
444bf215546Sopenharmony_ci
445bf215546Sopenharmony_ci        cl_end(&job->indirect, uniforms);
446bf215546Sopenharmony_ci
447bf215546Sopenharmony_ci        return uniform_stream;
448bf215546Sopenharmony_ci}
449bf215546Sopenharmony_ci
450bf215546Sopenharmony_civoid
451bf215546Sopenharmony_civ3d_set_shader_uniform_dirty_flags(struct v3d_compiled_shader *shader)
452bf215546Sopenharmony_ci{
453bf215546Sopenharmony_ci        uint32_t dirty = 0;
454bf215546Sopenharmony_ci
455bf215546Sopenharmony_ci        for (int i = 0; i < shader->prog_data.base->uniforms.count; i++) {
456bf215546Sopenharmony_ci                switch (shader->prog_data.base->uniforms.contents[i]) {
457bf215546Sopenharmony_ci                case QUNIFORM_CONSTANT:
458bf215546Sopenharmony_ci                        break;
459bf215546Sopenharmony_ci                case QUNIFORM_UNIFORM:
460bf215546Sopenharmony_ci                case QUNIFORM_UBO_ADDR:
461bf215546Sopenharmony_ci                        dirty |= V3D_DIRTY_CONSTBUF;
462bf215546Sopenharmony_ci                        break;
463bf215546Sopenharmony_ci
464bf215546Sopenharmony_ci                case QUNIFORM_VIEWPORT_X_SCALE:
465bf215546Sopenharmony_ci                case QUNIFORM_VIEWPORT_Y_SCALE:
466bf215546Sopenharmony_ci                case QUNIFORM_VIEWPORT_Z_OFFSET:
467bf215546Sopenharmony_ci                case QUNIFORM_VIEWPORT_Z_SCALE:
468bf215546Sopenharmony_ci                        dirty |= V3D_DIRTY_VIEWPORT;
469bf215546Sopenharmony_ci                        break;
470bf215546Sopenharmony_ci
471bf215546Sopenharmony_ci                case QUNIFORM_USER_CLIP_PLANE:
472bf215546Sopenharmony_ci                        dirty |= V3D_DIRTY_CLIP;
473bf215546Sopenharmony_ci                        break;
474bf215546Sopenharmony_ci
475bf215546Sopenharmony_ci                case QUNIFORM_TMU_CONFIG_P0:
476bf215546Sopenharmony_ci                case QUNIFORM_TMU_CONFIG_P1:
477bf215546Sopenharmony_ci                case QUNIFORM_TEXTURE_CONFIG_P1:
478bf215546Sopenharmony_ci                case QUNIFORM_TEXTURE_FIRST_LEVEL:
479bf215546Sopenharmony_ci                case QUNIFORM_TEXRECT_SCALE_X:
480bf215546Sopenharmony_ci                case QUNIFORM_TEXRECT_SCALE_Y:
481bf215546Sopenharmony_ci                case QUNIFORM_TEXTURE_WIDTH:
482bf215546Sopenharmony_ci                case QUNIFORM_TEXTURE_HEIGHT:
483bf215546Sopenharmony_ci                case QUNIFORM_TEXTURE_DEPTH:
484bf215546Sopenharmony_ci                case QUNIFORM_TEXTURE_ARRAY_SIZE:
485bf215546Sopenharmony_ci                case QUNIFORM_TEXTURE_LEVELS:
486bf215546Sopenharmony_ci                case QUNIFORM_SPILL_OFFSET:
487bf215546Sopenharmony_ci                case QUNIFORM_SPILL_SIZE_PER_THREAD:
488bf215546Sopenharmony_ci                        /* We could flag this on just the stage we're
489bf215546Sopenharmony_ci                         * compiling for, but it's not passed in.
490bf215546Sopenharmony_ci                         */
491bf215546Sopenharmony_ci                        dirty |= V3D_DIRTY_FRAGTEX | V3D_DIRTY_VERTTEX |
492bf215546Sopenharmony_ci                                 V3D_DIRTY_GEOMTEX | V3D_DIRTY_COMPTEX;
493bf215546Sopenharmony_ci                        break;
494bf215546Sopenharmony_ci
495bf215546Sopenharmony_ci                case QUNIFORM_SSBO_OFFSET:
496bf215546Sopenharmony_ci                case QUNIFORM_GET_SSBO_SIZE:
497bf215546Sopenharmony_ci                        dirty |= V3D_DIRTY_SSBO;
498bf215546Sopenharmony_ci                        break;
499bf215546Sopenharmony_ci
500bf215546Sopenharmony_ci                case QUNIFORM_IMAGE_TMU_CONFIG_P0:
501bf215546Sopenharmony_ci                case QUNIFORM_IMAGE_WIDTH:
502bf215546Sopenharmony_ci                case QUNIFORM_IMAGE_HEIGHT:
503bf215546Sopenharmony_ci                case QUNIFORM_IMAGE_DEPTH:
504bf215546Sopenharmony_ci                case QUNIFORM_IMAGE_ARRAY_SIZE:
505bf215546Sopenharmony_ci                        dirty |= V3D_DIRTY_SHADER_IMAGE;
506bf215546Sopenharmony_ci                        break;
507bf215546Sopenharmony_ci
508bf215546Sopenharmony_ci                case QUNIFORM_LINE_WIDTH:
509bf215546Sopenharmony_ci                case QUNIFORM_AA_LINE_WIDTH:
510bf215546Sopenharmony_ci                        dirty |= V3D_DIRTY_RASTERIZER;
511bf215546Sopenharmony_ci                        break;
512bf215546Sopenharmony_ci
513bf215546Sopenharmony_ci                case QUNIFORM_NUM_WORK_GROUPS:
514bf215546Sopenharmony_ci                case QUNIFORM_SHARED_OFFSET:
515bf215546Sopenharmony_ci                        /* Compute always recalculates uniforms. */
516bf215546Sopenharmony_ci                        break;
517bf215546Sopenharmony_ci
518bf215546Sopenharmony_ci                case QUNIFORM_FB_LAYERS:
519bf215546Sopenharmony_ci                        dirty |= V3D_DIRTY_FRAMEBUFFER;
520bf215546Sopenharmony_ci                        break;
521bf215546Sopenharmony_ci
522bf215546Sopenharmony_ci                default:
523bf215546Sopenharmony_ci                        assert(quniform_contents_is_texture_p0(shader->prog_data.base->uniforms.contents[i]));
524bf215546Sopenharmony_ci                        dirty |= V3D_DIRTY_FRAGTEX | V3D_DIRTY_VERTTEX |
525bf215546Sopenharmony_ci                                 V3D_DIRTY_GEOMTEX | V3D_DIRTY_COMPTEX;
526bf215546Sopenharmony_ci                        break;
527bf215546Sopenharmony_ci                }
528bf215546Sopenharmony_ci        }
529bf215546Sopenharmony_ci
530bf215546Sopenharmony_ci        shader->uniform_dirty_bits = dirty;
531bf215546Sopenharmony_ci}
532