1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2014-2017 Broadcom
3bf215546Sopenharmony_ci * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
8bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
10bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
13bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
14bf215546Sopenharmony_ci * Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22bf215546Sopenharmony_ci * IN THE SOFTWARE.
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#include "pipe/p_defines.h"
26bf215546Sopenharmony_ci#include "util/u_memory.h"
27bf215546Sopenharmony_ci#include "util/format/u_format.h"
28bf215546Sopenharmony_ci#include "util/u_inlines.h"
29bf215546Sopenharmony_ci#include "util/u_surface.h"
30bf215546Sopenharmony_ci#include "util/u_transfer_helper.h"
31bf215546Sopenharmony_ci#include "util/u_upload_mgr.h"
32bf215546Sopenharmony_ci#include "util/format/u_format_zs.h"
33bf215546Sopenharmony_ci#include "util/u_drm.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#include "drm-uapi/drm_fourcc.h"
36bf215546Sopenharmony_ci#include "v3d_screen.h"
37bf215546Sopenharmony_ci#include "v3d_context.h"
38bf215546Sopenharmony_ci#include "v3d_resource.h"
39bf215546Sopenharmony_ci#include "broadcom/cle/v3d_packet_v33_pack.h"
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_cistatic void
42bf215546Sopenharmony_civ3d_debug_resource_layout(struct v3d_resource *rsc, const char *caller)
43bf215546Sopenharmony_ci{
44bf215546Sopenharmony_ci        if (!(unlikely(V3D_DEBUG & V3D_DEBUG_SURFACE)))
45bf215546Sopenharmony_ci                return;
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci        struct pipe_resource *prsc = &rsc->base;
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci        if (prsc->target == PIPE_BUFFER) {
50bf215546Sopenharmony_ci                fprintf(stderr,
51bf215546Sopenharmony_ci                        "rsc %s %p (format %s), %dx%d buffer @0x%08x-0x%08x\n",
52bf215546Sopenharmony_ci                        caller, rsc,
53bf215546Sopenharmony_ci                        util_format_short_name(prsc->format),
54bf215546Sopenharmony_ci                        prsc->width0, prsc->height0,
55bf215546Sopenharmony_ci                        rsc->bo->offset,
56bf215546Sopenharmony_ci                        rsc->bo->offset + rsc->bo->size - 1);
57bf215546Sopenharmony_ci                return;
58bf215546Sopenharmony_ci        }
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci        static const char *const tiling_descriptions[] = {
61bf215546Sopenharmony_ci                [V3D_TILING_RASTER] = "R",
62bf215546Sopenharmony_ci                [V3D_TILING_LINEARTILE] = "LT",
63bf215546Sopenharmony_ci                [V3D_TILING_UBLINEAR_1_COLUMN] = "UB1",
64bf215546Sopenharmony_ci                [V3D_TILING_UBLINEAR_2_COLUMN] = "UB2",
65bf215546Sopenharmony_ci                [V3D_TILING_UIF_NO_XOR] = "UIF",
66bf215546Sopenharmony_ci                [V3D_TILING_UIF_XOR] = "UIF^",
67bf215546Sopenharmony_ci        };
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci        for (int i = 0; i <= prsc->last_level; i++) {
70bf215546Sopenharmony_ci                struct v3d_resource_slice *slice = &rsc->slices[i];
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci                int level_width = slice->stride / rsc->cpp;
73bf215546Sopenharmony_ci                int level_height = slice->padded_height;
74bf215546Sopenharmony_ci                int level_depth =
75bf215546Sopenharmony_ci                        u_minify(util_next_power_of_two(prsc->depth0), i);
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci                fprintf(stderr,
78bf215546Sopenharmony_ci                        "rsc %s %p (format %s), %dx%d: "
79bf215546Sopenharmony_ci                        "level %d (%s) %dx%dx%d -> %dx%dx%d, stride %d@0x%08x\n",
80bf215546Sopenharmony_ci                        caller, rsc,
81bf215546Sopenharmony_ci                        util_format_short_name(prsc->format),
82bf215546Sopenharmony_ci                        prsc->width0, prsc->height0,
83bf215546Sopenharmony_ci                        i, tiling_descriptions[slice->tiling],
84bf215546Sopenharmony_ci                        u_minify(prsc->width0, i),
85bf215546Sopenharmony_ci                        u_minify(prsc->height0, i),
86bf215546Sopenharmony_ci                        u_minify(prsc->depth0, i),
87bf215546Sopenharmony_ci                        level_width,
88bf215546Sopenharmony_ci                        level_height,
89bf215546Sopenharmony_ci                        level_depth,
90bf215546Sopenharmony_ci                        slice->stride,
91bf215546Sopenharmony_ci                        rsc->bo->offset + slice->offset);
92bf215546Sopenharmony_ci        }
93bf215546Sopenharmony_ci}
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_cistatic bool
96bf215546Sopenharmony_civ3d_resource_bo_alloc(struct v3d_resource *rsc)
97bf215546Sopenharmony_ci{
98bf215546Sopenharmony_ci        struct pipe_resource *prsc = &rsc->base;
99bf215546Sopenharmony_ci        struct pipe_screen *pscreen = prsc->screen;
100bf215546Sopenharmony_ci        struct v3d_bo *bo;
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci        bo = v3d_bo_alloc(v3d_screen(pscreen), rsc->size, "resource");
103bf215546Sopenharmony_ci        if (bo) {
104bf215546Sopenharmony_ci                v3d_bo_unreference(&rsc->bo);
105bf215546Sopenharmony_ci                rsc->bo = bo;
106bf215546Sopenharmony_ci                rsc->serial_id++;
107bf215546Sopenharmony_ci                v3d_debug_resource_layout(rsc, "alloc");
108bf215546Sopenharmony_ci                return true;
109bf215546Sopenharmony_ci        } else {
110bf215546Sopenharmony_ci                return false;
111bf215546Sopenharmony_ci        }
112bf215546Sopenharmony_ci}
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_cistatic void
115bf215546Sopenharmony_civ3d_resource_transfer_unmap(struct pipe_context *pctx,
116bf215546Sopenharmony_ci                            struct pipe_transfer *ptrans)
117bf215546Sopenharmony_ci{
118bf215546Sopenharmony_ci        struct v3d_context *v3d = v3d_context(pctx);
119bf215546Sopenharmony_ci        struct v3d_transfer *trans = v3d_transfer(ptrans);
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci        if (trans->map) {
122bf215546Sopenharmony_ci                struct v3d_resource *rsc = v3d_resource(ptrans->resource);
123bf215546Sopenharmony_ci                struct v3d_resource_slice *slice = &rsc->slices[ptrans->level];
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci                if (ptrans->usage & PIPE_MAP_WRITE) {
126bf215546Sopenharmony_ci                        for (int z = 0; z < ptrans->box.depth; z++) {
127bf215546Sopenharmony_ci                                void *dst = rsc->bo->map +
128bf215546Sopenharmony_ci                                        v3d_layer_offset(&rsc->base,
129bf215546Sopenharmony_ci                                                         ptrans->level,
130bf215546Sopenharmony_ci                                                         ptrans->box.z + z);
131bf215546Sopenharmony_ci                                v3d_store_tiled_image(dst,
132bf215546Sopenharmony_ci                                                      slice->stride,
133bf215546Sopenharmony_ci                                                      (trans->map +
134bf215546Sopenharmony_ci                                                       ptrans->stride *
135bf215546Sopenharmony_ci                                                       ptrans->box.height * z),
136bf215546Sopenharmony_ci                                                      ptrans->stride,
137bf215546Sopenharmony_ci                                                      slice->tiling, rsc->cpp,
138bf215546Sopenharmony_ci                                                      slice->padded_height,
139bf215546Sopenharmony_ci                                                      &ptrans->box);
140bf215546Sopenharmony_ci                        }
141bf215546Sopenharmony_ci                }
142bf215546Sopenharmony_ci                free(trans->map);
143bf215546Sopenharmony_ci        }
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci        pipe_resource_reference(&ptrans->resource, NULL);
146bf215546Sopenharmony_ci        slab_free(&v3d->transfer_pool, ptrans);
147bf215546Sopenharmony_ci}
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_cistatic void
150bf215546Sopenharmony_cirebind_sampler_views(struct v3d_context *v3d,
151bf215546Sopenharmony_ci                     struct v3d_resource *rsc)
152bf215546Sopenharmony_ci{
153bf215546Sopenharmony_ci        for (int st = 0; st < PIPE_SHADER_TYPES; st++) {
154bf215546Sopenharmony_ci                struct v3d_texture_stateobj *tex = v3d->tex + st;
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci                for (unsigned i = 0; i < tex->num_textures; i++) {
157bf215546Sopenharmony_ci                        struct pipe_sampler_view *psview = tex->textures[i];
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_ci                        if (psview->texture != &rsc->base)
160bf215546Sopenharmony_ci                                continue;
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_ci                        struct v3d_sampler_view *sview =
163bf215546Sopenharmony_ci                                v3d_sampler_view(psview);
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci                        v3d_create_texture_shader_state_bo(v3d, sview);
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci                        v3d_flag_dirty_sampler_state(v3d, st);
168bf215546Sopenharmony_ci                }
169bf215546Sopenharmony_ci        }
170bf215546Sopenharmony_ci}
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_cistatic void
173bf215546Sopenharmony_civ3d_map_usage_prep(struct pipe_context *pctx,
174bf215546Sopenharmony_ci                   struct pipe_resource *prsc,
175bf215546Sopenharmony_ci                   unsigned usage)
176bf215546Sopenharmony_ci{
177bf215546Sopenharmony_ci        struct v3d_context *v3d = v3d_context(pctx);
178bf215546Sopenharmony_ci        struct v3d_resource *rsc = v3d_resource(prsc);
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_ci        if (usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE) {
181bf215546Sopenharmony_ci                if (v3d_resource_bo_alloc(rsc)) {
182bf215546Sopenharmony_ci                        /* If it might be bound as one of our vertex buffers
183bf215546Sopenharmony_ci                         * or UBOs, make sure we re-emit vertex buffer state
184bf215546Sopenharmony_ci                         * or uniforms.
185bf215546Sopenharmony_ci                         */
186bf215546Sopenharmony_ci                        if (prsc->bind & PIPE_BIND_VERTEX_BUFFER)
187bf215546Sopenharmony_ci                                v3d->dirty |= V3D_DIRTY_VTXBUF;
188bf215546Sopenharmony_ci                        if (prsc->bind & PIPE_BIND_CONSTANT_BUFFER)
189bf215546Sopenharmony_ci                                v3d->dirty |= V3D_DIRTY_CONSTBUF;
190bf215546Sopenharmony_ci                        /* Since we are changing the texture BO we need to
191bf215546Sopenharmony_ci                         * update any bound samplers to point to the new
192bf215546Sopenharmony_ci                         * BO. Notice we can have samplers that are not
193bf215546Sopenharmony_ci                         * currently bound to the state that won't be
194bf215546Sopenharmony_ci                         * updated. These will be fixed when they are bound in
195bf215546Sopenharmony_ci                         * v3d_set_sampler_views.
196bf215546Sopenharmony_ci                         */
197bf215546Sopenharmony_ci                        if (prsc->bind & PIPE_BIND_SAMPLER_VIEW)
198bf215546Sopenharmony_ci                                rebind_sampler_views(v3d, rsc);
199bf215546Sopenharmony_ci                } else {
200bf215546Sopenharmony_ci                        /* If we failed to reallocate, flush users so that we
201bf215546Sopenharmony_ci                         * don't violate any syncing requirements.
202bf215546Sopenharmony_ci                         */
203bf215546Sopenharmony_ci                        v3d_flush_jobs_reading_resource(v3d, prsc,
204bf215546Sopenharmony_ci                                                        V3D_FLUSH_DEFAULT,
205bf215546Sopenharmony_ci                                                        false);
206bf215546Sopenharmony_ci                }
207bf215546Sopenharmony_ci        } else if (!(usage & PIPE_MAP_UNSYNCHRONIZED)) {
208bf215546Sopenharmony_ci                /* If we're writing and the buffer is being used by the CL, we
209bf215546Sopenharmony_ci                 * have to flush the CL first.  If we're only reading, we need
210bf215546Sopenharmony_ci                 * to flush if the CL has written our buffer.
211bf215546Sopenharmony_ci                 */
212bf215546Sopenharmony_ci                if (usage & PIPE_MAP_WRITE) {
213bf215546Sopenharmony_ci                        v3d_flush_jobs_reading_resource(v3d, prsc,
214bf215546Sopenharmony_ci                                                        V3D_FLUSH_ALWAYS,
215bf215546Sopenharmony_ci                                                        false);
216bf215546Sopenharmony_ci                } else {
217bf215546Sopenharmony_ci                        v3d_flush_jobs_writing_resource(v3d, prsc,
218bf215546Sopenharmony_ci                                                        V3D_FLUSH_ALWAYS,
219bf215546Sopenharmony_ci                                                        false);
220bf215546Sopenharmony_ci                }
221bf215546Sopenharmony_ci        }
222bf215546Sopenharmony_ci
223bf215546Sopenharmony_ci        if (usage & PIPE_MAP_WRITE) {
224bf215546Sopenharmony_ci                rsc->writes++;
225bf215546Sopenharmony_ci                rsc->initialized_buffers = ~0;
226bf215546Sopenharmony_ci        }
227bf215546Sopenharmony_ci}
228bf215546Sopenharmony_ci
229bf215546Sopenharmony_cistatic void *
230bf215546Sopenharmony_civ3d_resource_transfer_map(struct pipe_context *pctx,
231bf215546Sopenharmony_ci                          struct pipe_resource *prsc,
232bf215546Sopenharmony_ci                          unsigned level, unsigned usage,
233bf215546Sopenharmony_ci                          const struct pipe_box *box,
234bf215546Sopenharmony_ci                          struct pipe_transfer **pptrans)
235bf215546Sopenharmony_ci{
236bf215546Sopenharmony_ci        struct v3d_context *v3d = v3d_context(pctx);
237bf215546Sopenharmony_ci        struct v3d_resource *rsc = v3d_resource(prsc);
238bf215546Sopenharmony_ci        struct v3d_transfer *trans;
239bf215546Sopenharmony_ci        struct pipe_transfer *ptrans;
240bf215546Sopenharmony_ci        enum pipe_format format = prsc->format;
241bf215546Sopenharmony_ci        char *buf;
242bf215546Sopenharmony_ci
243bf215546Sopenharmony_ci        /* MSAA maps should have been handled by u_transfer_helper. */
244bf215546Sopenharmony_ci        assert(prsc->nr_samples <= 1);
245bf215546Sopenharmony_ci
246bf215546Sopenharmony_ci        /* Upgrade DISCARD_RANGE to WHOLE_RESOURCE if the whole resource is
247bf215546Sopenharmony_ci         * being mapped.
248bf215546Sopenharmony_ci         */
249bf215546Sopenharmony_ci        if ((usage & PIPE_MAP_DISCARD_RANGE) &&
250bf215546Sopenharmony_ci            !(usage & PIPE_MAP_UNSYNCHRONIZED) &&
251bf215546Sopenharmony_ci            !(prsc->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT) &&
252bf215546Sopenharmony_ci            prsc->last_level == 0 &&
253bf215546Sopenharmony_ci            prsc->width0 == box->width &&
254bf215546Sopenharmony_ci            prsc->height0 == box->height &&
255bf215546Sopenharmony_ci            prsc->depth0 == box->depth &&
256bf215546Sopenharmony_ci            prsc->array_size == 1 &&
257bf215546Sopenharmony_ci            rsc->bo->private) {
258bf215546Sopenharmony_ci                usage |= PIPE_MAP_DISCARD_WHOLE_RESOURCE;
259bf215546Sopenharmony_ci        }
260bf215546Sopenharmony_ci
261bf215546Sopenharmony_ci        v3d_map_usage_prep(pctx, prsc, usage);
262bf215546Sopenharmony_ci
263bf215546Sopenharmony_ci        trans = slab_zalloc(&v3d->transfer_pool);
264bf215546Sopenharmony_ci        if (!trans)
265bf215546Sopenharmony_ci                return NULL;
266bf215546Sopenharmony_ci
267bf215546Sopenharmony_ci        /* XXX: Handle DONTBLOCK, DISCARD_RANGE, PERSISTENT, COHERENT. */
268bf215546Sopenharmony_ci
269bf215546Sopenharmony_ci        ptrans = &trans->base;
270bf215546Sopenharmony_ci
271bf215546Sopenharmony_ci        pipe_resource_reference(&ptrans->resource, prsc);
272bf215546Sopenharmony_ci        ptrans->level = level;
273bf215546Sopenharmony_ci        ptrans->usage = usage;
274bf215546Sopenharmony_ci        ptrans->box = *box;
275bf215546Sopenharmony_ci
276bf215546Sopenharmony_ci        /* Note that the current kernel implementation is synchronous, so no
277bf215546Sopenharmony_ci         * need to do syncing stuff here yet.
278bf215546Sopenharmony_ci         */
279bf215546Sopenharmony_ci
280bf215546Sopenharmony_ci        if (usage & PIPE_MAP_UNSYNCHRONIZED)
281bf215546Sopenharmony_ci                buf = v3d_bo_map_unsynchronized(rsc->bo);
282bf215546Sopenharmony_ci        else
283bf215546Sopenharmony_ci                buf = v3d_bo_map(rsc->bo);
284bf215546Sopenharmony_ci        if (!buf) {
285bf215546Sopenharmony_ci                fprintf(stderr, "Failed to map bo\n");
286bf215546Sopenharmony_ci                goto fail;
287bf215546Sopenharmony_ci        }
288bf215546Sopenharmony_ci
289bf215546Sopenharmony_ci        *pptrans = ptrans;
290bf215546Sopenharmony_ci
291bf215546Sopenharmony_ci        /* Our load/store routines work on entire compressed blocks. */
292bf215546Sopenharmony_ci        u_box_pixels_to_blocks(&ptrans->box, &ptrans->box, format);
293bf215546Sopenharmony_ci
294bf215546Sopenharmony_ci        struct v3d_resource_slice *slice = &rsc->slices[level];
295bf215546Sopenharmony_ci        if (rsc->tiled) {
296bf215546Sopenharmony_ci                /* No direct mappings of tiled, since we need to manually
297bf215546Sopenharmony_ci                 * tile/untile.
298bf215546Sopenharmony_ci                 */
299bf215546Sopenharmony_ci                if (usage & PIPE_MAP_DIRECTLY)
300bf215546Sopenharmony_ci                        return NULL;
301bf215546Sopenharmony_ci
302bf215546Sopenharmony_ci                ptrans->stride = ptrans->box.width * rsc->cpp;
303bf215546Sopenharmony_ci                ptrans->layer_stride = ptrans->stride * ptrans->box.height;
304bf215546Sopenharmony_ci
305bf215546Sopenharmony_ci                trans->map = malloc(ptrans->layer_stride * ptrans->box.depth);
306bf215546Sopenharmony_ci
307bf215546Sopenharmony_ci                if (usage & PIPE_MAP_READ) {
308bf215546Sopenharmony_ci                        for (int z = 0; z < ptrans->box.depth; z++) {
309bf215546Sopenharmony_ci                                void *src = rsc->bo->map +
310bf215546Sopenharmony_ci                                        v3d_layer_offset(&rsc->base,
311bf215546Sopenharmony_ci                                                         ptrans->level,
312bf215546Sopenharmony_ci                                                         ptrans->box.z + z);
313bf215546Sopenharmony_ci                                v3d_load_tiled_image((trans->map +
314bf215546Sopenharmony_ci                                                      ptrans->stride *
315bf215546Sopenharmony_ci                                                      ptrans->box.height * z),
316bf215546Sopenharmony_ci                                                     ptrans->stride,
317bf215546Sopenharmony_ci                                                     src,
318bf215546Sopenharmony_ci                                                     slice->stride,
319bf215546Sopenharmony_ci                                                     slice->tiling, rsc->cpp,
320bf215546Sopenharmony_ci                                                     slice->padded_height,
321bf215546Sopenharmony_ci                                             &ptrans->box);
322bf215546Sopenharmony_ci                        }
323bf215546Sopenharmony_ci                }
324bf215546Sopenharmony_ci                return trans->map;
325bf215546Sopenharmony_ci        } else {
326bf215546Sopenharmony_ci                ptrans->stride = slice->stride;
327bf215546Sopenharmony_ci                ptrans->layer_stride = rsc->cube_map_stride;
328bf215546Sopenharmony_ci
329bf215546Sopenharmony_ci                return buf + slice->offset +
330bf215546Sopenharmony_ci                        ptrans->box.y * ptrans->stride +
331bf215546Sopenharmony_ci                        ptrans->box.x * rsc->cpp +
332bf215546Sopenharmony_ci                        ptrans->box.z * rsc->cube_map_stride;
333bf215546Sopenharmony_ci        }
334bf215546Sopenharmony_ci
335bf215546Sopenharmony_ci
336bf215546Sopenharmony_cifail:
337bf215546Sopenharmony_ci        v3d_resource_transfer_unmap(pctx, ptrans);
338bf215546Sopenharmony_ci        return NULL;
339bf215546Sopenharmony_ci}
340bf215546Sopenharmony_ci
341bf215546Sopenharmony_cistatic void
342bf215546Sopenharmony_civ3d_texture_subdata(struct pipe_context *pctx,
343bf215546Sopenharmony_ci                    struct pipe_resource *prsc,
344bf215546Sopenharmony_ci                    unsigned level,
345bf215546Sopenharmony_ci                    unsigned usage,
346bf215546Sopenharmony_ci                    const struct pipe_box *box,
347bf215546Sopenharmony_ci                    const void *data,
348bf215546Sopenharmony_ci                    unsigned stride,
349bf215546Sopenharmony_ci                    unsigned layer_stride)
350bf215546Sopenharmony_ci{
351bf215546Sopenharmony_ci        struct v3d_resource *rsc = v3d_resource(prsc);
352bf215546Sopenharmony_ci        struct v3d_resource_slice *slice = &rsc->slices[level];
353bf215546Sopenharmony_ci
354bf215546Sopenharmony_ci        /* For a direct mapping, we can just take the u_transfer path. */
355bf215546Sopenharmony_ci        if (!rsc->tiled) {
356bf215546Sopenharmony_ci                return u_default_texture_subdata(pctx, prsc, level, usage, box,
357bf215546Sopenharmony_ci                                                 data, stride, layer_stride);
358bf215546Sopenharmony_ci        }
359bf215546Sopenharmony_ci
360bf215546Sopenharmony_ci        /* Otherwise, map and store the texture data directly into the tiled
361bf215546Sopenharmony_ci         * texture.  Note that gallium's texture_subdata may be called with
362bf215546Sopenharmony_ci         * obvious usage flags missing!
363bf215546Sopenharmony_ci         */
364bf215546Sopenharmony_ci        v3d_map_usage_prep(pctx, prsc, usage | (PIPE_MAP_WRITE |
365bf215546Sopenharmony_ci                                                PIPE_MAP_DISCARD_RANGE));
366bf215546Sopenharmony_ci
367bf215546Sopenharmony_ci        void *buf;
368bf215546Sopenharmony_ci        if (usage & PIPE_MAP_UNSYNCHRONIZED)
369bf215546Sopenharmony_ci                buf = v3d_bo_map_unsynchronized(rsc->bo);
370bf215546Sopenharmony_ci        else
371bf215546Sopenharmony_ci                buf = v3d_bo_map(rsc->bo);
372bf215546Sopenharmony_ci
373bf215546Sopenharmony_ci        for (int i = 0; i < box->depth; i++) {
374bf215546Sopenharmony_ci                v3d_store_tiled_image(buf +
375bf215546Sopenharmony_ci                                      v3d_layer_offset(&rsc->base,
376bf215546Sopenharmony_ci                                                       level,
377bf215546Sopenharmony_ci                                                       box->z + i),
378bf215546Sopenharmony_ci                                      slice->stride,
379bf215546Sopenharmony_ci                                      (void *)data + layer_stride * i,
380bf215546Sopenharmony_ci                                      stride,
381bf215546Sopenharmony_ci                                      slice->tiling, rsc->cpp, slice->padded_height,
382bf215546Sopenharmony_ci                                      box);
383bf215546Sopenharmony_ci        }
384bf215546Sopenharmony_ci}
385bf215546Sopenharmony_ci
386bf215546Sopenharmony_cistatic void
387bf215546Sopenharmony_civ3d_resource_destroy(struct pipe_screen *pscreen,
388bf215546Sopenharmony_ci                     struct pipe_resource *prsc)
389bf215546Sopenharmony_ci{
390bf215546Sopenharmony_ci        struct v3d_screen *screen = v3d_screen(pscreen);
391bf215546Sopenharmony_ci        struct v3d_resource *rsc = v3d_resource(prsc);
392bf215546Sopenharmony_ci
393bf215546Sopenharmony_ci        if (rsc->scanout)
394bf215546Sopenharmony_ci                renderonly_scanout_destroy(rsc->scanout, screen->ro);
395bf215546Sopenharmony_ci
396bf215546Sopenharmony_ci        v3d_bo_unreference(&rsc->bo);
397bf215546Sopenharmony_ci        free(rsc);
398bf215546Sopenharmony_ci}
399bf215546Sopenharmony_ci
400bf215546Sopenharmony_cistatic uint64_t
401bf215546Sopenharmony_civ3d_resource_modifier(struct v3d_resource *rsc)
402bf215546Sopenharmony_ci{
403bf215546Sopenharmony_ci        if (rsc->tiled) {
404bf215546Sopenharmony_ci                /* A shared tiled buffer should always be allocated as UIF,
405bf215546Sopenharmony_ci                 * not UBLINEAR or LT.
406bf215546Sopenharmony_ci                 */
407bf215546Sopenharmony_ci                assert(rsc->slices[0].tiling == V3D_TILING_UIF_XOR ||
408bf215546Sopenharmony_ci                       rsc->slices[0].tiling == V3D_TILING_UIF_NO_XOR);
409bf215546Sopenharmony_ci                return DRM_FORMAT_MOD_BROADCOM_UIF;
410bf215546Sopenharmony_ci        } else {
411bf215546Sopenharmony_ci                return DRM_FORMAT_MOD_LINEAR;
412bf215546Sopenharmony_ci        }
413bf215546Sopenharmony_ci}
414bf215546Sopenharmony_ci
415bf215546Sopenharmony_cistatic bool
416bf215546Sopenharmony_civ3d_resource_get_handle(struct pipe_screen *pscreen,
417bf215546Sopenharmony_ci                        struct pipe_context *pctx,
418bf215546Sopenharmony_ci                        struct pipe_resource *prsc,
419bf215546Sopenharmony_ci                        struct winsys_handle *whandle,
420bf215546Sopenharmony_ci                        unsigned usage)
421bf215546Sopenharmony_ci{
422bf215546Sopenharmony_ci        struct v3d_screen *screen = v3d_screen(pscreen);
423bf215546Sopenharmony_ci        struct v3d_resource *rsc = v3d_resource(prsc);
424bf215546Sopenharmony_ci        struct v3d_bo *bo = rsc->bo;
425bf215546Sopenharmony_ci
426bf215546Sopenharmony_ci        whandle->stride = rsc->slices[0].stride;
427bf215546Sopenharmony_ci        whandle->offset = 0;
428bf215546Sopenharmony_ci        whandle->modifier = v3d_resource_modifier(rsc);
429bf215546Sopenharmony_ci
430bf215546Sopenharmony_ci        /* If we're passing some reference to our BO out to some other part of
431bf215546Sopenharmony_ci         * the system, then we can't do any optimizations about only us being
432bf215546Sopenharmony_ci         * the ones seeing it (like BO caching).
433bf215546Sopenharmony_ci         */
434bf215546Sopenharmony_ci        bo->private = false;
435bf215546Sopenharmony_ci
436bf215546Sopenharmony_ci        switch (whandle->type) {
437bf215546Sopenharmony_ci        case WINSYS_HANDLE_TYPE_SHARED:
438bf215546Sopenharmony_ci                return v3d_bo_flink(bo, &whandle->handle);
439bf215546Sopenharmony_ci        case WINSYS_HANDLE_TYPE_KMS:
440bf215546Sopenharmony_ci                if (screen->ro) {
441bf215546Sopenharmony_ci                        if (renderonly_get_handle(rsc->scanout, whandle)) {
442bf215546Sopenharmony_ci                                whandle->stride = rsc->slices[0].stride;
443bf215546Sopenharmony_ci                                return true;
444bf215546Sopenharmony_ci                        }
445bf215546Sopenharmony_ci                        return false;
446bf215546Sopenharmony_ci                }
447bf215546Sopenharmony_ci                whandle->handle = bo->handle;
448bf215546Sopenharmony_ci                return true;
449bf215546Sopenharmony_ci        case WINSYS_HANDLE_TYPE_FD:
450bf215546Sopenharmony_ci                whandle->handle = v3d_bo_get_dmabuf(bo);
451bf215546Sopenharmony_ci                return whandle->handle != -1;
452bf215546Sopenharmony_ci        }
453bf215546Sopenharmony_ci
454bf215546Sopenharmony_ci        return false;
455bf215546Sopenharmony_ci}
456bf215546Sopenharmony_ci
457bf215546Sopenharmony_cistatic bool
458bf215546Sopenharmony_civ3d_resource_get_param(struct pipe_screen *pscreen,
459bf215546Sopenharmony_ci                       struct pipe_context *pctx, struct pipe_resource *prsc,
460bf215546Sopenharmony_ci                       unsigned plane, unsigned layer, unsigned level,
461bf215546Sopenharmony_ci                       enum pipe_resource_param param,
462bf215546Sopenharmony_ci                       unsigned usage, uint64_t *value)
463bf215546Sopenharmony_ci{
464bf215546Sopenharmony_ci        struct v3d_resource *rsc = v3d_resource(prsc);
465bf215546Sopenharmony_ci
466bf215546Sopenharmony_ci        switch (param) {
467bf215546Sopenharmony_ci        case PIPE_RESOURCE_PARAM_STRIDE:
468bf215546Sopenharmony_ci                *value = rsc->slices[level].stride;
469bf215546Sopenharmony_ci                return true;
470bf215546Sopenharmony_ci        case PIPE_RESOURCE_PARAM_OFFSET:
471bf215546Sopenharmony_ci                *value = 0;
472bf215546Sopenharmony_ci                return true;
473bf215546Sopenharmony_ci        case PIPE_RESOURCE_PARAM_MODIFIER:
474bf215546Sopenharmony_ci                *value = v3d_resource_modifier(rsc);
475bf215546Sopenharmony_ci                return true;
476bf215546Sopenharmony_ci        default:
477bf215546Sopenharmony_ci                return false;
478bf215546Sopenharmony_ci        }
479bf215546Sopenharmony_ci}
480bf215546Sopenharmony_ci
481bf215546Sopenharmony_ci#define PAGE_UB_ROWS (V3D_UIFCFG_PAGE_SIZE / V3D_UIFBLOCK_ROW_SIZE)
482bf215546Sopenharmony_ci#define PAGE_UB_ROWS_TIMES_1_5 ((PAGE_UB_ROWS * 3) >> 1)
483bf215546Sopenharmony_ci#define PAGE_CACHE_UB_ROWS (V3D_PAGE_CACHE_SIZE / V3D_UIFBLOCK_ROW_SIZE)
484bf215546Sopenharmony_ci#define PAGE_CACHE_MINUS_1_5_UB_ROWS (PAGE_CACHE_UB_ROWS - PAGE_UB_ROWS_TIMES_1_5)
485bf215546Sopenharmony_ci
486bf215546Sopenharmony_ci/**
487bf215546Sopenharmony_ci * Computes the HW's UIFblock padding for a given height/cpp.
488bf215546Sopenharmony_ci *
489bf215546Sopenharmony_ci * The goal of the padding is to keep pages of the same color (bank number) at
490bf215546Sopenharmony_ci * least half a page away from each other vertically when crossing between
491bf215546Sopenharmony_ci * between columns of UIF blocks.
492bf215546Sopenharmony_ci */
493bf215546Sopenharmony_cistatic uint32_t
494bf215546Sopenharmony_civ3d_get_ub_pad(struct v3d_resource *rsc, uint32_t height)
495bf215546Sopenharmony_ci{
496bf215546Sopenharmony_ci        uint32_t utile_h = v3d_utile_height(rsc->cpp);
497bf215546Sopenharmony_ci        uint32_t uif_block_h = utile_h * 2;
498bf215546Sopenharmony_ci        uint32_t height_ub = height / uif_block_h;
499bf215546Sopenharmony_ci
500bf215546Sopenharmony_ci        uint32_t height_offset_in_pc = height_ub % PAGE_CACHE_UB_ROWS;
501bf215546Sopenharmony_ci
502bf215546Sopenharmony_ci        /* For the perfectly-aligned-for-UIF-XOR case, don't add any pad. */
503bf215546Sopenharmony_ci        if (height_offset_in_pc == 0)
504bf215546Sopenharmony_ci                return 0;
505bf215546Sopenharmony_ci
506bf215546Sopenharmony_ci        /* Try padding up to where we're offset by at least half a page. */
507bf215546Sopenharmony_ci        if (height_offset_in_pc < PAGE_UB_ROWS_TIMES_1_5) {
508bf215546Sopenharmony_ci                /* If we fit entirely in the page cache, don't pad. */
509bf215546Sopenharmony_ci                if (height_ub < PAGE_CACHE_UB_ROWS)
510bf215546Sopenharmony_ci                        return 0;
511bf215546Sopenharmony_ci                else
512bf215546Sopenharmony_ci                        return PAGE_UB_ROWS_TIMES_1_5 - height_offset_in_pc;
513bf215546Sopenharmony_ci        }
514bf215546Sopenharmony_ci
515bf215546Sopenharmony_ci        /* If we're close to being aligned to page cache size, then round up
516bf215546Sopenharmony_ci         * and rely on XOR.
517bf215546Sopenharmony_ci         */
518bf215546Sopenharmony_ci        if (height_offset_in_pc > PAGE_CACHE_MINUS_1_5_UB_ROWS)
519bf215546Sopenharmony_ci                return PAGE_CACHE_UB_ROWS - height_offset_in_pc;
520bf215546Sopenharmony_ci
521bf215546Sopenharmony_ci        /* Otherwise, we're far enough away (top and bottom) to not need any
522bf215546Sopenharmony_ci         * padding.
523bf215546Sopenharmony_ci         */
524bf215546Sopenharmony_ci        return 0;
525bf215546Sopenharmony_ci}
526bf215546Sopenharmony_ci
527bf215546Sopenharmony_cistatic void
528bf215546Sopenharmony_civ3d_setup_slices(struct v3d_resource *rsc, uint32_t winsys_stride,
529bf215546Sopenharmony_ci                 bool uif_top)
530bf215546Sopenharmony_ci{
531bf215546Sopenharmony_ci        struct pipe_resource *prsc = &rsc->base;
532bf215546Sopenharmony_ci        uint32_t width = prsc->width0;
533bf215546Sopenharmony_ci        uint32_t height = prsc->height0;
534bf215546Sopenharmony_ci        uint32_t depth = prsc->depth0;
535bf215546Sopenharmony_ci        /* Note that power-of-two padding is based on level 1.  These are not
536bf215546Sopenharmony_ci         * equivalent to just util_next_power_of_two(dimension), because at a
537bf215546Sopenharmony_ci         * level 0 dimension of 9, the level 1 power-of-two padded value is 4,
538bf215546Sopenharmony_ci         * not 8.
539bf215546Sopenharmony_ci         */
540bf215546Sopenharmony_ci        uint32_t pot_width = 2 * util_next_power_of_two(u_minify(width, 1));
541bf215546Sopenharmony_ci        uint32_t pot_height = 2 * util_next_power_of_two(u_minify(height, 1));
542bf215546Sopenharmony_ci        uint32_t pot_depth = 2 * util_next_power_of_two(u_minify(depth, 1));
543bf215546Sopenharmony_ci        uint32_t offset = 0;
544bf215546Sopenharmony_ci        uint32_t utile_w = v3d_utile_width(rsc->cpp);
545bf215546Sopenharmony_ci        uint32_t utile_h = v3d_utile_height(rsc->cpp);
546bf215546Sopenharmony_ci        uint32_t uif_block_w = utile_w * 2;
547bf215546Sopenharmony_ci        uint32_t uif_block_h = utile_h * 2;
548bf215546Sopenharmony_ci        uint32_t block_width = util_format_get_blockwidth(prsc->format);
549bf215546Sopenharmony_ci        uint32_t block_height = util_format_get_blockheight(prsc->format);
550bf215546Sopenharmony_ci        bool msaa = prsc->nr_samples > 1;
551bf215546Sopenharmony_ci
552bf215546Sopenharmony_ci        /* MSAA textures/renderbuffers are always laid out as single-level
553bf215546Sopenharmony_ci         * UIF.
554bf215546Sopenharmony_ci         */
555bf215546Sopenharmony_ci        uif_top |= msaa;
556bf215546Sopenharmony_ci
557bf215546Sopenharmony_ci        /* Check some easy mistakes to make in a resource_create() call that
558bf215546Sopenharmony_ci         * will break our setup.
559bf215546Sopenharmony_ci         */
560bf215546Sopenharmony_ci        assert(prsc->array_size != 0);
561bf215546Sopenharmony_ci        assert(prsc->depth0 != 0);
562bf215546Sopenharmony_ci
563bf215546Sopenharmony_ci        for (int i = prsc->last_level; i >= 0; i--) {
564bf215546Sopenharmony_ci                struct v3d_resource_slice *slice = &rsc->slices[i];
565bf215546Sopenharmony_ci
566bf215546Sopenharmony_ci                uint32_t level_width, level_height, level_depth;
567bf215546Sopenharmony_ci                if (i < 2) {
568bf215546Sopenharmony_ci                        level_width = u_minify(width, i);
569bf215546Sopenharmony_ci                        level_height = u_minify(height, i);
570bf215546Sopenharmony_ci                } else {
571bf215546Sopenharmony_ci                        level_width = u_minify(pot_width, i);
572bf215546Sopenharmony_ci                        level_height = u_minify(pot_height, i);
573bf215546Sopenharmony_ci                }
574bf215546Sopenharmony_ci                if (i < 1)
575bf215546Sopenharmony_ci                        level_depth = u_minify(depth, i);
576bf215546Sopenharmony_ci                else
577bf215546Sopenharmony_ci                        level_depth = u_minify(pot_depth, i);
578bf215546Sopenharmony_ci
579bf215546Sopenharmony_ci                if (msaa) {
580bf215546Sopenharmony_ci                        level_width *= 2;
581bf215546Sopenharmony_ci                        level_height *= 2;
582bf215546Sopenharmony_ci                }
583bf215546Sopenharmony_ci
584bf215546Sopenharmony_ci                level_width = DIV_ROUND_UP(level_width, block_width);
585bf215546Sopenharmony_ci                level_height = DIV_ROUND_UP(level_height, block_height);
586bf215546Sopenharmony_ci
587bf215546Sopenharmony_ci                if (!rsc->tiled) {
588bf215546Sopenharmony_ci                        slice->tiling = V3D_TILING_RASTER;
589bf215546Sopenharmony_ci                        if (prsc->target == PIPE_TEXTURE_1D)
590bf215546Sopenharmony_ci                                level_width = align(level_width, 64 / rsc->cpp);
591bf215546Sopenharmony_ci                } else {
592bf215546Sopenharmony_ci                        if ((i != 0 || !uif_top) &&
593bf215546Sopenharmony_ci                            (level_width <= utile_w ||
594bf215546Sopenharmony_ci                             level_height <= utile_h)) {
595bf215546Sopenharmony_ci                                slice->tiling = V3D_TILING_LINEARTILE;
596bf215546Sopenharmony_ci                                level_width = align(level_width, utile_w);
597bf215546Sopenharmony_ci                                level_height = align(level_height, utile_h);
598bf215546Sopenharmony_ci                        } else if ((i != 0 || !uif_top) &&
599bf215546Sopenharmony_ci                                   level_width <= uif_block_w) {
600bf215546Sopenharmony_ci                                slice->tiling = V3D_TILING_UBLINEAR_1_COLUMN;
601bf215546Sopenharmony_ci                                level_width = align(level_width, uif_block_w);
602bf215546Sopenharmony_ci                                level_height = align(level_height, uif_block_h);
603bf215546Sopenharmony_ci                        } else if ((i != 0 || !uif_top) &&
604bf215546Sopenharmony_ci                                   level_width <= 2 * uif_block_w) {
605bf215546Sopenharmony_ci                                slice->tiling = V3D_TILING_UBLINEAR_2_COLUMN;
606bf215546Sopenharmony_ci                                level_width = align(level_width, 2 * uif_block_w);
607bf215546Sopenharmony_ci                                level_height = align(level_height, uif_block_h);
608bf215546Sopenharmony_ci                        } else {
609bf215546Sopenharmony_ci                                /* We align the width to a 4-block column of
610bf215546Sopenharmony_ci                                 * UIF blocks, but we only align height to UIF
611bf215546Sopenharmony_ci                                 * blocks.
612bf215546Sopenharmony_ci                                 */
613bf215546Sopenharmony_ci                                level_width = align(level_width,
614bf215546Sopenharmony_ci                                                    4 * uif_block_w);
615bf215546Sopenharmony_ci                                level_height = align(level_height,
616bf215546Sopenharmony_ci                                                     uif_block_h);
617bf215546Sopenharmony_ci
618bf215546Sopenharmony_ci                                slice->ub_pad = v3d_get_ub_pad(rsc,
619bf215546Sopenharmony_ci                                                               level_height);
620bf215546Sopenharmony_ci                                level_height += slice->ub_pad * uif_block_h;
621bf215546Sopenharmony_ci
622bf215546Sopenharmony_ci                                /* If the padding set us to to be aligned to
623bf215546Sopenharmony_ci                                 * the page cache size, then the HW will use
624bf215546Sopenharmony_ci                                 * the XOR bit on odd columns to get us
625bf215546Sopenharmony_ci                                 * perfectly misaligned
626bf215546Sopenharmony_ci                                 */
627bf215546Sopenharmony_ci                                if ((level_height / uif_block_h) %
628bf215546Sopenharmony_ci                                    (V3D_PAGE_CACHE_SIZE /
629bf215546Sopenharmony_ci                                     V3D_UIFBLOCK_ROW_SIZE) == 0) {
630bf215546Sopenharmony_ci                                        slice->tiling = V3D_TILING_UIF_XOR;
631bf215546Sopenharmony_ci                                } else {
632bf215546Sopenharmony_ci                                        slice->tiling = V3D_TILING_UIF_NO_XOR;
633bf215546Sopenharmony_ci                                }
634bf215546Sopenharmony_ci                        }
635bf215546Sopenharmony_ci                }
636bf215546Sopenharmony_ci
637bf215546Sopenharmony_ci                slice->offset = offset;
638bf215546Sopenharmony_ci                if (winsys_stride)
639bf215546Sopenharmony_ci                        slice->stride = winsys_stride;
640bf215546Sopenharmony_ci                else
641bf215546Sopenharmony_ci                        slice->stride = level_width * rsc->cpp;
642bf215546Sopenharmony_ci                slice->padded_height = level_height;
643bf215546Sopenharmony_ci                slice->size = level_height * slice->stride;
644bf215546Sopenharmony_ci
645bf215546Sopenharmony_ci                uint32_t slice_total_size = slice->size * level_depth;
646bf215546Sopenharmony_ci
647bf215546Sopenharmony_ci                /* The HW aligns level 1's base to a page if any of level 1 or
648bf215546Sopenharmony_ci                 * below could be UIF XOR.  The lower levels then inherit the
649bf215546Sopenharmony_ci                 * alignment for as long as necessary, thanks to being power of
650bf215546Sopenharmony_ci                 * two aligned.
651bf215546Sopenharmony_ci                 */
652bf215546Sopenharmony_ci                if (i == 1 &&
653bf215546Sopenharmony_ci                    level_width > 4 * uif_block_w &&
654bf215546Sopenharmony_ci                    level_height > PAGE_CACHE_MINUS_1_5_UB_ROWS * uif_block_h) {
655bf215546Sopenharmony_ci                        slice_total_size = align(slice_total_size,
656bf215546Sopenharmony_ci                                                 V3D_UIFCFG_PAGE_SIZE);
657bf215546Sopenharmony_ci                }
658bf215546Sopenharmony_ci
659bf215546Sopenharmony_ci                offset += slice_total_size;
660bf215546Sopenharmony_ci
661bf215546Sopenharmony_ci        }
662bf215546Sopenharmony_ci        rsc->size = offset;
663bf215546Sopenharmony_ci
664bf215546Sopenharmony_ci        /* UIF/UBLINEAR levels need to be aligned to UIF-blocks, and LT only
665bf215546Sopenharmony_ci         * needs to be aligned to utile boundaries.  Since tiles are laid out
666bf215546Sopenharmony_ci         * from small to big in memory, we need to align the later UIF slices
667bf215546Sopenharmony_ci         * to UIF blocks, if they were preceded by non-UIF-block-aligned LT
668bf215546Sopenharmony_ci         * slices.
669bf215546Sopenharmony_ci         *
670bf215546Sopenharmony_ci         * We additionally align to 4k, which improves UIF XOR performance.
671bf215546Sopenharmony_ci         */
672bf215546Sopenharmony_ci        uint32_t page_align_offset = (align(rsc->slices[0].offset, 4096) -
673bf215546Sopenharmony_ci                                      rsc->slices[0].offset);
674bf215546Sopenharmony_ci        if (page_align_offset) {
675bf215546Sopenharmony_ci                rsc->size += page_align_offset;
676bf215546Sopenharmony_ci                for (int i = 0; i <= prsc->last_level; i++)
677bf215546Sopenharmony_ci                        rsc->slices[i].offset += page_align_offset;
678bf215546Sopenharmony_ci        }
679bf215546Sopenharmony_ci
680bf215546Sopenharmony_ci        /* Arrays and cube textures have a stride which is the distance from
681bf215546Sopenharmony_ci         * one full mipmap tree to the next (64b aligned).  For 3D textures,
682bf215546Sopenharmony_ci         * we need to program the stride between slices of miplevel 0.
683bf215546Sopenharmony_ci         */
684bf215546Sopenharmony_ci        if (prsc->target != PIPE_TEXTURE_3D) {
685bf215546Sopenharmony_ci                rsc->cube_map_stride = align(rsc->slices[0].offset +
686bf215546Sopenharmony_ci                                             rsc->slices[0].size, 64);
687bf215546Sopenharmony_ci                rsc->size += rsc->cube_map_stride * (prsc->array_size - 1);
688bf215546Sopenharmony_ci        } else {
689bf215546Sopenharmony_ci                rsc->cube_map_stride = rsc->slices[0].size;
690bf215546Sopenharmony_ci        }
691bf215546Sopenharmony_ci}
692bf215546Sopenharmony_ci
693bf215546Sopenharmony_ciuint32_t
694bf215546Sopenharmony_civ3d_layer_offset(struct pipe_resource *prsc, uint32_t level, uint32_t layer)
695bf215546Sopenharmony_ci{
696bf215546Sopenharmony_ci        struct v3d_resource *rsc = v3d_resource(prsc);
697bf215546Sopenharmony_ci        struct v3d_resource_slice *slice = &rsc->slices[level];
698bf215546Sopenharmony_ci
699bf215546Sopenharmony_ci        if (prsc->target == PIPE_TEXTURE_3D)
700bf215546Sopenharmony_ci                return slice->offset + layer * slice->size;
701bf215546Sopenharmony_ci        else
702bf215546Sopenharmony_ci                return slice->offset + layer * rsc->cube_map_stride;
703bf215546Sopenharmony_ci}
704bf215546Sopenharmony_ci
705bf215546Sopenharmony_cistatic struct v3d_resource *
706bf215546Sopenharmony_civ3d_resource_setup(struct pipe_screen *pscreen,
707bf215546Sopenharmony_ci                   const struct pipe_resource *tmpl)
708bf215546Sopenharmony_ci{
709bf215546Sopenharmony_ci        struct v3d_screen *screen = v3d_screen(pscreen);
710bf215546Sopenharmony_ci        struct v3d_resource *rsc = CALLOC_STRUCT(v3d_resource);
711bf215546Sopenharmony_ci        if (!rsc)
712bf215546Sopenharmony_ci                return NULL;
713bf215546Sopenharmony_ci        struct pipe_resource *prsc = &rsc->base;
714bf215546Sopenharmony_ci
715bf215546Sopenharmony_ci        *prsc = *tmpl;
716bf215546Sopenharmony_ci
717bf215546Sopenharmony_ci        pipe_reference_init(&prsc->reference, 1);
718bf215546Sopenharmony_ci        prsc->screen = pscreen;
719bf215546Sopenharmony_ci
720bf215546Sopenharmony_ci        if (prsc->nr_samples <= 1 ||
721bf215546Sopenharmony_ci            screen->devinfo.ver >= 40 ||
722bf215546Sopenharmony_ci            util_format_is_depth_or_stencil(prsc->format)) {
723bf215546Sopenharmony_ci                rsc->cpp = util_format_get_blocksize(prsc->format);
724bf215546Sopenharmony_ci                if (screen->devinfo.ver < 40 && prsc->nr_samples > 1)
725bf215546Sopenharmony_ci                        rsc->cpp *= prsc->nr_samples;
726bf215546Sopenharmony_ci        } else {
727bf215546Sopenharmony_ci                assert(v3d_rt_format_supported(&screen->devinfo, prsc->format));
728bf215546Sopenharmony_ci                uint32_t output_image_format =
729bf215546Sopenharmony_ci                        v3d_get_rt_format(&screen->devinfo, prsc->format);
730bf215546Sopenharmony_ci                uint32_t internal_type;
731bf215546Sopenharmony_ci                uint32_t internal_bpp;
732bf215546Sopenharmony_ci                v3d_get_internal_type_bpp_for_output_format(&screen->devinfo,
733bf215546Sopenharmony_ci                                                            output_image_format,
734bf215546Sopenharmony_ci                                                            &internal_type,
735bf215546Sopenharmony_ci                                                            &internal_bpp);
736bf215546Sopenharmony_ci                switch (internal_bpp) {
737bf215546Sopenharmony_ci                case V3D_INTERNAL_BPP_32:
738bf215546Sopenharmony_ci                        rsc->cpp = 4;
739bf215546Sopenharmony_ci                        break;
740bf215546Sopenharmony_ci                case V3D_INTERNAL_BPP_64:
741bf215546Sopenharmony_ci                        rsc->cpp = 8;
742bf215546Sopenharmony_ci                        break;
743bf215546Sopenharmony_ci                case V3D_INTERNAL_BPP_128:
744bf215546Sopenharmony_ci                        rsc->cpp = 16;
745bf215546Sopenharmony_ci                        break;
746bf215546Sopenharmony_ci                }
747bf215546Sopenharmony_ci        }
748bf215546Sopenharmony_ci
749bf215546Sopenharmony_ci        rsc->serial_id++;
750bf215546Sopenharmony_ci
751bf215546Sopenharmony_ci        assert(rsc->cpp);
752bf215546Sopenharmony_ci
753bf215546Sopenharmony_ci        return rsc;
754bf215546Sopenharmony_ci}
755bf215546Sopenharmony_ci
756bf215546Sopenharmony_cistatic struct pipe_resource *
757bf215546Sopenharmony_civ3d_resource_create_with_modifiers(struct pipe_screen *pscreen,
758bf215546Sopenharmony_ci                                   const struct pipe_resource *tmpl,
759bf215546Sopenharmony_ci                                   const uint64_t *modifiers,
760bf215546Sopenharmony_ci                                   int count)
761bf215546Sopenharmony_ci{
762bf215546Sopenharmony_ci        struct v3d_screen *screen = v3d_screen(pscreen);
763bf215546Sopenharmony_ci
764bf215546Sopenharmony_ci        bool linear_ok = drm_find_modifier(DRM_FORMAT_MOD_LINEAR, modifiers, count);
765bf215546Sopenharmony_ci        struct v3d_resource *rsc = v3d_resource_setup(pscreen, tmpl);
766bf215546Sopenharmony_ci        struct pipe_resource *prsc = &rsc->base;
767bf215546Sopenharmony_ci        /* Use a tiled layout if we can, for better 3D performance. */
768bf215546Sopenharmony_ci        bool should_tile = true;
769bf215546Sopenharmony_ci
770bf215546Sopenharmony_ci        assert(tmpl->target != PIPE_BUFFER ||
771bf215546Sopenharmony_ci               (tmpl->format == PIPE_FORMAT_NONE ||
772bf215546Sopenharmony_ci                util_format_get_blocksize(tmpl->format) == 1));
773bf215546Sopenharmony_ci
774bf215546Sopenharmony_ci        /* VBOs/PBOs/Texture Buffer Objects are untiled (and 1 height). */
775bf215546Sopenharmony_ci        if (tmpl->target == PIPE_BUFFER)
776bf215546Sopenharmony_ci                should_tile = false;
777bf215546Sopenharmony_ci
778bf215546Sopenharmony_ci        /* Cursors are always linear, and the user can request linear as well.
779bf215546Sopenharmony_ci         */
780bf215546Sopenharmony_ci        if (tmpl->bind & (PIPE_BIND_LINEAR | PIPE_BIND_CURSOR))
781bf215546Sopenharmony_ci                should_tile = false;
782bf215546Sopenharmony_ci
783bf215546Sopenharmony_ci        /* 1D and 1D_ARRAY textures are always raster-order. */
784bf215546Sopenharmony_ci        if (tmpl->target == PIPE_TEXTURE_1D ||
785bf215546Sopenharmony_ci            tmpl->target == PIPE_TEXTURE_1D_ARRAY)
786bf215546Sopenharmony_ci                should_tile = false;
787bf215546Sopenharmony_ci
788bf215546Sopenharmony_ci        /* Scanout BOs for simulator need to be linear for interaction with
789bf215546Sopenharmony_ci         * i965.
790bf215546Sopenharmony_ci         */
791bf215546Sopenharmony_ci        if (using_v3d_simulator &&
792bf215546Sopenharmony_ci            tmpl->bind & (PIPE_BIND_SHARED | PIPE_BIND_SCANOUT))
793bf215546Sopenharmony_ci                should_tile = false;
794bf215546Sopenharmony_ci
795bf215546Sopenharmony_ci        /* If using the old-school SCANOUT flag, we don't know what the screen
796bf215546Sopenharmony_ci         * might support other than linear. Just force linear.
797bf215546Sopenharmony_ci         */
798bf215546Sopenharmony_ci        if (tmpl->bind & PIPE_BIND_SCANOUT)
799bf215546Sopenharmony_ci                should_tile = false;
800bf215546Sopenharmony_ci
801bf215546Sopenharmony_ci        /* No user-specified modifier; determine our own. */
802bf215546Sopenharmony_ci        if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
803bf215546Sopenharmony_ci                linear_ok = true;
804bf215546Sopenharmony_ci                rsc->tiled = should_tile;
805bf215546Sopenharmony_ci        } else if (should_tile &&
806bf215546Sopenharmony_ci                   drm_find_modifier(DRM_FORMAT_MOD_BROADCOM_UIF,
807bf215546Sopenharmony_ci                                 modifiers, count)) {
808bf215546Sopenharmony_ci                rsc->tiled = true;
809bf215546Sopenharmony_ci        } else if (linear_ok) {
810bf215546Sopenharmony_ci                rsc->tiled = false;
811bf215546Sopenharmony_ci        } else {
812bf215546Sopenharmony_ci                fprintf(stderr, "Unsupported modifier requested\n");
813bf215546Sopenharmony_ci                goto fail;
814bf215546Sopenharmony_ci        }
815bf215546Sopenharmony_ci
816bf215546Sopenharmony_ci        rsc->internal_format = prsc->format;
817bf215546Sopenharmony_ci
818bf215546Sopenharmony_ci        v3d_setup_slices(rsc, 0, tmpl->bind & PIPE_BIND_SHARED);
819bf215546Sopenharmony_ci
820bf215546Sopenharmony_ci        if (screen->ro && (tmpl->bind & PIPE_BIND_SCANOUT)) {
821bf215546Sopenharmony_ci                struct winsys_handle handle;
822bf215546Sopenharmony_ci                struct pipe_resource scanout_tmpl = {
823bf215546Sopenharmony_ci                        .target = prsc->target,
824bf215546Sopenharmony_ci                        .format = PIPE_FORMAT_RGBA8888_UNORM,
825bf215546Sopenharmony_ci                        .width0 = 1024, /* one page */
826bf215546Sopenharmony_ci                        .height0 = align(rsc->size, 4096) / 4096,
827bf215546Sopenharmony_ci                        .depth0 = 1,
828bf215546Sopenharmony_ci                        .array_size = 1,
829bf215546Sopenharmony_ci                };
830bf215546Sopenharmony_ci
831bf215546Sopenharmony_ci                rsc->scanout =
832bf215546Sopenharmony_ci                        renderonly_scanout_for_resource(&scanout_tmpl,
833bf215546Sopenharmony_ci                                                        screen->ro,
834bf215546Sopenharmony_ci                                                        &handle);
835bf215546Sopenharmony_ci
836bf215546Sopenharmony_ci                if (!rsc->scanout) {
837bf215546Sopenharmony_ci                        fprintf(stderr, "Failed to create scanout resource\n");
838bf215546Sopenharmony_ci                        goto fail;
839bf215546Sopenharmony_ci                }
840bf215546Sopenharmony_ci                assert(handle.type == WINSYS_HANDLE_TYPE_FD);
841bf215546Sopenharmony_ci                rsc->bo = v3d_bo_open_dmabuf(screen, handle.handle);
842bf215546Sopenharmony_ci                close(handle.handle);
843bf215546Sopenharmony_ci
844bf215546Sopenharmony_ci                if (!rsc->bo)
845bf215546Sopenharmony_ci                        goto fail;
846bf215546Sopenharmony_ci
847bf215546Sopenharmony_ci                v3d_debug_resource_layout(rsc, "renderonly");
848bf215546Sopenharmony_ci
849bf215546Sopenharmony_ci                return prsc;
850bf215546Sopenharmony_ci        } else {
851bf215546Sopenharmony_ci                if (!v3d_resource_bo_alloc(rsc))
852bf215546Sopenharmony_ci                        goto fail;
853bf215546Sopenharmony_ci        }
854bf215546Sopenharmony_ci
855bf215546Sopenharmony_ci        return prsc;
856bf215546Sopenharmony_cifail:
857bf215546Sopenharmony_ci        v3d_resource_destroy(pscreen, prsc);
858bf215546Sopenharmony_ci        return NULL;
859bf215546Sopenharmony_ci}
860bf215546Sopenharmony_ci
861bf215546Sopenharmony_cistruct pipe_resource *
862bf215546Sopenharmony_civ3d_resource_create(struct pipe_screen *pscreen,
863bf215546Sopenharmony_ci                    const struct pipe_resource *tmpl)
864bf215546Sopenharmony_ci{
865bf215546Sopenharmony_ci        const uint64_t mod = DRM_FORMAT_MOD_INVALID;
866bf215546Sopenharmony_ci        return v3d_resource_create_with_modifiers(pscreen, tmpl, &mod, 1);
867bf215546Sopenharmony_ci}
868bf215546Sopenharmony_ci
869bf215546Sopenharmony_cistatic struct pipe_resource *
870bf215546Sopenharmony_civ3d_resource_from_handle(struct pipe_screen *pscreen,
871bf215546Sopenharmony_ci                         const struct pipe_resource *tmpl,
872bf215546Sopenharmony_ci                         struct winsys_handle *whandle,
873bf215546Sopenharmony_ci                         unsigned usage)
874bf215546Sopenharmony_ci{
875bf215546Sopenharmony_ci        struct v3d_screen *screen = v3d_screen(pscreen);
876bf215546Sopenharmony_ci        struct v3d_resource *rsc = v3d_resource_setup(pscreen, tmpl);
877bf215546Sopenharmony_ci        struct pipe_resource *prsc = &rsc->base;
878bf215546Sopenharmony_ci        struct v3d_resource_slice *slice = &rsc->slices[0];
879bf215546Sopenharmony_ci
880bf215546Sopenharmony_ci        if (!rsc)
881bf215546Sopenharmony_ci                return NULL;
882bf215546Sopenharmony_ci
883bf215546Sopenharmony_ci        switch (whandle->modifier) {
884bf215546Sopenharmony_ci        case DRM_FORMAT_MOD_LINEAR:
885bf215546Sopenharmony_ci                rsc->tiled = false;
886bf215546Sopenharmony_ci                break;
887bf215546Sopenharmony_ci        case DRM_FORMAT_MOD_BROADCOM_UIF:
888bf215546Sopenharmony_ci                rsc->tiled = true;
889bf215546Sopenharmony_ci                break;
890bf215546Sopenharmony_ci        case DRM_FORMAT_MOD_INVALID:
891bf215546Sopenharmony_ci                rsc->tiled = screen->ro == NULL;
892bf215546Sopenharmony_ci                break;
893bf215546Sopenharmony_ci        default:
894bf215546Sopenharmony_ci                switch(fourcc_mod_broadcom_mod(whandle->modifier)) {
895bf215546Sopenharmony_ci                case DRM_FORMAT_MOD_BROADCOM_SAND128:
896bf215546Sopenharmony_ci                        rsc->tiled = false;
897bf215546Sopenharmony_ci                        rsc->sand_col128_stride =
898bf215546Sopenharmony_ci                                fourcc_mod_broadcom_param(whandle->modifier);
899bf215546Sopenharmony_ci                        break;
900bf215546Sopenharmony_ci                default:
901bf215546Sopenharmony_ci                        fprintf(stderr,
902bf215546Sopenharmony_ci                                "Attempt to import unsupported modifier 0x%llx\n",
903bf215546Sopenharmony_ci                                (long long)whandle->modifier);
904bf215546Sopenharmony_ci                        goto fail;
905bf215546Sopenharmony_ci                }
906bf215546Sopenharmony_ci        }
907bf215546Sopenharmony_ci
908bf215546Sopenharmony_ci        switch (whandle->type) {
909bf215546Sopenharmony_ci        case WINSYS_HANDLE_TYPE_SHARED:
910bf215546Sopenharmony_ci                rsc->bo = v3d_bo_open_name(screen, whandle->handle);
911bf215546Sopenharmony_ci                break;
912bf215546Sopenharmony_ci        case WINSYS_HANDLE_TYPE_FD:
913bf215546Sopenharmony_ci                rsc->bo = v3d_bo_open_dmabuf(screen, whandle->handle);
914bf215546Sopenharmony_ci                break;
915bf215546Sopenharmony_ci        default:
916bf215546Sopenharmony_ci                fprintf(stderr,
917bf215546Sopenharmony_ci                        "Attempt to import unsupported handle type %d\n",
918bf215546Sopenharmony_ci                        whandle->type);
919bf215546Sopenharmony_ci                goto fail;
920bf215546Sopenharmony_ci        }
921bf215546Sopenharmony_ci
922bf215546Sopenharmony_ci        if (!rsc->bo)
923bf215546Sopenharmony_ci                goto fail;
924bf215546Sopenharmony_ci
925bf215546Sopenharmony_ci        rsc->internal_format = prsc->format;
926bf215546Sopenharmony_ci
927bf215546Sopenharmony_ci        v3d_setup_slices(rsc, whandle->stride, true);
928bf215546Sopenharmony_ci        v3d_debug_resource_layout(rsc, "import");
929bf215546Sopenharmony_ci
930bf215546Sopenharmony_ci        if (whandle->offset != 0) {
931bf215546Sopenharmony_ci                if (rsc->tiled) {
932bf215546Sopenharmony_ci                        fprintf(stderr,
933bf215546Sopenharmony_ci                                "Attempt to import unsupported winsys offset %u\n",
934bf215546Sopenharmony_ci                                whandle->offset);
935bf215546Sopenharmony_ci                        goto fail;
936bf215546Sopenharmony_ci                }
937bf215546Sopenharmony_ci                rsc->slices[0].offset += whandle->offset;
938bf215546Sopenharmony_ci
939bf215546Sopenharmony_ci                if (rsc->slices[0].offset + rsc->slices[0].size >
940bf215546Sopenharmony_ci                    rsc->bo->size) {
941bf215546Sopenharmony_ci                        fprintf(stderr, "Attempt to import "
942bf215546Sopenharmony_ci                                "with overflowing offset (%d + %d > %d)\n",
943bf215546Sopenharmony_ci                                whandle->offset,
944bf215546Sopenharmony_ci                                rsc->slices[0].size,
945bf215546Sopenharmony_ci                                rsc->bo->size);
946bf215546Sopenharmony_ci                         goto fail;
947bf215546Sopenharmony_ci                 }
948bf215546Sopenharmony_ci        }
949bf215546Sopenharmony_ci
950bf215546Sopenharmony_ci        if (screen->ro) {
951bf215546Sopenharmony_ci                /* Make sure that renderonly has a handle to our buffer in the
952bf215546Sopenharmony_ci                 * display's fd, so that a later renderonly_get_handle()
953bf215546Sopenharmony_ci                 * returns correct handles or GEM names.
954bf215546Sopenharmony_ci                 */
955bf215546Sopenharmony_ci                rsc->scanout =
956bf215546Sopenharmony_ci                        renderonly_create_gpu_import_for_resource(prsc,
957bf215546Sopenharmony_ci                                                                  screen->ro,
958bf215546Sopenharmony_ci                                                                  NULL);
959bf215546Sopenharmony_ci        }
960bf215546Sopenharmony_ci
961bf215546Sopenharmony_ci        if (rsc->tiled && whandle->stride != slice->stride) {
962bf215546Sopenharmony_ci                static bool warned = false;
963bf215546Sopenharmony_ci                if (!warned) {
964bf215546Sopenharmony_ci                        warned = true;
965bf215546Sopenharmony_ci                        fprintf(stderr,
966bf215546Sopenharmony_ci                                "Attempting to import %dx%d %s with "
967bf215546Sopenharmony_ci                                "unsupported stride %d instead of %d\n",
968bf215546Sopenharmony_ci                                prsc->width0, prsc->height0,
969bf215546Sopenharmony_ci                                util_format_short_name(prsc->format),
970bf215546Sopenharmony_ci                                whandle->stride,
971bf215546Sopenharmony_ci                                slice->stride);
972bf215546Sopenharmony_ci                }
973bf215546Sopenharmony_ci                goto fail;
974bf215546Sopenharmony_ci        } else if (!rsc->tiled) {
975bf215546Sopenharmony_ci                slice->stride = whandle->stride;
976bf215546Sopenharmony_ci        }
977bf215546Sopenharmony_ci
978bf215546Sopenharmony_ci        return prsc;
979bf215546Sopenharmony_ci
980bf215546Sopenharmony_cifail:
981bf215546Sopenharmony_ci        v3d_resource_destroy(pscreen, prsc);
982bf215546Sopenharmony_ci        return NULL;
983bf215546Sopenharmony_ci}
984bf215546Sopenharmony_ci
985bf215546Sopenharmony_civoid
986bf215546Sopenharmony_civ3d_update_shadow_texture(struct pipe_context *pctx,
987bf215546Sopenharmony_ci                          struct pipe_sampler_view *pview)
988bf215546Sopenharmony_ci{
989bf215546Sopenharmony_ci        struct v3d_context *v3d = v3d_context(pctx);
990bf215546Sopenharmony_ci        struct v3d_sampler_view *view = v3d_sampler_view(pview);
991bf215546Sopenharmony_ci        struct v3d_resource *shadow = v3d_resource(view->texture);
992bf215546Sopenharmony_ci        struct v3d_resource *orig = v3d_resource(pview->texture);
993bf215546Sopenharmony_ci
994bf215546Sopenharmony_ci        assert(view->texture != pview->texture);
995bf215546Sopenharmony_ci
996bf215546Sopenharmony_ci        if (shadow->writes == orig->writes && orig->bo->private)
997bf215546Sopenharmony_ci                return;
998bf215546Sopenharmony_ci
999bf215546Sopenharmony_ci        perf_debug("Updating %dx%d@%d shadow for linear texture\n",
1000bf215546Sopenharmony_ci                   orig->base.width0, orig->base.height0,
1001bf215546Sopenharmony_ci                   pview->u.tex.first_level);
1002bf215546Sopenharmony_ci
1003bf215546Sopenharmony_ci        for (int i = 0; i <= shadow->base.last_level; i++) {
1004bf215546Sopenharmony_ci                unsigned width = u_minify(shadow->base.width0, i);
1005bf215546Sopenharmony_ci                unsigned height = u_minify(shadow->base.height0, i);
1006bf215546Sopenharmony_ci                struct pipe_blit_info info = {
1007bf215546Sopenharmony_ci                        .dst = {
1008bf215546Sopenharmony_ci                                .resource = &shadow->base,
1009bf215546Sopenharmony_ci                                .level = i,
1010bf215546Sopenharmony_ci                                .box = {
1011bf215546Sopenharmony_ci                                        .x = 0,
1012bf215546Sopenharmony_ci                                        .y = 0,
1013bf215546Sopenharmony_ci                                        .z = 0,
1014bf215546Sopenharmony_ci                                        .width = width,
1015bf215546Sopenharmony_ci                                        .height = height,
1016bf215546Sopenharmony_ci                                        .depth = 1,
1017bf215546Sopenharmony_ci                                },
1018bf215546Sopenharmony_ci                                .format = shadow->base.format,
1019bf215546Sopenharmony_ci                        },
1020bf215546Sopenharmony_ci                        .src = {
1021bf215546Sopenharmony_ci                                .resource = &orig->base,
1022bf215546Sopenharmony_ci                                .level = pview->u.tex.first_level + i,
1023bf215546Sopenharmony_ci                                .box = {
1024bf215546Sopenharmony_ci                                        .x = 0,
1025bf215546Sopenharmony_ci                                        .y = 0,
1026bf215546Sopenharmony_ci                                        .z = 0,
1027bf215546Sopenharmony_ci                                        .width = width,
1028bf215546Sopenharmony_ci                                        .height = height,
1029bf215546Sopenharmony_ci                                        .depth = 1,
1030bf215546Sopenharmony_ci                                },
1031bf215546Sopenharmony_ci                                .format = orig->base.format,
1032bf215546Sopenharmony_ci                        },
1033bf215546Sopenharmony_ci                        .mask = util_format_get_mask(orig->base.format),
1034bf215546Sopenharmony_ci                };
1035bf215546Sopenharmony_ci                pctx->blit(pctx, &info);
1036bf215546Sopenharmony_ci        }
1037bf215546Sopenharmony_ci
1038bf215546Sopenharmony_ci        shadow->writes = orig->writes;
1039bf215546Sopenharmony_ci}
1040bf215546Sopenharmony_ci
1041bf215546Sopenharmony_cistatic struct pipe_surface *
1042bf215546Sopenharmony_civ3d_create_surface(struct pipe_context *pctx,
1043bf215546Sopenharmony_ci                   struct pipe_resource *ptex,
1044bf215546Sopenharmony_ci                   const struct pipe_surface *surf_tmpl)
1045bf215546Sopenharmony_ci{
1046bf215546Sopenharmony_ci        struct v3d_context *v3d = v3d_context(pctx);
1047bf215546Sopenharmony_ci        struct v3d_screen *screen = v3d->screen;
1048bf215546Sopenharmony_ci        struct v3d_surface *surface = CALLOC_STRUCT(v3d_surface);
1049bf215546Sopenharmony_ci        struct v3d_resource *rsc = v3d_resource(ptex);
1050bf215546Sopenharmony_ci
1051bf215546Sopenharmony_ci        if (!surface)
1052bf215546Sopenharmony_ci                return NULL;
1053bf215546Sopenharmony_ci
1054bf215546Sopenharmony_ci        struct pipe_surface *psurf = &surface->base;
1055bf215546Sopenharmony_ci        unsigned level = surf_tmpl->u.tex.level;
1056bf215546Sopenharmony_ci        struct v3d_resource_slice *slice = &rsc->slices[level];
1057bf215546Sopenharmony_ci
1058bf215546Sopenharmony_ci        pipe_reference_init(&psurf->reference, 1);
1059bf215546Sopenharmony_ci        pipe_resource_reference(&psurf->texture, ptex);
1060bf215546Sopenharmony_ci
1061bf215546Sopenharmony_ci        psurf->context = pctx;
1062bf215546Sopenharmony_ci        psurf->format = surf_tmpl->format;
1063bf215546Sopenharmony_ci        psurf->width = u_minify(ptex->width0, level);
1064bf215546Sopenharmony_ci        psurf->height = u_minify(ptex->height0, level);
1065bf215546Sopenharmony_ci        psurf->u.tex.level = level;
1066bf215546Sopenharmony_ci        psurf->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
1067bf215546Sopenharmony_ci        psurf->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
1068bf215546Sopenharmony_ci
1069bf215546Sopenharmony_ci        surface->offset = v3d_layer_offset(ptex, level,
1070bf215546Sopenharmony_ci                                           psurf->u.tex.first_layer);
1071bf215546Sopenharmony_ci        surface->tiling = slice->tiling;
1072bf215546Sopenharmony_ci
1073bf215546Sopenharmony_ci        surface->format = v3d_get_rt_format(&screen->devinfo, psurf->format);
1074bf215546Sopenharmony_ci
1075bf215546Sopenharmony_ci        const struct util_format_description *desc =
1076bf215546Sopenharmony_ci                util_format_description(psurf->format);
1077bf215546Sopenharmony_ci
1078bf215546Sopenharmony_ci        surface->swap_rb = (desc->swizzle[0] == PIPE_SWIZZLE_Z &&
1079bf215546Sopenharmony_ci                            psurf->format != PIPE_FORMAT_B5G6R5_UNORM);
1080bf215546Sopenharmony_ci
1081bf215546Sopenharmony_ci        if (util_format_is_depth_or_stencil(psurf->format)) {
1082bf215546Sopenharmony_ci                switch (psurf->format) {
1083bf215546Sopenharmony_ci                case PIPE_FORMAT_Z16_UNORM:
1084bf215546Sopenharmony_ci                        surface->internal_type = V3D_INTERNAL_TYPE_DEPTH_16;
1085bf215546Sopenharmony_ci                        break;
1086bf215546Sopenharmony_ci                case PIPE_FORMAT_Z32_FLOAT:
1087bf215546Sopenharmony_ci                case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1088bf215546Sopenharmony_ci                        surface->internal_type = V3D_INTERNAL_TYPE_DEPTH_32F;
1089bf215546Sopenharmony_ci                        break;
1090bf215546Sopenharmony_ci                default:
1091bf215546Sopenharmony_ci                        surface->internal_type = V3D_INTERNAL_TYPE_DEPTH_24;
1092bf215546Sopenharmony_ci                }
1093bf215546Sopenharmony_ci        } else {
1094bf215546Sopenharmony_ci                uint32_t bpp, type;
1095bf215546Sopenharmony_ci                v3d_get_internal_type_bpp_for_output_format(&screen->devinfo,
1096bf215546Sopenharmony_ci                                                            surface->format,
1097bf215546Sopenharmony_ci                                                            &type, &bpp);
1098bf215546Sopenharmony_ci                surface->internal_type = type;
1099bf215546Sopenharmony_ci                surface->internal_bpp = bpp;
1100bf215546Sopenharmony_ci        }
1101bf215546Sopenharmony_ci
1102bf215546Sopenharmony_ci        if (surface->tiling == V3D_TILING_UIF_NO_XOR ||
1103bf215546Sopenharmony_ci            surface->tiling == V3D_TILING_UIF_XOR) {
1104bf215546Sopenharmony_ci                surface->padded_height_of_output_image_in_uif_blocks =
1105bf215546Sopenharmony_ci                        (slice->padded_height /
1106bf215546Sopenharmony_ci                         (2 * v3d_utile_height(rsc->cpp)));
1107bf215546Sopenharmony_ci        }
1108bf215546Sopenharmony_ci
1109bf215546Sopenharmony_ci        if (rsc->separate_stencil) {
1110bf215546Sopenharmony_ci                surface->separate_stencil =
1111bf215546Sopenharmony_ci                        v3d_create_surface(pctx, &rsc->separate_stencil->base,
1112bf215546Sopenharmony_ci                                           surf_tmpl);
1113bf215546Sopenharmony_ci        }
1114bf215546Sopenharmony_ci
1115bf215546Sopenharmony_ci        return &surface->base;
1116bf215546Sopenharmony_ci}
1117bf215546Sopenharmony_ci
1118bf215546Sopenharmony_cistatic void
1119bf215546Sopenharmony_civ3d_surface_destroy(struct pipe_context *pctx, struct pipe_surface *psurf)
1120bf215546Sopenharmony_ci{
1121bf215546Sopenharmony_ci        struct v3d_surface *surf = v3d_surface(psurf);
1122bf215546Sopenharmony_ci
1123bf215546Sopenharmony_ci        if (surf->separate_stencil)
1124bf215546Sopenharmony_ci                pipe_surface_reference(&surf->separate_stencil, NULL);
1125bf215546Sopenharmony_ci
1126bf215546Sopenharmony_ci        pipe_resource_reference(&psurf->texture, NULL);
1127bf215546Sopenharmony_ci        FREE(psurf);
1128bf215546Sopenharmony_ci}
1129bf215546Sopenharmony_ci
1130bf215546Sopenharmony_cistatic void
1131bf215546Sopenharmony_civ3d_flush_resource(struct pipe_context *pctx, struct pipe_resource *resource)
1132bf215546Sopenharmony_ci{
1133bf215546Sopenharmony_ci        /* All calls to flush_resource are followed by a flush of the context,
1134bf215546Sopenharmony_ci         * so there's nothing to do.
1135bf215546Sopenharmony_ci         */
1136bf215546Sopenharmony_ci}
1137bf215546Sopenharmony_ci
1138bf215546Sopenharmony_cistatic enum pipe_format
1139bf215546Sopenharmony_civ3d_resource_get_internal_format(struct pipe_resource *prsc)
1140bf215546Sopenharmony_ci{
1141bf215546Sopenharmony_ci        return v3d_resource(prsc)->internal_format;
1142bf215546Sopenharmony_ci}
1143bf215546Sopenharmony_ci
1144bf215546Sopenharmony_cistatic void
1145bf215546Sopenharmony_civ3d_resource_set_stencil(struct pipe_resource *prsc,
1146bf215546Sopenharmony_ci                         struct pipe_resource *stencil)
1147bf215546Sopenharmony_ci{
1148bf215546Sopenharmony_ci        v3d_resource(prsc)->separate_stencil = v3d_resource(stencil);
1149bf215546Sopenharmony_ci}
1150bf215546Sopenharmony_ci
1151bf215546Sopenharmony_cistatic struct pipe_resource *
1152bf215546Sopenharmony_civ3d_resource_get_stencil(struct pipe_resource *prsc)
1153bf215546Sopenharmony_ci{
1154bf215546Sopenharmony_ci        struct v3d_resource *rsc = v3d_resource(prsc);
1155bf215546Sopenharmony_ci
1156bf215546Sopenharmony_ci        return &rsc->separate_stencil->base;
1157bf215546Sopenharmony_ci}
1158bf215546Sopenharmony_ci
1159bf215546Sopenharmony_cistatic const struct u_transfer_vtbl transfer_vtbl = {
1160bf215546Sopenharmony_ci        .resource_create          = v3d_resource_create,
1161bf215546Sopenharmony_ci        .resource_destroy         = v3d_resource_destroy,
1162bf215546Sopenharmony_ci        .transfer_map             = v3d_resource_transfer_map,
1163bf215546Sopenharmony_ci        .transfer_unmap           = v3d_resource_transfer_unmap,
1164bf215546Sopenharmony_ci        .transfer_flush_region    = u_default_transfer_flush_region,
1165bf215546Sopenharmony_ci        .get_internal_format      = v3d_resource_get_internal_format,
1166bf215546Sopenharmony_ci        .set_stencil              = v3d_resource_set_stencil,
1167bf215546Sopenharmony_ci        .get_stencil              = v3d_resource_get_stencil,
1168bf215546Sopenharmony_ci};
1169bf215546Sopenharmony_ci
1170bf215546Sopenharmony_civoid
1171bf215546Sopenharmony_civ3d_resource_screen_init(struct pipe_screen *pscreen)
1172bf215546Sopenharmony_ci{
1173bf215546Sopenharmony_ci        pscreen->resource_create_with_modifiers =
1174bf215546Sopenharmony_ci                v3d_resource_create_with_modifiers;
1175bf215546Sopenharmony_ci        pscreen->resource_create = u_transfer_helper_resource_create;
1176bf215546Sopenharmony_ci        pscreen->resource_from_handle = v3d_resource_from_handle;
1177bf215546Sopenharmony_ci        pscreen->resource_get_handle = v3d_resource_get_handle;
1178bf215546Sopenharmony_ci        pscreen->resource_get_param = v3d_resource_get_param;
1179bf215546Sopenharmony_ci        pscreen->resource_destroy = u_transfer_helper_resource_destroy;
1180bf215546Sopenharmony_ci        pscreen->transfer_helper = u_transfer_helper_create(&transfer_vtbl,
1181bf215546Sopenharmony_ci                                                            true, false,
1182bf215546Sopenharmony_ci                                                            true, true,
1183bf215546Sopenharmony_ci                                                            false);
1184bf215546Sopenharmony_ci}
1185bf215546Sopenharmony_ci
1186bf215546Sopenharmony_civoid
1187bf215546Sopenharmony_civ3d_resource_context_init(struct pipe_context *pctx)
1188bf215546Sopenharmony_ci{
1189bf215546Sopenharmony_ci        pctx->buffer_map = u_transfer_helper_transfer_map;
1190bf215546Sopenharmony_ci        pctx->texture_map = u_transfer_helper_transfer_map;
1191bf215546Sopenharmony_ci        pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
1192bf215546Sopenharmony_ci        pctx->buffer_unmap = u_transfer_helper_transfer_unmap;
1193bf215546Sopenharmony_ci        pctx->texture_unmap = u_transfer_helper_transfer_unmap;
1194bf215546Sopenharmony_ci        pctx->buffer_subdata = u_default_buffer_subdata;
1195bf215546Sopenharmony_ci        pctx->texture_subdata = v3d_texture_subdata;
1196bf215546Sopenharmony_ci        pctx->create_surface = v3d_create_surface;
1197bf215546Sopenharmony_ci        pctx->surface_destroy = v3d_surface_destroy;
1198bf215546Sopenharmony_ci        pctx->resource_copy_region = util_resource_copy_region;
1199bf215546Sopenharmony_ci        pctx->blit = v3d_blit;
1200bf215546Sopenharmony_ci        pctx->generate_mipmap = v3d_generate_mipmap;
1201bf215546Sopenharmony_ci        pctx->flush_resource = v3d_flush_resource;
1202bf215546Sopenharmony_ci}
1203