21std::runtime_error compileError(
const char *
message) {
22 return std::runtime_error(
message);
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...);
32 return std::runtime_error(
text);
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);
45 auto *gfx = eve::ModuleManager::getInstance<eve::graphics::Graphics>(
"Graphics");
46 if (!gfx) gfx = eve::graphics::Graphics::create();
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)");
56 auto *gfx = eve::ModuleManager::getInstance<eve::graphics::Graphics>(
"Graphics");
57 if (!gfx)
return false;
59 if (!vkg)
return false;
60 return static_cast<VkDevice
>(vkg->getDevice().instance) != VK_NULL_HANDLE;
70 const uint32_t graphicsFamily =
device.get_queue_index(vkb::QueueType::graphics);
72 if (
compute &&
device.get_queue_index(vkb::QueueType::compute) == graphicsFamily)
74 return device.getQueue(vkb::QueueType::graphics);
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());
89 if (glsl.empty())
throw compileError(
"Gpgpu.newShader: empty GLSL");
92 }
catch (
const std::exception &
error) {
95 throw compileError(
"Gpgpu.newShader: %s",
error.what());
EVENGINE_API_FOUNDATION public API.
EVENGINE_API_BACKENDS public API.
vk::CommandPool getUploadPool() const
Returns the upload pool.
vkb::Device & getDevice()
Returns the device.
Fluids module — interactive surface fluid simulation.
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.