xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/freedreno/freedreno_texture.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2012 Rob Clark <[email protected]>
3  * SPDX-License-Identifier: MIT
4  *
5  * Authors:
6  *    Rob Clark <[email protected]>
7  */
8 
9 #include "pipe/p_state.h"
10 #include "util/u_inlines.h"
11 #include "util/u_memory.h"
12 #include "util/u_string.h"
13 
14 #include "freedreno_context.h"
15 #include "freedreno_resource.h"
16 #include "freedreno_texture.h"
17 #include "freedreno_util.h"
18 
19 static void
fd_sampler_state_delete(struct pipe_context * pctx,void * hwcso)20 fd_sampler_state_delete(struct pipe_context *pctx, void *hwcso)
21 {
22    FREE(hwcso);
23 }
24 
25 static void
fd_sampler_view_destroy(struct pipe_context * pctx,struct pipe_sampler_view * view)26 fd_sampler_view_destroy(struct pipe_context *pctx,
27                         struct pipe_sampler_view *view)
28 {
29    pipe_resource_reference(&view->texture, NULL);
30    FREE(view);
31 }
32 
33 void
fd_sampler_states_bind(struct pipe_context * pctx,enum pipe_shader_type shader,unsigned start,unsigned nr,void ** hwcso)34 fd_sampler_states_bind(struct pipe_context *pctx, enum pipe_shader_type shader,
35                        unsigned start, unsigned nr, void **hwcso) in_dt
36 {
37    struct fd_context *ctx = fd_context(pctx);
38    struct fd_texture_stateobj *tex = &ctx->tex[shader];
39 
40    for (unsigned i = 0; i < nr; i++) {
41       unsigned p = i + start;
42       tex->samplers[p] = hwcso ? hwcso[i] : NULL;
43       if (tex->samplers[p])
44          tex->valid_samplers |= (1 << p);
45       else
46          tex->valid_samplers &= ~(1 << p);
47    }
48 
49    tex->num_samplers = util_last_bit(tex->valid_samplers);
50 
51    fd_context_dirty_shader(ctx, shader, FD_DIRTY_SHADER_TEX);
52 }
53 
54 void
fd_set_sampler_views(struct pipe_context * pctx,enum pipe_shader_type shader,unsigned start,unsigned nr,unsigned unbind_num_trailing_slots,bool take_ownership,struct pipe_sampler_view ** views)55 fd_set_sampler_views(struct pipe_context *pctx, enum pipe_shader_type shader,
56                      unsigned start, unsigned nr,
57                      unsigned unbind_num_trailing_slots,
58                      bool take_ownership,
59                      struct pipe_sampler_view **views) in_dt
60 {
61    struct fd_context *ctx = fd_context(pctx);
62    struct fd_texture_stateobj *tex = &ctx->tex[shader];
63    unsigned i;
64 
65    for (i = 0; i < nr; i++) {
66       struct pipe_sampler_view *view = views ? views[i] : NULL;
67       unsigned p = i + start;
68 
69       if (take_ownership) {
70          pipe_sampler_view_reference(&tex->textures[p], NULL);
71          tex->textures[p] = view;
72       } else {
73          pipe_sampler_view_reference(&tex->textures[p], view);
74       }
75 
76       if (tex->textures[p]) {
77          fd_resource_set_usage(tex->textures[p]->texture, FD_DIRTY_TEX);
78          fd_dirty_shader_resource(ctx, tex->textures[p]->texture,
79                                   shader, FD_DIRTY_SHADER_TEX, false);
80          tex->valid_textures |= (1 << p);
81       } else {
82          tex->valid_textures &= ~(1 << p);
83       }
84    }
85    for (; i < nr + unbind_num_trailing_slots; i++) {
86       unsigned p = i + start;
87       pipe_sampler_view_reference(&tex->textures[p], NULL);
88       tex->valid_textures &= ~(1 << p);
89    }
90 
91    tex->num_textures = util_last_bit(tex->valid_textures);
92 
93    fd_context_dirty_shader(ctx, shader, FD_DIRTY_SHADER_TEX);
94 }
95 
96 void
fd_texture_init(struct pipe_context * pctx)97 fd_texture_init(struct pipe_context *pctx)
98 {
99    if (!pctx->delete_sampler_state)
100       pctx->delete_sampler_state = fd_sampler_state_delete;
101    if (!pctx->sampler_view_destroy)
102       pctx->sampler_view_destroy = fd_sampler_view_destroy;
103 }
104 
105 /* helper for setting up border-color buffer for a3xx/a4xx: */
106 void
fd_setup_border_colors(struct fd_texture_stateobj * tex,void * ptr,unsigned offset)107 fd_setup_border_colors(struct fd_texture_stateobj *tex, void *ptr,
108                        unsigned offset)
109 {
110    unsigned i, j;
111 
112    for (i = 0; i < tex->num_samplers; i++) {
113       struct pipe_sampler_state *sampler = tex->samplers[i];
114       uint16_t *bcolor =
115          (uint16_t *)((uint8_t *)ptr + (BORDERCOLOR_SIZE * offset) +
116                       (BORDERCOLOR_SIZE * i));
117       uint32_t *bcolor32 = (uint32_t *)&bcolor[16];
118 
119       if (!sampler)
120          continue;
121 
122       enum pipe_format format = sampler->border_color_format;
123 
124       const struct util_format_description *desc =
125             util_format_description(sampler->border_color_format);
126       for (j = 0; j < 4; j++) {
127          if (desc->swizzle[j] >= 4)
128             continue;
129 
130          const struct util_format_channel_description *chan =
131                &desc->channel[desc->swizzle[j]];
132          uint8_t native = desc->swizzle[j];
133          /* Special cases:
134           *  - X24S8 is implemented with 8_8_8_8_UINT, so the 'native'
135           *    location is actually 0 rather than 1
136           *  - X32_S8X24_UINT has stencil with a secretly-S8_UINT resource
137           *    so again we want 0 rather than 1
138           *
139           * In both cases, there is only one non-void format, so we don't
140           * have to be too careful.
141           *
142           * Note that this only affects a4xx -- a3xx did not support
143           * stencil texturing, and a5xx+ don't use this helper.
144           */
145          if (format == PIPE_FORMAT_X24S8_UINT ||
146                format == PIPE_FORMAT_X32_S8X24_UINT) {
147             native = 0;
148          }
149 
150          if (chan->pure_integer) {
151             bcolor32[native + 4] = sampler->border_color.i[j];
152             bcolor[native + 8] = sampler->border_color.i[j];
153          } else {
154             bcolor32[native] = fui(sampler->border_color.f[j]);
155             bcolor[native] =
156                   _mesa_float_to_half(sampler->border_color.f[j]);
157          }
158       }
159    }
160 }
161