[Bf-blender-cvs] [74bfeec1a5e] temp-vulkan-shader: Merge branch 'master' into temp-vulkan-shader

Jeroen Bakker noreply at git.blender.org
Fri Nov 25 10:05:01 CET 2022


Commit: 74bfeec1a5e07dbf4370cf264690712e75a4fc2a
Author: Jeroen Bakker
Date:   Fri Nov 25 09:00:38 2022 +0100
Branches: temp-vulkan-shader
https://developer.blender.org/rB74bfeec1a5e07dbf4370cf264690712e75a4fc2a

Merge branch 'master' into temp-vulkan-shader

===================================================================



===================================================================

diff --cc source/blender/gpu/CMakeLists.txt
index f0e5fef26a0,2708b0fe084..ced1489ea87
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@@ -197,6 -195,6 +197,7 @@@ set(VULKAN_SR
    vulkan/vk_index_buffer.cc
    vulkan/vk_query.cc
    vulkan/vk_shader.cc
++  vulkan/vk_shader_log.cc
    vulkan/vk_storage_buffer.cc
    vulkan/vk_texture.cc
    vulkan/vk_uniform_buffer.cc
@@@ -210,6 -208,6 +211,7 @@@
    vulkan/vk_index_buffer.hh
    vulkan/vk_query.hh
    vulkan/vk_shader.hh
++  vulkan/vk_shader_log.hh
    vulkan/vk_storage_buffer.hh
    vulkan/vk_texture.hh
    vulkan/vk_uniform_buffer.hh
diff --cc source/blender/gpu/vulkan/vk_shader.cc
index 2ad19dd25b8,d628f3eb851..6067c5e1653
--- a/source/blender/gpu/vulkan/vk_shader.cc
+++ b/source/blender/gpu/vulkan/vk_shader.cc
@@@ -7,63 -7,7 +7,117 @@@
  
  #include "vk_shader.hh"
  
 +#include "vk_backend.hh"
++#include "vk_shader_log.hh"
 +
 +#include "BLI_string_utils.h"
 +#include "BLI_vector.hh"
 +
  namespace blender::gpu {
 +
- static std::string combine_sources(MutableSpan<const char *> sources)
++static const std::string to_stage_name(shaderc_shader_kind stage)
++{
++  switch (stage) {
++    case shaderc_compute_shader:
++      return std::string("compute");
++
++    default:
++      BLI_assert_msg(false, "Do not know how to convert shaderc_shader_kind to stage name.");
++      break;
++  }
++  return std::string("unknown stage");
++}
++
++static std::string combine_sources(Span<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)
++static char *glsl_patch_get()
++{
++  static char patch[512] = "\0";
++  if (patch[0] != '\0') {
++    return patch;
++  }
++
++  size_t slen = 0;
++  /* Version need to go first. */
++  STR_CONCAT(patch, slen, "#version 430\n");
++
++  BLI_assert(slen < sizeof(patch));
++  return patch;
++}
++
++Vector<uint32_t> VKShader::compile_glsl_to_spirv(Span<const char *> sources,
++                                                 shaderc_shader_kind stage)
 +{
++  std::string combined_sources = combine_sources(sources);
 +  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);
++  shaderc::SpvCompilationResult module = compiler.CompileGlslToSpv(
++      combined_sources, stage, name, options);
++  if (module.GetNumErrors() != 0 || module.GetNumWarnings() != 0) {
++    std::string log = module.GetErrorMessage();
++    Vector<char> logcstr(log.c_str(), log.c_str() + log.size() + 1);
++
++    VKLogParser parser;
++    print_log(sources,
++              logcstr.data(),
++              to_stage_name(stage).c_str(),
++              module.GetCompilationStatus() != shaderc_compilation_status_success,
++              &parser);
++  }
 +
 +  if (module.GetCompilationStatus() != shaderc_compilation_status_success) {
-     // TODO(jbakker): error handling.
++    return Vector<uint32_t>();
 +  }
 +
 +  return Vector<uint32_t>(module.cbegin(), module.cend());
 +}
 +
 +void VKShader::build_shader_module(Span<uint32_t> spirv_module, VkShaderModule *r_shader_module)
 +{
 +  VkShaderModuleCreateInfo create_info = {};
 +  create_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
 +  create_info.codeSize = spirv_module.size() * sizeof(uint32_t);
 +  create_info.pCode = spirv_module.data();
 +
 +  VKContext &context = *static_cast<VKContext *>(VKContext::get());
 +
 +  VkResult result = vkCreateShaderModule(
 +      context.device_get(), &create_info, nullptr, r_shader_module);
 +  if (result != VK_SUCCESS) {
 +    *r_shader_module = VK_NULL_HANDLE;
 +  }
 +}
 +
 +VKShader::VKShader(const char *name) : Shader(name)
 +{
 +  context_ = VKContext::get();
 +}
 +VKShader::~VKShader()
 +{
 +  VkDevice device = context_->device_get();
 +  if (compute_module_ != VK_NULL_HANDLE) {
 +    vkDestroyShaderModule(device, compute_module_, nullptr);
 +    compute_module_ = VK_NULL_HANDLE;
 +  }
 +}
 +
++void VKShader::build_shader_module(MutableSpan<const char *> sources,
++                                   shaderc_shader_kind stage,
++                                   VkShaderModule *r_shader_module)
++{
++  BLI_assert_msg(ELEM(stage, shaderc_compute_shader),
++                 "Only forced ShaderC shader kinds are supported.");
++  sources[0] = glsl_patch_get();
++  Vector<uint32_t> spirv_module = compile_glsl_to_spirv(sources, shaderc_compute_shader);
++  build_shader_module(spirv_module, &compute_module_);
++}
++
  void VKShader::vertex_shader_from_glsl(MutableSpan<const char *> /*sources*/)
  {
  }
@@@ -76,12 -20,8 +130,9 @@@ void VKShader::fragment_shader_from_gls
  {
  }
  
 -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_);
++  build_shader_module(sources, shaderc_compute_shader, &compute_module_);
  }
  
  bool VKShader::finalize(const shader::ShaderCreateInfo * /*info*/)
diff --cc source/blender/gpu/vulkan/vk_shader.hh
index 35513bf0e1c,9ab0aca67eb..f1cd25245da
--- a/source/blender/gpu/vulkan/vk_shader.hh
+++ b/source/blender/gpu/vulkan/vk_shader.hh
@@@ -51,10 -43,6 +51,13 @@@ 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);
++  Vector<uint32_t> compile_glsl_to_spirv(Span<const char *> sources, shaderc_shader_kind kind);
 +  void build_shader_module(Span<uint32_t> spirv_module, VkShaderModule *r_shader_module);
++  void build_shader_module(MutableSpan<const char *> sources,
++                           shaderc_shader_kind stage,
++                           VkShaderModule *r_shader_module);
  };
  
  }  // namespace blender::gpu
diff --cc source/blender/gpu/vulkan/vk_shader_log.cc
index 00000000000,00000000000..7549f983d9c
new file mode 100644
--- /dev/null
+++ b/source/blender/gpu/vulkan/vk_shader_log.cc
@@@ -1,0 -1,0 +1,49 @@@
++/* SPDX-License-Identifier: GPL-2.0-or-later
++ * Copyright 2022 Blender Foundation. All rights reserved. */
++
++/** \file
++ * \ingroup gpu
++ */
++
++#include "vk_shader_log.hh"
++
++#include "GPU_platform.h"
++
++namespace blender::gpu {
++
++char *VKLogParser::parse_line(char *log_line, GPULogItem &log_item)
++{
++  log_line = skip_name_and_stage(log_line);
++  log_line = skip_separators(log_line, ":");
++
++  /* Parse error line & char numbers. */
++  if (at_number(log_line)) {
++    char *error_line_number_end;
++    log_item.cursor.row = parse_number(log_line, &error_line_number_end);
++    log_line = error_line_number_end;
++  }
++  log_line = skip_separators(log_line, ": ");
++
++  /* Skip to message. Avoid redundant info. */
++  log_line = skip_severity_keyword(log_line, log_item);
++  log_line = skip_separators(log_line, ": ");
++
++  return log_line;
++}
++
++char *VKLogParser::skip_name_and_stage(char *log_line)
++{
++  char *name_skip = skip_until(log_line, '.');
++  if (name_skip == log_line) {
++    return log_line;
++  }
++
++  return skip_until(name_skip, ':');
++}
++
++char *VKLogParser::skip_severity_keyword(char *log_line, GPULogItem &log_item)
++{
++  return skip_severity(log_line, log_item, "error", "warning");
++}
++
++}  // namespace blender::gpu
diff --cc source/blender/gpu/vulkan/vk_shader_log.hh
index 00000000000,00000000000..cd5bd49e4fb
new file mode 100644
--- /dev/null
+++ b/source/blender/gpu/vulkan/vk_shader_log.hh
@@@ -1,0 -1,0 +1,16 @@@
++/* SPDX-License-Identifier: GPL-2.0-or-later
++ * Copyright 2022 Blender Foundation. All rights reserved. */
++
++#include "gpu_shader_private.hh"
++
++namespace blender::gpu {
++
++class VKLogParser : public GPULogParser {
++ public:
++  char *parse_line(char *log_line, GPULogItem &log_item) override;
++
++ protected:
++  char *skip_name_and_stage(char *log_line);
++  char *skip_severity_keyword(char *log_line, GPULogItem &log_item);
++};
++}  // namespace blender::gpu



More information about the Bf-blender-cvs mailing list