[Bf-blender-cvs] [92c9a36c32a] temp-gpu-uniform-builtin-structs: Updating and binding of shader block.

Jeroen Bakker noreply at git.blender.org
Fri Jul 9 12:23:19 CEST 2021


Commit: 92c9a36c32a5fb9b3c1b6dd16b1ed1492f246392
Author: Jeroen Bakker
Date:   Fri Jul 9 12:21:43 2021 +0200
Branches: temp-gpu-uniform-builtin-structs
https://developer.blender.org/rB92c9a36c32a5fb9b3c1b6dd16b1ed1492f246392

Updating and binding of shader block.

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

M	source/blender/gpu/GPU_shader_block_types.h
M	source/blender/gpu/intern/gpu_shader.cc
M	source/blender/gpu/intern/gpu_shader_block.cc
M	source/blender/gpu/intern/gpu_shader_block.hh
M	source/blender/gpu/intern/gpu_shader_private.hh

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

diff --git a/source/blender/gpu/GPU_shader_block_types.h b/source/blender/gpu/GPU_shader_block_types.h
index fb372582311..db994dd2f93 100644
--- a/source/blender/gpu/GPU_shader_block_types.h
+++ b/source/blender/gpu/GPU_shader_block_types.h
@@ -23,6 +23,8 @@
 
 #pragma once
 
+#include "BLI_assert.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -39,8 +41,11 @@ typedef struct GPUShaderBlock3dColor {
   float color[4];
   float WorldClipPlanes[6][4];
   int SrgbTransform;
+  int _pad[3];
 } GPUShaderBlock3dColor;
 
+BLI_STATIC_ASSERT_ALIGN(GPUShaderBlock3dColor, 16)
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/gpu/intern/gpu_shader.cc b/source/blender/gpu/intern/gpu_shader.cc
index b7cef158d57..09be33145ca 100644
--- a/source/blender/gpu/intern/gpu_shader.cc
+++ b/source/blender/gpu/intern/gpu_shader.cc
@@ -62,6 +62,30 @@ Shader::~Shader()
   delete m_shader_struct;
 }
 
+const bool Shader::has_shader_block() const
+{
+  return m_shader_struct;
+}
+
+const bool Shader::shader_block_dirty_get() const
+{
+  BLI_assert(m_shader_struct);
+  return m_shader_struct->flags().is_dirty;
+}
+
+void Shader::shader_block_update() const
+{
+  BLI_assert(m_shader_struct);
+  m_shader_struct->update();
+}
+
+void Shader::shader_block_bind() const
+{
+  BLI_assert(m_shader_struct);
+  const int binding = interface->ubo_builtin(GPU_UNIFORM_BLOCK_SHADER);
+  m_shader_struct->bind(binding);
+}
+
 static void standard_defines(Vector<const char *> &sources)
 {
   BLI_assert(sources.size() == 0);
@@ -395,6 +419,13 @@ void GPU_shader_bind(GPUShader *gpu_shader)
       GPU_matrix_bind(gpu_shader);
     }
   }
+
+  if (shader->has_shader_block()) {
+    if (shader->shader_block_dirty_get()) {
+      shader->shader_block_update();
+    }
+    shader->shader_block_bind();
+  }
 }
 
 void GPU_shader_unbind(void)
diff --git a/source/blender/gpu/intern/gpu_shader_block.cc b/source/blender/gpu/intern/gpu_shader_block.cc
index 87c09e94713..a1181218534 100644
--- a/source/blender/gpu/intern/gpu_shader_block.cc
+++ b/source/blender/gpu/intern/gpu_shader_block.cc
@@ -243,10 +243,13 @@ ShaderBlockBuffer::ShaderBlockBuffer(const GPUShaderBlockType type)
     : m_type_info(ShaderBlockType::get(type))
 {
   m_data = MEM_mallocN(m_type_info.data_size(), __func__);
+  m_ubo = GPUBackend::get()->uniformbuf_alloc(m_type_info.data_size(), __func__);
 }
 
 ShaderBlockBuffer::~ShaderBlockBuffer()
 {
+  delete m_ubo;
+  m_ubo = nullptr;
   MEM_freeN(m_data);
   m_data = nullptr;
 }
@@ -292,6 +295,19 @@ bool ShaderBlockBuffer::uniform_float(int location,
   return true;
 }
 
+void ShaderBlockBuffer::update()
+{
+  if (m_flags.is_dirty) {
+    m_ubo->update(m_data);
+    m_flags.is_dirty = false;
+  }
+}
+
+void ShaderBlockBuffer::bind(int binding)
+{
+  m_ubo->bind(binding);
+}
+
 /** \} */
 
 }  // namespace blender::gpu
diff --git a/source/blender/gpu/intern/gpu_shader_block.hh b/source/blender/gpu/intern/gpu_shader_block.hh
index d96762a7530..74ccac10472 100644
--- a/source/blender/gpu/intern/gpu_shader_block.hh
+++ b/source/blender/gpu/intern/gpu_shader_block.hh
@@ -27,7 +27,9 @@
 #include <optional>
 
 #include "GPU_shader_block_types.h"
+#include "gpu_backend.hh"
 #include "gpu_shader_interface.hh"
+#include "gpu_uniform_buffer_private.hh"
 
 #include <array>
 
@@ -81,6 +83,11 @@ class ShaderBlockBuffer {
 
   ~ShaderBlockBuffer();
 
+  const Flags &flags() const
+  {
+    return m_flags;
+  }
+
   void *data() const
   {
     return m_data;
@@ -93,11 +100,14 @@ class ShaderBlockBuffer {
 
   bool uniform_int(int location, int comp_len, int array_size, const int *data);
   bool uniform_float(int location, int comp_len, int array_size, const float *data);
+  void update();
+  void bind(int binding);
 
  private:
   Flags m_flags;
   const ShaderBlockType &m_type_info;
   void *m_data;
+  UniformBuf *m_ubo;
 };
 
 std::optional<const GPUShaderBlockType> find_smallest_uniform_builtin_struct(
diff --git a/source/blender/gpu/intern/gpu_shader_private.hh b/source/blender/gpu/intern/gpu_shader_private.hh
index 4f138e8a5fb..574b94ce82e 100644
--- a/source/blender/gpu/intern/gpu_shader_private.hh
+++ b/source/blender/gpu/intern/gpu_shader_private.hh
@@ -89,6 +89,11 @@ class Shader {
     m_shader_struct = new ShaderBlockBuffer(struct_type);
   }
 
+  const bool has_shader_block() const;
+  const bool shader_block_dirty_get() const;
+  void shader_block_update() const;
+  void shader_block_bind() const;
+
  protected:
   void print_log(Span<const char *> sources,
                  char *log,



More information about the Bf-blender-cvs mailing list