261bdde5201a8141cfeba11edd52fa36a2c3d5af
[mesa.git] / src / gallium / drivers / zink / zink_pipeline.c
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
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 VkPipelineInputAssemblyStateCreateInfo primitive_state = {};
50 primitive_state.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
51 primitive_state.topology = primitive_topology;
52 primitive_state.primitiveRestartEnable = VK_FALSE;
53
54 VkPipelineColorBlendStateCreateInfo blend_state = {};
55 blend_state.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
56 blend_state.pAttachments = state->blend_state->attachments;
57 blend_state.attachmentCount = state->num_attachments;
58 blend_state.logicOpEnable = state->blend_state->logicop_enable;
59 blend_state.logicOp = state->blend_state->logicop_func;
60
61 VkPipelineMultisampleStateCreateInfo ms_state = {};
62 ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
63 ms_state.rasterizationSamples = state->rast_samples;
64 ms_state.alphaToCoverageEnable = state->blend_state->alpha_to_coverage;
65 ms_state.alphaToOneEnable = state->blend_state->alpha_to_one;
66 ms_state.pSampleMask = state->sample_mask ? &state->sample_mask : NULL;
67
68 VkPipelineViewportStateCreateInfo viewport_state = {};
69 viewport_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
70 viewport_state.viewportCount = 1;
71 viewport_state.pViewports = NULL;
72 viewport_state.scissorCount = 1;
73 viewport_state.pScissors = NULL;
74
75 VkPipelineRasterizationStateCreateInfo rast_state = {};
76 rast_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
77
78 rast_state.depthClampEnable = state->rast_state->depth_clamp;
79 rast_state.rasterizerDiscardEnable = state->rast_state->rasterizer_discard;
80 rast_state.polygonMode = state->rast_state->polygon_mode;
81 rast_state.cullMode = state->rast_state->cull_mode;
82 rast_state.frontFace = state->rast_state->front_face;
83
84 rast_state.depthBiasEnable = VK_TRUE;
85 rast_state.depthBiasConstantFactor = 0.0;
86 rast_state.depthBiasClamp = 0.0;
87 rast_state.depthBiasSlopeFactor = 0.0;
88 rast_state.lineWidth = 1.0f;
89
90 VkPipelineDepthStencilStateCreateInfo depth_stencil_state = {};
91 depth_stencil_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
92 depth_stencil_state.depthTestEnable = state->depth_stencil_alpha_state->depth_test;
93 depth_stencil_state.depthCompareOp = state->depth_stencil_alpha_state->depth_compare_op;
94 depth_stencil_state.depthBoundsTestEnable = state->depth_stencil_alpha_state->depth_bounds_test;
95 depth_stencil_state.minDepthBounds = state->depth_stencil_alpha_state->min_depth_bounds;
96 depth_stencil_state.maxDepthBounds = state->depth_stencil_alpha_state->max_depth_bounds;
97 depth_stencil_state.stencilTestEnable = state->depth_stencil_alpha_state->stencil_test;
98 depth_stencil_state.front = state->depth_stencil_alpha_state->stencil_front;
99 depth_stencil_state.back = state->depth_stencil_alpha_state->stencil_back;
100 depth_stencil_state.depthWriteEnable = state->depth_stencil_alpha_state->depth_write;
101
102 VkDynamicState dynamicStateEnables[] = {
103 VK_DYNAMIC_STATE_VIEWPORT,
104 VK_DYNAMIC_STATE_SCISSOR,
105 VK_DYNAMIC_STATE_LINE_WIDTH,
106 VK_DYNAMIC_STATE_DEPTH_BIAS,
107 VK_DYNAMIC_STATE_BLEND_CONSTANTS,
108 VK_DYNAMIC_STATE_STENCIL_REFERENCE,
109 };
110
111 VkPipelineDynamicStateCreateInfo pipelineDynamicStateCreateInfo = {};
112 pipelineDynamicStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
113 pipelineDynamicStateCreateInfo.pDynamicStates = dynamicStateEnables;
114 pipelineDynamicStateCreateInfo.dynamicStateCount = ARRAY_SIZE(dynamicStateEnables);
115
116 VkGraphicsPipelineCreateInfo pci = {};
117 pci.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
118 pci.flags = VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT;
119 pci.layout = prog->layout;
120 pci.renderPass = state->render_pass->render_pass;
121 pci.pVertexInputState = &vertex_input_state;
122 pci.pInputAssemblyState = &primitive_state;
123 pci.pRasterizationState = &rast_state;
124 pci.pColorBlendState = &blend_state;
125 pci.pMultisampleState = &ms_state;
126 pci.pViewportState = &viewport_state;
127 pci.pDepthStencilState = &depth_stencil_state;
128 pci.pDynamicState = &pipelineDynamicStateCreateInfo;
129
130 VkPipelineShaderStageCreateInfo shader_stages[PIPE_SHADER_TYPES - 1];
131 uint32_t num_stages = 0;
132 for (int i = 0; i < PIPE_SHADER_TYPES - 1; ++i) {
133 if (!prog->stages[i])
134 continue;
135
136 VkPipelineShaderStageCreateInfo stage = {};
137 stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
138 stage.stage = zink_shader_stage(i);
139 stage.module = prog->stages[i]->shader_module;
140 stage.pName = "main";
141 shader_stages[num_stages++] = stage;
142 }
143 assert(num_stages > 0);
144
145 pci.pStages = shader_stages;
146 pci.stageCount = num_stages;
147
148 VkPipeline pipeline;
149 if (vkCreateGraphicsPipelines(screen->dev, VK_NULL_HANDLE, 1, &pci,
150 NULL, &pipeline) != VK_SUCCESS) {
151 debug_printf("vkCreateGraphicsPipelines failed\n");
152 return VK_NULL_HANDLE;
153 }
154
155 return pipeline;
156 }