glsl,nir: Switch the enum representing shader image formats to PIPE_FORMAT.
[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 unsigned
43 ir3_ssbo_to_ibo(struct ir3_shader *shader, unsigned ssbo)
44 {
45 return ssbo;
46 }
47
48 unsigned
49 ir3_ssbo_to_tex(struct ir3_ibo_mapping *mapping, unsigned ssbo)
50 {
51 if (mapping->ssbo_to_tex[ssbo] == IBO_INVALID) {
52 unsigned tex = mapping->num_tex++;
53 mapping->ssbo_to_tex[ssbo] = tex;
54 mapping->tex_to_image[tex] = IBO_SSBO | ssbo;
55 }
56 return mapping->ssbo_to_tex[ssbo] + mapping->tex_base;
57 }
58
59 unsigned
60 ir3_image_to_ibo(struct ir3_shader *shader, unsigned image)
61 {
62 return shader->nir->info.num_ssbos + image;
63 }
64
65 unsigned
66 ir3_image_to_tex(struct ir3_ibo_mapping *mapping, unsigned image)
67 {
68 if (mapping->image_to_tex[image] == IBO_INVALID) {
69 unsigned tex = mapping->num_tex++;
70 mapping->image_to_tex[image] = tex;
71 mapping->tex_to_image[tex] = image;
72 }
73 return mapping->image_to_tex[image] + mapping->tex_base;
74 }
75
76 /* Helper to parse the deref for an image to get image slot. This should be
77 * mapped to tex or ibo idx using ir3_image_to_tex() or ir3_image_to_ibo().
78 */
79 unsigned
80 ir3_get_image_slot(nir_deref_instr *deref)
81 {
82 unsigned int loc = 0;
83 unsigned inner_size = 1;
84
85 while (deref->deref_type != nir_deref_type_var) {
86 assert(deref->deref_type == nir_deref_type_array);
87 unsigned const_index = nir_src_as_uint(deref->arr.index);
88
89 /* Go to the next instruction */
90 deref = nir_deref_instr_parent(deref);
91
92 assert(glsl_type_is_array(deref->type));
93 const unsigned array_len = glsl_get_length(deref->type);
94 loc += MIN2(const_index, array_len - 1) * inner_size;
95
96 /* Update the inner size */
97 inner_size *= array_len;
98 }
99
100 loc += deref->var->data.driver_location;
101
102 return loc;
103 }
104
105 /* see tex_info() for equiv logic for texture instructions.. it would be
106 * nice if this could be better unified..
107 */
108 unsigned
109 ir3_get_image_coords(const nir_variable *var, unsigned *flagsp)
110 {
111 const struct glsl_type *type = glsl_without_array(var->type);
112 unsigned coords = glsl_get_sampler_coordinate_components(type);
113 unsigned flags = 0;
114
115 if (coords == 3)
116 flags |= IR3_INSTR_3D;
117
118 if (glsl_sampler_type_is_array(type))
119 flags |= IR3_INSTR_A;
120
121 if (flagsp)
122 *flagsp = flags;
123
124 return coords;
125 }
126
127 type_t
128 ir3_get_image_type(const nir_variable *var)
129 {
130 switch (glsl_get_sampler_result_type(glsl_without_array(var->type))) {
131 case GLSL_TYPE_UINT:
132 return TYPE_U32;
133 case GLSL_TYPE_INT:
134 return TYPE_S32;
135 case GLSL_TYPE_FLOAT:
136 return TYPE_F32;
137 case GLSL_TYPE_UINT16:
138 return TYPE_U16;
139 case GLSL_TYPE_INT16:
140 return TYPE_S16;
141 case GLSL_TYPE_FLOAT16:
142 return TYPE_F16;
143 default:
144 unreachable("bad sampler type.");
145 return 0;
146 }
147 }
148
149 /* Returns the number of components for the different image formats
150 * supported by the GLES 3.1 spec, plus those added by the
151 * GL_NV_image_formats extension.
152 */
153 unsigned
154 ir3_get_num_components_for_image_format(GLuint format)
155 {
156 if (format == PIPE_FORMAT_NONE)
157 return 4;
158 else
159 return util_format_get_nr_components(format);
160 }