Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / vulkan / anv_pipeline.c
1 /*
2 * Copyright © 2015 Intel Corporation
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "anv_private.h"
31 #include "brw_nir.h"
32 #include "anv_nir.h"
33 #include "glsl/nir/nir_spirv.h"
34
35 /* Needed for SWIZZLE macros */
36 #include "program/prog_instruction.h"
37
38 // Shader functions
39
40 VkResult anv_CreateShaderModule(
41 VkDevice _device,
42 const VkShaderModuleCreateInfo* pCreateInfo,
43 VkShaderModule* pShaderModule)
44 {
45 ANV_FROM_HANDLE(anv_device, device, _device);
46 struct anv_shader_module *module;
47
48 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO);
49 assert(pCreateInfo->flags == 0);
50
51 module = anv_device_alloc(device, sizeof(*module) + pCreateInfo->codeSize, 8,
52 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
53 if (module == NULL)
54 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
55
56 module->nir = NULL;
57 module->size = pCreateInfo->codeSize;
58 memcpy(module->data, pCreateInfo->pCode, module->size);
59
60 *pShaderModule = anv_shader_module_to_handle(module);
61
62 return VK_SUCCESS;
63 }
64
65 void anv_DestroyShaderModule(
66 VkDevice _device,
67 VkShaderModule _module)
68 {
69 ANV_FROM_HANDLE(anv_device, device, _device);
70 ANV_FROM_HANDLE(anv_shader_module, module, _module);
71
72 anv_device_free(device, module);
73 }
74
75 VkResult anv_CreateShader(
76 VkDevice _device,
77 const VkShaderCreateInfo* pCreateInfo,
78 VkShader* pShader)
79 {
80 ANV_FROM_HANDLE(anv_device, device, _device);
81 ANV_FROM_HANDLE(anv_shader_module, module, pCreateInfo->module);
82 struct anv_shader *shader;
83
84 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_CREATE_INFO);
85 assert(pCreateInfo->flags == 0);
86
87 const char *name = pCreateInfo->pName ? pCreateInfo->pName : "main";
88 size_t name_len = strlen(name);
89
90 shader = anv_device_alloc(device, sizeof(*shader) + name_len + 1, 8,
91 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
92 if (shader == NULL)
93 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
94
95 shader->module = module,
96 memcpy(shader->entrypoint, name, name_len + 1);
97
98 *pShader = anv_shader_to_handle(shader);
99
100 return VK_SUCCESS;
101 }
102
103 void anv_DestroyShader(
104 VkDevice _device,
105 VkShader _shader)
106 {
107 ANV_FROM_HANDLE(anv_device, device, _device);
108 ANV_FROM_HANDLE(anv_shader, shader, _shader);
109
110 anv_device_free(device, shader);
111 }
112
113 #define SPIR_V_MAGIC_NUMBER 0x07230203
114
115 static const gl_shader_stage vk_shader_stage_to_mesa_stage[] = {
116 [VK_SHADER_STAGE_VERTEX] = MESA_SHADER_VERTEX,
117 [VK_SHADER_STAGE_TESS_CONTROL] = -1,
118 [VK_SHADER_STAGE_TESS_EVALUATION] = -1,
119 [VK_SHADER_STAGE_GEOMETRY] = MESA_SHADER_GEOMETRY,
120 [VK_SHADER_STAGE_FRAGMENT] = MESA_SHADER_FRAGMENT,
121 [VK_SHADER_STAGE_COMPUTE] = MESA_SHADER_COMPUTE,
122 };
123
124 static bool
125 is_scalar_shader_stage(const struct brw_compiler *compiler, VkShaderStage stage)
126 {
127 switch (stage) {
128 case VK_SHADER_STAGE_VERTEX:
129 return compiler->scalar_vs;
130 case VK_SHADER_STAGE_GEOMETRY:
131 return false;
132 case VK_SHADER_STAGE_FRAGMENT:
133 case VK_SHADER_STAGE_COMPUTE:
134 return true;
135 default:
136 unreachable("Unsupported shader stage");
137 }
138 }
139
140 /* Eventually, this will become part of anv_CreateShader. Unfortunately,
141 * we can't do that yet because we don't have the ability to copy nir.
142 */
143 static nir_shader *
144 anv_shader_compile_to_nir(struct anv_device *device,
145 struct anv_shader *shader, VkShaderStage vk_stage)
146 {
147 if (strcmp(shader->entrypoint, "main") != 0) {
148 anv_finishme("Multiple shaders per module not really supported");
149 }
150
151 gl_shader_stage stage = vk_shader_stage_to_mesa_stage[vk_stage];
152 const struct brw_compiler *compiler =
153 device->instance->physicalDevice.compiler;
154 const nir_shader_compiler_options *nir_options =
155 compiler->glsl_compiler_options[stage].NirOptions;
156
157 nir_shader *nir;
158 if (shader->module->nir) {
159 /* Some things such as our meta clear/blit code will give us a NIR
160 * shader directly. In that case, we just ignore the SPIR-V entirely
161 * and just use the NIR shader */
162 nir = shader->module->nir;
163 nir->options = nir_options;
164 } else {
165 uint32_t *spirv = (uint32_t *) shader->module->data;
166 assert(spirv[0] == SPIR_V_MAGIC_NUMBER);
167 assert(shader->module->size % 4 == 0);
168
169 nir = spirv_to_nir(spirv, shader->module->size / 4, stage, nir_options);
170 }
171 nir_validate_shader(nir);
172
173 /* Make sure the provided shader has exactly one entrypoint and that the
174 * name matches the name that came in from the VkShader.
175 */
176 nir_function_impl *entrypoint = NULL;
177 nir_foreach_overload(nir, overload) {
178 if (strcmp(shader->entrypoint, overload->function->name) == 0 &&
179 overload->impl) {
180 assert(entrypoint == NULL);
181 entrypoint = overload->impl;
182 }
183 }
184 assert(entrypoint != NULL);
185
186 brw_preprocess_nir(nir, &device->info,
187 is_scalar_shader_stage(compiler, vk_stage));
188
189 nir_shader_gather_info(nir, entrypoint);
190
191 return nir;
192 }
193
194 VkResult anv_CreatePipelineCache(
195 VkDevice device,
196 const VkPipelineCacheCreateInfo* pCreateInfo,
197 VkPipelineCache* pPipelineCache)
198 {
199 pPipelineCache->handle = 1;
200
201 stub_return(VK_SUCCESS);
202 }
203
204 void anv_DestroyPipelineCache(
205 VkDevice _device,
206 VkPipelineCache _cache)
207 {
208 }
209
210 size_t anv_GetPipelineCacheSize(
211 VkDevice device,
212 VkPipelineCache pipelineCache)
213 {
214 stub_return(0);
215 }
216
217 VkResult anv_GetPipelineCacheData(
218 VkDevice device,
219 VkPipelineCache pipelineCache,
220 void* pData)
221 {
222 stub_return(VK_UNSUPPORTED);
223 }
224
225 VkResult anv_MergePipelineCaches(
226 VkDevice device,
227 VkPipelineCache destCache,
228 uint32_t srcCacheCount,
229 const VkPipelineCache* pSrcCaches)
230 {
231 stub_return(VK_UNSUPPORTED);
232 }
233
234 void anv_DestroyPipeline(
235 VkDevice _device,
236 VkPipeline _pipeline)
237 {
238 ANV_FROM_HANDLE(anv_device, device, _device);
239 ANV_FROM_HANDLE(anv_pipeline, pipeline, _pipeline);
240
241 anv_reloc_list_finish(&pipeline->batch_relocs, pipeline->device);
242 anv_state_stream_finish(&pipeline->program_stream);
243 anv_state_pool_free(&device->dynamic_state_pool, pipeline->blend_state);
244 anv_device_free(pipeline->device, pipeline);
245 }
246
247 static const uint32_t vk_to_gen_primitive_type[] = {
248 [VK_PRIMITIVE_TOPOLOGY_POINT_LIST] = _3DPRIM_POINTLIST,
249 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST] = _3DPRIM_LINELIST,
250 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP] = _3DPRIM_LINESTRIP,
251 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST] = _3DPRIM_TRILIST,
252 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP] = _3DPRIM_TRISTRIP,
253 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN] = _3DPRIM_TRIFAN,
254 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST_ADJ] = _3DPRIM_LINELIST_ADJ,
255 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_ADJ] = _3DPRIM_LINESTRIP_ADJ,
256 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_ADJ] = _3DPRIM_TRILIST_ADJ,
257 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_ADJ] = _3DPRIM_TRISTRIP_ADJ,
258 [VK_PRIMITIVE_TOPOLOGY_PATCH] = _3DPRIM_PATCHLIST_1
259 };
260
261 static void
262 populate_sampler_prog_key(const struct brw_device_info *devinfo,
263 struct brw_sampler_prog_key_data *key)
264 {
265 /* XXX: Handle texture swizzle on HSW- */
266 for (int i = 0; i < MAX_SAMPLERS; i++) {
267 /* Assume color sampler, no swizzling. (Works for BDW+) */
268 key->swizzles[i] = SWIZZLE_XYZW;
269 }
270 }
271
272 static void
273 populate_vs_prog_key(const struct brw_device_info *devinfo,
274 struct brw_vs_prog_key *key)
275 {
276 memset(key, 0, sizeof(*key));
277
278 populate_sampler_prog_key(devinfo, &key->tex);
279
280 /* XXX: Handle vertex input work-arounds */
281
282 /* XXX: Handle sampler_prog_key */
283 }
284
285 static void
286 populate_wm_prog_key(const struct brw_device_info *devinfo,
287 const VkGraphicsPipelineCreateInfo *info,
288 struct brw_wm_prog_key *key)
289 {
290 ANV_FROM_HANDLE(anv_render_pass, render_pass, info->renderPass);
291
292 memset(key, 0, sizeof(*key));
293
294 populate_sampler_prog_key(devinfo, &key->tex);
295
296 /* Vulkan doesn't specify a default */
297 key->high_quality_derivatives = false;
298
299 /* XXX Vulkan doesn't appear to specify */
300 key->clamp_fragment_color = false;
301
302 /* Vulkan always specifies upper-left coordinates */
303 key->drawable_height = 0;
304 key->render_to_fbo = false;
305
306 key->nr_color_regions = render_pass->subpasses[info->subpass].color_count;
307
308 key->replicate_alpha = key->nr_color_regions > 1 &&
309 info->pColorBlendState->alphaToCoverageEnable;
310
311 if (info->pMultisampleState && info->pMultisampleState->rasterSamples > 1) {
312 /* We should probably pull this out of the shader, but it's fairly
313 * harmless to compute it and then let dead-code take care of it.
314 */
315 key->persample_shading = info->pMultisampleState->sampleShadingEnable;
316 if (key->persample_shading)
317 key->persample_2x = info->pMultisampleState->rasterSamples == 2;
318
319 key->compute_pos_offset = info->pMultisampleState->sampleShadingEnable;
320 key->compute_sample_id = info->pMultisampleState->sampleShadingEnable;
321 }
322 }
323
324 static void
325 populate_cs_prog_key(const struct brw_device_info *devinfo,
326 struct brw_cs_prog_key *key)
327 {
328 memset(key, 0, sizeof(*key));
329
330 populate_sampler_prog_key(devinfo, &key->tex);
331 }
332
333 static nir_shader *
334 anv_pipeline_compile(struct anv_pipeline *pipeline,
335 struct anv_shader *shader,
336 VkShaderStage stage,
337 struct brw_stage_prog_data *prog_data)
338 {
339 const struct brw_compiler *compiler =
340 pipeline->device->instance->physicalDevice.compiler;
341
342 nir_shader *nir = anv_shader_compile_to_nir(pipeline->device, shader, stage);
343 if (nir == NULL)
344 return NULL;
345
346 bool have_push_constants = false;
347 nir_foreach_variable(var, &nir->uniforms) {
348 if (!glsl_type_is_sampler(var->type)) {
349 have_push_constants = true;
350 break;
351 }
352 }
353
354 /* Figure out the number of parameters */
355 prog_data->nr_params = 0;
356
357 if (have_push_constants) {
358 /* If the shader uses any push constants at all, we'll just give
359 * them the maximum possible number
360 */
361 prog_data->nr_params += MAX_PUSH_CONSTANTS_SIZE / sizeof(float);
362 }
363
364 if (pipeline->layout && pipeline->layout->stage[stage].has_dynamic_offsets)
365 prog_data->nr_params += MAX_DYNAMIC_BUFFERS;
366
367 if (prog_data->nr_params > 0) {
368 prog_data->param = (const gl_constant_value **)
369 anv_device_alloc(pipeline->device,
370 prog_data->nr_params * sizeof(gl_constant_value *),
371 8, VK_SYSTEM_ALLOC_TYPE_INTERNAL_SHADER);
372
373 /* We now set the param values to be offsets into a
374 * anv_push_constant_data structure. Since the compiler doesn't
375 * actually dereference any of the gl_constant_value pointers in the
376 * params array, it doesn't really matter what we put here.
377 */
378 struct anv_push_constants *null_data = NULL;
379 if (have_push_constants) {
380 /* Fill out the push constants section of the param array */
381 for (unsigned i = 0; i < MAX_PUSH_CONSTANTS_SIZE / sizeof(float); i++)
382 prog_data->param[i] = (const gl_constant_value *)
383 &null_data->client_data[i * sizeof(float)];
384 }
385 }
386
387 /* Set up dynamic offsets */
388 anv_nir_apply_dynamic_offsets(pipeline, nir, prog_data);
389
390 /* Apply the actual pipeline layout to UBOs, SSBOs, and textures */
391 anv_nir_apply_pipeline_layout(nir, pipeline->layout);
392
393 /* All binding table offsets provided by apply_pipeline_layout() are
394 * relative to the start of the bindint table (plus MAX_RTS for VS).
395 */
396 unsigned bias = stage == VK_SHADER_STAGE_FRAGMENT ? MAX_RTS : 0;
397 prog_data->binding_table.size_bytes = 0;
398 prog_data->binding_table.texture_start = bias;
399 prog_data->binding_table.ubo_start = bias;
400 prog_data->binding_table.image_start = bias;
401
402 /* Finish the optimization and compilation process */
403 brw_postprocess_nir(nir, &pipeline->device->info,
404 is_scalar_shader_stage(compiler, stage));
405
406 /* nir_lower_io will only handle the push constants; we need to set this
407 * to the full number of possible uniforms.
408 */
409 nir->num_uniforms = prog_data->nr_params;
410
411 return nir;
412 }
413
414 static uint32_t
415 anv_pipeline_upload_kernel(struct anv_pipeline *pipeline,
416 const void *data, size_t size)
417 {
418 struct anv_state state =
419 anv_state_stream_alloc(&pipeline->program_stream, size, 64);
420
421 assert(size < pipeline->program_stream.block_pool->block_size);
422
423 memcpy(state.map, data, size);
424
425 return state.offset;
426 }
427 static void
428 anv_pipeline_add_compiled_stage(struct anv_pipeline *pipeline,
429 VkShaderStage stage,
430 struct brw_stage_prog_data *prog_data)
431 {
432 struct brw_device_info *devinfo = &pipeline->device->info;
433 uint32_t max_threads[] = {
434 [VK_SHADER_STAGE_VERTEX] = devinfo->max_vs_threads,
435 [VK_SHADER_STAGE_TESS_CONTROL] = 0,
436 [VK_SHADER_STAGE_TESS_EVALUATION] = 0,
437 [VK_SHADER_STAGE_GEOMETRY] = devinfo->max_gs_threads,
438 [VK_SHADER_STAGE_FRAGMENT] = devinfo->max_wm_threads,
439 [VK_SHADER_STAGE_COMPUTE] = devinfo->max_cs_threads,
440 };
441
442 pipeline->prog_data[stage] = prog_data;
443 pipeline->active_stages |= 1 << stage;
444 pipeline->scratch_start[stage] = pipeline->total_scratch;
445 pipeline->total_scratch =
446 align_u32(pipeline->total_scratch, 1024) +
447 prog_data->total_scratch * max_threads[stage];
448 }
449
450 static VkResult
451 anv_pipeline_compile_vs(struct anv_pipeline *pipeline,
452 const VkGraphicsPipelineCreateInfo *info,
453 struct anv_shader *shader)
454 {
455 const struct brw_compiler *compiler =
456 pipeline->device->instance->physicalDevice.compiler;
457 struct brw_vs_prog_data *prog_data = &pipeline->vs_prog_data;
458 struct brw_vs_prog_key key;
459
460 populate_vs_prog_key(&pipeline->device->info, &key);
461
462 /* TODO: Look up shader in cache */
463
464 memset(prog_data, 0, sizeof(*prog_data));
465
466 nir_shader *nir = anv_pipeline_compile(pipeline, shader,
467 VK_SHADER_STAGE_VERTEX,
468 &prog_data->base.base);
469 if (nir == NULL)
470 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
471
472 void *mem_ctx = ralloc_context(NULL);
473
474 if (shader->module->nir == NULL)
475 ralloc_steal(mem_ctx, nir);
476
477 prog_data->inputs_read = nir->info.inputs_read;
478 pipeline->writes_point_size = nir->info.outputs_written & VARYING_SLOT_PSIZ;
479
480 brw_compute_vue_map(&pipeline->device->info,
481 &prog_data->base.vue_map,
482 nir->info.outputs_written,
483 false /* XXX: Do SSO? */);
484
485 unsigned code_size;
486 const unsigned *shader_code =
487 brw_compile_vs(compiler, NULL, mem_ctx, &key, prog_data, nir,
488 NULL, false, -1, &code_size, NULL);
489 if (shader_code == NULL) {
490 ralloc_free(mem_ctx);
491 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
492 }
493
494 const uint32_t offset =
495 anv_pipeline_upload_kernel(pipeline, shader_code, code_size);
496 if (prog_data->base.dispatch_mode == DISPATCH_MODE_SIMD8) {
497 pipeline->vs_simd8 = offset;
498 pipeline->vs_vec4 = NO_KERNEL;
499 } else {
500 pipeline->vs_simd8 = NO_KERNEL;
501 pipeline->vs_vec4 = offset;
502 }
503
504 ralloc_free(mem_ctx);
505
506 anv_pipeline_add_compiled_stage(pipeline, VK_SHADER_STAGE_VERTEX,
507 &prog_data->base.base);
508
509 return VK_SUCCESS;
510 }
511
512 static VkResult
513 anv_pipeline_compile_fs(struct anv_pipeline *pipeline,
514 const VkGraphicsPipelineCreateInfo *info,
515 struct anv_shader *shader)
516 {
517 const struct brw_compiler *compiler =
518 pipeline->device->instance->physicalDevice.compiler;
519 struct brw_wm_prog_data *prog_data = &pipeline->wm_prog_data;
520 struct brw_wm_prog_key key;
521
522 populate_wm_prog_key(&pipeline->device->info, info, &key);
523
524 if (pipeline->use_repclear)
525 key.nr_color_regions = 1;
526
527 /* TODO: Look up shader in cache */
528
529 memset(prog_data, 0, sizeof(*prog_data));
530
531 prog_data->binding_table.render_target_start = 0;
532
533 nir_shader *nir = anv_pipeline_compile(pipeline, shader,
534 VK_SHADER_STAGE_FRAGMENT,
535 &prog_data->base);
536 if (nir == NULL)
537 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
538
539 void *mem_ctx = ralloc_context(NULL);
540
541 if (shader->module->nir == NULL)
542 ralloc_steal(mem_ctx, nir);
543
544 unsigned code_size;
545 const unsigned *shader_code =
546 brw_compile_fs(compiler, NULL, mem_ctx, &key, prog_data, nir,
547 NULL, -1, -1, pipeline->use_repclear, &code_size, NULL);
548 if (shader_code == NULL) {
549 ralloc_free(mem_ctx);
550 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
551 }
552
553 uint32_t offset = anv_pipeline_upload_kernel(pipeline,
554 shader_code, code_size);
555 if (prog_data->no_8)
556 pipeline->ps_simd8 = NO_KERNEL;
557 else
558 pipeline->ps_simd8 = offset;
559
560 if (prog_data->no_8 || prog_data->prog_offset_16) {
561 pipeline->ps_simd16 = offset + prog_data->prog_offset_16;
562 } else {
563 pipeline->ps_simd16 = NO_KERNEL;
564 }
565
566 pipeline->ps_ksp2 = 0;
567 pipeline->ps_grf_start2 = 0;
568 if (pipeline->ps_simd8 != NO_KERNEL) {
569 pipeline->ps_ksp0 = pipeline->ps_simd8;
570 pipeline->ps_grf_start0 = prog_data->base.dispatch_grf_start_reg;
571 if (pipeline->ps_simd16 != NO_KERNEL) {
572 pipeline->ps_ksp2 = pipeline->ps_simd16;
573 pipeline->ps_grf_start2 = prog_data->dispatch_grf_start_reg_16;
574 }
575 } else if (pipeline->ps_simd16 != NO_KERNEL) {
576 pipeline->ps_ksp0 = pipeline->ps_simd16;
577 pipeline->ps_grf_start0 = prog_data->dispatch_grf_start_reg_16;
578 }
579
580 ralloc_free(mem_ctx);
581
582 anv_pipeline_add_compiled_stage(pipeline, VK_SHADER_STAGE_FRAGMENT,
583 &prog_data->base);
584
585 return VK_SUCCESS;
586 }
587
588 VkResult
589 anv_pipeline_compile_cs(struct anv_pipeline *pipeline,
590 const VkComputePipelineCreateInfo *info,
591 struct anv_shader *shader)
592 {
593 const struct brw_compiler *compiler =
594 pipeline->device->instance->physicalDevice.compiler;
595 struct brw_cs_prog_data *prog_data = &pipeline->cs_prog_data;
596 struct brw_cs_prog_key key;
597
598 populate_cs_prog_key(&pipeline->device->info, &key);
599
600 /* TODO: Look up shader in cache */
601
602 memset(prog_data, 0, sizeof(*prog_data));
603
604 nir_shader *nir = anv_pipeline_compile(pipeline, shader,
605 VK_SHADER_STAGE_COMPUTE,
606 &prog_data->base);
607 if (nir == NULL)
608 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
609
610 void *mem_ctx = ralloc_context(NULL);
611
612 if (shader->module->nir == NULL)
613 ralloc_steal(mem_ctx, nir);
614
615 unsigned code_size;
616 const unsigned *shader_code =
617 brw_compile_cs(compiler, NULL, mem_ctx, &key, prog_data, nir,
618 -1, &code_size, NULL);
619 if (shader_code == NULL) {
620 ralloc_free(mem_ctx);
621 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
622 }
623
624 pipeline->cs_simd = anv_pipeline_upload_kernel(pipeline,
625 shader_code, code_size);
626 ralloc_free(mem_ctx);
627
628 anv_pipeline_add_compiled_stage(pipeline, VK_SHADER_STAGE_COMPUTE,
629 &prog_data->base);
630
631 return VK_SUCCESS;
632 }
633
634 static const int gen8_push_size = 32 * 1024;
635
636 static void
637 gen7_compute_urb_partition(struct anv_pipeline *pipeline)
638 {
639 const struct brw_device_info *devinfo = &pipeline->device->info;
640 bool vs_present = pipeline->active_stages & VK_SHADER_STAGE_VERTEX_BIT;
641 unsigned vs_size = vs_present ? pipeline->vs_prog_data.base.urb_entry_size : 1;
642 unsigned vs_entry_size_bytes = vs_size * 64;
643 bool gs_present = pipeline->active_stages & VK_SHADER_STAGE_GEOMETRY_BIT;
644 unsigned gs_size = gs_present ? pipeline->gs_prog_data.base.urb_entry_size : 1;
645 unsigned gs_entry_size_bytes = gs_size * 64;
646
647 /* From p35 of the Ivy Bridge PRM (section 1.7.1: 3DSTATE_URB_GS):
648 *
649 * VS Number of URB Entries must be divisible by 8 if the VS URB Entry
650 * Allocation Size is less than 9 512-bit URB entries.
651 *
652 * Similar text exists for GS.
653 */
654 unsigned vs_granularity = (vs_size < 9) ? 8 : 1;
655 unsigned gs_granularity = (gs_size < 9) ? 8 : 1;
656
657 /* URB allocations must be done in 8k chunks. */
658 unsigned chunk_size_bytes = 8192;
659
660 /* Determine the size of the URB in chunks. */
661 unsigned urb_chunks = devinfo->urb.size * 1024 / chunk_size_bytes;
662
663 /* Reserve space for push constants */
664 unsigned push_constant_bytes = gen8_push_size;
665 unsigned push_constant_chunks =
666 push_constant_bytes / chunk_size_bytes;
667
668 /* Initially, assign each stage the minimum amount of URB space it needs,
669 * and make a note of how much additional space it "wants" (the amount of
670 * additional space it could actually make use of).
671 */
672
673 /* VS has a lower limit on the number of URB entries */
674 unsigned vs_chunks =
675 ALIGN(devinfo->urb.min_vs_entries * vs_entry_size_bytes,
676 chunk_size_bytes) / chunk_size_bytes;
677 unsigned vs_wants =
678 ALIGN(devinfo->urb.max_vs_entries * vs_entry_size_bytes,
679 chunk_size_bytes) / chunk_size_bytes - vs_chunks;
680
681 unsigned gs_chunks = 0;
682 unsigned gs_wants = 0;
683 if (gs_present) {
684 /* There are two constraints on the minimum amount of URB space we can
685 * allocate:
686 *
687 * (1) We need room for at least 2 URB entries, since we always operate
688 * the GS in DUAL_OBJECT mode.
689 *
690 * (2) We can't allocate less than nr_gs_entries_granularity.
691 */
692 gs_chunks = ALIGN(MAX2(gs_granularity, 2) * gs_entry_size_bytes,
693 chunk_size_bytes) / chunk_size_bytes;
694 gs_wants =
695 ALIGN(devinfo->urb.max_gs_entries * gs_entry_size_bytes,
696 chunk_size_bytes) / chunk_size_bytes - gs_chunks;
697 }
698
699 /* There should always be enough URB space to satisfy the minimum
700 * requirements of each stage.
701 */
702 unsigned total_needs = push_constant_chunks + vs_chunks + gs_chunks;
703 assert(total_needs <= urb_chunks);
704
705 /* Mete out remaining space (if any) in proportion to "wants". */
706 unsigned total_wants = vs_wants + gs_wants;
707 unsigned remaining_space = urb_chunks - total_needs;
708 if (remaining_space > total_wants)
709 remaining_space = total_wants;
710 if (remaining_space > 0) {
711 unsigned vs_additional = (unsigned)
712 round(vs_wants * (((double) remaining_space) / total_wants));
713 vs_chunks += vs_additional;
714 remaining_space -= vs_additional;
715 gs_chunks += remaining_space;
716 }
717
718 /* Sanity check that we haven't over-allocated. */
719 assert(push_constant_chunks + vs_chunks + gs_chunks <= urb_chunks);
720
721 /* Finally, compute the number of entries that can fit in the space
722 * allocated to each stage.
723 */
724 unsigned nr_vs_entries = vs_chunks * chunk_size_bytes / vs_entry_size_bytes;
725 unsigned nr_gs_entries = gs_chunks * chunk_size_bytes / gs_entry_size_bytes;
726
727 /* Since we rounded up when computing *_wants, this may be slightly more
728 * than the maximum allowed amount, so correct for that.
729 */
730 nr_vs_entries = MIN2(nr_vs_entries, devinfo->urb.max_vs_entries);
731 nr_gs_entries = MIN2(nr_gs_entries, devinfo->urb.max_gs_entries);
732
733 /* Ensure that we program a multiple of the granularity. */
734 nr_vs_entries = ROUND_DOWN_TO(nr_vs_entries, vs_granularity);
735 nr_gs_entries = ROUND_DOWN_TO(nr_gs_entries, gs_granularity);
736
737 /* Finally, sanity check to make sure we have at least the minimum number
738 * of entries needed for each stage.
739 */
740 assert(nr_vs_entries >= devinfo->urb.min_vs_entries);
741 if (gs_present)
742 assert(nr_gs_entries >= 2);
743
744 /* Lay out the URB in the following order:
745 * - push constants
746 * - VS
747 * - GS
748 */
749 pipeline->urb.vs_start = push_constant_chunks;
750 pipeline->urb.vs_size = vs_size;
751 pipeline->urb.nr_vs_entries = nr_vs_entries;
752
753 pipeline->urb.gs_start = push_constant_chunks + vs_chunks;
754 pipeline->urb.gs_size = gs_size;
755 pipeline->urb.nr_gs_entries = nr_gs_entries;
756 }
757
758 static void
759 anv_pipeline_init_dynamic_state(struct anv_pipeline *pipeline,
760 const VkGraphicsPipelineCreateInfo *pCreateInfo)
761 {
762 anv_cmd_dirty_mask_t states = ANV_CMD_DIRTY_DYNAMIC_ALL;
763 ANV_FROM_HANDLE(anv_render_pass, pass, pCreateInfo->renderPass);
764 struct anv_subpass *subpass = &pass->subpasses[pCreateInfo->subpass];
765
766 pipeline->dynamic_state = default_dynamic_state;
767
768 if (pCreateInfo->pDynamicState) {
769 /* Remove all of the states that are marked as dynamic */
770 uint32_t count = pCreateInfo->pDynamicState->dynamicStateCount;
771 for (uint32_t s = 0; s < count; s++)
772 states &= ~(1 << pCreateInfo->pDynamicState->pDynamicStates[s]);
773 }
774
775 struct anv_dynamic_state *dynamic = &pipeline->dynamic_state;
776
777 dynamic->viewport.count = pCreateInfo->pViewportState->viewportCount;
778 if (states & (1 << VK_DYNAMIC_STATE_VIEWPORT)) {
779 typed_memcpy(dynamic->viewport.viewports,
780 pCreateInfo->pViewportState->pViewports,
781 pCreateInfo->pViewportState->viewportCount);
782 }
783
784 dynamic->scissor.count = pCreateInfo->pViewportState->scissorCount;
785 if (states & (1 << VK_DYNAMIC_STATE_SCISSOR)) {
786 typed_memcpy(dynamic->scissor.scissors,
787 pCreateInfo->pViewportState->pScissors,
788 pCreateInfo->pViewportState->scissorCount);
789 }
790
791 if (states & (1 << VK_DYNAMIC_STATE_LINE_WIDTH)) {
792 assert(pCreateInfo->pRasterState);
793 dynamic->line_width = pCreateInfo->pRasterState->lineWidth;
794 }
795
796 if (states & (1 << VK_DYNAMIC_STATE_DEPTH_BIAS)) {
797 assert(pCreateInfo->pRasterState);
798 dynamic->depth_bias.bias = pCreateInfo->pRasterState->depthBias;
799 dynamic->depth_bias.clamp = pCreateInfo->pRasterState->depthBiasClamp;
800 dynamic->depth_bias.slope_scaled =
801 pCreateInfo->pRasterState->slopeScaledDepthBias;
802 }
803
804 if (states & (1 << VK_DYNAMIC_STATE_BLEND_CONSTANTS)) {
805 assert(pCreateInfo->pColorBlendState);
806 typed_memcpy(dynamic->blend_constants,
807 pCreateInfo->pColorBlendState->blendConst, 4);
808 }
809
810 /* If there is no depthstencil attachment, then don't read
811 * pDepthStencilState. The Vulkan spec states that pDepthStencilState may
812 * be NULL in this case. Even if pDepthStencilState is non-NULL, there is
813 * no need to override the depthstencil defaults in
814 * anv_pipeline::dynamic_state when there is no depthstencil attachment.
815 *
816 * From the Vulkan spec (20 Oct 2015, git-aa308cb):
817 *
818 * pDepthStencilState [...] may only be NULL if renderPass and subpass
819 * specify a subpass that has no depth/stencil attachment.
820 */
821 if (subpass->depth_stencil_attachment != VK_ATTACHMENT_UNUSED) {
822 if (states & (1 << VK_DYNAMIC_STATE_DEPTH_BOUNDS)) {
823 assert(pCreateInfo->pDepthStencilState);
824 dynamic->depth_bounds.min =
825 pCreateInfo->pDepthStencilState->minDepthBounds;
826 dynamic->depth_bounds.max =
827 pCreateInfo->pDepthStencilState->maxDepthBounds;
828 }
829
830 if (states & (1 << VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK)) {
831 assert(pCreateInfo->pDepthStencilState);
832 dynamic->stencil_compare_mask.front =
833 pCreateInfo->pDepthStencilState->front.stencilCompareMask;
834 dynamic->stencil_compare_mask.back =
835 pCreateInfo->pDepthStencilState->back.stencilCompareMask;
836 }
837
838 if (states & (1 << VK_DYNAMIC_STATE_STENCIL_WRITE_MASK)) {
839 assert(pCreateInfo->pDepthStencilState);
840 dynamic->stencil_write_mask.front =
841 pCreateInfo->pDepthStencilState->front.stencilWriteMask;
842 dynamic->stencil_write_mask.back =
843 pCreateInfo->pDepthStencilState->back.stencilWriteMask;
844 }
845
846 if (states & (1 << VK_DYNAMIC_STATE_STENCIL_REFERENCE)) {
847 assert(pCreateInfo->pDepthStencilState);
848 dynamic->stencil_reference.front =
849 pCreateInfo->pDepthStencilState->front.stencilReference;
850 dynamic->stencil_reference.back =
851 pCreateInfo->pDepthStencilState->back.stencilReference;
852 }
853 }
854
855 pipeline->dynamic_state_mask = states;
856 }
857
858 static void
859 anv_pipeline_validate_create_info(const VkGraphicsPipelineCreateInfo *info)
860 {
861 struct anv_render_pass *renderpass = NULL;
862 struct anv_subpass *subpass = NULL;
863
864 /* Assert that all required members of VkGraphicsPipelineCreateInfo are
865 * present, as explained by the Vulkan (20 Oct 2015, git-aa308cb), Section
866 * 4.2 Graphics Pipeline.
867 */
868 assert(info->sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO);
869
870 renderpass = anv_render_pass_from_handle(info->renderPass);
871 assert(renderpass);
872
873 if (renderpass != &anv_meta_dummy_renderpass) {
874 assert(info->subpass < renderpass->subpass_count);
875 subpass = &renderpass->subpasses[info->subpass];
876 }
877
878 assert(info->stageCount >= 1);
879 assert(info->pVertexInputState);
880 assert(info->pInputAssemblyState);
881 assert(info->pViewportState);
882 assert(info->pRasterState);
883 assert(info->pMultisampleState);
884
885 if (subpass && subpass->depth_stencil_attachment != VK_ATTACHMENT_UNUSED)
886 assert(info->pDepthStencilState);
887
888 if (subpass && subpass->color_count > 0)
889 assert(info->pColorBlendState);
890
891 for (uint32_t i = 0; i < info->stageCount; ++i) {
892 switch (info->pStages[i].stage) {
893 case VK_SHADER_STAGE_TESS_CONTROL:
894 case VK_SHADER_STAGE_TESS_EVALUATION:
895 assert(info->pTessellationState);
896 break;
897 default:
898 break;
899 }
900 }
901 }
902
903 VkResult
904 anv_pipeline_init(struct anv_pipeline *pipeline, struct anv_device *device,
905 const VkGraphicsPipelineCreateInfo *pCreateInfo,
906 const struct anv_graphics_pipeline_create_info *extra)
907 {
908 VkResult result;
909
910 anv_validate {
911 anv_pipeline_validate_create_info(pCreateInfo);
912 }
913
914 pipeline->device = device;
915 pipeline->layout = anv_pipeline_layout_from_handle(pCreateInfo->layout);
916
917 result = anv_reloc_list_init(&pipeline->batch_relocs, device);
918 if (result != VK_SUCCESS) {
919 anv_device_free(device, pipeline);
920 return result;
921 }
922 pipeline->batch.next = pipeline->batch.start = pipeline->batch_data;
923 pipeline->batch.end = pipeline->batch.start + sizeof(pipeline->batch_data);
924 pipeline->batch.relocs = &pipeline->batch_relocs;
925
926 anv_state_stream_init(&pipeline->program_stream,
927 &device->instruction_block_pool);
928
929 anv_pipeline_init_dynamic_state(pipeline, pCreateInfo);
930
931 if (pCreateInfo->pTessellationState)
932 anv_finishme("VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO");
933 if (pCreateInfo->pMultisampleState &&
934 pCreateInfo->pMultisampleState->rasterSamples > 1)
935 anv_finishme("VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO");
936
937 pipeline->use_repclear = extra && extra->use_repclear;
938 pipeline->writes_point_size = false;
939
940 /* When we free the pipeline, we detect stages based on the NULL status
941 * of various prog_data pointers. Make them NULL by default.
942 */
943 memset(pipeline->prog_data, 0, sizeof(pipeline->prog_data));
944 memset(pipeline->scratch_start, 0, sizeof(pipeline->scratch_start));
945
946 pipeline->vs_simd8 = NO_KERNEL;
947 pipeline->vs_vec4 = NO_KERNEL;
948 pipeline->gs_vec4 = NO_KERNEL;
949
950 pipeline->active_stages = 0;
951 pipeline->total_scratch = 0;
952
953 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
954 ANV_FROM_HANDLE(anv_shader, shader, pCreateInfo->pStages[i].shader);
955
956 switch (pCreateInfo->pStages[i].stage) {
957 case VK_SHADER_STAGE_VERTEX:
958 anv_pipeline_compile_vs(pipeline, pCreateInfo, shader);
959 break;
960 case VK_SHADER_STAGE_FRAGMENT:
961 anv_pipeline_compile_fs(pipeline, pCreateInfo, shader);
962 break;
963 default:
964 anv_finishme("Unsupported shader stage");
965 }
966 }
967
968 if (!(pipeline->active_stages & VK_SHADER_STAGE_VERTEX_BIT)) {
969 /* Vertex is only optional if disable_vs is set */
970 assert(extra->disable_vs);
971 memset(&pipeline->vs_prog_data, 0, sizeof(pipeline->vs_prog_data));
972 }
973
974 gen7_compute_urb_partition(pipeline);
975
976 const VkPipelineVertexInputStateCreateInfo *vi_info =
977 pCreateInfo->pVertexInputState;
978 pipeline->vb_used = 0;
979 for (uint32_t i = 0; i < vi_info->bindingCount; i++) {
980 const VkVertexInputBindingDescription *desc =
981 &vi_info->pVertexBindingDescriptions[i];
982
983 pipeline->vb_used |= 1 << desc->binding;
984 pipeline->binding_stride[desc->binding] = desc->strideInBytes;
985
986 /* Step rate is programmed per vertex element (attribute), not
987 * binding. Set up a map of which bindings step per instance, for
988 * reference by vertex element setup. */
989 switch (desc->stepRate) {
990 default:
991 case VK_VERTEX_INPUT_STEP_RATE_VERTEX:
992 pipeline->instancing_enable[desc->binding] = false;
993 break;
994 case VK_VERTEX_INPUT_STEP_RATE_INSTANCE:
995 pipeline->instancing_enable[desc->binding] = true;
996 break;
997 }
998 }
999
1000 const VkPipelineInputAssemblyStateCreateInfo *ia_info =
1001 pCreateInfo->pInputAssemblyState;
1002 pipeline->primitive_restart = ia_info->primitiveRestartEnable;
1003 pipeline->topology = vk_to_gen_primitive_type[ia_info->topology];
1004
1005 if (extra && extra->use_rectlist)
1006 pipeline->topology = _3DPRIM_RECTLIST;
1007
1008 return VK_SUCCESS;
1009 }
1010
1011 VkResult
1012 anv_graphics_pipeline_create(
1013 VkDevice _device,
1014 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1015 const struct anv_graphics_pipeline_create_info *extra,
1016 VkPipeline *pPipeline)
1017 {
1018 ANV_FROM_HANDLE(anv_device, device, _device);
1019
1020 switch (device->info.gen) {
1021 case 7:
1022 return gen7_graphics_pipeline_create(_device, pCreateInfo, extra, pPipeline);
1023 case 8:
1024 return gen8_graphics_pipeline_create(_device, pCreateInfo, extra, pPipeline);
1025 default:
1026 unreachable("unsupported gen\n");
1027 }
1028 }
1029
1030 VkResult anv_CreateGraphicsPipelines(
1031 VkDevice _device,
1032 VkPipelineCache pipelineCache,
1033 uint32_t count,
1034 const VkGraphicsPipelineCreateInfo* pCreateInfos,
1035 VkPipeline* pPipelines)
1036 {
1037 VkResult result = VK_SUCCESS;
1038
1039 unsigned i = 0;
1040 for (; i < count; i++) {
1041 result = anv_graphics_pipeline_create(_device, &pCreateInfos[i],
1042 NULL, &pPipelines[i]);
1043 if (result != VK_SUCCESS) {
1044 for (unsigned j = 0; j < i; j++) {
1045 anv_DestroyPipeline(_device, pPipelines[j]);
1046 }
1047
1048 return result;
1049 }
1050 }
1051
1052 return VK_SUCCESS;
1053 }
1054
1055 static VkResult anv_compute_pipeline_create(
1056 VkDevice _device,
1057 const VkComputePipelineCreateInfo* pCreateInfo,
1058 VkPipeline* pPipeline)
1059 {
1060 ANV_FROM_HANDLE(anv_device, device, _device);
1061
1062 switch (device->info.gen) {
1063 case 7:
1064 return gen7_compute_pipeline_create(_device, pCreateInfo, pPipeline);
1065 case 8:
1066 return gen8_compute_pipeline_create(_device, pCreateInfo, pPipeline);
1067 default:
1068 unreachable("unsupported gen\n");
1069 }
1070 }
1071
1072 VkResult anv_CreateComputePipelines(
1073 VkDevice _device,
1074 VkPipelineCache pipelineCache,
1075 uint32_t count,
1076 const VkComputePipelineCreateInfo* pCreateInfos,
1077 VkPipeline* pPipelines)
1078 {
1079 VkResult result = VK_SUCCESS;
1080
1081 unsigned i = 0;
1082 for (; i < count; i++) {
1083 result = anv_compute_pipeline_create(_device, &pCreateInfos[i],
1084 &pPipelines[i]);
1085 if (result != VK_SUCCESS) {
1086 for (unsigned j = 0; j < i; j++) {
1087 anv_DestroyPipeline(_device, pPipelines[j]);
1088 }
1089
1090 return result;
1091 }
1092 }
1093
1094 return VK_SUCCESS;
1095 }
1096
1097 // Pipeline layout functions
1098
1099 VkResult anv_CreatePipelineLayout(
1100 VkDevice _device,
1101 const VkPipelineLayoutCreateInfo* pCreateInfo,
1102 VkPipelineLayout* pPipelineLayout)
1103 {
1104 ANV_FROM_HANDLE(anv_device, device, _device);
1105 struct anv_pipeline_layout l, *layout;
1106
1107 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
1108
1109 l.num_sets = pCreateInfo->descriptorSetCount;
1110
1111 unsigned dynamic_offset_count = 0;
1112
1113 memset(l.stage, 0, sizeof(l.stage));
1114 for (uint32_t set = 0; set < pCreateInfo->descriptorSetCount; set++) {
1115 ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout,
1116 pCreateInfo->pSetLayouts[set]);
1117 l.set[set].layout = set_layout;
1118
1119 l.set[set].dynamic_offset_start = dynamic_offset_count;
1120 for (uint32_t b = 0; b < set_layout->binding_count; b++) {
1121 if (set_layout->binding[b].dynamic_offset_index >= 0)
1122 dynamic_offset_count += set_layout->binding[b].array_size;
1123 }
1124
1125 for (VkShaderStage s = 0; s < VK_SHADER_STAGE_NUM; s++) {
1126 l.set[set].stage[s].surface_start = l.stage[s].surface_count;
1127 l.set[set].stage[s].sampler_start = l.stage[s].sampler_count;
1128
1129 for (uint32_t b = 0; b < set_layout->binding_count; b++) {
1130 unsigned array_size = set_layout->binding[b].array_size;
1131
1132 if (set_layout->binding[b].stage[s].surface_index >= 0) {
1133 l.stage[s].surface_count += array_size;
1134
1135 if (set_layout->binding[b].dynamic_offset_index >= 0)
1136 l.stage[s].has_dynamic_offsets = true;
1137 }
1138
1139 if (set_layout->binding[b].stage[s].sampler_index >= 0)
1140 l.stage[s].sampler_count += array_size;
1141 }
1142 }
1143 }
1144
1145 unsigned num_bindings = 0;
1146 for (VkShaderStage s = 0; s < VK_SHADER_STAGE_NUM; s++)
1147 num_bindings += l.stage[s].surface_count + l.stage[s].sampler_count;
1148
1149 size_t size = sizeof(*layout) + num_bindings * sizeof(layout->entries[0]);
1150
1151 layout = anv_device_alloc(device, size, 8, VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1152 if (layout == NULL)
1153 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1154
1155 /* Now we can actually build our surface and sampler maps */
1156 struct anv_pipeline_binding *entry = layout->entries;
1157 for (VkShaderStage s = 0; s < VK_SHADER_STAGE_NUM; s++) {
1158 l.stage[s].surface_to_descriptor = entry;
1159 entry += l.stage[s].surface_count;
1160 l.stage[s].sampler_to_descriptor = entry;
1161 entry += l.stage[s].sampler_count;
1162
1163 int surface = 0;
1164 int sampler = 0;
1165 for (uint32_t set = 0; set < pCreateInfo->descriptorSetCount; set++) {
1166 struct anv_descriptor_set_layout *set_layout = l.set[set].layout;
1167
1168 unsigned set_offset = 0;
1169 for (uint32_t b = 0; b < set_layout->binding_count; b++) {
1170 unsigned array_size = set_layout->binding[b].array_size;
1171
1172 if (set_layout->binding[b].stage[s].surface_index >= 0) {
1173 assert(surface == l.set[set].stage[s].surface_start +
1174 set_layout->binding[b].stage[s].surface_index);
1175 for (unsigned i = 0; i < array_size; i++) {
1176 l.stage[s].surface_to_descriptor[surface + i].set = set;
1177 l.stage[s].surface_to_descriptor[surface + i].offset = set_offset + i;
1178 }
1179 surface += array_size;
1180 }
1181
1182 if (set_layout->binding[b].stage[s].sampler_index >= 0) {
1183 assert(sampler == l.set[set].stage[s].sampler_start +
1184 set_layout->binding[b].stage[s].sampler_index);
1185 for (unsigned i = 0; i < array_size; i++) {
1186 l.stage[s].sampler_to_descriptor[sampler + i].set = set;
1187 l.stage[s].sampler_to_descriptor[sampler + i].offset = set_offset + i;
1188 }
1189 sampler += array_size;
1190 }
1191
1192 set_offset += array_size;
1193 }
1194 }
1195 }
1196
1197 /* Finally, we're done setting it up, copy into the allocated version */
1198 *layout = l;
1199
1200 *pPipelineLayout = anv_pipeline_layout_to_handle(layout);
1201
1202 return VK_SUCCESS;
1203 }
1204
1205 void anv_DestroyPipelineLayout(
1206 VkDevice _device,
1207 VkPipelineLayout _pipelineLayout)
1208 {
1209 ANV_FROM_HANDLE(anv_device, device, _device);
1210 ANV_FROM_HANDLE(anv_pipeline_layout, pipeline_layout, _pipelineLayout);
1211
1212 anv_device_free(device, pipeline_layout);
1213 }