OSDN Git Service

anv: Add a struct for storing a compiled shader
[android-x86/external-mesa.git] / src / intel / vulkan / genX_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 "anv_private.h"
25
26 #include "genxml/gen_macros.h"
27 #include "genxml/genX_pack.h"
28
29 VkResult
30 genX(compute_pipeline_create)(
31     VkDevice                                    _device,
32     struct anv_pipeline_cache *                 cache,
33     const VkComputePipelineCreateInfo*          pCreateInfo,
34     const VkAllocationCallbacks*                pAllocator,
35     VkPipeline*                                 pPipeline)
36 {
37    ANV_FROM_HANDLE(anv_device, device, _device);
38    struct anv_pipeline *pipeline;
39    VkResult result;
40
41    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO);
42
43    pipeline = anv_alloc2(&device->alloc, pAllocator, sizeof(*pipeline), 8,
44                          VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
45    if (pipeline == NULL)
46       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
47
48    pipeline->device = device;
49    pipeline->layout = anv_pipeline_layout_from_handle(pCreateInfo->layout);
50
51    pipeline->blend_state.map = NULL;
52
53    result = anv_reloc_list_init(&pipeline->batch_relocs,
54                                 pAllocator ? pAllocator : &device->alloc);
55    if (result != VK_SUCCESS) {
56       anv_free2(&device->alloc, pAllocator, pipeline);
57       return result;
58    }
59    pipeline->batch.next = pipeline->batch.start = pipeline->batch_data;
60    pipeline->batch.end = pipeline->batch.start + sizeof(pipeline->batch_data);
61    pipeline->batch.relocs = &pipeline->batch_relocs;
62
63    /* When we free the pipeline, we detect stages based on the NULL status
64     * of various prog_data pointers.  Make them NULL by default.
65     */
66    memset(pipeline->prog_data, 0, sizeof(pipeline->prog_data));
67    memset(pipeline->bindings, 0, sizeof(pipeline->bindings));
68
69    pipeline->vs_simd8 = NO_KERNEL;
70    pipeline->vs_vec4 = NO_KERNEL;
71    pipeline->gs_kernel = NO_KERNEL;
72
73    pipeline->active_stages = 0;
74
75    pipeline->needs_data_cache = false;
76
77    assert(pCreateInfo->stage.stage == VK_SHADER_STAGE_COMPUTE_BIT);
78    ANV_FROM_HANDLE(anv_shader_module, module,  pCreateInfo->stage.module);
79    result = anv_pipeline_compile_cs(pipeline, cache, pCreateInfo, module,
80                                     pCreateInfo->stage.pName,
81                                     pCreateInfo->stage.pSpecializationInfo);
82    if (result != VK_SUCCESS) {
83       anv_free2(&device->alloc, pAllocator, pipeline);
84       return result;
85    }
86
87    pipeline->use_repclear = false;
88
89    anv_setup_pipeline_l3_config(pipeline);
90
91    const struct brw_cs_prog_data *cs_prog_data = get_cs_prog_data(pipeline);
92
93    uint32_t group_size = cs_prog_data->local_size[0] *
94       cs_prog_data->local_size[1] * cs_prog_data->local_size[2];
95    uint32_t remainder = group_size & (cs_prog_data->simd_size - 1);
96
97    if (remainder > 0)
98       pipeline->cs_right_mask = ~0u >> (32 - remainder);
99    else
100       pipeline->cs_right_mask = ~0u >> (32 - cs_prog_data->simd_size);
101
102    const uint32_t vfe_curbe_allocation =
103       ALIGN(cs_prog_data->push.per_thread.regs * cs_prog_data->threads +
104             cs_prog_data->push.cross_thread.regs, 2);
105
106    anv_batch_emit(&pipeline->batch, GENX(MEDIA_VFE_STATE), vfe) {
107       vfe.ScratchSpaceBasePointer = (struct anv_address) {
108          .bo = anv_scratch_pool_alloc(device, &device->scratch_pool,
109                                       MESA_SHADER_COMPUTE,
110                                       cs_prog_data->base.total_scratch),
111          .offset = 0,
112       };
113       vfe.PerThreadScratchSpace  = ffs(cs_prog_data->base.total_scratch / 2048);
114 #if GEN_GEN > 7
115       vfe.StackSize              = 0;
116 #else
117       vfe.GPGPUMode              = true;
118 #endif
119       vfe.MaximumNumberofThreads = device->info.max_cs_threads - 1;
120       vfe.NumberofURBEntries     = GEN_GEN <= 7 ? 0 : 2;
121       vfe.ResetGatewayTimer      = true;
122 #if GEN_GEN <= 8
123       vfe.BypassGatewayControl   = true;
124 #endif
125       vfe.URBEntryAllocationSize = GEN_GEN <= 7 ? 0 : 2;
126       vfe.CURBEAllocationSize    = vfe_curbe_allocation;
127    }
128
129    *pPipeline = anv_pipeline_to_handle(pipeline);
130
131    return VK_SUCCESS;
132 }