tzh
2024-08-22 c7d0944258c7d0943aa7b2211498fd612971ce27
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
/*
 * Copyright 2018 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 */
 
//
//
//
 
#include <stdio.h>
 
//
//
//
 
#include "cache_vk.h"
#include "assert_vk.h"
#include "host_alloc.h"
 
//
//
//
 
void
vk_pipeline_cache_create(VkDevice                            device,
                         VkAllocationCallbacks const *       allocator,
                         char                  const * const name,
                         VkPipelineCache             *       pipeline_cache)
{
  VkPipelineCacheCreateInfo pipeline_cache_info = {
    .sType           = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO,
    .pNext           = NULL,
    .flags           = 0,
    .initialDataSize = 0,
    .pInitialData    = NULL
  };
 
  FILE * f    = fopen(name,"rb");
  void * data = NULL;
 
  if (f != NULL)
    {
      if (fseek(f,0L,SEEK_END) == 0)
        {
          pipeline_cache_info.initialDataSize = ftell(f);
 
          if (pipeline_cache_info.initialDataSize > 0)
            {
              fseek(f, 0L, SEEK_SET);
 
              data = vk_host_alloc(allocator,pipeline_cache_info.initialDataSize);
 
              size_t read_size = fread(data,pipeline_cache_info.initialDataSize,1,f);
 
              pipeline_cache_info.pInitialData = data;
            }
        }
 
      fclose(f);
    }
 
  vk(CreatePipelineCache(device,
                         &pipeline_cache_info,
                         allocator,
                         pipeline_cache));
 
 
  if (data != NULL)
    vk_host_free(allocator,data);
}
 
//
//
//
 
void
vk_pipeline_cache_destroy(VkDevice                            device,
                          VkAllocationCallbacks const *       allocator,
                          char                  const * const name,
                          VkPipelineCache                     pipeline_cache)
{
  size_t data_size;
 
  vkGetPipelineCacheData(device,pipeline_cache,&data_size,NULL);
 
  if (data_size > 0)
    {
      void * data = vk_host_alloc(allocator,data_size);
 
      vkGetPipelineCacheData(device,pipeline_cache,&data_size,data);
 
      FILE * f = fopen(name,"wb");
 
      if (f != NULL)
        {
          fwrite(data,data_size,1,f);
          fclose(f);
        }
 
      vk_host_free(allocator,data);
    }
 
  vkDestroyPipelineCache(device,pipeline_cache,allocator);
}
 
//
//
//