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