• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 Collabora Ltd.
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  * on the rights to use, copy, modify, merge, publish, distribute, sub
8  * license, and/or sell copies of the Software, and to permit persons to whom
9  * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21  * USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "zink_pipeline.h"
25 
26 #include "zink_compiler.h"
27 #include "zink_context.h"
28 #include "zink_program.h"
29 #include "zink_render_pass.h"
30 #include "zink_screen.h"
31 #include "zink_state.h"
32 
33 #include "util/u_debug.h"
34 #include "util/u_prim.h"
35 
36 VkPipeline
zink_create_gfx_pipeline(struct zink_screen * screen,struct zink_gfx_program * prog,struct zink_gfx_pipeline_state * state,VkPrimitiveTopology primitive_topology)37 zink_create_gfx_pipeline(struct zink_screen *screen,
38                          struct zink_gfx_program *prog,
39                          struct zink_gfx_pipeline_state *state,
40                          VkPrimitiveTopology primitive_topology)
41 {
42    VkPipelineVertexInputStateCreateInfo vertex_input_state = {};
43    vertex_input_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
44    vertex_input_state.pVertexBindingDescriptions = state->bindings;
45    vertex_input_state.vertexBindingDescriptionCount = state->element_state->num_bindings;
46    vertex_input_state.pVertexAttributeDescriptions = state->element_state->attribs;
47    vertex_input_state.vertexAttributeDescriptionCount = state->element_state->num_attribs;
48 
49    VkPipelineVertexInputDivisorStateCreateInfoEXT vdiv_state = {};
50    if (state->divisors_present) {
51        vertex_input_state.pNext = &vdiv_state;
52        vdiv_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_DIVISOR_STATE_CREATE_INFO_EXT;
53        vdiv_state.vertexBindingDivisorCount = state->divisors_present;
54        vdiv_state.pVertexBindingDivisors = state->divisors;
55    }
56 
57    VkPipelineInputAssemblyStateCreateInfo primitive_state = {};
58    primitive_state.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
59    primitive_state.topology = primitive_topology;
60    switch (primitive_topology) {
61    case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
62    case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
63    case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
64    case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
65    case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
66    case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
67       if (state->primitive_restart)
68          debug_printf("restart_index set with unsupported primitive topology %u\n", primitive_topology);
69       primitive_state.primitiveRestartEnable = VK_FALSE;
70       break;
71    default:
72       primitive_state.primitiveRestartEnable = state->primitive_restart ? VK_TRUE : VK_FALSE;
73    }
74 
75    VkPipelineColorBlendStateCreateInfo blend_state = {};
76    blend_state.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
77    blend_state.pAttachments = state->blend_state->attachments;
78    blend_state.attachmentCount = state->num_attachments;
79    blend_state.logicOpEnable = state->blend_state->logicop_enable;
80    blend_state.logicOp = state->blend_state->logicop_func;
81 
82    VkPipelineMultisampleStateCreateInfo ms_state = {};
83    ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
84    ms_state.rasterizationSamples = state->rast_samples;
85    ms_state.alphaToCoverageEnable = state->blend_state->alpha_to_coverage;
86    ms_state.alphaToOneEnable = state->blend_state->alpha_to_one;
87    ms_state.pSampleMask = state->sample_mask ? &state->sample_mask : NULL;
88 
89    VkPipelineViewportStateCreateInfo viewport_state = {};
90    viewport_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
91    viewport_state.viewportCount = state->num_viewports;
92    viewport_state.pViewports = NULL;
93    viewport_state.scissorCount = state->num_viewports;
94    viewport_state.pScissors = NULL;
95 
96    VkPipelineRasterizationStateCreateInfo rast_state = {};
97    rast_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
98 
99    rast_state.depthClampEnable = state->rast_state->depth_clamp;
100    rast_state.rasterizerDiscardEnable = state->rast_state->rasterizer_discard;
101    rast_state.polygonMode = state->rast_state->polygon_mode;
102    rast_state.cullMode = state->rast_state->cull_mode;
103    rast_state.frontFace = state->rast_state->front_face;
104 
105    rast_state.depthBiasEnable = VK_TRUE;
106    rast_state.depthBiasConstantFactor = 0.0;
107    rast_state.depthBiasClamp = 0.0;
108    rast_state.depthBiasSlopeFactor = 0.0;
109    rast_state.lineWidth = 1.0f;
110 
111    VkPipelineDepthStencilStateCreateInfo depth_stencil_state = {};
112    depth_stencil_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
113    depth_stencil_state.depthTestEnable = state->depth_stencil_alpha_state->depth_test;
114    depth_stencil_state.depthCompareOp = state->depth_stencil_alpha_state->depth_compare_op;
115    depth_stencil_state.depthBoundsTestEnable = state->depth_stencil_alpha_state->depth_bounds_test;
116    depth_stencil_state.minDepthBounds = state->depth_stencil_alpha_state->min_depth_bounds;
117    depth_stencil_state.maxDepthBounds = state->depth_stencil_alpha_state->max_depth_bounds;
118    depth_stencil_state.stencilTestEnable = state->depth_stencil_alpha_state->stencil_test;
119    depth_stencil_state.front = state->depth_stencil_alpha_state->stencil_front;
120    depth_stencil_state.back = state->depth_stencil_alpha_state->stencil_back;
121    depth_stencil_state.depthWriteEnable = state->depth_stencil_alpha_state->depth_write;
122 
123    VkDynamicState dynamicStateEnables[] = {
124       VK_DYNAMIC_STATE_VIEWPORT,
125       VK_DYNAMIC_STATE_SCISSOR,
126       VK_DYNAMIC_STATE_LINE_WIDTH,
127       VK_DYNAMIC_STATE_DEPTH_BIAS,
128       VK_DYNAMIC_STATE_BLEND_CONSTANTS,
129       VK_DYNAMIC_STATE_STENCIL_REFERENCE,
130    };
131 
132    VkPipelineDynamicStateCreateInfo pipelineDynamicStateCreateInfo = {};
133    pipelineDynamicStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
134    pipelineDynamicStateCreateInfo.pDynamicStates = dynamicStateEnables;
135    pipelineDynamicStateCreateInfo.dynamicStateCount = ARRAY_SIZE(dynamicStateEnables);
136 
137    VkGraphicsPipelineCreateInfo pci = {};
138    pci.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
139    pci.flags = VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT;
140    pci.layout = prog->layout;
141    pci.renderPass = state->render_pass->render_pass;
142    pci.pVertexInputState = &vertex_input_state;
143    pci.pInputAssemblyState = &primitive_state;
144    pci.pRasterizationState = &rast_state;
145    pci.pColorBlendState = &blend_state;
146    pci.pMultisampleState = &ms_state;
147    pci.pViewportState = &viewport_state;
148    pci.pDepthStencilState = &depth_stencil_state;
149    pci.pDynamicState = &pipelineDynamicStateCreateInfo;
150 
151    VkPipelineShaderStageCreateInfo shader_stages[ZINK_SHADER_COUNT];
152    uint32_t num_stages = 0;
153    for (int i = 0; i < ZINK_SHADER_COUNT; ++i) {
154       if (!prog->modules[i])
155          continue;
156 
157       VkPipelineShaderStageCreateInfo stage = {};
158       stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
159       stage.stage = zink_shader_stage(i);
160       stage.module = prog->modules[i]->shader;
161       stage.pName = "main";
162       shader_stages[num_stages++] = stage;
163    }
164    assert(num_stages > 0);
165 
166    pci.pStages = shader_stages;
167    pci.stageCount = num_stages;
168 
169    VkPipeline pipeline;
170    if (vkCreateGraphicsPipelines(screen->dev, VK_NULL_HANDLE, 1, &pci,
171                                  NULL, &pipeline) != VK_SUCCESS) {
172       debug_printf("vkCreateGraphicsPipelines failed\n");
173       return VK_NULL_HANDLE;
174    }
175 
176    return pipeline;
177 }
178