Refactor: Grease Pencil: Port usage of DRW_texture_create_* to Texture

Removes call to legacy API.

Pull Request: https://projects.blender.org/blender/blender/pulls/131636
This commit is contained in:
Clément Foucault
2024-12-10 11:21:32 +01:00
committed by Clément Foucault
parent 89102b3b5d
commit a005efd680
4 changed files with 74 additions and 77 deletions

View File

@@ -17,7 +17,6 @@ void GPENCIL_antialiasing_init(GPENCIL_Data *vedata)
GPENCIL_Instance *inst = vedata->instance;
GPENCIL_PrivateData *pd = vedata->stl->pd;
GPENCIL_FramebufferList *fbl = vedata->fbl;
GPENCIL_TextureList *txl = vedata->txl;
const float *size = DRW_viewport_size_get();
const float *sizeinv = DRW_viewport_invert_size_get();
@@ -39,23 +38,20 @@ void GPENCIL_antialiasing_init(GPENCIL_Data *vedata)
return;
}
eGPUTextureUsage usage = GPU_TEXTURE_USAGE_SHADER_READ | GPU_TEXTURE_USAGE_ATTACHMENT;
if (!inst->smaa_search_tx.is_valid()) {
eGPUTextureUsage usage = GPU_TEXTURE_USAGE_SHADER_READ;
inst->smaa_search_tx.ensure_2d(GPU_R8, int2(SEARCHTEX_WIDTH, SEARCHTEX_HEIGHT), usage);
GPU_texture_update(inst->smaa_search_tx, GPU_DATA_UBYTE, searchTexBytes);
if (txl->smaa_search_tx == nullptr) {
inst->smaa_area_tx.ensure_2d(GPU_RG8, int2(AREATEX_WIDTH, AREATEX_HEIGHT), usage);
GPU_texture_update(inst->smaa_area_tx, GPU_DATA_UBYTE, areaTexBytes);
txl->smaa_search_tx = GPU_texture_create_2d(
"smaa_search", SEARCHTEX_WIDTH, SEARCHTEX_HEIGHT, 1, GPU_R8, usage, nullptr);
GPU_texture_update(txl->smaa_search_tx, GPU_DATA_UBYTE, searchTexBytes);
txl->smaa_area_tx = GPU_texture_create_2d(
"smaa_area", AREATEX_WIDTH, AREATEX_HEIGHT, 1, GPU_RG8, usage, nullptr);
GPU_texture_update(txl->smaa_area_tx, GPU_DATA_UBYTE, areaTexBytes);
GPU_texture_filter_mode(txl->smaa_search_tx, true);
GPU_texture_filter_mode(txl->smaa_area_tx, true);
GPU_texture_filter_mode(inst->smaa_search_tx, true);
GPU_texture_filter_mode(inst->smaa_area_tx, true);
}
{
eGPUTextureUsage usage = GPU_TEXTURE_USAGE_SHADER_READ | GPU_TEXTURE_USAGE_ATTACHMENT;
pd->smaa_edge_tx = DRW_texture_pool_query_2d_ex(
size[0], size[1], GPU_RG8, usage, &draw_engine_gpencil_type);
pd->smaa_weight_tx = DRW_texture_pool_query_2d_ex(
@@ -94,8 +90,8 @@ void GPENCIL_antialiasing_init(GPENCIL_Data *vedata)
pass.state_set(DRW_STATE_WRITE_COLOR);
pass.shader_set(GPENCIL_shader_antialiasing(1));
pass.bind_texture("edgesTex", pd->smaa_edge_tx);
pass.bind_texture("areaTex", txl->smaa_area_tx);
pass.bind_texture("searchTex", txl->smaa_search_tx);
pass.bind_texture("areaTex", inst->smaa_area_tx);
pass.bind_texture("searchTex", inst->smaa_search_tx);
pass.push_constant("viewportMetrics", metrics);
pass.clear_color(float4(0.0f));
pass.draw_procedural(GPU_PRIM_TRIS, 1, 3);

View File

@@ -52,6 +52,7 @@ struct GPENCIL_tVfx;
struct GPENCIL_tLayer;
using PassSimple = blender::draw::PassSimple;
using Texture = blender::draw::Texture;
/* NOTE: These do not preserve the PassSimple memory across frames.
* If that becomes a bottleneck, these containers can be improved. */
using GPENCIL_tVfx_Pool = blender::draw::detail::SubPassVector<GPENCIL_tVfx>;
@@ -166,22 +167,6 @@ typedef struct GPENCIL_FramebufferList {
struct GPUFrameBuffer *smaa_weight_fb;
} GPENCIL_FramebufferList;
typedef struct GPENCIL_TextureList {
/* Dummy texture to avoid errors cause by empty sampler. */
struct GPUTexture *dummy_texture;
struct GPUTexture *dummy_depth;
/* Snapshot for smoother drawing. */
struct GPUTexture *snapshot_depth_tx;
struct GPUTexture *snapshot_color_tx;
struct GPUTexture *snapshot_reveal_tx;
/* Textures used by Antialiasing. */
struct GPUTexture *smaa_area_tx;
struct GPUTexture *smaa_search_tx;
/* Textures used during render. Containing underlying rendered scene. */
struct GPUTexture *render_depth_tx;
struct GPUTexture *render_color_tx;
} GPENCIL_TextureList;
struct GPENCIL_Instance {
PassSimple smaa_edge_ps = {"smaa_edge"};
PassSimple smaa_weight_ps = {"smaa_weight"};
@@ -192,12 +177,26 @@ struct GPENCIL_Instance {
PassSimple mask_invert_ps = {"mask_invert_ps"};
float4x4 object_bound_mat;
/* Dummy texture to avoid errors cause by empty sampler. */
Texture dummy_texture = {"dummy_texture"};
Texture dummy_depth = {"dummy_depth"};
/* Textures used during render. Containing underlying rendered scene. */
Texture render_depth_tx = {"render_depth_tx"};
Texture render_color_tx = {"render_color_tx"};
/* Snapshot for smoother drawing. */
Texture snapshot_depth_tx = {"snapshot_depth_tx"};
Texture snapshot_color_tx = {"snapshot_color_tx"};
Texture snapshot_reveal_tx = {"snapshot_reveal_tx"};
/* Textures used by Antialiasing. */
Texture smaa_area_tx = {"smaa_area_tx"};
Texture smaa_search_tx = {"smaa_search_tx"};
};
struct GPENCIL_Data {
void *engine_type; /* Required */
struct GPENCIL_FramebufferList *fbl;
struct GPENCIL_TextureList *txl;
DRWViewportEmptyList *txl;
DRWViewportEmptyList *psl;
struct GPENCIL_StorageList *stl;
struct GPENCIL_Instance *instance;

View File

@@ -54,7 +54,6 @@ void GPENCIL_engine_init(void *ved)
{
GPENCIL_Data *vedata = (GPENCIL_Data *)ved;
GPENCIL_StorageList *stl = vedata->stl;
GPENCIL_TextureList *txl = vedata->txl;
GPENCIL_FramebufferList *fbl = vedata->fbl;
DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
@@ -64,19 +63,21 @@ void GPENCIL_engine_init(void *ved)
if (vedata->instance == nullptr) {
vedata->instance = new GPENCIL_Instance();
}
GPENCIL_Instance &inst = *vedata->instance;
if (!stl->pd) {
stl->pd = static_cast<GPENCIL_PrivateData *>(
MEM_callocN(sizeof(GPENCIL_PrivateData), "GPENCIL_PrivateData"));
}
if (txl->dummy_texture == nullptr) {
if (!inst.dummy_texture.is_valid()) {
const float pixels[1][4] = {{1.0f, 0.0f, 1.0f, 1.0f}};
txl->dummy_texture = DRW_texture_create_2d(1, 1, GPU_RGBA8, DRW_TEX_WRAP, (float *)pixels);
inst.dummy_texture.ensure_2d(GPU_RGBA8, int2(1), GPU_TEXTURE_USAGE_SHADER_READ, &pixels[0][0]);
}
if (txl->dummy_depth == nullptr) {
if (!inst.dummy_depth.is_valid()) {
const float pixels[1] = {1.0f};
txl->dummy_depth = DRW_texture_create_2d(1, 1, GPU_DEPTH_COMPONENT24, DRW_TEX_WRAP, pixels);
inst.dummy_depth.ensure_2d(
GPU_DEPTH_COMPONENT24, int2(1), GPU_TEXTURE_USAGE_SHADER_READ, &pixels[0]);
}
GPENCIL_ViewLayerData *vldata = GPENCIL_view_layer_data_ensure();
@@ -106,12 +107,12 @@ void GPENCIL_engine_init(void *ved)
stl->pd->tobjects_infront.last = nullptr;
stl->pd->sbuffer_tobjects.first = nullptr;
stl->pd->sbuffer_tobjects.last = nullptr;
stl->pd->dummy_tx = txl->dummy_texture;
stl->pd->dummy_depth = txl->dummy_depth;
stl->pd->dummy_tx = inst.dummy_texture;
stl->pd->dummy_depth = inst.dummy_depth;
stl->pd->draw_wireframe = (v3d && v3d->shading.type == OB_WIRE);
stl->pd->scene_depth_tx = dtxl->depth;
stl->pd->scene_fb = dfbl->default_fb;
stl->pd->is_render = txl->render_depth_tx || (v3d && v3d->shading.type == OB_RENDER);
stl->pd->is_render = inst.render_depth_tx.is_valid() || (v3d && v3d->shading.type == OB_RENDER);
stl->pd->is_viewport = (v3d != nullptr);
stl->pd->global_light_pool = gpencil_light_pool_add(stl->pd);
stl->pd->shadeless_light_pool = gpencil_light_pool_add(stl->pd);
@@ -156,8 +157,8 @@ void GPENCIL_engine_init(void *ved)
stl->pd->use_lighting = (v3d && v3d->shading.type > OB_SOLID) || stl->pd->is_render;
stl->pd->use_lights = use_scene_lights;
if (txl->render_depth_tx != nullptr) {
stl->pd->scene_depth_tx = txl->render_depth_tx;
if (inst.render_depth_tx.is_valid()) {
stl->pd->scene_depth_tx = inst.render_depth_tx;
stl->pd->scene_fb = fbl->render_fb;
}
@@ -191,7 +192,6 @@ void GPENCIL_cache_init(void *ved)
using namespace blender::draw;
GPENCIL_Data *vedata = (GPENCIL_Data *)ved;
GPENCIL_Instance *inst = vedata->instance;
GPENCIL_TextureList *txl = vedata->txl;
GPENCIL_FramebufferList *fbl = vedata->fbl;
GPENCIL_PrivateData *pd = vedata->stl->pd;
@@ -253,28 +253,27 @@ void GPENCIL_cache_init(void *ved)
}
if (pd->do_fast_drawing) {
pd->snapshot_buffer_dirty = (txl->snapshot_color_tx == nullptr);
pd->snapshot_buffer_dirty = !inst->snapshot_depth_tx.is_valid();
const float *size = DRW_viewport_size_get();
DRW_texture_ensure_2d(
&txl->snapshot_depth_tx, size[0], size[1], GPU_DEPTH24_STENCIL8, DRWTextureFlag(0));
DRW_texture_ensure_2d(
&txl->snapshot_color_tx, size[0], size[1], GPU_R11F_G11F_B10F, DRWTextureFlag(0));
DRW_texture_ensure_2d(
&txl->snapshot_reveal_tx, size[0], size[1], GPU_R11F_G11F_B10F, DRWTextureFlag(0));
eGPUTextureUsage usage = GPU_TEXTURE_USAGE_ATTACHMENT;
inst->snapshot_depth_tx.ensure_2d(GPU_DEPTH24_STENCIL8, int2(size), usage);
inst->snapshot_color_tx.ensure_2d(GPU_R11F_G11F_B10F, int2(size), usage);
inst->snapshot_reveal_tx.ensure_2d(GPU_R11F_G11F_B10F, int2(size), usage);
GPU_framebuffer_ensure_config(&fbl->snapshot_fb,
{
GPU_ATTACHMENT_TEXTURE(txl->snapshot_depth_tx),
GPU_ATTACHMENT_TEXTURE(txl->snapshot_color_tx),
GPU_ATTACHMENT_TEXTURE(txl->snapshot_reveal_tx),
GPU_ATTACHMENT_TEXTURE(inst->snapshot_depth_tx),
GPU_ATTACHMENT_TEXTURE(inst->snapshot_color_tx),
GPU_ATTACHMENT_TEXTURE(inst->snapshot_reveal_tx),
});
}
else {
/* Free unneeded buffers. */
GPU_FRAMEBUFFER_FREE_SAFE(fbl->snapshot_fb);
DRW_TEXTURE_FREE_SAFE(txl->snapshot_depth_tx);
DRW_TEXTURE_FREE_SAFE(txl->snapshot_color_tx);
DRW_TEXTURE_FREE_SAFE(txl->snapshot_reveal_tx);
inst->snapshot_depth_tx.free();
inst->snapshot_color_tx.free();
inst->snapshot_reveal_tx.free();
}
{
@@ -376,10 +375,7 @@ static bool use_layer_in_render(const GreasePencil &grease_pencil,
}
static GPENCIL_tObject *grease_pencil_object_cache_populate(
GPENCIL_PrivateData *pd,
GPENCIL_TextureList *txl,
Object *ob,
blender::draw::ResourceHandle res_handle)
GPENCIL_PrivateData *pd, Object *ob, blender::draw::ResourceHandle res_handle)
{
using namespace blender;
using namespace blender::ed::greasepencil;
@@ -399,8 +395,8 @@ static GPENCIL_tObject *grease_pencil_object_cache_populate(
int mat_ofs = 0;
GPENCIL_MaterialPool *matpool = gpencil_material_pool_create(pd, ob, &mat_ofs, is_vertex_mode);
GPUTexture *tex_fill = txl->dummy_texture;
GPUTexture *tex_stroke = txl->dummy_texture;
GPUTexture *tex_fill = pd->dummy_tx;
GPUTexture *tex_stroke = pd->dummy_tx;
blender::gpu::Batch *iter_geom = nullptr;
PassSimple *last_pass = nullptr;
@@ -613,7 +609,6 @@ void GPENCIL_cache_populate(void *ved, Object *ob)
{
GPENCIL_Data *vedata = (GPENCIL_Data *)ved;
GPENCIL_PrivateData *pd = vedata->stl->pd;
GPENCIL_TextureList *txl = vedata->txl;
/* object must be visible */
if (!(DRW_object_visibility_in_active_context(ob) & OB_VISIBLE_SELF)) {
@@ -625,7 +620,7 @@ void GPENCIL_cache_populate(void *ved, Object *ob)
blender::draw::ObjectRef ob_ref = DRW_object_ref_get(ob);
blender::draw::ResourceHandle res_handle = manager->unique_handle(ob_ref);
GPENCIL_tObject *tgp_ob = grease_pencil_object_cache_populate(pd, txl, ob, res_handle);
GPENCIL_tObject *tgp_ob = grease_pencil_object_cache_populate(pd, ob, res_handle);
gpencil_vfx_cache_populate(
vedata,
ob,

View File

@@ -28,7 +28,11 @@ void GPENCIL_render_init(GPENCIL_Data *vedata,
const rcti *rect)
{
GPENCIL_FramebufferList *fbl = vedata->fbl;
GPENCIL_TextureList *txl = vedata->txl;
if (vedata->instance == nullptr) {
vedata->instance = new GPENCIL_Instance();
}
GPENCIL_Instance &inst = *vedata->instance;
Scene *scene = DEG_get_evaluated_scene(depsgraph);
const float *viewport_size = DRW_viewport_size_get();
@@ -90,25 +94,28 @@ void GPENCIL_render_init(GPENCIL_Data *vedata,
/* FIXME(fclem): we have a precision loss in the depth buffer because of this re-upload.
* Find where it comes from! */
/* In multi view render the textures can be reused. */
if (txl->render_depth_tx && !do_clear_z) {
GPU_texture_update(txl->render_depth_tx, GPU_DATA_FLOAT, pix_z);
if (inst.render_depth_tx.is_valid() && !do_clear_z) {
GPU_texture_update(inst.render_depth_tx, GPU_DATA_FLOAT, pix_z);
}
else {
txl->render_depth_tx = DRW_texture_create_2d(
size[0], size[1], GPU_DEPTH_COMPONENT24, DRWTextureFlag(0), do_region ? nullptr : pix_z);
eGPUTextureUsage usage = GPU_TEXTURE_USAGE_SHADER_READ | GPU_TEXTURE_USAGE_ATTACHMENT |
GPU_TEXTURE_USAGE_HOST_READ;
inst.render_depth_tx.ensure_2d(
GPU_DEPTH_COMPONENT24, int2(size), usage, do_region ? nullptr : pix_z);
}
if (txl->render_color_tx && !do_clear_col) {
GPU_texture_update(txl->render_color_tx, GPU_DATA_FLOAT, pix_col);
if (inst.render_color_tx.is_valid() && !do_clear_col) {
GPU_texture_update(inst.render_color_tx, GPU_DATA_FLOAT, pix_col);
}
else {
txl->render_color_tx = DRW_texture_create_2d(
size[0], size[1], GPU_RGBA16F, DRWTextureFlag(0), do_region ? nullptr : pix_col);
eGPUTextureUsage usage = GPU_TEXTURE_USAGE_SHADER_READ | GPU_TEXTURE_USAGE_ATTACHMENT |
GPU_TEXTURE_USAGE_HOST_READ;
inst.render_color_tx.ensure_2d(GPU_RGBA16F, int2(size), usage, do_region ? nullptr : pix_col);
}
GPU_framebuffer_ensure_config(&fbl->render_fb,
{
GPU_ATTACHMENT_TEXTURE(txl->render_depth_tx),
GPU_ATTACHMENT_TEXTURE(txl->render_color_tx),
GPU_ATTACHMENT_TEXTURE(inst.render_depth_tx),
GPU_ATTACHMENT_TEXTURE(inst.render_color_tx),
});
if (do_clear_z || do_clear_col) {
@@ -129,10 +136,10 @@ void GPENCIL_render_init(GPENCIL_Data *vedata,
int w = BLI_rcti_size_x(rect);
int h = BLI_rcti_size_y(rect);
if (pix_col) {
GPU_texture_update_sub(txl->render_color_tx, GPU_DATA_FLOAT, pix_col, x, y, 0, w, h, 0);
GPU_texture_update_sub(inst.render_color_tx, GPU_DATA_FLOAT, pix_col, x, y, 0, w, h, 0);
}
if (pix_z) {
GPU_texture_update_sub(txl->render_depth_tx, GPU_DATA_FLOAT, pix_z, x, y, 0, w, h, 0);
GPU_texture_update_sub(inst.render_depth_tx, GPU_DATA_FLOAT, pix_z, x, y, 0, w, h, 0);
}
}