@@ -105,6 +105,40 @@ VkImageLayout image_layout_ti_to_vk(ImageLayout layout) {
105
105
return image_layout_ti_2_vk.at (layout);
106
106
}
107
107
108
+ const std::unordered_map<BlendOp, VkBlendOp> blend_op_ti_2_vk = {
109
+ {BlendOp::add, VK_BLEND_OP_ADD},
110
+ {BlendOp::subtract, VK_BLEND_OP_SUBTRACT},
111
+ {BlendOp::reverse_subtract, VK_BLEND_OP_REVERSE_SUBTRACT},
112
+ {BlendOp::min, VK_BLEND_OP_MIN},
113
+ {BlendOp::max, VK_BLEND_OP_MAX}};
114
+
115
+ VkBlendOp blend_op_ti_to_vk (BlendOp op) {
116
+ if (blend_op_ti_2_vk.find (op) == blend_op_ti_2_vk.end ()) {
117
+ TI_ERROR (" BlendOp cannot be mapped to vk" );
118
+ }
119
+ return blend_op_ti_2_vk.at (op);
120
+ }
121
+
122
+ const std::unordered_map<BlendFactor, VkBlendFactor> blend_factor_ti_2_vk = {
123
+ {BlendFactor::zero, VK_BLEND_FACTOR_ZERO},
124
+ {BlendFactor::one, VK_BLEND_FACTOR_ONE},
125
+ {BlendFactor::src_color, VK_BLEND_FACTOR_SRC_COLOR},
126
+ {BlendFactor::one_minus_src_color, VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR},
127
+ {BlendFactor::dst_color, VK_BLEND_FACTOR_DST_COLOR},
128
+ {BlendFactor::one_minus_dst_color, VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR},
129
+ {BlendFactor::src_alpha, VK_BLEND_FACTOR_SRC_ALPHA},
130
+ {BlendFactor::one_minus_src_alpha, VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA},
131
+ {BlendFactor::dst_alpha, VK_BLEND_FACTOR_DST_ALPHA},
132
+ {BlendFactor::one_minus_dst_alpha, VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA},
133
+ };
134
+
135
+ VkBlendFactor blend_factor_ti_to_vk (BlendFactor factor) {
136
+ if (blend_factor_ti_2_vk.find (factor) == blend_factor_ti_2_vk.end ()) {
137
+ TI_ERROR (" BlendFactor cannot be mapped to vk" );
138
+ }
139
+ return blend_factor_ti_2_vk.at (factor);
140
+ }
141
+
108
142
VulkanPipeline::VulkanPipeline (const Params ¶ms)
109
143
: device_(params.device->vk_device ()), name_(params.name) {
110
144
create_descriptor_set_layout (params);
@@ -124,6 +158,9 @@ VulkanPipeline::VulkanPipeline(
124
158
const std::vector<VertexInputBinding> &vertex_inputs,
125
159
const std::vector<VertexInputAttribute> &vertex_attrs)
126
160
: device_(params.device->vk_device ()), name_(params.name) {
161
+ this ->graphics_pipeline_template_ =
162
+ std::make_unique<GraphicsPipelineTemplate>();
163
+
127
164
create_descriptor_set_layout (params);
128
165
create_shader_stages (params);
129
166
create_pipeline_layout ();
@@ -159,38 +196,12 @@ vkapi::IVkPipeline VulkanPipeline::graphics_pipeline(
159
196
return graphics_pipeline_.at (renderpass);
160
197
}
161
198
162
- std::vector<VkPipelineColorBlendAttachmentState> blend_attachments (
163
- renderpass_desc.color_attachments .size ());
164
- for (int i = 0 ; i < renderpass_desc.color_attachments .size (); i++) {
165
- blend_attachments[i].colorWriteMask =
166
- VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
167
- VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
168
- blend_attachments[i].blendEnable = VK_TRUE;
169
- blend_attachments[i].srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
170
- blend_attachments[i].dstColorBlendFactor =
171
- VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
172
- blend_attachments[i].colorBlendOp = VK_BLEND_OP_ADD;
173
- blend_attachments[i].srcAlphaBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
174
- blend_attachments[i].dstAlphaBlendFactor =
175
- VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
176
- blend_attachments[i].alphaBlendOp = VK_BLEND_OP_ADD;
177
- }
178
-
179
- graphics_pipeline_template_->color_blending .attachmentCount =
180
- renderpass_desc.color_attachments .size ();
181
- graphics_pipeline_template_->color_blending .pAttachments =
182
- blend_attachments.data ();
183
-
184
199
vkapi::IVkPipeline pipeline = vkapi::create_graphics_pipeline (
185
200
device_, &graphics_pipeline_template_->pipeline_info , renderpass,
186
201
pipeline_layout_);
187
202
188
203
graphics_pipeline_[renderpass] = pipeline;
189
204
190
- graphics_pipeline_template_->color_blending .attachmentCount = 0 ;
191
- graphics_pipeline_template_->color_blending .pAttachments = nullptr ;
192
- graphics_pipeline_template_->pipeline_info .renderPass = VK_NULL_HANDLE;
193
-
194
205
return pipeline;
195
206
}
196
207
@@ -252,6 +263,39 @@ void VulkanPipeline::create_descriptor_set_layout(const Params ¶ms) {
252
263
// TI_WARN("attrib {}:{}", location, type->type_name);
253
264
// }
254
265
// }
266
+
267
+ if (code_view.stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
268
+ uint32_t render_target_count = 0 ;
269
+ result = spvReflectEnumerateOutputVariables (&module, &render_target_count,
270
+ nullptr );
271
+ TI_ASSERT (result == SPV_REFLECT_RESULT_SUCCESS);
272
+
273
+ std::vector<SpvReflectInterfaceVariable *> variables (render_target_count);
274
+ result = spvReflectEnumerateOutputVariables (&module, &render_target_count,
275
+ variables.data ());
276
+
277
+ render_target_count = 0 ;
278
+
279
+ for (auto var : variables) {
280
+ // We want to remove auxiliary outputs such as frag depth
281
+ if (var->built_in == -1 ) {
282
+ render_target_count++;
283
+ }
284
+ }
285
+
286
+ graphics_pipeline_template_->blend_attachments .resize (
287
+ render_target_count);
288
+
289
+ VkPipelineColorBlendAttachmentState default_state{};
290
+ default_state.colorWriteMask =
291
+ VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
292
+ VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
293
+ default_state.blendEnable = VK_FALSE;
294
+
295
+ std::fill (graphics_pipeline_template_->blend_attachments .begin (),
296
+ graphics_pipeline_template_->blend_attachments .end (),
297
+ default_state);
298
+ }
255
299
}
256
300
257
301
for (uint32_t set : sets_used) {
@@ -295,9 +339,6 @@ void VulkanPipeline::create_graphics_pipeline(
295
339
const RasterParams &raster_params,
296
340
const std::vector<VertexInputBinding> &vertex_inputs,
297
341
const std::vector<VertexInputAttribute> &vertex_attrs) {
298
- this ->graphics_pipeline_template_ =
299
- std::make_unique<GraphicsPipelineTemplate>();
300
-
301
342
// Use dynamic viewport state. These two are just dummies
302
343
VkViewport viewport;
303
344
viewport.width = 1 ;
@@ -411,13 +452,45 @@ void VulkanPipeline::create_graphics_pipeline(
411
452
VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
412
453
color_blending.logicOpEnable = VK_FALSE;
413
454
color_blending.logicOp = VK_LOGIC_OP_COPY;
414
- color_blending.attachmentCount = 0 ;
415
- color_blending.pAttachments = nullptr ; // Filled in later
455
+ color_blending.attachmentCount =
456
+ graphics_pipeline_template_->blend_attachments .size ();
457
+ color_blending.pAttachments =
458
+ graphics_pipeline_template_->blend_attachments .data ();
416
459
color_blending.blendConstants [0 ] = 0 .0f ;
417
460
color_blending.blendConstants [1 ] = 0 .0f ;
418
461
color_blending.blendConstants [2 ] = 0 .0f ;
419
462
color_blending.blendConstants [3 ] = 0 .0f ;
420
463
464
+ if (raster_params.blending .size ()) {
465
+ TI_ASSERT_INFO (raster_params.blending .size () ==
466
+ graphics_pipeline_template_->blend_attachments .size (),
467
+ " RasterParams::blending (size={}) must either be zero sized "
468
+ " or match the number of fragment shader outputs (size={})." ,
469
+ raster_params.blending .size (),
470
+ graphics_pipeline_template_->blend_attachments .size ());
471
+
472
+ for (int i = 0 ; i < raster_params.blending .size (); i++) {
473
+ auto &state = graphics_pipeline_template_->blend_attachments [i];
474
+ auto &ti_param = raster_params.blending [i];
475
+ state.blendEnable = ti_param.enable ;
476
+ if (ti_param.enable ) {
477
+ state.colorBlendOp = blend_op_ti_to_vk (ti_param.color .op );
478
+ state.srcColorBlendFactor =
479
+ blend_factor_ti_to_vk (ti_param.color .src_factor );
480
+ state.dstColorBlendFactor =
481
+ blend_factor_ti_to_vk (ti_param.color .dst_factor );
482
+ state.alphaBlendOp = blend_op_ti_to_vk (ti_param.alpha .op );
483
+ state.srcAlphaBlendFactor =
484
+ blend_factor_ti_to_vk (ti_param.alpha .src_factor );
485
+ state.dstAlphaBlendFactor =
486
+ blend_factor_ti_to_vk (ti_param.alpha .dst_factor );
487
+ state.colorWriteMask =
488
+ VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
489
+ VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
490
+ }
491
+ }
492
+ }
493
+
421
494
VkPipelineDynamicStateCreateInfo &dynamic_state =
422
495
graphics_pipeline_template_->dynamic_state ;
423
496
dynamic_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
0 commit comments