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    } else {
48       /* can this be non-const buffer_index?  how do we handle that? */
49       int ssbo_idx = nir_src_as_uint(src);
50       return create_immed(ctx->block, ssbo_idx);
51    }
52 }
53 
54 unsigned
ir3_ssbo_to_tex(struct ir3_ibo_mapping * mapping,unsigned ssbo)55 ir3_ssbo_to_tex(struct ir3_ibo_mapping *mapping, unsigned ssbo)
56 {
57    if (mapping->ssbo_to_tex[ssbo] == IBO_INVALID) {
58       unsigned tex = mapping->num_tex++;
59       mapping->ssbo_to_tex[ssbo] = tex;
60       mapping->tex_to_image[tex] = IBO_SSBO | ssbo;
61    }
62    return mapping->ssbo_to_tex[ssbo] + mapping->tex_base;
63 }
64 
65 struct ir3_instruction *
ir3_image_to_ibo(struct ir3_context * ctx,nir_src src)66 ir3_image_to_ibo(struct ir3_context *ctx, nir_src src)
67 {
68    if (ir3_bindless_resource(src)) {
69       ctx->so->bindless_ibo = true;
70       return ir3_get_src(ctx, &src)[0];
71    } else {
72       /* can this be non-const buffer_index?  how do we handle that? */
73       int image_idx = nir_src_as_uint(src);
74       return create_immed(ctx->block, ctx->s->info.num_ssbos + image_idx);
75    }
76 }
77 
78 unsigned
ir3_image_to_tex(struct ir3_ibo_mapping * mapping,unsigned image)79 ir3_image_to_tex(struct ir3_ibo_mapping *mapping, unsigned image)
80 {
81    if (mapping->image_to_tex[image] == IBO_INVALID) {
82       unsigned tex = mapping->num_tex++;
83       mapping->image_to_tex[image] = tex;
84       mapping->tex_to_image[tex] = image;
85    }
86    return mapping->image_to_tex[image] + mapping->tex_base;
87 }
88 
89 /* see tex_info() for equiv logic for texture instructions.. it would be
90  * nice if this could be better unified..
91  */
92 unsigned
ir3_get_image_coords(const nir_intrinsic_instr * instr,unsigned * flagsp)93 ir3_get_image_coords(const nir_intrinsic_instr *instr, unsigned *flagsp)
94 {
95    unsigned coords = nir_image_intrinsic_coord_components(instr);
96    unsigned flags = 0;
97 
98    if (coords == 3)
99       flags |= IR3_INSTR_3D;
100 
101    if (nir_intrinsic_image_array(instr))
102       flags |= IR3_INSTR_A;
103 
104    if (flagsp)
105       *flagsp = flags;
106 
107    return coords;
108 }
109 
110 type_t
ir3_get_type_for_image_intrinsic(const nir_intrinsic_instr * instr)111 ir3_get_type_for_image_intrinsic(const nir_intrinsic_instr *instr)
112 {
113    const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
114    int bit_size = info->has_dest ? nir_dest_bit_size(instr->dest) : 32;
115 
116    nir_alu_type type = nir_type_uint;
117    switch (instr->intrinsic) {
118    case nir_intrinsic_image_load:
119    case nir_intrinsic_bindless_image_load:
120       type = nir_alu_type_get_base_type(nir_intrinsic_dest_type(instr));
121       /* SpvOpAtomicLoad doesn't have dest type */
122       if (type == nir_type_invalid)
123          type = nir_type_uint;
124       break;
125 
126    case nir_intrinsic_image_store:
127    case nir_intrinsic_bindless_image_store:
128       type = nir_alu_type_get_base_type(nir_intrinsic_src_type(instr));
129       /* SpvOpAtomicStore doesn't have src type */
130       if (type == nir_type_invalid)
131          type = nir_type_uint;
132       break;
133 
134    case nir_intrinsic_image_atomic_add:
135    case nir_intrinsic_bindless_image_atomic_add:
136    case nir_intrinsic_image_atomic_umin:
137    case nir_intrinsic_bindless_image_atomic_umin:
138    case nir_intrinsic_image_atomic_umax:
139    case nir_intrinsic_bindless_image_atomic_umax:
140    case nir_intrinsic_image_atomic_and:
141    case nir_intrinsic_bindless_image_atomic_and:
142    case nir_intrinsic_image_atomic_or:
143    case nir_intrinsic_bindless_image_atomic_or:
144    case nir_intrinsic_image_atomic_xor:
145    case nir_intrinsic_bindless_image_atomic_xor:
146    case nir_intrinsic_image_atomic_exchange:
147    case nir_intrinsic_bindless_image_atomic_exchange:
148    case nir_intrinsic_image_atomic_comp_swap:
149    case nir_intrinsic_bindless_image_atomic_comp_swap:
150    case nir_intrinsic_image_atomic_inc_wrap:
151    case nir_intrinsic_bindless_image_atomic_inc_wrap:
152       type = nir_type_uint;
153       break;
154 
155    case nir_intrinsic_image_atomic_imin:
156    case nir_intrinsic_bindless_image_atomic_imin:
157    case nir_intrinsic_image_atomic_imax:
158    case nir_intrinsic_bindless_image_atomic_imax:
159       type = nir_type_int;
160       break;
161 
162    default:
163       unreachable("Unhandled NIR image intrinsic");
164    }
165 
166    switch (type) {
167    case nir_type_uint:
168       return bit_size == 16 ? TYPE_U16 : TYPE_U32;
169    case nir_type_int:
170       return bit_size == 16 ? TYPE_S16 : TYPE_S32;
171    case nir_type_float:
172       return bit_size == 16 ? TYPE_F16 : TYPE_F32;
173    default:
174       unreachable("bad type");
175    }
176 }
177 
178 /* Returns the number of components for the different image formats
179  * supported by the GLES 3.1 spec, plus those added by the
180  * GL_NV_image_formats extension.
181  */
182 unsigned
ir3_get_num_components_for_image_format(enum pipe_format format)183 ir3_get_num_components_for_image_format(enum pipe_format format)
184 {
185    if (format == PIPE_FORMAT_NONE)
186       return 4;
187    else
188       return util_format_get_nr_components(format);
189 }
190