vsg: include/vsg/state/ShaderModule.h Source File

vsg 1.1.3 VulkanSceneGraph library

Loading...
Searching...
No Matches
ShaderModule.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2018 Robert Osfield
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
13</editor-fold> */
14
15#include <fstream>
16
17#include <vsg/vk/Device.h>
18#include <vsg/vk/vk_buffer.h>
19
20namespace vsg
21{
22 // forward declare
23 class Context;
24
27 class VSG_DECLSPEC ShaderCompileSettings : public Inherit<Object, ShaderCompileSettings>
28 {
29 public:
30 enum Language
31 {
32 GLSL,
33 HLSL
34 };
35
36 enum SpirvTarget
37 {
38 SPIRV_1_0 = (1 << 16),
39 SPIRV_1_1 = (1 << 16) | (1 << 8),
40 SPIRV_1_2 = (1 << 16) | (2 << 8),
41 SPIRV_1_3 = (1 << 16) | (3 << 8),
42 SPIRV_1_4 = (1 << 16) | (4 << 8),
43 SPIRV_1_5 = (1 << 16) | (5 << 8)
44 };
45
46 uint32_t vulkanVersion = VK_API_VERSION_1_0;
47 int clientInputVersion = 100;
48 Language language = GLSL;
49 int defaultVersion = 450;
50 SpirvTarget target = SPIRV_1_0;
51 bool forwardCompatible = false;
52 bool generateDebugInfo = false; // maps to SpvOptions::generateDebugInfo
53
54 std::set<std::string> defines;
55
56 public:
57 int compare(const Object& rhs_object) const override;
58
59 void read(Input& input) override;
60 void write(Output& output) const override;
61 };
62 VSG_type_name(vsg::ShaderCompileSettings);
63
66 class VSG_DECLSPEC ShaderModule : public Inherit<Object, ShaderModule>
67 {
68 public:
69 using SPIRV = std::vector<uint32_t>;
70
71 ShaderModule();
72 explicit ShaderModule(const std::string& in_source, ref_ptr<ShaderCompileSettings> in_hints = {});
73 explicit ShaderModule(const SPIRV& in_code);
74 ShaderModule(const std::string& source, const SPIRV& in_code);
75
76 std::string source;
77 ref_ptr<ShaderCompileSettings> hints;
78
80 SPIRV code;
81
83 VkShaderModule vk(uint32_t deviceID) const { return _implementation[deviceID]->_shaderModule; }
84
85 // compile the Vulkan object, context parameter used for Device
86 void compile(Context& context);
87
88 // remove the local reference to the Vulkan implementation
89 void release(uint32_t deviceID) { _implementation[deviceID] = {}; }
90 void release() { _implementation.clear(); }
91
92 public:
93 int compare(const Object& rhs_object) const override;
94
95 void read(Input& input) override;
96 void write(Output& output) const override;
97
98 protected:
99 virtual ~ShaderModule();
100
101 struct Implementation : public Inherit<Object, Implementation>
102 {
103 Implementation(Device* device, ShaderModule* shader);
104
105 virtual ~Implementation();
106
107 VkShaderModule _shaderModule;
108 ref_ptr<Device> _device;
109 };
110
111 vk_buffer<ref_ptr<Implementation>> _implementation;
112 };
113 VSG_type_name(vsg::ShaderModule);
114
116 extern VSG_DECLSPEC std::string insertIncludes(const std::string& source, ref_ptr<const Options> options);
117
118} // namespace vsg
vsg::Context
Definition Context.h:67
vsg::Device
Device encapsulates VkDevice, a logical handle to the PhysicalDevice with capabilities specified duri...
Definition Device.h:37
vsg::Inherit
Definition Inherit.h:28
vsg::Input
Definition Input.h:44
vsg::Object
Definition Object.h:60
vsg::Output
Definition Output.h:41
vsg::ShaderCompileSettings
Definition ShaderModule.h:28
vsg::ShaderCompileSettings::compare
int compare(const Object &rhs_object) const override
compare two objects, return -1 if this object is less than rhs, return 0 if it's equal,...
vsg::ShaderModule
Definition ShaderModule.h:67
vsg::ShaderModule::vk
VkShaderModule vk(uint32_t deviceID) const
Vulkan VkShaderModule handle.
Definition ShaderModule.h:83
vsg::ShaderModule::code
SPIRV code
VkShaderModuleCreateInfo settings.
Definition ShaderModule.h:80
vsg::ShaderModule::compare
int compare(const Object &rhs_object) const override
compare two objects, return -1 if this object is less than rhs, return 0 if it's equal,...
vsg::ref_ptr
Definition ref_ptr.h:22
vsg::ShaderModule::Implementation
Definition ShaderModule.h:102
vsg::vk_buffer
vk_buffer that manages a single logical device supported.
Definition vk_buffer.h:28