st/mesa: translate additional flags in MemoryBarrier
[mesa.git] / src / mesa / state_tracker / st_atom_image.c
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 #include "main/imports.h"
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
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 static void
47 st_bind_images(struct st_context *st, struct gl_shader *shader,
48 unsigned shader_type)
49 {
50 unsigned i;
51 struct pipe_image_view images[MAX_IMAGE_UNIFORMS];
52 struct gl_program_constants *c;
53
54 if (!shader || !st->pipe->set_shader_images)
55 return;
56
57 c = &st->ctx->Const.Program[shader->Stage];
58
59 for (i = 0; i < shader->NumImages; i++) {
60 struct gl_image_unit *u = &st->ctx->ImageUnits[shader->ImageUnits[i]];
61 struct st_texture_object *stObj = st_texture_object(u->TexObj);
62 struct pipe_image_view *img = &images[i];
63
64 if (!_mesa_is_image_unit_valid(st->ctx, u) ||
65 !st_finalize_texture(st->ctx, st->pipe, u->TexObj) ||
66 !stObj->pt) {
67 memset(img, 0, sizeof(*img));
68 continue;
69 }
70
71 img->resource = stObj->pt;
72 img->format = st_mesa_format_to_pipe_format(st, u->_ActualFormat);
73
74 switch (u->Access) {
75 case GL_READ_ONLY:
76 img->access = PIPE_IMAGE_ACCESS_READ;
77 break;
78 case GL_WRITE_ONLY:
79 img->access = PIPE_IMAGE_ACCESS_WRITE;
80 break;
81 case GL_READ_WRITE:
82 img->access = PIPE_IMAGE_ACCESS_READ_WRITE;
83 break;
84 default:
85 unreachable("bad gl_image_unit::Access");
86 }
87
88 if (stObj->pt->target == PIPE_BUFFER) {
89 unsigned base, size;
90 unsigned f, n;
91 const struct util_format_description *desc
92 = util_format_description(img->format);
93
94 base = stObj->base.BufferOffset;
95 assert(base < stObj->pt->width0);
96 size = MIN2(stObj->pt->width0 - base, (unsigned)stObj->base.BufferSize);
97
98 f = (base / (desc->block.bits / 8)) * desc->block.width;
99 n = (size / (desc->block.bits / 8)) * desc->block.width;
100 assert(n > 0);
101 img->u.buf.first_element = f;
102 img->u.buf.last_element = f + (n - 1);
103 } else {
104 img->u.tex.level = u->Level + stObj->base.MinLevel;
105 if (stObj->pt->target == PIPE_TEXTURE_3D) {
106 if (u->Layered) {
107 img->u.tex.first_layer = 0;
108 img->u.tex.last_layer = u_minify(stObj->pt->depth0, img->u.tex.level) - 1;
109 } else {
110 img->u.tex.first_layer = u->_Layer;
111 img->u.tex.last_layer = u->_Layer;
112 }
113 } else {
114 img->u.tex.first_layer = u->_Layer + stObj->base.MinLayer;
115 img->u.tex.last_layer = u->_Layer + stObj->base.MinLayer;
116 if (u->Layered && img->resource->array_size > 1) {
117 if (stObj->base.Immutable)
118 img->u.tex.last_layer += stObj->base.NumLayers - 1;
119 else
120 img->u.tex.last_layer += img->resource->array_size - 1;
121 }
122 }
123 }
124 }
125 st->pipe->set_shader_images(st->pipe, shader_type, 0, shader->NumImages,
126 images);
127 /* clear out any stale shader images */
128 if (shader->NumImages < c->MaxImageUniforms)
129 st->pipe->set_shader_images(
130 st->pipe, shader_type,
131 shader->NumImages,
132 c->MaxImageUniforms - shader->NumImages,
133 NULL);
134 }
135
136 static void bind_vs_images(struct st_context *st)
137 {
138 struct gl_shader_program *prog =
139 st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
140
141 if (!prog)
142 return;
143
144 st_bind_images(st, prog->_LinkedShaders[MESA_SHADER_VERTEX], PIPE_SHADER_VERTEX);
145 }
146
147 const struct st_tracked_state st_bind_vs_images = {
148 "st_bind_vs_images",
149 {
150 0,
151 ST_NEW_VERTEX_PROGRAM | ST_NEW_IMAGE_UNITS,
152 },
153 bind_vs_images
154 };
155
156 static void bind_fs_images(struct st_context *st)
157 {
158 struct gl_shader_program *prog =
159 st->ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
160
161 if (!prog)
162 return;
163
164 st_bind_images(st, prog->_LinkedShaders[MESA_SHADER_FRAGMENT], PIPE_SHADER_FRAGMENT);
165 }
166
167 const struct st_tracked_state st_bind_fs_images = {
168 "st_bind_fs_images",
169 {
170 0,
171 ST_NEW_FRAGMENT_PROGRAM | ST_NEW_IMAGE_UNITS,
172 },
173 bind_fs_images
174 };
175
176 static void bind_gs_images(struct st_context *st)
177 {
178 struct gl_shader_program *prog =
179 st->ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
180
181 if (!prog)
182 return;
183
184 st_bind_images(st, prog->_LinkedShaders[MESA_SHADER_GEOMETRY], PIPE_SHADER_GEOMETRY);
185 }
186
187 const struct st_tracked_state st_bind_gs_images = {
188 "st_bind_gs_images",
189 {
190 0,
191 ST_NEW_GEOMETRY_PROGRAM | ST_NEW_IMAGE_UNITS,
192 },
193 bind_gs_images
194 };
195
196 static void bind_tcs_images(struct st_context *st)
197 {
198 struct gl_shader_program *prog =
199 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
200
201 if (!prog)
202 return;
203
204 st_bind_images(st, prog->_LinkedShaders[MESA_SHADER_TESS_CTRL], PIPE_SHADER_TESS_CTRL);
205 }
206
207 const struct st_tracked_state st_bind_tcs_images = {
208 "st_bind_tcs_images",
209 {
210 0,
211 ST_NEW_TESSCTRL_PROGRAM | ST_NEW_IMAGE_UNITS,
212 },
213 bind_tcs_images
214 };
215
216 static void bind_tes_images(struct st_context *st)
217 {
218 struct gl_shader_program *prog =
219 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
220
221 if (!prog)
222 return;
223
224 st_bind_images(st, prog->_LinkedShaders[MESA_SHADER_TESS_EVAL], PIPE_SHADER_TESS_EVAL);
225 }
226
227 const struct st_tracked_state st_bind_tes_images = {
228 "st_bind_tes_images",
229 {
230 0,
231 ST_NEW_TESSEVAL_PROGRAM | ST_NEW_IMAGE_UNITS,
232 },
233 bind_tes_images
234 };
235
236 static void bind_cs_images(struct st_context *st)
237 {
238 struct gl_shader_program *prog =
239 st->ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
240
241 if (!prog)
242 return;
243
244 st_bind_images(st, prog->_LinkedShaders[MESA_SHADER_COMPUTE], PIPE_SHADER_COMPUTE);
245 }
246
247 const struct st_tracked_state st_bind_cs_images = {
248 "st_bind_cs_images",
249 {
250 0,
251 ST_NEW_COMPUTE_PROGRAM | ST_NEW_IMAGE_UNITS,
252 },
253 bind_cs_images
254 };