[Bf-blender-cvs] [2e7ea42377b] temp-gpu-push-constants: GPU: Binding uniformbuffer as push constant.

Jeroen Bakker noreply at git.blender.org
Fri Jul 2 16:39:49 CEST 2021


Commit: 2e7ea42377bb91416477290eb0c43244ddc85bf5
Author: Jeroen Bakker
Date:   Fri Jul 2 16:06:15 2021 +0200
Branches: temp-gpu-push-constants
https://developer.blender.org/rB2e7ea42377bb91416477290eb0c43244ddc85bf5

GPU: Binding uniformbuffer as push constant.

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

M	source/blender/gpu/GPU_shader.h
M	source/blender/gpu/intern/gpu_shader.cc
M	source/blender/gpu/intern/gpu_shader_private.hh
M	source/blender/gpu/opengl/gl_shader.cc
M	source/blender/gpu/opengl/gl_shader.hh
M	source/blender/gpu/tests/gpu_shader_push_constants_test.cc

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

diff --git a/source/blender/gpu/GPU_shader.h b/source/blender/gpu/GPU_shader.h
index f834ee5b234..79b29cc5cdc 100644
--- a/source/blender/gpu/GPU_shader.h
+++ b/source/blender/gpu/GPU_shader.h
@@ -29,6 +29,7 @@ extern "C" {
 
 struct GPUIndexBuf;
 struct GPUVertBuf;
+struct GPUUniformBuf;
 
 /** Opaque type hiding #blender::gpu::Shader */
 typedef struct GPUShader GPUShader;
@@ -157,7 +158,7 @@ void GPU_shader_uniform_4fv(GPUShader *sh, const char *name, const float data[4]
 void GPU_shader_uniform_mat4(GPUShader *sh, const char *name, const float data[4][4]);
 void GPU_shader_uniform_2fv_array(GPUShader *sh, const char *name, int len, const float (*val)[2]);
 void GPU_shader_uniform_4fv_array(GPUShader *sh, const char *name, int len, const float (*val)[4]);
-
+void GPU_shader_uniform_push_constant(GPUShader *sh, struct GPUUniformBuf *ubo);
 int GPU_shader_get_attribute(GPUShader *shader, const char *name);
 
 void GPU_shader_set_framebuffer_srgb_target(int use_srgb_to_linear);
diff --git a/source/blender/gpu/intern/gpu_shader.cc b/source/blender/gpu/intern/gpu_shader.cc
index c754a649924..1c66b43d93e 100644
--- a/source/blender/gpu/intern/gpu_shader.cc
+++ b/source/blender/gpu/intern/gpu_shader.cc
@@ -556,6 +556,12 @@ void GPU_shader_uniform_4fv_array(GPUShader *sh, const char *name, int len, cons
   GPU_shader_uniform_vector(sh, loc, 4, len, (const float *)val);
 }
 
+void GPU_shader_uniform_push_constant(GPUShader *sh, GPUUniformBuf *ubo)
+{
+  UniformBuf *buf = unwrap(ubo);
+  unwrap(sh)->uniform_push_constant(buf);
+}
+
 /** \} */
 
 /* -------------------------------------------------------------------- */
diff --git a/source/blender/gpu/intern/gpu_shader_private.hh b/source/blender/gpu/intern/gpu_shader_private.hh
index 65720e457d8..6a50a092407 100644
--- a/source/blender/gpu/intern/gpu_shader_private.hh
+++ b/source/blender/gpu/intern/gpu_shader_private.hh
@@ -25,6 +25,7 @@
 
 #include "GPU_shader.h"
 #include "gpu_shader_interface.hh"
+#include "gpu_uniform_buffer_private.hh"
 #include "gpu_vertex_buffer_private.hh"
 
 namespace blender {
@@ -65,6 +66,7 @@ class Shader {
 
   virtual void uniform_float(int location, int comp_len, int array_size, const float *data) = 0;
   virtual void uniform_int(int location, int comp_len, int array_size, const int *data) = 0;
+  virtual void uniform_push_constant(UniformBuf *buf) = 0;
 
   virtual void vertformat_from_shader(GPUVertFormat *) const = 0;
 
diff --git a/source/blender/gpu/opengl/gl_shader.cc b/source/blender/gpu/opengl/gl_shader.cc
index b89f59f9557..d644a5dd69d 100644
--- a/source/blender/gpu/opengl/gl_shader.cc
+++ b/source/blender/gpu/opengl/gl_shader.cc
@@ -36,6 +36,10 @@
 #include "gl_shader.hh"
 #include "gl_shader_interface.hh"
 
+#include "CLG_log.h"
+
+static CLG_LogRef LOG = {"gpu.gl.shader"};
+
 using namespace blender;
 using namespace blender::gpu;
 
@@ -364,6 +368,18 @@ void GLShader::uniform_int(int location, int comp_len, int array_size, const int
   }
 }
 
+void GLShader::uniform_push_constant(UniformBuf *buf)
+{
+  GLShaderInterface *gl_interface = static_cast<GLShaderInterface *>(interface);
+  const ShaderInput *input = gl_interface->push_constant_get();
+  if (!input) {
+    CLOG_WARN(&LOG, "Cannot bind push_constants, Shader does not have a push constant defined.");
+    return;
+  }
+
+  buf->bind(input->binding);
+}
+
 /** \} */
 
 /* -------------------------------------------------------------------- */
diff --git a/source/blender/gpu/opengl/gl_shader.hh b/source/blender/gpu/opengl/gl_shader.hh
index dfc44119e50..4e828a3c020 100644
--- a/source/blender/gpu/opengl/gl_shader.hh
+++ b/source/blender/gpu/opengl/gl_shader.hh
@@ -74,6 +74,7 @@ class GLShader : public Shader {
 
   void uniform_float(int location, int comp_len, int array_size, const float *data) override;
   void uniform_int(int location, int comp_len, int array_size, const int *data) override;
+  void uniform_push_constant(struct UniformBuf *ubo) override;
 
   void vertformat_from_shader(GPUVertFormat *format) const override;
 
diff --git a/source/blender/gpu/tests/gpu_shader_push_constants_test.cc b/source/blender/gpu/tests/gpu_shader_push_constants_test.cc
index dca6810068b..9c561fd4274 100644
--- a/source/blender/gpu/tests/gpu_shader_push_constants_test.cc
+++ b/source/blender/gpu/tests/gpu_shader_push_constants_test.cc
@@ -3,11 +3,16 @@
 #include "testing/testing.h"
 
 #include "GPU_shader.h"
+#include "GPU_uniform_buffer.h"
 
 #include "gpu_testing.hh"
 
 namespace blender::gpu::tests {
 
+struct PushConstants {
+  float color[4];
+};
+
 static void test_gpu_shader_push_constants()
 {
   const char *vert_glsl = R"(
@@ -41,6 +46,12 @@ void main()
       vert_glsl, frag_glsl, nullptr, nullptr, nullptr, "test_gpu_shader_push_constants");
   EXPECT_NE(shader, nullptr);
 
+  PushConstants push_constants;
+  GPUUniformBuf *push_constants_buffer = GPU_uniformbuf_create_ex(
+      sizeof(PushConstants), &push_constants, __func__);
+  GPU_shader_uniform_push_constant(shader, push_constants_buffer);
+  GPU_uniformbuf_free(push_constants_buffer);
+
   GPU_shader_free(shader);
 }
 GPU_TEST(gpu_shader_push_constants)



More information about the Bf-blender-cvs mailing list