gtk/gsk/ngl/gskngluniformstate.c

275 lines
7.9 KiB
C
Raw Normal View History

gsk: add OpenGL based GskNglRenderer The primary goal here was to cleanup the current GL renderer to make maintenance easier going forward. Furthermore, it tracks state to allow us to implement more advanced renderer features going forward. Reordering This renderer will reorder batches by render target to reduce the number of times render targets are changed. In the future, we could also reorder by program within the render target if we can determine that vertices do not overlap. Uniform Snapshots To allow for reordering of batches all uniforms need to be tracked for the programs. This allows us to create the full uniform state when the batch has been moved into a new position. Some care was taken as it can be performance sensitive. Attachment Snapshots Similar to uniform snapshots, we need to know all of the texture attachments so that we can rebind them when necessary. Render Jobs To help isolate the process of creating GL commands from the renderer abstraction a render job abstraction was added. This could be extended in the future if we decided to do tiling. Command Queue Render jobs create batches using the command queue. The command queue will snapshot uniform and attachment state so that it can reorder batches right before executing them. Currently, the only reordering done is to ensure that we only visit each render target once. We could extend this by tracking vertices, attachments, and others. This code currently uses an inline array helper to reduce overhead from GArray which was showing up on profiles. It could be changed to use GdkArray without too much work, but had roughly double the instructions. Cycle counts have not yet been determined. GLSL Programs This was simplified to use XMACROS so that we can just extend one file (gskglprograms.defs) instead of multiple places. The programs are added as fields in the driver for easy access. Driver The driver manages textures, render targets, access to atlases, programs, and more. There is one driver per display, by using the shared GL context. Some work could be done here to batch uploads so that we make fewer calls to upload when sending icon theme data to the GPU. We'd need to keep a copy of the atlas data for such purposes.
2020-12-19 01:36:59 +00:00
/* gskngluniformstate.c
*
* Copyright 2020 Christian Hergert <chergert@redhat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "config.h"
#include <gsk/gskroundedrectprivate.h>
#include <string.h>
#include "gskngluniformstateprivate.h"
static const guint8 uniform_sizes[] = {
0,
sizeof (Uniform1f),
sizeof (Uniform2f),
sizeof (Uniform3f),
sizeof (Uniform4f),
sizeof (Uniform1f),
sizeof (Uniform2f),
sizeof (Uniform3f),
sizeof (Uniform4f),
sizeof (Uniform1i),
sizeof (Uniform2i),
sizeof (Uniform3i),
sizeof (Uniform4i),
sizeof (Uniform1ui),
sizeof (guint),
sizeof (graphene_matrix_t),
sizeof (GskRoundedRect),
sizeof (GdkRGBA),
0,
};
GskNglUniformState *
gsk_ngl_uniform_state_new (void)
{
GskNglUniformState *state;
state = g_atomic_rc_box_new0 (GskNglUniformState);
state->programs = g_hash_table_new_full (NULL, NULL, NULL, g_free);
state->values_len = 4096;
state->values_pos = 0;
state->values_buf = g_malloc (4096);
memset (state->apply_hash, 0, sizeof state->apply_hash);
gsk: add OpenGL based GskNglRenderer The primary goal here was to cleanup the current GL renderer to make maintenance easier going forward. Furthermore, it tracks state to allow us to implement more advanced renderer features going forward. Reordering This renderer will reorder batches by render target to reduce the number of times render targets are changed. In the future, we could also reorder by program within the render target if we can determine that vertices do not overlap. Uniform Snapshots To allow for reordering of batches all uniforms need to be tracked for the programs. This allows us to create the full uniform state when the batch has been moved into a new position. Some care was taken as it can be performance sensitive. Attachment Snapshots Similar to uniform snapshots, we need to know all of the texture attachments so that we can rebind them when necessary. Render Jobs To help isolate the process of creating GL commands from the renderer abstraction a render job abstraction was added. This could be extended in the future if we decided to do tiling. Command Queue Render jobs create batches using the command queue. The command queue will snapshot uniform and attachment state so that it can reorder batches right before executing them. Currently, the only reordering done is to ensure that we only visit each render target once. We could extend this by tracking vertices, attachments, and others. This code currently uses an inline array helper to reduce overhead from GArray which was showing up on profiles. It could be changed to use GdkArray without too much work, but had roughly double the instructions. Cycle counts have not yet been determined. GLSL Programs This was simplified to use XMACROS so that we can just extend one file (gskglprograms.defs) instead of multiple places. The programs are added as fields in the driver for easy access. Driver The driver manages textures, render targets, access to atlases, programs, and more. There is one driver per display, by using the shared GL context. Some work could be done here to batch uploads so that we make fewer calls to upload when sending icon theme data to the GPU. We'd need to keep a copy of the atlas data for such purposes.
2020-12-19 01:36:59 +00:00
return g_steal_pointer (&state);
}
GskNglUniformState *
gsk_ngl_uniform_state_ref (GskNglUniformState *state)
{
return g_atomic_rc_box_acquire (state);
}
static void
gsk_ngl_uniform_state_finalize (gpointer data)
{
GskNglUniformState *state = data;
g_clear_pointer (&state->programs, g_hash_table_unref);
g_clear_pointer (&state->values_buf, g_free);
}
void
gsk_ngl_uniform_state_unref (GskNglUniformState *state)
{
g_atomic_rc_box_release_full (state, gsk_ngl_uniform_state_finalize);
}
gpointer
gsk_ngl_uniform_state_init_value (GskNglUniformState *state,
GskNglUniformProgram *program,
GskNglUniformFormat format,
guint array_count,
guint location,
GskNglUniformInfoElement **infoptr)
{
GskNglUniformInfoElement *info;
guint offset;
g_assert (state != NULL);
g_assert (array_count < 32);
g_assert ((int)format >= 0 && format < GSK_NGL_UNIFORM_FORMAT_LAST);
g_assert (format > 0);
g_assert (program != NULL);
g_assert (program->sparse != NULL);
g_assert (program->n_sparse <= program->n_uniforms);
g_assert (location < GL_MAX_UNIFORM_LOCATIONS || location == (guint)-1);
g_assert (location < program->n_uniforms);
/* Handle unused uniforms gracefully */
if G_UNLIKELY (location == (guint)-1)
return NULL;
info = &program->uniforms[location];
if G_LIKELY (format == info->info.format)
{
if G_LIKELY (array_count <= info->info.array_count)
{
*infoptr = info;
return GSK_NGL_UNIFORM_VALUE (state->values_buf, info->info.offset);
}
/* We found the uniform, but there is not enough space for the
* amount that was requested. Instead, allocate new space and
* set the value to "initial" so that the caller just writes
* over the previous value.
*
* This can happen when using dynamic array lengths like the
* "n_color_stops" in gradient shaders.
*/
goto setup_info;
}
else if (info->info.format == 0)
{
goto setup_info;
}
else
{
g_critical ("Attempt to access uniform with different type of value "
"than it was initialized with. Program %u Location %u. "
"Was %d now %d (array length %d now %d).",
program->program_id, location, info->info.format, format,
info->info.array_count, array_count);
*infoptr = NULL;
return NULL;
}
setup_info:
gsk_ngl_uniform_state_realloc (state,
uniform_sizes[format] * MAX (1, array_count),
&offset);
/* we have 21 bits for offset */
g_assert (offset < (1 << GSK_NGL_UNIFORM_OFFSET_BITS));
/* We could once again be setting up this info if the array size grew.
* So make sure that we have space in our space array for the value.
*/
g_assert (info->info.format != 0 || program->n_sparse < program->n_uniforms);
if (info->info.format == 0)
program->sparse[program->n_sparse++] = location;
info->info.format = format;
info->info.offset = offset;
info->info.array_count = array_count;
info->info.initial = TRUE;
info->stamp = 0;
*infoptr = info;
return GSK_NGL_UNIFORM_VALUE (state->values_buf, info->info.offset);
}
void
gsk_ngl_uniform_state_end_frame (GskNglUniformState *state)
{
GHashTableIter iter;
GskNglUniformProgram *program;
guint allocator = 0;
g_return_if_fail (state != NULL);
/* After a frame finishes, we want to remove all our copies of uniform
* data that isn't needed any longer. Since we treat it as uninitialized
* after this frame (to reset it on first use next frame) we can just
* discard it but keep an allocation around to reuse.
*/
g_hash_table_iter_init (&iter, state->programs);
while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&program))
{
for (guint j = 0; j < program->n_sparse; j++)
{
guint location = program->sparse[j];
GskNglUniformInfoElement *info = &program->uniforms[location];
guint size;
g_assert (info->info.format > 0);
/* Calculate how much size is needed for the uniform, including arrays */
size = uniform_sizes[info->info.format] * MAX (1, info->info.array_count);
/* Adjust alignment for value */
allocator += gsk_ngl_uniform_state_align (allocator, size);
/* Offset is in slots of 4 bytes */
info->info.offset = allocator / 4;
info->info.initial = TRUE;
info->stamp = 0;
/* Now advance for this items data */
allocator += size;
}
}
state->values_pos = allocator;
g_assert (allocator <= state->values_len);
memset (state->apply_hash, 0, sizeof state->apply_hash);
gsk: add OpenGL based GskNglRenderer The primary goal here was to cleanup the current GL renderer to make maintenance easier going forward. Furthermore, it tracks state to allow us to implement more advanced renderer features going forward. Reordering This renderer will reorder batches by render target to reduce the number of times render targets are changed. In the future, we could also reorder by program within the render target if we can determine that vertices do not overlap. Uniform Snapshots To allow for reordering of batches all uniforms need to be tracked for the programs. This allows us to create the full uniform state when the batch has been moved into a new position. Some care was taken as it can be performance sensitive. Attachment Snapshots Similar to uniform snapshots, we need to know all of the texture attachments so that we can rebind them when necessary. Render Jobs To help isolate the process of creating GL commands from the renderer abstraction a render job abstraction was added. This could be extended in the future if we decided to do tiling. Command Queue Render jobs create batches using the command queue. The command queue will snapshot uniform and attachment state so that it can reorder batches right before executing them. Currently, the only reordering done is to ensure that we only visit each render target once. We could extend this by tracking vertices, attachments, and others. This code currently uses an inline array helper to reduce overhead from GArray which was showing up on profiles. It could be changed to use GdkArray without too much work, but had roughly double the instructions. Cycle counts have not yet been determined. GLSL Programs This was simplified to use XMACROS so that we can just extend one file (gskglprograms.defs) instead of multiple places. The programs are added as fields in the driver for easy access. Driver The driver manages textures, render targets, access to atlases, programs, and more. There is one driver per display, by using the shared GL context. Some work could be done here to batch uploads so that we make fewer calls to upload when sending icon theme data to the GPU. We'd need to keep a copy of the atlas data for such purposes.
2020-12-19 01:36:59 +00:00
}
gsize
gsk_ngl_uniform_format_size (GskNglUniformFormat format)
{
g_assert (format > 0);
g_assert (format < GSK_NGL_UNIFORM_FORMAT_LAST);
return uniform_sizes[format];
}
GskNglUniformProgram *
gsk_ngl_uniform_state_get_program (GskNglUniformState *state,
guint program,
guint n_uniforms)
{
GskNglUniformProgram *ret;
g_return_val_if_fail (state != NULL, NULL);
g_return_val_if_fail (program > 0, NULL);
g_return_val_if_fail (program < G_MAXUINT, NULL);
ret = g_hash_table_lookup (state->programs, GUINT_TO_POINTER (program));
if (ret == NULL)
{
gsize uniform_size = n_uniforms * sizeof (GskNglUniformInfoElement);
gsize sparse_size = n_uniforms * sizeof (guint);
gsize size = sizeof (GskNglUniformProgram) + uniform_size + sparse_size;
/* Must be multiple of 4 for space pointer to align */
G_STATIC_ASSERT (sizeof (GskNglUniformInfoElement) == 8);
ret = g_malloc0 (size);
ret->program_id = program;
ret->n_uniforms = n_uniforms;
ret->n_sparse = 0;
ret->sparse = (guint *)&ret->uniforms[n_uniforms];
for (guint i = 0; i < n_uniforms; i++)
ret->uniforms[i].info.initial = TRUE;
g_hash_table_insert (state->programs, GUINT_TO_POINTER (program), ret);
}
return ret;
}