anv: Completely rework descriptor set layouts
[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
32 // Shader functions
33
34 VkResult anv_CreateShaderModule(
35 VkDevice _device,
36 const VkShaderModuleCreateInfo* pCreateInfo,
37 VkShaderModule* pShaderModule)
38 {
39 ANV_FROM_HANDLE(anv_device, device, _device);
40 struct anv_shader_module *module;
41
42 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO);
43 assert(pCreateInfo->flags == 0);
44
45 module = anv_device_alloc(device, sizeof(*module) + pCreateInfo->codeSize, 8,
46 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
47 if (module == NULL)
48 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
49
50 module->nir = NULL;
51 module->size = pCreateInfo->codeSize;
52 memcpy(module->data, pCreateInfo->pCode, module->size);
53
54 *pShaderModule = anv_shader_module_to_handle(module);
55
56 return VK_SUCCESS;
57 }
58
59 void anv_DestroyShaderModule(
60 VkDevice _device,
61 VkShaderModule _module)
62 {
63 ANV_FROM_HANDLE(anv_device, device, _device);
64 ANV_FROM_HANDLE(anv_shader_module, module, _module);
65
66 anv_device_free(device, module);
67 }
68
69 VkResult anv_CreateShader(
70 VkDevice _device,
71 const VkShaderCreateInfo* pCreateInfo,
72 VkShader* pShader)
73 {
74 ANV_FROM_HANDLE(anv_device, device, _device);
75 ANV_FROM_HANDLE(anv_shader_module, module, pCreateInfo->module);
76 struct anv_shader *shader;
77
78 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_CREATE_INFO);
79 assert(pCreateInfo->flags == 0);
80
81 const char *name = pCreateInfo->pName ? pCreateInfo->pName : "main";
82 size_t name_len = strlen(name);
83
84 if (strcmp(name, "main") != 0) {
85 anv_finishme("Multiple shaders per module not really supported");
86 }
87
88 shader = anv_device_alloc(device, sizeof(*shader) + name_len + 1, 8,
89 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
90 if (shader == NULL)
91 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
92
93 shader->module = module;
94 memcpy(shader->entrypoint, name, name_len + 1);
95
96 *pShader = anv_shader_to_handle(shader);
97
98 return VK_SUCCESS;
99 }
100
101 void anv_DestroyShader(
102 VkDevice _device,
103 VkShader _shader)
104 {
105 ANV_FROM_HANDLE(anv_device, device, _device);
106 ANV_FROM_HANDLE(anv_shader, shader, _shader);
107
108 anv_device_free(device, shader);
109 }
110
111
112 VkResult anv_CreatePipelineCache(
113 VkDevice device,
114 const VkPipelineCacheCreateInfo* pCreateInfo,
115 VkPipelineCache* pPipelineCache)
116 {
117 pPipelineCache->handle = 1;
118
119 stub_return(VK_SUCCESS);
120 }
121
122 void anv_DestroyPipelineCache(
123 VkDevice _device,
124 VkPipelineCache _cache)
125 {
126 }
127
128 size_t anv_GetPipelineCacheSize(
129 VkDevice device,
130 VkPipelineCache pipelineCache)
131 {
132 stub_return(0);
133 }
134
135 VkResult anv_GetPipelineCacheData(
136 VkDevice device,
137 VkPipelineCache pipelineCache,
138 void* pData)
139 {
140 stub_return(VK_UNSUPPORTED);
141 }
142
143 VkResult anv_MergePipelineCaches(
144 VkDevice device,
145 VkPipelineCache destCache,
146 uint32_t srcCacheCount,
147 const VkPipelineCache* pSrcCaches)
148 {
149 stub_return(VK_UNSUPPORTED);
150 }
151
152 void anv_DestroyPipeline(
153 VkDevice _device,
154 VkPipeline _pipeline)
155 {
156 ANV_FROM_HANDLE(anv_device, device, _device);
157 ANV_FROM_HANDLE(anv_pipeline, pipeline, _pipeline);
158
159 anv_compiler_free(pipeline);
160 anv_reloc_list_finish(&pipeline->batch_relocs, pipeline->device);
161 anv_state_stream_finish(&pipeline->program_stream);
162 anv_state_pool_free(&device->dynamic_state_pool, pipeline->blend_state);
163 anv_device_free(pipeline->device, pipeline);
164 }
165
166 static const uint32_t vk_to_gen_primitive_type[] = {
167 [VK_PRIMITIVE_TOPOLOGY_POINT_LIST] = _3DPRIM_POINTLIST,
168 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST] = _3DPRIM_LINELIST,
169 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP] = _3DPRIM_LINESTRIP,
170 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST] = _3DPRIM_TRILIST,
171 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP] = _3DPRIM_TRISTRIP,
172 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN] = _3DPRIM_TRIFAN,
173 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST_ADJ] = _3DPRIM_LINELIST_ADJ,
174 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_ADJ] = _3DPRIM_LINESTRIP_ADJ,
175 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_ADJ] = _3DPRIM_TRILIST_ADJ,
176 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_ADJ] = _3DPRIM_TRISTRIP_ADJ,
177 [VK_PRIMITIVE_TOPOLOGY_PATCH] = _3DPRIM_PATCHLIST_1
178 };
179
180 static void
181 anv_pipeline_init_dynamic_state(struct anv_pipeline *pipeline,
182 const VkGraphicsPipelineCreateInfo *pCreateInfo)
183 {
184 pipeline->dynamic_state_mask = 0;
185
186 if (pCreateInfo->pDynamicState == NULL)
187 return;
188
189 uint32_t count = pCreateInfo->pDynamicState->dynamicStateCount;
190 struct anv_dynamic_state *dynamic = &pipeline->dynamic_state;
191
192 for (uint32_t s = 0; s < count; s++) {
193 VkDynamicState state = pCreateInfo->pDynamicState->pDynamicStates[s];
194
195 assert(state < 32);
196 pipeline->dynamic_state_mask |= (1u << state);
197
198 switch (state) {
199 case VK_DYNAMIC_STATE_VIEWPORT:
200 assert(pCreateInfo->pViewportState);
201 dynamic->viewport.count = pCreateInfo->pViewportState->viewportCount;
202 typed_memcpy(dynamic->viewport.viewports,
203 pCreateInfo->pViewportState->pViewports,
204 pCreateInfo->pViewportState->viewportCount);
205 break;
206
207 case VK_DYNAMIC_STATE_SCISSOR:
208 assert(pCreateInfo->pViewportState);
209 dynamic->scissor.count = pCreateInfo->pViewportState->scissorCount;
210 typed_memcpy(dynamic->scissor.scissors,
211 pCreateInfo->pViewportState->pScissors,
212 pCreateInfo->pViewportState->scissorCount);
213 break;
214
215 case VK_DYNAMIC_STATE_LINE_WIDTH:
216 assert(pCreateInfo->pRasterState);
217 dynamic->line_width = pCreateInfo->pRasterState->lineWidth;
218 break;
219
220 case VK_DYNAMIC_STATE_DEPTH_BIAS:
221 assert(pCreateInfo->pRasterState);
222 dynamic->depth_bias.bias = pCreateInfo->pRasterState->depthBias;
223 dynamic->depth_bias.clamp = pCreateInfo->pRasterState->depthBiasClamp;
224 dynamic->depth_bias.slope_scaled =
225 pCreateInfo->pRasterState->slopeScaledDepthBias;
226 break;
227
228 case VK_DYNAMIC_STATE_BLEND_CONSTANTS:
229 assert(pCreateInfo->pColorBlendState);
230 typed_memcpy(dynamic->blend_constants,
231 pCreateInfo->pColorBlendState->blendConst, 4);
232 break;
233
234 case VK_DYNAMIC_STATE_DEPTH_BOUNDS:
235 assert(pCreateInfo->pDepthStencilState);
236 dynamic->depth_bounds.min =
237 pCreateInfo->pDepthStencilState->minDepthBounds;
238 dynamic->depth_bounds.max =
239 pCreateInfo->pDepthStencilState->maxDepthBounds;
240 break;
241
242 case VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK:
243 assert(pCreateInfo->pDepthStencilState);
244 dynamic->stencil_compare_mask.front =
245 pCreateInfo->pDepthStencilState->front.stencilCompareMask;
246 dynamic->stencil_compare_mask.back =
247 pCreateInfo->pDepthStencilState->back.stencilCompareMask;
248 break;
249
250 case VK_DYNAMIC_STATE_STENCIL_WRITE_MASK:
251 assert(pCreateInfo->pDepthStencilState);
252 dynamic->stencil_write_mask.front =
253 pCreateInfo->pDepthStencilState->front.stencilWriteMask;
254 dynamic->stencil_write_mask.back =
255 pCreateInfo->pDepthStencilState->back.stencilWriteMask;
256 break;
257
258 case VK_DYNAMIC_STATE_STENCIL_REFERENCE:
259 assert(pCreateInfo->pDepthStencilState);
260 dynamic->stencil_reference.front =
261 pCreateInfo->pDepthStencilState->front.stencilReference;
262 dynamic->stencil_reference.back =
263 pCreateInfo->pDepthStencilState->back.stencilReference;
264 break;
265
266 default:
267 assert(!"Invalid dynamic state");
268 }
269 }
270 }
271
272 VkResult
273 anv_pipeline_init(struct anv_pipeline *pipeline, struct anv_device *device,
274 const VkGraphicsPipelineCreateInfo *pCreateInfo,
275 const struct anv_graphics_pipeline_create_info *extra)
276 {
277 VkResult result;
278
279 pipeline->device = device;
280 pipeline->layout = anv_pipeline_layout_from_handle(pCreateInfo->layout);
281 memset(pipeline->shaders, 0, sizeof(pipeline->shaders));
282
283 result = anv_reloc_list_init(&pipeline->batch_relocs, device);
284 if (result != VK_SUCCESS) {
285 anv_device_free(device, pipeline);
286 return result;
287 }
288 pipeline->batch.next = pipeline->batch.start = pipeline->batch_data;
289 pipeline->batch.end = pipeline->batch.start + sizeof(pipeline->batch_data);
290 pipeline->batch.relocs = &pipeline->batch_relocs;
291
292 anv_state_stream_init(&pipeline->program_stream,
293 &device->instruction_block_pool);
294
295 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
296 pipeline->shaders[pCreateInfo->pStages[i].stage] =
297 anv_shader_from_handle(pCreateInfo->pStages[i].shader);
298 }
299
300 anv_pipeline_init_dynamic_state(pipeline, pCreateInfo);
301
302 if (pCreateInfo->pTessellationState)
303 anv_finishme("VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO");
304 if (pCreateInfo->pViewportState)
305 anv_finishme("VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO");
306 if (pCreateInfo->pMultisampleState &&
307 pCreateInfo->pMultisampleState->rasterSamples > 1)
308 anv_finishme("VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO");
309
310 pipeline->use_repclear = extra && extra->use_repclear;
311
312 anv_compiler_run(device->compiler, pipeline);
313
314 const struct brw_wm_prog_data *wm_prog_data = &pipeline->wm_prog_data;
315
316 pipeline->ps_ksp2 = 0;
317 pipeline->ps_grf_start2 = 0;
318 if (pipeline->ps_simd8 != NO_KERNEL) {
319 pipeline->ps_ksp0 = pipeline->ps_simd8;
320 pipeline->ps_grf_start0 = wm_prog_data->base.dispatch_grf_start_reg;
321 if (pipeline->ps_simd16 != NO_KERNEL) {
322 pipeline->ps_ksp2 = pipeline->ps_simd16;
323 pipeline->ps_grf_start2 = wm_prog_data->dispatch_grf_start_reg_16;
324 }
325 } else if (pipeline->ps_simd16 != NO_KERNEL) {
326 pipeline->ps_ksp0 = pipeline->ps_simd16;
327 pipeline->ps_grf_start0 = wm_prog_data->dispatch_grf_start_reg_16;
328 } else {
329 unreachable("no ps shader");
330 }
331
332 const VkPipelineVertexInputStateCreateInfo *vi_info =
333 pCreateInfo->pVertexInputState;
334 pipeline->vb_used = 0;
335 for (uint32_t i = 0; i < vi_info->bindingCount; i++) {
336 const VkVertexInputBindingDescription *desc =
337 &vi_info->pVertexBindingDescriptions[i];
338
339 pipeline->vb_used |= 1 << desc->binding;
340 pipeline->binding_stride[desc->binding] = desc->strideInBytes;
341
342 /* Step rate is programmed per vertex element (attribute), not
343 * binding. Set up a map of which bindings step per instance, for
344 * reference by vertex element setup. */
345 switch (desc->stepRate) {
346 default:
347 case VK_VERTEX_INPUT_STEP_RATE_VERTEX:
348 pipeline->instancing_enable[desc->binding] = false;
349 break;
350 case VK_VERTEX_INPUT_STEP_RATE_INSTANCE:
351 pipeline->instancing_enable[desc->binding] = true;
352 break;
353 }
354 }
355
356 const VkPipelineInputAssemblyStateCreateInfo *ia_info =
357 pCreateInfo->pInputAssemblyState;
358 pipeline->primitive_restart = ia_info->primitiveRestartEnable;
359 pipeline->topology = vk_to_gen_primitive_type[ia_info->topology];
360
361 if (extra && extra->use_rectlist)
362 pipeline->topology = _3DPRIM_RECTLIST;
363
364 return VK_SUCCESS;
365 }
366
367 VkResult
368 anv_graphics_pipeline_create(
369 VkDevice _device,
370 const VkGraphicsPipelineCreateInfo *pCreateInfo,
371 const struct anv_graphics_pipeline_create_info *extra,
372 VkPipeline *pPipeline)
373 {
374 ANV_FROM_HANDLE(anv_device, device, _device);
375
376 switch (device->info.gen) {
377 case 7:
378 return gen7_graphics_pipeline_create(_device, pCreateInfo, extra, pPipeline);
379 case 8:
380 return gen8_graphics_pipeline_create(_device, pCreateInfo, extra, pPipeline);
381 default:
382 unreachable("unsupported gen\n");
383 }
384 }
385
386 VkResult anv_CreateGraphicsPipelines(
387 VkDevice _device,
388 VkPipelineCache pipelineCache,
389 uint32_t count,
390 const VkGraphicsPipelineCreateInfo* pCreateInfos,
391 VkPipeline* pPipelines)
392 {
393 VkResult result = VK_SUCCESS;
394
395 unsigned i = 0;
396 for (; i < count; i++) {
397 result = anv_graphics_pipeline_create(_device, &pCreateInfos[i],
398 NULL, &pPipelines[i]);
399 if (result != VK_SUCCESS) {
400 for (unsigned j = 0; j < i; j++) {
401 anv_DestroyPipeline(_device, pPipelines[j]);
402 }
403
404 return result;
405 }
406 }
407
408 return VK_SUCCESS;
409 }
410
411 static VkResult anv_compute_pipeline_create(
412 VkDevice _device,
413 const VkComputePipelineCreateInfo* pCreateInfo,
414 VkPipeline* pPipeline)
415 {
416 ANV_FROM_HANDLE(anv_device, device, _device);
417
418 switch (device->info.gen) {
419 case 7:
420 return gen7_compute_pipeline_create(_device, pCreateInfo, pPipeline);
421 case 8:
422 return gen8_compute_pipeline_create(_device, pCreateInfo, pPipeline);
423 default:
424 unreachable("unsupported gen\n");
425 }
426 }
427
428 VkResult anv_CreateComputePipelines(
429 VkDevice _device,
430 VkPipelineCache pipelineCache,
431 uint32_t count,
432 const VkComputePipelineCreateInfo* pCreateInfos,
433 VkPipeline* pPipelines)
434 {
435 VkResult result = VK_SUCCESS;
436
437 unsigned i = 0;
438 for (; i < count; i++) {
439 result = anv_compute_pipeline_create(_device, &pCreateInfos[i],
440 &pPipelines[i]);
441 if (result != VK_SUCCESS) {
442 for (unsigned j = 0; j < i; j++) {
443 anv_DestroyPipeline(_device, pPipelines[j]);
444 }
445
446 return result;
447 }
448 }
449
450 return VK_SUCCESS;
451 }
452
453 // Pipeline layout functions
454
455 VkResult anv_CreatePipelineLayout(
456 VkDevice _device,
457 const VkPipelineLayoutCreateInfo* pCreateInfo,
458 VkPipelineLayout* pPipelineLayout)
459 {
460 ANV_FROM_HANDLE(anv_device, device, _device);
461 struct anv_pipeline_layout l, *layout;
462
463 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
464
465 l.num_sets = pCreateInfo->descriptorSetCount;
466
467 unsigned dynamic_offset_count = 0;
468
469 memset(l.stage, 0, sizeof(l.stage));
470 for (uint32_t set = 0; set < pCreateInfo->descriptorSetCount; set++) {
471 ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout,
472 pCreateInfo->pSetLayouts[set]);
473 l.set[set].layout = set_layout;
474
475 l.set[set].dynamic_offset_start = dynamic_offset_count;
476 for (uint32_t b = 0; b < set_layout->binding_count; b++) {
477 if (set_layout->binding[b].dynamic_offset_index >= 0)
478 dynamic_offset_count += set_layout->binding[b].array_size;
479 }
480
481 for (VkShaderStage s = 0; s < VK_SHADER_STAGE_NUM; s++) {
482 l.set[set].stage[s].surface_start = l.stage[s].surface_count;
483 l.set[set].stage[s].sampler_start = l.stage[s].sampler_count;
484
485 for (uint32_t b = 0; b < set_layout->binding_count; b++) {
486 unsigned array_size = set_layout->binding[b].array_size;
487
488 if (set_layout->binding[b].stage[s].surface_index >= 0) {
489 l.stage[s].surface_count += array_size;
490
491 if (set_layout->binding[b].dynamic_offset_index >= 0)
492 l.stage[s].has_dynamic_offsets = true;
493 }
494
495 if (set_layout->binding[b].stage[s].sampler_index >= 0)
496 l.stage[s].sampler_count += array_size;
497 }
498 }
499 }
500
501 unsigned num_bindings = 0;
502 for (VkShaderStage s = 0; s < VK_SHADER_STAGE_NUM; s++)
503 num_bindings += l.stage[s].surface_count + l.stage[s].sampler_count;
504
505 size_t size = sizeof(*layout) + num_bindings * sizeof(layout->entries[0]);
506
507 layout = anv_device_alloc(device, size, 8, VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
508 if (layout == NULL)
509 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
510
511 /* Now we can actually build our surface and sampler maps */
512 struct anv_pipeline_binding *entry = layout->entries;
513 for (VkShaderStage s = 0; s < VK_SHADER_STAGE_NUM; s++) {
514 l.stage[s].surface_to_descriptor = entry;
515 entry += l.stage[s].surface_count;
516 l.stage[s].sampler_to_descriptor = entry;
517 entry += l.stage[s].sampler_count;
518
519 int surface = 0;
520 int sampler = 0;
521 for (uint32_t set = 0; set < pCreateInfo->descriptorSetCount; set++) {
522 struct anv_descriptor_set_layout *set_layout = l.set[set].layout;
523
524 unsigned set_offset = 0;
525 for (uint32_t b = 0; b < set_layout->binding_count; b++) {
526 unsigned array_size = set_layout->binding[b].array_size;
527
528 if (set_layout->binding[b].stage[s].surface_index >= 0) {
529 assert(surface == l.set[set].stage[s].surface_start +
530 set_layout->binding[b].stage[s].surface_index);
531 for (unsigned i = 0; i < array_size; i++) {
532 l.stage[s].surface_to_descriptor[surface + i].set = set;
533 l.stage[s].surface_to_descriptor[surface + i].offset = set_offset + i;
534 }
535 surface += array_size;
536 }
537
538 if (set_layout->binding[b].stage[s].sampler_index >= 0) {
539 assert(sampler == l.set[set].stage[s].sampler_start +
540 set_layout->binding[b].stage[s].sampler_index);
541 for (unsigned i = 0; i < array_size; i++) {
542 l.stage[s].sampler_to_descriptor[sampler + i].set = set;
543 l.stage[s].sampler_to_descriptor[sampler + i].offset = set_offset + i;
544 }
545 sampler += array_size;
546 }
547
548 set_offset += array_size;
549 }
550 }
551 }
552
553 /* Finally, we're done setting it up, copy into the allocated version */
554 *layout = l;
555
556 *pPipelineLayout = anv_pipeline_layout_to_handle(layout);
557
558 return VK_SUCCESS;
559 }
560
561 void anv_DestroyPipelineLayout(
562 VkDevice _device,
563 VkPipelineLayout _pipelineLayout)
564 {
565 ANV_FROM_HANDLE(anv_device, device, _device);
566 ANV_FROM_HANDLE(anv_pipeline_layout, pipeline_layout, _pipelineLayout);
567
568 anv_device_free(device, pipeline_layout);
569 }