2010-06-15 11:44:52 -04:00
|
|
|
/*
|
|
|
|
* Cogl
|
|
|
|
*
|
|
|
|
* An object oriented GL/GLES Abstraction/Utility Layer
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008,2009,2010 Intel Corporation.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Robert Bragg <robert@linux.intel.com>
|
2010-12-02 09:00:46 -05:00
|
|
|
* Neil Roberts <neil@linux.intel.com>
|
2010-06-15 11:44:52 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2010-11-19 05:44:27 -05:00
|
|
|
#include <string.h>
|
|
|
|
|
2010-10-27 13:54:57 -04:00
|
|
|
#include "cogl-pipeline-private.h"
|
2010-08-12 05:37:55 -04:00
|
|
|
#include "cogl-shader-private.h"
|
2010-11-19 05:44:27 -05:00
|
|
|
#include "cogl-blend-string.h"
|
2010-07-01 20:07:36 -04:00
|
|
|
|
2010-11-29 11:56:41 -05:00
|
|
|
#ifdef COGL_PIPELINE_FRAGEND_GLSL
|
2010-07-01 20:07:36 -04:00
|
|
|
|
2010-06-15 11:44:52 -04:00
|
|
|
#include "cogl.h"
|
|
|
|
#include "cogl-internal.h"
|
|
|
|
#include "cogl-context.h"
|
|
|
|
#include "cogl-handle.h"
|
2010-08-12 09:05:01 -04:00
|
|
|
#include "cogl-shader-private.h"
|
2010-10-15 13:00:29 -04:00
|
|
|
#include "cogl-program-private.h"
|
2010-06-15 11:44:52 -04:00
|
|
|
|
2010-10-15 13:00:29 -04:00
|
|
|
#ifndef HAVE_COGL_GLES2
|
2010-09-13 06:30:30 -04:00
|
|
|
|
2010-11-19 05:44:27 -05:00
|
|
|
#define glCreateShader ctx->drv.pf_glCreateShader
|
|
|
|
#define glGetShaderiv ctx->drv.pf_glGetShaderiv
|
|
|
|
#define glGetShaderInfoLog ctx->drv.pf_glGetShaderInfoLog
|
|
|
|
#define glCompileShader ctx->drv.pf_glCompileShader
|
|
|
|
#define glShaderSource ctx->drv.pf_glShaderSource
|
|
|
|
#define glDeleteShader ctx->drv.pf_glDeleteShader
|
2010-10-15 13:00:29 -04:00
|
|
|
|
|
|
|
#endif /* HAVE_COGL_GLES2 */
|
2010-06-15 11:44:52 -04:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
/*
|
2010-10-27 13:54:57 -04:00
|
|
|
* GL/GLES compatability defines for pipeline thingies:
|
2010-06-15 11:44:52 -04:00
|
|
|
*/
|
|
|
|
|
2010-11-19 05:44:27 -05:00
|
|
|
/* This might not be defined on GLES */
|
|
|
|
#ifndef GL_TEXTURE_3D
|
|
|
|
#define GL_TEXTURE_3D 0x806F
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct _UnitState
|
|
|
|
{
|
|
|
|
unsigned int sampled:1;
|
|
|
|
unsigned int combine_constant_used:1;
|
|
|
|
} UnitState;
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
typedef struct _GlslShaderState
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
|
|
|
int ref_count;
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
GLuint gl_shader;
|
2010-11-19 05:44:27 -05:00
|
|
|
GString *header, *source;
|
|
|
|
UnitState *unit_state;
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
/* Age of the user program that was current when the shader was
|
|
|
|
generated. We need to keep track of this because if the user
|
|
|
|
program changes then we may need to redecide whether to generate
|
|
|
|
a shader at all */
|
|
|
|
unsigned int user_program_age;
|
|
|
|
} GlslShaderState;
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2010-11-29 11:56:41 -05:00
|
|
|
typedef struct _CoglPipelineFragendGlslPrivate
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
2010-12-02 09:00:46 -05:00
|
|
|
GlslShaderState *glsl_shader_state;
|
2010-11-29 11:56:41 -05:00
|
|
|
} CoglPipelineFragendGlslPrivate;
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2010-11-29 11:56:41 -05:00
|
|
|
const CoglPipelineFragend _cogl_pipeline_glsl_backend;
|
2010-09-14 07:13:30 -04:00
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
static GlslShaderState *
|
|
|
|
glsl_shader_state_new (int n_layers)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
2010-12-02 09:00:46 -05:00
|
|
|
GlslShaderState *state = g_slice_new0 (GlslShaderState);
|
2010-10-15 13:00:29 -04:00
|
|
|
|
|
|
|
state->ref_count = 1;
|
2010-11-19 05:44:27 -05:00
|
|
|
state->unit_state = g_new0 (UnitState, n_layers);
|
2010-10-15 13:00:29 -04:00
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
static GlslShaderState *
|
|
|
|
glsl_shader_state_ref (GlslShaderState *state)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
|
|
|
state->ref_count++;
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-12-02 09:00:46 -05:00
|
|
|
glsl_shader_state_unref (GlslShaderState *state)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
|
|
|
g_return_if_fail (state->ref_count > 0);
|
|
|
|
|
|
|
|
state->ref_count--;
|
|
|
|
if (state->ref_count == 0)
|
|
|
|
{
|
2010-12-02 09:00:46 -05:00
|
|
|
if (state->gl_shader)
|
|
|
|
GE( glDeleteShader (state->gl_shader) );
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2010-11-19 05:44:27 -05:00
|
|
|
g_free (state->unit_state);
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
g_slice_free (GlslShaderState, state);
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-29 11:56:41 -05:00
|
|
|
static CoglPipelineFragendGlslPrivate *
|
2010-10-27 13:54:57 -04:00
|
|
|
get_glsl_priv (CoglPipeline *pipeline)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
2010-11-29 11:56:41 -05:00
|
|
|
if (!(pipeline->fragend_priv_set_mask & COGL_PIPELINE_FRAGEND_GLSL_MASK))
|
2010-10-15 13:00:29 -04:00
|
|
|
return NULL;
|
|
|
|
|
2010-11-29 11:56:41 -05:00
|
|
|
return pipeline->fragend_privs[COGL_PIPELINE_FRAGEND_GLSL];
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-11-29 11:56:41 -05:00
|
|
|
set_glsl_priv (CoglPipeline *pipeline, CoglPipelineFragendGlslPrivate *priv)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
|
|
|
if (priv)
|
|
|
|
{
|
2010-11-29 11:56:41 -05:00
|
|
|
pipeline->fragend_privs[COGL_PIPELINE_FRAGEND_GLSL] = priv;
|
|
|
|
pipeline->fragend_priv_set_mask |= COGL_PIPELINE_FRAGEND_GLSL_MASK;
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
|
|
|
else
|
2010-11-29 11:56:41 -05:00
|
|
|
pipeline->fragend_priv_set_mask &= ~COGL_PIPELINE_FRAGEND_GLSL_MASK;
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
static GlslShaderState *
|
|
|
|
get_glsl_shader_state (CoglPipeline *pipeline)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
2010-11-29 11:56:41 -05:00
|
|
|
CoglPipelineFragendGlslPrivate *priv = get_glsl_priv (pipeline);
|
2010-10-15 13:00:29 -04:00
|
|
|
if (!priv)
|
|
|
|
return NULL;
|
2010-12-02 09:00:46 -05:00
|
|
|
return priv->glsl_shader_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint
|
|
|
|
_cogl_pipeline_fragend_glsl_get_shader (CoglPipeline *pipeline)
|
|
|
|
{
|
|
|
|
GlslShaderState *glsl_shader_state = get_glsl_shader_state (pipeline);
|
|
|
|
|
|
|
|
if (glsl_shader_state)
|
|
|
|
return glsl_shader_state->gl_shader;
|
|
|
|
else
|
|
|
|
return 0;
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-12-02 09:00:46 -05:00
|
|
|
dirty_glsl_shader_state (CoglPipeline *pipeline)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
2010-11-29 11:56:41 -05:00
|
|
|
CoglPipelineFragendGlslPrivate *priv;
|
2010-10-15 13:00:29 -04:00
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
2010-10-27 13:54:57 -04:00
|
|
|
priv = get_glsl_priv (pipeline);
|
2010-10-15 13:00:29 -04:00
|
|
|
if (!priv)
|
|
|
|
return;
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
if (priv->glsl_shader_state)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
2010-12-02 09:00:46 -05:00
|
|
|
glsl_shader_state_unref (priv->glsl_shader_state);
|
|
|
|
priv->glsl_shader_state = NULL;
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-15 11:44:52 -04:00
|
|
|
static gboolean
|
2010-11-29 11:56:41 -05:00
|
|
|
_cogl_pipeline_fragend_glsl_start (CoglPipeline *pipeline,
|
2010-06-15 11:44:52 -04:00
|
|
|
int n_layers,
|
2010-12-02 16:08:30 -05:00
|
|
|
unsigned long pipelines_difference,
|
|
|
|
int n_tex_coord_attribs)
|
2010-06-15 11:44:52 -04:00
|
|
|
{
|
2010-11-29 11:56:41 -05:00
|
|
|
CoglPipelineFragendGlslPrivate *priv;
|
2010-10-27 13:54:57 -04:00
|
|
|
CoglPipeline *authority;
|
2010-11-29 11:56:41 -05:00
|
|
|
CoglPipelineFragendGlslPrivate *authority_priv;
|
2010-10-15 13:00:29 -04:00
|
|
|
CoglProgram *user_program;
|
2010-11-19 05:44:27 -05:00
|
|
|
int i;
|
2010-08-04 12:53:51 -04:00
|
|
|
|
2010-06-15 11:44:52 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, FALSE);
|
|
|
|
|
|
|
|
if (!cogl_features_available (COGL_FEATURE_SHADERS_GLSL))
|
|
|
|
return FALSE;
|
|
|
|
|
2010-10-27 13:54:57 -04:00
|
|
|
user_program = cogl_pipeline_get_user_program (pipeline);
|
2010-12-02 09:00:46 -05:00
|
|
|
|
2010-12-02 15:48:45 -05:00
|
|
|
/* If the user fragment shader isn't GLSL then we should let
|
|
|
|
another backend handle it */
|
2010-11-19 05:44:27 -05:00
|
|
|
if (user_program &&
|
2010-12-02 15:48:45 -05:00
|
|
|
_cogl_program_has_fragment_shader (user_program) &&
|
2010-10-15 13:00:29 -04:00
|
|
|
_cogl_program_get_language (user_program) != COGL_SHADER_LANGUAGE_GLSL)
|
2010-11-19 05:44:27 -05:00
|
|
|
return FALSE;
|
2010-06-15 11:44:52 -04:00
|
|
|
|
2010-10-15 13:00:29 -04:00
|
|
|
/* Now lookup our glsl backend private state (allocating if
|
|
|
|
* necessary) */
|
2010-10-27 13:54:57 -04:00
|
|
|
priv = get_glsl_priv (pipeline);
|
2010-10-15 13:00:29 -04:00
|
|
|
if (!priv)
|
|
|
|
{
|
2010-11-29 11:56:41 -05:00
|
|
|
priv = g_slice_new0 (CoglPipelineFragendGlslPrivate);
|
2010-10-27 13:54:57 -04:00
|
|
|
set_glsl_priv (pipeline, priv);
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
if (!priv->glsl_shader_state)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
2010-12-02 09:00:46 -05:00
|
|
|
/* If we don't have an associated glsl shader yet then find the
|
2010-10-15 13:00:29 -04:00
|
|
|
* glsl-authority (the oldest ancestor whose state will result in
|
2010-12-02 09:00:46 -05:00
|
|
|
* the same shader being generated as for this pipeline).
|
2010-10-15 13:00:29 -04:00
|
|
|
*
|
2010-12-02 09:00:46 -05:00
|
|
|
* We always make sure to associate new shader with the
|
2010-10-27 13:54:57 -04:00
|
|
|
* glsl-authority to maximize the chance that other pipelines can
|
2010-10-15 13:00:29 -04:00
|
|
|
* share it.
|
|
|
|
*/
|
2010-12-01 12:06:18 -05:00
|
|
|
authority = _cogl_pipeline_find_equivalent_parent
|
|
|
|
(pipeline,
|
|
|
|
COGL_PIPELINE_STATE_AFFECTS_FRAGMENT_CODEGEN &
|
|
|
|
~COGL_PIPELINE_STATE_LAYERS,
|
|
|
|
COGL_PIPELINE_LAYER_STATE_AFFECTS_FRAGMENT_CODEGEN,
|
|
|
|
COGL_PIPELINE_FIND_EQUIVALENT_COMPARE_TEXTURE_TARGET);
|
|
|
|
|
2010-10-15 13:00:29 -04:00
|
|
|
authority_priv = get_glsl_priv (authority);
|
|
|
|
if (!authority_priv)
|
|
|
|
{
|
2010-11-29 11:56:41 -05:00
|
|
|
authority_priv = g_slice_new0 (CoglPipelineFragendGlslPrivate);
|
2010-10-15 13:00:29 -04:00
|
|
|
set_glsl_priv (authority, authority_priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we don't have an existing program associated with the
|
2010-12-02 09:00:46 -05:00
|
|
|
* glsl-authority then start generating code for a new shader...
|
2010-10-15 13:00:29 -04:00
|
|
|
*/
|
2010-12-02 09:00:46 -05:00
|
|
|
if (!authority_priv->glsl_shader_state)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
2010-12-02 09:00:46 -05:00
|
|
|
GlslShaderState *glsl_shader_state =
|
|
|
|
glsl_shader_state_new (n_layers);
|
|
|
|
authority_priv->glsl_shader_state = glsl_shader_state;
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
2010-11-19 05:44:27 -05:00
|
|
|
|
|
|
|
/* If the pipeline isn't actually its own glsl-authority
|
|
|
|
* then take a reference to the program state associated
|
|
|
|
* with the glsl-authority... */
|
|
|
|
if (authority != pipeline)
|
2010-12-02 09:00:46 -05:00
|
|
|
priv->glsl_shader_state =
|
|
|
|
glsl_shader_state_ref (authority_priv->glsl_shader_state);
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
if (priv->glsl_shader_state->gl_shader)
|
2010-12-02 11:30:55 -05:00
|
|
|
{
|
2010-12-02 09:00:46 -05:00
|
|
|
/* If we already have a valid GLSL shader then we don't need to
|
|
|
|
generate a new one. However if there's a user program and it
|
|
|
|
has changed since the last link then we do need a new shader */
|
2010-12-02 11:30:55 -05:00
|
|
|
if (user_program == NULL ||
|
2010-12-02 09:00:46 -05:00
|
|
|
(priv->glsl_shader_state->user_program_age == user_program->age))
|
2010-12-02 11:30:55 -05:00
|
|
|
return TRUE;
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
/* We need to recreate the shader so destroy the existing one */
|
|
|
|
GE( glDeleteShader (priv->glsl_shader_state->gl_shader) );
|
|
|
|
priv->glsl_shader_state->gl_shader = 0;
|
2010-12-02 11:30:55 -05:00
|
|
|
}
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
/* If we make it here then we have a glsl_shader_state struct
|
|
|
|
without a gl_shader either because this is the first time we've
|
|
|
|
encountered it or because the user program has changed */
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2010-11-19 05:44:27 -05:00
|
|
|
if (user_program)
|
2010-12-02 09:00:46 -05:00
|
|
|
priv->glsl_shader_state->user_program_age = user_program->age;
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
/* If the user program contains a fragment shader then we don't need
|
|
|
|
to generate one */
|
|
|
|
if (user_program &&
|
|
|
|
_cogl_program_has_fragment_shader (user_program))
|
|
|
|
return TRUE;
|
2010-11-19 05:44:27 -05:00
|
|
|
|
|
|
|
/* We reuse two grow-only GStrings for code-gen. One string
|
|
|
|
contains the uniform and attribute declarations while the
|
|
|
|
other contains the main function. We need two strings
|
|
|
|
because we need to dynamically declare attributes as the
|
|
|
|
add_layer callback is invoked */
|
2010-12-02 13:05:22 -05:00
|
|
|
g_string_set_size (ctx->codegen_header_buffer, 0);
|
|
|
|
g_string_set_size (ctx->codegen_source_buffer, 0);
|
|
|
|
priv->glsl_shader_state->header = ctx->codegen_header_buffer;
|
|
|
|
priv->glsl_shader_state->source = ctx->codegen_source_buffer;
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
g_string_append (priv->glsl_shader_state->source,
|
2010-11-19 05:44:27 -05:00
|
|
|
"void\n"
|
|
|
|
"main ()\n"
|
|
|
|
"{\n");
|
|
|
|
|
|
|
|
for (i = 0; i < n_layers; i++)
|
2010-07-23 12:46:41 -04:00
|
|
|
{
|
2010-12-02 09:00:46 -05:00
|
|
|
priv->glsl_shader_state->unit_state[i].sampled = FALSE;
|
|
|
|
priv->glsl_shader_state->unit_state[i].combine_constant_used = FALSE;
|
2010-07-23 12:46:41 -04:00
|
|
|
}
|
|
|
|
|
2010-11-19 05:44:27 -05:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-12-02 09:00:46 -05:00
|
|
|
add_constant_lookup (GlslShaderState *glsl_shader_state,
|
2010-11-19 05:44:27 -05:00
|
|
|
CoglPipeline *pipeline,
|
|
|
|
CoglPipelineLayer *layer,
|
|
|
|
const char *swizzle)
|
|
|
|
{
|
|
|
|
int unit_index = _cogl_pipeline_layer_get_unit_index (layer);
|
|
|
|
|
|
|
|
/* Create a sampler uniform for this layer if we haven't already */
|
2010-12-02 09:00:46 -05:00
|
|
|
if (!glsl_shader_state->unit_state[unit_index].combine_constant_used)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
2010-12-02 09:00:46 -05:00
|
|
|
g_string_append_printf (glsl_shader_state->header,
|
2010-11-19 05:44:27 -05:00
|
|
|
"uniform vec4 _cogl_layer_constant_%i;\n",
|
|
|
|
unit_index);
|
2010-12-02 09:00:46 -05:00
|
|
|
glsl_shader_state->unit_state[unit_index].combine_constant_used = TRUE;
|
2010-11-19 05:44:27 -05:00
|
|
|
}
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
g_string_append_printf (glsl_shader_state->source,
|
2010-11-19 05:44:27 -05:00
|
|
|
"_cogl_layer_constant_%i.%s",
|
|
|
|
unit_index, swizzle);
|
|
|
|
}
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2010-11-19 05:44:27 -05:00
|
|
|
static void
|
2010-12-02 09:00:46 -05:00
|
|
|
add_texture_lookup (GlslShaderState *glsl_shader_state,
|
2010-11-19 05:44:27 -05:00
|
|
|
CoglPipeline *pipeline,
|
|
|
|
CoglPipelineLayer *layer,
|
|
|
|
const char *swizzle)
|
|
|
|
{
|
|
|
|
CoglHandle texture;
|
|
|
|
int unit_index = _cogl_pipeline_layer_get_unit_index (layer);
|
|
|
|
const char *target_string, *tex_coord_swizzle;
|
|
|
|
|
|
|
|
texture = _cogl_pipeline_layer_get_texture (layer);
|
2010-07-23 12:46:41 -04:00
|
|
|
|
2010-11-19 05:44:27 -05:00
|
|
|
if (texture == COGL_INVALID_HANDLE)
|
|
|
|
{
|
|
|
|
target_string = "2D";
|
|
|
|
tex_coord_swizzle = "st";
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
2010-11-19 05:44:27 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
GLenum gl_target;
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2010-11-19 05:44:27 -05:00
|
|
|
cogl_texture_get_gl_texture (texture, NULL, &gl_target);
|
|
|
|
switch (gl_target)
|
|
|
|
{
|
|
|
|
#ifndef HAVE_COGL_GLES2
|
|
|
|
case GL_TEXTURE_1D:
|
|
|
|
target_string = "1D";
|
|
|
|
tex_coord_swizzle = "s";
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case GL_TEXTURE_2D:
|
|
|
|
target_string = "2D";
|
|
|
|
tex_coord_swizzle = "st";
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef GL_ARB_texture_rectangle
|
|
|
|
case GL_TEXTURE_RECTANGLE_ARB:
|
|
|
|
target_string = "2DRect";
|
|
|
|
tex_coord_swizzle = "st";
|
|
|
|
break;
|
2010-07-23 12:46:41 -04:00
|
|
|
#endif
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2010-11-19 05:44:27 -05:00
|
|
|
case GL_TEXTURE_3D:
|
|
|
|
target_string = "3D";
|
|
|
|
tex_coord_swizzle = "stp";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a sampler uniform for this layer if we haven't already */
|
2010-12-02 09:00:46 -05:00
|
|
|
if (!glsl_shader_state->unit_state[unit_index].sampled)
|
2010-11-19 05:44:27 -05:00
|
|
|
{
|
2010-12-02 09:00:46 -05:00
|
|
|
g_string_append_printf (glsl_shader_state->header,
|
2010-11-19 05:44:27 -05:00
|
|
|
"uniform sampler%s _cogl_sampler_%i;\n",
|
|
|
|
target_string,
|
|
|
|
unit_index);
|
2010-12-02 09:00:46 -05:00
|
|
|
glsl_shader_state->unit_state[unit_index].sampled = TRUE;
|
2010-11-19 05:44:27 -05:00
|
|
|
}
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
g_string_append_printf (glsl_shader_state->source,
|
2010-11-19 05:44:27 -05:00
|
|
|
"texture%s (_cogl_sampler_%i, ",
|
|
|
|
target_string, unit_index);
|
|
|
|
|
|
|
|
/* If point sprite coord generation is being used then divert to the
|
|
|
|
built-in varying var for that instead of the texture
|
2010-11-24 10:30:56 -05:00
|
|
|
coordinates. We don't want to do this under GL because in that
|
|
|
|
case we will instead use glTexEnv(GL_COORD_REPLACE) to replace
|
|
|
|
the texture coords with the point sprite coords. Although GL also
|
|
|
|
supports the gl_PointCoord variable, it requires GLSL 1.2 which
|
|
|
|
would mean we would have to declare the GLSL version and check
|
|
|
|
for it */
|
|
|
|
#ifdef HAVE_COGL_GLES2
|
2010-11-19 05:44:27 -05:00
|
|
|
if (cogl_pipeline_get_layer_point_sprite_coords_enabled (pipeline,
|
|
|
|
layer->index))
|
2010-12-02 09:00:46 -05:00
|
|
|
g_string_append_printf (glsl_shader_state->source,
|
2010-11-19 05:44:27 -05:00
|
|
|
"gl_PointCoord.%s",
|
|
|
|
tex_coord_swizzle);
|
|
|
|
else
|
2010-11-24 10:30:56 -05:00
|
|
|
#endif
|
2010-12-02 09:00:46 -05:00
|
|
|
g_string_append_printf (glsl_shader_state->source,
|
2010-11-19 05:44:27 -05:00
|
|
|
"cogl_tex_coord_in[%d].%s",
|
|
|
|
unit_index, tex_coord_swizzle);
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
g_string_append_printf (glsl_shader_state->source, ").%s", swizzle);
|
2010-11-19 05:44:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int unit_index;
|
|
|
|
CoglPipelineLayer *layer;
|
|
|
|
} FindPipelineLayerData;
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
find_pipeline_layer_cb (CoglPipelineLayer *layer,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
FindPipelineLayerData *data = user_data;
|
|
|
|
int unit_index;
|
|
|
|
|
|
|
|
unit_index = _cogl_pipeline_layer_get_unit_index (layer);
|
|
|
|
|
|
|
|
if (unit_index == data->unit_index)
|
|
|
|
{
|
|
|
|
data->layer = layer;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2010-08-04 12:53:51 -04:00
|
|
|
return TRUE;
|
2010-06-15 11:44:52 -04:00
|
|
|
}
|
|
|
|
|
2010-11-19 05:44:27 -05:00
|
|
|
static void
|
2010-12-02 09:00:46 -05:00
|
|
|
add_arg (GlslShaderState *glsl_shader_state,
|
2010-11-19 05:44:27 -05:00
|
|
|
CoglPipeline *pipeline,
|
|
|
|
CoglPipelineLayer *layer,
|
2010-12-06 16:29:56 -05:00
|
|
|
CoglPipelineCombineSource src,
|
|
|
|
CoglPipelineCombineOp operand,
|
2010-11-19 05:44:27 -05:00
|
|
|
const char *swizzle)
|
|
|
|
{
|
2010-12-02 09:00:46 -05:00
|
|
|
GString *shader_source = glsl_shader_state->source;
|
2010-11-19 05:44:27 -05:00
|
|
|
char alpha_swizzle[5] = "aaaa";
|
|
|
|
|
|
|
|
g_string_append_c (shader_source, '(');
|
|
|
|
|
2010-12-06 16:29:56 -05:00
|
|
|
if (operand == COGL_PIPELINE_COMBINE_OP_ONE_MINUS_SRC_COLOR ||
|
|
|
|
operand == COGL_PIPELINE_COMBINE_OP_ONE_MINUS_SRC_ALPHA)
|
2010-11-19 05:44:27 -05:00
|
|
|
g_string_append_printf (shader_source,
|
|
|
|
"vec4(1.0, 1.0, 1.0, 1.0).%s - ",
|
|
|
|
swizzle);
|
|
|
|
|
|
|
|
/* If the operand is reading from the alpha then replace the swizzle
|
|
|
|
with the same number of copies of the alpha */
|
2010-12-06 16:29:56 -05:00
|
|
|
if (operand == COGL_PIPELINE_COMBINE_OP_SRC_ALPHA ||
|
|
|
|
operand == COGL_PIPELINE_COMBINE_OP_ONE_MINUS_SRC_ALPHA)
|
2010-11-19 05:44:27 -05:00
|
|
|
{
|
|
|
|
alpha_swizzle[strlen (swizzle)] = '\0';
|
|
|
|
swizzle = alpha_swizzle;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (src)
|
|
|
|
{
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_SOURCE_TEXTURE:
|
2010-12-02 09:00:46 -05:00
|
|
|
add_texture_lookup (glsl_shader_state,
|
2010-11-19 05:44:27 -05:00
|
|
|
pipeline,
|
|
|
|
layer,
|
|
|
|
swizzle);
|
|
|
|
break;
|
|
|
|
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_SOURCE_CONSTANT:
|
2010-12-02 09:00:46 -05:00
|
|
|
add_constant_lookup (glsl_shader_state,
|
2010-11-19 05:44:27 -05:00
|
|
|
pipeline,
|
|
|
|
layer,
|
|
|
|
swizzle);
|
|
|
|
break;
|
|
|
|
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_SOURCE_PREVIOUS:
|
2010-11-19 05:44:27 -05:00
|
|
|
if (_cogl_pipeline_layer_get_unit_index (layer) > 0)
|
|
|
|
{
|
|
|
|
g_string_append_printf (shader_source, "cogl_color_out.%s", swizzle);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* flow through */
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_SOURCE_PRIMARY_COLOR:
|
2010-11-19 05:44:27 -05:00
|
|
|
g_string_append_printf (shader_source, "cogl_color_in.%s", swizzle);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2010-12-06 16:29:56 -05:00
|
|
|
if (src >= COGL_PIPELINE_COMBINE_SOURCE_TEXTURE0 &&
|
|
|
|
src < COGL_PIPELINE_COMBINE_SOURCE_TEXTURE0 + 32)
|
2010-11-19 05:44:27 -05:00
|
|
|
{
|
|
|
|
FindPipelineLayerData data;
|
|
|
|
|
2010-12-06 16:29:56 -05:00
|
|
|
data.unit_index = src - COGL_PIPELINE_COMBINE_SOURCE_TEXTURE0;
|
2010-11-19 05:44:27 -05:00
|
|
|
data.layer = layer;
|
|
|
|
|
|
|
|
_cogl_pipeline_foreach_layer_internal (pipeline,
|
|
|
|
find_pipeline_layer_cb,
|
|
|
|
&data);
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
add_texture_lookup (glsl_shader_state,
|
2010-11-19 05:44:27 -05:00
|
|
|
pipeline,
|
|
|
|
data.layer,
|
|
|
|
swizzle);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_string_append_c (shader_source, ')');
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
append_masked_combine (CoglPipeline *pipeline,
|
|
|
|
CoglPipelineLayer *layer,
|
|
|
|
const char *swizzle,
|
2010-12-06 16:29:56 -05:00
|
|
|
CoglPipelineCombineFunc function,
|
|
|
|
CoglPipelineCombineSource *src,
|
|
|
|
CoglPipelineCombineOp *op)
|
2010-11-19 05:44:27 -05:00
|
|
|
{
|
2010-12-02 09:00:46 -05:00
|
|
|
GlslShaderState *glsl_shader_state = get_glsl_shader_state (pipeline);
|
|
|
|
GString *shader_source = glsl_shader_state->source;
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
g_string_append_printf (glsl_shader_state->source,
|
2010-11-19 05:44:27 -05:00
|
|
|
" cogl_color_out.%s = ", swizzle);
|
|
|
|
|
|
|
|
switch (function)
|
|
|
|
{
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_FUNC_REPLACE:
|
2010-12-02 09:00:46 -05:00
|
|
|
add_arg (glsl_shader_state, pipeline, layer,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[0], op[0], swizzle);
|
|
|
|
break;
|
|
|
|
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_FUNC_MODULATE:
|
2010-12-02 09:00:46 -05:00
|
|
|
add_arg (glsl_shader_state, pipeline, layer,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[0], op[0], swizzle);
|
|
|
|
g_string_append (shader_source, " * ");
|
2010-12-02 09:00:46 -05:00
|
|
|
add_arg (glsl_shader_state, pipeline, layer,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[1], op[1], swizzle);
|
|
|
|
break;
|
|
|
|
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_FUNC_ADD:
|
2010-12-02 09:00:46 -05:00
|
|
|
add_arg (glsl_shader_state, pipeline, layer,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[0], op[0], swizzle);
|
|
|
|
g_string_append (shader_source, " + ");
|
2010-12-02 09:00:46 -05:00
|
|
|
add_arg (glsl_shader_state, pipeline, layer,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[1], op[1], swizzle);
|
|
|
|
break;
|
|
|
|
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_FUNC_ADD_SIGNED:
|
2010-12-02 09:00:46 -05:00
|
|
|
add_arg (glsl_shader_state, pipeline, layer,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[0], op[0], swizzle);
|
|
|
|
g_string_append (shader_source, " + ");
|
2010-12-02 09:00:46 -05:00
|
|
|
add_arg (glsl_shader_state, pipeline, layer,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[1], op[1], swizzle);
|
|
|
|
g_string_append_printf (shader_source,
|
|
|
|
" - vec4(0.5, 0.5, 0.5, 0.5).%s",
|
|
|
|
swizzle);
|
|
|
|
break;
|
|
|
|
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_FUNC_SUBTRACT:
|
2010-12-02 09:00:46 -05:00
|
|
|
add_arg (glsl_shader_state, pipeline, layer,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[0], op[0], swizzle);
|
|
|
|
g_string_append (shader_source, " - ");
|
2010-12-02 09:00:46 -05:00
|
|
|
add_arg (glsl_shader_state, pipeline, layer,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[1], op[1], swizzle);
|
|
|
|
break;
|
|
|
|
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_FUNC_INTERPOLATE:
|
2010-12-02 09:00:46 -05:00
|
|
|
add_arg (glsl_shader_state, pipeline, layer,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[0], op[0], swizzle);
|
|
|
|
g_string_append (shader_source, " * ");
|
2010-12-02 09:00:46 -05:00
|
|
|
add_arg (glsl_shader_state, pipeline, layer,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[2], op[2], swizzle);
|
|
|
|
g_string_append (shader_source, " + ");
|
2010-12-02 09:00:46 -05:00
|
|
|
add_arg (glsl_shader_state, pipeline, layer,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[1], op[1], swizzle);
|
|
|
|
g_string_append_printf (shader_source,
|
|
|
|
" * (vec4(1.0, 1.0, 1.0, 1.0).%s - ",
|
|
|
|
swizzle);
|
2010-12-02 09:00:46 -05:00
|
|
|
add_arg (glsl_shader_state, pipeline, layer,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[2], op[2], swizzle);
|
|
|
|
g_string_append_c (shader_source, ')');
|
|
|
|
break;
|
|
|
|
|
2010-12-06 16:29:56 -05:00
|
|
|
case COGL_PIPELINE_COMBINE_FUNC_DOT3_RGB:
|
|
|
|
case COGL_PIPELINE_COMBINE_FUNC_DOT3_RGBA:
|
2010-12-02 15:48:45 -05:00
|
|
|
g_string_append (shader_source, "vec4(4.0 * ((");
|
2010-12-02 09:00:46 -05:00
|
|
|
add_arg (glsl_shader_state, pipeline, layer,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[0], op[0], "r");
|
|
|
|
g_string_append (shader_source, " - 0.5) * (");
|
2010-12-02 09:00:46 -05:00
|
|
|
add_arg (glsl_shader_state, pipeline, layer,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[1], op[1], "r");
|
|
|
|
g_string_append (shader_source, " - 0.5) + (");
|
2010-12-02 09:00:46 -05:00
|
|
|
add_arg (glsl_shader_state, pipeline, layer,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[0], op[0], "g");
|
|
|
|
g_string_append (shader_source, " - 0.5) * (");
|
2010-12-02 09:00:46 -05:00
|
|
|
add_arg (glsl_shader_state, pipeline, layer,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[1], op[1], "g");
|
|
|
|
g_string_append (shader_source, " - 0.5) + (");
|
2010-12-02 09:00:46 -05:00
|
|
|
add_arg (glsl_shader_state, pipeline, layer,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[0], op[0], "b");
|
|
|
|
g_string_append (shader_source, " - 0.5) * (");
|
2010-12-02 09:00:46 -05:00
|
|
|
add_arg (glsl_shader_state, pipeline, layer,
|
2010-11-19 05:44:27 -05:00
|
|
|
src[1], op[1], "b");
|
|
|
|
g_string_append_printf (shader_source, " - 0.5))).%s", swizzle);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_string_append_printf (shader_source, ";\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2010-11-29 11:56:41 -05:00
|
|
|
_cogl_pipeline_fragend_glsl_add_layer (CoglPipeline *pipeline,
|
2010-11-19 05:44:27 -05:00
|
|
|
CoglPipelineLayer *layer,
|
|
|
|
unsigned long layers_difference)
|
2010-06-15 11:44:52 -04:00
|
|
|
{
|
2010-12-02 09:00:46 -05:00
|
|
|
GlslShaderState *glsl_shader_state = get_glsl_shader_state (pipeline);
|
2010-11-19 05:44:27 -05:00
|
|
|
CoglPipelineLayer *combine_authority =
|
|
|
|
_cogl_pipeline_layer_get_authority (layer,
|
|
|
|
COGL_PIPELINE_LAYER_STATE_COMBINE);
|
|
|
|
CoglPipelineLayerBigState *big_state = combine_authority->big_state;
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
if (!glsl_shader_state->source)
|
2010-11-19 05:44:27 -05:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (!_cogl_pipeline_need_texture_combine_separate (combine_authority) ||
|
|
|
|
/* GL_DOT3_RGBA Is a bit weird as a GL_COMBINE_RGB function
|
|
|
|
* since if you use it, it overrides your ALPHA function...
|
|
|
|
*/
|
2010-12-06 16:29:56 -05:00
|
|
|
big_state->texture_combine_rgb_func ==
|
|
|
|
COGL_PIPELINE_COMBINE_FUNC_DOT3_RGBA)
|
2010-11-19 05:44:27 -05:00
|
|
|
append_masked_combine (pipeline,
|
|
|
|
layer,
|
|
|
|
"rgba",
|
|
|
|
big_state->texture_combine_rgb_func,
|
|
|
|
big_state->texture_combine_rgb_src,
|
|
|
|
big_state->texture_combine_rgb_op);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
append_masked_combine (pipeline,
|
|
|
|
layer,
|
|
|
|
"rgb",
|
|
|
|
big_state->texture_combine_rgb_func,
|
|
|
|
big_state->texture_combine_rgb_src,
|
|
|
|
big_state->texture_combine_rgb_op);
|
|
|
|
append_masked_combine (pipeline,
|
|
|
|
layer,
|
|
|
|
"a",
|
|
|
|
big_state->texture_combine_alpha_func,
|
|
|
|
big_state->texture_combine_alpha_src,
|
|
|
|
big_state->texture_combine_alpha_op);
|
|
|
|
}
|
|
|
|
|
2010-06-15 11:44:52 -04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2010-11-29 11:56:41 -05:00
|
|
|
_cogl_pipeline_fragend_glsl_passthrough (CoglPipeline *pipeline)
|
2010-06-15 11:44:52 -04:00
|
|
|
{
|
2010-12-02 09:00:46 -05:00
|
|
|
GlslShaderState *glsl_shader_state = get_glsl_shader_state (pipeline);
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
if (!glsl_shader_state->source)
|
2010-11-19 05:44:27 -05:00
|
|
|
return TRUE;
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
g_string_append (glsl_shader_state->source,
|
2010-11-19 05:44:27 -05:00
|
|
|
" cogl_color_out = cogl_color_in;\n");
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-11-22 13:33:49 -05:00
|
|
|
/* GLES2 doesn't have alpha testing so we need to implement it in the
|
|
|
|
shader */
|
|
|
|
|
|
|
|
#ifdef HAVE_COGL_GLES2
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_alpha_test_snippet (CoglPipeline *pipeline,
|
2010-12-02 09:00:46 -05:00
|
|
|
GlslShaderState *glsl_shader_state)
|
2010-11-22 13:33:49 -05:00
|
|
|
{
|
|
|
|
CoglPipelineAlphaFunc alpha_func;
|
|
|
|
|
|
|
|
alpha_func = cogl_pipeline_get_alpha_test_function (pipeline);
|
|
|
|
|
|
|
|
if (alpha_func == COGL_PIPELINE_ALPHA_FUNC_ALWAYS)
|
|
|
|
/* Do nothing */
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (alpha_func == COGL_PIPELINE_ALPHA_FUNC_NEVER)
|
|
|
|
{
|
|
|
|
/* Always discard the fragment */
|
2010-12-02 09:00:46 -05:00
|
|
|
g_string_append (glsl_shader_state->source,
|
2010-11-22 13:33:49 -05:00
|
|
|
" discard;\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For all of the other alpha functions we need a uniform for the
|
|
|
|
reference */
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
g_string_append (glsl_shader_state->header,
|
2010-11-22 13:33:49 -05:00
|
|
|
"uniform float _cogl_alpha_test_ref;\n");
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
g_string_append (glsl_shader_state->source,
|
2010-11-22 13:33:49 -05:00
|
|
|
" if (cogl_color_out.a ");
|
|
|
|
|
|
|
|
switch (alpha_func)
|
|
|
|
{
|
|
|
|
case COGL_PIPELINE_ALPHA_FUNC_LESS:
|
2010-12-02 09:00:46 -05:00
|
|
|
g_string_append (glsl_shader_state->source, ">=");
|
2010-11-22 13:33:49 -05:00
|
|
|
break;
|
|
|
|
case COGL_PIPELINE_ALPHA_FUNC_EQUAL:
|
2010-12-02 09:00:46 -05:00
|
|
|
g_string_append (glsl_shader_state->source, "!=");
|
2010-11-22 13:33:49 -05:00
|
|
|
break;
|
|
|
|
case COGL_PIPELINE_ALPHA_FUNC_LEQUAL:
|
2010-12-02 09:00:46 -05:00
|
|
|
g_string_append (glsl_shader_state->source, ">");
|
2010-11-22 13:33:49 -05:00
|
|
|
break;
|
|
|
|
case COGL_PIPELINE_ALPHA_FUNC_GREATER:
|
2010-12-02 09:00:46 -05:00
|
|
|
g_string_append (glsl_shader_state->source, "<=");
|
2010-11-22 13:33:49 -05:00
|
|
|
break;
|
|
|
|
case COGL_PIPELINE_ALPHA_FUNC_NOTEQUAL:
|
2010-12-02 09:00:46 -05:00
|
|
|
g_string_append (glsl_shader_state->source, "==");
|
2010-11-22 13:33:49 -05:00
|
|
|
break;
|
|
|
|
case COGL_PIPELINE_ALPHA_FUNC_GEQUAL:
|
2010-12-02 09:00:46 -05:00
|
|
|
g_string_append (glsl_shader_state->source, "< ");
|
2010-11-22 13:33:49 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case COGL_PIPELINE_ALPHA_FUNC_ALWAYS:
|
|
|
|
case COGL_PIPELINE_ALPHA_FUNC_NEVER:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
g_string_append (glsl_shader_state->source,
|
2010-11-22 13:33:49 -05:00
|
|
|
" _cogl_alpha_test_ref)\n discard;\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_COGL_GLES2 */
|
|
|
|
|
2010-06-15 11:44:52 -04:00
|
|
|
gboolean
|
2010-11-29 11:56:41 -05:00
|
|
|
_cogl_pipeline_fragend_glsl_end (CoglPipeline *pipeline,
|
2010-10-27 13:54:57 -04:00
|
|
|
unsigned long pipelines_difference)
|
2010-06-15 11:44:52 -04:00
|
|
|
{
|
2010-12-02 09:00:46 -05:00
|
|
|
GlslShaderState *glsl_shader_state = get_glsl_shader_state (pipeline);
|
2010-11-19 05:44:27 -05:00
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, FALSE);
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
if (glsl_shader_state->source)
|
2010-11-19 05:44:27 -05:00
|
|
|
{
|
2010-12-02 09:00:46 -05:00
|
|
|
const char *source_strings[2];
|
|
|
|
GLint lengths[2];
|
|
|
|
GLint compile_status;
|
|
|
|
GLuint shader;
|
|
|
|
int n_tex_coord_attribs = 0;
|
|
|
|
int i, n_layers;
|
|
|
|
|
|
|
|
COGL_STATIC_COUNTER (fragend_glsl_compile_counter,
|
2010-12-02 15:48:45 -05:00
|
|
|
"glsl fragment compile counter",
|
2010-12-02 09:00:46 -05:00
|
|
|
"Increments each time a new GLSL "
|
2010-12-02 15:48:45 -05:00
|
|
|
"fragment shader is compiled",
|
2010-12-02 09:00:46 -05:00
|
|
|
0 /* no application private data */);
|
|
|
|
COGL_COUNTER_INC (_cogl_uprof_context, fragend_glsl_compile_counter);
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2010-11-22 13:33:49 -05:00
|
|
|
#ifdef HAVE_COGL_GLES2
|
2010-12-02 09:00:46 -05:00
|
|
|
add_alpha_test_snippet (pipeline, glsl_shader_state);
|
2010-11-22 13:33:49 -05:00
|
|
|
#endif
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
g_string_append (glsl_shader_state->source, "}\n");
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
GE_RET( shader, glCreateShader (GL_FRAGMENT_SHADER) );
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
lengths[0] = glsl_shader_state->header->len;
|
|
|
|
source_strings[0] = glsl_shader_state->header->str;
|
|
|
|
lengths[1] = glsl_shader_state->source->len;
|
|
|
|
source_strings[1] = glsl_shader_state->source->str;
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
/* Find the highest texture unit that is sampled to pass as the
|
|
|
|
number of texture coordinate attributes */
|
|
|
|
n_layers = cogl_pipeline_get_n_layers (pipeline);
|
|
|
|
for (i = 0; i < n_layers; i++)
|
|
|
|
if (glsl_shader_state->unit_state[i].sampled)
|
|
|
|
n_tex_coord_attribs = i + 1;
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
_cogl_shader_set_source_with_boilerplate (shader, GL_FRAGMENT_SHADER,
|
|
|
|
n_tex_coord_attribs,
|
|
|
|
2, /* count */
|
|
|
|
source_strings, lengths);
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
GE( glCompileShader (shader) );
|
|
|
|
GE( glGetShaderiv (shader, GL_COMPILE_STATUS, &compile_status) );
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
if (!compile_status)
|
|
|
|
{
|
|
|
|
GLint len = 0;
|
|
|
|
char *shader_log;
|
2010-11-19 05:44:27 -05:00
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
GE( glGetShaderiv (shader, GL_INFO_LOG_LENGTH, &len) );
|
|
|
|
shader_log = g_alloca (len);
|
|
|
|
GE( glGetShaderInfoLog (shader, len, &len, shader_log) );
|
|
|
|
g_warning ("Shader compilation failed:\n%s", shader_log);
|
2010-11-19 05:44:27 -05:00
|
|
|
}
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
glsl_shader_state->header = NULL;
|
|
|
|
glsl_shader_state->source = NULL;
|
|
|
|
glsl_shader_state->gl_shader = shader;
|
2010-11-22 13:33:49 -05:00
|
|
|
}
|
2010-10-15 13:00:29 -04:00
|
|
|
|
2010-06-15 11:44:52 -04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-10-15 13:00:29 -04:00
|
|
|
static void
|
2010-11-29 11:56:41 -05:00
|
|
|
_cogl_pipeline_fragend_glsl_pre_change_notify (CoglPipeline *pipeline,
|
2010-10-27 13:54:57 -04:00
|
|
|
CoglPipelineState change,
|
2010-10-15 13:00:29 -04:00
|
|
|
const CoglColor *new_color)
|
|
|
|
{
|
2010-12-02 09:00:46 -05:00
|
|
|
if ((change & COGL_PIPELINE_STATE_AFFECTS_FRAGMENT_CODEGEN))
|
|
|
|
dirty_glsl_shader_state (pipeline);
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
|
|
|
|
2010-11-19 05:44:27 -05:00
|
|
|
/* NB: layers are considered immutable once they have any dependants
|
|
|
|
* so although multiple pipelines can end up depending on a single
|
|
|
|
* static layer, we can guarantee that if a layer is being *changed*
|
|
|
|
* then it can only have one pipeline depending on it.
|
|
|
|
*
|
|
|
|
* XXX: Don't forget this is *pre* change, we can't read the new value
|
|
|
|
* yet!
|
|
|
|
*/
|
|
|
|
static void
|
2010-11-29 11:56:41 -05:00
|
|
|
_cogl_pipeline_fragend_glsl_layer_pre_change_notify (
|
2010-11-19 05:44:27 -05:00
|
|
|
CoglPipeline *owner,
|
|
|
|
CoglPipelineLayer *layer,
|
|
|
|
CoglPipelineLayerState change)
|
|
|
|
{
|
2010-11-29 11:56:41 -05:00
|
|
|
CoglPipelineFragendGlslPrivate *priv;
|
2010-11-19 05:44:27 -05:00
|
|
|
|
|
|
|
priv = get_glsl_priv (owner);
|
|
|
|
if (!priv)
|
|
|
|
return;
|
|
|
|
|
2010-12-02 09:00:46 -05:00
|
|
|
if ((change & COGL_PIPELINE_LAYER_STATE_AFFECTS_FRAGMENT_CODEGEN))
|
2010-11-19 05:44:27 -05:00
|
|
|
{
|
2010-12-02 09:00:46 -05:00
|
|
|
dirty_glsl_shader_state (owner);
|
2010-11-19 05:44:27 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: we could be saving snippets of texture combine code along
|
|
|
|
* with each layer and then when a layer changes we would just free
|
|
|
|
* the snippet. */
|
|
|
|
}
|
|
|
|
|
2010-10-15 13:00:29 -04:00
|
|
|
static void
|
2010-11-29 11:56:41 -05:00
|
|
|
_cogl_pipeline_fragend_glsl_free_priv (CoglPipeline *pipeline)
|
2010-10-15 13:00:29 -04:00
|
|
|
{
|
2010-11-29 11:56:41 -05:00
|
|
|
CoglPipelineFragendGlslPrivate *priv = get_glsl_priv (pipeline);
|
2010-10-15 13:00:29 -04:00
|
|
|
if (priv)
|
|
|
|
{
|
2010-12-02 09:00:46 -05:00
|
|
|
if (priv->glsl_shader_state)
|
|
|
|
glsl_shader_state_unref (priv->glsl_shader_state);
|
2010-11-29 11:56:41 -05:00
|
|
|
g_slice_free (CoglPipelineFragendGlslPrivate, priv);
|
2010-10-27 13:54:57 -04:00
|
|
|
set_glsl_priv (pipeline, NULL);
|
2010-10-15 13:00:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-29 11:56:41 -05:00
|
|
|
const CoglPipelineFragend _cogl_pipeline_glsl_fragend =
|
2010-06-15 11:44:52 -04:00
|
|
|
{
|
2010-11-29 11:56:41 -05:00
|
|
|
_cogl_pipeline_fragend_glsl_start,
|
|
|
|
_cogl_pipeline_fragend_glsl_add_layer,
|
|
|
|
_cogl_pipeline_fragend_glsl_passthrough,
|
|
|
|
_cogl_pipeline_fragend_glsl_end,
|
|
|
|
_cogl_pipeline_fragend_glsl_pre_change_notify,
|
2010-10-27 13:54:57 -04:00
|
|
|
NULL, /* pipeline_set_parent_notify */
|
2010-11-29 11:56:41 -05:00
|
|
|
_cogl_pipeline_fragend_glsl_layer_pre_change_notify,
|
|
|
|
_cogl_pipeline_fragend_glsl_free_priv,
|
2010-06-15 11:44:52 -04:00
|
|
|
};
|
|
|
|
|
2010-11-29 11:56:41 -05:00
|
|
|
#endif /* COGL_PIPELINE_FRAGEND_GLSL */
|
2010-07-01 20:07:36 -04:00
|
|
|
|