From 0271c736a9f6aec1c326501a8e73bf8c12329882 Mon Sep 17 00:00:00 2001 From: Jeroen Bakker Date: Wed, 7 Jul 2021 08:52:34 +0200 Subject: Moved to own unit. --- source/blender/gpu/CMakeLists.txt | 1 + source/blender/gpu/GPU_shader.h | 16 +--- source/blender/gpu/GPU_uniform_buffer_types.h | 46 +++++++++++ source/blender/gpu/intern/gpu_shader.cc | 3 +- source/blender/gpu/intern/gpu_shader_interface.cc | 53 ------------ source/blender/gpu/intern/gpu_shader_interface.hh | 4 +- .../gpu/intern/gpu_uniform_buffer_private.hh | 22 +++++ .../gpu/intern/gpu_uniform_buffer_structs.cc | 94 ++++++++++++++++++++++ .../material/gpu_shader_builtin_uniforms_lib.glsl | 15 ++++ 9 files changed, 183 insertions(+), 71 deletions(-) create mode 100644 source/blender/gpu/GPU_uniform_buffer_types.h create mode 100644 source/blender/gpu/intern/gpu_uniform_buffer_structs.cc create mode 100644 source/blender/gpu/shaders/material/gpu_shader_builtin_uniforms_lib.glsl diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt index abb7330d292..919e42f4377 100644 --- a/source/blender/gpu/CMakeLists.txt +++ b/source/blender/gpu/CMakeLists.txt @@ -87,6 +87,7 @@ set(SRC intern/gpu_state.cc intern/gpu_texture.cc intern/gpu_uniform_buffer.cc + intern/gpu_uniform_buffer_structs.cc intern/gpu_vertex_buffer.cc intern/gpu_vertex_format.cc intern/gpu_viewport.c diff --git a/source/blender/gpu/GPU_shader.h b/source/blender/gpu/GPU_shader.h index 5bc47be0459..d0b3bb5e9e6 100644 --- a/source/blender/gpu/GPU_shader.h +++ b/source/blender/gpu/GPU_shader.h @@ -23,6 +23,8 @@ #pragma once +#include "GPU_uniform_buffer_types.h" + #ifdef __cplusplus extern "C" { #endif @@ -40,12 +42,6 @@ typedef enum eGPUShaderTFBType { GPU_SHADER_TFB_TRIANGLES = 3, } eGPUShaderTFBType; -typedef enum GPUUniformBuiltinStructType { - GPU_UNIFORM_STRUCT_NONE = 0, - GPU_UNIFORM_STRUCT_1, - GPU_NUM_UNIFORM_STRUCTS, /* Special value, denotes number of structs. */ -} GPUUniformBuiltinStructType; - GPUShader *GPU_shader_create(const char *vertcode, const char *fragcode, const char *geomcode, @@ -125,14 +121,6 @@ typedef enum { GPU_NUM_UNIFORMS, /* Special value, denotes number of builtin uniforms. */ } GPUUniformBuiltin; -typedef struct GPUUniformBuiltinStruct1 { - float ModelMatrix[4][4]; - float ModelViewProjectionMatrix[4][4]; - float color[4]; - float WorldClipPlanes[6][4]; - int SrgbTransform; -} GPUUniformBuiltinStruct1; - typedef enum { GPU_UNIFORM_BLOCK_VIEW = 0, /* viewBlock */ GPU_UNIFORM_BLOCK_MODEL, /* modelBlock */ diff --git a/source/blender/gpu/GPU_uniform_buffer_types.h b/source/blender/gpu/GPU_uniform_buffer_types.h new file mode 100644 index 00000000000..36ce53d21c7 --- /dev/null +++ b/source/blender/gpu/GPU_uniform_buffer_types.h @@ -0,0 +1,46 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2021 Blender Foundation. + * All rights reserved. + */ + +/** \file + * \ingroup gpu + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum GPUUniformBuiltinStructType { + GPU_UNIFORM_STRUCT_NONE = 0, + GPU_UNIFORM_STRUCT_1, + GPU_NUM_UNIFORM_STRUCTS, /* Special value, denotes number of structs. */ +} GPUUniformBuiltinStructType; + +typedef struct GPUUniformBuiltinStruct1 { + float ModelMatrix[4][4]; + float ModelViewProjectionMatrix[4][4]; + float color[4]; + float WorldClipPlanes[6][4]; + int SrgbTransform; +} GPUUniformBuiltinStruct1; + +#ifdef __cplusplus +} +#endif diff --git a/source/blender/gpu/intern/gpu_shader.cc b/source/blender/gpu/intern/gpu_shader.cc index 310b506b2e5..373162d35f7 100644 --- a/source/blender/gpu/intern/gpu_shader.cc +++ b/source/blender/gpu/intern/gpu_shader.cc @@ -34,6 +34,7 @@ #include "gpu_backend.hh" #include "gpu_context_private.hh" #include "gpu_shader_private.hh" +#include "gpu_uniform_buffer_private.hh" #include "CLG_log.h" @@ -184,7 +185,7 @@ GPUShader *GPU_shader_create_ex(const char *vertcode, if (G.debug & G_DEBUG_GPU) { std::optional best_struct_type = - shader->interface->best_builtin_uniform_struct(); + find_smallest_uniform_builtin_struct(*shader->interface); if (best_struct_type) { if (/*uniform_struct_type != GPU_UNIFORM_STRUCT_NONE &&*/ uniform_struct_type != *best_struct_type) { diff --git a/source/blender/gpu/intern/gpu_shader_interface.cc b/source/blender/gpu/intern/gpu_shader_interface.cc index a4d077eef61..364ec1aacfc 100644 --- a/source/blender/gpu/intern/gpu_shader_interface.cc +++ b/source/blender/gpu/intern/gpu_shader_interface.cc @@ -129,45 +129,6 @@ void ShaderInterface::debug_print() printf("\n"); } -/* TODO(jbakker): move to new compile unit (GPU_uniform_structs.cc). */ - -static bool can_hold_element(const GPUUniformBuiltinStructType struct_type, - const GPUUniformBuiltin builtin_uniform) -{ - switch (struct_type) { - case GPU_UNIFORM_STRUCT_NONE: - return false; - break; - - case GPU_UNIFORM_STRUCT_1: - return ELEM(builtin_uniform, - GPU_UNIFORM_MODEL, - GPU_UNIFORM_MVP, - GPU_UNIFORM_COLOR, - GPU_UNIFORM_CLIPPLANES, - GPU_UNIFORM_SRGB_TRANSFORM); - break; - - case GPU_NUM_UNIFORM_STRUCTS: - return false; - break; - } - return false; -} - -static bool can_hold_data(const ShaderInterface &interface, - const GPUUniformBuiltinStructType struct_type) -{ - for (int i = 0; i < GPU_NUM_UNIFORMS; i++) { - const GPUUniformBuiltin builtin_uniform = static_cast(i); - const bool builtin_is_used = interface.builtins_[i] != -1; - if (builtin_is_used && !can_hold_element(struct_type, builtin_uniform)) { - return false; - } - } - return true; -} - bool ShaderInterface::has_builtin_uniforms() const { for (int i = 0; i < GPU_NUM_UNIFORMS; i++) { @@ -178,18 +139,4 @@ bool ShaderInterface::has_builtin_uniforms() const return false; } -std::optional ShaderInterface::best_builtin_uniform_struct() - const -{ - if (!this->has_builtin_uniforms()) { - return std::nullopt; - } - - if (can_hold_data(*this, GPU_UNIFORM_STRUCT_1)) { - return std::make_optional(GPU_UNIFORM_STRUCT_1); - } - - return std::nullopt; -} - } // namespace blender::gpu diff --git a/source/blender/gpu/intern/gpu_shader_interface.hh b/source/blender/gpu/intern/gpu_shader_interface.hh index 00afc4dde98..d882e71e656 100644 --- a/source/blender/gpu/intern/gpu_shader_interface.hh +++ b/source/blender/gpu/intern/gpu_shader_interface.hh @@ -77,6 +77,7 @@ class ShaderInterface { virtual ~ShaderInterface(); void debug_print(void); + bool has_builtin_uniforms() const; inline const ShaderInput *attr_get(const char *name) const { @@ -126,8 +127,6 @@ class ShaderInterface { return builtin_blocks_[builtin]; } - std::optional best_builtin_uniform_struct() const; - protected: static inline const char *builtin_uniform_name(GPUUniformBuiltin u); static inline const char *builtin_uniform_block_name(GPUUniformBlockBuiltin u); @@ -145,7 +144,6 @@ class ShaderInterface { inline const ShaderInput *input_lookup(const ShaderInput *const inputs, const uint inputs_len, const int binding) const; - bool has_builtin_uniforms() const; }; inline const char *ShaderInterface::builtin_uniform_name(GPUUniformBuiltin u) diff --git a/source/blender/gpu/intern/gpu_uniform_buffer_private.hh b/source/blender/gpu/intern/gpu_uniform_buffer_private.hh index e8fc1343eaf..b514b312c11 100644 --- a/source/blender/gpu/intern/gpu_uniform_buffer_private.hh +++ b/source/blender/gpu/intern/gpu_uniform_buffer_private.hh @@ -24,11 +24,18 @@ #include "BLI_sys_types.h" +#include + +#include "GPU_shader.h" +#include "GPU_uniform_buffer_types.h" + struct GPUUniformBuf; namespace blender { namespace gpu { +class ShaderInterface; + #ifdef DEBUG # define DEBUG_NAME_LEN 64 #else @@ -79,6 +86,21 @@ static inline const UniformBuf *unwrap(const GPUUniformBuf *vert) return reinterpret_cast(vert); } +class UniformBuiltinStructType { + public: + UniformBuiltinStructType(const GPUUniformBuiltinStructType type); + + GPUUniformBuiltinStructType type; + + bool has_all_builtin_uniforms(const ShaderInterface &interface) const; + + private: + bool has_attribute(const GPUUniformBuiltin builtin_uniform) const; +}; + +std::optional find_smallest_uniform_builtin_struct( + const ShaderInterface &interface); + #undef DEBUG_NAME_LEN } // namespace gpu diff --git a/source/blender/gpu/intern/gpu_uniform_buffer_structs.cc b/source/blender/gpu/intern/gpu_uniform_buffer_structs.cc new file mode 100644 index 00000000000..897d1088e43 --- /dev/null +++ b/source/blender/gpu/intern/gpu_uniform_buffer_structs.cc @@ -0,0 +1,94 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2021 Blender Foundation. + * All rights reserved. + */ + +/** \file + * \ingroup gpu + */ + +#include "gpu_uniform_buffer_private.hh" + +#include "GPU_shader.h" +#include "GPU_uniform_buffer_types.h" +#include "gpu_shader_interface.hh" + +namespace blender::gpu { + +/* -------------------------------------------------------------------- */ +/** \name Struct type + * \{ */ + +UniformBuiltinStructType::UniformBuiltinStructType(const GPUUniformBuiltinStructType type) + : type(type) +{ +} + +bool UniformBuiltinStructType::has_attribute(const GPUUniformBuiltin builtin_uniform) const +{ + switch (type) { + case GPU_UNIFORM_STRUCT_NONE: + return false; + break; + + case GPU_UNIFORM_STRUCT_1: + return ELEM(builtin_uniform, + GPU_UNIFORM_MODEL, + GPU_UNIFORM_MVP, + GPU_UNIFORM_COLOR, + GPU_UNIFORM_CLIPPLANES, + GPU_UNIFORM_SRGB_TRANSFORM); + break; + + case GPU_NUM_UNIFORM_STRUCTS: + return false; + break; + } + return false; +} + +bool UniformBuiltinStructType::has_all_builtin_uniforms(const ShaderInterface &interface) const +{ + for (int i = 0; i < GPU_NUM_UNIFORMS; i++) { + const GPUUniformBuiltin builtin_uniform = static_cast(i); + const bool builtin_is_used = interface.builtins_[i] != -1; + if (builtin_is_used && !has_attribute(builtin_uniform)) { + return false; + } + } + return true; +} + +std::optional find_smallest_uniform_builtin_struct( + const ShaderInterface &interface) +{ + if (!interface.has_builtin_uniforms()) { + return std::nullopt; + } + + const UniformBuiltinStructType struct1(GPU_UNIFORM_STRUCT_1); + + if (struct1.has_all_builtin_uniforms(interface)) { + return std::make_optional(struct1.type); + } + + return std::nullopt; +} + +/** \} */ + +} // namespace blender::gpu diff --git a/source/blender/gpu/shaders/material/gpu_shader_builtin_uniforms_lib.glsl b/source/blender/gpu/shaders/material/gpu_shader_builtin_uniforms_lib.glsl new file mode 100644 index 00000000000..7d99b4dc99d --- /dev/null +++ b/source/blender/gpu/shaders/material/gpu_shader_builtin_uniforms_lib.glsl @@ -0,0 +1,15 @@ + +#ifdef BUILTIN_UNIFORMS_BUFFER +# if BUILTIN_UNIFORMS_BUFFER == 1 + +layout(std140) uniform shaderBlock +{ + mat4 ModelMatrix; + mat4 ModelViewProjectionMatrix; + vec4 color; + vec4 WorldClipPlanes[6]; + bool srgbTransform; +}; + +# endif +#endif \ No newline at end of file -- cgit v1.2.3