From 8e7b8b70dc5f98e82f62536deaea22f351b7b63e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 31 Jan 2020 11:34:34 +0530 Subject: [PATCH] A VAO is not needed to draw background images --- kitty/bgimage_fragment.glsl | 9 ++++----- kitty/bgimage_vertex.glsl | 18 +++++++++++++++--- kitty/shaders.c | 35 +++++++---------------------------- kitty/state.h | 3 +-- 4 files changed, 27 insertions(+), 38 deletions(-) diff --git a/kitty/bgimage_fragment.glsl b/kitty/bgimage_fragment.glsl index d8a6d4c14..7256b0157 100644 --- a/kitty/bgimage_fragment.glsl +++ b/kitty/bgimage_fragment.glsl @@ -6,17 +6,16 @@ uniform float bgimage_opacity; #ifdef TILED uniform float bgimage_scale; -// These are of the window, not the screen. -uniform float width; -uniform float height; +uniform float window_width; +uniform float window_height; #endif in vec2 texcoord; out vec4 color; void main() { #ifdef TILED - vec2 txsz = vec2(width,height) / textureSize(image,0); - txsz /= bgimage_scale; + ivec2 image_size = textureSize(image, 0); + vec2 txsz = vec2(window_width / (float(image_size[0]) * bgimage_scale), window_height / (float(image_size[1]) * bgimage_scale)); color = texture(image, texcoord * txsz); #endif #ifdef SIMPLE diff --git a/kitty/bgimage_vertex.glsl b/kitty/bgimage_vertex.glsl index a53d8b320..d28176a81 100644 --- a/kitty/bgimage_vertex.glsl +++ b/kitty/bgimage_vertex.glsl @@ -1,9 +1,21 @@ #version GLSL_VERSION +#define left -1.0f +#define top 1.0f +#define right 1.0f +#define bottom -1.0f -layout(location=0) in vec4 src; out vec2 texcoord; +const vec2 pos_map[] = vec2[4]( + vec2(left, top), + vec2(left, bottom), + vec2(right, bottom), + vec2(right, top) +); + + void main() { - texcoord = clamp(vec2(src[0], src[1]*-1), 0, 1); - gl_Position = src; + vec2 vertex = pos_map[gl_VertexID]; + texcoord = clamp(vec2(vertex[0], vertex[1]*-1), 0, 1); + gl_Position = vec4(vertex, 0, 1); } diff --git a/kitty/shaders.c b/kitty/shaders.c index 72f46ab7a..21e40a001 100644 --- a/kitty/shaders.c +++ b/kitty/shaders.c @@ -334,28 +334,11 @@ cell_prepare_to_render(ssize_t vao_idx, ssize_t gvao_idx, Screen *screen, GLfloa static void draw_bg(int program, OSWindow *w) { - if (w->bvao_idx == 0) { - const GLfloat screenrect[4][2] = { - { -1.0, 1.0 }, - { -1.0, -1.0 }, - { 1.0, -1.0 }, - { 1.0, 1.0 }, - }; - w->bvao_idx = create_vao(); - bind_vertex_array(w->bvao_idx); - glGenBuffers(1, &w->vbo_idx); - glBindBuffer(GL_ARRAY_BUFFER, w->vbo_idx); - glBufferData(GL_ARRAY_BUFFER, sizeof(screenrect), screenrect, GL_STATIC_DRAW); - } - bind_vertex_array(w->bvao_idx); - glBindBuffer(GL_ARRAY_BUFFER, w->vbo_idx); - glClear(GL_COLOR_BUFFER_BIT); - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); - glEnableVertexAttribArray(0); - bind_program(program); - static bool bgimage_constants_set; + bind_vertex_array(blit_vertex_array); + + static bool bgimage_constants_set = false; if (!bgimage_constants_set) { glUniform1i(glGetUniformLocation(program_id(program), "image"), BGIMAGE_UNIT); glUniform1f(glGetUniformLocation(program_id(program), "bgimage_scale"), OPT(background_image_scale)); @@ -658,15 +641,11 @@ create_border_vao(void) { void draw_borders(ssize_t vao_idx, unsigned int num_border_rects, BorderRect *rect_buf, bool rect_data_is_dirty, uint32_t viewport_width, uint32_t viewport_height, color_type active_window_bg, unsigned int num_visible_windows, bool all_windows_have_same_bg, OSWindow *w) { - glEnable(GL_BLEND); - BLEND_ONTO_OPAQUE; if (w->bgimage != NULL) { - int program; - if (OPT(background_image_layout) == TILING || OPT(background_image_layout) == MIRRORED) program = BGIMAGE_TILED_PROGRAM; - else program = BGIMAGE_PROGRAM; - - draw_bg(program, w); + glEnable(GL_BLEND); + BLEND_ONTO_OPAQUE; + draw_bg((OPT(background_image_layout) == TILING || OPT(background_image_layout) == MIRRORED) ? BGIMAGE_TILED_PROGRAM : BGIMAGE_PROGRAM, w); } if (num_border_rects) { @@ -691,7 +670,7 @@ draw_borders(ssize_t vao_idx, unsigned int num_border_rects, BorderRect *rect_bu unbind_vertex_array(); unbind_program(); } - glDisable(GL_BLEND); + if (w->bgimage != NULL) glDisable(GL_BLEND); } // }}} diff --git a/kitty/state.h b/kitty/state.h index 3fb3952de..9cb43c104 100644 --- a/kitty/state.h +++ b/kitty/state.h @@ -169,8 +169,7 @@ typedef struct { monotonic_t last_render_frame_received_at; uint64_t render_calls; id_type last_focused_counter; - ssize_t gvao_idx, bvao_idx; - unsigned int vbo_idx; + ssize_t gvao_idx; } OSWindow;