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