zink: introduce opengl over vulkan
[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_screen.h"
30 #include "zink_state.h"
31
32 #include "util/u_debug.h"
33 #include "util/u_prim.h"
34
35 VkPipeline
36 zink_create_gfx_pipeline(VkDevice dev, struct zink_gfx_program *prog,
37 struct zink_gfx_pipeline_state *state,
38 VkRenderPass render_pass)
39 {
40 VkPipelineVertexInputStateCreateInfo vertex_input_state = {};
41 vertex_input_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
42 vertex_input_state.pVertexBindingDescriptions = state->bindings;
43 vertex_input_state.vertexBindingDescriptionCount = state->element_state->num_bindings;
44 vertex_input_state.pVertexAttributeDescriptions = state->element_state->attribs;
45 vertex_input_state.vertexAttributeDescriptionCount = state->element_state->num_attribs;
46
47 VkPipelineInputAssemblyStateCreateInfo primitive_state = {};
48 primitive_state.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
49 primitive_state.topology = state->primitive_topology;
50 primitive_state.primitiveRestartEnable = VK_FALSE;
51
52 VkPipelineColorBlendStateCreateInfo blend_state = {};
53 blend_state.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
54 blend_state.pAttachments = state->blend_state->attachments;
55 blend_state.attachmentCount = state->num_attachments;
56 blend_state.logicOpEnable = state->blend_state->logicop_enable;
57 blend_state.logicOp = state->blend_state->logicop_func;
58
59 VkPipelineMultisampleStateCreateInfo ms_state = {};
60 ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
61 ms_state.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
62 ms_state.alphaToCoverageEnable = state->blend_state->alpha_to_coverage;
63 ms_state.alphaToOneEnable = state->blend_state->alpha_to_one;
64
65 VkPipelineViewportStateCreateInfo viewport_state = {};
66 viewport_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
67 viewport_state.viewportCount = 1;
68 viewport_state.pViewports = NULL;
69 viewport_state.scissorCount = 1;
70 viewport_state.pScissors = NULL;
71
72 VkPipelineRasterizationStateCreateInfo rast_state = {};
73 rast_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
74
75 rast_state.depthClampEnable = state->rast_state->depth_clamp;
76 rast_state.rasterizerDiscardEnable = state->rast_state->rasterizer_discard;
77 rast_state.polygonMode = state->rast_state->polygon_mode;
78 rast_state.cullMode = state->rast_state->cull_mode;
79 rast_state.frontFace = state->rast_state->front_face;
80
81 rast_state.depthBiasEnable = VK_TRUE;
82 rast_state.depthBiasConstantFactor = 0.0;
83 rast_state.depthBiasClamp = 0.0;
84 rast_state.depthBiasSlopeFactor = 0.0;
85 rast_state.lineWidth = state->line_width;
86
87 VkPipelineDepthStencilStateCreateInfo depth_stencil_state = {};
88 depth_stencil_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
89 depth_stencil_state.depthTestEnable = state->depth_stencil_alpha_state->depth_test;
90 depth_stencil_state.depthCompareOp = state->depth_stencil_alpha_state->depth_compare_op;
91 depth_stencil_state.depthBoundsTestEnable = state->depth_stencil_alpha_state->depth_bounds_test;
92 depth_stencil_state.minDepthBounds = state->depth_stencil_alpha_state->min_depth_bounds;
93 depth_stencil_state.maxDepthBounds = state->depth_stencil_alpha_state->max_depth_bounds;
94 depth_stencil_state.stencilTestEnable = state->depth_stencil_alpha_state->stencil_test;
95 depth_stencil_state.front = state->depth_stencil_alpha_state->stencil_front;
96 depth_stencil_state.back = state->depth_stencil_alpha_state->stencil_back;
97 depth_stencil_state.depthWriteEnable = state->depth_stencil_alpha_state->depth_write;
98
99 VkDynamicState dynamicStateEnables[] = {
100 VK_DYNAMIC_STATE_DEPTH_BIAS,
101 VK_DYNAMIC_STATE_SCISSOR,
102 VK_DYNAMIC_STATE_STENCIL_REFERENCE,
103 VK_DYNAMIC_STATE_VIEWPORT,
104 VK_DYNAMIC_STATE_BLEND_CONSTANTS,
105 };
106
107 VkPipelineDynamicStateCreateInfo pipelineDynamicStateCreateInfo = {};
108 pipelineDynamicStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
109 pipelineDynamicStateCreateInfo.pDynamicStates = dynamicStateEnables;
110 pipelineDynamicStateCreateInfo.dynamicStateCount = ARRAY_SIZE(dynamicStateEnables);
111
112 VkGraphicsPipelineCreateInfo pci = {};
113 pci.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
114 pci.flags = VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT;
115 pci.layout = prog->layout;
116 pci.renderPass = render_pass;
117 pci.pVertexInputState = &vertex_input_state;
118 pci.pInputAssemblyState = &primitive_state;
119 pci.pRasterizationState = &rast_state;
120 pci.pColorBlendState = &blend_state;
121 pci.pMultisampleState = &ms_state;
122 pci.pViewportState = &viewport_state;
123 pci.pDepthStencilState = &depth_stencil_state;
124 pci.pDynamicState = &pipelineDynamicStateCreateInfo;
125
126 VkPipelineShaderStageCreateInfo shader_stages[PIPE_SHADER_TYPES - 1];
127 uint32_t num_stages = 0;
128 for (int i = 0; i < PIPE_SHADER_TYPES - 1; ++i) {
129 if (!prog->stages[i])
130 continue;
131
132 VkPipelineShaderStageCreateInfo stage = {};
133 stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
134 stage.stage = zink_shader_stage(i);
135 stage.module = prog->stages[i]->shader_module;
136 stage.pName = "main";
137 shader_stages[num_stages++] = stage;
138 }
139 assert(num_stages > 0);
140
141 pci.pStages = shader_stages;
142 pci.stageCount = num_stages;
143
144 VkPipeline pipeline;
145 if (vkCreateGraphicsPipelines(dev, VK_NULL_HANDLE, 1, &pci, NULL, &pipeline) != VK_SUCCESS) {
146 debug_printf("vkCreateGraphicsPipelines failed\n");
147 return VK_NULL_HANDLE;
148 }
149
150 return pipeline;
151 }