• 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 #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,unsigned shader_access)51 st_convert_image(const struct st_context *st, const struct gl_image_unit *u,
52                  struct pipe_image_view *img, unsigned 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    switch (shader_access) {
73    case GL_NONE:
74       img->shader_access = 0;
75       break;
76    case GL_READ_ONLY:
77       img->shader_access = PIPE_IMAGE_ACCESS_READ;
78       break;
79    case GL_WRITE_ONLY:
80       img->shader_access = PIPE_IMAGE_ACCESS_WRITE;
81       break;
82    case GL_READ_WRITE:
83       img->shader_access = PIPE_IMAGE_ACCESS_READ_WRITE;
84       break;
85    default:
86       unreachable("bad gl_image_unit::Access");
87    }
88 
89    if (stObj->Target == GL_TEXTURE_BUFFER) {
90       struct gl_buffer_object *stbuf = stObj->BufferObject;
91       unsigned base, size;
92 
93       if (!stbuf || !stbuf->buffer) {
94          memset(img, 0, sizeof(*img));
95          return;
96       }
97       struct pipe_resource *buf = stbuf->buffer;
98 
99       base = stObj->BufferOffset;
100       assert(base < buf->width0);
101       size = MIN2(buf->width0 - base, (unsigned)stObj->BufferSize);
102 
103       img->resource = stbuf->buffer;
104       img->u.buf.offset = base;
105       img->u.buf.size = size;
106    } else {
107       if (!st_finalize_texture(st->ctx, st->pipe, u->TexObj, 0) ||
108           !stObj->pt) {
109          memset(img, 0, sizeof(*img));
110          return;
111       }
112 
113       img->resource = stObj->pt;
114       img->u.tex.level = u->Level + stObj->Attrib.MinLevel;
115       assert(img->u.tex.level <= img->resource->last_level);
116       if (stObj->pt->target == PIPE_TEXTURE_3D) {
117          if (u->Layered) {
118             img->u.tex.first_layer = 0;
119             img->u.tex.last_layer = u_minify(stObj->pt->depth0, img->u.tex.level) - 1;
120          } else {
121             img->u.tex.first_layer = u->_Layer;
122             img->u.tex.last_layer = u->_Layer;
123          }
124       } else {
125          img->u.tex.first_layer = u->_Layer + stObj->Attrib.MinLayer;
126          img->u.tex.last_layer = u->_Layer + stObj->Attrib.MinLayer;
127          if (u->Layered && img->resource->array_size > 1) {
128             if (stObj->Immutable)
129                img->u.tex.last_layer += stObj->Attrib.NumLayers - 1;
130             else
131                img->u.tex.last_layer += img->resource->array_size - 1;
132          }
133       }
134    }
135 }
136 
137 /**
138  * Get a pipe_image_view object from an image unit.
139  */
140 void
st_convert_image_from_unit(const struct st_context * st,struct pipe_image_view * img,GLuint imgUnit,unsigned shader_access)141 st_convert_image_from_unit(const struct st_context *st,
142                            struct pipe_image_view *img,
143                            GLuint imgUnit,
144                            unsigned shader_access)
145 {
146    struct gl_image_unit *u = &st->ctx->ImageUnits[imgUnit];
147 
148    if (!_mesa_is_image_unit_valid(st->ctx, u)) {
149       memset(img, 0, sizeof(*img));
150       return;
151    }
152 
153    st_convert_image(st, u, img, shader_access);
154 }
155 
156 static void
st_bind_images(struct st_context * st,struct gl_program * prog,enum pipe_shader_type shader_type)157 st_bind_images(struct st_context *st, struct gl_program *prog,
158                enum pipe_shader_type shader_type)
159 {
160    unsigned i;
161    struct pipe_image_view images[MAX_IMAGE_UNIFORMS];
162 
163    if (!prog || !st->pipe->set_shader_images)
164       return;
165 
166    unsigned num_images = prog->info.num_images;
167 
168    for (i = 0; i < num_images; i++) {
169       struct pipe_image_view *img = &images[i];
170 
171       st_convert_image_from_unit(st, img, prog->sh.ImageUnits[i],
172                                  prog->sh.ImageAccess[i]);
173    }
174 
175    struct pipe_context *pipe = st->pipe;
176    unsigned last_num_images = st->state.num_images[shader_type];
177    unsigned unbind_slots = last_num_images > num_images ?
178                               last_num_images - num_images : 0;
179    pipe->set_shader_images(pipe, shader_type, 0, num_images, unbind_slots,
180                            images);
181    st->state.num_images[shader_type] = num_images;
182 }
183 
st_bind_vs_images(struct st_context * st)184 void st_bind_vs_images(struct st_context *st)
185 {
186    struct gl_program *prog =
187       st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
188 
189    st_bind_images(st, prog, PIPE_SHADER_VERTEX);
190 }
191 
st_bind_fs_images(struct st_context * st)192 void st_bind_fs_images(struct st_context *st)
193 {
194    struct gl_program *prog =
195       st->ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
196 
197    st_bind_images(st, prog, PIPE_SHADER_FRAGMENT);
198 }
199 
st_bind_gs_images(struct st_context * st)200 void st_bind_gs_images(struct st_context *st)
201 {
202    struct gl_program *prog =
203       st->ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
204 
205    st_bind_images(st, prog, PIPE_SHADER_GEOMETRY);
206 }
207 
st_bind_tcs_images(struct st_context * st)208 void st_bind_tcs_images(struct st_context *st)
209 {
210    struct gl_program *prog =
211       st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
212 
213    st_bind_images(st, prog, PIPE_SHADER_TESS_CTRL);
214 }
215 
st_bind_tes_images(struct st_context * st)216 void st_bind_tes_images(struct st_context *st)
217 {
218    struct gl_program *prog =
219       st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
220 
221    st_bind_images(st, prog, PIPE_SHADER_TESS_EVAL);
222 }
223 
st_bind_cs_images(struct st_context * st)224 void st_bind_cs_images(struct st_context *st)
225 {
226    struct gl_program *prog =
227       st->ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
228 
229    st_bind_images(st, prog, PIPE_SHADER_COMPUTE);
230 }
231