2023-08-12 20:10:16 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gskglframeprivate.h"
|
|
|
|
|
2023-08-21 00:18:37 +00:00
|
|
|
#include "gskgpuglobalsopprivate.h"
|
2023-08-12 20:10:16 +00:00
|
|
|
#include "gskgpuopprivate.h"
|
2023-09-24 06:40:05 +00:00
|
|
|
#include "gskgpushaderopprivate.h"
|
2023-08-21 00:18:37 +00:00
|
|
|
#include "gskglbufferprivate.h"
|
2023-10-03 19:04:21 +00:00
|
|
|
#include "gskgldescriptorsprivate.h"
|
2023-08-21 00:18:37 +00:00
|
|
|
#include "gskgldeviceprivate.h"
|
2023-11-01 16:14:43 +00:00
|
|
|
#include "gskglimageprivate.h"
|
|
|
|
|
2023-11-02 05:45:09 +00:00
|
|
|
#include "gdkdmabuftextureprivate.h"
|
|
|
|
#include "gdkglcontextprivate.h"
|
2023-11-01 16:14:43 +00:00
|
|
|
#include "gdkgltextureprivate.h"
|
2023-08-12 20:10:16 +00:00
|
|
|
|
|
|
|
struct _GskGLFrame
|
|
|
|
{
|
|
|
|
GskGpuFrame parent_instance;
|
2023-08-21 00:18:37 +00:00
|
|
|
|
|
|
|
GLuint globals_buffer_id;
|
|
|
|
guint next_texture_slot;
|
|
|
|
|
|
|
|
GHashTable *vaos;
|
2023-08-12 20:10:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GskGLFrameClass
|
|
|
|
{
|
|
|
|
GskGpuFrameClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GskGLFrame, gsk_gl_frame, GSK_TYPE_GPU_FRAME)
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gsk_gl_frame_is_busy (GskGpuFrame *frame)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-08-21 00:18:37 +00:00
|
|
|
gsk_gl_frame_setup (GskGpuFrame *frame)
|
|
|
|
{
|
|
|
|
GskGLFrame *self = GSK_GL_FRAME (frame);
|
|
|
|
|
|
|
|
glGenBuffers (1, &self->globals_buffer_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_gl_frame_cleanup (GskGpuFrame *frame)
|
2023-08-12 20:10:16 +00:00
|
|
|
{
|
2023-08-21 00:18:37 +00:00
|
|
|
GskGLFrame *self = GSK_GL_FRAME (frame);
|
|
|
|
|
|
|
|
self->next_texture_slot = 0;
|
|
|
|
|
|
|
|
GSK_GPU_FRAME_CLASS (gsk_gl_frame_parent_class)->cleanup (frame);
|
|
|
|
}
|
|
|
|
|
2023-11-01 16:14:43 +00:00
|
|
|
static GskGpuImage *
|
|
|
|
gsk_gl_frame_upload_texture (GskGpuFrame *frame,
|
|
|
|
gboolean with_mipmap,
|
|
|
|
GdkTexture *texture)
|
|
|
|
{
|
|
|
|
if (GDK_IS_GL_TEXTURE (texture))
|
|
|
|
{
|
|
|
|
GdkGLTexture *gl_texture = GDK_GL_TEXTURE (texture);
|
|
|
|
|
|
|
|
if (gdk_gl_context_is_shared (GDK_GL_CONTEXT (gsk_gpu_frame_get_context (frame)),
|
|
|
|
gdk_gl_texture_get_context (gl_texture)))
|
|
|
|
{
|
|
|
|
GskGpuImage *image;
|
|
|
|
GLsync sync;
|
|
|
|
|
|
|
|
image = gsk_gl_image_new_for_texture (GSK_GL_DEVICE (gsk_gpu_frame_get_device (frame)),
|
|
|
|
texture,
|
|
|
|
gdk_gl_texture_get_id (gl_texture),
|
|
|
|
FALSE,
|
|
|
|
gdk_gl_texture_has_mipmap (gl_texture) ? (GSK_GPU_IMAGE_CAN_MIPMAP | GSK_GPU_IMAGE_MIPMAP) : 0);
|
|
|
|
|
|
|
|
/* This is a hack, but it works */
|
|
|
|
sync = gdk_gl_texture_get_sync (gl_texture);
|
|
|
|
if (sync)
|
|
|
|
glWaitSync (sync, 0, GL_TIMEOUT_IGNORED);
|
|
|
|
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
}
|
2023-11-02 05:45:09 +00:00
|
|
|
else if (GDK_IS_DMABUF_TEXTURE (texture))
|
|
|
|
{
|
|
|
|
gboolean external;
|
|
|
|
GLuint tex_id;
|
|
|
|
|
|
|
|
tex_id = gdk_gl_context_import_dmabuf (GDK_GL_CONTEXT (gsk_gpu_frame_get_context (frame)),
|
|
|
|
gdk_texture_get_width (texture),
|
|
|
|
gdk_texture_get_height (texture),
|
|
|
|
gdk_dmabuf_texture_get_dmabuf (GDK_DMABUF_TEXTURE (texture)),
|
|
|
|
&external);
|
|
|
|
if (tex_id)
|
|
|
|
{
|
|
|
|
return gsk_gl_image_new_for_texture (GSK_GL_DEVICE (gsk_gpu_frame_get_device (frame)),
|
|
|
|
texture,
|
|
|
|
tex_id,
|
|
|
|
TRUE,
|
|
|
|
(external ? GSK_GPU_IMAGE_EXTERNAL | GSK_GPU_IMAGE_NO_BLIT : 0));
|
|
|
|
}
|
|
|
|
}
|
2023-11-01 16:14:43 +00:00
|
|
|
|
|
|
|
return GSK_GPU_FRAME_CLASS (gsk_gl_frame_parent_class)->upload_texture (frame, with_mipmap, texture);
|
|
|
|
}
|
|
|
|
|
2023-10-03 19:04:21 +00:00
|
|
|
static GskGpuDescriptors *
|
|
|
|
gsk_gl_frame_create_descriptors (GskGpuFrame *frame)
|
2023-08-21 00:18:37 +00:00
|
|
|
{
|
2023-10-03 19:04:21 +00:00
|
|
|
return GSK_GPU_DESCRIPTORS (gsk_gl_descriptors_new (GSK_GL_DEVICE (gsk_gpu_frame_get_device (frame))));
|
2023-08-21 00:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GskGpuBuffer *
|
|
|
|
gsk_gl_frame_create_vertex_buffer (GskGpuFrame *frame,
|
|
|
|
gsize size)
|
|
|
|
{
|
2023-09-24 06:40:05 +00:00
|
|
|
GskGLFrame *self = GSK_GL_FRAME (frame);
|
|
|
|
|
|
|
|
/* We could also reassign them all to the new buffer here?
|
|
|
|
* Is that faster?
|
|
|
|
*/
|
|
|
|
g_hash_table_remove_all (self->vaos);
|
|
|
|
|
2023-08-21 00:18:37 +00:00
|
|
|
return gsk_gl_buffer_new (GL_ARRAY_BUFFER, size, GL_WRITE_ONLY);
|
|
|
|
}
|
|
|
|
|
2023-08-30 19:57:41 +00:00
|
|
|
static GskGpuBuffer *
|
|
|
|
gsk_gl_frame_create_storage_buffer (GskGpuFrame *frame,
|
|
|
|
gsize size)
|
|
|
|
{
|
|
|
|
return gsk_gl_buffer_new (GL_UNIFORM_BUFFER, size, GL_WRITE_ONLY);
|
|
|
|
}
|
|
|
|
|
2023-08-21 00:18:37 +00:00
|
|
|
static void
|
|
|
|
gsk_gl_frame_submit (GskGpuFrame *frame,
|
|
|
|
GskGpuBuffer *vertex_buffer,
|
|
|
|
GskGpuOp *op)
|
|
|
|
{
|
|
|
|
GskGLFrame *self = GSK_GL_FRAME (frame);
|
2023-11-04 02:57:38 +00:00
|
|
|
GskGLCommandState state = { 0, };
|
2023-08-21 00:18:37 +00:00
|
|
|
|
2023-08-28 08:42:04 +00:00
|
|
|
glEnable (GL_SCISSOR_TEST);
|
|
|
|
|
2023-08-21 00:18:37 +00:00
|
|
|
glEnable (GL_DEPTH_TEST);
|
|
|
|
glDepthFunc (GL_LEQUAL);
|
|
|
|
glEnable (GL_BLEND);
|
|
|
|
|
2023-08-30 19:57:41 +00:00
|
|
|
if (vertex_buffer)
|
|
|
|
gsk_gl_buffer_bind (GSK_GL_BUFFER (vertex_buffer));
|
2023-11-19 14:27:10 +00:00
|
|
|
|
|
|
|
gsk_gl_frame_bind_globals (self);
|
2023-08-21 00:18:37 +00:00
|
|
|
glBufferData (GL_UNIFORM_BUFFER,
|
|
|
|
sizeof (GskGpuGlobalsInstance),
|
|
|
|
NULL,
|
2023-10-04 01:23:42 +00:00
|
|
|
GL_STREAM_DRAW);
|
2023-08-21 00:18:37 +00:00
|
|
|
|
2023-08-12 20:10:16 +00:00
|
|
|
while (op)
|
|
|
|
{
|
2023-11-04 02:57:38 +00:00
|
|
|
op = gsk_gpu_op_gl_command (op, frame, &state);
|
2023-08-12 20:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 00:18:37 +00:00
|
|
|
static void
|
|
|
|
gsk_gl_frame_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GskGLFrame *self = GSK_GL_FRAME (object);
|
|
|
|
|
|
|
|
g_hash_table_unref (self->vaos);
|
|
|
|
glDeleteBuffers (1, &self->globals_buffer_id);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gsk_gl_frame_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2023-08-12 20:10:16 +00:00
|
|
|
static void
|
|
|
|
gsk_gl_frame_class_init (GskGLFrameClass *klass)
|
|
|
|
{
|
|
|
|
GskGpuFrameClass *gpu_frame_class = GSK_GPU_FRAME_CLASS (klass);
|
2023-08-21 00:18:37 +00:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
2023-08-12 20:10:16 +00:00
|
|
|
|
|
|
|
gpu_frame_class->is_busy = gsk_gl_frame_is_busy;
|
2023-08-21 00:18:37 +00:00
|
|
|
gpu_frame_class->setup = gsk_gl_frame_setup;
|
|
|
|
gpu_frame_class->cleanup = gsk_gl_frame_cleanup;
|
2023-11-01 16:14:43 +00:00
|
|
|
gpu_frame_class->upload_texture = gsk_gl_frame_upload_texture;
|
2023-10-03 19:04:21 +00:00
|
|
|
gpu_frame_class->create_descriptors = gsk_gl_frame_create_descriptors;
|
2023-08-21 00:18:37 +00:00
|
|
|
gpu_frame_class->create_vertex_buffer = gsk_gl_frame_create_vertex_buffer;
|
2023-08-30 19:57:41 +00:00
|
|
|
gpu_frame_class->create_storage_buffer = gsk_gl_frame_create_storage_buffer;
|
2023-08-12 20:10:16 +00:00
|
|
|
gpu_frame_class->submit = gsk_gl_frame_submit;
|
2023-08-21 00:18:37 +00:00
|
|
|
|
|
|
|
object_class->finalize = gsk_gl_frame_finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_vao (gpointer vao)
|
|
|
|
{
|
|
|
|
glDeleteVertexArrays (1, (GLuint[1]) { GPOINTER_TO_UINT (vao) });
|
2023-08-12 20:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_gl_frame_init (GskGLFrame *self)
|
|
|
|
{
|
2023-08-21 00:18:37 +00:00
|
|
|
self->vaos = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, free_vao);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gsk_gl_frame_use_program (GskGLFrame *self,
|
|
|
|
const GskGpuShaderOpClass *op_class,
|
2023-12-31 04:38:56 +00:00
|
|
|
guint32 variation,
|
2023-11-02 01:53:50 +00:00
|
|
|
GskGpuShaderClip clip,
|
|
|
|
guint n_external_textures)
|
2023-08-21 00:18:37 +00:00
|
|
|
{
|
|
|
|
GLuint vao;
|
|
|
|
|
|
|
|
gsk_gl_device_use_program (GSK_GL_DEVICE (gsk_gpu_frame_get_device (GSK_GPU_FRAME (self))),
|
|
|
|
op_class,
|
2023-12-31 04:38:56 +00:00
|
|
|
variation,
|
2023-11-02 01:53:50 +00:00
|
|
|
clip,
|
|
|
|
n_external_textures);
|
2023-08-21 00:18:37 +00:00
|
|
|
|
|
|
|
vao = GPOINTER_TO_UINT (g_hash_table_lookup (self->vaos, op_class));
|
|
|
|
if (vao)
|
|
|
|
{
|
2023-09-24 06:40:05 +00:00
|
|
|
glBindVertexArray (vao);
|
2023-08-21 00:18:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-09-24 06:40:05 +00:00
|
|
|
glGenVertexArrays (1, &vao);
|
|
|
|
glBindVertexArray (vao);
|
|
|
|
op_class->setup_vao (0);
|
2023-08-21 00:18:37 +00:00
|
|
|
|
|
|
|
g_hash_table_insert (self->vaos, (gpointer) op_class, GUINT_TO_POINTER (vao));
|
2023-08-12 20:10:16 +00:00
|
|
|
}
|
|
|
|
|
2023-11-19 14:27:10 +00:00
|
|
|
void
|
|
|
|
gsk_gl_frame_bind_globals (GskGLFrame *self)
|
|
|
|
{
|
|
|
|
glBindBufferBase (GL_UNIFORM_BUFFER, 0, self->globals_buffer_id);
|
|
|
|
}
|
|
|
|
|