Example #1
0
static gboolean
clutter_shader_glsl_bind (ClutterShader      *self,
                          ClutterShaderType   shader_type,
                          GError            **error)
{
  ClutterShaderPrivate *priv = self->priv;
  CoglHandle shader = COGL_INVALID_HANDLE;

  switch (shader_type)
    {
    case CLUTTER_VERTEX_SHADER:
      shader = cogl_create_shader (COGL_SHADER_TYPE_VERTEX);
      cogl_shader_source (shader, priv->vertex_source);

      priv->vertex_shader = shader;
      break;

    case CLUTTER_FRAGMENT_SHADER:
      shader = cogl_create_shader (COGL_SHADER_TYPE_FRAGMENT);
      cogl_shader_source (shader, priv->fragment_source);

      priv->fragment_shader = shader;
      break;
    }

  g_assert (shader != COGL_INVALID_HANDLE);

  cogl_shader_compile (shader);
  if (!cogl_shader_is_compiled (shader))
    {
      gchar *log_buf;

      log_buf = cogl_shader_get_info_log (shader);

      /* translators: the first %s is the type of the shader, either
       * Vertex shader or Fragment shader; the second %s is the actual
       * error as reported by COGL
       */
      g_set_error (error, CLUTTER_SHADER_ERROR,
                   CLUTTER_SHADER_ERROR_COMPILE,
                   _("%s compilation failed: %s"),
                   shader_type == CLUTTER_VERTEX_SHADER ? _("Vertex shader")
                                                        : _("Fragment shader"),
                   log_buf);

      g_free (log_buf);

      return FALSE;
    }

  cogl_program_attach_shader (priv->program, shader);

  return TRUE;
}
static void
clutter_gst_video_sink_set_glsl_shader (ClutterGstVideoSink *sink,
                                        const gchar         *shader_src)
{
  ClutterGstVideoSinkPrivate *priv = sink->priv;
  
  if (priv->texture)
    clutter_actor_set_shader (CLUTTER_ACTOR (priv->texture), NULL);

  if (priv->program)
    {
      cogl_program_unref (priv->program);
      priv->program = NULL;
    }
  
  if (priv->shader)
    {
      cogl_shader_unref (priv->shader);
      priv->shader = NULL;
    }
  
  if (shader_src)
    {
      ClutterShader *shader;

      /* Set a dummy shader so we don't interfere with the shader stack */
      shader = clutter_shader_new ();
      clutter_shader_set_fragment_source (shader, dummy_shader, -1);
      clutter_actor_set_shader (CLUTTER_ACTOR (priv->texture), shader);
      g_object_unref (shader);

      /* Create shader through COGL - necessary as we need to be able to set
       * integer uniform variables for multi-texturing.
       */
      priv->shader = cogl_create_shader (COGL_SHADER_TYPE_FRAGMENT);
      cogl_shader_source (priv->shader, shader_src);
      cogl_shader_compile (priv->shader);
      
      priv->program = cogl_create_program ();
      cogl_program_attach_shader (priv->program, priv->shader);
      cogl_program_link (priv->program);
    }
}
static void
st_scroll_view_fade_init (StScrollViewFade *self)
{
  static CoglHandle shader = COGL_INVALID_HANDLE;

  if (shader == COGL_INVALID_HANDLE)
    {
      if (clutter_feature_available (CLUTTER_FEATURE_SHADERS_GLSL))
        {
          shader = cogl_create_shader (COGL_SHADER_TYPE_FRAGMENT);
          cogl_shader_source (shader, fade_glsl_shader);
          cogl_shader_compile (shader);
          if (!cogl_shader_is_compiled (shader))
            {
              gchar *log_buf = cogl_shader_get_info_log (shader);

              g_warning (G_STRLOC ": Unable to compile the fade shader: %s",
                         log_buf);
              g_free (log_buf);

              cogl_handle_unref (shader);
              shader = COGL_INVALID_HANDLE;
          }
        }
    }

  self->shader = shader;
  self->is_attached = FALSE;
  self->tex_uniform = -1;
  self->height_uniform = -1;
  self->width_uniform = -1;
  self->scrollbar_width_uniform = -1;
  self->scrollbar_height_uniform = -1;
  self->rtl_uniform = -1;
  self->offset_top_uniform = -1;
  self->offset_bottom_uniform = -1;
  self->fade_offset = DEFAULT_FADE_OFFSET;

  if (shader != COGL_INVALID_HANDLE)
    cogl_handle_ref (self->shader);
}
Example #4
0
static CoglPipeline *
create_pipeline_for_shader (TestState *state, const char *shader_source)
{
  CoglPipeline *pipeline;
  CoglHandle shader;
  CoglHandle program;

  pipeline = cogl_pipeline_new (test_ctx);

  shader = cogl_create_shader (COGL_SHADER_TYPE_FRAGMENT);
  cogl_shader_source (shader, shader_source);

  program = cogl_create_program ();
  cogl_program_attach_shader (program, shader);

  cogl_pipeline_set_user_program (pipeline, program);

  cogl_handle_unref (shader);
  cogl_handle_unref (program);

  return pipeline;
}
static void
set_shader_num (int new_no)
{
  CoglHandle shader;
  CoglHandle program;
  int image_width = cogl_texture_get_width (redhand);
  int image_height = cogl_texture_get_height (redhand);
  float param0[4];
  int uniform_no;

  g_print ("setting shaders[%i] named '%s'\n",
           new_no,
           shaders[new_no].name);

  shader = cogl_create_shader (COGL_SHADER_TYPE_FRAGMENT);
  cogl_shader_source (shader, shaders[new_no].source);
  cogl_shader_compile (shader);

  program = cogl_create_program ();
  cogl_program_attach_shader (program, shader);
  cogl_handle_unref (shader);
  cogl_program_link (program);

  param0[0] = 1.0f/image_width; /* texel x step delta */
  param0[1] = 1.0f/image_height; /* texel y step delta */
  param0[2] = 0.4; /* brightness */
  param0[3] = -1.9; /* contrast */

  uniform_no = cogl_program_get_uniform_location (program, "program.local[0]");
  cogl_program_set_uniform_float (program, uniform_no, 4, 1, param0);

  cogl_material_set_user_program (material, program);
  cogl_handle_unref (program);

  shader_no = new_no;
}
Example #6
0
static gboolean
clutter_colorize_effect_pre_paint (ClutterEffect *effect)
{
  ClutterColorizeEffect *self = CLUTTER_COLORIZE_EFFECT (effect);
  ClutterEffectClass *parent_class;

  if (!clutter_actor_meta_get_enabled (CLUTTER_ACTOR_META (effect)))
    return FALSE;

  self->actor = clutter_actor_meta_get_actor (CLUTTER_ACTOR_META (effect));
  if (self->actor == NULL)
    return FALSE;

  if (!clutter_feature_available (CLUTTER_FEATURE_SHADERS_GLSL))
    {
      /* if we don't have support for GLSL shaders then we
       * forcibly disable the ActorMeta
       */
      g_warning ("Unable to use the ShaderEffect: the graphics hardware "
                 "or the current GL driver does not implement support "
                 "for the GLSL shading language.");
      clutter_actor_meta_set_enabled (CLUTTER_ACTOR_META (effect), FALSE);
      return FALSE;
    }

  if (self->shader == COGL_INVALID_HANDLE)
    {
      self->shader = cogl_create_shader (COGL_SHADER_TYPE_FRAGMENT);
      cogl_shader_source (self->shader, colorize_glsl_shader);

      self->is_compiled = FALSE;
      self->tex_uniform = -1;
      self->tint_uniform = -1;
    }

  if (self->program == COGL_INVALID_HANDLE)
    self->program = cogl_create_program ();

  if (!self->is_compiled)
    {
      g_assert (self->shader != COGL_INVALID_HANDLE);
      g_assert (self->program != COGL_INVALID_HANDLE);

      cogl_shader_compile (self->shader);
      if (!cogl_shader_is_compiled (self->shader))
        {
          gchar *log_buf = cogl_shader_get_info_log (self->shader);

          g_warning (G_STRLOC ": Unable to compile the colorize shader: %s",
                     log_buf);
          g_free (log_buf);

          cogl_handle_unref (self->shader);
          cogl_handle_unref (self->program);

          self->shader = COGL_INVALID_HANDLE;
          self->program = COGL_INVALID_HANDLE;
        }
      else
        {
          cogl_program_attach_shader (self->program, self->shader);
          cogl_program_link (self->program);

          cogl_handle_unref (self->shader);

          self->is_compiled = TRUE;

          self->tex_uniform =
            cogl_program_get_uniform_location (self->program, "tex");
          self->tint_uniform =
            cogl_program_get_uniform_location (self->program, "tint");
        }
    }

  parent_class = CLUTTER_EFFECT_CLASS (clutter_colorize_effect_parent_class);
  return parent_class->pre_paint (effect);
}
static void rc_set_shader(CoglHandle material, const RendererClutterShaderInfo *shaderInfo)
{
	CoglHandle shader;
	CoglHandle program;
	gint uniform_no;

	shader = cogl_create_shader (COGL_SHADER_TYPE_FRAGMENT);
	cogl_shader_source (shader,
	"vec3 checker(vec2 texc, vec3 color0, vec3 color1)						\n"
	"{																		\n"
	"  if (mod(floor(texc.x) + floor(texc.y), 2.0) == 0.0)					\n"
	"    return color0;														\n"
	"  else																	\n"
	"    return color1;														\n"
	"}																		\n"
	"																		\n"
	"uniform sampler2D tex;													\n"
	"uniform sampler3D clut;												\n"
	"uniform float scale;													\n"
	"uniform float offset;													\n"
	"uniform float checkersize;												\n"
	"uniform vec3 color0;													\n"
	"uniform vec3 color1;													\n"
	"																		\n"
	"void main(void)														\n"
	"{																		\n"
	"    vec3 bg = checker(gl_FragCoord.xy / checkersize, color0, color1);	\n"
	"    vec4 img4 = texture2D(tex, gl_TexCoord[0].xy);						\n"
	"    vec3 img3 = img4.bgr;												\n"
	"    img3 = img3 * scale + offset;										\n"
	"    img3 = texture3D(clut, img3).rgb;									\n"
	"																		\n"
	"    gl_FragColor = vec4(img3 * img4.a + bg * (1.0 - img4.a), 1.0);		\n"
	"}																		\n"
	);
	cogl_shader_compile(shader);
	gchar *err = cogl_shader_get_info_log(shader);
	DEBUG_3("%s\n",err);
	g_free(err);

	program = cogl_create_program ();
	cogl_program_attach_shader (program, shader);
	cogl_handle_unref (shader);
	cogl_program_link (program);

	uniform_no = cogl_program_get_uniform_location (program, "tex");
	cogl_program_set_uniform_1i (program, uniform_no, 0);

	uniform_no = cogl_program_get_uniform_location (program, "clut");
	cogl_program_set_uniform_1i (program, uniform_no, 1);

	uniform_no = cogl_program_get_uniform_location (program, "scale");
	cogl_program_set_uniform_1f (program, uniform_no, (double) (CLUT_SIZE - 1) / CLUT_SIZE);

	uniform_no = cogl_program_get_uniform_location (program, "offset");
	cogl_program_set_uniform_1f (program, uniform_no, 1.0 / (2 * CLUT_SIZE));

	uniform_no = cogl_program_get_uniform_location (program, "checkersize");
	cogl_program_set_uniform_1f (program, uniform_no, shaderInfo->checkersize);

	uniform_no = cogl_program_get_uniform_location (program, "color0");
	cogl_program_set_uniform_float (program, uniform_no, 3, 1, shaderInfo->checkercolor0);

	uniform_no = cogl_program_get_uniform_location (program, "color1");
	cogl_program_set_uniform_float (program, uniform_no, 3, 1, shaderInfo->checkercolor1);

	cogl_material_set_user_program (material, program);
	cogl_handle_unref (program);
}
static void
paint (TestState *state)
{
  CoglPipeline *pipeline = cogl_pipeline_new (test_ctx);
  CoglTexture *tex;
  CoglColor color;
  CoglError *error = NULL;
  CoglHandle shader, program;

  cogl_color_init_from_4ub (&color, 0, 0, 0, 255);
  cogl_clear (&color, COGL_BUFFER_BIT_COLOR);

  /* Set the primary vertex color as red */
  cogl_color_set_from_4ub (&color, 0xff, 0x00, 0x00, 0xff);
  cogl_pipeline_set_color (pipeline, &color);

  /* Override the vertex color in the texture environment with a
     constant green color provided by a texture */
  tex = create_dummy_texture ();
  cogl_pipeline_set_layer_texture (pipeline, 0, tex);
  cogl_object_unref (tex);
  if (!cogl_pipeline_set_layer_combine (pipeline, 0,
                                        "RGBA=REPLACE(TEXTURE)",
                                        &error))
    {
      g_warning ("Error setting layer combine: %s", error->message);
      g_assert_not_reached ();
    }

  /* Set up a dummy vertex shader that does nothing but the usual
     fixed function transform */
  shader = cogl_create_shader (COGL_SHADER_TYPE_VERTEX);
  cogl_shader_source (shader,
                      "void\n"
                      "main ()\n"
                      "{\n"
                      "  cogl_position_out = "
                      "cogl_modelview_projection_matrix * "
                      "cogl_position_in;\n"
                      "  cogl_color_out = cogl_color_in;\n"
                      "  cogl_tex_coord_out[0] = cogl_tex_coord_in;\n"
                      "}\n");
  cogl_shader_compile (shader);
  if (!cogl_shader_is_compiled (shader))
    {
      char *log = cogl_shader_get_info_log (shader);
      g_warning ("Shader compilation failed:\n%s", log);
      g_free (log);
      g_assert_not_reached ();
    }

  program = cogl_create_program ();
  cogl_program_attach_shader (program, shader);
  cogl_program_link (program);

  cogl_handle_unref (shader);

  /* Draw something without the program */
  cogl_set_source (pipeline);
  cogl_rectangle (0, 0, 50, 50);

  /* Draw it again using the program. It should look exactly the same */
  cogl_pipeline_set_user_program (pipeline, program);
  cogl_handle_unref (program);

  cogl_rectangle (50, 0, 100, 50);
  cogl_pipeline_set_user_program (pipeline, COGL_INVALID_HANDLE);

  cogl_object_unref (pipeline);
}
static gboolean
cd_icc_effect_pre_paint (ClutterEffect *effect)
{
  CdIccEffect *self = CD_ICC_EFFECT (effect);
  ClutterEffectClass *parent_class;
  ClutterActorBox allocation;
  gfloat width, height;

  if (!clutter_actor_meta_get_enabled (CLUTTER_ACTOR_META (effect)))
    return FALSE;

  self->actor = clutter_actor_meta_get_actor (CLUTTER_ACTOR_META (effect));
  if (self->actor == NULL)
    return FALSE;

  if (!clutter_feature_available (CLUTTER_FEATURE_SHADERS_GLSL))
    {
      /* if we don't have support for GLSL shaders then we
       * forcibly disable the ActorMeta
       */
      g_warning ("Unable to use the ShaderEffect: the graphics hardware "
                 "or the current GL driver does not implement support "
                 "for the GLSL shading language.");
      clutter_actor_meta_set_enabled (CLUTTER_ACTOR_META (effect), FALSE);
      return FALSE;
    }

  clutter_actor_get_allocation_box (self->actor, &allocation);
  clutter_actor_box_get_size (&allocation, &width, &height);

  if (self->shader == COGL_INVALID_HANDLE)
    {
      self->shader = cogl_create_shader (COGL_SHADER_TYPE_FRAGMENT);
      cogl_shader_source (self->shader, glsl_shader);

      self->is_compiled = FALSE;
      self->main_texture_uniform = -1;
      self->indirect_texture_uniform = -1;
      self->color_data1_uniform = -1;
      self->color_data2_uniform = -1;
    }

  if (self->program == COGL_INVALID_HANDLE)
    self->program = cogl_create_program ();

  if (!self->is_compiled)
    {
      g_assert (self->shader != COGL_INVALID_HANDLE);
      g_assert (self->program != COGL_INVALID_HANDLE);

      cogl_shader_compile (self->shader);
      if (!cogl_shader_is_compiled (self->shader))
        {
          gchar *log_buf = cogl_shader_get_info_log (self->shader);

          g_warning (G_STRLOC ": Unable to compile the icc shader: %s",
                     log_buf);
          g_free (log_buf);

          cogl_handle_unref (self->shader);
          cogl_handle_unref (self->program);

          self->shader = COGL_INVALID_HANDLE;
          self->program = COGL_INVALID_HANDLE;
        }
      else
        {
          cogl_program_attach_shader (self->program, self->shader);
          cogl_program_link (self->program);

          cogl_handle_unref (self->shader);

          self->is_compiled = TRUE;

          self->main_texture_uniform =
            cogl_program_get_uniform_location (self->program, "main_texture");
          self->indirect_texture_uniform =
            cogl_program_get_uniform_location (self->program, "indirect_texture");
          self->color_data1_uniform =
            cogl_program_get_uniform_location (self->program, "color_data1");
          self->color_data2_uniform =
            cogl_program_get_uniform_location (self->program, "color_data2");
        }
    }

  parent_class = CLUTTER_EFFECT_CLASS (cd_icc_effect_parent_class);
  return parent_class->pre_paint (effect);
}