Welcome to mirror list, hosted at ThFree Co, Russian Federation.

vulkan_pipeline.cpp « vulkan « drape - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 532901bb4b2aa35b9f02e99297575f20b5a0e6a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#include "drape/vulkan/vulkan_pipeline.hpp"
#include "drape/vulkan/vulkan_utils.hpp"

#include "platform/platform.hpp"

#include "coding/file_name_utils.hpp"
#include "coding/file_reader.hpp"
#include "coding/file_writer.hpp"
#include "coding/write_to_sink.hpp"

#include "base/assert.hpp"

#include <string>
#include <utility>
#include <vector>

namespace dp
{
namespace vulkan
{
namespace
{
std::string const kDumpFileName = "vulkan_dump.bin";

// Stencil package.
uint8_t constexpr kStencilBackFunctionByte = 7;
uint8_t constexpr kStencilBackFailActionByte = 6;
uint8_t constexpr kStencilBackDepthFailActionByte = 5;
uint8_t constexpr kStencilBackPassActionByte = 4;
uint8_t constexpr kStencilFrontFunctionByte = 3;
uint8_t constexpr kStencilFrontFailActionByte = 2;
uint8_t constexpr kStencilFrontDepthFailActionByte = 1;
uint8_t constexpr kStencilFrontPassActionByte = 0;

VkCompareOp DecodeTestFunction(uint8_t testFunctionByte)
{
  switch (static_cast<TestFunction>(testFunctionByte))
  {
  case TestFunction::Never: return VK_COMPARE_OP_NEVER;
  case TestFunction::Less: return VK_COMPARE_OP_LESS;
  case TestFunction::Equal: return VK_COMPARE_OP_EQUAL;
  case TestFunction::LessOrEqual: return VK_COMPARE_OP_LESS_OR_EQUAL;
  case TestFunction::Greater: return VK_COMPARE_OP_GREATER;
  case TestFunction::NotEqual: return VK_COMPARE_OP_NOT_EQUAL;
  case TestFunction::GreaterOrEqual: return VK_COMPARE_OP_GREATER_OR_EQUAL;
  case TestFunction::Always: return VK_COMPARE_OP_ALWAYS;
  }
  ASSERT(false, ());
}

VkStencilOp DecodeStencilAction(uint8_t stencilActionByte)
{
  switch (static_cast<StencilAction>(stencilActionByte))
  {
  case StencilAction::Keep: return VK_STENCIL_OP_KEEP;
  case StencilAction::Zero: return VK_STENCIL_OP_ZERO;
  case StencilAction::Replace: return VK_STENCIL_OP_REPLACE;
  case StencilAction::Increment: return VK_STENCIL_OP_INCREMENT_AND_CLAMP;
  case StencilAction::IncrementWrap: return VK_STENCIL_OP_INCREMENT_AND_WRAP;
  case StencilAction::Decrement: return VK_STENCIL_OP_DECREMENT_AND_CLAMP;
  case StencilAction::DecrementWrap: return VK_STENCIL_OP_DECREMENT_AND_WRAP;
  case StencilAction::Invert: return VK_STENCIL_OP_INVERT;
  }
  ASSERT(false, ());
}

VkFormat GetAttributeFormat(uint8_t componentCount, glConst componentType)
{
  if (componentType == gl_const::GLFloatType)
  {
    switch (componentCount)
    {
    case 1: return VK_FORMAT_R32_SFLOAT;
    case 2: return VK_FORMAT_R32G32_SFLOAT;
    case 3: return VK_FORMAT_R32G32B32_SFLOAT;
    case 4: return VK_FORMAT_R32G32B32A32_SFLOAT;
    }
  }
  else if (componentType == gl_const::GLByteType)
  {
    switch (componentCount)
    {
    case 1: return VK_FORMAT_R8_SINT;
    case 2: return VK_FORMAT_R8G8_SINT;
    case 3: return VK_FORMAT_R8G8B8_SINT;
    case 4: return VK_FORMAT_R8G8B8A8_SINT;
    }
  }
  else if (componentType == gl_const::GLUnsignedByteType)
  {
    switch (componentCount)
    {
    case 1: return VK_FORMAT_R8_UINT;
    case 2: return VK_FORMAT_R8G8_UINT;
    case 3: return VK_FORMAT_R8G8B8_UINT;
    case 4: return VK_FORMAT_R8G8B8A8_UINT;
    }
  }
  else if (componentType == gl_const::GLShortType)
  {
    switch (componentCount)
    {
    case 1: return VK_FORMAT_R16_SINT;
    case 2: return VK_FORMAT_R16G16_SINT;
    case 3: return VK_FORMAT_R16G16B16_SINT;
    case 4: return VK_FORMAT_R16G16B16A16_SINT;
    }
  }
  else if (componentType == gl_const::GLUnsignedShortType)
  {
    switch (componentCount)
    {
    case 1: return VK_FORMAT_R16_UINT;
    case 2: return VK_FORMAT_R16G16_UINT;
    case 3: return VK_FORMAT_R16G16B16_UINT;
    case 4: return VK_FORMAT_R16G16B16A16_UINT;
    }
  }
  else if (componentType == gl_const::GLIntType)
  {
    switch (componentCount)
    {
    case 1: return VK_FORMAT_R32_SINT;
    case 2: return VK_FORMAT_R32G32_SINT;
    case 3: return VK_FORMAT_R32G32B32_SINT;
    case 4: return VK_FORMAT_R32G32B32A32_SINT;
    }
  }
  else if (componentType == gl_const::GLUnsignedIntType)
  {
    switch (componentCount)
    {
    case 1: return VK_FORMAT_R32_UINT;
    case 2: return VK_FORMAT_R32G32_UINT;
    case 3: return VK_FORMAT_R32G32B32_UINT;
    case 4: return VK_FORMAT_R32G32B32A32_UINT;
    }
  }

  CHECK(false, ("Unsupported attribute format.", componentCount, componentType));
  return VK_FORMAT_UNDEFINED;
}

std::string GetDumpFilePath()
{
  return base::JoinPath(GetPlatform().TmpDir(), kDumpFileName);
}
}  // namespace

VulkanPipeline::VulkanPipeline(VkDevice device, int appVersionCode)
  : m_appVersionCode(appVersionCode)
{
  // Read dump.
  std::vector<uint8_t> dumpData;
  auto const dumpFilePath = GetDumpFilePath();
  if (GetPlatform().IsFileExistsByFullPath(dumpFilePath))
  {
    try
    {
      FileReader r(dumpFilePath);
      NonOwningReaderSource src(r);

      auto const v = ReadPrimitiveFromSource<int>(src);
      if (v != appVersionCode)
      {
        // Dump is obsolete.
        FileWriter::DeleteFileX(dumpFilePath);
      }
      else
      {
        dumpData.resize(static_cast<size_t>(r.Size() - sizeof(int)));
        src.Read(dumpData.data(), dumpData.size());
      }
    }
    catch (FileReader::Exception const & exception)
    {
      LOG(LWARNING, ("Exception while reading file:", dumpFilePath,
        "reason:", exception.what()));
    }
  }

  VkPipelineCacheCreateInfo pipelineCacheCreateInfo = {};
  pipelineCacheCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
  pipelineCacheCreateInfo.initialDataSize = dumpData.size();
  pipelineCacheCreateInfo.pInitialData = dumpData.data();
  CHECK_VK_CALL(vkCreatePipelineCache(device, &pipelineCacheCreateInfo, nullptr, &m_vulkanPipelineCache));
}

void VulkanPipeline::Dump(VkDevice device)
{
  if (!m_isChanged)
    return;

  size_t constexpr kMaxCacheSizeInBytes = 500 * 1024;

  size_t cacheSize;
  VkResult statusCode;
  statusCode = vkGetPipelineCacheData(device, m_vulkanPipelineCache, &cacheSize, nullptr);
  if (statusCode == VK_SUCCESS && cacheSize > 0)
  {
    if (cacheSize <= kMaxCacheSizeInBytes)
    {
      std::vector<uint8_t> dumpData(cacheSize);
      statusCode = vkGetPipelineCacheData(device, m_vulkanPipelineCache, &cacheSize, dumpData.data());
      if (statusCode == VK_SUCCESS)
      {
        auto const dumpFilePath = GetDumpFilePath();
        try
        {
          FileWriter w(dumpFilePath);
          WriteToSink(w, m_appVersionCode);
          w.Write(dumpData.data(), dumpData.size());
        }
        catch (FileWriter::Exception const & exception)
        {
          LOG(LWARNING, ("Exception while writing file:", dumpFilePath,
            "reason:", exception.what()));
        }
        m_isChanged = false;
      }
    }
    else
    {
      LOG(LWARNING, ("Maximum pipeline cache size exceeded (", cacheSize, "/", kMaxCacheSizeInBytes,
                     "bytes)"));
    }
  }
}

void VulkanPipeline::ResetCache(VkDevice device)
{
  for (auto const & p : m_pipelineCache)
    vkDestroyPipeline(device, p.second, nullptr);
  m_pipelineCache.clear();
  m_isChanged = true;
}

void VulkanPipeline::Destroy(VkDevice device)
{
  Dump(device);

  for (auto const & p : m_pipelineCache)
    vkDestroyPipeline(device, p.second, nullptr);
  m_pipelineCache.clear();

  vkDestroyPipelineCache(device, m_vulkanPipelineCache, nullptr);
}

VkPipeline VulkanPipeline::GetPipeline(VkDevice device, PipelineKey const & key)
{
  CHECK(key.m_renderPass != VK_NULL_HANDLE, ());

  auto const it = m_pipelineCache.find(key);
  if (it != m_pipelineCache.end())
    return it->second;

  // Primitives.
  VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCreateInfo = {};
  inputAssemblyStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
  inputAssemblyStateCreateInfo.topology = key.m_primitiveTopology;

  // Rasterization.
  VkPipelineRasterizationStateCreateInfo rasterizationStateCreateInfo = {};
  rasterizationStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
  rasterizationStateCreateInfo.polygonMode = VK_POLYGON_MODE_FILL;
  rasterizationStateCreateInfo.cullMode = VK_CULL_MODE_BACK_BIT;
  rasterizationStateCreateInfo.frontFace = VK_FRONT_FACE_CLOCKWISE;
  rasterizationStateCreateInfo.lineWidth = 1.0f;

  // Blending.
  VkPipelineColorBlendAttachmentState pipelineColorBlendAttachmentState = {};
  pipelineColorBlendAttachmentState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
    VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
  pipelineColorBlendAttachmentState.blendEnable = key.m_blendingEnabled ? VK_TRUE : VK_FALSE;
  if (key.m_blendingEnabled)
  {
    pipelineColorBlendAttachmentState.colorBlendOp = VK_BLEND_OP_ADD;
    pipelineColorBlendAttachmentState.alphaBlendOp = VK_BLEND_OP_ADD;
    pipelineColorBlendAttachmentState.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
    pipelineColorBlendAttachmentState.srcAlphaBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
    pipelineColorBlendAttachmentState.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
    pipelineColorBlendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
  }
  VkPipelineColorBlendStateCreateInfo colorBlendStateCreateInfo = {};
  colorBlendStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
  colorBlendStateCreateInfo.attachmentCount = 1;
  colorBlendStateCreateInfo.pAttachments = &pipelineColorBlendAttachmentState;

  // Viewport.
  VkPipelineViewportStateCreateInfo viewportStateCreateInfo = {};
  viewportStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
  viewportStateCreateInfo.viewportCount = 1;
  viewportStateCreateInfo.scissorCount = 1;

  // Multisampling.
  VkPipelineMultisampleStateCreateInfo multisampleStateCreateInfo = {};
  multisampleStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
  multisampleStateCreateInfo.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;

  // Dynamic.
  static std::vector<VkDynamicState> dynamicState = {
    VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR, VK_DYNAMIC_STATE_LINE_WIDTH,
    VK_DYNAMIC_STATE_STENCIL_REFERENCE};
  VkPipelineDynamicStateCreateInfo dynamicStateCreateInfo = {};
  dynamicStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
  dynamicStateCreateInfo.pDynamicStates = dynamicState.data();
  dynamicStateCreateInfo.dynamicStateCount = static_cast<uint32_t>(dynamicState.size());

  // Input state.
  std::vector<VkVertexInputBindingDescription> bindingDescriptions(key.m_bindingInfoCount);
  size_t attribsCount = 0;
  for (size_t i = 0; i < key.m_bindingInfoCount; ++i)
  {
    bindingDescriptions[i].binding = static_cast<uint32_t>(i);
    bindingDescriptions[i].stride = key.m_bindingInfo[i].GetElementSize();
    bindingDescriptions[i].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
    attribsCount += key.m_bindingInfo[i].GetCount();
  }

  std::vector<VkVertexInputAttributeDescription> attributeDescriptions(attribsCount);
  uint32_t bindingCounter = 0;
  for (size_t i = 0; i < key.m_bindingInfoCount; ++i)
  {
    for (uint8_t j = 0; j < key.m_bindingInfo[i].GetCount(); ++j)
    {
      BindingDecl const & bindingDecl = key.m_bindingInfo[i].GetBindingDecl(j);
      attributeDescriptions[bindingCounter].location = bindingCounter;
      attributeDescriptions[bindingCounter].binding = static_cast<uint32_t>(i);
      attributeDescriptions[bindingCounter].format = GetAttributeFormat(bindingDecl.m_componentCount,
                                                                        bindingDecl.m_componentType);
      attributeDescriptions[bindingCounter].offset = bindingDecl.m_offset;

      bindingCounter++;
    }
  }

  VkPipelineVertexInputStateCreateInfo inputStateCreateInfo = {};
  inputStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
  inputStateCreateInfo.vertexBindingDescriptionCount = static_cast<uint32_t>(bindingDescriptions.size());
  inputStateCreateInfo.pVertexBindingDescriptions = bindingDescriptions.data();
  inputStateCreateInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(attributeDescriptions.size());
  inputStateCreateInfo.pVertexAttributeDescriptions = attributeDescriptions.data();

  // Depth stencil.
  VkPipelineDepthStencilStateCreateInfo depthStencilState = {};
  depthStencilState.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
  depthStencilState.depthTestEnable = key.m_depthStencil.m_depthEnabled ? VK_TRUE : VK_FALSE;
  if (key.m_depthStencil.m_depthEnabled)
  {
    depthStencilState.depthWriteEnable = VK_TRUE;
    depthStencilState.depthCompareOp =
      DecodeTestFunction(static_cast<uint8_t>(key.m_depthStencil.m_depthFunction));
  }
  else
  {
    depthStencilState.depthWriteEnable = VK_FALSE;
    depthStencilState.depthCompareOp = VK_COMPARE_OP_ALWAYS;
  }

  if (key.m_depthStencil.m_stencilEnabled)
  {
    depthStencilState.front.compareOp =
      DecodeTestFunction(GetStateByte(key.m_depthStencil.m_stencil, kStencilFrontFunctionByte));
    depthStencilState.front.failOp =
      DecodeStencilAction(GetStateByte(key.m_depthStencil.m_stencil, kStencilFrontFailActionByte));
    depthStencilState.front.depthFailOp = DecodeStencilAction(
      GetStateByte(key.m_depthStencil.m_stencil, kStencilFrontDepthFailActionByte));
    depthStencilState.front.passOp =
      DecodeStencilAction(GetStateByte(key.m_depthStencil.m_stencil, kStencilFrontPassActionByte));
    depthStencilState.front.writeMask = 0xffffffff;
    depthStencilState.front.compareMask = 0xffffffff;
    depthStencilState.front.reference = 1;

    depthStencilState.back.compareOp =
      DecodeTestFunction(GetStateByte(key.m_depthStencil.m_stencil, kStencilBackFunctionByte));
    depthStencilState.back.failOp =
      DecodeStencilAction(GetStateByte(key.m_depthStencil.m_stencil, kStencilBackFailActionByte));
    depthStencilState.back.depthFailOp = DecodeStencilAction(
      GetStateByte(key.m_depthStencil.m_stencil, kStencilBackDepthFailActionByte));
    depthStencilState.back.passOp =
      DecodeStencilAction(GetStateByte(key.m_depthStencil.m_stencil, kStencilBackPassActionByte));
    depthStencilState.back.writeMask = 0xffffffff;
    depthStencilState.back.compareMask = 0xffffffff;
    depthStencilState.back.reference = 1;
  }
  else
  {
    depthStencilState.front.compareOp = VK_COMPARE_OP_ALWAYS;
    depthStencilState.back.compareOp = VK_COMPARE_OP_ALWAYS;
  }

  // Pipeline.
  VkGraphicsPipelineCreateInfo pipelineCreateInfo = {};
  pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
  pipelineCreateInfo.layout = key.m_program->GetPipelineLayout();
  CHECK(pipelineCreateInfo.layout != VK_NULL_HANDLE, ());
  pipelineCreateInfo.renderPass = key.m_renderPass;
  pipelineCreateInfo.basePipelineIndex = -1;
  pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
  pipelineCreateInfo.pVertexInputState = &inputStateCreateInfo;
  pipelineCreateInfo.pInputAssemblyState = &inputAssemblyStateCreateInfo;
  pipelineCreateInfo.pRasterizationState = &rasterizationStateCreateInfo;
  pipelineCreateInfo.pColorBlendState = &colorBlendStateCreateInfo;
  pipelineCreateInfo.pMultisampleState = &multisampleStateCreateInfo;
  pipelineCreateInfo.pViewportState = &viewportStateCreateInfo;
  pipelineCreateInfo.pDepthStencilState = &depthStencilState;
  pipelineCreateInfo.pDynamicState = &dynamicStateCreateInfo;
  auto shaders = key.m_program->GetShaders();
  pipelineCreateInfo.stageCount = static_cast<uint32_t>(shaders.size());
  pipelineCreateInfo.pStages = shaders.data();

  VkPipeline pipeline;
  CHECK_VK_CALL(vkCreateGraphicsPipelines(device, m_vulkanPipelineCache, 1, &pipelineCreateInfo,
                                          nullptr, &pipeline));

  m_pipelineCache.insert(std::make_pair(key, pipeline));
  m_isChanged = true;

  return pipeline;
}

void VulkanPipeline::DepthStencilKey::SetDepthTestEnabled(bool enabled)
{
  m_depthEnabled = enabled;
}
  
void VulkanPipeline::DepthStencilKey::SetDepthTestFunction(TestFunction depthFunction)
{
  m_depthFunction = depthFunction;
}
  
void VulkanPipeline::DepthStencilKey::SetStencilTestEnabled(bool enabled)
{
  m_stencilEnabled = enabled;
}
  
void VulkanPipeline::DepthStencilKey::SetStencilFunction(StencilFace face, TestFunction stencilFunction)
{
  switch (face)
  {
  case StencilFace::Front:
    SetStateByte(m_stencil, static_cast<uint8_t>(stencilFunction), kStencilFrontFunctionByte);
    break;
  case StencilFace::Back:
    SetStateByte(m_stencil, static_cast<uint8_t>(stencilFunction), kStencilBackFunctionByte);
    break;
  case StencilFace::FrontAndBack:
    SetStateByte(m_stencil, static_cast<uint8_t>(stencilFunction), kStencilFrontFunctionByte);
    SetStateByte(m_stencil, static_cast<uint8_t>(stencilFunction), kStencilBackFunctionByte);
    break;
  }
}
  
void VulkanPipeline::DepthStencilKey::SetStencilActions(StencilFace face, StencilAction stencilFailAction,
                                                        StencilAction depthFailAction, StencilAction passAction)
{
  switch (face)
  {
  case StencilFace::Front:
    SetStateByte(m_stencil, static_cast<uint8_t>(stencilFailAction), kStencilFrontFailActionByte);
    SetStateByte(m_stencil, static_cast<uint8_t>(depthFailAction), kStencilFrontDepthFailActionByte);
    SetStateByte(m_stencil, static_cast<uint8_t>(passAction), kStencilFrontPassActionByte);
    break;
  case StencilFace::Back:
    SetStateByte(m_stencil, static_cast<uint8_t>(stencilFailAction), kStencilBackFailActionByte);
    SetStateByte(m_stencil, static_cast<uint8_t>(depthFailAction), kStencilBackDepthFailActionByte);
    SetStateByte(m_stencil, static_cast<uint8_t>(passAction), kStencilBackPassActionByte);
    break;
  case StencilFace::FrontAndBack:
    SetStateByte(m_stencil, static_cast<uint8_t>(stencilFailAction), kStencilFrontFailActionByte);
    SetStateByte(m_stencil, static_cast<uint8_t>(depthFailAction), kStencilFrontDepthFailActionByte);
    SetStateByte(m_stencil, static_cast<uint8_t>(passAction), kStencilFrontPassActionByte);
    SetStateByte(m_stencil, static_cast<uint8_t>(stencilFailAction), kStencilBackFailActionByte);
    SetStateByte(m_stencil, static_cast<uint8_t>(depthFailAction), kStencilBackDepthFailActionByte);
    SetStateByte(m_stencil, static_cast<uint8_t>(passAction), kStencilBackPassActionByte);
    break;
  }
}

bool VulkanPipeline::DepthStencilKey::operator<(DepthStencilKey const & rhs) const
{
  if (m_depthEnabled != rhs.m_depthEnabled)
    return m_depthEnabled < rhs.m_depthEnabled;
  
  if (m_stencilEnabled != rhs.m_stencilEnabled)
    return m_stencilEnabled < rhs.m_stencilEnabled;
  
  if (m_depthFunction != rhs.m_depthFunction)
    return m_depthFunction < rhs.m_depthFunction;
  
  return m_stencil < rhs.m_stencil;
}

bool VulkanPipeline::DepthStencilKey::operator!=(DepthStencilKey const & rhs) const
{
  return m_depthEnabled != rhs.m_depthEnabled || m_stencilEnabled != rhs.m_stencilEnabled ||
         m_depthFunction != rhs.m_depthFunction || m_stencil != rhs.m_stencil;
}

bool VulkanPipeline::PipelineKey::operator<(PipelineKey const & rhs) const
{
  if (m_renderPass != rhs.m_renderPass)
    return m_renderPass < rhs.m_renderPass;

  if (m_program != rhs.m_program)
    return m_program < rhs.m_program;
  
  if (m_depthStencil != rhs.m_depthStencil)
    return m_depthStencil < rhs.m_depthStencil;

  if (m_bindingInfoCount != rhs.m_bindingInfoCount)
    return m_bindingInfoCount < rhs.m_bindingInfoCount;

  for (uint8_t i = 0; i < m_bindingInfoCount; ++i)
  {
    if (m_bindingInfo[i] != rhs.m_bindingInfo[i])
      return m_bindingInfo[i] < rhs.m_bindingInfo[i];
  }

  if (m_primitiveTopology != rhs.m_primitiveTopology)
    return m_primitiveTopology < rhs.m_primitiveTopology;

  return m_blendingEnabled < rhs.m_blendingEnabled;
}
}  // namespace vulkan
}  // namespace dp