载入中...
搜索中...
未找到
VulkanUtil.cpp
浏览该文件的文档.
2
3#include "common/Exception.h"
4#include "common/Module.h"
9
10#include <cstdio>
11#include <memory>
12#include <stdexcept>
13#include <string>
14#include <vector>
15
16namespace eve::gpgpu {
17namespace {
18
19// Compiler failures must not call Exception's global render-tracer callback:
20// this path also executes on CPU workers without a Graphics lifetime.
21std::runtime_error compileError(const char *message) {
22 return std::runtime_error(message);
23}
24
25template <class... Args>
26std::runtime_error compileError(const char *format, Args... args) {
27 const int count = std::snprintf(nullptr, 0, format, args...);
28 if (count < 0) return std::runtime_error("Compute compilation failed");
29 std::string text(static_cast<size_t>(count) + 1, '\0');
30 std::snprintf(text.data(), text.size(), format, args...);
31 text.pop_back();
32 return std::runtime_error(text);
33}
34
35std::vector<uint32_t> loadSpirvBytes(const void *data, size_t size) {
36 if (!data || size < 4 || (size % 4) != 0) throw compileError("Gpgpu SPIR-V: invalid size %zu", size);
37 const auto *words = static_cast<const uint32_t *>(data);
38 if (words[0] != 0x07230203) throw compileError("Gpgpu SPIR-V: bad magic (expected 0x07230203)");
39 return std::vector<uint32_t>(words, words + size / 4);
40}
41
42} // namespace
43
45 auto *gfx = eve::ModuleManager::getInstance<eve::graphics::Graphics>("Graphics");
46 if (!gfx) gfx = eve::graphics::Graphics::create();
47 auto *vkg = dynamic_cast<graphics::vulkan::Graphics *>(gfx);
48 if (!vkg) throw Exception("Gpgpu: requires Vulkan Graphics backend");
49 if (!static_cast<VkDevice>(vkg->getDevice().instance))
50 throw Exception("Gpgpu: Graphics device not initialized (create a window first)");
51 return vkg;
52}
53
55 try {
56 auto *gfx = eve::ModuleManager::getInstance<eve::graphics::Graphics>("Graphics");
57 if (!gfx) return false;
58 auto *vkg = dynamic_cast<graphics::vulkan::Graphics *>(gfx);
59 if (!vkg) return false;
60 return static_cast<VkDevice>(vkg->getDevice().instance) != VK_NULL_HANDLE;
61 } catch (...) {
62 return false;
63 }
64}
65
67 // Must match computeCommandPool (uploadPool is graphics-family). Only use a
68 // dedicated compute queue when it shares that family; otherwise submit on graphics.
69 auto &device = vkg->getDevice();
70 const uint32_t graphicsFamily = device.get_queue_index(vkb::QueueType::graphics);
71 vk::Queue compute = device.getQueue(vkb::QueueType::compute);
72 if (compute && device.get_queue_index(vkb::QueueType::compute) == graphicsFamily)
73 return compute;
74 return device.getQueue(vkb::QueueType::graphics);
75}
76
78 return vkg->getUploadPool();
79}
80
81std::vector<uint32_t> loadSpirvFile(const std::string &path) {
82 auto *fs = eve::filesystem::Filesystem::create();
83 std::unique_ptr<eve::filesystem::FileData> fd(fs->read(path));
84 if (!fd) throw Exception("Gpgpu.newShaderFromSpvFile: failed to read '%s'", path.c_str());
85 return loadSpirvBytes(fd->getData(), fd->getSize());
86}
87
88std::vector<uint32_t> compileComputeGlsl(const std::string &glsl) {
89 if (glsl.empty()) throw compileError("Gpgpu.newShader: empty GLSL");
90 try {
91 return graphics::compileGlslToSpirv(glsl, graphics::GlslStage::eCompute, "eve_compute.comp");
92 } catch (const std::exception &error) {
93 // The shared compiler helper reports plain runtime errors (it also runs on
94 // CPU workers); keep the Gpgpu.newShader prefix callers rely on.
95 throw compileError("Gpgpu.newShader: %s", error.what());
96 }
97}
98
99} // namespace eve::gpgpu
std::string message
vkb::Device & device
std::string text
OnnxCompute * compute
std::string error
Definition Package.cpp:60
std::string path
Definition PlayHost.cpp:110
std::uint32_t count
float size
Definition TreeMesh.cpp:156
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
EVENGINE_API_BACKENDS public API.
Definition Graphics.h:342
vk::CommandPool getUploadPool() const
Returns the upload pool.
Definition Graphics.h:1016
vkb::Device & getDevice()
Returns the device.
Definition Graphics.h:1014
Fluids module — interactive surface fluid simulation.
Definition Fluids.h:34
bool vulkanGraphicsReady()
True if Vulkan Graphics device is ready.
std::vector< uint32_t > loadSpirvFile(const std::string &path)
Loads spirv file.
std::vector< uint32_t > compileComputeGlsl(const std::string &glsl)
Compiles compute glsl.
vk::Queue computeQueue(graphics::vulkan::Graphics *vkg)
Computes queue.
graphics::vulkan::Graphics * requireVulkanGraphics()
Resolve live Vulkan Graphics (throws if missing / not initialized).
vk::CommandPool computeCommandPool(graphics::vulkan::Graphics *vkg)
Computes command pool.
std::vector< std::uint32_t > compileGlslToSpirv(const std::string &source, GlslStage stage, const std::string &debugName)
Compile GLSL text to SPIR-V words.