diff options
author | Jeroen Bakker <jeroen@blender.org> | 2022-01-17 16:45:22 +0300 |
---|---|---|
committer | Jeroen Bakker <jeroen@blender.org> | 2022-01-17 16:46:32 +0300 |
commit | 9d3f35a0bf1bf5776363bfd61d53a7c85b5827a4 (patch) | |
tree | 595fe492f09aea9f97049339385f7966ac02f0e1 /source/blender/gpu/intern | |
parent | edee5a947b7ea3e1324aa334a22c7c9bbf47f5f7 (diff) |
Revert "Revert "GPUShaderCreateInfo for interface abstraction""
This reverts commit edee5a947b7ea3e1324aa334a22c7c9bbf47f5f7.
Fixes compilation error (Missing file BLI_float2.hh)
Diffstat (limited to 'source/blender/gpu/intern')
-rw-r--r-- | source/blender/gpu/intern/gpu_immediate.cc | 6 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_init_exit.c | 8 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_shader.cc | 192 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_shader_builder.cc | 102 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_shader_builder_stubs.cc | 258 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_shader_builtin.c | 244 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_shader_create_info.cc | 173 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_shader_create_info.hh | 595 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_shader_create_info_private.hh | 46 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_shader_dependency.cc | 197 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_shader_dependency_private.h | 44 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_shader_info_baked.cc | 24 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_shader_interface.hh | 26 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_shader_private.hh | 12 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_shader_shared_utils.h | 105 |
15 files changed, 1878 insertions, 154 deletions
diff --git a/source/blender/gpu/intern/gpu_immediate.cc b/source/blender/gpu/intern/gpu_immediate.cc index 7ca93252683..e28776c87b3 100644 --- a/source/blender/gpu/intern/gpu_immediate.cc +++ b/source/blender/gpu/intern/gpu_immediate.cc @@ -629,6 +629,12 @@ void immBindTextureSampler(const char *name, GPUTexture *tex, eGPUSamplerState s GPU_texture_bind_ex(tex, state, binding, true); } +void immBindUniformBuf(const char *name, GPUUniformBuf *ubo) +{ + int binding = GPU_shader_get_uniform_block_binding(imm->shader, name); + GPU_uniformbuf_bind(ubo, binding); +} + /* --- convenience functions for setting "uniform vec4 color" --- */ void immUniformColor4f(float r, float g, float b, float a) diff --git a/source/blender/gpu/intern/gpu_init_exit.c b/source/blender/gpu/intern/gpu_init_exit.c index 0eb2fe57c28..5815fb10b0c 100644 --- a/source/blender/gpu/intern/gpu_init_exit.c +++ b/source/blender/gpu/intern/gpu_init_exit.c @@ -32,6 +32,8 @@ #include "intern/gpu_codegen.h" #include "intern/gpu_material_library.h" #include "intern/gpu_private.h" +#include "intern/gpu_shader_create_info_private.hh" +#include "intern/gpu_shader_dependency_private.h" /** * although the order of initialization and shutdown should not matter @@ -49,6 +51,9 @@ void GPU_init(void) initialized = true; + gpu_shader_dependency_init(); + gpu_shader_create_info_init(); + gpu_codegen_init(); gpu_material_library_init(); @@ -70,6 +75,9 @@ void GPU_exit(void) gpu_material_library_exit(); gpu_codegen_exit(); + gpu_shader_dependency_exit(); + gpu_shader_create_info_exit(); + initialized = false; } diff --git a/source/blender/gpu/intern/gpu_shader.cc b/source/blender/gpu/intern/gpu_shader.cc index 3f5a639d2a0..3b41e804fd4 100644 --- a/source/blender/gpu/intern/gpu_shader.cc +++ b/source/blender/gpu/intern/gpu_shader.cc @@ -31,10 +31,32 @@ #include "gpu_backend.hh" #include "gpu_context_private.hh" +#include "gpu_shader_create_info.hh" +#include "gpu_shader_create_info_private.hh" +#include "gpu_shader_dependency_private.h" #include "gpu_shader_private.hh" +#include <string> + extern "C" char datatoc_gpu_shader_colorspace_lib_glsl[]; +namespace blender::gpu { + +std::string Shader::defines_declare(const shader::ShaderCreateInfo &info) const +{ + std::string defines; + for (const auto &def : info.defines_) { + defines += "#define "; + defines += def[0]; + defines += " "; + defines += def[1]; + defines += "\n"; + } + return defines; +} + +} // namespace blender::gpu + using namespace blender; using namespace blender::gpu; @@ -59,6 +81,8 @@ static void standard_defines(Vector<const char *> &sources) BLI_assert(sources.size() == 0); /* Version needs to be first. Exact values will be added by implementation. */ sources.append("version"); + /* Define to identify code usage in shading language. */ + sources.append("#define GPU_SHADER\n"); /* some useful defines to detect GPU type */ if (GPU_type_matches(GPU_DEVICE_ATI, GPU_OS_ANY, GPU_DRIVER_ANY)) { sources.append("#define GPU_ATI\n"); @@ -225,6 +249,174 @@ GPUShader *GPU_shader_create_compute(const char *computecode, shname); } +GPUShader *GPU_shader_create_from_info(const GPUShaderCreateInfo *_info) +{ + using namespace blender::gpu::shader; + const ShaderCreateInfo &info = *reinterpret_cast<const ShaderCreateInfo *>(_info); + + const_cast<ShaderCreateInfo &>(info).finalize(); + + /* At least a vertex shader and a fragment shader are required, or only a compute shader. */ + if (info.compute_source_.is_empty()) { + if (info.vertex_source_.is_empty()) { + printf("Missing vertex shader in %s.\n", info.name_.c_str()); + } + if (info.fragment_source_.is_empty()) { + printf("Missing fragment shader in %s.\n", info.name_.c_str()); + } + BLI_assert(!info.vertex_source_.is_empty() && !info.fragment_source_.is_empty()); + } + else { + if (!info.vertex_source_.is_empty()) { + printf("Compute shader has vertex_source_ shader attached in %s.\n", info.name_.c_str()); + } + if (!info.geometry_source_.is_empty()) { + printf("Compute shader has geometry_source_ shader attached in %s.\n", info.name_.c_str()); + } + if (!info.fragment_source_.is_empty()) { + printf("Compute shader has fragment_source_ shader attached in %s.\n", info.name_.c_str()); + } + BLI_assert(info.vertex_source_.is_empty() && info.geometry_source_.is_empty() && + info.fragment_source_.is_empty()); + } + + Shader *shader = GPUBackend::get()->shader_alloc(info.name_.c_str()); + + std::string defines = shader->defines_declare(info); + std::string resources = shader->resources_declare(info); + char *shader_shared_utils = nullptr; + + defines += "#define USE_GPU_SHADER_CREATE_INFO\n"; + + Vector<char *> typedefs; + for (auto filename : info.typedef_sources_) { + typedefs.append(gpu_shader_dependency_get_source(filename.c_str())); + } + if (!typedefs.is_empty()) { + shader_shared_utils = gpu_shader_dependency_get_source("gpu_shader_shared_utils.h"); + } + + if (!info.vertex_source_.is_empty()) { + uint32_t builtins = 0; + std::string interface = shader->vertex_interface_declare(info); + char *code = gpu_shader_dependency_get_resolved_source(info.vertex_source_.c_str(), &builtins); + + Vector<const char *> sources; + standard_defines(sources); + sources.append("#define GPU_VERTEX_SHADER\n"); + if (!info.geometry_source_.is_empty()) { + sources.append("#define USE_GEOMETRY_SHADER\n"); + } + sources.append(defines.c_str()); + if (!typedefs.is_empty()) { + sources.append(shader_shared_utils); + } + for (auto *types : typedefs) { + sources.append(types); + } + sources.append(resources.c_str()); + sources.append(interface.c_str()); + sources.append(code); + + shader->vertex_shader_from_glsl(sources); + + free(code); + } + + if (!info.fragment_source_.is_empty()) { + uint32_t builtins = 0; + std::string interface = shader->fragment_interface_declare(info); + char *code = gpu_shader_dependency_get_resolved_source(info.fragment_source_.c_str(), + &builtins); + + Vector<const char *> sources; + standard_defines(sources); + sources.append("#define GPU_FRAGMENT_SHADER\n"); + if (!info.geometry_source_.is_empty()) { + sources.append("#define USE_GEOMETRY_SHADER\n"); + } + sources.append(defines.c_str()); + if (!typedefs.is_empty()) { + sources.append(shader_shared_utils); + } + for (auto *types : typedefs) { + sources.append(types); + } + sources.append(resources.c_str()); + sources.append(interface.c_str()); + sources.append(code); + + shader->fragment_shader_from_glsl(sources); + + free(code); + } + + if (!info.geometry_source_.is_empty()) { + uint32_t builtins = 0; + std::string interface = shader->geometry_interface_declare(info); + std::string layout = shader->geometry_layout_declare(info); + char *code = gpu_shader_dependency_get_resolved_source(info.geometry_source_.c_str(), + &builtins); + + Vector<const char *> sources; + standard_defines(sources); + sources.append("#define GPU_GEOMETRY_SHADER\n"); + sources.append(defines.c_str()); + if (!typedefs.is_empty()) { + sources.append(shader_shared_utils); + } + for (auto *types : typedefs) { + sources.append(types); + } + sources.append(resources.c_str()); + sources.append(layout.c_str()); + sources.append(interface.c_str()); + sources.append(code); + + shader->geometry_shader_from_glsl(sources); + + free(code); + } + + if (!info.compute_source_.is_empty()) { + uint32_t builtins = 0; + char *code = gpu_shader_dependency_get_resolved_source(info.compute_source_.c_str(), + &builtins); + + Vector<const char *> sources; + standard_defines(sources); + sources.append("#define GPU_COMPUTE_SHADER\n"); + sources.append(defines.c_str()); + if (!typedefs.is_empty()) { + sources.append(shader_shared_utils); + } + for (auto *types : typedefs) { + sources.append(types); + } + sources.append(resources.c_str()); + sources.append(code); + + shader->compute_shader_from_glsl(sources); + + free(code); + } + + for (auto *types : typedefs) { + free(types); + } + + if (shader_shared_utils) { + free(shader_shared_utils); + } + + if (!shader->finalize(&info)) { + delete shader; + return nullptr; + } + + return wrap(shader); +} + GPUShader *GPU_shader_create_from_python(const char *vertcode, const char *fragcode, const char *geomcode, diff --git a/source/blender/gpu/intern/gpu_shader_builder.cc b/source/blender/gpu/intern/gpu_shader_builder.cc new file mode 100644 index 00000000000..334bdb2ec58 --- /dev/null +++ b/source/blender/gpu/intern/gpu_shader_builder.cc @@ -0,0 +1,102 @@ +/* + * 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 + * + * Compile time automation of shader compilation and validation. + */ + +#include <iostream> + +#include "GHOST_C-api.h" + +#include "GPU_context.h" +#include "GPU_init_exit.h" +#include "gpu_shader_create_info_private.hh" + +#include "CLG_log.h" + +namespace blender::gpu::shader_builder { + +class ShaderBuilder { + private: + GHOST_SystemHandle ghost_system_; + GHOST_ContextHandle ghost_context_; + GPUContext *gpu_context_ = nullptr; + + public: + void init(); + bool bake_create_infos(); + void exit(); +}; + +bool ShaderBuilder::bake_create_infos() +{ + return gpu_shader_create_info_compile_all(); +} + +void ShaderBuilder::init() +{ + CLG_init(); + + GHOST_GLSettings glSettings = {0}; + ghost_system_ = GHOST_CreateSystem(); + ghost_context_ = GHOST_CreateOpenGLContext(ghost_system_, glSettings); + GHOST_ActivateOpenGLContext(ghost_context_); + + gpu_context_ = GPU_context_create(nullptr); + GPU_init(); +} + +void ShaderBuilder::exit() +{ + GPU_backend_exit(); + GPU_exit(); + + GPU_context_discard(gpu_context_); + + GHOST_DisposeOpenGLContext(ghost_system_, ghost_context_); + GHOST_DisposeSystem(ghost_system_); + + CLG_exit(); +} + +} // namespace blender::gpu::shader_builder + +/** \brief Entry point for the shader_builder. */ +int main(int argc, const char *argv[]) +{ + if (argc < 2) { + printf("Usage: %s <data_file_to>\n", argv[0]); + exit(1); + } + + int exit_code = 0; + + blender::gpu::shader_builder::ShaderBuilder builder; + builder.init(); + if (!builder.bake_create_infos()) { + exit_code = 1; + } + builder.exit(); + exit(exit_code); + + return exit_code; +} diff --git a/source/blender/gpu/intern/gpu_shader_builder_stubs.cc b/source/blender/gpu/intern/gpu_shader_builder_stubs.cc new file mode 100644 index 00000000000..40e54ab4394 --- /dev/null +++ b/source/blender/gpu/intern/gpu_shader_builder_stubs.cc @@ -0,0 +1,258 @@ +/* + * 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 + * + * Stubs to reduce linking time for shader_builder. + */ + +#include "BLI_utildefines.h" + +#include "IMB_imbuf.h" +#include "IMB_imbuf_types.h" + +#include "BKE_customdata.h" +#include "BKE_global.h" +#include "BKE_material.h" +#include "BKE_mesh.h" +#include "BKE_node.h" +#include "BKE_paint.h" +#include "BKE_pbvh.h" +#include "BKE_subdiv_ccg.h" + +#include "DNA_userdef_types.h" + +#include "DRW_engine.h" + +#include "bmesh.h" + +#include "UI_resources.h" + +extern "C" { + +Global G; +UserDef U; + +/* -------------------------------------------------------------------- */ +/** \name Stubs of BLI_imbuf_types.h + * \{ */ + +void IMB_freeImBuf(ImBuf *UNUSED(ibuf)) +{ + BLI_assert_unreachable(); +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Stubs of UI_resources.h + * \{ */ + +void UI_GetThemeColor4fv(int UNUSED(colorid), float UNUSED(col[4])) +{ + BLI_assert_unreachable(); +} + +void UI_GetThemeColor3fv(int UNUSED(colorid), float UNUSED(col[3])) +{ + BLI_assert_unreachable(); +} + +void UI_GetThemeColorShade4fv(int UNUSED(colorid), int UNUSED(offset), float UNUSED(col[4])) +{ + BLI_assert_unreachable(); +} + +void UI_GetThemeColorShadeAlpha4fv(int UNUSED(colorid), + int UNUSED(coloffset), + int UNUSED(alphaoffset), + float UNUSED(col[4])) +{ + BLI_assert_unreachable(); +} +void UI_GetThemeColorBlendShade4fv(int UNUSED(colorid1), + int UNUSED(colorid2), + float UNUSED(fac), + int UNUSED(offset), + float UNUSED(col[4])) +{ + BLI_assert_unreachable(); +} + +void UI_GetThemeColorBlend3ubv(int UNUSED(colorid1), + int UNUSED(colorid2), + float UNUSED(fac), + unsigned char UNUSED(col[3])) +{ + BLI_assert_unreachable(); +} + +void UI_GetThemeColorShadeAlpha4ubv(int UNUSED(colorid), + int UNUSED(coloffset), + int UNUSED(alphaoffset), + unsigned char UNUSED(col[4])) +{ + BLI_assert_unreachable(); +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Stubs of BKE_paint.h + * \{ */ +bool paint_is_face_hidden(const struct MLoopTri *UNUSED(lt), + const struct MVert *UNUSED(mvert), + const struct MLoop *UNUSED(mloop)) +{ + BLI_assert_unreachable(); + return false; +} + +void BKE_paint_face_set_overlay_color_get(const int UNUSED(face_set), + const int UNUSED(seed), + uchar UNUSED(r_color[4])) +{ + BLI_assert_unreachable(); +} + +bool paint_is_grid_face_hidden(const unsigned int *UNUSED(grid_hidden), + int UNUSED(gridsize), + int UNUSED(x), + int UNUSED(y)) +{ + BLI_assert_unreachable(); + return false; +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Stubs of BKE_mesh.h + * \{ */ +void BKE_mesh_calc_poly_normal(const struct MPoly *UNUSED(mpoly), + const struct MLoop *UNUSED(loopstart), + const struct MVert *UNUSED(mvarray), + float UNUSED(r_no[3])) +{ + BLI_assert_unreachable(); +} + +void BKE_mesh_looptri_get_real_edges(const struct Mesh *UNUSED(mesh), + const struct MLoopTri *UNUSED(looptri), + int UNUSED(r_edges[3])) +{ + BLI_assert_unreachable(); +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Stubs of BKE_material.h + * \{ */ + +void BKE_material_defaults_free_gpu(void) +{ + /* This function is reachable via GPU_exit. */ +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Stubs of BKE_customdata.h + * \{ */ + +int CustomData_get_offset(const struct CustomData *UNUSED(data), int UNUSED(type)) +{ + BLI_assert_unreachable(); + return 0; +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Stubs of BKE_pbvh.h + * \{ */ + +int BKE_pbvh_count_grid_quads(BLI_bitmap **UNUSED(grid_hidden), + const int *UNUSED(grid_indices), + int UNUSED(totgrid), + int UNUSED(gridsize)) +{ + BLI_assert_unreachable(); + return 0; +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Stubs of BKE_subdiv_ccg.h + * \{ */ +int BKE_subdiv_ccg_grid_to_face_index(const SubdivCCG *UNUSED(subdiv_ccg), + const int UNUSED(grid_index)) +{ + BLI_assert_unreachable(); + return 0; +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Stubs of BKE_node.h + * \{ */ +void ntreeGPUMaterialNodes(struct bNodeTree *UNUSED(localtree), + struct GPUMaterial *UNUSED(mat), + bool *UNUSED(has_surface_output), + bool *UNUSED(has_volume_output)) +{ + BLI_assert_unreachable(); +} + +struct bNodeTree *ntreeLocalize(struct bNodeTree *UNUSED(ntree)) +{ + BLI_assert_unreachable(); + return nullptr; +} + +void ntreeFreeLocalTree(struct bNodeTree *UNUSED(ntree)) +{ + BLI_assert_unreachable(); +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Stubs of bmesh.h + * \{ */ +void BM_face_as_array_vert_tri(BMFace *UNUSED(f), BMVert *UNUSED(r_verts[3])) +{ + BLI_assert_unreachable(); +} +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Stubs of DRW_engine.h + * \{ */ +void DRW_deferred_shader_remove(struct GPUMaterial *UNUSED(mat)) +{ + BLI_assert_unreachable(); +} + +/** \} */ +} diff --git a/source/blender/gpu/intern/gpu_shader_builtin.c b/source/blender/gpu/intern/gpu_shader_builtin.c index 24ca691be78..cae7383f885 100644 --- a/source/blender/gpu/intern/gpu_shader_builtin.c +++ b/source/blender/gpu/intern/gpu_shader_builtin.c @@ -41,6 +41,9 @@ #include "GPU_texture.h" #include "GPU_uniform_buffer.h" +/* TODO(jbakker): Need a better way to retrieve create_infos. */ +#include "gpu_shader_create_info_private.hh" + /* Adjust these constants as needed. */ #define MAX_DEFINE_LENGTH 256 #define MAX_EXT_DEFINE_LENGTH 512 @@ -145,106 +148,68 @@ typedef struct { const char *frag; /** Optional. */ const char *defs; + + const char *create_info; + const char *clipped_create_info; } GPUShaderStages; static const GPUShaderStages builtin_shader_stages[GPU_SHADER_BUILTIN_LEN] = { [GPU_SHADER_TEXT] = { .name = "GPU_SHADER_TEXT", - .vert = datatoc_gpu_shader_text_vert_glsl, - .frag = datatoc_gpu_shader_text_frag_glsl, + .create_info = "gpu_shader_text", }, [GPU_SHADER_KEYFRAME_SHAPE] = { .name = "GPU_SHADER_KEYFRAME_SHAPE", - .vert = datatoc_gpu_shader_keyframe_shape_vert_glsl, - .frag = datatoc_gpu_shader_keyframe_shape_frag_glsl, + .create_info = "gpu_shader_keyframe_shape", }, [GPU_SHADER_SIMPLE_LIGHTING] = { .name = "GPU_SHADER_SIMPLE_LIGHTING", - .vert = datatoc_gpu_shader_3D_normal_vert_glsl, - .frag = datatoc_gpu_shader_simple_lighting_frag_glsl, + .create_info = "gpu_shader_simple_lighting", }, [GPU_SHADER_3D_IMAGE_MODULATE_ALPHA] = { - .vert = datatoc_gpu_shader_3D_image_vert_glsl, - .frag = datatoc_gpu_shader_image_modulate_alpha_frag_glsl, + .name = "GPU_SHADER_3D_IMAGE_MODULATE_ALPHA", + .create_info = "gpu_shader_3D_image_modulate_alpha", }, [GPU_SHADER_2D_CHECKER] = { .name = "GPU_SHADER_2D_CHECKER", - .vert = datatoc_gpu_shader_2D_vert_glsl, - .frag = datatoc_gpu_shader_checker_frag_glsl, + .create_info = "gpu_shader_2D_checker", }, [GPU_SHADER_2D_DIAG_STRIPES] = { .name = "GPU_SHADER_2D_DIAG_STRIPES", - .vert = datatoc_gpu_shader_2D_vert_glsl, - .frag = datatoc_gpu_shader_diag_stripes_frag_glsl, + .create_info = "gpu_shader_2D_diag_stripes", }, - [GPU_SHADER_2D_UNIFORM_COLOR] = - { - .name = "GPU_SHADER_2D_UNIFORM_COLOR", - .vert = datatoc_gpu_shader_2D_vert_glsl, - .frag = datatoc_gpu_shader_uniform_color_frag_glsl, - }, - [GPU_SHADER_2D_FLAT_COLOR] = - { - .name = "GPU_SHADER_2D_FLAT_COLOR", - .vert = datatoc_gpu_shader_2D_flat_color_vert_glsl, - .frag = datatoc_gpu_shader_flat_color_frag_glsl, - }, - [GPU_SHADER_2D_SMOOTH_COLOR] = - { - .name = "GPU_SHADER_2D_SMOOTH_COLOR", - .vert = datatoc_gpu_shader_2D_smooth_color_vert_glsl, - .frag = datatoc_gpu_shader_2D_smooth_color_frag_glsl, - }, - [GPU_SHADER_2D_IMAGE_OVERLAYS_MERGE] = - { - .name = "GPU_SHADER_2D_IMAGE_OVERLAYS_MERGE", - .vert = datatoc_gpu_shader_2D_image_vert_glsl, - .frag = datatoc_gpu_shader_image_overlays_merge_frag_glsl, - }, + [GPU_SHADER_2D_UNIFORM_COLOR] = {.name = "GPU_SHADER_2D_UNIFORM_COLOR", + .create_info = "gpu_shader_2D_uniform_color"}, + [GPU_SHADER_2D_FLAT_COLOR] = {.name = "GPU_SHADER_2D_FLAT_COLOR", + .create_info = "gpu_shader_2D_flat_color"}, + [GPU_SHADER_2D_SMOOTH_COLOR] = {.name = "GPU_SHADER_2D_SMOOTH_COLOR", + .create_info = "gpu_shader_2D_smooth_color"}, + [GPU_SHADER_2D_IMAGE_OVERLAYS_MERGE] = {.name = "GPU_SHADER_2D_IMAGE_OVERLAYS_MERGE", + .create_info = "gpu_shader_2D_image_overlays_merge"}, [GPU_SHADER_2D_IMAGE_OVERLAYS_STEREO_MERGE] = - { - .name = "GPU_SHADER_2D_IMAGE_OVERLAYS_STEREO_MERGE", - .vert = datatoc_gpu_shader_2D_vert_glsl, - .frag = datatoc_gpu_shader_image_overlays_stereo_merge_frag_glsl, - }, - [GPU_SHADER_2D_IMAGE] = - { - .name = "GPU_SHADER_2D_IMAGE", - .vert = datatoc_gpu_shader_2D_image_vert_glsl, - .frag = datatoc_gpu_shader_image_frag_glsl, - }, - [GPU_SHADER_2D_IMAGE_COLOR] = - { - .name = "GPU_SHADER_2D_IMAGE_COLOR", - .vert = datatoc_gpu_shader_2D_image_vert_glsl, - .frag = datatoc_gpu_shader_image_color_frag_glsl, - }, - [GPU_SHADER_2D_IMAGE_DESATURATE_COLOR] = - { - .name = "GPU_SHADER_2D_IMAGE_DESATURATE_COLOR", - .vert = datatoc_gpu_shader_2D_image_vert_glsl, - .frag = datatoc_gpu_shader_image_desaturate_frag_glsl, - }, + {.name = "GPU_SHADER_2D_IMAGE_OVERLAYS_STEREO_MERGE", + .create_info = "gpu_shader_2D_image_overlays_stereo_merge"}, + [GPU_SHADER_2D_IMAGE] = {.name = "GPU_SHADER_2D_IMAGE", .create_info = "gpu_shader_2D_image"}, + [GPU_SHADER_2D_IMAGE_COLOR] = {.name = "GPU_SHADER_2D_IMAGE_COLOR", + .create_info = "gpu_shader_2D_image_color"}, + [GPU_SHADER_2D_IMAGE_DESATURATE_COLOR] = {.name = "GPU_SHADER_2D_IMAGE_DESATURATE_COLOR", + .create_info = + "gpu_shader_2D_image_desaturate_color"}, [GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR] = { .name = "GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR", - .vert = datatoc_gpu_shader_2D_image_vert_glsl, - .frag = datatoc_gpu_shader_image_shuffle_color_frag_glsl, - }, - [GPU_SHADER_2D_IMAGE_RECT_COLOR] = - { - .name = "GPU_SHADER_2D_IMAGE_RECT_COLOR", - .vert = datatoc_gpu_shader_2D_image_rect_vert_glsl, - .frag = datatoc_gpu_shader_image_color_frag_glsl, + .create_info = "gpu_shader_2D_image_shuffle_color", }, + [GPU_SHADER_2D_IMAGE_RECT_COLOR] = {.name = "GPU_SHADER_2D_IMAGE_RECT_COLOR", + .create_info = "gpu_shader_2D_image_rect_color"}, [GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR] = { .name = "GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR", @@ -255,27 +220,18 @@ static const GPUShaderStages builtin_shader_stages[GPU_SHADER_BUILTIN_LEN] = { [GPU_SHADER_3D_UNIFORM_COLOR] = { .name = "GPU_SHADER_3D_UNIFORM_COLOR", - .vert = datatoc_gpu_shader_3D_vert_glsl, - .frag = datatoc_gpu_shader_uniform_color_frag_glsl, - }, - [GPU_SHADER_3D_FLAT_COLOR] = - { - .name = "GPU_SHADER_3D_FLAT_COLOR", - .vert = datatoc_gpu_shader_3D_flat_color_vert_glsl, - .frag = datatoc_gpu_shader_flat_color_frag_glsl, - }, - [GPU_SHADER_3D_SMOOTH_COLOR] = - { - .name = "GPU_SHADER_3D_SMOOTH_COLOR", - .vert = datatoc_gpu_shader_3D_smooth_color_vert_glsl, - .frag = datatoc_gpu_shader_3D_smooth_color_frag_glsl, - }, - [GPU_SHADER_3D_DEPTH_ONLY] = - { - .name = "GPU_SHADER_3D_DEPTH_ONLY", - .vert = datatoc_gpu_shader_3D_vert_glsl, - .frag = datatoc_gpu_shader_depth_only_frag_glsl, - }, + .create_info = "gpu_shader_3D_uniform_color", + .clipped_create_info = "gpu_shader_3D_uniform_color_clipped", + }, + [GPU_SHADER_3D_FLAT_COLOR] = {.name = "GPU_SHADER_3D_FLAT_COLOR", + .create_info = "gpu_shader_3D_flat_color", + .clipped_create_info = "gpu_shader_3D_flat_color_clipped"}, + [GPU_SHADER_3D_SMOOTH_COLOR] = {.name = "GPU_SHADER_3D_SMOOTH_COLOR", + .create_info = "gpu_shader_3D_smooth_color", + .clipped_create_info = "gpu_shader_3D_smooth_color_clipped"}, + [GPU_SHADER_3D_DEPTH_ONLY] = {.name = "GPU_SHADER_3D_DEPTH_ONLY", + .create_info = "gpu_shader_3D_depth_only", + .clipped_create_info = "gpu_shader_3D_depth_only_clipped"}, [GPU_SHADER_3D_CLIPPED_UNIFORM_COLOR] = { .name = "GPU_SHADER_3D_CLIPPED_UNIFORM_COLOR", @@ -333,33 +289,23 @@ static const GPUShaderStages builtin_shader_stages[GPU_SHADER_BUILTIN_LEN] = { [GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA] = { .name = "GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA", - .vert = datatoc_gpu_shader_2D_point_uniform_size_aa_vert_glsl, - .frag = datatoc_gpu_shader_point_uniform_color_aa_frag_glsl, + .create_info = "gpu_shader_2D_point_uniform_size_uniform_color_aa", }, [GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA] = { .name = "GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA", - .vert = datatoc_gpu_shader_2D_point_uniform_size_outline_aa_vert_glsl, - .frag = datatoc_gpu_shader_point_uniform_color_outline_aa_frag_glsl, + .create_info = "gpu_shader_2D_point_uniform_size_uniform_color_outline_aa", }, [GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR] = - { - .name = "GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR", - .vert = datatoc_gpu_shader_3D_point_fixed_size_varying_color_vert_glsl, - .frag = datatoc_gpu_shader_point_varying_color_frag_glsl, - }, + {.name = "GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR", + .create_info = "gpu_shader_3D_point_fixed_size_varying_color"}, [GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR] = - { - .name = "GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR", - .vert = datatoc_gpu_shader_3D_point_varying_size_varying_color_vert_glsl, - .frag = datatoc_gpu_shader_point_varying_color_frag_glsl, - }, + {.name = "GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR", + .create_info = "gpu_shader_3D_point_varying_size_varying_color"}, [GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA] = - { - .name = "GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA", - .vert = datatoc_gpu_shader_3D_point_uniform_size_aa_vert_glsl, - .frag = datatoc_gpu_shader_point_uniform_color_aa_frag_glsl, - }, + {.name = "GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA", + .create_info = "gpu_shader_3D_point_uniform_size_uniform_color_aa", + .clipped_create_info = "gpu_shader_3D_point_uniform_size_uniform_color_aa_clipped"}, [GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE] = { @@ -369,12 +315,8 @@ static const GPUShaderStages builtin_shader_stages[GPU_SHADER_BUILTIN_LEN] = { .defs = "#define UNIFORM_SCALE\n", }, - [GPU_SHADER_2D_AREA_BORDERS] = - { - .name = "GPU_SHADER_2D_AREA_BORDERS", - .vert = datatoc_gpu_shader_2D_area_borders_vert_glsl, - .frag = datatoc_gpu_shader_2D_area_borders_frag_glsl, - }, + [GPU_SHADER_2D_AREA_BORDERS] = {.name = "GPU_SHADER_2D_AREA_BORDERS", + .create_info = "gpu_shader_2D_area_borders"}, [GPU_SHADER_2D_WIDGET_BASE] = { .name = "GPU_SHADER_2D_WIDGET_BASE", @@ -394,27 +336,14 @@ static const GPUShaderStages builtin_shader_stages[GPU_SHADER_BUILTIN_LEN] = { .vert = datatoc_gpu_shader_2D_widget_shadow_vert_glsl, .frag = datatoc_gpu_shader_2D_widget_shadow_frag_glsl, }, - [GPU_SHADER_2D_NODELINK] = - { - .name = "GPU_SHADER_2D_NODELINK", - .vert = datatoc_gpu_shader_2D_nodelink_vert_glsl, - .frag = datatoc_gpu_shader_2D_nodelink_frag_glsl, - }, - [GPU_SHADER_2D_NODELINK_INST] = - { - .name = "GPU_SHADER_2D_NODELINK_INST", - .vert = datatoc_gpu_shader_2D_nodelink_vert_glsl, - .frag = datatoc_gpu_shader_2D_nodelink_frag_glsl, - .defs = "#define USE_INSTANCE\n", - }, + [GPU_SHADER_2D_NODELINK] = {.name = "GPU_SHADER_2D_NODELINK", + .create_info = "gpu_shader_2D_nodelink"}, - [GPU_SHADER_GPENCIL_STROKE] = - { - .name = "GPU_SHADER_GPENCIL_STROKE", - .vert = datatoc_gpu_shader_gpencil_stroke_vert_glsl, - .geom = datatoc_gpu_shader_gpencil_stroke_geom_glsl, - .frag = datatoc_gpu_shader_gpencil_stroke_frag_glsl, - }, + [GPU_SHADER_2D_NODELINK_INST] = {.name = "GPU_SHADER_2D_NODELINK_INST", + .create_info = "gpu_shader_2D_nodelink_inst"}, + + [GPU_SHADER_GPENCIL_STROKE] = {.name = "GPU_SHADER_GPENCIL_STROKE", + .create_info = "gpu_shader_gpencil_stroke"}, }; GPUShader *GPU_shader_get_builtin_shader_with_config(eGPUBuiltinShader shader, @@ -429,14 +358,20 @@ GPUShader *GPU_shader_get_builtin_shader_with_config(eGPUBuiltinShader shader, /* common case */ if (sh_cfg == GPU_SHADER_CFG_DEFAULT) { - *sh_p = GPU_shader_create_from_arrays_named( - stages->name, - { - .vert = (const char *[]){stages->vert, NULL}, - .geom = (const char *[]){stages->geom, NULL}, - .frag = (const char *[]){datatoc_gpu_shader_colorspace_lib_glsl, stages->frag, NULL}, - .defs = (const char *[]){stages->defs, NULL}, - }); + if (stages->create_info != NULL) { + *sh_p = GPU_shader_create_from_info(gpu_shader_create_info_get(stages->create_info)); + } + else { + *sh_p = GPU_shader_create_from_arrays_named( + stages->name, + { + .vert = (const char *[]){stages->vert, NULL}, + .geom = (const char *[]){stages->geom, NULL}, + .frag = + (const char *[]){datatoc_gpu_shader_colorspace_lib_glsl, stages->frag, NULL}, + .defs = (const char *[]){stages->defs, NULL}, + }); + } } else if (sh_cfg == GPU_SHADER_CFG_CLIPPED) { /* Remove eventually, for now ensure support for each shader has been added. */ @@ -448,17 +383,24 @@ GPUShader *GPU_shader_get_builtin_shader_with_config(eGPUBuiltinShader shader, GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA, GPU_SHADER_3D_FLAT_COLOR, GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR)); - const char *world_clip_lib = datatoc_gpu_shader_cfg_world_clip_lib_glsl; - const char *world_clip_def = "#define USE_WORLD_CLIP_PLANES\n"; /* In rare cases geometry shaders calculate clipping themselves. */ - *sh_p = GPU_shader_create_from_arrays_named( - stages->name, - { - .vert = (const char *[]){world_clip_lib, stages->vert, NULL}, - .geom = (const char *[]){stages->geom ? world_clip_lib : NULL, stages->geom, NULL}, - .frag = (const char *[]){datatoc_gpu_shader_colorspace_lib_glsl, stages->frag, NULL}, - .defs = (const char *[]){world_clip_def, stages->defs, NULL}, - }); + if (stages->clipped_create_info != NULL) { + *sh_p = GPU_shader_create_from_info( + gpu_shader_create_info_get(stages->clipped_create_info)); + } + else { + const char *world_clip_lib = datatoc_gpu_shader_cfg_world_clip_lib_glsl; + const char *world_clip_def = "#define USE_WORLD_CLIP_PLANES\n"; + *sh_p = GPU_shader_create_from_arrays_named( + stages->name, + { + .vert = (const char *[]){world_clip_lib, stages->vert, NULL}, + .geom = (const char *[]){stages->geom ? world_clip_lib : NULL, stages->geom, NULL}, + .frag = + (const char *[]){datatoc_gpu_shader_colorspace_lib_glsl, stages->frag, NULL}, + .defs = (const char *[]){world_clip_def, stages->defs, NULL}, + }); + } } else { BLI_assert(0); diff --git a/source/blender/gpu/intern/gpu_shader_create_info.cc b/source/blender/gpu/intern/gpu_shader_create_info.cc new file mode 100644 index 00000000000..e4e5034026f --- /dev/null +++ b/source/blender/gpu/intern/gpu_shader_create_info.cc @@ -0,0 +1,173 @@ +/* + * 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 + * + * Descriptior type used to define shader structure, resources and interfaces. + */ + +#include "BLI_map.hh" +#include "BLI_set.hh" +#include "BLI_string_ref.hh" + +#include "GPU_shader.h" +#include "GPU_texture.h" + +#include "gpu_shader_create_info.hh" +#include "gpu_shader_create_info_private.hh" + +#undef GPU_SHADER_INTERFACE_INFO +#undef GPU_SHADER_CREATE_INFO + +namespace blender::gpu::shader { + +using CreateInfoDictionnary = Map<StringRef, ShaderCreateInfo *>; +using InterfaceDictionnary = Map<StringRef, StageInterfaceInfo *>; + +static CreateInfoDictionnary *g_create_infos = nullptr; +static InterfaceDictionnary *g_interfaces = nullptr; + +void ShaderCreateInfo::finalize() +{ + if (finalized_) { + return; + } + finalized_ = true; + + for (auto &info_name : additional_infos_) { + const ShaderCreateInfo &info = *reinterpret_cast<const ShaderCreateInfo *>( + gpu_shader_create_info_get(info_name.c_str())); + + /* Recursive. */ + const_cast<ShaderCreateInfo &>(info).finalize(); + + interface_names_size_ += info.interface_names_size_; + + vertex_inputs_.extend(info.vertex_inputs_); + fragment_outputs_.extend(info.fragment_outputs_); + vertex_out_interfaces_.extend(info.vertex_out_interfaces_); + geometry_out_interfaces_.extend(info.geometry_out_interfaces_); + + push_constants_.extend(info.push_constants_); + defines_.extend(info.defines_); + + batch_resources_.extend(info.batch_resources_); + pass_resources_.extend(info.pass_resources_); + typedef_sources_.extend(info.typedef_sources_); + + if (info.local_group_size_[0] != 0) { + BLI_assert(local_group_size_[0] == 0); + for (int i = 0; i < 3; i++) { + local_group_size_[i] = info.local_group_size_[i]; + } + } + if (!info.vertex_source_.is_empty()) { + BLI_assert(vertex_source_.is_empty()); + vertex_source_ = info.vertex_source_; + } + if (!info.geometry_source_.is_empty()) { + BLI_assert(geometry_source_.is_empty()); + geometry_source_ = info.geometry_source_; + } + if (!info.fragment_source_.is_empty()) { + BLI_assert(fragment_source_.is_empty()); + fragment_source_ = info.fragment_source_; + } + if (!info.compute_source_.is_empty()) { + BLI_assert(compute_source_.is_empty()); + compute_source_ = info.compute_source_; + } + + do_static_compilation_ = do_static_compilation_ || info.do_static_compilation_; + } +} + +} // namespace blender::gpu::shader + +using namespace blender::gpu::shader; + +void gpu_shader_create_info_init() +{ + g_create_infos = new CreateInfoDictionnary(); + g_interfaces = new InterfaceDictionnary(); + +#define GPU_SHADER_INTERFACE_INFO(_interface, _inst_name) \ + auto *ptr_##_interface = new StageInterfaceInfo(#_interface, _inst_name); \ + auto &_interface = *ptr_##_interface; \ + g_interfaces->add_new(#_interface, ptr_##_interface); \ + _interface + +#define GPU_SHADER_CREATE_INFO(_info) \ + auto *ptr_##_info = new ShaderCreateInfo(#_info); \ + auto &_info = *ptr_##_info; \ + g_create_infos->add_new(#_info, ptr_##_info); \ + _info + +/* Declare, register and construct the infos. */ +#include "gpu_shader_create_info_list.hh" + +/* Baked shader data appended to create infos. */ +/* TODO(jbakker): should call a function with a callback. so we could switch implementations. We + * cannot compile bf_gpu twice.*/ +#ifdef GPU_RUNTIME +# include "gpu_shader_baked.hh" +#endif + + /* TEST */ + // gpu_shader_create_info_compile_all(); +} + +void gpu_shader_create_info_exit() +{ + for (auto *value : g_create_infos->values()) { + delete value; + } + delete g_create_infos; + + for (auto *value : g_interfaces->values()) { + delete value; + } + delete g_interfaces; +} + +bool gpu_shader_create_info_compile_all() +{ + for (ShaderCreateInfo *info : g_create_infos->values()) { + if (info->do_static_compilation_) { + // printf("Compiling %s: ... \n", info->name_.c_str()); + GPUShader *shader = GPU_shader_create_from_info( + reinterpret_cast<const GPUShaderCreateInfo *>(info)); + if (shader == nullptr) { + printf("Compilation %s Failed\n", info->name_.c_str()); + return false; + } + GPU_shader_free(shader); + // printf("Success\n"); + } + } + return true; +} + +/* Runtime create infos are not registered in the dictionnary and cannot be searched. */ +const GPUShaderCreateInfo *gpu_shader_create_info_get(const char *info_name) +{ + ShaderCreateInfo *info = g_create_infos->lookup(info_name); + return reinterpret_cast<const GPUShaderCreateInfo *>(info); +} diff --git a/source/blender/gpu/intern/gpu_shader_create_info.hh b/source/blender/gpu/intern/gpu_shader_create_info.hh new file mode 100644 index 00000000000..ced7f7039e7 --- /dev/null +++ b/source/blender/gpu/intern/gpu_shader_create_info.hh @@ -0,0 +1,595 @@ +/* + * 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 + * + * Descriptior type used to define shader structure, resources and interfaces. + * + * Some rule of thumb: + * - Do not include anything else than this file in each info file. + */ + +#pragma once + +#include "BLI_string_ref.hh" +#include "BLI_vector.hh" +#include "GPU_texture.h" + +namespace blender::gpu::shader { + +#ifndef GPU_SHADER_CREATE_INFO +/* Helps intelisense / auto-completion. */ +# define GPU_SHADER_INTERFACE_INFO(_interface, _inst_name) \ + StageInterfaceInfo _interface(#_interface, _inst_name); \ + _interface +# define GPU_SHADER_CREATE_INFO(_info) \ + ShaderCreateInfo _info(#_info); \ + _info +#endif + +enum class Type { + FLOAT = 0, + VEC2, + VEC3, + VEC4, + MAT3, + MAT4, + UINT, + UVEC2, + UVEC3, + UVEC4, + INT, + IVEC2, + IVEC3, + IVEC4, + BOOL, +}; + +enum class BuiltinBits { + /** Allow getting barycentic coordinates inside the fragment shader. NOTE: emulated on OpenGL. */ + BARYCENTRIC_COORD = (1 << 0), + FRAG_COORD = (1 << 2), + FRONT_FACING = (1 << 4), + GLOBAL_INVOCATION_ID = (1 << 5), + INSTANCE_ID = (1 << 6), + LAYER = (1 << 7), + LOCAL_INVOCATION_ID = (1 << 8), + LOCAL_INVOCATION_INDEX = (1 << 9), + NUM_WORK_GROUP = (1 << 10), + POINT_COORD = (1 << 11), + POINT_SIZE = (1 << 12), + PRIMITIVE_ID = (1 << 13), + VERTEX_ID = (1 << 14), + WORK_GROUP_ID = (1 << 15), + WORK_GROUP_SIZE = (1 << 16), +}; +ENUM_OPERATORS(BuiltinBits, BuiltinBits::WORK_GROUP_SIZE); + +/* Samplers & images. */ +enum class ImageType { + /** Color samplers/image. */ + FLOAT_BUFFER = 0, + FLOAT_1D, + FLOAT_1D_ARRAY, + FLOAT_2D, + FLOAT_2D_ARRAY, + FLOAT_3D, + FLOAT_CUBE, + FLOAT_CUBE_ARRAY, + INT_BUFFER, + INT_1D, + INT_1D_ARRAY, + INT_2D, + INT_2D_ARRAY, + INT_3D, + INT_CUBE, + INT_CUBE_ARRAY, + UINT_BUFFER, + UINT_1D, + UINT_1D_ARRAY, + UINT_2D, + UINT_2D_ARRAY, + UINT_3D, + UINT_CUBE, + UINT_CUBE_ARRAY, + /** Depth samplers (not supported as image). */ + SHADOW_2D, + SHADOW_2D_ARRAY, + SHADOW_CUBE, + SHADOW_CUBE_ARRAY, + DEPTH_2D, + DEPTH_2D_ARRAY, + DEPTH_CUBE, + DEPTH_CUBE_ARRAY, +}; + +/* Storage qualifiers. */ +enum class Qualifier { + RESTRICT = (1 << 0), + READ_ONLY = (1 << 1), + WRITE_ONLY = (1 << 2), + QUALIFIER_MAX = (WRITE_ONLY << 1) - 1, +}; +ENUM_OPERATORS(Qualifier, Qualifier::QUALIFIER_MAX); + +enum class Frequency { + BATCH = 0, + PASS, +}; + +/* Dual Source Blending Index. */ +enum class DualBlend { + NONE = 0, + SRC_0, + SRC_1, +}; + +/* Interpolation qualifiers. */ +enum class Interpolation { + SMOOTH = 0, + FLAT, + NO_PERSPECTIVE, +}; + +/** Input layout for geometry shader. */ +enum class InputLayout { + POINTS = 0, + LINES, + LINES_ADJACENCY, + TRIANGLES, + TRIANGLES_ADJACENCY, +}; + +/** Output layout for geometry shader. */ +enum class OutputLayout { + POINTS = 0, + LINE_STRIP, + TRIANGLE_STRIP, +}; + +struct StageInterfaceInfo { + struct InOut { + Interpolation interp; + Type type; + StringRefNull name; + }; + + StringRefNull name; + /** Name of the instance of the block (used to access). + * Can be empty string (i.e: "") only if not using geometry shader. */ + StringRefNull instance_name; + /** List of all members of the interface. */ + Vector<InOut> inouts; + + StageInterfaceInfo(const char *name_, const char *instance_name_) + : name(name_), instance_name(instance_name_){}; + ~StageInterfaceInfo(){}; + + using Self = StageInterfaceInfo; + + Self &smooth(Type type, StringRefNull _name) + { + inouts.append({Interpolation::SMOOTH, type, _name}); + return *(Self *)this; + } + + Self &flat(Type type, StringRefNull _name) + { + inouts.append({Interpolation::FLAT, type, _name}); + return *(Self *)this; + } + + Self &no_perspective(Type type, StringRefNull _name) + { + inouts.append({Interpolation::NO_PERSPECTIVE, type, _name}); + return *(Self *)this; + } +}; + +/** + * @brief Describe inputs & outputs, stage interfaces, resources and sources of a shader. + * If all data is correctly provided, this is all that is needed to create and compile + * a GPUShader. + * + * IMPORTANT: All strings are references only. Make sure all the strings used by a + * ShaderCreateInfo are not freed until it is consumed or deleted. + */ +struct ShaderCreateInfo { + /** Shader name for debugging. */ + StringRefNull name_; + /** True if the shader is static and can be precompiled at compile time. */ + bool do_static_compilation_ = false; + /** If true, all additionaly linked create info will be merged into this one. */ + bool finalized_ = false; + /** + * Maximum length of all the resource names including each null terminator. + * Only for names used by gpu::ShaderInterface. + */ + size_t interface_names_size_ = 0; + /** Only for compute shaders. */ + int local_group_size_[3] = {0, 0, 0}; + + struct VertIn { + int index; + Type type; + StringRefNull name; + }; + Vector<VertIn> vertex_inputs_; + + struct GeomIn { + InputLayout layout; + }; + GeomIn geom_in_; + + struct GeomOut { + OutputLayout layout; + int max_vertices; + }; + GeomOut geom_out_; + + struct FragOut { + int index; + Type type; + DualBlend blend; + StringRefNull name; + }; + Vector<FragOut> fragment_outputs_; + + struct Sampler { + ImageType type; + eGPUSamplerState sampler; + StringRefNull name; + }; + + struct Image { + eGPUTextureFormat format; + ImageType type; + Qualifier qualifiers; + StringRefNull name; + }; + + struct UniformBuf { + StringRefNull type_name; + StringRefNull name; + }; + + struct StorageBuf { + Qualifier qualifiers; + StringRefNull type_name; + StringRefNull name; + }; + + struct Resource { + enum BindType { + UNIFORM_BUFFER = 0, + STORAGE_BUFFER, + SAMPLER, + IMAGE, + }; + + BindType bind_type; + int slot; + union { + Sampler sampler; + Image image; + UniformBuf uniformbuf; + StorageBuf storagebuf; + }; + + Resource(BindType type, int _slot) : bind_type(type), slot(_slot){}; + }; + /** + * Resources are grouped by frequency of change. + * Pass resources are meants to be valid for the whole pass. + * Batch resources can be changed in a more granular manner (per object/material). + * Mis-usage will only produce suboptimal performance. + */ + Vector<Resource> pass_resources_, batch_resources_; + + Vector<StageInterfaceInfo *> vertex_out_interfaces_; + Vector<StageInterfaceInfo *> geometry_out_interfaces_; + + struct PushConst { + int index; + Type type; + StringRefNull name; + int array_size; + }; + + Vector<PushConst> push_constants_; + + /* Sources for resources type definitions. */ + Vector<StringRefNull> typedef_sources_; + + StringRefNull vertex_source_, geometry_source_, fragment_source_, compute_source_; + + Vector<std::array<StringRefNull, 2>> defines_; + /** + * Name of other infos to recursively merge with this one. + * No data slot must overlap otherwise we throw an error. + */ + Vector<StringRefNull> additional_infos_; + + public: + ShaderCreateInfo(const char *name) : name_(name){}; + ~ShaderCreateInfo(){}; + + using Self = ShaderCreateInfo; + + /* -------------------------------------------------------------------- */ + /** \name Shaders in/outs (fixed function pipeline config) + * \{ */ + + Self &vertex_in(int slot, Type type, StringRefNull name) + { + vertex_inputs_.append({slot, type, name}); + interface_names_size_ += name.size() + 1; + return *(Self *)this; + } + + Self &vertex_out(StageInterfaceInfo &interface) + { + vertex_out_interfaces_.append(&interface); + return *(Self *)this; + } + + Self &geometry_layout(InputLayout layout_in, OutputLayout layout_out, int max_vertices) + { + geom_in_.layout = layout_in; + geom_out_.layout = layout_out; + geom_out_.max_vertices = max_vertices; + return *(Self *)this; + } + + /* Only needed if geometry shader is enabled. */ + Self &geometry_out(StageInterfaceInfo &interface) + { + geometry_out_interfaces_.append(&interface); + return *(Self *)this; + } + + Self &fragment_out(int slot, Type type, StringRefNull name, DualBlend blend = DualBlend::NONE) + { + fragment_outputs_.append({slot, type, blend, name}); + return *(Self *)this; + } + + /** \} */ + + /* -------------------------------------------------------------------- */ + /** \name Resources bindings points + * \{ */ + + Self &uniform_buf(int slot, + StringRefNull type_name, + StringRefNull name, + Frequency freq = Frequency::PASS) + { + Resource res(Resource::BindType::UNIFORM_BUFFER, slot); + res.uniformbuf.name = name; + res.uniformbuf.type_name = type_name; + ((freq == Frequency::PASS) ? pass_resources_ : batch_resources_).append(res); + interface_names_size_ += name.size() + 1; + return *(Self *)this; + } + + Self &storage_buf(int slot, + Qualifier qualifiers, + StringRefNull type_name, + StringRefNull name, + Frequency freq = Frequency::PASS) + { + Resource res(Resource::BindType::STORAGE_BUFFER, slot); + res.storagebuf.qualifiers = qualifiers; + res.storagebuf.type_name = type_name; + res.storagebuf.name = name; + ((freq == Frequency::PASS) ? pass_resources_ : batch_resources_).append(res); + interface_names_size_ += name.size() + 1; + return *(Self *)this; + } + + Self &image(int slot, + eGPUTextureFormat format, + Qualifier qualifiers, + ImageType type, + StringRefNull name, + Frequency freq = Frequency::PASS) + { + Resource res(Resource::BindType::IMAGE, slot); + res.image.format = format; + res.image.qualifiers = qualifiers; + res.image.type = type; + res.image.name = name; + ((freq == Frequency::PASS) ? pass_resources_ : batch_resources_).append(res); + interface_names_size_ += name.size() + 1; + return *(Self *)this; + } + + Self &sampler(int slot, + ImageType type, + StringRefNull name, + Frequency freq = Frequency::PASS, + eGPUSamplerState sampler = (eGPUSamplerState)-1) + { + Resource res(Resource::BindType::SAMPLER, slot); + res.sampler.type = type; + res.sampler.name = name; + res.sampler.sampler = sampler; + ((freq == Frequency::PASS) ? pass_resources_ : batch_resources_).append(res); + interface_names_size_ += name.size() + 1; + return *(Self *)this; + } + + /** \} */ + + /* -------------------------------------------------------------------- */ + /** \name Shader Source + * \{ */ + + Self &vertex_source(StringRefNull filename) + { + vertex_source_ = filename; + return *(Self *)this; + } + + Self &geometry_source(StringRefNull filename) + { + geometry_source_ = filename; + return *(Self *)this; + } + + Self &fragment_source(StringRefNull filename) + { + fragment_source_ = filename; + return *(Self *)this; + } + + Self &compute_source(StringRefNull filename) + { + compute_source_ = filename; + return *(Self *)this; + } + + /** \} */ + + /* -------------------------------------------------------------------- */ + /** \name Push constants + * + * Data managed by GPUShader. Can be set through uniform functions. Must be less than 128bytes. + * One slot represents 4bytes. Each element needs to have enough empty space left after it. + * example: + * [0] = PUSH_CONSTANT(MAT4, "ModelMatrix"), + * ---- 16 slots occupied by ModelMatrix ---- + * [16] = PUSH_CONSTANT(VEC4, "color"), + * ---- 4 slots occupied by color ---- + * [20] = PUSH_CONSTANT(BOOL, "srgbToggle"), + * The maximum slot is 31. + * \{ */ + + Self &push_constant(int slot, Type type, StringRefNull name, int array_size = 0) + { + BLI_assert_msg(name.find("[") == -1, + "Array syntax is forbidden for push constants." + "Use the array_size parameter instead."); + push_constants_.append({slot, type, name, array_size}); + interface_names_size_ += name.size() + 1; + return *(Self *)this; + } + + /** \} */ + + /* -------------------------------------------------------------------- */ + /** \name Compute shaders Local Group Size + * \{ */ + + Self &local_group_size(int x, int y = 1, int z = 1) + { + local_group_size_[0] = x; + local_group_size_[1] = y; + local_group_size_[2] = z; + return *(Self *)this; + } + + /** \} */ + + /* -------------------------------------------------------------------- */ + /** \name Defines + * \{ */ + + Self &define(StringRefNull name, StringRefNull value = "") + { + defines_.append({name, value}); + return *(Self *)this; + } + + /** \} */ + + /* -------------------------------------------------------------------- */ + /** \name Defines + * \{ */ + + Self &do_static_compilation(bool value) + { + do_static_compilation_ = value; + return *(Self *)this; + } + + /** \} */ + + /* -------------------------------------------------------------------- */ + /** \name Additional Create Info + * + * Used to share parts of the infos that are common to many shaders. + * \{ */ + + Self &additional_info(StringRefNull info_name0, + StringRefNull info_name1 = "", + StringRefNull info_name2 = "", + StringRefNull info_name3 = "", + StringRefNull info_name4 = "") + { + additional_infos_.append(info_name0); + if (!info_name1.is_empty()) { + additional_infos_.append(info_name1); + } + if (!info_name2.is_empty()) { + additional_infos_.append(info_name2); + } + if (!info_name3.is_empty()) { + additional_infos_.append(info_name3); + } + if (!info_name4.is_empty()) { + additional_infos_.append(info_name4); + } + return *(Self *)this; + } + + /** \} */ + + /* -------------------------------------------------------------------- */ + /** \name Typedef Sources + * + * Some resource declarations might need some special structure defined. + * Adding a file using typedef_source will include it before the resource + * and interface definitions. + * \{ */ + + Self &typedef_source(StringRefNull filename) + { + typedef_sources_.append(filename); + return *(Self *)this; + } + + /** \} */ + + /* -------------------------------------------------------------------- */ + /** \name Recursive evaluation. + * + * Flatten all dependency so that this descriptor contains all the data from the additional + * descriptors. This avoids tedious traversal in shader source creation. + * \{ */ + + /* WARNING: Recursive. */ + void finalize(); + + /** \} */ +}; + +} // namespace blender::gpu::shader diff --git a/source/blender/gpu/intern/gpu_shader_create_info_private.hh b/source/blender/gpu/intern/gpu_shader_create_info_private.hh new file mode 100644 index 00000000000..7010fa5e3a2 --- /dev/null +++ b/source/blender/gpu/intern/gpu_shader_create_info_private.hh @@ -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 + * + * Descriptior type used to define shader structure, resources and interfaces. + * + * Some rule of thumb: + * - Do not include anything else than this file in each descriptor file. + */ + +#pragma once + +#include "GPU_shader.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void gpu_shader_create_info_init(void); +void gpu_shader_create_info_exit(void); + +bool gpu_shader_create_info_compile_all(void); + +const GPUShaderCreateInfo *gpu_shader_create_info_get(const char *info_name); + +#ifdef __cplusplus +} +#endif diff --git a/source/blender/gpu/intern/gpu_shader_dependency.cc b/source/blender/gpu/intern/gpu_shader_dependency.cc new file mode 100644 index 00000000000..a866e9f1fda --- /dev/null +++ b/source/blender/gpu/intern/gpu_shader_dependency.cc @@ -0,0 +1,197 @@ +/* + * 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 + * + * Shader source dependency builder that make possible to support #include directive inside the + * shader files. + */ + +#include <iostream> + +#include "BLI_map.hh" +#include "BLI_set.hh" +#include "BLI_string_ref.hh" + +#include "gpu_shader_create_info.hh" +#include "gpu_shader_dependency_private.h" + +extern "C" { +#define SHADER_SOURCE(datatoc, filename) extern char datatoc[]; +#include "glsl_draw_source_list.h" +#include "glsl_gpu_source_list.h" +#undef SHADER_SOURCE +} + +namespace blender::gpu { + +using GPUSourceDictionnary = Map<StringRef, struct GPUSource *>; + +struct GPUSource { + StringRefNull filename; + StringRefNull source; + Vector<GPUSource *> dependencies; + bool dependencies_init = false; + shader::BuiltinBits builtins = (shader::BuiltinBits)0; + + GPUSource(const char *file, const char *datatoc) : filename(file), source(datatoc) + { + /* Scan for builtins. */ + /* FIXME: This can trigger false positive caused by disabled #if blocks. */ + /* TODO(fclem): Could be made faster by scanning once. */ + /* TODO(fclem): BARYCENTRIC_COORD. */ + if (source.find("gl_FragCoord", 0)) { + builtins |= shader::BuiltinBits::FRAG_COORD; + } + if (source.find("gl_FrontFacing", 0)) { + builtins |= shader::BuiltinBits::FRONT_FACING; + } + if (source.find("gl_GlobalInvocationID", 0)) { + builtins |= shader::BuiltinBits::GLOBAL_INVOCATION_ID; + } + if (source.find("gl_InstanceID", 0)) { + builtins |= shader::BuiltinBits::INSTANCE_ID; + } + if (source.find("gl_Layer", 0)) { + builtins |= shader::BuiltinBits::LAYER; + } + if (source.find("gl_LocalInvocationID", 0)) { + builtins |= shader::BuiltinBits::LOCAL_INVOCATION_ID; + } + if (source.find("gl_LocalInvocationIndex", 0)) { + builtins |= shader::BuiltinBits::LOCAL_INVOCATION_INDEX; + } + if (source.find("gl_NumWorkGroup", 0)) { + builtins |= shader::BuiltinBits::NUM_WORK_GROUP; + } + if (source.find("gl_PointCoord", 0)) { + builtins |= shader::BuiltinBits::POINT_COORD; + } + if (source.find("gl_PointSize", 0)) { + builtins |= shader::BuiltinBits::POINT_SIZE; + } + if (source.find("gl_PrimitiveID", 0)) { + builtins |= shader::BuiltinBits::PRIMITIVE_ID; + } + if (source.find("gl_VertexID", 0)) { + builtins |= shader::BuiltinBits::VERTEX_ID; + } + if (source.find("gl_WorkGroupID", 0)) { + builtins |= shader::BuiltinBits::WORK_GROUP_ID; + } + if (source.find("gl_WorkGroupSize", 0)) { + builtins |= shader::BuiltinBits::WORK_GROUP_SIZE; + } + }; + + void init_dependencies(const GPUSourceDictionnary &dict) + { + if (dependencies_init) { + return; + } + dependencies_init = true; + int64_t pos = 0; + while (true) { + pos = source.find("pragma BLENDER_REQUIRE(", pos); + if (pos == -1) { + return; + } + int64_t start = source.find("(", pos) + 1; + int64_t end = source.find(")", pos); + if (end == -1) { + /* TODO Use clog. */ + std::cout << "Error: " << filename << " : Malformed BLENDER_REQUIRE: Missing \")\"." + << std::endl; + return; + } + StringRef dependency_name = source.substr(start, end - start); + GPUSource *dependency_source = dict.lookup_default(dependency_name, nullptr); + if (dependency_source == nullptr) { + /* TODO Use clog. */ + std::cout << "Error: " << filename << " : Dependency not found \"" << dependency_name + << "\"." << std::endl; + return; + } + /* Recursive. */ + dependency_source->init_dependencies(dict); + + for (auto *dep : dependency_source->dependencies) { + dependencies.append_non_duplicates(dep); + } + dependencies.append_non_duplicates(dependency_source); + pos++; + }; + } + + /* Returns the final string with all inlcudes done. */ + void build(std::string &str, shader::BuiltinBits &out_builtins) + { + for (auto *dep : dependencies) { + out_builtins |= builtins; + str += dep->source; + } + str += source; + } +}; + +} // namespace blender::gpu + +using namespace blender::gpu; + +static GPUSourceDictionnary *g_sources = nullptr; + +void gpu_shader_dependency_init() +{ + g_sources = new GPUSourceDictionnary(); + +#define SHADER_SOURCE(datatoc, filename) \ + g_sources->add_new(filename, new GPUSource(filename, datatoc)); +#include "glsl_draw_source_list.h" +#include "glsl_gpu_source_list.h" +#undef SHADER_SOURCE + + for (auto *value : g_sources->values()) { + value->init_dependencies(*g_sources); + } +} + +void gpu_shader_dependency_exit() +{ + for (auto *value : g_sources->values()) { + delete value; + } + delete g_sources; +} + +char *gpu_shader_dependency_get_resolved_source(const char *shader_source_name, uint32_t *builtins) +{ + GPUSource *source = g_sources->lookup(shader_source_name); + std::string str; + shader::BuiltinBits out_builtins; + source->build(str, out_builtins); + *builtins |= (uint32_t)out_builtins; + return strdup(str.c_str()); +} + +char *gpu_shader_dependency_get_source(const char *shader_source_name) +{ + GPUSource *src = g_sources->lookup(shader_source_name); + return strdup(src->source.c_str()); +} diff --git a/source/blender/gpu/intern/gpu_shader_dependency_private.h b/source/blender/gpu/intern/gpu_shader_dependency_private.h new file mode 100644 index 00000000000..b129ca74a48 --- /dev/null +++ b/source/blender/gpu/intern/gpu_shader_dependency_private.h @@ -0,0 +1,44 @@ +/* + * 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 + * + * Shader source dependency builder that make possible to support #include directive inside the + * shader files. + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +void gpu_shader_dependency_init(void); + +void gpu_shader_dependency_exit(void); + +/* User must free the resulting string using free. */ +char *gpu_shader_dependency_get_resolved_source(const char *shader_source_name, + uint32_t *builtins); +char *gpu_shader_dependency_get_source(const char *shader_source_name); + +#ifdef __cplusplus +} +#endif diff --git a/source/blender/gpu/intern/gpu_shader_info_baked.cc b/source/blender/gpu/intern/gpu_shader_info_baked.cc new file mode 100644 index 00000000000..00af803f765 --- /dev/null +++ b/source/blender/gpu/intern/gpu_shader_info_baked.cc @@ -0,0 +1,24 @@ +/* + * 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 + * + * Intentionally empty for compiling shader builder. + */
\ No newline at end of file diff --git a/source/blender/gpu/intern/gpu_shader_interface.hh b/source/blender/gpu/intern/gpu_shader_interface.hh index 4bd8f245e2b..735b8fea71d 100644 --- a/source/blender/gpu/intern/gpu_shader_interface.hh +++ b/source/blender/gpu/intern/gpu_shader_interface.hh @@ -34,6 +34,7 @@ #include "BLI_utildefines.h" #include "GPU_shader.h" +#include "gpu_shader_create_info.hh" namespace blender::gpu { @@ -50,6 +51,7 @@ typedef struct ShaderInput { * Base class which is then specialized for each implementation (GL, VK, ...). */ class ShaderInterface { + friend shader::ShaderCreateInfo; /* TODO(fclem): should be protected. */ public: /** Flat array. In this order: Attributes, Ubos, Uniforms. */ @@ -72,6 +74,7 @@ class ShaderInterface { public: ShaderInterface(); + ShaderInterface(const shader::ShaderCreateInfo &info); virtual ~ShaderInterface(); void debug_print(); @@ -129,6 +132,10 @@ class ShaderInterface { static inline const char *builtin_uniform_block_name(GPUUniformBlockBuiltin u); inline uint32_t set_input_name(ShaderInput *input, char *name, uint32_t name_len) const; + inline void copy_input_name(ShaderInput *input, + const StringRefNull &name, + char *name_buffer, + uint32_t &name_buffer_offset) const; /** * Finalize interface construction by sorting the #ShaderInputs for faster lookups. @@ -216,8 +223,12 @@ inline uint32_t ShaderInterface::set_input_name(ShaderInput *input, { /* remove "[0]" from array name */ if (name[name_len - 1] == ']') { - name[name_len - 3] = '\0'; - name_len -= 3; + for (; name_len > 1; name_len--) { + if (name[name_len] == '[') { + name[name_len] = '\0'; + break; + } + } } input->name_offset = (uint32_t)(name - name_buffer_); @@ -225,6 +236,17 @@ inline uint32_t ShaderInterface::set_input_name(ShaderInput *input, return name_len + 1; /* include NULL terminator */ } +inline void ShaderInterface::copy_input_name(ShaderInput *input, + const StringRefNull &name, + char *name_buffer, + uint32_t &name_buffer_offset) const +{ + uint32_t name_len = name.size(); + /* Copy include NULL terminator. */ + memcpy(name_buffer + name_buffer_offset, name.c_str(), name_len + 1); + name_buffer_offset += set_input_name(input, name_buffer + name_buffer_offset, name_len); +} + inline const ShaderInput *ShaderInterface::input_lookup(const ShaderInput *const inputs, const uint inputs_len, const char *name) const diff --git a/source/blender/gpu/intern/gpu_shader_private.hh b/source/blender/gpu/intern/gpu_shader_private.hh index 498dd8f3877..7837af0dcf2 100644 --- a/source/blender/gpu/intern/gpu_shader_private.hh +++ b/source/blender/gpu/intern/gpu_shader_private.hh @@ -24,9 +24,12 @@ #include "BLI_string_ref.hh" #include "GPU_shader.h" +#include "gpu_shader_create_info.hh" #include "gpu_shader_interface.hh" #include "gpu_vertex_buffer_private.hh" +#include <string> + namespace blender { namespace gpu { @@ -53,7 +56,7 @@ class Shader { virtual void geometry_shader_from_glsl(MutableSpan<const char *> sources) = 0; virtual void fragment_shader_from_glsl(MutableSpan<const char *> sources) = 0; virtual void compute_shader_from_glsl(MutableSpan<const char *> sources) = 0; - virtual bool finalize() = 0; + virtual bool finalize(const shader::ShaderCreateInfo *info = nullptr) = 0; virtual void transform_feedback_names_set(Span<const char *> name_list, eGPUShaderTFBType geom_type) = 0; @@ -68,6 +71,13 @@ class Shader { virtual void vertformat_from_shader(GPUVertFormat *) const = 0; + std::string defines_declare(const shader::ShaderCreateInfo &info) const; + virtual std::string resources_declare(const shader::ShaderCreateInfo &info) const = 0; + virtual std::string vertex_interface_declare(const shader::ShaderCreateInfo &info) const = 0; + virtual std::string fragment_interface_declare(const shader::ShaderCreateInfo &info) const = 0; + virtual std::string geometry_interface_declare(const shader::ShaderCreateInfo &info) const = 0; + virtual std::string geometry_layout_declare(const shader::ShaderCreateInfo &info) const = 0; + /* DEPRECATED: Kept only because of BGL API. */ virtual int program_handle_get() const = 0; diff --git a/source/blender/gpu/intern/gpu_shader_shared_utils.h b/source/blender/gpu/intern/gpu_shader_shared_utils.h new file mode 100644 index 00000000000..f2e2aab208c --- /dev/null +++ b/source/blender/gpu/intern/gpu_shader_shared_utils.h @@ -0,0 +1,105 @@ +/* + * 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) 2022 Blender Foundation. + * All rights reserved. + */ + +/** \file + * \ingroup gpu + * + * Glue definition to make shared declaration of struct & functions work in both C / C++ and GLSL. + * We use the same vector and matrix types as Blender C++. Some math functions are defined to use + * the float version to match the GLSL syntax. + * This file can be used for C & C++ code and the syntax used should follow the same rules. + * Some preprocessing is done by the GPU backend to make it GLSL compatible. + * + * IMPORTANT: + * - Don't add trailing comma at the end of the enum. Our custom pre-processor will noy trim it + * for GLSL. + * - Always use `u` suffix for enum values. GLSL do not support implicit cast. + * - Define all values. This is in order to simplify custom pre-processor code. + * - Always use uint32_t as underlying type. + * - Use float suffix by default for float literals to avoid double promotion in C++. + * - Pack one float or int after a vec3/ivec3 to fullfil alligment rules. + * + * NOTE: Due to alignment restriction and buggy drivers, do not try to use mat3 inside structs. + * NOTE: (UBO only) Do not use arrays of float. They are padded to arrays of vec4 and are not worth + * it. This does not apply to SSBO. + * + * IMPORTANT: Do not forget to align mat4, vec3 and vec4 to 16 bytes, and vec2 to 8 bytes. + * + * NOTE: You can use bool type using bool1 a int boolean type matching the GLSL type. + */ + +#ifdef GPU_SHADER +# define BLI_STATIC_ASSERT_ALIGN(type_, align_) +# define BLI_STATIC_ASSERT_SIZE(type_, size_) +# define static +# define inline +# define cosf cos +# define sinf sin +# define tanf tan +# define acosf acos +# define asinf asin +# define atanf atan +# define floorf floor +# define ceilf ceil +# define sqrtf sqrt + +# define float2 vec2 +# define float3 vec3 +# define float4 vec4 +# define float4x4 mat4 +# define int2 ivec2 +# define int3 ivec3 +# define int4 ivec4 +# define uint2 uvec2 +# define uint3 uvec3 +# define uint4 uvec4 +# define bool1 bool +# define bool2 bvec2 +# define bool3 bvec3 +# define bool4 bvec4 + +#else /* C */ +# pragma once + +# include "BLI_assert.h" + +# ifdef __cplusplus +# include "BLI_float4x4.hh" +# include "BLI_float4x4.hh" +# else +typedef float float2[2]; +typedef float float3[3]; +typedef float float4[4]; +typedef float float4x4[4][4]; +# endif +typedef int int2[2]; +typedef int int3[2]; +typedef int int4[4]; +typedef uint uint2[2]; +typedef uint uint3[3]; +typedef uint uint4[4]; +typedef int int2[2]; +typedef int int3[2]; +typedef int int4[4]; +typedef int bool1; +typedef int bool2[2]; +typedef int bool3[2]; +typedef int bool4[4]; + +#endif
\ No newline at end of file |