xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/svga/svga_sampler_view.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (c) 2008-2024 Broadcom. All Rights Reserved.
3  * The term “Broadcom” refers to Broadcom Inc.
4  * and/or its subsidiaries.
5  * SPDX-License-Identifier: MIT
6  */
7 
8 #include "svga_cmd.h"
9 
10 #include "pipe/p_state.h"
11 #include "pipe/p_defines.h"
12 #include "util/u_inlines.h"
13 #include "util/u_thread.h"
14 #include "util/format/u_format.h"
15 #include "util/u_math.h"
16 #include "util/u_memory.h"
17 #include "util/u_string.h"
18 
19 #include "svga_format.h"
20 #include "svga_screen.h"
21 #include "svga_context.h"
22 #include "svga_resource_texture.h"
23 #include "svga_sampler_view.h"
24 #include "svga_debug.h"
25 #include "svga_surface.h"
26 
27 
28 void
svga_debug_describe_sampler_view(char * buf,const struct svga_sampler_view * sv)29 svga_debug_describe_sampler_view(char *buf, const struct svga_sampler_view *sv)
30 {
31    char res[128];
32    debug_describe_resource(res, sv->texture);
33    sprintf(buf, "svga_sampler_view<%s,[%u,%u]>",
34                 res, sv->min_lod, sv->max_lod);
35 }
36 
37 
38 struct svga_sampler_view *
svga_get_tex_sampler_view(struct pipe_context * pipe,struct pipe_resource * pt,unsigned min_lod,unsigned max_lod)39 svga_get_tex_sampler_view(struct pipe_context *pipe,
40 			  struct pipe_resource *pt,
41                           unsigned min_lod, unsigned max_lod)
42 {
43    struct svga_context *svga = svga_context(pipe);
44    struct svga_screen *ss = svga_screen(pipe->screen);
45    struct svga_texture *tex = svga_texture(pt);
46    struct svga_sampler_view *sv = NULL;
47    SVGA3dSurface1Flags flags = SVGA3D_SURFACE_HINT_TEXTURE;
48    SVGA3dSurfaceFormat format = svga_translate_format(ss, pt->format,
49                                                       PIPE_BIND_SAMPLER_VIEW);
50    bool view = true;
51 
52    assert(pt);
53    assert(min_lod <= max_lod);
54    assert(max_lod <= pt->last_level);
55    assert(!svga_have_vgpu10(svga));
56 
57    /* Is a view needed */
58    {
59       /*
60        * Can't control max lod. For first level views and when we only
61        * look at one level we disable mip filtering to achive the same
62        * results as a view.
63        */
64       if (min_lod == 0 && max_lod >= pt->last_level)
65          view = false;
66 
67       if (ss->debug.no_sampler_view)
68          view = false;
69 
70       if (ss->debug.force_sampler_view)
71          view = true;
72    }
73 
74    /* First try the cache */
75    if (view) {
76       mtx_lock(&ss->tex_mutex);
77       if (tex->cached_view &&
78           tex->cached_view->min_lod == min_lod &&
79           tex->cached_view->max_lod == max_lod) {
80          svga_sampler_view_reference(&sv, tex->cached_view);
81          mtx_unlock(&ss->tex_mutex);
82          SVGA_DBG(DEBUG_VIEWS, "svga: Sampler view: reuse %p, %u %u, last %u\n",
83                               pt, min_lod, max_lod, pt->last_level);
84          svga_validate_sampler_view(svga_context(pipe), sv);
85          return sv;
86       }
87       mtx_unlock(&ss->tex_mutex);
88    }
89 
90    sv = CALLOC_STRUCT(svga_sampler_view);
91    if (!sv)
92       return NULL;
93 
94    pipe_reference_init(&sv->reference, 1);
95 
96    /* Note: we're not refcounting the texture resource here to avoid
97     * a circular dependency.
98     */
99    sv->texture = pt;
100 
101    sv->min_lod = min_lod;
102    sv->max_lod = max_lod;
103 
104    /* No view needed just use the whole texture */
105    if (!view) {
106       SVGA_DBG(DEBUG_VIEWS,
107                "svga: Sampler view: no %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
108                pt, min_lod, max_lod,
109                max_lod - min_lod + 1,
110                pt->width0,
111                pt->height0,
112                pt->depth0,
113                pt->last_level);
114       sv->key.cachable = 0;
115       sv->handle = tex->handle;
116       debug_reference(&sv->reference,
117                       (debug_reference_descriptor)svga_debug_describe_sampler_view, 0);
118       return sv;
119    }
120 
121    SVGA_DBG(DEBUG_VIEWS,
122             "svga: Sampler view: yes %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
123             pt, min_lod, max_lod,
124             max_lod - min_lod + 1,
125             pt->width0,
126             pt->height0,
127             pt->depth0,
128             pt->last_level);
129 
130    sv->age = tex->age;
131    sv->handle = svga_texture_view_surface(svga, tex,
132                                           PIPE_BIND_SAMPLER_VIEW,
133                                           flags, format,
134                                           min_lod,
135                                           max_lod - min_lod + 1,
136                                           -1, 1, -1, false,
137                                           &sv->key);
138 
139    if (!sv->handle) {
140       sv->key.cachable = 0;
141       sv->handle = tex->handle;
142       debug_reference(&sv->reference,
143                       (debug_reference_descriptor)
144                       svga_debug_describe_sampler_view, 0);
145       return sv;
146    }
147 
148    mtx_lock(&ss->tex_mutex);
149    svga_sampler_view_reference(&tex->cached_view, sv);
150    mtx_unlock(&ss->tex_mutex);
151 
152    debug_reference(&sv->reference,
153                    (debug_reference_descriptor)
154                    svga_debug_describe_sampler_view, 0);
155 
156    return sv;
157 }
158 
159 
160 void
svga_validate_sampler_view(struct svga_context * svga,struct svga_sampler_view * v)161 svga_validate_sampler_view(struct svga_context *svga,
162                            struct svga_sampler_view *v)
163 {
164    struct svga_texture *tex = svga_texture(v->texture);
165    unsigned numFaces;
166    unsigned age = 0;
167    int i;
168    unsigned k;
169 
170    assert(svga);
171    assert(!svga_have_vgpu10(svga));
172 
173    if (v->handle == tex->handle)
174       return;
175 
176    age = tex->age;
177 
178    if (tex->b.target == PIPE_TEXTURE_CUBE)
179       numFaces = 6;
180    else
181       numFaces = 1;
182 
183    for (i = v->min_lod; i <= v->max_lod; i++) {
184       for (k = 0; k < numFaces; k++) {
185          assert(i < ARRAY_SIZE(tex->view_age));
186          if (v->age < tex->view_age[i])
187             svga_texture_copy_handle(svga,
188                                      tex->handle, 0, 0, 0, i, k,
189                                      v->handle, 0, 0, 0, i - v->min_lod, k,
190                                      u_minify(tex->b.width0, i),
191                                      u_minify(tex->b.height0, i),
192                                      u_minify(tex->b.depth0, i));
193       }
194    }
195 
196    v->age = age;
197 }
198 
199 
200 void
svga_destroy_sampler_view_priv(struct svga_sampler_view * v)201 svga_destroy_sampler_view_priv(struct svga_sampler_view *v)
202 {
203    struct svga_texture *tex = svga_texture(v->texture);
204 
205    if (v->handle != tex->handle) {
206       struct svga_screen *ss = svga_screen(v->texture->screen);
207       SVGA_DBG(DEBUG_DMA, "unref sid %p (sampler view)\n", v->handle);
208       svga_screen_surface_destroy(ss, &v->key,
209                                   svga_was_texture_rendered_to(tex),
210                                   &v->handle);
211    }
212 
213    /* Note: we're not refcounting the texture resource here to avoid
214     * a circular dependency.
215     */
216    v->texture = NULL;
217 
218    FREE(v);
219 }
220