• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
32 #include "pipe/p_context.h"
33 #include "pipe/p_defines.h"
34 #include "util/u_inlines.h"
35 #include "util/u_surface.h"
36 #include "cso_cache/cso_context.h"
37 
38 #include "st_cb_texture.h"
39 #include "st_debug.h"
40 #include "st_texture.h"
41 #include "st_context.h"
42 #include "st_atom.h"
43 #include "st_program.h"
44 #include "st_format.h"
45 
46 /**
47  * Convert a gl_image_unit object to a pipe_image_view object.
48  */
49 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)50 st_convert_image(const struct st_context *st, const struct gl_image_unit *u,
51                  struct pipe_image_view *img, enum gl_access_qualifier shader_access)
52 {
53    struct gl_texture_object *stObj = u->TexObj;
54 
55    img->format = st_mesa_format_to_pipe_format(st, u->_ActualFormat);
56 
57    switch (u->Access) {
58    case GL_READ_ONLY:
59       img->access = PIPE_IMAGE_ACCESS_READ;
60       break;
61    case GL_WRITE_ONLY:
62       img->access = PIPE_IMAGE_ACCESS_WRITE;
63       break;
64    case GL_READ_WRITE:
65       img->access = PIPE_IMAGE_ACCESS_READ_WRITE;
66       break;
67    default:
68       unreachable("bad gl_image_unit::Access");
69    }
70 
71    img->shader_access = 0;
72    if (!(shader_access & ACCESS_NON_READABLE))
73       img->shader_access |= PIPE_IMAGE_ACCESS_READ;
74    if (!(shader_access & ACCESS_NON_WRITEABLE))
75       img->shader_access |= PIPE_IMAGE_ACCESS_WRITE;
76    if (shader_access & ACCESS_COHERENT)
77       img->shader_access |= PIPE_IMAGE_ACCESS_COHERENT;
78    if (shader_access & ACCESS_VOLATILE)
79       img->shader_access |= PIPE_IMAGE_ACCESS_VOLATILE;
80 
81    if (stObj->Target == GL_TEXTURE_BUFFER) {
82       struct gl_buffer_object *stbuf = stObj->BufferObject;
83       unsigned base, size;
84 
85       if (!stbuf || !stbuf->buffer) {
86          memset(img, 0, sizeof(*img));
87          return;
88       }
89       struct pipe_resource *buf = stbuf->buffer;
90 
91       base = stObj->BufferOffset;
92       assert(base < buf->width0);
93       size = MIN2(buf->width0 - base, (unsigned)stObj->BufferSize);
94 
95       img->resource = stbuf->buffer;
96       img->u.buf.offset = base;
97       img->u.buf.size = size;
98    } else {
99       if (!st_finalize_texture(st->ctx, st->pipe, u->TexObj, 0) ||
100           !stObj->pt) {
101          memset(img, 0, sizeof(*img));
102          return;
103       }
104 
105       img->resource = stObj->pt;
106       img->u.tex.level = u->Level + stObj->Attrib.MinLevel;
107       img->u.tex.single_layer_view = !u->Layered;
108       assert(img->u.tex.level <= img->resource->last_level);
109       if (stObj->pt->target == PIPE_TEXTURE_3D) {
110          if (u->Layered) {
111             img->u.tex.first_layer = 0;
112             img->u.tex.last_layer = u_minify(stObj->pt->depth0, img->u.tex.level) - 1;
113          } else {
114             img->u.tex.first_layer = u->_Layer;
115             img->u.tex.last_layer = u->_Layer;
116             img->u.tex.is_2d_view_of_3d = true;
117          }
118       } else {
119          img->u.tex.first_layer = u->_Layer + stObj->Attrib.MinLayer;
120          img->u.tex.last_layer = u->_Layer + stObj->Attrib.MinLayer;
121          if (u->Layered && img->resource->array_size > 1) {
122             if (stObj->Immutable)
123                img->u.tex.last_layer += stObj->Attrib.NumLayers - 1;
124             else
125                img->u.tex.last_layer += img->resource->array_size - 1;
126          }
127       }
128    }
129 }
130 
131 /**
132  * Get a pipe_image_view object from an image unit.
133  */
134 void
st_convert_image_from_unit(const struct st_context * st,struct pipe_image_view * img,GLuint imgUnit,enum gl_access_qualifier image_access)135 st_convert_image_from_unit(const struct st_context *st,
136                            struct pipe_image_view *img,
137                            GLuint imgUnit,
138                            enum gl_access_qualifier image_access)
139 {
140    struct gl_image_unit *u = &st->ctx->ImageUnits[imgUnit];
141 
142    if (!_mesa_is_image_unit_valid(st->ctx, u)) {
143       memset(img, 0, sizeof(*img));
144       return;
145    }
146 
147    st_convert_image(st, u, img, image_access);
148 }
149 
150 static void
st_bind_images(struct st_context * st,struct gl_program * prog,enum pipe_shader_type shader_type)151 st_bind_images(struct st_context *st, struct gl_program *prog,
152                enum pipe_shader_type shader_type)
153 {
154    unsigned i;
155    struct pipe_image_view images[MAX_IMAGE_UNIFORMS];
156 
157    if (!prog || !st->pipe->set_shader_images)
158       return;
159 
160    unsigned num_images = prog->info.num_images;
161 
162    for (i = 0; i < num_images; i++) {
163       struct pipe_image_view *img = &images[i];
164 
165       st_convert_image_from_unit(st, img, prog->sh.ImageUnits[i],
166                                  prog->sh.image_access[i]);
167    }
168 
169    struct pipe_context *pipe = st->pipe;
170    unsigned last_num_images = st->state.num_images[shader_type];
171    unsigned unbind_slots = last_num_images > num_images ?
172                               last_num_images - num_images : 0;
173    pipe->set_shader_images(pipe, shader_type, 0, num_images, unbind_slots,
174                            images);
175    st->state.num_images[shader_type] = num_images;
176 }
177 
st_bind_vs_images(struct st_context * st)178 void st_bind_vs_images(struct st_context *st)
179 {
180    struct gl_program *prog =
181       st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
182 
183    st_bind_images(st, prog, PIPE_SHADER_VERTEX);
184 }
185 
st_bind_fs_images(struct st_context * st)186 void st_bind_fs_images(struct st_context *st)
187 {
188    struct gl_program *prog =
189       st->ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
190 
191    st_bind_images(st, prog, PIPE_SHADER_FRAGMENT);
192 }
193 
st_bind_gs_images(struct st_context * st)194 void st_bind_gs_images(struct st_context *st)
195 {
196    struct gl_program *prog =
197       st->ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
198 
199    st_bind_images(st, prog, PIPE_SHADER_GEOMETRY);
200 }
201 
st_bind_tcs_images(struct st_context * st)202 void st_bind_tcs_images(struct st_context *st)
203 {
204    struct gl_program *prog =
205       st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
206 
207    st_bind_images(st, prog, PIPE_SHADER_TESS_CTRL);
208 }
209 
st_bind_tes_images(struct st_context * st)210 void st_bind_tes_images(struct st_context *st)
211 {
212    struct gl_program *prog =
213       st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
214 
215    st_bind_images(st, prog, PIPE_SHADER_TESS_EVAL);
216 }
217 
st_bind_cs_images(struct st_context * st)218 void st_bind_cs_images(struct st_context *st)
219 {
220    struct gl_program *prog =
221       st->ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
222 
223    st_bind_images(st, prog, PIPE_SHADER_COMPUTE);
224 }
225