~sircmpwn/xrgears

ref: f162555056b018a3805f96df49158518ec63f94c xrgears/vitamin-k/render/vikShader.hpp -rw-r--r-- 1.9 KiB
f1625550 — Lubosz Sarnecki vik: Use struct and array initializers. 2 years ago
                                                                                
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
 /*
 * vitamin-k
 *
 * Copyright 2016 Sascha Willems - www.saschawillems.de
 * Copyright 2017-2018 Collabora Ltd.
 *
 * Authors: Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
 * SPDX-License-Identifier: MIT
 */

#pragma once

#include <vulkan/vulkan.h>

#include <string>

#include "vikTools.hpp"

#include "../system/vikAssets.hpp"

namespace vik {
class Shader {
 public:
  static VkPipelineShaderStageCreateInfo load(const VkDevice& device, std::string fileName, VkShaderStageFlagBits stage) {
    std::string path = Assets::get_shader_path() + fileName;
    VkShaderModule module = load(path.c_str(), device);
    assert(module != VK_NULL_HANDLE);

    VkPipelineShaderStageCreateInfo shaderStage = {
      .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
      .stage = stage,
      .module = module,
      .pName = "main"
    };

    return shaderStage;
  }

  static VkShaderModule load(const char *fileName, VkDevice device) {
    std::ifstream is(fileName, std::ios::binary | std::ios::in | std::ios::ate);

    if (is.is_open()) {
      size_t size = is.tellg();
      is.seekg(0, std::ios::beg);
      char* shaderCode = new char[size];
      is.read(shaderCode, size);
      is.close();

      assert(size > 0);

      VkShaderModule shaderModule;
      VkShaderModuleCreateInfo moduleCreateInfo = {
        .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
        .codeSize = size,
        .pCode = (uint32_t*)shaderCode
      };

      vik_log_check(vkCreateShaderModule(device, &moduleCreateInfo,
                                         NULL, &shaderModule));

      // vik_log_d("Creating shader module %p (%s)", shaderModule, fileName);

      delete[] shaderCode;

      return shaderModule;
    } else {
      std::cerr << "Error: Could not open shader file \"" << fileName << "\"" << std::endl;
      return VK_NULL_HANDLE;
    }
  }
};
}  // namespace vik