From e2c2a332e6bd957db10dda9ae5639c76d2bd12e0 Mon Sep 17 00:00:00 2001 From: Adam Jackson Date: Wed, 15 Jan 2020 15:45:17 -0500 Subject: [PATCH] cogl: Add and use sampler init/free hooks in the CoglDriverVtable next_fake_sampler_object_number moves to CoglGLContext. https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1194 --- cogl/cogl/cogl-driver.h | 9 +++ cogl/cogl/cogl-sampler-cache.c | 57 +------------------ .../driver/gl/cogl-pipeline-opengl-private.h | 8 +++ cogl/cogl/driver/gl/cogl-pipeline-opengl.c | 42 ++++++++++++++ cogl/cogl/driver/gl/cogl-util-gl-private.h | 4 ++ cogl/cogl/driver/gl/cogl-util-gl.c | 1 + cogl/cogl/driver/gl/gl/cogl-driver-gl.c | 3 + cogl/cogl/driver/gl/gles/cogl-driver-gles.c | 3 + 8 files changed, 72 insertions(+), 55 deletions(-) diff --git a/cogl/cogl/cogl-driver.h b/cogl/cogl/cogl-driver.h index e52a99cfb..6ad24d65c 100644 --- a/cogl/cogl/cogl-driver.h +++ b/cogl/cogl/cogl-driver.h @@ -35,6 +35,7 @@ #include "cogl-offscreen.h" #include "cogl-framebuffer-private.h" #include "cogl-attribute-private.h" +#include "cogl-sampler-cache-private.h" typedef struct _CoglDriverVtable CoglDriverVtable; @@ -265,6 +266,14 @@ struct _CoglDriverVtable const void *data, unsigned int size, GError **error); + + void + (*sampler_init) (CoglContext *context, + CoglSamplerCacheEntry *entry); + + void + (*sampler_free) (CoglContext *context, + CoglSamplerCacheEntry *entry); }; #define COGL_DRIVER_ERROR (_cogl_driver_error_quark ()) diff --git a/cogl/cogl/cogl-sampler-cache.c b/cogl/cogl/cogl-sampler-cache.c index f2dbaeb63..2e0e761f0 100644 --- a/cogl/cogl/cogl-sampler-cache.c +++ b/cogl/cogl/cogl-sampler-cache.c @@ -34,11 +34,6 @@ #include "cogl-sampler-cache-private.h" #include "cogl-context-private.h" -#include "driver/gl/cogl-util-gl-private.h" - -#ifndef GL_TEXTURE_WRAP_R -#define GL_TEXTURE_WRAP_R 0x8072 -#endif struct _CoglSamplerCache { @@ -54,10 +49,6 @@ struct _CoglSamplerCache GL state. */ GHashTable *hash_table_cogl; GHashTable *hash_table_gl; - - /* This is used for generated fake unique sampler object numbers - when the sampler object extension is not supported */ - GLuint next_fake_sampler_object_number; }; static CoglSamplerCacheWrapMode @@ -176,22 +167,10 @@ _cogl_sampler_cache_new (CoglContext *context) sampler_state_equal_gl); cache->hash_table_cogl = g_hash_table_new (hash_sampler_state_cogl, sampler_state_equal_cogl); - cache->next_fake_sampler_object_number = 1; return cache; } -static void -set_wrap_mode (CoglContext *context, - GLuint sampler_object, - GLenum param, - CoglSamplerCacheWrapMode wrap_mode) -{ - GE( context, glSamplerParameteri (sampler_object, - param, - wrap_mode) ); -} - static CoglSamplerCacheEntry * _cogl_sampler_cache_get_entry_gl (CoglSamplerCache *cache, const CoglSamplerCacheEntry *key) @@ -202,39 +181,9 @@ _cogl_sampler_cache_get_entry_gl (CoglSamplerCache *cache, if (entry == NULL) { - CoglContext *context = cache->context; - entry = g_slice_dup (CoglSamplerCacheEntry, key); - if (_cogl_has_private_feature (context, - COGL_PRIVATE_FEATURE_SAMPLER_OBJECTS)) - { - GE( context, glGenSamplers (1, &entry->sampler_object) ); - - GE( context, glSamplerParameteri (entry->sampler_object, - GL_TEXTURE_MIN_FILTER, - entry->min_filter) ); - GE( context, glSamplerParameteri (entry->sampler_object, - GL_TEXTURE_MAG_FILTER, - entry->mag_filter) ); - - set_wrap_mode (context, - entry->sampler_object, - GL_TEXTURE_WRAP_S, - entry->wrap_mode_s); - set_wrap_mode (context, - entry->sampler_object, - GL_TEXTURE_WRAP_T, - entry->wrap_mode_t); - } - else - { - /* If sampler objects aren't supported then we'll invent a - unique number so that pipelines can still compare the - unique state just by comparing the sampler object - numbers */ - entry->sampler_object = cache->next_fake_sampler_object_number++; - } + cache->context->driver_vtable->sampler_init (cache->context, entry); g_hash_table_insert (cache->hash_table_gl, entry, entry); } @@ -320,9 +269,7 @@ hash_table_free_gl_cb (void *key, CoglContext *context = user_data; CoglSamplerCacheEntry *entry = value; - if (_cogl_has_private_feature (context, - COGL_PRIVATE_FEATURE_SAMPLER_OBJECTS)) - GE( context, glDeleteSamplers (1, &entry->sampler_object) ); + context->driver_vtable->sampler_free (context, entry); g_slice_free (CoglSamplerCacheEntry, entry); } diff --git a/cogl/cogl/driver/gl/cogl-pipeline-opengl-private.h b/cogl/cogl/driver/gl/cogl-pipeline-opengl-private.h index 8cae99cc0..e10ca69dc 100644 --- a/cogl/cogl/driver/gl/cogl-pipeline-opengl-private.h +++ b/cogl/cogl/driver/gl/cogl-pipeline-opengl-private.h @@ -155,5 +155,13 @@ _cogl_glsl_shader_set_source_with_boilerplate (CoglContext *ctx, const char **strings_in, const GLint *lengths_in); +void +_cogl_sampler_gl_init (CoglContext *context, + CoglSamplerCacheEntry *entry); + +void +_cogl_sampler_gl_free (CoglContext *context, + CoglSamplerCacheEntry *entry); + #endif /* __COGL_PIPELINE_OPENGL_PRIVATE_H */ diff --git a/cogl/cogl/driver/gl/cogl-pipeline-opengl.c b/cogl/cogl/driver/gl/cogl-pipeline-opengl.c index df1ce13e2..3264a0029 100644 --- a/cogl/cogl/driver/gl/cogl-pipeline-opengl.c +++ b/cogl/cogl/driver/gl/cogl-pipeline-opengl.c @@ -694,6 +694,48 @@ _cogl_pipeline_layer_forward_wrap_modes (CoglPipelineLayer *layer, gl_wrap_mode_t); } +void +_cogl_sampler_gl_init (CoglContext *context, CoglSamplerCacheEntry *entry) +{ + if (_cogl_has_private_feature (context, + COGL_PRIVATE_FEATURE_SAMPLER_OBJECTS)) + { + GE( context, glGenSamplers (1, &entry->sampler_object) ); + + GE( context, glSamplerParameteri (entry->sampler_object, + GL_TEXTURE_MIN_FILTER, + entry->min_filter) ); + GE( context, glSamplerParameteri (entry->sampler_object, + GL_TEXTURE_MAG_FILTER, + entry->mag_filter) ); + + GE (context, glSamplerParameteri (entry->sampler_object, + GL_TEXTURE_WRAP_S, + entry->wrap_mode_s) ); + GE (context, glSamplerParameteri (entry->sampler_object, + GL_TEXTURE_WRAP_T, + entry->wrap_mode_t) ); + } + else + { + CoglGLContext *gl_context = context->driver_context; + + /* If sampler objects aren't supported then we'll invent a + unique number so that pipelines can still compare the + unique state just by comparing the sampler object + numbers */ + entry->sampler_object = gl_context->next_fake_sampler_object_number++; + } +} + +void +_cogl_sampler_gl_free (CoglContext *context, CoglSamplerCacheEntry *entry) +{ + if (_cogl_has_private_feature (context, + COGL_PRIVATE_FEATURE_SAMPLER_OBJECTS)) + GE( context, glDeleteSamplers (1, &entry->sampler_object) ); +} + /* OpenGL associates the min/mag filters and repeat modes with the * texture object not the texture unit so we always have to re-assert * the filter and repeat modes whenever we use a texture since it may diff --git a/cogl/cogl/driver/gl/cogl-util-gl-private.h b/cogl/cogl/driver/gl/cogl-util-gl-private.h index 0d23da1a8..b3367973f 100644 --- a/cogl/cogl/driver/gl/cogl-util-gl-private.h +++ b/cogl/cogl/driver/gl/cogl-util-gl-private.h @@ -79,6 +79,10 @@ _cogl_gl_error_to_string (GLenum error_code); typedef struct _CoglGLContext { GArray *texture_units; int active_texture_unit; + + /* This is used for generated fake unique sampler object numbers + when the sampler object extension is not supported */ + GLuint next_fake_sampler_object_number; } CoglGLContext; CoglGLContext * diff --git a/cogl/cogl/driver/gl/cogl-util-gl.c b/cogl/cogl/driver/gl/cogl-util-gl.c index c49c5289f..1e2381934 100644 --- a/cogl/cogl/driver/gl/cogl-util-gl.c +++ b/cogl/cogl/driver/gl/cogl-util-gl.c @@ -93,6 +93,7 @@ _cogl_driver_gl_context_init (CoglContext *context) if (!gl_context) return FALSE; + gl_context->next_fake_sampler_object_number = 1; gl_context->texture_units = g_array_new (FALSE, FALSE, sizeof (CoglTextureUnit)); diff --git a/cogl/cogl/driver/gl/gl/cogl-driver-gl.c b/cogl/cogl/driver/gl/gl/cogl-driver-gl.c index 42351c4d8..d066ff072 100644 --- a/cogl/cogl/driver/gl/gl/cogl-driver-gl.c +++ b/cogl/cogl/driver/gl/gl/cogl-driver-gl.c @@ -42,6 +42,7 @@ #include "driver/gl/cogl-attribute-gl-private.h" #include "driver/gl/cogl-clip-stack-gl-private.h" #include "driver/gl/cogl-buffer-gl-private.h" +#include "driver/gl/cogl-pipeline-opengl-private.h" static gboolean _cogl_driver_gl_real_context_init (CoglContext *context) @@ -562,4 +563,6 @@ _cogl_driver_gl = _cogl_buffer_gl_map_range, _cogl_buffer_gl_unmap, _cogl_buffer_gl_set_data, + _cogl_sampler_gl_init, + _cogl_sampler_gl_free, }; diff --git a/cogl/cogl/driver/gl/gles/cogl-driver-gles.c b/cogl/cogl/driver/gl/gles/cogl-driver-gles.c index d85d2f9f2..34f850fd5 100644 --- a/cogl/cogl/driver/gl/gles/cogl-driver-gles.c +++ b/cogl/cogl/driver/gl/gles/cogl-driver-gles.c @@ -42,6 +42,7 @@ #include "driver/gl/cogl-attribute-gl-private.h" #include "driver/gl/cogl-clip-stack-gl-private.h" #include "driver/gl/cogl-buffer-gl-private.h" +#include "driver/gl/cogl-pipeline-opengl-private.h" #ifndef GL_UNSIGNED_INT_24_8 #define GL_UNSIGNED_INT_24_8 0x84FA @@ -426,4 +427,6 @@ _cogl_driver_gles = _cogl_buffer_gl_map_range, _cogl_buffer_gl_unmap, _cogl_buffer_gl_set_data, + _cogl_sampler_gl_init, + _cogl_sampler_gl_free, };