blob: 123a6f13f96a378d6359a3893501b4a45d49949e [file] [log] [blame]
Chia-I Wu1f7540b2014-08-22 13:56:18 +08001/*
2 * XGL
3 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#include "pipeline_priv.h"
26
27static struct intel_rmap_slot *rmap_get_slot(struct intel_rmap *rmap,
28 XGL_DESCRIPTOR_SET_SLOT_TYPE type,
29 XGL_UINT index)
30{
31 const XGL_UINT resource_offset = rmap->rt_count;
32 const XGL_UINT uav_offset = resource_offset + rmap->resource_count;
33 const XGL_UINT sampler_offset = uav_offset + rmap->uav_count;
34 struct intel_rmap_slot *slot;
35
36 switch (type) {
37 case XGL_SLOT_UNUSED:
38 slot = NULL;
39 break;
40 case XGL_SLOT_SHADER_RESOURCE:
41 slot = &rmap->slots[resource_offset + index];
42 break;
43 case XGL_SLOT_SHADER_UAV:
44 slot = &rmap->slots[uav_offset + index];
45 break;
46 case XGL_SLOT_SHADER_SAMPLER:
47 slot = &rmap->slots[sampler_offset + index];
48 break;
49 default:
50 assert(!"unknown rmap slot type");
51 slot = NULL;
52 break;
53 }
54
55 return slot;
56}
57
58static bool rmap_init_slots_with_path(struct intel_rmap *rmap,
59 const XGL_DESCRIPTOR_SET_MAPPING *mapping,
60 XGL_UINT *nest_path,
61 XGL_UINT nest_level)
62{
63 XGL_UINT i;
64
65 for (i = 0; i < mapping->descriptorCount; i++) {
66 const XGL_DESCRIPTOR_SLOT_INFO *info = &mapping->pDescriptorInfo[i];
67 struct intel_rmap_slot *slot;
68
69 if (info->slotObjectType == XGL_SLOT_NEXT_DESCRIPTOR_SET) {
70 nest_path[nest_level] = i;
71 if (!rmap_init_slots_with_path(rmap, info->pNextLevelSet,
72 nest_path, nest_level + 1))
73 return false;
74
75 continue;
76 }
77
78 slot = rmap_get_slot(rmap, info->slotObjectType,
79 info->shaderEntityIndex);
80 if (!slot)
81 continue;
82
83 assert(!slot->path_len);
84 slot->path_len = nest_level + 1;
85
86 if (nest_level) {
87 slot->u.path = icd_alloc(sizeof(slot->u.path[0]) *
88 slot->path_len, 0, XGL_SYSTEM_ALLOC_INTERNAL);
89 if (!slot->u.path) {
90 slot->path_len = 0;
91 return false;
92 }
93
94 memcpy(slot->u.path, nest_path,
95 sizeof(slot->u.path[0]) * nest_level);
96 slot->u.path[nest_level] = i;
97 } else {
98 slot->u.index = i;
99 }
100 }
101
102 return true;
103}
104
105static bool rmap_init_slots(struct intel_rmap *rmap,
106 const XGL_DESCRIPTOR_SET_MAPPING *mapping,
107 XGL_UINT depth)
108{
109 XGL_UINT *nest_path;
110 bool ok;
111
112 if (depth) {
113 nest_path = icd_alloc(sizeof(nest_path[0]) * depth,
114 0, XGL_SYSTEM_ALLOC_INTERNAL_TEMP);
115 if (!nest_path)
116 return false;
117 } else {
118 nest_path = NULL;
119 }
120
121 ok = rmap_init_slots_with_path(rmap, mapping, nest_path, 0);
122
123 if (nest_path)
124 icd_free(nest_path);
125
126 return ok;
127}
128
129static void rmap_update_count(struct intel_rmap *rmap,
130 XGL_DESCRIPTOR_SET_SLOT_TYPE type,
131 XGL_UINT index)
132{
133 switch (type) {
134 case XGL_SLOT_UNUSED:
135 break;
136 case XGL_SLOT_SHADER_RESOURCE:
137 if (rmap->resource_count < index + 1)
138 rmap->resource_count = index + 1;
139 break;
140 case XGL_SLOT_SHADER_UAV:
141 if (rmap->uav_count < index + 1)
142 rmap->uav_count = index + 1;
143 break;
144 case XGL_SLOT_SHADER_SAMPLER:
145 if (rmap->sampler_count < index + 1)
146 rmap->sampler_count = index + 1;
147 break;
148 default:
149 assert(!"unknown rmap slot type");
150 break;
151 }
152}
153
154static XGL_UINT rmap_init_counts(struct intel_rmap *rmap,
155 const XGL_DESCRIPTOR_SET_MAPPING *mapping)
156{
157 XGL_UINT depth = 0;
158 XGL_UINT i;
159
160 for (i = 0; i < mapping->descriptorCount; i++) {
161 const XGL_DESCRIPTOR_SLOT_INFO *info = &mapping->pDescriptorInfo[i];
162
163 if (info->slotObjectType == XGL_SLOT_NEXT_DESCRIPTOR_SET) {
164 const XGL_UINT d = rmap_init_counts(rmap,
165 info->pNextLevelSet);
166 if (depth < d + 1)
167 depth = d + 1;
168
169 continue;
170 }
171
172 rmap_update_count(rmap, info->slotObjectType,
173 info->shaderEntityIndex);
174 }
175
176 return depth;
177}
178
179struct intel_rmap *intel_rmap_create(struct intel_dev *dev,
180 const XGL_DESCRIPTOR_SET_MAPPING *mapping,
181 const XGL_DYNAMIC_MEMORY_VIEW_SLOT_INFO *dyn,
182 XGL_UINT rt_count)
183{
184 struct intel_rmap *rmap;
185 struct intel_rmap_slot *slot;
186 XGL_UINT depth, rt;
187
188 rmap = icd_alloc(sizeof(*rmap), 0, XGL_SYSTEM_ALLOC_INTERNAL);
189 if (!rmap)
190 return NULL;
191
192 memset(rmap, 0, sizeof(*rmap));
193
194 depth = rmap_init_counts(rmap, mapping);
195
196 /* add RTs and the dynamic memory view */
197 rmap_update_count(rmap, dyn->slotObjectType, dyn->shaderEntityIndex);
198 rmap->rt_count = rt_count;
199
200 rmap->slot_count = rmap->rt_count + rmap->resource_count +
201 rmap->uav_count + rmap->sampler_count;
202
203 rmap->slots = icd_alloc(sizeof(rmap->slots[0]) * rmap->slot_count,
204 0, XGL_SYSTEM_ALLOC_INTERNAL);
205 if (!rmap->slots) {
206 icd_free(rmap);
207 return NULL;
208 }
209
210 memset(rmap->slots, 0, sizeof(rmap->slots[0]) * rmap->slot_count);
211
212 if (!rmap_init_slots(rmap, mapping, depth)) {
213 intel_rmap_destroy(rmap);
214 return NULL;
215 }
216
217 /* add RTs and the dynamic memory view */
218 slot = rmap_get_slot(rmap, dyn->slotObjectType, dyn->shaderEntityIndex);
219 if (slot) {
220 slot->path_len = INTEL_RMAP_SLOT_DYN;
221 slot->u.index = 0;
222 }
223 for (rt = 0; rt < rmap->rt_count; rt++) {
224 slot = &rmap->slots[rt];
225 slot->path_len = INTEL_RMAP_SLOT_RT;
226 slot->u.index = rt;
227 }
228
229 return rmap;
230}
231
232void intel_rmap_destroy(struct intel_rmap *rmap)
233{
234 XGL_UINT i;
235
236 for (i = 0; i < rmap->slot_count; i++) {
237 struct intel_rmap_slot *slot = &rmap->slots[i];
238
239 switch (slot->path_len) {
240 case 0:
241 case 1:
242 case INTEL_RMAP_SLOT_RT:
243 case INTEL_RMAP_SLOT_DYN:
244 break;
245 default:
246 icd_free(slot->u.path);
247 break;
248 }
249 }
250
251 icd_free(rmap->slots);
252 icd_free(rmap);
253}