Home | History | Annotate | Download | only in vk
      1 /*
      2  * Copyright 2018 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  *
      7  */
      8 
      9 //
     10 //
     11 //
     12 
     13 #include <stdio.h>
     14 
     15 //
     16 //
     17 //
     18 
     19 #include "cache_vk.h"
     20 #include "assert_vk.h"
     21 #include "host_alloc.h"
     22 
     23 //
     24 //
     25 //
     26 
     27 void
     28 vk_pipeline_cache_create(VkDevice                            device,
     29                          VkAllocationCallbacks const *       allocator,
     30                          char                  const * const name,
     31                          VkPipelineCache             *       pipeline_cache)
     32 {
     33   VkPipelineCacheCreateInfo pipeline_cache_info = {
     34     .sType           = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO,
     35     .pNext           = NULL,
     36     .flags           = 0,
     37     .initialDataSize = 0,
     38     .pInitialData    = NULL
     39   };
     40 
     41   FILE * f    = fopen(name,"rb");
     42   void * data = NULL;
     43 
     44   if (f != NULL)
     45     {
     46       if (fseek(f,0L,SEEK_END) == 0)
     47         {
     48           pipeline_cache_info.initialDataSize = ftell(f);
     49 
     50           if (pipeline_cache_info.initialDataSize > 0)
     51             {
     52               fseek(f, 0L, SEEK_SET);
     53 
     54               data = vk_host_alloc(allocator,pipeline_cache_info.initialDataSize);
     55 
     56               size_t read_size = fread(data,pipeline_cache_info.initialDataSize,1,f);
     57 
     58               pipeline_cache_info.pInitialData = data;
     59             }
     60         }
     61 
     62       fclose(f);
     63     }
     64 
     65   vk(CreatePipelineCache(device,
     66                          &pipeline_cache_info,
     67                          allocator,
     68                          pipeline_cache));
     69 
     70 
     71   if (data != NULL)
     72     vk_host_free(allocator,data);
     73 }
     74 
     75 //
     76 //
     77 //
     78 
     79 void
     80 vk_pipeline_cache_destroy(VkDevice                            device,
     81                           VkAllocationCallbacks const *       allocator,
     82                           char                  const * const name,
     83                           VkPipelineCache                     pipeline_cache)
     84 {
     85   size_t data_size;
     86 
     87   vkGetPipelineCacheData(device,pipeline_cache,&data_size,NULL);
     88 
     89   if (data_size > 0)
     90     {
     91       void * data = vk_host_alloc(allocator,data_size);
     92 
     93       vkGetPipelineCacheData(device,pipeline_cache,&data_size,data);
     94 
     95       FILE * f = fopen(name,"wb");
     96 
     97       if (f != NULL)
     98         {
     99           fwrite(data,data_size,1,f);
    100           fclose(f);
    101         }
    102 
    103       vk_host_free(allocator,data);
    104     }
    105 
    106   vkDestroyPipelineCache(device,pipeline_cache,allocator);
    107 }
    108 
    109 //
    110 //
    111 //
    112