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;
CoglHandle program;
CoglHandle shader;
CoglShader *shader;
GHashTable *uniforms;
};
@ -148,7 +148,7 @@ typedef struct _ClutterShaderEffectClassPrivate
calling set_shader_source. They will be shared by all instances
of this class */
CoglHandle program;
CoglHandle shader;
CoglShader *shader;
} ClutterShaderEffectClassPrivate;
enum
@ -177,7 +177,7 @@ clutter_shader_effect_clear (ClutterShaderEffect *self,
if (priv->shader != NULL)
{
cogl_object_unref (priv->shader);
g_object_unref (priv->shader);
priv->shader = NULL;
}
@ -302,7 +302,7 @@ clutter_shader_effect_set_actor (ClutterActorMeta *meta,
G_OBJECT_TYPE_NAME (meta));
}
static CoglHandle
static CoglShader*
clutter_shader_effect_create_shader (ClutterShaderEffect *self)
{
ClutterShaderEffectPrivate *priv = self->priv;
@ -361,7 +361,7 @@ clutter_shader_effect_try_static_source (ClutterShaderEffect *self)
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)
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,
* or %NULL
*/
CoglHandle
CoglShader*
clutter_shader_effect_get_shader (ClutterShaderEffect *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);
CLUTTER_EXPORT
CoglHandle clutter_shader_effect_get_shader (ClutterShaderEffect *effect);
CoglShader* clutter_shader_effect_get_shader (ClutterShaderEffect *effect);
CLUTTER_EXPORT
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
* program with a #CoglPipeline:
* |[
* CoglHandle shader;
* CoglShader *shader;
* CoglHandle program;
* CoglPipeline *pipeline;
*

View File

@ -58,7 +58,7 @@ _cogl_program_free (CoglProgram *program)
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
/* 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++)
{
@ -91,21 +91,21 @@ cogl_create_program (void)
}
void
cogl_program_attach_shader (CoglHandle program_handle,
CoglHandle shader_handle)
cogl_program_attach_shader (CoglHandle program_handle,
CoglShader *shader)
{
CoglProgram *program;
_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;
program = program_handle;
program->attached_shaders
= g_slist_prepend (program->attached_shaders,
cogl_object_ref (shader_handle));
g_object_ref (shader));
program->age++;
}

View File

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

View File

@ -31,7 +31,6 @@
#include "cogl-config.h"
#include "cogl/cogl-context-private.h"
#include "cogl/cogl-object-private.h"
#include "cogl/cogl-glsl-shader-boilerplate.h"
#include "cogl/driver/gl/cogl-util-gl-private.h"
#include "cogl/deprecated/cogl-shader-private.h"
@ -40,24 +39,37 @@
#include <string.h>
static void _cogl_shader_free (CoglShader *shader);
COGL_HANDLE_DEFINE (Shader, shader);
G_DEFINE_TYPE (CoglShader, cogl_shader, G_TYPE_OBJECT);
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
released! Do that separately before this! */
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
if (shader->gl_handle)
GE (ctx, glDeleteShader (shader->gl_handle));
if (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)
{
CoglShader *shader;
@ -75,43 +87,31 @@ cogl_create_shader (CoglShaderType type)
return NULL;
}
shader = g_new0 (CoglShader, 1);
shader = g_object_new (COGL_TYPE_SHADER, NULL);
shader->gl_handle = 0;
shader->compilation_pipeline = NULL;
shader->type = type;
return _cogl_shader_handle_new (shader);
return shader;
}
void
cogl_shader_source (CoglHandle handle,
const char *source)
cogl_shader_source (CoglShader *self,
const char *source)
{
CoglShader *shader;
g_return_if_fail (COGL_IS_SHADER (self));
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
if (!cogl_is_shader (handle))
return;
shader = handle;
shader->source = g_strdup (source);
self->source = g_strdup (source);
}
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);
if (!cogl_is_shader (handle))
{
g_warning ("Non shader handle type passed to cogl_shader_get_type");
return COGL_SHADER_TYPE_VERTEX;
}
shader = handle;
return shader->type;
return self->type;
}

View File

@ -41,8 +41,9 @@
G_BEGIN_DECLS
/**
* SECTION:cogl-shaders
* @short_description: Functions for accessing the programmable GL pipeline
* CoglShader:
*
* Functions for accessing the programmable GL pipeline
*
* Cogl allows accessing the GL programmable pipeline in order to create
* vertex and fragment shaders.
@ -219,6 +220,15 @@ G_BEGIN_DECLS
* 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:
* @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
* 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
*/
COGL_DEPRECATED_FOR (cogl_snippet_)
COGL_EXPORT CoglHandle
COGL_EXPORT CoglShader*
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:
* @shader: #CoglHandle for a shader.
* @self: A shader.
* @source: Shader source.
*
* 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_EXPORT void
cogl_shader_source (CoglHandle shader,
cogl_shader_source (CoglShader *self,
const char *source);
/**
* cogl_shader_get_type:
* @handle: #CoglHandle for a shader.
* cogl_shader_get_shader_type:
* @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
* 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_EXPORT CoglShaderType
cogl_shader_get_type (CoglHandle handle);
cogl_shader_get_shader_type (CoglShader *self);
/**
* cogl_create_program:
@ -323,7 +319,7 @@ cogl_is_program (CoglHandle handle);
/**
* cogl_program_attach_shader:
* @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
* vertex or fragment shaders but only one of them may provide a
@ -334,8 +330,8 @@ cogl_is_program (CoglHandle handle);
*/
COGL_DEPRECATED_FOR (cogl_snippet_)
COGL_EXPORT void
cogl_program_attach_shader (CoglHandle program_handle,
CoglHandle shader_handle);
cogl_program_attach_shader (CoglHandle program_handle,
CoglShader *shader);
/**
* cogl_program_link:

View File

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

View File

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

View File

@ -31,7 +31,8 @@ paint (TestState *state)
CoglTexture *tex;
CoglColor color;
GError *error = NULL;
CoglHandle shader, program;
CoglShader *shader;
CoglHandle program;
cogl_color_init_from_4ub (&color, 0, 0, 0, 255);
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_link (program);
cogl_object_unref (shader);
g_object_unref (shader);
/* Draw something without the program */
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)
{
CoglPipeline *pipeline;
CoglHandle shader;
CoglShader *shader;
CoglHandle program;
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_object_unref (shader);
g_object_unref (shader);
cogl_object_unref (program);
return pipeline;