xref: /aosp_15_r20/external/mesa3d/src/mesa/state_tracker/st_atom_image.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2016 Ilia Mirkin. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sub license, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial portions
15  * of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  **************************************************************************/
26 
27 
28 #include "main/shaderimage.h"
29 #include "program/prog_parameter.h"
30 #include "program/prog_print.h"
31 #include "compiler/glsl/ir_uniform.h"
32 
33 #include "pipe/p_context.h"
34 #include "pipe/p_defines.h"
35 #include "util/u_inlines.h"
36 #include "util/u_surface.h"
37 #include "cso_cache/cso_context.h"
38 
39 #include "st_cb_texture.h"
40 #include "st_debug.h"
41 #include "st_texture.h"
42 #include "st_context.h"
43 #include "st_atom.h"
44 #include "st_program.h"
45 #include "st_format.h"
46 
47 /**
48  * Convert a gl_image_unit object to a pipe_image_view object.
49  */
50 void
st_convert_image(const struct st_context * st,const struct gl_image_unit * u,struct pipe_image_view * img,enum gl_access_qualifier shader_access)51 st_convert_image(const struct st_context *st, const struct gl_image_unit *u,
52                  struct pipe_image_view *img, enum gl_access_qualifier shader_access)
53 {
54    struct gl_texture_object *stObj = u->TexObj;
55 
56    img->format = st_mesa_format_to_pipe_format(st, u->_ActualFormat);
57 
58    switch (u->Access) {
59    case GL_READ_ONLY:
60       img->access = PIPE_IMAGE_ACCESS_READ;
61       break;
62    case GL_WRITE_ONLY:
63       img->access = PIPE_IMAGE_ACCESS_WRITE;
64       break;
65    case GL_READ_WRITE:
66       img->access = PIPE_IMAGE_ACCESS_READ_WRITE;
67       break;
68    default:
69       unreachable("bad gl_image_unit::Access");
70    }
71 
72    img->shader_access = 0;
73    if (!(shader_access & ACCESS_NON_READABLE))
74       img->shader_access |= PIPE_IMAGE_ACCESS_READ;
75    if (!(shader_access & ACCESS_NON_WRITEABLE))
76       img->shader_access |= PIPE_IMAGE_ACCESS_WRITE;
77    if (shader_access & ACCESS_COHERENT)
78       img->shader_access |= PIPE_IMAGE_ACCESS_COHERENT;
79    if (shader_access & ACCESS_VOLATILE)
80       img->shader_access |= PIPE_IMAGE_ACCESS_VOLATILE;
81 
82    if (stObj->Target == GL_TEXTURE_BUFFER) {
83       struct gl_buffer_object *stbuf = stObj->BufferObject;
84       unsigned base, size;
85 
86       if (!stbuf || !stbuf->buffer) {
87          memset(img, 0, sizeof(*img));
88          return;
89       }
90       struct pipe_resource *buf = stbuf->buffer;
91 
92       base = stObj->BufferOffset;
93       assert(base < buf->width0);
94       size = MIN2(buf->width0 - base, (unsigned)stObj->BufferSize);
95 
96       img->resource = stbuf->buffer;
97       img->u.buf.offset = base;
98       img->u.buf.size = size;
99    } else {
100       if (!st_finalize_texture(st->ctx, st->pipe, u->TexObj, 0) ||
101           !stObj->pt) {
102          memset(img, 0, sizeof(*img));
103          return;
104       }
105 
106       img->resource = stObj->pt;
107       img->u.tex.level = u->Level + stObj->Attrib.MinLevel;
108       img->u.tex.single_layer_view = !u->Layered;
109       assert(img->u.tex.level <= img->resource->last_level);
110       if (stObj->pt->target == PIPE_TEXTURE_3D) {
111          if (u->Layered) {
112             img->u.tex.first_layer = 0;
113             img->u.tex.last_layer = u_minify(stObj->pt->depth0, img->u.tex.level) - 1;
114          } else {
115             img->u.tex.first_layer = u->_Layer;
116             img->u.tex.last_layer = u->_Layer;
117             img->u.tex.is_2d_view_of_3d = true;
118          }
119       } else {
120          img->u.tex.first_layer = u->_Layer + stObj->Attrib.MinLayer;
121          img->u.tex.last_layer = u->_Layer + stObj->Attrib.MinLayer;
122          if (u->Layered && img->resource->array_size > 1) {
123             if (stObj->Immutable)
124                img->u.tex.last_layer += stObj->Attrib.NumLayers - 1;
125             else
126                img->u.tex.last_layer += img->resource->array_size - 1;
127          }
128       }
129    }
130 }
131 
132 /**
133  * Get a pipe_image_view object from an image unit.
134  */
135 void
st_convert_image_from_unit(const struct st_context * st,struct pipe_image_view * img,GLuint imgUnit,enum gl_access_qualifier image_access)136 st_convert_image_from_unit(const struct st_context *st,
137                            struct pipe_image_view *img,
138                            GLuint imgUnit,
139                            enum gl_access_qualifier image_access)
140 {
141    struct gl_image_unit *u = &st->ctx->ImageUnits[imgUnit];
142 
143    if (!_mesa_is_image_unit_valid(st->ctx, u)) {
144       memset(img, 0, sizeof(*img));
145       return;
146    }
147 
148    st_convert_image(st, u, img, image_access);
149 }
150 
151 static void
st_bind_images(struct st_context * st,struct gl_program * prog,enum pipe_shader_type shader_type)152 st_bind_images(struct st_context *st, struct gl_program *prog,
153                enum pipe_shader_type shader_type)
154 {
155    unsigned i;
156    struct pipe_image_view images[MAX_IMAGE_UNIFORMS];
157 
158    if (!prog || !st->pipe->set_shader_images)
159       return;
160 
161    unsigned num_images = prog->info.num_images;
162 
163    for (i = 0; i < num_images; i++) {
164       struct pipe_image_view *img = &images[i];
165 
166       st_convert_image_from_unit(st, img, prog->sh.ImageUnits[i],
167                                  prog->sh.image_access[i]);
168    }
169 
170    struct pipe_context *pipe = st->pipe;
171    unsigned last_num_images = st->state.num_images[shader_type];
172    unsigned unbind_slots = last_num_images > num_images ?
173                               last_num_images - num_images : 0;
174    pipe->set_shader_images(pipe, shader_type, 0, num_images, unbind_slots,
175                            images);
176    st->state.num_images[shader_type] = num_images;
177 }
178 
st_bind_vs_images(struct st_context * st)179 void st_bind_vs_images(struct st_context *st)
180 {
181    struct gl_program *prog =
182       st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
183 
184    st_bind_images(st, prog, PIPE_SHADER_VERTEX);
185 }
186 
st_bind_fs_images(struct st_context * st)187 void st_bind_fs_images(struct st_context *st)
188 {
189    struct gl_program *prog =
190       st->ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
191 
192    st_bind_images(st, prog, PIPE_SHADER_FRAGMENT);
193 }
194 
st_bind_gs_images(struct st_context * st)195 void st_bind_gs_images(struct st_context *st)
196 {
197    struct gl_program *prog =
198       st->ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
199 
200    st_bind_images(st, prog, PIPE_SHADER_GEOMETRY);
201 }
202 
st_bind_tcs_images(struct st_context * st)203 void st_bind_tcs_images(struct st_context *st)
204 {
205    struct gl_program *prog =
206       st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
207 
208    st_bind_images(st, prog, PIPE_SHADER_TESS_CTRL);
209 }
210 
st_bind_tes_images(struct st_context * st)211 void st_bind_tes_images(struct st_context *st)
212 {
213    struct gl_program *prog =
214       st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
215 
216    st_bind_images(st, prog, PIPE_SHADER_TESS_EVAL);
217 }
218 
st_bind_cs_images(struct st_context * st)219 void st_bind_cs_images(struct st_context *st)
220 {
221    struct gl_program *prog =
222       st->ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
223 
224    st_bind_images(st, prog, PIPE_SHADER_COMPUTE);
225 }
226