GPUTexture: Remove bind to edit calls

This is going to be unecessary after the GPU opengl texture backend refactor.

For now add a save/restore mechanism to leave the state untouched.

Also remove some calls where the caller would bind to particular binding
point and set the shader uniform.
This commit is contained in:
2020-09-02 00:27:41 +02:00
parent c598e939ad
commit f72c1c4547
11 changed files with 76 additions and 46 deletions

View File

@@ -187,8 +187,6 @@ static GPUTexture *blf_batch_cache_texture_load(void)
int offset_x = bitmap_len_landed % tex_width;
int offset_y = bitmap_len_landed / tex_width;
GPU_texture_bind(gc->texture, 0);
/* TODO(germano): Update more than one row in a single call. */
while (remain) {
int remain_row = tex_width - offset_x;
@@ -229,16 +227,17 @@ void blf_batch_draw(void)
#endif
GPUTexture *texture = blf_batch_cache_texture_load();
GPU_texture_bind(texture, 0);
GPU_vertbuf_data_len_set(g_batch.verts, g_batch.glyph_len);
GPU_vertbuf_use(g_batch.verts); /* send data */
GPU_batch_program_set_builtin(g_batch.batch, GPU_SHADER_TEXT);
GPU_batch_uniform_1i(g_batch.batch, "glyph", 0);
GPU_batch_texture_bind(g_batch.batch, "glyph", texture);
GPU_batch_draw(g_batch.batch);
GPU_blend(GPU_BLEND_NONE);
GPU_texture_unbind(texture);
/* restart to 1st vertex data pointers */
GPU_vertbuf_attr_get_raw_data(g_batch.verts, g_batch.pos_loc, &g_batch.pos_step);
GPU_vertbuf_attr_get_raw_data(g_batch.verts, g_batch.col_loc, &g_batch.col_step);

View File

@@ -182,8 +182,6 @@ static GPUTexture *gpu_texture_create_tile_array(Image *ima, ImBuf *main_ibuf)
GPUTexture *tex = IMB_touch_gpu_texture(
main_ibuf, arraywidth, arrayheight, arraylayers, use_high_bitdepth);
GPU_texture_bind(tex, 0);
/* Upload each tile one by one. */
LISTBASE_FOREACH (ImageTile *, tile, &ima->tiles) {
int tilelayer = tile->runtime.tilearray_layer;
@@ -225,8 +223,6 @@ static GPUTexture *gpu_texture_create_tile_array(Image *ima, ImBuf *main_ibuf)
GPU_texture_mipmap_mode(tex, false, true);
}
GPU_texture_unbind(tex);
return tex;
}
@@ -323,9 +319,7 @@ static GPUTexture *image_get_gpu_texture(Image *ima,
*tex = IMB_create_gpu_texture(ibuf_intern, use_high_bitdepth, store_premultiplied);
if (GPU_mipmap_enabled()) {
GPU_texture_bind(*tex, 0);
GPU_texture_generate_mipmap(*tex);
GPU_texture_unbind(*tex);
if (ima) {
ima->gpuflag |= IMA_GPU_MIPMAP_COMPLETE;
}
@@ -666,8 +660,6 @@ static void gpu_texture_update_from_ibuf(
}
}
GPU_texture_bind(tex, 0);
if (scaled) {
/* Slower update where we first have to scale the input pixels. */
if (tile != NULL) {

View File

@@ -143,11 +143,9 @@ 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_bind(txl->render_color_tx, 0);
GPU_texture_update_sub(txl->render_color_tx, GPU_DATA_FLOAT, pix_col, x, y, 0, w, h, 0);
}
if (pix_z) {
GPU_texture_bind(txl->render_depth_tx, 0);
GPU_texture_update_sub(txl->render_depth_tx, GPU_DATA_FLOAT, pix_z, x, y, 0, w, h, 0);
}
}

View File

@@ -265,10 +265,8 @@ static DRWVolumeGrid *volume_grid_cache_get(Volume *volume,
voxels,
NULL);
GPU_texture_bind(cache_grid->texture, 0);
GPU_texture_swizzle_set(cache_grid->texture, (channels == 3) ? "rgb1" : "rrr1");
GPU_texture_wrap_mode(cache_grid->texture, false, false);
GPU_texture_unbind(cache_grid->texture);
MEM_freeN(voxels);

View File

@@ -53,10 +53,10 @@ void DRW_transform_none(GPUTexture *tex)
GPUBatch *geom = DRW_cache_fullscreen_quad_get();
GPU_batch_program_set_builtin(geom, GPU_SHADER_2D_IMAGE_COLOR);
GPU_batch_uniform_4f(geom, "color", 1.0f, 1.0f, 1.0f, 1.0f);
GPU_batch_uniform_1i(geom, "image", 0);
GPU_batch_texture_bind(geom, "image", tex);
GPU_texture_bind(tex, 0);
GPU_batch_draw(geom);
GPU_texture_unbind(tex);
}

View File

@@ -128,9 +128,7 @@ static void swizzle_texture_channel_single(GPUTexture *tex)
{
/* Swizzle texture channels so that we get useful RGBA values when sampling
* a texture with fewer channels, e.g. when using density as color. */
GPU_texture_bind(tex, 0);
GPU_texture_swizzle_set(tex, "rrr1");
GPU_texture_unbind(tex);
}
static GPUTexture *create_field_texture(FluidDomainSettings *fds)

View File

@@ -67,9 +67,7 @@ void drw_texture_set_parameters(GPUTexture *tex, DRWTextureFlag flags)
if (flags & DRW_TEX_MIPMAP) {
GPU_texture_mipmap_mode(tex, true, flags & DRW_TEX_FILTER);
GPU_texture_bind(tex, 0);
GPU_texture_generate_mipmap(tex);
GPU_texture_unbind(tex);
}
else {
GPU_texture_filter_mode(tex, flags & DRW_TEX_FILTER);
@@ -172,9 +170,7 @@ void DRW_texture_ensure_2d(
void DRW_texture_generate_mipmaps(GPUTexture *tex)
{
GPU_texture_bind(tex, 0);
GPU_texture_generate_mipmap(tex);
GPU_texture_unbind(tex);
}
void DRW_texture_free(GPUTexture *tex)

View File

@@ -352,9 +352,7 @@ static int load_tex(Brush *br, ViewContext *vc, float zoom, bool col, bool prima
size, size, 0, 2, buffer, format, GPU_DATA_UNSIGNED_BYTE, 0, false, NULL);
if (!col) {
GPU_texture_bind(target->overlay_texture, 0);
GPU_texture_swizzle_set(target->overlay_texture, "rrrr");
GPU_texture_unbind(target->overlay_texture);
}
}
@@ -471,9 +469,7 @@ static int load_tex_cursor(Brush *br, ViewContext *vc, float zoom)
cursor_snap.overlay_texture = GPU_texture_create_nD(
size, size, 0, 2, buffer, GPU_R8, GPU_DATA_UNSIGNED_BYTE, 0, false, NULL);
GPU_texture_bind(cursor_snap.overlay_texture, 0);
GPU_texture_swizzle_set(cursor_snap.overlay_texture, "rrrr");
GPU_texture_unbind(cursor_snap.overlay_texture);
}
if (init) {

View File

@@ -146,6 +146,8 @@ void GPU_batch_program_set_builtin_with_config(GPUBatch *batch,
GPU_shader_uniform_4fv_array((batch)->shader, name, len, val);
#define GPU_batch_uniform_mat4(batch, name, val) \
GPU_shader_uniform_mat4((batch)->shader, name, val);
#define GPU_batch_texture_bind(batch, name, tex) \
GPU_texture_bind(tex, GPU_shader_get_texture_binding((batch)->shader, name));
void GPU_batch_draw(GPUBatch *batch);
void GPU_batch_draw_range(GPUBatch *batch, int v_first, int v_count);

View File

@@ -46,16 +46,6 @@
#include "gpu_context_private.hh"
#include "gpu_framebuffer_private.hh"
#define WARN_NOT_BOUND(_tex) \
{ \
if (_tex->number == -1) { \
fprintf(stderr, "Warning : Trying to set parameter on a texture not bound.\n"); \
BLI_assert(0); \
return; \
} \
} \
((void)0)
static struct GPUTextureGlobal {
/** Texture used in place of invalid textures (not loaded correctly, missing). */
GPUTexture *invalid_tex_1D;
@@ -231,6 +221,33 @@ static const char *gl_enum_to_str(GLenum e)
#undef ENUM_TO_STRING
}
static GLenum gl_enum_target_to_binding(GLenum target)
{
switch (target) {
default:
BLI_assert(0);
ATTR_FALLTHROUGH;
case GL_TEXTURE_1D:
return GL_TEXTURE_BINDING_1D;
case GL_TEXTURE_1D_ARRAY:
return GL_TEXTURE_BINDING_1D_ARRAY;
case GL_TEXTURE_2D:
return GL_TEXTURE_BINDING_2D;
case GL_TEXTURE_2D_ARRAY:
return GL_TEXTURE_BINDING_2D_ARRAY;
case GL_TEXTURE_2D_MULTISAMPLE:
return GL_TEXTURE_BINDING_2D_MULTISAMPLE;
case GL_TEXTURE_3D:
return GL_TEXTURE_BINDING_3D;
case GL_TEXTURE_BUFFER:
return GL_TEXTURE_BINDING_BUFFER;
case GL_TEXTURE_CUBE_MAP:
return GL_TEXTURE_BINDING_CUBE_MAP;
case GL_TEXTURE_CUBE_MAP_ARRAY_ARB:
return GL_TEXTURE_BINDING_CUBE_MAP_ARRAY_ARB;
}
}
static int gpu_get_component_count(eGPUTextureFormat format)
{
switch (format) {
@@ -1493,7 +1510,16 @@ void GPU_texture_update_sub(GPUTexture *tex,
GLenum data_format = gpu_get_gl_dataformat(tex->format, &tex->format_flag);
GLenum data_type = gpu_get_gl_datatype(gpu_data_format);
WARN_NOT_BOUND(tex);
/* Save and restore. */
GLint texture_bound = 0;
if (tex->number == -1) {
glActiveTexture(GL_TEXTURE0);
glGetIntegerv(gl_enum_target_to_binding(tex->target), &texture_bound);
glBindTexture(tex->target, tex->bindcode);
}
else {
glActiveTexture(GL_TEXTURE0 + tex->number);
}
switch (tex->target) {
case GL_TEXTURE_1D:
@@ -1522,6 +1548,10 @@ void GPU_texture_update_sub(GPUTexture *tex,
default:
BLI_assert(!"tex->target mode not supported");
}
if (tex->number == -1) {
glBindTexture(tex->target, texture_bound);
}
}
void *GPU_texture_read(GPUTexture *tex, eGPUDataFormat gpu_data_format, int miplvl)
@@ -1822,12 +1852,19 @@ void GPU_texture_unbind_all(void)
void GPU_texture_generate_mipmap(GPUTexture *tex)
{
WARN_NOT_BOUND(tex);
gpu_texture_memory_footprint_remove(tex);
int levels = 1 + floor(log2(max_ii(tex->w, tex->h)));
glActiveTexture(GL_TEXTURE0 + tex->number);
/* Save and restore. */
GLint texture_bound = 0;
if (tex->number == -1) {
glActiveTexture(GL_TEXTURE0);
glGetIntegerv(gl_enum_target_to_binding(tex->target), &texture_bound);
glBindTexture(tex->target, tex->bindcode);
}
else {
glActiveTexture(GL_TEXTURE0 + tex->number);
}
if (GPU_texture_depth(tex)) {
/* Some drivers have bugs when using glGenerateMipmap with depth textures (see T56789).
@@ -1846,6 +1883,10 @@ void GPU_texture_generate_mipmap(GPUTexture *tex)
tex->mipmaps = levels;
gpu_texture_memory_footprint_add(tex);
if (tex->number == -1) {
glBindTexture(tex->target, texture_bound);
}
}
static GLenum gpu_texture_default_attachment(GPUTexture *tex)
@@ -2002,7 +2043,16 @@ static int gpu_texture_swizzle_to_enum(const char swizzle)
void GPU_texture_swizzle_set(GPUTexture *tex, const char swizzle[4])
{
WARN_NOT_BOUND(tex);
/* Save and restore. */
GLint texture_bound = 0;
if (tex->number == -1) {
glActiveTexture(GL_TEXTURE0);
glGetIntegerv(gl_enum_target_to_binding(tex->target), &texture_bound);
glBindTexture(tex->target, tex->bindcode);
}
else {
glActiveTexture(GL_TEXTURE0 + tex->number);
}
GLint gl_swizzle[4] = {gpu_texture_swizzle_to_enum(swizzle[0]),
gpu_texture_swizzle_to_enum(swizzle[1]),
@@ -2011,6 +2061,10 @@ void GPU_texture_swizzle_set(GPUTexture *tex, const char swizzle[4])
glActiveTexture(GL_TEXTURE0 + tex->number);
glTexParameteriv(tex->target_base, GL_TEXTURE_SWIZZLE_RGBA, gl_swizzle);
if (tex->number == -1) {
glBindTexture(tex->target, texture_bound);
}
}
void GPU_texture_free(GPUTexture *tex)

View File

@@ -2220,10 +2220,7 @@ static void radial_control_set_tex(RadialControl *rc)
rc->texture = GPU_texture_create_nD(
ibuf->x, ibuf->y, 0, 2, ibuf->rect_float, GPU_R8, GPU_DATA_FLOAT, 0, false, NULL);
GPU_texture_filter_mode(rc->texture, true);
GPU_texture_bind(rc->texture, 0);
GPU_texture_swizzle_set(rc->texture, "111r");
GPU_texture_unbind(rc->texture);
MEM_freeN(ibuf->rect_float);
MEM_freeN(ibuf);