From 98a5658239bb61a701f20309c741ab895ed92b7a Mon Sep 17 00:00:00 2001 From: Jeroen Bakker Date: Wed, 24 Nov 2021 11:23:44 +0100 Subject: Cleanup: Renamed DefaultDrawingMode ImageSpaceDrawingMode. Explains more what it does, not how it is used. --- source/blender/draw/CMakeLists.txt | 3 + .../draw/engines/image/image_drawing_mode.hh | 147 --------------------- .../image/image_drawing_mode_image_space.hh | 147 +++++++++++++++++++++ source/blender/draw/engines/image/image_engine.cc | 10 +- 4 files changed, 158 insertions(+), 149 deletions(-) delete mode 100644 source/blender/draw/engines/image/image_drawing_mode.hh create mode 100644 source/blender/draw/engines/image/image_drawing_mode_image_space.hh (limited to 'source/blender/draw') diff --git a/source/blender/draw/CMakeLists.txt b/source/blender/draw/CMakeLists.txt index b8ca22d33d3..30a3b8087c0 100644 --- a/source/blender/draw/CMakeLists.txt +++ b/source/blender/draw/CMakeLists.txt @@ -223,6 +223,9 @@ set(SRC engines/external/external_engine.h engines/image/image_engine.h engines/image/image_private.hh + engines/image/image_drawing_mode_image_space.hh + engines/image/image_space_image.hh + engines/image/image_space_node.hh engines/workbench/workbench_engine.h engines/workbench/workbench_private.h engines/select/select_engine.h diff --git a/source/blender/draw/engines/image/image_drawing_mode.hh b/source/blender/draw/engines/image/image_drawing_mode.hh deleted file mode 100644 index 528e47b7a1e..00000000000 --- a/source/blender/draw/engines/image/image_drawing_mode.hh +++ /dev/null @@ -1,147 +0,0 @@ -/* - * 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. - * - * Copyright 2021, Blender Foundation. - */ - -/** \file - * \ingroup draw_engine - */ - -#pragma once - -#include "image_private.hh" - -namespace blender::draw::image_engine { - -class DefaultDrawingMode : public AbstractDrawingMode { - private: - DRWPass *create_image_pass() const - { - /* Write depth is needed for background overlay rendering. Near depth is used for - * transparency checker and Far depth is used for indicating the image size. */ - DRWState state = static_cast(DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | - DRW_STATE_DEPTH_ALWAYS | DRW_STATE_BLEND_ALPHA_PREMUL); - return DRW_pass_create("Image", state); - } - - void add_to_shgroup(AbstractSpaceAccessor *space, - DRWShadingGroup *grp, - const Image *image, - const ImBuf *image_buffer) const - { - float image_mat[4][4]; - - const DRWContextState *draw_ctx = DRW_context_state_get(); - const ARegion *region = draw_ctx->region; - space->get_image_mat(image_buffer, region, image_mat); - - GPUBatch *geom = DRW_cache_quad_get(); - - const float translate_x = image_mat[3][0]; - const float translate_y = image_mat[3][1]; - LISTBASE_FOREACH (ImageTile *, tile, &image->tiles) { - const int tile_x = ((tile->tile_number - 1001) % 10); - const int tile_y = ((tile->tile_number - 1001) / 10); - image_mat[3][0] = (float)tile_x + translate_x; - image_mat[3][1] = (float)tile_y + translate_y; - DRW_shgroup_call_obmat(grp, geom, image_mat); - } - } - - public: - void cache_init(IMAGE_Data *vedata) const override - { - IMAGE_PassList *psl = vedata->psl; - - psl->image_pass = create_image_pass(); - } - - void cache_image(AbstractSpaceAccessor *space, - IMAGE_Data *vedata, - Image *image, - ImageUser *iuser, - ImBuf *image_buffer) const override - { - IMAGE_PassList *psl = vedata->psl; - IMAGE_StorageList *stl = vedata->stl; - IMAGE_PrivateData *pd = stl->pd; - - GPUTexture *tex_tile_data = nullptr; - space->get_gpu_textures( - image, iuser, image_buffer, &pd->texture, &pd->owns_texture, &tex_tile_data); - if (pd->texture == nullptr) { - return; - } - const bool is_tiled_texture = tex_tile_data != nullptr; - - ShaderParameters sh_params; - sh_params.use_premul_alpha = BKE_image_has_gpu_texture_premultiplied_alpha(image, - image_buffer); - const DRWContextState *draw_ctx = DRW_context_state_get(); - const Scene *scene = draw_ctx->scene; - if (scene->camera && scene->camera->type == OB_CAMERA) { - Camera *camera = static_cast(scene->camera->data); - copy_v2_fl2(sh_params.far_near, camera->clip_end, camera->clip_start); - } - space->get_shader_parameters(sh_params, image_buffer, is_tiled_texture); - - GPUShader *shader = IMAGE_shader_image_get(is_tiled_texture); - DRWShadingGroup *shgrp = DRW_shgroup_create(shader, psl->image_pass); - if (is_tiled_texture) { - DRW_shgroup_uniform_texture_ex(shgrp, "imageTileArray", pd->texture, GPU_SAMPLER_DEFAULT); - DRW_shgroup_uniform_texture(shgrp, "imageTileData", tex_tile_data); - } - else { - DRW_shgroup_uniform_texture_ex(shgrp, "imageTexture", pd->texture, GPU_SAMPLER_DEFAULT); - } - DRW_shgroup_uniform_vec2_copy(shgrp, "farNearDistances", sh_params.far_near); - DRW_shgroup_uniform_vec4_copy(shgrp, "color", ShaderParameters::color); - DRW_shgroup_uniform_vec4_copy(shgrp, "shuffle", sh_params.shuffle); - DRW_shgroup_uniform_int_copy(shgrp, "drawFlags", sh_params.flags); - DRW_shgroup_uniform_bool_copy(shgrp, "imgPremultiplied", sh_params.use_premul_alpha); - - add_to_shgroup(space, shgrp, image, image_buffer); - } - - void draw_finish(IMAGE_Data *vedata) const override - { - IMAGE_StorageList *stl = vedata->stl; - IMAGE_PrivateData *pd = stl->pd; - - if (pd->texture && pd->owns_texture) { - GPU_texture_free(pd->texture); - pd->owns_texture = false; - } - pd->texture = nullptr; - } - - void draw_scene(IMAGE_Data *vedata) const override - { - IMAGE_PassList *psl = vedata->psl; - IMAGE_PrivateData *pd = vedata->stl->pd; - - DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get(); - GPU_framebuffer_bind(dfbl->default_fb); - static float clear_col[4] = {0.0f, 0.0f, 0.0f, 0.0f}; - GPU_framebuffer_clear_color_depth(dfbl->default_fb, clear_col, 1.0); - - DRW_view_set_active(pd->view); - DRW_draw_pass(psl->image_pass); - DRW_view_set_active(nullptr); - } -}; - -} // namespace blender::draw::image_engine diff --git a/source/blender/draw/engines/image/image_drawing_mode_image_space.hh b/source/blender/draw/engines/image/image_drawing_mode_image_space.hh new file mode 100644 index 00000000000..26f4bc28106 --- /dev/null +++ b/source/blender/draw/engines/image/image_drawing_mode_image_space.hh @@ -0,0 +1,147 @@ +/* + * 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. + * + * Copyright 2021, Blender Foundation. + */ + +/** \file + * \ingroup draw_engine + */ + +#pragma once + +#include "image_private.hh" + +namespace blender::draw::image_engine { + +class ImageSpaceDrawingMode : public AbstractDrawingMode { + private: + DRWPass *create_image_pass() const + { + /* Write depth is needed for background overlay rendering. Near depth is used for + * transparency checker and Far depth is used for indicating the image size. */ + DRWState state = static_cast(DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | + DRW_STATE_DEPTH_ALWAYS | DRW_STATE_BLEND_ALPHA_PREMUL); + return DRW_pass_create("Image", state); + } + + void add_to_shgroup(AbstractSpaceAccessor *space, + DRWShadingGroup *grp, + const Image *image, + const ImBuf *image_buffer) const + { + float image_mat[4][4]; + + const DRWContextState *draw_ctx = DRW_context_state_get(); + const ARegion *region = draw_ctx->region; + space->get_image_mat(image_buffer, region, image_mat); + + GPUBatch *geom = DRW_cache_quad_get(); + + const float translate_x = image_mat[3][0]; + const float translate_y = image_mat[3][1]; + LISTBASE_FOREACH (ImageTile *, tile, &image->tiles) { + const int tile_x = ((tile->tile_number - 1001) % 10); + const int tile_y = ((tile->tile_number - 1001) / 10); + image_mat[3][0] = (float)tile_x + translate_x; + image_mat[3][1] = (float)tile_y + translate_y; + DRW_shgroup_call_obmat(grp, geom, image_mat); + } + } + + public: + void cache_init(IMAGE_Data *vedata) const override + { + IMAGE_PassList *psl = vedata->psl; + + psl->image_pass = create_image_pass(); + } + + void cache_image(AbstractSpaceAccessor *space, + IMAGE_Data *vedata, + Image *image, + ImageUser *iuser, + ImBuf *image_buffer) const override + { + IMAGE_PassList *psl = vedata->psl; + IMAGE_StorageList *stl = vedata->stl; + IMAGE_PrivateData *pd = stl->pd; + + GPUTexture *tex_tile_data = nullptr; + space->get_gpu_textures( + image, iuser, image_buffer, &pd->texture, &pd->owns_texture, &tex_tile_data); + if (pd->texture == nullptr) { + return; + } + const bool is_tiled_texture = tex_tile_data != nullptr; + + ShaderParameters sh_params; + sh_params.use_premul_alpha = BKE_image_has_gpu_texture_premultiplied_alpha(image, + image_buffer); + const DRWContextState *draw_ctx = DRW_context_state_get(); + const Scene *scene = draw_ctx->scene; + if (scene->camera && scene->camera->type == OB_CAMERA) { + Camera *camera = static_cast(scene->camera->data); + copy_v2_fl2(sh_params.far_near, camera->clip_end, camera->clip_start); + } + space->get_shader_parameters(sh_params, image_buffer, is_tiled_texture); + + GPUShader *shader = IMAGE_shader_image_get(is_tiled_texture); + DRWShadingGroup *shgrp = DRW_shgroup_create(shader, psl->image_pass); + if (is_tiled_texture) { + DRW_shgroup_uniform_texture_ex(shgrp, "imageTileArray", pd->texture, GPU_SAMPLER_DEFAULT); + DRW_shgroup_uniform_texture(shgrp, "imageTileData", tex_tile_data); + } + else { + DRW_shgroup_uniform_texture_ex(shgrp, "imageTexture", pd->texture, GPU_SAMPLER_DEFAULT); + } + DRW_shgroup_uniform_vec2_copy(shgrp, "farNearDistances", sh_params.far_near); + DRW_shgroup_uniform_vec4_copy(shgrp, "color", ShaderParameters::color); + DRW_shgroup_uniform_vec4_copy(shgrp, "shuffle", sh_params.shuffle); + DRW_shgroup_uniform_int_copy(shgrp, "drawFlags", sh_params.flags); + DRW_shgroup_uniform_bool_copy(shgrp, "imgPremultiplied", sh_params.use_premul_alpha); + + add_to_shgroup(space, shgrp, image, image_buffer); + } + + void draw_finish(IMAGE_Data *vedata) const override + { + IMAGE_StorageList *stl = vedata->stl; + IMAGE_PrivateData *pd = stl->pd; + + if (pd->texture && pd->owns_texture) { + GPU_texture_free(pd->texture); + pd->owns_texture = false; + } + pd->texture = nullptr; + } + + void draw_scene(IMAGE_Data *vedata) const override + { + IMAGE_PassList *psl = vedata->psl; + IMAGE_PrivateData *pd = vedata->stl->pd; + + DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get(); + GPU_framebuffer_bind(dfbl->default_fb); + static float clear_col[4] = {0.0f, 0.0f, 0.0f, 0.0f}; + GPU_framebuffer_clear_color_depth(dfbl->default_fb, clear_col, 1.0); + + DRW_view_set_active(pd->view); + DRW_draw_pass(psl->image_pass); + DRW_view_set_active(nullptr); + } +}; + +} // namespace blender::draw::image_engine diff --git a/source/blender/draw/engines/image/image_engine.cc b/source/blender/draw/engines/image/image_engine.cc index 491fbec978b..37db7246e67 100644 --- a/source/blender/draw/engines/image/image_engine.cc +++ b/source/blender/draw/engines/image/image_engine.cc @@ -41,7 +41,7 @@ #include "GPU_batch.h" -#include "image_drawing_mode.hh" +#include "image_drawing_mode_image_space.hh" #include "image_engine.h" #include "image_private.hh" #include "image_space_image.hh" @@ -63,12 +63,18 @@ static std::unique_ptr space_accessor_from_context( return nullptr; } +template< + /** \brief Drawing mode to use. + * + * Useful during development to switch between drawing implementations. + */ + typename DrawingMode = ImageSpaceDrawingMode> class ImageEngine { private: const DRWContextState *draw_ctx; IMAGE_Data *vedata; std::unique_ptr space; - DefaultDrawingMode drawing_mode; + DrawingMode drawing_mode; public: ImageEngine(const DRWContextState *draw_ctx, IMAGE_Data *vedata) -- cgit v1.2.3