turnip: rework format_to_ifmt
[mesa.git] / src / freedreno / ir3 / ir3_image.c
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
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 *
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
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 *
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
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
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
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 enum pipe_format format = nir_intrinsic_format(instr);
117
118 if (util_format_is_pure_uint(format))
119 return bit_size == 16 ? TYPE_U16 : TYPE_U32;
120 else if (util_format_is_pure_sint(format))
121 return bit_size == 16 ? TYPE_S16 : TYPE_S32;
122 else
123 return bit_size == 16 ? TYPE_F16 : TYPE_F32;
124 }
125
126 /* Returns the number of components for the different image formats
127 * supported by the GLES 3.1 spec, plus those added by the
128 * GL_NV_image_formats extension.
129 */
130 unsigned
131 ir3_get_num_components_for_image_format(enum pipe_format format)
132 {
133 if (format == PIPE_FORMAT_NONE)
134 return 4;
135 else
136 return util_format_get_nr_components(format);
137 }