Fix #140934: Compositor: Support Grease Pencil pass in viewport

This patch adds support for viewport compositor for grease pencil pass.
It also comes with an option for viewing grease pencil pass directly
inside the viewport without compositor.

Pull Request: https://projects.blender.org/blender/blender/pulls/140960
This commit is contained in:
YimingWu
2025-07-01 16:39:28 +02:00
committed by YimingWu
parent caf659880f
commit d6fcb6d1fe
3 changed files with 25 additions and 2 deletions

View File

@@ -117,6 +117,12 @@ void Instance::antialiasing_draw(Manager &manager)
GPU_framebuffer_bind(this->scene_fb);
manager.submit(this->smaa_resolve_ps);
if (this->use_separate_pass) {
GPU_framebuffer_bind(this->gpencil_pass_fb);
GPU_framebuffer_clear(this->gpencil_pass_fb, GPU_COLOR_BIT, float4(0, 0, 0, 0), 0, 0);
manager.submit(this->smaa_resolve_ps);
}
}
static float erfinv_approx(const float x)

View File

@@ -8,6 +8,7 @@
#include "DRW_engine.hh"
#include "DRW_render.hh"
#include "BKE_compositor.hh"
#include "BKE_context.hh"
#include "BKE_curves.hh"
#include "BKE_gpencil_geom_legacy.h"
@@ -171,8 +172,13 @@ void Instance::begin_sync()
this->use_layer_fb = false;
this->use_object_fb = false;
this->use_mask_fb = false;
/* Always use high precision for render. */
this->use_signed_fb = !this->is_viewport;
this->use_separate_pass =
draw_ctx->is_viewport_compositor_enabled() ?
bke::compositor::get_used_passes(*scene, view_layer).contains("GreasePencil") :
false;
/* Always use high precision for render and viewport compositor (viewport compositor only takes
* RGBA16F/32F formats). */
this->use_signed_fb = this->use_separate_pass || !this->is_viewport;
if (draw_ctx->v3d) {
const bool hide_overlay = ((draw_ctx->v3d->flag2 & V3D_HIDE_OVERLAYS) != 0);
@@ -663,6 +669,13 @@ void Instance::acquire_resources()
GPU_ATTACHMENT_TEXTURE(this->mask_color_tx),
GPU_ATTACHMENT_TEXTURE(this->mask_tx));
}
if (this->use_separate_pass) {
const int2 size = int2(draw_ctx->viewport_size_get());
draw::TextureFromPool &output_pass_texture = DRW_viewport_pass_texture_get("GreasePencil");
output_pass_texture.acquire(size, GPU_RGBA16F);
this->gpencil_pass_fb.ensure(GPU_ATTACHMENT_NONE, GPU_ATTACHMENT_TEXTURE(output_pass_texture));
}
}
void Instance::release_resources()

View File

@@ -165,6 +165,7 @@ struct Instance final : public DrawEngine {
Framebuffer render_fb = {"render_fb"};
Framebuffer gpencil_fb = {"gpencil_fb"};
Framebuffer gpencil_pass_fb = {"gpencil_pass_fb"};
Framebuffer snapshot_fb = {"snapshot_fb"};
Framebuffer layer_fb = {"layer_fb"};
Framebuffer object_fb = {"object_fb"};
@@ -275,6 +276,9 @@ struct Instance final : public DrawEngine {
bool use_layer_fb;
bool use_object_fb;
bool use_mask_fb;
/* If viewport compositor is active, we need to render grease pencil onto another additional
* pass. */
bool use_separate_pass;
/* Some blend mode needs to add negative values.
* This is only supported if target texture is signed. */
bool use_signed_fb;