1 /**************************************************************************
3 * Copyright 2016 Ilia Mirkin. All Rights Reserved.
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:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
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.
25 **************************************************************************/
28 #include "main/shaderimage.h"
29 #include "program/prog_parameter.h"
30 #include "program/prog_print.h"
31 #include "compiler/glsl/ir_uniform.h"
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"
39 #include "st_cb_bufferobjects.h"
40 #include "st_cb_texture.h"
42 #include "st_texture.h"
43 #include "st_context.h"
45 #include "st_program.h"
46 #include "st_format.h"
49 * Convert a gl_image_unit object to a pipe_image_view object.
52 st_convert_image(const struct st_context
*st
, const struct gl_image_unit
*u
,
53 struct pipe_image_view
*img
, unsigned shader_access
)
55 struct st_texture_object
*stObj
= st_texture_object(u
->TexObj
);
57 img
->format
= st_mesa_format_to_pipe_format(st
, u
->_ActualFormat
);
61 img
->access
= PIPE_IMAGE_ACCESS_READ
;
64 img
->access
= PIPE_IMAGE_ACCESS_WRITE
;
67 img
->access
= PIPE_IMAGE_ACCESS_READ_WRITE
;
70 unreachable("bad gl_image_unit::Access");
73 switch (shader_access
) {
75 img
->shader_access
= 0;
78 img
->shader_access
= PIPE_IMAGE_ACCESS_READ
;
81 img
->shader_access
= PIPE_IMAGE_ACCESS_WRITE
;
84 img
->shader_access
= PIPE_IMAGE_ACCESS_READ_WRITE
;
87 unreachable("bad gl_image_unit::Access");
90 if (stObj
->base
.Target
== GL_TEXTURE_BUFFER
) {
91 struct st_buffer_object
*stbuf
=
92 st_buffer_object(stObj
->base
.BufferObject
);
95 if (!stbuf
|| !stbuf
->buffer
) {
96 memset(img
, 0, sizeof(*img
));
99 struct pipe_resource
*buf
= stbuf
->buffer
;
101 base
= stObj
->base
.BufferOffset
;
102 assert(base
< buf
->width0
);
103 size
= MIN2(buf
->width0
- base
, (unsigned)stObj
->base
.BufferSize
);
105 img
->resource
= stbuf
->buffer
;
106 img
->u
.buf
.offset
= base
;
107 img
->u
.buf
.size
= size
;
109 if (!st_finalize_texture(st
->ctx
, st
->pipe
, u
->TexObj
, 0) ||
111 memset(img
, 0, sizeof(*img
));
115 img
->resource
= stObj
->pt
;
116 img
->u
.tex
.level
= u
->Level
+ stObj
->base
.MinLevel
;
117 assert(img
->u
.tex
.level
<= img
->resource
->last_level
);
118 if (stObj
->pt
->target
== PIPE_TEXTURE_3D
) {
120 img
->u
.tex
.first_layer
= 0;
121 img
->u
.tex
.last_layer
= u_minify(stObj
->pt
->depth0
, img
->u
.tex
.level
) - 1;
123 img
->u
.tex
.first_layer
= u
->_Layer
;
124 img
->u
.tex
.last_layer
= u
->_Layer
;
127 img
->u
.tex
.first_layer
= u
->_Layer
+ stObj
->base
.MinLayer
;
128 img
->u
.tex
.last_layer
= u
->_Layer
+ stObj
->base
.MinLayer
;
129 if (u
->Layered
&& img
->resource
->array_size
> 1) {
130 if (stObj
->base
.Immutable
)
131 img
->u
.tex
.last_layer
+= stObj
->base
.NumLayers
- 1;
133 img
->u
.tex
.last_layer
+= img
->resource
->array_size
- 1;
140 * Get a pipe_image_view object from an image unit.
143 st_convert_image_from_unit(const struct st_context
*st
,
144 struct pipe_image_view
*img
,
146 unsigned shader_access
)
148 struct gl_image_unit
*u
= &st
->ctx
->ImageUnits
[imgUnit
];
150 if (!_mesa_is_image_unit_valid(st
->ctx
, u
)) {
151 memset(img
, 0, sizeof(*img
));
155 st_convert_image(st
, u
, img
, shader_access
);
159 st_bind_images(struct st_context
*st
, struct gl_program
*prog
,
160 enum pipe_shader_type shader_type
)
163 struct pipe_image_view images
[MAX_IMAGE_UNIFORMS
];
164 struct gl_program_constants
*c
;
166 if (!prog
|| !st
->pipe
->set_shader_images
)
169 c
= &st
->ctx
->Const
.Program
[prog
->info
.stage
];
171 for (i
= 0; i
< prog
->info
.num_images
; i
++) {
172 struct pipe_image_view
*img
= &images
[i
];
174 st_convert_image_from_unit(st
, img
, prog
->sh
.ImageUnits
[i
],
175 prog
->sh
.ImageAccess
[i
]);
177 cso_set_shader_images(st
->cso_context
, shader_type
, 0,
178 prog
->info
.num_images
, images
);
179 /* clear out any stale shader images */
180 if (prog
->info
.num_images
< c
->MaxImageUniforms
)
181 cso_set_shader_images(
182 st
->cso_context
, shader_type
, prog
->info
.num_images
,
183 c
->MaxImageUniforms
- prog
->info
.num_images
, NULL
);
186 void st_bind_vs_images(struct st_context
*st
)
188 struct gl_program
*prog
=
189 st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
];
191 st_bind_images(st
, prog
, PIPE_SHADER_VERTEX
);
194 void st_bind_fs_images(struct st_context
*st
)
196 struct gl_program
*prog
=
197 st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
];
199 st_bind_images(st
, prog
, PIPE_SHADER_FRAGMENT
);
202 void st_bind_gs_images(struct st_context
*st
)
204 struct gl_program
*prog
=
205 st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
];
207 st_bind_images(st
, prog
, PIPE_SHADER_GEOMETRY
);
210 void st_bind_tcs_images(struct st_context
*st
)
212 struct gl_program
*prog
=
213 st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_CTRL
];
215 st_bind_images(st
, prog
, PIPE_SHADER_TESS_CTRL
);
218 void st_bind_tes_images(struct st_context
*st
)
220 struct gl_program
*prog
=
221 st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
];
223 st_bind_images(st
, prog
, PIPE_SHADER_TESS_EVAL
);
226 void st_bind_cs_images(struct st_context
*st
)
228 struct gl_program
*prog
=
229 st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_COMPUTE
];
231 st_bind_images(st
, prog
, PIPE_SHADER_COMPUTE
);