st/mesa: remove struct st_tracked_state
[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 /**
48 * Convert a gl_image_unit object to a pipe_image_view object.
49 */
50 void
51 st_convert_image(const struct st_context *st, const struct gl_image_unit *u,
52 struct pipe_image_view *img)
53 {
54 struct st_texture_object *stObj = st_texture_object(u->TexObj);
55
56 img->resource = stObj->pt;
57 img->format = st_mesa_format_to_pipe_format(st, u->_ActualFormat);
58
59 switch (u->Access) {
60 case GL_READ_ONLY:
61 img->access = PIPE_IMAGE_ACCESS_READ;
62 break;
63 case GL_WRITE_ONLY:
64 img->access = PIPE_IMAGE_ACCESS_WRITE;
65 break;
66 case GL_READ_WRITE:
67 img->access = PIPE_IMAGE_ACCESS_READ_WRITE;
68 break;
69 default:
70 unreachable("bad gl_image_unit::Access");
71 }
72
73 if (stObj->pt->target == PIPE_BUFFER) {
74 unsigned base, size;
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 img->u.buf.offset = base;
81 img->u.buf.size = size;
82 } else {
83 img->u.tex.level = u->Level + stObj->base.MinLevel;
84 if (stObj->pt->target == PIPE_TEXTURE_3D) {
85 if (u->Layered) {
86 img->u.tex.first_layer = 0;
87 img->u.tex.last_layer = u_minify(stObj->pt->depth0, img->u.tex.level) - 1;
88 } else {
89 img->u.tex.first_layer = u->_Layer;
90 img->u.tex.last_layer = u->_Layer;
91 }
92 } else {
93 img->u.tex.first_layer = u->_Layer + stObj->base.MinLayer;
94 img->u.tex.last_layer = u->_Layer + stObj->base.MinLayer;
95 if (u->Layered && img->resource->array_size > 1) {
96 if (stObj->base.Immutable)
97 img->u.tex.last_layer += stObj->base.NumLayers - 1;
98 else
99 img->u.tex.last_layer += img->resource->array_size - 1;
100 }
101 }
102 }
103 }
104
105 static void
106 st_bind_images(struct st_context *st, struct gl_program *prog,
107 enum pipe_shader_type shader_type)
108 {
109 unsigned i;
110 struct pipe_image_view images[MAX_IMAGE_UNIFORMS];
111 struct gl_program_constants *c;
112
113 if (!prog || !st->pipe->set_shader_images)
114 return;
115
116 c = &st->ctx->Const.Program[prog->info.stage];
117
118 for (i = 0; i < prog->info.num_images; i++) {
119 struct gl_image_unit *u =
120 &st->ctx->ImageUnits[prog->sh.ImageUnits[i]];
121 struct st_texture_object *stObj = st_texture_object(u->TexObj);
122 struct pipe_image_view *img = &images[i];
123
124 if (!_mesa_is_image_unit_valid(st->ctx, u) ||
125 !st_finalize_texture(st->ctx, st->pipe, u->TexObj, 0) ||
126 !stObj->pt) {
127 memset(img, 0, sizeof(*img));
128 continue;
129 }
130
131 st_convert_image(st, u, img);
132 }
133 cso_set_shader_images(st->cso_context, shader_type, 0,
134 prog->info.num_images, images);
135 /* clear out any stale shader images */
136 if (prog->info.num_images < c->MaxImageUniforms)
137 cso_set_shader_images(
138 st->cso_context, shader_type, prog->info.num_images,
139 c->MaxImageUniforms - prog->info.num_images, NULL);
140 }
141
142 void st_bind_vs_images(struct st_context *st)
143 {
144 struct gl_program *prog =
145 st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
146
147 st_bind_images(st, prog, PIPE_SHADER_VERTEX);
148 }
149
150 void st_bind_fs_images(struct st_context *st)
151 {
152 struct gl_program *prog =
153 st->ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
154
155 st_bind_images(st, prog, PIPE_SHADER_FRAGMENT);
156 }
157
158 void st_bind_gs_images(struct st_context *st)
159 {
160 struct gl_program *prog =
161 st->ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
162
163 st_bind_images(st, prog, PIPE_SHADER_GEOMETRY);
164 }
165
166 void st_bind_tcs_images(struct st_context *st)
167 {
168 struct gl_program *prog =
169 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
170
171 st_bind_images(st, prog, PIPE_SHADER_TESS_CTRL);
172 }
173
174 void st_bind_tes_images(struct st_context *st)
175 {
176 struct gl_program *prog =
177 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
178
179 st_bind_images(st, prog, PIPE_SHADER_TESS_EVAL);
180 }
181
182 void st_bind_cs_images(struct st_context *st)
183 {
184 struct gl_program *prog =
185 st->ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
186
187 st_bind_images(st, prog, PIPE_SHADER_COMPUTE);
188 }