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