Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeroen Bakker <jeroen@blender.org>2020-09-11 08:59:48 +0300
committerJeroen Bakker <jeroen@blender.org>2020-09-11 09:08:46 +0300
commitd6525e8d133b787655bdb2c2fcef218591a457c3 (patch)
treeb4dbb06b75005deee918d7816c074a5735be930c /source/blender/draw/engines/image
parentd023c4104cb642c839d5868411a3b719f3528421 (diff)
Use DrawManager for Image/UV Editor
This project moves the current UV/Image editor drawing to the draw manager. Why would we do this: **Performance**: Current implementation would draw each texel per time. Multiple texels could be drawn per pixel what would overwrite the previous result. You can notice this when working with large textures. Repeat image drawing made this visible by drawing for a small period of time and stop drawing the rest. Now the rendering is fast and all repeated images are drawn. **Alpha drawing**: Current implementation would draw directly in display space. Giving incorrect results when displaying alpha transparent images. This addresses {T52680}, {T74709}, {T79518} The image editor now can show emission only colors. See {D8234} for examples. **Current Limitations** Using images that are larger than supported by your GPU are resized (eg larger than 16000x16000 are resized to 8k). This leaves some blurring artifacts. It is a low priority to add support back of displaying individual pixels of huge images. There is a design task {T80113} with more detail. **Implementation overview** Introduced an Image Engine in the draw module. this engine is responsible for drawing the texture in the main area of the UV/Image editor. The overlay engine has a edit_uv overlay which is responsible to draw the UV's, shadows and overlays specifically for the UV Image editor. The background + checker pattern is drawn by the overlay_background. The patch will allow us to share overlays between the 3d viewport and UV/Image editor more easily. In most cases we just need to switch the `pos` with the `u` attribute in the vertex shader. The project can be activated in the user preferences as experimental features. In a later commit this will be reversed. Reviewed By: Clément Foucault Differential Revision: https://developer.blender.org/D8234
Diffstat (limited to 'source/blender/draw/engines/image')
-rw-r--r--source/blender/draw/engines/image/image_engine.c304
-rw-r--r--source/blender/draw/engines/image/image_engine.h25
-rw-r--r--source/blender/draw/engines/image/image_private.h69
-rw-r--r--source/blender/draw/engines/image/image_shader.c77
-rw-r--r--source/blender/draw/engines/image/shaders/engine_image_frag.glsl91
-rw-r--r--source/blender/draw/engines/image/shaders/engine_image_vert.glsl31
6 files changed, 597 insertions, 0 deletions
diff --git a/source/blender/draw/engines/image/image_engine.c b/source/blender/draw/engines/image/image_engine.c
new file mode 100644
index 00000000000..9f1278b473b
--- /dev/null
+++ b/source/blender/draw/engines/image/image_engine.c
@@ -0,0 +1,304 @@
+/*
+ * 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 2020, Blender Foundation.
+ */
+
+/** \file
+ * \ingroup draw_editors
+ *
+ * Draw engine to draw the Image/UV editor
+ */
+
+#include "DRW_render.h"
+
+#include "BKE_image.h"
+#include "BKE_object.h"
+
+#include "DNA_camera_types.h"
+
+#include "IMB_imbuf_types.h"
+
+#include "ED_image.h"
+
+#include "GPU_batch.h"
+
+#include "image_engine.h"
+#include "image_private.h"
+
+#define SIMA_DRAW_FLAG_SHOW_ALPHA (1 << 0)
+#define SIMA_DRAW_FLAG_APPLY_ALPHA (1 << 1)
+#define SIMA_DRAW_FLAG_SHUFFLING (1 << 2)
+#define SIMA_DRAW_FLAG_DEPTH (1 << 3)
+#define SIMA_DRAW_FLAG_TILED (1 << 4)
+#define SIMA_DRAW_FLAG_DO_REPEAT (1 << 5)
+
+static void image_cache_image_add(DRWShadingGroup *grp, Image *image)
+{
+ const bool is_tiled_texture = image && image->source == IMA_SRC_TILED;
+ float obmat[4][4];
+ unit_m4(obmat);
+
+ GPUBatch *geom = DRW_cache_quad_get();
+
+ if (is_tiled_texture) {
+ LISTBASE_FOREACH (ImageTile *, tile, &image->tiles) {
+ const int tile_x = ((tile->tile_number - 1001) % 10);
+ const int tile_y = ((tile->tile_number - 1001) / 10);
+ obmat[3][1] = (float)tile_y;
+ obmat[3][0] = (float)tile_x;
+ DRW_shgroup_call_obmat(grp, geom, obmat);
+ }
+ }
+ else {
+ DRW_shgroup_call_obmat(grp, geom, obmat);
+ }
+}
+
+static void image_gpu_texture_get(Image *image,
+ ImageUser *iuser,
+ ImBuf *ibuf,
+ GPUTexture **r_gpu_texture,
+ bool *r_owns_texture,
+ GPUTexture **r_tex_tile_data)
+{
+
+ const DRWContextState *draw_ctx = DRW_context_state_get();
+ SpaceImage *sima = (SpaceImage *)draw_ctx->space_data;
+
+ if (image) {
+ if (BKE_image_is_multilayer(image)) {
+ /* update multiindex and pass for the current eye */
+ BKE_image_multilayer_index(image->rr, &sima->iuser);
+ }
+ else {
+ BKE_image_multiview_index(image, &sima->iuser);
+ }
+
+ if (ibuf) {
+ if (sima->flag & SI_SHOW_ZBUF && (ibuf->zbuf || ibuf->zbuf_float || (ibuf->channels == 1))) {
+ if (ibuf->zbuf) {
+ BLI_assert(!"Integer based depth buffers not supported");
+ }
+ else if (ibuf->zbuf_float) {
+ *r_gpu_texture = GPU_texture_create_2d(
+ __func__, ibuf->x, ibuf->y, 0, GPU_R16F, ibuf->zbuf_float);
+ *r_owns_texture = true;
+ }
+ else if (ibuf->rect_float && ibuf->channels == 1) {
+ *r_gpu_texture = GPU_texture_create_2d(
+ __func__, ibuf->x, ibuf->y, 0, GPU_R16F, ibuf->rect_float);
+ *r_owns_texture = true;
+ }
+ }
+ else if (image->source == IMA_SRC_TILED) {
+ *r_gpu_texture = BKE_image_get_gpu_tiles(image, iuser, ibuf);
+ *r_tex_tile_data = BKE_image_get_gpu_tilemap(image, iuser, NULL);
+ *r_owns_texture = false;
+ }
+ else {
+ *r_gpu_texture = BKE_image_get_gpu_texture(image, iuser, ibuf);
+ *r_owns_texture = false;
+ }
+ }
+ }
+}
+
+static void image_cache_image(IMAGE_Data *vedata, Image *image, ImageUser *iuser, ImBuf *ibuf)
+{
+ IMAGE_PassList *psl = vedata->psl;
+ IMAGE_StorageList *stl = vedata->stl;
+ IMAGE_PrivateData *pd = stl->pd;
+
+ const DRWContextState *draw_ctx = DRW_context_state_get();
+ const Scene *scene = draw_ctx->scene;
+ SpaceImage *sima = (SpaceImage *)draw_ctx->space_data;
+
+ GPUTexture *tex_tile_data = NULL;
+ image_gpu_texture_get(image, iuser, ibuf, &pd->texture, &pd->owns_texture, &tex_tile_data);
+
+ if (pd->texture) {
+ static float color[4] = {1.0f, 1.0f, 1.0f, 1.0f};
+ static float shuffle[4] = {1.0f, 1.0f, 1.0f, 1.0f};
+ static float far_near[2] = {100.0f, 0.0f};
+
+ if (scene->camera && scene->camera->type == OB_CAMERA) {
+ far_near[1] = ((Camera *)scene->camera->data)->clip_start;
+ far_near[0] = ((Camera *)scene->camera->data)->clip_end;
+ }
+
+ const bool use_premul_alpha = image->alpha_mode == IMA_ALPHA_PREMUL;
+ const bool is_tiled_texture = tex_tile_data != NULL;
+ const bool do_repeat = (!is_tiled_texture) && ((sima->flag & SI_DRAW_TILE) != 0);
+ const bool is_zoom_out = sima->zoom < 1.0f;
+
+ /* use interpolation filtering when zooming out */
+ eGPUSamplerState state = 0;
+ SET_FLAG_FROM_TEST(state, is_zoom_out, GPU_SAMPLER_FILTER);
+
+ int draw_flags = 0;
+ SET_FLAG_FROM_TEST(draw_flags, do_repeat, SIMA_DRAW_FLAG_DO_REPEAT);
+
+ if ((sima->flag & SI_USE_ALPHA) != 0) {
+ /* Show RGBA */
+ draw_flags |= SIMA_DRAW_FLAG_SHOW_ALPHA | SIMA_DRAW_FLAG_APPLY_ALPHA;
+ }
+ else if ((sima->flag & SI_SHOW_ALPHA) != 0) {
+ draw_flags |= SIMA_DRAW_FLAG_SHUFFLING;
+ copy_v4_fl4(shuffle, 0.0f, 0.0f, 0.0f, 1.0f);
+ }
+ else if ((sima->flag & SI_SHOW_ZBUF) != 0) {
+ draw_flags |= SIMA_DRAW_FLAG_DEPTH | SIMA_DRAW_FLAG_SHUFFLING;
+ copy_v4_fl4(shuffle, 1.0f, 0.0f, 0.0f, 0.0f);
+ }
+ else if ((sima->flag & SI_SHOW_R) != 0) {
+ draw_flags |= SIMA_DRAW_FLAG_APPLY_ALPHA | SIMA_DRAW_FLAG_SHUFFLING;
+ copy_v4_fl4(shuffle, 1.0f, 0.0f, 0.0f, 0.0f);
+ }
+ else if ((sima->flag & SI_SHOW_G) != 0) {
+ draw_flags |= SIMA_DRAW_FLAG_APPLY_ALPHA | SIMA_DRAW_FLAG_SHUFFLING;
+ copy_v4_fl4(shuffle, 0.0f, 1.0f, 0.0f, 0.0f);
+ }
+ else if ((sima->flag & SI_SHOW_B) != 0) {
+ draw_flags |= SIMA_DRAW_FLAG_APPLY_ALPHA | SIMA_DRAW_FLAG_SHUFFLING;
+ copy_v4_fl4(shuffle, 0.0f, 0.0f, 1.0f, 0.0f);
+ }
+ else /* RGB */ {
+ draw_flags |= SIMA_DRAW_FLAG_APPLY_ALPHA;
+ }
+
+ GPUShader *shader = IMAGE_shader_image_get();
+ DRWShadingGroup *shgrp = DRW_shgroup_create(shader, psl->image_pass);
+ if (tex_tile_data != NULL) {
+ draw_flags |= SIMA_DRAW_FLAG_TILED;
+ DRW_shgroup_uniform_texture_ex(shgrp, "imageTileArray", pd->texture, state);
+ DRW_shgroup_uniform_texture(shgrp, "imageTileData", tex_tile_data);
+ }
+ else {
+ DRW_shgroup_uniform_texture_ex(shgrp, "imageTexture", pd->texture, state);
+ }
+ DRW_shgroup_uniform_vec2_copy(shgrp, "farNearDistances", far_near);
+ DRW_shgroup_uniform_vec4_copy(shgrp, "color", color);
+ DRW_shgroup_uniform_vec4_copy(shgrp, "shuffle", shuffle);
+ DRW_shgroup_uniform_int_copy(shgrp, "drawFlags", draw_flags);
+ DRW_shgroup_uniform_bool_copy(shgrp, "imgPremultiplied", use_premul_alpha);
+ image_cache_image_add(shgrp, image);
+ }
+}
+
+/* -------------------------------------------------------------------- */
+/** \name Engine Callbacks
+ * \{ */
+static void IMAGE_engine_init(void *ved)
+{
+ IMAGE_shader_library_ensure();
+ IMAGE_Data *vedata = (IMAGE_Data *)ved;
+ IMAGE_StorageList *stl = vedata->stl;
+ if (!stl->pd) {
+ stl->pd = MEM_callocN(sizeof(IMAGE_PrivateData), __func__);
+ }
+ IMAGE_PrivateData *pd = stl->pd;
+
+ pd->ibuf = NULL;
+ pd->lock = NULL;
+ pd->texture = NULL;
+}
+
+static void IMAGE_cache_init(void *ved)
+{
+ IMAGE_Data *vedata = (IMAGE_Data *)ved;
+ IMAGE_StorageList *stl = vedata->stl;
+ IMAGE_PrivateData *pd = stl->pd;
+ IMAGE_PassList *psl = vedata->psl;
+
+ const DRWContextState *draw_ctx = DRW_context_state_get();
+ SpaceImage *sima = (SpaceImage *)draw_ctx->space_data;
+
+ {
+ /* 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 = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS |
+ DRW_STATE_BLEND_ALPHA_PREMUL;
+ psl->image_pass = DRW_pass_create("Image", state);
+ }
+
+ 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);
+
+ {
+ Image *image = ED_space_image(sima);
+ ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &pd->lock, 0);
+ image_cache_image(vedata, image, &sima->iuser, ibuf);
+ pd->ibuf = ibuf;
+ }
+}
+
+static void IMAGE_cache_populate(void *UNUSED(vedata), Object *UNUSED(ob))
+{
+ /* Function intentional left empty. `cache_populate` is required to be implemented. */
+}
+
+static void image_draw_finish(IMAGE_Data *ved)
+{
+ IMAGE_Data *vedata = (IMAGE_Data *)ved;
+ IMAGE_StorageList *stl = vedata->stl;
+ IMAGE_PrivateData *pd = stl->pd;
+ const DRWContextState *draw_ctx = DRW_context_state_get();
+ SpaceImage *sima = (SpaceImage *)draw_ctx->space_data;
+
+ ED_space_image_release_buffer(sima, pd->ibuf, pd->lock);
+
+ if (pd->texture && pd->owns_texture) {
+ GPU_texture_free(pd->texture);
+ pd->owns_texture = false;
+ }
+ pd->texture = NULL;
+}
+
+static void IMAGE_draw_scene(void *ved)
+{
+ IMAGE_Data *vedata = (IMAGE_Data *)ved;
+ IMAGE_PassList *psl = vedata->psl;
+
+ DRW_draw_pass(psl->image_pass);
+
+ image_draw_finish(vedata);
+}
+
+static void IMAGE_engine_free(void)
+{
+ IMAGE_shader_free();
+}
+
+/* \} */
+static const DrawEngineDataSize IMAGE_data_size = DRW_VIEWPORT_DATA_SIZE(IMAGE_Data);
+
+DrawEngineType draw_engine_image_type = {
+ NULL, /* next */
+ NULL, /* prev */
+ N_("UV/Image"), /* idname */
+ &IMAGE_data_size, /* vedata_size */
+ &IMAGE_engine_init, /* engine_init */
+ &IMAGE_engine_free, /* engine_free */
+ &IMAGE_cache_init, /* cache_init */
+ &IMAGE_cache_populate, /* cache_populate */
+ NULL, /* cache_finish */
+ &IMAGE_draw_scene, /* draw_scene */
+ NULL, /* view_update */
+ NULL, /* id_update */
+ NULL, /* render_to_image */
+};
diff --git a/source/blender/draw/engines/image/image_engine.h b/source/blender/draw/engines/image/image_engine.h
new file mode 100644
index 00000000000..0098d863ef9
--- /dev/null
+++ b/source/blender/draw/engines/image/image_engine.h
@@ -0,0 +1,25 @@
+/*
+ * 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 2020, Blender Foundation.
+ */
+
+/** \file
+ * \ingroup draw_editors
+ */
+
+#pragma once
+
+extern DrawEngineType draw_engine_image_type;
diff --git a/source/blender/draw/engines/image/image_private.h b/source/blender/draw/engines/image/image_private.h
new file mode 100644
index 00000000000..d11d868d4d2
--- /dev/null
+++ b/source/blender/draw/engines/image/image_private.h
@@ -0,0 +1,69 @@
+/*
+ * 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 2020, Blender Foundation.
+ */
+
+/** \file
+ * \ingroup draw_engine
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Forward declarations */
+struct rcti;
+struct GPUBatch;
+struct Image;
+struct ImBuf;
+struct GPUTexture;
+
+/* *********** LISTS *********** */
+
+/* GPUViewport.storage
+ * Is freed everytime the viewport engine changes */
+typedef struct IMAGE_PassList {
+ DRWPass *image_pass;
+} IMAGE_PassList;
+
+typedef struct IMAGE_PrivateData {
+ void *lock;
+ struct ImBuf *ibuf;
+
+ struct GPUTexture *texture;
+ bool owns_texture;
+} IMAGE_PrivateData;
+
+typedef struct IMAGE_StorageList {
+ IMAGE_PrivateData *pd;
+} IMAGE_StorageList;
+
+typedef struct IMAGE_Data {
+ void *engine_type;
+ DRWViewportEmptyList *fbl;
+ DRWViewportEmptyList *txl;
+ IMAGE_PassList *psl;
+ IMAGE_StorageList *stl;
+} IMAGE_Data;
+
+/* image_shader.c */
+GPUShader *IMAGE_shader_image_get(void);
+void IMAGE_shader_library_ensure(void);
+void IMAGE_shader_free(void);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/source/blender/draw/engines/image/image_shader.c b/source/blender/draw/engines/image/image_shader.c
new file mode 100644
index 00000000000..433c79e20cf
--- /dev/null
+++ b/source/blender/draw/engines/image/image_shader.c
@@ -0,0 +1,77 @@
+/*
+ * 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 2020, Blender Foundation.
+ */
+
+/** \file
+ * \ingroup draw_engine
+ */
+
+#include "DRW_render.h"
+
+#include "BLI_dynstr.h"
+
+#include "GPU_batch.h"
+
+#include "image_engine.h"
+#include "image_private.h"
+
+extern char datatoc_common_colormanagement_lib_glsl[];
+extern char datatoc_common_globals_lib_glsl[];
+extern char datatoc_common_view_lib_glsl[];
+
+extern char datatoc_engine_image_frag_glsl[];
+extern char datatoc_engine_image_vert_glsl[];
+
+typedef struct IMAGE_Shaders {
+ GPUShader *image_sh;
+} IMAGE_Shaders;
+
+static struct {
+ IMAGE_Shaders shaders;
+ DRWShaderLibrary *lib;
+} e_data = {0}; /* Engine data */
+
+void IMAGE_shader_library_ensure(void)
+{
+ if (e_data.lib == NULL) {
+ e_data.lib = DRW_shader_library_create();
+ /* NOTE: Theses needs to be ordered by dependencies. */
+ DRW_SHADER_LIB_ADD(e_data.lib, common_colormanagement_lib);
+ DRW_SHADER_LIB_ADD(e_data.lib, common_globals_lib);
+ DRW_SHADER_LIB_ADD(e_data.lib, common_view_lib);
+ }
+}
+
+GPUShader *IMAGE_shader_image_get(void)
+{
+ IMAGE_Shaders *sh_data = &e_data.shaders;
+ if (!sh_data->image_sh) {
+ sh_data->image_sh = DRW_shader_create_with_shaderlib(
+ datatoc_engine_image_vert_glsl, NULL, datatoc_engine_image_frag_glsl, e_data.lib, NULL);
+ }
+ return sh_data->image_sh;
+}
+
+void IMAGE_shader_free(void)
+{
+ GPUShader **sh_data_as_array = (GPUShader **)&e_data.shaders;
+ for (int i = 0; i < (sizeof(IMAGE_Shaders) / sizeof(GPUShader *)); i++) {
+ DRW_SHADER_FREE_SAFE(sh_data_as_array[i]);
+ }
+
+ DRW_SHADER_LIB_FREE_SAFE(e_data.lib);
+} \ No newline at end of file
diff --git a/source/blender/draw/engines/image/shaders/engine_image_frag.glsl b/source/blender/draw/engines/image/shaders/engine_image_frag.glsl
new file mode 100644
index 00000000000..5c5d9362dfc
--- /dev/null
+++ b/source/blender/draw/engines/image/shaders/engine_image_frag.glsl
@@ -0,0 +1,91 @@
+#pragma BLENDER_REQUIRE(common_colormanagement_lib.glsl)
+
+/* Keep in sync with image_engine.c */
+#define SIMA_DRAW_FLAG_SHOW_ALPHA (1 << 0)
+#define SIMA_DRAW_FLAG_APPLY_ALPHA (1 << 1)
+#define SIMA_DRAW_FLAG_SHUFFLING (1 << 2)
+#define SIMA_DRAW_FLAG_DEPTH (1 << 3)
+#define SIMA_DRAW_FLAG_TILED (1 << 4)
+#define SIMA_DRAW_FLAG_DO_REPEAT (1 << 5)
+
+uniform sampler2DArray imageTileArray;
+uniform sampler1DArray imageTileData;
+uniform sampler2D imageTexture;
+
+uniform bool imgPremultiplied;
+uniform int drawFlags;
+uniform vec2 farNearDistances;
+uniform vec4 color;
+uniform vec4 shuffle;
+
+#define FAR_DISTANCE farNearDistances.x
+#define NEAR_DISTANCE farNearDistances.y
+
+in vec2 uvs;
+
+out vec4 fragColor;
+
+/* TODO(fclem) deduplicate code. */
+bool node_tex_tile_lookup(inout vec3 co, sampler2DArray ima, sampler1DArray map)
+{
+ vec2 tile_pos = floor(co.xy);
+
+ if (tile_pos.x < 0 || tile_pos.y < 0 || tile_pos.x >= 10) {
+ return false;
+ }
+
+ float tile = 10.0 * tile_pos.y + tile_pos.x;
+ if (tile >= textureSize(map, 0).x) {
+ return false;
+ }
+
+ /* Fetch tile information. */
+ float tile_layer = texelFetch(map, ivec2(tile, 0), 0).x;
+ if (tile_layer < 0.0) {
+ return false;
+ }
+
+ vec4 tile_info = texelFetch(map, ivec2(tile, 1), 0);
+
+ co = vec3(((co.xy - tile_pos) * tile_info.zw) + tile_info.xy, tile_layer);
+ return true;
+}
+
+void main()
+{
+ vec4 tex_color;
+ /* Read texture */
+ if ((drawFlags & SIMA_DRAW_FLAG_TILED) != 0) {
+ vec3 co = vec3(uvs, 0.0);
+ if (node_tex_tile_lookup(co, imageTileArray, imageTileData)) {
+ tex_color = texture(imageTileArray, co);
+ }
+ else {
+ tex_color = vec4(1.0, 0.0, 1.0, 1.0);
+ }
+ }
+ else {
+ vec2 uvs_clamped = ((drawFlags & SIMA_DRAW_FLAG_DO_REPEAT) != 0) ?
+ fract(uvs) :
+ clamp(uvs, vec2(0.0), vec2(1.0));
+ tex_color = texture(imageTexture, uvs_clamped);
+ }
+
+ if ((drawFlags & SIMA_DRAW_FLAG_APPLY_ALPHA) != 0) {
+ if (!imgPremultiplied && tex_color.a != 0.0 && tex_color.a != 1.0) {
+ tex_color.rgb *= tex_color.a;
+ }
+ }
+ if ((drawFlags & SIMA_DRAW_FLAG_DEPTH) != 0) {
+ tex_color = smoothstep(FAR_DISTANCE, NEAR_DISTANCE, tex_color);
+ }
+
+ if ((drawFlags & SIMA_DRAW_FLAG_SHUFFLING) != 0) {
+ tex_color = color * dot(tex_color, shuffle);
+ }
+ if ((drawFlags & SIMA_DRAW_FLAG_SHOW_ALPHA) == 0) {
+ tex_color.a = 1.0;
+ }
+
+ fragColor = tex_color;
+}
diff --git a/source/blender/draw/engines/image/shaders/engine_image_vert.glsl b/source/blender/draw/engines/image/shaders/engine_image_vert.glsl
new file mode 100644
index 00000000000..3f1fb154d44
--- /dev/null
+++ b/source/blender/draw/engines/image/shaders/engine_image_vert.glsl
@@ -0,0 +1,31 @@
+#pragma BLENDER_REQUIRE(common_view_lib.glsl)
+
+#define SIMA_DRAW_FLAG_DO_REPEAT (1 << 5)
+
+uniform int drawFlags;
+
+in vec3 pos;
+out vec2 uvs;
+
+void main()
+{
+ /* `pos` contains the coordinates of a quad (-1..1). but we need the coordinates of an image
+ * plane (0..1) */
+ vec3 image_pos = pos * 0.5 + 0.5;
+
+ if ((drawFlags & SIMA_DRAW_FLAG_DO_REPEAT) != 0) {
+ gl_Position = vec4(pos.xy, 0.0, 1.0);
+ uvs = point_view_to_object(image_pos).xy;
+ }
+ else {
+ vec3 world_pos = point_object_to_world(image_pos);
+ vec4 position = point_world_to_ndc(world_pos);
+ /* Move drawn pixels to the front. In the overlay engine the depth is used
+ * to detect if a transparency texture or the background color should be drawn.
+ * Vertices are between 0.0 and 0.2, Edges between 0.2 and 0.4
+ * actual pixels are at 0.75, 1.0 is used for the background. */
+ position.z = 0.75;
+ gl_Position = position;
+ uvs = world_pos.xy;
+ }
+}