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