Kale
Loading...
Searching...
No Matches
Shader.cpp
Go to the documentation of this file.
1/*
2 Copyright 2022 Rishi Challa
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#ifdef KALE_VULKAN
18
19#include "Shader.hpp"
20
23
24#include <fstream>
25#include <stdexcept>
26
27using namespace Kale;
28using namespace Kale::Vulkan;
29
34 // Empty Body
35}
36
44Shader::Shader(const std::string& filename, ShaderType type, Device& device) : ChildResource(device), type(type) {
45 std::vector<char> code = readFile(filename);
47}
48
56void Shader::init(const std::string& filename, ShaderType type, Device& device) {
57 ChildResource::init(device);
58 this->type = type;
59 std::vector<char> code = readFile(filename);
61}
62
69std::vector<char> Shader::readFile(const std::string& filename) const {
70 std::ifstream file(mainApp->getAssetFolderPath() + "shaders/" + filename, std::ios::ate | std::ios::binary);
71 if (!file.is_open()) throw std::runtime_error("Unable to open the file "+filename);
72 size_t fileSize = static_cast<size_t>(file.tellg());
73 std::vector<char> code(fileSize);
74 file.seekg(0);
75 file.read(code.data(), fileSize);
76 file.close();
77 return code;
78}
79
84void Shader::createShaderModule(const std::vector<char>& code) {
85 vk::ShaderModuleCreateInfo createInfo(vk::ShaderModuleCreateFlags(), code.size(),
86 reinterpret_cast<const uint32_t*>(code.data()));
87 shader = parentPtr->logicalDevice->createShaderModuleUnique(createInfo);
88}
89
93void Shader::freeResources(bool remove) {
95 shader.reset();
96}
97
102vk::ShaderStageFlagBits Shader::getShaderStage() const {
103 switch (type) {
105 return vk::ShaderStageFlagBits::eVertex;
107 return vk::ShaderStageFlagBits::eFragment;
108 default:
109 throw std::runtime_error("Invalid Shader Type");
110 }
111}
112
117vk::PipelineShaderStageCreateInfo Shader::getShaderPipelineInfo() const {
118 vk::PipelineShaderStageCreateInfo createInfo;
119 createInfo.stage = getShaderStage();
120 createInfo.module = shader.get();
121 createInfo.pName = "main"; // All kale shaders are REQUIRED to have a main function
122
123 return createInfo;
124}
125
126#endif
std::string getAssetFolderPath() const
virtual void freeResources(bool remove=true)
virtual void init(Parent &parent)
vk::UniqueDevice logicalDevice
Definition Device.hpp:59
ShaderType type
Definition Shader.hpp:65
vk::UniqueShaderModule shader
Definition Shader.hpp:70
std::vector< char > readFile(const std::string &filename) const
Definition Shader.cpp:69
vk::PipelineShaderStageCreateInfo getShaderPipelineInfo() const
Definition Shader.cpp:117
vk::ShaderStageFlagBits getShaderStage() const
Definition Shader.cpp:102
void freeResources(bool remove=true) override
Definition Shader.cpp:93
void createShaderModule(const std::vector< char > &code)
Definition Shader.cpp:84
void init(const std::string &filename, ShaderType type, Device &device)
Definition Shader.cpp:56
Application * mainApp