2016-03-17 13:48:19 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gskglrendererprivate.h"
|
|
|
|
|
|
|
|
#include "gskdebugprivate.h"
|
|
|
|
#include "gskenums.h"
|
2016-07-08 15:21:13 +00:00
|
|
|
#include "gskgldriverprivate.h"
|
2016-07-12 13:50:48 +00:00
|
|
|
#include "gskglprofilerprivate.h"
|
2016-08-03 15:51:21 +00:00
|
|
|
#include "gskprofilerprivate.h"
|
2016-03-17 13:48:19 +00:00
|
|
|
#include "gskrendererprivate.h"
|
|
|
|
#include "gskrendernodeprivate.h"
|
2016-07-03 20:12:22 +00:00
|
|
|
#include "gskshaderbuilderprivate.h"
|
2016-11-07 16:59:38 +00:00
|
|
|
#include "gsktextureprivate.h"
|
2016-03-17 13:48:19 +00:00
|
|
|
|
|
|
|
#include "gskprivate.h"
|
|
|
|
|
|
|
|
#include <epoxy/gl.h>
|
|
|
|
|
2016-11-01 15:00:31 +00:00
|
|
|
#define SHADER_VERSION_GLES 100
|
|
|
|
#define SHADER_VERSION_GL2_LEGACY 110
|
|
|
|
#define SHADER_VERSION_GL3_LEGACY 130
|
2016-07-03 21:54:30 +00:00
|
|
|
#define SHADER_VERSION_GL3 150
|
|
|
|
|
2016-06-30 14:07:16 +00:00
|
|
|
typedef struct {
|
2016-07-21 16:17:49 +00:00
|
|
|
int render_target_id;
|
2016-07-20 16:55:35 +00:00
|
|
|
int vao_id;
|
|
|
|
int buffer_id;
|
|
|
|
int texture_id;
|
|
|
|
int program_id;
|
|
|
|
|
|
|
|
int mvp_location;
|
2016-07-21 16:17:49 +00:00
|
|
|
int source_location;
|
|
|
|
int mask_location;
|
2016-07-20 16:55:35 +00:00
|
|
|
int uv_location;
|
|
|
|
int position_location;
|
|
|
|
int alpha_location;
|
|
|
|
int blendMode_location;
|
2016-06-30 14:07:16 +00:00
|
|
|
} RenderData;
|
|
|
|
|
2016-03-17 13:48:19 +00:00
|
|
|
typedef struct {
|
|
|
|
/* Back pointer to the node, only meant for comparison */
|
|
|
|
GskRenderNode *node;
|
|
|
|
|
|
|
|
graphene_point3d_t min;
|
|
|
|
graphene_point3d_t max;
|
|
|
|
|
|
|
|
graphene_size_t size;
|
|
|
|
|
|
|
|
graphene_matrix_t mvp;
|
|
|
|
|
|
|
|
float opacity;
|
|
|
|
float z;
|
|
|
|
|
|
|
|
const char *name;
|
|
|
|
|
2016-06-30 16:06:31 +00:00
|
|
|
GskBlendMode blend_mode;
|
|
|
|
|
2016-06-30 14:07:16 +00:00
|
|
|
RenderData render_data;
|
2016-06-30 16:06:31 +00:00
|
|
|
RenderData *parent_data;
|
2016-07-25 16:15:05 +00:00
|
|
|
|
|
|
|
GArray *children;
|
2016-03-17 13:48:19 +00:00
|
|
|
} RenderItem;
|
|
|
|
|
2016-07-03 21:54:30 +00:00
|
|
|
enum {
|
|
|
|
MVP,
|
2016-07-21 16:17:49 +00:00
|
|
|
SOURCE,
|
|
|
|
MASK,
|
2016-07-03 21:54:30 +00:00
|
|
|
ALPHA,
|
|
|
|
BLEND_MODE,
|
|
|
|
N_UNIFORMS
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
POSITION,
|
|
|
|
UV,
|
|
|
|
N_ATTRIBUTES
|
|
|
|
};
|
|
|
|
|
2016-08-03 15:51:21 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
|
|
|
typedef struct {
|
|
|
|
GQuark frames;
|
|
|
|
GQuark draw_calls;
|
|
|
|
} ProfileCounters;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
GQuark cpu_time;
|
|
|
|
GQuark gpu_time;
|
|
|
|
} ProfileTimers;
|
|
|
|
#endif
|
|
|
|
|
2016-12-04 15:33:13 +00:00
|
|
|
typedef enum {
|
|
|
|
RENDER_FULL,
|
|
|
|
RENDER_SCISSOR
|
|
|
|
} RenderMode;
|
|
|
|
|
2016-03-17 13:48:19 +00:00
|
|
|
struct _GskGLRenderer
|
|
|
|
{
|
|
|
|
GskRenderer parent_instance;
|
|
|
|
|
|
|
|
graphene_matrix_t mvp;
|
|
|
|
graphene_frustum_t frustum;
|
|
|
|
|
|
|
|
guint frame_buffer;
|
|
|
|
guint depth_stencil_buffer;
|
|
|
|
|
2016-07-03 21:54:30 +00:00
|
|
|
GQuark uniforms[N_UNIFORMS];
|
|
|
|
GQuark attributes[N_ATTRIBUTES];
|
|
|
|
|
2016-07-27 10:34:20 +00:00
|
|
|
GdkGLContext *gl_context;
|
2016-07-08 15:21:13 +00:00
|
|
|
GskGLDriver *gl_driver;
|
2016-07-12 13:50:48 +00:00
|
|
|
GskGLProfiler *gl_profiler;
|
2016-07-04 12:46:22 +00:00
|
|
|
GskShaderBuilder *shader_builder;
|
|
|
|
|
|
|
|
int blend_program_id;
|
2016-07-04 12:55:00 +00:00
|
|
|
int blit_program_id;
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-07-21 16:19:53 +00:00
|
|
|
GArray *render_items;
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-08-03 15:51:21 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
|
|
|
ProfileCounters profile_counters;
|
|
|
|
ProfileTimers profile_timers;
|
|
|
|
#endif
|
|
|
|
|
2016-12-04 15:33:13 +00:00
|
|
|
RenderMode render_mode;
|
|
|
|
|
2016-03-17 13:48:19 +00:00
|
|
|
gboolean has_buffers : 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _GskGLRendererClass
|
|
|
|
{
|
|
|
|
GskRendererClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GskGLRenderer, gsk_gl_renderer, GSK_TYPE_RENDERER)
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_gl_renderer_dispose (GObject *gobject)
|
|
|
|
{
|
|
|
|
GskGLRenderer *self = GSK_GL_RENDERER (gobject);
|
|
|
|
|
2016-07-27 10:34:20 +00:00
|
|
|
g_clear_object (&self->gl_context);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gsk_gl_renderer_parent_class)->dispose (gobject);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-07-19 13:42:54 +00:00
|
|
|
gsk_gl_renderer_create_buffers (GskGLRenderer *self,
|
|
|
|
int width,
|
2016-07-27 15:48:35 +00:00
|
|
|
int height,
|
|
|
|
int scale_factor)
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
|
|
|
if (self->has_buffers)
|
|
|
|
return;
|
|
|
|
|
2016-07-27 15:48:35 +00:00
|
|
|
GSK_NOTE (OPENGL, g_print ("Creating buffers (w:%d, h:%d, scale:%d)\n", width, height, scale_factor));
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-07-19 13:42:54 +00:00
|
|
|
self->has_buffers = TRUE;
|
2016-03-17 13:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_gl_renderer_destroy_buffers (GskGLRenderer *self)
|
|
|
|
{
|
2016-07-27 10:34:20 +00:00
|
|
|
if (self->gl_context == NULL)
|
2016-03-17 13:48:19 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (!self->has_buffers)
|
|
|
|
return;
|
|
|
|
|
|
|
|
GSK_NOTE (OPENGL, g_print ("Destroying buffers\n"));
|
|
|
|
|
2016-07-27 10:34:20 +00:00
|
|
|
gdk_gl_context_make_current (self->gl_context);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
|
|
|
self->has_buffers = FALSE;
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:12:22 +00:00
|
|
|
static gboolean
|
2016-11-29 03:51:07 +00:00
|
|
|
gsk_gl_renderer_create_programs (GskGLRenderer *self,
|
|
|
|
GError **error)
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
2016-07-03 20:12:22 +00:00
|
|
|
GskShaderBuilder *builder;
|
2016-11-29 03:51:07 +00:00
|
|
|
GError *shader_error = NULL;
|
2016-07-03 20:12:22 +00:00
|
|
|
gboolean res = FALSE;
|
2016-07-03 21:54:30 +00:00
|
|
|
|
2016-07-03 20:12:22 +00:00
|
|
|
builder = gsk_shader_builder_new ();
|
|
|
|
|
|
|
|
gsk_shader_builder_set_resource_base_path (builder, "/org/gtk/libgsk/glsl");
|
|
|
|
|
2016-07-21 16:17:49 +00:00
|
|
|
self->uniforms[MVP] = gsk_shader_builder_add_uniform (builder, "uMVP");
|
|
|
|
self->uniforms[SOURCE] = gsk_shader_builder_add_uniform (builder, "uSource");
|
|
|
|
self->uniforms[MASK] = gsk_shader_builder_add_uniform (builder, "uMask");
|
|
|
|
self->uniforms[ALPHA] = gsk_shader_builder_add_uniform (builder, "uAlpha");
|
|
|
|
self->uniforms[BLEND_MODE] = gsk_shader_builder_add_uniform (builder, "uBlendMode");
|
2016-07-03 20:12:22 +00:00
|
|
|
|
2016-07-21 16:17:49 +00:00
|
|
|
self->attributes[POSITION] = gsk_shader_builder_add_attribute (builder, "aPosition");
|
|
|
|
self->attributes[UV] = gsk_shader_builder_add_attribute (builder, "aUv");
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-07-27 10:34:20 +00:00
|
|
|
if (gdk_gl_context_get_use_es (self->gl_context))
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
2016-07-03 20:12:22 +00:00
|
|
|
gsk_shader_builder_set_version (builder, SHADER_VERSION_GLES);
|
2016-07-04 12:46:22 +00:00
|
|
|
gsk_shader_builder_set_vertex_preamble (builder, "es2_common.vs.glsl");
|
|
|
|
gsk_shader_builder_set_fragment_preamble (builder, "es2_common.fs.glsl");
|
2016-07-03 20:12:22 +00:00
|
|
|
gsk_shader_builder_add_define (builder, "GSK_GLES", "1");
|
|
|
|
}
|
2016-07-27 10:34:20 +00:00
|
|
|
else if (gdk_gl_context_is_legacy (self->gl_context))
|
2016-07-03 20:12:22 +00:00
|
|
|
{
|
2016-11-01 15:00:31 +00:00
|
|
|
int maj, min;
|
|
|
|
gdk_gl_context_get_version (self->gl_context, &maj, &min);
|
|
|
|
|
|
|
|
if (maj == 3)
|
|
|
|
gsk_shader_builder_set_version (builder, SHADER_VERSION_GL3_LEGACY);
|
|
|
|
else
|
|
|
|
gsk_shader_builder_set_version (builder, SHADER_VERSION_GL2_LEGACY);
|
|
|
|
|
2016-07-04 12:46:22 +00:00
|
|
|
gsk_shader_builder_set_vertex_preamble (builder, "gl_common.vs.glsl");
|
|
|
|
gsk_shader_builder_set_fragment_preamble (builder, "gl_common.fs.glsl");
|
2016-07-03 20:12:22 +00:00
|
|
|
gsk_shader_builder_add_define (builder, "GSK_LEGACY", "1");
|
2016-03-17 13:48:19 +00:00
|
|
|
}
|
2016-07-03 20:12:22 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
gsk_shader_builder_set_version (builder, SHADER_VERSION_GL3);
|
2016-07-04 12:46:22 +00:00
|
|
|
gsk_shader_builder_set_vertex_preamble (builder, "gl3_common.vs.glsl");
|
|
|
|
gsk_shader_builder_set_fragment_preamble (builder, "gl3_common.fs.glsl");
|
2016-07-03 20:12:22 +00:00
|
|
|
gsk_shader_builder_add_define (builder, "GSK_GL3", "1");
|
|
|
|
}
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-07-03 20:12:22 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
|
|
|
if (GSK_RENDER_MODE_CHECK (SHADERS))
|
|
|
|
gsk_shader_builder_add_define (builder, "GSK_DEBUG", "1");
|
|
|
|
#endif
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-07-04 12:46:22 +00:00
|
|
|
self->blend_program_id =
|
2016-11-29 03:51:07 +00:00
|
|
|
gsk_shader_builder_create_program (builder, "blend.vs.glsl", "blend.fs.glsl", &shader_error);
|
|
|
|
if (shader_error != NULL)
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
2016-11-29 03:51:07 +00:00
|
|
|
g_propagate_prefixed_error (error,
|
|
|
|
shader_error,
|
|
|
|
"Unable to create 'blend' program: ");
|
2016-07-04 12:46:22 +00:00
|
|
|
g_object_unref (builder);
|
2016-03-17 13:48:19 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2016-07-04 12:55:00 +00:00
|
|
|
self->blit_program_id =
|
2016-11-29 03:51:07 +00:00
|
|
|
gsk_shader_builder_create_program (builder, "blit.vs.glsl", "blit.fs.glsl", &shader_error);
|
|
|
|
if (shader_error != NULL)
|
2016-07-04 12:55:00 +00:00
|
|
|
{
|
2016-11-29 03:51:07 +00:00
|
|
|
g_propagate_prefixed_error (error,
|
|
|
|
shader_error,
|
|
|
|
"Unable to create 'blit' program: ");
|
2016-07-04 12:55:00 +00:00
|
|
|
g_object_unref (builder);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Keep a pointer to query for the uniform and attribute locations
|
|
|
|
* when rendering the scene
|
|
|
|
*/
|
2016-07-04 12:46:22 +00:00
|
|
|
self->shader_builder = builder;
|
2016-07-03 20:12:22 +00:00
|
|
|
|
|
|
|
res = TRUE;
|
2016-03-17 13:48:19 +00:00
|
|
|
|
|
|
|
out:
|
2016-07-03 20:12:22 +00:00
|
|
|
return res;
|
2016-03-17 13:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-07-03 20:12:22 +00:00
|
|
|
gsk_gl_renderer_destroy_programs (GskGLRenderer *self)
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
2016-07-04 12:46:22 +00:00
|
|
|
g_clear_object (&self->shader_builder);
|
2016-03-17 13:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-11-29 03:51:07 +00:00
|
|
|
gsk_gl_renderer_realize (GskRenderer *renderer,
|
|
|
|
GdkWindow *window,
|
|
|
|
GError **error)
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
|
|
|
GskGLRenderer *self = GSK_GL_RENDERER (renderer);
|
|
|
|
|
|
|
|
/* If we didn't get a GdkGLContext before realization, try creating
|
|
|
|
* one now, for our exclusive use.
|
|
|
|
*/
|
2016-07-27 10:34:20 +00:00
|
|
|
if (self->gl_context == NULL)
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
2016-11-29 03:51:07 +00:00
|
|
|
self->gl_context = gdk_window_create_gl_context (window, error);
|
|
|
|
if (self->gl_context == NULL)
|
|
|
|
return FALSE;
|
2016-03-17 13:48:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-29 03:51:07 +00:00
|
|
|
if (!gdk_gl_context_realize (self->gl_context, error))
|
|
|
|
return FALSE;
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-07-27 10:34:20 +00:00
|
|
|
gdk_gl_context_make_current (self->gl_context);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-07-08 15:21:13 +00:00
|
|
|
g_assert (self->gl_driver == NULL);
|
2016-07-27 10:34:20 +00:00
|
|
|
self->gl_driver = gsk_gl_driver_new (self->gl_context);
|
|
|
|
self->gl_profiler = gsk_gl_profiler_new (self->gl_context);
|
2016-07-08 15:21:13 +00:00
|
|
|
|
2016-03-17 13:48:19 +00:00
|
|
|
GSK_NOTE (OPENGL, g_print ("Creating buffers and programs\n"));
|
2016-11-29 03:51:07 +00:00
|
|
|
if (!gsk_gl_renderer_create_programs (self, error))
|
2016-07-03 20:12:22 +00:00
|
|
|
return FALSE;
|
2016-03-17 13:48:19 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_gl_renderer_unrealize (GskRenderer *renderer)
|
|
|
|
{
|
|
|
|
GskGLRenderer *self = GSK_GL_RENDERER (renderer);
|
|
|
|
|
2016-07-27 10:34:20 +00:00
|
|
|
if (self->gl_context == NULL)
|
2016-03-17 13:48:19 +00:00
|
|
|
return;
|
|
|
|
|
2016-07-27 10:34:20 +00:00
|
|
|
gdk_gl_context_make_current (self->gl_context);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-07-20 16:55:35 +00:00
|
|
|
/* We don't need to iterate to destroy the associated GL resources,
|
|
|
|
* as they will be dropped when we finalize the GskGLDriver
|
|
|
|
*/
|
2016-07-21 16:19:53 +00:00
|
|
|
g_clear_pointer (&self->render_items, g_array_unref);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
|
|
|
gsk_gl_renderer_destroy_buffers (self);
|
2016-07-03 20:12:22 +00:00
|
|
|
gsk_gl_renderer_destroy_programs (self);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-07-12 13:50:48 +00:00
|
|
|
g_clear_object (&self->gl_profiler);
|
2016-07-08 15:21:13 +00:00
|
|
|
g_clear_object (&self->gl_driver);
|
|
|
|
|
2016-07-27 10:34:20 +00:00
|
|
|
if (self->gl_context == gdk_gl_context_get_current ())
|
2016-03-17 13:48:19 +00:00
|
|
|
gdk_gl_context_clear_current ();
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:37:31 +00:00
|
|
|
static GdkDrawingContext *
|
|
|
|
gsk_gl_renderer_begin_draw_frame (GskRenderer *renderer,
|
2016-12-04 15:33:13 +00:00
|
|
|
const cairo_region_t *update_area)
|
2016-11-27 15:37:31 +00:00
|
|
|
{
|
|
|
|
GskGLRenderer *self = GSK_GL_RENDERER (renderer);
|
2016-12-04 15:33:13 +00:00
|
|
|
cairo_region_t *damage;
|
2016-11-27 15:37:31 +00:00
|
|
|
GdkDrawingContext *result;
|
2016-12-04 15:33:13 +00:00
|
|
|
GdkRectangle whole_window;
|
2016-11-27 15:37:31 +00:00
|
|
|
GdkWindow *window;
|
|
|
|
|
|
|
|
window = gsk_renderer_get_window (renderer);
|
2016-12-04 15:33:13 +00:00
|
|
|
whole_window = (GdkRectangle) {
|
|
|
|
0, 0,
|
|
|
|
gdk_window_get_width (window),
|
|
|
|
gdk_window_get_height (window)
|
|
|
|
};
|
|
|
|
damage = gdk_gl_context_get_damage (self->gl_context);
|
|
|
|
cairo_region_union (damage, update_area);
|
2016-11-27 15:37:31 +00:00
|
|
|
|
2016-12-04 15:33:13 +00:00
|
|
|
if (cairo_region_contains_rectangle (damage, &whole_window) == CAIRO_REGION_OVERLAP_IN)
|
|
|
|
{
|
|
|
|
self->render_mode = RENDER_FULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GdkRectangle extents;
|
2016-11-27 15:37:31 +00:00
|
|
|
|
2016-12-04 15:33:13 +00:00
|
|
|
cairo_region_get_extents (damage, &extents);
|
|
|
|
cairo_region_union_rectangle (damage, &extents);
|
2016-11-27 15:37:31 +00:00
|
|
|
|
2016-12-04 15:33:13 +00:00
|
|
|
if (gdk_rectangle_equal (&extents, &whole_window))
|
|
|
|
self->render_mode = RENDER_FULL;
|
|
|
|
else
|
|
|
|
self->render_mode = RENDER_SCISSOR;
|
|
|
|
}
|
2016-11-27 15:37:31 +00:00
|
|
|
|
2016-12-04 15:33:13 +00:00
|
|
|
result = gdk_window_begin_draw_frame (window,
|
|
|
|
GDK_DRAW_CONTEXT (self->gl_context),
|
|
|
|
damage);
|
2016-11-27 15:37:31 +00:00
|
|
|
|
2016-12-04 15:33:13 +00:00
|
|
|
cairo_region_destroy (damage);
|
2016-11-27 15:37:31 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-03-17 13:48:19 +00:00
|
|
|
static void
|
2016-06-22 16:30:36 +00:00
|
|
|
gsk_gl_renderer_resize_viewport (GskGLRenderer *self,
|
2016-07-27 15:48:35 +00:00
|
|
|
const graphene_rect_t *viewport,
|
|
|
|
int scale_factor)
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
2016-07-27 15:48:35 +00:00
|
|
|
int width = viewport->size.width * scale_factor;
|
|
|
|
int height = viewport->size.height * scale_factor;
|
2016-06-22 16:30:36 +00:00
|
|
|
|
2016-07-27 15:48:35 +00:00
|
|
|
GSK_NOTE (OPENGL, g_print ("glViewport(0, 0, %d, %d) [scale:%d]\n",
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
scale_factor));
|
|
|
|
|
|
|
|
glViewport (0, 0, width, height);
|
2016-03-17 13:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-06-22 16:30:36 +00:00
|
|
|
gsk_gl_renderer_update_frustum (GskGLRenderer *self,
|
|
|
|
const graphene_matrix_t *modelview,
|
|
|
|
const graphene_matrix_t *projection)
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
2016-08-03 02:00:50 +00:00
|
|
|
GSK_NOTE (TRANSFORMS, g_print ("Updating the modelview/projection\n"));
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-08-16 10:44:39 +00:00
|
|
|
graphene_matrix_multiply (modelview, projection, &self->mvp);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
|
|
|
graphene_frustum_init_from_matrix (&self->frustum, &self->mvp);
|
2016-06-30 16:06:31 +00:00
|
|
|
|
2016-08-03 02:00:50 +00:00
|
|
|
GSK_NOTE (TRANSFORMS,
|
|
|
|
g_print ("Renderer MVP:\n");
|
|
|
|
graphene_matrix_print (&self->mvp);
|
|
|
|
g_print ("\n"));
|
2016-03-17 13:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define N_VERTICES 6
|
|
|
|
|
|
|
|
static void
|
2016-07-08 15:21:13 +00:00
|
|
|
render_item (GskGLRenderer *self,
|
|
|
|
RenderItem *item)
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
|
|
|
float mvp[16];
|
2016-07-25 16:15:05 +00:00
|
|
|
float opacity;
|
|
|
|
|
|
|
|
if (item->children != NULL)
|
|
|
|
{
|
|
|
|
if (gsk_gl_driver_bind_render_target (self->gl_driver, item->render_data.render_target_id))
|
|
|
|
{
|
|
|
|
glViewport (0, 0, item->size.width, item->size.height);
|
|
|
|
|
|
|
|
glClearColor (0.0, 0.0, 0.0, 0.0);
|
|
|
|
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
}
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-07-08 15:21:13 +00:00
|
|
|
gsk_gl_driver_bind_vao (self->gl_driver, item->render_data.vao_id);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-06-30 14:07:16 +00:00
|
|
|
glUseProgram (item->render_data.program_id);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-08-09 15:30:13 +00:00
|
|
|
if (item->render_data.texture_id != 0)
|
2016-06-30 16:06:31 +00:00
|
|
|
{
|
2016-08-09 15:30:13 +00:00
|
|
|
/* Use texture unit 0 for the source */
|
|
|
|
glUniform1i (item->render_data.source_location, 0);
|
|
|
|
gsk_gl_driver_bind_source_texture (self->gl_driver, item->render_data.texture_id);
|
2016-07-08 15:21:13 +00:00
|
|
|
|
2016-08-09 15:30:13 +00:00
|
|
|
if (item->parent_data != NULL)
|
2016-07-21 16:17:49 +00:00
|
|
|
{
|
2016-08-09 15:30:13 +00:00
|
|
|
glUniform1i (item->render_data.blendMode_location, item->blend_mode);
|
|
|
|
|
|
|
|
/* Use texture unit 1 for the mask */
|
|
|
|
if (item->parent_data->texture_id != 0)
|
|
|
|
{
|
|
|
|
glUniform1i (item->render_data.mask_location, 1);
|
|
|
|
gsk_gl_driver_bind_mask_texture (self->gl_driver, item->parent_data->texture_id);
|
|
|
|
}
|
2016-07-21 16:17:49 +00:00
|
|
|
}
|
2016-06-30 16:06:31 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 13:48:19 +00:00
|
|
|
/* Pass the opacity component */
|
2016-12-11 10:48:32 +00:00
|
|
|
if (item->children != NULL)
|
2016-07-25 16:15:05 +00:00
|
|
|
opacity = 1.0;
|
|
|
|
else
|
|
|
|
opacity = item->opacity;
|
|
|
|
|
|
|
|
glUniform1f (item->render_data.alpha_location, opacity);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
|
|
|
/* Pass the mvp to the vertex shader */
|
2016-08-03 02:00:50 +00:00
|
|
|
GSK_NOTE (TRANSFORMS, graphene_matrix_print (&item->mvp));
|
2016-03-17 13:48:19 +00:00
|
|
|
graphene_matrix_to_float (&item->mvp, mvp);
|
2016-06-30 14:07:16 +00:00
|
|
|
glUniformMatrix4fv (item->render_data.mvp_location, 1, GL_FALSE, mvp);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
|
|
|
/* Draw the quad */
|
2016-08-03 02:00:50 +00:00
|
|
|
GSK_NOTE2 (OPENGL, TRANSFORMS,
|
2016-12-12 17:54:49 +00:00
|
|
|
g_print ("Drawing item <%s>[%p] (w:%g, h:%g) with opacity: %g blend mode: %d\n",
|
2016-08-03 02:00:50 +00:00
|
|
|
item->name,
|
|
|
|
item,
|
|
|
|
item->size.width, item->size.height,
|
2016-12-11 10:48:32 +00:00
|
|
|
item->opacity,
|
2016-08-13 08:07:38 +00:00
|
|
|
item->blend_mode));
|
2016-03-17 13:48:19 +00:00
|
|
|
|
|
|
|
glDrawArrays (GL_TRIANGLES, 0, N_VERTICES);
|
2016-07-25 16:15:05 +00:00
|
|
|
|
2016-08-03 15:51:21 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
|
|
|
gsk_profiler_counter_inc (gsk_renderer_get_profiler (GSK_RENDERER (self)),
|
|
|
|
self->profile_counters.draw_calls);
|
|
|
|
#endif
|
|
|
|
|
2016-07-25 16:15:05 +00:00
|
|
|
/* Render all children items, so we can take the result
|
|
|
|
* render target texture during the compositing
|
|
|
|
*/
|
|
|
|
if (item->children != NULL)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < item->children->len; i++)
|
|
|
|
{
|
|
|
|
RenderItem *child = &g_array_index (item->children, RenderItem, i);
|
|
|
|
|
|
|
|
render_item (self, child);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bind the parent render target */
|
|
|
|
if (item->parent_data != NULL)
|
|
|
|
gsk_gl_driver_bind_render_target (self->gl_driver, item->parent_data->render_target_id);
|
|
|
|
|
|
|
|
/* Bind the same VAO, as the render target is created with the same size
|
|
|
|
* and vertices as the texture target
|
|
|
|
*/
|
|
|
|
gsk_gl_driver_bind_vao (self->gl_driver, item->render_data.vao_id);
|
|
|
|
|
|
|
|
/* Since we're rendering the target texture, we only need the blit program */
|
|
|
|
glUseProgram (self->blit_program_id);
|
|
|
|
|
|
|
|
/* Use texture unit 0 for the render target */
|
|
|
|
glUniform1i (item->render_data.source_location, 0);
|
|
|
|
gsk_gl_driver_bind_source_texture (self->gl_driver, item->render_data.render_target_id);
|
|
|
|
|
|
|
|
/* Pass the opacity component; if we got here, we know that the original render
|
|
|
|
* target is neither fully opaque nor at full opacity
|
|
|
|
*/
|
|
|
|
glUniform1f (item->render_data.alpha_location, item->opacity);
|
|
|
|
|
|
|
|
/* Pass the mvp to the vertex shader */
|
2016-08-03 02:00:50 +00:00
|
|
|
GSK_NOTE (TRANSFORMS, graphene_matrix_print (&item->mvp));
|
2016-07-25 16:15:05 +00:00
|
|
|
graphene_matrix_to_float (&item->mvp, mvp);
|
|
|
|
glUniformMatrix4fv (item->render_data.mvp_location, 1, GL_FALSE, mvp);
|
|
|
|
|
|
|
|
/* Draw the quad */
|
2016-08-03 02:00:50 +00:00
|
|
|
GSK_NOTE2 (OPENGL, TRANSFORMS,
|
2016-12-12 17:54:49 +00:00
|
|
|
g_print ("Drawing offscreen item <%s>[%p] (w:%g, h:%g) with opacity: %g\n",
|
2016-08-03 02:00:50 +00:00
|
|
|
item->name,
|
|
|
|
item,
|
|
|
|
item->size.width, item->size.height,
|
|
|
|
item->opacity));
|
2016-07-25 16:15:05 +00:00
|
|
|
|
|
|
|
glDrawArrays (GL_TRIANGLES, 0, N_VERTICES);
|
|
|
|
}
|
2016-03-17 13:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-08-13 07:34:22 +00:00
|
|
|
get_gl_scaling_filters (GskRenderNode *node,
|
|
|
|
int *min_filter_r,
|
|
|
|
int *mag_filter_r)
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
2016-08-13 07:34:22 +00:00
|
|
|
switch (node->min_filter)
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
|
|
|
case GSK_SCALING_FILTER_NEAREST:
|
|
|
|
*min_filter_r = GL_NEAREST;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GSK_SCALING_FILTER_LINEAR:
|
|
|
|
*min_filter_r = GL_LINEAR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GSK_SCALING_FILTER_TRILINEAR:
|
|
|
|
*min_filter_r = GL_LINEAR_MIPMAP_LINEAR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-08-13 07:34:22 +00:00
|
|
|
switch (node->mag_filter)
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
|
|
|
case GSK_SCALING_FILTER_NEAREST:
|
|
|
|
*mag_filter_r = GL_NEAREST;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* There's no point in using anything above GL_LINEAR for
|
|
|
|
* magnification filters
|
|
|
|
*/
|
|
|
|
case GSK_SCALING_FILTER_LINEAR:
|
|
|
|
case GSK_SCALING_FILTER_TRILINEAR:
|
|
|
|
*mag_filter_r = GL_LINEAR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-22 16:30:36 +00:00
|
|
|
#if 0
|
2016-03-17 13:48:19 +00:00
|
|
|
static gboolean
|
|
|
|
check_in_frustum (const graphene_frustum_t *frustum,
|
|
|
|
RenderItem *item)
|
|
|
|
{
|
|
|
|
graphene_box_t aabb;
|
|
|
|
|
|
|
|
graphene_box_init (&aabb, &item->min, &item->max);
|
|
|
|
graphene_matrix_transform_box (&item->mvp, &aabb, &aabb);
|
|
|
|
|
|
|
|
return graphene_frustum_intersects_box (frustum, &aabb);
|
|
|
|
}
|
2016-06-22 16:30:36 +00:00
|
|
|
#endif
|
2016-03-17 13:48:19 +00:00
|
|
|
|
|
|
|
static float
|
|
|
|
project_item (const graphene_matrix_t *projection,
|
|
|
|
const graphene_matrix_t *modelview)
|
|
|
|
{
|
|
|
|
graphene_vec4_t vec;
|
|
|
|
|
|
|
|
graphene_matrix_get_row (modelview, 3, &vec);
|
|
|
|
graphene_matrix_transform_vec4 (projection, &vec, &vec);
|
|
|
|
|
|
|
|
return graphene_vec4_get_z (&vec) / graphene_vec4_get_w (&vec);
|
|
|
|
}
|
|
|
|
|
2016-07-25 16:15:05 +00:00
|
|
|
static gboolean
|
|
|
|
render_node_needs_render_target (GskRenderNode *node)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2016-03-17 13:48:19 +00:00
|
|
|
static void
|
2016-08-10 16:50:03 +00:00
|
|
|
gsk_gl_renderer_add_render_item (GskGLRenderer *self,
|
|
|
|
const graphene_matrix_t *projection,
|
2016-12-12 23:11:06 +00:00
|
|
|
const graphene_matrix_t *modelview,
|
2016-08-10 16:50:03 +00:00
|
|
|
GArray *render_items,
|
|
|
|
GskRenderNode *node,
|
|
|
|
RenderItem *parent)
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
|
|
|
graphene_rect_t viewport;
|
|
|
|
RenderItem item;
|
2016-07-06 16:10:12 +00:00
|
|
|
RenderItem *ritem = NULL;
|
2016-07-04 12:46:22 +00:00
|
|
|
int program_id;
|
2016-07-27 15:48:35 +00:00
|
|
|
int scale_factor;
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-06-30 16:06:31 +00:00
|
|
|
memset (&item, 0, sizeof (RenderItem));
|
|
|
|
|
2016-03-17 13:48:19 +00:00
|
|
|
gsk_renderer_get_viewport (GSK_RENDERER (self), &viewport);
|
|
|
|
|
2016-11-01 03:58:29 +00:00
|
|
|
scale_factor = gsk_renderer_get_scale_factor (GSK_RENDERER (self));
|
2016-07-27 15:48:35 +00:00
|
|
|
if (scale_factor < 1)
|
|
|
|
scale_factor = 1;
|
|
|
|
|
2016-03-17 13:48:19 +00:00
|
|
|
item.node = node;
|
|
|
|
item.name = node->name != NULL ? node->name : "unnamed";
|
|
|
|
|
|
|
|
/* The texture size */
|
2016-12-21 10:21:38 +00:00
|
|
|
item.size.width = node->bounds.size.width * scale_factor;
|
|
|
|
item.size.height = node->bounds.size.height * scale_factor;
|
2016-03-17 13:48:19 +00:00
|
|
|
|
|
|
|
/* Each render item is an axis-aligned bounding box that we
|
|
|
|
* transform using the given transformation matrix
|
|
|
|
*/
|
2016-12-21 10:21:38 +00:00
|
|
|
item.min.x = node->bounds.origin.x;
|
|
|
|
item.min.y = node->bounds.origin.y;
|
2016-03-17 13:48:19 +00:00
|
|
|
item.min.z = 0.f;
|
|
|
|
|
2016-12-21 10:21:38 +00:00
|
|
|
item.max.x = item.min.x + node->bounds.size.width;
|
|
|
|
item.max.y = item.min.y + node->bounds.size.height;
|
2016-03-17 13:48:19 +00:00
|
|
|
item.max.z = 0.f;
|
|
|
|
|
|
|
|
/* The location of the item, in normalized world coordinates */
|
2016-12-12 23:11:06 +00:00
|
|
|
graphene_matrix_multiply (modelview, &self->mvp, &item.mvp);
|
|
|
|
item.z = project_item (projection, modelview);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-12-13 04:49:57 +00:00
|
|
|
item.opacity = 1.0;
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-12-15 03:24:37 +00:00
|
|
|
item.blend_mode = GSK_BLEND_MODE_DEFAULT;
|
2016-06-30 16:06:31 +00:00
|
|
|
|
2016-07-08 15:21:13 +00:00
|
|
|
/* Back-pointer to the parent node */
|
|
|
|
if (parent != NULL)
|
|
|
|
item.parent_data = &(parent->render_data);
|
|
|
|
else
|
|
|
|
item.parent_data = NULL;
|
2016-07-03 21:54:30 +00:00
|
|
|
|
2016-07-04 12:55:00 +00:00
|
|
|
/* Select the program to use */
|
|
|
|
if (parent != NULL)
|
|
|
|
program_id = self->blend_program_id;
|
|
|
|
else
|
|
|
|
program_id = self->blit_program_id;
|
2016-07-03 21:54:30 +00:00
|
|
|
|
2016-07-08 15:21:13 +00:00
|
|
|
item.render_data.program_id = program_id;
|
|
|
|
|
|
|
|
/* Retrieve all the uniforms and attributes */
|
2016-07-21 16:17:49 +00:00
|
|
|
item.render_data.source_location =
|
|
|
|
gsk_shader_builder_get_uniform_location (self->shader_builder, program_id, self->uniforms[SOURCE]);
|
|
|
|
item.render_data.mask_location =
|
|
|
|
gsk_shader_builder_get_uniform_location (self->shader_builder, program_id, self->uniforms[MASK]);
|
2016-07-03 21:54:30 +00:00
|
|
|
item.render_data.mvp_location =
|
2016-07-04 12:46:22 +00:00
|
|
|
gsk_shader_builder_get_uniform_location (self->shader_builder, program_id, self->uniforms[MVP]);
|
2016-07-03 21:54:30 +00:00
|
|
|
item.render_data.alpha_location =
|
2016-07-04 12:46:22 +00:00
|
|
|
gsk_shader_builder_get_uniform_location (self->shader_builder, program_id, self->uniforms[ALPHA]);
|
2016-07-03 21:54:30 +00:00
|
|
|
item.render_data.blendMode_location =
|
2016-07-04 12:46:22 +00:00
|
|
|
gsk_shader_builder_get_uniform_location (self->shader_builder, program_id, self->uniforms[BLEND_MODE]);
|
2016-07-03 21:54:30 +00:00
|
|
|
|
|
|
|
item.render_data.position_location =
|
2016-07-04 12:46:22 +00:00
|
|
|
gsk_shader_builder_get_attribute_location (self->shader_builder, program_id, self->attributes[POSITION]);
|
2016-07-03 21:54:30 +00:00
|
|
|
item.render_data.uv_location =
|
2016-07-04 12:46:22 +00:00
|
|
|
gsk_shader_builder_get_attribute_location (self->shader_builder, program_id, self->attributes[UV]);
|
2016-06-30 16:06:31 +00:00
|
|
|
|
2016-08-09 15:30:13 +00:00
|
|
|
if (render_node_needs_render_target (node))
|
|
|
|
{
|
|
|
|
item.render_data.render_target_id =
|
2016-11-16 04:37:20 +00:00
|
|
|
gsk_gl_driver_create_texture (self->gl_driver, item.size.width, item.size.height);
|
2016-08-09 15:30:13 +00:00
|
|
|
gsk_gl_driver_init_texture_empty (self->gl_driver, item.render_data.render_target_id);
|
|
|
|
gsk_gl_driver_create_render_target (self->gl_driver, item.render_data.render_target_id, TRUE, TRUE);
|
|
|
|
|
2016-12-12 18:30:41 +00:00
|
|
|
item.children = g_array_new (FALSE, FALSE, sizeof (RenderItem));
|
2016-08-09 15:30:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-23 04:54:50 +00:00
|
|
|
item.render_data.render_target_id = 0;
|
2016-08-09 15:30:13 +00:00
|
|
|
item.children = NULL;
|
|
|
|
}
|
|
|
|
|
2016-12-11 01:33:58 +00:00
|
|
|
switch (gsk_render_node_get_node_type (node))
|
2016-08-12 15:44:29 +00:00
|
|
|
{
|
2016-12-11 01:33:58 +00:00
|
|
|
case GSK_TEXTURE_NODE:
|
|
|
|
{
|
|
|
|
GskTexture *texture = gsk_texture_node_get_texture (node);
|
|
|
|
int gl_min_filter = GL_NEAREST, gl_mag_filter = GL_NEAREST;
|
|
|
|
|
|
|
|
get_gl_scaling_filters (node, &gl_min_filter, &gl_mag_filter);
|
|
|
|
|
|
|
|
item.render_data.texture_id = gsk_gl_driver_get_texture_for_texture (self->gl_driver,
|
|
|
|
texture,
|
|
|
|
gl_min_filter,
|
|
|
|
gl_mag_filter);
|
|
|
|
}
|
|
|
|
break;
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2016-12-11 01:33:58 +00:00
|
|
|
case GSK_CAIRO_NODE:
|
|
|
|
{
|
|
|
|
cairo_surface_t *surface = gsk_cairo_node_get_surface (node);
|
|
|
|
int gl_min_filter = GL_NEAREST, gl_mag_filter = GL_NEAREST;
|
|
|
|
|
2016-12-15 09:08:46 +00:00
|
|
|
if (surface == NULL)
|
|
|
|
return;
|
|
|
|
|
2016-12-11 01:33:58 +00:00
|
|
|
get_gl_scaling_filters (node, &gl_min_filter, &gl_mag_filter);
|
|
|
|
|
|
|
|
/* Upload the Cairo surface to a GL texture */
|
|
|
|
item.render_data.texture_id = gsk_gl_driver_create_texture (self->gl_driver,
|
|
|
|
item.size.width,
|
|
|
|
item.size.height);
|
|
|
|
gsk_gl_driver_bind_source_texture (self->gl_driver, item.render_data.texture_id);
|
|
|
|
gsk_gl_driver_init_texture_with_surface (self->gl_driver,
|
|
|
|
item.render_data.texture_id,
|
|
|
|
surface,
|
|
|
|
gl_min_filter,
|
|
|
|
gl_mag_filter);
|
|
|
|
}
|
|
|
|
break;
|
2016-11-16 03:14:32 +00:00
|
|
|
|
2016-12-12 18:30:41 +00:00
|
|
|
case GSK_CONTAINER_NODE:
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
for (i = 0; i < gsk_container_node_get_n_children (node); i++)
|
|
|
|
{
|
|
|
|
GskRenderNode *child = gsk_container_node_get_child (node, i);
|
2016-12-12 23:11:06 +00:00
|
|
|
gsk_gl_renderer_add_render_item (self, projection, modelview, render_items, child, ritem);
|
2016-12-12 18:30:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
2016-12-12 23:11:06 +00:00
|
|
|
case GSK_TRANSFORM_NODE:
|
|
|
|
{
|
|
|
|
graphene_matrix_t transform, transformed_mv;
|
|
|
|
|
|
|
|
gsk_transform_node_get_transform (node, &transform);
|
|
|
|
graphene_matrix_multiply (&transform, modelview, &transformed_mv);
|
|
|
|
gsk_gl_renderer_add_render_item (self,
|
|
|
|
projection, &transformed_mv,
|
|
|
|
render_items,
|
|
|
|
gsk_transform_node_get_child (node),
|
|
|
|
ritem);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
2016-12-12 18:30:41 +00:00
|
|
|
case GSK_NOT_A_RENDER_NODE:
|
2016-12-13 03:20:04 +00:00
|
|
|
g_assert_not_reached ();
|
2016-12-12 18:30:41 +00:00
|
|
|
return;
|
2016-12-13 03:20:04 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
cairo_surface_t *surface;
|
|
|
|
cairo_t *cr;
|
|
|
|
|
|
|
|
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
|
2016-12-21 10:21:38 +00:00
|
|
|
ceil (node->bounds.size.width),
|
|
|
|
ceil (node->bounds.size.height));
|
2016-12-13 03:20:04 +00:00
|
|
|
cr = cairo_create (surface);
|
2016-12-21 10:21:38 +00:00
|
|
|
cairo_translate (cr, -node->bounds.origin.x, -node->bounds.origin.y);
|
2016-12-13 03:20:04 +00:00
|
|
|
|
|
|
|
gsk_render_node_draw (node, cr);
|
|
|
|
|
|
|
|
cairo_destroy (cr);
|
|
|
|
|
|
|
|
/* Upload the Cairo surface to a GL texture */
|
|
|
|
item.render_data.texture_id = gsk_gl_driver_create_texture (self->gl_driver,
|
|
|
|
item.size.width,
|
|
|
|
item.size.height);
|
|
|
|
gsk_gl_driver_bind_source_texture (self->gl_driver, item.render_data.texture_id);
|
|
|
|
gsk_gl_driver_init_texture_with_surface (self->gl_driver,
|
|
|
|
item.render_data.texture_id,
|
|
|
|
surface,
|
|
|
|
GL_NEAREST, GL_NEAREST);
|
|
|
|
|
|
|
|
cairo_surface_destroy (surface);
|
|
|
|
}
|
|
|
|
break;
|
2016-08-12 15:44:29 +00:00
|
|
|
}
|
2016-08-09 15:30:13 +00:00
|
|
|
|
2016-07-08 15:21:13 +00:00
|
|
|
/* Create the vertex buffers holding the geometry of the quad */
|
|
|
|
{
|
|
|
|
GskQuadVertex vertex_data[N_VERTICES] = {
|
|
|
|
{ { item.min.x, item.min.y }, { 0, 0 }, },
|
|
|
|
{ { item.min.x, item.max.y }, { 0, 1 }, },
|
|
|
|
{ { item.max.x, item.min.y }, { 1, 0 }, },
|
|
|
|
|
|
|
|
{ { item.max.x, item.max.y }, { 1, 1 }, },
|
|
|
|
{ { item.min.x, item.max.y }, { 0, 1 }, },
|
|
|
|
{ { item.max.x, item.min.y }, { 1, 0 }, },
|
|
|
|
};
|
|
|
|
|
|
|
|
item.render_data.vao_id =
|
|
|
|
gsk_gl_driver_create_vao_for_quad (self->gl_driver,
|
|
|
|
item.render_data.position_location,
|
|
|
|
item.render_data.uv_location,
|
2016-08-04 18:14:51 +00:00
|
|
|
N_VERTICES,
|
2016-07-08 15:21:13 +00:00
|
|
|
vertex_data);
|
|
|
|
}
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-12-12 17:54:49 +00:00
|
|
|
GSK_NOTE (OPENGL, g_print ("Adding node <%s>[%p] to render items\n",
|
2016-03-17 13:48:19 +00:00
|
|
|
node->name != NULL ? node->name : "unnamed",
|
|
|
|
node));
|
2016-07-25 16:15:05 +00:00
|
|
|
g_array_append_val (render_items, item);
|
2016-08-09 15:30:13 +00:00
|
|
|
ritem = &g_array_index (render_items, RenderItem, render_items->len - 1);
|
2016-07-25 16:15:05 +00:00
|
|
|
|
|
|
|
if (item.children != NULL)
|
|
|
|
render_items = item.children;
|
2016-03-17 13:48:19 +00:00
|
|
|
}
|
|
|
|
|
2016-06-22 16:30:36 +00:00
|
|
|
static gboolean
|
2016-08-10 16:50:03 +00:00
|
|
|
gsk_gl_renderer_validate_tree (GskGLRenderer *self,
|
|
|
|
GskRenderNode *root,
|
|
|
|
const graphene_matrix_t *projection)
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
2016-12-12 15:52:55 +00:00
|
|
|
graphene_matrix_t identity;
|
|
|
|
|
2016-07-27 10:34:20 +00:00
|
|
|
if (self->gl_context == NULL)
|
2016-06-30 16:06:31 +00:00
|
|
|
{
|
|
|
|
GSK_NOTE (OPENGL, g_print ("No valid GL context associated to the renderer"));
|
|
|
|
return FALSE;
|
|
|
|
}
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-12-12 15:52:55 +00:00
|
|
|
graphene_matrix_init_identity (&identity);
|
|
|
|
|
2016-07-27 10:34:20 +00:00
|
|
|
gdk_gl_context_make_current (self->gl_context);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-12-11 10:55:10 +00:00
|
|
|
self->render_items = g_array_new (FALSE, FALSE, sizeof (RenderItem));
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-07-20 16:55:35 +00:00
|
|
|
gsk_gl_driver_begin_frame (self->gl_driver);
|
|
|
|
|
2016-03-17 13:48:19 +00:00
|
|
|
GSK_NOTE (OPENGL, g_print ("RenderNode -> RenderItem\n"));
|
2016-12-12 15:52:55 +00:00
|
|
|
gsk_gl_renderer_add_render_item (self, projection, &identity, self->render_items, root, NULL);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-12-11 10:55:10 +00:00
|
|
|
GSK_NOTE (OPENGL, g_print ("Total render items: %d\n",
|
|
|
|
self->render_items->len));
|
2016-06-22 16:30:36 +00:00
|
|
|
|
2016-07-20 16:55:35 +00:00
|
|
|
gsk_gl_driver_end_frame (self->gl_driver);
|
|
|
|
|
2016-06-22 16:30:36 +00:00
|
|
|
return TRUE;
|
2016-03-17 13:48:19 +00:00
|
|
|
}
|
|
|
|
|
2016-04-26 13:23:12 +00:00
|
|
|
static void
|
2016-06-22 16:30:36 +00:00
|
|
|
gsk_gl_renderer_clear_tree (GskGLRenderer *self)
|
2016-04-26 13:23:12 +00:00
|
|
|
{
|
2016-08-09 10:24:05 +00:00
|
|
|
int removed_textures, removed_vaos;
|
2016-07-20 16:55:35 +00:00
|
|
|
|
2016-07-27 10:34:20 +00:00
|
|
|
if (self->gl_context == NULL)
|
2016-04-26 13:23:12 +00:00
|
|
|
return;
|
|
|
|
|
2016-07-27 10:34:20 +00:00
|
|
|
gdk_gl_context_make_current (self->gl_context);
|
2016-07-20 16:55:35 +00:00
|
|
|
|
2016-07-21 16:19:53 +00:00
|
|
|
g_clear_pointer (&self->render_items, g_array_unref);
|
2016-08-04 11:37:54 +00:00
|
|
|
|
2016-08-09 10:24:05 +00:00
|
|
|
removed_textures = gsk_gl_driver_collect_textures (self->gl_driver);
|
|
|
|
removed_vaos = gsk_gl_driver_collect_vaos (self->gl_driver);
|
|
|
|
|
|
|
|
GSK_NOTE (OPENGL, g_print ("Collected: %d textures, %d vaos\n",
|
|
|
|
removed_textures,
|
|
|
|
removed_vaos));
|
2016-04-26 13:23:12 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 13:48:19 +00:00
|
|
|
static void
|
2016-06-22 16:30:36 +00:00
|
|
|
gsk_gl_renderer_clear (GskGLRenderer *self)
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
2016-06-22 16:30:36 +00:00
|
|
|
GSK_NOTE (OPENGL, g_print ("Clearing viewport\n"));
|
|
|
|
glClearColor (0, 0, 0, 0);
|
2016-07-19 13:42:54 +00:00
|
|
|
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
2016-03-17 13:48:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 15:33:13 +00:00
|
|
|
static void
|
|
|
|
gsk_gl_renderer_setup_render_mode (GskGLRenderer *self)
|
|
|
|
{
|
|
|
|
switch (self->render_mode)
|
|
|
|
{
|
|
|
|
case RENDER_FULL:
|
|
|
|
glDisable (GL_SCISSOR_TEST);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RENDER_SCISSOR:
|
|
|
|
{
|
|
|
|
GdkDrawingContext *context = gsk_renderer_get_drawing_context (GSK_RENDERER (self));
|
|
|
|
GdkWindow *window = gsk_renderer_get_window (GSK_RENDERER (self));
|
|
|
|
GdkRectangle extents;
|
|
|
|
cairo_region_get_extents (gdk_drawing_context_get_clip (context), &extents);
|
|
|
|
glScissor (extents.x, gdk_window_get_height (window) - extents.height - extents.y, extents.width, extents.height);
|
|
|
|
glEnable (GL_SCISSOR_TEST);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-10 16:50:03 +00:00
|
|
|
#define ORTHO_NEAR_PLANE -10000
|
|
|
|
#define ORTHO_FAR_PLANE 10000
|
|
|
|
|
2016-03-17 13:48:19 +00:00
|
|
|
static void
|
2016-08-24 15:21:50 +00:00
|
|
|
gsk_gl_renderer_render (GskRenderer *renderer,
|
|
|
|
GskRenderNode *root)
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
|
|
|
GskGLRenderer *self = GSK_GL_RENDERER (renderer);
|
2016-06-22 16:30:36 +00:00
|
|
|
graphene_matrix_t modelview, projection;
|
2016-03-17 13:48:19 +00:00
|
|
|
graphene_rect_t viewport;
|
|
|
|
guint i;
|
2016-07-27 15:48:35 +00:00
|
|
|
int scale_factor;
|
2016-08-03 15:51:21 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
|
|
|
GskProfiler *profiler;
|
|
|
|
gint64 gpu_time, cpu_time;
|
|
|
|
#endif
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-07-27 10:34:20 +00:00
|
|
|
if (self->gl_context == NULL)
|
2016-03-17 13:48:19 +00:00
|
|
|
return;
|
|
|
|
|
2016-08-24 15:21:50 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
2016-08-03 15:51:21 +00:00
|
|
|
profiler = gsk_renderer_get_profiler (renderer);
|
2016-08-24 15:21:50 +00:00
|
|
|
#endif
|
2016-08-03 15:51:21 +00:00
|
|
|
|
2016-07-27 10:34:20 +00:00
|
|
|
gdk_gl_context_make_current (self->gl_context);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
|
|
|
gsk_renderer_get_viewport (renderer, &viewport);
|
2016-07-27 15:48:35 +00:00
|
|
|
scale_factor = gsk_renderer_get_scale_factor (renderer);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-07-20 16:55:35 +00:00
|
|
|
gsk_gl_driver_begin_frame (self->gl_driver);
|
2016-07-27 15:48:35 +00:00
|
|
|
gsk_gl_renderer_create_buffers (self, viewport.size.width, viewport.size.height, scale_factor);
|
2016-07-20 16:55:35 +00:00
|
|
|
gsk_gl_driver_end_frame (self->gl_driver);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-08-10 16:50:03 +00:00
|
|
|
/* Set up the modelview and projection matrices to fit our viewport */
|
|
|
|
graphene_matrix_init_scale (&modelview, scale_factor, scale_factor, 1.0);
|
|
|
|
graphene_matrix_init_ortho (&projection,
|
|
|
|
0, viewport.size.width * scale_factor,
|
|
|
|
viewport.size.height * scale_factor, 0,
|
|
|
|
ORTHO_NEAR_PLANE,
|
|
|
|
ORTHO_FAR_PLANE);
|
|
|
|
|
2016-07-08 15:21:13 +00:00
|
|
|
gsk_gl_renderer_update_frustum (self, &modelview, &projection);
|
|
|
|
|
2016-08-10 16:50:03 +00:00
|
|
|
if (!gsk_gl_renderer_validate_tree (self, root, &projection))
|
2016-07-08 15:21:13 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
gsk_gl_driver_begin_frame (self->gl_driver);
|
2016-08-03 15:51:21 +00:00
|
|
|
|
|
|
|
#ifdef G_ENABLE_DEBUG
|
2016-07-20 16:55:35 +00:00
|
|
|
gsk_gl_profiler_begin_gpu_region (self->gl_profiler);
|
2016-08-03 15:51:21 +00:00
|
|
|
gsk_profiler_timer_begin (profiler, self->profile_timers.cpu_time);
|
|
|
|
#endif
|
2016-03-17 13:48:19 +00:00
|
|
|
|
|
|
|
/* Ensure that the viewport is up to date */
|
2016-11-23 04:54:50 +00:00
|
|
|
if (gsk_gl_driver_bind_render_target (self->gl_driver, 0))
|
2016-07-27 15:48:35 +00:00
|
|
|
gsk_gl_renderer_resize_viewport (self, &viewport, scale_factor);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-12-04 15:33:13 +00:00
|
|
|
gsk_gl_renderer_setup_render_mode (self);
|
|
|
|
|
2016-06-22 16:30:36 +00:00
|
|
|
gsk_gl_renderer_clear (self);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-07-19 13:42:54 +00:00
|
|
|
glEnable (GL_DEPTH_TEST);
|
|
|
|
glDepthFunc (GL_LEQUAL);
|
2016-07-08 15:40:42 +00:00
|
|
|
|
2016-03-17 13:48:19 +00:00
|
|
|
glEnable (GL_BLEND);
|
2016-08-09 15:27:18 +00:00
|
|
|
glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-07-21 16:19:53 +00:00
|
|
|
GSK_NOTE (OPENGL, g_print ("Rendering %u items\n", self->render_items->len));
|
|
|
|
for (i = 0; i < self->render_items->len; i++)
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
2016-07-21 16:19:53 +00:00
|
|
|
RenderItem *item = &g_array_index (self->render_items, RenderItem, i);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-07-08 15:21:13 +00:00
|
|
|
render_item (self, item);
|
2016-03-17 13:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Draw the output of the GL rendering to the window */
|
2016-07-12 13:50:48 +00:00
|
|
|
gsk_gl_driver_end_frame (self->gl_driver);
|
2016-08-03 15:51:21 +00:00
|
|
|
|
|
|
|
#ifdef G_ENABLE_DEBUG
|
|
|
|
gsk_profiler_counter_inc (profiler, self->profile_counters.frames);
|
|
|
|
|
|
|
|
cpu_time = gsk_profiler_timer_end (profiler, self->profile_timers.cpu_time);
|
|
|
|
gsk_profiler_timer_set (profiler, self->profile_timers.cpu_time, cpu_time);
|
|
|
|
|
2016-07-12 13:50:48 +00:00
|
|
|
gpu_time = gsk_gl_profiler_end_gpu_region (self->gl_profiler);
|
2016-08-03 15:51:21 +00:00
|
|
|
gsk_profiler_timer_set (profiler, self->profile_timers.gpu_time, gpu_time);
|
|
|
|
|
|
|
|
gsk_profiler_push_samples (profiler);
|
|
|
|
#endif
|
2016-07-12 13:50:48 +00:00
|
|
|
|
2016-06-22 16:30:36 +00:00
|
|
|
out:
|
2016-07-27 10:34:20 +00:00
|
|
|
gdk_gl_context_make_current (self->gl_context);
|
2016-06-22 16:30:36 +00:00
|
|
|
gsk_gl_renderer_clear_tree (self);
|
2016-07-19 13:42:54 +00:00
|
|
|
gsk_gl_renderer_destroy_buffers (self);
|
2016-03-17 13:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_gl_renderer_class_init (GskGLRendererClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
GskRendererClass *renderer_class = GSK_RENDERER_CLASS (klass);
|
|
|
|
|
|
|
|
gobject_class->dispose = gsk_gl_renderer_dispose;
|
|
|
|
|
|
|
|
renderer_class->realize = gsk_gl_renderer_realize;
|
|
|
|
renderer_class->unrealize = gsk_gl_renderer_unrealize;
|
2016-11-27 15:37:31 +00:00
|
|
|
renderer_class->begin_draw_frame = gsk_gl_renderer_begin_draw_frame;
|
2016-03-17 13:48:19 +00:00
|
|
|
renderer_class->render = gsk_gl_renderer_render;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_gl_renderer_init (GskGLRenderer *self)
|
|
|
|
{
|
|
|
|
gsk_ensure_resources ();
|
|
|
|
|
|
|
|
graphene_matrix_init_identity (&self->mvp);
|
2016-08-03 15:51:21 +00:00
|
|
|
|
|
|
|
#ifdef G_ENABLE_DEBUG
|
|
|
|
{
|
|
|
|
GskProfiler *profiler = gsk_renderer_get_profiler (GSK_RENDERER (self));
|
|
|
|
|
|
|
|
self->profile_counters.frames = gsk_profiler_add_counter (profiler, "frames", "Frames", FALSE);
|
|
|
|
self->profile_counters.draw_calls = gsk_profiler_add_counter (profiler, "draws", "glDrawArrays", TRUE);
|
|
|
|
|
|
|
|
self->profile_timers.cpu_time = gsk_profiler_add_timer (profiler, "cpu-time", "CPU time", FALSE, TRUE);
|
|
|
|
self->profile_timers.gpu_time = gsk_profiler_add_timer (profiler, "gpu-time", "GPU time", FALSE, TRUE);
|
|
|
|
}
|
|
|
|
#endif
|
2016-03-17 13:48:19 +00:00
|
|
|
}
|