cogl: Port Shader away from CoglObject

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/3193>
This commit is contained in:
Bilal Elmoussaoui 2023-08-18 11:41:03 +02:00 committed by Marge Bot
parent c1e6948e42
commit 3aaae11d6b
11 changed files with 76 additions and 80 deletions

View File

@ -136,7 +136,7 @@ struct _ClutterShaderEffectPrivate
ClutterShaderType shader_type; ClutterShaderType shader_type;
CoglHandle program; CoglHandle program;
CoglHandle shader; CoglShader *shader;
GHashTable *uniforms; GHashTable *uniforms;
}; };
@ -148,7 +148,7 @@ typedef struct _ClutterShaderEffectClassPrivate
calling set_shader_source. They will be shared by all instances calling set_shader_source. They will be shared by all instances
of this class */ of this class */
CoglHandle program; CoglHandle program;
CoglHandle shader; CoglShader *shader;
} ClutterShaderEffectClassPrivate; } ClutterShaderEffectClassPrivate;
enum enum
@ -177,7 +177,7 @@ clutter_shader_effect_clear (ClutterShaderEffect *self,
if (priv->shader != NULL) if (priv->shader != NULL)
{ {
cogl_object_unref (priv->shader); g_object_unref (priv->shader);
priv->shader = NULL; priv->shader = NULL;
} }
@ -302,7 +302,7 @@ clutter_shader_effect_set_actor (ClutterActorMeta *meta,
G_OBJECT_TYPE_NAME (meta)); G_OBJECT_TYPE_NAME (meta));
} }
static CoglHandle static CoglShader*
clutter_shader_effect_create_shader (ClutterShaderEffect *self) clutter_shader_effect_create_shader (ClutterShaderEffect *self)
{ {
ClutterShaderEffectPrivate *priv = self->priv; ClutterShaderEffectPrivate *priv = self->priv;
@ -361,7 +361,7 @@ clutter_shader_effect_try_static_source (ClutterShaderEffect *self)
cogl_program_link (class_priv->program); cogl_program_link (class_priv->program);
} }
priv->shader = cogl_object_ref (class_priv->shader); priv->shader = g_object_ref (class_priv->shader);
if (class_priv->program != NULL) if (class_priv->program != NULL)
priv->program = cogl_object_ref (class_priv->program); priv->program = cogl_object_ref (class_priv->program);
@ -506,7 +506,7 @@ clutter_shader_effect_new (ClutterShaderType shader_type)
* Return value: (transfer none): a pointer to the shader's handle, * Return value: (transfer none): a pointer to the shader's handle,
* or %NULL * or %NULL
*/ */
CoglHandle CoglShader*
clutter_shader_effect_get_shader (ClutterShaderEffect *effect) clutter_shader_effect_get_shader (ClutterShaderEffect *effect)
{ {
g_return_val_if_fail (CLUTTER_IS_SHADER_EFFECT (effect), g_return_val_if_fail (CLUTTER_IS_SHADER_EFFECT (effect),

View File

@ -93,7 +93,7 @@ void clutter_shader_effect_set_uniform_value (ClutterShaderEffect *ef
const GValue *value); const GValue *value);
CLUTTER_EXPORT CLUTTER_EXPORT
CoglHandle clutter_shader_effect_get_shader (ClutterShaderEffect *effect); CoglShader* clutter_shader_effect_get_shader (ClutterShaderEffect *effect);
CLUTTER_EXPORT CLUTTER_EXPORT
CoglHandle clutter_shader_effect_get_program (ClutterShaderEffect *effect); CoglHandle clutter_shader_effect_get_program (ClutterShaderEffect *effect);

View File

@ -366,7 +366,7 @@ cogl_pipeline_get_user_program (CoglPipeline *pipeline);
* This is an example of how it can be used to associate an ARBfp * This is an example of how it can be used to associate an ARBfp
* program with a #CoglPipeline: * program with a #CoglPipeline:
* |[ * |[
* CoglHandle shader; * CoglShader *shader;
* CoglHandle program; * CoglHandle program;
* CoglPipeline *pipeline; * CoglPipeline *pipeline;
* *

View File

@ -58,7 +58,7 @@ _cogl_program_free (CoglProgram *program)
_COGL_GET_CONTEXT (ctx, NO_RETVAL); _COGL_GET_CONTEXT (ctx, NO_RETVAL);
/* Unref all of the attached shaders and destroy the list */ /* Unref all of the attached shaders and destroy the list */
g_slist_free_full (program->attached_shaders, cogl_object_unref); g_slist_free_full (program->attached_shaders, g_object_unref);
for (i = 0; i < program->custom_uniforms->len; i++) for (i = 0; i < program->custom_uniforms->len; i++)
{ {
@ -92,20 +92,20 @@ cogl_create_program (void)
void void
cogl_program_attach_shader (CoglHandle program_handle, cogl_program_attach_shader (CoglHandle program_handle,
CoglHandle shader_handle) CoglShader *shader)
{ {
CoglProgram *program; CoglProgram *program;
_COGL_GET_CONTEXT (ctx, NO_RETVAL); _COGL_GET_CONTEXT (ctx, NO_RETVAL);
if (!cogl_is_program (program_handle) || !cogl_is_shader (shader_handle)) if (!cogl_is_program (program_handle) || !COGL_IS_SHADER (shader))
return; return;
program = program_handle; program = program_handle;
program->attached_shaders program->attached_shaders
= g_slist_prepend (program->attached_shaders, = g_slist_prepend (program->attached_shaders,
cogl_object_ref (shader_handle)); g_object_ref (shader));
program->age++; program->age++;
} }

View File

@ -30,7 +30,6 @@
#pragma once #pragma once
#include "cogl/cogl-object-private.h"
#include "cogl/deprecated/cogl-shader.h" #include "cogl/deprecated/cogl-shader.h"
#include "cogl/cogl-gl-header.h" #include "cogl/cogl-gl-header.h"
#include "cogl/cogl-pipeline.h" #include "cogl/cogl-pipeline.h"
@ -39,7 +38,8 @@ typedef struct _CoglShader CoglShader;
struct _CoglShader struct _CoglShader
{ {
CoglObject _parent; GObject parent_instance;
GLuint gl_handle; GLuint gl_handle;
CoglPipeline *compilation_pipeline; CoglPipeline *compilation_pipeline;
CoglShaderType type; CoglShaderType type;

View File

@ -31,7 +31,6 @@
#include "cogl-config.h" #include "cogl-config.h"
#include "cogl/cogl-context-private.h" #include "cogl/cogl-context-private.h"
#include "cogl/cogl-object-private.h"
#include "cogl/cogl-glsl-shader-boilerplate.h" #include "cogl/cogl-glsl-shader-boilerplate.h"
#include "cogl/driver/gl/cogl-util-gl-private.h" #include "cogl/driver/gl/cogl-util-gl-private.h"
#include "cogl/deprecated/cogl-shader-private.h" #include "cogl/deprecated/cogl-shader-private.h"
@ -40,13 +39,13 @@
#include <string.h> #include <string.h>
static void _cogl_shader_free (CoglShader *shader); G_DEFINE_TYPE (CoglShader, cogl_shader, G_TYPE_OBJECT);
COGL_HANDLE_DEFINE (Shader, shader);
static void static void
_cogl_shader_free (CoglShader *shader) cogl_shader_dispose (GObject *object)
{ {
CoglShader *shader = COGL_SHADER (object);
/* Frees shader resources but its handle is not /* Frees shader resources but its handle is not
released! Do that separately before this! */ released! Do that separately before this! */
_COGL_GET_CONTEXT (ctx, NO_RETVAL); _COGL_GET_CONTEXT (ctx, NO_RETVAL);
@ -54,10 +53,23 @@ _cogl_shader_free (CoglShader *shader)
if (shader->gl_handle) if (shader->gl_handle)
GE (ctx, glDeleteShader (shader->gl_handle)); GE (ctx, glDeleteShader (shader->gl_handle));
g_free (shader); G_OBJECT_CLASS (cogl_shader_parent_class)->dispose (object);
} }
CoglHandle static void
cogl_shader_init (CoglShader *shader)
{
}
static void
cogl_shader_class_init (CoglShaderClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
object_class->dispose = cogl_shader_dispose;
}
CoglShader*
cogl_create_shader (CoglShaderType type) cogl_create_shader (CoglShaderType type)
{ {
CoglShader *shader; CoglShader *shader;
@ -75,43 +87,31 @@ cogl_create_shader (CoglShaderType type)
return NULL; return NULL;
} }
shader = g_new0 (CoglShader, 1); shader = g_object_new (COGL_TYPE_SHADER, NULL);
shader->gl_handle = 0; shader->gl_handle = 0;
shader->compilation_pipeline = NULL; shader->compilation_pipeline = NULL;
shader->type = type; shader->type = type;
return _cogl_shader_handle_new (shader); return shader;
} }
void void
cogl_shader_source (CoglHandle handle, cogl_shader_source (CoglShader *self,
const char *source) const char *source)
{ {
CoglShader *shader; g_return_if_fail (COGL_IS_SHADER (self));
_COGL_GET_CONTEXT (ctx, NO_RETVAL); _COGL_GET_CONTEXT (ctx, NO_RETVAL);
if (!cogl_is_shader (handle)) self->source = g_strdup (source);
return;
shader = handle;
shader->source = g_strdup (source);
} }
CoglShaderType CoglShaderType
cogl_shader_get_type (CoglHandle handle) cogl_shader_get_shader_type (CoglShader *self)
{ {
CoglShader *shader; g_return_val_if_fail (COGL_IS_SHADER (self), COGL_SHADER_TYPE_VERTEX);
_COGL_GET_CONTEXT (ctx, COGL_SHADER_TYPE_VERTEX); _COGL_GET_CONTEXT (ctx, COGL_SHADER_TYPE_VERTEX);
if (!cogl_is_shader (handle)) return self->type;
{
g_warning ("Non shader handle type passed to cogl_shader_get_type");
return COGL_SHADER_TYPE_VERTEX;
}
shader = handle;
return shader->type;
} }

View File

@ -41,8 +41,9 @@
G_BEGIN_DECLS G_BEGIN_DECLS
/** /**
* SECTION:cogl-shaders * CoglShader:
* @short_description: Functions for accessing the programmable GL pipeline *
* Functions for accessing the programmable GL pipeline
* *
* Cogl allows accessing the GL programmable pipeline in order to create * Cogl allows accessing the GL programmable pipeline in order to create
* vertex and fragment shaders. * vertex and fragment shaders.
@ -219,6 +220,15 @@ G_BEGIN_DECLS
* experimental #CoglShader API is the proposed replacement. * experimental #CoglShader API is the proposed replacement.
*/ */
#define COGL_TYPE_SHADER (cogl_shader_get_type ())
COGL_EXPORT
G_DECLARE_FINAL_TYPE (CoglShader,
cogl_shader,
COGL,
SHADER,
GObject)
/** /**
* CoglShaderType: * CoglShaderType:
* @COGL_SHADER_TYPE_VERTEX: A program for processing vertices * @COGL_SHADER_TYPE_VERTEX: A program for processing vertices
@ -239,30 +249,16 @@ typedef enum
* Create a new shader handle, use cogl_shader_source() to set the * Create a new shader handle, use cogl_shader_source() to set the
* source code to be used on it. * source code to be used on it.
* *
* Returns: a new shader handle. * Returns: (transfer full): a new shader handle.
* Deprecated: 1.16: Use #CoglSnippet api * Deprecated: 1.16: Use #CoglSnippet api
*/ */
COGL_DEPRECATED_FOR (cogl_snippet_) COGL_DEPRECATED_FOR (cogl_snippet_)
COGL_EXPORT CoglHandle COGL_EXPORT CoglShader*
cogl_create_shader (CoglShaderType shader_type); cogl_create_shader (CoglShaderType shader_type);
/**
* cogl_is_shader:
* @handle: A CoglHandle
*
* Gets whether the given handle references an existing shader object.
*
* Returns: %TRUE if the handle references a shader,
* %FALSE otherwise
* Deprecated: 1.16: Use #CoglSnippet api
*/
COGL_DEPRECATED_FOR (cogl_snippet_)
COGL_EXPORT gboolean
cogl_is_shader (CoglHandle handle);
/** /**
* cogl_shader_source: * cogl_shader_source:
* @shader: #CoglHandle for a shader. * @self: A shader.
* @source: Shader source. * @source: Shader source.
* *
* Replaces the current source associated with a shader with a new * Replaces the current source associated with a shader with a new
@ -275,14 +271,14 @@ cogl_is_shader (CoglHandle handle);
*/ */
COGL_DEPRECATED_FOR (cogl_snippet_) COGL_DEPRECATED_FOR (cogl_snippet_)
COGL_EXPORT void COGL_EXPORT void
cogl_shader_source (CoglHandle shader, cogl_shader_source (CoglShader *self,
const char *source); const char *source);
/** /**
* cogl_shader_get_type: * cogl_shader_get_shader_type:
* @handle: #CoglHandle for a shader. * @self: #CoglShader for a shader.
* *
* Retrieves the type of a shader #CoglHandle * Retrieves the type of a shader
* *
* Return value: %COGL_SHADER_TYPE_VERTEX if the shader is a vertex processor * Return value: %COGL_SHADER_TYPE_VERTEX if the shader is a vertex processor
* or %COGL_SHADER_TYPE_FRAGMENT if the shader is a fragment processor * or %COGL_SHADER_TYPE_FRAGMENT if the shader is a fragment processor
@ -290,7 +286,7 @@ cogl_shader_source (CoglHandle shader,
*/ */
COGL_DEPRECATED_FOR (cogl_snippet_) COGL_DEPRECATED_FOR (cogl_snippet_)
COGL_EXPORT CoglShaderType COGL_EXPORT CoglShaderType
cogl_shader_get_type (CoglHandle handle); cogl_shader_get_shader_type (CoglShader *self);
/** /**
* cogl_create_program: * cogl_create_program:
@ -323,7 +319,7 @@ cogl_is_program (CoglHandle handle);
/** /**
* cogl_program_attach_shader: * cogl_program_attach_shader:
* @program_handle: a #CoglHandle for a shdaer program. * @program_handle: a #CoglHandle for a shdaer program.
* @shader_handle: a #CoglHandle for a vertex of fragment shader. * @shader: a #CoglShader for a vertex of fragment shader.
* *
* Attaches a shader to a program object. A program can have multiple * Attaches a shader to a program object. A program can have multiple
* vertex or fragment shaders but only one of them may provide a * vertex or fragment shaders but only one of them may provide a
@ -335,7 +331,7 @@ cogl_is_program (CoglHandle handle);
COGL_DEPRECATED_FOR (cogl_snippet_) COGL_DEPRECATED_FOR (cogl_snippet_)
COGL_EXPORT void COGL_EXPORT void
cogl_program_attach_shader (CoglHandle program_handle, cogl_program_attach_shader (CoglHandle program_handle,
CoglHandle shader_handle); CoglShader *shader);
/** /**
* cogl_program_link: * cogl_program_link:

View File

@ -634,10 +634,9 @@ _cogl_pipeline_progend_glsl_start (CoglPipeline *pipeline)
} }
static void static void
_cogl_shader_compile_real (CoglHandle handle, _cogl_shader_compile_real (CoglShader *shader,
CoglPipeline *pipeline) CoglPipeline *pipeline)
{ {
CoglShader *shader = handle;
GLenum gl_type; GLenum gl_type;
GLint status; GLint status;

View File

@ -187,7 +187,7 @@ on_paint (ClutterActor *actor,
static void static void
set_shader_num (int new_no) set_shader_num (int new_no)
{ {
CoglHandle shader; CoglShader *shader;
CoglHandle program; CoglHandle program;
CoglPipeline *pipeline; CoglPipeline *pipeline;
CoglContext *ctx = CoglContext *ctx =
@ -207,7 +207,7 @@ set_shader_num (int new_no)
program = cogl_create_program (); program = cogl_create_program ();
cogl_program_attach_shader (program, shader); cogl_program_attach_shader (program, shader);
cogl_object_unref (shader); g_object_unref (shader);
cogl_program_link (program); cogl_program_link (program);
uniform_no = cogl_program_get_uniform_location (program, "tex"); uniform_no = cogl_program_get_uniform_location (program, "tex");

View File

@ -31,7 +31,8 @@ paint (TestState *state)
CoglTexture *tex; CoglTexture *tex;
CoglColor color; CoglColor color;
GError *error = NULL; GError *error = NULL;
CoglHandle shader, program; CoglShader *shader;
CoglHandle program;
cogl_color_init_from_4ub (&color, 0, 0, 0, 255); cogl_color_init_from_4ub (&color, 0, 0, 0, 255);
cogl_framebuffer_clear (test_fb, COGL_BUFFER_BIT_COLOR, &color); cogl_framebuffer_clear (test_fb, COGL_BUFFER_BIT_COLOR, &color);
@ -71,7 +72,7 @@ paint (TestState *state)
cogl_program_attach_shader (program, shader); cogl_program_attach_shader (program, shader);
cogl_program_link (program); cogl_program_link (program);
cogl_object_unref (shader); g_object_unref (shader);
/* Draw something without the program */ /* Draw something without the program */
cogl_framebuffer_draw_rectangle (test_fb, pipeline, cogl_framebuffer_draw_rectangle (test_fb, pipeline,

View File

@ -87,7 +87,7 @@ static CoglPipeline *
create_pipeline_for_shader (TestState *state, const char *shader_source) create_pipeline_for_shader (TestState *state, const char *shader_source)
{ {
CoglPipeline *pipeline; CoglPipeline *pipeline;
CoglHandle shader; CoglShader *shader;
CoglHandle program; CoglHandle program;
pipeline = cogl_pipeline_new (test_ctx); pipeline = cogl_pipeline_new (test_ctx);
@ -100,7 +100,7 @@ create_pipeline_for_shader (TestState *state, const char *shader_source)
cogl_pipeline_set_user_program (pipeline, program); cogl_pipeline_set_user_program (pipeline, program);
cogl_object_unref (shader); g_object_unref (shader);
cogl_object_unref (program); cogl_object_unref (program);
return pipeline; return pipeline;