st/mesa: add support for ARB_sample_locations
[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_bufferobjects.h"
40 #include "st_cb_texture.h"
41 #include "st_debug.h"
42 #include "st_texture.h"
43 #include "st_context.h"
44 #include "st_atom.h"
45 #include "st_program.h"
46 #include "st_format.h"
47
48 /**
49 * Convert a gl_image_unit object to a pipe_image_view object.
50 */
51 void
52 st_convert_image(const struct st_context *st, const struct gl_image_unit *u,
53 struct pipe_image_view *img)
54 {
55 struct st_texture_object *stObj = st_texture_object(u->TexObj);
56
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->base.Target == GL_TEXTURE_BUFFER) {
74 struct st_buffer_object *stbuf =
75 st_buffer_object(stObj->base.BufferObject);
76 unsigned base, size;
77
78 if (!stbuf || !stbuf->buffer) {
79 memset(img, 0, sizeof(*img));
80 return;
81 }
82 struct pipe_resource *buf = stbuf->buffer;
83
84 base = stObj->base.BufferOffset;
85 assert(base < buf->width0);
86 size = MIN2(buf->width0 - base, (unsigned)stObj->base.BufferSize);
87
88 img->resource = stbuf->buffer;
89 img->u.buf.offset = base;
90 img->u.buf.size = size;
91 } else {
92 if (!st_finalize_texture(st->ctx, st->pipe, u->TexObj, 0) ||
93 !stObj->pt) {
94 memset(img, 0, sizeof(*img));
95 return;
96 }
97
98 img->resource = stObj->pt;
99 img->u.tex.level = u->Level + stObj->base.MinLevel;
100 assert(img->u.tex.level <= img->resource->last_level);
101 if (stObj->pt->target == PIPE_TEXTURE_3D) {
102 if (u->Layered) {
103 img->u.tex.first_layer = 0;
104 img->u.tex.last_layer = u_minify(stObj->pt->depth0, img->u.tex.level) - 1;
105 } else {
106 img->u.tex.first_layer = u->_Layer;
107 img->u.tex.last_layer = u->_Layer;
108 }
109 } else {
110 img->u.tex.first_layer = u->_Layer + stObj->base.MinLayer;
111 img->u.tex.last_layer = u->_Layer + stObj->base.MinLayer;
112 if (u->Layered && img->resource->array_size > 1) {
113 if (stObj->base.Immutable)
114 img->u.tex.last_layer += stObj->base.NumLayers - 1;
115 else
116 img->u.tex.last_layer += img->resource->array_size - 1;
117 }
118 }
119 }
120 }
121
122 /**
123 * Get a pipe_image_view object from an image unit.
124 */
125 void
126 st_convert_image_from_unit(const struct st_context *st,
127 struct pipe_image_view *img,
128 GLuint imgUnit)
129 {
130 struct gl_image_unit *u = &st->ctx->ImageUnits[imgUnit];
131
132 if (!_mesa_is_image_unit_valid(st->ctx, u)) {
133 memset(img, 0, sizeof(*img));
134 return;
135 }
136
137 st_convert_image(st, u, img);
138 }
139
140 static void
141 st_bind_images(struct st_context *st, struct gl_program *prog,
142 enum pipe_shader_type shader_type)
143 {
144 unsigned i;
145 struct pipe_image_view images[MAX_IMAGE_UNIFORMS];
146 struct gl_program_constants *c;
147
148 if (!prog || !st->pipe->set_shader_images)
149 return;
150
151 c = &st->ctx->Const.Program[prog->info.stage];
152
153 for (i = 0; i < prog->info.num_images; i++) {
154 struct pipe_image_view *img = &images[i];
155
156 st_convert_image_from_unit(st, img, prog->sh.ImageUnits[i]);
157 }
158 cso_set_shader_images(st->cso_context, shader_type, 0,
159 prog->info.num_images, images);
160 /* clear out any stale shader images */
161 if (prog->info.num_images < c->MaxImageUniforms)
162 cso_set_shader_images(
163 st->cso_context, shader_type, prog->info.num_images,
164 c->MaxImageUniforms - prog->info.num_images, NULL);
165 }
166
167 void st_bind_vs_images(struct st_context *st)
168 {
169 struct gl_program *prog =
170 st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
171
172 st_bind_images(st, prog, PIPE_SHADER_VERTEX);
173 }
174
175 void st_bind_fs_images(struct st_context *st)
176 {
177 struct gl_program *prog =
178 st->ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
179
180 st_bind_images(st, prog, PIPE_SHADER_FRAGMENT);
181 }
182
183 void st_bind_gs_images(struct st_context *st)
184 {
185 struct gl_program *prog =
186 st->ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
187
188 st_bind_images(st, prog, PIPE_SHADER_GEOMETRY);
189 }
190
191 void st_bind_tcs_images(struct st_context *st)
192 {
193 struct gl_program *prog =
194 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
195
196 st_bind_images(st, prog, PIPE_SHADER_TESS_CTRL);
197 }
198
199 void st_bind_tes_images(struct st_context *st)
200 {
201 struct gl_program *prog =
202 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
203
204 st_bind_images(st, prog, PIPE_SHADER_TESS_EVAL);
205 }
206
207 void st_bind_cs_images(struct st_context *st)
208 {
209 struct gl_program *prog =
210 st->ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
211
212 st_bind_images(st, prog, PIPE_SHADER_COMPUTE);
213 }