blob: d99cc56f186049bf520f34fcdbaee0cced9729dc [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.
Chia-I Wu44e42362014-09-02 08:32:09 +080023 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
Chia-I Wu1f7540b2014-08-22 13:56:18 +080026 */
27
Chia-I Wu98824592014-09-02 09:42:46 +080028#include "shader.h"
Chia-I Wu1f7540b2014-08-22 13:56:18 +080029#include "pipeline_priv.h"
30
31static struct intel_rmap_slot *rmap_get_slot(struct intel_rmap *rmap,
32 XGL_DESCRIPTOR_SET_SLOT_TYPE type,
33 XGL_UINT index)
34{
35 const XGL_UINT resource_offset = rmap->rt_count;
36 const XGL_UINT uav_offset = resource_offset + rmap->resource_count;
37 const XGL_UINT sampler_offset = uav_offset + rmap->uav_count;
38 struct intel_rmap_slot *slot;
39
40 switch (type) {
41 case XGL_SLOT_UNUSED:
42 slot = NULL;
43 break;
44 case XGL_SLOT_SHADER_RESOURCE:
45 slot = &rmap->slots[resource_offset + index];
46 break;
47 case XGL_SLOT_SHADER_UAV:
48 slot = &rmap->slots[uav_offset + index];
49 break;
50 case XGL_SLOT_SHADER_SAMPLER:
51 slot = &rmap->slots[sampler_offset + index];
52 break;
53 default:
54 assert(!"unknown rmap slot type");
55 slot = NULL;
56 break;
57 }
58
59 return slot;
60}
61
62static bool rmap_init_slots_with_path(struct intel_rmap *rmap,
63 const XGL_DESCRIPTOR_SET_MAPPING *mapping,
64 XGL_UINT *nest_path,
65 XGL_UINT nest_level)
66{
67 XGL_UINT i;
68
69 for (i = 0; i < mapping->descriptorCount; i++) {
70 const XGL_DESCRIPTOR_SLOT_INFO *info = &mapping->pDescriptorInfo[i];
71 struct intel_rmap_slot *slot;
72
73 if (info->slotObjectType == XGL_SLOT_NEXT_DESCRIPTOR_SET) {
74 nest_path[nest_level] = i;
75 if (!rmap_init_slots_with_path(rmap, info->pNextLevelSet,
76 nest_path, nest_level + 1))
77 return false;
78
79 continue;
80 }
81
82 slot = rmap_get_slot(rmap, info->slotObjectType,
83 info->shaderEntityIndex);
84 if (!slot)
85 continue;
86
87 assert(!slot->path_len);
88 slot->path_len = nest_level + 1;
89
90 if (nest_level) {
91 slot->u.path = icd_alloc(sizeof(slot->u.path[0]) *
92 slot->path_len, 0, XGL_SYSTEM_ALLOC_INTERNAL);
93 if (!slot->u.path) {
94 slot->path_len = 0;
95 return false;
96 }
97
98 memcpy(slot->u.path, nest_path,
99 sizeof(slot->u.path[0]) * nest_level);
100 slot->u.path[nest_level] = i;
101 } else {
102 slot->u.index = i;
103 }
104 }
105
106 return true;
107}
108
109static bool rmap_init_slots(struct intel_rmap *rmap,
110 const XGL_DESCRIPTOR_SET_MAPPING *mapping,
111 XGL_UINT depth)
112{
113 XGL_UINT *nest_path;
114 bool ok;
115
116 if (depth) {
117 nest_path = icd_alloc(sizeof(nest_path[0]) * depth,
118 0, XGL_SYSTEM_ALLOC_INTERNAL_TEMP);
119 if (!nest_path)
120 return false;
121 } else {
122 nest_path = NULL;
123 }
124
125 ok = rmap_init_slots_with_path(rmap, mapping, nest_path, 0);
126
127 if (nest_path)
128 icd_free(nest_path);
129
130 return ok;
131}
132
133static void rmap_update_count(struct intel_rmap *rmap,
134 XGL_DESCRIPTOR_SET_SLOT_TYPE type,
135 XGL_UINT index)
136{
137 switch (type) {
138 case XGL_SLOT_UNUSED:
139 break;
140 case XGL_SLOT_SHADER_RESOURCE:
141 if (rmap->resource_count < index + 1)
142 rmap->resource_count = index + 1;
143 break;
144 case XGL_SLOT_SHADER_UAV:
145 if (rmap->uav_count < index + 1)
146 rmap->uav_count = index + 1;
147 break;
148 case XGL_SLOT_SHADER_SAMPLER:
149 if (rmap->sampler_count < index + 1)
150 rmap->sampler_count = index + 1;
151 break;
152 default:
153 assert(!"unknown rmap slot type");
154 break;
155 }
156}
157
158static XGL_UINT rmap_init_counts(struct intel_rmap *rmap,
159 const XGL_DESCRIPTOR_SET_MAPPING *mapping)
160{
161 XGL_UINT depth = 0;
162 XGL_UINT i;
163
164 for (i = 0; i < mapping->descriptorCount; i++) {
165 const XGL_DESCRIPTOR_SLOT_INFO *info = &mapping->pDescriptorInfo[i];
166
167 if (info->slotObjectType == XGL_SLOT_NEXT_DESCRIPTOR_SET) {
168 const XGL_UINT d = rmap_init_counts(rmap,
169 info->pNextLevelSet);
170 if (depth < d + 1)
171 depth = d + 1;
172
173 continue;
174 }
175
176 rmap_update_count(rmap, info->slotObjectType,
177 info->shaderEntityIndex);
178 }
179
180 return depth;
181}
182
Chia-I Wua6d50aa2014-09-02 10:21:34 +0800183static void rmap_destroy(struct intel_rmap *rmap)
184{
185 XGL_UINT i;
186
187 for (i = 0; i < rmap->slot_count; i++) {
188 struct intel_rmap_slot *slot = &rmap->slots[i];
189
190 switch (slot->path_len) {
191 case 0:
192 case 1:
193 case INTEL_RMAP_SLOT_RT:
194 case INTEL_RMAP_SLOT_DYN:
195 break;
196 default:
197 icd_free(slot->u.path);
198 break;
199 }
200 }
201
202 icd_free(rmap->slots);
203 icd_free(rmap);
204}
205
206static struct intel_rmap *rmap_create(struct intel_dev *dev,
207 const XGL_DESCRIPTOR_SET_MAPPING *mapping,
208 const XGL_DYNAMIC_MEMORY_VIEW_SLOT_INFO *dyn,
209 XGL_UINT rt_count)
Chia-I Wu1f7540b2014-08-22 13:56:18 +0800210{
211 struct intel_rmap *rmap;
212 struct intel_rmap_slot *slot;
213 XGL_UINT depth, rt;
214
215 rmap = icd_alloc(sizeof(*rmap), 0, XGL_SYSTEM_ALLOC_INTERNAL);
216 if (!rmap)
217 return NULL;
218
219 memset(rmap, 0, sizeof(*rmap));
220
221 depth = rmap_init_counts(rmap, mapping);
222
223 /* add RTs and the dynamic memory view */
224 rmap_update_count(rmap, dyn->slotObjectType, dyn->shaderEntityIndex);
225 rmap->rt_count = rt_count;
226
227 rmap->slot_count = rmap->rt_count + rmap->resource_count +
228 rmap->uav_count + rmap->sampler_count;
229
230 rmap->slots = icd_alloc(sizeof(rmap->slots[0]) * rmap->slot_count,
231 0, XGL_SYSTEM_ALLOC_INTERNAL);
232 if (!rmap->slots) {
233 icd_free(rmap);
234 return NULL;
235 }
236
237 memset(rmap->slots, 0, sizeof(rmap->slots[0]) * rmap->slot_count);
238
239 if (!rmap_init_slots(rmap, mapping, depth)) {
Chia-I Wua6d50aa2014-09-02 10:21:34 +0800240 rmap_destroy(rmap);
Chia-I Wu1f7540b2014-08-22 13:56:18 +0800241 return NULL;
242 }
243
244 /* add RTs and the dynamic memory view */
245 slot = rmap_get_slot(rmap, dyn->slotObjectType, dyn->shaderEntityIndex);
246 if (slot) {
247 slot->path_len = INTEL_RMAP_SLOT_DYN;
248 slot->u.index = 0;
249 }
250 for (rt = 0; rt < rmap->rt_count; rt++) {
251 slot = &rmap->slots[rt];
252 slot->path_len = INTEL_RMAP_SLOT_RT;
253 slot->u.index = rt;
254 }
255
256 return rmap;
257}
258
Chia-I Wu39026c92014-09-02 10:03:19 +0800259static XGL_RESULT pipeline_shader_copy_ir(struct intel_pipe_shader *sh,
260 const struct intel_shader *ir)
Chia-I Wu98824592014-09-02 09:42:46 +0800261{
Chia-I Wu39026c92014-09-02 10:03:19 +0800262 sh->pCode = icd_alloc(ir->ir->size, 0, XGL_SYSTEM_ALLOC_INTERNAL_SHADER);
263 if (!sh->pCode)
Chia-I Wu98824592014-09-02 09:42:46 +0800264 return XGL_ERROR_OUT_OF_MEMORY;
265
Chia-I Wu39026c92014-09-02 10:03:19 +0800266 memcpy(sh->pCode, ir->ir->kernel, ir->ir->size);
267 sh->codeSize = ir->ir->size;
Chia-I Wu98824592014-09-02 09:42:46 +0800268
Chia-I Wu39026c92014-09-02 10:03:19 +0800269 sh->uses = ir->uses;
Chia-I Wu98824592014-09-02 09:42:46 +0800270
Chia-I Wu39026c92014-09-02 10:03:19 +0800271 sh->in_count = ir->in_count;
272 sh->out_count = ir->out_count;
273 sh->sampler_count = ir->sampler_count;
274 sh->surface_count = ir->surface_count;
275 sh->urb_grf_start = ir->urb_grf_start;
276 sh->urb_read_length = ir->urb_read_length;
277 sh->barycentric_interps = ir->barycentric_interps;
278
279 return XGL_SUCCESS;
280}
281
282static XGL_RESULT pipeline_build_vs(struct intel_pipeline *pipeline,
283 const struct intel_pipeline_create_info *info)
284{
285 struct intel_pipe_shader *vs = &pipeline->intel_vs;
286 XGL_RESULT ret;
287
288 ret = pipeline_shader_copy_ir(vs, intel_shader(info->vs.shader));
289 if (ret != XGL_SUCCESS)
290 return ret;
291
292 vs->rmap = rmap_create(pipeline->dev,
293 &info->vs.descriptorSetMapping[0],
294 &info->vs.dynamicMemoryViewMapping, 0);
295 if (!vs->rmap) {
296 icd_free(vs->pCode);
297 return XGL_ERROR_OUT_OF_MEMORY;
Chia-I Wu98824592014-09-02 09:42:46 +0800298 }
299
Chia-I Wu39026c92014-09-02 10:03:19 +0800300 pipeline->vs = info->vs;
301 pipeline->active_shaders |= SHADER_VERTEX_FLAG;
302
303 return XGL_SUCCESS;
304}
305
306static XGL_RESULT pipeline_build_tcs(struct intel_pipeline *pipeline,
307 const struct intel_pipeline_create_info *info)
308{
309 struct intel_pipe_shader *tcs = &pipeline->tess_control;
310 XGL_RESULT ret;
311
312 ret = pipeline_shader_copy_ir(tcs, intel_shader(info->tcs.shader));
313 if (ret != XGL_SUCCESS)
314 return ret;
315
316 pipeline->active_shaders |= SHADER_TESS_CONTROL_FLAG;
317
318 return XGL_SUCCESS;
319}
320
321static XGL_RESULT pipeline_build_tes(struct intel_pipeline *pipeline,
322 const struct intel_pipeline_create_info *info)
323{
324 struct intel_pipe_shader *tes = &pipeline->tess_eval;
325 XGL_RESULT ret;
326
327 ret = pipeline_shader_copy_ir(tes, intel_shader(info->tes.shader));
328 if (ret != XGL_SUCCESS)
329 return ret;
330
331 pipeline->active_shaders |= SHADER_TESS_EVAL_FLAG;
332
333 return XGL_SUCCESS;
334}
335
336static XGL_RESULT pipeline_build_gs(struct intel_pipeline *pipeline,
337 const struct intel_pipeline_create_info *info)
338{
339 struct intel_pipe_shader *gs = &pipeline->gs;
340 XGL_RESULT ret;
341
342 ret = pipeline_shader_copy_ir(gs, intel_shader(info->gs.shader));
343 if (ret != XGL_SUCCESS)
344 return ret;
345
346 pipeline->active_shaders |= SHADER_GEOMETRY_FLAG;
347
348 return XGL_SUCCESS;
349}
350
351static XGL_RESULT pipeline_build_fs(struct intel_pipeline *pipeline,
352 const struct intel_pipeline_create_info *info)
353{
354 struct intel_pipe_shader *fs = &pipeline->intel_fs;
355 XGL_RESULT ret;
356
357 ret = pipeline_shader_copy_ir(fs, intel_shader(info->fs.shader));
358 if (ret != XGL_SUCCESS)
359 return ret;
360
361 /* assuming one RT; need to parse the shader */
362 fs->rmap = rmap_create(pipeline->dev,
363 &info->fs.descriptorSetMapping[0],
364 &info->fs.dynamicMemoryViewMapping, 1);
365 if (!fs->rmap) {
366 icd_free(fs->pCode);
367 return XGL_ERROR_OUT_OF_MEMORY;
368 }
369
370 pipeline->fs = info->fs;
371 pipeline->active_shaders |= SHADER_FRAGMENT_FLAG;
372
373 return XGL_SUCCESS;
374}
375
376static XGL_RESULT pipeline_build_cs(struct intel_pipeline *pipeline,
377 const struct intel_pipeline_create_info *info)
378{
379 struct intel_pipe_shader *cs = &pipeline->compute;
380 XGL_RESULT ret;
381
382 ret = pipeline_shader_copy_ir(cs, intel_shader(info->compute.cs.shader));
383 if (ret != XGL_SUCCESS)
384 return ret;
385
386 pipeline->active_shaders |= SHADER_COMPUTE_FLAG;
387
Chia-I Wu98824592014-09-02 09:42:46 +0800388 return XGL_SUCCESS;
389}
390
391XGL_RESULT pipeline_build_shaders(struct intel_pipeline *pipeline,
392 const struct intel_pipeline_create_info *info)
393{
394 XGL_RESULT ret = XGL_SUCCESS;
395
396 if (ret == XGL_SUCCESS && info->vs.shader)
Chia-I Wu39026c92014-09-02 10:03:19 +0800397 ret = pipeline_build_vs(pipeline, info);
Chia-I Wu98824592014-09-02 09:42:46 +0800398 if (ret == XGL_SUCCESS && info->tcs.shader)
Chia-I Wu39026c92014-09-02 10:03:19 +0800399 ret = pipeline_build_tcs(pipeline, info);
Chia-I Wu98824592014-09-02 09:42:46 +0800400 if (ret == XGL_SUCCESS && info->tes.shader)
Chia-I Wu39026c92014-09-02 10:03:19 +0800401 ret = pipeline_build_tes(pipeline, info);
Chia-I Wu98824592014-09-02 09:42:46 +0800402 if (ret == XGL_SUCCESS && info->gs.shader)
Chia-I Wu39026c92014-09-02 10:03:19 +0800403 ret = pipeline_build_gs(pipeline, info);
Chia-I Wu98824592014-09-02 09:42:46 +0800404 if (ret == XGL_SUCCESS && info->fs.shader)
Chia-I Wu39026c92014-09-02 10:03:19 +0800405 ret = pipeline_build_fs(pipeline, info);
406
407 if (ret == XGL_SUCCESS && info->compute.cs.shader)
408 ret = pipeline_build_cs(pipeline, info);
Chia-I Wu98824592014-09-02 09:42:46 +0800409
410 return ret;
411}
412
Chia-I Wu39026c92014-09-02 10:03:19 +0800413static void pipeline_tear_shader(struct intel_pipe_shader *sh)
414{
415 icd_free(sh->pCode);
416 if (sh->rmap)
417 rmap_destroy(sh->rmap);
418}
419
Chia-I Wu98824592014-09-02 09:42:46 +0800420void pipeline_tear_shaders(struct intel_pipeline *pipeline)
421{
422 if (pipeline->active_shaders & SHADER_VERTEX_FLAG) {
Chia-I Wu39026c92014-09-02 10:03:19 +0800423 pipeline_tear_shader(&pipeline->intel_vs);
Chia-I Wu98824592014-09-02 09:42:46 +0800424 }
425
Chia-I Wu39026c92014-09-02 10:03:19 +0800426 if (pipeline->active_shaders & SHADER_TESS_CONTROL_FLAG) {
427 pipeline_tear_shader(&pipeline->tess_control);
428 }
429
430 if (pipeline->active_shaders & SHADER_TESS_EVAL_FLAG) {
431 pipeline_tear_shader(&pipeline->tess_eval);
432 }
433
434 if (pipeline->active_shaders & SHADER_GEOMETRY_FLAG) {
435 pipeline_tear_shader(&pipeline->gs);
436 }
437
438 if (pipeline->active_shaders & SHADER_FRAGMENT_FLAG) {
439 pipeline_tear_shader(&pipeline->intel_fs);
440 }
441
442 if (pipeline->active_shaders & SHADER_COMPUTE_FLAG) {
443 pipeline_tear_shader(&pipeline->compute);
444 }
Chia-I Wu98824592014-09-02 09:42:46 +0800445}