Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/drivers/zink/zink_pipeline.c
4570 views
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 "compiler/spirv/spirv.h"
25
26
#include "zink_pipeline.h"
27
28
#include "zink_compiler.h"
29
#include "zink_context.h"
30
#include "zink_program.h"
31
#include "zink_render_pass.h"
32
#include "zink_screen.h"
33
#include "zink_state.h"
34
35
#include "util/u_debug.h"
36
#include "util/u_prim.h"
37
38
static VkBlendFactor
39
clamp_void_blend_factor(VkBlendFactor f)
40
{
41
if (f == VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA)
42
return VK_BLEND_FACTOR_ZERO;
43
if (f == VK_BLEND_FACTOR_DST_ALPHA)
44
return VK_BLEND_FACTOR_ONE;
45
return f;
46
}
47
48
VkPipeline
49
zink_create_gfx_pipeline(struct zink_screen *screen,
50
struct zink_gfx_program *prog,
51
struct zink_gfx_pipeline_state *state,
52
VkPrimitiveTopology primitive_topology)
53
{
54
VkPipelineVertexInputStateCreateInfo vertex_input_state = {0};
55
vertex_input_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
56
vertex_input_state.pVertexBindingDescriptions = state->element_state->bindings;
57
vertex_input_state.vertexBindingDescriptionCount = state->element_state->num_bindings;
58
vertex_input_state.pVertexAttributeDescriptions = state->element_state->attribs;
59
vertex_input_state.vertexAttributeDescriptionCount = state->element_state->num_attribs;
60
61
VkPipelineVertexInputDivisorStateCreateInfoEXT vdiv_state = {0};
62
if (state->element_state->divisors_present) {
63
vertex_input_state.pNext = &vdiv_state;
64
vdiv_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_DIVISOR_STATE_CREATE_INFO_EXT;
65
vdiv_state.vertexBindingDivisorCount = state->element_state->divisors_present;
66
vdiv_state.pVertexBindingDivisors = state->element_state->divisors;
67
}
68
69
VkPipelineInputAssemblyStateCreateInfo primitive_state = {0};
70
primitive_state.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
71
primitive_state.topology = primitive_topology;
72
switch (primitive_topology) {
73
case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
74
case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
75
case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
76
case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
77
case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
78
case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
79
if (state->primitive_restart)
80
debug_printf("restart_index set with unsupported primitive topology %u\n", primitive_topology);
81
primitive_state.primitiveRestartEnable = VK_FALSE;
82
break;
83
default:
84
primitive_state.primitiveRestartEnable = state->primitive_restart ? VK_TRUE : VK_FALSE;
85
}
86
87
VkPipelineColorBlendAttachmentState blend_att[PIPE_MAX_COLOR_BUFS];
88
VkPipelineColorBlendStateCreateInfo blend_state = {0};
89
blend_state.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
90
if (state->void_alpha_attachments) {
91
for (unsigned i = 0; i < state->num_attachments; i++) {
92
blend_att[i] = state->blend_state->attachments[i];
93
if (state->void_alpha_attachments & BITFIELD_BIT(i)) {
94
blend_att[i].dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
95
blend_att[i].srcColorBlendFactor = clamp_void_blend_factor(blend_att[i].srcColorBlendFactor);
96
blend_att[i].dstColorBlendFactor = clamp_void_blend_factor(blend_att[i].dstColorBlendFactor);
97
}
98
}
99
blend_state.pAttachments = blend_att;
100
} else
101
blend_state.pAttachments = state->blend_state->attachments;
102
blend_state.attachmentCount = state->num_attachments;
103
blend_state.logicOpEnable = state->blend_state->logicop_enable;
104
blend_state.logicOp = state->blend_state->logicop_func;
105
106
VkPipelineMultisampleStateCreateInfo ms_state = {0};
107
ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
108
ms_state.rasterizationSamples = state->rast_samples;
109
ms_state.alphaToCoverageEnable = state->blend_state->alpha_to_coverage;
110
ms_state.alphaToOneEnable = state->blend_state->alpha_to_one;
111
ms_state.pSampleMask = state->sample_mask ? &state->sample_mask : NULL;
112
if (state->rast_state->force_persample_interp) {
113
ms_state.sampleShadingEnable = VK_TRUE;
114
ms_state.minSampleShading = 1.0;
115
}
116
117
VkPipelineViewportStateCreateInfo viewport_state = {0};
118
viewport_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
119
viewport_state.viewportCount = state->num_viewports;
120
viewport_state.pViewports = NULL;
121
viewport_state.scissorCount = state->num_viewports;
122
viewport_state.pScissors = NULL;
123
124
VkPipelineRasterizationStateCreateInfo rast_state = {0};
125
rast_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
126
127
rast_state.depthClampEnable = state->rast_state->depth_clamp;
128
rast_state.rasterizerDiscardEnable = state->rast_state->rasterizer_discard;
129
rast_state.polygonMode = state->rast_state->polygon_mode;
130
rast_state.cullMode = state->rast_state->cull_mode;
131
rast_state.frontFace = state->front_face;
132
133
rast_state.depthBiasEnable = VK_TRUE;
134
rast_state.depthBiasConstantFactor = 0.0;
135
rast_state.depthBiasClamp = 0.0;
136
rast_state.depthBiasSlopeFactor = 0.0;
137
rast_state.lineWidth = 1.0f;
138
139
VkPipelineRasterizationProvokingVertexStateCreateInfoEXT pv_state;
140
pv_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_PROVOKING_VERTEX_STATE_CREATE_INFO_EXT;
141
pv_state.provokingVertexMode = state->rast_state->pv_mode;
142
if (screen->info.have_EXT_provoking_vertex &&
143
state->rast_state->pv_mode == VK_PROVOKING_VERTEX_MODE_LAST_VERTEX_EXT) {
144
pv_state.pNext = rast_state.pNext;
145
rast_state.pNext = &pv_state;
146
}
147
148
VkPipelineRasterizationLineStateCreateInfoEXT rast_line_state;
149
if (screen->info.have_EXT_line_rasterization) {
150
rast_line_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_LINE_STATE_CREATE_INFO_EXT;
151
rast_line_state.pNext = rast_state.pNext;
152
rast_line_state.lineRasterizationMode = state->rast_state->line_mode;
153
154
if (state->rast_state->line_stipple_pattern != UINT16_MAX) {
155
rast_line_state.stippledLineEnable = VK_TRUE;
156
rast_line_state.lineStippleFactor = state->rast_state->line_stipple_factor + 1;
157
rast_line_state.lineStipplePattern = state->rast_state->line_stipple_pattern;
158
} else {
159
rast_line_state.stippledLineEnable = VK_FALSE;
160
rast_line_state.lineStippleFactor = 0;
161
rast_line_state.lineStipplePattern = 0;
162
}
163
rast_state.pNext = &rast_line_state;
164
}
165
166
VkPipelineDepthStencilStateCreateInfo depth_stencil_state = {0};
167
depth_stencil_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
168
depth_stencil_state.depthTestEnable = state->depth_stencil_alpha_state->depth_test;
169
depth_stencil_state.depthCompareOp = state->depth_stencil_alpha_state->depth_compare_op;
170
depth_stencil_state.depthBoundsTestEnable = state->depth_stencil_alpha_state->depth_bounds_test;
171
depth_stencil_state.minDepthBounds = state->depth_stencil_alpha_state->min_depth_bounds;
172
depth_stencil_state.maxDepthBounds = state->depth_stencil_alpha_state->max_depth_bounds;
173
depth_stencil_state.stencilTestEnable = state->depth_stencil_alpha_state->stencil_test;
174
depth_stencil_state.front = state->depth_stencil_alpha_state->stencil_front;
175
depth_stencil_state.back = state->depth_stencil_alpha_state->stencil_back;
176
depth_stencil_state.depthWriteEnable = state->depth_stencil_alpha_state->depth_write;
177
178
VkDynamicState dynamicStateEnables[24] = {
179
VK_DYNAMIC_STATE_LINE_WIDTH,
180
VK_DYNAMIC_STATE_DEPTH_BIAS,
181
VK_DYNAMIC_STATE_BLEND_CONSTANTS,
182
VK_DYNAMIC_STATE_STENCIL_REFERENCE,
183
};
184
unsigned state_count = 4;
185
if (screen->info.have_EXT_extended_dynamic_state) {
186
dynamicStateEnables[state_count++] = VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT;
187
dynamicStateEnables[state_count++] = VK_DYNAMIC_STATE_SCISSOR_WITH_COUNT_EXT;
188
dynamicStateEnables[state_count++] = VK_DYNAMIC_STATE_DEPTH_BOUNDS;
189
dynamicStateEnables[state_count++] = VK_DYNAMIC_STATE_DEPTH_BOUNDS_TEST_ENABLE_EXT;
190
dynamicStateEnables[state_count++] = VK_DYNAMIC_STATE_DEPTH_COMPARE_OP_EXT;
191
dynamicStateEnables[state_count++] = VK_DYNAMIC_STATE_DEPTH_TEST_ENABLE_EXT;
192
dynamicStateEnables[state_count++] = VK_DYNAMIC_STATE_DEPTH_WRITE_ENABLE_EXT;
193
dynamicStateEnables[state_count++] = VK_DYNAMIC_STATE_STENCIL_WRITE_MASK;
194
dynamicStateEnables[state_count++] = VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK;
195
dynamicStateEnables[state_count++] = VK_DYNAMIC_STATE_STENCIL_OP_EXT;
196
dynamicStateEnables[state_count++] = VK_DYNAMIC_STATE_STENCIL_TEST_ENABLE_EXT;
197
dynamicStateEnables[state_count++] = VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE_EXT;
198
dynamicStateEnables[state_count++] = VK_DYNAMIC_STATE_FRONT_FACE_EXT;
199
if (state->sample_locations_enabled)
200
dynamicStateEnables[state_count++] = VK_DYNAMIC_STATE_SAMPLE_LOCATIONS_EXT;
201
} else {
202
dynamicStateEnables[state_count++] = VK_DYNAMIC_STATE_VIEWPORT;
203
dynamicStateEnables[state_count++] = VK_DYNAMIC_STATE_SCISSOR;
204
}
205
206
VkPipelineDynamicStateCreateInfo pipelineDynamicStateCreateInfo = {0};
207
pipelineDynamicStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
208
pipelineDynamicStateCreateInfo.pDynamicStates = dynamicStateEnables;
209
pipelineDynamicStateCreateInfo.dynamicStateCount = state_count;
210
211
VkGraphicsPipelineCreateInfo pci = {0};
212
pci.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
213
pci.layout = prog->base.layout;
214
pci.renderPass = state->render_pass->render_pass;
215
pci.pVertexInputState = &vertex_input_state;
216
pci.pInputAssemblyState = &primitive_state;
217
pci.pRasterizationState = &rast_state;
218
pci.pColorBlendState = &blend_state;
219
pci.pMultisampleState = &ms_state;
220
pci.pViewportState = &viewport_state;
221
pci.pDepthStencilState = &depth_stencil_state;
222
pci.pDynamicState = &pipelineDynamicStateCreateInfo;
223
224
VkPipelineTessellationStateCreateInfo tci = {0};
225
VkPipelineTessellationDomainOriginStateCreateInfo tdci = {0};
226
if (prog->shaders[PIPE_SHADER_TESS_CTRL] && prog->shaders[PIPE_SHADER_TESS_EVAL]) {
227
tci.sType = VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO;
228
tci.patchControlPoints = state->vertices_per_patch;
229
pci.pTessellationState = &tci;
230
tci.pNext = &tdci;
231
tdci.sType = VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_DOMAIN_ORIGIN_STATE_CREATE_INFO;
232
tdci.domainOrigin = VK_TESSELLATION_DOMAIN_ORIGIN_LOWER_LEFT;
233
}
234
235
VkPipelineShaderStageCreateInfo shader_stages[ZINK_SHADER_COUNT];
236
uint32_t num_stages = 0;
237
for (int i = 0; i < ZINK_SHADER_COUNT; ++i) {
238
if (!prog->modules[i])
239
continue;
240
241
VkPipelineShaderStageCreateInfo stage = {0};
242
stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
243
stage.stage = zink_shader_stage(i);
244
stage.module = prog->modules[i]->shader;
245
stage.pName = "main";
246
shader_stages[num_stages++] = stage;
247
}
248
assert(num_stages > 0);
249
250
pci.pStages = shader_stages;
251
pci.stageCount = num_stages;
252
253
VkPipeline pipeline;
254
if (vkCreateGraphicsPipelines(screen->dev, prog->base.pipeline_cache, 1, &pci,
255
NULL, &pipeline) != VK_SUCCESS) {
256
debug_printf("vkCreateGraphicsPipelines failed\n");
257
return VK_NULL_HANDLE;
258
}
259
zink_screen_update_pipeline_cache(screen, &prog->base);
260
261
return pipeline;
262
}
263
264
VkPipeline
265
zink_create_compute_pipeline(struct zink_screen *screen, struct zink_compute_program *comp, struct zink_compute_pipeline_state *state)
266
{
267
VkComputePipelineCreateInfo pci = {0};
268
pci.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO;
269
pci.layout = comp->base.layout;
270
271
VkPipelineShaderStageCreateInfo stage = {0};
272
stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
273
stage.stage = VK_SHADER_STAGE_COMPUTE_BIT;
274
stage.module = comp->module->shader;
275
stage.pName = "main";
276
277
VkSpecializationInfo sinfo = {0};
278
VkSpecializationMapEntry me[3];
279
if (state->use_local_size) {
280
stage.pSpecializationInfo = &sinfo;
281
sinfo.mapEntryCount = 3;
282
sinfo.pMapEntries = &me[0];
283
sinfo.dataSize = sizeof(state->local_size);
284
sinfo.pData = &state->local_size[0];
285
uint32_t ids[] = {ZINK_WORKGROUP_SIZE_X, ZINK_WORKGROUP_SIZE_Y, ZINK_WORKGROUP_SIZE_Z};
286
for (int i = 0; i < 3; i++) {
287
me[i].size = sizeof(uint32_t);
288
me[i].constantID = ids[i];
289
me[i].offset = i * sizeof(uint32_t);
290
}
291
}
292
293
pci.stage = stage;
294
295
VkPipeline pipeline;
296
if (vkCreateComputePipelines(screen->dev, comp->base.pipeline_cache, 1, &pci,
297
NULL, &pipeline) != VK_SUCCESS) {
298
debug_printf("vkCreateComputePipelines failed\n");
299
return VK_NULL_HANDLE;
300
}
301
zink_screen_update_pipeline_cache(screen, &comp->base);
302
303
return pipeline;
304
}
305
306