Cogs.Core
EffectsVK.cpp
1#include "EffectsVK.h"
2
3// #include <ShaderCompiler/ShaderCompiler.h> // TODO
4
5#include "GraphicsDeviceVK.h"
6#include "BuffersVK.h"
7
8namespace
9{
10 Cogs::Logging::Log logger = Cogs::Logging::getLogger("EffectsVK");
11
12 VkShaderModule prepareShader(VkDevice device, const void * data, const size_t size)
13 {
14 VkShaderModuleCreateInfo moduleCreateInfo;
15 VkShaderModule module;
16 VkResult err;
17
18 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
19 moduleCreateInfo.pNext = nullptr;
20
21 moduleCreateInfo.codeSize = size;
22 moduleCreateInfo.pCode = reinterpret_cast<const uint32_t *>(data);
23 moduleCreateInfo.flags = 0;
24
25 err = vkCreateShaderModule(device, &moduleCreateInfo, nullptr, &module);
26
27 assert(!err);
28
29 return module;
30 }
31}
32
33void Cogs::EffectsVK::initialize(GraphicsDeviceVK * graphicsDevice)
34{
35 this->graphicsDevice = graphicsDevice;
36
37 EffectsCommon::initialize(graphicsDevice->getBuffers());
38}
39
41{
42 return EffectHandle();
43}
44
46{
47 return EffectHandle();
48}
49
51{
52}
53
55{
56}
57
58bool Cogs::EffectsVK::initializeEffectLayout(EffectVK & effect)
59{
60 const VkDescriptorType slotTypes[] = {
61 VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
62 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
63 VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
64 VK_DESCRIPTOR_TYPE_SAMPLER
65 };
66
67 const VkShaderStageFlagBits shaderTypes[] = {
68 VK_SHADER_STAGE_VERTEX_BIT,
69 VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT,
70 VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT,
71 VK_SHADER_STAGE_GEOMETRY_BIT,
72 VK_SHADER_STAGE_FRAGMENT_BIT,
73 };
74
75 Shader * shaders[] = {
76 &effect.vertexShader,
77 nullptr,
78 nullptr,
79 &effect.geometryShader,
80 &effect.pixelShader
81 };
82
83 for (size_t j = 0; j < ShaderType::NumShaderTypes; ++j) {
84 std::vector<VkDescriptorSetLayoutBinding> layoutBindings;
85
86 uint32_t binding = 0;
87 for (size_t i = 0; i < SlotType::NumSlotTypes - 1; ++i) {
88 auto slots = effect.signature.slots[i].values[j];
89
90 if (slots) {
91 for (size_t k = 0; k < slots; ++k) {
92 const VkDescriptorSetLayoutBinding layoutBinding = {
93 binding++,
94 slotTypes[i],
95 1,
96 static_cast<VkShaderStageFlags>(shaderTypes[j]),
97 nullptr,
98 };
99
100 layoutBindings.push_back(layoutBinding);
101 }
102 }
103 }
104
105 if (!layoutBindings.size()) continue;
106
107 const VkDescriptorSetLayoutCreateInfo descriptorLayoutInfo = {
108 VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
109 nullptr,
110 0,
111 static_cast<uint32_t>(layoutBindings.size()),
112 layoutBindings.data(),
113 };
114
115 VkDescriptorSetLayout descriptorSetLayout;
116
117 auto result = vkCreateDescriptorSetLayout(graphicsDevice->device, &descriptorLayoutInfo, nullptr, &descriptorSetLayout);
118
119 if (result != VK_SUCCESS) {
120 LOG_ERROR(logger, "Error creating descriptor set layout.");
121 return false;
122 }
123
124 effect.descriptorSetLayouts.push_back(descriptorSetLayout);
125 }
126
127 const VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = {
128 VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
129 nullptr,
130 0,
131 static_cast<uint32_t>(effect.descriptorSetLayouts.size()),
132 effect.descriptorSetLayouts.data(),
133 0,
134 nullptr
135 };
136
137 auto result = vkCreatePipelineLayout(graphicsDevice->device, &pipelineLayoutCreateInfo, nullptr, &effect.pipelineLayout);
138
139 if (result != VK_SUCCESS) {
140 LOG_ERROR(logger, "Error creating pipeline layout.");
141 return false;
142 }
143
144 return true;
145}
146
147Cogs::EffectHandle Cogs::EffectsVK::load(
148 const ProcessedContent & vsSource,
149 const ProcessedContent & hsSource,
150 const ProcessedContent & dsSource,
151 const ProcessedContent & gsSource,
152 const ProcessedContent & psSource,
153 const StringView & vsEntryPoint,
154 const StringView & hsEntryPoint,
155 const StringView & dsEntryPoint,
156 const StringView & gsEntryPoint,
157 const StringView & psEntryPoint,
158 const EffectDescription & desc)
159{
160 // EffectByteCode byteCode;
161
162 // EffectSource source;
163 // source.vsSource = vsSource.content;
164 // source.gsSource = gsSource.content;
165 // source.psSource = psSource.content;
166
167 // source.sourceLanguage = EffectLanguage::HLSL;
168 // source.targetLanguage = GlslVariant::VersionVulkan;
169
170 // Cogs::compileEffect(source, desc.definitions, EffectFlags::HLSL,
171 // byteCode,
172 // vsEntryPoint.size() ? vsEntryPoint.data() : nullptr,
173 // gsEntryPoint.size() ? gsEntryPoint.data() : nullptr,
174 // psEntryPoint.size() ? psEntryPoint.data() : nullptr);
175
176 // EffectVK effect;
177
178 // if (byteCode.vsByteCode) {
179 // Cogs::reflectShader(byteCode.vsByteCode, effect.vertexShader.reflection);
180 // }
181
182 // if (byteCode.gsByteCode) {
183 // Cogs::reflectShader(byteCode.gsByteCode, effect.geometryShader.reflection);
184 // }
185
186 // if (byteCode.psByteCode) {
187 // Cogs::reflectShader(byteCode.psByteCode, effect.pixelShader.reflection);
188 // }
189
190 // Cogs::crossCompileEffect(byteCode, GlslVariant::VersionVulkan, source.gsSource.size() ? CompilerFlags::GeometryShader : CompilerFlags::None, source);
191
192 // const bool dumpShaderContents = (graphicsDevice->getSettings().flags & GraphicsDeviceFlags::DumpShaderContents) != 0;
193
194 // StringView sources[] = {
195 // source.vsSource,
196 // nullptr,
197 // nullptr,
198 // source.gsSource,
199 // source.psSource,
200 // };
201
202 // StringView profiles[] = {
203 // "vs_vk",
204 // "hs_vk",
205 // "ds_vk",
206 // "gs_vk",
207 // "ps_vk",
208 // };
209
210 // if (dumpShaderContents && desc.name.size()) {
211 // for (int i = 0; i < ShaderType::NumShaderTypes; i++) {
212 // if (sources[i].empty()) {
213 // continue;
214 // }
215
216 // auto dumpName = desc.name.to_string() + "." + profiles[i].to_string() + ".glsl";
217 // handler->writeBinaryFile(IIOHandler::FileType::ShaderDump, dumpName, sources[i].data(), sources[i].size());
218 // }
219
220 // std::string meta;
221 // meta.reserve(4096);
222
223 // meta.append("{\n");
224
225 // meta.append(" \"name\": ");
226
227 // meta.append("\"");
228 // meta.append(desc.name.empty() ? "N/A" : desc.name.data());
229 // meta.append("\"\n");
230
231 // if (!desc.definitions.empty()) {
232 // meta.append(" \"definitions\": {\n");
233
234 // for (auto & d : desc.definitions) {
235 // meta.append(" \"");
236 // meta.append(d.first);
237 // meta.append("\": ");
238
239 // meta.append("\"");
240 // meta.append(d.second);
241 // meta.append("\",\n");
242 // }
243
244 // meta.append(" },\n");
245 // }
246
247 // size_t effectId = 0;
248 // for (int i = 0; i < ShaderType::NumShaderTypes; i++) {
249 // if (sources[i].empty()) {
250 // continue;
251 // }
252
253 // meta.append(" \"");
254 // meta.append(profiles[i].to_string());
255 // meta.append("\": ");
256
257 // auto dumpName = desc.name.to_string() + "." + profiles[i].to_string() + ".glsl";
258 // meta.append("\"");
259 // meta.append(dumpName);
260 // meta.append("\"\n");
261 // }
262
263 // meta.append("}\n");
264
265 // auto metaName = desc.name.empty() ? std::to_string(effectId) + ".json" : desc.name.to_string() + ".vk.json";
266 // handler->writeBinaryFile(IIOHandler::FileType::ShaderDump, metaName, meta.data(), meta.size());
267 // }
268
269 // effect.vertexShader.shaderModule = prepareShader(graphicsDevice->device, source.vsSource.data(), source.vsSource.size());
270
271 // if (source.gsSource.size()) {
272 // effect.geometryShader.shaderModule = prepareShader(graphicsDevice->device, source.gsSource.data(), source.gsSource.size());
273 // }
274
275 // if (source.psSource.size()) {
276 // effect.pixelShader.shaderModule = prepareShader(graphicsDevice->device, source.psSource.data(), source.psSource.size());
277 // }
278
279 // effect.buildSignature();
280
281 // initializeEffectLayout(effect);
282
283 // return effects.addResource(effect);
285}
EffectHandle loadComputeEffect(const StringView &fileName, EffectFlags::EEffectFlags effectFlags=EffectFlags::None) override
Load the compute shader with the given file name and create an effect.
Definition: EffectsVK.cpp:40
void releaseEffect(EffectHandle effectHandle) override
Release the effect with the given handle, freeing all resources generated during program loading.
Definition: EffectsVK.cpp:50
void releaseResources() override
Release all allocated effect resources.
Definition: EffectsVK.cpp:54
Log implementation class.
Definition: LogManager.h:139
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:180
std::vector< PreprocessorDefinition > PreprocessorDefinitions
A set of preprocessor definitions.
Definition: IEffects.h:13
EEffectFlags
Effect source flags.
Definition: IEffects.h:20
static const Handle_t NoHandle
Represents a handle to nothing.
Definition: Common.h:77