[Bf-blender-cvs] [44ad59592b6] temp-vulkan-shader: Vulkan: compile shader to spirv.
Jeroen Bakker
noreply at git.blender.org
Mon Nov 21 14:01:57 CET 2022
Commit: 44ad59592b6824d83074f3cb2008c3bd86689575
Author: Jeroen Bakker
Date: Mon Nov 21 14:01:47 2022 +0100
Branches: temp-vulkan-shader
https://developer.blender.org/rB44ad59592b6824d83074f3cb2008c3bd86689575
Vulkan: compile shader to spirv.
===================================================================
M build_files/cmake/platform/platform_apple.cmake
M source/blender/gpu/vulkan/vk_backend.cc
M source/blender/gpu/vulkan/vk_backend.hh
M source/blender/gpu/vulkan/vk_shader.cc
M source/blender/gpu/vulkan/vk_shader.hh
===================================================================
diff --git a/build_files/cmake/platform/platform_apple.cmake b/build_files/cmake/platform/platform_apple.cmake
index 403d131cbfa..3f1b87ec0b0 100644
--- a/build_files/cmake/platform/platform_apple.cmake
+++ b/build_files/cmake/platform/platform_apple.cmake
@@ -107,7 +107,7 @@ if(WITH_VULKAN_BACKEND)
set(VULKAN_INCLUDE_DIR ${VULKAN_ROOT_DIR}/include)
set(VULKAN_INCLUDE_DIRS ${VULKAN_INCLUDE_DIR})
set(VULKAN_LIBRARY ${VULKAN_ROOT_DIR}/lib/libvulkan.1.dylib)
- set(SHADERC_LIBRARY ${VULKAN_ROOT_DIR}/lib/libshaderc.a)
+ set(SHADERC_LIBRARY ${VULKAN_ROOT_DIR}/lib/libshaderc_combined.a)
set(VULKAN_LIBRARIES ${VULKAN_LIBRARY} ${SHADERC_LIBRARY})
else()
message(WARNING "Vulkan was not found, disabling WITH_VULKAN_BACKEND")
diff --git a/source/blender/gpu/vulkan/vk_backend.cc b/source/blender/gpu/vulkan/vk_backend.cc
index d22275cebef..c6241e03652 100644
--- a/source/blender/gpu/vulkan/vk_backend.cc
+++ b/source/blender/gpu/vulkan/vk_backend.cc
@@ -132,4 +132,9 @@ void VKBackend::render_step()
{
}
+shaderc::Compiler &VKBackend::get_shaderc_compiler()
+{
+ return shaderc_compiler_;
+}
+
} // namespace blender::gpu
\ No newline at end of file
diff --git a/source/blender/gpu/vulkan/vk_backend.hh b/source/blender/gpu/vulkan/vk_backend.hh
index b52f17ae40c..d4ceb3dcd9d 100644
--- a/source/blender/gpu/vulkan/vk_backend.hh
+++ b/source/blender/gpu/vulkan/vk_backend.hh
@@ -9,10 +9,19 @@
#include "gpu_backend.hh"
+#ifdef __APPLE__
+# include <MoltenVK/vk_mvk_moltenvk.h>
+#else
+# include <vulkan/vulkan.h>
+#endif
+#include "shaderc/shaderc.hpp"
namespace blender::gpu {
class VKBackend : public GPUBackend {
+ private:
+ shaderc::Compiler shaderc_compiler_;
+
public:
VKBackend()
{
@@ -49,6 +58,8 @@ class VKBackend : public GPUBackend {
void render_end() override;
void render_step() override;
+ shaderc::Compiler &get_shaderc_compiler();
+
private:
static void init_platform();
static void platform_exit();
diff --git a/source/blender/gpu/vulkan/vk_shader.cc b/source/blender/gpu/vulkan/vk_shader.cc
index 6ebca5d5904..37d26767c7a 100644
--- a/source/blender/gpu/vulkan/vk_shader.cc
+++ b/source/blender/gpu/vulkan/vk_shader.cc
@@ -7,14 +7,45 @@
#include "vk_shader.hh"
-#ifdef __APPLE__
-# include <MoltenVK/vk_mvk_moltenvk.h>
-#else
-# include <vulkan/vulkan.h>
-#endif
-#include "shaderc/shaderc.hpp"
+#include "vk_backend.hh"
+
+#include "BLI_string_utils.h"
+#include "BLI_vector.hh"
namespace blender::gpu {
+
+static std::string combine_sources(MutableSpan<const char *> sources)
+{
+ char *sources_combined = BLI_string_join_arrayN((const char **)sources.data(), sources.size());
+ return std::string(sources_combined);
+}
+
+Vector<uint32_t> VKShader::compile_glsl_to_spirv(StringRef source, shaderc_shader_kind kind)
+{
+ VKBackend &backend = static_cast<VKBackend &>(*VKBackend::get());
+ shaderc::Compiler &compiler = backend.get_shaderc_compiler();
+ shaderc::CompileOptions options;
+
+ shaderc::SpvCompilationResult module = compiler.CompileGlslToSpv(source, kind, name, options);
+
+ if (module.GetCompilationStatus() != shaderc_compilation_status_success) {
+ // TODO(jbakker): error handling.
+ }
+
+ return Vector<uint32_t>(module.cbegin(), module.cend());
+}
+
+void VKShader::build_shader_module(Span<uint32_t> spirv_module, VkShaderModule *r_shader_module)
+{
+ VkShaderModuleCreateInfo createInfo = {};
+ createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
+ createInfo.codeSize = spirv_module.size() * sizeof(uint32_t);
+ createInfo.pCode = spirv_module.data();
+
+ // TODO(jbakker): retrieve allocator and device
+ // VkResult result = vkCreateShaderModule(*device, &create_info, nullptr, r_shader_module);
+}
+
void VKShader::vertex_shader_from_glsl(MutableSpan<const char *> /*sources*/)
{
}
@@ -27,8 +58,12 @@ void VKShader::fragment_shader_from_glsl(MutableSpan<const char *> /*sources*/)
{
}
-void VKShader::compute_shader_from_glsl(MutableSpan<const char *> /*sources*/)
+void VKShader::compute_shader_from_glsl(MutableSpan<const char *> sources)
{
+ std::string source = combine_sources(sources);
+
+ Vector<uint32_t> spirv_module = compile_glsl_to_spirv(StringRef(source), shaderc_compute_shader);
+ build_shader_module(spirv_module, &compute_module_);
}
bool VKShader::finalize(const shader::ShaderCreateInfo * /*info*/)
diff --git a/source/blender/gpu/vulkan/vk_shader.hh b/source/blender/gpu/vulkan/vk_shader.hh
index 9ab0aca67eb..e6cdf246f3e 100644
--- a/source/blender/gpu/vulkan/vk_shader.hh
+++ b/source/blender/gpu/vulkan/vk_shader.hh
@@ -9,9 +9,16 @@
#include "gpu_shader_private.hh"
+#include "vk_backend.hh"
+
+#include "BLI_string_ref.hh"
+
namespace blender::gpu {
class VKShader : public Shader {
+ private:
+ VkShaderModule compute_module_ = nullptr;
+
public:
VKShader(const char *name) : Shader(name)
{
@@ -43,6 +50,10 @@ class VKShader : public Shader {
/* DEPRECATED: Kept only because of BGL API. */
int program_handle_get() const override;
+
+ private:
+ Vector<uint32_t> compile_glsl_to_spirv(StringRef source, shaderc_shader_kind kind);
+ void build_shader_module(Span<uint32_t> spirv_module, VkShaderModule *r_shader_module);
};
} // namespace blender::gpu
\ No newline at end of file
More information about the Bf-blender-cvs
mailing list