1 /*
2 * Copyright (C) 2017-2018 Rob Clark <robclark@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "ir3_image.h"
28
29 /*
30 * SSBO/Image to/from IBO/tex hw mapping table:
31 */
32
33 void
ir3_ibo_mapping_init(struct ir3_ibo_mapping * mapping,unsigned num_textures)34 ir3_ibo_mapping_init(struct ir3_ibo_mapping *mapping, unsigned num_textures)
35 {
36 memset(mapping, IBO_INVALID, sizeof(*mapping));
37 mapping->num_tex = 0;
38 mapping->tex_base = num_textures;
39 }
40
41 struct ir3_instruction *
ir3_ssbo_to_ibo(struct ir3_context * ctx,nir_src src)42 ir3_ssbo_to_ibo(struct ir3_context *ctx, nir_src src)
43 {
44 if (ir3_bindless_resource(src))
45 ctx->so->bindless_ibo = true;
46 return ir3_get_src(ctx, &src)[0];
47 }
48
49 unsigned
ir3_ssbo_to_tex(struct ir3_ibo_mapping * mapping,unsigned ssbo)50 ir3_ssbo_to_tex(struct ir3_ibo_mapping *mapping, unsigned ssbo)
51 {
52 if (mapping->ssbo_to_tex[ssbo] == IBO_INVALID) {
53 unsigned tex = mapping->num_tex++;
54 mapping->ssbo_to_tex[ssbo] = tex;
55 mapping->tex_to_image[tex] = IBO_SSBO | ssbo;
56 }
57 return mapping->ssbo_to_tex[ssbo] + mapping->tex_base;
58 }
59
60 struct ir3_instruction *
ir3_image_to_ibo(struct ir3_context * ctx,nir_src src)61 ir3_image_to_ibo(struct ir3_context *ctx, nir_src src)
62 {
63 if (ir3_bindless_resource(src)) {
64 ctx->so->bindless_ibo = true;
65 return ir3_get_src(ctx, &src)[0];
66 }
67
68 if (nir_src_is_const(src)) {
69 int image_idx = nir_src_as_uint(src);
70 return create_immed(ctx->block, ctx->s->info.num_ssbos + image_idx);
71 } else {
72 struct ir3_instruction *image_idx = ir3_get_src(ctx, &src)[0];
73 if (ctx->s->info.num_ssbos) {
74 return ir3_ADD_U(ctx->block,
75 image_idx, 0,
76 create_immed(ctx->block, ctx->s->info.num_ssbos), 0);
77 } else {
78 return image_idx;
79 }
80 }
81 }
82
83 unsigned
ir3_image_to_tex(struct ir3_ibo_mapping * mapping,unsigned image)84 ir3_image_to_tex(struct ir3_ibo_mapping *mapping, unsigned image)
85 {
86 if (mapping->image_to_tex[image] == IBO_INVALID) {
87 unsigned tex = mapping->num_tex++;
88 mapping->image_to_tex[image] = tex;
89 mapping->tex_to_image[tex] = image;
90 }
91 return mapping->image_to_tex[image] + mapping->tex_base;
92 }
93
94 /* see tex_info() for equiv logic for texture instructions.. it would be
95 * nice if this could be better unified..
96 */
97 unsigned
ir3_get_image_coords(const nir_intrinsic_instr * instr,unsigned * flagsp)98 ir3_get_image_coords(const nir_intrinsic_instr *instr, unsigned *flagsp)
99 {
100 enum glsl_sampler_dim dim = nir_intrinsic_image_dim(instr);
101 unsigned coords = nir_image_intrinsic_coord_components(instr);
102 unsigned flags = 0;
103
104 if (dim == GLSL_SAMPLER_DIM_CUBE || nir_intrinsic_image_array(instr))
105 flags |= IR3_INSTR_A;
106 else if (dim == GLSL_SAMPLER_DIM_3D)
107 flags |= IR3_INSTR_3D;
108
109 if (flagsp)
110 *flagsp = flags;
111
112 return coords;
113 }
114
115 type_t
ir3_get_type_for_image_intrinsic(const nir_intrinsic_instr * instr)116 ir3_get_type_for_image_intrinsic(const nir_intrinsic_instr *instr)
117 {
118 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
119 int bit_size = info->has_dest ? instr->def.bit_size : nir_src_bit_size(instr->src[3]);
120
121 nir_alu_type type = nir_type_uint;
122 switch (instr->intrinsic) {
123 case nir_intrinsic_image_load:
124 case nir_intrinsic_bindless_image_load:
125 type = nir_alu_type_get_base_type(nir_intrinsic_dest_type(instr));
126 /* SpvOpAtomicLoad doesn't have dest type */
127 if (type == nir_type_invalid)
128 type = nir_type_uint;
129 break;
130
131 case nir_intrinsic_image_store:
132 case nir_intrinsic_bindless_image_store:
133 type = nir_alu_type_get_base_type(nir_intrinsic_src_type(instr));
134 /* SpvOpAtomicStore doesn't have src type */
135 if (type == nir_type_invalid)
136 type = nir_type_uint;
137 break;
138
139 case nir_intrinsic_image_atomic:
140 case nir_intrinsic_bindless_image_atomic:
141 case nir_intrinsic_image_atomic_swap:
142 case nir_intrinsic_bindless_image_atomic_swap:
143 type = nir_atomic_op_type(nir_intrinsic_atomic_op(instr));
144 break;
145
146 default:
147 unreachable("Unhandled NIR image intrinsic");
148 }
149
150 switch (type) {
151 case nir_type_uint:
152 return bit_size == 16 ? TYPE_U16 : TYPE_U32;
153 case nir_type_int:
154 return bit_size == 16 ? TYPE_S16 : TYPE_S32;
155 case nir_type_float:
156 return bit_size == 16 ? TYPE_F16 : TYPE_F32;
157 default:
158 unreachable("bad type");
159 }
160 }
161
162 /* Returns the number of components for the different image formats
163 * supported by the GLES 3.1 spec, plus those added by the
164 * GL_NV_image_formats extension.
165 */
166 unsigned
ir3_get_num_components_for_image_format(enum pipe_format format)167 ir3_get_num_components_for_image_format(enum pipe_format format)
168 {
169 if (format == PIPE_FORMAT_NONE)
170 return 4;
171 else
172 return util_format_get_nr_components(format);
173 }
174